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 {
55class StructureSolver;
56}
57
58namespace kinwave {
59
60// ============================================================================
61// Tree-layout routing helpers — shared by KINWAVE and STEADY
62// ============================================================================
63//
64// Legacy keeps these in flowrout.c because Steady Flow and Kinematic Wave run
65// the SAME outer loop (flowrout_execute) over a topologically sorted link
66// array; only the per-conduit kernel differs. They live here so the KW loop
67// and Router::executeSteadyFlow share one implementation.
68
92double getLinkInflow(SimulationContext& ctx, hydstruct::StructureSolver* structures, int j,
93 double dt);
94
115void updateStorageState(SimulationContext& ctx, hydstruct::StructureSolver* structures,
116 const std::vector<int>& order, int pos, int i, double dt);
117
138void finishRouting(SimulationContext& ctx, hydstruct::StructureSolver* structures,
139 const std::vector<int>& order, const std::vector<char>& storage_updated,
140 const std::vector<double>& link_y1, const std::vector<double>& link_y2,
141 double dt);
142
156double capConduitLoss(SimulationContext& ctx, std::size_t ucr, double q);
157
158// ============================================================================
159// Constants (matching legacy)
160// ============================================================================
161
162constexpr double WX = 0.6;
163constexpr double WT = 0.6;
164constexpr double EPSIL = 0.001;
165
166// ============================================================================
167// KW solver — operates on entire conduit set
168// ============================================================================
169
176class KWSolver {
177public:
189 void init(int n_conduits, const XSectGroups& groups);
190
200 void setLinkOrder(const std::vector<int>& sorted_links) { sorted_links_ = sorted_links; }
201
218 int execute(SimulationContext& ctx, double dt,
219 hydstruct::StructureSolver* structures = nullptr);
220
222 std::vector<int> sorted_links_;
223
226 std::vector<char> storage_updated_;
227
253 int solveConduit(int idx, const XSectParams& xs, double q_full, double a_full, double s_full,
254 double beta, double length, double dt, double loss_rate);
255
256 // Per-conduit SoA state (indexed by conduit-link index)
257 // Public so that unit tests can set up / inspect working arrays directly.
258 std::vector<double> q1_;
259 std::vector<double> a1_;
260 std::vector<double> q2_;
261 std::vector<double> a2_;
262
263 // Working buffers (reused each timestep)
264 std::vector<double> q_in_;
265 std::vector<double> a_in_;
266 std::vector<double> q_out_;
267 std::vector<double> a_out_;
268 std::vector<double> sf_in_;
269 std::vector<double> y1_;
270 std::vector<double> y2_;
271
272private:
273 int n_conduits_ = 0;
274};
275
276} // namespace kinwave
277} // namespace openswmm
278
279#endif // OPENSWMM_KINEMATIC_WAVE_HPP
Cross-section geometry — unified batch + per-element API.
Shape-grouped cross-section manager for batch computation.
Definition XSectBatch.hpp:447
Definition HydStructures.hpp:201
Kinematic wave solver state.
Definition KinematicWave.hpp:176
void setLinkOrder(const std::vector< int > &sorted_links)
Copy the topologically sorted global link order used for tree routing.
Definition KinematicWave.hpp:200
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)
Solve one native per-barrel KW continuity update with safeguarded Newton.
Definition KinematicWave.cpp:252
std::vector< double > q_in_
Inflow to each conduit (cfs)
Definition KinematicWave.hpp:264
std::vector< double > a_in_
Inlet area from inflow (ft2)
Definition KinematicWave.hpp:265
std::vector< double > y2_
End-of-step downstream flow depth (ft)
Definition KinematicWave.hpp:270
std::vector< double > a_out_
Outlet area from Newton solve (ft2)
Definition KinematicWave.hpp:267
std::vector< double > a2_
Previous outlet area (ft2)
Definition KinematicWave.hpp:261
int execute(SimulationContext &ctx, double dt, hydstruct::StructureSolver *structures=nullptr)
Route one tree-layout kinematic-wave step and update node/link native state.
Definition KinematicWave.cpp:448
std::vector< double > a1_
Previous inlet area (ft2)
Definition KinematicWave.hpp:259
std::vector< double > sf_in_
Section factor at inlet.
Definition KinematicWave.hpp:268
std::vector< double > q2_
Previous outlet flow (cfs)
Definition KinematicWave.hpp:260
std::vector< int > sorted_links_
Topological link order (upstream → downstream)
Definition KinematicWave.hpp:222
std::vector< double > q1_
Previous inlet flow (cfs)
Definition KinematicWave.hpp:258
std::vector< double > y1_
End-of-step upstream flow depth (ft)
Definition KinematicWave.hpp:269
std::vector< double > q_out_
Computed outflow (cfs)
Definition KinematicWave.hpp:266
void init(int n_conduits, const XSectGroups &groups)
Allocate/reset kinematic-wave history and work vectors by global link slot.
Definition KinematicWave.cpp:230
std::vector< char > storage_updated_
Definition KinematicWave.hpp:226
Definition HydStructures.cpp:48
Definition KinematicWave.cpp:49
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:622
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:162
constexpr double WX
Distance weighting factor.
Definition KinematicWave.hpp:162
double capConduitLoss(SimulationContext &ctx, std::size_t ucr, double q)
Limit positive conduit loss components by available tree-routing inflow.
Definition KinematicWave.cpp:750
constexpr double EPSIL
Newton convergence tolerance.
Definition KinematicWave.hpp:164
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:109
constexpr double WT
Time weighting factor.
Definition KinematicWave.hpp:163
Definition ExchangeLaw.hpp:60
Central, reentrant simulation context.
Definition SimulationContext.hpp:390
Native cross-section geometry and borrowed optional normalized transect tables.
Definition XSectBatch.hpp:119