OpenSWMM Engine  6.0.0-alpha.1
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.1)
Loading...
Searching...
No Matches
DynamicWave.hpp
Go to the documentation of this file.
1
28#ifndef OPENSWMM_DYNAMIC_WAVE_HPP
29#define OPENSWMM_DYNAMIC_WAVE_HPP
30
31#include "XSectBatch.hpp"
32#include "../core/Constants.hpp"
33#include "../core/SimulationOptions.hpp"
34#include "../data/NodeData.hpp"
35#include "../data/LinkData.hpp"
36#include <cstdint>
37#include <functional>
38#include <vector>
39
40namespace openswmm {
41
42struct SimulationContext;
43
44namespace dynwave {
45
46// ============================================================================
47// Constants — imported from global Constants.hpp
48// ============================================================================
49
60
61// ============================================================================
62// Dynamic Preissmann Slot (DPS) configuration and per-link state
63// Sharior, Hodges & Vasconcelos (2023), J. Hydraul. Eng. 149(11)
64// ============================================================================
65
67struct DPSConfig {
68 double c_pT = 25.0;
69 double alpha = 3.0;
70 double r = 0.5;
71 double c_pT_sq = 625.0;
72};
73
86 std::vector<double> As;
87 std::vector<double> hs;
88 std::vector<double> hs_prev_iter;
89 std::vector<double> P;
90 std::vector<double> P_hat;
91 std::vector<double> P_hat_0;
92 std::vector<double> T_s_target;
93 std::vector<double> t_s;
94 std::vector<uint8_t> surcharged;
95
96 void resize(std::size_t n) {
97 As.assign(n, 0.0);
98 hs.assign(n, 0.0);
99 hs_prev_iter.assign(n, 0.0);
100 P.assign(n, 1.0);
101 P_hat.assign(n, 1.0);
102 P_hat_0.assign(n, 1.0);
103 T_s_target.assign(n, 0.0);
104 t_s.assign(n, 0.0);
105 surcharged.assign(n, 0);
106 }
107};
108
109// ============================================================================
110// Per-node extended state for DW iterations
111// ============================================================================
112
116 std::vector<double> new_surf_area;
117 std::vector<double> old_surf_area;
118 std::vector<double> sumdqdh;
119 std::vector<double> dYdT;
120 std::vector<uint8_t> converged;
121 std::vector<uint8_t> is_surcharged;
122
123 void resize(std::size_t n) {
124 new_surf_area.assign(n, 0.0);
125 old_surf_area.assign(n, 0.0);
126 sumdqdh.assign(n, 0.0);
127 dYdT.assign(n, 0.0);
128 converged.assign(n, 0);
129 is_surcharged.assign(n, 0);
130 }
131};
132
133// ============================================================================
134// DW solver — batch-oriented
135// ============================================================================
136
141enum class SurchargeMethod : int {
142 EXTRAN = 0,
143 SLOT = 1,
144 DYNAMIC_SLOT = 2
145};
146
154enum class MomentumCategory : uint8_t {
155 SKIP_DRY = 0,
156 MANNING_OPEN = 1,
159 FORCE_MAIN_HW = 4,
160 FORCE_MAIN_DW = 5,
161 N_CATEGORIES = 6
162};
163
172class DWSolver {
173public:
174 void init(int n_nodes, int n_links, const XSectGroups& groups,
175 const SimulationContext& ctx);
176
186 void setNumThreads(int n);
187
190 using NonConduitFlowFunc = std::function<void(SimulationContext&, double, int)>;
191
201 int execute(SimulationContext& ctx, double dt,
202 NonConduitFlowFunc non_conduit_fn = nullptr);
203
210 double fixed_step, double courant_factor);
211
212 double head_tol = DEFAULT_HEAD_TOL;
213 int max_trials = DEFAULT_MAX_TRIALS;
214 double omega = OMEGA;
217 bool anderson_accel = false;
218
221 double evap_rate = 0.0;
222
223private:
224 int n_nodes_ = 0;
225 int n_links_ = 0;
226 int n_conduits_ = 0;
227 int num_threads_ = 1;
228 const XSectGroups* groups_ = nullptr;
229
230 // Pre-built conduit index list for skipping non-conduits in inner loops
231 std::vector<int> conduit_idx_;
232
233 // Pre-computed per-link invariants (populated once at first execute, reused)
234 // Using uint8_t instead of bool to allow .data() pointer access for SIMD/restrict
235 std::vector<uint8_t> is_open_;
236 std::vector<uint8_t> is_force_main_;
237 std::vector<uint8_t> has_losses_;
238 std::vector<double> barrels_d_;
239 std::vector<double> cached_length_;
240 std::vector<double> inv_length_;
241
242 // ------------------------------------------------------------------------
243 // Phase A — Conduit-dense "hot tile" of timestep-invariant data.
244 //
245 // Sized n_conduits_, accessed by ci (0..n_conduits_-1) for dense linear
246 // memory access pattern. This replaces sparse `links.X[uj]` /
247 // `nodes.X[un]` reads inside the Picard inner loops, where uj/un are
248 // sparse-indexed via conduit_idx_ + links.node1/node2. Each ci-indexed
249 // read maps to one contiguous cache line that holds 8+ conduits' data,
250 // versus the sparse pattern where each uj read can miss into a new line.
251 //
252 // All fields below are populated once in init() (or refreshConduitTile
253 // when a hot-start changes invariants) and remain constant for the rest
254 // of the simulation. None of these change per Picard iter, per
255 // timestep, or per outfall update.
256 // ------------------------------------------------------------------------
257 std::vector<int> tile_uj_;
258 std::vector<int> tile_n1_;
259 std::vector<int> tile_n2_;
260 std::vector<double> tile_inv1_elev_;
261 std::vector<double> tile_inv2_elev_;
262 std::vector<double> tile_z1_off_;
263 std::vector<double> tile_z2_off_;
264 std::vector<double> tile_y_full_;
265 std::vector<double> tile_a_full_;
266 std::vector<double> tile_r_full_;
267 std::vector<double> tile_w_max_;
268 std::vector<double> tile_length_;
269 std::vector<double> tile_inv_length_;
270 std::vector<double> tile_links_length_;
271 std::vector<double> tile_beta_;
272 std::vector<double> tile_q_max_;
273 std::vector<double> tile_rough_factor_;
274 std::vector<double> tile_barrels_d_;
275 std::vector<uint8_t> tile_is_open_;
276 std::vector<uint8_t> tile_is_force_main_;
277 std::vector<uint8_t> tile_is_closed_;
278 std::vector<uint8_t> tile_has_losses_;
279 std::vector<int> tile_xsect_batch_shape_;
280 std::vector<XsectShape> tile_shape_;
287 std::vector<uint8_t> tile_has_offset_;
292 std::vector<int> tile_uj_to_ci_;
295 std::vector<int> tile_culvert_code_;
296 std::vector<double> tile_slope_;
297 std::vector<double> tile_q_limit_;
298 std::vector<double> tile_loss_inlet_;
299 std::vector<double> tile_loss_outlet_;
300 std::vector<uint8_t> tile_has_flap_gate_;
301 std::vector<int8_t> tile_direction_;
302
306 void refreshConduitTile(const SimulationContext& ctx);
307
308 // Per-timestep constants
309 double dt_gravity_ = 0.0;
310
320 double min_surf_area_ = constants::MIN_SURFAREA;
321
322 // Pre-allocated width-capping buffers (avoids thread_local per-call allocation)
323 std::vector<double> wcap_d1_, wcap_d2_, wcap_dm_;
324
325 // Variable timestep state (matching legacy VariableStep in dynwave.c)
326 mutable double variable_step_ = 0.0;
327
328 // Per-node working state (SoA)
329 DWNodeArrays xnode_;
330
331 // Per-link pre-computed geometry (batch-filled by XSectGroups each iteration)
332 std::vector<double> area1_;
333 std::vector<double> area2_;
334 std::vector<double> area_mid_;
335 std::vector<double> hrad_mid_;
336 std::vector<double> width_mid_;
337 std::vector<double> depth1_;
338 std::vector<double> depth2_;
339 std::vector<double> depth_mid_;
340
341 // Per-link momentum working arrays
342 std::vector<double> velocity_;
343 std::vector<double> froude_;
344 std::vector<double> sigma_;
345 std::vector<double> dqdh_;
346 std::vector<double> new_flow_;
347
348 // Per-link area from previous iteration (for unsteady term)
349 std::vector<double> area_old_;
350
351 // Per-link bypass flag (true when both end nodes converged; skip momentum solve)
352 // uint8_t instead of bool: avoids std::vector<bool> bit-packing overhead
353 std::vector<uint8_t> bypassed_;
354
355 // Per-link surface area contributions to upstream/downstream nodes
356 // (matching legacy Link[].surfArea1/surfArea2 from dwflow.c findSurfArea)
357 std::vector<double> surf_area1_;
358 std::vector<double> surf_area2_;
359
360 // Per-link upstream geometry (for proper weighted hyd. radius)
361 std::vector<double> hrad1_;
362 std::vector<double> width1_;
363 std::vector<double> width2_;
364
365 // Per-link head values (persisted from computeLinkGeometry for solveMomentumBatch,
366 // may be modified by flow classification for UP_CRITICAL/DN_CRITICAL cases)
367 std::vector<double> h1_;
368 std::vector<double> h2_;
369 std::vector<double> fasnh_;
370
371 // Anderson acceleration state (per-node, depth-2 mixing)
372 std::vector<double> aa_y_prev_;
373 std::vector<double> aa_g_prev_;
374 std::vector<double> aa_r_prev_;
375 std::vector<uint8_t> aa_skip_;
376
377 // Per-conduit momentum category (rebuilt each Picard iteration).
378 // solveMomentumBatch dispatches on category_[uj] inline — no auxiliary
379 // per-category index list is needed.
380 std::vector<MomentumCategory> category_;
381
382 // Internal methods
383 void initNodeStates(SimulationContext& ctx);
384 void findBypassedLinks(const SimulationContext& ctx);
385 void computeLinkGeometry(SimulationContext& ctx);
386 void solveMomentumBatch(SimulationContext& ctx, double dt, int step);
387 void classifyMomentumCategories(SimulationContext& ctx);
388
393 void processDryLink(SimulationContext& ctx, double dt, std::size_t uj);
394 void processManningLink(SimulationContext& ctx, double dt, int step,
395 std::size_t uj, MomentumCategory cat);
396 void processForceMainLink(SimulationContext& ctx, double dt, int step,
397 std::size_t uj, MomentumCategory cat);
398 void applyFlowLimits(SimulationContext& ctx, double dt, int step,
399 std::size_t uj, double& q, double qLast,
400 double barrels_d, bool isFull);
401 void updateNodeFlows(SimulationContext& ctx, bool conduits_only = false);
402 void computeAASkipFlags(const SimulationContext& ctx);
403 bool updateNodeDepths(SimulationContext& ctx, double dt, int step);
404 void setNodeDepth(SimulationContext& ctx, int node_idx, double dt, int step);
405 double getLinkStep(const SimulationContext& ctx, int link_idx) const;
406
407public:
409 uint8_t& nodeSurchargedFlag(int idx) { return xnode_.is_surcharged[static_cast<std::size_t>(idx)]; }
410
412 double* nodeNewSurfAreaDataMut() { return xnode_.new_surf_area.data(); }
413
415 double& nodeSumDqdh(int n) { return xnode_.sumdqdh[static_cast<std::size_t>(n)]; }
416
418 const std::vector<uint8_t>& aaSkipFlags() const { return aa_skip_; }
419
421 const DPSLinkArrays& dpsState() const { return dps_; }
423 const DPSConfig& dpsConfig() const { return dps_config_; }
425 DPSLinkArrays& dpsStateMut() { return dps_; }
426private:
427
428 // Preissmann slot helpers (matching legacy dwflow.c)
429 double getSlotWidth(double y, double y_full, double w_max, XsectShape shape) const;
430 double getSlotArea(double y, double y_full, double a_full, double slot_width) const;
431 double getSlotHydRad(double y, double y_full, double r_full) const;
432 double getCrownCutoff() const;
433
434 // Dynamic Preissmann Slot (DPS) state and methods
435 DPSConfig dps_config_;
436 DPSLinkArrays dps_;
437 double sim_time_ = 0.0;
438
440 void applyDPSGeometry(SimulationContext& ctx);
441
443 void updateDPSState(SimulationContext& ctx, double dt);
444
446 void spatialSmoothP(const SimulationContext& ctx);
447};
448
449} // namespace dynwave
450} // namespace openswmm
451
452#endif // OPENSWMM_DYNAMIC_WAVE_HPP
Cross-section geometry — unified batch + per-element API.
Shape-grouped cross-section manager for batch computation.
Definition XSectBatch.hpp:208
Dynamic wave solver — operates on entire link/node system.
Definition DynamicWave.hpp:172
double evap_rate
Definition DynamicWave.hpp:221
void setNumThreads(int n)
Set the number of OpenMP threads for parallel loops.
Definition DynamicWave.cpp:610
SurchargeMethod surcharge_method
Definition DynamicWave.hpp:215
void init(int n_nodes, int n_links, const XSectGroups &groups, const SimulationContext &ctx)
Definition DynamicWave.cpp:353
DPSLinkArrays & dpsStateMut()
Mutable access to DPS state for tests that need to seed slot conditions.
Definition DynamicWave.hpp:425
const DPSLinkArrays & dpsState() const
Read-only access to DPS per-conduit state arrays (for tests/diagnostics).
Definition DynamicWave.hpp:421
double head_tol
Definition DynamicWave.hpp:212
NodeContinuity node_continuity
Definition DynamicWave.hpp:216
const DPSConfig & dpsConfig() const
Read-only access to DPS configuration (for tests/diagnostics).
Definition DynamicWave.hpp:423
bool anderson_accel
Enable Anderson acceleration.
Definition DynamicWave.hpp:217
double omega
Definition DynamicWave.hpp:214
int execute(SimulationContext &ctx, double dt, NonConduitFlowFunc non_conduit_fn=nullptr)
Execute one DW routing timestep.
Definition DynamicWave.cpp:628
double & nodeSumDqdh(int n)
Mutable reference to the per-node sumdqdh accumulator at index n.
Definition DynamicWave.hpp:415
uint8_t & nodeSurchargedFlag(int idx)
Direct write access to the per-node is_surcharged flag (for tests/non-conduit scatter).
Definition DynamicWave.hpp:409
double * nodeNewSurfAreaDataMut()
Mutable pointer to the per-node new_surf_area array (for HydStructures scatter).
Definition DynamicWave.hpp:412
int max_trials
Definition DynamicWave.hpp:213
double getRoutingStep(SimulationContext &ctx, double fixed_step, double courant_factor)
Compute CFL-based variable timestep.
Definition DynamicWave.cpp:2173
std::function< void(SimulationContext &, double, int)> NonConduitFlowFunc
Definition DynamicWave.hpp:190
const std::vector< uint8_t > & aaSkipFlags() const
Access per-node AA skip flags (read-only, for testing/diagnostics).
Definition DynamicWave.hpp:418
constexpr double MIN_SURFAREA
Definition Constants.hpp:79
constexpr double DEFAULT_HEAD_TOL
Definition Constants.hpp:105
constexpr double FUDGE
Definition Constants.hpp:75
constexpr double EXTRAN_CROWN_CUTOFF
Definition Constants.hpp:121
constexpr double MAX_VELOCITY
Definition Constants.hpp:113
constexpr double SLOT_WIDTH_FACTOR
Preissmann slot width factor (slot_width = y_full * this factor).
Definition Constants.hpp:127
constexpr double OMEGA
Definition Constants.hpp:101
constexpr int DEFAULT_MAX_TRIALS
Definition Constants.hpp:109
constexpr double MIN_TIMESTEP
Definition Constants.hpp:117
constexpr double SLOT_CROWN_CUTOFF
Preissmann slot crown cutoff fraction.
Definition Constants.hpp:124
SurchargeMethod
Surcharge method: EXTRAN (classic) or SLOT (Preissmann).
Definition DynamicWave.hpp:141
@ SLOT
Preissmann slot — fictitious narrow slot above crown.
@ DYNAMIC_SLOT
Dynamic slot — slot width varies with flow conditions (experimental) Sharior, S., Hodges,...
@ EXTRAN
Classic EXTRAN approach — dQ/dH for surcharged nodes.
MomentumCategory
Momentum category for branch-free per-category kernel dispatch.
Definition DynamicWave.hpp:154
@ MANNING_OPEN
Standard Manning, open channel (Froude-based sigma)
@ MANNING_CLOSED_FULL
Manning, closed conduit, surcharged (fr=0, sig=0)
@ FORCE_MAIN_HW
Force main, Hazen-Williams friction.
@ FORCE_MAIN_DW
Force main, Darcy-Weisbach friction.
@ MANNING_CLOSED_FS
Manning, closed conduit, free surface.
@ SKIP_DRY
DRY/UP_DRY/DN_DRY, aMid<=FUDGE, or is_closed.
Definition NodeCoupling.cpp:15
NodeContinuity
Node continuity formulation for depth update.
Definition SimulationOptions.hpp:99
@ EXPLICIT
Classic explicit two-branch (default)
XsectShape
Conduit cross-section shape code.
Definition LinkData.hpp:54
double * y
Definition odesolve.c:28
Central, reentrant simulation context.
Definition SimulationContext.hpp:274
DPS configuration parameters (derived from SimulationOptions at init).
Definition DynamicWave.hpp:67
double alpha
Surcharge shock parameter (>= 2)
Definition DynamicWave.hpp:69
double c_pT_sq
c_pT^2 (pre-computed)
Definition DynamicWave.hpp:71
double c_pT
Target pressure celerity (ft/s, internal units)
Definition DynamicWave.hpp:68
double r
Decay time scale for P → 1 (seconds)
Definition DynamicWave.hpp:70
Definition DynamicWave.hpp:115
std::vector< double > dYdT
Definition DynamicWave.hpp:119
std::vector< double > old_surf_area
Surface area from last non-surcharged state.
Definition DynamicWave.hpp:117
std::vector< double > new_surf_area
Definition DynamicWave.hpp:116
void resize(std::size_t n)
Definition DynamicWave.hpp:123
std::vector< uint8_t > is_surcharged
TRUE when node depth > crown elevation.
Definition DynamicWave.hpp:121
std::vector< double > sumdqdh
Definition DynamicWave.hpp:118
std::vector< uint8_t > converged
Definition DynamicWave.hpp:120