Skip to content

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
  • Loading branch information
visr committed Jan 22, 2024
1 parent 75635c9 commit 6dee563
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 44 deletions.
12 changes: 2 additions & 10 deletions core/src/bmi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
26 changes: 19 additions & 7 deletions core/src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 383 in core/src/utils.jl

View check run for this annotation

Codecov / codecov/patch

core/src/utils.jl#L382-L383

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

"""
Expand Down
24 changes: 0 additions & 24 deletions core/src/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions core/test/utils_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6dee563

Please sign in to comment.