OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
ReactionData.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
37
38#ifndef OPENSWMM_ENGINE_DATA_REACTION_DATA_HPP
39#define OPENSWMM_ENGINE_DATA_REACTION_DATA_HPP
40
41#include <cstdint>
42#include <string>
43#include <string_view>
44#include <vector>
45
46#include "ReactionTokens.hpp"
47
48namespace openswmm {
49
50enum class ReactionSolverKind : int { EUL = 0, RK5 = 1, ROS2 = 2, BDF2 = 3 };
51enum class ReactionCoupling : int { NONE = 0, FULL = 1 };
52enum class ReactionRateUnits : int { SEC = 0, MIN = 1, HR = 2, DAY = 3 };
53enum class ReactionAreaUnits : int { FT2 = 0, M2 = 1, CM2 = 2 };
54enum class ReactionExprForm : int { NONE = 0, RATE = 1, EQUIL = 2, FORMULA = 3 };
55
57 // ---- [REACTION_OPTIONS] ------------------------------------------------
89 double timestep = 0.0;
90 double atol = 1.0e-6;
91 double rtol = 1.0e-4;
95 double default_temp_c = 20.0;
96
97 // ---- [REACTION_SPECIES] — index-aligned with the SpeciesRegistry MSX
98 // block (registry index = registry_base + i). ------------------------
99 int registry_base = -1;
100 std::vector<std::string> species_name;
101 std::vector<uint8_t> species_is_wall;
102 std::vector<std::string> species_units;
103 std::vector<double> species_atol;
104 std::vector<double> species_rtol;
105
106 // ---- [REACTION_COEFFICIENTS] -------------------------------------------
107 std::vector<std::string> coef_name;
108 std::vector<uint8_t> coef_is_param;
109 std::vector<double> coef_value;
111
112 // ---- [REACTION_TERMS] --------------------------------------------------
113 std::vector<std::string> term_name;
114 std::vector<std::string> term_expr_src;
115
116 // ---- [REACTION_PIPES] / [REACTION_TANKS] — dense per species -----------
117 std::vector<ReactionExprForm> pipe_form;
118 std::vector<std::string> pipe_expr_src;
119 std::vector<ReactionExprForm> tank_form;
120 std::vector<std::string> tank_expr_src;
121
122 // ---- [REACTION_QUALITY] GLOBAL initial values (R1) ---------------------
123 std::vector<double> init_global;
124
125 // ---- [REACTION_QUALITY] NODE/LINK per-element overrides (E-B) ----------
126 // Sparse row table over the GLOBAL seed; covered by clear() via the
127 // *this = ReactionData{} reset.
128 std::vector<uint8_t> init_elem_is_link;
129 std::vector<int> init_elem_idx;
130 std::vector<int> init_elem_species;
131 std::vector<double> init_elem_value;
132
133 // ---- Compiled bytecode (R2 — D-L3 flat pool; spans index token_pool) ----
134 std::vector<RxToken> token_pool;
135 std::vector<RxExprSpan> term_expr;
136 std::vector<RxExprSpan> pipe_expr;
137 std::vector<RxExprSpan> tank_expr;
138 bool compiled = false;
140
141 // ---- R4: MSX species element state under QUALITY_SOLVER LEGACY --------
142 // [element * n_species + s]; sized lazily by the legacy binding. R4b
143 // (2026-09-01): species react per element AND advect between elements
144 // (routeLegacyMsx, the CSTR mirror family) — the not-transported
145 // warning and its once-per-run flag are gone with the limitation.
146 std::vector<double> msx_node_conc;
147 std::vector<double> msx_link_conc;
148
149 // ---- U2 (2026-09-07): external MSX loads at the node seam ------------
150 // [node * n_species + m], a mass RATE in the species' internal unit
151 // convention (conc-units × ft3/s — the qual_mass_in shape), assembled
152 // every routing step by InflowSolver::computeAll from [INFLOWS] rows
153 // that name a species (CONCEN × node external flow, or MASS ÷ LperFT3).
154 // Empty until a species inflow row exists. Consumed by every quality
155 // engine's node mixing: routeLegacyMsx (LEGACY), ArdEngine stage 1b /
156 // 1a' (EULERIAN_ARD) and the LARD node stage.
157 std::vector<double> msx_ext_mass_in;
158
159 bool configured = false;
160
161 int n_species() const noexcept {
162 return static_cast<int>(species_name.size());
163 }
164 int find_species(std::string_view n) const {
165 for (std::size_t i = 0; i < species_name.size(); ++i)
166 if (species_name[i] == n) return static_cast<int>(i);
167 return -1;
168 }
169 int find_coef(std::string_view n) const {
170 for (std::size_t i = 0; i < coef_name.size(); ++i)
171 if (coef_name[i] == n) return static_cast<int>(i);
172 return -1;
173 }
174 int find_term(std::string_view n) const {
175 for (std::size_t i = 0; i < term_name.size(); ++i)
176 if (term_name[i] == n) return static_cast<int>(i);
177 return -1;
178 }
179
180 void clear() { *this = ReactionData{}; }
181};
182
183} // namespace openswmm
184
185#endif // OPENSWMM_ENGINE_DATA_REACTION_DATA_HPP
POD reaction-VM instruction + span types (phase R2, D-L3 flat pool). Data-layer header: no dependenci...
@ NONE
Definition SimulationContext.hpp:180
Definition NodeCoupling.cpp:16
ReactionSolverKind
Definition ReactionData.hpp:50
@ ROS2
Definition ReactionData.hpp:50
@ EUL
Definition ReactionData.hpp:50
@ BDF2
Definition ReactionData.hpp:50
@ RK5
Definition ReactionData.hpp:50
ReactionRateUnits
Definition ReactionData.hpp:52
@ DAY
Definition ReactionData.hpp:52
@ SEC
Definition ReactionData.hpp:52
@ MIN
Definition ReactionData.hpp:52
@ HR
Definition ReactionData.hpp:52
ReactionCoupling
Definition ReactionData.hpp:51
@ NONE
Definition ReactionData.hpp:51
@ FULL
Definition ReactionData.hpp:51
ReactionExprForm
Definition ReactionData.hpp:54
@ EQUIL
Definition ReactionData.hpp:54
@ RATE
Definition ReactionData.hpp:54
@ FORMULA
Definition ReactionData.hpp:54
ReactionAreaUnits
Definition ReactionData.hpp:53
@ CM2
Definition ReactionData.hpp:53
@ M2
Definition ReactionData.hpp:53
@ FT2
Definition ReactionData.hpp:53
Definition ReactionData.hpp:56
int find_species(std::string_view n) const
Definition ReactionData.hpp:164
std::vector< uint8_t > coef_is_param
1 = PARAMETER (overridable), 0 = CONSTANT
Definition ReactionData.hpp:108
std::vector< ReactionExprForm > pipe_form
size n_species
Definition ReactionData.hpp:117
void clear()
Definition ReactionData.hpp:180
std::vector< RxExprSpan > tank_expr
per species
Definition ReactionData.hpp:137
bool compiled
R2 compile pass succeeded.
Definition ReactionData.hpp:138
double default_temp_c
Definition ReactionData.hpp:95
double rtol
Definition ReactionData.hpp:91
std::vector< std::string > pipe_expr_src
size n_species (cold)
Definition ReactionData.hpp:118
std::vector< RxExprSpan > pipe_expr
per species (len 0 ⇒ none)
Definition ReactionData.hpp:136
std::vector< int > init_elem_species
0..n_species()-1
Definition ReactionData.hpp:130
bool warned_react_failure
Definition ReactionData.hpp:139
double atol
Definition ReactionData.hpp:90
std::vector< double > species_rtol
hot (0 ⇒ global)
Definition ReactionData.hpp:104
std::vector< std::string > species_name
cold
Definition ReactionData.hpp:100
std::vector< double > msx_ext_mass_in
Definition ReactionData.hpp:157
ReactionAreaUnits area_units
Definition ReactionData.hpp:88
int registry_base
first MSX index in the registry
Definition ReactionData.hpp:99
std::vector< double > msx_node_conc
Definition ReactionData.hpp:146
int find_term(std::string_view n) const
Definition ReactionData.hpp:174
std::vector< uint8_t > init_elem_is_link
0 = NODE row, 1 = LINK row
Definition ReactionData.hpp:128
std::vector< RxToken > token_pool
hot: one contiguous pool
Definition ReactionData.hpp:134
std::vector< int > init_elem_idx
resolved node/link index
Definition ReactionData.hpp:129
std::vector< ReactionExprForm > tank_form
Definition ReactionData.hpp:119
int find_coef(std::string_view n) const
Definition ReactionData.hpp:169
double timestep
0 ⇒ follow QUALITY_STEP
Definition ReactionData.hpp:89
int n_species() const noexcept
Definition ReactionData.hpp:161
std::vector< std::string > species_units
cold
Definition ReactionData.hpp:102
std::vector< std::string > coef_name
cold
Definition ReactionData.hpp:107
std::vector< double > coef_value
Definition ReactionData.hpp:109
std::vector< std::string > term_name
cold
Definition ReactionData.hpp:113
std::vector< double > msx_link_conc
Definition ReactionData.hpp:147
ReactionSolverKind solver
Default integrator.
Definition ReactionData.hpp:85
std::vector< double > species_atol
hot (0 ⇒ global)
Definition ReactionData.hpp:103
std::vector< std::string > term_expr_src
cold (compiled in R2)
Definition ReactionData.hpp:114
bool configured
a reactions component applied
Definition ReactionData.hpp:159
ReactionRateUnits rate_units
Definition ReactionData.hpp:87
std::vector< double > init_elem_value
Definition ReactionData.hpp:131
std::vector< RxExprSpan > term_expr
per term, in-order evaluation
Definition ReactionData.hpp:135
std::vector< std::string > tank_expr_src
Definition ReactionData.hpp:120
ReactionCoupling coupling
Definition ReactionData.hpp:86
std::vector< uint8_t > species_is_wall
0 = BULK, 1 = WALL
Definition ReactionData.hpp:101
std::vector< double > init_global
size n_species
Definition ReactionData.hpp:123