55#include <gtest/gtest.h>
76struct Discrete_Predator_Prey_Rule
80 using fields = std::tuple<std::int32_t, std::int32_t>;
84 std::int32_t operator()(
const fields &cur,
const nb_views &nb)
const noexcept =
delete;
86 fields operator_impl(
const fields &cur,
const nb_views &nb)
const noexcept
88 const auto prey = std::get<0>(cur);
89 const auto pred = std::get<1>(cur);
90 std::int32_t prey_n = 0;
91 std::int32_t pred_n = 0;
92 for (
const auto &p :
std::get<0>(nb)) prey_n += p;
93 for (
const auto &p :
std::get<1>(nb)) pred_n += p;
97 const std::int32_t next_prey = (prey + (prey_n / 8) -
pred);
98 const std::int32_t next_pred = ((pred_n / 8) + (prey / 4) - (
pred / 2));
99 return {next_prey, next_pred};
106 using fields = std::tuple<std::int32_t, std::int32_t>;
109 fields operator()(
const fields &cur,
const nb_views &nb)
const noexcept
111 const auto prey = std::get<0>(cur);
112 const auto pred = std::get<1>(cur);
113 std::int32_t prey_n = 0;
114 std::int32_t pred_n = 0;
115 for (
const auto &p :
std::get<0>(nb)) prey_n += p;
116 for (
const auto &p :
std::get<1>(nb)) pred_n += p;
117 const std::int32_t next_prey = (prey + (prey_n / 8) -
pred);
118 const std::int32_t next_pred = ((pred_n / 8) + (prey / 4) - (
pred / 2));
119 return {next_prey, next_pred};
123template <
typename Layout>
125 std::int32_t, std::int32_t>;
127template <
typename Layout>
128PP_Lattice<Layout> make_seeded_pp(
const ca_size_t side,
const std::uint32_t
seed)
130 PP_Lattice<Layout> lat({side, side});
135 const std::int32_t prey =
static_cast<std::int32_t
>((i + j +
seed) % 7);
136 const std::int32_t
pred =
static_cast<std::int32_t
>((i * 2 + j +
seed) % 5);
145template <
typename A,
typename B>
146bool frames_equal_pp(
const A &a,
const B &b)
148 if (a.extents() != b.extents())
150 for (
ca_size_t i = 0; i < a.size(0); ++i)
151 for (
ca_size_t j = 0; j < a.size(1); ++j)
154 if (a.template at<0>(c) != b.template at<0>(c))
156 if (a.template at<1>(c) != b.template at<1>(c))
168TEST(CAMultiField, AoSAndSoAProduceIdenticalFramesUnderDiscreteRule)
171 auto aos = make_seeded_pp<Layout_AoS>(side, 17);
172 auto soa = make_seeded_pp<Layout_SoA>(side, 17);
174 ASSERT_TRUE(frames_equal_pp(aos, soa)) <<
"initial frames differ";
179 Aos_Eng aeng(std::move(aos), Discrete_PP{},
Moore<2, 1>{});
180 Soa_Eng seng(std::move(soa), Discrete_PP{},
Moore<2, 1>{});
182 for (std::size_t t = 0; t < 25; ++t)
184 ASSERT_TRUE(frames_equal_pp(aeng.frame(), seng.frame()))
185 <<
"diverged at step " << t;
189 EXPECT_TRUE(frames_equal_pp(aeng.frame(), seng.frame()))
190 <<
"diverged after 25 steps";
197TEST(CAMultiField, SoAFieldExposesMonoFieldLatticeForLegacyEngine)
202 auto &f0 = lat.
field<0>();
206 static_cast<int>((i + j) & 1));
218 lat.at<1>({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)}),
234class Multi_Field_Gray_Scott
236 double feed_, kill_, du_, dv_, dt_;
240 Multi_Field_Gray_Scott(
double feed,
double kill,
double du,
double dv,
double dt = 1.0)
241 : feed_(feed), kill_(kill), du_(du), dv_(dv), dt_(dt)
244 std::tuple<double, double>
245 operator()(
const std::tuple<double, double> &cur,
248 const double u = std::get<0>(cur);
249 const double v = std::get<1>(cur);
250 const auto &uv = std::get<0>(nb);
251 const auto &vv = std::get<1>(nb);
252 double lu = u * lap_.
center();
253 double lv = v * lap_.
center();
254 for (std::size_t
k = 0;
k < uv.size(); ++
k)
256 for (std::size_t
k = 0;
k < vv.size(); ++
k)
258 const double uvv = u * v * v;
259 double next_u = u + dt_ * (du_ * lu - uvv + feed_ * (1.0 - u));
260 double next_v = v + dt_ * (dv_ * lv + uvv - (feed_ + kill_) * v);
262 if (next_u < 0.0) next_u = 0.0;
263 else if (next_u > 1.0) next_u = 1.0;
264 if (next_v < 0.0) next_v = 0.0;
265 else if (next_v > 1.0) next_v = 1.0;
266 return {next_u, next_v};
272TEST(CAMultiField, GrayScottMonolithicAndMultiFieldAgreeWithinTolerance)
277 Mono_Lat mono({side, side}, Cell{1.0, 0.0});
281 MF_Lat mf({side, side}, std::tuple{1.0, 0.0});
284 for (
ca_size_t i = side / 2 - 2; i < side / 2 + 2; ++i)
285 for (
ca_size_t j = side / 2 - 2; j < side / 2 + 2; ++j)
288 mono.
set(c, Cell{0.5, 0.25});
297 std::move(mf), Multi_Field_Gray_Scott{0.0367, 0.0649, 0.16, 0.08, 1.0},
300 for (std::size_t t = 0; t < 50; ++t)
316 constexpr double kGSTolerance = 1e-12;
321 const Cell mc = meng.frame().at(c);
322 ASSERT_NEAR(feng.frame().at<0>(c), mc.u, kGSTolerance)
323 <<
"u differs at (" << i <<
"," << j <<
")";
324 ASSERT_NEAR(feng.frame().at<1>(c), mc.v, kGSTolerance)
325 <<
"v differs at (" << i <<
"," << j <<
")";
336class Lotka_Volterra_Rule
338 double alpha_, beta_, delta_, gamma_, dt_;
342 Lotka_Volterra_Rule(
double alpha,
double beta,
double delta,
double gamma,
343 double dt,
double diffusion)
344 : alpha_(alpha), beta_(beta), delta_(
delta), gamma_(
gamma), dt_(dt),
347 std::tuple<double, double>
348 operator()(
const std::tuple<double, double> &cur,
351 const double prey = std::get<0>(cur);
352 const double pred = std::get<1>(cur);
354 double prey_lap = -
static_cast<double>(std::get<0>(nb).size()) * prey;
355 double pred_lap = -
static_cast<double>(std::get<1>(nb).size()) *
pred;
356 for (
const auto &p :
std::get<0>(nb)) prey_lap += p;
357 for (
const auto &p :
std::get<1>(nb)) pred_lap += p;
358 const double next_prey = prey + dt_ * (alpha_ * prey - beta_ * prey *
pred
360 const double next_pred =
pred + dt_ * (delta_ * prey *
pred - gamma_ *
pred
362 return {next_prey < 0.0 ? 0.0 : next_prey,
363 next_pred < 0.0 ? 0.0 : next_pred};
369TEST(CAMultiField, LotkaVolterraStaysBoundedAndOscillates)
373 L lat({side, side}, std::tuple{1.0, 0.5});
376 for (
ca_size_t i = side / 2 - 1; i <= side / 2 + 1; ++i)
377 for (
ca_size_t j = side / 2 - 1; j <= side / 2 + 1; ++j)
385 std::move(lat), Lotka_Volterra_Rule{1.0, 0.5, 0.4, 0.5, 0.05, 0.05},
390 std::vector<double> prey_history;
391 std::vector<double> pred_history;
392 prey_history.reserve(200);
393 pred_history.reserve(200);
394 for (std::size_t t = 0; t < 200; ++t)
397 double prey_total = 0;
398 double pred_total = 0;
403 prey_total += eng.frame().at<0>(c);
404 pred_total += eng.frame().at<1>(c);
405 ASSERT_GE(eng.frame().at<0>(c), 0.0) <<
"prey<0 at step " << t;
406 ASSERT_GE(eng.frame().at<1>(c), 0.0) <<
"pred<0 at step " << t;
407 ASSERT_LT(eng.frame().at<0>(c), 1e6) <<
"prey blew up at step " << t;
408 ASSERT_LT(eng.frame().at<1>(c), 1e6) <<
"pred blew up at step " << t;
410 prey_history.push_back(prey_total);
411 pred_history.push_back(pred_total);
417 std::size_t turning_points = 0;
418 for (std::size_t
k = 1;
k + 1 < prey_history.size(); ++
k)
420 const double a = prey_history[
k - 1];
421 const double b = prey_history[
k];
422 const double c = prey_history[
k + 1];
423 if ((b > a
and b > c) or (b < a
and b < c))
426 EXPECT_GT(turning_points, 1u)
427 <<
"Lotka-Volterra prey trajectory shows no oscillation";
434TEST(CAMultiField, FieldSliceRulePreservesOtherFields)
437 L lat({3, 3}, std::tuple{0, 7});
439 lat.
set<0>({1, 1}, 1);
450 EXPECT_EQ(eng.frame().at<1>({
static_cast<ca_index_t>(i),
459 EXPECT_EQ(eng.frame().at<0>({static_cast<ca_index_t>(1),
460 static_cast<ca_index_t>(1)}),
466 EXPECT_EQ(eng.frame().at<0>({static_cast<ca_index_t>(i),
467 static_cast<ca_index_t>(j)}),
Common typedefs and tag types for the Cellular Automata module.
Adaptor: apply a mono-field rule to field I, leave the other fields unchanged.
Gray-Scott reaction-diffusion rule.
Dense odd-sized 2-D convolution kernel.
T neighbour_weight(const std::size_t k) const
Return a centre-skipping neighbour weight.
constexpr T center() const noexcept
Return the centre weight.
Lattice that adds boundary-aware access on top of a storage.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Synchronous double-buffered engine for multi-field lattices.
void step()
Apply the multi-field rule to every cell once and swap buffers.
Multi-field lattice with selectable storage layout.
auto & field()
Mutable reference to the underlying single-field lattice for field I.
void set(const coord_type &c, const V &v)
Strict write access to field I.
Synchronous double-buffered engine.
Von Neumann (L1) neighborhood of radius R in N dimensions.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_gamma_function > > gamma(const __gmp_expr< T, U > &expr)
Freq_Node * pred
Predecessor node in level-order traversal.
constexpr std::uint32_t delta
File contains only the cells that changed relative to a baseline.
constexpr Game_Of_Life_Rule make_game_of_life_rule() noexcept
Build the canonical Game of Life rule.
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.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Outer_Totalistic_Rule< Game_Of_Life_Functor > Game_Of_Life_Rule
Outer-totalistic rule type implementing Conway's Game of Life.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Out-of-range neighbours behave as if the lattice ended.
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.
Phase 14 synchronous double-buffered engine for multi-field cellular automata.
Phase 14 multi-field cellular automata lattice (AoS / SoA).
Phase 14 multi-field local rules for Aleph::CA.
Neighborhoods catalogue for Aleph::CA.
Rule mechanisms for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).