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.openmm;
39  
40  import com.sun.jna.Pointer;
41  import com.sun.jna.ptr.PointerByReference;
42  
43  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Boolean.OpenMM_True;
44  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Force_getForceGroup;
45  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Force_getName;
46  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Force_setForceGroup;
47  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Force_setName;
48  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Force_usesPeriodicBoundaryConditions;
49  
50  /**
51   * Force objects apply forces to the particles in a System, or alter their behavior in other
52   * ways. This is an abstract class. Subclasses define particular forces.
53   * <p>
54   * More specifically, a Force object can do any or all of the following:
55   *
56   * <ul>
57   * <li>Add a contribution to the force on each particle</li>
58   * <li>Add a contribution to the potential energy of the System</li>
59   * <li>Modify the positions and velocities of particles at the start of each time step</li>
60   * <li>Define parameters which are stored in the Context and can be modified by the user</li>
61   * <li>Change the values of parameters defined by other Force objects at the start of each time step</li>
62   * </ul>
63   * <p>
64   * Forces may be organized into "force groups". This is used for multiple time step integration,
65   * and allows subsets of the Forces in a System to be evaluated at different times. By default,
66   * all Forces are in group 0. Call setForceGroup() to change this. Some Force subclasses may
67   * provide additional methods to further split their computations into multiple groups.  Be aware
68   * that particular Platforms may place restrictions on the use of force groups, such as requiring
69   * all nonbonded forces to be in the same group.
70   */
71  public abstract class Force {
72  
73    /**
74     * The forcePointer is allocated and deallocated by classes that extend OpenMMForce.
75     */
76    protected PointerByReference pointer = null;
77  
78    /**
79     * The forceIndex is returned by OpenMMSystem.addForce and is used to remove the force.
80     */
81    private int forceIndex = -1;
82  
83    /**
84     * Get the pointer to the OpenMM Force.
85     *
86     * @return The pointer to the OpenMM Force.
87     */
88    public PointerByReference getPointer() {
89      return pointer;
90    }
91  
92    /**
93     * Set the force group.
94     *
95     * @param forceGroup The force group.
96     */
97    public void setForceGroup(int forceGroup) {
98      OpenMM_Force_setForceGroup(pointer, forceGroup);
99    }
100 
101   /**
102    * Get the force group.
103    *
104    * @return The force group.
105    */
106   public int getForceGroup() {
107     return OpenMM_Force_getForceGroup(pointer);
108   }
109 
110   /**
111    * Set the name of the force.
112    *
113    * @param name The name of the force.
114    */
115   public void setName(String name) {
116     OpenMM_Force_setName(pointer, name);
117   }
118 
119   /**
120    * Get the name of the force.
121    *
122    * @return The name of the force.
123    */
124   public String getName() {
125     Pointer pointer = OpenMM_Force_getName(this.pointer);
126     if (pointer == null) {
127       return null;
128     }
129     return pointer.getString(0);
130   }
131 
132   /**
133    * Set the force index.
134    *
135    * @param forceIndex The force index.
136    */
137   public void setForceIndex(int forceIndex) {
138     this.forceIndex = forceIndex;
139   }
140 
141   /**
142    * Get the force index.
143    *
144    * @return The force index.
145    */
146   public int getForceIndex() {
147     return forceIndex;
148   }
149 
150   /**
151    * Check if the force use periodic boundary conditions. This is a virtual method
152    * that must be implemented by classes that extend OpenMMForce.
153    *
154    * @return True if the force uses periodic boundary conditions.
155    */
156   public boolean usesPeriodicBoundaryConditions() {
157     int pbc = OpenMM_Force_usesPeriodicBoundaryConditions(pointer);
158     return pbc == OpenMM_True;
159   }
160 
161 }