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.openmm;
39  
40  import ffx.openmm.Force;
41  import ffx.openmm.amoeba.GKCavitationForce;
42  import ffx.potential.bonded.Atom;
43  import ffx.potential.nonbonded.GeneralizedKirkwood;
44  import ffx.potential.nonbonded.implicit.ChandlerCavitation;
45  import ffx.potential.nonbonded.implicit.DispersionRegion;
46  import ffx.potential.nonbonded.implicit.GaussVol;
47  
48  import java.util.logging.Level;
49  import java.util.logging.Logger;
50  
51  import static edu.uiowa.jopenmm.OpenMMAmoebaLibrary.OpenMM_AmoebaGKCavitationForce_NonbondedMethod.OpenMM_AmoebaGKCavitationForce_NoCutoff;
52  import static edu.uiowa.jopenmm.OpenMMAmoebaLibrary.OpenMM_KJPerKcal;
53  import static edu.uiowa.jopenmm.OpenMMAmoebaLibrary.OpenMM_NmPerAngstrom;
54  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Boolean.OpenMM_False;
55  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Boolean.OpenMM_True;
56  import static java.lang.String.format;
57  
58  /**
59   * AmoebaCavitationForce.
60   */
61  public class AmoebaGKCavitationForce extends GKCavitationForce {
62  
63    private static final Logger logger = Logger.getLogger(AmoebaGKCavitationForce.class.getName());
64  
65    /**
66     * Constructor.
67     *
68     * @param openMMEnergy OpenMM energy.
69     */
70    public AmoebaGKCavitationForce(OpenMMEnergy openMMEnergy) {
71      GeneralizedKirkwood generalizedKirkwood = openMMEnergy.getGK();
72      if (generalizedKirkwood == null) {
73        destroy();
74        return;
75      }
76      ChandlerCavitation chandlerCavitation = generalizedKirkwood.getChandlerCavitation();
77      if (chandlerCavitation == null) {
78        destroy();
79        return;
80      }
81      GaussVol gaussVol = chandlerCavitation.getGaussVol();
82      if (gaussVol == null) {
83        destroy();
84        return;
85      }
86  
87      double surfaceTension = chandlerCavitation.getSurfaceTension()
88          * OpenMM_KJPerKcal / OpenMM_NmPerAngstrom / OpenMM_NmPerAngstrom;
89      double[] rad = gaussVol.getRadii();
90  
91      int index = 0;
92      Atom[] atoms = openMMEnergy.getMolecularAssembly().getAtomArray();
93      for (Atom atom : atoms) {
94        int isHydrogen = OpenMM_False;
95        double radius = rad[index++];
96        if (atom.isHydrogen()) {
97          isHydrogen = OpenMM_True;
98          radius = 0.0;
99        }
100       addParticle(radius * OpenMM_NmPerAngstrom, surfaceTension, isHydrogen);
101     }
102 
103     setNonbondedMethod(OpenMM_AmoebaGKCavitationForce_NoCutoff);
104 
105     int forceGroup = openMMEnergy.getMolecularAssembly().getForceField().getInteger("GK_FORCE_GROUP", 2);
106     setForceGroup(forceGroup);
107     logger.log(Level.INFO, format("  GaussVol cavitation force \t\t%d", forceGroup));
108   }
109 
110   /**
111    * Convenience method to construct an AMOEBA Cavitation Force.
112    *
113    * @param openMMEnergy The OpenMM Energy instance that contains the cavitation information.
114    * @return An AMOEBA Cavitation Force, or null if there are no cavitation interactions.
115    */
116   public static Force constructForce(OpenMMEnergy openMMEnergy) {
117     GeneralizedKirkwood gk = openMMEnergy.getGK();
118     if (gk == null) {
119       return null;
120     }
121     DispersionRegion dispersionRegion = gk.getDispersionRegion();
122     if (dispersionRegion == null) {
123       return null;
124     }
125     return new AmoebaGKCavitationForce(openMMEnergy);
126   }
127 
128   /**
129    * Update the Cavitation force.
130    *
131    * @param atoms        The atoms to update.
132    * @param openMMEnergy The OpenMM energy term.
133    */
134   public void updateForce(Atom[] atoms, OpenMMEnergy openMMEnergy) {
135     GeneralizedKirkwood generalizedKirkwood = openMMEnergy.getGK();
136     if (generalizedKirkwood == null) {
137       return;
138     }
139     ChandlerCavitation chandlerCavitation = generalizedKirkwood.getChandlerCavitation();
140     if (chandlerCavitation == null) {
141       return;
142     }
143     GaussVol gaussVol = chandlerCavitation.getGaussVol();
144     if (gaussVol == null) {
145       return;
146     }
147 
148     double surfaceTension = chandlerCavitation.getSurfaceTension()
149         * OpenMM_KJPerKcal / OpenMM_NmPerAngstrom / OpenMM_NmPerAngstrom;
150     double lambdaElec = openMMEnergy.getSystem().getLambdaElec();
151 
152     // Changing cavitation radii is not supported.
153     // for (int i=0; i<nAtoms; i++) {
154     //  gaussVol.updateAtom(i);
155     // }
156     double[] rad = gaussVol.getRadii();
157 
158     for (Atom atom : atoms) {
159       int index = atom.getXyzIndex() - 1;
160       double useFactor = 1.0;
161       if (!atom.getUse()) {
162         useFactor = 0.0;
163       }
164       // Scale all implicit solvent terms with the square of electrostatics lambda
165       // (so dUcav / dL is 0 at lambdaElec = 0).
166       double lambdaScale = lambdaElec * lambdaElec;
167       if (!atom.applyLambda()) {
168         lambdaScale = 1.0;
169       }
170       useFactor *= lambdaScale;
171 
172       double radius = rad[index];
173       int isHydrogen = OpenMM_False;
174       if (atom.isHydrogen()) {
175         isHydrogen = OpenMM_True;
176         radius = 0.0;
177       }
178 
179       setParticleParameters(index, radius * OpenMM_NmPerAngstrom, surfaceTension * useFactor, isHydrogen);
180     }
181     updateParametersInContext(openMMEnergy.getContext());
182   }
183 }