Skip to content

Commit

Permalink
Shorten datetimes passed to Ribasim Python (#1253)
Browse files Browse the repository at this point in the history
This is a little easier on the eyes, and most simulations will start and
end at midnight anyway. Ribasim Python still writes the time to the
TOML, though the core also accepts it without.
  • Loading branch information
visr authored Mar 14, 2024
1 parent 08bcb1c commit 1f20b68
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 121 deletions.
36 changes: 18 additions & 18 deletions docs/python/examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"metadata": {},
"outputs": [],
"source": [
"model = Model(starttime=\"2020-01-01 00:00:00\", endtime=\"2021-01-01 00:00:00\")"
"model = Model(starttime=\"2020-01-01\", endtime=\"2021-01-01\")"
]
},
{
Expand Down Expand Up @@ -444,7 +444,7 @@
"metadata": {},
"outputs": [],
"source": [
"model = Model(starttime=\"2020-01-01 00:00:00\", endtime=\"2021-01-01 00:00:00\")"
"model = Model(starttime=\"2020-01-01\", endtime=\"2021-01-01\")"
]
},
{
Expand Down Expand Up @@ -755,8 +755,8 @@
"outputs": [],
"source": [
"model = Model(\n",
" starttime=\"2020-01-01 00:00:00\",\n",
" endtime=\"2020-12-01 00:00:00\",\n",
" starttime=\"2020-01-01\",\n",
" endtime=\"2020-12-01\",\n",
")"
]
},
Expand Down Expand Up @@ -873,10 +873,10 @@
" [\n",
" pid_control.Time(\n",
" time=[\n",
" \"2020-01-01 00:00:00\",\n",
" \"2020-05-01 00:00:00\",\n",
" \"2020-07-01 00:00:00\",\n",
" \"2020-12-01 00:00:00\",\n",
" \"2020-01-01\",\n",
" \"2020-05-01\",\n",
" \"2020-07-01\",\n",
" \"2020-12-01\",\n",
" ],\n",
" listen_node_id=[2, 2, 2, 2],\n",
" listen_node_type=[\"Basin\", \"Basin\", \"Basin\", \"Basin\"],\n",
Expand All @@ -892,10 +892,10 @@
" [\n",
" pid_control.Time(\n",
" time=[\n",
" \"2020-01-01 00:00:00\",\n",
" \"2020-05-01 00:00:00\",\n",
" \"2020-07-01 00:00:00\",\n",
" \"2020-12-01 00:00:00\",\n",
" \"2020-01-01\",\n",
" \"2020-05-01\",\n",
" \"2020-07-01\",\n",
" \"2020-12-01\",\n",
" ],\n",
" listen_node_id=[2, 2, 2, 2],\n",
" listen_node_type=[\"Basin\", \"Basin\", \"Basin\", \"Basin\"],\n",
Expand Down Expand Up @@ -1041,8 +1041,8 @@
"outputs": [],
"source": [
"model = Model(\n",
" starttime=\"2020-01-01 00:00:00\",\n",
" endtime=\"2020-01-20 00:00:00\",\n",
" starttime=\"2020-01-01\",\n",
" endtime=\"2020-01-20\",\n",
")"
]
},
Expand Down Expand Up @@ -1239,7 +1239,7 @@
" return_factor=[0.0, 0.0, 0.0, 0.0],\n",
" min_level=[-1.0, -1.0, -1.0, -1.0],\n",
" priority=[1, 1, 2, 2],\n",
" time=2 * [\"2020-01-01 00:00:00\", \"2020-01-20 00:00:00\"],\n",
" time=2 * [\"2020-01-01\", \"2020-01-20\"],\n",
" )\n",
" ],\n",
")"
Expand Down Expand Up @@ -1434,8 +1434,8 @@
"outputs": [],
"source": [
"model = ribasim.Model(\n",
" starttime=\"2020-01-01 00:00:00\",\n",
" endtime=\"2020-02-01 00:00:00\",\n",
" starttime=\"2020-01-01\",\n",
" endtime=\"2020-02-01\",\n",
")"
]
},
Expand All @@ -1461,7 +1461,7 @@
" [\n",
" *basin_data,\n",
" basin.Time(\n",
" time=[\"2020-01-01 00:00:00\", \"2020-01-16 00:00:00\"],\n",
" time=[\"2020-01-01\", \"2020-01-16\"],\n",
" drainage=[0.0, 0.0],\n",
" potential_evaporation=[0.0, 0.0],\n",
" infiltration=[0.0, 0.0],\n",
Expand Down
44 changes: 22 additions & 22 deletions python/ribasim_testmodels/ribasim_testmodels/allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def user_demand_model() -> Model:
"""Create a UserDemand test model with static and dynamic UserDemand on the same basin."""

model = Model(
starttime="2020-01-01 00:00:00",
endtime="2021-01-01 00:00:00",
starttime="2020-01-01",
endtime="2021-01-01",
solver=Solver(algorithm="Tsit5"),
)

Expand Down Expand Up @@ -74,8 +74,8 @@ def subnetwork_model() -> Model:
"""

model = Model(
starttime="2020-01-01 00:00:00",
endtime="2020-04-01 00:00:00",
starttime="2020-01-01",
endtime="2020-04-01",
allocation=Allocation(use_allocation=True, timestep=86400),
)

Expand Down Expand Up @@ -158,8 +158,8 @@ def looped_subnetwork_model() -> Model:
"""

model = Model(
starttime="2020-01-01 00:00:00",
endtime="2021-01-01 00:00:00",
starttime="2020-01-01",
endtime="2021-01-01",
allocation=Allocation(use_allocation=True, timestep=86400),
)

Expand Down Expand Up @@ -284,8 +284,8 @@ def minimal_subnetwork_model() -> Model:
"""Create a subnetwork that is minimal with non-trivial allocation."""

model = Model(
starttime="2020-01-01 00:00:00",
endtime="2021-01-01 00:00:00",
starttime="2020-01-01",
endtime="2021-01-01",
allocation=Allocation(use_allocation=True, timestep=86400),
)

Expand Down Expand Up @@ -316,7 +316,7 @@ def minimal_subnetwork_model() -> Model:
Node(6, Point(1, 4), subnetwork_id=2),
[
user_demand.Time(
time=["2020-01-01 00:00:00", "2021-01-01 00:00:00"],
time=["2020-01-01", "2021-01-01"],
demand=[1e-3, 2e-3],
return_factor=0.9,
min_level=0.9,
Expand All @@ -342,8 +342,8 @@ def fractional_flow_subnetwork_model() -> Model:
"""

model = Model(
starttime="2020-01-01 00:00:00",
endtime="2021-01-01 00:00:00",
starttime="2020-01-01",
endtime="2021-01-01",
allocation=Allocation(use_allocation=True, timestep=86400),
)

Expand All @@ -356,7 +356,7 @@ def fractional_flow_subnetwork_model() -> Model:
Node(1, Point(0, 0), subnetwork_id=2),
[
flow_boundary.Time(
time=["2020-01-01 00:00:00", "2021-01-01 00:00:00"],
time=["2020-01-01", "2021-01-01"],
flow_rate=[2.0e-3, 4.0e-3],
)
],
Expand Down Expand Up @@ -388,7 +388,7 @@ def fractional_flow_subnetwork_model() -> Model:
Node(9, Point(3, 5), subnetwork_id=2),
[
user_demand.Time(
time=["2020-01-01 00:00:00", "2021-01-01 00:00:00"],
time=["2020-01-01", "2021-01-01"],
demand=[1e-3, 2e-3],
return_factor=0.9,
min_level=0.9,
Expand Down Expand Up @@ -429,8 +429,8 @@ def allocation_example_model() -> Model:
"""Generate a model that is used as an example of allocation in the docs."""

model = Model(
starttime="2020-01-01 00:00:00",
endtime="2020-01-20 00:00:00",
starttime="2020-01-01",
endtime="2020-01-20",
allocation=Allocation(use_allocation=True, timestep=86400),
)

Expand Down Expand Up @@ -508,7 +508,7 @@ def allocation_example_model() -> Model:
Node(13, Point(5, 1), subnetwork_id=2),
[
user_demand.Time(
time=2 * ["2020-01-01 00:00:00", "2020-01-20 00:00:00"],
time=2 * ["2020-01-01", "2020-01-20"],
demand=[0.0, 1.0, 1.2, 1.2],
return_factor=0.0,
min_level=-1.0,
Expand Down Expand Up @@ -541,8 +541,8 @@ def main_network_with_subnetworks_model() -> Model:
"""Generate a model which consists of a main network and multiple connected subnetworks."""

model = Model(
starttime="2020-01-01 00:00:00",
endtime="2020-03-01 00:00:00",
starttime="2020-01-01",
endtime="2020-03-01",
allocation=Allocation(use_allocation=True, timestep=86400),
)

Expand Down Expand Up @@ -659,7 +659,7 @@ def main_network_with_subnetworks_model() -> Model:
Node(32, Point(17, 8), subnetwork_id=5),
[
user_demand.Time(
time=["2020-01-01 00:00:00", "2021-01-01 00:00:00"],
time=["2020-01-01", "2021-01-01"],
demand=[1e-3, 2e-3],
return_factor=0.9,
min_level=0.9,
Expand Down Expand Up @@ -843,8 +843,8 @@ def main_network_with_subnetworks_model() -> Model:

def level_demand_model() -> Model:
model = Model(
starttime="2020-01-01 00:00:00",
endtime="2020-02-01 00:00:00",
starttime="2020-01-01",
endtime="2020-02-01",
allocation=Allocation(use_allocation=True, timestep=1e5),
)
model.flow_boundary.add(
Expand All @@ -855,7 +855,7 @@ def level_demand_model() -> Model:
[
basin.Profile(area=1000.0, level=[0.0, 1.0]),
basin.Time(
time=["2020-01-01 00:00:00", "2020-01-16 00:00:00"],
time=["2020-01-01", "2020-01-16"],
precipitation=[1e-6, 0.0],
),
basin.State(level=[0.5]),
Expand Down
4 changes: 2 additions & 2 deletions python/ribasim_testmodels/ribasim_testmodels/backwater.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def backwater_model():
ids = np.arange(1, node_type.size + 1, dtype=int)

model = ribasim.Model(
starttime="2020-01-01 00:00:00",
endtime="2021-01-01 00:00:00",
starttime="2020-01-01",
endtime="2021-01-01",
)

model.flow_boundary.add(
Expand Down
18 changes: 9 additions & 9 deletions python/ribasim_testmodels/ribasim_testmodels/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
def basic_model() -> ribasim.Model:
# Setup model
model = ribasim.Model(
starttime="2020-01-01 00:00:00",
endtime="2021-01-01 00:00:00",
starttime="2020-01-01",
endtime="2021-01-01",
)
model.logging = ribasim.Logging(verbosity="debug")

Expand Down Expand Up @@ -251,8 +251,8 @@ def tabulated_rating_curve_model() -> ribasim.Model:

# Setup a model:
model = ribasim.Model(
starttime="2020-01-01 00:00:00",
endtime="2021-01-01 00:00:00",
starttime="2020-01-01",
endtime="2021-01-01",
)

# Setup tabulated rating curve:
Expand Down Expand Up @@ -332,8 +332,8 @@ def tabulated_rating_curve_model() -> ribasim.Model:
def outlet_model():
"""Set up a basic model with an outlet that encounters various physical constraints."""
model = ribasim.Model(
starttime="2020-01-01 00:00:00",
endtime="2021-01-01 00:00:00",
starttime="2020-01-01",
endtime="2021-01-01",
solver=ribasim.Solver(saveat=0),
)

Expand All @@ -352,9 +352,9 @@ def outlet_model():
[
level_boundary.Time(
time=[
"2020-01-01 00:00:00",
"2020-06-01 00:00:00",
"2021-01-01 00:00:00",
"2020-01-01",
"2020-06-01",
"2021-01-01",
],
level=[1.0, 3.0, 3.0],
)
Expand Down
8 changes: 4 additions & 4 deletions python/ribasim_testmodels/ribasim_testmodels/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def bucket_model() -> ribasim.Model:
"""Bucket model with just a single basin."""

model = ribasim.Model(
starttime="2020-01-01 00:00:00",
endtime="2021-01-01 00:00:00",
starttime="2020-01-01",
endtime="2021-01-01",
)

model.basin.add(
Expand All @@ -40,8 +40,8 @@ def leaky_bucket_model() -> ribasim.Model:
"""Bucket model with dynamic forcing with missings."""

model = ribasim.Model(
starttime="2020-01-01 00:00:00",
endtime="2020-01-05 00:00:00",
starttime="2020-01-01",
endtime="2020-01-05",
)

model.basin.add(
Expand Down
28 changes: 12 additions & 16 deletions python/ribasim_testmodels/ribasim_testmodels/discrete_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def pump_discrete_control_model() -> Model:
"""

model = Model(
starttime="2020-01-01 00:00:00",
endtime="2021-01-01 00:00:00",
starttime="2020-01-01",
endtime="2021-01-01",
)

model.basin.add(
Expand Down Expand Up @@ -119,15 +119,15 @@ def flow_condition_model() -> Model:
"""Set up a basic model that involves discrete control based on a flow condition"""

model = Model(
starttime="2020-01-01 00:00:00",
endtime="2021-01-01 00:00:00",
starttime="2020-01-01",
endtime="2021-01-01",
)

model.flow_boundary.add(
Node(1, Point(0, 0)),
[
flow_boundary.Time(
time=["2020-01-01 00:00:00", "2022-01-01 00:00:00"],
time=["2020-01-01", "2022-01-01"],
flow_rate=[0.0, 40 / 86400],
)
],
Expand Down Expand Up @@ -183,17 +183,13 @@ def level_boundary_condition_model() -> Model:
"""Set up a small model with a condition on a level boundary."""

model = Model(
starttime="2020-01-01 00:00:00",
endtime="2021-01-01 00:00:00",
starttime="2020-01-01",
endtime="2021-01-01",
)

model.level_boundary.add(
Node(1, Point(0, 0)),
[
level_boundary.Time(
time=["2020-01-01 00:00:00", "2022-01-01 00:00:00"], level=[5.0, 10.0]
)
],
[level_boundary.Time(time=["2020-01-01", "2022-01-01"], level=[5.0, 10.0])],
)
model.linear_resistance.add(
Node(2, Point(1, 0)), [linear_resistance.Static(resistance=[5e3])]
Expand Down Expand Up @@ -263,8 +259,8 @@ def tabulated_rating_curve_control_model() -> Model:
"""

model = Model(
starttime="2020-01-01 00:00:00",
endtime="2021-01-01 00:00:00",
starttime="2020-01-01",
endtime="2021-01-01",
)

model.basin.add(
Expand Down Expand Up @@ -329,8 +325,8 @@ def level_setpoint_with_minmax_model() -> Model:
"""

model = Model(
starttime="2020-01-01 00:00:00",
endtime="2021-01-01 00:00:00",
starttime="2020-01-01",
endtime="2021-01-01",
)

model.basin.add(
Expand Down
Loading

0 comments on commit 1f20b68

Please sign in to comment.