Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LayerStructure updates for abiotic_simple #448

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
61 changes: 21 additions & 40 deletions tests/models/abiotic_simple/test_abiotic_simple_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,48 +160,30 @@ def test_generate_abiotic_simple_model(
log_check(caplog, expected_log_entries)


def test_setup(dummy_climate_data_varying_canopy, fixture_empty_array):
def test_setup(dummy_climate_data_varying_canopy, fixture_core_components):
"""Test set up and update."""
from virtual_ecosystem.core.config import Config
from virtual_ecosystem.core.core_components import CoreComponents

from virtual_ecosystem.models.abiotic_simple.abiotic_simple_model import (
AbioticSimpleModel,
)

# Build the config object and core components
config = Config(
cfg_strings="[core.timing]\nupdate_interval = '1 week'\n[abiotic_simple]\n"
)
core_components = CoreComponents(config)

# initialise model
model = AbioticSimpleModel.from_config(
model = AbioticSimpleModel(
data=dummy_climate_data_varying_canopy,
core_components=core_components,
config=config,
core_components=fixture_core_components,
)

model.setup()
exp_soil_temp = DataArray(
np.full((15, 3), np.nan),
dims=["layers", "cell_id"],
coords={
"layers": np.arange(0, 15),
"layer_roles": (
"layers",
core_components.layer_structure.layer_roles,
),
"cell_id": [0, 1, 2],
},
)

exp_soil_temp = fixture_core_components.layer_structure.from_template()
xr.testing.assert_allclose(model.data["soil_temperature"], exp_soil_temp)

xr.testing.assert_allclose(
model.data["vapour_pressure_deficit_ref"],
DataArray(
np.full((3, 3), 0.141727),
np.full((4, 3), 0.141727),
dims=["cell_id", "time_index"],
coords={"cell_id": [0, 1, 2]},
coords={"cell_id": [0, 1, 2, 3]},
),
)

Expand All @@ -218,20 +200,19 @@ def test_setup(dummy_climate_data_varying_canopy, fixture_empty_array):
]:
assert var in model.data

exp_air_temp = fixture_empty_array.copy()
exp_air_temp[[0, 1, 2, 3, 11, 12], :] = [
[30.0, 30.0, 30.0],
[29.91965, 29.946434, 29.973217],
[29.414851, 29.609901, np.nan],
[28.551891, np.nan, np.nan],
[26.19, 27.46, 28.73],
[22.81851, 25.21234, 27.60617],
exp_air_temp = fixture_core_components.layer_structure.from_template()
exp_air_temp[[0, 1, 2, 3, 11], :] = [
[30.0, 30.0, 30.0, 30.0],
[29.91965, 29.946434, 29.973217, 29.973217],
[29.414851, 29.609901, np.nan, np.nan],
[28.551891, np.nan, np.nan, np.nan],
[22.81851, 25.21234, 27.60617, 27.60617],
]
xr.testing.assert_allclose(model.data["air_temperature"], exp_air_temp)

exp_soil_temp = fixture_empty_array.copy()
exp_soil_temp[[13, 14], :] = [[20.712458, 21.317566, 21.922674], [20.0, 20.0, 20.0]]
exp_soil_temp = fixture_core_components.layer_structure.from_template()
exp_soil_temp[[12, 13], :] = [
[20.712458, 21.317566, 21.922674, 21.922674],
[20.0, 20.0, 20.0, 20.0],
]
xr.testing.assert_allclose(model.data["soil_temperature"], exp_soil_temp)

xr.testing.assert_allclose(
dummy_climate_data_varying_canopy["air_temperature"], exp_air_temp
)
163 changes: 70 additions & 93 deletions tests/models/abiotic_simple/test_microclimate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
from xarray import DataArray


def test_log_interpolation(
dummy_climate_data, fixture_core_components, fixture_empty_array
):
def test_log_interpolation(dummy_climate_data, fixture_core_components):
"""Test interpolation for temperature and humidity non-negative."""

from virtual_ecosystem.models.abiotic_simple.microclimate import log_interpolation
Expand All @@ -20,38 +18,38 @@ def test_log_interpolation(
data=data,
reference_data=data["air_temperature_ref"].isel(time_index=0),
leaf_area_index_sum=leaf_area_index_sum,
layer_roles=fixture_core_components.layer_structure.layer_roles,
layer_structure=fixture_core_components.layer_structure,
layer_heights=data["layer_heights"],
upper_bound=80,
lower_bound=0,
gradient=-2.45,
)

exp_air_temp = fixture_empty_array.copy()
t_vals = [30.0, 29.844995, 28.87117, 27.206405, 22.65, 16.145945]
exp_air_temp.T[..., [0, 1, 2, 3, 11, 12]] = t_vals
exp_air_temp = fixture_core_components.layer_structure.from_template()
t_vals = [30.0, 29.844995, 28.87117, 27.206405, 16.145945]
exp_air_temp.T[..., [0, 1, 2, 3, 11]] = t_vals
xr.testing.assert_allclose(result, exp_air_temp)

# relative humidity
result_hum = log_interpolation(
data=data,
reference_data=data["relative_humidity_ref"].isel(time_index=0),
leaf_area_index_sum=leaf_area_index_sum,
layer_roles=fixture_core_components.layer_structure.layer_roles,
layer_structure=fixture_core_components.layer_structure,
layer_heights=data["layer_heights"],
upper_bound=100,
lower_bound=0,
gradient=5.4,
)

exp_humidity = fixture_empty_array.copy()
humidity_vals = [90.0, 90.341644, 92.488034, 96.157312, 100.0, 100.0]
exp_humidity.T[..., [0, 1, 2, 3, 11, 12]] = humidity_vals
exp_humidity = fixture_core_components.layer_structure.from_template()
humidity_vals = [90.0, 90.341644, 92.488034, 96.157312, 100.0]
exp_humidity.T[..., [0, 1, 2, 3, 11]] = humidity_vals
xr.testing.assert_allclose(result_hum, exp_humidity)


def test_varying_canopy_log_interpolation(
dummy_climate_data_varying_canopy, fixture_core_components, fixture_empty_array
dummy_climate_data_varying_canopy, fixture_core_components
):
"""Test interpolation for temperature and humidity non-negative."""

Expand All @@ -65,21 +63,20 @@ def test_varying_canopy_log_interpolation(
data=data,
reference_data=data["air_temperature_ref"].isel(time_index=0),
leaf_area_index_sum=leaf_area_index_sum,
layer_roles=fixture_core_components.layer_structure.layer_roles,
layer_structure=fixture_core_components.layer_structure,
layer_heights=data["layer_heights"],
upper_bound=80,
lower_bound=0,
gradient=-2.45,
)

exp_air_temp = fixture_empty_array.copy()
exp_air_temp[[0, 1, 2, 3, 11, 12], :] = [
[30.0, 30.0, 30.0],
[29.844995, 29.896663, 29.948332],
[28.87117, 29.247446, np.nan],
[27.206405, np.nan, np.nan],
[22.65, 25.1, 27.55],
[16.145945, 20.763963, 25.381982],
exp_air_temp = fixture_core_components.layer_structure.from_template()
exp_air_temp[[0, 1, 2, 3, 11], :] = [
[30.0, 30.0, 30.0, 30.0],
[29.844995, 29.896663, 29.948332, 29.948332],
[28.87117, 29.247446, np.nan, np.nan],
[27.206405, np.nan, np.nan, np.nan],
[16.145945, 20.763963, 25.381982, 25.381982],
]
xr.testing.assert_allclose(result, exp_air_temp)

Expand All @@ -103,27 +100,28 @@ def test_calculate_saturation_vapour_pressure(dummy_climate_data):
)

exp_output = DataArray(
[1.41727, 1.41727, 1.41727],
np.repeat(1.41727, 4),
dims=["cell_id"],
coords={"cell_id": [0, 1, 2]},
coords={"cell_id": [0, 1, 2, 3]},
)
xr.testing.assert_allclose(result, exp_output)


def test_calculate_vapour_pressure_deficit(fixture_empty_array):
def test_calculate_vapour_pressure_deficit(fixture_core_components):
"""Test calculation of VPD."""

from virtual_ecosystem.models.abiotic_simple.constants import AbioticSimpleConsts
from virtual_ecosystem.models.abiotic_simple.microclimate import (
calculate_vapour_pressure_deficit,
)

temperature = fixture_empty_array.copy()
t_vals = [30.0, 29.844995, 28.87117, 27.206405, 22.65, 16.145945]
temperature.T[..., [0, 1, 2, 3, 11, 12]] = t_vals
rel_humidity = fixture_empty_array.copy()
humidity_vals = [90.0, 90.341644, 92.488034, 96.157312, 100.0, 100.0]
rel_humidity.T[..., [0, 1, 2, 3, 11, 12]] = humidity_vals
temperature = fixture_core_components.layer_structure.from_template()
t_vals = [30.0, 29.844995, 28.87117, 27.206405, 16.145945]
temperature.T[..., [0, 1, 2, 3, 11]] = t_vals

rel_humidity = fixture_core_components.layer_structure.from_template()
humidity_vals = [90.0, 90.341644, 92.488034, 96.157312, 100.0]
rel_humidity.T[..., [0, 1, 2, 3, 11]] = humidity_vals

constants = AbioticSimpleConsts()
result = calculate_vapour_pressure_deficit(
Expand All @@ -133,15 +131,15 @@ def test_calculate_vapour_pressure_deficit(fixture_empty_array):
constants.saturation_vapour_pressure_factors
),
)
exp_output = fixture_empty_array.copy()
vpd_vals = [0.141727, 0.136357, 0.103501, 0.050763, 0.0, 0.0]
exp_output.T[..., [0, 1, 2, 3, 11, 12]] = vpd_vals
exp_output = fixture_core_components.layer_structure.from_template()
vpd_vals = [0.141727, 0.136357, 0.103501, 0.050763, 0.0]
exp_output.T[..., [0, 1, 2, 3, 11]] = vpd_vals

xr.testing.assert_allclose(result["vapour_pressure_deficit"], exp_output)


def test_varying_canopy_calculate_vapour_pressure_deficit(
fixture_empty_array, dummy_climate_data_varying_canopy
fixture_core_components, dummy_climate_data_varying_canopy
):
"""Test calculation of VPD with different number of canopy layers."""

Expand All @@ -159,21 +157,18 @@ def test_varying_canopy_calculate_vapour_pressure_deficit(
constants.saturation_vapour_pressure_factors
),
)
exp_output = fixture_empty_array.copy()
exp_output[[0, 1, 2, 3, 11, 12], :] = [
[0.141727, 0.141727, 0.141727],
[0.136357, 0.136357, 0.136357],
[0.103501, 0.103501, np.nan],
[0.050763, np.nan, np.nan],
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
exp_output = fixture_core_components.layer_structure.from_template()
exp_output[[0, 1, 2, 3, 11], :] = [
[0.141727, 0.141727, 0.141727, 0.141727],
[0.136357, 0.136357, 0.136357, 0.136357],
[0.103501, 0.103501, np.nan, np.nan],
[0.050763, np.nan, np.nan, np.nan],
[0.0, 0.0, 0.0, 0.0],
]
xr.testing.assert_allclose(result["vapour_pressure_deficit"], exp_output)


def test_run_microclimate(
dummy_climate_data, fixture_core_components, fixture_empty_array
):
def test_run_microclimate(dummy_climate_data, fixture_core_components):
"""Test interpolation of all variables."""

from virtual_ecosystem.models.abiotic_simple.constants import (
Expand All @@ -186,37 +181,29 @@ def test_run_microclimate(

result = run_microclimate(
data=data,
layer_roles=fixture_core_components.layer_structure.layer_roles,
layer_structure=fixture_core_components.layer_structure,
time_index=0,
constants=AbioticSimpleConsts(),
bounds=AbioticSimpleBounds(),
)

exp_air_temp = fixture_empty_array.copy()
exp_air_temp[[0, 1, 2, 3, 11, 12], :] = [
[30.0, 30.0, 30.0],
[29.91965, 29.91965, 29.91965],
[29.414851, 29.414851, 29.414851],
[28.551891, 28.551891, 28.551891],
[26.19, 26.19, 26.19],
[22.81851, 22.81851, 22.81851],
]
exp_air_temp = fixture_core_components.layer_structure.from_template()
exp_air_temp[[0, 1, 2, 3, 11]] = np.array(
[30.0, 29.91965, 29.414851, 28.551891, 22.81851]
)[:, None]
xr.testing.assert_allclose(result["air_temperature"], exp_air_temp)

exp_soil_temp = fixture_empty_array.copy()
exp_soil_temp[[13, 14], :] = [[20.712458, 20.712458, 20.712458], [20.0, 20.0, 20.0]]
exp_soil_temp = fixture_core_components.layer_structure.from_template()
exp_soil_temp[[12, 13]] = np.array([20.712458, 20.0])[:, None]
xr.testing.assert_allclose(result["soil_temperature"], exp_soil_temp)

soil_values = DataArray(np.full((2, 3), np.nan), dims=["layers", "cell_id"])
pressure_values = DataArray(np.full((13, 3), 96.0), dims=["layers", "cell_id"])
exp_pressure = xr.concat(
[pressure_values, soil_values], dim="layers"
).assign_coords(data["layer_heights"].coords)
exp_pressure = fixture_core_components.layer_structure.from_template()
exp_pressure[np.arange(0, 12)] = 96
xr.testing.assert_allclose(result["atmospheric_pressure"], exp_pressure)


def test_run_microclimate_varying_canopy(
dummy_climate_data_varying_canopy, fixture_core_components, fixture_empty_array
dummy_climate_data_varying_canopy, fixture_core_components
):
"""Test interpolation of all variables with varying canopy arrays."""

Expand All @@ -230,36 +217,35 @@ def test_run_microclimate_varying_canopy(

result = run_microclimate(
data=data,
layer_roles=fixture_core_components.layer_structure.layer_roles,
layer_structure=fixture_core_components.layer_structure,
time_index=0,
constants=AbioticSimpleConsts(),
bounds=AbioticSimpleBounds(),
)

exp_air_temp = fixture_empty_array.copy()
exp_air_temp[[0, 1, 2, 3, 11, 12], :] = [
[30.0, 30.0, 30.0],
[29.91965, 29.946434, 29.973217],
[29.414851, 29.609901, np.nan],
[28.551891, np.nan, np.nan],
[26.19, 27.46, 28.73],
[22.81851, 25.21234, 27.60617],
exp_air_temp = fixture_core_components.layer_structure.from_template()
exp_air_temp[[0, 1, 2, 3, 11], :] = [
[30.0, 30.0, 30.0, 30.0],
[29.91965, 29.946434, 29.973217, 29.973217],
[29.414851, 29.609901, np.nan, np.nan],
[28.551891, np.nan, np.nan, np.nan],
[22.81851, 25.21234, 27.60617, 27.60617],
]
xr.testing.assert_allclose(result["air_temperature"], exp_air_temp)

exp_soil_temp = fixture_empty_array.copy()
exp_soil_temp[[13, 14], :] = [[20.712458, 21.317566, 21.922674], [20.0, 20.0, 20.0]]
exp_soil_temp = fixture_core_components.layer_structure.from_template()
exp_soil_temp[[12, 13], :] = [
[20.712458, 21.317566, 21.922674, 21.922674],
[20.0, 20.0, 20.0, 20.0],
]
xr.testing.assert_allclose(result["soil_temperature"], exp_soil_temp)

soil_values = DataArray(np.full((2, 3), np.nan), dims=["layers", "cell_id"])
pressure_values = DataArray(np.full((13, 3), 96.0), dims=["layers", "cell_id"])
exp_pressure = xr.concat(
[pressure_values, soil_values], dim="layers"
).assign_coords(data["layer_heights"].coords)
exp_pressure = fixture_core_components.layer_structure.from_template()
exp_pressure[np.arange(0, 12)] = 96
xr.testing.assert_allclose(result["atmospheric_pressure"], exp_pressure)


def test_interpolate_soil_temperature(dummy_climate_data):
def test_interpolate_soil_temperature(dummy_climate_data, fixture_core_components):
"""Test soil temperature interpolation."""

from virtual_ecosystem.models.abiotic_simple.microclimate import (
Expand All @@ -268,26 +254,17 @@ def test_interpolate_soil_temperature(dummy_climate_data):

data = dummy_climate_data

surface_temperature = DataArray([22.0, 22.0, 22.0], dims="cell_id")
surface_temperature = DataArray([22.0, 22.0, 22.0, 22.0], dims="cell_id")
result = interpolate_soil_temperature(
layer_heights=data["layer_heights"],
surface_temperature=surface_temperature,
mean_annual_temperature=data["mean_annual_temperature"],
layer_structure=fixture_core_components.layer_structure,
upper_bound=50.0,
lower_bound=-10.0,
)

exp_output = DataArray(
[
[20.505557, 20.505557, 20.505557],
[20.0, 20.0, 20.0],
],
dims=["layers", "cell_id"],
coords={
"layers": [13, 14],
"layer_roles": ("layers", ["soil", "soil"]),
"cell_id": [0, 1, 2],
},
)
exp_output = fixture_core_components.layer_structure.from_template()
exp_output[[12, 13]] = np.array([20.505557, 20.0])[:, None]

xr.testing.assert_allclose(result, exp_output)
Loading
Loading