Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add update_subgrid_level to BMI functions #986

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
return model
end

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

Check warning on line 638 in core/src/bmi.jl

View check run for this annotation

Codecov / codecov/patch

core/src/bmi.jl#L636-L638

Added lines #L636 - L638 were not covered by tests
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