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.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.log(Level.INFO, format("  Angles \t\t%6d\t\t%1d", nAngles, forceGroup));
113     }
114   }
115 
116   /**
117    * Convenience method to construct an OpenMM Angle Force.
118    *
119    * @param openMMEnergy The OpenMM Energy instance that contains the angles.
120    * @return An Angle Force, or null if there are no angles.
121    */
122   public static Force constructForce(OpenMMEnergy openMMEnergy) {
123     Angle[] angles = openMMEnergy.getAngles();
124     if (angles == null || angles.length < 1) {
125       return null;
126     }
127     AngleForce angleForce = new AngleForce(openMMEnergy);
128     if (angleForce.nAngles > 0) {
129       return angleForce;
130     }
131     return null;
132   }
133 
134   /**
135    * Update an existing angle force for the OpenMM System.
136    *
137    * @param openMMEnergy The OpenMM Energy instance that contains the angles.
138    */
139   public void updateForce(OpenMMEnergy openMMEnergy) {
140     Angle[] angles = openMMEnergy.getAngles();
141     if (angles == null || angles.length < 1) {
142       return;
143     }
144 
145     DoubleArray parameters = new DoubleArray(0);
146     int index = 0;
147     for (Angle angle : angles) {
148       AngleType.AngleMode angleMode = angle.angleType.angleMode;
149       if (!manyBodyTitration && angleMode == AngleType.AngleMode.IN_PLANE) {
150         // Skip In-Plane angles unless this is ManyBody Titration.
151       }
152       // Update angles that do not involve rigid hydrogen atoms.
153       else if (!rigidHydrogenAngles || !isHydrogenAngle(angle)) {
154         int i1 = angle.getAtom(0).getXyzIndex() - 1;
155         int i2 = angle.getAtom(1).getXyzIndex() - 1;
156         int i3 = angle.getAtom(2).getXyzIndex() - 1;
157         double theta0 = angle.angleType.angle[angle.nh];
158         double k = OpenMM_KJPerKcal * angle.angleType.angleUnit * angle.angleType.forceConstant;
159         if (angleMode == AngleType.AngleMode.IN_PLANE) {
160           // Zero the force constant for In-Plane Angles.
161           k = 0.0;
162         }
163         parameters.append(theta0);
164         parameters.append(k);
165         setAngleParameters(index++, i1, i2, i3, parameters);
166         parameters.resize(0);
167       }
168     }
169     parameters.destroy();
170     updateParametersInContext(openMMEnergy.getContext());
171   }
172 
173   /**
174    * Check to see if an angle is a hydrogen angle. This method only returns true for hydrogen
175    * angles that are less than 160 degrees.
176    *
177    * @param angle Angle to check.
178    * @return boolean indicating whether an angle is a hydrogen angle that is less than 160 degrees.
179    */
180   private boolean isHydrogenAngle(Angle angle) {
181     if (angle.containsHydrogen()) {
182       // Equilibrium angle value in degrees.
183       double angleVal = angle.angleType.angle[angle.nh];
184       // Make sure angle is less than 160 degrees because greater than 160 degrees will not be
185       // constrained
186       // well using the law of cosines.
187       if (angleVal < 160.0) {
188         Atom atom1 = angle.getAtom(0);
189         Atom atom2 = angle.getAtom(1);
190         Atom atom3 = angle.getAtom(2);
191         // Setting constraints only on angles where atom1 or atom3 is a hydrogen while atom2 is
192         // not a hydrogen.
193         return atom1.isHydrogen() && atom3.isHydrogen() && !atom2.isHydrogen();
194       }
195     }
196     return false;
197   }
198 
199 }