OpenSWMM Engine  6.0.0-alpha.1
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.1)
Loading...
Searching...
No Matches
SurfaceStateData.hpp
Go to the documentation of this file.
1
16#ifndef OPENSWMM_ENGINE_2D_SURFACE_STATE_DATA_HPP
17#define OPENSWMM_ENGINE_2D_SURFACE_STATE_DATA_HPP
18
19#include <vector>
20#include <cstring>
21#include <algorithm>
22
23namespace openswmm::twoD {
24
33
34 // -----------------------------------------------------------------------
35 // State variables — per triangle [0, n_triangles)
36 // -----------------------------------------------------------------------
37
38 std::vector<double> depth;
39 std::vector<double> head;
40
41 // Gradient fields (per triangle)
42 std::vector<double> grad_hx;
43 std::vector<double> grad_hy;
44 std::vector<double> grad_hx_lim;
45 std::vector<double> grad_hy_lim;
46
47 // Reconstructed head at vertices — [0, n_vertices)
48 std::vector<double> vert_head;
49
50 // Fluxes — flat 2D: [tri * 3 + edge]
51 std::vector<double> edge_flux;
52
53 // Source/sink terms — per triangle
54 std::vector<double> rainfall;
55 std::vector<double> coupling_flux;
56 std::vector<double> net_source;
57
58 // -----------------------------------------------------------------------
59 // Forcing overrides (optional external control)
60 // -----------------------------------------------------------------------
61
62 std::vector<int8_t> rainfall_forced;
63 std::vector<int8_t> rainfall_persist;
64 std::vector<double> rainfall_force_val;
65 std::vector<int8_t> coupling_forced;
66 std::vector<int8_t> coupling_persist;
67 std::vector<double> coupling_force_val;
68
69 // -----------------------------------------------------------------------
70 // Previous step state
71 // -----------------------------------------------------------------------
72
73 std::vector<double> old_depth;
74
75 // -----------------------------------------------------------------------
76 // Cumulative statistics
77 // -----------------------------------------------------------------------
78
79 std::vector<double> stat_max_depth;
80 std::vector<double> stat_cum_volume;
81
82 // -----------------------------------------------------------------------
83 // Lifecycle
84 // -----------------------------------------------------------------------
85
86 void resize(int n_triangles, int n_vertices) {
87 auto nt = static_cast<std::size_t>(n_triangles);
88 auto nv = static_cast<std::size_t>(n_vertices);
89 auto n3 = nt * 3;
90
91 depth.assign(nt, 0.0);
92 head.assign(nt, 0.0);
93 grad_hx.assign(nt, 0.0);
94 grad_hy.assign(nt, 0.0);
95 grad_hx_lim.assign(nt, 0.0);
96 grad_hy_lim.assign(nt, 0.0);
97 vert_head.assign(nv, 0.0);
98 edge_flux.assign(n3, 0.0);
99 rainfall.assign(nt, 0.0);
100 coupling_flux.assign(nt, 0.0);
101 net_source.assign(nt, 0.0);
102
103 rainfall_forced.assign(nt, 0);
104 rainfall_persist.assign(nt, 0);
105 rainfall_force_val.assign(nt, 0.0);
106 coupling_forced.assign(nt, 0);
107 coupling_persist.assign(nt, 0);
108 coupling_force_val.assign(nt, 0.0);
109
110 old_depth.assign(nt, 0.0);
111 stat_max_depth.assign(nt, 0.0);
112 stat_cum_volume.assign(nt, 0.0);
113 }
114
115 void save_state() noexcept {
116 std::memcpy(old_depth.data(), depth.data(),
117 depth.size() * sizeof(double));
118 }
119
120 void reset_state() noexcept {
121 std::memcpy(depth.data(), old_depth.data(),
122 old_depth.size() * sizeof(double));
123 }
124
126 void clear_reset_forcings() noexcept {
127 for (std::size_t i = 0; i < rainfall_forced.size(); ++i) {
128 if (rainfall_persist[i] == 0) {
129 rainfall_forced[i] = 0;
130 rainfall_force_val[i] = 0.0;
131 }
132 if (coupling_persist[i] == 0) {
133 coupling_forced[i] = 0;
134 coupling_force_val[i] = 0.0;
135 }
136 }
137 }
138
140 void update_statistics(const std::vector<double>& tri_area,
141 double dt) noexcept {
142 for (std::size_t i = 0; i < depth.size(); ++i) {
143 if (depth[i] > stat_max_depth[i])
144 stat_max_depth[i] = depth[i];
145 stat_cum_volume[i] += depth[i] * tri_area[i] * dt;
146 }
147 }
148};
149
150} // namespace openswmm::twoD
151
152#endif // OPENSWMM_ENGINE_2D_SURFACE_STATE_DATA_HPP
Definition NodeCoupling.cpp:15
SoA storage for 2D surface routing state variables.
Definition SurfaceStateData.hpp:32
std::vector< double > grad_hy_lim
Limited gradient Y.
Definition SurfaceStateData.hpp:45
std::vector< double > grad_hy
∂h/∂y (unlimited gradient)
Definition SurfaceStateData.hpp:43
void save_state() noexcept
Definition SurfaceStateData.hpp:115
std::vector< int8_t > coupling_forced
0=computed, 1=override, 2=add
Definition SurfaceStateData.hpp:65
std::vector< double > coupling_flux
Exchange with SWMM node (m/s, + = into 2D)
Definition SurfaceStateData.hpp:55
std::vector< int8_t > rainfall_forced
0=computed, 1=override, 2=add
Definition SurfaceStateData.hpp:62
std::vector< double > stat_max_depth
Maximum depth seen at each cell.
Definition SurfaceStateData.hpp:79
std::vector< double > net_source
Net source/sink per cell (m/s)
Definition SurfaceStateData.hpp:56
std::vector< double > rainfall_force_val
Forced rainfall value.
Definition SurfaceStateData.hpp:64
std::vector< double > stat_cum_volume
Cumulative volume through cell.
Definition SurfaceStateData.hpp:80
void clear_reset_forcings() noexcept
Clear RESET forcings after each step.
Definition SurfaceStateData.hpp:126
std::vector< double > depth
Overland flow depth ψ_o (m)
Definition SurfaceStateData.hpp:38
void update_statistics(const std::vector< double > &tri_area, double dt) noexcept
Update cumulative statistics.
Definition SurfaceStateData.hpp:140
std::vector< double > grad_hx
∂h/∂x (unlimited gradient)
Definition SurfaceStateData.hpp:42
std::vector< double > rainfall
Rainfall intensity (m/s)
Definition SurfaceStateData.hpp:54
std::vector< double > coupling_force_val
Forced coupling value.
Definition SurfaceStateData.hpp:67
std::vector< double > vert_head
Head reconstructed at vertices.
Definition SurfaceStateData.hpp:48
void reset_state() noexcept
Definition SurfaceStateData.hpp:120
std::vector< double > head
Total head h_o = z_s + ψ_o (m)
Definition SurfaceStateData.hpp:39
std::vector< double > grad_hx_lim
Limited gradient X.
Definition SurfaceStateData.hpp:44
std::vector< int8_t > coupling_persist
0=reset, 1=persist
Definition SurfaceStateData.hpp:66
void resize(int n_triangles, int n_vertices)
Definition SurfaceStateData.hpp:86
std::vector< double > old_depth
Depth at start of coupling interval.
Definition SurfaceStateData.hpp:73
std::vector< int8_t > rainfall_persist
0=reset, 1=persist
Definition SurfaceStateData.hpp:63
std::vector< double > edge_flux
Normal flux through each edge.
Definition SurfaceStateData.hpp:51