Skip to content

Commit

Permalink
Fix part of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Apr 10, 2024
1 parent 0cec537 commit 78e8364
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
7 changes: 5 additions & 2 deletions core/src/callback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,10 @@ function discrete_control_affect_upcrossing!(integrator, condition_idx)
# only possibly the du. Parameter changes can change the flow on an edge discontinuously,
# giving the possibility of logical paradoxes where certain parameter changes immediately
# undo the truth state that caused that parameter change.
is_basin = id_index(basin.node_id, discrete_control.listen_node_id[condition_idx])[1]
listen_node_ids = discrete_control.listen_node_id[condition_idx]
is_basin =
length(listen_node_ids) == 1 ? id_index(basin.node_id, only(listen_node_ids))[1] :
false
# NOTE: The above no longer works when listen feature ids can be something other than node ids
# I think the more durable option is to give all possible condition types a different variable string,
# e.g. basin.level and level_boundary.level
Expand Down Expand Up @@ -374,7 +377,7 @@ function discrete_control_affect!(
discrete_control.logic_mapping[(discrete_control_node_id, truth_state)]
else
error(
"Control state specified for neither $truth_state_crossing_specific nor $truth_state for DiscreteControl node $discrete_control_node_id.",
"Control state specified for neither $truth_state_crossing_specific nor $truth_state for $discrete_control_node_id.",
)
end

Expand Down
43 changes: 23 additions & 20 deletions core/src/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -552,29 +552,32 @@ function valid_discrete_control(p::Parameters, config::Config)::Bool
end
end
end
for (Δt, var, node_id) in zip(look_ahead, variable, listen_node_id)
if !iszero(Δt)
node_type = node_id.type
# TODO: If more transient listen variables must be supported, this validation must be more specific
# (e.g. for some node some variables are transient, some not).
if node_type [NodeType.FlowBoundary, NodeType.LevelBoundary]
errors = true
@error "Look ahead supplied for non-timeseries listen variable '$var' from listen node $node_id."
else
if Δt < 0
for (look_aheads, variables, listen_node_ids) in
zip(look_ahead, variable, listen_node_id)
for (Δt, var, node_id) in zip(look_aheads, variables, listen_node_ids)
if !iszero(Δt)
node_type = node_id.type
# TODO: If more transient listen variables must be supported, this validation must be more specific
# (e.g. for some node some variables are transient, some not).
if node_type [NodeType.FlowBoundary, NodeType.LevelBoundary]
errors = true
@error "Negative look ahead supplied for listen variable '$var' from listen node $node_id."
@error "Look ahead supplied for non-timeseries listen variable '$var' from listen node $node_id."
else
node = getfield(p, graph[node_id].type)
idx = if node_type == NodeType.Basin
id_index(node.node_id, node_id)
else
searchsortedfirst(node.node_id, node_id)
end
interpolation = getfield(node, Symbol(var))[idx]
if t_end + Δt > interpolation.t[end]
if Δt < 0
errors = true
@error "Look ahead for listen variable '$var' from listen node $node_id goes past timeseries end during simulation."
@error "Negative look ahead supplied for listen variable '$var' from listen node $node_id."
else
node = getfield(p, graph[node_id].type)
idx = if node_type == NodeType.Basin
id_index(node.node_id, node_id)
else
searchsortedfirst(node.node_id, node_id)
end
interpolation = getfield(node, Symbol(var))[idx]
if t_end + Δt > interpolation.t[end]
errors = true
@error "Look ahead for listen variable '$var' from listen node $node_id goes past timeseries end during simulation."
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions core/test/control_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ end
p = model.integrator.p
(; discrete_control, flow_boundary) = p

Δt = discrete_control.look_ahead[1]
Δt = discrete_control.look_ahead[1][1]

t = Ribasim.tsaves(model)
t_control = discrete_control.record.time[2]
Expand All @@ -78,7 +78,7 @@ end
p = model.integrator.p
(; discrete_control, level_boundary) = p

Δt = discrete_control.look_ahead[1]
Δt = discrete_control.look_ahead[1][1]

t = Ribasim.tsaves(model)
t_control = discrete_control.record.time[2]
Expand Down

0 comments on commit 78e8364

Please sign in to comment.