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
65struct MeshData {
66
67 // -----------------------------------------------------------------------
68 // Vertex arrays — indexed by vertex index [0, n_vertices)
69 // -----------------------------------------------------------------------
70
71 std::vector<double> vx;
72 std::vector<double> vy;
73 std::vector<double> vz;
74 std::vector<std::string> vtag;
75
76 // -----------------------------------------------------------------------
77 // Cell static properties — indexed by cell index [0, n_triangles())
78 // -----------------------------------------------------------------------
79
81 std::vector<uint8_t> cell_nv;
86 std::vector<int> cell_v;
89 std::vector<int> cell_nbr;
90
91 // Precomputed geometry (per cell)
92 std::vector<double> tri_area;
93 std::vector<double> tri_cx;
94 std::vector<double> tri_cy;
95 std::vector<double> tri_cz;
96
97 // Edge geometry — flat 2D: [cell * kMaxCellVerts + k]
98 std::vector<double> edge_length;
99 std::vector<double> edge_nx;
100 std::vector<double> edge_ny;
101 std::vector<double> edge_mx;
102 std::vector<double> edge_my;
103 std::vector<double> edge_mz;
108 std::vector<double> edge_dist_c;
109
115 std::vector<double> quad_vfr_z;
116 std::vector<double> quad_vfr_a;
117
131 std::vector<double> edge_conveyance;
132
133 // Surface properties
134 std::vector<double> mannings_n;
135 std::vector<double> tri_init_depth;
136 std::vector<double> tri_init_u;
137 std::vector<double> tri_init_v;
138 std::vector<std::string> tri_tag;
139
140 // -----------------------------------------------------------------------
141 // Vertex reconstruction stencil (pseudo-Laplacian weights)
142 // -----------------------------------------------------------------------
143 // Stored as CSR (compressed sparse row) for variable stencil sizes
144 std::vector<int> vert_stencil_ptr;
145 std::vector<int> vert_stencil_idx;
146 std::vector<double> vert_stencil_wt;
147
148 // -----------------------------------------------------------------------
149 // Coupling maps
150 // -----------------------------------------------------------------------
151
152 std::vector<int> vert_coupled_node;
153 std::vector<int> tri_coupled_node;
154
155 // Coupling parameters (per vertex/triangle coupling point)
156 std::vector<double> vert_coupling_cd;
159 std::vector<uint8_t> vert_coupling_cd_set;
160 std::vector<double> vert_coupling_area;
164 std::vector<uint8_t> vert_coupling_area_set;
165 std::vector<double> tri_coupling_cd;
166 std::vector<double> tri_coupling_area;
167
168 // Deferred resolution names (populated during parsing, cleared after resolve)
169 std::vector<std::string> vert_coupled_node_name;
170 std::vector<std::string> tri_coupled_node_name;
171
181 int tri = -1;
182 int node = -1;
183 std::string node_name;
184 double cd = 0.65;
185 bool cd_set = false;
187 double area = 1.0;
188 bool area_set = false;
191 };
192 std::vector<TriCouplingRow> tri_couplings;
193
194 // -----------------------------------------------------------------------
195 // Capacity queries
196 // -----------------------------------------------------------------------
197
204 int n_vertices() const noexcept { return static_cast<int>(vx.size()); }
213 int n_triangles() const noexcept { return static_cast<int>(cell_nv.size()); }
219 int n_cells() const noexcept { return static_cast<int>(cell_nv.size()); }
228 int n_quads() const noexcept {
229 int q = 0;
230 for (uint8_t nv : cell_nv)
231 q += (nv == 4);
232 return q;
233 }
234
244 int edge_stride() const noexcept { return n_quads() > 0 ? kMaxCellVerts : 3; }
252 int n_edge_slots() const noexcept { return n_cells() * kMaxCellVerts; }
253
254 // -----------------------------------------------------------------------
255 // Cell / edge-slot accessors
256 // -----------------------------------------------------------------------
257
267 static constexpr int slot(int c, int k) noexcept { return c * kMaxCellVerts + k; }
276 static constexpr int slot_local(int s) noexcept { return s % kMaxCellVerts; }
283 static constexpr int slot_cell(int s) noexcept { return s / kMaxCellVerts; }
284
292 int cell_vertex_count(int c) const noexcept { return cell_nv[static_cast<std::size_t>(c)]; }
302 int cell_vertex(int c, int k) const noexcept {
303 return cell_v[static_cast<std::size_t>(slot(c, k))];
304 }
305
314 int cell_neighbour(int c, int k) const noexcept {
315 return cell_nbr[static_cast<std::size_t>(slot(c, k))];
316 }
317
324 bool is_quad(int c) const noexcept { return cell_nv[static_cast<std::size_t>(c)] == 4; }
325
336 void cell_edge_vertices(int c, int k, int& a, int& b) const noexcept {
337 const int nv = cell_nv[static_cast<std::size_t>(c)];
338 a = cell_v[static_cast<std::size_t>(slot(c, (k + 1) % nv))];
339 b = cell_v[static_cast<std::size_t>(slot(c, (k + 2) % nv))];
340 }
341
352 void set_triangle(int c, int v0, int v1, int v2) noexcept {
353 cell_nv[static_cast<std::size_t>(c)] = 3;
354 cell_v[static_cast<std::size_t>(slot(c, 0))] = v0;
355 cell_v[static_cast<std::size_t>(slot(c, 1))] = v1;
356 cell_v[static_cast<std::size_t>(slot(c, 2))] = v2;
357 cell_v[static_cast<std::size_t>(slot(c, 3))] = -1;
358 }
359
370 void set_quad(int c, int v0, int v1, int v2, int v3) noexcept {
371 cell_nv[static_cast<std::size_t>(c)] = 4;
372 cell_v[static_cast<std::size_t>(slot(c, 0))] = v0;
373 cell_v[static_cast<std::size_t>(slot(c, 1))] = v1;
374 cell_v[static_cast<std::size_t>(slot(c, 2))] = v2;
375 cell_v[static_cast<std::size_t>(slot(c, 3))] = v3;
376 }
377
378 // -----------------------------------------------------------------------
379 // Resize / allocation
380 // -----------------------------------------------------------------------
381
391 void resize_vertices(int nv) {
392 auto n = static_cast<std::size_t>(nv);
393 vx.resize(n, 0.0);
394 vy.resize(n, 0.0);
395 vz.resize(n, 0.0);
396 vtag.resize(n);
397 vert_coupled_node.resize(n, -1);
398 vert_coupled_node_name.resize(n);
399 vert_coupling_cd.resize(n, 0.65);
400 vert_coupling_cd_set.resize(n, 0);
401 vert_coupling_area.resize(n, 1.0);
402 vert_coupling_area_set.resize(n, 0);
403 }
404
417 void resize_triangles(int nt) {
418 auto n = static_cast<std::size_t>(nt);
419 const auto old_n = cell_nv.size();
420 cell_nv.resize(n, 3);
421 const auto n4 = n * static_cast<std::size_t>(kMaxCellVerts);
422 cell_v.resize(n4, 0);
423 cell_nbr.resize(n4, -1);
424 // Padding slot of every NEW triangle: vertex −1, neighbour −2.
425 for (std::size_t c = old_n; c < n; ++c) {
426 cell_v[c * kMaxCellVerts + 3] = -1;
427 cell_nbr[c * kMaxCellVerts + 3] = -2;
428 }
429 tri_area.resize(n, 0.0);
430 tri_cx.resize(n, 0.0);
431 tri_cy.resize(n, 0.0);
432 tri_cz.resize(n, 0.0);
433
434 edge_length.resize(n4, 0.0);
435 edge_nx.resize(n4, 0.0);
436 edge_ny.resize(n4, 0.0);
437 edge_mx.resize(n4, 0.0);
438 edge_my.resize(n4, 0.0);
439 edge_mz.resize(n4, 0.0);
440 edge_dist_c.resize(n4, 0.0);
441 edge_conveyance.resize(n4, 1.0); // §11A — default unrestricted
442
443 mannings_n.resize(n, 0.035);
444 tri_init_depth.resize(n, 0.0);
445 tri_init_u.resize(n, 0.0);
446 tri_init_v.resize(n, 0.0);
447 tri_tag.resize(n);
448 tri_coupled_node.resize(n, -1);
449 tri_coupled_node_name.resize(n);
450 tri_coupling_cd.resize(n, 0.65);
451 tri_coupling_area.resize(n, 1.0);
452 }
453};
454
455} // namespace openswmm::twoD
456
457#endif // OPENSWMM_ENGINE_2D_MESH_DATA_HPP
Definition ExchangeLaw.hpp:60
constexpr int kMaxCellVerts
Definition MeshData.hpp:49
Definition MeshData.hpp:180
int node
SWMM node index (-1 until resolved)
Definition MeshData.hpp:182
bool area_set
Definition MeshData.hpp:188
bool cd_set
Definition MeshData.hpp:185
std::string node_name
Deferred name (cleared after resolve)
Definition MeshData.hpp:183
double cd
Discharge coefficient.
Definition MeshData.hpp:184
double area
Effective exchange area.
Definition MeshData.hpp:187
int tri
Triangle index.
Definition MeshData.hpp:181
SoA storage for 2D mixed triangle/quad mesh geometry and topology.
Definition MeshData.hpp:65
std::vector< double > tri_coupling_area
Effective exchange area.
Definition MeshData.hpp:166
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
std::vector< uint8_t > vert_coupling_area_set
Definition MeshData.hpp:164
std::vector< int > vert_stencil_idx
Column indices (triangle indices)
Definition MeshData.hpp:145
std::vector< double > tri_area
Planimetric area (m²)
Definition MeshData.hpp:92
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
int n_quads() const noexcept
Number of quadrilateral cells (O(n) scan; not a hot path).
Definition MeshData.hpp:228
std::vector< double > edge_nx
Outward normal X component.
Definition MeshData.hpp:99
std::vector< double > edge_length
Length of each edge.
Definition MeshData.hpp:98
void resize_triangles(int nt)
Resize the per-cell arrays to nt cells. New cells are TRIANGLES (cell_nv = 3, vertex slots 0 / paddin...
Definition MeshData.hpp:417
std::vector< std::string > tri_coupled_node_name
Definition MeshData.hpp:170
std::vector< std::string > vtag
Optional vertex tag.
Definition MeshData.hpp:74
int cell_vertex(int c, int k) const noexcept
Read one mixed-cell topology field.
Definition MeshData.hpp:302
std::vector< double > tri_cy
Centroid Y (area centroid)
Definition MeshData.hpp:94
std::vector< double > vy
Vertex Y coordinate.
Definition MeshData.hpp:72
std::vector< int > tri_coupled_node
SWMM node index (-1 = none)
Definition MeshData.hpp:153
void set_triangle(int c, int v0, int v1, int v2) noexcept
Assign 3-vertex connectivity to one preallocated cell.
Definition MeshData.hpp:352
static constexpr int slot(int c, int k) noexcept
Flat edge/vertex slot of local index k in cell c.
Definition MeshData.hpp:267
std::vector< int > vert_coupled_node
SWMM node index (-1 = none)
Definition MeshData.hpp:152
std::vector< double > tri_init_u
[2D_INITIAL_VELOCITY] u (m/s, default 0)
Definition MeshData.hpp:136
int n_cells() const noexcept
Read the mixed-cell extent.
Definition MeshData.hpp:219
std::vector< double > tri_init_depth
Initial water depth (m, default 0 = dry)
Definition MeshData.hpp:135
std::vector< double > vert_coupling_area
Definition MeshData.hpp:160
std::vector< double > tri_cx
Centroid X (area centroid)
Definition MeshData.hpp:93
std::vector< std::string > tri_tag
Optional triangle tag.
Definition MeshData.hpp:138
std::vector< uint8_t > cell_nv
Vertices per cell: 3 (triangle) or 4 (quadrilateral).
Definition MeshData.hpp:81
void set_quad(int c, int v0, int v1, int v2, int v3) noexcept
Assign 4-vertex connectivity to one preallocated cell.
Definition MeshData.hpp:370
std::vector< int > cell_v
Definition MeshData.hpp:86
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
std::vector< double > edge_my
Edge midpoint Y.
Definition MeshData.hpp:102
int cell_vertex_count(int c) const noexcept
Read one mixed-cell topology field.
Definition MeshData.hpp:292
std::vector< int > vert_stencil_ptr
[n_vertices + 1] row pointers
Definition MeshData.hpp:144
std::vector< double > vz
Vertex Z (ground elevation)
Definition MeshData.hpp:73
std::vector< std::string > vert_coupled_node_name
Definition MeshData.hpp:169
std::vector< double > edge_ny
Outward normal Y component.
Definition MeshData.hpp:100
std::vector< double > tri_cz
Mean of the cell's vertex elevations.
Definition MeshData.hpp:95
static constexpr int slot_local(int s) noexcept
Local index k of a flat slot; cell of a flat slot.
Definition MeshData.hpp:276
std::vector< double > quad_vfr_a
Definition MeshData.hpp:116
std::vector< double > mannings_n
Manning's roughness coefficient.
Definition MeshData.hpp:134
std::vector< double > vx
Vertex X coordinate.
Definition MeshData.hpp:71
int edge_stride() const noexcept
Edge-slot stride of the PUBLIC (API / HDF5) layout: 3 for an all-triangle mesh (byte-compatible with ...
Definition MeshData.hpp:244
std::vector< double > edge_mx
Edge midpoint X.
Definition MeshData.hpp:101
std::vector< double > vert_stencil_wt
Pseudo-Laplacian weights.
Definition MeshData.hpp:146
int n_vertices() const noexcept
Read the vertex-array extent.
Definition MeshData.hpp:204
std::vector< double > tri_coupling_cd
Discharge coefficient.
Definition MeshData.hpp:165
void resize_vertices(int nv)
Resize vertex coordinates, tags and vertex coupling arrays.
Definition MeshData.hpp:391
std::vector< double > edge_mz
Definition MeshData.hpp:103
std::vector< double > tri_init_v
[2D_INITIAL_VELOCITY] v (m/s, default 0)
Definition MeshData.hpp:137
std::vector< double > edge_dist_c
Definition MeshData.hpp:108
int cell_neighbour(int c, int k) const noexcept
Read one mixed-cell topology field.
Definition MeshData.hpp:314
std::vector< TriCouplingRow > tri_couplings
Definition MeshData.hpp:192
bool is_quad(int c) const noexcept
Read one mixed-cell topology field.
Definition MeshData.hpp:324
std::vector< uint8_t > vert_coupling_cd_set
Definition MeshData.hpp:159
std::vector< int > cell_nbr
Definition MeshData.hpp:89
std::vector< double > quad_vfr_z
Definition MeshData.hpp:115
std::vector< double > vert_coupling_cd
Definition MeshData.hpp:156