Skip to content

Commit

Permalink
Add extra timestep at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
visr committed Mar 19, 2024
1 parent b0d870a commit b9658e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
14 changes: 12 additions & 2 deletions coupling/delwaq/delwaq_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Utilities to write Delwaq (binary) input files."""

import struct
from datetime import timedelta
from pathlib import Path

import numpy as np
Expand Down Expand Up @@ -48,7 +49,7 @@ def write_lengths(fn: Path | str, data: np.ndarray[np.float32]):
f.write(data.astype("float32").tobytes())


def write_volumes(fn: Path | str, data: pd.DataFrame):
def write_volumes(fn: Path | str, data: pd.DataFrame, timestep: timedelta):
"""Write volumes file for Delwaq.
The format is an int defining the time
Expand All @@ -64,8 +65,12 @@ def write_volumes(fn: Path | str, data: pd.DataFrame):
f.write(struct.pack("<i", int(time)))
f.write(group.storage.to_numpy().astype("float32").tobytes())

# Delwaq needs an extra timestep after the end
f.write(struct.pack("<i", int(time + timestep.total_seconds())))
f.write(group.storage.to_numpy().astype("float32").tobytes())

def write_flows(fn: Path | str, data: pd.DataFrame):

def write_flows(fn: Path | str, data: pd.DataFrame, timestep: timedelta):
"""Write flows file for Delwaq.
The format is an int defining the time
Expand All @@ -81,6 +86,10 @@ def write_flows(fn: Path | str, data: pd.DataFrame):
f.write(struct.pack("<i", int(time)))
f.write(group.flow_rate.to_numpy().astype("float32").tobytes())

# Delwaq needs an extra timestep after the end
f.write(struct.pack("<i", int(time + timestep.total_seconds())))
f.write(group.flow_rate.to_numpy().astype("float32").tobytes())


def ugridify(model: ribasim.Model):
node_df = model.node_table().df
Expand All @@ -91,6 +100,7 @@ def ugridify(model: ribasim.Model):
node_df.sort_values("node_id", inplace=True)

edge_df = model.edge.df.copy()
# edge_df = edge_df[edge_df.edge_type == "flow"]
edge_df.set_crs(epsg=28992, inplace=True, allow_override=True)
node_df.set_crs(epsg=28992, inplace=True, allow_override=True)

Expand Down
13 changes: 7 additions & 6 deletions coupling/delwaq/gen_delwaq.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

# Read in model and results
modelfn = repo_dir / "generated_testmodels/basic/ribasim.toml"
# modelfn = Path("hws_2024_3_0/hws.toml") # fixed hws model
# modelfn = repo_dir / "models/hws_2024_3_2/hws.toml"
model = ribasim.Model.read(modelfn)
# model.write("nl_2024/ribasim.toml") # write to new location
basins = pd.read_feather(modelfn.parent / "results" / "basin.arrow")
Expand All @@ -46,6 +46,7 @@
edge = model.edge.df
assert edge is not None
assert (edge.edge_type == "flow").all(), "control edges not yet supported"
# edge = edge[edge.edge_type == "flow"]
edge.set_crs(epsg=28992, inplace=True, allow_override=True)

# Flows on non-existing edges indicate where the boundaries are
Expand Down Expand Up @@ -88,9 +89,9 @@
# Time is internal clock, not real time!
flows.time = (flows.time - flows.time[0]).dt.total_seconds().astype("int32")
flows.drop(columns=["edge_id", "from_node_id", "to_node_id"], inplace=True)
write_flows(output_folder / "ribasim.flo", flows)
write_flows(output_folder / "ribasim.flo", flows, timestep)
write_flows(
output_folder / "ribasim.are", flows
output_folder / "ribasim.are", flows, timestep
) # same as flow, so velocity becomes 1 m/s


Expand All @@ -111,9 +112,9 @@
volumes.sort_values(by=["time", "node_id"], inplace=True)
# volumes.to_csv(output_folder / "volumes.csv", index=False) # not needed
volumes.drop(columns=["node_id"], inplace=True)
write_volumes(output_folder / "ribasim.vol", volumes)
write_volumes(output_folder / "ribasim.vol", volumes, timestep)
volumes.storage = 1 # m/s
write_volumes(output_folder / "ribasim.vel", volumes)
write_volumes(output_folder / "ribasim.vel", volumes, timestep)

# Length file
# Timestep and flattened (2, nsegments) of identical lengths
Expand All @@ -125,7 +126,7 @@
"flow_rate": np.tile(list(lengths), len(rtime)),
}
)
write_flows(output_folder / "ribasim.len", lengths)
write_flows(output_folder / "ribasim.len", lengths, timestep)

# Find our boundaries

Expand Down

0 comments on commit b9658e4

Please sign in to comment.