mal_core.habitat.availability

Shared temporal availability responses A[i,k(g),t].

Two responses only (plan 02 §5.2-§5.4):

  • persistent — A = capacity.persistent_availability every day (MVP: the persistent offer is not reduced by extraordinary drought; a future calibrated scarcity term is a documented extension, never part of this contract).
  • pluvial — state z[i,t] in [0, 1] with exponential drying memory and saturating rain recharge:

    z_dry = z_prev * exp(-delta_days / pluvial.dry_memory_days)
    z     = z_dry + (1 - z_dry) * (1 - exp(-rain_mm / pluvial.rain_scale_mm))
    

    Applied once per (cell, calendar day) in the daily ABM loop — never per patch and never per species. Causal warm-up (plan §6.5 rule of daily step from previous dates): the state before the first simulated day is integrated forward from the days of the env NC that precede it, with z[i, 0] = 0 at the cold start, so the first available day is not artificially dry.

The Python functions here mirror mal_abm_fast::update_z_series / mal_abm_fast::habitat_availability.hpp 1:1.

 1"""Shared temporal availability responses A[i,k(g),t].
 2
 3Two responses only (plan 02 §5.2-§5.4):
 4
 5* ``persistent``   — A = capacity.persistent_availability every day (MVP:
 6  the persistent offer is not reduced by extraordinary drought; a future
 7  calibrated scarcity term is a documented extension, never part of this
 8  contract).
 9* ``pluvial``      — state z[i,t] in [0, 1] with exponential drying memory
10  and saturating rain recharge:
11
12      z_dry = z_prev * exp(-delta_days / pluvial.dry_memory_days)
13      z     = z_dry + (1 - z_dry) * (1 - exp(-rain_mm / pluvial.rain_scale_mm))
14
15  Applied once per (cell, calendar day) in the daily ABM loop — never per
16  patch and never per species. Causal warm-up (plan §6.5 rule of daily
17  step from previous dates): the state before the first simulated day is
18  integrated forward from the days of the env NC that precede it, with
19  z[i, 0] = 0 at the cold start, so the first available day is not
20  artificially dry.
21
22The Python functions here mirror ``mal_abm_fast::update_z_series`` /
23``mal_abm_fast::habitat_availability.hpp`` 1:1.
24"""
25from __future__ import annotations
26
27import math
28
29import numpy as np
30
31from .config import CONFIG
32
33_PLUVIAL_RAIN_SCALE_MM = float(CONFIG["pluvial"]["rain_scale_mm"])
34_PLUVIAL_DRY_MEMORY_DAYS = float(CONFIG["pluvial"]["dry_memory_days"])
35_DELTA_DAYS = float(CONFIG["pluvial"]["delta_days"])
36
37
38def update_pluvial_state(
39    z_prev: np.ndarray | float,
40    rain_mm: np.ndarray | float,
41    *, delta_days: float | None = None,
42) -> np.ndarray | float:
43    """One pluvial-response step (pure; state z in [0, 1])."""
44    import numpy as np
45
46    if delta_days is None:
47        delta_days = _DELTA_DAYS
48    z_arr = np.atleast_1d(np.asarray(z_prev, dtype=np.float64))
49    rain = np.atleast_1d(np.asarray(rain_mm, dtype=np.float64))
50    z_dry = z_arr * math.exp(-float(delta_days) / _PLUVIAL_DRY_MEMORY_DAYS)
51    recharge = np.exp(-rain / _PLUVIAL_RAIN_SCALE_MM)
52    z = z_dry + (1.0 - z_dry) * (1.0 - recharge)
53    z = np.clip(z, 0.0, 1.0)
54    if np.isscalar(z_prev) and np.isscalar(rain_mm):
55        return float(z[0])
56    return z
57
58
59def warm_up_pluvial(
60    rain_series: np.ndarray, *, cold_start: float | None = None
61) -> np.ndarray:
62    """Integrate the pluvial response over a (n_steps,) rain series.
63
64    Returns the z state after the last step (or the cold start value when
65    the series is empty). Used by the C++ engine before the first
66    simulated day and by tests to prove warm-up causality: a heavy rain
67    before day 0 must raise day-0 availability, and a dry warm-up must
68    leave it at the cold start.
69    """
70    if cold_start is None:
71        cold_start = float(CONFIG["pluvial"]["cold_start_z"])
72    z: np.ndarray | float = cold_start
73    rain_series = np.asarray(rain_series, dtype=np.float64)
74    for r in rain_series:
75        z = update_pluvial_state(z, float(r), delta_days=_DELTA_DAYS)
76    return _as_array(z)
77
78
79def _as_array(x) -> np.ndarray:
80    """Return ``x`` as a float64 array with NaN replaced by 0.0."""
81    arr = np.asarray(x, dtype=np.float64)
82    return np.nan_to_num(arr, nan=0.0)
83
84
85__all__ = ["update_pluvial_state", "warm_up_pluvial"]
def update_pluvial_state( z_prev: numpy.ndarray | float, rain_mm: numpy.ndarray | float, *, delta_days: float | None = None) -> numpy.ndarray | float:
39def update_pluvial_state(
40    z_prev: np.ndarray | float,
41    rain_mm: np.ndarray | float,
42    *, delta_days: float | None = None,
43) -> np.ndarray | float:
44    """One pluvial-response step (pure; state z in [0, 1])."""
45    import numpy as np
46
47    if delta_days is None:
48        delta_days = _DELTA_DAYS
49    z_arr = np.atleast_1d(np.asarray(z_prev, dtype=np.float64))
50    rain = np.atleast_1d(np.asarray(rain_mm, dtype=np.float64))
51    z_dry = z_arr * math.exp(-float(delta_days) / _PLUVIAL_DRY_MEMORY_DAYS)
52    recharge = np.exp(-rain / _PLUVIAL_RAIN_SCALE_MM)
53    z = z_dry + (1.0 - z_dry) * (1.0 - recharge)
54    z = np.clip(z, 0.0, 1.0)
55    if np.isscalar(z_prev) and np.isscalar(rain_mm):
56        return float(z[0])
57    return z

One pluvial-response step (pure; state z in [0, 1]).

def warm_up_pluvial( rain_series: numpy.ndarray, *, cold_start: float | None = None) -> numpy.ndarray:
60def warm_up_pluvial(
61    rain_series: np.ndarray, *, cold_start: float | None = None
62) -> np.ndarray:
63    """Integrate the pluvial response over a (n_steps,) rain series.
64
65    Returns the z state after the last step (or the cold start value when
66    the series is empty). Used by the C++ engine before the first
67    simulated day and by tests to prove warm-up causality: a heavy rain
68    before day 0 must raise day-0 availability, and a dry warm-up must
69    leave it at the cold start.
70    """
71    if cold_start is None:
72        cold_start = float(CONFIG["pluvial"]["cold_start_z"])
73    z: np.ndarray | float = cold_start
74    rain_series = np.asarray(rain_series, dtype=np.float64)
75    for r in rain_series:
76        z = update_pluvial_state(z, float(r), delta_days=_DELTA_DAYS)
77    return _as_array(z)

Integrate the pluvial response over a (n_steps,) rain series.

Returns the z state after the last step (or the cold start value when the series is empty). Used by the C++ engine before the first simulated day and by tests to prove warm-up causality: a heavy rain before day 0 must raise day-0 availability, and a dry warm-up must leave it at the cold start.