Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Nov 20, 2023
1 parent 93e56ff commit 33a3fb7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
10 changes: 7 additions & 3 deletions core/src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ end
"""
Store the connectivity information
graph_flow, graph_control: directed graph with vertices equal to ids
graph: a directed metagraph with data of nodes (NodeMetadata):
- Node type (snake case)
- Allocation network ID
and data of edges (EdgeMetadata):
- Edge ID (EdgeID)
- type (flow/control)
flow: store the flow on every flow edge
edge_ids_flow, edge_ids_control: get the external edge id from (src, dst)
edge_connection_type_flow, edge_connection_types_control: get (src_node_type, dst_node_type) from edge id
Expand Down Expand Up @@ -744,8 +749,7 @@ function continuous_control!(
if controls_pump
for id in outneighbor_labels_type(graph, controlled_node_id, EdgeType.flow)
if id in fractional_flow.node_id
after_ff_id =
only(outneighbor_labels_type(graph_flow, id, EdgeType.flow))
after_ff_id = only(outneighbor_labels_type(graph, id, EdgeType.flow))
ff_idx = findsorted(fractional_flow, id)
flow_rate_fraction = fractional_flow.fraction[ff_idx] * flow_rate
flow[id, after_ff_id] = flow_rate_fraction
Expand Down
6 changes: 3 additions & 3 deletions core/src/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ Check that nodes that have fractional flow outneighbors do not have any other ty
outneighbor, that the fractions leaving a node add up to ≈1 and that the fractions are non-negative.
"""
function valid_fractional_flow(
graph_flow::MetaGraph,
graph::MetaGraph,
node_id::Vector{NodeID},
control_mapping::Dict{Tuple{NodeID, String}, NamedTuple},
)::Bool
Expand All @@ -581,14 +581,14 @@ function valid_fractional_flow(
src_ids = Set{NodeID}()

for id in node_id
union!(src_ids, inneighbor_labels(graph_flow, id))
union!(src_ids, inneighbor_labels(graph, id))
end

node_id_set = Set{NodeID}(node_id)
control_states = Set{String}([key[2] for key in keys(control_mapping)])

for src_id in src_ids
src_outneighbor_ids = Set(outneighbor_labels(graph_flow, src_id))
src_outneighbor_ids = Set(outneighbor_labels(graph, src_id))
if src_outneighbor_ids node_id
errors = true
@error(
Expand Down
8 changes: 5 additions & 3 deletions core/test/validation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ end
end

@testitem "Neighbor count validation" begin
using MetaGraphsNext: MetaGraph
using Graphs: DiGraph
using Logging

Expand Down Expand Up @@ -135,6 +136,7 @@ end
end

@testitem "PidControl connectivity validation" begin
using MetaGraphsNext: MetaGraph
using Graphs: DiGraph
using Dictionaries: Indices
using Logging
Expand Down Expand Up @@ -209,7 +211,7 @@ end
logger = TestLogger()
with_logger(logger) do
@test !Ribasim.valid_fractional_flow(
connectivity.graph_flow,
connectivity.graph,
fractional_flow.node_id,
fractional_flow.control_mapping,
)
Expand All @@ -222,13 +224,13 @@ end
@test logger.logs[2].level == Error
@test logger.logs[2].message ==
"Fractional flow nodes must have non-negative fractions."
@test logger.logs[2].kwargs[:node_id] == 3
@test logger.logs[2].kwargs[:node_id].value == 3
@test logger.logs[2].kwargs[:fraction] -0.1
@test logger.logs[2].kwargs[:control_state] == ""
@test logger.logs[3].level == Error
@test logger.logs[3].message ==
"The sum of fractional flow fractions leaving a node must be ≈1."
@test logger.logs[3].kwargs[:node_id] == 7
@test logger.logs[3].kwargs[:node_id].value == 7
@test logger.logs[3].kwargs[:fraction_sum] 0.4
@test logger.logs[3].kwargs[:control_state] == ""
end
Expand Down

0 comments on commit 33a3fb7

Please sign in to comment.