OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
Treatment.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2//
3// Copyright 2026 Caleb Buahin
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
47
48#ifndef OPENSWMM_TREATMENT_HPP
49#define OPENSWMM_TREATMENT_HPP
50
51#include <string>
52#include <vector>
53
54namespace openswmm {
55
56namespace treatment {
57
58// ============================================================================
59// Expression token types
60// ============================================================================
61
62enum class TokenType : int {
63 NUMBER = 0,
65 ADD = 2,
66 SUB = 3,
67 MUL = 4,
68 DIV = 5,
69 POW = 6,
70 NEG = 7,
78 FUNC_STEP = 15,
79 LPAREN = 16,
80 RPAREN = 17,
81 COMMA = 18
82};
83
84enum class TreatVar : int {
85 C = 0,
86 R = 1,
87 DT = 2,
88 HRT = 3,
89 Q = 4,
90 V = 5,
91 D = 6,
94 AREA = 9
95};
96
97// ============================================================================
98// Expression token
99// ============================================================================
100
107
108// ============================================================================
109// Treatment expression (parsed, ready to evaluate)
110// ============================================================================
111
112struct TreatExpr {
113 std::vector<Token> tokens;
114 int pollutant_idx = -1;
115 bool is_removal = false;
116};
117
118// ============================================================================
119// Functions
120// ============================================================================
121
139int parse(const std::string& expr_str, TreatExpr& result);
140
149int parse(const std::string& expr_str, TreatExpr& result,
150 int (*pollut_lookup)(const std::string& name));
151
168int validate(const std::string& expr_str, std::string& msg, int& col);
169
182double evaluate(const TreatExpr& expr, double c, double dt,
183 double hrt, double q, double v, double d, double area = 0.0);
184
201double evaluate(const TreatExpr& expr, double c, double dt,
202 double hrt, double q, double v, double d,
203 const double* cin, const double* removal, int n_pollut,
204 double area = 0.0);
205
224double applyTreatment(const TreatExpr& expr, double c_in, double dt,
225 double hrt, double q, double v, double d, double area = 0.0);
226
227} // namespace treatment
228} // namespace openswmm
229
230#endif // OPENSWMM_TREATMENT_HPP
Definition Treatment.cpp:42
double applyTreatment(const TreatExpr &expr, double c_in, double dt, double hrt, double q, double v, double d, double area)
Apply treatment at a node for one pollutant.
Definition Treatment.cpp:644
TokenType
Definition Treatment.hpp:62
@ POW
Definition Treatment.hpp:69
@ SUB
Definition Treatment.hpp:66
@ DIV
Definition Treatment.hpp:68
@ MUL
Definition Treatment.hpp:67
@ NUMBER
Definition Treatment.hpp:63
@ LPAREN
Left parenthesis (parser internal, never in output)
Definition Treatment.hpp:79
@ FUNC_SQRT
Definition Treatment.hpp:73
@ COMMA
Comma separator for min/max (parser internal)
Definition Treatment.hpp:81
@ NEG
Unary negation.
Definition Treatment.hpp:70
@ FUNC_MIN
Definition Treatment.hpp:74
@ FUNC_SGN
Definition Treatment.hpp:77
@ FUNC_MAX
Definition Treatment.hpp:75
@ FUNC_LOG
Definition Treatment.hpp:72
@ FUNC_STEP
Heaviside step.
Definition Treatment.hpp:78
@ ADD
Definition Treatment.hpp:65
@ FUNC_EXP
Definition Treatment.hpp:71
@ VARIABLE
Definition Treatment.hpp:64
@ FUNC_ABS
Definition Treatment.hpp:76
@ RPAREN
Right parenthesis (parser internal, never in output)
Definition Treatment.hpp:80
double evaluate(const TreatExpr &expr, double c, double dt, double hrt, double q, double v, double d, double area)
Evaluate a treatment expression with given variable values.
Definition Treatment.cpp:529
TreatVar
Definition Treatment.hpp:84
@ C
Current concentration (this pollutant)
Definition Treatment.hpp:85
@ R_POLLUT
Removal fraction of another pollutant (uses pollut_ref, co-treatment)
Definition Treatment.hpp:93
@ C_POLLUT
Concentration of another pollutant (uses pollut_ref)
Definition Treatment.hpp:92
@ HRT
Hydraulic residence time (hours, per legacy convention)
Definition Treatment.hpp:88
@ DT
Timestep (sec)
Definition Treatment.hpp:87
@ V
Volume (ft3 or m3)
Definition Treatment.hpp:90
@ AREA
Node surface area in user units² ((a1+a2)/2*UCF(LENGTH)², matching legacy pvAREA)
Definition Treatment.hpp:94
@ R
Removal fraction (this pollutant, unused in expressions)
Definition Treatment.hpp:86
@ Q
Flow rate in user units (Q*UCF(FLOW), matching legacy pvFLOW)
Definition Treatment.hpp:89
@ D
Depth in user units (y*UCF(LENGTH), matching legacy pvDEPTH)
Definition Treatment.hpp:91
int parse(const std::string &expr_str, TreatExpr &result)
Parse a treatment expression string into postfix tokens.
Definition Treatment.cpp:291
int validate(const std::string &expr_str, std::string &msg, int &col)
Validate a treatment expression without touching engine state.
Definition Treatment.cpp:346
Definition NodeCoupling.cpp:16
Definition Treatment.hpp:101
double value
For NUMBER tokens.
Definition Treatment.hpp:103
int pollut_ref
Pollutant index for C_POLLUT/R_POLLUT.
Definition Treatment.hpp:105
TokenType type
Definition Treatment.hpp:102
TreatVar var
For VARIABLE tokens.
Definition Treatment.hpp:104
Definition Treatment.hpp:112
bool is_removal
True if expression computes R (removal), false if C.
Definition Treatment.hpp:115
std::vector< Token > tokens
Postfix (RPN) token list.
Definition Treatment.hpp:113
int pollutant_idx
Which pollutant this expression applies to.
Definition Treatment.hpp:114