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.potential;
39  
40  import java.util.Arrays;
41  
42  import static java.lang.System.arraycopy;
43  
44  /**
45   * The current state of the molecular dynamics simulation.
46   */
47  public class SystemState {
48  
49    /**
50     * Number of dynamics variables. The length of x, v, a, aPrevious, gradient, and mass.
51     */
52    protected final int numberOfVariables;
53    /** Coordinates. */
54    protected final double[] x;
55    /** Velocities. */
56    protected final double[] v;
57    /** Accelerations. */
58    protected final double[] a;
59    /** Previous accelerations. */
60    protected final double[] aPrevious;
61    /** The gradient. */
62    protected final double[] gradient;
63    /** Mass for each degree of freedom. */
64    protected final double[] mass;
65    /** Current temperature. */
66    double temperature;
67    /** Current kinetic energy. */
68    double kineticEnergy;
69    /** Current potential energy. */
70    double potentialEnergy;
71  
72    /**
73     * Constructor for MDState.
74     *
75     * @param numberOfVariables The number of variables.
76     */
77    public SystemState(int numberOfVariables) {
78      this.numberOfVariables = numberOfVariables;
79      x = new double[numberOfVariables];
80      v = new double[numberOfVariables];
81      a = new double[numberOfVariables];
82      aPrevious = new double[numberOfVariables];
83      gradient = new double[numberOfVariables];
84      mass = new double[numberOfVariables];
85    }
86  
87    /**
88     * Get an unmodifiable view of the current state.
89     */
90    public UnmodifiableState getUnmodifiableState() {
91      return new UnmodifiableState(x, v, a, aPrevious, mass, gradient, kineticEnergy,
92          potentialEnergy, temperature);
93    }
94  
95    /**
96     * Get the number of variables.
97     *
98     * @return The number of variables.
99     */
100   public int getNumberOfVariables() {
101     return numberOfVariables;
102   }
103 
104   /**
105    * Revert the current state to the passed UnmodifiableMDState.
106    *
107    * @param state The state to revert to.
108    */
109   public void revertState(UnmodifiableState state) {
110     assert (state.x().length == numberOfVariables);
111     arraycopy(state.x(), 0, x, 0, numberOfVariables);
112     arraycopy(state.v(), 0, v, 0, numberOfVariables);
113     arraycopy(state.a(), 0, a, 0, numberOfVariables);
114     arraycopy(state.aPrevious(), 0, aPrevious, 0, numberOfVariables);
115     arraycopy(state.mass(), 0, mass, 0, numberOfVariables);
116     arraycopy(state.gradient(), 0, gradient, 0, numberOfVariables);
117     kineticEnergy = state.kineticEnergy();
118     potentialEnergy = state.potentialEnergy();
119     temperature = state.temperature();
120   }
121 
122   /**
123    * Set the mass of each degree of freedom.
124    *
125    * @param mass The mass of each degree of freedom.
126    */
127   public void setMass(double[] mass) {
128     assert (mass.length == numberOfVariables);
129     arraycopy(mass, 0, this.mass, 0, numberOfVariables);
130   }
131 
132   /**
133    * Set the coordinates via a copy of the passed array into the internal array.
134    *
135    * @param x The coordinates.
136    */
137   public void setCoordinates(double[] x) {
138     assert (x.length == numberOfVariables);
139     arraycopy(x, 0, this.x, 0, numberOfVariables);
140   }
141 
142   /**
143    * Set the velocities via a copy of the passed array into the internal array.
144    *
145    * @param v The velocities.
146    */
147   public void setVelocities(double[] v) {
148     assert (v.length == numberOfVariables);
149     arraycopy(v, 0, this.v, 0, numberOfVariables);
150   }
151 
152   /**
153    * Set the accelerations via a copy of the passed array into the internal array.
154    *
155    * @param a The accelerations.
156    */
157   public void setAccelerations(double[] a) {
158     assert (a.length == numberOfVariables);
159     arraycopy(a, 0, this.a, 0, numberOfVariables);
160   }
161 
162   /**
163    * Set the previous accelerations via a copy of the passed array into the internal array.
164    *
165    * @param aPrevious The previous accelerations.
166    */
167   public void setPreviousAccelerations(double[] aPrevious) {
168     assert (aPrevious.length == numberOfVariables);
169     arraycopy(aPrevious, 0, this.aPrevious, 0, numberOfVariables);
170   }
171 
172   /**
173    * Get a reference to the internal coordinates array.
174    *
175    * @return The coordinates.
176    */
177   public double[] x() {
178     return x;
179   }
180 
181   /**
182    * Get a reference to the internal velocities array.
183    *
184    * @return The velocities.
185    */
186   public double[] v() {
187     return v;
188   }
189 
190   /**
191    * Get a reference to the internal accelerations array.
192    *
193    * @return The accelerations.
194    */
195   public double[] a() {
196     return a;
197   }
198 
199   /**
200    * Get a reference to the internal previous accelerations array.
201    *
202    * @return The previous accelerations.
203    */
204   public double[] aPrevious() {
205     return aPrevious;
206   }
207 
208   /**
209    * Get a reference to the internal mass array.
210    *
211    * @return The mass.
212    */
213   public double[] getMass() {
214     return mass;
215   }
216 
217   /**
218    * Get a reference to the internal gradient array.
219    *
220    * @return The gradient.
221    */
222   public double[] gradient() {
223     return gradient;
224   }
225 
226   /**
227    * Get a copy of the internal coordinate array.
228    *
229    * @return The coordinates.
230    */
231   public double[] getCoordinatesCopy() {
232     return Arrays.copyOf(x, numberOfVariables);
233   }
234 
235   /**
236    * Copy the current accelerations to the previous accelerations.
237    */
238   public void copyAccelerationsToPrevious() {
239     arraycopy(a, 0, aPrevious, 0, numberOfVariables);
240   }
241 
242   /**
243    * Set the temperature.
244    *
245    * @param temperature The temperature.
246    */
247   public void setTemperature(double temperature) {
248     this.temperature = temperature;
249   }
250 
251   /**
252    * Set the kinetic energy.
253    *
254    * @param kineticEnergy The kinetic energy.
255    */
256   public void setKineticEnergy(double kineticEnergy) {
257     this.kineticEnergy = kineticEnergy;
258   }
259 
260   /**
261    * Set the potential energy.
262    *
263    * @param potentialEnergy The potential energy.
264    */
265   public void setPotentialEnergy(double potentialEnergy) {
266     this.potentialEnergy = potentialEnergy;
267   }
268 
269   /**
270    * Get the temperature.
271    */
272   public double getTemperature() {
273     return temperature;
274   }
275 
276   /**
277    * Get the kinetic energy.
278    */
279   public double getKineticEnergy() {
280     return kineticEnergy;
281   }
282 
283   /**
284    * Get the potential energy.
285    */
286   public double getPotentialEnergy() {
287     return potentialEnergy;
288   }
289 
290   /**
291    * Get the total energy as the sum of the kinetic and potential energies.
292    */
293   public double getTotalEnergy() {
294     return kineticEnergy + potentialEnergy;
295   }
296 
297 }