OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
Runoff.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_RUNOFF_HPP
39#define OPENSWMM_RUNOFF_HPP
40
42#include "Infiltration.hpp"
43#include <vector>
44
45namespace openswmm {
46
48
49namespace runoff {
50
51// ============================================================================
52// Constants
53// ============================================================================
54
55constexpr double MEXP = 1.6666667;
56constexpr double ODETOL = 0.0001;
57constexpr double PHI = 1.486;
58
59// ============================================================================
60// Per-subcatchment subarea state (SoA for vectorization)
61// ============================================================================
62
63struct RunoffSoA {
64 int n_subcatch = 0;
65
66 // Per-subcatchment properties (set at init)
67 std::vector<double> area;
68 std::vector<double> width;
69 std::vector<double> slope;
70 std::vector<double> imperv_pct;
71 std::vector<double> imperv0_pct;
72
73 // Per-subarea SoA: alpha = runoff coefficient
74 std::vector<double> alpha_imperv;
75 std::vector<double> alpha_perv;
76
77 // Per-subarea SoA: depression storage (ft)
78 std::vector<double> ds_imperv;
79 std::vector<double> ds_perv;
80
81 // Per-subarea SoA: Manning's n
82 std::vector<double> n_imperv;
83 std::vector<double> n_perv;
84
85 // Per-subarea SoA: ponded depth (state, updated each step)
86 std::vector<double> depth_imperv0;
87 std::vector<double> depth_imperv1;
88 std::vector<double> depth_perv;
89
90 // Per-subarea SoA: previous-step runoff rates (ft/sec, area-averaged)
91 // Used for inter-subarea routing (legacy subcatch_getRunon)
92 std::vector<double> old_runoff_imperv0;
93 std::vector<double> old_runoff_imperv1;
94 std::vector<double> old_runoff_perv;
95
96 // Per-subcatchment: computed runoff (output)
97 std::vector<double> runoff;
98 std::vector<double> evap_loss;
99 std::vector<double> infil_loss;
100
101 // Per-subcatchment: per-subarea runoff CFS from non-LID area (Gap #23)
102 // Used by SWMMEngine to compute LID unit inflow from impervious/pervious fractions.
103 std::vector<double> imperv_runoff_cfs;
104 std::vector<double> perv_runoff_cfs;
105
106 void resize(int n);
107 void computeAlpha();
108};
109
110// ============================================================================
111// Runoff solver
112// ============================================================================
113
115public:
116 void init(SimulationContext& ctx);
121 void execute(SimulationContext& ctx, double dt, double evap_rate = 0.0,
122 double infil_factor = 1.0, double recovery_factor = 1.0,
123 int month = -1);
124
125 const RunoffSoA& soa() const { return soa_; }
126
127 // -----------------------------------------------------------------------
128 // Hot start helpers — Gap #54
129 // -----------------------------------------------------------------------
130
143 void infil_get_state(int i, int& model, double state[6]) const noexcept;
144
155 void infil_set_state(int i, int model, const double state[6]) noexcept;
156
157private:
158 RunoffSoA soa_;
159
160 // Infiltration state (one per subcatchment)
161 std::vector<InfilModel> infil_models_;
162 std::vector<HortonState> horton_states_;
163 std::vector<GreenAmptState> grnampt_states_;
164 std::vector<CurveNumState> curvenum_states_;
165
166 // Working buffers (reused each step, sized to n_subcatch)
167 std::vector<double> precip_;
168 std::vector<double> evap_rate_;
169 std::vector<double> infil_rate_;
170
173 static void updatePondedDepth(double& depth, double inflow, double alpha,
174 double dStore, double dt);
175
177 static double getRunoffRate(double depth, double dStore, double alpha);
178};
179
180} // namespace runoff
181} // namespace openswmm
182
183#endif // OPENSWMM_RUNOFF_HPP
Infiltration models — Horton, Green-Ampt, SCS Curve Number.
Structure-of-Arrays (SoA) storage for subcatchments.
Definition Runoff.hpp:114
void infil_set_state(int i, int model, const double state[6]) noexcept
Restore infiltration state for subcatchment i from a flat 6-element array.
Definition Runoff.cpp:672
void init(SimulationContext &ctx)
Definition Runoff.cpp:186
const RunoffSoA & soa() const
Definition Runoff.hpp:125
void infil_get_state(int i, int &model, double state[6]) const noexcept
Pack infiltration state for subcatchment i into a flat 6-element array.
Definition Runoff.cpp:629
void execute(SimulationContext &ctx, double dt, double evap_rate=0.0, double infil_factor=1.0, double recovery_factor=1.0, int month=-1)
Definition Runoff.cpp:263
Definition Inflow.cpp:41
Definition HotStartManager.hpp:90
constexpr double PHI
Manning's US customary constant.
Definition Runoff.hpp:57
constexpr double ODETOL
ODE solver tolerance (matching legacy)
Definition Runoff.hpp:56
constexpr double MEXP
Manning's exponent (legacy subcatch.c:55 literal — match bit-for-bit, NOT 5.0/3.0)
Definition Runoff.hpp:55
Definition NodeCoupling.cpp:16
Central, reentrant simulation context.
Definition SimulationContext.hpp:353
Definition Runoff.hpp:63
std::vector< double > depth_imperv0
Ponded depth, IMPERV0 (ft) — dStore=0.
Definition Runoff.hpp:86
std::vector< double > depth_perv
Ponded depth, PERV (ft)
Definition Runoff.hpp:88
int n_subcatch
Definition Runoff.hpp:64
std::vector< double > alpha_perv
Alpha for pervious subarea.
Definition Runoff.hpp:75
std::vector< double > imperv_runoff_cfs
Impervious subarea runoff (CFS, non-LID area)
Definition Runoff.hpp:103
void resize(int n)
Definition Runoff.cpp:62
std::vector< double > ds_perv
Depression storage for PERV.
Definition Runoff.hpp:79
std::vector< double > ds_imperv
Depression storage for IMPERV1.
Definition Runoff.hpp:78
std::vector< double > old_runoff_imperv1
Previous IMPERV1 runoff (ft/sec)
Definition Runoff.hpp:93
std::vector< double > area
Subcatchment area (ft²)
Definition Runoff.hpp:67
std::vector< double > imperv_pct
Impervious fraction (0-1)
Definition Runoff.hpp:70
std::vector< double > slope
Average slope (ft/ft)
Definition Runoff.hpp:69
std::vector< double > old_runoff_imperv0
Previous IMPERV0 runoff (ft/sec)
Definition Runoff.hpp:92
std::vector< double > runoff
Total runoff rate (cfs)
Definition Runoff.hpp:97
std::vector< double > perv_runoff_cfs
Pervious subarea runoff (CFS, non-LID area)
Definition Runoff.hpp:104
std::vector< double > width
Subcatchment width (ft)
Definition Runoff.hpp:68
std::vector< double > depth_imperv1
Ponded depth, IMPERV1 (ft) — dStore>0.
Definition Runoff.hpp:87
std::vector< double > imperv0_pct
Fraction of imperv with zero dStore (0-1)
Definition Runoff.hpp:71
std::vector< double > infil_loss
Infiltration loss (ft3)
Definition Runoff.hpp:99
void computeAlpha()
Definition Runoff.cpp:94
std::vector< double > evap_loss
Evaporation loss (ft3)
Definition Runoff.hpp:98
std::vector< double > old_runoff_perv
Previous PERV runoff (ft/sec)
Definition Runoff.hpp:94
std::vector< double > n_imperv
Manning's n, impervious.
Definition Runoff.hpp:82
std::vector< double > n_perv
Manning's n, pervious.
Definition Runoff.hpp:83
std::vector< double > alpha_imperv
Alpha for impervious subareas.
Definition Runoff.hpp:74