From 6dee56358f355dc4f8aff24fa9ca7a03d7d3059a Mon Sep 17 00:00:00 2001 From: Martijn Visser Date: Mon, 22 Jan 2024 11:39:02 +0100 Subject: [PATCH] finish --- core/src/bmi.jl | 12 ++---------- core/src/utils.jl | 26 +++++++++++++++++++------- core/src/validation.jl | 24 ------------------------ core/test/utils_test.jl | 4 +--- 4 files changed, 22 insertions(+), 44 deletions(-) diff --git a/core/src/bmi.jl b/core/src/bmi.jl index d2d7f43bb..7f11ee34d 100644 --- a/core/src/bmi.jl +++ b/core/src/bmi.jl @@ -73,16 +73,8 @@ function BMI.initialize(T::Type{Model}, config::Config)::Model end @debug "Read database into memory." - storage = if isempty(state) - # default to nearly empty basins, perhaps make required input - fill(1.0, n) - else - storages, errors = get_storages_from_levels(parameters.basin, state.level) - if errors - error("Encountered errors while parsing the initial levels of basins.") - end - storages - end + storage = get_storages_from_levels(parameters.basin, state.level) + # Synchronize level with storage set_current_basin_properties!(parameters.basin, storage) diff --git a/core/src/utils.jl b/core/src/utils.jl index 5040f786c..20ff9f75c 100644 --- a/core/src/utils.jl +++ b/core/src/utils.jl @@ -374,16 +374,28 @@ function get_storage_from_level(basin::Basin, state_idx::Int, level::Float64)::F end """Compute the storages of the basins based on the water level of the basins.""" -function get_storages_from_levels( - basin::Basin, - levels::Vector, -)::Tuple{Vector{Float64}, Bool} - storages = Float64[] +function get_storages_from_levels(basin::Basin, levels::Vector)::Vector{Float64} + errors = false + state_length = length(levels) + basin_length = length(basin.level) + if state_length != basin_length + @error "Unexpected 'Basin / state' length." state_length basin_length + errors = true + end + storages = zeros(state_length) for (i, level) in enumerate(levels) - push!(storages, get_storage_from_level(basin, i, level)) + storage = get_storage_from_level(basin, i, level) + if isnan(storage) + errors = true + end + storages[i] = storage + end + if errors + error("Encountered errors while parsing the initial levels of basins.") end - return storages, any(isnan.(storages)) + + return storages end """ diff --git a/core/src/validation.jl b/core/src/validation.jl index d49dc3506..922b02fe2 100644 --- a/core/src/validation.jl +++ b/core/src/validation.jl @@ -338,30 +338,6 @@ function variable_nt(s::Any) NamedTuple{names}((getfield(s, x) for x in names)) end -function is_consistent(node, edge, state, static, profile, time) - - # Check that node ids exist - # TODO Do we need to check the reverse as well? All ids in use? - ids = node.fid - @assert edge.from_node_id ⊆ ids "Edge from_node_id not in node ids" - @assert edge.to_node_id ⊆ ids "Edge to_node_id not in node ids" - @assert state.node_id ⊆ ids "State id not in node ids" - @assert static.node_id ⊆ ids "Static id not in node ids" - @assert profile.node_id ⊆ ids "Profile id not in node ids" - @assert time.node_id ⊆ ids "Time id not in node ids" - - # Check edges for uniqueness - @assert allunique(edge, [:from_node_id, :to_node_id]) "Duplicate edge found" - - # TODO Check states - - # TODO Check statics - - # TODO Check forcings - - true -end - # functions used by sort(x; by) sort_by_fid(row) = row.fid sort_by_id(row) = row.node_id diff --git a/core/test/utils_test.jl b/core/test/utils_test.jl index ed2bc1a1e..d4263a878 100644 --- a/core/test/utils_test.jl +++ b/core/test/utils_test.jl @@ -125,9 +125,7 @@ end logger = TestLogger() with_logger(logger) do - storages, errors = Ribasim.get_storages_from_levels(basin, [-1.0]) - @test isnan(storages[1]) - @test errors + @test_throws ErrorException Ribasim.get_storages_from_levels(basin, [-1.0]) end @test length(logger.logs) == 1