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
82using DerivFunc = std::function<void(double x, const double* y, double* dydx)>;
83
84// ============================================================================
85// Per-element integrator
86// ============================================================================
87
100int integrate(double* y, int n, double x1, double x2,
101 double eps, double h1, const DerivFunc& derivs);
102
103// ============================================================================
104// Batch integrator (SoA, for vectorized subcatchment processing)
105// ============================================================================
106
126using BatchDerivFunc = std::function<void(double x,
127 const double* y0, const double* y1,
128 double* dy0, double* dy1, int n_sys)>;
129
130int integrate_batch_2eq(double* y0, double* y1, int n_sys,
131 double x1, double x2, double eps, double h1,
132 const BatchDerivFunc& derivs);
133
134} // namespace ode
135} // namespace openswmm
136
137#endif // OPENSWMM_ODE_SOLVER_HPP
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
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
Derivative function signature.
Definition OdeSolver.hpp:82
int integrate(double *ystart, int n, double x1, double x2, double eps, double h1, const DerivFunc &derivs)
Integrate an ODE system from x1 to x2 using RK45 Cash-Karp.
Definition OdeSolver.cpp:201
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)
Definition OdeSolver.cpp:235
std::function< void(double x, const double *y0, const double *y1, double *dy0, double *dy1, int n_sys)> BatchDerivFunc
Integrate N independent 2-equation ODE systems in batch.
Definition OdeSolver.hpp:126
constexpr double ERRCON
= (5/SAFETY)^(1/PGROW)
Definition OdeSolver.hpp:60
constexpr int ODE_TOO_MANY
n > allocated max
Definition OdeSolver.hpp:67
Definition NodeCoupling.cpp:16
double * dydx
Definition odesolve.c:32
double * y
Definition odesolve.c:28