Discrete master equation
The discrete-time master equation of states on a ring.
- Class:
DiscreteMasterEquationSystem - Import:
from skcomplex.systems.physics.statistical_mechanics import DiscreteMasterEquationSystem - Field:
physics/statistical_mechanics - Topics:
master equation,probability,detailed balance,lattice - References:
- Jensen 2023
- van Kampen 2007
Use this system
The block imports the class and simulates the system with its default parameters and its tuned run settings.
from skcomplex.systems.physics.statistical_mechanics import DiscreteMasterEquationSystem
trajectory = DiscreteMasterEquationSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
n_states |
50 |
beta |
1.0 |
depth |
2.0 |
w |
0.5 |
solver |
'auto' |
backend |
'auto' |
Tags
The system with its default parameters has these capability tags, which solvers and analyses check.
| Tag | Values |
|---|---|
| Time | discrete |
| Space | lattice |
| Laws | update |
| Randomness | deterministic |
| Domains | real |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class DiscreteMasterEquationSystem(BaseSystem):
"""The discrete-time master equation of states on a ring.
The equation relaxes to the Boltzmann distribution.
The potential is `U = depth cos(2 pi i / n_states)`, the probability starts on the state of highest potential, and
moves to neighbouring states with Glauber probabilities, which satisfy detailed balance. The system has the
probabilities `states.P` and potentials `states.U`.
Args:
n_states:
The number of states.
beta:
The inverse temperature.
depth:
The amplitude of the potential.
w:
The attempt probability of a move to each neighbour, at most 1/2.
label:
The label of the system, which names it when it is nested in another one.
solver:
The solver of the problem, or `'auto'` for the one the system chooses.
backend:
The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.
Examples:
>>> from skcomplex.systems.physics.statistical_mechanics import DiscreteMasterEquationSystem
>>> DiscreteMasterEquationSystem(10).prepare().system_.elements[0].space.prepare().size_
10
"""
field = 'physics/statistical_mechanics'
topics = ('master equation', 'probability', 'detailed balance', 'lattice')
references = ('Jensen 2023', 'van Kampen 2007')
until = 500
dt = 10
def __init__(
self,
n_states: int = 50,
*,
beta: float = 1.0,
depth: float = 2.0,
w: float = 0.5,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.n_states = n_states
self.beta = beta
self.depth = depth
self.w = w
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set `system_` to the ring of states with all the probability on the state of highest potential.
Args:
context:
The observables to compile with the problem, or `None` for none.
"""
super()._prepare(context)
potential = self.depth * np.cos(2 * np.pi * np.arange(self.n_states) / self.n_states)
probability = np.zeros(self.n_states)
probability[0] = 1.0
states = MicrostateElement('states', P=probability, U=potential, space=LatticeSpace(self.n_states))
moves = GlauberMasterInteraction('moves', target='states', beta=self.beta, w=self.w)
self.system_ = System(self.label, elements=[states], interactions=[moves])
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
MicrostateElement, fromElement
Interactions:
GlauberMasterInteraction, fromInteraction
class MicrostateElement(Element):
"""State of a discrete master equation on a ring, with the probability `P` and the potential `U`.
Args:
label:
The label of the state or the ring of states.
P:
The initial probability.
U:
The potential.
space:
The ring whose sites hold one state each, or `None` for one state.
Examples:
>>> from skcomplex.systems.physics.statistical_mechanics import MicrostateElement
>>> MicrostateElement('state', U=1.5).U
1.5
"""
def __init__(
self,
label: str,
*,
P: Annotated[object, RealSet.closed_open(0, oo)] = 0.0,
U: object = 0.0,
space: BaseSpace | None = None,
) -> None:
super().__init__(label, space=space)
self.P = P
self.U = U
class GlauberMasterInteraction(Interaction):
"""Discrete master equation of states on a ring with Glauber moves between neighbours.
Every step the probability moves to each neighbouring state `B` from a state `A` with the Glauber probability
`w / (1 + exp(beta (U_B - U_A)))`, so `P_A(t + 1) = P_A + sum_B T_AB P_B - sum_B T_BA P_A`, and the stationary
distribution is `exp(-beta U) / Z`. It is a molecule hopping between conformations, a walker in a landscape, or a
queue whose length changes by one.
Args:
label:
The label of the interaction.
target:
The path of the ring of states.
beta:
The inverse temperature.
w:
The attempt probability of a move to each neighbour, at most 1/2.
Examples:
>>> from skcomplex.systems.physics.statistical_mechanics import GlauberMasterInteraction
>>> GlauberMasterInteraction('moves', target='states').w
0.5
"""
law_classes = (UpdateLaw,)
def __init__(
self,
label: str,
*,
target: Role,
beta: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
w: Annotated[float, RealSet.closed_open(0, oo)] = 0.5,
) -> None:
super().__init__(label)
self.target = target
self.beta = beta
self.w = w
@staticmethod
def _get_glauber(source: Array, destination: Array, beta: float, w: float) -> Array:
"""Return the probability of a move between two states.
The probability is `w / (1 + exp(beta (U_destination - U_source)))`.
"""
return w / (1 + np.exp(beta * (destination - source)))
def _get_master_step(self, *around: Array, **kwargs: object) -> Array:
"""Return the probabilities after one step of the master equation.
The neighbourhood columns are the probabilities and then the potentials of the left state, the state, and the
right state.
"""
left, center, right, energy_left, energy, energy_right = around
beta, w = self.beta, self.w
gain = (
self._get_glauber(energy_left, energy, beta, w) * left
+ self._get_glauber(energy_right, energy, beta, w) * right
)
loss = (
self._get_glauber(energy, energy_left, beta, w) + self._get_glauber(energy, energy_right, beta, w)
) * center
return center + gain - loss
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the master equation step of the probabilities of the target.
Args:
context:
The namespace of the target, the time `t`, and the parameters.
"""
super()._prepare(context)
s = context.target
self.laws_ = [UpdateLaw(s.P, self._get_master_step, neighborhood=RING, reads=(s.P, s.U), label=self.label)]