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 static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_MonteCarloBarostat_computeCurrentPressure;
41  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_MonteCarloBarostat_create;
42  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_MonteCarloBarostat_destroy;
43  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_MonteCarloBarostat_getDefaultPressure;
44  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_MonteCarloBarostat_getDefaultTemperature;
45  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_MonteCarloBarostat_getFrequency;
46  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_MonteCarloBarostat_getRandomNumberSeed;
47  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_MonteCarloBarostat_setDefaultPressure;
48  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_MonteCarloBarostat_setDefaultTemperature;
49  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_MonteCarloBarostat_setFrequency;
50  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_MonteCarloBarostat_setRandomNumberSeed;
51  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_MonteCarloBarostat_usesPeriodicBoundaryConditions;
52  
53  /**
54   * This class uses a Monte Carlo algorithm to adjust the size of the periodic box, simulating the
55   * effect of constant pressure.
56   * <p>
57   * This class assumes the simulation is also being run at constant temperature, and requires you
58   * to specify the system temperature (since it affects the acceptance probability for Monte Carlo
59   * moves).  It does not actually perform temperature regulation, however.  You must use another
60   * mechanism along with it to maintain the temperature, such as LangevinIntegrator or AndersenThermostat.
61   */
62  public class MonteCarloBarostat extends Force {
63  
64    /**
65     * Create a MonteCarloBarostat.
66     *
67     * @param pressure    the default pressure acting on the system (in bar)
68     * @param temperature the default temperature at which the system is being maintained (in Kelvin)
69     * @param frequency   the frequency at which Monte Carlo pressure changes should be attempted (in time steps)
70     */
71    public MonteCarloBarostat(double pressure, double temperature, int frequency) {
72      super(OpenMM_MonteCarloBarostat_create(pressure, temperature, frequency));
73    }
74  
75    /**
76     * Compute the instantaneous pressure of a system to which this barostat is applied.
77     * <p>
78     * The pressure is computed from the molecular virial, using a finite difference to
79     * calculate the derivative of potential energy with respect to volume.  For most systems
80     * in equilibrium, the time average of the instantaneous pressure should equal the
81     * pressure applied by the barostat.  Fluctuations around the average value can be
82     * extremely large, however, and it may take a very long simulation to accurately
83     * compute the average.
84     *
85     * @param context the Context for which to compute the current pressure
86     * @return the instantaneous pressure
87     */
88    public double computeCurrentPressure(Context context) {
89      return OpenMM_MonteCarloBarostat_computeCurrentPressure(pointer, context.getPointer());
90    }
91  
92    /**
93     * Destroy the force.
94     */
95    @Override
96    public void destroy() {
97      if (pointer != null) {
98        OpenMM_MonteCarloBarostat_destroy(pointer);
99        pointer = null;
100     }
101   }
102 
103   /**
104    * Get the default pressure acting on the system (in bar).
105    *
106    * @return the default pressure acting on the system, measured in bar.
107    */
108   public double getDefaultPressure() {
109     return OpenMM_MonteCarloBarostat_getDefaultPressure(pointer);
110   }
111 
112   /**
113    * Get the default temperature at which the system is being maintained, measured in Kelvin.
114    *
115    * @return the default temperature at which the system is being maintained, measured in Kelvin.
116    */
117   public double getDefaultTemperature() {
118     return OpenMM_MonteCarloBarostat_getDefaultTemperature(pointer);
119   }
120 
121   /**
122    * Get the frequency.
123    *
124    * @return The frequency.
125    */
126   public int getFrequency() {
127     return OpenMM_MonteCarloBarostat_getFrequency(pointer);
128   }
129 
130   /**
131    * Get the random number seed.
132    *
133    * @return The random number seed.
134    */
135   public int getRandomNumberSeed() {
136     return OpenMM_MonteCarloBarostat_getRandomNumberSeed(pointer);
137   }
138 
139   /**
140    * Set the default pressure acting on the system.  This will affect any new Contexts you create,
141    * but not ones that already exist.
142    *
143    * @param pressure the default pressure acting on the system, measured in bar.
144    */
145   public void setDefaultPressure(double pressure) {
146     OpenMM_MonteCarloBarostat_setDefaultPressure(pointer, pressure);
147   }
148 
149   /**
150    * Set the default temperature at which the system is being maintained.  This will affect any new Contexts you create,
151    * but not ones that already exist.
152    *
153    * @param temperature the system temperature, measured in Kelvin.
154    */
155   public void setDefaultTemperature(double temperature) {
156     OpenMM_MonteCarloBarostat_setDefaultTemperature(pointer, temperature);
157   }
158 
159   /**
160    * Set the frequency.
161    *
162    * @param frequency The frequency.
163    */
164   public void setFrequency(int frequency) {
165     OpenMM_MonteCarloBarostat_setFrequency(pointer, frequency);
166   }
167 
168   /**
169    * Set the random number seed.
170    *
171    * @param seed The random number seed.
172    */
173   public void setRandomNumberSeed(int seed) {
174     OpenMM_MonteCarloBarostat_setRandomNumberSeed(pointer, seed);
175   }
176 
177   /**
178    * Does the force use periodic boundary conditions?
179    *
180    * @return True if the force uses periodic boundary conditions.
181    */
182   public boolean usesPeriodicBoundaryConditions() {
183     int periodic = OpenMM_MonteCarloBarostat_usesPeriodicBoundaryConditions(pointer);
184     return periodic == 1;
185   }
186 
187 }