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
//! Physical properties of the simulated system.

use nalgebra::Vector3;
use serde::{Deserialize, Serialize};

#[cfg(feature = "rayon")]
use rayon::prelude::*;

use crate::constants::BOLTZMANN;
use crate::potentials::Potentials;
use crate::system::System;

/// Calculates a system-wide property.
pub trait Property {
    /// The property's return type.
    type Res;
    /// Returns a physical property of the system.
    fn calculate(&self, system: &System, potentials: &Potentials) -> Self::Res;
}

/// Calculates a system-wide property without using the applied potentials.
pub trait IntrinsicProperty {
    /// The property's return type.
    type Res;
    /// Returns a physical property of the system without accessing the associated potentials.
    fn calculate_intrinsic(&self, system: &System) -> Self::Res;
}

impl<T: IntrinsicProperty> Property for T {
    type Res = T::Res;

    fn calculate(&self, system: &System, _: &Potentials) -> Self::Res {
        <T as IntrinsicProperty>::calculate_intrinsic(&self, system)
    }
}

/// Force acting on each atom in the system.
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct Forces;

impl Property for Forces {
    type Res = Vec<Vector3<f32>>;

    #[cfg(not(feature = "rayon"))]
    fn calculate(&self, system: &System, potentials: &Potentials) -> Self::Res {
        let pairwise_forces: Vec<Vec<((usize, usize), Vector3<f32>)>> = potentials
            .pairs
            .iter()
            .map(|descriptor| {
                descriptor
                    .indices
                    .iter()
                    .map(|(i, j)| {
                        let pos_i = system.positions[*i];
                        let pos_j = system.positions[*j];
                        let r = system.cell().distance(&pos_i, &pos_j);
                        let indices = (*i, *j);
                        let mut force = Vector3::zeros();
                        if descriptor.meta.cutoff > r {
                            let dir = system.cell().direction(&pos_i, &pos_j);
                            force = descriptor.potential.force(r) * dir;
                        }
                        (indices, force)
                    })
                    .collect()
            })
            .collect();

        let mut forces: Vec<Vector3<f32>> = vec![Vector3::zeros(); system.size()];
        pairwise_forces.iter().for_each(|pair_group| {
            pair_group.iter().for_each(|((i, j), force)| {
                forces[*i] += force;
                forces[*j] -= force;
            })
        });
        forces
    }

    #[cfg(feature = "rayon")]
    fn calculate(&self, system: &System, potentials: &Potentials) -> Self::Res {
        let pairwise_forces: Vec<Vec<((usize, usize), Vector3<f32>)>> = potentials
            .pairs
            .iter()
            .map(|descriptor| {
                descriptor
                    .indices
                    .par_iter()
                    .map(|(i, j)| {
                        let pos_i = system.positions[*i];
                        let pos_j = system.positions[*j];
                        let r = system.cell().distance(&pos_i, &pos_j);
                        let indices = (*i, *j);
                        let mut force = Vector3::zeros();
                        if descriptor.meta.cutoff > r {
                            let dir = system.cell().direction(&pos_i, &pos_j);
                            force = descriptor.potential.force(r) * dir;
                        }
                        (indices, force)
                    })
                    .collect()
            })
            .collect();

        let mut forces: Vec<Vector3<f32>> = vec![Vector3::zeros(); system.size()];
        pairwise_forces.iter().for_each(|pair_group| {
            pair_group.iter().for_each(|((i, j), force)| {
                forces[*i] += force;
                forces[*j] -= force;
            })
        });
        forces
    }
}

/// Potential energy of the whole system.
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct PotentialEnergy;

impl Property for PotentialEnergy {
    type Res = f32;

    #[cfg(not(feature = "rayon"))]
    fn calculate(&self, system: &System, potentials: &Potentials) -> Self::Res {
        potentials
            .pairs
            .iter()
            .map(|descriptor| {
                let _sum: f32 = descriptor
                    .indices
                    .iter()
                    .map(|(i, j)| {
                        let pos_i = system.positions[*i];
                        let pos_j = system.positions[*j];
                        let r = system.cell().distance(&pos_i, &pos_j);
                        let mut energy = 0 as f32;
                        if descriptor.meta.cutoff > r {
                            energy = descriptor.potential.energy(r)
                        }
                        energy
                    })
                    .sum();
                _sum
            })
            .sum()
    }

    #[cfg(feature = "rayon")]
    fn calculate(&self, system: &System, potentials: &Potentials) -> Self::Res {
        potentials
            .pairs
            .iter()
            .map(|descriptor| {
                let _sum: f32 = descriptor
                    .indices
                    .par_iter()
                    .map(|(i, j)| {
                        let pos_i = system.positions[*i];
                        let pos_j = system.positions[*j];
                        let r = system.cell().distance(&pos_i, &pos_j);
                        let mut energy = 0 as f32;
                        if descriptor.meta.cutoff > r {
                            energy = descriptor.potential.energy(r)
                        }
                        energy
                    })
                    .sum();
                _sum
            })
            .sum()
    }
}

/// Kinetic energy of the whole system
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct KineticEnergy;

impl IntrinsicProperty for KineticEnergy {
    type Res = f32;

    fn calculate_intrinsic(&self, system: &System) -> <Self as IntrinsicProperty>::Res {
        let kinetic_energy: f32 = system
            .elements
            .iter()
            .zip(system.velocities.iter())
            .map(|(elem, vel)| 0.5 * elem.mass() * vel.norm_squared())
            .sum();
        kinetic_energy
    }
}

/// Total energy of the system.
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct TotalEnergy;

impl Property for TotalEnergy {
    type Res = f32;

    fn calculate(&self, system: &System, potentials: &Potentials) -> Self::Res {
        let kinetic = KineticEnergy.calculate(system, potentials);
        let potential = PotentialEnergy.calculate(system, potentials);
        kinetic + potential
    }
}

/// Instantaneous temperature of the system.
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct Temperature;

impl IntrinsicProperty for Temperature {
    type Res = f32;

    fn calculate_intrinsic(&self, system: &System) -> <Self as IntrinsicProperty>::Res {
        let kinetic = KineticEnergy.calculate_intrinsic(system);
        // NOTE: Calculating DOF this way is a potentially nasty bug if future
        // support is added for degrees of freedom beyond just 3D particles.
        let dof = (system.size() * 3) as f32;
        2.0 * kinetic / (dof * BOLTZMANN)
    }
}