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.OutOfPlaneBend;
46  import ffx.potential.parameters.OutOfPlaneBendType;
47  import ffx.potential.terms.OutOfPlaneBendPotentialEnergy;
48  
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 Out-of-Plane Bend Force.
56   */
57  public class OutOfPlaneBendForce extends CustomCompoundBondForce {
58  
59    private static final Logger logger = Logger.getLogger(OutOfPlaneBendForce.class.getName());
60  
61    /**
62     * Create an Out-of-Plane Bend Force.
63     *
64     * @param outOfPlaneBendPotentialEnergy The OutOfPlaneBendPotentialEnergy instance that contains the out-of-plane bends.
65     */
66    public OutOfPlaneBendForce(OutOfPlaneBendPotentialEnergy outOfPlaneBendPotentialEnergy) {
67      super(4, outOfPlaneBendPotentialEnergy.getOutOfPlaneEnergyString());
68      OutOfPlaneBend[] outOfPlaneBends = outOfPlaneBendPotentialEnergy.getOutOfPlaneBendArray();
69      addPerBondParameter("k");
70      setName("OutOfPlaneBend");
71  
72      IntArray particles = new IntArray(0);
73      DoubleArray parameters = new DoubleArray(0);
74      for (OutOfPlaneBend outOfPlaneBend : outOfPlaneBends) {
75        OutOfPlaneBendType outOfPlaneBendType = outOfPlaneBend.outOfPlaneBendType;
76        int i1 = outOfPlaneBend.getAtom(0).getArrayIndex();
77        int i2 = outOfPlaneBend.getAtom(1).getArrayIndex();
78        int i3 = outOfPlaneBend.getAtom(2).getArrayIndex();
79        int i4 = outOfPlaneBend.getAtom(3).getArrayIndex();
80        double k = OpenMM_KJPerKcal * outOfPlaneBendType.forceConstant * outOfPlaneBendType.opBendUnit;
81        particles.append(i1);
82        particles.append(i2);
83        particles.append(i3);
84        particles.append(i4);
85        parameters.append(k);
86        addBond(particles, parameters);
87        particles.resize(0);
88        parameters.resize(0);
89      }
90      particles.destroy();
91      parameters.destroy();
92      int forceGroup = outOfPlaneBendPotentialEnergy.getForceGroup();
93      setForceGroup(forceGroup);
94      logger.info(format("  Out-of-Plane Bends:                %10d", outOfPlaneBends.length));
95      logger.fine(format("   Force Group:                      %10d", forceGroup));
96    }
97  
98    /**
99     * Create an Out-of-Plane Bend Force for Dual Topology.
100    *
101    * @param outOfPlaneBendPotentialEnergy The OutOfPlaneBendPotentialEnergy instance that contains the out-of-plane bends.
102    * @param topology                      The topology index for the OpenMM System.
103    * @param openMMDualTopologyEnergy      The OpenMMDualTopologyEnergy instance.
104    */
105   public OutOfPlaneBendForce(OutOfPlaneBendPotentialEnergy outOfPlaneBendPotentialEnergy,
106                              int topology, OpenMMDualTopologyEnergy openMMDualTopologyEnergy) {
107     super(4, outOfPlaneBendPotentialEnergy.getOutOfPlaneEnergyString());
108     OutOfPlaneBend[] outOfPlaneBends = outOfPlaneBendPotentialEnergy.getOutOfPlaneBendArray();
109     addPerBondParameter("k");
110     setName("OutOfPlaneBend");
111 
112     double scale = openMMDualTopologyEnergy.getTopologyScale(topology);
113 
114     IntArray particles = new IntArray(0);
115     DoubleArray parameters = new DoubleArray(0);
116     for (OutOfPlaneBend outOfPlaneBend : outOfPlaneBends) {
117       OutOfPlaneBendType outOfPlaneBendType = outOfPlaneBend.outOfPlaneBendType;
118       int i1 = outOfPlaneBend.getAtom(0).getArrayIndex();
119       int i2 = outOfPlaneBend.getAtom(1).getArrayIndex();
120       int i3 = outOfPlaneBend.getAtom(2).getArrayIndex();
121       int i4 = outOfPlaneBend.getAtom(3).getArrayIndex();
122       double k = OpenMM_KJPerKcal * outOfPlaneBendType.forceConstant * outOfPlaneBendType.opBendUnit;
123       // Don't apply lambda scale to alchemcial out-of-plane bend
124       if (!outOfPlaneBend.applyLambda()) {
125         k = k * scale;
126       }
127       i1 = openMMDualTopologyEnergy.mapToDualTopologyIndex(topology, i1);
128       i2 = openMMDualTopologyEnergy.mapToDualTopologyIndex(topology, i2);
129       i3 = openMMDualTopologyEnergy.mapToDualTopologyIndex(topology, i3);
130       i4 = openMMDualTopologyEnergy.mapToDualTopologyIndex(topology, i4);
131       particles.append(i1);
132       particles.append(i2);
133       particles.append(i3);
134       particles.append(i4);
135       parameters.append(k);
136       addBond(particles, parameters);
137       particles.resize(0);
138       parameters.resize(0);
139     }
140     particles.destroy();
141     parameters.destroy();
142     int forceGroup = outOfPlaneBendPotentialEnergy.getForceGroup();
143     setForceGroup(forceGroup);
144     logger.info(format("  Out-of-Plane Bends:                %10d", outOfPlaneBends.length));
145     logger.fine(format("   Force Group:                      %10d", forceGroup));
146   }
147 
148   /**
149    * Convenience method to construct an OpenMM Out-of-Plane Bend Force.
150    *
151    * @param openMMEnergy The OpenMM Energy instance that contains the out-of-plane bends.
152    * @return An OpenMM Out-of-Plane Bend Force, or null if there are no out-of-plane bends.
153    */
154   public static Force constructForce(OpenMMEnergy openMMEnergy) {
155     OutOfPlaneBendPotentialEnergy outOfPlaneBendPotentialEnergy = openMMEnergy.getOutOfPlaneBendPotentialEnergy();
156     if (outOfPlaneBendPotentialEnergy == null) {
157       return null;
158     }
159     return new OutOfPlaneBendForce(outOfPlaneBendPotentialEnergy);
160   }
161 
162   /**
163    * Convenience method to construct a Dual-Topology OpenMM Out-of-Plane Bend Force.
164    *
165    * @param topology                 The topology index for the OpenMM System.
166    * @param openMMDualTopologyEnergy The OpenMMDualTopologyEnergy instance.
167    * @return An OpenMM Out-of-Plane Bend Force, or null if there are no out-of-plane bends.
168    */
169   public static Force constructForce(int topology, OpenMMDualTopologyEnergy openMMDualTopologyEnergy) {
170     ForceFieldEnergy forceFieldEnergy = openMMDualTopologyEnergy.getForceFieldEnergy(topology);
171     OutOfPlaneBendPotentialEnergy outOfPlaneBendPotentialEnergy = forceFieldEnergy.getOutOfPlaneBendPotentialEnergy();
172     if (outOfPlaneBendPotentialEnergy == null) {
173       return null;
174     }
175     return new OutOfPlaneBendForce(outOfPlaneBendPotentialEnergy, topology, openMMDualTopologyEnergy);
176   }
177 
178   /**
179    * Update an existing angle force for the OpenMM System.
180    *
181    * @param openMMEnergy The OpenMM Energy instance that contains the angles.
182    */
183   public void updateForce(OpenMMEnergy openMMEnergy) {
184     OutOfPlaneBendPotentialEnergy outOfPlaneBendPotentialEnergy = openMMEnergy.getOutOfPlaneBendPotentialEnergy();
185     if (outOfPlaneBendPotentialEnergy == null) {
186       return;
187     }
188 
189     OutOfPlaneBend[] outOfPlaneBends = outOfPlaneBendPotentialEnergy.getOutOfPlaneBendArray();
190 
191     IntArray particles = new IntArray(0);
192     DoubleArray parameters = new DoubleArray(0);
193     int index = 0;
194     for (OutOfPlaneBend outOfPlaneBend : outOfPlaneBends) {
195       OutOfPlaneBendType outOfPlaneBendType = outOfPlaneBend.outOfPlaneBendType;
196       int i1 = outOfPlaneBend.getAtom(0).getArrayIndex();
197       int i2 = outOfPlaneBend.getAtom(1).getArrayIndex();
198       int i3 = outOfPlaneBend.getAtom(2).getArrayIndex();
199       int i4 = outOfPlaneBend.getAtom(3).getArrayIndex();
200       double k = OpenMM_KJPerKcal * outOfPlaneBendType.forceConstant * outOfPlaneBendType.opBendUnit;
201       particles.append(i1);
202       particles.append(i2);
203       particles.append(i3);
204       particles.append(i4);
205       parameters.append(k);
206       setBondParameters(index++, particles, parameters);
207       particles.resize(0);
208       parameters.resize(0);
209     }
210     particles.destroy();
211     parameters.destroy();
212 
213     updateParametersInContext(openMMEnergy.getContext());
214   }
215 
216   /**
217    * Update an existing angle force for the Dual-Topology OpenMM System.
218    *
219    * @param topology                 The topology index for the OpenMM System.
220    * @param openMMDualTopologyEnergy The OpenMMDualTopologyEnergy instance.
221    */
222   public void updateForce(int topology, OpenMMDualTopologyEnergy openMMDualTopologyEnergy) {
223     ForceFieldEnergy forceFieldEnergy = openMMDualTopologyEnergy.getForceFieldEnergy(topology);
224     OutOfPlaneBendPotentialEnergy forceFieldEnergyOutOfPlaneBend = forceFieldEnergy.getOutOfPlaneBendPotentialEnergy();
225     if (forceFieldEnergyOutOfPlaneBend == null) {
226       return;
227     }
228     OutOfPlaneBend[] outOfPlaneBends = forceFieldEnergyOutOfPlaneBend.getOutOfPlaneBendArray();
229     double scale = openMMDualTopologyEnergy.getTopologyScale(topology);
230 
231     IntArray particles = new IntArray(0);
232     DoubleArray parameters = new DoubleArray(0);
233     int index = 0;
234     for (OutOfPlaneBend outOfPlaneBend : outOfPlaneBends) {
235       OutOfPlaneBendType outOfPlaneBendType = outOfPlaneBend.outOfPlaneBendType;
236       int i1 = outOfPlaneBend.getAtom(0).getArrayIndex();
237       int i2 = outOfPlaneBend.getAtom(1).getArrayIndex();
238       int i3 = outOfPlaneBend.getAtom(2).getArrayIndex();
239       int i4 = outOfPlaneBend.getAtom(3).getArrayIndex();
240       double k = OpenMM_KJPerKcal * outOfPlaneBendType.forceConstant * outOfPlaneBendType.opBendUnit;
241       // Don't apply lambda scale to alchemcial out-of-plane bend
242       if (!outOfPlaneBend.applyLambda()) {
243         k = k * scale;
244       }
245       i1 = openMMDualTopologyEnergy.mapToDualTopologyIndex(topology, i1);
246       i2 = openMMDualTopologyEnergy.mapToDualTopologyIndex(topology, i2);
247       i3 = openMMDualTopologyEnergy.mapToDualTopologyIndex(topology, i3);
248       i4 = openMMDualTopologyEnergy.mapToDualTopologyIndex(topology, i4);
249       particles.append(i1);
250       particles.append(i2);
251       particles.append(i3);
252       particles.append(i4);
253       parameters.append(k);
254       setBondParameters(index++, particles, parameters);
255       particles.resize(0);
256       parameters.resize(0);
257     }
258     particles.destroy();
259     parameters.destroy();
260 
261     updateParametersInContext(openMMDualTopologyEnergy.getContext());
262   }
263 
264 }