Skip to content

Commit

Permalink
Merge pull request #325 from ImperialCollegeLondon/feature/clean_up
Browse files Browse the repository at this point in the history
Remove old temperature and moisture dependance
  • Loading branch information
jacobcook1995 authored Oct 9, 2023
2 parents 62f0612 + a8b1697 commit 8d826e7
Show file tree
Hide file tree
Showing 12 changed files with 444 additions and 363 deletions.
22 changes: 21 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ def dummy_carbon_data(layer_roles_fixture):
DataArray(np.full((13, 4), np.nan), dims=["layers", "cell_id"]),
# At present the soil model only uses the top soil layer, so this is the
# only one with real test values in
DataArray([[0.5, 0.7, 0.6, 0.2]], dims=["layers", "cell_id"]),
DataArray(
[[0.472467929, 0.399900047, 0.256053640, 0.153616897]],
dims=["layers", "cell_id"],
),
DataArray(np.full((1, 4), np.nan), dims=["layers", "cell_id"]),
],
dim="layers",
Expand All @@ -151,6 +154,23 @@ def dummy_carbon_data(layer_roles_fixture):
"cell_id": data.grid.cell_id,
}
)
# TODO - Eventually this should replace the dummy soil moisture entirely
data["matric_potential"] = xr.concat(
[
DataArray(np.full((13, 4), np.nan), dims=["layers", "cell_id"]),
# At present the soil model only uses the top soil layer, so this is the
# only one with real test values in
DataArray([[-3.0, -10.0, -250.0, -10000.0]], dims=["layers", "cell_id"]),
DataArray(np.full((1, 4), np.nan), dims=["layers", "cell_id"]),
],
dim="layers",
).assign_coords(
{
"layers": np.arange(0, 15),
"layer_roles": ("layers", layer_roles_fixture),
"cell_id": data.grid.cell_id,
}
)
data["soil_temperature"] = xr.concat(
[
DataArray(np.full((13, 4), np.nan), dims=["dim_0", "cell_id"]),
Expand Down
7 changes: 5 additions & 2 deletions tests/models/litter/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ def dummy_litter_data(layer_roles_fixture):
)

# The layer dependant data has to be handled separately
data["soil_moisture"] = concat(
data["matric_potential"] = concat(
[
DataArray(np.full((13, 3), np.nan), dims=["layers", "cell_id"]),
# At present the soil model only uses the top soil layer, so this is the
# only one with real test values in
DataArray([[0.25, 0.45, 0.3]], dims=["layers", "cell_id"]),
DataArray(
[[-10.0, -25.0, -100.0]],
dims=["layers", "cell_id"],
),
DataArray(np.full((1, 3), np.nan), dims=["layers", "cell_id"]),
],
dim="layers",
Expand Down
26 changes: 3 additions & 23 deletions tests/models/litter/test_litter_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,12 @@ def test_update(litter_model_fixture, dummy_litter_data):
end_above_meta = [0.29587973, 0.14851276, 0.07041856]
end_above_struct = [0.50055126, 0.25010012, 0.0907076]
end_woody = [4.702103, 11.802315, 7.300997]
end_below_meta = [0.394145, 0.35923, 0.069006]
end_below_struct = [0.60027118, 0.30975403, 0.02047743]
end_below_meta = [0.38949196, 0.36147436, 0.06906041]
end_below_struct = [0.60011634, 0.30989963, 0.02047753]
end_lignin_above_struct = [0.4996410, 0.1004310, 0.6964345]
end_lignin_woody = [0.49989001, 0.79989045, 0.34998229]
end_lignin_below_struct = [0.499760108, 0.249922519, 0.737107757]
c_mineral = [0.0212182, 0.02746286, 0.00796359]
c_mineral = [0.02987233, 0.02316114, 0.00786517]

litter_model_fixture.update(time_index=0)

Expand All @@ -468,23 +468,3 @@ def test_update(litter_model_fixture, dummy_litter_data):
dummy_litter_data["lignin_below_structural"], end_lignin_below_struct
)
assert np.allclose(dummy_litter_data["litter_C_mineralisation_rate"], c_mineral)


def test_convert_soil_moisture_to_water_potential(
dummy_litter_data, top_soil_layer_index
):
"""Test that function to convert soil moisture to a water potential works."""
from virtual_rainforest.models.litter.litter_model import (
convert_soil_moisture_to_water_potential,
)

expected_potentials = [-297.14104, -4.2647655, -79.666189]

actual_potentials = convert_soil_moisture_to_water_potential(
dummy_litter_data["soil_moisture"][top_soil_layer_index].to_numpy(),
air_entry_water_potential=LitterConsts.air_entry_water_potential,
water_retention_curvature=LitterConsts.water_retention_curvature,
saturated_water_content=LitterConsts.saturated_water_content,
)

assert np.allclose(actual_potentials, expected_potentials)
53 changes: 18 additions & 35 deletions tests/models/litter/test_litter_pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,25 @@ def temp_and_water_factors(
dummy_litter_data, surface_layer_index, top_soil_layer_index
):
"""Temperature and water factors for the various litter layers."""
from virtual_rainforest.models.litter.litter_model import (
convert_soil_moisture_to_water_potential,
)
from virtual_rainforest.models.litter.litter_pools import (
calculate_environmental_factors,
)

water_potentials = convert_soil_moisture_to_water_potential(
dummy_litter_data["soil_moisture"][top_soil_layer_index],
air_entry_water_potential=LitterConsts.air_entry_water_potential,
water_retention_curvature=LitterConsts.water_retention_curvature,
saturated_water_content=LitterConsts.saturated_water_content,
)

environmental_factors = calculate_environmental_factors(
surface_temp=dummy_litter_data["air_temperature"][surface_layer_index],
topsoil_temp=dummy_litter_data["soil_temperature"][top_soil_layer_index],
water_potential=water_potentials,
water_potential=dummy_litter_data["matric_potential"][top_soil_layer_index],
constants=LitterConsts,
)

return environmental_factors


# TODO - Compare the below
# [-297.1410435034187, -4.264765510307134, -79.66618999943468]
# [-10.0, -25.0, -100.0]


@pytest.fixture
def decay_rates(dummy_litter_data, temp_and_water_factors):
"""Decay rates for the various litter pools."""
Expand All @@ -59,17 +54,14 @@ def test_calculate_environmental_factors(
calculate_environmental_factors,
)

# TODO - hardcoding this in, but eventually this should be added to the data object.
water_potentials = [-10.0, -25.0, -100.0]

expected_water_factors = [1.0, 0.88496823, 0.71093190]
expected_temp_above_factors = [0.1878681, 0.1878681, 0.1878681]
expected_temp_below_factors = [0.2732009, 0.2732009, 0.2732009]

environmental_factors = calculate_environmental_factors(
surface_temp=dummy_litter_data["air_temperature"][surface_layer_index],
topsoil_temp=dummy_litter_data["soil_temperature"][top_soil_layer_index],
water_potential=water_potentials,
water_potential=dummy_litter_data["matric_potential"][top_soil_layer_index],
constants=LitterConsts,
)

Expand Down Expand Up @@ -104,7 +96,7 @@ def test_calculate_moisture_effect_on_litter_decomp(top_soil_layer_index):
calculate_moisture_effect_on_litter_decomp,
)

water_potentials = [-10.0, -25.0, -100.0, -400.0]
water_potentials = np.array([-10.0, -25.0, -100.0, -400.0])

expected_factor = [1.0, 0.88496823, 0.71093190, 0.53689556]

Expand Down Expand Up @@ -139,9 +131,6 @@ def test_calculate_change_in_litter_variables(
dummy_litter_data, surface_layer_index, top_soil_layer_index
):
"""Test that litter pool update calculation is correct."""
from virtual_rainforest.models.litter.litter_model import (
convert_soil_moisture_to_water_potential,
)
from virtual_rainforest.models.litter.litter_pools import (
calculate_change_in_litter_variables,
)
Expand All @@ -150,30 +139,24 @@ def test_calculate_change_in_litter_variables(
"litter_pool_above_metabolic": [0.29587973, 0.14851276, 0.07041856],
"litter_pool_above_structural": [0.50055126, 0.25010012, 0.0907076],
"litter_pool_woody": [4.702103, 11.802315, 7.300997],
"litter_pool_below_metabolic": [0.394145, 0.35923, 0.069006],
"litter_pool_below_structural": [0.60027118, 0.30975403, 0.02047743],
"litter_pool_below_metabolic": [0.38949196, 0.36147436, 0.06906041],
"litter_pool_below_structural": [0.60011634, 0.30989963, 0.02047753],
"lignin_above_structural": [0.4996410, 0.1004310, 0.6964345],
"lignin_woody": [0.49989001, 0.79989045, 0.34998229],
"lignin_below_structural": [0.499760108, 0.249922519, 0.737107757],
"litter_C_mineralisation_rate": [0.0212182, 0.02746286, 0.00796359],
"litter_C_mineralisation_rate": [0.02987233, 0.02316114, 0.00786517],
}

# Calculate water potential
water_potential = convert_soil_moisture_to_water_potential(
dummy_litter_data["soil_moisture"][top_soil_layer_index].to_numpy(),
air_entry_water_potential=LitterConsts.air_entry_water_potential,
water_retention_curvature=LitterConsts.water_retention_curvature,
saturated_water_content=LitterConsts.saturated_water_content,
)

result = calculate_change_in_litter_variables(
surface_temp=dummy_litter_data["air_temperature"][
surface_layer_index
].to_numpy(),
topsoil_temp=dummy_litter_data["soil_temperature"][
top_soil_layer_index
].to_numpy(),
water_potential=water_potential,
water_potential=dummy_litter_data["matric_potential"][
top_soil_layer_index
].to_numpy(),
above_metabolic=dummy_litter_data["litter_pool_above_metabolic"].to_numpy(),
above_structural=dummy_litter_data["litter_pool_above_structural"].to_numpy(),
woody=dummy_litter_data["litter_pool_woody"].to_numpy(),
Expand All @@ -200,8 +183,8 @@ def test_calculate_decay_rates(dummy_litter_data, temp_and_water_factors):
"metabolic_above": [0.00450883, 0.00225442, 0.00105206],
"structural_above": [1.67429665e-4, 6.18573593e-4, 1.10869077e-5],
"woody": [0.0004832, 0.00027069, 0.0015888],
"metabolic_below": [0.00627503, 0.01118989, 0.00141417],
"structural_below": [2.08818451e-4, 7.25965456e-4, 2.56818870e-6],
"metabolic_below": [0.01092804, 0.00894564, 0.00135959],
"structural_below": [3.63659952e-04, 5.80365659e-04, 2.46907410e-06],
}

actual_decay = calculate_decay_rates(
Expand Down Expand Up @@ -362,7 +345,7 @@ def test_calculate_litter_decay_metabolic_below(
calculate_litter_decay_metabolic_below,
)

expected_decay = [0.00627503, 0.01118989, 0.00141417]
expected_decay = [0.01092804, 0.00894564, 0.00135959]

actual_decay = calculate_litter_decay_metabolic_below(
temperature_factor=temp_and_water_factors["temp_below"],
Expand All @@ -382,7 +365,7 @@ def test_calculate_litter_decay_structural_below(
calculate_litter_decay_structural_below,
)

expected_decay = [2.08818451e-04, 7.25965456e-04, 2.56818870e-06]
expected_decay = [3.63659952e-04, 5.80365659e-04, 2.46907410e-06]

actual_decay = calculate_litter_decay_structural_below(
temperature_factor=temp_and_water_factors["temp_below"],
Expand Down
Loading

0 comments on commit 8d826e7

Please sign in to comment.