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
102 void allocate(int n) {
103 max_periods = n;
104 past_rain.assign(static_cast<std::size_t>(n), 0.0);
105 past_month.assign(static_cast<std::size_t>(n), 0);
106 period = 0;
107 has_past_rain = 0;
108 ia_used = 0.0;
109 swe = 0.0;
110 dry_seconds = static_cast<long>(n) * 300 + 1; // start dry
111 }
112};
113
121double getRecoveryRate(const ExpDecayParams& dp, double T_celsius);
122
142double updateIA_exp(const UnitHydParams& uh, UHResponseData& rd, const ExpDecayParams& dp,
143 int month, int response, double rainDepth, double dt_sec,
144 const SimulationContext& ctx);
145
147 int count = 0;
148 std::vector<int> node_idx;
149 std::vector<int> uh_idx;
150 std::vector<int> gage_idx;
151 std::vector<double> area;
152
154 std::vector<UHResponseData> uh_data;
155
156 std::vector<int> rain_interval;
157
158 // Strict-grid driver state (legacy createRdiiFile embedded at runtime).
159 // Chunk starts advance in rain_interval steps from simulation start;
160 // rates are recorded per chunk while the runoff substep containing the
161 // chunk's start is current (the substep never crosses a gage entry
162 // boundary, so the current gage rate IS the rate at the chunk start).
163 std::vector<double> gage_elapsed;
164 std::vector<double> rate_recorded_until;
165 std::vector<std::vector<double>> pending_rates;
166
172 void resize(int n);
173};
174
182public:
195 void init(SimulationContext& ctx);
196
203 int addUnitHydParams(const std::string& name, const UnitHydParams& params);
204
208 int findUnitHyd(const std::string& name) const;
209
211 using UhNameMap = std::unordered_map<std::string, int, CiHash, CiEqual>;
212
217 const UhNameMap& uhNameIndex() const { return uh_name_to_idx_; }
218
232 void advance(SimulationContext& ctx, double new_elapsed_sec);
233
242 void applyRdiiInflows(SimulationContext& ctx, double elapsed_sec) const;
243
244 std::vector<UnitHydParams> uh_params;
245
249 std::vector<std::array<ExpDecayParams, 3>> decay_params;
250
256 const std::vector<double>& nodeFlows() const { return node_rdii_flow_; }
257
263 std::vector<int> rdiiNodeList() const;
264
271 static int getRainInterval(const UnitHydParams& uh, double wet_step);
272
279 static int getMaxPeriods(const UnitHydParams& uh, int response, int rainInterval);
280
281private:
282 RDIIGroupSoA groups_;
283 UhNameMap uh_name_to_idx_;
284 std::vector<double> node_rdii_flow_;
285
286 // Strict grid (legacy createRdiiFile records, kept in memory).
287 double grid_step_ = 0.0;
288 double next_tick_ = 0.0;
289 std::vector<int> grid_node_;
290 std::vector<float> grid_flows_;
291
303 void emitTick(SimulationContext& ctx, double T);
304
312 double uhOrdinate(const UnitHydParams& uh, int month, int response, double t) const;
313
320 void validateExpDecay(SimulationContext& ctx) const;
321};
322
323} // namespace rdii
324} // namespace openswmm
325
326#endif // OPENSWMM_RDII_HPP
Case-insensitive string helpers matching legacy SWMM name semantics.
Mutable strict-grid UH convolution, rainfall queues and node-flow history. Configure validated UH/dec...
Definition RDII.hpp:181
std::vector< int > rdiiNodeList() const
Copy sorted unique node indices currently stored in group assignment vectors.
Definition RDII.cpp:748
std::vector< std::array< ExpDecayParams, 3 > > decay_params
Definition RDII.hpp:249
static int getMaxPeriods(const UnitHydParams &uh, int response, int rainInterval)
Size one response rainfall ring to its longest monthly base duration.
Definition RDII.cpp:123
const std::vector< double > & nodeFlows() const
Borrow the latest emitted strict-grid node-flow row.
Definition RDII.hpp:256
void advance(SimulationContext &ctx, double new_elapsed_sec)
Record current rain chunks and emit every reached fixed wet-step grid tick.
Definition RDII.cpp:561
void init(SimulationContext &ctx)
Resolve authored UH/gage/decay assignments and reset the strict-grid driver.
Definition RDII.cpp:204
std::vector< UnitHydParams > uh_params
Definition RDII.hpp:244
void applyRdiiInflows(SimulationContext &ctx, double elapsed_sec) const
Add an emitted zero-order-held RDII row into the decomposed node inflow array.
Definition RDII.cpp:723
int findUnitHyd(const std::string &name) const
Find a registered UH parameter table by case-insensitive name.
Definition RDII.cpp:96
static int getRainInterval(const UnitHydParams &uh, double wet_step)
Select whole-second UH rainfall processing interval.
Definition RDII.cpp:103
const UhNameMap & uhNameIndex() const
Borrow the case-insensitive UH registry index.
Definition RDII.hpp:217
int addUnitHydParams(const std::string &name, const UnitHydParams &params)
Copy a named monthly UH parameter table into the registry.
Definition RDII.cpp:84
std::unordered_map< std::string, int, CiHash, CiEqual > UhNameMap
UH name → index map type; case-insensitive (legacy hash.c parity).
Definition RDII.hpp:211
Definition RDII.cpp:37
double updateIA_exp(const UnitHydParams &uh, UHResponseData &rd, const ExpDecayParams &dp, int month, int response, double rainDepth, double dt_sec, const SimulationContext &ctx)
Update exponential abstraction and optional degree-day SWE for one rain chunk.
Definition RDII.cpp:459
double getRecoveryRate(const ExpDecayParams &dp, double T_celsius)
Compute exponential-IA additive recovery at one air temperature.
Definition RDII.cpp:429
Definition ExchangeLaw.hpp:60
Central, reentrant simulation context.
Definition SimulationContext.hpp:390
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:146
void resize(int n)
Reset group metadata/queues and resize response objects.
Definition RDII.cpp:39
std::vector< int > gage_idx
Rain gage index per UH group (legacy: UnitHyd[j].rainGage)
Definition RDII.hpp:150
std::vector< int > uh_idx
Unit hydrograph parameter index.
Definition RDII.hpp:149
std::vector< double > rate_recorded_until
Chunk starts with recorded rates (sec)
Definition RDII.hpp:164
std::vector< double > gage_elapsed
Chunk cursor (sec, legacy UHGroup.gageDate)
Definition RDII.hpp:163
std::vector< double > area
Contributing area, project land-area units (acres/ha)
Definition RDII.hpp:151
std::vector< int > node_idx
Target node index.
Definition RDII.hpp:148
std::vector< UHResponseData > uh_data
Per-response data: [group * 3 + response].
Definition RDII.hpp:154
std::vector< std::vector< double > > pending_rates
FIFO of recorded chunk rates.
Definition RDII.hpp:165
std::vector< int > rain_interval
Rain processing interval (sec) per group.
Definition RDII.hpp:156
int count
Definition RDII.hpp:147
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)
Reset one response's circular rainfall/month buffers and abstraction/SWE state.
Definition RDII.hpp:102
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]
Linear recovery, project rain-depth units/day.
Definition RDII.hpp:57
double iaMax[12][3]
Maximum abstraction, project rain-depth units.
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 used abstraction, project rain-depth units.
Definition RDII.hpp:58