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.Force;
41  import ffx.openmm.HarmonicBondForce;
42  import ffx.potential.ForceFieldEnergy;
43  import ffx.potential.bonded.UreyBradley;
44  import ffx.potential.parameters.UreyBradleyType;
45  import ffx.potential.terms.UreyBradleyPotentialEnergy;
46  
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 java.lang.String.format;
52  
53  /**
54   * Urey-Bradley Force.
55   */
56  public class UreyBradleyForce extends HarmonicBondForce {
57  
58    private static final Logger logger = Logger.getLogger(UreyBradleyForce.class.getName());
59  
60    /**
61     * Urey-Bradly Force constructor.
62     *
63     * @param ureyBradleyPotentialEnergy The UreyBradleyPotentialEnergy instance.
64     */
65    public UreyBradleyForce(UreyBradleyPotentialEnergy ureyBradleyPotentialEnergy) {
66      UreyBradley[] ureyBradleys = ureyBradleyPotentialEnergy.getUreyBradleyArray();
67      double kParameterConversion = OpenMM_KJPerKcal / (OpenMM_NmPerAngstrom * OpenMM_NmPerAngstrom);
68  
69      for (UreyBradley ureyBradley : ureyBradleys) {
70        int i1 = ureyBradley.getAtom(0).getArrayIndex();
71        int i2 = ureyBradley.getAtom(2).getArrayIndex();
72        UreyBradleyType ureyBradleyType = ureyBradley.ureyBradleyType;
73        double length = ureyBradleyType.distance * OpenMM_NmPerAngstrom;
74        // The implementation of UreyBradley in FFX & Tinker: k x^2
75        // The implementation of Harmonic Bond Force in OpenMM:  k x^2 / 2
76        double k = 2.0 * ureyBradleyType.forceConstant * ureyBradleyType.ureyUnit * kParameterConversion;
77        addBond(i1, i2, length, k);
78      }
79  
80      int forceGroup = ureyBradleyPotentialEnergy.getForceGroup();
81      setForceGroup(forceGroup);
82      logger.info(format("  Urey-Bradleys:                     %10d", ureyBradleys.length));
83      logger.fine(format("   Force Group:                      %10d", forceGroup));
84    }
85  
86    /**
87     * Urey-Bradly Force constructor.
88     *
89     * @param topology                 The topology index for the OpenMM System.
90     * @param openMMDualTopologyEnergy The OpenMMDualTopologyEnergy instance.
91     */
92    public UreyBradleyForce(UreyBradleyPotentialEnergy ureyBradleyPotentialEnergy,
93                            int topology, OpenMMDualTopologyEnergy openMMDualTopologyEnergy) {
94      UreyBradley[] ureyBradleys = ureyBradleyPotentialEnergy.getUreyBradleyArray();
95      double scale = openMMDualTopologyEnergy.getTopologyScale(topology);
96      double kParameterConversion = OpenMM_KJPerKcal / (OpenMM_NmPerAngstrom * OpenMM_NmPerAngstrom);
97  
98      for (UreyBradley ureyBradley : ureyBradleys) {
99        int i1 = ureyBradley.getAtom(0).getArrayIndex();
100       int i2 = ureyBradley.getAtom(2).getArrayIndex();
101       UreyBradleyType ureyBradleyType = ureyBradley.ureyBradleyType;
102       double length = ureyBradleyType.distance * OpenMM_NmPerAngstrom;
103       // The implementation of UreyBradley in FFX & Tinker: k x^2
104       // The implementation of Harmonic Bond Force in OpenMM:  k x^2 / 2
105       double k = 2.0 * ureyBradleyType.forceConstant * ureyBradleyType.ureyUnit * kParameterConversion;
106       // Don't apply lambda scale to alchemical Urey-Bradley
107       if (!ureyBradley.applyLambda()) {
108         k = k * scale;
109       }
110       i1 = openMMDualTopologyEnergy.mapToDualTopologyIndex(topology, i1);
111       i2 = openMMDualTopologyEnergy.mapToDualTopologyIndex(topology, i2);
112       addBond(i1, i2, length, k);
113     }
114 
115     int forceGroup = ureyBradleyPotentialEnergy.getForceGroup();
116     setForceGroup(forceGroup);
117     logger.info(format("  Urey-Bradleys:                     %10d", ureyBradleys.length));
118     logger.fine(format("   Force Group:                      %10d", forceGroup));
119   }
120 
121   /**
122    * Convenience method to construct an OpenMM Urey-Bradley Force.
123    *
124    * @param openMMEnergy The OpenMM Energy instance that contains the angles.
125    * @return An OpenMM Urey-Bradley Force, or null if there are no Urey-Bradley.
126    */
127   public static Force constructForce(OpenMMEnergy openMMEnergy) {
128     UreyBradleyPotentialEnergy ureyBradleyPotentialEnergy = openMMEnergy.getUreyBradleyPotentialEnergy();
129     if (ureyBradleyPotentialEnergy == null) {
130       return null;
131     }
132     return new UreyBradleyForce(ureyBradleyPotentialEnergy);
133   }
134 
135   /**
136    * Convenience method to construct an OpenMM Urey-Bradley Force.
137    *
138    * @param topology                 The topology index for the OpenMM System.
139    * @param openMMDualTopologyEnergy The OpenMMDualTopologyEnergy instance.
140    */
141   public static Force constructForce(int topology, OpenMMDualTopologyEnergy openMMDualTopologyEnergy) {
142     ForceFieldEnergy forceFieldEnergy = openMMDualTopologyEnergy.getForceFieldEnergy(topology);
143     UreyBradleyPotentialEnergy ureyBradleyPotentialEnergy = forceFieldEnergy.getUreyBradleyPotentialEnergy();
144     if (ureyBradleyPotentialEnergy == null) {
145       return null;
146     }
147     return new UreyBradleyForce(ureyBradleyPotentialEnergy, topology, openMMDualTopologyEnergy);
148   }
149 
150   /**
151    * Update the Urey-Bradley parameters in the OpenMM Context.
152    *
153    * @param openMMEnergy The OpenMM Energy instance that contains the Urey-Bradley terms.
154    */
155   public void updateForce(OpenMMEnergy openMMEnergy) {
156     UreyBradleyPotentialEnergy ureyBradleyPotentialEnergy = openMMEnergy.getUreyBradleyPotentialEnergy();
157     if (ureyBradleyPotentialEnergy == null) {
158       return;
159     }
160     UreyBradley[] ureyBradleys = ureyBradleyPotentialEnergy.getUreyBradleyArray();
161     double kParameterConversion = OpenMM_KJPerKcal / (OpenMM_NmPerAngstrom * OpenMM_NmPerAngstrom);
162 
163     int index = 0;
164     for (UreyBradley ureyBradley : ureyBradleys) {
165       int i1 = ureyBradley.getAtom(0).getArrayIndex();
166       int i2 = ureyBradley.getAtom(2).getArrayIndex();
167       UreyBradleyType ureyBradleyType = ureyBradley.ureyBradleyType;
168       double length = ureyBradleyType.distance * OpenMM_NmPerAngstrom;
169       // The implementation of UreyBradley in FFX & Tinker: k x^2
170       // The implementation of Harmonic Bond Force in OpenMM:  k x^2 / 2
171       double k = 2.0 * ureyBradleyType.forceConstant * ureyBradleyType.ureyUnit * kParameterConversion;
172       setBondParameters(index++, i1, i2, length, k);
173     }
174 
175     updateParametersInContext(openMMEnergy.getContext());
176   }
177 
178   /**
179    * Update the Urey-Bradley parameters in the OpenMM Context.
180    *
181    * @param topology                 The topology index for the OpenMM System.
182    * @param openMMDualTopologyEnergy The OpenMMDualTopologyEnergy instance.
183    */
184   public void updateForce(int topology, OpenMMDualTopologyEnergy openMMDualTopologyEnergy) {
185     ForceFieldEnergy forceFieldEnergy = openMMDualTopologyEnergy.getForceFieldEnergy(topology);
186     UreyBradleyPotentialEnergy forceFieldEnergyUreyBradley =
187         forceFieldEnergy.getUreyBradleyPotentialEnergy();
188     if (forceFieldEnergyUreyBradley == null) {
189       return;
190     }
191     UreyBradley[] ureyBradleys = forceFieldEnergyUreyBradley.getUreyBradleyArray();
192     double scale = openMMDualTopologyEnergy.getTopologyScale(topology);
193     double kParameterConversion = OpenMM_KJPerKcal / (OpenMM_NmPerAngstrom * OpenMM_NmPerAngstrom);
194 
195     int index = 0;
196     for (UreyBradley ureyBradley : ureyBradleys) {
197       int i1 = ureyBradley.getAtom(0).getArrayIndex();
198       int i2 = ureyBradley.getAtom(2).getArrayIndex();
199       UreyBradleyType ureyBradleyType = ureyBradley.ureyBradleyType;
200       double length = ureyBradleyType.distance * OpenMM_NmPerAngstrom;
201       // The implementation of UreyBradley in FFX & Tinker: k x^2
202       // The implementation of Harmonic Bond Force in OpenMM:  k x^2 / 2
203       double k = 2.0 * ureyBradleyType.forceConstant * ureyBradleyType.ureyUnit * kParameterConversion;
204       // Don't apply lambda scale to alchemical Urey-Bradley
205       if (!ureyBradley.applyLambda()) {
206         k = k * scale;
207       }
208       i1 = openMMDualTopologyEnergy.mapToDualTopologyIndex(topology, i1);
209       i2 = openMMDualTopologyEnergy.mapToDualTopologyIndex(topology, i2);
210       setBondParameters(index++, i1, i2, length, k);
211     }
212 
213     updateParametersInContext(openMMDualTopologyEnergy.getContext());
214   }
215 
216 }