diff --git a/core/src/callback.jl b/core/src/callback.jl index 09dc5edc6..f9dbd438a 100644 --- a/core/src/callback.jl +++ b/core/src/callback.jl @@ -151,6 +151,7 @@ end function apply_discrete_control!(u, t, integrator)::Nothing (; p) = integrator (; discrete_control) = p + condition_idx = 0 discrete_control_condition!(u, t, integrator) diff --git a/core/src/util.jl b/core/src/util.jl index 1c48e8518..f8ebeabe3 100644 --- a/core/src/util.jl +++ b/core/src/util.jl @@ -709,3 +709,19 @@ function get_influx(basin::Basin, basin_idx::Int)::Float64 return precipitation[basin_idx] - evaporation[basin_idx] + drainage[basin_idx] - infiltration[basin_idx] end + +function get_discrete_control_indices(discrete_control::DiscreteControl, condition_idx::Int) + (; greater_than) = discrete_control + condition_idx_now = 1 + + for (compound_variable_idx, vec) in enumerate(greater_than) + l = length(vec) + + if condition_idx_now + l > condition_idx + greater_than_idx = condition_idx - condition_idx_now + 1 + return compound_variable_idx, greater_than_idx + end + + condition_idx_now += l + end +end diff --git a/core/src/validation.jl b/core/src/validation.jl index 821375e0c..dc39244fc 100644 --- a/core/src/validation.jl +++ b/core/src/validation.jl @@ -573,8 +573,6 @@ function valid_discrete_control(p::Parameters, config::Config)::Bool 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 "Look ahead supplied for non-timeseries listen variable '$var' from listen node $node_id." diff --git a/docs/core/usage.qmd b/docs/core/usage.qmd index a736c41cd..88374f8ef 100644 --- a/docs/core/usage.qmd +++ b/docs/core/usage.qmd @@ -639,7 +639,7 @@ which can be for instance an average or a difference of variables. If a variable column | type | unit | restriction -------------------- | -------- | ------- | ----------- node_id | Int32 | - | sorted -compound_variable_id | String | - | sorted per node_id +compound_variable_id | Int32 | - | sorted per node_id listen_node_type | String | - | known node type listen_node_id | Int32 | - | sorted per node_id variable | String | - | must be "level" or "flow_rate", sorted per listen_node_id @@ -648,7 +648,7 @@ look_ahead | Float64 | $s$ | Only on transient boundary condition ## DiscreteControl / condition -The condition schema defines conditions of the form 'the `discrete_control` node with this `node_id`listens to whether the variable given by the `node_id` and `compound_variable_id` is greater than `greater_than`'. If the condition variable comes from a time-series, a look ahead $\Delta t$ can be supplied. Multiple conditions with different `greater_than` values can be defined on the same compound_variable. +The condition schema defines conditions of the form 'the `discrete_control` node with this `node_id`listens to whether the variable given by the `node_id` and `compound_variable_id` is greater than `greater_than`'. Multiple conditions with different `greater_than` values can be defined on the same compound_variable. column | type | unit | restriction -------------------- | -------- | ------- | -----------