Skip to content

Commit

Permalink
Merge pull request #374 from ImperialCollegeLondon/369-simplifying-th…
Browse files Browse the repository at this point in the history
…e-model-creation-setup

Defining the CoreComponents, LayerStructure and ModelTiming classes
  • Loading branch information
davidorme authored Feb 5, 2024
2 parents 24b49fd + b4fb178 commit 76f1906
Show file tree
Hide file tree
Showing 32 changed files with 1,631 additions and 1,380 deletions.
24 changes: 24 additions & 0 deletions docs/source/api/core/core_components.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
jupytext:
cell_metadata_filter: -all
formats: md:myst
main_language: python
text_representation:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.13.8
kernelspec:
display_name: vr_python3
language: python
name: vr_python3
---

# API documentation for the {mod}`~virtual_rainforest.core.core_components` module

```{eval-rst}
.. automodule:: virtual_rainforest.core.core_components
:autosummary:
:members:
:special-members: __post_init__
```
5 changes: 5 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ class MyReferenceStyle(AuthorYearReferenceStyle):
nitpick_ignore = [
("py:class", "numpy.int64"),
("py:class", "numpy.float32"),
# HACK - core_components docstrings are being odd.
("py:class", "np.timedelta64"),
("py:class", "np.datetime64"),
("py:class", "InitVar"),
("py:class", "Quantity"),
("py:class", "numpy._typing._array_like._ScalarType_co"),
# TODO - Delete this once Vivienne has merged this feature into develop
("py:class", "virtual_rainforest.models.abiotic.energy_balance.EnergyBalance"),
Expand Down
1 change: 1 addition & 0 deletions docs/source/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ team.
File readers <api/core/readers.md>
Core axes <api/core/axes.md>
Base Model <api/core/base_model.md>
Core Components <api/core/core_components.md>
Core Constants <api/core/constants.md>
Constants Classes <api/core/constants_class.md>
Constants Loader <api/core/constants_loader.md>
Expand Down
159 changes: 134 additions & 25 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,99 @@ def data_instance():


@pytest.fixture
def dummy_carbon_data(layer_roles_fixture):
def fixture_config():
"""Simple configuration fixture for use in tests."""

from virtual_rainforest.core.config import Config

cfg_string = """
[core]
[core.grid]
cell_nx = 10
cell_ny = 10
[core.timing]
start_date = "2020-01-01"
update_interval = "2 weeks"
run_length = "50 years"
[core.data_output_options]
save_initial_state = true
save_final_state = true
out_initial_file_name = "model_at_start.nc"
out_final_file_name = "model_at_end.nc"
[core.layers]
canopy_layers = 10
soil_layers = [-0.25, -1.0]
above_canopy_height_offset = 2.0
surface_layer_height = 0.1
subcanopy_layer_height = 1.5
[plants]
a_plant_integer = 12
[[plants.ftypes]]
pft_name = "shrub"
max_height = 1.0
[[plants.ftypes]]
pft_name = "broadleaf"
max_height = 50.0
[[animals.functional_groups]]
name = "carnivorous_bird"
taxa = "bird"
diet = "carnivore"
metabolic_type = "endothermic"
birth_mass = 0.1
adult_mass = 1.0
[[animals.functional_groups]]
name = "herbivorous_bird"
taxa = "bird"
diet = "herbivore"
metabolic_type = "endothermic"
birth_mass = 0.05
adult_mass = 0.5
[[animals.functional_groups]]
name = "carnivorous_mammal"
taxa = "mammal"
diet = "carnivore"
metabolic_type = "endothermic"
birth_mass = 4.0
adult_mass = 40.0
[[animals.functional_groups]]
name = "herbivorous_mammal"
taxa = "mammal"
diet = "herbivore"
metabolic_type = "endothermic"
birth_mass = 1.0
adult_mass = 10.0
[[animals.functional_groups]]
name = "carnivorous_insect"
taxa = "insect"
diet = "carnivore"
metabolic_type = "ectothermic"
birth_mass = 0.001
adult_mass = 0.01
[[animals.functional_groups]]
name = "herbivorous_insect"
taxa = "insect"
diet = "herbivore"
metabolic_type = "ectothermic"
birth_mass = 0.0005
adult_mass = 0.005
"""

return Config(cfg_strings=cfg_string)


@pytest.fixture
def fixture_core_components(fixture_config):
"""A CoreComponents instance for use in testing."""
from virtual_rainforest.core.core_components import CoreComponents

return CoreComponents(fixture_config)


@pytest.fixture
def dummy_carbon_data(fixture_core_components):
"""Creates a dummy carbon data object for use in tests."""

from virtual_rainforest.core.data import Data
Expand Down Expand Up @@ -206,7 +298,10 @@ def dummy_carbon_data(layer_roles_fixture):
data["soil_moisture"] = data["soil_moisture"].assign_coords(
{
"layers": np.arange(0, 15),
"layer_roles": ("layers", layer_roles_fixture),
"layer_roles": (
"layers",
fixture_core_components.layer_structure.layer_roles,
),
"cell_id": data.grid.cell_id,
}
)
Expand All @@ -222,7 +317,10 @@ def dummy_carbon_data(layer_roles_fixture):
).assign_coords(
{
"layers": np.arange(0, 15),
"layer_roles": ("layers", layer_roles_fixture),
"layer_roles": (
"layers",
fixture_core_components.layer_structure.layer_roles,
),
"cell_id": data.grid.cell_id,
}
)
Expand All @@ -242,7 +340,10 @@ def dummy_carbon_data(layer_roles_fixture):
.assign_coords(
{
"layers": np.arange(0, 15),
"layer_roles": ("layers", layer_roles_fixture),
"layer_roles": (
"layers",
fixture_core_components.layer_structure.layer_roles,
),
"cell_id": data.grid.cell_id,
}
)
Expand All @@ -252,15 +353,15 @@ def dummy_carbon_data(layer_roles_fixture):


@pytest.fixture
def top_soil_layer_index(layer_roles_fixture):
def top_soil_layer_index(fixture_core_components):
"""The index of the top soil layer in the data fixtures."""
return next(i for i, v in enumerate(layer_roles_fixture) if v == "soil")
return fixture_core_components.layer_structure.layer_roles.index("soil")


@pytest.fixture
def surface_layer_index(layer_roles_fixture):
def surface_layer_index(fixture_core_components):
"""The index of the top soil layer in the data fixtures."""
return next(i for i, v in enumerate(layer_roles_fixture) if v == "surface")
return fixture_core_components.layer_structure.layer_roles.index("surface")


@pytest.fixture
Expand Down Expand Up @@ -297,17 +398,7 @@ def run_validation(


@pytest.fixture
def layer_roles_fixture():
"""Create list of layer roles for 10 canopy layers and 2 soil layers."""
from virtual_rainforest.models.abiotic_simple.abiotic_simple_model import (
set_layer_roles,
)

return set_layer_roles(10, [-0.25, -1.0])


@pytest.fixture
def dummy_climate_data(layer_roles_fixture):
def dummy_climate_data(fixture_core_components):
"""Creates a dummy climate data object for use in tests."""

from virtual_rainforest.core.data import Data
Expand Down Expand Up @@ -354,7 +445,10 @@ def dummy_climate_data(layer_roles_fixture):
dims=["layers", "cell_id"],
coords={
"layers": np.arange(15),
"layer_roles": ("layers", layer_roles_fixture),
"layer_roles": (
"layers",
fixture_core_components.layer_structure.layer_roles,
),
"cell_id": data.grid.cell_id,
},
name="evapotranspiration",
Expand All @@ -365,7 +459,10 @@ def dummy_climate_data(layer_roles_fixture):
dims=["layers", "cell_id"],
coords={
"layers": np.arange(15),
"layer_roles": ("layers", layer_roles_fixture),
"layer_roles": (
"layers",
fixture_core_components.layer_structure.layer_roles,
),
"cell_id": data.grid.cell_id,
},
name="leaf_area_index",
Expand All @@ -380,7 +477,10 @@ def dummy_climate_data(layer_roles_fixture):
dims=["layers", "cell_id"],
coords={
"layers": np.arange(15),
"layer_roles": ("layers", layer_roles_fixture),
"layer_roles": (
"layers",
fixture_core_components.layer_structure.layer_roles,
),
"cell_id": data.grid.cell_id,
},
name="layer_heights",
Expand Down Expand Up @@ -411,7 +511,10 @@ def dummy_climate_data(layer_roles_fixture):
.assign_coords(
{
"layers": np.arange(0, 15),
"layer_roles": ("layers", layer_roles_fixture),
"layer_roles": (
"layers",
fixture_core_components.layer_structure.layer_roles,
),
"cell_id": data.grid.cell_id,
}
)
Expand Down Expand Up @@ -442,7 +545,10 @@ def dummy_climate_data(layer_roles_fixture):
).assign_coords(
{
"layers": np.arange(0, 15),
"layer_roles": ("layers", layer_roles_fixture[0:15]),
"layer_roles": (
"layers",
fixture_core_components.layer_structure.layer_roles[0:15],
),
"cell_id": data.grid.cell_id,
}
)
Expand Down Expand Up @@ -472,7 +578,10 @@ def dummy_climate_data(layer_roles_fixture):
).assign_coords(
{
"layers": np.arange(0, 15),
"layer_roles": ("layers", layer_roles_fixture[0:15]),
"layer_roles": (
"layers",
fixture_core_components.layer_structure.layer_roles[0:15],
),
"cell_id": data.grid.cell_id,
}
)
Expand Down
Loading

0 comments on commit 76f1906

Please sign in to comment.