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[] = {
80 "HGW", "HSW", "HCB", "HGS", "KS", "K",
81 "THETA", "PHI", "FI", "FU", "A"
82};
83
85static const char* GW_VAR_DESCRIPTIONS[] = {
86 "Water table height above aquifer bottom (ft or m)",
87 "Surface-water head at the receiving node above aquifer bottom (ft or m)",
88 "Channel bottom height above aquifer bottom (Hstar) (ft or m)",
89 "Ground surface height above aquifer bottom (ft or m)",
90 "Saturated hydraulic conductivity (in/hr or mm/hr)",
91 "Unsaturated hydraulic conductivity (in/hr or mm/hr)",
92 "Upper-zone moisture content (fraction)",
93 "Soil porosity (fraction)",
94 "Surface infiltration rate (in/hr or mm/hr)",
95 "Upper-zone percolation rate (in/hr or mm/hr)",
96 "Subcatchment area (ac or ha)"
97};
98
115int gwf_validate(const std::string& expr, std::string& msg, int& col);
116
117// ============================================================================
118// Per-subcatchment GW state (SoA for vectorization)
119// ============================================================================
120
121struct GWSoA {
122 int n_subcatch = 0;
123
124 // Aquifer properties (set at init, constant during sim)
125 std::vector<double> porosity;
126 std::vector<double> field_cap;
127 std::vector<double> wilt_point;
128 std::vector<double> k_sat;
129 std::vector<double> k_slope;
130 std::vector<double> tension_slope;
131 std::vector<double> upper_evap_frac;
132 std::vector<int> upper_evap_pat;
133 std::vector<double> lower_evap_depth;
134 std::vector<double> lower_loss_coeff;
135 std::vector<double> total_depth;
136
137 // Lateral flow coefficients
138 std::vector<double> a1, b1;
139 std::vector<double> a2, b2;
140 std::vector<double> a3;
141 std::vector<double> h_star;
142
143 // State variables (updated each timestep)
144 std::vector<double> theta;
145 std::vector<double> lower_depth;
146
147 // State
148 std::vector<double> old_flow;
149
150 // Outputs
151 std::vector<double> gw_flow;
152 std::vector<double> upper_evap;
153 std::vector<double> lower_evap;
154 std::vector<double> deep_loss;
158 std::vector<double> max_infil_vol;
159
160 // Custom flow expressions (from [GWF] section)
162 std::vector<mathexpr::Expression> lateral_expr;
164 std::vector<mathexpr::Expression> deep_expr;
165
166 void resize(int n);
167};
168
169// ============================================================================
170// GW solver
171// ============================================================================
172
173class GWSolver {
174public:
175 void init(int n_subcatch);
176
195 void execute(SimulationContext& ctx, double dt, double max_evap,
196 const double* infil_rate, const double* sw_head,
197 const double* frac_perv, const double* perv_evap_rate);
198
199 GWSoA& state() { return soa_; }
200 const GWSoA& state() const { return soa_; }
201
202private:
203 GWSoA soa_;
204
206 static void batchUpperPerc(
207 const double* OPENSWMM_RESTRICT theta,
208 const double* OPENSWMM_RESTRICT field_cap,
209 const double* OPENSWMM_RESTRICT k_sat,
210 const double* OPENSWMM_RESTRICT k_slope,
211 double* OPENSWMM_RESTRICT perc,
212 int count
213 );
214
216 static void batchGWFlow(
217 const double* OPENSWMM_RESTRICT lower_depth,
218 const double* OPENSWMM_RESTRICT h_star,
219 const double* OPENSWMM_RESTRICT a1, const double* OPENSWMM_RESTRICT b1,
220 const double* OPENSWMM_RESTRICT a2, const double* OPENSWMM_RESTRICT b2,
221 const double* OPENSWMM_RESTRICT a3,
222 const double* OPENSWMM_RESTRICT sw_head,
223 double* OPENSWMM_RESTRICT gw_flow,
224 int count
225 );
226};
227
228} // namespace groundwater
229} // namespace openswmm
230
231#endif // OPENSWMM_GROUNDWATER_HPP
Mathematical expression parser and evaluator.
#define OPENSWMM_RESTRICT
Definition XSectBatch.hpp:57
Definition Groundwater.hpp:173
GWSoA & state()
Definition Groundwater.hpp:199
const GWSoA & state() const
Definition Groundwater.hpp:200
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)
Compute groundwater for all subcatchments (batch).
Definition Groundwater.cpp:255
void init(int n_subcatch)
Definition Groundwater.cpp:82
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] flow expression without compiling or storing it.
Definition Groundwater.cpp:592
Definition NodeCoupling.cpp:16
Central, reentrant simulation context.
Definition SimulationContext.hpp:353
Definition Groundwater.hpp:121
void resize(int n)
Definition Groundwater.cpp:47
std::vector< double > total_depth
Aquifer thickness (ft)
Definition Groundwater.hpp:135
std::vector< double > gw_flow
Lateral GW flow to node (cfs)
Definition Groundwater.hpp:151
std::vector< double > upper_evap
Upper zone evap (ft3/sec)
Definition Groundwater.hpp:152
std::vector< mathexpr::Expression > lateral_expr
Per-subcatch compiled lateral flow expression (added to standard formula).
Definition Groundwater.hpp:162
std::vector< double > lower_loss_coeff
Deep percolation coeff.
Definition Groundwater.hpp:134
std::vector< double > b2
Surface water interaction.
Definition Groundwater.hpp:139
std::vector< double > theta
Upper zone moisture content (0-φ)
Definition Groundwater.hpp:144
std::vector< double > tension_slope
Definition Groundwater.hpp:130
std::vector< double > porosity
Definition Groundwater.hpp:125
std::vector< double > old_flow
Previous step GW flow (for trapezoidal avg)
Definition Groundwater.hpp:148
std::vector< int > upper_evap_pat
Pattern index for monthly evap adjustment (-1 = none)
Definition Groundwater.hpp:132
std::vector< double > field_cap
Definition Groundwater.hpp:126
std::vector< double > max_infil_vol
Definition Groundwater.hpp:158
std::vector< double > lower_evap_depth
Definition Groundwater.hpp:133
std::vector< double > k_slope
Exponential decay slope.
Definition Groundwater.hpp:129
int n_subcatch
Definition Groundwater.hpp:122
std::vector< double > deep_loss
Definition Groundwater.hpp:154
std::vector< mathexpr::Expression > deep_expr
Per-subcatch compiled deep percolation expression (replaces standard formula).
Definition Groundwater.hpp:164
std::vector< double > b1
GW outflow.
Definition Groundwater.hpp:138
std::vector< double > wilt_point
Definition Groundwater.hpp:127
std::vector< double > a3
Cross-interaction.
Definition Groundwater.hpp:140
std::vector< double > upper_evap_frac
Definition Groundwater.hpp:131
std::vector< double > lower_depth
Lower zone depth (ft)
Definition Groundwater.hpp:145
std::vector< double > k_sat
Saturated conductivity (ft/sec)
Definition Groundwater.hpp:128
std::vector< double > a1
Definition Groundwater.hpp:138
std::vector< double > h_star
Threshold water table height (ft)
Definition Groundwater.hpp:141
std::vector< double > a2
Definition Groundwater.hpp:139
std::vector< double > lower_evap
Lower zone evap (ft3/sec)
Definition Groundwater.hpp:153