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
use serde::{Deserialize, Serialize};

use crate::integrators::Integrator;
use crate::potentials::Potentials;
use crate::system::System;
use crate::thermostats::Thermostat;

#[typetag::serde(tag = "type")]
pub trait Propagator: Send + Sync {
    fn setup(&mut self, _: &mut System, _: &Potentials) {}
    fn propagate(&mut self, _: &mut System, _: &Potentials) {}
}

#[derive(Serialize, Deserialize)]
pub struct MolecularDynamics {
    integrator: Box<dyn Integrator>,
    thermostat: Box<dyn Thermostat>,
}

impl MolecularDynamics {
    pub fn new(
        integrator: Box<dyn Integrator>,
        thermostat: Box<dyn Thermostat>,
    ) -> MolecularDynamics {
        MolecularDynamics {
            integrator,
            thermostat,
        }
    }
}

#[typetag::serde]
impl Propagator for MolecularDynamics {
    fn setup(&mut self, system: &mut System, potentials: &Potentials) {
        self.integrator.setup(system, potentials);
        self.thermostat.setup(system);
    }

    fn propagate(&mut self, system: &mut System, potentials: &Potentials) {
        self.thermostat.pre_integrate(system);
        self.integrator.integrate(system, potentials);
        self.thermostat.post_integrate(system);
    }
}