From 4245e01140818d0a04dfcd7d3f031ab5d04ba2c9 Mon Sep 17 00:00:00 2001 From: Rolf Hut Date: Fri, 23 Sep 2022 15:15:00 -0600 Subject: [PATCH 1/2] added get and set state to bmi-python --- bmipy/bmi.py | 54 +++++++++++++++++++++++++++++++++++++++++++++ tests/test_bmipy.py | 12 ++++++++++ 2 files changed, 66 insertions(+) diff --git a/bmipy/bmi.py b/bmipy/bmi.py index aacf206..67556b4 100644 --- a/bmipy/bmi.py +++ b/bmipy/bmi.py @@ -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. diff --git a/tests/test_bmipy.py b/tests/test_bmipy.py index d4bad78..7525ed2 100644 --- a/tests/test_bmipy.py +++ b/tests/test_bmipy.py @@ -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 From 48301f24f5b51f786726548a6c2be718187c2c52 Mon Sep 17 00:00:00 2001 From: Rolf Hut Date: Fri, 23 Sep 2022 15:33:33 -0600 Subject: [PATCH 2/2] Update bmi.py with set_state --- bmipy/bmi.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/bmipy/bmi.py b/bmipy/bmi.py index 67556b4..1879cc8 100644 --- a/bmipy/bmi.py +++ b/bmipy/bmi.py @@ -486,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.