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.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       OpenMM_System_addForce(pointer, force.getPointer());
107     }
108   }
109 
110   /**
111    * Add a particle to the system.
112    *
113    * @param mass The mass of the particle.
114    */
115   public void addParticle(double mass) {
116     OpenMM_System_addParticle(pointer, mass);
117   }
118 
119   /**
120    * Get the number of constraints in the system.
121    *
122    * @return The number of constraints in the system.
123    */
124   public int getNumConstraints() {
125     return OpenMM_System_getNumConstraints(pointer);
126   }
127 
128   /**
129    * Remove a force from the system.
130    *
131    * @param index The index of the force to remove.
132    */
133   public void removeForce(int index) {
134     OpenMM_System_removeForce(pointer, index);
135   }
136 
137   /**
138    * Set the default periodic box vectors.
139    *
140    * @param a The first vector.
141    * @param b The second vector.
142    * @param c The third vector.
143    */
144   public void setDefaultPeriodicBoxVectors(OpenMM_Vec3 a, OpenMM_Vec3 b, OpenMM_Vec3 c) {
145     OpenMM_System_setDefaultPeriodicBoxVectors(pointer, a, b, c);
146   }
147 
148 
149   /**
150    * Set the mass of a particle.
151    *
152    * @param index The index of the particle.
153    * @param mass  The mass of the particle.
154    */
155   public void setParticleMass(int index, double mass) {
156     OpenMM_System_setParticleMass(pointer, index, mass);
157   }
158 
159   /**
160    * Get the pointer to the system.
161    *
162    * @return The pointer to the system.
163    */
164   public PointerByReference getPointer() {
165     return pointer;
166   }
167 
168   /**
169    * Destroy the system.
170    */
171   public void destroy() {
172     if (pointer != null) {
173       OpenMM_System_destroy(pointer);
174       pointer = null;
175     }
176   }
177 
178 }