Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
- name: Build docs using sphinx
run: |
cd docs
poetry run sphinx-build --jobs auto -W --keep-going source build
poetry run sphinx-build -W --keep-going source build

- name: Archive built docs for error checking on failure
if: failure()
Expand Down
3 changes: 3 additions & 0 deletions virtual_ecosystem/core/model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class CoreConstants(Configuration):
gravity: float = constants.gravitational_constant
"""Newtonian constant of gravitation, [m s-1]."""

boltzmann_constant: float = constants.Boltzmann
"""The Boltzmann constant, [J K-1]"""

stefan_boltzmann_constant: float = constants.Stefan_Boltzmann
"""Stefan-Boltzmann constant, [W m-2 K-4].

Expand Down
14 changes: 10 additions & 4 deletions virtual_ecosystem/models/animal/animal_cohorts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import virtual_ecosystem.models.animal.scaling_functions as sf
from virtual_ecosystem.core.grid import Grid
from virtual_ecosystem.core.logger import LOGGER
from virtual_ecosystem.core.model_config import CoreConstants
from virtual_ecosystem.models.animal.animal_traits import VerticalOccupancy
from virtual_ecosystem.models.animal.cnp import CNP
from virtual_ecosystem.models.animal.decay import (
Expand Down Expand Up @@ -42,6 +43,7 @@ def __init__(
centroid_key: int,
grid: Grid,
constants: AnimalConstants = AnimalConstants(),
core_constants: CoreConstants = CoreConstants(),
) -> None:
if age < 0:
raise ValueError("Age must be a positive number.")
Expand All @@ -63,6 +65,8 @@ def __init__(
"""The the grid structure of the simulation."""
self.constants = constants
"""Animal constants."""
self.core_constants = core_constants
"""Core constants."""
self.location_status: Literal["active", "migrated", "aquatic"] = "active"
"""Location status of the cohort, active means present and participating."""
self.remaining_time_away: float = 0.0
Expand Down Expand Up @@ -255,10 +259,12 @@ def metabolize(self, temperature: float, dt: timedelta64) -> dict[str, float]:

# Calculate potential carbon metabolized (kg/day * number of days)
potential_carbon_metabolized = sf.metabolic_rate(
self.mass_current,
temperature,
self.functional_group.metabolic_rate_terms,
self.functional_group.metabolic_type,
mass=self.mass_current,
temperature=temperature,
terms=self.functional_group.metabolic_rate_terms,
metabolic_type=self.functional_group.metabolic_type,
metabolic_scaling_coefficients=self.constants.metabolic_scaling_coefficients,
boltzmann_constant=self.core_constants.boltzmann_constant,
) * float(dt / timedelta64(1, "D"))

# Ensure metabolized carbon does not exceed available carbon
Expand Down
1 change: 1 addition & 0 deletions virtual_ecosystem/models/animal/animal_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,7 @@ def create_new_cohort(
centroid_key=centroid_key,
grid=self.data.grid,
constants=self.model_constants,
core_constants=self.core_constants,
)

self.assign_prey_groups(cohort)
Expand Down
16 changes: 16 additions & 0 deletions virtual_ecosystem/models/animal/model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ def get_population_density_terms(
madingley_biomass_scaling_terms: tuple[float, float] = (0.6, 300000.0)
"""Biomass scaling terms from the Madingley model."""

metabolic_scaling_coefficients: tuple[float, float, float] = (
0.037, # Es
0.5, # sig
0.69, # Ea
)
"""Metabolic rate scaling coefficients.

These are the coefficients of Madingley style scaling of metabolic rate with mass
and temperature, assuming a power-law relationship with mass and an exponential
relationship with temperature. The three values are:

* $E_s$ - energy to mass conversion constant (g/kJ)
* $\sigma$ - proportion of time-step with temp in active range (toy default value)
* $E_a$ - aggregate activation energy of metabolic reactions
"""

metabolic_rate_terms: dict[MetabolicType, dict[str, tuple[float, float]]] = Field(
default_factory=lambda: {
MetabolicType.ENDOTHERMIC: {
Expand Down
21 changes: 13 additions & 8 deletions virtual_ecosystem/models/animal/scaling_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

import numpy as np

from virtual_ecosystem.core.model_config import CoreConstants
from virtual_ecosystem.models.animal.animal_traits import DietType, MetabolicType

# from virtual_ecosystem.models.animal.constants import BOLTZMANN_CONSTANT
from virtual_ecosystem.models.animal.functional_group import FunctionalGroup
from virtual_ecosystem.models.animal.model_config import AnimalConstants


def damuths_law(mass: float, terms: tuple) -> float:
Expand Down Expand Up @@ -75,29 +75,34 @@ def metabolic_rate(
temperature: float,
terms: dict,
metabolic_type: MetabolicType,
metabolic_scaling_coefficients: tuple[
float, float, float
] = AnimalConstants().metabolic_scaling_coefficients,
boltzmann_constant: float = CoreConstants().boltzmann_constant,
) -> float:
"""Calculates metabolic rate in grams of body mass per day.
r"""Calculates metabolic rate in grams of body mass per day.

This follows the Madingley implementation, assuming a power-law relationship with
mass and an exponential relationship with temperature.

TODO: Implement activity windows to properly parameterize sigma.
TODO: Move constants to constants file.

Args:
mass: The body-mass [kg] of an AnimalCohort.
temperature: The temperature [Celsius] of the environment.
terms: The tuple of metabolic rate terms used.
metabolic_type: The metabolic type of the animal [ENDOTHERMIC or ECTOTHERMIC].
metabolic_scaling_coefficients: A tuple providing the $E_s, \sigma, E_a$
coefficients of the Madingley metabolic rate model (see
:attr:`~virtual_ecosystem.models.animal.model_config.AnimalConstants.metabolic_scaling_coefficients`)
boltzmann_constant: The Boltzmann constant ($k_B$)

Returns:
The metabolic rate of an individual of the given cohort in [g/d].
"""

Es = 3.7 * 10 ** (-2) # energy to mass conversion constant (g/kJ)
sig = 0.5 # proportion of time-step with temp in active range (toy)
Ea = 0.69 # aggregate activation energy of metabolic reactions
kB = 1.380649e-23
Es, sig, Ea = metabolic_scaling_coefficients
kB = boltzmann_constant
mass_g = mass * 1000 # convert mass to grams

if metabolic_type == MetabolicType.ENDOTHERMIC:
Expand Down