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