pub trait TotalEnergy<M> {
// Required method
fn total_energy(&self, microstate: &M) -> f64;
// Provided method
fn delta_energy_total(
&self,
initial_microstate: &M,
final_microstate: &M,
) -> f64 { ... }
}Expand description
Compute the total energy of a potential applied to the microstate.
The TotalEnergy trait describes a type that can compute the energy of a
given microstate. Depending on the type, total_energy might compute the
total potential energy of the whole Hamiltonian or a single term, such as
the Lennard-Jones potential energy.
§Example
use hoomd_interaction::{
PairwiseCutoff, SitePairEnergy, TotalEnergy, pairwise::Isotropic,
univariate::LennardJones,
};
use hoomd_microstate::{
Body, Microstate,
property::{Point, Position},
};
use hoomd_vector::{Cartesian, Vector};
let mut microstate = Microstate::new();
microstate.extend_bodies([
Body::point(Cartesian::from([0.0, 0.0])),
Body::point(Cartesian::from([1.0, 0.0])),
Body::point(Cartesian::from([0.0, 5.0])),
Body::point(Cartesian::from([-1.0, 5.0])),
])?;
let lennard_jones: LennardJones = LennardJones {
epsilon: 1.5,
sigma: 1.0 / 2.0_f64.powf(1.0 / 6.0),
};
let pairwise_cutoff = PairwiseCutoff(Isotropic {
interaction: lennard_jones,
r_cut: 2.5,
});
let total_energy = pairwise_cutoff.total_energy(µstate);
assert_eq!(total_energy, -3.0);§Derive macro
Use the TotalEnergy derive macro to automatically implement
the TotalEnergy trait on a type. The derived implementation sums the result of
total_energy over all fields in the struct (in the order in which fields
are named in the struct definition). The sum short circuits and returns
f64::INFINITY when any one field returns infinity.
use hoomd_interaction::{
External, PairwiseCutoff, TotalEnergy, external::Linear,
pairwise::Isotropic, univariate::Boxcar,
};
use hoomd_microstate::{Body, Microstate, property::Point};
use hoomd_vector::Cartesian;
#[derive(TotalEnergy)]
struct Hamiltonian {
linear: External<Linear<Cartesian<2>>>,
pairwise_cutoff: PairwiseCutoff<Isotropic<Boxcar>>,
}
let mut microstate = Microstate::new();
microstate.extend_bodies([
Body::point(Cartesian::from([0.0, 4.0])),
Body::point(Cartesian::from([1.0, 4.0])),
])?;
let epsilon = 2.0;
let (left, right) = (0.0, 1.5);
let boxcar = Boxcar {
epsilon,
left,
right,
};
let pairwise_cutoff = PairwiseCutoff(Isotropic {
interaction: boxcar,
r_cut: right,
});
let linear = External(Linear {
alpha: 1.0,
plane_origin: Cartesian::default(),
plane_normal: [0.0, 1.0].try_into()?,
});
let hamiltonian = Hamiltonian {
pairwise_cutoff,
linear,
};
let total_energy = hamiltonian.total_energy(µstate);
assert_eq!(total_energy, 10.0);Required Methods§
Sourcefn total_energy(&self, microstate: &M) -> f64
fn total_energy(&self, microstate: &M) -> f64
Compute the energy.
Provided Methods§
Sourcefn delta_energy_total(
&self,
initial_microstate: &M,
final_microstate: &M,
) -> f64
fn delta_energy_total( &self, initial_microstate: &M, final_microstate: &M, ) -> f64
Compute the difference in energy between two microstates.
Returns $E_\mathrm{final} - E_\mathrm{initial}$.