1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package ffx.algorithms.commands;
39
40 import ffx.algorithms.cli.AlgorithmsCommand;
41 import ffx.algorithms.cli.AnnealOptions;
42 import ffx.algorithms.cli.DynamicsOptions;
43 import ffx.algorithms.optimize.anneal.SimulatedAnnealing;
44 import ffx.numerics.Potential;
45 import ffx.potential.cli.AtomSelectionOptions;
46 import ffx.potential.cli.WriteoutOptions;
47 import ffx.utilities.FFXBinding;
48 import org.apache.commons.io.FilenameUtils;
49 import picocli.CommandLine.Command;
50 import picocli.CommandLine.Mixin;
51 import picocli.CommandLine.Parameters;
52
53 import java.io.File;
54 import java.util.Collections;
55 import java.util.List;
56
57 import static java.lang.String.format;
58
59
60
61
62
63
64
65
66 @Command(description = " Run simulated annealing on a system.", name = "Anneal")
67 public class Anneal extends AlgorithmsCommand {
68
69 @Mixin
70 private AtomSelectionOptions atomSelectionOptions;
71
72 @Mixin
73 private DynamicsOptions dynamics;
74
75 @Mixin
76 private AnnealOptions anneal;
77
78 @Mixin
79 private WriteoutOptions writeOut;
80
81
82
83
84 @Parameters(arity = "1", paramLabel = "file",
85 description = "XYZ or PDB input file.")
86 private String filename;
87
88 private SimulatedAnnealing simulatedAnnealing = null;
89
90 private Potential potential;
91
92
93
94
95 public Anneal() {
96 super();
97 }
98
99
100
101
102
103
104 public Anneal(FFXBinding binding) {
105 super(binding);
106 }
107
108
109
110
111
112
113 public Anneal(String[] args) {
114 super(args);
115 }
116
117
118
119
120 @Override
121 public Anneal run() {
122
123
124 if (!init()) {
125 return this;
126 }
127
128
129 dynamics.init();
130
131
132 activeAssembly = getActiveAssembly(filename);
133 if (activeAssembly == null) {
134 logger.info(helpString());
135 return this;
136 }
137
138
139 filename = activeAssembly.getFile().getAbsolutePath();
140
141
142 atomSelectionOptions.setActiveAtoms(activeAssembly);
143
144 logger.info("\n Running simulated annealing on " + filename + "\n");
145
146
147 File dyn = new File(FilenameUtils.removeExtension(filename) + ".dyn");
148 if (!dyn.exists()) {
149 dyn = null;
150 }
151
152 potential = activeAssembly.getPotentialEnergy();
153
154 simulatedAnnealing = anneal.createAnnealer(dynamics, activeAssembly,
155 activeAssembly.getPotentialEnergy(), algorithmListener, dyn);
156
157 simulatedAnnealing.setPrintInterval(dynamics.getReport());
158 simulatedAnnealing.setSaveFrequency(dynamics.getWrite());
159 simulatedAnnealing.setRestartFrequency(dynamics.getCheckpoint());
160 simulatedAnnealing.setTrajectorySteps(dynamics.getTrajSteps());
161
162 simulatedAnnealing.anneal();
163
164 if (baseDir == null || !baseDir.exists() || !baseDir.isDirectory() || !baseDir.canWrite()) {
165 baseDir = new File(FilenameUtils.getFullPath(filename));
166 }
167
168 String dirName = baseDir.toString() + File.separator;
169 String fileName = FilenameUtils.getName(filename);
170 fileName = FilenameUtils.removeExtension(fileName);
171
172 writeOut.saveFile(format("%s%s", dirName, fileName), algorithmFunctions, activeAssembly);
173
174 return this;
175 }
176
177
178
179
180
181
182 public SimulatedAnnealing getAnnealing() {
183 return simulatedAnnealing;
184 }
185
186
187
188
189 @Override
190 public List<Potential> getPotentials() {
191 List<Potential> potentials;
192 if (potential == null) {
193 potentials = Collections.emptyList();
194 } else {
195 potentials = Collections.singletonList(potential);
196 }
197 return potentials;
198 }
199
200
201
202
203 @Override
204 public boolean destroyPotentials() {
205 return getPotentials().stream().allMatch(Potential::destroy);
206 }
207 }