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