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-2024.
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.groovy;
39  
40  import static org.apache.commons.math3.util.FastMath.floor;
41  import static org.apache.commons.math3.util.FastMath.random;
42  import static org.junit.Assert.assertEquals;
43  
44  import ffx.potential.ForceFieldEnergy;
45  import ffx.potential.groovy.test.Gradient;
46  import ffx.potential.groovy.test.LambdaGradient;
47  import ffx.potential.utils.PotentialTest;
48  import java.util.Arrays;
49  import java.util.Collection;
50  import org.junit.Test;
51  import org.junit.runner.RunWith;
52  import org.junit.runners.Parameterized;
53  import org.junit.runners.Parameterized.Parameters;
54  
55  /** Test OPLS-AA energy and gradient. */
56  @RunWith(Parameterized.class)
57  public class OPLSAAEnergyTest extends PotentialTest {
58  
59    private final String info;
60    private final String filepath;
61    private final int nAtoms;
62    private final int nBonds;
63    private final int nAngles;
64    private final int nTorsions;
65    private final int nImproperTorsions;
66    private final int nVanDerWaals;
67    private final int nFixedCharge;
68    private final double bondEnergy;
69    private final double angleEnergy;
70    private final double torsionEnergy;
71    private final double improperTorsionEnergy;
72    private final double vanDerWaalsEnergy;
73    private final double fixedChargeEnergy;
74  
75    // The peptide coordinates under OPLS-AA and OPLS-AA/L result in a large gradient for some atoms,
76    // which necessitates a larger tolerance.
77    private final double tolerance = 1.0e-2;
78  
79    public OPLSAAEnergyTest(
80        String info,
81        String filename,
82        int nAtoms,
83        double bondEnergy,
84        int nBonds,
85        double angleEnergy,
86        int nAngles,
87        double torsionEnergy,
88        int nTorsions,
89        double improperTorsionEnergy,
90        int nImproperTorsions,
91        double vanDerWaalsEnergy,
92        int nVanDerWaals,
93        double fixedChargeEnergy,
94        int nFixedCharge) {
95  
96      this.info = info;
97      this.nAtoms = nAtoms;
98      this.bondEnergy = bondEnergy;
99      this.nBonds = nBonds;
100     this.angleEnergy = angleEnergy;
101     this.nAngles = nAngles;
102     this.torsionEnergy = torsionEnergy;
103     this.nTorsions = nTorsions;
104     this.improperTorsionEnergy = improperTorsionEnergy;
105     this.nImproperTorsions = nImproperTorsions;
106     this.vanDerWaalsEnergy = vanDerWaalsEnergy;
107     this.nVanDerWaals = nVanDerWaals;
108     this.fixedChargeEnergy = fixedChargeEnergy;
109     this.nFixedCharge = nFixedCharge;
110     this.filepath = getResourcePath(filename);
111   }
112 
113   @Parameters
114   public static Collection<Object[]> data() {
115     return Arrays.asList(
116         new Object[][] {
117             {
118                 "OPLS Acetanilide Benchmark",
119                 "acetanilide-oplsaa.xyz",
120                 19,
121                 0.45009773,
122                 19,
123                 1.99659439,
124                 30,
125                 3.12713717,
126                 38,
127                 0.03415131,
128                 12,
129                 -10.22874283,
130                 7293,
131                 -24.11087720,
132                 2229
133             },
134             {
135                 "OPLS-AA Peptide",
136                 "peptide-oplsaa.xyz",
137                 328,
138                 72.08575480,
139                 333,
140                 32.38121260,
141                 596,
142                 65.56283245,
143                 875,
144                 0.85331537,
145                 186,
146                 91224.14951970,
147                 40511,
148                 -665.41688158,
149                 52699
150             },
151             {
152                 "OPLS-AA/L Peptide",
153                 "peptide-oplsaal.xyz",
154                 328,
155                 39.69175722,
156                 333,
157                 41.54908436,
158                 596,
159                 41.53603210,
160                 875,
161                 0.92410847,
162                 97,
163                 112122.04255274,
164                 40511,
165                 -671.66812023,
166                 52699,
167             },
168             {
169                 "OPLS Ethylparaben Benchmark",
170                 "ethylparaben-oplsaa.xyz",
171                 44,
172                 1.23483793,
173                 44,
174                 1.17950432,
175                 70,
176                 0.58946840,
177                 88,
178                 0.01347012,
179                 22,
180                 -23.22505628,
181                 16759,
182                 -28.45972542,
183                 5029
184             },
185             {
186                 "OPLS Methylparaben Benchmark",
187                 "methylparaben-oplsaa.xyz",
188                 19,
189                 0.48582171,
190                 19,
191                 0.47331501,
192                 29,
193                 0.53855046,
194                 35,
195                 0.02598434,
196                 11,
197                 -8.71533364,
198                 7647,
199                 -16.384524645738537,
200                 2302
201             },
202             {
203                 "OPLS Paracetamol Benchmark",
204                 "paracetamol-oplsaa.xyz",
205                 20,
206                 0.63722563,
207                 20,
208                 2.63545869,
209                 31,
210                 3.01547224,
211                 40,
212                 0.06569712,
213                 10,
214                 -9.45895598,
215                 7831,
216                 -41.34298872,
217                 3334
218             },
219             {
220                 "OPLS Phenacetin Benchmark",
221                 "phenacetin-oplsaa.xyz",
222                 26,
223                 0.53495810,
224                 26,
225                 3.68523677,
226                 43,
227                 4.12682233,
228                 52,
229                 0.03932507,
230                 10,
231                 -14.31979877,
232                 10338,
233                 -31.561832525438113,
234                 3116
235             }
236         });
237   }
238 
239   @Test
240   public void testEnergy() {
241     logger.info(" Testing energy for " + info);
242 
243     // Set-up the input arguments for the Energy script.
244     String[] args = {filepath};
245     binding.setVariable("args", args);
246 
247     // Evaluate the script.
248     Energy energy = new Energy(binding).run();
249     potentialScript = energy;
250 
251     ForceFieldEnergy forceFieldEnergy = energy.forceFieldEnergy;
252 
253     // Bond Energy
254     assertEquals(info + " Bond Energy", bondEnergy, forceFieldEnergy.getBondEnergy(), tolerance);
255     assertEquals(info + " Bond Count", nBonds, forceFieldEnergy.getNumberofBonds());
256     // Angle Energy
257     assertEquals(info + " Angle Energy", angleEnergy, forceFieldEnergy.getAngleEnergy(), tolerance);
258     assertEquals(info + " Angle Count", nAngles, forceFieldEnergy.getNumberofAngles());
259     // Torsional Angle
260     assertEquals(info + " Torsion Energy", torsionEnergy, forceFieldEnergy.getTorsionEnergy(), tolerance);
261     assertEquals(info + " Torsion Count", nTorsions, forceFieldEnergy.getNumberofTorsions());
262     // Improper Torsional Angle
263     assertEquals(info + " Improper Torsion Energy", improperTorsionEnergy, forceFieldEnergy.getImproperTorsionEnergy(), tolerance);
264     assertEquals(info + " Improper Torsion Count", nImproperTorsions, forceFieldEnergy.getNumberofImproperTorsions());
265     // van Der Waals
266     assertEquals(info + " van Der Waals Energy", vanDerWaalsEnergy, forceFieldEnergy.getVanDerWaalsEnergy(), tolerance);
267     assertEquals(info + " van Der Waals Count", nVanDerWaals, forceFieldEnergy.getVanDerWaalsInteractions());
268     // Permanent Multipoles
269     assertEquals(info + " Fixed Charge Energy", fixedChargeEnergy, forceFieldEnergy.getPermanentMultipoleEnergy(), tolerance);
270     assertEquals(info + " Fixed Charge Count", nFixedCharge, forceFieldEnergy.getPermanentInteractions());
271   }
272 
273   @Test
274   public void testGradient() {
275     logger.info(" Testing Cartesian gradient(s) for " + info);
276 
277     // Set-up the input arguments for the Gradient script.
278     // Choose a random atom to test.
279     int atomID = (int) floor(random() * nAtoms) + 1;
280     double stepSize = 1.0e-5;
281     String[] args = {
282         "--ga", Integer.toString(atomID),
283         "--tol", Double.toString(tolerance),
284         "--dx", Double.toString(stepSize),
285         filepath
286     };
287     binding.setVariable("args", args);
288 
289     // Construct and evaluate the script.
290     Gradient gradient = new Gradient(binding).run();
291     potentialScript = gradient;
292 
293     assertEquals(info + " gradient failures: ", 0, gradient.nFailures);
294   }
295 
296   @Test
297   public void testLambdaGradient() {
298     logger.info(" Testing lambda gradient(s) for " + info);
299 
300     // Set-up the input arguments for the LambdaGradient script.
301     // Choose a random atom to test dEdX gradient.
302     int atomID = (int) floor(random() * nAtoms) + 1;
303     double stepSize = 1.0e-5;
304     String[] args = {
305         "--ga", Integer.toString(atomID),
306         "--dx", Double.toString(stepSize),
307         "--tol", Double.toString(tolerance),
308         "--ac", "ALL",
309         "-l", "0.5",
310         filepath
311     };
312     binding.setVariable("args", args);
313 
314     // Construct and evaluate the script.
315     LambdaGradient lambdaGradient = new LambdaGradient(binding).run();
316     potentialScript = lambdaGradient;
317 
318     assertEquals(info + " dEdL failures: ", 0, lambdaGradient.ndEdLFailures);
319     assertEquals(info + " d2EdL2 failures: ", 0, lambdaGradient.nd2EdL2Failures);
320     assertEquals(info + " dEdXdL failures: ", 0, lambdaGradient.ndEdXdLFailures);
321     assertEquals(info + " dEdX failures: ", 0, lambdaGradient.ndEdXFailures);
322   }
323 }