OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
Climate.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
35
36#ifndef OPENSWMM_CLIMATE_HPP
37#define OPENSWMM_CLIMATE_HPP
38
39namespace openswmm {
40
42
43namespace climate {
44
45// ============================================================================
46// Constants
47// ============================================================================
48
49constexpr double MM_PER_INCH = 25.40;
50
51// ============================================================================
52// Evaporation method
53// ============================================================================
54
55enum class EvapMethod : int {
60 PAN = 4
61};
62
63// ============================================================================
64// 7-day temperature moving average (for Hargreaves ET)
65// ============================================================================
66
67struct MovingAvg7 {
68 double ta[7] = {};
69 double tr[7] = {};
70 int front = 0;
71 int count = 0;
72
74 void push(double t_avg, double t_range);
75
77 double avg_temp() const;
78
80 double avg_range() const;
81};
82
83// ============================================================================
84// Daily climate state (scalar — broadcast to all subcatchments)
85// ============================================================================
86
88 double temperature = 70.0;
89 double temp_range = 0.0;
90 double evap_rate = 0.0;
91 double wind_speed = 0.0;
92 double humidity = 50.0;
93
94 // Source/default bases (pre-forcing). The per-step source lookup writes
95 // these; forcing then resolves effective values on top. Keeping them
96 // separate lets a one-shot/cleared forcing revert to the data source (or
97 // default) even when no source overwrites the value each step.
98 double temperature_src = 70.0;
99 double wind_speed_src = 0.0;
100
101 // Derived values
102 double gamma = 0.0;
103 double ea = 0.0;
104
105 // Hargreaves parameters
106 double latitude = 0.0;
107
108 // Site elevation for psychrometric constant (matching legacy Temp.elev)
109 double elev = 0.0;
110
111 // Monthly evaporation table (for MONTHLY method)
112 double monthly_evap[12] = {};
113
114 // EVAPRATE unit-conversion factor (in/day US or mm/day SI → ft/sec) for the
115 // project's unit system. Set at init; MONTHLY uses it so SI models convert
116 // mm/day correctly (defaults to the US factor for back-compatibility).
117 double evaprate_ucf = 1036800.0;
118
119 // Monthly adjustment factors
120 double adjust_evap[12] = {1,1,1,1,1,1,1,1,1,1,1,1};
121 double adjust_temp[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
122 double adjust_rain[12] = {1,1,1,1,1,1,1,1,1,1,1,1};
123 double adjust_hydcon[12] = {1,1,1,1,1,1,1,1,1,1,1,1};
124
126
127 // Monthly adjustment for infiltration capacity (scales f0/fmin/Ks)
128 // @see Legacy: InfilFactor (set from hydcon adjustment pattern)
129 double infil_factor = 1.0;
130
131 // 7-day moving average for Hargreaves ET (matching legacy TMovAve Tma)
133
134 // Recovery factor for infiltration models
135 // @see Legacy: Evap.recoveryFactor (set from recovery adjustment pattern)
136 double recovery_factor = 1.0;
137
138 // Resolved timeseries/pattern indices (set at init, -1 = none)
139 int temp_ts_index = -1;
140 int evap_ts_index = -1;
143
144 // Sub-daily sinusoidal temperature interpolation (Gap #9)
145 // Valid only when temperature comes from a daily min/max source.
146 // @see Legacy: setTemp() / updateTempTimes() in climate.c
147 double tmin_daily = 0.0;
148 double tmax_daily = 0.0;
149 double prev_tmax = 0.0;
150 double hrsr = 6.0;
151 double hrss = 15.0;
152 double hrday = 10.5;
153 double dhrdy = -9.0;
154 double dydif = 15.0;
155 double dtlong = 0.0;
156 bool has_minmax = false;
157 int last_temp_doy = -1;
158};
159
160// ============================================================================
161// Functions
162// ============================================================================
163
173double hargreaves(double latitude, int day_of_year, double t_avg, double t_range);
174
182void updateDailyClimate(ClimateState& state, int day_of_year, int month);
183
194void updateTempTimes(ClimateState& state, int doy);
195
212double getSubdailyTemp(const ClimateState& state, double hour);
213
226void batchDistributeEvap(double evap_rate, const double* ponded_depth,
227 double* evap_out, int n, double dt);
228
229} // namespace climate
230} // namespace openswmm
231
232#endif // OPENSWMM_CLIMATE_HPP
Definition Climate.cpp:34
double getSubdailyTemp(const ClimateState &state, double hour)
Compute sub-daily temperature using a three-zone sinusoidal model.
Definition Climate.cpp:209
void updateDailyClimate(ClimateState &state, int day_of_year, int month)
Update daily climate state.
Definition Climate.cpp:105
constexpr double MM_PER_INCH
Definition Climate.hpp:49
EvapMethod
Definition Climate.hpp:55
@ TIMESERIES
Definition Climate.hpp:58
@ CONSTANT
Definition Climate.hpp:56
@ TEMPERATURE
Hargreaves method.
Definition Climate.hpp:59
@ MONTHLY
Definition Climate.hpp:57
@ PAN
Definition Climate.hpp:60
void updateTempTimes(ClimateState &state, int doy)
Update sunrise/sunset parameters for sub-daily temperature interpolation.
Definition Climate.cpp:178
void batchDistributeEvap(double evap_rate, const double *ponded_depth, double *evap_out, int n, double dt)
Batch distribute evaporation to all subcatchments.
Definition Climate.cpp:164
double hargreaves(double latitude, int day_of_year, double t_avg, double t_range)
Compute Hargreaves evapotranspiration.
Definition Climate.cpp:67
Definition NodeCoupling.cpp:16
Central, reentrant simulation context.
Definition SimulationContext.hpp:353
Definition Climate.hpp:87
double ea
Saturation vapor pressure.
Definition Climate.hpp:103
double latitude
Latitude (degrees)
Definition Climate.hpp:106
double adjust_hydcon[12]
Infiltration conductivity multipliers.
Definition Climate.hpp:123
int humidity_ts_index
Humidity / dew-point timeseries table index.
Definition Climate.hpp:141
int temp_ts_index
Temperature timeseries table index.
Definition Climate.hpp:139
MovingAvg7 temp_ma
Definition Climate.hpp:132
bool has_minmax
True when tmin_daily/tmax_daily are valid.
Definition Climate.hpp:156
double recovery_factor
Definition Climate.hpp:136
double wind_speed_src
Source/default wind speed (mph)
Definition Climate.hpp:99
double infil_factor
Definition Climate.hpp:129
double evaprate_ucf
Definition Climate.hpp:117
double hrsr
Sunrise hour (time of min temp), 0-24.
Definition Climate.hpp:150
double dydif
24 + hrsr - hrss (hours from max to next min)
Definition Climate.hpp:154
double adjust_evap[12]
Definition Climate.hpp:120
double dtlong
Longitude correction (hrs); 0 = solar time.
Definition Climate.hpp:155
double elev
Site elevation above sea level (ft)
Definition Climate.hpp:109
EvapMethod evap_method
Definition Climate.hpp:125
double hrss
Sunset-3 hour (time of max temp), 0-24.
Definition Climate.hpp:151
double temperature_src
Source/default air temperature (deg F)
Definition Climate.hpp:98
double tmax_daily
Current day's maximum temperature (deg F)
Definition Climate.hpp:148
double hrday
Mid-hour between hrsr and hrss.
Definition Climate.hpp:152
double monthly_evap[12]
Definition Climate.hpp:112
int recovery_pat_index
Recovery pattern index in ctx.patterns.
Definition Climate.hpp:142
double humidity
Relative humidity (%)
Definition Climate.hpp:92
double wind_speed
Wind speed (mph) — effective (post-forcing)
Definition Climate.hpp:91
int last_temp_doy
Day-of-year when hrsr/hrss were last computed.
Definition Climate.hpp:157
double temp_range
Daily temperature range (deg F)
Definition Climate.hpp:89
double dhrdy
hrsr - hrss (negative)
Definition Climate.hpp:153
double gamma
Psychrometric constant.
Definition Climate.hpp:102
double prev_tmax
Previous day's maximum temperature (deg F)
Definition Climate.hpp:149
double adjust_temp[12]
Definition Climate.hpp:121
double evap_rate
Evaporation rate (ft/sec)
Definition Climate.hpp:90
double adjust_rain[12]
Definition Climate.hpp:122
double temperature
Air temperature (deg F) — effective (post-forcing)
Definition Climate.hpp:88
int evap_ts_index
Evaporation timeseries table index.
Definition Climate.hpp:140
double tmin_daily
Current day's minimum temperature (deg F)
Definition Climate.hpp:147
Definition Climate.hpp:67
void push(double t_avg, double t_range)
Push a new day's values into the buffer.
Definition Climate.cpp:42
double tr[7]
Daily temp ranges (deg F)
Definition Climate.hpp:69
double avg_range() const
Current moving average of temperature range.
Definition Climate.cpp:56
int count
Number of values stored (max 7)
Definition Climate.hpp:71
int front
Circular buffer write index.
Definition Climate.hpp:70
double ta[7]
Daily average temps (deg F)
Definition Climate.hpp:68
double avg_temp() const
Current moving average of temperature.
Definition Climate.cpp:49