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-2026.
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.algorithms.commands;
39  
40  import edu.rit.pj.Comm;
41  import ffx.algorithms.cli.AlgorithmsCommand;
42  import ffx.algorithms.cli.BarostatOptions;
43  import ffx.algorithms.cli.DynamicsOptions;
44  import ffx.algorithms.cli.RepExOptions;
45  import ffx.algorithms.dynamics.Barostat;
46  import ffx.algorithms.dynamics.MolecularDynamics;
47  import ffx.algorithms.dynamics.ReplicaExchange;
48  import ffx.crystal.CrystalPotential;
49  import ffx.numerics.Potential;
50  import ffx.potential.cli.AtomSelectionOptions;
51  import ffx.potential.cli.WriteoutOptions;
52  import ffx.utilities.FFXBinding;
53  import org.apache.commons.io.FilenameUtils;
54  import picocli.CommandLine.Command;
55  import picocli.CommandLine.Mixin;
56  import picocli.CommandLine.Parameters;
57  
58  import java.io.File;
59  import java.util.Collections;
60  import java.util.List;
61  
62  /**
63   * The Dynamics script implements molecular and stochastic dynamics algorithms.
64   * <br>
65   * Usage:
66   * <br>
67   * ffxc Dynamics [options] &lt;filename&gt; [file2...]
68   */
69  @Command(description = " Run dynamics on a system.", name = "Dynamics")
70  public class Dynamics extends AlgorithmsCommand {
71  
72    @Mixin
73    private AtomSelectionOptions atomSelectionOptions;
74  
75    @Mixin
76    private DynamicsOptions dynamicsOptions;
77  
78    @Mixin
79    private BarostatOptions barostatOptions;
80  
81    @Mixin
82    private WriteoutOptions writeOut;
83  
84    @Mixin
85    private RepExOptions repEx;
86  
87    /**
88     * A PDB or XYZ filename.
89     */
90    @Parameters(arity = "1", paramLabel = "file",
91        description = "XYZ or PDB input file.")
92    private String filename;
93  
94    /**
95     * Creation of a public field to try and make the JUnit test work, original code does not declare this as a public field.
96     * Originally it is declared in the run method.
97     */
98    public Potential potential = null;
99    public MolecularDynamics molDyn = null;
100 
101   public MolecularDynamics getMolecularDynamics() {
102     return molDyn;
103   }
104 
105   public Potential getPotentialObject() {
106     return potential;
107   }
108 
109   /**
110    * Dynamics Constructor.
111    */
112   public Dynamics() {
113     super();
114   }
115 
116   /**
117    * Dynamics Constructor.
118    *
119    * @param binding The Binding to use.
120    */
121   public Dynamics(FFXBinding binding) {
122     super(binding);
123   }
124 
125   /**
126    * Dynamics constructor that sets the command line arguments.
127    *
128    * @param args Command line arguments.
129    */
130   public Dynamics(String[] args) {
131     super(args);
132   }
133 
134   /**
135    * {@inheritDoc}
136    */
137   @Override
138   public Dynamics run() {
139 
140     // Init the context and bind variables.
141     if (!init()) {
142       return this;
143     }
144 
145     // Init DynamicsOptions (e.g., the thermostat and barostat flags).
146     dynamicsOptions.init();
147 
148     // Load the MolecularAssembly.
149     activeAssembly = getActiveAssembly(filename);
150     if (activeAssembly == null) {
151       logger.info(helpString());
152       return this;
153     }
154 
155     // Set the filename.
156     filename = activeAssembly.getFile().getAbsolutePath();
157 
158     // Set active atoms.
159     atomSelectionOptions.setActiveAtoms(activeAssembly);
160 
161     potential = activeAssembly.getPotentialEnergy();
162     double[] x = new double[potential.getNumberOfVariables()];
163     potential.getCoordinates(x);
164     potential.energy(x, true);
165 
166     if (barostatOptions.getPressure() > 0) {
167       CrystalPotential crystalPotential = (CrystalPotential) potential;
168       potential = barostatOptions.createBarostat(activeAssembly, crystalPotential);
169     }
170 
171     Comm world = Comm.world();
172     int size = world.size();
173 
174     if (!repEx.getRepEx() || size < 2) {
175       logger.info("\n Running molecular dynamics on " + filename);
176       // Restart File
177       File dyn = new File(FilenameUtils.removeExtension(filename) + ".dyn");
178       if (!dyn.exists()) {
179         dyn = null;
180       }
181 
182       molDyn = dynamicsOptions.getDynamics(writeOut, potential, activeAssembly, algorithmListener);
183 
184       molDyn.dynamic(dynamicsOptions.getSteps(), dynamicsOptions.getDt(),
185           dynamicsOptions.getReport(), dynamicsOptions.getWrite(), dynamicsOptions.getTemperature(), true, dyn);
186 
187     } else {
188       logger.info("\n Running replica exchange molecular dynamics on " + filename);
189       int rank = (size > 1) ? world.rank() : 0;
190       logger.info("Rank:" + rank);
191 
192       File structureFile = new File(filename);
193       String baseFilename = FilenameUtils.removeExtension(structureFile.getName());
194       File rankDirectory = new File(structureFile.getParent() + File.separator + rank);
195       if (!rankDirectory.exists()) {
196         rankDirectory.mkdir();
197       }
198 
199       // TODO: Finish setting up restart support.
200       String withRankName = rankDirectory.getPath() + File.separator + baseFilename;
201       File dyn = new File(withRankName + ".dyn");
202       if (!dyn.exists()) {
203         dyn = null;
204       }
205 
206       final String newMolAssemblyFile = rankDirectory.getPath() + File.separator + structureFile.getName();
207       logger.info("Set activeAssembly filename: " + newMolAssemblyFile);
208       activeAssembly.setFile(new File(newMolAssemblyFile));
209       molDyn = dynamicsOptions.getDynamics(writeOut, potential, activeAssembly, algorithmListener);
210       ReplicaExchange replicaExchange = new ReplicaExchange(molDyn, algorithmListener,
211           dynamicsOptions.getTemperature(), repEx.getExponent(), repEx.getMonteCarlo());
212 
213       long totalSteps = dynamicsOptions.getSteps();
214       int nSteps = repEx.getReplicaSteps();
215       int cycles = (int) (totalSteps / nSteps);
216       if (cycles <= 0) {
217         cycles = 1;
218       }
219 
220       replicaExchange.sample(cycles, nSteps, dynamicsOptions.getDt(), dynamicsOptions.getReport(), dynamicsOptions.getWrite());
221     }
222 
223     return this;
224   }
225 
226   /**
227    * {@inheritDoc}
228    */
229   @Override
230   public List<Potential> getPotentials() {
231     List<Potential> potentials;
232     if (potential == null) {
233       potentials = Collections.emptyList();
234     } else {
235       potentials = Collections.singletonList(potential);
236     }
237     return potentials;
238   }
239 
240 }