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.DoubleArray;
41  import ffx.openmm.Force;
42  import ffx.openmm.IntArray;
43  import ffx.openmm.CustomCompoundBondForce;
44  import ffx.potential.bonded.StretchBend;
45  
46  import java.util.logging.Level;
47  import java.util.logging.Logger;
48  
49  import static edu.uiowa.jopenmm.OpenMMAmoebaLibrary.OpenMM_KJPerKcal;
50  import static edu.uiowa.jopenmm.OpenMMAmoebaLibrary.OpenMM_NmPerAngstrom;
51  import static edu.uiowa.jopenmm.OpenMMAmoebaLibrary.OpenMM_RadiansPerDegree;
52  import static java.lang.String.format;
53  
54  /**
55   * OpenMM Stretch-Bend Force.
56   */
57  public class StretchBendForce extends CustomCompoundBondForce {
58  
59    private static final Logger logger = Logger.getLogger(StretchBendForce.class.getName());
60  
61    /**
62     * Create an OpenMM Stretch-Bend Force.
63     *
64     * @param openMMEnergy The OpenMM Energy instance that contains the Stretch-Bends.
65     */
66    public StretchBendForce(OpenMMEnergy openMMEnergy) {
67      super(3, openMMEnergy.getStretchBendEnergyString());
68      StretchBend[] stretchBends = openMMEnergy.getStretchBends();
69      if (stretchBends == null || stretchBends.length < 1) {
70        return;
71      }
72      addPerBondParameter("r12");
73      addPerBondParameter("r23");
74      addPerBondParameter("theta0");
75      addPerBondParameter("k1");
76      addPerBondParameter("k2");
77      setName("AmoebaStretchBend");
78  
79      IntArray particles = new IntArray(0);
80      DoubleArray parameters = new DoubleArray(0);
81      for (StretchBend stretchBend : stretchBends) {
82        int i1 = stretchBend.getAtom(0).getXyzIndex() - 1;
83        int i2 = stretchBend.getAtom(1).getXyzIndex() - 1;
84        int i3 = stretchBend.getAtom(2).getXyzIndex() - 1;
85        double r12 = stretchBend.bond0Eq * OpenMM_NmPerAngstrom;
86        double r23 = stretchBend.bond1Eq * OpenMM_NmPerAngstrom;
87        double theta0 = stretchBend.angleEq * OpenMM_RadiansPerDegree;
88        double k1 = stretchBend.force0 * OpenMM_KJPerKcal / OpenMM_NmPerAngstrom;
89        double k2 = stretchBend.force1 * OpenMM_KJPerKcal / OpenMM_NmPerAngstrom;
90        particles.append(i1);
91        particles.append(i2);
92        particles.append(i3);
93        parameters.append(r12);
94        parameters.append(r23);
95        parameters.append(theta0);
96        parameters.append(k1);
97        parameters.append(k2);
98        addBond(particles, parameters);
99        particles.resize(0);
100       parameters.resize(0);
101     }
102     particles.destroy();
103     parameters.destroy();
104 
105     int forceGroup = openMMEnergy.getMolecularAssembly().getForceField().getInteger("STRETCH_BEND_FORCE_GROUP", 0);
106     setForceGroup(forceGroup);
107     logger.log(Level.INFO, format("  Stretch-Bends \t%6d\t\t%1d", stretchBends.length, forceGroup));
108   }
109 
110   /**
111    * Convenience method to construct an OpenMM Stretch-Bend Force.
112    *
113    * @param openMMEnergy The OpenMM Energy instance that contains the stretch-bends.
114    * @return An OpenMM Stretch-Bend Force, or null if there are no stretch-bends.
115    */
116   public static Force constructForce(OpenMMEnergy openMMEnergy) {
117     StretchBend[] stretchBends = openMMEnergy.getStretchBends();
118     if (stretchBends == null || stretchBends.length < 1) {
119       return null;
120     }
121     return new StretchBendForce(openMMEnergy);
122   }
123 
124   /**
125    * Update this Stretch-Bend Force.
126    *
127    * @param openMMEnergy The OpenMM Energy instance that contains the stretch-bends.
128    */
129   public void updateForce(OpenMMEnergy openMMEnergy) {
130     StretchBend[] stretchBends = openMMEnergy.getStretchBends();
131     if (stretchBends == null || stretchBends.length < 1) {
132       return;
133     }
134 
135     IntArray particles = new IntArray(0);
136     DoubleArray parameters = new DoubleArray(0);
137     int index = 0;
138     for (StretchBend stretchBend : stretchBends) {
139       int i1 = stretchBend.getAtom(0).getXyzIndex() - 1;
140       int i2 = stretchBend.getAtom(1).getXyzIndex() - 1;
141       int i3 = stretchBend.getAtom(2).getXyzIndex() - 1;
142       double r12 = stretchBend.bond0Eq * OpenMM_NmPerAngstrom;
143       double r23 = stretchBend.bond1Eq * OpenMM_NmPerAngstrom;
144       double theta0 = stretchBend.angleEq * OpenMM_RadiansPerDegree;
145       double k1 = stretchBend.force0 * OpenMM_KJPerKcal / OpenMM_NmPerAngstrom;
146       double k2 = stretchBend.force1 * OpenMM_KJPerKcal / OpenMM_NmPerAngstrom;
147       particles.append(i1);
148       particles.append(i2);
149       particles.append(i3);
150       parameters.append(r12);
151       parameters.append(r23);
152       parameters.append(theta0);
153       parameters.append(k1);
154       parameters.append(k2);
155       setBondParameters(index++, particles, parameters);
156       particles.resize(0);
157       parameters.resize(0);
158     }
159     particles.destroy();
160     parameters.destroy();
161 
162     updateParametersInContext(openMMEnergy.getContext());
163   }
164 }