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.openmm;
39  
40  import com.sun.jna.ptr.DoubleByReference;
41  import com.sun.jna.ptr.IntByReference;
42  
43  import java.nio.DoubleBuffer;
44  import java.nio.IntBuffer;
45  
46  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Boolean.OpenMM_True;
47  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_HarmonicBondForce_addBond;
48  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_HarmonicBondForce_create;
49  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_HarmonicBondForce_destroy;
50  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_HarmonicBondForce_getBondParameters;
51  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_HarmonicBondForce_getNumBonds;
52  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_HarmonicBondForce_setBondParameters;
53  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_HarmonicBondForce_setUsesPeriodicBoundaryConditions;
54  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_HarmonicBondForce_updateParametersInContext;
55  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_HarmonicBondForce_usesPeriodicBoundaryConditions;
56  
57  /**
58   * This class implements an interaction between pairs of particles that varies harmonically with the distance
59   * between them.  To use it, create a HarmonicBondForce object then call addBond() once for each bond.  After
60   * a bond has been added, you can modify its force field parameters by calling setBondParameters().  This will
61   * have no effect on Contexts that already exist unless you call updateParametersInContext().
62   */
63  public class HarmonicBondForce extends Force {
64  
65    /**
66     * Create a new HarmonicBondForce.
67     */
68    public HarmonicBondForce() {
69      super(OpenMM_HarmonicBondForce_create());
70    }
71  
72    /**
73     * Add a bond term to the force field.
74     *
75     * @param i1     The index of the first particle connected by the bond.
76     * @param i2     The index of the second particle connected by the bond.
77     * @param length The equilibrium length of the bond, measured in nm.
78     * @param k      The harmonic force constant for the bond, measured in kJ/mol/nmˆ2.
79     * @return The index of the bond that was added.
80     */
81    public int addBond(int i1, int i2, double length, double k) {
82      return OpenMM_HarmonicBondForce_addBond(pointer, i1, i2, length, k);
83    }
84  
85    /**
86     * Destroy the force.
87     */
88    @Override
89    public void destroy() {
90      if (pointer != null) {
91        OpenMM_HarmonicBondForce_destroy(pointer);
92        pointer = null;
93      }
94    }
95  
96    /**
97     * Get the force field parameters for a bond term.
98     *
99     * @param index  The index of the bond for which to get parameters.
100    * @param i1     The index of the first particle connected by the bond (output).
101    * @param i2     The index of the second particle connected by the bond (output).
102    * @param length The equilibrium length of the bond, measured in nm (output).
103    * @param k      The harmonic force constant for the bond, measured in kJ/mol/nmˆ2 (output).
104    */
105   public void getBondParameters(int index, IntByReference i1, IntByReference i2,
106                                 DoubleByReference length, DoubleByReference k) {
107     OpenMM_HarmonicBondForce_getBondParameters(pointer, index, i1, i2, length, k);
108   }
109 
110   /**
111    * Get the force field parameters for a bond term.
112    *
113    * @param index  The index of the bond for which to get parameters.
114    * @param i1     The index of the first particle connected by the bond (output).
115    * @param i2     The index of the second particle connected by the bond (output).
116    * @param length The equilibrium length of the bond, measured in nm (output).
117    * @param k      The harmonic force constant for the bond, measured in kJ/mol/nmˆ2 (output).
118    */
119   public void getBondParameters(int index, IntBuffer i1, IntBuffer i2,
120                                 DoubleBuffer length, DoubleBuffer k) {
121     OpenMM_HarmonicBondForce_getBondParameters(pointer, index, i1, i2, length, k);
122   }
123 
124   /**
125    * Get the number of bonds.
126    *
127    * @return The number of bonds.
128    */
129   public int getNumBonds() {
130     return OpenMM_HarmonicBondForce_getNumBonds(pointer);
131   }
132 
133   /**
134    * Set the force field parameters for a bond term.
135    *
136    * @param i      The index of the bond for which to set parameters.
137    * @param i1     The index of the first particle connected by the bond.
138    * @param i2     The index of the second particle connected by the bond.
139    * @param length The equilibrium length of the bond, measured in nm.
140    * @param k      The harmonic force constant for the bond, measured in kJ/mol/nmˆ2.
141    */
142   public void setBondParameters(int i, int i1, int i2, double length, double k) {
143     OpenMM_HarmonicBondForce_setBondParameters(pointer, i, i1, i2, length, k);
144   }
145 
146   /**
147    * Set whether this force should apply periodic boundary conditions when calculating displacements.
148    *
149    * @param periodic If true, periodic boundary conditions will be applied.
150    */
151   public void setUsesPeriodicBoundaryConditions(boolean periodic) {
152     OpenMM_HarmonicBondForce_setUsesPeriodicBoundaryConditions(pointer, periodic ? 1 : 0);
153   }
154 
155   /**
156    * Update the parameters in the OpenMM Context.
157    *
158    * @param context The OpenMM Context.
159    */
160   public void updateParametersInContext(Context context) {
161     if (context.hasContextPointer()) {
162       OpenMM_HarmonicBondForce_updateParametersInContext(pointer, context.getPointer());
163     }
164   }
165 
166   /**
167    * Check if the force uses periodic boundary conditions.
168    *
169    * @return True if the force uses periodic boundary conditions.
170    */
171   @Override
172   public boolean usesPeriodicBoundaryConditions() {
173     int pbc = OpenMM_HarmonicBondForce_usesPeriodicBoundaryConditions(pointer);
174     return pbc == OpenMM_True;
175   }
176 }