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.potential.commands;
39  
40  import ffx.numerics.Potential;
41  import ffx.potential.ForceFieldEnergy;
42  import ffx.potential.cli.PotentialCommand;
43  import ffx.potential.cli.TimerOptions;
44  import ffx.utilities.FFXBinding;
45  import picocli.CommandLine.Command;
46  import picocli.CommandLine.Mixin;
47  import picocli.CommandLine.Parameters;
48  
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.List;
52  
53  import static java.lang.String.format;
54  import static org.apache.commons.math3.util.FastMath.sqrt;
55  
56  /**
57   * The Timer command evaluates the wall clock time for energy and forces.
58   *
59   * Usage:
60   *   ffxc Timer [options] <filename>
61   */
62  @Command(name = "Timer", description = " Time energy evaluations.")
63  public class Timer extends PotentialCommand {
64  
65    /** Mix in timing options. */
66    @Mixin
67    private TimerOptions timer = new TimerOptions();
68  
69    /** The final argument is an atomic coordinate file in PDB or XYZ format. */
70    @Parameters(arity = "1", paramLabel = "file",
71        description = "The atomic coordinate file in PDB or XYZ format.")
72    private String filename = null;
73  
74    private ForceFieldEnergy energy = null;
75  
76    public Timer() { super(); }
77    public Timer(FFXBinding binding) { super(binding); }
78    public Timer(String[] args) { super(args); }
79  
80    @Override
81    public Timer run() {
82      // Init the context and bind variables.
83      if (!init()) {
84        return this;
85      }
86  
87      // Set the number of threads.
88      if (timer.getThreads() > 0) {
89        System.setProperty("pj.nt", Integer.toString(timer.getThreads()));
90      }
91  
92      // Load the MolecularAssembly.
93      activeAssembly = getActiveAssembly(filename);
94      if (activeAssembly == null) {
95        logger.info(helpString());
96        return this;
97      }
98  
99      // Set the filename.
100     filename = activeAssembly.getFile().getAbsolutePath();
101 
102     if (timer.getNoGradient()) {
103       logger.info("\n Timing energy for " + filename);
104     } else {
105       logger.info("\n Timing energy and gradient for " + filename);
106     }
107 
108     int nEvals = timer.getIterations(); // The number of iterations.
109 
110     energy = activeAssembly.getPotentialEnergy();
111 
112     long minTime = Long.MAX_VALUE;
113     double sumTime2 = 0.0;
114     int halfnEvals = (nEvals % 2 == 1) ? (nEvals / 2) : (nEvals / 2) - 1; // Halfway point
115     for (int i = 0; i < nEvals; i++) {
116       long time = -System.nanoTime();
117       double e = energy.energy(!timer.getNoGradient(), timer.getVerbose());
118       time += System.nanoTime();
119       if (!timer.getVerbose()) {
120         logger.info(format(" %4d Energy %16.8f in %6.3f (sec)", i, e, time * 1.0E-9));
121       }
122       minTime = Math.min(time, minTime);
123       if (i >= (nEvals / 2)) {
124         double time2 = time * 1.0E-9;
125         sumTime2 += (time2 * time2);
126       }
127     }
128 
129     ++halfnEvals;
130     double rmsTime = sqrt(sumTime2 / halfnEvals);
131     logger.info(format("\n Minimum time:           %6.3f (sec)", minTime * 1.0E-9));
132     logger.info(format(" RMS time (latter half): %6.3f (sec)", rmsTime));
133 
134     return this;
135   }
136 
137   @Override
138   public List<Potential> getPotentials() {
139     if (energy == null) {
140       return Collections.emptyList();
141     } else {
142       List<Potential> list = new ArrayList<>(1);
143       list.add(energy);
144       return list;
145     }
146   }
147 }