feat(biology): procedural currents, habitat format 3 and moving food
Phase 2 biology. CellularFlowField generates 2-n seeded closed looping currents inside the region, each with its own width, speed and direction, strongest at the centreline and still at the stream edges and walls, rasterized to a Q10 grid with integer bilinear sampling. The field digest matches across g++-14, clang-19 and MSVC, so clients regenerate it from the configuration. Habitat format 3 adds per-deposit induced drift, sub-micrometre carry and a merged count, with up to 8,192 deposits. advect_cellular_deposits moves bubbles with the flow plus drift from passing cells (falloff and decay), merges touching bubbles into the lower id up to the merged cap, keeps pools fixed and addresses unique. A 2,000-tick run of 1,800 bubbles conserves stock exactly and leaves nothing piled at the walls; it costs under 0.7 ms per tick optimized. The tuned grid pitch is capped at 10 mm. Refreshes the DEV session window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
#include "evo/biology/cellular_currents.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace {
|
||||
using namespace evo::biology;
|
||||
void check(bool value, std::source_location at = std::source_location::current()) {
|
||||
if (!value)
|
||||
throw std::runtime_error("cellular currents line " + std::to_string(at.line()));
|
||||
}
|
||||
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
||||
|
||||
constexpr std::int64_t um_per_s = cellular_velocity_q10_per_um_s;
|
||||
|
||||
// The v0 tuning: 60 x 60 mm, 2-4 routes, 3-6 mm wide, 60-300 um/s, 250 um grid.
|
||||
CellularCurrentConfig v0(std::uint64_t seed) {
|
||||
return {seed, 60'000, 60'000, 250, 2, 4, 3'000, 6'000, 60, 300};
|
||||
}
|
||||
|
||||
CellularFlowField generate(const CellularCurrentConfig& config) {
|
||||
auto field = CellularFlowField::generate(config);
|
||||
check(field.has_value());
|
||||
return std::move(*field);
|
||||
}
|
||||
|
||||
std::int64_t speed(CellularFlowSample sample) {
|
||||
const auto x = static_cast<double>(sample.x_q10), y = static_cast<double>(sample.y_q10);
|
||||
return std::llround(std::sqrt(x * x + y * y));
|
||||
}
|
||||
|
||||
void deterministic_generation() {
|
||||
const auto first = generate(v0(7));
|
||||
const auto second = generate(v0(7));
|
||||
CHECK(first.digest() == second.digest() && first.routes().size() == second.routes().size());
|
||||
CHECK(generate(v0(8)).digest() != first.digest());
|
||||
CHECK(first.columns() == 242 && first.rows() == 242);
|
||||
std::printf("current field digest %llu\n", static_cast<unsigned long long>(first.digest()));
|
||||
CHECK(first.digest() == 6'763'241'105'898'603'476ULL);
|
||||
}
|
||||
|
||||
void routes_within_configuration() {
|
||||
for (std::uint64_t seed = 1; seed <= 40; ++seed) {
|
||||
const auto field = generate(v0(seed));
|
||||
CHECK(field.routes().size() >= 2 && field.routes().size() <= 4);
|
||||
for (const auto& route : field.routes()) {
|
||||
CHECK(route.width_um >= 3'000 && route.width_um <= 6'000);
|
||||
CHECK(route.speed_um_per_s >= 60 && route.speed_um_per_s <= 300);
|
||||
CHECK(route.points.size() == cellular_current_route_points);
|
||||
const std::int64_t clear = route.width_um / 2;
|
||||
for (const auto& point : route.points)
|
||||
CHECK(point.x_um > clear && point.x_um < 60'000 - clear && point.y_um > clear &&
|
||||
point.y_um < 60'000 - clear);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void bounded_with_quiet_water_and_still_walls() {
|
||||
for (std::uint64_t seed = 1; seed <= 20; ++seed) {
|
||||
const auto field = generate(v0(seed));
|
||||
std::size_t still{}, flowing{};
|
||||
for (const auto& node : field.nodes()) {
|
||||
CHECK(speed(node) <= 300 * um_per_s);
|
||||
(node.x_q10 == 0 && node.y_q10 == 0 ? still : flowing) += 1;
|
||||
}
|
||||
// Both quiet water and currents exist in every region.
|
||||
CHECK(still > field.nodes().size() / 5 && flowing > field.nodes().size() / 20);
|
||||
// Walls are still, so nothing is carried into them.
|
||||
for (std::int64_t along = 0; along <= 60'000; along += 250) {
|
||||
CHECK(field.sample(0, along) == CellularFlowSample{});
|
||||
CHECK(field.sample(60'000, along) == CellularFlowSample{});
|
||||
CHECK(field.sample(along, 0) == CellularFlowSample{});
|
||||
CHECK(field.sample(along, 60'000) == CellularFlowSample{});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void route_centreline_runs_at_route_speed() {
|
||||
const auto field = generate(v0(3));
|
||||
const auto& route = field.routes().front();
|
||||
const auto& a = route.points[4];
|
||||
const auto& b = route.points[5];
|
||||
const auto middle = field.sample((a.x_um + b.x_um) / 2, (a.y_um + b.y_um) / 2);
|
||||
const auto expected = std::int64_t{route.speed_um_per_s} * um_per_s;
|
||||
// Near full speed at the centreline, allowing for overlap and the grid.
|
||||
CHECK(speed(middle) > expected * 8 / 10 && speed(middle) <= expected);
|
||||
// Flow follows the route direction.
|
||||
const auto along = (b.x_um - a.x_um) * middle.x_q10 + (b.y_um - a.y_um) * middle.y_q10;
|
||||
CHECK(along > 0);
|
||||
}
|
||||
|
||||
void interpolation_is_continuous() {
|
||||
const auto field = generate(v0(5));
|
||||
for (std::int64_t y = 1'000; y < 59'000; y += 1'777)
|
||||
for (std::int64_t x = 1'000; x < 59'000; x += 13) {
|
||||
const auto here = field.sample(x, y), next = field.sample(x + 1, y);
|
||||
CHECK(std::abs(std::int64_t{here.x_q10} - next.x_q10) < 4 * um_per_s);
|
||||
CHECK(std::abs(std::int64_t{here.y_q10} - next.y_q10) < 4 * um_per_s);
|
||||
}
|
||||
}
|
||||
|
||||
// A passive cell placed in a stream circulates for minutes without reaching a wall.
|
||||
void passive_drift_circulates() {
|
||||
const auto field = generate(v0(11));
|
||||
const auto& start = field.routes().front().points.front();
|
||||
constexpr std::uint32_t extent = 20;
|
||||
CellularPosition position{static_cast<std::uint64_t>(start.x_um - extent / 2),
|
||||
static_cast<std::uint64_t>(start.y_um - extent / 2)};
|
||||
CellularMotionState state{};
|
||||
const CellularHandling passive{.forward_drag_ppm = 95'163, .lateral_drag_ppm = 95'163};
|
||||
std::uint64_t travelled{};
|
||||
for (int tick = 0; tick < 20 * 60 * 5; ++tick) {
|
||||
const auto next =
|
||||
step_cellular_kinematics({60'000, 60'000}, position, extent, state, {}, passive,
|
||||
field.at_footprint(position, extent), {});
|
||||
CHECK(next.has_value());
|
||||
travelled += next->path_um;
|
||||
position = next->position;
|
||||
state = next->state;
|
||||
CHECK(position.x_um > 0 && position.y_um > 0 && position.x_um < 60'000 - extent &&
|
||||
position.y_um < 60'000 - extent);
|
||||
}
|
||||
// Five minutes in a 60-300 um/s stream covers several centimetres.
|
||||
CHECK(travelled > 10'000);
|
||||
}
|
||||
|
||||
void invalid_configurations() {
|
||||
auto bad = v0(1);
|
||||
bad.pitch_um = 0;
|
||||
CHECK(!CellularFlowField::generate(bad));
|
||||
bad = v0(1);
|
||||
bad.pitch_um = max_cellular_current_pitch_um + 1;
|
||||
CHECK(!CellularFlowField::generate(bad));
|
||||
bad = v0(1);
|
||||
bad.min_count = 5;
|
||||
CHECK(!CellularFlowField::generate(bad));
|
||||
bad = v0(1);
|
||||
bad.max_speed_um_per_s = 2'000'000;
|
||||
CHECK(!CellularFlowField::generate(bad));
|
||||
bad = v0(1);
|
||||
bad.width_um = 3'000;
|
||||
const auto tiny = CellularFlowField::generate(bad);
|
||||
CHECK(!tiny && tiny.error() == CellularCurrentError::region_too_small);
|
||||
auto none = v0(1);
|
||||
none.min_count = none.max_count = 0;
|
||||
const auto still = generate(none);
|
||||
CHECK(still.routes().empty() && still.sample(30'000, 30'000) == CellularFlowSample{});
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
try {
|
||||
routes_within_configuration();
|
||||
bounded_with_quiet_water_and_still_walls();
|
||||
route_centreline_runs_at_route_speed();
|
||||
interpolation_is_continuous();
|
||||
passive_drift_circulates();
|
||||
invalid_configurations();
|
||||
deterministic_generation();
|
||||
} catch (const std::exception& error) {
|
||||
std::fprintf(stderr, "%s\n", error.what());
|
||||
return 1;
|
||||
}
|
||||
std::puts("cellular currents contracts passed");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user