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.PointerByReference;
41  import edu.uiowa.jopenmm.OpenMM_Vec3;
42  
43  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_System_addConstraint;
44  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_System_addForce;
45  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_System_addParticle;
46  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_System_create;
47  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_System_destroy;
48  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_System_getNumConstraints;
49  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_System_removeForce;
50  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_System_setDefaultPeriodicBoxVectors;
51  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_System_setParticleMass;
52  
53  /**
54   * This class represents a molecular system.  The definition of a System involves
55   * four elements:
56   *
57   * <ul>
58   * <li>The set of particles in the system</li>
59   * <li>The forces acting on them</li>
60   * <li>Pairs of particles whose separation should be constrained to a fixed value</li>
61   * <li>For periodic systems, the dimensions of the periodic box</li>
62   * </ul>
63   *
64   * The particles and constraints are defined directly by the System object, while
65   * forces are defined by objects that extend the Force class.  After creating a
66   * System, call addParticle() once for each particle, addConstraint() for each constraint,
67   * and addForce() for each Force.
68   *
69   * In addition, particles may be designated as "virtual sites".  These are particles
70   * whose positions are computed automatically based on the positions of other particles.
71   * To define a virtual site, call setVirtualSite(), passing in a VirtualSite object
72   * that defines the rules for computing its position.
73   */
74  public class System {
75  
76    /**
77     * Context pointer.
78     */
79    PointerByReference pointer;
80  
81    /**
82     * Constructor.
83     */
84    public System() {
85      pointer = OpenMM_System_create();
86    }
87  
88    /**
89     * Add a constraint to the system.
90     *
91     * @param particle1 The first particle.
92     * @param particle2 The second particle.
93     * @param distance  The distance between the particles.
94     */
95    public void addConstraint(int particle1, int particle2, double distance) {
96      OpenMM_System_addConstraint(pointer, particle1, particle2, distance);
97    }
98  
99    /**
100    * Add a force to the system.
101    *
102    * @param force The force to add.
103    */
104   public void addForce(Force force) {
105     if (force != null) {
106       int forceIndex = OpenMM_System_addForce(pointer, force.getPointer());
107       force.setForceIndex(forceIndex);
108     }
109   }
110 
111   /**
112    * Add a particle to the system.
113    *
114    * @param mass The mass of the particle.
115    */
116   public void addParticle(double mass) {
117     OpenMM_System_addParticle(pointer, mass);
118   }
119 
120   /**
121    * Get the number of constraints in the system.
122    *
123    * @return The number of constraints in the system.
124    */
125   public int getNumConstraints() {
126     return OpenMM_System_getNumConstraints(pointer);
127   }
128 
129   /**
130    * Remove a force from the system.
131    *
132    * @param index The index of the force to remove.
133    */
134   public void removeForce(int index) {
135     OpenMM_System_removeForce(pointer, index);
136   }
137 
138   /**
139    * Set the default periodic box vectors.
140    *
141    * @param a The first vector.
142    * @param b The second vector.
143    * @param c The third vector.
144    */
145   public void setDefaultPeriodicBoxVectors(OpenMM_Vec3 a, OpenMM_Vec3 b, OpenMM_Vec3 c) {
146     OpenMM_System_setDefaultPeriodicBoxVectors(pointer, a, b, c);
147   }
148 
149 
150   /**
151    * Set the mass of a particle.
152    *
153    * @param index The index of the particle.
154    * @param mass  The mass of the particle.
155    */
156   public void setParticleMass(int index, double mass) {
157     OpenMM_System_setParticleMass(pointer, index, mass);
158   }
159 
160   /**
161    * Get the pointer to the system.
162    *
163    * @return The pointer to the system.
164    */
165   public PointerByReference getPointer() {
166     return pointer;
167   }
168 
169   /**
170    * Destroy the system.
171    */
172   public void destroy() {
173     if (pointer != null) {
174       OpenMM_System_destroy(pointer);
175       pointer = null;
176     }
177   }
178 
179 }