Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Oct 17, 2023
1 parent d7c613a commit 98528a5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
17 changes: 7 additions & 10 deletions core/src/allocation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,7 @@ function get_allocation_model(
)
end

function allocate!(
p::Parameters,
allocation_model::AllocationModel,
t::Float64;
return_flows::Bool = false,
)::Union{Nothing, Vector{Float64}}
function allocate!(p::Parameters, allocation_model::AllocationModel, t::Float64;)::Nothing
(; node_id_mapping, source_edge_mapping, model) = allocation_model
(; user, connectivity) = p
(; priorities, demand) = user
Expand Down Expand Up @@ -498,9 +493,11 @@ function allocate!(

optimize!(model)

if return_flows
return value.(model[:F])
else
return nothing
for (subnetwork_node_id, (AG_node_id, AG_node_type)) in node_id_mapping
if AG_node_type == :user
user_idx = findsorted(user.node_id, subnetwork_node_id)
base_name = "A_user_$AG_node_id"
user.allocated[user_idx] .= value.(model[Symbol(base_name)])
end
end
end
32 changes: 32 additions & 0 deletions core/test/allocation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Ribasim

@testset "Allocation solve" begin
toml_path = normpath(@__DIR__, "../../generated_testmodels/subnetwork/subnetwork.toml")
@test ispath(toml_path)
cfg = Ribasim.Config(toml_path)
gpkg_path = Ribasim.input_path(cfg, cfg.geopackage)
db = SQLite.DB(gpkg_path)

p = Ribasim.Parameters(db, cfg)
close(db)

# Inputs specific for this test model
subgraph_node_ids = unique(keys(p.lookup))
source_edge_ids = [p.connectivity.edge_ids_flow[(1, 2)]]
flow = Ribasim.get_tmp(p.connectivity.flow, 0)
flow[1, 2] = 4.5 # Source flow
Δt_allocation = 24.0 * 60^2
t = 0.0

allocation_model =
Ribasim.get_allocation_model(p, subgraph_node_ids, source_edge_ids, Δt_allocation)
Ribasim.allocate!(p, allocation_model, t)

F = value.(allocation_model.model[:F])
@test F [3.0, 3.0, 0.5, 3.5, 1.0, 4.5]

allocated = p.user.allocated
@test allocated[1] [0.0, 1.0, 0.0]
@test allocated[2] [0.0, 0.5, 0.0]
@test allocated[3] [3.0, 0.0, 0.0]
end
1 change: 1 addition & 0 deletions core/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using SafeTestsets: @safetestset
@safetestset "Basic Model Interface" include("bmi.jl")
@safetestset "Utility functions" include("utils.jl")
@safetestset "Control" include("control.jl")
@safetesteset "Allocation" include("allocation.jl")
@safetestset "Time" include("time.jl")
@safetestset "Docs" include("docs.jl")
@safetestset "Command Line Interface" include("cli.jl")
Expand Down

0 comments on commit 98528a5

Please sign in to comment.