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.realspace.commands;
39  
40  import ffx.algorithms.cli.AlgorithmsCommand;
41  import ffx.algorithms.cli.MinimizeOptions;
42  import ffx.potential.MolecularAssembly;
43  import ffx.potential.cli.AtomSelectionOptions;
44  import ffx.realspace.RealSpaceData;
45  import ffx.realspace.cli.RealSpaceOptions;
46  import ffx.realspace.parsers.RealSpaceFile;
47  import ffx.utilities.FFXBinding;
48  import ffx.xray.RefinementMinimize;
49  import org.apache.commons.io.FilenameUtils;
50  import picocli.CommandLine.Command;
51  import picocli.CommandLine.Mixin;
52  import picocli.CommandLine.Parameters;
53  
54  import java.io.File;
55  import java.util.List;
56  
57  import static ffx.utilities.TinkerUtils.version;
58  
59  /**
60   * The Real Space Minimize script.
61   * <br>
62   * Usage:
63   * <br>
64   * ffxc realspace.Minimize [options] &lt;filename [file2...]&gt;
65   */
66  @Command(description = " Minimization on a Real Space target.", name = "realspace.Minimize")
67  public class Minimize extends AlgorithmsCommand {
68  
69    @Mixin
70    private MinimizeOptions minimizeOptions;
71  
72    @Mixin
73    private RealSpaceOptions realSpaceOptions;
74  
75    @Mixin
76    private AtomSelectionOptions atomSelectionOptions;
77  
78    /**
79     * One or more filenames.
80     */
81    @Parameters(arity = "1..*", paramLabel = "files", description = "PDB and Real Space input files.")
82    private List<String> filenames;
83    private MolecularAssembly[] molecularAssemblies;
84    private RealSpaceData realspaceData;
85  
86    /**
87     * Minimize constructor.
88     */
89    public Minimize() {
90      super();
91    }
92  
93    /**
94     * Minimize constructor that sets the command line arguments.
95     * @param args Command line arguments.
96     */
97    public Minimize(String[] args) {
98      super(args);
99    }
100 
101   /**
102    * Minimize constructor.
103    * @param binding The Binding to use.
104    */
105   public Minimize(FFXBinding binding) {
106     super(binding);
107   }
108 
109   @Override
110   public Minimize run() {
111 
112     if (!init()) {
113       return this;
114     }
115 
116     String filename;
117     if (filenames != null && !filenames.isEmpty()) {
118       activeAssembly = algorithmFunctions.open(filenames.get(0));
119       filename = filenames.get(0);
120     } else if (activeAssembly == null) {
121       logger.info(helpString());
122       return this;
123     } else {
124       filename = activeAssembly.getFile().getAbsolutePath();
125     }
126     molecularAssemblies = new MolecularAssembly[]{activeAssembly};
127 
128     logger.info("\n Running Real Space Minimization on " + filename);
129 
130     // Set atom (in)active selections.
131     atomSelectionOptions.setActiveAtoms(activeAssembly);
132 
133     RealSpaceFile[] mapFiles =
134         realSpaceOptions.processData(filenames, activeAssembly).toArray(new RealSpaceFile[0]);
135     realspaceData = new RealSpaceData(molecularAssemblies, activeAssembly.getProperties(),
136         activeAssembly.getParallelTeam(), mapFiles);
137 
138     // Beginning force field energy.
139     algorithmFunctions.energy(activeAssembly);
140 
141     RefinementMinimize refinementMinimize = new RefinementMinimize(realspaceData,
142         realSpaceOptions.refinementMode);
143 
144     int nBFGS = minimizeOptions.getNBFGS();
145     double eps = minimizeOptions.getEps();
146     int maxiter = minimizeOptions.getIterations();
147     if (eps < 0.0) {
148       eps = 1.0;
149     }
150 
151     if (maxiter < Integer.MAX_VALUE) {
152       logger.info(String.
153           format("\n RMS gradient convergence criteria: %8.5f, Maximum iterations %d", eps,
154               maxiter));
155     } else {
156       logger.info(String.format("\n RMS gradient convergence criteria: %8.5f", eps));
157     }
158 
159     refinementMinimize.minimize(nBFGS, eps, maxiter);
160 
161     // Final target function.
162     algorithmFunctions.energy(activeAssembly);
163 
164     File file = version(new File(FilenameUtils.removeExtension(filename) + ".pdb"));
165     algorithmFunctions.saveAsPDB(molecularAssemblies, file);
166 
167     return this;
168   }
169 
170   @Override
171   public boolean destroyPotentials() {
172     boolean destroyedSuccess = true;
173     if (molecularAssemblies != null) {
174       if (realspaceData != null) {
175         destroyedSuccess = realspaceData.destroy();
176       }
177       for (int i = 1; i < molecularAssemblies.length; i++) {
178         destroyedSuccess = destroyedSuccess && molecularAssemblies[i].getPotentialEnergy().destroy();
179       }
180     }
181     return destroyedSuccess;
182   }
183 }