Fully connected Ising system with Glauber dynamics
The fully connected Ising system with Glauber dynamics.
- Class:
CurieWeissSystem - Import:
from skcomplex.systems.physics.statistical_mechanics import CurieWeissSystem - Field:
physics/statistical_mechanics - Topics:
mean field,phase transition,spins,jump process - References:
- Bertin 2021
- Glauber 1963
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 CurieWeissSystem
trajectory = CurieWeissSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
n_spins |
500 |
temperature |
0.5 |
J |
1.0 |
h |
0.0 |
initial |
'random' |
random_state |
0 |
solver |
'auto' |
backend |
'auto' |
Tags
The system with its default parameters has these capability tags, which solvers and analyses check.
| Tag | Values |
|---|---|
| Time | continuous |
| Space | lattice |
| Laws | jump |
| Randomness | stochastic |
| Domains | real |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class CurieWeissSystem(BaseSystem):
"""The fully connected Ising system with Glauber dynamics.
The system orders below the critical temperature `T_c = J`.
The energy is `-(J / N) sum_{i<j} s_i s_j - h sum_i s_i`. In the large system limit the magnetization `m` solves `m
= tanh((J m + h) / T)`, which has a nonzero root below `T_c` when `h = 0`. One unit of time is one attempted flip
per spin. The spins are drawn in a row, which orders the drawing only. The system has the spins `spins.spin` and the
observable `magnetization`.
Args:
n_spins:
The number of spins.
temperature:
The temperature, which can change during a run.
J:
The coupling.
h:
The external field.
initial:
`'random'` for random spins or `'up'` for all spins up.
random_state:
The seed or generator of the random initial spins.
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 CurieWeissSystem
>>> trajectory = CurieWeissSystem(10).prepare().simulate(until=0.2, random_state=0)
>>> trajectory.get_values('spins.spin').shape
(3, 10)
"""
field = 'physics/statistical_mechanics'
topics = ('mean field', 'phase transition', 'spins', 'jump process')
references = ('Bertin 2021', 'Glauber 1963')
until = 30.0
dt = 0.1
observables = (_get_magnetization(),)
@staticmethod
def _get_initial_spins(n_spins: int, initial: str, random_state: int | np.random.Generator | None) -> Array:
"""Return all spins up, or random spins, for `initial` set to `'up'` or `'random'`."""
if initial == 'up':
return np.ones(n_spins)
return np.random.default_rng(random_state).choice([-1.0, 1.0], size=n_spins).astype(np.float64)
def __init__(
self,
n_spins: int = 500,
*,
temperature: float = 0.5,
J: float = 1.0,
h: float = 0.0,
initial: str = 'random',
random_state: int | np.random.Generator | None = 0,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.n_spins = n_spins
self.temperature = temperature
self.J = J
self.h = h
self.initial = initial
self.random_state = random_state
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the spins and their Glauber flips.
Args:
context:
The observables to compile with the problem, or `None` for none.
"""
super()._prepare(context)
spins = CurieWeissSystem._get_initial_spins(self.n_spins, self.initial, self.random_state)
self.system_ = System(
self.label,
elements=[FullyConnectedSpinElement('spins', spin=spins, space=LatticeSpace(self.n_spins))],
interactions=[
GlauberFlipInteraction('flips', target='spins', J=self.J, h=self.h, temperature=self.temperature),
],
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
FullyConnectedSpinElement, fromElement
Interactions:
GlauberFlipInteraction, fromInteraction
class FullyConnectedSpinElement(Element):
"""Ising spins of 1 or -1, each coupled to every other spin.
Args:
label:
The label of the spin or the group.
spin:
The initial spin, 1 or -1, or one spin per site.
space:
The row of sites of the spins, or `None` for one spin.
Examples:
>>> from skcomplex.systems.physics.statistical_mechanics import FullyConnectedSpinElement
>>> FullyConnectedSpinElement('spin').spin
1.0
"""
visual = VisualSpec('image', {'states': ('spin',), 'low': -1.0, 'high': 1.0})
def __init__(self, label: str, *, spin: object = 1.0, space: BaseSpace | None = None) -> None:
super().__init__(label, space=space)
self.spin = spin
class GlauberFlipInteraction(Interaction):
"""Glauber flips of fully connected Ising spins.
A spin flips at the rate `1 / (1 + exp(2 s h / T))`. The local field is `h = J (sum_{j != i} s_j) / N + h_ext` of
the Curie-Weiss energy `-(J / N) sum_{i<j} s_i s_j`, or `h = J sum_j C_ij s_j + h_ext` with a symmetric coupling
matrix `C` of zero diagonal. The rate satisfies detailed balance with the Boltzmann distribution at the temperature
`T`.
Args:
label:
The label of the interaction.
target:
The path of the spins.
J:
The scale of the couplings.
h:
The external field.
temperature:
The temperature.
couplings:
The coupling matrix `C`, one row and one column per spin, or `None` for the uniform couplings `1 / N`.
Examples:
>>> from skcomplex.systems.physics.statistical_mechanics import GlauberFlipInteraction
>>> GlauberFlipInteraction('flips', target='spins').temperature
0.5
"""
law_classes = (JumpLaw,)
def __init__(
self,
label: str,
*,
target: Role,
J: float = 1.0,
h: float = 0.0,
temperature: Annotated[float, RealSet.open(0, oo)] = 0.5,
couplings: npt.ArrayLike | None = None,
) -> None:
super().__init__(label)
self.target = target
self.J = J
self.h = h
self.temperature = temperature
self.couplings = couplings
def _get_field(self, s: SimpleNamespace) -> Expression:
"""Return the local field of a spin, without its own contribution."""
spin = s.target.spin
if self.couplings is None:
return s.J * (mean(spin) - spin / count()) + s.h
return s.J * total(spin, weights=np.asarray(self.couplings, dtype=float)) + s.h
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the flips of up spins and of down spins at the Glauber rate.
Args:
context:
The namespace of the target, the time `t`, and the parameters.
"""
super()._prepare(context)
spin = context.target.spin
rate = 1 / (1 + exp(2 * spin * self._get_field(context) / context.temperature))
self.laws_ = [JumpLaw(where(spin > 0, rate, 0), {spin: -2}), JumpLaw(where(spin < 0, rate, 0), {spin: 2})]