Skip to content

Commit 926d9bb

Browse files
committed
Add DampedShiftedForce potential
1 parent 8b1b808 commit 926d9bb

File tree

7 files changed

+71
-20
lines changed

7 files changed

+71
-20
lines changed

crates/velvet-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ edition = "2018"
99

1010
[dependencies]
1111
indicatif = "0.15"
12+
libm = "0.2"
1213
nalgebra = "0.26"
1314
rand = "0.7"
1415
rand_distr = "0.3"

crates/velvet-core/src/potentials/coulomb.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
//! Potentials which describe Coulombic electrostatic interactions.
22
3+
#[cfg(feature = "f64")]
4+
use libm::erfc as erfc;
5+
6+
#[cfg(not(feature = "f64"))]
7+
use libm::erfcf as erfc;
8+
39
use crate::internal::consts::COULOMB;
410
use crate::internal::Float;
5-
use crate::potentials::types::StandardCoulombic;
11+
use crate::internal::consts::FRAC_2_SQRT_PI;
12+
use crate::potentials::types::{DampedShiftedForce, StandardCoulombic};
613
use crate::potentials::Potential;
714
use crate::selection::{setup_pairs_with_charge, update_pairs_by_cutoff_radius, Selection};
815
use crate::system::System;
@@ -15,6 +22,36 @@ pub trait CoulombPotential: Potential {
1522
fn force(&self, qi: Float, qj: Float, r: Float) -> Float;
1623
}
1724

25+
impl CoulombPotential for DampedShiftedForce {
26+
fn energy(&self, qi: Float, qj: Float, r: Float) -> Float {
27+
let factor = FRAC_2_SQRT_PI * self.alpha;
28+
let alpha2 = self.alpha.powi(2);
29+
let cutoff2 = self.cutoff.powi(2);
30+
31+
let term_a = erfc(self.alpha * r) / r;
32+
let term_b = erfc(self.alpha * self.cutoff) / self.cutoff;
33+
let term_c = erfc(self.alpha * self.cutoff) / cutoff2;
34+
let term_d = factor * (Float::exp(-alpha2 * cutoff2) / self.cutoff);
35+
let term_e = r - self.cutoff;
36+
37+
qi * qj * (term_a - term_b + (term_c + term_d) * term_e)
38+
}
39+
40+
fn force(&self, qi: Float, qj: Float, r: Float) -> Float {
41+
let factor = FRAC_2_SQRT_PI * self.alpha;
42+
let r2 = r.powi(2);
43+
let alpha2 = self.alpha.powi(2);
44+
let cutoff2 = self.cutoff.powi(2);
45+
46+
let term_a = erfc(self.alpha * r) / r2;
47+
let term_b = factor * Float::exp(-alpha2 * r2) / r;
48+
let term_c = erfc(self.alpha * self.cutoff) / cutoff2;
49+
let term_d = factor * Float::exp(-alpha2 * cutoff2) / self.cutoff;
50+
51+
qi * qj * ((term_a + term_b) - (term_c + term_d))
52+
}
53+
}
54+
1855
impl CoulombPotential for StandardCoulombic {
1956
fn energy(&self, qi: Float, qj: Float, r: Float) -> Float {
2057
(COULOMB * qi * qj) / (self.dielectric * r)

crates/velvet-core/src/potentials/types.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@ impl Buckingham {
2323

2424
impl Potential for Buckingham {}
2525

26+
/// [Damped Shifted Force](https://lammps.sandia.gov/doc/pair_coul.html#description) potential.
27+
#[derive(Clone, Copy, Debug)]
28+
pub struct DampedShiftedForce {
29+
/// Damping parameter.
30+
pub alpha: Float,
31+
/// Cutoff radius
32+
pub cutoff: Float,
33+
}
34+
35+
impl DampedShiftedForce {
36+
/// Returns a new [`DampedShiftedForce`] potential.
37+
pub fn new(alpha: Float, cutoff: Float) -> DampedShiftedForce {
38+
DampedShiftedForce {alpha, cutoff}
39+
}
40+
}
41+
42+
impl Potential for DampedShiftedForce {}
43+
44+
45+
2646
/// [Harmonic](https://lammps.sandia.gov/doc/bond_harmonic.html#description) oscillator potential.
2747
#[derive(Clone, Copy, Debug)]
2848
pub struct Harmonic {

examples/magnesium-oxide.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,21 @@ fn main() {
1616
let buck_mg_o = Buckingham::new(18946.9176, 0.32, 0.0);
1717
let buck_o_o = Buckingham::new(524960.604, 0.149, 642.94068);
1818

19-
// Initialize standard Coulombic potentials between all charged particles.
20-
let coul = StandardCoulombic::new(1.0);
19+
// Initialize a DSF potential to evaluate electrostatic interactions.
20+
let dsf = DampedShiftedForce::new(0.1, 10.0);
21+
// let coul = StandardCoulombic::new(1.0);
2122

2223
// Store all of the system's potentials in a Potentials struct.
23-
let cutoff_buck = 5.0;
24-
let cutoff_coul = 10.0;
25-
let thickness = 3.0;
2624
let potentials = PotentialsBuilder::new()
27-
.update_frequency(3)
28-
.pair(buck_mg_o, (magnesium, oxygen), cutoff_buck, thickness)
29-
.pair(buck_o_o, (oxygen, oxygen), cutoff_buck, thickness)
30-
.coulomb(coul, cutoff_coul, thickness)
25+
.update_frequency(1)
26+
.coulomb(dsf, 10.0, 3.0)
3127
.build();
3228

3329
// Initialize a velocity Verlet style integrator.
34-
let velocity_verlet = VelocityVerlet::new(1e-6);
30+
let velocity_verlet = VelocityVerlet::new(0.001);
3531

3632
// Initialize a Nose-Hoover style thermostat.
37-
let nose_hoover = NoseHoover::new(300.0, 1.25, 1e-4);
33+
let nose_hoover = NoseHoover::new(300.0, 1.25, 0.1);
3834

3935
// Run MD with a Nose-Hoover thermostat to simulate the NVT ensemble.
4036
let md = MolecularDynamics::new(velocity_verlet, nose_hoover);
@@ -56,5 +52,5 @@ fn main() {
5652

5753
// Run the simulation.
5854
let mut sim = Simulation::new(system, potentials, md, config);
59-
sim.run(250_000);
55+
sim.run(50_000);
6056
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:97738dfdc2e5afcae0e5fd552e7d931dba5e3c63753bfd20f2e1fd0b6331ead0
3-
size 4017
2+
oid sha256:c65504f513f0af286c3fdda2927dfada301266ef7d6014c5d2bc8d5e6c3e7ca6
3+
size 396
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:fd7994cdc5ee50c39f9955d72bc451decbc11958ea2bf963a4cb525a2cdfd186
3-
size 890
2+
oid sha256:9a6e41063b0b29ea9bf13cff6b56989de865aef742887ccbf81747f87ad3fc53
3+
size 939
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
version https://git-lfs.github.com/spec/v1
2-
oid sha256:f37d8c76b6889ab5d32418e01f823346cab3d866c7bc4578d951e3434e635d96
3-
size 11123

0 commit comments

Comments
 (0)