OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
OdeSolver.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
41
42#ifndef OPENSWMM_ODE_SOLVER_HPP
43#define OPENSWMM_ODE_SOLVER_HPP
44
45#include <functional>
46#include <vector>
47
48namespace openswmm {
49namespace ode {
50
51// ============================================================================
52// Constants (matching legacy)
53// ============================================================================
54
55constexpr int MAXSTP = 10000;
56constexpr double TINY = 1.0e-30;
57constexpr double SAFETY = 0.9;
58constexpr double PGROW = -0.2;
59constexpr double PSHRNK = -0.25;
60constexpr double ERRCON = 1.89e-4;
61
62// ============================================================================
63// Return codes
64// ============================================================================
65
66constexpr int ODE_OK = 0;
67constexpr int ODE_TOO_MANY = 1;
68constexpr int ODE_UNDERFLOW = 2;
69constexpr int ODE_MAX_STEPS = 3;
70
71// ============================================================================
72// Derivative callback
73// ============================================================================
74
83using DerivFunc = std::function<void(double x, const double* y, double* dydx)>;
84
85// ============================================================================
86// Per-element integrator
87// ============================================================================
88
109int integrate(double* y, int n, double x1, double x2, double eps, double h1,
110 const DerivFunc& derivs);
111
118template <typename F>
119int integrateCallable(double* y, int n, double x1, double x2, double eps, double h1, F&& derivs);
120
121// ============================================================================
122// Batch integrator (SoA, for vectorized subcatchment processing)
123// ============================================================================
124
136using BatchDerivFunc = std::function<void(double x, const double* y0, const double* y1, double* dy0,
137 double* dy1, int n_sys)>;
138
155int integrate_batch_2eq(double* y0, double* y1, int n_sys, double x1, double x2, double eps,
156 double h1, const BatchDerivFunc& derivs);
157
158} // namespace ode
159} // namespace openswmm
160
161#include "OdeSolverDetail.hpp"
162
163#endif // OPENSWMM_ODE_SOLVER_HPP
Shared Cash-Karp implementation for typed and erased derivative callbacks.
Definition OdeSolver.cpp:35
constexpr int MAXSTP
Maximum integration steps.
Definition OdeSolver.hpp:55
constexpr double PSHRNK
Exponent for step decrease.
Definition OdeSolver.hpp:59
int integrateCallable(double *y, int n, double x1, double x2, double eps, double h1, F &&derivs)
Integrate with a concrete synchronous derivative callable.
Definition OdeSolverDetail.hpp:223
constexpr double SAFETY
Step adjustment safety factor.
Definition OdeSolver.hpp:57
constexpr double PGROW
Exponent for step increase.
Definition OdeSolver.hpp:58
constexpr int ODE_UNDERFLOW
Step size underflowed.
Definition OdeSolver.hpp:68
constexpr int ODE_OK
Success.
Definition OdeSolver.hpp:66
std::function< void(double x, const double *y, double *dydx)> DerivFunc
Synchronous derivative evaluator for an n-equation ODE system.
Definition OdeSolver.hpp:83
int integrate(double *ystart, int n, double x1, double x2, double eps, double h1, const DerivFunc &derivs)
Advance an ODE system with adaptive Cash-Karp RK45 steps.
Definition OdeSolver.cpp:112
constexpr int ODE_MAX_STEPS
Exceeded MAXSTP.
Definition OdeSolver.hpp:69
constexpr double TINY
Underflow protection.
Definition OdeSolver.hpp:56
int integrate_batch_2eq(double *y0, double *y1, int n_sys, double x1, double x2, double eps, double h1, const BatchDerivFunc &derivs)
Advance independent two-state systems with one explicit Euler step.
Definition OdeSolver.cpp:122
std::function< void(double x, const double *y0, const double *y1, double *dy0, double *dy1, int n_sys)> BatchDerivFunc
Synchronous derivative evaluator for a batch of two-state systems.
Definition OdeSolver.hpp:136
constexpr double ERRCON
= (5/SAFETY)^(1/PGROW)
Definition OdeSolver.hpp:60
constexpr int ODE_TOO_MANY
n > allocated max
Definition OdeSolver.hpp:67
Definition ExchangeLaw.hpp:60
double * dydx
Definition odesolve.c:41
double * y
Definition odesolve.c:37