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

Add get and set state #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
111 changes: 110 additions & 1 deletion bmipy/bmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,60 @@ def get_value_at_indices(
"""
...

@abstractmethod
def get_state(self) -> str:
"""Get all the information the model needs to re-initaite itself

This is a getter for the model, used to get all of the model's
current state. State is defined as all variables needed for the model to
advance to the next time step. If the output of get_state() is given
as input to set_state() of a freshly initiated model and update() is run
this should result in exactly the same new state of the model as running
update() on the original model.

Returns
-------
str
A string that contains all the information as explained above. The
format of this string is up to the modeller but best practices include:
- netCDF for models that have gridded geospatial data for their states.
- json for non-gridded models
using these format allows for easy writing to file of the state when
that is required and easy parsing back into a running model using
set_state()
remember to include time as a variable in the state as well!

"""
...

@abstractmethod
def get_state_ptr(self) -> np.ndarray:
"""Get all the information the model needs to re-initaite itself

This is a getter for the model, used to get a reference to all of the model's
current state. State is defined as all variables needed for the model to
advance to the next time step. If the output of get_state_ptr() is given
as input to set_state_ptr() of a freshly initiated model and update() is run
this should result in exactly the same new state of the model as running
update() on the original model.

Returns
-------
array_like
A reference to the state. The format format of how the state is stored at
the reference is up to the modeller but best practices include:
- netCDF for models that have gridded geospatial data for their states.
- json for non-gridded models
using these format allows for easy writing to file of the state when
that is required and easy parsing back into a running model using
set_state()
remember to include time as a variable in the state as well!

"""
...



@abstractmethod
def set_value(self, name: str, src: np.ndarray) -> None:
"""Specify a new value for a model variable.
Expand Down Expand Up @@ -432,7 +486,62 @@ def set_value_at_indices(
"""
...

# Grid information
@abstractmethod
def set_state(self, state: str) -> None:
"""Set all the information the model needs to re-initaite itself

This is a setter for the model, used to set all of the model's
current state. State is defined as all variables needed for the model to
advance to the next time step. If the output of get_state() is given
as input to set_state() of a freshly initiated model and update() is run
this should result in exactly the same new state of the model as running
update() on the original model.

Parameters
-------
state, str
A string that contains all the information as explained above. The
format of this string is up to the modeller but best practices include:
- netCDF for models that have gridded geospatial data for their states.
- json for non-gridded models
using these format allows for easy writing to file of the state when
that is required and easy parsing back into a running model using
set_state()
remember to include time as a variable in the state as well!

"""
...

@abstractmethod
def set_state_ptr(self, state_loc: np.ndarray) -> None:
"""Set all the information the model needs to re-initaite itself

This is a Setter for the model, used to set all of the model's
current state. State is defined as all variables needed for the model to
advance to the next time step. If the output of get_state_ptr() is given
as input to set_state_ptr() of a freshly initiated model and update() is run
this should result in exactly the same new state of the model as running
update() on the original model.

Returns
-------
array_like
A reference to the state. The format format of how the state is stored at
the reference is up to the modeller but best practices include:
- netCDF for models that have gridded geospatial data for their states.
- json for non-gridded models
using these format allows for easy writing to file of the state when
that is required and easy parsing back into a running model using
set_state()
remember to include time as a variable in the state as well!

"""
...




# Grid information
@abstractmethod
def get_grid_rank(self, grid: int) -> int:
"""Get number of dimensions of the computational grid.
Expand Down
12 changes: 12 additions & 0 deletions tests/test_bmipy.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,24 @@ def get_value(self, var_name):
def get_value_at_indices(self, var_name, indices):
pass

def get_state(self):
pass

def get_state_ptr(self, var_name):
pass

def set_value(self, var_name, src):
pass

def set_value_at_indices(self, var_name, src, indices):
pass

def set_state(self, src):
pass

def get_state_ptr(self, src):
pass

def get_component_name(self):
pass

Expand Down