OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
RDII.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
35
36#ifndef OPENSWMM_RDII_HPP
37#define OPENSWMM_RDII_HPP
38
39#include <array>
40#include <vector>
41#include <string>
42#include <unordered_map>
43
45
46namespace openswmm {
47
49
50namespace rdii {
51
53 double r[12][3] = {};
54 double tPeak[12][3] = {};
55 double tBase[12][3] = {};
56 double iaMax[12][3] = {};
57 double iaRecov[12][3] = {};
58 double iaInit[12][3] = {};
59};
60
68 bool active = false;
69 double k_dep = 0.0;
70 double k_0 = 0.0;
71 double k_T = 0.0;
72 double T_ref = 10.0;
73 double theta_rec = 0.0;
74 double T_freeze = 0.0;
75
76 // Optional degree-day snow model. When `snow_on`, precipitation at
77 // T <= snow_T accumulates as SWE with no liquid input; at T > snow_T,
78 // melt = min(SWE, snow_ddf*(T - snow_T)*dt_days) is added to rainfall
79 // (rain-on-snow) before the IA depletion step.
80 bool snow_on = false;
81 double snow_T = 1.0;
82 double snow_ddf = 0.0;
83};
84
88 std::vector<double> past_rain;
89 std::vector<int> past_month;
90 int period = 0;
91 int max_periods = 0;
92 int has_past_rain = 0;
93 double ia_used = 0.0;
94 double swe = 0.0;
95 long dry_seconds = 0;
96
97 void allocate(int n) {
98 max_periods = n;
99 past_rain.assign(static_cast<std::size_t>(n), 0.0);
100 past_month.assign(static_cast<std::size_t>(n), 0);
101 period = 0;
102 has_past_rain = 0;
103 ia_used = 0.0;
104 swe = 0.0;
105 dry_seconds = static_cast<long>(n) * 300 + 1; // start dry
106 }
107};
108
112double getRecoveryRate(const ExpDecayParams& dp, double T_celsius);
113
121double updateIA_exp(const UnitHydParams& uh, UHResponseData& rd,
122 const ExpDecayParams& dp, int month, int response,
123 double rainDepth, double dt_sec,
124 const SimulationContext& ctx);
125
127 int count = 0;
128 std::vector<int> node_idx;
129 std::vector<int> uh_idx;
130 std::vector<int> gage_idx;
131 std::vector<double> area;
132
134 std::vector<UHResponseData> uh_data;
135
136 std::vector<int> rain_interval;
137
138 // Strict-grid driver state (legacy createRdiiFile embedded at runtime).
139 // Chunk starts advance in rain_interval steps from simulation start;
140 // rates are recorded per chunk while the runoff substep containing the
141 // chunk's start is current (the substep never crosses a gage entry
142 // boundary, so the current gage rate IS the rate at the chunk start).
143 std::vector<double> gage_elapsed;
144 std::vector<double> rate_recorded_until;
145 std::vector<std::vector<double>> pending_rates;
146
147 void resize(int n);
148};
149
151public:
152 void init(SimulationContext& ctx);
153
161 int addUnitHydParams(const std::string& name, const UnitHydParams& params);
162
167 int findUnitHyd(const std::string& name) const;
168
170 using UhNameMap = std::unordered_map<std::string, int, CiHash, CiEqual>;
171
173 const UhNameMap& uhNameIndex() const { return uh_name_to_idx_; }
174
198 void advance(SimulationContext& ctx, double new_elapsed_sec);
199
209 void applyRdiiInflows(SimulationContext& ctx, double elapsed_sec) const;
210
211 std::vector<UnitHydParams> uh_params;
212
216 std::vector<std::array<ExpDecayParams, 3>> decay_params;
217
220 const std::vector<double>& nodeFlows() const { return node_rdii_flow_; }
221
224 std::vector<int> rdiiNodeList() const;
225
227 static int getRainInterval(const UnitHydParams& uh, double wet_step);
228
230 static int getMaxPeriods(const UnitHydParams& uh, int response, int rainInterval);
231
232private:
233 RDIIGroupSoA groups_;
234 UhNameMap uh_name_to_idx_;
235 std::vector<double> node_rdii_flow_;
236
237 // Strict grid (legacy createRdiiFile records, kept in memory).
238 double grid_step_ = 0.0;
239 double next_tick_ = 0.0;
240 std::vector<int> grid_node_;
241 std::vector<float> grid_flows_;
242
245 void emitTick(SimulationContext& ctx, double T);
246
248 double uhOrdinate(const UnitHydParams& uh, int month, int response, double t) const;
249
251 void validateExpDecay(SimulationContext& ctx) const;
252};
253
254} // namespace rdii
255} // namespace openswmm
256
257#endif // OPENSWMM_RDII_HPP
Case-insensitive string helpers matching legacy SWMM name semantics.
Definition RDII.hpp:150
std::vector< int > rdiiNodeList() const
Definition RDII.cpp:682
std::vector< std::array< ExpDecayParams, 3 > > decay_params
Definition RDII.hpp:216
static int getMaxPeriods(const UnitHydParams &uh, int response, int rainInterval)
Compute max past periods for a UH response given a rain interval.
Definition RDII.cpp:116
const std::vector< double > & nodeFlows() const
Definition RDII.hpp:220
void advance(SimulationContext &ctx, double new_elapsed_sec)
Advance the strict-grid RDII computation to a runoff clock time.
Definition RDII.cpp:514
void init(SimulationContext &ctx)
Definition RDII.cpp:190
std::vector< UnitHydParams > uh_params
Definition RDII.hpp:211
void applyRdiiInflows(SimulationContext &ctx, double elapsed_sec) const
Apply RDII inflows at a routing time to node lateral flows.
Definition RDII.cpp:660
int findUnitHyd(const std::string &name) const
Look up unit hydrograph index by name.
Definition RDII.cpp:93
static int getRainInterval(const UnitHydParams &uh, double wet_step)
Compute rain processing interval for a UH (minimum limb duration, capped by wet_step).
Definition RDII.cpp:99
const UhNameMap & uhNameIndex() const
Read-only access to the UH name → index map (for validation).
Definition RDII.hpp:173
int addUnitHydParams(const std::string &name, const UnitHydParams &params)
Register a unit hydrograph parameter set by name.
Definition RDII.cpp:80
std::unordered_map< std::string, int, CiHash, CiEqual > UhNameMap
UH name → index map type; case-insensitive (legacy hash.c parity).
Definition RDII.hpp:170
Definition RDII.cpp:36
double updateIA_exp(const UnitHydParams &uh, UHResponseData &rd, const ExpDecayParams &dp, int month, int response, double rainDepth, double dt_sec, const SimulationContext &ctx)
Definition RDII.cpp:412
double getRecoveryRate(const ExpDecayParams &dp, double T_celsius)
Definition RDII.cpp:385
Definition NodeCoupling.cpp:16
Central, reentrant simulation context.
Definition SimulationContext.hpp:353
Definition RDII.hpp:67
bool snow_on
Degree-day snow model enabled.
Definition RDII.hpp:80
double k_dep
Depletion rate (1/project-depth) — temperature-independent.
Definition RDII.hpp:69
double T_freeze
Recovery suppressed below this temperature (deg C)
Definition RDII.hpp:74
double snow_ddf
Degree-day melt factor (project-depth/degC/day)
Definition RDII.hpp:82
double k_T
Thermal recovery rate at T_ref (1/hr)
Definition RDII.hpp:71
double k_0
Base recovery rate (1/hr)
Definition RDII.hpp:70
double snow_T
Rain/snow threshold & melt base (deg C)
Definition RDII.hpp:81
double T_ref
Reference temperature (deg C)
Definition RDII.hpp:72
bool active
Definition RDII.hpp:68
double theta_rec
Temperature sensitivity (1/deg C)
Definition RDII.hpp:73
Definition RDII.hpp:126
void resize(int n)
Definition RDII.cpp:38
std::vector< int > gage_idx
Rain gage index per UH group (legacy: UnitHyd[j].rainGage)
Definition RDII.hpp:130
std::vector< int > uh_idx
Unit hydrograph parameter index.
Definition RDII.hpp:129
std::vector< double > rate_recorded_until
Chunk starts with recorded rates (sec)
Definition RDII.hpp:144
std::vector< double > gage_elapsed
Chunk cursor (sec, legacy UHGroup.gageDate)
Definition RDII.hpp:143
std::vector< double > area
Contributing area (acres, project units)
Definition RDII.hpp:131
std::vector< int > node_idx
Target node index.
Definition RDII.hpp:128
std::vector< UHResponseData > uh_data
Per-response data: [group * 3 + response].
Definition RDII.hpp:134
std::vector< std::vector< double > > pending_rates
FIFO of recorded chunk rates.
Definition RDII.hpp:145
std::vector< int > rain_interval
Rain processing interval (sec) per group.
Definition RDII.hpp:136
int count
Definition RDII.hpp:127
Definition RDII.hpp:87
int has_past_rain
true if any non-zero past rain
Definition RDII.hpp:92
long dry_seconds
seconds since last non-zero rainfall
Definition RDII.hpp:95
std::vector< int > past_month
month for each past rainfall entry
Definition RDII.hpp:89
int period
current buffer write position
Definition RDII.hpp:90
int max_periods
buffer capacity
Definition RDII.hpp:91
std::vector< double > past_rain
circular buffer of past rainfall depths
Definition RDII.hpp:88
void allocate(int n)
Definition RDII.hpp:97
double ia_used
initial abstraction used so far
Definition RDII.hpp:93
double swe
snow water equivalent (project depth; degree-day snow model)
Definition RDII.hpp:94
Definition RDII.hpp:52
double iaRecov[12][3]
IA recovery rate (linear model)
Definition RDII.hpp:57
double iaMax[12][3]
Initial abstraction max depth.
Definition RDII.hpp:56
double tPeak[12][3]
Time to peak (sec) per month × response.
Definition RDII.hpp:54
double tBase[12][3]
Base time (sec) per month × response.
Definition RDII.hpp:55
double r[12][3]
Rainfall fraction per month × response.
Definition RDII.hpp:53
double iaInit[12][3]
Initial IA used.
Definition RDII.hpp:58