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.TimerOptions;
44 import ffx.utilities.FFXBinding;
45 import ffx.xray.DiffractionData;
46 import ffx.xray.RefinementEnergy;
47 import ffx.xray.cli.XrayOptions;
48 import org.apache.commons.configuration2.CompositeConfiguration;
49 import picocli.CommandLine.Command;
50 import picocli.CommandLine.Mixin;
51 import picocli.CommandLine.Parameters;
52
53 import java.util.Collections;
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
75
76
77 @Parameters(arity = "1..*", paramLabel = "files", description = "PDB and Diffraction input files.")
78 private List<String> filenames;
79
80 private RefinementEnergy refinementEnergy;
81
82
83
84
85 public Timer() {
86 super();
87 }
88
89
90
91
92
93
94 public Timer(String[] args) {
95 super(args);
96 }
97
98
99
100
101
102
103 public Timer(FFXBinding binding) {
104 super(binding);
105 }
106
107
108
109
110 @Override
111 public Timer run() {
112
113 if (!init()) {
114 return this;
115 }
116
117 xrayOptions.init();
118
119
120 if (timerOptions.getThreads() > 0) {
121 System.setProperty("pj.nt", Integer.toString(timerOptions.getThreads()));
122 }
123
124 String filename;
125 MolecularAssembly[] molecularAssemblies;
126 if (filenames != null && filenames.size() > 0) {
127 molecularAssemblies = algorithmFunctions.openAll(filenames.get(0));
128 activeAssembly = molecularAssemblies[0];
129 filename = filenames.get(0);
130 } else if (activeAssembly == null) {
131 logger.info(helpString());
132 return this;
133 } else {
134 molecularAssemblies = new MolecularAssembly[]{activeAssembly};
135 filename = activeAssembly.getFile().getAbsolutePath();
136 }
137
138 logger.info("\n Running xray.Timer on " + filename);
139
140
141 CompositeConfiguration properties = activeAssembly.getProperties();
142 xrayOptions.setProperties(parseResult, properties);
143
144
145 DiffractionData diffractionData = xrayOptions.getDiffractionData(filenames, molecularAssemblies, properties);
146 refinementEnergy = xrayOptions.toXrayEnergy(diffractionData);
147
148
149 algorithmFunctions.energy(molecularAssemblies);
150
151 int n = refinementEnergy.getNumberOfVariables();
152 double[] x = new double[n];
153 double[] g = new double[n];
154 refinementEnergy.getCoordinates(x);
155 Potential energy = refinementEnergy.getDataEnergy();
156
157 int nEvals = timerOptions.getIterations();
158 long minTime = Long.MAX_VALUE;
159 double sumTime2 = 0.0;
160 int halfnEvals = (nEvals % 2 == 1) ? (nEvals / 2) : (nEvals / 2) - 1;
161 for (int i = 0; i < nEvals; i++) {
162 long time = -System.nanoTime();
163 double e;
164 if (timerOptions.getNoGradient()) {
165 e = energy.energy(x);
166 } else {
167 e = energy.energyAndGradient(x, g);
168 }
169 time += System.nanoTime();
170 logger.info(format(" Target energy %16.8f in %6.3f (sec)", e, time * 1.0E-9));
171 minTime = time < minTime ? time : minTime;
172 if (i >= nEvals / 2) {
173 double time2 = time * 1.0E-9;
174 sumTime2 += (time2 * time2);
175 }
176 }
177
178 ++halfnEvals;
179 double rmsTime = Math.sqrt(sumTime2 / halfnEvals);
180 logger.info(format("\n Minimum time: %6.3f (sec)", minTime * 1.0E-9));
181 logger.info(format(" RMS time (latter half): %6.3f (sec)", rmsTime));
182
183 return this;
184 }
185
186
187
188
189 @Override
190 public List<Potential> getPotentials() {
191 return refinementEnergy == null ? Collections.emptyList() :
192 Collections.singletonList(refinementEnergy);
193 }
194 }