MalariaSentinel — C++ reference
The contract-defining headers of the ABM engine
Loading...
Searching...
No Matches
habitat_availability.hpp
1
21#pragma once
22
23#include "mal_abm_fast/config.hpp"
24
25#include <algorithm>
26#include <array>
27#include <cmath>
28#include <cstddef>
29#include <cstdint>
30
31namespace mal_abm_fast {
32
40
41
47 float jrc_persistent = 0.0f;
50 float lc_built = 0.0f;
54 float lc_wetland = 0.0f;
58 float lc_open_plus_water = 0.0f;
61 float lc_crop = 0.0f;
62
66 static HabitatFeatures from_bands(float jrc, float built, float wetland,
67 float open_plus, float crop) {
68 return HabitatFeatures{std::max(0.0f, jrc), std::max(0.0f, built),
69 std::max(0.0f, wetland), std::max(0.0f, open_plus),
70 std::max(0.0f, crop)};
71 }
72};
73
76inline std::array<float, kNumZoneId> habitat_potential(
77 const HabitatFeatures& f) {
78 std::array<float, kNumZoneId> h{};
79 h[static_cast<size_t>(ZoneId::JRC_PERSISTENT)] =
80 std::clamp(f.jrc_persistent, 0.0f, 1.0f);
81 // Urban persistent supply — extends the lease.
82 h[static_cast<size_t>(ZoneId::URBAN_PERSISTENT)] =
83 std::clamp(static_cast<float>(
84 load_config().urban.beta_urban) *
85 f.lc_built,
86 0.0f, 1.0f);
87 h[static_cast<size_t>(ZoneId::WETLAND)] =
88 std::clamp(f.lc_wetland, 0.0f, 1.0f);
89 // Open pluvial: open/low-veg + water + rain pools on the built surface…
90 // (documented rule; does NOT double-count man-made supply: pools go
91 // under a distinct habitat type and a distinct temporal dynamic.)
92 h[static_cast<size_t>(ZoneId::OPEN_PLUVIAL)] =
93 std::clamp(f.lc_open_plus_water +
94 static_cast<float>(load_config()
95 .urban.rain_pools_built_damage) *
96 f.lc_built,
97 0.0f, 1.0f);
98 h[static_cast<size_t>(ZoneId::CROP)] =
99 std::clamp(f.lc_crop, 0.0f, 1.0f);
100 h[static_cast<size_t>(ZoneId::FOREST)] = 0.0f;
101 return h;
102}
103
107inline float habitat_total_potential(const std::array<float, kNumZoneId>& h) {
108 float total = 0.0f;
109 for (const float v : h) total += v;
110 return std::min(total, 1.0f);
111}
112
118inline float update_z_step(float z, float rain_mm, float delta_days) {
119 const auto& pluvial = load_config().pluvial;
120 const float z_dry =
121 z * std::exp(-delta_days / static_cast<float>(pluvial.dry_memory_days));
122 const float fill =
123 1.0f - std::exp(-std::max(0.0f, rain_mm) /
124 static_cast<float>(pluvial.rain_scale_mm));
125 return std::clamp(z_dry + (1.0f - z_dry) * fill, 0.0f, 1.0f);
126}
127
131
134enum class SpeciesId : std::size_t {
135 FUNESTUS = 0,
136 GAMBIAE_SS,
137 COLUZZII,
138 ARABIENSIS,
139};
140
144inline const char* species_name(SpeciesId id) {
145 switch (id) {
146 case SpeciesId::FUNESTUS: return "funestus";
147 case SpeciesId::GAMBIAE_SS: return "gambiae_ss";
148 case SpeciesId::COLUZZII: return "coluzzii";
149 case SpeciesId::ARABIENSIS: return "arabiensis";
150 }
151 return "unknown";
152}
153
157inline std::array<float, kNumZoneId> default_species_weights(SpeciesId s) {
158 const auto& rows = load_config().species.rows;
159 const auto it = rows.find(species_name(s));
160 if (it != rows.end()) {
161 std::array<float, kNumZoneId> out{};
162 for (size_t g = 0; g < kNumZoneId; ++g)
163 out[g] = static_cast<float>(it->second.weights[g]);
164 return out;
165 }
166 return {};
167}
168
171inline std::array<float, kNumZoneId> default_species_weights() {
172 std::array<float, kNumZoneId> out{};
173 const auto& w = load_config().species.neutral_weights;
174 for (size_t g = 0; g < kNumZoneId; ++g) out[g] = static_cast<float>(w[g]);
175 return out;
176}
177
181
186inline float species_accuracy(const std::array<float, kNumZoneId>& h,
187 const std::array<float, kNumZoneId>& w,
188 float z_pluvial) {
189 // Persistent/pluvial split comes from habitat_types.dynamics (names),
190 // precomputed as a ZoneId bitset at load time (no positional rule).
191 const auto& persistent_zone = load_config().habitat_types.persistent_zone;
192 float s = 0.0f;
193 for (size_t g = 0; g < kNumZoneId; ++g) {
194 if (w[g] <= 0.0f || h[g] <= 0.0f) continue;
195 const float availability =
196 persistent_zone[g]
197 ? static_cast<float>(
199 : std::clamp(z_pluvial, 0.0f, 1.0f);
200 s += w[g] * h[g] * availability;
201 }
202 return std::clamp(s, 0.0f, 1.0f);
203}
204
210inline double k_habitat(double s, double cell_area_km2) {
211 return load_config().species.k_ref * cell_area_km2 * std::max(0.0, s);
212}
213
215inline double cell_area_km2(double lat_deg, double cell_size_m) {
216 // Equirectangular approximation, adequate at the scale of a single cell:
217 // area = dx * dy where dx ≈ cell_size * cos(lat).
218 constexpr double kDegToRad = 3.14159265358979323846 / 180.0;
219 const double dx_m = cell_size_m * std::cos(lat_deg * kDegToRad);
220 return (dx_m * cell_size_m) / 1.0e6;
221}
222
223} // namespace mal_abm_fast
Definition wire.hpp:33
double cell_area_km2(double lat_deg, double cell_size_m)
Approximate cell area (km²) at a given latitude and edge cell size (m).
Definition habitat_availability.hpp:215
float species_accuracy(const std::array< float, kNumZoneId > &h, const std::array< float, kNumZoneId > &w, float z_pluvial)
Definition habitat_availability.hpp:186
SpeciesId
Definition habitat_availability.hpp:134
const char * species_name(SpeciesId id)
Definition habitat_availability.hpp:144
const ModelConfig & load_config()
float habitat_total_potential(const std::array< float, kNumZoneId > &h)
Definition habitat_availability.hpp:107
std::array< float, kNumZoneId > habitat_potential(const HabitatFeatures &f)
Definition habitat_availability.hpp:76
double k_habitat(double s, double cell_area_km2)
Definition habitat_availability.hpp:210
std::array< float, kNumZoneId > default_species_weights(SpeciesId s)
Definition habitat_availability.hpp:157
float update_z_step(float z, float rain_mm, float delta_days)
Definition habitat_availability.hpp:118
double persistent_availability
Definition config.hpp:191
std::array< bool, kNumZoneId > persistent_zone
Definition config.hpp:63
Static bundle of features read from the env contract (v3.0 planes).
Definition habitat_availability.hpp:43
float jrc_persistent
Definition habitat_availability.hpp:47
float lc_open_plus_water
Definition habitat_availability.hpp:58
float lc_crop
Definition habitat_availability.hpp:61
float lc_built
Definition habitat_availability.hpp:50
float lc_wetland
Definition habitat_availability.hpp:54
static HabitatFeatures from_bands(float jrc, float built, float wetland, float open_plus, float crop)
Definition habitat_availability.hpp:66
SpeciesCfg species
Definition config.hpp:560
CapacityCfg capacity
Definition config.hpp:563
HabitatCfg habitat_types
Definition config.hpp:548
PluvialCfg pluvial
Definition config.hpp:552
double k_ref
[ind·km⁻²·d⁻¹]
Definition config.hpp:177
std::array< double, kNumZoneId > neutral_weights
Definition config.hpp:182
std::map< std::string, SpeciesRow > rows
Definition config.hpp:178