OpenSWMM Engine  6.0.0-alpha.1
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.1)
Loading...
Searching...
No Matches
XSectBatch.hpp
Go to the documentation of this file.
1
34#ifndef OPENSWMM_XSECT_BATCH_HPP
35#define OPENSWMM_XSECT_BATCH_HPP
36
37#ifndef OPENSWMM_RESTRICT
38# if defined(_MSC_VER)
39# define OPENSWMM_RESTRICT __restrict
40# else
41# define OPENSWMM_RESTRICT __restrict__
42# endif
43#endif
44
45#include <vector>
46#include <cstddef>
47#include <cstdint>
48#include <cmath>
49
50namespace openswmm {
51
52// Forward declaration
53struct SimulationContext;
54
55// ============================================================================
56// Cross-section shape codes (matches legacy enums.h XsectType)
57// ============================================================================
58
59enum class XSectShape : int {
60 DUMMY = 0,
61 CIRCULAR = 1,
63 RECT_CLOSED = 3,
64 RECT_OPEN = 4,
65 TRAPEZOIDAL = 5,
66 TRIANGULAR = 6,
67 PARABOLIC = 7,
68 POWERFUNC = 8,
69 RECT_TRIANG = 9,
70 RECT_ROUND = 10,
71 MOD_BASKET = 11,
72 HORIZ_ELLIPSE = 12,
73 VERT_ELLIPSE = 13,
74 ARCH = 14,
75 EGGSHAPED = 15,
76 HORSESHOE = 16,
77 GOTHIC = 17,
78 CATENARY = 18,
79 SEMIELLIPTICAL = 19,
80 BASKETHANDLE = 20,
81 SEMICIRCULAR = 21,
82 IRREGULAR = 22,
83 CUSTOM = 23,
84 FORCE_MAIN = 24,
85 STREET_XSECT = 25
86};
87
88// ============================================================================
89// Cross-section parameter struct (mirrors legacy TXsect)
90// ============================================================================
91
93 int type = 0;
94 int culvert_code = 0;
95 int transect = -1;
96
97 double y_full = 0.0;
98 double w_max = 0.0;
99 double yw_max = 0.0;
100 double a_full = 0.0;
101 double r_full = 0.0;
102 double s_full = 0.0;
103 double s_max = 0.0;
104
105 double y_bot = 0.0;
106 double a_bot = 0.0;
107 double s_bot = 0.0;
108 double r_bot = 0.0;
109};
110
111// ============================================================================
112// Per-element functions (xsect:: namespace)
113// ============================================================================
114
115namespace xsect {
116
117double getAofY(const XSectParams& xs, double y);
118double getRofY(const XSectParams& xs, double y);
119double getWofY(const XSectParams& xs, double y);
120double getYofA(const XSectParams& xs, double a);
121double getSofA(const XSectParams& xs, double a);
122double getRofA(const XSectParams& xs, double a);
123double getdSdA(const XSectParams& xs, double a);
124double getAofS(const XSectParams& xs, double s_factor);
125double getAmax(const XSectParams& xs);
126double getYcrit(const XSectParams& xs, double q);
127bool isOpen(int type);
128int setParams(XSectParams& xs, int type, const double p[], double ucf);
129
130// Lookup table helpers (exposed for batch kernels and testing)
131double lookup(double x, const double* table, int n_items);
132double invLookup(double y, const double* table, int n_items);
133int locate(double y, const double* table, int n);
134double getYcircular(double alpha);
135double getScircular(double alpha);
136
137} // namespace xsect
138
139// ============================================================================
140// Shape group — contiguous SoA for all links sharing one shape type
141// ============================================================================
142
155 int count = 0;
156
157 // Mapping back to global link arrays
158 std::vector<int> link_idx;
159
160 // Geometry parameters (contiguous, aligned for SIMD)
161 std::vector<double> y_full;
162 std::vector<double> a_full;
163 std::vector<double> r_full;
164 std::vector<double> s_full;
165 std::vector<double> w_max;
166
167 // Pre-computed reciprocal of y_full (avoids per-element division in kernels)
168 std::vector<double> inv_y_full;
169
170 // Multi-purpose parameters (meaning depends on shape)
171 std::vector<double> y_bot;
172 std::vector<double> a_bot;
173 std::vector<double> s_bot;
174 std::vector<double> r_bot;
175
176 // Per-link transect table pointers (IRREGULAR shapes only)
177 // Each pointer → a normalized table of N_TRANSECT_TBL entries.
178 std::vector<const double*> area_tables;
179 std::vector<const double*> hrad_tables;
180 std::vector<const double*> width_tables;
182
183 // Pre-allocated working buffers (avoids per-call allocation in hot loop)
184 mutable std::vector<double> buf_d;
185 mutable std::vector<double> buf_r;
186 mutable std::vector<double> buf_r2;
187
189 void resize(int n);
190};
191
192// ============================================================================
193// XSectGroups — the shape-grouped index over all links
194// ============================================================================
195
209public:
219 void build(const SimulationContext& ctx);
220
231
240 void build(const XSectParams* params, int n_links);
241
242 // ========================================================================
243 // Batch compute — results scattered to global link arrays
244 // ========================================================================
245
253 void computeAreas(const double* depths, double* areas, int n_links) const;
254
262 void computeHydRad(const double* depths, double* hydrad, int n_links) const;
263
271 void computeWidths(const double* depths, double* widths, int n_links) const;
272
281 void computeAreaAndHydRad(const double* depths, double* areas,
282 double* hydrad, int n_links) const;
283
290 const double* d1, const double* d2, const double* dm,
291 double* a1, double* a2, double* am,
292 double* hrad1, double* hrad_mid, int n_links) const;
293
298 const double* d1, const double* d2, const double* dm,
299 double* w1, double* w2, double* wm, int n_links) const;
300
308 void computeSectionFactors(const double* areas, double* sfact, int n_links) const;
309
317 void computeDepthsFromArea(const double* areas, double* depths, int n_links) const;
318
319 // ========================================================================
320 // Accessors
321 // ========================================================================
322
324 int numGroups() const { return static_cast<int>(groups_.size()); }
325
327 const ShapeGroup& group(int i) const { return groups_[i]; }
328
330 const ShapeGroup* findGroup(XSectShape shape) const;
331
332private:
333 std::vector<ShapeGroup> groups_;
334};
335
336// ============================================================================
337// Shape-specific batch kernels (called by XSectGroups, also usable directly)
338// ============================================================================
339
340namespace xsect_batch {
341
349void area_circular(
350 const double* OPENSWMM_RESTRICT depth,
351 const double* OPENSWMM_RESTRICT y_full,
352 const double* OPENSWMM_RESTRICT a_full,
353 double* OPENSWMM_RESTRICT area,
354 int count
355);
356
358void area_rect(
359 const double* OPENSWMM_RESTRICT depth,
360 const double* OPENSWMM_RESTRICT w_max,
361 double* OPENSWMM_RESTRICT area,
362 int count
363);
364
367 const double* OPENSWMM_RESTRICT depth,
368 const double* OPENSWMM_RESTRICT y_bot,
369 const double* OPENSWMM_RESTRICT s_bot,
370 double* OPENSWMM_RESTRICT area,
371 int count
372);
373
375void area_triangular(
376 const double* OPENSWMM_RESTRICT depth,
377 const double* OPENSWMM_RESTRICT s_bot,
378 double* OPENSWMM_RESTRICT area,
379 int count
380);
381
383void area_parabolic(
384 const double* OPENSWMM_RESTRICT depth,
385 const double* OPENSWMM_RESTRICT r_bot,
386 double* OPENSWMM_RESTRICT area,
387 int count
388);
389
391void area_powerfunc(
392 const double* OPENSWMM_RESTRICT depth,
393 const double* OPENSWMM_RESTRICT s_bot,
394 const double* OPENSWMM_RESTRICT r_bot,
395 double* OPENSWMM_RESTRICT area,
396 int count
397);
398
400void area_tabulated(
401 const double* OPENSWMM_RESTRICT depth,
402 const double* OPENSWMM_RESTRICT y_full,
403 const double* OPENSWMM_RESTRICT a_full,
404 const double* table,
405 int table_size,
406 double* OPENSWMM_RESTRICT area,
407 int count
408);
409
412 const double* OPENSWMM_RESTRICT depth,
413 const double* OPENSWMM_RESTRICT y_full,
414 const double* OPENSWMM_RESTRICT a_full,
415 const double* table,
416 int table_size,
417 double* OPENSWMM_RESTRICT area,
418 int count
419);
420
421// --- Hydraulic radius batch kernels ---
422
423void hydrad_circular(
424 const double* OPENSWMM_RESTRICT depth,
425 const double* OPENSWMM_RESTRICT y_full,
426 const double* OPENSWMM_RESTRICT r_full,
427 double* OPENSWMM_RESTRICT hydrad,
428 int count
429);
430
432 const double* OPENSWMM_RESTRICT depth,
433 const double* OPENSWMM_RESTRICT y_bot,
434 const double* OPENSWMM_RESTRICT s_bot,
435 const double* OPENSWMM_RESTRICT r_bot,
436 double* OPENSWMM_RESTRICT hydrad,
437 int count
438);
439
441 const double* OPENSWMM_RESTRICT depth,
442 const double* OPENSWMM_RESTRICT s_bot,
443 const double* OPENSWMM_RESTRICT r_bot,
444 double* OPENSWMM_RESTRICT hydrad,
445 int count
446);
447
448void hydrad_rect(
449 const double* OPENSWMM_RESTRICT depth,
450 const double* OPENSWMM_RESTRICT w_max,
451 double* OPENSWMM_RESTRICT hydrad,
452 int count
453);
454
456 const double* OPENSWMM_RESTRICT depth,
457 const double* OPENSWMM_RESTRICT y_full,
458 const double* OPENSWMM_RESTRICT r_full,
459 const double* table,
460 int table_size,
461 double* OPENSWMM_RESTRICT hydrad,
462 int count
463);
464
465// --- Top width batch kernels ---
466
467void width_circular(
468 const double* OPENSWMM_RESTRICT depth,
469 const double* OPENSWMM_RESTRICT y_full,
470 const double* OPENSWMM_RESTRICT w_max,
471 double* OPENSWMM_RESTRICT width,
472 int count
473);
474
476 const double* OPENSWMM_RESTRICT depth,
477 const double* OPENSWMM_RESTRICT y_bot,
478 const double* OPENSWMM_RESTRICT s_bot,
479 double* OPENSWMM_RESTRICT width,
480 int count
481);
482
484 const double* OPENSWMM_RESTRICT depth,
485 const double* OPENSWMM_RESTRICT s_bot,
486 double* OPENSWMM_RESTRICT width,
487 int count
488);
489
490void width_rect(
491 const double* OPENSWMM_RESTRICT w_max,
492 double* OPENSWMM_RESTRICT width,
493 int count
494);
495
496void width_tabulated(
497 const double* OPENSWMM_RESTRICT depth,
498 const double* OPENSWMM_RESTRICT y_full,
499 const double* OPENSWMM_RESTRICT w_max,
500 const double* table,
501 int table_size,
502 double* OPENSWMM_RESTRICT width,
503 int count
504);
505
506} // namespace xsect_batch
507
508} // namespace openswmm
509
510#endif // OPENSWMM_XSECT_BATCH_HPP
#define OPENSWMM_RESTRICT
Definition XSectBatch.hpp:41
Shape-grouped cross-section manager for batch computation.
Definition XSectBatch.hpp:208
void computeAreas(const double *depths, double *areas, int n_links) const
Compute area for every link, reading depth from depths[link].
Definition XSectBatch.cpp:800
void computeWidths(const double *depths, double *widths, int n_links) const
Compute top width for every link.
Definition XSectBatch.cpp:1103
void computeAreaAndHydRad(const double *depths, double *areas, double *hydrad, int n_links) const
Fused area + hydraulic radius computation (single gather/scatter).
Definition XSectBatch.cpp:965
void build(const SimulationContext &ctx)
Build shape groups from SimulationContext link data.
void computeAreaHydRadTriple(const double *d1, const double *d2, const double *dm, double *a1, double *a2, double *am, double *hrad1, double *hrad_mid, int n_links) const
Fused triple: d1→(a1,hrad1), d2→a2, dm→(am,hrad_mid) in one pass over shape groups....
Definition XSectBatch.cpp:1174
void computeWidthsTriple(const double *d1, const double *d2, const double *dm, double *w1, double *w2, double *wm, int n_links) const
Fused triple: d1→w1, d2→w2, dm→wm in one pass over shape groups.
Definition XSectBatch.cpp:1209
const ShapeGroup & group(int i) const
Access a specific shape group.
Definition XSectBatch.hpp:327
void computeDepthsFromArea(const double *areas, double *depths, int n_links) const
Compute depth from area for every link (inverse).
Definition XSectBatch.cpp:1254
void computeHydRad(const double *depths, double *hydrad, int n_links) const
Compute hydraulic radius for every link.
Definition XSectBatch.cpp:892
void computeSectionFactors(const double *areas, double *sfact, int n_links) const
Compute section factor for every link (from area, not depth).
Definition XSectBatch.cpp:1235
void attachTransectTables(const SimulationContext &ctx)
Attach transect tables to the IRREGULAR shape group.
Definition XSectBatch.cpp:120
int numGroups() const
Number of non-empty shape groups.
Definition XSectBatch.hpp:324
const ShapeGroup * findGroup(XSectShape shape) const
Find the group for a given shape (returns nullptr if no links have that shape).
Definition XSectBatch.cpp:153
void area_trapezoidal(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT y_bot, const double *OPENSWMM_RESTRICT s_bot, double *OPENSWMM_RESTRICT area, int count)
Batch area for TRAPEZOIDAL: area = (y_bot + s_bot * depth) * depth.
Definition XSectBatch.cpp:207
void width_circular(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT inv_y_full, const double *OPENSWMM_RESTRICT w_max, double *OPENSWMM_RESTRICT width, int count)
Definition XSectBatch.cpp:454
void hydrad_tabulated(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT inv_y_full, const double *OPENSWMM_RESTRICT r_full, const double *table, int table_size, double *OPENSWMM_RESTRICT hydrad, int count)
Definition XSectBatch.cpp:420
void hydrad_circular(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT inv_y_full, const double *OPENSWMM_RESTRICT r_full, double *OPENSWMM_RESTRICT hydrad, int count)
Definition XSectBatch.cpp:348
void width_rect(const double *OPENSWMM_RESTRICT w_max, double *OPENSWMM_RESTRICT width, int count)
Definition XSectBatch.cpp:505
void hydrad_triangular(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT s_bot, const double *OPENSWMM_RESTRICT r_bot, double *OPENSWMM_RESTRICT hydrad, int count)
Definition XSectBatch.cpp:391
void hydrad_trapezoidal(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT y_bot, const double *OPENSWMM_RESTRICT s_bot, const double *OPENSWMM_RESTRICT r_bot, double *OPENSWMM_RESTRICT hydrad, int count)
Definition XSectBatch.cpp:372
void width_triangular(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT s_bot, double *OPENSWMM_RESTRICT width, int count)
Definition XSectBatch.cpp:492
void hydrad_rect(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT w_max, double *OPENSWMM_RESTRICT hydrad, int count)
Definition XSectBatch.cpp:405
void area_inv_tabulated(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT inv_y_full, const double *OPENSWMM_RESTRICT a_full, const double *table, int table_size, double *OPENSWMM_RESTRICT area, int count)
Batch area for shapes using invLookup (gothic, catenary, semielliptical, semicircular).
Definition XSectBatch.cpp:294
void area_powerfunc(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT s_bot, const double *OPENSWMM_RESTRICT r_bot, double *OPENSWMM_RESTRICT area, int count)
Batch area for POWERFUNC: area = r_bot * depth^(s_bot+1).
Definition XSectBatch.cpp:251
void area_circular(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT inv_y_full, const double *OPENSWMM_RESTRICT a_full, double *OPENSWMM_RESTRICT area, int count)
Batch area for CIRCULAR/FORCE_MAIN — lookup table interpolation.
Definition XSectBatch.cpp:166
void area_rect(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT w_max, double *OPENSWMM_RESTRICT area, int count)
Batch area for RECT_CLOSED / RECT_OPEN: area = depth * w_max.
Definition XSectBatch.cpp:197
void width_tabulated(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT inv_y_full, const double *OPENSWMM_RESTRICT w_max, const double *table, int table_size, double *OPENSWMM_RESTRICT width, int count)
Definition XSectBatch.cpp:515
void width_trapezoidal(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT y_bot, const double *OPENSWMM_RESTRICT s_bot, double *OPENSWMM_RESTRICT width, int count)
Definition XSectBatch.cpp:478
void area_tabulated(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT inv_y_full, const double *OPENSWMM_RESTRICT a_full, const double *table, int table_size, double *OPENSWMM_RESTRICT area, int count)
Batch area for any tabulated shape (egg, horseshoe, arch, ellipse, etc.).
Definition XSectBatch.cpp:264
void area_triangular(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT s_bot, double *OPENSWMM_RESTRICT area, int count)
Batch area for TRIANGULAR: area = s_bot * depth^2.
Definition XSectBatch.cpp:222
void area_parabolic(const double *OPENSWMM_RESTRICT depth, const double *OPENSWMM_RESTRICT r_bot, double *OPENSWMM_RESTRICT area, int count)
Batch area for PARABOLIC: area = (4/3) * r_bot * depth^(3/2).
Definition XSectBatch.cpp:236
double getAmax(const XSectParams &xs)
Definition XSection.cpp:796
double getdSdA(const XSectParams &xs, double a)
Definition XSection.cpp:760
double lookup(double x, const double *table, int n_items)
Definition XSection.cpp:42
double invLookup(double y, const double *table, int n_items)
Definition XSection.cpp:63
double getYofA(const XSectParams &xs, double a)
Definition XSection.cpp:639
double getScircular(double alpha)
Definition XSection.cpp:101
double getWofY(const XSectParams &xs, double y)
Definition XSection.cpp:517
double getSofA(const XSectParams &xs, double a)
Definition XSection.cpp:701
double getRofY(const XSectParams &xs, double y)
Definition XSection.cpp:583
double getYcrit(const XSectParams &xs, double q)
Definition XSection.cpp:806
int locate(double y, const double *table, int n)
Definition XSection.cpp:32
double getAofY(const XSectParams &xs, double y)
Definition XSection.cpp:455
double getAofS(const XSectParams &xs, double s_factor)
Definition XSection.cpp:773
bool isOpen(int type)
Definition XSection.cpp:903
int setParams(XSectParams &xs, int type, const double p[], double ucf)
Definition XSection.cpp:921
double getRofA(const XSectParams &xs, double a)
Definition XSection.cpp:750
double getYcircular(double alpha)
Definition XSection.cpp:95
Definition NodeCoupling.cpp:15
XSectShape
Definition XSectBatch.hpp:59
@ FORCE_MAIN
Circular force main (Hazen-Williams or D-W)
@ STREET_XSECT
Street cross-section.
@ IRREGULAR
User-supplied shape curve.
@ HORIZ_ELLIPSE
Horizontal elliptical pipe.
@ VERT_ELLIPSE
Vertical elliptical pipe.
@ CUSTOM
Shape from CURVE_SHAPE table.
@ RECT_ROUND
Rectangular-round bottom.
@ DUMMY
Dummy (no geometry)
@ RECT_TRIANG
Rectangular-triangular bottom.
double * y
Definition odesolve.c:28
SoA parameter block for all links of one cross-section shape.
Definition XSectBatch.hpp:153
std::vector< double > r_full
Hyd. radius at full (ft)
Definition XSectBatch.hpp:163
std::vector< const double * > hrad_tables
Per-link hyd-rad table.
Definition XSectBatch.hpp:179
std::vector< double > w_max
Max width (ft)
Definition XSectBatch.hpp:165
std::vector< double > buf_r
Scatter buffer for results.
Definition XSectBatch.hpp:185
std::vector< double > buf_r2
Second scatter buffer (for fused ops)
Definition XSectBatch.hpp:186
std::vector< double > inv_y_full
1.0 / y_full (or 0 if y_full==0)
Definition XSectBatch.hpp:168
XSectShape shape
Definition XSectBatch.hpp:154
int transect_tbl_size
Table size (same for all)
Definition XSectBatch.hpp:181
std::vector< double > r_bot
Definition XSectBatch.hpp:174
std::vector< double > s_bot
Definition XSectBatch.hpp:173
std::vector< double > y_bot
Definition XSectBatch.hpp:171
void resize(int n)
Resize all arrays to n elements.
Definition XSectBatch.cpp:48
std::vector< const double * > area_tables
Per-link area table.
Definition XSectBatch.hpp:178
std::vector< const double * > width_tables
Per-link width table.
Definition XSectBatch.hpp:180
std::vector< int > link_idx
link_idx[i] = index in SimulationContext
Definition XSectBatch.hpp:158
std::vector< double > buf_d
Gather buffer for depths.
Definition XSectBatch.hpp:184
int count
Definition XSectBatch.hpp:155
std::vector< double > a_bot
Definition XSectBatch.hpp:172
std::vector< double > a_full
Full area (ft2)
Definition XSectBatch.hpp:162
std::vector< double > s_full
Section factor at full.
Definition XSectBatch.hpp:164
std::vector< double > y_full
Full depth (ft)
Definition XSectBatch.hpp:161
Central, reentrant simulation context.
Definition SimulationContext.hpp:274
Definition XSectBatch.hpp:92
double s_full
Section factor when full (ft^4/3)
Definition XSectBatch.hpp:102
double s_bot
Slope of bottom section / exponent.
Definition XSectBatch.hpp:107
int culvert_code
Definition XSectBatch.hpp:94
int transect
Definition XSectBatch.hpp:95
double a_full
Area when full (ft2)
Definition XSectBatch.hpp:100
double r_bot
Radius of bottom section / coefficient.
Definition XSectBatch.hpp:108
double s_max
Section factor at max flow (ft^4/3)
Definition XSectBatch.hpp:103
int type
Definition XSectBatch.hpp:93
double a_bot
Area of bottom section.
Definition XSectBatch.hpp:106
double w_max
Width at widest point (ft)
Definition XSectBatch.hpp:98
double yw_max
Depth at widest point (ft)
Definition XSectBatch.hpp:99
double y_bot
Depth of bottom section / fill depth.
Definition XSectBatch.hpp:105
double y_full
Full depth (ft)
Definition XSectBatch.hpp:97
double r_full
Hydraulic radius when full (ft)
Definition XSectBatch.hpp:101