-
Notifications
You must be signed in to change notification settings - Fork 18
/
integrators.rs
289 lines (250 loc) · 9.48 KB
/
integrators.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Lumol, an extensible molecular simulation engine
// Copyright (C) 2015-2016 G. Fraux — BSD license
use types::{Vector3D, Matrix3, One, Zero};
use sys::System;
/// The `Integrator` trait define integrator interface for molecular dynamics.
/// An integrator is an algorithm responsible for propagating the equations of
/// motion in the system.
pub trait Integrator {
/// Setup the integrator. This function is called once by every simulation
/// run.
fn setup(&mut self, _: &System) {}
/// Integrate the equations of motion. This is called at every step of the
/// simulation.
fn integrate(&mut self, system: &mut System);
}
/// Velocity-Verlet integrator. This one is reversible and symplectic.
pub struct VelocityVerlet {
/// Timestep for the integrator
timestep: f64,
/// Storing the accelerations
accelerations: Vec<Vector3D>
}
impl VelocityVerlet {
/// Create a new integrator with a timestep of `timestep`.
pub fn new(timestep: f64) -> VelocityVerlet {
VelocityVerlet{
timestep: timestep,
accelerations: Vec::new(),
}
}
}
impl Integrator for VelocityVerlet {
fn setup(&mut self, system: &System) {
self.accelerations = vec![Vector3D::zero(); system.size()];
}
fn integrate(&mut self, system: &mut System) {
let dt = self.timestep;
// Update velocities at t + ∆t/2 and positions at t + ∆t
for (i, part) in system.iter_mut().enumerate() {
part.velocity += 0.5 * dt * self.accelerations[i];
part.position += part.velocity * dt;
}
let forces = system.forces();
// Update accelerations at t + ∆t and velocities at t + ∆t
for (i, part) in system.iter_mut().enumerate() {
self.accelerations[i] = forces[i] / part.mass;
part.velocity += 0.5 * dt * self.accelerations[i];
}
}
}
/******************************************************************************/
/// Verlet integrator. This one is reversible and symplectic.
pub struct Verlet {
/// Timestep for the integrator
timestep: f64,
/// Previous positions
prevpos: Vec<Vector3D>,
}
impl Verlet {
/// Create a new integrator with a timestep of `timestep`.
pub fn new(timestep: f64) -> Verlet {
Verlet{
timestep: timestep,
prevpos: Vec::new(),
}
}
}
impl Integrator for Verlet {
fn setup(&mut self, system: &System) {
self.prevpos = vec![Vector3D::zero(); system.size()];
let dt = self.timestep;
// Approximate the positions at t - ∆t
for (i, part) in system.iter().enumerate() {
self.prevpos[i] = part.position - part.velocity * dt;
}
}
fn integrate(&mut self, system: &mut System) {
let dt = self.timestep;
let dt2 = self.timestep * self.timestep;
let forces = system.forces();
for (i, part) in system.iter_mut().enumerate() {
// Save positions at t
let tmp = part.position;
// Update positions at t + ∆t
let position = 2.0 * tmp - self.prevpos[i] + dt2/part.mass * forces[i];
// Update velocities at t
let velocity = (position - self.prevpos[i]) / (2.0 * dt);
part.position = position;
part.velocity = velocity;
// Update saved position
self.prevpos[i] = tmp;
}
}
}
/******************************************************************************/
/// Leap-frog integrator. This one is reversible and symplectic.
pub struct LeapFrog {
/// Timestep for the integrator
timestep: f64,
/// Storing the accelerations
accelerations: Vec<Vector3D>
}
impl LeapFrog {
/// Create a new integrator with a timestep of `timestep`.
pub fn new(timestep: f64) -> LeapFrog {
LeapFrog{
timestep: timestep,
accelerations: Vec::new(),
}
}
}
impl Integrator for LeapFrog {
fn setup(&mut self, system: &System) {
self.accelerations = vec![Vector3D::zero(); system.size()];
}
fn integrate(&mut self, system: &mut System) {
let dt = self.timestep;
let dt2 = self.timestep * self.timestep;
for (i, part) in system.iter_mut().enumerate() {
part.position += part.velocity * dt + 0.5 * self.accelerations[i] * dt2;
}
let forces = system.forces();
for (i, part) in system.iter_mut().enumerate() {
let mass = part.mass;
let acceleration = forces[i]/mass;
part.velocity += 0.5 * (self.accelerations[i] + acceleration)* dt;
self.accelerations[i] = acceleration;
}
}
}
/******************************************************************************/
/// This is needed for the `BerendsenBarostat` implementation. The value comes
/// from the DL_POLY source code.
const WATER_COMPRESSIBILITY: f64 = 7372.0;
/// Berendsen barostat integrator based on velocity-Verlet. This one neither
/// reversible nor symplectic.
pub struct BerendsenBarostat {
/// Timestep for the integrator
timestep: f64,
/// Target pressure for the barostat
pressure: f64,
/// Barostat time scale, expressed in units of the timestep.
tau: f64,
/// Storing the accelerations
accelerations: Vec<Vector3D>,
/// Storing the scaling factor
eta: f64,
}
impl BerendsenBarostat {
/// Create a new Berendsen barostat with an integration timestep of
/// `timestep`, and a target pressure of `pressure` and the barostat time
/// scale `tau`.
pub fn new(timestep: f64, pressure: f64, tau: f64) -> BerendsenBarostat {
BerendsenBarostat{
timestep: timestep,
pressure: pressure,
tau: tau,
accelerations: Vec::new(),
eta: 1.0,
}
}
}
impl Integrator for BerendsenBarostat {
fn setup(&mut self, system: &System) {
self.accelerations = vec![Vector3D::zero(); system.size()];
}
fn integrate(&mut self, system: &mut System) {
let dt = self.timestep;
// Update velocities at t + ∆t/2 and positions at t + ∆t
for (i, part) in system.iter_mut().enumerate() {
part.velocity += 0.5 * dt * self.accelerations[i];
// Scale all positions
part.position *= self.eta;
part.position += part.velocity * dt;
}
system.cell_mut().scale_mut(self.eta*self.eta*self.eta * Matrix3::one());
self.eta = f64::cbrt(1.0 - WATER_COMPRESSIBILITY / self.tau * (self.pressure - system.pressure()));
let forces = system.forces();
// Update accelerations at t + ∆t and velocities at t + ∆t
for (i, part) in system.iter_mut().enumerate() {
self.accelerations[i] = forces[i] / part.mass;
part.velocity += 0.5 * dt * self.accelerations[i];
}
}
}
/// Anisotropic Berendsen barostat integrator based on velocity-Verlet. This one
/// neither reversible nor symplectic.
pub struct AnisoBerendsenBarostat {
/// Timestep for the integrator
timestep: f64,
/// Target stress matrix for the barostat
stress: Matrix3,
/// Barostat time scale, expressed in units of the timestep
tau: f64,
/// Storing the accelerations
accelerations: Vec<Vector3D>,
/// Storing the scaling factor
eta: Matrix3,
}
impl AnisoBerendsenBarostat {
/// Create a new anisotropic Berendsen barostat with an integration timestep
/// of `timestep`, and a target stress matrix of `stress` and the barostat
/// time scale `tau`.
pub fn new(timestep: f64, stress: Matrix3, tau: f64) -> AnisoBerendsenBarostat {
AnisoBerendsenBarostat{
timestep: timestep,
stress: stress,
tau: tau,
accelerations: Vec::new(),
eta: Matrix3::one(),
}
}
/// Create a new anisotropic Berendsen barostat with an integration timestep
/// of `timestep`, using an hydrostatic stress matrix corresponding to the
/// pressure `pressure` and the barostat time scale `tau`.
pub fn hydrostatic(timestep: f64, pressure: f64, tau: f64) -> AnisoBerendsenBarostat {
AnisoBerendsenBarostat::new(timestep, pressure * Matrix3::one(), tau)
}
}
impl Integrator for AnisoBerendsenBarostat {
fn setup(&mut self, system: &System) {
self.accelerations = vec![Vector3D::zero(); system.size()];
}
fn integrate(&mut self, system: &mut System) {
let dt = self.timestep;
// Update velocities at t + ∆t/2 and positions at t + ∆t
for (i, part) in system.iter_mut().enumerate() {
part.velocity += 0.5 * dt * self.accelerations[i];
// Scale all positions
part.position = self.eta * part.position;
part.position += part.velocity * dt;
}
system.cell_mut().scale_mut(self.eta);
let factor = self.timestep * WATER_COMPRESSIBILITY / self.tau;
self.eta = Matrix3::one() - factor * (self.stress - system.stress());
// Make the eta matrix symmetric here
for i in 0..3 {
for j in 0..i {
self.eta[(i, j)] = 0.5 * (self.eta[(i, j)] + self.eta[(j, i)]);
self.eta[(j, i)] = self.eta[(i, j)];
}
}
let forces = system.forces();
// Update accelerations at t + ∆t and velocities at t + ∆t
for (i, part) in system.iter_mut().enumerate() {
self.accelerations[i] = forces[i] / part.mass;
part.velocity += 0.5 * dt * self.accelerations[i];
}
}
}