Skip to content

Commit

Permalink
Add update_subgrid_level to BMI functions (#986)
Browse files Browse the repository at this point in the history
Fixes #985
  • Loading branch information
Huite authored Jan 25, 2024
1 parent d928870 commit 2d7c382
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions build/libribasim/src/libribasim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ Base.@ccallable function update_until(time::Cdouble)::Cint
end
end

Base.@ccallable function update_subgrid_level()::Cint
@try_c begin
Ribasim.update_subgrid_level(model)
end
end

Base.@ccallable function get_current_time(time::Ptr{Cdouble})::Cint
@try_c begin
t = BMI.get_current_time(model)
Expand Down
5 changes: 5 additions & 0 deletions core/src/bmi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,11 @@ function BMI.update_until(model::Model, time)::Model
return model
end

function update_subgrid_level(model::Model)::Model
update_subgrid_level!(model.integrator)
return model
end

function BMI.get_value_ptr(model::Model, name::AbstractString)
if name == "volume"
model.integrator.u.storage
Expand Down
11 changes: 11 additions & 0 deletions python/ribasim_api/tests/test_bmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ def test_update_until(libribasim, basic, tmp_path):
assert actual_time == pytest.approx(expected_time)


def test_update_subgrid_level(libribasim, basic, tmp_path):
basic.write(tmp_path / "ribasim.toml")
config_file = str(tmp_path / "ribasim.toml")
libribasim.initialize(config_file)
libribasim.update_subgrid_level()
level = libribasim.get_value_ptr("subgrid_level")
# The subgrid levels are initialized with NaN.
# After calling update, they should have regular values.
assert np.isfinite(level).all()


def test_get_var_type(libribasim, basic, tmp_path):
basic.write(tmp_path / "ribasim.toml")
config_file = str(tmp_path / "ribasim.toml")
Expand Down
11 changes: 10 additions & 1 deletion python/ribasim_testmodels/ribasim_testmodels/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@ def basic_model() -> ribasim.Model:
state = pd.DataFrame(
data={"node_id": static["node_id"], "level": 0.04471158417652035}
)
basin = ribasim.Basin(profile=profile, static=static, state=state)
# This is a 1:1 translation.
subgrid = pd.DataFrame(
data={
"node_id": profile["node_id"],
"subgrid_id": profile["node_id"],
"basin_level": profile["level"],
"subgrid_level": profile["level"],
}
)
basin = ribasim.Basin(profile=profile, static=static, state=state, subgrid=subgrid)

# Setup linear resistance:
linear_resistance = ribasim.LinearResistance(
Expand Down

0 comments on commit 2d7c382

Please sign in to comment.