OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
MeshData.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
33
34#ifndef OPENSWMM_ENGINE_2D_MESH_DATA_HPP
35#define OPENSWMM_ENGINE_2D_MESH_DATA_HPP
36
37#include <vector>
38#include <string>
39#include <cstdint>
40
41namespace openswmm::twoD {
42
49inline constexpr int kMaxCellVerts = 4;
50
67struct MeshData {
68
69 // -----------------------------------------------------------------------
70 // Vertex arrays — indexed by vertex index [0, n_vertices)
71 // -----------------------------------------------------------------------
72
73 std::vector<double> vx;
74 std::vector<double> vy;
75 std::vector<double> vz;
76 std::vector<std::string> vtag;
77
78 // -----------------------------------------------------------------------
79 // Cell static properties — indexed by cell index [0, n_triangles())
80 // -----------------------------------------------------------------------
81
83 std::vector<uint8_t> cell_nv;
88 std::vector<int> cell_v;
91 std::vector<int> cell_nbr;
92
93 // Precomputed geometry (per cell)
94 std::vector<double> tri_area;
95 std::vector<double> tri_cx;
96 std::vector<double> tri_cy;
97 std::vector<double> tri_cz;
98
99 // Edge geometry — flat 2D: [cell * kMaxCellVerts + k]
100 std::vector<double> edge_length;
101 std::vector<double> edge_nx;
102 std::vector<double> edge_ny;
103 std::vector<double> edge_mx;
104 std::vector<double> edge_my;
105 std::vector<double> edge_mz;
110 std::vector<double> edge_dist_c;
111
117 std::vector<double> quad_vfr_z;
118 std::vector<double> quad_vfr_a;
119
133 std::vector<double> edge_conveyance;
134
135 // Surface properties
136 std::vector<double> mannings_n;
137 std::vector<double> tri_init_depth;
138 std::vector<double> tri_init_u;
139 std::vector<double> tri_init_v;
140 std::vector<std::string> tri_tag;
141
142 // -----------------------------------------------------------------------
143 // Vertex reconstruction stencil (pseudo-Laplacian weights)
144 // -----------------------------------------------------------------------
145 // Stored as CSR (compressed sparse row) for variable stencil sizes
146 std::vector<int> vert_stencil_ptr;
147 std::vector<int> vert_stencil_idx;
148 std::vector<double> vert_stencil_wt;
149
150 // -----------------------------------------------------------------------
151 // Coupling maps
152 // -----------------------------------------------------------------------
153
154 std::vector<int> vert_coupled_node;
155 std::vector<int> tri_coupled_node;
156
157 // Coupling parameters (per vertex/triangle coupling point)
158 std::vector<double> vert_coupling_cd;
159 std::vector<double> vert_coupling_area;
163 std::vector<uint8_t> vert_coupling_area_set;
164 std::vector<double> tri_coupling_cd;
165 std::vector<double> tri_coupling_area;
166
167 // Deferred resolution names (populated during parsing, cleared after resolve)
168 std::vector<std::string> vert_coupled_node_name;
169 std::vector<std::string> tri_coupled_node_name;
170
180 int tri = -1;
181 int node = -1;
182 std::string node_name;
183 double cd = 0.65;
184 double area = 1.0;
185 bool area_set = false;
188 };
189 std::vector<TriCouplingRow> tri_couplings;
190
191 // -----------------------------------------------------------------------
192 // Capacity queries
193 // -----------------------------------------------------------------------
194
195 int n_vertices() const noexcept { return static_cast<int>(vx.size()); }
198 int n_triangles() const noexcept { return static_cast<int>(cell_nv.size()); }
199 int n_cells() const noexcept { return static_cast<int>(cell_nv.size()); }
201 int n_quads() const noexcept {
202 int q = 0;
203 for (uint8_t nv : cell_nv) q += (nv == 4);
204 return q;
205 }
206
210 int edge_stride() const noexcept { return n_quads() > 0 ? kMaxCellVerts : 3; }
212 int n_edge_slots() const noexcept { return n_cells() * kMaxCellVerts; }
213
214 // -----------------------------------------------------------------------
215 // Cell / edge-slot accessors
216 // -----------------------------------------------------------------------
217
219 static constexpr int slot(int c, int k) noexcept { return c * kMaxCellVerts + k; }
221 static constexpr int slot_local(int s) noexcept { return s % kMaxCellVerts; }
222 static constexpr int slot_cell(int s) noexcept { return s / kMaxCellVerts; }
223
224 int cell_vertex_count(int c) const noexcept { return cell_nv[static_cast<std::size_t>(c)]; }
225 int cell_vertex(int c, int k) const noexcept { return cell_v[static_cast<std::size_t>(slot(c, k))]; }
226 int cell_neighbour(int c, int k) const noexcept { return cell_nbr[static_cast<std::size_t>(slot(c, k))]; }
227 bool is_quad(int c) const noexcept { return cell_nv[static_cast<std::size_t>(c)] == 4; }
228
230 void cell_edge_vertices(int c, int k, int& a, int& b) const noexcept {
231 const int nv = cell_nv[static_cast<std::size_t>(c)];
232 a = cell_v[static_cast<std::size_t>(slot(c, (k + 1) % nv))];
233 b = cell_v[static_cast<std::size_t>(slot(c, (k + 2) % nv))];
234 }
235
236 void set_triangle(int c, int v0, int v1, int v2) noexcept {
237 cell_nv[static_cast<std::size_t>(c)] = 3;
238 cell_v[static_cast<std::size_t>(slot(c, 0))] = v0;
239 cell_v[static_cast<std::size_t>(slot(c, 1))] = v1;
240 cell_v[static_cast<std::size_t>(slot(c, 2))] = v2;
241 cell_v[static_cast<std::size_t>(slot(c, 3))] = -1;
242 }
243 void set_quad(int c, int v0, int v1, int v2, int v3) noexcept {
244 cell_nv[static_cast<std::size_t>(c)] = 4;
245 cell_v[static_cast<std::size_t>(slot(c, 0))] = v0;
246 cell_v[static_cast<std::size_t>(slot(c, 1))] = v1;
247 cell_v[static_cast<std::size_t>(slot(c, 2))] = v2;
248 cell_v[static_cast<std::size_t>(slot(c, 3))] = v3;
249 }
250
251 // -----------------------------------------------------------------------
252 // Resize / allocation
253 // -----------------------------------------------------------------------
254
255 void resize_vertices(int nv) {
256 auto n = static_cast<std::size_t>(nv);
257 vx.resize(n, 0.0);
258 vy.resize(n, 0.0);
259 vz.resize(n, 0.0);
260 vtag.resize(n);
261 vert_coupled_node.resize(n, -1);
262 vert_coupled_node_name.resize(n);
263 vert_coupling_cd.resize(n, 0.65);
264 vert_coupling_area.resize(n, 1.0);
265 vert_coupling_area_set.resize(n, 0);
266 }
267
270 void resize_triangles(int nt) {
271 auto n = static_cast<std::size_t>(nt);
272 const auto old_n = cell_nv.size();
273 cell_nv.resize(n, 3);
274 const auto n4 = n * static_cast<std::size_t>(kMaxCellVerts);
275 cell_v.resize(n4, 0);
276 cell_nbr.resize(n4, -1);
277 // Padding slot of every NEW triangle: vertex −1, neighbour −2.
278 for (std::size_t c = old_n; c < n; ++c) {
279 cell_v[c * kMaxCellVerts + 3] = -1;
280 cell_nbr[c * kMaxCellVerts + 3] = -2;
281 }
282 tri_area.resize(n, 0.0);
283 tri_cx.resize(n, 0.0);
284 tri_cy.resize(n, 0.0);
285 tri_cz.resize(n, 0.0);
286
287 edge_length.resize(n4, 0.0);
288 edge_nx.resize(n4, 0.0);
289 edge_ny.resize(n4, 0.0);
290 edge_mx.resize(n4, 0.0);
291 edge_my.resize(n4, 0.0);
292 edge_mz.resize(n4, 0.0);
293 edge_dist_c.resize(n4, 0.0);
294 edge_conveyance.resize(n4, 1.0); // §11A — default unrestricted
295
296 mannings_n.resize(n, 0.035);
297 tri_init_depth.resize(n, 0.0);
298 tri_init_u.resize(n, 0.0);
299 tri_init_v.resize(n, 0.0);
300 tri_tag.resize(n);
301 tri_coupled_node.resize(n, -1);
302 tri_coupled_node_name.resize(n);
303 tri_coupling_cd.resize(n, 0.65);
304 tri_coupling_area.resize(n, 1.0);
305 }
306};
307
308} // namespace openswmm::twoD
309
310#endif // OPENSWMM_ENGINE_2D_MESH_DATA_HPP
Definition NodeCoupling.cpp:16
constexpr int kMaxCellVerts
Definition MeshData.hpp:49
Definition MeshData.hpp:179
int node
SWMM node index (-1 until resolved)
Definition MeshData.hpp:181
bool area_set
Definition MeshData.hpp:185
std::string node_name
Deferred name (cleared after resolve)
Definition MeshData.hpp:182
double cd
Discharge coefficient.
Definition MeshData.hpp:183
double area
Effective exchange area.
Definition MeshData.hpp:184
int tri
Triangle index.
Definition MeshData.hpp:180
SoA storage for 2D mixed triangle/quad mesh geometry and topology.
Definition MeshData.hpp:67
std::vector< double > tri_coupling_area
Effective exchange area.
Definition MeshData.hpp:165
std::vector< double > edge_conveyance
Definition MeshData.hpp:133
int n_triangles() const noexcept
Definition MeshData.hpp:198
std::vector< uint8_t > vert_coupling_area_set
Definition MeshData.hpp:163
std::vector< int > vert_stencil_idx
Column indices (triangle indices)
Definition MeshData.hpp:147
std::vector< double > tri_area
Planimetric area (m²)
Definition MeshData.hpp:94
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:230
int n_quads() const noexcept
Number of quadrilateral cells (O(n) scan; not a hot path).
Definition MeshData.hpp:201
std::vector< double > edge_nx
Outward normal X component.
Definition MeshData.hpp:101
std::vector< double > edge_length
Length of each edge.
Definition MeshData.hpp:100
void resize_triangles(int nt)
Definition MeshData.hpp:270
std::vector< std::string > tri_coupled_node_name
Definition MeshData.hpp:169
std::vector< std::string > vtag
Optional vertex tag.
Definition MeshData.hpp:76
int cell_vertex(int c, int k) const noexcept
Definition MeshData.hpp:225
std::vector< double > tri_cy
Centroid Y (area centroid)
Definition MeshData.hpp:96
std::vector< double > vy
Vertex Y coordinate.
Definition MeshData.hpp:74
std::vector< int > tri_coupled_node
SWMM node index (-1 = none)
Definition MeshData.hpp:155
void set_triangle(int c, int v0, int v1, int v2) noexcept
Definition MeshData.hpp:236
static constexpr int slot(int c, int k) noexcept
Flat edge/vertex slot of local index k in cell c.
Definition MeshData.hpp:219
std::vector< int > vert_coupled_node
SWMM node index (-1 = none)
Definition MeshData.hpp:154
std::vector< double > tri_init_u
[2D_INITIAL_VELOCITY] u (m/s, default 0)
Definition MeshData.hpp:138
int n_cells() const noexcept
Definition MeshData.hpp:199
std::vector< double > tri_init_depth
Initial water depth (m, default 0 = dry)
Definition MeshData.hpp:137
std::vector< double > vert_coupling_area
Definition MeshData.hpp:159
std::vector< double > tri_cx
Centroid X (area centroid)
Definition MeshData.hpp:95
std::vector< std::string > tri_tag
Optional triangle tag.
Definition MeshData.hpp:140
std::vector< uint8_t > cell_nv
Vertices per cell: 3 (triangle) or 4 (quadrilateral).
Definition MeshData.hpp:83
void set_quad(int c, int v0, int v1, int v2, int v3) noexcept
Definition MeshData.hpp:243
std::vector< int > cell_v
Definition MeshData.hpp:88
int n_edge_slots() const noexcept
Number of internal edge slots (n_cells * kMaxCellVerts).
Definition MeshData.hpp:212
static constexpr int slot_cell(int s) noexcept
Definition MeshData.hpp:222
std::vector< double > edge_my
Edge midpoint Y.
Definition MeshData.hpp:104
int cell_vertex_count(int c) const noexcept
Definition MeshData.hpp:224
std::vector< int > vert_stencil_ptr
[n_vertices + 1] row pointers
Definition MeshData.hpp:146
std::vector< double > vz
Vertex Z (ground elevation)
Definition MeshData.hpp:75
std::vector< std::string > vert_coupled_node_name
Definition MeshData.hpp:168
std::vector< double > edge_ny
Outward normal Y component.
Definition MeshData.hpp:102
std::vector< double > tri_cz
Mean of the cell's vertex elevations.
Definition MeshData.hpp:97
static constexpr int slot_local(int s) noexcept
Local index k of a flat slot; cell of a flat slot.
Definition MeshData.hpp:221
std::vector< double > quad_vfr_a
Definition MeshData.hpp:118
std::vector< double > mannings_n
Manning's roughness coefficient.
Definition MeshData.hpp:136
std::vector< double > vx
Vertex X coordinate.
Definition MeshData.hpp:73
int edge_stride() const noexcept
Definition MeshData.hpp:210
std::vector< double > edge_mx
Edge midpoint X.
Definition MeshData.hpp:103
std::vector< double > vert_stencil_wt
Pseudo-Laplacian weights.
Definition MeshData.hpp:148
int n_vertices() const noexcept
Definition MeshData.hpp:195
std::vector< double > tri_coupling_cd
Discharge coefficient.
Definition MeshData.hpp:164
void resize_vertices(int nv)
Definition MeshData.hpp:255
std::vector< double > edge_mz
Definition MeshData.hpp:105
std::vector< double > tri_init_v
[2D_INITIAL_VELOCITY] v (m/s, default 0)
Definition MeshData.hpp:139
std::vector< double > edge_dist_c
Definition MeshData.hpp:110
int cell_neighbour(int c, int k) const noexcept
Definition MeshData.hpp:226
std::vector< TriCouplingRow > tri_couplings
Definition MeshData.hpp:189
bool is_quad(int c) const noexcept
Definition MeshData.hpp:227
std::vector< int > cell_nbr
Definition MeshData.hpp:91
std::vector< double > quad_vfr_z
Definition MeshData.hpp:117
std::vector< double > vert_coupling_cd
Discharge coefficient (default 0.65)
Definition MeshData.hpp:158