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_VariableLangevinIntegrator_create;
41 import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_VariableLangevinIntegrator_destroy;
42 import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_VariableLangevinIntegrator_getErrorTolerance;
43 import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_VariableLangevinIntegrator_getFriction;
44 import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_VariableLangevinIntegrator_getMaximumStepSize;
45 import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_VariableLangevinIntegrator_getRandomNumberSeed;
46 import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_VariableLangevinIntegrator_getTemperature;
47 import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_VariableLangevinIntegrator_setErrorTolerance;
48 import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_VariableLangevinIntegrator_setFriction;
49 import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_VariableLangevinIntegrator_setMaximumStepSize;
50 import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_VariableLangevinIntegrator_setRandomNumberSeed;
51 import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_VariableLangevinIntegrator_setTemperature;
52 import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_VariableLangevinIntegrator_step;
53 import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_VariableLangevinIntegrator_stepTo;
54
55 /**
56 * This class implements a Langevin integrator with variable time stepping.
57 * The integrator automatically adjusts the step size to maintain a specified
58 * error tolerance, making it suitable for systems with widely varying time scales.
59 * <p>
60 * The variable step size algorithm monitors the local truncation error and
61 * adjusts the step size accordingly. This can lead to more efficient integration
62 * for systems where different parts evolve on different time scales, such as
63 * systems with both fast vibrations and slow conformational changes.
64 */
65 public class VariableLangevinIntegrator extends Integrator {
66
67 /**
68 * Create a VariableLangevinIntegrator.
69 *
70 * @param temperature The temperature of the heat bath (in Kelvin).
71 * @param frictionCoeff The friction coefficient which couples the system to the heat bath (in 1/ps).
72 * @param errorTol The error tolerance for adaptive step sizing.
73 */
74 public VariableLangevinIntegrator(double temperature, double frictionCoeff, double errorTol) {
75 super(OpenMM_VariableLangevinIntegrator_create(temperature, frictionCoeff, errorTol));
76 }
77
78 /**
79 * Destroy the integrator.
80 */
81 @Override
82 public void destroy() {
83 if (pointer != null) {
84 OpenMM_VariableLangevinIntegrator_destroy(pointer);
85 pointer = null;
86 }
87 }
88
89 /**
90 * Get the error tolerance for adaptive step sizing.
91 *
92 * @return The error tolerance.
93 */
94 public double getErrorTolerance() {
95 return OpenMM_VariableLangevinIntegrator_getErrorTolerance(pointer);
96 }
97
98 /**
99 * Get the friction coefficient which determines how strongly the system is coupled to
100 * the heat bath (in 1/ps).
101 *
102 * @return The friction coefficient.
103 */
104 public double getFriction() {
105 return OpenMM_VariableLangevinIntegrator_getFriction(pointer);
106 }
107
108 /**
109 * Get the maximum step size the integrator is allowed to use (in ps).
110 *
111 * @return The maximum step size.
112 */
113 public double getMaximumStepSize() {
114 return OpenMM_VariableLangevinIntegrator_getMaximumStepSize(pointer);
115 }
116
117 /**
118 * Get the random number seed. See setRandomNumberSeed() for details.
119 *
120 * @return The random number seed.
121 */
122 public int getRandomNumberSeed() {
123 return OpenMM_VariableLangevinIntegrator_getRandomNumberSeed(pointer);
124 }
125
126 /**
127 * Get the temperature of the heat bath (in Kelvin).
128 *
129 * @return The temperature of the heat bath.
130 */
131 public double getTemperature() {
132 return OpenMM_VariableLangevinIntegrator_getTemperature(pointer);
133 }
134
135 /**
136 * Set the error tolerance for adaptive step sizing.
137 *
138 * @param tol The error tolerance.
139 */
140 public void setErrorTolerance(double tol) {
141 OpenMM_VariableLangevinIntegrator_setErrorTolerance(pointer, tol);
142 }
143
144 /**
145 * Set the friction coefficient which determines how strongly the system is coupled to
146 * the heat bath (in 1/ps).
147 *
148 * @param coeff The friction coefficient.
149 */
150 public void setFriction(double coeff) {
151 OpenMM_VariableLangevinIntegrator_setFriction(pointer, coeff);
152 }
153
154 /**
155 * Set the maximum step size the integrator is allowed to use (in ps).
156 *
157 * @param size The maximum step size.
158 */
159 public void setMaximumStepSize(double size) {
160 OpenMM_VariableLangevinIntegrator_setMaximumStepSize(pointer, size);
161 }
162
163 /**
164 * Set the random number seed. The precise meaning of this parameter is undefined, and is left up
165 * to each Platform to interpret in an appropriate way. It is guaranteed that if two simulations
166 * are run with different random number seeds, the sequence of random numbers will be different.
167 * On the other hand, no guarantees are made about the behavior of simulations that use the same seed.
168 * In particular, Platforms are permitted to use non-deterministic algorithms which produce different
169 * results on successive runs, even if those runs were initialized identically.
170 * <p>
171 * If seed is set to 0 (which is the default value assigned), a unique seed is chosen when a Context
172 * is created from this Force. This is done to ensure that each Context receives unique random seeds
173 * without you needing to set them explicitly.
174 *
175 * @param seed The random number seed.
176 */
177 public void setRandomNumberSeed(int seed) {
178 OpenMM_VariableLangevinIntegrator_setRandomNumberSeed(pointer, seed);
179 }
180
181 /**
182 * Set the temperature of the heat bath (in Kelvin).
183 *
184 * @param temp The temperature of the heat bath.
185 */
186 public void setTemperature(double temp) {
187 OpenMM_VariableLangevinIntegrator_setTemperature(pointer, temp);
188 }
189
190 /**
191 * Advance a simulation through time by taking a series of time steps.
192 *
193 * @param steps The number of time steps to take.
194 */
195 public void step(int steps) {
196 OpenMM_VariableLangevinIntegrator_step(pointer, steps);
197 }
198
199 /**
200 * Advance the simulation by integrating until a specified time is reached.
201 *
202 * @param time The time to which the simulation should be advanced (in ps).
203 */
204 public void stepTo(double time) {
205 OpenMM_VariableLangevinIntegrator_stepTo(pointer, time);
206 }
207 }