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.cli;
39
40 import ffx.algorithms.AlgorithmFunctions;
41 import ffx.algorithms.AlgorithmListener;
42 import ffx.algorithms.AlgorithmUtils;
43 import ffx.crystal.Crystal;
44 import ffx.numerics.Potential;
45 import ffx.potential.MolecularAssembly;
46 import ffx.utilities.FFXCommand;
47 import ffx.utilities.FFXBinding;
48 import org.apache.commons.lang3.Strings;
49
50 import javax.annotation.Nullable;
51 import java.io.File;
52 import java.util.ArrayList;
53 import java.util.List;
54
55 import static java.lang.String.format;
56
57
58
59
60
61
62
63 public class AlgorithmsCommand extends FFXCommand {
64
65
66
67
68 public AlgorithmFunctions algorithmFunctions;
69
70
71
72
73
74 public MolecularAssembly activeAssembly;
75
76
77
78
79 public AlgorithmListener algorithmListener;
80
81
82
83
84 protected File baseDir;
85
86 public AlgorithmsCommand() {
87 super();
88 }
89
90 public AlgorithmsCommand(FFXBinding binding) {
91 super(binding);
92 }
93
94
95
96
97
98
99 public AlgorithmsCommand(String[] args) {
100 super(args);
101 }
102
103
104
105
106
107
108 public boolean destroyPotentials() {
109 boolean allSucceeded = true;
110 for (Potential potent : getPotentials()) {
111 logger.fine(format(" Potential %s is being destroyed. ", potent));
112 allSucceeded = allSucceeded && potent.destroy();
113 }
114 return allSucceeded;
115 }
116
117
118
119
120
121
122 public List<Potential> getPotentials() {
123 List<Potential> plist = new ArrayList<>();
124 if (activeAssembly != null && activeAssembly.getPotentialEnergy() != null) {
125 plist.add(activeAssembly.getPotentialEnergy());
126 }
127 return plist;
128 }
129
130
131
132
133
134
135 @Override
136 public boolean init() {
137 if (!super.init()) {
138 return false;
139 }
140
141 if (binding.hasVariable("functions")) {
142 algorithmFunctions = (AlgorithmFunctions) binding.getVariable("functions");
143 } else {
144 algorithmFunctions = new AlgorithmUtils();
145 binding.setVariable("functions", algorithmFunctions);
146 }
147
148 activeAssembly = null;
149 if (binding.hasVariable("active")) {
150 activeAssembly = (MolecularAssembly) binding.getVariable("active");
151 }
152
153 algorithmListener = null;
154 if (binding.hasVariable("listener")) {
155 algorithmListener = (AlgorithmListener) binding.getVariable("listener");
156 }
157
158 if (binding.hasVariable("baseDir")) {
159 baseDir = (File) binding.getVariable("baseDir");
160 }
161
162 return true;
163 }
164
165
166
167
168
169
170 public void setBaseDir(File baseDir) {
171 this.baseDir = baseDir;
172 }
173
174
175
176
177
178
179
180
181
182
183 protected File saveDirFile(File file) {
184 if (baseDir == null || !baseDir.exists() || !baseDir.isDirectory() || !baseDir.canWrite()) {
185 return file;
186 } else {
187 String baseName = file.getName();
188 String newName = baseDir.getAbsolutePath() + File.separator + baseName;
189 return new File(newName);
190 }
191 }
192
193
194
195
196
197
198
199
200 public MolecularAssembly getActiveAssembly(@Nullable String filename) {
201 if (filename != null) {
202
203 MolecularAssembly[] assemblies = {algorithmFunctions.open(filename)};
204 activeAssembly = assemblies[0];
205 }
206 return activeAssembly;
207 }
208
209
210
211
212
213
214
215
216 public MolecularAssembly[] getActiveAssemblies(@Nullable String filename) {
217 MolecularAssembly[] assemblies;
218 if (filename != null) {
219
220 assemblies = algorithmFunctions.openAll(filename);
221 activeAssembly = assemblies[0];
222 return assemblies;
223 } else {
224 assemblies = new MolecularAssembly[]{activeAssembly};
225 }
226 return assemblies;
227 }
228
229
230
231
232
233
234
235
236 public void setActiveAssembly(MolecularAssembly molecularAssembly) {
237 activeAssembly = molecularAssembly;
238 }
239
240
241
242
243
244
245 public void updateTitle(double energy) {
246
247 String oldName = activeAssembly.getName();
248 Crystal crystal = activeAssembly.getCrystal();
249 if (crystal != null && !crystal.aperiodic()) {
250 double density = crystal.getDensity(activeAssembly.getMass());
251 if (Strings.CI.contains(oldName, "Energy:")
252 || Strings.CI.contains(oldName, "Density:")) {
253 String[] tokens = oldName.trim().split(" +");
254 int numTokens = tokens.length;
255
256 StringBuilder sb = new StringBuilder();
257 for (int i = 0; i < numTokens; i++) {
258 if (Strings.CI.contains(tokens[i], "Energy:")) {
259
260 tokens[i++] = Double.toString(energy);
261 } else if (Strings.CI.contains(tokens[i], "Density:")) {
262
263 tokens[i++] = Double.toString(density);
264 } else {
265
266 sb.append(tokens[i]).append(" ");
267 }
268 }
269
270 activeAssembly.setName(format("%s Energy: %9.4f Density: %9.4f",
271 sb, energy, density));
272 } else {
273
274 activeAssembly.setName(format("%s Energy: %9.4f Density: %9.4f",
275 oldName, energy, density));
276 }
277 } else {
278 if (Strings.CI.contains(oldName, "Energy:")) {
279 String[] tokens = oldName.trim().split(" +");
280 int numTokens = tokens.length;
281
282 StringBuilder sb = new StringBuilder();
283 for (int i = 0; i < numTokens; i++) {
284 if (Strings.CI.contains(tokens[i], "Energy:")) {
285
286 tokens[i++] = Double.toString(energy);
287 } else {
288
289 sb.append(tokens[i]).append(" ");
290 }
291 }
292
293 activeAssembly.setName(format("%s Energy: %9.4f", sb, energy));
294 } else {
295
296 activeAssembly.setName(format("%s Energy: %9.4f", oldName, energy));
297 }
298 }
299 }
300 }