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_State_destroy;
44  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_State_getDataTypes;
45  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_State_getForces;
46  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_State_getKineticEnergy;
47  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_State_getPeriodicBoxVectors;
48  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_State_getPeriodicBoxVolume;
49  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_State_getPositions;
50  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_State_getPotentialEnergy;
51  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_State_getTime;
52  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_State_getVelocities;
53  
54  /**
55   * A State object records a snapshot of the current state of a simulation at a point in time.
56   * You create it by calling getState() on a Context.
57   *
58   * When a State is created, you specify what information should be stored in it.  This saves
59   * time and memory by only copying in the information that you actually want.  This is especially
60   * important for forces and energies, since they may need to be calculated.  If you query a
61   * State object for a piece of information which is not available (because it was not requested
62   * when the State was created), it will throw an exception.
63   */
64  public class State {
65  
66    /**
67     * State pointer.
68     */
69    PointerByReference pointer;
70  
71    /**
72     * Constructor.
73     *
74     * @param pointer Pointer to the state returned by a Context.
75     */
76    public State(PointerByReference pointer) {
77      this.pointer = pointer;
78    }
79  
80    /**
81     * Get the pointer to the state.
82     *
83     * @return The pointer to the state.
84     */
85    public PointerByReference getPointer() {
86      return pointer;
87    }
88  
89    /**
90     * Get the data types.
91     *
92     * @return The data types.
93     */
94    public int getDataTypes() {
95      return OpenMM_State_getDataTypes(pointer);
96    }
97  
98    /**
99     * Get the positions.
100    *
101    * @return The positions.
102    */
103   public double[] getPositions() {
104     Vec3Array positions = new Vec3Array(OpenMM_State_getPositions(pointer));
105     return positions.getArray();
106   }
107 
108   /**
109    * Get the velocities.
110    *
111    * @return The velocities.
112    */
113   public double[] getVelocities() {
114     Vec3Array velocities = new Vec3Array(OpenMM_State_getVelocities(pointer));
115     return velocities.getArray();
116   }
117 
118   /**
119    * Get the forces.
120    *
121    * @return The forces.
122    */
123   public double[] getForces() {
124     Vec3Array forces = new Vec3Array(OpenMM_State_getForces(pointer));
125     return forces.getArray();
126   }
127 
128   /**
129    * Get the time.
130    *
131    * @return The time.
132    */
133   public double getTime() {
134     return OpenMM_State_getTime(pointer);
135   }
136 
137   /**
138    * Get the kinetic energy.
139    *
140    * @return The kinetic energy.
141    */
142   public double getKineticEnergy() {
143     return OpenMM_State_getKineticEnergy(pointer);
144   }
145 
146   /**
147    * Get the potential energy.
148    *
149    * @return The potential energy.
150    */
151   public double getPotentialEnergy() {
152     return OpenMM_State_getPotentialEnergy(pointer);
153   }
154 
155   /**
156    * Get the periodic box vectors.
157    *
158    * @return The periodic box vectors.
159    */
160   public double[][] getPeriodicBoxVectors() {
161     OpenMM_Vec3 a = new OpenMM_Vec3();
162     OpenMM_Vec3 b = new OpenMM_Vec3();
163     OpenMM_Vec3 c = new OpenMM_Vec3();
164     OpenMM_State_getPeriodicBoxVectors(pointer, a, b, c);
165     double[][] latticeVectors = new double[3][3];
166     latticeVectors[0][0] = a.x;
167     latticeVectors[0][1] = a.y;
168     latticeVectors[0][2] = a.z;
169     latticeVectors[1][0] = b.x;
170     latticeVectors[1][1] = b.y;
171     latticeVectors[1][2] = b.z;
172     latticeVectors[2][0] = c.x;
173     latticeVectors[2][1] = c.y;
174     latticeVectors[2][2] = c.z;
175     return latticeVectors;
176   }
177 
178   /**
179    * Get the periodic box volume.
180    *
181    * @return The periodic box volume.
182    */
183   public double getPeriodicBoxVolume() {
184     return OpenMM_State_getPeriodicBoxVolume(pointer);
185   }
186 
187   /**
188    * Destroy the state.
189    */
190   public void destroy() {
191     if (pointer != null) {
192       OpenMM_State_destroy(pointer);
193       pointer = null;
194     }
195   }
196 
197 }