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.commands.test.OSTGradient;
41  import ffx.algorithms.misc.AlgorithmsTest;
42  import org.apache.commons.math3.util.FastMath;
43  import org.junit.Test;
44  import org.junit.runner.RunWith;
45  import org.junit.runners.Parameterized;
46  import org.junit.runners.Parameterized.Parameters;
47  
48  import java.util.Arrays;
49  import java.util.Collection;
50  
51  import static java.lang.Math.random;
52  import static org.apache.commons.math3.util.FastMath.floor;
53  import static org.junit.Assert.assertEquals;
54  
55  /**
56   * @author Michael J. Schnieders
57   */
58  @RunWith(Parameterized.class)
59  public class OSTGradientTest extends AlgorithmsTest {
60  
61    private final String info;
62    private final String filename;
63    private final int nAtoms;
64    private final double tolerance = 1.0e-2;
65  
66    public OSTGradientTest(final String info, final String filename, final int nAtoms) {
67      this.info = info;
68      this.filename = filename;
69      this.nAtoms = nAtoms;
70    }
71  
72    @Parameters
73    public static Collection<Object[]> data() {
74      return Arrays.asList(
75          new Object[][]{
76              {
77                  "C23 OST Test", // info
78                  "compound23.xyz", // filename
79                  43 // Number of atoms.
80              }
81          });
82    }
83  
84    @Test
85    public void testOSTBiasHelp() {
86      // Set up the input arguments for the script.
87      String[] args = {"-h"};
88      binding.setVariable("args", args);
89  
90      // Construct and evaluate the script.
91      OSTGradient ostGradient = new OSTGradient(binding).run();
92      algorithmsScript = ostGradient;
93    }
94  
95    @Test
96    public void testOSTGradient() {
97  
98      final double lambda = random();
99      final int atomID = (int) floor(FastMath.random() * nAtoms) + 1;
100 
101     // Set up the input arguments for the script.
102     String[] args = {
103         "--ac", "ALL",
104         "-l", Double.toString(lambda),
105         "--ga", Integer.toString(atomID),
106         getResourcePath(filename)
107     };
108     binding.setVariable("args", args);
109 
110     // Construct and evaluate the script.
111     OSTGradient ostGradient = new OSTGradient(binding).run();
112     algorithmsScript = ostGradient;
113 
114     double dUdLError = ostGradient.dUdLError;
115     double nFailures = ostGradient.nFailures;
116 
117     // Assert that energy is conserved at the end of the dynamics trajectory.
118     assertEquals(info + ": dUdL error for atom " + atomID + " at lambda " + args[4] + ": ", 0.0, dUdLError, tolerance);
119     assertEquals(info + ": Number of coordinate gradient errors for atom " + atomID + " at lambda " + args[4] + ": ", 0, nFailures, 0);
120   }
121 
122   @Test
123   public void testMetaDynamicsGradient() {
124 
125     double lambda = random();
126     int atomID = (int) floor(FastMath.random() * nAtoms) + 1;
127 
128     // Set up the input arguments for the script.
129     String[] args = {
130         "--ac", "ALL",
131         "--meta",
132         "-l", Double.toString(lambda),
133         "--ga", Integer.toString(atomID),
134         getResourcePath(filename)
135     };
136     binding.setVariable("args", args);
137 
138     // Construct and evaluate the script.
139     OSTGradient ostGradient = new OSTGradient(binding).run();
140     algorithmsScript = ostGradient;
141 
142     double dUdLError = ostGradient.dUdLError;
143     double nFailures = ostGradient.nFailures;
144 
145     // Assert that energy is conserved at the end of the dynamics trajectory.
146     assertEquals(info + ": dUdL error for atom " + args[6] + " at lambda " + args[4] + ": ", 0.0, dUdLError, tolerance);
147     assertEquals(info + ": Number of coordinate gradient errors for atom " + args[6] + " at lambda " + args[4] + ": ", 0, nFailures, 0);
148   }
149 }