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#include "Radiation.hpp"
39
40namespace openswmm {
41
43
44namespace climate {
45
46// ============================================================================
47// Constants
48// ============================================================================
49
50constexpr double MM_PER_INCH = 25.40;
51
52// ============================================================================
53// Evaporation method
54// ============================================================================
55
56enum class EvapMethod : int {
61 PAN = 4
62};
63
64// ============================================================================
65// 7-day temperature moving average (for Hargreaves ET)
66// ============================================================================
67
68struct MovingAvg7 {
69 double ta[7] = {};
70 double tr[7] = {};
71 int front = 0;
72 int count = 0;
73
80 void push(double t_avg, double t_range);
81
85 double avg_temp() const;
86
90 double avg_range() const;
91};
92
93// ============================================================================
94// Daily climate state (scalar — broadcast to all subcatchments)
95// ============================================================================
96
99 double temperature = 70.0;
100 double temp_range = 0.0;
101 double evap_rate = 0.0;
102 double wind_speed = 0.0;
103 double humidity = 50.0;
104
105 // Source/default bases (pre-forcing). The per-step source lookup writes
106 // these; forcing then resolves effective values on top. Keeping them
107 // separate lets a one-shot/cleared forcing revert to the data source (or
108 // default) even when no source overwrites the value each step.
109 double temperature_src = 70.0;
110 double file_tmin = 70.0;
111 double file_tmax = 70.0;
112 double wind_speed_src = 0.0;
113
114 // Derived values
115 double gamma = 0.0;
116 double ea = 0.0;
117
118 // Hargreaves parameters
119 double latitude = 0.0;
120
121 // Site elevation for psychrometric constant (matching legacy Temp.elev)
122 double elev = 0.0;
123 double snow_divt = 34.0;
124
125 // Monthly evaporation table (for MONTHLY method)
126 double monthly_evap[12] = {};
127
128 // EVAPRATE unit-conversion factor (in/day US or mm/day SI → ft/sec) for the
129 // project's unit system. Set at init; MONTHLY uses it so SI models convert
130 // mm/day correctly (defaults to the US factor for back-compatibility).
131 double evaprate_ucf = 1036800.0;
132
133 // Monthly adjustment factors
134 double adjust_evap[12] = {
135 0, 0, 0, 0, 0, 0,
136 0, 0, 0, 0, 0, 0};
137 // Legacy climate.c NextEvapDate / NextEvapRate: the date the evaporation
138 // source next changes (every type — a year ahead for CONSTANT, the first
139 // of next month for MONTHLY, the next series entry for TIMESERIES, the
140 // next day for a climate file) and the rate that entry carries. A
141 // time-series evaporation is STEP-WISE: the rate holds until the date
142 // is reached, and the runoff step never crosses it (runoff_getTimeStep).
143 double next_evap_date = 0.0;
144 double next_evap_rate = 0.0;
146 -1;
147 double adjust_temp[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
148 double adjust_rain[12] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
149 double adjust_hydcon[12] = {1, 1, 1, 1, 1, 1,
150 1, 1, 1, 1, 1, 1};
151
153
154 // Monthly adjustment for infiltration capacity (scales f0/fmin/Ks)
155 // @see Legacy: InfilFactor (set from hydcon adjustment pattern)
156 double infil_factor = 1.0;
157
158 // 7-day moving average for Hargreaves ET (matching legacy TMovAve Tma)
160
161 // Recovery factor for infiltration models
162 // @see Legacy: Evap.recoveryFactor (set from recovery adjustment pattern)
163 double recovery_factor = 1.0;
164
165 // Resolved timeseries/pattern indices (set at init, -1 = none)
166 int temp_ts_index = -1;
167 int evap_ts_index = -1;
170
171 // Sub-daily sinusoidal temperature interpolation (Gap #9)
172 // Valid only when temperature comes from a daily min/max source.
173 // @see Legacy: setTemp() / updateTempTimes() in climate.c
174 double tmin_daily = 0.0;
175 double tmax_daily = 0.0;
176 double prev_tmax = 0.0;
177 double hrsr = 6.0;
178 double hrss = 15.0;
179 double hrday = 10.5;
180 double dhrdy = -9.0;
181 double dydif = 15.0;
182 double dtlong = 0.0;
183 bool has_minmax = false;
184 int last_temp_doy = -1;
185};
186
187// ============================================================================
188// Functions
189// ============================================================================
190
201double hargreaves(double latitude, int day_of_year, double t_avg, double t_range);
202
217void updateDailyClimate(ClimateState& state, int day_of_year, int month);
218
227void updateTempTimes(ClimateState& state, int doy);
228
236double getSubdailyTemp(const ClimateState& state, double hour);
237
249void batchDistributeEvap(double evap_rate, const double* ponded_depth, double* evap_out, int n,
250 double dt);
251
252} // namespace climate
253} // namespace openswmm
254
255#endif // OPENSWMM_CLIMATE_HPP
Solar geometry, atmospheric radiation and interval weather forcing.
Definition Rk2RuntimeSources.hpp:19
double getSubdailyTemp(const ClimateState &state, double hour)
Evaluate the three-segment native diurnal temperature curve.
Definition Climate.cpp:220
void updateDailyClimate(ClimateState &state, int day_of_year, int month)
Update scalar evaporation and snow-related atmospheric coefficients.
Definition Climate.cpp:111
constexpr double MM_PER_INCH
Definition Climate.hpp:50
EvapMethod
Definition Climate.hpp:56
@ TIMESERIES
Definition Climate.hpp:59
@ CONSTANT
Definition Climate.hpp:57
@ TEMPERATURE
Hargreaves method.
Definition Climate.hpp:60
@ MONTHLY
Definition Climate.hpp:58
@ PAN
Definition Climate.hpp:61
void updateTempTimes(ClimateState &state, int doy)
Derive daily sinusoidal temperature timing from latitude and season.
Definition Climate.cpp:189
void batchDistributeEvap(double evap_rate, const double *ponded_depth, double *evap_out, int n, double dt)
Bound one scalar evaporation rate by each ponded-water supply rate.
Definition Climate.cpp:175
double hargreaves(double latitude, int day_of_year, double t_avg, double t_range)
Estimate potential evapotranspiration using Hargreaves radiation/temperature.
Definition Climate.cpp:72
Definition ExchangeLaw.hpp:60
Central, reentrant simulation context.
Definition SimulationContext.hpp:390
Definition Climate.hpp:97
double ea
Saturation vapor pressure.
Definition Climate.hpp:116
double latitude
Latitude (degrees)
Definition Climate.hpp:119
double adjust_hydcon[12]
Infiltration conductivity multipliers.
Definition Climate.hpp:149
int humidity_ts_index
Humidity / dew-point timeseries table index.
Definition Climate.hpp:168
int temp_ts_index
Temperature timeseries table index.
Definition Climate.hpp:166
MovingAvg7 temp_ma
Definition Climate.hpp:159
bool has_minmax
True when tmin_daily/tmax_daily are valid.
Definition Climate.hpp:183
double next_evap_rate
user units
Definition Climate.hpp:144
int evap_ts_pos
index of the last series entry read (legacy table_getNextEntry cursor)
Definition Climate.hpp:145
RadiationState radiation
Definition Climate.hpp:98
double recovery_factor
Definition Climate.hpp:163
double wind_speed_src
Source/default wind speed (mph)
Definition Climate.hpp:112
double infil_factor
Definition Climate.hpp:156
double evaprate_ucf
Definition Climate.hpp:131
double hrsr
Sunrise hour (time of min temp), 0-24.
Definition Climate.hpp:177
double dydif
24 + hrsr - hrss (hours from max to next min)
Definition Climate.hpp:181
double adjust_evap[12]
ADDED to the rate (ft/s), legacy Adjust.evap /= UCF(EVAPRATE)
Definition Climate.hpp:134
double dtlong
Longitude correction (hrs); 0 = solar time.
Definition Climate.hpp:182
double elev
Site elevation above sea level (ft)
Definition Climate.hpp:122
EvapMethod evap_method
Definition Climate.hpp:152
double hrss
Sunset-3 hour (time of max temp), 0-24.
Definition Climate.hpp:178
double file_tmin
Last daily min read from a climate file (deg F)
Definition Climate.hpp:110
double temperature_src
Source/default air temperature (deg F)
Definition Climate.hpp:109
double tmax_daily
Current day's maximum temperature (deg F)
Definition Climate.hpp:175
double hrday
Mid-hour between hrsr and hrss.
Definition Climate.hpp:179
double monthly_evap[12]
Definition Climate.hpp:126
int recovery_pat_index
Recovery pattern index in ctx.patterns.
Definition Climate.hpp:169
double snow_divt
Rain/snow dividing temperature (degF), legacy Snow.snotmp.
Definition Climate.hpp:123
double humidity
Relative humidity (%)
Definition Climate.hpp:103
double wind_speed
Wind speed (mph) — effective (post-forcing)
Definition Climate.hpp:102
double file_tmax
Last daily max read from a climate file (deg F)
Definition Climate.hpp:111
int last_temp_doy
Day-of-year when hrsr/hrss were last computed.
Definition Climate.hpp:184
double temp_range
Daily temperature range (deg F)
Definition Climate.hpp:100
double next_evap_date
Definition Climate.hpp:143
double dhrdy
hrsr - hrss (negative)
Definition Climate.hpp:180
double gamma
Psychrometric constant.
Definition Climate.hpp:115
double prev_tmax
Previous day's maximum temperature (deg F)
Definition Climate.hpp:176
double adjust_temp[12]
Definition Climate.hpp:147
double evap_rate
Evaporation rate (ft/sec)
Definition Climate.hpp:101
double adjust_rain[12]
Definition Climate.hpp:148
double temperature
Air temperature (deg F) — effective (post-forcing)
Definition Climate.hpp:99
int evap_ts_index
Evaporation timeseries table index.
Definition Climate.hpp:167
double tmin_daily
Current day's minimum temperature (deg F)
Definition Climate.hpp:174
Definition Climate.hpp:68
void push(double t_avg, double t_range)
Append a daily temperature/range pair to the seven-slot circular history.
Definition Climate.cpp:42
double tr[7]
Daily temp ranges (deg F)
Definition Climate.hpp:70
double avg_range() const
Read the arithmetic mean of retained daily temperature ranges.
Definition Climate.cpp:59
int count
Number of values stored (max 7)
Definition Climate.hpp:72
int front
Circular buffer write index.
Definition Climate.hpp:71
double ta[7]
Daily average temps (deg F)
Definition Climate.hpp:69
double avg_temp() const
Read the arithmetic mean of retained daily temperature samples.
Definition Climate.cpp:50
Definition Radiation.hpp:55