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

Adds support for static models #373

Merged
merged 49 commits into from
Dec 13, 2024
Merged
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
a96e90a
Adds psudocode for static models
dalonsoa Jan 23, 2024
45fb958
Integrate renamed project
dalonsoa Aug 21, 2024
6f9b5c5
Add bypassing of methods
dalonsoa Aug 21, 2024
f0f90f7
Prevent overriding init
dalonsoa Aug 21, 2024
fcdc3ee
Prevent overriding update
dalonsoa Aug 21, 2024
32a08d4
Merge branch 'develop' into frozen_models
dalonsoa Sep 3, 2024
7e730a4
Merge branch 'develop' into frozen_models
dalonsoa Sep 30, 2024
0976da4
Merge branch 'develop' into frozen_models
dalonsoa Oct 15, 2024
f9520dc
Merge branch 'develop' into frozen_models
dalonsoa Oct 15, 2024
6ae09ea
:recycle: Rename update with _update in models.
dalonsoa Oct 15, 2024
7c8b987
:recycle: Update setup in abiotic model.
dalonsoa Oct 15, 2024
a528278
:recycle: Update setup in abiotic simple model.
dalonsoa Oct 15, 2024
dedb2a6
:recycle: Update setup in abiotic animal model.
dalonsoa Oct 15, 2024
6176eb2
:recycle: Add class attribute descriptions to abiotic and abiotic sim…
dalonsoa Oct 15, 2024
bbabc61
:recycle: Remove __init__ from hydorlogy model.
dalonsoa Oct 15, 2024
b309d52
:recycle: Remove __init__ from litter model.
dalonsoa Oct 15, 2024
f336981
:recycle: Remove __init__ from plants model.
dalonsoa Oct 15, 2024
9d6e4af
:recycle: Remove __init__ from soil model.
dalonsoa Oct 15, 2024
d9a4fb8
:recycle: Add static flag to abiotic model.
dalonsoa Oct 15, 2024
590846a
:recycle: Add static flag to abiotic simple model.
dalonsoa Oct 15, 2024
b94526a
:recycle: Add static flag to animal model.
dalonsoa Oct 15, 2024
38c527e
:recycle: Add static flag to animal model.
dalonsoa Oct 15, 2024
93efe7b
:recycle: Add static flag to litter model.
dalonsoa Oct 15, 2024
bcfb968
:recycle: Add static flag to plants model.
dalonsoa Oct 15, 2024
3a4627d
:recycle: Add static flag to soil model.
dalonsoa Oct 15, 2024
7b26336
:recycle: Fix abiotic and abiotic_simple tests.
dalonsoa Oct 15, 2024
04468d0
:recycle: Simplify fix abiotic and abiotic_simple tests.
dalonsoa Oct 15, 2024
d335b08
:construction: Fix hydrology tests.
dalonsoa Oct 15, 2024
3bdf682
Merge branch 'develop' into frozen_models
dalonsoa Oct 24, 2024
0a43623
:white_check_mark: Fix remaining tests.
dalonsoa Oct 24, 2024
90fb257
:white_check_mark: Add tests for _bypass_setup_due_to_static_configur…
dalonsoa Oct 24, 2024
4951376
:white_check_mark: Add tests for _run_update_due_to_static_configurat…
dalonsoa Oct 24, 2024
58ada3e
:memo: Adds docs about the static flag.
dalonsoa Oct 24, 2024
05ff05b
:recycle: Remove vars requires for init from update check.
dalonsoa Oct 25, 2024
5d3a200
:recycle: Adapt cli and main tests.
dalonsoa Oct 25, 2024
0171525
:recycle: Adapt litter tests.
dalonsoa Oct 25, 2024
763289f
:recycle: Adapt soil tests.
dalonsoa Oct 25, 2024
7a7e27b
:recycle: Clean up a bit hydrology tests.
dalonsoa Oct 25, 2024
d691a34
Apply suggestions from code review
dalonsoa Oct 30, 2024
75f1253
:recycle: Include reviewers comments.
dalonsoa Oct 30, 2024
1ce3aa4
:white_check_mark: Fix failing tests.
dalonsoa Oct 30, 2024
cc0590d
Merge branch 'develop' into frozen_models
dalonsoa Nov 6, 2024
2bcb3e9
Merge branch 'develop' into frozen_models
dalonsoa Nov 6, 2024
4948a80
:recycle: Fix issue resulting from merge conflict.
dalonsoa Nov 6, 2024
e70adb2
Merge branch 'develop' into frozen_models
dalonsoa Nov 14, 2024
23ab6d7
:twisted_rightwards_arrows: Fix conflicts and finish merge.
dalonsoa Dec 11, 2024
d513125
:recycle: Remove unnecessary call to setup.
dalonsoa Dec 11, 2024
78d1a2a
Fixing error with animal respiration in testing data.
TaranRallings Dec 13, 2024
732cea3
Merge branch 'develop' into frozen_models
TaranRallings Dec 13, 2024
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
59 changes: 57 additions & 2 deletions virtual_rainforest/core/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Any
from typing import Any, Optional

import pint
import xarray as xr

from virtual_rainforest.core.axes import AXIS_VALIDATORS
from virtual_rainforest.core.config import Config
Expand Down Expand Up @@ -159,6 +160,8 @@ def __init__(
self,
data: Data,
update_interval: pint.Quantity,
static: bool = False,
static_data: Optional[xr.Dataset] = None,
**kwargs: Any,
):
"""Performs core initialisation for BaseModel subclasses.
Expand All @@ -180,6 +183,10 @@ def __init__(
"""The time interval between model updates."""
self._repr = ["update_interval"]
"""A list of attributes to be included in the class __repr__ output"""
self._static = static
"""Flag indicating if the model is static, i.e. does not change with time."""
self._static_data = static_data
"""Pre-set data for the whole simulation provided as input."""

# Check the required init variables
self.check_init_data()
Expand All @@ -192,13 +199,61 @@ def setup(self) -> None:
def spinup(self) -> None:
"""Function to spin up the model."""

@abstractmethod
def update(self, time_index: int, **kwargs: Any) -> None:
"""Function to update the model.

Args:
time_index: The index representing the current time step in the data object.
"""
if not self._static:
self._update(time_index, **kwargs)
return

self._update_static(time_index, **kwargs)

@abstractmethod
def _update(self, time_index: int, **kwargs: Any) -> None:
"""Function to update the model.

Args:
time_index: The index representing the current time step in the data object.
"""

def _update_static(self, time_index: int, **kwargs: Any) -> None:
"""Function to update the model in the static case.

Args:
time_index: The index representing the current time step in the data object.
"""
if self._static_data is None:
self._update(time_index, **kwargs)
self._build_static_data(time_index)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure that I follow this bit, does the update happen and then new static data gets built for the next time step?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. This is run only once, the first time that we are required to run the update and, thereafter the updated values are used for updating the data object again and again. We need to run update at least once in this case since, otherwise, we do not know what to update the data with.

If pre-set data is provided as an external file/s, then the static_data object is not none and this step is not needed.

Keeping aside changes in initialisation/validation, the only change to existing models would be to replace update by _update, since they are overwritting only one of the two options for updating the model. The _update_static option is not model specific, and therefore is coded directly in the base model.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh right that makes sense now, cheers!

return

self._update_with_static_data(time_index)

def _build_static_data(self, time_index: int) -> None:
"""Populates the static data attribute with the data for time index.

It extracts :attr:`~virtual_rainforest.core.base_model.BaseModel.vars_updated`
from :attr:`~virtual_rainforest.core.base_model.BaseModel.data` at `time_index`
to create :attr:`~virtual_rainforest.core.base_model.BaseModel._static_data`,
which then is used at any future times.

Args:
time_index: The index representing the current time step in the data object.
"""

def _update_with_static_data(self, time_index: int) -> None:
"""Updates data object with with static data at time_index.

It updates :attr:`~virtual_rainforest.core.base_model.BaseModel.data` with the
:attr:`~virtual_rainforest.core.base_model.BaseModel._static_data` at
`time_index`.

Args:
time_index: The index representing the current time step in the data object.
"""

@abstractmethod
def cleanup(self) -> None:
Expand Down
Loading