OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
Groundwater.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
41
42#ifndef OPENSWMM_GROUNDWATER_HPP
43#define OPENSWMM_GROUNDWATER_HPP
44
45#ifndef OPENSWMM_RESTRICT
46#if defined(_MSC_VER)
47#define OPENSWMM_RESTRICT __restrict
48#else
49#define OPENSWMM_RESTRICT __restrict__
50#endif
51#endif
52
53#include <vector>
54#include "../math/MathExpr.hpp"
55
56namespace openswmm {
57
58struct SimulationContext;
59
60namespace groundwater {
61
63enum GWVar : int {
64 GWV_HGW = 0,
65 GWV_HSW = 1,
66 GWV_HCB = 2,
67 GWV_HGS = 3,
68 GWV_KS = 4,
69 GWV_K = 5,
71 GWV_PHI = 7,
72 GWV_FI = 8,
73 GWV_FU = 9,
74 GWV_A = 10,
76};
77
79static const char* GW_VAR_NAMES[] = {"HGW", "HSW", "HCB", "HGS", "KS", "K",
80 "THETA", "PHI", "FI", "FU", "A"};
81
83static const char* GW_VAR_DESCRIPTIONS[] = {
84 "Water table height above aquifer bottom (ft or m)",
85 "Surface-water head at the receiving node above aquifer bottom (ft or m)",
86 "Channel bottom height above aquifer bottom (Hstar) (ft or m)",
87 "Ground surface height above aquifer bottom (ft or m)",
88 "Saturated hydraulic conductivity (in/hr or mm/hr)",
89 "Unsaturated hydraulic conductivity (in/hr or mm/hr)",
90 "Upper-zone moisture content (fraction)",
91 "Soil porosity (fraction)",
92 "Surface infiltration rate (in/hr or mm/hr)",
93 "Upper-zone percolation rate (in/hr or mm/hr)",
94 "Subcatchment area (ac or ha)"};
95
110int gwf_validate(const std::string& expr, std::string& msg, int& col);
111
112// ============================================================================
113// Per-subcatchment GW state (SoA for vectorization)
114// ============================================================================
115
116struct GWSoA {
117 int n_subcatch = 0;
118
119 // Aquifer properties (set at init, constant during sim)
120 std::vector<double> porosity;
121 std::vector<double> field_cap;
122 std::vector<double> wilt_point;
123 std::vector<double> k_sat;
124 std::vector<double> k_slope;
125 std::vector<double> tension_slope;
126 std::vector<double> upper_evap_frac;
127 std::vector<int> upper_evap_pat;
128 std::vector<double> lower_evap_depth;
129 std::vector<double> lower_loss_coeff;
130 std::vector<double> total_depth;
131 std::vector<double>
133
134 // Lateral flow coefficients
135 std::vector<double> a1, b1;
136 std::vector<double> a2, b2;
137 std::vector<double> a3;
138 std::vector<double> h_star;
139
140 // State variables (updated each timestep)
141 std::vector<double> theta;
142 std::vector<double> lower_depth;
143
144 // State
145 std::vector<double> old_flow;
146
147 // Outputs
148 std::vector<double> gw_flow;
149 std::vector<double> upper_evap;
150 std::vector<double> lower_evap;
151 std::vector<double> deep_loss;
155 std::vector<double> max_infil_vol;
156
157 // Custom flow expressions (from [GWF] section)
159 std::vector<mathexpr::Expression> lateral_expr;
161 std::vector<mathexpr::Expression> deep_expr;
162
169 void resize(int n);
170};
171
172// ============================================================================
173// GW solver
174// ============================================================================
175
181class GWSolver {
182public:
188 void init(int n_subcatch);
189
220 void execute(SimulationContext& ctx, double dt, double max_evap, const double* infil_rate,
221 const double* sw_head, const double* frac_perv, const double* perv_evap_rate,
222 int evap_pattern_month = 0);
223
228 GWSoA& state() { return soa_; }
233 const GWSoA& state() const { return soa_; }
234
235private:
236 GWSoA soa_;
237
247 static void batchUpperPerc(const double* OPENSWMM_RESTRICT theta,
248 const double* OPENSWMM_RESTRICT field_cap,
249 const double* OPENSWMM_RESTRICT k_sat,
250 const double* OPENSWMM_RESTRICT k_slope,
251 double* OPENSWMM_RESTRICT perc, int count);
252
266 static void batchGWFlow(const double* OPENSWMM_RESTRICT lower_depth,
267 const double* OPENSWMM_RESTRICT h_star,
268 const double* OPENSWMM_RESTRICT a1, const double* OPENSWMM_RESTRICT b1,
269 const double* OPENSWMM_RESTRICT a2, const double* OPENSWMM_RESTRICT b2,
270 const double* OPENSWMM_RESTRICT a3,
271 const double* OPENSWMM_RESTRICT sw_head,
272 double* OPENSWMM_RESTRICT gw_flow, int count);
273};
274
275} // namespace groundwater
276} // namespace openswmm
277
278#endif // OPENSWMM_GROUNDWATER_HPP
#define OPENSWMM_RESTRICT
Definition KernelPointers.hpp:9
Mathematical expression parser and evaluator.
Mutable two-zone groundwater state with independent scalar per-subcatchment RKF45....
Definition Groundwater.hpp:181
GWSoA & state()
Borrow mutable aquifer parameters, history, outputs and compiled expressions.
Definition Groundwater.hpp:228
const GWSoA & state() const
Borrow immutable aquifer state and latest endpoint outputs.
Definition Groundwater.hpp:233
void execute(SimulationContext &ctx, double dt, double max_evap, const double *infil_rate, const double *sw_head, const double *frac_perv, const double *perv_evap_rate, int evap_pattern_month=0)
Integrate each active two-zone aquifer and book its groundwater water balance.
Definition Groundwater.cpp:280
void init(int n_subcatch)
Initialize the owned SoA through GWSoA::resize().
Definition Groundwater.cpp:87
GWVar
GW expression variable indices (matching legacy GWvariables enum).
Definition Groundwater.hpp:63
@ GWV_HSW
Surface water head.
Definition Groundwater.hpp:65
@ GWV_HCB
Channel bottom height (h_star)
Definition Groundwater.hpp:66
@ GWV_HGS
Ground surface height (total_depth)
Definition Groundwater.hpp:67
@ GWV_K
Unsaturated conductivity.
Definition Groundwater.hpp:69
@ GWV_FU
Upper zone percolation rate.
Definition Groundwater.hpp:73
@ GWV_A
Subcatchment area.
Definition Groundwater.hpp:74
@ GWV_PHI
Porosity.
Definition Groundwater.hpp:71
@ GWV_HGW
Water table height.
Definition Groundwater.hpp:64
@ GWV_MAX
Definition Groundwater.hpp:75
@ GWV_THETA
Upper zone moisture content.
Definition Groundwater.hpp:70
@ GWV_FI
Surface infiltration rate.
Definition Groundwater.hpp:72
@ GWV_KS
Saturated conductivity.
Definition Groundwater.hpp:68
int gwf_validate(const std::string &expr, std::string &msg, int &col)
Validate a GWF expression and probe the production parser without storing it.
Definition Groundwater.cpp:749
Definition ExchangeLaw.hpp:60
Central, reentrant simulation context.
Definition SimulationContext.hpp:390
Definition Groundwater.hpp:116
void resize(int n)
Resize/reset all scalar groundwater rows while retaining expression prefixes.
Definition Groundwater.cpp:49
std::vector< double > total_depth
Aquifer thickness (ft)
Definition Groundwater.hpp:130
std::vector< double > gw_flow
Lateral GW flux over full subcatchment area (ft/s)
Definition Groundwater.hpp:148
std::vector< double > upper_evap
Upper-zone evaporation flux (ft/s over full area)
Definition Groundwater.hpp:149
std::vector< mathexpr::Expression > lateral_expr
Per-subcatch compiled lateral flow expression (added to standard formula).
Definition Groundwater.hpp:159
std::vector< double > lower_loss_coeff
Deep percolation coeff.
Definition Groundwater.hpp:129
std::vector< double > b2
Surface water interaction.
Definition Groundwater.hpp:136
std::vector< double > theta
Upper zone moisture content (0-φ)
Definition Groundwater.hpp:141
std::vector< double > tension_slope
Definition Groundwater.hpp:125
std::vector< double > porosity
Definition Groundwater.hpp:120
std::vector< double > old_flow
Previous step GW flow (for trapezoidal avg)
Definition Groundwater.hpp:145
std::vector< int > upper_evap_pat
Pattern index for monthly evap adjustment (-1 = none)
Definition Groundwater.hpp:127
std::vector< double > field_cap
Definition Groundwater.hpp:121
std::vector< double > max_infil_vol
Definition Groundwater.hpp:155
std::vector< double > lower_evap_depth
Definition Groundwater.hpp:128
std::vector< double > k_slope
Exponential decay slope.
Definition Groundwater.hpp:124
int n_subcatch
Definition Groundwater.hpp:117
std::vector< double > bottom_elev
Effective aquifer bottom elevation (ft): the [GROUNDWATER] row's Ebot, else the aquifer's.
Definition Groundwater.hpp:132
std::vector< double > deep_loss
Definition Groundwater.hpp:151
std::vector< mathexpr::Expression > deep_expr
Per-subcatch compiled deep percolation expression (replaces standard formula).
Definition Groundwater.hpp:161
std::vector< double > b1
GW outflow.
Definition Groundwater.hpp:135
std::vector< double > wilt_point
Definition Groundwater.hpp:122
std::vector< double > a3
Cross-interaction.
Definition Groundwater.hpp:137
std::vector< double > upper_evap_frac
Definition Groundwater.hpp:126
std::vector< double > lower_depth
Lower zone depth (ft)
Definition Groundwater.hpp:142
std::vector< double > k_sat
Saturated conductivity (ft/sec)
Definition Groundwater.hpp:123
std::vector< double > a1
Definition Groundwater.hpp:135
std::vector< double > h_star
Threshold water table height (ft)
Definition Groundwater.hpp:138
std::vector< double > a2
Definition Groundwater.hpp:136
std::vector< double > lower_evap
Lower-zone evaporation flux (ft/s over full area)
Definition Groundwater.hpp:150