smash.Model.set_rr_initial_states#
- Model.set_rr_initial_states(key, value)[source]#
Set the values of a Model rainfall-runoff initial state.
This method performs an in-place operation on the Model object.
- Parameters:
- keystr
The name of the rainfall-runoff initial state.
- valuefloat or
numpy.ndarray
The value(s) to set to the rainfall-runoff initial state. If the value is a
numpy.ndarray
, its shape must be broadcastable into the rainfall-runoff initial state shape.
See also
Model.get_rr_initial_states
Get the values of a Model rainfall-runoff initial state.
Model.rr_initial_states
Model rainfall-runoff initial states.
Examples
>>> from smash.factory import load_dataset >>> setup, mesh = load_dataset("cance") >>> model = smash.Model(setup, mesh)
Set a specific value to a rainfall-runoff initial state grid
>>> model.set_rr_initial_states("hp", 0.22)
Access its value
>>> model.get_rr_initial_states("hp") array([[0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, ... 0.22, 0.22, 0.22, 0.22, 0.22, 0.22]], dtype=float32)
Set a grid with a shape equivalent to the rainfall-runoff initial state filled with random values between
0
and1
Get the rainfall-runoff initial state shape
>>> shape = model.get_rr_initial_states("hp").shape >>> shape (28, 28)
Generate the random grid
>>> np.random.seed(99) >>> random_arr = np.random.rand(*shape) >>> random_arr array([[6.72278559e-01, 4.88078399e-01, 8.25495174e-01, 3.14463876e-02, 8.08049963e-01, 5.65617420e-01, 2.97622499e-01, 4.66957205e-02, ... 8.83966213e-01, 3.73980927e-01, 2.98742432e-01, 8.37281270e-01]])
Set to the rainfall-runoff initial state the random grid
>>> model.set_rr_initial_states("hp", random_arr) >>> model.get_rr_initial_states("hp") array([[6.72278559e-01, 4.88078399e-01, 8.25495174e-01, 3.14463876e-02, 8.08049963e-01, 5.65617420e-01, 2.97622499e-01, 4.66957205e-02, ... 8.83966213e-01, 3.73980927e-01, 2.98742432e-01, 8.37281270e-01]], dtype=float32)
Note
This method is equivalent to directly slicing the
rr_initial_states.values
array (as shown below) and change the values but is simpler andsafer
to useAccess the rainfall-runoff initial state keys
>>> model.rr_initial_states.keys array(['hi', 'hp', 'ht', 'hlr'], dtype='<U3')
Get the index of the rainfall-runoff initial state
'hp'
>>> ind = np.argwhere(model.rr_initial_states.keys == "hp").item() >>> ind 1
Slice the
rr_initial_states.values
array on the last axis and change its values>>> model.rr_initial_states.values[..., ind] = 0.56 >>> model.rr_initial_states.values[..., ind] array([[0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, 0.56, ... 0.56, 0.56, 0.56, 0.56, 0.56, 0.56]], dtype=float32)
Warning
In that case, there’s no problem to set the value
0.56
to the rainfall-runoff initial state'hp'
, but each rainfall-runoff initial state has a feasibility domain, and that outside this domain, the model cannot run. For example, the feasibility domain of'hp'
is \(]0, 1[\).Trying to set a value greater than 1 to the rainfall-runoff initial state
'hp'
without the setter>>> model.rr_initial_states.values[..., ind] = 21 >>> model.rr_initial_states.values[..., ind] array([[21., 21., 21., 21., 21., 21., 21., 21., 21., 21., 21., 21., 21., 21., 21., 21., 21., 21., 21., 21., 21., 21., ... 21., 21., 21., 21., 21., 21.]], dtype=float32)
No particular problem doing this but trying with the setter
>>> model.set_rr_initial_states("hp", 21) ValueError: Invalid value for model rr_initial_state 'hp'. rr_initial_state domain [21, 21] is not included in the feasible domain ]0, 1[
Finally, trying to run the Model with a value greater than 1 set to the rainfall-runoff initial state
'hp'
leads to the same error>>> model.forward_run() ValueError: Invalid value for model rr_initial_state 'hp'. rr_initial_state domain [21, 21] is not included in the feasible domain ]0, 1[