pub struct Metropolis<E, R, S, Res, T> { /* private fields */ }
Expand description

Create a metropolis simulation

Citation see, e.g,

M. E. J. Newman and G. T. Barkema, “Monte Carlo Methods in Statistical Physics” Clarendon Press, 1999, ISBN: 978-0-19-8517979

Explanation

  • used for large-deviation simulations
  • Performes markov chain using the markov chain trait

All self.metropolis* functions do the following

  • Let the current state of the system (i.e., the ensemble) be S(i) with corresponding energy E(i) = energy_fn(S(i)).
  • Now perform a markov step, such that the new system is S_new with energy E_new.
  • The new state will be accepted (meaning S(i+1) = S_new) with probability: min[1.0, exp{m_beta * (E_new - E(i))}]
  • otherwise the new state will be rejected, meaning S(i + 1) = S(i).
  • the measure function is called: measure(S(i + 1))

Implementations§

source§

impl<R, E, S, Res, T> Metropolis<E, R, S, Res, T>
where T: Copy + AsPrimitive<f64>,

source

pub fn m_beta(&self) -> f64

returns stored m_beta value (-β for metropolis)

source

pub fn set_m_beta(&mut self, m_beta: f64)

sets m_beta (minus beta). Is related to the temperature: m_beta = -1 / temperature

source

pub fn set_temperature(&mut self, temperature: f64)

sets m_beta according to m_beta = -1 / temperature

source

pub fn energy(&self) -> T

returns stored value for current_energy

source

pub unsafe fn set_energy(&mut self, energy: T) -> Result<(), ()>

set stored value for current_energy
  • Will return Err() if you try to set the energy to nan
  • otherwise it will set the stored energy and return Ok(())
Important
  • It is very unlikely that you need this function - Only use it, if you know what you are doing
Safety

This is not unsafe in the programming sense, but I chose to make it unsafe anyway to make the user aknowledge that this will result in a logical error for the algorithms if set to the incorrect energy

source

pub fn ensemble(&self) -> &E

returns reference to ensemble

source

pub unsafe fn ensemble_mut(&mut self) -> &mut E

returns mutable reference to ensemble

  • use with care!
Safety
  • if you change your ensemble, this might invalidate the simulation!
  • The metropolis functions do not calculate the energy of the current state
  • Unsafe purely for logical reasons, in the programming sense this function didn’t need to be unsafe
source

pub fn counter(&self) -> usize

returns stored value for the counter, i.e., where to resume iteration
  • note: counter is a wrapping counter
  • counter is increase each time, a markov step is performed, i.e, each time ensemble.m_steps(step_size) is called, the counter will increase by 1 (not by step_size)
source

pub fn reset_counter(&mut self)

resets the counter to 0
  • note: counter is a wrapping counter
source

pub fn step_size(&self) -> usize

return current stepsize

source

pub fn set_step_size(&mut self, step_size: usize) -> Result<(), ()>

  • change the stepsize
  • returns err if you try to set stepsize to 0, because that would be invalid
source§

impl<E, R, S, Res, T> Metropolis<E, R, S, Res, T>
where R: Rng, E: MarkovChain<S, Res>, T: Copy + AsPrimitive<f64>,

source

pub fn new_from_m_beta( rng: R, ensemble: E, energy: T, m_beta: f64, step_size: usize ) -> Result<Self, MetropolisError>

Create a new Metropolis struct - used for Metropolis simulations
meaning
rngthe Rng used to decide, if a state should be accepted or rejected
ensemblethe ensemble that is explored with the markov chain
energycurrent energy of the ensemble - cannot be NAN, should match energy_fn(ensemble) (see metropolis* functions)
m_betaminus beta, has to be finite - used for acceptance, i.e., probability to accept a markov step from Energy E to Energy E_new is min[1.0, exp{m_beta * (E_new - E)}]
step_sizeis used for each markov step, i.e., ensemble.m_steps(stepsize) is called
  • will return Err if energy is nan or m_beta is not finite
source

pub fn new_from_temperature( rng: R, ensemble: E, energy: T, temperature: f64, step_size: usize ) -> Result<Self, MetropolisError>

Create a new Metropolis struct - used for Metropolis simulations
meaning
rngthe Rng used to decide, if a state should be accepted or rejected
ensemblethe ensemble that is explored with the markov chain
energycurrent energy of the ensemble - cannot be NAN, should match energy_fn(ensemble) (see metropolis* functions)
temperaturem_beta = -1.0/temperature. Used for acceptance, i.e., probability to accept a markov step from Energy E to Energy E_new is min[1.0, exp{m_beta * (E_new - E)}]
step_sizeis used for each markov step, i.e., ensemble.m_steps(stepsize) is called
  • will return Err if energy is nan or m_beta is not finite
source

pub fn change_markov_chain<S2, Res2>(self) -> Metropolis<E, R, S2, Res2, T>
where E: MarkovChain<S2, Res2>,

Change, which markov chain is used for the metropolis simulations
  • Use this if there are different ways to perform a markov chain for your problem and you want to switch between them
source

pub fn metropolis<Energy, Mes>( &mut self, step_target: usize, energy_fn: Energy, measure: Mes )
where Energy: FnMut(&E) -> Option<T>, Mes: FnMut(&Self),

Metropolis Simulation
  • see
  • performs self.counter..=step_target markov steps
  • energy_fn(self.ensemble) is assumed to equal self.energy at the beginning!
  • if energy_fn returns None, the step will always be rejected
  • after each acceptance/rejection, measure is called
Note
  • I assume, that the energy_fn never returns nan (when cast as f64) If nan is possible, please check for that beforhand and return None in that case
  • Maybe do the same for infinity, it is unlikely, that infinit energys make sense
source

pub unsafe fn metropolis_unsafe<Energy, Mes>( &mut self, step_target: usize, energy_fn: Energy, measure: Mes )
where Energy: FnMut(&mut E) -> Option<T>, Mes: FnMut(&mut Self),

Metropolis Simulation
  • see
  • performs self.counter..=step_target markov steps
  • energy_fn(self.ensemble) is assumed to equal self.energy at the beginning!
  • if energy_fn returns None, the step will always be rejected
  • after each acceptance/rejection, measure is called
Important
  • if possible, prefere self.metropolis as it is safer
  • use this, if your energy function needs mutable access, or measureneeds mutable access. Be careful though, this can invalidate the results of your simulation
Safety
  • I assume, that the energy_fn never returns nan (when cast as f64) If nan is possible, please check for that beforhand and return None in that case
  • Maybe do the same for infinity, it is unlikely, that infinit energys make sense
  • Note: I chose to make this function unsafe to force users to aknowledge the (purely logical) limitations regarding the usage of the mutable ensemble. From a programming point of view this will not lead to any undefined behavior or such regardless of if the user fullfills the requirements
source

pub fn metropolis_efficient<Energy, Mes>( &mut self, step_target: usize, energy_fn: Energy, measure: Mes )
where Energy: FnMut(&E, T, &[S]) -> Option<T>, Mes: FnMut(&Self),

Metropolis Simulation
  • see
  • performs self.counter..=step_target markov steps
  • energy_fn(self.ensemble) is assumed to equal self.energy at the beginning!
  • if energy_fn returns None, the step will always be rejected
  • after each acceptance/rejection, measure is called
Difference to self.metropolis
  • Function parameter of energy_fn: &ensemble, old_energy, &[steps] - that means, you should prefere this, if you can calculate the new energy more efficient by accessing the old energy and the information about what the markov step changed
Note
  • I assume, that the energy_fn never returns nan (when cast as f64) If nan is possible, please check for that beforhand and return None in that case
  • Maybe do the same for infinity, it is unlikely, that infinit energys make sense
source

pub unsafe fn metropolis_efficient_unsafe<Energy, Mes>( &mut self, step_target: usize, energy_fn: Energy, measure: Mes )
where Energy: Fn(&mut E, T, &[S]) -> Option<T>, Mes: FnMut(&mut Self),

Metropolis Simulation
  • see
  • performs self.counter..=step_target markov steps
  • energy_fn(self.ensemble) is assumed to equal self.energy at the beginning!
  • if energy_fn returns None, the step will always be rejected
  • after each acceptance/rejection, measure is called
Difference to self.metropolis
  • Function parameter of energy_fn: &ensemble, old_energy, &[steps] - that means, you should prefere this, if you can calculate the new energy more efficient by accessing the old energy and the information about what the markov step changed
Safety
  • I assume, that the energy_fn never returns nan (when cast as f64) If nan is possible, please check for that beforhand and return None in that case
  • Maybe do the same for infinity, it is unlikely, that infinite energys make sense
source

pub fn metropolis_while<Energy, Mes, Cond>( &mut self, energy_fn: Energy, measure: Mes, condition: Cond )
where Energy: FnMut(&E) -> Option<T>, Mes: FnMut(&Self), Cond: FnMut(&Self) -> bool,

Metropolis Simulation
  • see
  • checks condition(self) after each metropolis_step(&mut energy_fn) and returns when false is returned by the condition
  • energy_fn(self.ensemble) is assumed to equal self.energy at the beginning!
  • if energy_fn returns None, the step will always be rejected
  • after each acceptance/rejection, measure is called
Note
  • I assume, that the energy_fn never returns nan (when cast as f64) If nan is possible, please check for that beforhand and return None in that case
  • Maybe do the same for infinity, it is unlikely, that infinit energys make sense
source

pub unsafe fn metropolis_while_unsafe<Energy, Mes, Cond>( &mut self, energy_fn: Energy, measure: Mes, condition: Cond )
where Energy: FnMut(&mut E) -> Option<T>, Mes: FnMut(&mut Self), Cond: FnMut(&mut Self) -> bool,

Metropolis simulation
Difference
  • energy_fn now works with a mutable reference of E (the ensemble).
Note
  • prefere metropolis_while as it is safer.
  • the changeing of the Ensemble must not affect subsequent Energy calculations - otherwise the logic of the algorithm breaks down
Safety
  • Note: I chose to make this function unsafe to force users to aknowledge the (purely logical) limitations regarding the usage of the mutable ensemble. From a programming point of view this will not lead to any undefined behavior or such regardless of if the user fullfills the requirements
source

pub fn metropolis_efficient_while<Energy, Mes, Cond>( &mut self, energy_fn: Energy, measure: Mes, condition: Cond )
where Energy: FnMut(&E, T, &[S]) -> Option<T>, Mes: FnMut(&Self), Cond: FnMut(&Self) -> bool,

Metropolis simulation
Difference
  • energy fn can use the old energy and the performed markov steps to more efficiently calculate the current Energy
source

pub unsafe fn metropolis_efficient_while_unsafe<Energy, Mes, Cond>( &mut self, energy_fn: Energy, measure: Mes, condition: Cond )
where Energy: FnMut(&mut E, T, &[S]) -> Option<T>, Mes: FnMut(&mut Self), Cond: FnMut(&mut Self) -> bool,

Metropolis simulation
Difference
  • now energy_fn works with a mutable reference of the ensemble instead
  • This is intended for usages in which the energy can be calculated much more efficiently using a mutable reference than an immutable one
Safety
  • Only use this, if it is absolutly nessessary. The ensemble must not be changed in a way, which affects successive energy calculations (or the markov steps)

Trait Implementations§

source§

impl<E: Clone, R: Clone, S: Clone, Res: Clone, T: Clone> Clone for Metropolis<E, R, S, Res, T>

source§

fn clone(&self) -> Metropolis<E, R, S, Res, T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'de, E, R, S, Res, T> Deserialize<'de> for Metropolis<E, R, S, Res, T>
where E: Deserialize<'de>, R: Deserialize<'de>, S: Deserialize<'de>, T: Deserialize<'de>,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<E, R, S, Res, T> HasRng<R> for Metropolis<E, R, S, Res, T>
where R: Rng,

source§

fn rng(&mut self) -> &mut R

Access RNG Read more
source§

fn swap_rng(&mut self, rng: &mut R)

If you need to exchange the internal rng Read more
source§

impl<E, R, S, Res, T> Serialize for Metropolis<E, R, S, Res, T>
where E: Serialize, R: Serialize, S: Serialize, T: Serialize,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<E, R, S, Res, T> RefUnwindSafe for Metropolis<E, R, S, Res, T>

§

impl<E, R, S, Res, T> Send for Metropolis<E, R, S, Res, T>
where E: Send, R: Send, Res: Send, S: Send, T: Send,

§

impl<E, R, S, Res, T> Sync for Metropolis<E, R, S, Res, T>
where E: Sync, R: Sync, Res: Sync, S: Sync, T: Sync,

§

impl<E, R, S, Res, T> Unpin for Metropolis<E, R, S, Res, T>
where E: Unpin, R: Unpin, Res: Unpin, S: Unpin, T: Unpin,

§

impl<E, R, S, Res, T> UnwindSafe for Metropolis<E, R, S, Res, T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<S, T> Cast<T> for S
where T: Conv<S>,

§

fn cast(self) -> T

Cast from Self to T Read more
§

fn try_cast(self) -> Result<T, Error>

Try converting from Self to T Read more
§

impl<S, T> CastApprox<T> for S
where T: ConvApprox<S>,

§

fn try_cast_approx(self) -> Result<T, Error>

Try approximate conversion from Self to T Read more
§

fn cast_approx(self) -> T

Cast approximately from Self to T Read more
§

impl<S, T> CastFloat<T> for S
where T: ConvFloat<S>,

§

fn cast_trunc(self) -> T

Cast to integer, truncating Read more
§

fn cast_nearest(self) -> T

Cast to the nearest integer Read more
§

fn cast_floor(self) -> T

Cast the floor to an integer Read more
§

fn cast_ceil(self) -> T

Cast the ceiling to an integer Read more
§

fn try_cast_trunc(self) -> Result<T, Error>

Try converting to integer with truncation Read more
§

fn try_cast_nearest(self) -> Result<T, Error>

Try converting to the nearest integer Read more
§

fn try_cast_floor(self) -> Result<T, Error>

Try converting the floor to an integer Read more
§

fn try_cast_ceil(self) -> Result<T, Error>

Try convert the ceiling to an integer Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,