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.xray.commands;
39
40 import ffx.algorithms.cli.AlgorithmsCommand;
41 import ffx.numerics.Potential;
42 import ffx.potential.MolecularAssembly;
43 import ffx.potential.cli.AtomSelectionOptions;
44 import ffx.potential.cli.TimerOptions;
45 import ffx.utilities.FFXBinding;
46 import ffx.xray.DiffractionData;
47 import ffx.xray.RefinementEnergy;
48 import ffx.xray.cli.XrayOptions;
49 import org.apache.commons.configuration2.CompositeConfiguration;
50 import picocli.CommandLine.Command;
51 import picocli.CommandLine.Mixin;
52 import picocli.CommandLine.Parameters;
53
54 import java.util.List;
55
56 import static java.lang.String.format;
57
58
59
60
61
62
63
64
65 @Command(description = " Time calculation of the X-ray target.", name = "xray.Timer")
66 public class Timer extends AlgorithmsCommand {
67
68 @Mixin
69 private TimerOptions timerOptions;
70
71 @Mixin
72 private XrayOptions xrayOptions;
73
74 @Mixin
75 AtomSelectionOptions atomSelectionOptions;
76
77
78
79
80 @Parameters(arity = "1..*", paramLabel = "files", description = "PDB and Diffraction input files.")
81 private List<String> filenames;
82
83 private DiffractionData diffractionData;
84 private MolecularAssembly[] molecularAssemblies;
85
86 private RefinementEnergy refinementEnergy;
87
88
89
90
91 public Timer() {
92 super();
93 }
94
95
96
97
98
99
100 public Timer(String[] args) {
101 super(args);
102 }
103
104
105
106
107
108
109 public Timer(FFXBinding binding) {
110 super(binding);
111 }
112
113
114
115
116 @Override
117 public Timer run() {
118
119 if (!init()) {
120 return this;
121 }
122
123 xrayOptions.init();
124
125
126 if (timerOptions.getThreads() > 0) {
127 System.setProperty("pj.nt", Integer.toString(timerOptions.getThreads()));
128 }
129
130 String filename;
131 if (filenames != null && !filenames.isEmpty()) {
132
133 molecularAssemblies = algorithmFunctions.openAll(filenames.get(0));
134 activeAssembly = molecularAssemblies[0];
135 filename = filenames.get(0);
136 } else if (activeAssembly == null) {
137 logger.info(helpString());
138 return this;
139 } else {
140 molecularAssemblies = new MolecularAssembly[]{activeAssembly};
141 filename = activeAssembly.getFile().getAbsolutePath();
142 }
143
144 logger.info("\n Running xray.Timer on " + filename);
145
146
147 for (MolecularAssembly molecularAssembly : molecularAssemblies) {
148 atomSelectionOptions.setActiveAtoms(molecularAssembly);
149 }
150
151
152 CompositeConfiguration properties = activeAssembly.getProperties();
153 xrayOptions.setProperties(parseResult, properties);
154
155
156 diffractionData = xrayOptions.getDiffractionData(filenames, molecularAssemblies, properties);
157 refinementEnergy = xrayOptions.toXrayEnergy(diffractionData);
158
159
160 algorithmFunctions.energy(molecularAssemblies);
161
162 int n = refinementEnergy.getNumberOfVariables();
163 double[] x = new double[n];
164 double[] g = new double[n];
165 refinementEnergy.getCoordinates(x);
166 Potential energy = refinementEnergy.getDataEnergy();
167
168 logger.info("\n Beginning Timings\n");
169
170 int nEvals = timerOptions.getIterations();
171 boolean gradient = !timerOptions.getNoGradient();
172 long minTime = Long.MAX_VALUE;
173 double sumTime2 = 0.0;
174 int halfnEvals = (nEvals % 2 == 1) ? (nEvals / 2) : (nEvals / 2) - 1;
175 for (int i = 0; i < nEvals; i++) {
176 long time = -System.nanoTime();
177 double e;
178 if (gradient) {
179 e = energy.energyAndGradient(x, g);
180 } else {
181 e = energy.energy(x);
182 }
183 time += System.nanoTime();
184 if (gradient) {
185 logger.info(format(" Energy & Gradient: %12.6f in %6.3f (sec)", e, time * 1.0E-9));
186 } else {
187 logger.info(format(" Energy: %12.6f in %6.3f (sec)", e, time * 1.0E-9));
188 }
189
190 minTime = time < minTime ? time : minTime;
191 if (i >= nEvals / 2) {
192 double time2 = time * 1.0E-9;
193 sumTime2 += (time2 * time2);
194 }
195 }
196
197 ++halfnEvals;
198 double rmsTime = Math.sqrt(sumTime2 / halfnEvals);
199 logger.info(format("\n Minimum time: %6.3f (sec)", minTime * 1.0E-9));
200 logger.info(format(" RMS time (latter half): %6.3f (sec)", rmsTime));
201
202 return this;
203 }
204
205 @Override
206 public List<Potential> getPotentials() {
207 return getPotentialsFromAssemblies(molecularAssemblies);
208 }
209
210 @Override
211 public boolean destroyPotentials() {
212 return diffractionData == null ? true : diffractionData.destroy();
213 }
214 }