21#ifndef OPENSWMM_CONTROLS_HPP
22#define OPENSWMM_CONTROLS_HPP
26#include <unordered_map>
27#include "../math/MathExpr.hpp"
31struct SimulationContext;
167 void init(
const std::vector<Rule>&
rules);
193 int addExpression(
const std::string& name,
const std::string& formula);
195 std::vector<Rule>&
rules() {
return rules_; }
242 double current_time,
double half_step);
245 std::vector<Rule> rules_;
246 std::vector<PIDState> pid_states_;
247 std::vector<NamedVariable> named_vars_;
248 std::vector<mathexpr::Expression> expressions_;
249 std::unordered_map<std::string, int> expr_index_;
252 std::vector<PremiseSoA> premise_groups_;
255 std::vector<bool> rule_results_;
259 std::vector<double> link_time_last_set_;
262 double control_value_ = 0.0;
263 double set_point_ = 0.0;
266 struct PendingAction {
272 std::vector<PendingAction> pending_actions_;
276 double current_time,
int param = 0)
const;
279 const Premise& p,
double current_time,
double half_step);
281 bool compareValues(
double lhs,
CompareOp op,
double rhs)
const;
282 bool compareTimes(
double lhs,
CompareOp op,
double rhs,
double half_step)
const;
284 double computePIDSetting(
PIDState& pid,
double control_value,
double dt);
287 double current_time,
double dt);
293 double getNamedVariableValue(
const std::string& name,
295 double current_time)
const;
Definition Controls.hpp:165
int parseRuleText(const std::string &text, SimulationContext &ctx)
Definition Controls.cpp:677
std::vector< Rule > & rules()
Definition Controls.hpp:195
void buildPremiseSoA()
Build SoA index from rules. Called once at init().
Definition Controls.cpp:46
int addExpression(const std::string &name, const std::string &formula)
Definition Controls.cpp:513
void init(const std::vector< Rule > &rules)
Definition Controls.cpp:29
int evaluate(SimulationContext &ctx, double current_time, double dt)
Evaluate all control rules and set link target settings.
Definition Controls.cpp:159
void batchEvaluateGroup(PremiseSoA &group, const SimulationContext &ctx, double current_time, double half_step)
Batch evaluate all premises of one variable type.
Definition Controls.cpp:113
void addNamedVariable(const std::string &name, ConditionVar var, int idx)
Definition Controls.cpp:509
ConditionVar
Definition Controls.hpp:46
@ GAGE_RAIN_PAST
Past n-hours rainfall (n stored in idx field)
@ GAGE_RAIN
Current rainfall intensity.
@ SIM_DAYOFYEAR
Day of year (1-365)
@ SIM_DAY
Day of week (1-7, Sun=1)
LogicOp
Definition Controls.hpp:40
CompareOp
Definition Controls.hpp:39
ActionType
Definition Controls.hpp:84
@ CURVE
Setting from curve lookup(ControlValue)
@ PID
PID controller output.
@ TIMESERIES
Setting from timeseries lookup(currentTime)
@ NUMERIC
Direct numeric setting value.
Definition Controls.cpp:24
Central, reentrant simulation context.
Definition SimulationContext.hpp:141
Definition Controls.hpp:116
int rule_idx
Parent rule index (for priority lookup)
Definition Controls.hpp:118
int link_idx
Link being controlled.
Definition Controls.hpp:117
ActionType type
Definition Controls.hpp:119
int curve_idx
Curve index (for CURVE type)
Definition Controls.hpp:121
double value
Direct value or computed result.
Definition Controls.hpp:120
int tseries_idx
Timeseries index (for TIMESERIES type)
Definition Controls.hpp:122
Pre-sorted premise index for batch evaluation.
Definition Controls.hpp:216
ConditionVar var_type
All premises in this group test this variable.
Definition Controls.hpp:217
std::vector< bool > rhs_is_variable
True if RHS is a variable (not batch-able)
Definition Controls.hpp:228
std::vector< int > premise_idx
Position within the rule's premise list.
Definition Controls.hpp:222
std::vector< int > op
CompareOp as int.
Definition Controls.hpp:224
std::vector< bool > results
Comparison results.
Definition Controls.hpp:233
std::vector< bool > is_expression
True if LHS is an expression.
Definition Controls.hpp:229
std::vector< double > rhs_value
RHS threshold value.
Definition Controls.hpp:225
std::vector< double > lhs_values
Gathered LHS values.
Definition Controls.hpp:232
std::vector< int > obj_idx
Object index (node/link/gage)
Definition Controls.hpp:223
std::vector< int > rule_idx
Which rule this premise belongs to.
Definition Controls.hpp:221
int count
Definition Controls.hpp:218
Definition Controls.hpp:143
int idx
Definition Controls.hpp:146
ConditionVar var
Definition Controls.hpp:145
std::string name
Definition Controls.hpp:144
Definition Controls.hpp:129
double kd
Definition Controls.hpp:132
double kp
Definition Controls.hpp:130
double e2
Error two steps back.
Definition Controls.hpp:135
double ki
Integral time (minutes, 0=disable)
Definition Controls.hpp:131
int action_idx
Associated action index.
Definition Controls.hpp:136
double setpoint
Definition Controls.hpp:133
double e1
Previous error.
Definition Controls.hpp:134
Definition Controls.hpp:95
int lhs_param
Extra parameter (e.g. n-hours for GAGE_RAIN_PAST)
Definition Controls.hpp:102
double rhs_value
Definition Controls.hpp:109
ConditionVar lhs_var
Definition Controls.hpp:100
int expr_idx
Index into expressions_ (-1 if N/A)
Definition Controls.hpp:98
int lhs_idx
Object index (node/link/gage)
Definition Controls.hpp:101
bool is_expression
True if LHS is a math expression.
Definition Controls.hpp:97
ConditionVar rhs_var
Definition Controls.hpp:107
int rhs_idx
Definition Controls.hpp:108
bool rhs_is_variable
Definition Controls.hpp:106
LogicOp logic
Definition Controls.hpp:96
CompareOp op
Definition Controls.hpp:104
Definition Controls.hpp:153
std::string name
Definition Controls.hpp:154
double priority
Definition Controls.hpp:158
std::vector< Action > else_actions
Definition Controls.hpp:157
std::vector< Action > then_actions
Definition Controls.hpp:156
std::vector< Premise > premises
Definition Controls.hpp:155