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.CustomCompoundBondForce;
41  import ffx.openmm.DoubleArray;
42  import ffx.openmm.Force;
43  import ffx.openmm.IntArray;
44  import ffx.potential.ForceFieldEnergy;
45  import ffx.potential.bonded.AngleTorsion;
46  import ffx.potential.bonded.Atom;
47  import ffx.potential.terms.AngleTorsionPotentialEnergy;
48  
49  import java.util.logging.Logger;
50  
51  import static edu.uiowa.jopenmm.OpenMMAmoebaLibrary.OpenMM_KJPerKcal;
52  import static edu.uiowa.jopenmm.OpenMMAmoebaLibrary.OpenMM_RadiansPerDegree;
53  import static java.lang.String.format;
54  
55  /**
56   * OpenMM Angle-Torsion Force.
57   */
58  public class AngleTorsionForce extends CustomCompoundBondForce {
59  
60    private static final Logger logger = Logger.getLogger(AngleTorsionForce.class.getName());
61  
62    /**
63     * Create an OpenMM Angle-Torsion Force.
64     *
65     * @param angleTorsionPotentialEnergy The AngleTorsionPotentialEnergy instance that contains the Angle-Torsions
66     */
67    public AngleTorsionForce(AngleTorsionPotentialEnergy angleTorsionPotentialEnergy) {
68      super(4, AngleTorsion.angleTorsionForm());
69      AngleTorsion[] angleTorsions = angleTorsionPotentialEnergy.getAngleTorsionArray();
70      addGlobalParameter("phi1", 0);
71      addGlobalParameter("phi2", Math.PI);
72      addGlobalParameter("phi3", 0);
73      for (int m = 1; m < 3; m++) {
74        for (int n = 1; n < 4; n++) {
75          addPerBondParameter(format("k%d%d", m, n));
76        }
77      }
78      for (int m = 1; m < 3; m++) {
79        addPerBondParameter(format("a%d", m));
80      }
81      for (AngleTorsion angleTorsion : angleTorsions) {
82        double[] constants = angleTorsion.getConstants();
83        DoubleArray parameters = new DoubleArray(0);
84        for (int m = 0; m < 2; m++) {
85          for (int n = 0; n < 3; n++) {
86            int index = (3 * m) + n;
87            parameters.append(constants[index] * OpenMM_KJPerKcal);
88          }
89        }
90        Atom[] atoms = angleTorsion.getAtomArray(true);
91        parameters.append(angleTorsion.angleType1.angle[0] * OpenMM_RadiansPerDegree);
92        parameters.append(angleTorsion.angleType2.angle[0] * OpenMM_RadiansPerDegree);
93  
94        IntArray particles = new IntArray(0);
95        for (int i = 0; i < 4; i++) {
96          particles.append(atoms[i].getArrayIndex());
97        }
98  
99        addBond(particles, parameters);
100       parameters.destroy();
101       particles.destroy();
102     }
103 
104     int forceGroup = angleTorsionPotentialEnergy.getForceGroup();
105     setForceGroup(forceGroup);
106     logger.info(format("  Angle-Torsions:                    %10d", angleTorsions.length));
107     logger.fine(format("   Force Group:                      %10d", forceGroup));
108   }
109 
110   /**
111    * Create a Dual Topology OpenMM Angle-Torsion Force.
112    *
113    * @param angleTorsionPotentialEnergy The AngleTorsionPotentialEnergy instance that contains the Angle-Torsions.
114    * @param topology                 The topology index for the OpenMM System.
115    * @param openMMDualTopologyEnergy The OpenMMDualTopologyEnergy instance.
116    */
117   public AngleTorsionForce(AngleTorsionPotentialEnergy angleTorsionPotentialEnergy,
118                            int topology, OpenMMDualTopologyEnergy openMMDualTopologyEnergy) {
119     super(4, AngleTorsion.angleTorsionForm());
120     AngleTorsion[] angleTorsions = angleTorsionPotentialEnergy.getAngleTorsionArray();
121     addGlobalParameter("phi1", 0);
122     addGlobalParameter("phi2", Math.PI);
123     addGlobalParameter("phi3", 0);
124     for (int m = 1; m < 3; m++) {
125       for (int n = 1; n < 4; n++) {
126         addPerBondParameter(format("k%d%d", m, n));
127       }
128     }
129     for (int m = 1; m < 3; m++) {
130       addPerBondParameter(format("a%d", m));
131     }
132 
133     double scaleDT = openMMDualTopologyEnergy.getTopologyScale(topology);
134 
135     for (AngleTorsion angleTorsion : angleTorsions) {
136       double scale = 1.0;
137       // Don't apply lambda scale to alchemical stretch-torsion
138       if (!angleTorsion.applyLambda()) {
139         scale = scaleDT;
140       }
141       double[] constants = angleTorsion.getConstants();
142       DoubleArray parameters = new DoubleArray(0);
143       for (int m = 0; m < 2; m++) {
144         for (int n = 0; n < 3; n++) {
145           int index = (3 * m) + n;
146           parameters.append(constants[index] * OpenMM_KJPerKcal * scale);
147         }
148       }
149       Atom[] atoms = angleTorsion.getAtomArray(true);
150       parameters.append(angleTorsion.angleType1.angle[0] * OpenMM_RadiansPerDegree);
151       parameters.append(angleTorsion.angleType2.angle[0] * OpenMM_RadiansPerDegree);
152 
153       IntArray particles = new IntArray(0);
154       for (int i = 0; i < 4; i++) {
155         int atomIndex = atoms[i].getArrayIndex();
156         atomIndex = openMMDualTopologyEnergy.mapToDualTopologyIndex(topology, atomIndex);
157         particles.append(atomIndex);
158       }
159 
160       addBond(particles, parameters);
161       parameters.destroy();
162       particles.destroy();
163     }
164 
165     int forceGroup = angleTorsionPotentialEnergy.getForceGroup();
166     setForceGroup(forceGroup);
167     logger.info(format("  Angle-Torsions:                    %10d", angleTorsions.length));
168     logger.fine(format("   Force Group:                      %10d", forceGroup));
169   }
170 
171   /**
172    * Convenience method to construct an OpenMM Angle-Torsion Force.
173    *
174    * @param openMMEnergy The OpenMM Energy instance that contains the angle-torsions.
175    * @return An OpenMM Stretch-Bend Force, or null if there are no angle-torsion.
176    */
177   public static Force constructForce(OpenMMEnergy openMMEnergy) {
178     AngleTorsionPotentialEnergy angleTorsionPotentialEnergy = openMMEnergy.getAngleTorsionPotentialEnergy();
179     if (angleTorsionPotentialEnergy == null) {
180       return null;
181     }
182     return new AngleTorsionForce(angleTorsionPotentialEnergy);
183   }
184 
185   /**
186    * Convenience method to construct a Dual Topology OpenMM Angle-Torsion Force.
187    *
188    * @param topology                 The topology index for the OpenMM System.
189    * @param openMMDualTopologyEnergy The OpenMMDualTopologyEnergy instance.
190    * @return An OpenMM Stretch-Bend Force, or null if there are no angle-torsion.
191    */
192   public static Force constructForce(int topology, OpenMMDualTopologyEnergy openMMDualTopologyEnergy) {
193     ForceFieldEnergy forceFieldEnergy = openMMDualTopologyEnergy.getForceFieldEnergy(topology);
194     AngleTorsionPotentialEnergy angleTorsionPotentialEnergy = forceFieldEnergy.getAngleTorsionPotentialEnergy();
195     if (angleTorsionPotentialEnergy == null) {
196       return null;
197     }
198     return new AngleTorsionForce(angleTorsionPotentialEnergy, topology, openMMDualTopologyEnergy);
199   }
200 
201   /**
202    * Update the Dual Topology Angle-Torsion Force.
203    *
204    * @param topology                 The topology index for the OpenMM System.
205    * @param openMMDualTopologyEnergy The OpenMMDualTopologyEnergy instance.
206    */
207   public void updateForce(int topology, OpenMMDualTopologyEnergy openMMDualTopologyEnergy) {
208     ForceFieldEnergy forceFieldEnergy = openMMDualTopologyEnergy.getForceFieldEnergy(topology);
209     AngleTorsionPotentialEnergy angleTorsionPotentialEnergy = forceFieldEnergy.getAngleTorsionPotentialEnergy();
210     // Check if this system has angle-torsions.
211     if (angleTorsionPotentialEnergy == null) {
212       return;
213     }
214     AngleTorsion[] angleTorsions = angleTorsionPotentialEnergy.getAngleTorsionArray();
215 
216     double scaleDT = openMMDualTopologyEnergy.getTopologyScale(topology);
217 
218     int atIndex = 0;
219     for (AngleTorsion angleTorsion : angleTorsions) {
220       double scale = 1.0;
221       // Don't apply lambda scale to alchemical stretch-torsion
222       if (!angleTorsion.applyLambda()) {
223         scale = scaleDT;
224       }
225       double[] constants = angleTorsion.getConstants();
226       DoubleArray parameters = new DoubleArray(0);
227       for (int m = 0; m < 2; m++) {
228         for (int n = 0; n < 3; n++) {
229           int index = (3 * m) + n;
230           parameters.append(constants[index] * OpenMM_KJPerKcal * scale);
231         }
232       }
233       Atom[] atoms = angleTorsion.getAtomArray(true);
234       parameters.append(angleTorsion.angleType1.angle[0] * OpenMM_RadiansPerDegree);
235       parameters.append(angleTorsion.angleType2.angle[0] * OpenMM_RadiansPerDegree);
236 
237       IntArray particles = new IntArray(0);
238       for (int i = 0; i < 4; i++) {
239         int atomIndex = atoms[i].getArrayIndex();
240         atomIndex = openMMDualTopologyEnergy.mapToDualTopologyIndex(topology, atomIndex);
241         particles.append(atomIndex);
242       }
243 
244       setBondParameters(atIndex++, particles, parameters);
245       parameters.destroy();
246       particles.destroy();
247     }
248 
249     updateParametersInContext(openMMDualTopologyEnergy.getContext());
250   }
251 }