OpenSWMM Engine  6.0.0-alpha.1
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.1)
Loading...
Searching...
No Matches
Serialize2D.hpp
Go to the documentation of this file.
1
25
26#ifndef OPENSWMM_ENGINE_2D_SERIALIZE_2D_HPP
27#define OPENSWMM_ENGINE_2D_SERIALIZE_2D_HPP
28
29#include "MeshData.hpp"
30#include "BoundaryData.hpp"
31#include "PendingRows2D.hpp"
32
33#include <algorithm>
34#include <cstdint>
35#include <string>
36#include <unordered_map>
37#include <unordered_set>
38#include <vector>
39
40namespace openswmm::twoD {
41
59inline std::vector<PendingBoundaryRow> collectBCRows(
60 const std::vector<PendingBoundaryRow>* pending,
61 const BoundaryData* boundary,
62 bool drained) {
63
64 if (!drained) {
65 if (pending && !pending->empty()) return *pending;
66 // Not drained and no pending rows: nothing has been authored yet
67 // (boundary, if sized at all, holds only WALL defaults).
68 }
69
70 std::vector<PendingBoundaryRow> rows;
71 if (!boundary) return rows;
72
73 const int n = boundary->size();
74 for (int idx = 0; idx < n; ++idx) {
75 const auto type =
76 static_cast<BoundaryType>(boundary->edge_bc_type[idx]);
77 if (type == BoundaryType::WALL) continue;
78
80 r.tri = idx / 3;
81 r.edge = idx % 3;
82 r.bc_type = static_cast<int>(type);
83
84 switch (type) {
86 r.param1 = boundary->edge_bed_slope[idx];
87 break;
89 if (!boundary->edge_bc_tseries_name[idx].empty())
90 r.name = boundary->edge_bc_tseries_name[idx];
91 else
92 r.param1 = boundary->edge_bc_head[idx];
93 break;
95 if (!boundary->edge_bc_flow_tseries_name[idx].empty())
96 r.name = boundary->edge_bc_flow_tseries_name[idx];
97 else
98 r.param1 = boundary->edge_bc_flow[idx];
99 break;
101 r.name = boundary->edge_bc_rating_curve_name[idx];
102 break;
104 break;
105 }
106 rows.push_back(std::move(r));
107 }
108
109 // Re-attach the authored GROUP labels (organizational metadata that
110 // BoundaryData does not carry) from the retained pending rows.
111 if (pending && !pending->empty() && !rows.empty()) {
112 std::unordered_map<int, const std::string*> groups;
113 groups.reserve(pending->size());
114 for (const auto& p : *pending) {
115 if (!p.group.empty()) groups[p.tri * 3 + p.edge] = &p.group;
116 }
117 if (!groups.empty()) {
118 for (auto& r : rows) {
119 auto it = groups.find(r.tri * 3 + r.edge);
120 if (it != groups.end()) r.group = *it->second;
121 }
122 }
123 }
124 return rows;
125}
126
146inline std::vector<PendingEdgeConveyanceRow> collectConveyanceRows(
147 const std::vector<PendingEdgeConveyanceRow>* pending,
148 const MeshData* mesh,
149 bool drained) {
150
151 if (!drained && pending && !pending->empty()) return *pending;
152
153 std::vector<PendingEdgeConveyanceRow> rows;
154 if (!mesh) return rows;
155
156 const int nt = mesh->n_triangles();
157 if (nt < 1 ||
158 mesh->edge_conveyance.size() < static_cast<std::size_t>(nt) * 3)
159 return rows;
160
161 std::unordered_set<std::int64_t> seen;
162 for (int t = 0; t < nt; ++t) {
163 const int v[3] = { mesh->tri_v0[t], mesh->tri_v1[t], mesh->tri_v2[t] };
164 for (int e = 0; e < 3; ++e) {
165 const double k = mesh->edge_conveyance[t * 3 + e];
166 if (k == 1.0) continue;
167 const int va = std::min(v[(e + 1) % 3], v[(e + 2) % 3]);
168 const int vb = std::max(v[(e + 1) % 3], v[(e + 2) % 3]);
169 const std::int64_t key =
170 (static_cast<std::int64_t>(va) << 32)
171 | (static_cast<std::int64_t>(vb) & 0xFFFFFFFFLL);
172 if (!seen.insert(key).second) continue;
173 rows.push_back(PendingEdgeConveyanceRow{va, vb, k});
174 }
175 }
176 return rows;
177}
178
179} // namespace openswmm::twoD
180
181#endif // OPENSWMM_ENGINE_2D_SERIALIZE_2D_HPP
Structure-of-Arrays (SoA) storage for 2D mesh boundary conditions.
Structure-of-Arrays (SoA) storage for 2D triangular mesh geometry.
Parse-time scratch rows for [2D_BOUNDARY_CONDITIONS] and [2D_EDGE_CONVEYANCE] input sections.
Definition NodeCoupling.cpp:15
std::vector< PendingBoundaryRow > collectBCRows(const std::vector< PendingBoundaryRow > *pending, const BoundaryData *boundary, bool drained)
Collect [2D_BOUNDARY_CONDITIONS]-shaped rows for serialization.
Definition Serialize2D.hpp:59
std::vector< PendingEdgeConveyanceRow > collectConveyanceRows(const std::vector< PendingEdgeConveyanceRow > *pending, const MeshData *mesh, bool drained)
Collect [2D_EDGE_CONVEYANCE]-shaped rows for serialization.
Definition Serialize2D.hpp:146
BoundaryType
Boundary condition types for 2D mesh edges.
Definition BoundaryData.hpp:40
@ RATING_CURVE
Stage → flow lookup (curve registry index)
Definition BoundaryData.hpp:45
@ SPECIFIED_FLOW
Prescribed discharge per metre of edge (constant or TS)
Definition BoundaryData.hpp:44
@ NORMAL_FLOW
Manning outflow using bed slope.
Definition BoundaryData.hpp:42
@ WALL
Zero-flux wall (default)
Definition BoundaryData.hpp:41
@ SPECIFIED_STAGE
Prescribed water surface elevation (constant or TS)
Definition BoundaryData.hpp:43
SoA storage for per-edge boundary conditions.
Definition BoundaryData.hpp:55
std::vector< std::string > edge_bc_flow_tseries_name
Timeseries name for deferred resolution (cleared after resolve).
Definition BoundaryData.hpp:95
std::vector< int8_t > edge_bc_type
Boundary condition type per edge (cast from BoundaryType)
Definition BoundaryData.hpp:58
std::vector< std::string > edge_bc_tseries_name
Timeseries name for deferred resolution (cleared after resolve).
Definition BoundaryData.hpp:74
std::vector< double > edge_bed_slope
Definition BoundaryData.hpp:62
std::vector< double > edge_bc_flow
Definition BoundaryData.hpp:87
std::vector< double > edge_bc_head
Definition BoundaryData.hpp:66
int size() const noexcept
Definition BoundaryData.hpp:131
std::vector< std::string > edge_bc_rating_curve_name
Curve name for deferred resolution (cleared after resolve).
Definition BoundaryData.hpp:106
SoA storage for 2D triangular mesh geometry and topology.
Definition MeshData.hpp:34
std::vector< int > tri_v1
Vertex 1 index.
Definition MeshData.hpp:51
std::vector< double > edge_conveyance
Definition MeshData.hpp:86
int n_triangles() const noexcept
Definition MeshData.hpp:122
std::vector< int > tri_v2
Vertex 2 index.
Definition MeshData.hpp:52
std::vector< int > tri_v0
Vertex 0 index.
Definition MeshData.hpp:50
Per-row buffer for [2D_BOUNDARY_CONDITIONS] parse output.
Definition PendingRows2D.hpp:37
double param1
slope (NormalFlow) / head (Stage) / flow (Flow)
Definition PendingRows2D.hpp:41
std::string name
TS name or curve name (TS_/Rating variants)
Definition PendingRows2D.hpp:42
int bc_type
openswmm::twoD::BoundaryType cast
Definition PendingRows2D.hpp:40
int tri
Definition PendingRows2D.hpp:38
int edge
0..2
Definition PendingRows2D.hpp:39
Per-row buffer for [2D_EDGE_CONVEYANCE] parse output (§11A).
Definition PendingRows2D.hpp:56