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.Force;
41  import ffx.openmm.HarmonicBondForce;
42  import ffx.potential.bonded.UreyBradley;
43  import ffx.potential.parameters.UreyBradleyType;
44  
45  import java.util.logging.Level;
46  import java.util.logging.Logger;
47  
48  import static edu.uiowa.jopenmm.OpenMMAmoebaLibrary.OpenMM_KJPerKcal;
49  import static edu.uiowa.jopenmm.OpenMMAmoebaLibrary.OpenMM_NmPerAngstrom;
50  import static java.lang.String.format;
51  
52  /**
53   * Urey-Bradley Force.
54   */
55  public class UreyBradleyForce extends HarmonicBondForce {
56  
57    private static final Logger logger = Logger.getLogger(UreyBradleyForce.class.getName());
58  
59    /**
60     * Urey-Bradly Force constructor.
61     *
62     * @param openMMEnergy The OpenMMEnergy instance that contains the Urey-Bradley terms.
63     */
64    public UreyBradleyForce(OpenMMEnergy openMMEnergy) {
65      UreyBradley[] ureyBradleys = openMMEnergy.getUreyBradleys();
66      if (ureyBradleys == null || ureyBradleys.length < 1) {
67        return;
68      }
69  
70      double kParameterConversion = OpenMM_KJPerKcal / (OpenMM_NmPerAngstrom * OpenMM_NmPerAngstrom);
71  
72      for (UreyBradley ureyBradley : ureyBradleys) {
73        int i1 = ureyBradley.getAtom(0).getXyzIndex() - 1;
74        int i2 = ureyBradley.getAtom(2).getXyzIndex() - 1;
75        UreyBradleyType ureyBradleyType = ureyBradley.ureyBradleyType;
76        double length = ureyBradleyType.distance * OpenMM_NmPerAngstrom;
77        // The implementation of UreyBradley in FFX & Tinker: k x^2
78        // The implementation of Harmonic Bond Force in OpenMM:  k x^2 / 2
79        double k = 2.0 * ureyBradleyType.forceConstant * ureyBradleyType.ureyUnit * kParameterConversion;
80        addBond(i1, i2, length, k);
81      }
82  
83      int forceGroup = openMMEnergy.getMolecularAssembly().getForceField().getInteger("UREY_BRADLEY_FORCE", 0);
84      setForceGroup(forceGroup);
85      logger.log(Level.INFO, format("  Urey-Bradleys \t%6d\t\t%1d", ureyBradleys.length, forceGroup));
86    }
87  
88    /**
89     * Convenience method to construct an OpenMM Urey-Bradley Force.
90     *
91     * @param openMMEnergy The OpenMM Energy instance that contains the angles.
92     * @return An OpenMM Urey-Bradley Force, or null if there are no Urey-Bradley.
93     */
94    public static Force constructForce(OpenMMEnergy openMMEnergy) {
95      UreyBradley[] ureyBradleys = openMMEnergy.getUreyBradleys();
96      if (ureyBradleys == null || ureyBradleys.length < 1) {
97        return null;
98      }
99      return new UreyBradleyForce(openMMEnergy);
100   }
101 
102   /**
103    * Update the Urey-Bradley parameters in the OpenMM Context.
104    *
105    * @param openMMEnergy The OpenMM Energy instance that contains the Urey-Bradley terms.
106    */
107   public void updateForce(OpenMMEnergy openMMEnergy) {
108     UreyBradley[] ureyBradleys = openMMEnergy.getUreyBradleys();
109     if (ureyBradleys == null || ureyBradleys.length < 1) {
110       return;
111     }
112 
113     double kParameterConversion = OpenMM_KJPerKcal / (OpenMM_NmPerAngstrom * OpenMM_NmPerAngstrom);
114 
115     int index = 0;
116     for (UreyBradley ureyBradley : ureyBradleys) {
117       int i1 = ureyBradley.getAtom(0).getXyzIndex() - 1;
118       int i2 = ureyBradley.getAtom(2).getXyzIndex() - 1;
119       UreyBradleyType ureyBradleyType = ureyBradley.ureyBradleyType;
120       double length = ureyBradleyType.distance * OpenMM_NmPerAngstrom;
121       // The implementation of UreyBradley in FFX & Tinker: k x^2
122       // The implementation of Harmonic Bond Force in OpenMM:  k x^2 / 2
123       double k = 2.0 * ureyBradleyType.forceConstant * ureyBradleyType.ureyUnit * kParameterConversion;
124       setBondParameters(index++, i1, i2, length, k);
125     }
126 
127     updateParametersInContext(openMMEnergy.getContext());
128   }
129 
130 }