30#include <gtest/gtest.h>
47constexpr double eps = 1e-12;
52double seeded_noise(
const std::uint64_t master,
const Coord_Vec<2> &coord)
54 const std::uint64_t
h = cell_seed<2>(master, 0, coord);
55 const double unit =
static_cast<double>(
h >> 11)
56 * (1.0 / 9007199254740992.0);
61 const std::uint64_t master)
63 RD_Lattice lat({n, n}, RD_Cell{1.0, 0.0});
71 const double z = 0.02 * seeded_noise(master, c);
72 lat.set(c, RD_Cell{0.50 + z, 0.25 - z});
82 std::size_t peaks = 0;
85VStats stats_for_v(
const RD_Lattice &lat)
87 const double total =
static_cast<double>(lat.size(0) * lat.size(1));
91 std::size_t peaks = 0;
93 for (
ca_size_t i = 0; i < lat.size(0); ++i)
94 for (
ca_size_t j = 0; j < lat.size(1); ++j)
106 const double mean =
sum / total;
107 return {
mean, sum2 / total -
mean *
mean, max_v, peaks};
110bool frames_equal(
const RD_Lattice &a,
const RD_Lattice &b)
112 if (a.extents() != b.extents())
114 for (
ca_size_t i = 0; i < a.size(0); ++i)
115 for (
ca_size_t j = 0; j < a.size(1); ++j)
116 if (a.at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)})
117 != b.at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)}))
124TEST(CAContinuousRule, AppliesScalarConvolutionAndDt)
127 [](
double current,
double conv,
double dt)
129 return current + dt * (conv - current);
131 mean_3x3_kernel<double>(),
136 const std::array<double, 8> neighbours{1.0, 1.0, 1.0, 1.0,
143TEST(CAContinuousRule, ValidatesDtNeighbourCountAndOutputRange)
146 [](
double,
double,
double) {
return 0.0; },
147 mean_3x3_kernel<double>(),
152 [](
double,
double,
double) {
return 2.0; },
153 mean_3x3_kernel<double>(),
157 const std::array<double, 8> neighbours{};
163 [](
double,
double conv) {
return conv; },
164 mean_3x3_kernel<double>());
165 const std::array<double, 2> too_short{};
171TEST(CAGrayScottRule, ValidatesParameters)
183TEST(CAGrayScottRule, UniformRestStateIsStable)
186 const std::array<RD_Cell, 8> neighbours{RD_Cell{1.0, 0.0},
195 const RD_Cell
next = rule(RD_Cell{1.0, 0.0},
198 EXPECT_DOUBLE_EQ(
next.u, 1.0);
199 EXPECT_DOUBLE_EQ(
next.v, 0.0);
202TEST(CAGrayScottRule, LocalPatchStepMatchesExplicitEulerFormula)
205 const std::array<RD_Cell, 8> neighbours{RD_Cell{1.0, 0.0},
214 const RD_Cell
next = rule(RD_Cell{0.5, 0.25},
217 EXPECT_NEAR(
next.u, 0.80875, eps);
218 EXPECT_NEAR(
next.v, 0.17625, eps);
221TEST(CAGrayScottRule, DeterministicSeedProducesReproduciblePatterns)
224 constexpr std::uint64_t master = 0x9E3779B97F4A7C15ull;
225 constexpr std::size_t
steps = 360;
229 a(make_gray_scott_seed(n, master), rule);
231 b(make_gray_scott_seed(n, master), rule);
238 const VStats stats = stats_for_v(a.
frame());
239 EXPECT_GT(stats.max_value, 0.18);
240 EXPECT_GT(stats.variance, 1e-4);
241 EXPECT_GT(stats.peaks, 8u);
242 EXPECT_LT(stats.peaks,
static_cast<std::size_t
>(n * n / 2));
245TEST(CAFitzHughNagumoRule, LocalRestStepMatchesEquation)
253 const std::array<RD_Cell, 8> neighbours{};
254 const RD_Cell
next = rule(RD_Cell{0.0, 0.0},
258 EXPECT_NEAR(
next.u, 0.0, eps);
259 EXPECT_NEAR(
next.v, 0.00056, eps);
262TEST(CAFitzHughNagumoRule, ValidatesParametersAndBlowUp)
270 const std::array<RD_Cell, 8> neighbours{};
271 EXPECT_THROW(rule(RD_Cell{1000.0, 0.0},
277TEST(CAHistoryCell, RingBufferKeepsRecentStates)
280 EXPECT_EQ(
h.current(), 1);
281 EXPECT_EQ(
h.previous(1), 1);
284 EXPECT_EQ(
h.current(), 2);
285 EXPECT_EQ(
h.previous(1), 1);
286 EXPECT_EQ(
h.previous(2), 1);
290 EXPECT_EQ(
h.current(), 5);
291 EXPECT_EQ(
h.previous(1), 3);
292 EXPECT_EQ(
h.previous(2), 2);
293 EXPECT_THROW(
h.previous(3), std::out_of_range);
296TEST(CAHistoryRule, DirectInvocationSupportsHistoryAwareFunctor)
299 const auto rule = make_history_rule<3>(
302 int sum = current.current() + current.previous(1);
303 for (
const auto &n : neighbours)
308 const Cell current(2);
309 const std::array<Cell, 2> neighbours{Cell(3), Cell(5)};
312 EXPECT_EQ(
next.current(), 12);
313 EXPECT_EQ(
next.previous(1), 2);
316TEST(CAHistoryRule, IntegratesWithSynchronousEngine)
321 Hist_Lattice lat({4}, Cell(1));
322 const auto fib_like = make_history_rule<3>(
323 [](
int current,
int previous) ->
int
325 return current + previous;
332 EXPECT_EQ(
engine.frame().at({0}).current(), 2);
333 EXPECT_EQ(
engine.frame().at({0}).previous(1), 1);
336 EXPECT_EQ(
engine.frame().at({0}).current(), 3);
337 EXPECT_EQ(
engine.frame().at({0}).previous(1), 2);
340 EXPECT_EQ(
engine.frame().at({0}).current(), 5);
341 EXPECT_EQ(
engine.frame().at({0}).previous(1), 3);
static int cell(const aleph_ca_engine_t *e, size_t r, size_t c)
Fixed 2-D convolution kernels for continuous cellular automata.
Reproducible random-number support for stochastic CA rules (Phase 8).
Common typedefs and tag types for the Cellular Automata module.
FitzHugh-Nagumo excitable-media rule.
Gray-Scott reaction-diffusion rule.
Cell state carrying a fixed-depth ring buffer of past values.
Lattice that adds boundary-aware access on top of a storage.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Synchronous double-buffered engine.
void run(const std::size_t steps)
Run several synchronous steps.
const Lattice & frame() const noexcept
Return the current frame.
Gray_Scott_Lattice make_gray_scott_seed(const ca_size_t side, const std::uint64_t master_seed)
Build the deterministic finite-amplitude Gray-Scott perturbation.
std::span< const T > Neighbor_View
Read-only view over a contiguous range of neighbour values.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
std::array< ca_index_t, N > Coord_Vec
Default coordinate vector.
bool frames_equal(const Lattice &a, const Lattice &b)
Continuous_Rule< F, Kernel > make_continuous_rule(F f, Kernel k, typename Kernel::value_type dt=typename Kernel::value_type{1}, typename Kernel::value_type min_value=-std::numeric_limits< typename Kernel::value_type >::infinity(), typename Kernel::value_type max_value=std::numeric_limits< typename Kernel::value_type >::infinity())
Helper factory for Continuous_Rule.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Main namespace for Aleph-w library functions.
auto variance(const Container &data, bool population=false) -> std::decay_t< decltype(*std::begin(data))>
Compute variance using Welford's numerically stable algorithm.
auto mean(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute the arithmetic mean.
void next()
Advance all underlying iterators (bounds-checked).
auto max_value(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute maximum value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Zero-gradient (Neumann) boundary.
Two-field state for reaction-diffusion cellular automata.
The lattice wraps around on every axis.
Continuous and memory-bearing CA rules (Phase 9).
Synchronous double-buffered engine for cellular automata.
Cellular automata lattice with pluggable boundary policies.
Neighborhoods catalogue for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).