Skip to content

Commit

Permalink
Added profile interpolation test
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Jun 16, 2023
1 parent cddd1de commit 9b3acdd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
9 changes: 9 additions & 0 deletions core/src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ function get_area_and_level(
area_discrete = basin.area[state_idx]
level_discrete = basin.level[state_idx]

return get_area_and_level(storage_discrete, area_discrete, level_discrete, storage)
end

function get_area_and_level(
storage_discrete::Vector{Float64},
area_discrete::Vector{Float64},
level_discrete::Vector{Float64},
storage::Float64,
)::Tuple{Float64, Float64}
# storage_idx: smallest index such that storage_discrete[storage_idx] >= storage
storage_idx = searchsortedfirst(storage_discrete, storage)

Expand Down
40 changes: 39 additions & 1 deletion core/test/basin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ using Ribasim
import BasicModelInterface as BMI
using SciMLBase


@testset "trivial model" begin
toml_path = normpath(@__DIR__, "../../data/trivial/trivial.toml")
@test ispath(toml_path)
Expand Down Expand Up @@ -44,3 +43,42 @@ end
# the highest level in the dynamic table is updated to 1.2 from the callback
@test model.integrator.p.tabulated_rating_curve.tables[end].t[end] == 1.2
end

@testset "Profile" begin
n_interpolations = 100
storage = range(0.0, 1000.0, n_interpolations)

# Covers interpolation for constant and non-constant area, extrapolation for constant area
area_discrete = [0.0, 100.0, 100.0]
level_discrete = [0.0, 10.0, 15.0]
storage_discrete = Ribasim.profile_storage(level_discrete, area_discrete)

area, level = zip(
[
Ribasim.get_area_and_level(storage_discrete, area_discrete, level_discrete, s) for s in storage
]...,
)

level_expected =
ifelse.(storage .< 500.0, sqrt.(storage ./ 5), 10.0 .+ (storage .- 500.0) ./ 100.0)

@test all(level .≈ level_expected)
area_expected = min.(10.0 * level_expected, 100.0)
@test all(area .≈ area_expected)

# Covers extrapolation for non-constant area
area_discrete = [0.0, 100.0]
level_discrete = [0.0, 10.0]
storage_discrete = Ribasim.profile_storage(level_discrete, area_discrete)

area, level = zip(
[
Ribasim.get_area_and_level(storage_discrete, area_discrete, level_discrete, s) for s in storage
]...,
)

level_expected = sqrt.(storage ./ 5)
@test all(level .≈ level_expected)
area_expected = 10.0 * level_expected
@test all(area .≈ area_expected)
end

0 comments on commit 9b3acdd

Please sign in to comment.