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.algorithms.commands;
39  
40  import ffx.algorithms.cli.AlgorithmsCommand;
41  import ffx.algorithms.cli.AnnealOptions;
42  import ffx.algorithms.cli.DynamicsOptions;
43  import ffx.algorithms.optimize.anneal.SimulatedAnnealing;
44  import ffx.numerics.Potential;
45  import ffx.potential.cli.AtomSelectionOptions;
46  import ffx.potential.cli.WriteoutOptions;
47  import ffx.utilities.FFXBinding;
48  import org.apache.commons.io.FilenameUtils;
49  import picocli.CommandLine.Command;
50  import picocli.CommandLine.Mixin;
51  import picocli.CommandLine.Parameters;
52  
53  import java.io.File;
54  import java.util.Collections;
55  import java.util.List;
56  
57  import static java.lang.String.format;
58  
59  /**
60   * The Anneal script.
61   * <br>
62   * Usage:
63   * <br>
64   * ffxc Anneal [options] &lt;filename&gt;
65   */
66  @Command(description = " Run simulated annealing on a system.", name = "Anneal")
67  public class Anneal extends AlgorithmsCommand {
68  
69    @Mixin
70    private AtomSelectionOptions atomSelectionOptions;
71  
72    @Mixin
73    private DynamicsOptions dynamics;
74  
75    @Mixin
76    private AnnealOptions anneal;
77  
78    @Mixin
79    private WriteoutOptions writeOut;
80  
81    /**
82     * An XYZ or PDB input file.
83     */
84    @Parameters(arity = "1", paramLabel = "file",
85        description = "XYZ or PDB input file.")
86    private String filename;
87  
88    private SimulatedAnnealing simulatedAnnealing = null;
89  
90    private Potential potential;
91  
92    /**
93     * Anneal Constructor.
94     */
95    public Anneal() {
96      super();
97    }
98  
99    /**
100    * Anneal Constructor.
101    *
102    * @param binding The Binding to use.
103    */
104   public Anneal(FFXBinding binding) {
105     super(binding);
106   }
107 
108   /**
109    * Anneal constructor that sets the command line arguments.
110    *
111    * @param args Command line arguments.
112    */
113   public Anneal(String[] args) {
114     super(args);
115   }
116 
117   /**
118    * {@inheritDoc}
119    */
120   @Override
121   public Anneal run() {
122 
123     // Init the context and bind variables.
124     if (!init()) {
125       return this;
126     }
127 
128     // Init DynamicsOptions (e.g. the thermostat and barostat flags).
129     dynamics.init();
130 
131     // Load the MolecularAssembly.
132     activeAssembly = getActiveAssembly(filename);
133     if (activeAssembly == null) {
134       logger.info(helpString());
135       return this;
136     }
137 
138     // Set the filename.
139     filename = activeAssembly.getFile().getAbsolutePath();
140 
141     // Set active atoms.
142     atomSelectionOptions.setActiveAtoms(activeAssembly);
143 
144     logger.info("\n Running simulated annealing on " + filename + "\n");
145 
146     // Define the path to the restart File.
147     File dyn = new File(FilenameUtils.removeExtension(filename) + ".dyn");
148     if (!dyn.exists()) {
149       dyn = null;
150     }
151 
152     potential = activeAssembly.getPotentialEnergy();
153 
154     simulatedAnnealing = anneal.createAnnealer(dynamics, activeAssembly,
155         activeAssembly.getPotentialEnergy(), algorithmListener, dyn);
156 
157     simulatedAnnealing.setPrintInterval(dynamics.getReport());
158     simulatedAnnealing.setSaveFrequency(dynamics.getWrite());
159     simulatedAnnealing.setRestartFrequency(dynamics.getCheckpoint());
160     simulatedAnnealing.setTrajectorySteps(dynamics.getTrajSteps());
161 
162     simulatedAnnealing.anneal();
163 
164     if (baseDir == null || !baseDir.exists() || !baseDir.isDirectory() || !baseDir.canWrite()) {
165       baseDir = new File(FilenameUtils.getFullPath(filename));
166     }
167 
168     String dirName = baseDir.toString() + File.separator;
169     String fileName = FilenameUtils.getName(filename);
170     fileName = FilenameUtils.removeExtension(fileName);
171 
172     writeOut.saveFile(format("%s%s", dirName, fileName), algorithmFunctions, activeAssembly);
173 
174     return this;
175   }
176 
177   /**
178    * Get the SimulatedAnnealing object.
179    *
180    * @return The SimulatedAnnealing instance.
181    */
182   public SimulatedAnnealing getAnnealing() {
183     return simulatedAnnealing;
184   }
185 
186   /**
187    * {@inheritDoc}
188    */
189   @Override
190   public List<Potential> getPotentials() {
191     List<Potential> potentials;
192     if (potential == null) {
193       potentials = Collections.emptyList();
194     } else {
195       potentials = Collections.singletonList(potential);
196     }
197     return potentials;
198   }
199 
200   /**
201    * {@inheritDoc}
202    */
203   @Override
204   public boolean destroyPotentials() {
205     return getPotentials().stream().allMatch(Potential::destroy);
206   }
207 }