OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
ReactionIntegrator.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
53
54#ifndef OPENSWMM_ENGINE_TRANSPORT_REACTION_INTEGRATOR_HPP
55#define OPENSWMM_ENGINE_TRANSPORT_REACTION_INTEGRATOR_HPP
56
57#include <string>
58#include <span>
59#include <vector>
60
62
63namespace openswmm::transport {
64
69 bool ok = true;
70 bool interval_limit = false;
71 int substeps = 0;
72 int newton_iters = 0;
73 std::string error;
74};
75// Several aqueous phase contexts may contribute RATE terms to the same
76// physical inventory. Each scale is phase aqueous volume / shared capacity.
77// All contexts see the same species state at every integrator evaluation.
78// Algebraic phase projections retain their separate existing contract.
80 const std::vector<ReactionExprForm>* forms = nullptr;
81 const std::vector<RxExprSpan>* expressions = nullptr;
82 double* hydvar = nullptr;
83 const double* pollutants = nullptr;
84 const double* kinetic_scale = nullptr;
85};
86
90public:
91 void init(const ReactionData& rx);
97 bool initialized() const noexcept { return n_ > 0 || terms_.capacity() > 0; }
98
99private:
100 friend class ReactionIntegrator;
101 int n_ = 0;
102 std::vector<unsigned char> term_needed_;
103 std::vector<double> terms_;
104 std::vector<double> y_, y0_, ytmp_, rates_, err_;
105 std::vector<double> k1_, k2_, k3_, k4_, k5_, k6_;
106 std::vector<double> jac_, lu_;
107 std::vector<int> piv_;
108 std::vector<int> rate_idx_, equil_idx_, formula_idx_;
109 std::vector<double> res_, res2_, dy_;
110 std::vector<double> grp_out_;
111
112 // FD-Jacobian / LU reuse across substeps. The Jacobian costs `gn` extra
113 // RHS evaluations, each of which re-evaluates every term and rate
114 // expression — the dominant per-substep cost once a system has more than
115 // a species or two. J depends on y (not on h), so it survives a step-size
116 // change; the FACTORED matrix I - c*h*J does not, hence the separate
117 // scale key.
118 bool jac_valid_ = false;
119 int jac_age_ = 0;
120 bool lu_valid_ = false;
121 double lu_scale_ = 0.0;
122};
123
125public:
126 // Explicit reactor context. Expressions and forms refer to the same
127 // compiled token pool; groundwater never borrows pipe/tank laws.
170 static RxStepReport step(const ReactionData& rx, const std::vector<ReactionExprForm>& forms,
171 const std::vector<RxExprSpan>& expressions, double dt, double* species,
172 double* hydvar, RxWorkspace& ws, const double* pollutants = nullptr,
173 const double* kinetic_scale = nullptr,
174 const double* coefficients = nullptr,
175 std::span<const RxRateContext> contexts = {});
176 // Opt-in transactional retry for an owned interval. Only a work/floor limit
177 // permits bisection; invalid laws and Newton failures retain their error.
178 // Tolerances are unchanged, and failure restores every supplied species.
220 static RxStepReport
221 stepSubcycled(const ReactionData& rx, const std::vector<ReactionExprForm>& forms,
222 const std::vector<RxExprSpan>& expressions, double dt, double* species,
223 double* hydvar, RxWorkspace& ws, const double* pollutants = nullptr,
224 const double* kinetic_scale = nullptr, const double* coefficients = nullptr,
225 std::span<const RxRateContext> contexts = {});
260 static RxStepReport step(const ReactionData& rx, bool tank, double dt, double* species,
261 double* hydvar, RxWorkspace& ws, const double* pollutants = nullptr,
262 const double* coefficients = nullptr);
263};
264
265} // namespace openswmm::transport
266
267#endif // OPENSWMM_ENGINE_TRANSPORT_REACTION_INTEGRATOR_HPP
Multispecies reaction system data (EPANET-MSX conventions) — SoA, hot/cold split per LARD plan §16 D-...
Definition ReactionIntegrator.hpp:124
static RxStepReport step(const ReactionData &rx, const std::vector< ReactionExprForm > &forms, const std::vector< RxExprSpan > &expressions, double dt, double *species, double *hydvar, RxWorkspace &ws, const double *pollutants=nullptr, const double *kinetic_scale=nullptr, const double *coefficients=nullptr, std::span< const RxRateContext > contexts={})
Integrate one explicit reactor law set over a local species block.
Definition ReactionIntegrator.cpp:370
static RxStepReport stepSubcycled(const ReactionData &rx, const std::vector< ReactionExprForm > &forms, const std::vector< RxExprSpan > &expressions, double dt, double *species, double *hydvar, RxWorkspace &ws, const double *pollutants=nullptr, const double *kinetic_scale=nullptr, const double *coefficients=nullptr, std::span< const RxRateContext > contexts={})
Retry an owned reaction interval by bounded bisection while restoring species on returned failure.
Definition ReactionIntegrator.cpp:329
Definition ReactionIntegrator.hpp:89
friend class ReactionIntegrator
Definition ReactionIntegrator.hpp:100
void init(const ReactionData &rx)
Allocate/reset private integration scratch for a compiled reaction system.
Definition ReactionIntegrator.cpp:285
bool initialized() const noexcept
Read whether this workspace has retained species/term allocation.
Definition ReactionIntegrator.hpp:97
Definition ExplicitFvSolver.hpp:53
Definition ReactionData.hpp:58
Definition ReactionIntegrator.hpp:79
const std::vector< ReactionExprForm > * forms
Definition ReactionIntegrator.hpp:80
const double * kinetic_scale
Definition ReactionIntegrator.hpp:84
double * hydvar
Definition ReactionIntegrator.hpp:82
const double * pollutants
Definition ReactionIntegrator.hpp:83
const std::vector< RxExprSpan > * expressions
Definition ReactionIntegrator.hpp:81
Definition ReactionIntegrator.hpp:68
int substeps
RATE substeps actually taken.
Definition ReactionIntegrator.hpp:71
bool ok
Definition ReactionIntegrator.hpp:69
std::string error
set when !ok
Definition ReactionIntegrator.hpp:73
int newton_iters
EQUIL + implicit-solver Newton iterations.
Definition ReactionIntegrator.hpp:72
bool interval_limit
work/floor limit: caller may retry a smaller interval
Definition ReactionIntegrator.hpp:70