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.DoubleArray;
41  import ffx.openmm.Force;
42  import ffx.openmm.CustomAngleForce;
43  import ffx.potential.bonded.Angle;
44  import ffx.potential.bonded.Atom;
45  import ffx.potential.parameters.AngleType;
46  import ffx.potential.parameters.ForceField;
47  
48  import java.util.logging.Level;
49  import java.util.logging.Logger;
50  
51  import static edu.uiowa.jopenmm.OpenMMAmoebaLibrary.OpenMM_KJPerKcal;
52  import static java.lang.String.format;
53  
54  /**
55   * OpenMM Angle Force.
56   */
57  public class AngleForce extends CustomAngleForce {
58  
59    private static final Logger logger = Logger.getLogger(AngleForce.class.getName());
60  
61    private int nAngles = 0;
62    private final boolean manyBodyTitration;
63    private final boolean rigidHydrogenAngles;
64  
65    /**
66     * Create an OpenMM Angle Force.
67     *
68     * @param openMMEnergy The OpenMM Energy instance that contains the angles.
69     */
70    public AngleForce(OpenMMEnergy openMMEnergy) {
71      super(openMMEnergy.getAngleEnergyString());
72      ForceField forceField = openMMEnergy.getMolecularAssembly().getForceField();
73      manyBodyTitration = forceField.getBoolean("MANYBODY_TITRATION", false);
74      rigidHydrogenAngles = forceField.getBoolean("RIGID_HYDROGEN_ANGLES", false);
75      addPerAngleParameter("theta0");
76      addPerAngleParameter("k");
77      setName("Angle");
78  
79      DoubleArray parameters = new DoubleArray(0);
80      Angle[] angles = openMMEnergy.getAngles();
81      for (Angle angle : angles) {
82        AngleType angleType = angle.getAngleType();
83        AngleType.AngleMode angleMode = angleType.angleMode;
84        if (!manyBodyTitration && angleMode == AngleType.AngleMode.IN_PLANE) {
85          // Skip In-Plane angles unless this is ManyBody Titration.
86        } else if (isHydrogenAngle(angle) && rigidHydrogenAngles) {
87          logger.log(Level.INFO, " Constrained angle %s was not added the AngleForce.", angle);
88        } else {
89          int i1 = angle.getAtom(0).getXyzIndex() - 1;
90          int i2 = angle.getAtom(1).getXyzIndex() - 1;
91          int i3 = angle.getAtom(2).getXyzIndex() - 1;
92  
93          double theta0 = angleType.angle[angle.nh];
94          double k = OpenMM_KJPerKcal * angleType.angleUnit * angleType.forceConstant;
95          if (angleMode == AngleType.AngleMode.IN_PLANE) {
96            // This is a place-holder Angle, in case the In-Plane Angle is swtiched to a
97            // Normal Angle during in the udpateAngleForce.
98            k = 0.0;
99          }
100         parameters.append(theta0);
101         parameters.append(k);
102         addAngle(i1, i2, i3, parameters);
103         nAngles++;
104         parameters.resize(0);
105       }
106     }
107     parameters.destroy();
108 
109     if (nAngles > 0) {
110       int forceGroup = forceField.getInteger("ANGLE_FORCE_GROUP", 0);
111       setForceGroup(forceGroup);
112       logger.info(format("  Angles:                            %10d", nAngles));
113       logger.fine(format("   Force Group:                      %10d", forceGroup));
114     }
115   }
116 
117   /**
118    * Convenience method to construct an OpenMM Angle Force.
119    *
120    * @param openMMEnergy The OpenMM Energy instance that contains the angles.
121    * @return An Angle Force, or null if there are no angles.
122    */
123   public static Force constructForce(OpenMMEnergy openMMEnergy) {
124     Angle[] angles = openMMEnergy.getAngles();
125     if (angles == null || angles.length < 1) {
126       return null;
127     }
128     AngleForce angleForce = new AngleForce(openMMEnergy);
129     if (angleForce.nAngles > 0) {
130       return angleForce;
131     }
132     return null;
133   }
134 
135   /**
136    * Update an existing angle force for the OpenMM System.
137    *
138    * @param openMMEnergy The OpenMM Energy instance that contains the angles.
139    */
140   public void updateForce(OpenMMEnergy openMMEnergy) {
141     Angle[] angles = openMMEnergy.getAngles();
142     if (angles == null || angles.length < 1) {
143       return;
144     }
145 
146     DoubleArray parameters = new DoubleArray(0);
147     int index = 0;
148     for (Angle angle : angles) {
149       AngleType.AngleMode angleMode = angle.angleType.angleMode;
150       if (!manyBodyTitration && angleMode == AngleType.AngleMode.IN_PLANE) {
151         // Skip In-Plane angles unless this is ManyBody Titration.
152       }
153       // Update angles that do not involve rigid hydrogen atoms.
154       else if (!rigidHydrogenAngles || !isHydrogenAngle(angle)) {
155         int i1 = angle.getAtom(0).getXyzIndex() - 1;
156         int i2 = angle.getAtom(1).getXyzIndex() - 1;
157         int i3 = angle.getAtom(2).getXyzIndex() - 1;
158         double theta0 = angle.angleType.angle[angle.nh];
159         double k = OpenMM_KJPerKcal * angle.angleType.angleUnit * angle.angleType.forceConstant;
160         if (angleMode == AngleType.AngleMode.IN_PLANE) {
161           // Zero the force constant for In-Plane Angles.
162           k = 0.0;
163         }
164         parameters.append(theta0);
165         parameters.append(k);
166         setAngleParameters(index++, i1, i2, i3, parameters);
167         parameters.resize(0);
168       }
169     }
170     parameters.destroy();
171     updateParametersInContext(openMMEnergy.getContext());
172   }
173 
174   /**
175    * Check to see if an angle is a hydrogen angle. This method only returns true for hydrogen
176    * angles that are less than 160 degrees.
177    *
178    * @param angle Angle to check.
179    * @return boolean indicating whether an angle is a hydrogen angle that is less than 160 degrees.
180    */
181   private boolean isHydrogenAngle(Angle angle) {
182     if (angle.containsHydrogen()) {
183       // Equilibrium angle value in degrees.
184       double angleVal = angle.angleType.angle[angle.nh];
185       // Make sure angle is less than 160 degrees because greater than 160 degrees will not be
186       // constrained
187       // well using the law of cosines.
188       if (angleVal < 160.0) {
189         Atom atom1 = angle.getAtom(0);
190         Atom atom2 = angle.getAtom(1);
191         Atom atom3 = angle.getAtom(2);
192         // Setting constraints only on angles where atom1 or atom3 is a hydrogen while atom2 is
193         // not a hydrogen.
194         return atom1.isHydrogen() && atom3.isHydrogen() && !atom2.isHydrogen();
195       }
196     }
197     return false;
198   }
199 
200 }