OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
KinematicWave.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
43
44#ifndef OPENSWMM_KINEMATIC_WAVE_HPP
45#define OPENSWMM_KINEMATIC_WAVE_HPP
46
47#include "XSectBatch.hpp"
48#include <vector>
49
50namespace openswmm {
51
53
54namespace hydstruct { class StructureSolver; }
55
56namespace kinwave {
57
58// ============================================================================
59// Tree-layout routing helpers — shared by KINWAVE and STEADY
60// ============================================================================
61//
62// Legacy keeps these in flowrout.c because Steady Flow and Kinematic Wave run
63// the SAME outer loop (flowrout_execute) over a topologically sorted link
64// array; only the per-conduit kernel differs. They live here so the KW loop
65// and Router::executeSteadyFlow share one implementation.
66
90double getLinkInflow(SimulationContext& ctx,
91 hydstruct::StructureSolver* structures,
92 int j, double dt);
93
114void updateStorageState(SimulationContext& ctx,
115 hydstruct::StructureSolver* structures,
116 const std::vector<int>& order,
117 int pos, int i, double dt);
118
139void finishRouting(SimulationContext& ctx,
140 hydstruct::StructureSolver* structures,
141 const std::vector<int>& order,
142 const std::vector<char>& storage_updated,
143 const std::vector<double>& link_y1,
144 const std::vector<double>& link_y2,
145 double dt);
146
147// ============================================================================
148// Constants (matching legacy)
149// ============================================================================
150
151constexpr double WX = 0.6;
152constexpr double WT = 0.6;
153constexpr double EPSIL = 0.001;
154
155// ============================================================================
156// KW solver — operates on entire conduit set
157// ============================================================================
158
165class KWSolver {
166public:
169 void init(int n_conduits, const XSectGroups& groups);
170
172 void setLinkOrder(const std::vector<int>& sorted_links) {
173 sorted_links_ = sorted_links;
174 }
175
189 int execute(SimulationContext& ctx, double dt,
190 hydstruct::StructureSolver* structures = nullptr);
191
193 std::vector<int> sorted_links_;
194
197 std::vector<char> storage_updated_;
198
200 int solveConduit(int idx, const XSectParams& xs,
201 double q_full, double a_full, double s_full,
202 double beta, double length, double dt,
203 double loss_rate);
204
205 // Per-conduit SoA state (indexed by conduit-link index)
206 // Public so that unit tests can set up / inspect working arrays directly.
207 std::vector<double> q1_;
208 std::vector<double> a1_;
209 std::vector<double> q2_;
210 std::vector<double> a2_;
211
212 // Working buffers (reused each timestep)
213 std::vector<double> q_in_;
214 std::vector<double> a_in_;
215 std::vector<double> q_out_;
216 std::vector<double> a_out_;
217 std::vector<double> sf_in_;
218 std::vector<double> y1_;
219 std::vector<double> y2_;
220
221private:
222 int n_conduits_ = 0;
223};
224
225} // namespace kinwave
226} // namespace openswmm
227
228#endif // OPENSWMM_KINEMATIC_WAVE_HPP
Cross-section geometry — unified batch + per-element API.
Shape-grouped cross-section manager for batch computation.
Definition XSectBatch.hpp:243
Definition HydStructures.hpp:170
Kinematic wave solver state.
Definition KinematicWave.hpp:165
void setLinkOrder(const std::vector< int > &sorted_links)
Set topological link order (must be called after init, before first execute).
Definition KinematicWave.hpp:172
int solveConduit(int idx, const XSectParams &xs, double q_full, double a_full, double s_full, double beta, double length, double dt, double loss_rate)
Per-conduit Newton solve. Returns iteration count.
Definition KinematicWave.cpp:215
std::vector< double > q_in_
Inflow to each conduit (cfs)
Definition KinematicWave.hpp:213
std::vector< double > a_in_
Inlet area from inflow (ft2)
Definition KinematicWave.hpp:214
std::vector< double > y2_
End-of-step downstream flow depth (ft)
Definition KinematicWave.hpp:219
std::vector< double > a_out_
Outlet area from Newton solve (ft2)
Definition KinematicWave.hpp:216
std::vector< double > a2_
Previous outlet area (ft2)
Definition KinematicWave.hpp:210
int execute(SimulationContext &ctx, double dt, hydstruct::StructureSolver *structures=nullptr)
Route all conduits for one KW timestep.
Definition KinematicWave.cpp:404
std::vector< double > a1_
Previous inlet area (ft2)
Definition KinematicWave.hpp:208
std::vector< double > sf_in_
Section factor at inlet.
Definition KinematicWave.hpp:217
std::vector< double > q2_
Previous outlet flow (cfs)
Definition KinematicWave.hpp:209
std::vector< int > sorted_links_
Topological link order (upstream → downstream)
Definition KinematicWave.hpp:193
std::vector< double > q1_
Previous inlet flow (cfs)
Definition KinematicWave.hpp:207
std::vector< double > y1_
End-of-step upstream flow depth (ft)
Definition KinematicWave.hpp:218
std::vector< double > q_out_
Computed outflow (cfs)
Definition KinematicWave.hpp:215
void init(int n_conduits, const XSectGroups &groups)
Definition KinematicWave.cpp:193
std::vector< char > storage_updated_
Definition KinematicWave.hpp:197
Definition HydStructures.cpp:46
Definition KinematicWave.cpp:47
void finishRouting(SimulationContext &ctx, hydstruct::StructureSolver *structures, const std::vector< int > &order, const std::vector< char > &storage_updated, const std::vector< double > &link_y1, const std::vector< double > &link_y2, double dt)
End-of-step node/link state for tree-layout routing (KW/steady).
Definition KinematicWave.cpp:560
void updateStorageState(SimulationContext &ctx, hydstruct::StructureSolver *structures, const std::vector< int > &order, int pos, int i, double dt)
Update a storage node's depth & volume by successive approximation.
Definition KinematicWave.cpp:130
constexpr double WX
Distance weighting factor.
Definition KinematicWave.hpp:151
constexpr double EPSIL
Newton convergence tolerance.
Definition KinematicWave.hpp:153
double getLinkInflow(SimulationContext &ctx, hydstruct::StructureSolver *structures, int j, double dt)
Flow into the upstream end of a link under Steady/Kin. Wave routing.
Definition KinematicWave.cpp:84
constexpr double WT
Time weighting factor.
Definition KinematicWave.hpp:152
Definition NodeCoupling.cpp:16
Central, reentrant simulation context.
Definition SimulationContext.hpp:353
Definition XSectBatch.hpp:110