Skip to content

Commit

Permalink
Merge pull request #137 from ImperialCollegeLondon/apply-surface-over…
Browse files Browse the repository at this point in the history
…rides

add and fix test
  • Loading branch information
barneydobson authored Dec 12, 2024
2 parents 3195955 + 5651d26 commit d05486d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
93 changes: 93 additions & 0 deletions tests/test_land.py
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,99 @@ def test_nutrientpool_overrides(self):
)
self.assertEqual(growingsurface.nutrient_pool.fraction_dry_n_to_fast, 0.71)

"""Test surface parameter override."""


def test_apply_surface_overrides(tmp_path):
"""Test surface overrides are applied in model load."""
import yaml
from wsimod.orchestration.model import Model

config = {
"arcs": {
"arc1": {
"name": "arc1",
"in_port": "land1",
"out_port": "river1",
"type_": "Arc",
"capacity": 10,
"preference": 1,
},
},
"nodes": {
"land1": {
"name": "land1",
"type_": "Land",
"percolation_residence_time": 0.1,
"surfaces": {
"Woodland": {
"area": 100,
"datum": 10,
"type_": "GrowingSurface",
"ET_depletion_factor": 0.75,
"surface": "Woodland",
},
"Grass": {
"area": 200,
"datum": 20,
"type_": "GrowingSurface",
"ET_depletion_factor": 0.75,
"surface": "Grass",
},
},
},
"river1": {
"name": "river1",
"type_": "River",
},
},
"overrides": {
"arcs": {
"arc1": {
"name": "arc1",
"type_": "Arc",
"capacity": 20,
}
},
"nodes": {
"land1": {
"surfaces": {
"Woodland": {
"surface": "Woodland",
"type_": "GrowingSurface",
"area": 1000,
"ET_depletion_factor": 0.8,
}
},
"percolation_residence_time": 1,
"name": "land1",
"type_": "Land",
}
},
},
}

# Create the config file
with (tmp_path / "config.yml").open("w") as f:
yaml.dump(config, f)

# Load in the model
model = Model()
model.load(tmp_path)

# Perform the test
assert hasattr(model.nodes["land1"], "surfaces")
assert hasattr(model.nodes["land1"].get_surface("Woodland"), "surface")
assert hasattr(model.nodes["land1"].get_surface("Grass"), "surface")
assert model.arcs["arc1"].capacity == 20
assert model.arcs["arc1"].preference == 1
assert model.nodes["land1"].percolation_residence_time == 1
assert model.nodes["land1"].get_surface("Woodland").datum == 10
assert model.nodes["land1"].get_surface("Grass").area == 200
assert model.nodes["land1"].get_surface("Grass").datum == 20
assert model.nodes["land1"].get_surface("Woodland").area == 1000
assert model.nodes["land1"].get_surface("Woodland").ET_depletion_factor == 0.8


if __name__ == "__main__":
unittest.main()
3 changes: 3 additions & 0 deletions wsimod/nodes/land.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ def apply_overrides(self, overrides=Dict[str, Any]):
self.percolation.residence_time = self.percolation_residence_time
super().apply_overrides(overrides)

for surface, override in overrides.get("surfaces", {}).items():
self.get_surface(surface).apply_overrides(override)

def apply_irrigation(self):
"""Iterate over any irrigation functions (needs further testing..
Expand Down

0 comments on commit d05486d

Please sign in to comment.