63#ifndef TPL_CA_STOCHASTIC_RULES_H
64#define TPL_CA_STOCHASTIC_RULES_H
113template <
typename Engine = std::mt19937_64>
137 <<
"Forest_Fire_Rule: p_growth=" <<
p_growth <<
" outside [0, 1]";
139 <<
"Forest_Fire_Rule: p_lightning=" <<
p_lightning <<
" outside [0, 1]";
177 template <
typename State, std::
size_t Rank>
182 const auto cur =
static_cast<int>(current);
189 for (
const auto &n : neighbours)
240template <
typename Engine = std::mt19937_64>
264 <<
"SIR_Rule: gamma=" <<
gamma <<
" outside [0, 1]";
302 template <
typename State, std::
size_t Rank>
307 const auto cur =
static_cast<int>(current);
321 for (
const auto &n : neighbours)
322 if (
static_cast<int>(n) ==
static_cast<int>(
SIR_Cell::I))
340namespace ca_ising_detail {
345template <
typename State>
348 if constexpr (std::is_signed_v<State>)
349 return v >= State{0} ? +1 : -1;
351 return v != State{} ? +1 : -1;
356template <
typename State>
359 if constexpr (std::is_signed_v<State>)
360 return spin > 0 ?
static_cast<State
>(1) :
static_cast<State
>(-1);
362 return spin > 0 ?
static_cast<State
>(1) :
static_cast<State
>(0);
366template <
typename State>
374 for (
const auto &n : neighbours)
376 return 2.0 *
static_cast<double>(s) * (
J *
static_cast<double>(
sum) + H);
398template <
typename Engine = std::mt19937_64>
418 const double J = 1.0,
419 const double H = 0.0,
460 template <
typename State, std::
size_t Rank>
469 const double x =
dE /
T_;
470 const double p_flip = x >= 0.0 ? 1.0 / (1.0 + std::exp(x)) : std::exp(-x) / (1.0 + std::exp(-x));
488template <
typename Engine = std::mt19937_64>
508 const double J = 1.0,
509 const double H = 0.0,
513 ah_domain_error_if(
T <= 0.0) <<
"Ising_Metropolis_Rule: temperature must be positive, got " <<
T;
550 template <
typename State, std::
size_t Rank>
602template <
typename Engine = std::mt19937_64>
626 const double p_move = 1.0,
627 const double p_fill = 1.0,
632 <<
"Schelling_Rule: threshold=" <<
threshold <<
" outside [0, 1]";
634 <<
"Schelling_Rule: p_move=" <<
p_move <<
" outside [0, 1]";
636 <<
"Schelling_Rule: p_fill=" <<
p_fill <<
" outside [0, 1]";
668 template <
typename State, std::
size_t Rank>
673 std::size_t a = 0, b = 0;
674 for (
const auto &n : neighbours)
680 const auto cur =
static_cast<int>(current);
693 const std::size_t
total = a + b;
699 if (
const double fraction =
static_cast<double>(
same) /
static_cast<double>(
total);
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Reproducible random-number support for stochastic CA rules (Phase 8).
Common typedefs and tag types for the Cellular Automata module.
Forest-fire rule (Drossel & Schwabl, 1992).
std::uint64_t master_seed() const noexcept
void set_master_seed(std::uint64_t s) noexcept
Replace the master seed; takes effect on the next step.
Engine engine_type
Underlying RNG engine type.
Forest_Fire_Rule(const double p_growth, const double p_lightning, std::uint64_t master_seed=0)
Build a forest-fire rule with the given probabilities.
double p_growth() const noexcept
std::uint64_t master_seed_
double p_lightning() const noexcept
State operator()(const State ¤t, Neighbor_View< State > neighbours, const Cell_Context< Rank > &ctx) const
Compute the next forest-fire state for cell current.
Ising rule with Glauber single-spin-flip dynamics.
double temperature() const noexcept
std::uint64_t master_seed_
Ising_Glauber_Rule(const double T, const double J=1.0, const double H=0.0, const std::uint64_t master_seed=0)
Build a Glauber Ising rule.
void set_master_seed(std::uint64_t s) noexcept
std::uint64_t master_seed() const noexcept
double coupling() const noexcept
double field() const noexcept
State operator()(const State ¤t, Neighbor_View< State > neighbours, const Cell_Context< Rank > &ctx) const
Compute the next Ising state for cell current.
Ising rule with Metropolis–Hastings single-spin-flip dynamics.
double temperature() const noexcept
std::uint64_t master_seed_
std::uint64_t master_seed() const noexcept
double field() const noexcept
State operator()(const State ¤t, Neighbor_View< State > neighbours, const Cell_Context< Rank > &ctx) const
Compute the next Ising state for cell current (Metropolis dynamics).
Ising_Metropolis_Rule(const double T, const double J=1.0, const double H=0.0, const std::uint64_t master_seed=0)
Build a Metropolis Ising rule.
void set_master_seed(std::uint64_t s) noexcept
double coupling() const noexcept
SIR epidemic transition rule.
std::uint64_t master_seed_
std::uint64_t master_seed() const noexcept
void set_master_seed(std::uint64_t s) noexcept
Replace the master seed.
double gamma() const noexcept
SIR_Rule(double beta, double gamma, std::uint64_t master_seed=0)
Build an SIR rule with infection rate beta and recovery rate gamma.
State operator()(const State ¤t, Neighbor_View< State > neighbours, const Cell_Context< Rank > &ctx) const
Compute the next SIR state for cell current.
double beta() const noexcept
Local approximation of the Schelling segregation model.
double threshold() const noexcept
State operator()(const State ¤t, Neighbor_View< State > neighbours, const Cell_Context< Rank > &ctx) const
Compute the next Schelling state for cell current.
std::uint64_t master_seed() const noexcept
void set_master_seed(std::uint64_t s) noexcept
double p_fill() const noexcept
Schelling_Rule(const double threshold, const double p_move=1.0, const double p_fill=1.0, const std::uint64_t master_seed=0)
Build a Schelling rule.
double p_move() const noexcept
std::uint64_t master_seed_
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
double delta_energy(const State ¤t, Neighbor_View< State > neighbours, double J, double H)
Compute the local energy delta for flipping the centre spin.
State spin_to_state(int spin) noexcept
Inverse of state_to_spin.
int state_to_spin(const State &v) noexcept
Map any integral state to a spin in {-1, +1}.
Forest_Cell
Discrete states of the forest-fire automaton.
@ EMPTY
Empty ground cell.
@ BURNING
Tree currently on fire (lasts exactly one step).
SIR_Cell
Discrete states of the SIR automaton.
@ I
Infected (and infectious).
@ R
Recovered (and immune).
std::span< const T > Neighbor_View
Read-only view over a contiguous range of neighbour values.
Schelling_Cell
Discrete states of the Schelling automaton.
double uniform_unit(Engine &eng)
Map a 64-bit RNG output to a uniform value in [0, 1).
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Per-cell context handed to rules that need to know "where" and "when" they are firing.