OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
Serialize2D.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
41
42#ifndef OPENSWMM_ENGINE_2D_SERIALIZE_2D_HPP
43#define OPENSWMM_ENGINE_2D_SERIALIZE_2D_HPP
44
45#include "MeshData.hpp"
46#include "BoundaryData.hpp"
47#include "PendingRows2D.hpp"
48
49#include <algorithm>
50#include <cstdint>
51#include <string>
52#include <unordered_map>
53#include <unordered_set>
54#include <vector>
55
56namespace openswmm::twoD {
57
77inline std::vector<PendingBoundaryRow> collectBCRows(const std::vector<PendingBoundaryRow>* pending,
78 const BoundaryData* boundary, bool drained,
79 double flow_to_si_applied = 1.0) {
80
81 const double flow_to_display = (flow_to_si_applied > 0.0) ? 1.0 / flow_to_si_applied : 1.0;
82
83 if (!drained) {
84 if (pending && !pending->empty())
85 return *pending;
86 // Not drained and no pending rows: nothing has been authored yet
87 // (boundary, if sized at all, holds only WALL defaults).
88 }
89
90 std::vector<PendingBoundaryRow> rows;
91 if (!boundary)
92 return rows;
93
94 std::unordered_map<int, std::string> groups;
95 if (pending)
96 for (const auto& p : *pending) groups[MeshData::slot(p.tri,p.edge)] = p.group;
97 const int n = boundary->size();
98 for (int idx = 0; idx < n; ++idx) {
99 const auto type = static_cast<BoundaryType>(boundary->edge_bc_type[idx]);
100 if (type == BoundaryType::WALL && boundary->edge_wall_drag[idx] == 0 &&
101 (groups.find(idx) == groups.end() || groups.at(idx).empty()))
102 continue;
103
105 r.tri = MeshData::slot_cell(idx);
106 r.edge = MeshData::slot_local(idx);
107 r.bc_type = static_cast<int>(type);
108 if (auto it = groups.find(idx); it != groups.end()) r.group = it->second;
109
110 switch (type) {
112 r.param1 = boundary->edge_bed_slope[idx];
113 break;
115 if (!boundary->edge_bc_tseries_name[idx].empty())
116 r.name = boundary->edge_bc_tseries_name[idx];
117 else
118 r.param1 = boundary->edge_bc_head[idx];
119 break;
121 if (!boundary->edge_bc_flow_tseries_name[idx].empty())
122 r.name = boundary->edge_bc_flow_tseries_name[idx];
123 else
124 r.param1 = boundary->edge_bc_flow[idx] * flow_to_display;
125 break;
127 r.name = boundary->edge_bc_rating_curve_name[idx];
128 break;
130 r.param1 = boundary->edge_wall_drag[idx];
131 break;
132 }
133 rows.push_back(std::move(r));
134 }
135
136 return rows;
137}
138
152inline std::vector<PendingEdgeConveyanceRow>
153collectConveyanceRows(const std::vector<PendingEdgeConveyanceRow>* pending, const MeshData* mesh,
154 bool drained) {
155
156 if (!drained && pending && !pending->empty())
157 return *pending;
158
159 std::vector<PendingEdgeConveyanceRow> rows;
160 if (!mesh)
161 return rows;
162
163 const int nt = mesh->n_triangles();
164 if (nt < 1 || mesh->edge_conveyance.size() < static_cast<std::size_t>(mesh->n_edge_slots()))
165 return rows;
166
167 std::unordered_set<std::int64_t> seen;
168 for (int t = 0; t < nt; ++t) {
169 const int nvc = mesh->cell_vertex_count(t);
170 for (int e = 0; e < nvc; ++e) {
171 const double k = mesh->edge_conveyance[MeshData::slot(t, e)];
172 if (k == 1.0)
173 continue;
174 int ea, eb;
175 mesh->cell_edge_vertices(t, e, ea, eb);
176 const int va = std::min(ea, eb);
177 const int vb = std::max(ea, eb);
178 const std::int64_t key = (static_cast<std::int64_t>(va) << 32) |
179 (static_cast<std::int64_t>(vb) & 0xFFFFFFFFLL);
180 if (!seen.insert(key).second)
181 continue;
182 rows.push_back(PendingEdgeConveyanceRow{va, vb, k});
183 }
184 }
185 return rows;
186}
187
188} // namespace openswmm::twoD
189
190#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 ExchangeLaw.hpp:60
std::vector< PendingBoundaryRow > collectBCRows(const std::vector< PendingBoundaryRow > *pending, const BoundaryData *boundary, bool drained, double flow_to_si_applied=1.0)
Copy authored boundary rows or reconstruct them from live edge storage.
Definition Serialize2D.hpp:77
std::vector< PendingEdgeConveyanceRow > collectConveyanceRows(const std::vector< PendingEdgeConveyanceRow > *pending, const MeshData *mesh, bool drained)
Copy authored conveyance rows or reconstruct unique nondefault mesh edges.
Definition Serialize2D.hpp:153
BoundaryType
Boundary condition types for 2D mesh edges.
Definition BoundaryData.hpp:58
@ RATING_CURVE
Stage → flow lookup (curve registry index)
Definition BoundaryData.hpp:63
@ SPECIFIED_FLOW
Prescribed discharge per metre of edge (constant or TS)
Definition BoundaryData.hpp:62
@ NORMAL_FLOW
Manning outflow using bed slope.
Definition BoundaryData.hpp:60
@ WALL
Zero-flux wall (default)
Definition BoundaryData.hpp:59
@ SPECIFIED_STAGE
Prescribed water surface elevation (constant or TS)
Definition BoundaryData.hpp:61
SoA storage for per-edge boundary conditions.
Definition BoundaryData.hpp:73
std::vector< std::string > edge_bc_flow_tseries_name
Timeseries name for deferred resolution (cleared after resolve).
Definition BoundaryData.hpp:120
std::vector< int8_t > edge_bc_type
Boundary condition type per edge (cast from BoundaryType)
Definition BoundaryData.hpp:76
std::vector< std::string > edge_bc_tseries_name
Timeseries name for deferred resolution (cleared after resolve).
Definition BoundaryData.hpp:99
std::vector< double > edge_bed_slope
Definition BoundaryData.hpp:87
std::vector< double > edge_bc_flow
Definition BoundaryData.hpp:112
std::vector< double > edge_bc_head
Definition BoundaryData.hpp:91
int size() const noexcept
Report the number of padded boundary edge slots.
Definition BoundaryData.hpp:247
std::vector< std::string > edge_bc_rating_curve_name
Curve name for deferred resolution (cleared after resolve).
Definition BoundaryData.hpp:131
std::vector< double > edge_wall_drag
Optional tangential wall drag: tau/rho = -Cw |ut| ut. Zero = free slip.
Definition BoundaryData.hpp:79
SoA storage for 2D mixed triangle/quad mesh geometry and topology.
Definition MeshData.hpp:65
std::vector< double > edge_conveyance
Definition MeshData.hpp:131
int n_triangles() const noexcept
Number of CELLS (triangles + quads). The historical name is kept for the ~200 call sites; n_cells() i...
Definition MeshData.hpp:213
void cell_edge_vertices(int c, int k, int &a, int &b) const noexcept
Endpoint vertices of local edge k of cell c: v[(k+1)nv], v[(k+2)nv].
Definition MeshData.hpp:336
static constexpr int slot(int c, int k) noexcept
Flat edge/vertex slot of local index k in cell c.
Definition MeshData.hpp:267
int n_edge_slots() const noexcept
Number of internal edge slots (n_cells * kMaxCellVerts).
Definition MeshData.hpp:252
static constexpr int slot_cell(int s) noexcept
Convert mixed-mesh padded slot coordinates.
Definition MeshData.hpp:283
int cell_vertex_count(int c) const noexcept
Read one mixed-cell topology field.
Definition MeshData.hpp:292
static constexpr int slot_local(int s) noexcept
Local index k of a flat slot; cell of a flat slot.
Definition MeshData.hpp:276
Per-row buffer for [2D_BOUNDARY_CONDITIONS] parse output.
Definition PendingRows2D.hpp:53
double param1
slope / head / flow / Cw (WALL_DRAG)
Definition PendingRows2D.hpp:57
std::string group
named group ("" = none)
Definition PendingRows2D.hpp:59
std::string name
TS name or curve name (TS_/Rating variants)
Definition PendingRows2D.hpp:58
int bc_type
openswmm::twoD::BoundaryType cast
Definition PendingRows2D.hpp:56
int tri
Definition PendingRows2D.hpp:54
int edge
0..2
Definition PendingRows2D.hpp:55
Per-row buffer for [2D_EDGE_CONVEYANCE] parse output (§11A).
Definition PendingRows2D.hpp:72