OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
SurfaceQuality2D.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
72
73#ifndef OPENSWMM_ENGINE_2D_QUALITY_SURFACE_QUALITY_2D_HPP
74#define OPENSWMM_ENGINE_2D_QUALITY_SURFACE_QUALITY_2D_HPP
75
76#include <cstddef>
77#include <cstdint>
78#include <string>
79#include <utility>
80#include <vector>
81
83
84namespace openswmm {
86}
87
88namespace openswmm::twoD {
89
90struct MeshData;
91struct SurfaceStateData;
93struct SolverOptions2D;
94
96enum class SqScope : int8_t { GLOBAL = 0, TAG = 1, CELL = 2 };
97
98struct SqScopeKey {
100 std::string tag;
101 int cell = -1;
102};
103
107 std::vector<std::pair<std::string, double>> uses;
108};
109
112 std::string species;
113 double value = 0.0;
114};
115
116struct CurbRow2D {
118 double length = 0.0;
119};
120
126public:
127 // ---- authoring (section handlers, C API, writer) -----------------------
128 std::vector<CoverageRow2D> coverage_rows;
129 std::vector<LoadingRow2D> loading_rows;
130 std::vector<CurbRow2D> curb_rows;
136 bool authored() const noexcept {
137 return !coverage_rows.empty() || !loading_rows.empty() || !curb_rows.empty();
138 }
139
145 void clearRows() {
146 coverage_rows.clear();
147 loading_rows.clear();
148 curb_rows.clear();
149 }
150
173 std::vector<std::string> resolve(const SimulationContext& ctx, const MeshData& mesh,
174 const SurfaceTransportState& tr, const SolverOptions2D& opts);
175
182 bool active() const noexcept { return active_; }
183
198 void initState(const SimulationContext& ctx, const MeshData& mesh);
199
221 void step(const SimulationContext& ctx, const MeshData& mesh, SurfaceStateData& state,
222 double dt_runoff, double abs_time, bool is_raining);
223
224 // ---- read-back --------------------------------------------------------
231 int nLandUses() const noexcept { return n_lu_; }
238 int nSpecies() const noexcept {
239 return n_sp_;
240 }
241
248 int nCells() const noexcept { return n_cells_; }
257 int rowOf(int s) const noexcept { return row_of_[static_cast<std::size_t>(s)]; }
265 const std::string& speciesName(int s) const noexcept {
266 return names_[static_cast<std::size_t>(s)];
267 }
268
275 double coverage(int c, int lu) const noexcept { return cov_[cidx(c, lu)]; }
283 double curbLength(int c) const noexcept { return curb_[static_cast<std::size_t>(c)]; }
295 double buildup(int c, int lu, int s) const noexcept { return bu_[bidx(c, lu, s)]; }
306 double buildupPerArea(int c, int s) const noexcept;
316 const std::vector<double>& ledInitBuildup() const noexcept { return led_init_; }
325 const std::vector<double>& ledBuildup() const noexcept { return led_bu_; }
334 const std::vector<double>& ledWashoff() const noexcept { return led_wo_; }
342 const std::vector<double>& ledSweeping() const noexcept { return led_sw_; }
350 const std::vector<double>& ledBmp() const noexcept { return led_bmp_; }
361 double mcf(int s) const noexcept { return mcf_[static_cast<std::size_t>(s)]; }
372 double runoffVolume(int c) const noexcept { return runoff_vol_[static_cast<std::size_t>(c)]; }
383 double wetRunoffVolume(int c) const noexcept {
384 return wet_runoff_vol_[static_cast<std::size_t>(c)];
385 }
386
387private:
395 std::size_t cidx(int c, int lu) const noexcept {
396 return static_cast<std::size_t>(c) * static_cast<std::size_t>(n_lu_) +
397 static_cast<std::size_t>(lu);
398 }
407 std::size_t bidx(int c, int lu, int s) const noexcept {
408 return (static_cast<std::size_t>(lu) * static_cast<std::size_t>(n_sp_) +
409 static_cast<std::size_t>(s)) *
410 static_cast<std::size_t>(n_cells_) +
411 static_cast<std::size_t>(c);
412 }
413 struct Params { // per (lu, s), read from the 1D tables at resolve
414 int bu_type = 0;
415 double c0 = 0, c1 = 0, c2 = 0;
416 int ts_idx = -1;
417 int normalizer = 0;
418 double max_days = 0;
419 int wo_type = 0;
420 double coeff = 0, expon = 0, sweep_effic = 0, bmp_effic = 0;
421 };
429 const Params& param(int lu, int s) const noexcept {
430 return params_[static_cast<std::size_t>(lu) * static_cast<std::size_t>(n_sp_) +
431 static_cast<std::size_t>(s)];
432 }
444 void loadParams(const SimulationContext& ctx);
445
446 bool active_ = false;
447 int n_cells_ = 0, n_lu_ = 0, n_sp_ = 0;
448 bool si_ = false;
449 double area_to_landarea_ = 1.0;
450 std::vector<int> row_of_;
451 std::vector<std::string> names_;
452 std::vector<double> mcf_;
453 std::vector<Params> params_;
454 std::vector<double> cov_;
455 std::vector<double> curb_;
456 std::vector<double> area_la_;
457 std::vector<double> init_;
458 std::vector<double> bu_;
459 std::vector<double> last_swept_;
460 std::vector<double> runoff_vol_;
461 std::vector<double> wet_runoff_vol_;
462 std::vector<double> led_init_, led_bu_, led_wo_, led_sw_, led_bmp_;
463};
464
465// ---- section parsers (exposed for tests) -----------------------------------
478std::string parse2DCoveragesLine(const std::vector<std::string>& tokens,
479 std::vector<CoverageRow2D>& rows);
492std::string parse2DLoadingsLine(const std::vector<std::string>& tokens,
493 std::vector<LoadingRow2D>& rows);
506std::string parse2DCurbLengthLine(const std::vector<std::string>& tokens,
507 std::vector<CurbRow2D>& rows);
508
520void registerSurfaceQualitySections(SurfaceQuality2D& sq, input::SectionRegistry& registry);
521
530std::string formatSqScope(const SqScopeKey& k);
544std::string parseSqScope(const std::vector<std::string>& tokens, std::size_t& at, SqScopeKey& key,
545 const char* section);
546
547} // namespace openswmm::twoD
548
549#endif // OPENSWMM_ENGINE_2D_QUALITY_SURFACE_QUALITY_2D_HPP
Registry that maps SWMM section tags to handler functions.
The S7 store: authored rows, resolved per-cell coverages and state, and the per-species ledgers.
Definition SurfaceQuality2D.hpp:125
const std::string & speciesName(int s) const noexcept
Read resolved per-species metadata.
Definition SurfaceQuality2D.hpp:265
std::vector< std::string > resolve(const SimulationContext &ctx, const MeshData &mesh, const SurfaceTransportState &tr, const SolverOptions2D &opts)
Bind the rows to the mesh, the land uses and the transport rows.
Definition SurfaceQuality2D.cpp:323
double runoffVolume(int c) const noexcept
Cumulative runoff volume (m³) the cell produced (the positive part of its net outflow per step,...
Definition SurfaceQuality2D.hpp:372
int nSpecies() const noexcept
Read the resolved pollutant plus MSX buildup row count, excluding age/temperature.
Definition SurfaceQuality2D.hpp:238
void initState(const SimulationContext &ctx, const MeshData &mesh)
Seed initial buildup: [2D_LOADINGS] row, else the buildup function at [OPTIONS] DRY_DAYS (legacy land...
Definition SurfaceQuality2D.cpp:587
double buildupPerArea(int c, int s) const noexcept
Total buildup of species s on cell c per unit land area (user mass / acre or ha) — what the results f...
Definition SurfaceQuality2D.cpp:620
const std::vector< double > & ledWashoff() const noexcept
Borrow cumulative net washoff delivered to surface transport after BMP per surface species.
Definition SurfaceQuality2D.hpp:334
double wetRunoffVolume(int c) const noexcept
…of which on steps fast enough to wash off (legacy MIN_RUNOFF): the volume an EMC washes off at its c...
Definition SurfaceQuality2D.hpp:383
int nLandUses() const noexcept
Read the resolved land-use count.
Definition SurfaceQuality2D.hpp:231
std::vector< LoadingRow2D > loading_rows
Definition SurfaceQuality2D.hpp:129
double buildup(int c, int lu, int s) const noexcept
Buildup of species s on land use lu of cell c, user mass per normalizer unit.
Definition SurfaceQuality2D.hpp:295
int nCells() const noexcept
< surface species (rows that build up)
Definition SurfaceQuality2D.hpp:248
int rowOf(int s) const noexcept
Transport row index of surface species s (pollutants first, then MSX).
Definition SurfaceQuality2D.hpp:257
void step(const SimulationContext &ctx, const MeshData &mesh, SurfaceStateData &state, double dt_runoff, double abs_time, bool is_raining)
One runoff step: buildup accrual, washoff into the cell rows, street sweeping.
Definition SurfaceQuality2D.cpp:639
double curbLength(int c) const noexcept
Read resolved curb normalization length.
Definition SurfaceQuality2D.hpp:283
const std::vector< double > & ledBmp() const noexcept
Borrow cumulative washoff removed by BMP treatment per surface species.
Definition SurfaceQuality2D.hpp:350
const std::vector< double > & ledSweeping() const noexcept
Borrow cumulative buildup removed by sweeping per surface species.
Definition SurfaceQuality2D.hpp:342
const std::vector< double > & ledInitBuildup() const noexcept
Per-species ledgers, user mass, cumulative.
Definition SurfaceQuality2D.hpp:316
bool active() const noexcept
Read whether the last resolution activated mesh surface quality.
Definition SurfaceQuality2D.hpp:182
std::vector< CoverageRow2D > coverage_rows
Definition SurfaceQuality2D.hpp:128
const std::vector< double > & ledBuildup() const noexcept
Borrow cumulative accrued buildup, including generated storeless-washoff supply per surface species.
Definition SurfaceQuality2D.hpp:325
double mcf(int s) const noexcept
Per-species conversion: concentration-mass units → user mass (lbs / kg).
Definition SurfaceQuality2D.hpp:361
void clearRows()
Clear all authored coverage, loading and curb-length rows.
Definition SurfaceQuality2D.hpp:145
bool authored() const noexcept
Test whether any coverage, loading or curb authoring row exists.
Definition SurfaceQuality2D.hpp:136
double coverage(int c, int lu) const noexcept
Read one resolved cell/land-use coverage fraction.
Definition SurfaceQuality2D.hpp:275
std::vector< CurbRow2D > curb_rows
Definition SurfaceQuality2D.hpp:130
Definition ExchangeLaw.hpp:60
std::string parseSqScope(const std::vector< std::string > &tokens, std::size_t &at, SqScopeKey &key, const char *section)
Parse a scoped surface-quality row prefix and expose its first payload token.
Definition SurfaceQuality2D.cpp:143
@ CELL
Definition SolverOptions2D.hpp:143
@ GLOBAL
the * row
Definition GwTransportData.hpp:66
@ TAG
every cell carrying tag
Definition GwTransportData.hpp:67
std::string parse2DCurbLengthLine(const std::vector< std::string > &tokens, std::vector< CurbRow2D > &rows)
Parse one scoped 2D curb authoring row.
Definition SurfaceQuality2D.cpp:222
SqScope
Row scope — the [2D_INFILTRATION] / [GW_*] ladder.
Definition SurfaceQuality2D.hpp:96
@ GLOBAL
Definition SurfaceQuality2D.hpp:96
std::string parse2DCoveragesLine(const std::vector< std::string > &tokens, std::vector< CoverageRow2D > &rows)
Parse one scoped 2D coverage authoring row.
Definition SurfaceQuality2D.cpp:174
std::string parse2DLoadingsLine(const std::vector< std::string > &tokens, std::vector< LoadingRow2D > &rows)
Parse one scoped 2D loading authoring row.
Definition SurfaceQuality2D.cpp:206
void registerSurfaceQualitySections(SurfaceQuality2D &sq, input::SectionRegistry &registry)
Register [2D_COVERAGES], [2D_LOADINGS], [2D_CURB_LENGTH] writing into sq.
Definition SurfaceQuality2D.cpp:237
std::string formatSqScope(const SqScopeKey &k)
Scope key ↔ file token ("*", "TAG name", "CELL n" — 1-based).
Definition SurfaceQuality2D.cpp:132
Definition ExchangeLaw.hpp:60
Central, reentrant simulation context.
Definition SimulationContext.hpp:390
[2D_COVERAGES] — one row = the whole coverage set of its scope.
Definition SurfaceQuality2D.hpp:105
SqScopeKey key
Definition SurfaceQuality2D.hpp:106
std::vector< std::pair< std::string, double > > uses
(land use, percent)
Definition SurfaceQuality2D.hpp:107
[2D_CURB_LENGTH] — curb length per cell, project length units.
Definition SurfaceQuality2D.hpp:116
SqScopeKey key
Definition SurfaceQuality2D.hpp:117
double length
Definition SurfaceQuality2D.hpp:118
[2D_LOADINGS] — initial buildup, user mass per unit land area.
Definition SurfaceQuality2D.hpp:110
std::string species
Definition SurfaceQuality2D.hpp:112
double value
Definition SurfaceQuality2D.hpp:113
SqScopeKey key
Definition SurfaceQuality2D.hpp:111
SoA storage for 2D mixed triangle/quad mesh geometry and topology.
Definition MeshData.hpp:65
Definition SolverOptions2D.hpp:257
Definition SurfaceQuality2D.hpp:98
int cell
CELL rows, 0-based internally (1-based in the file)
Definition SurfaceQuality2D.hpp:101
std::string tag
TAG rows.
Definition SurfaceQuality2D.hpp:100
SqScope scope
Definition SurfaceQuality2D.hpp:99
Definition SurfaceStateData.hpp:64
Definition SurfaceTransportState.hpp:70