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