View Javadoc
1   //******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2025.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
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   * The X-ray Timer script.
60   * <br>
61   * Usage:
62   * <br>
63   * ffxc xray.Timer [options] &lt;filename&gt;
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     * One or more filenames.
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     * Timer constructor.
84     */
85    public Timer() {
86      super();
87    }
88  
89    /**
90     * Timer constructor that sets the command line arguments.
91     *
92     * @param args Command line arguments.
93     */
94    public Timer(String[] args) {
95      super(args);
96    }
97  
98    /**
99     * Timer constructor.
100    *
101    * @param binding The Binding to use.
102    */
103   public Timer(FFXBinding binding) {
104     super(binding);
105   }
106 
107   /**
108    * {@inheritDoc}
109    */
110   @Override
111   public Timer run() {
112 
113     if (!init()) {
114       return this;
115     }
116 
117     xrayOptions.init();
118 
119     // Set the number of threads (needs to be done before opening the files).
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     // Combine script flags (in parseResult) with properties.
141     CompositeConfiguration properties = activeAssembly.getProperties();
142     xrayOptions.setProperties(parseResult, properties);
143 
144     // Set up diffraction data (can be multiple files)
145     DiffractionData diffractionData = xrayOptions.getDiffractionData(filenames, molecularAssemblies, properties);
146     refinementEnergy = xrayOptions.toXrayEnergy(diffractionData);
147 
148     // Print the initial energy of each conformer.
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; // Halfway point
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    * {@inheritDoc}
188    */
189   @Override
190   public List<Potential> getPotentials() {
191     return refinementEnergy == null ? Collections.emptyList() :
192         Collections.singletonList(refinementEnergy);
193   }
194 }