Skip to content

Commit

Permalink
Update docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Apr 16, 2024
1 parent 9765dc2 commit 267d412
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
24 changes: 6 additions & 18 deletions core/src/callback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ function set_initial_discrete_controlled_parameters!(
condition_diffs = zeros(Float64, n_conditions)
discrete_control_condition(condition_diffs, storage0, integrator.t, integrator)

# Set the discrete control value (bool) per compound variable
idx_start = 1
for (i, vec) in enumerate(discrete_control.condition_value)
for (compound_variable_idx, vec) in enumerate(discrete_control.condition_value)
l = length(vec)
idx_end = idx_start + l - 1
discrete_control.condition_value[i] .= (condition_diffs[idx_start:idx_end] .> 0)
discrete_control.condition_value[compound_variable_idx] .=
(condition_diffs[idx_start:idx_end] .> 0)
idx_start += l
end

Expand Down Expand Up @@ -202,6 +204,7 @@ function discrete_control_condition(out, u, t, integrator)
(; discrete_control) = p
condition_idx = 0

# Loop over compound variables
for (listen_node_ids, variables, weights, greater_thans, look_aheads) in zip(
discrete_control.listen_node_id,
discrete_control.variable,
Expand All @@ -214,6 +217,7 @@ function discrete_control_condition(out, u, t, integrator)
zip(listen_node_ids, variables, weights, look_aheads)
value += weight * get_value(p, listen_node_id, variable, look_ahead, u, t)
end
# Loop over greater_than values for this compound_variable
for greater_than in greater_thans
condition_idx += 1
diff = value - greater_than
Expand Down Expand Up @@ -268,22 +272,6 @@ function get_value(
return value
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

"""
An upcrossing means that a condition (always greater than) becomes true.
"""
Expand Down
15 changes: 7 additions & 8 deletions core/src/parameter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -443,14 +443,13 @@ struct Terminal <: AbstractParameterNode
end

"""
node_id: node ID of the DiscreteControl node; these are not unique but repeated
by the amount of conditions of this DiscreteControl node
listen_node_id: the IDs of the nodes being condition on
variable: the names of the variables in the condition
weight: the weight of the variables in the condition
look_ahead: the look ahead of variables in the condition in seconds
greater_than: The threshold value in the condition
condition_value: The current value of each condition
node_id: node ID of the DiscreteControl node per compound variable (can contain repeats)
listen_node_id: the IDs of the nodes being condition on per compound variable
variable: the names of the variables in the condition per compound variable
weight: the weight of the variables in the condition per compound variable
look_ahead: the look ahead of variables in the condition in seconds per compound_variable
greater_than: The threshold values per compound variable
condition_value: The current truth value of each condition per compound_variable per greater_than
control_state: Dictionary: node ID => (control state, control state start)
logic_mapping: Dictionary: (control node ID, truth state) => control state
record: Namedtuple with discrete control information for results
Expand Down
2 changes: 2 additions & 0 deletions core/src/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,11 @@ function parse_variables_and_conditions(compound_variable, condition)
condition_value = BitVector[]
errors = false

# Loop over unique discrete_control node IDs (on which at least one condition is defined)
for id in unique(condition.node_id)
condition_group_id = filter(row -> row.node_id == id, condition)
variable_group_id = filter(row -> row.node_id == id, compound_variable)
# Loop over compound variables for this node ID
for compound_variable_id in unique(condition_group_id.compound_variable_id)
condition_group_variable = filter(
row -> row.compound_variable_id == compound_variable_id,
Expand Down
16 changes: 16 additions & 0 deletions core/src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 267d412

Please sign in to comment.