OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
GageData.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
37
38#ifndef OPENSWMM_ENGINE_GAGE_DATA_HPP
39#define OPENSWMM_ENGINE_GAGE_DATA_HPP
40
42#include "TableData.hpp"
43#include <vector>
44#include <string>
45#include <cstdint>
46
47namespace openswmm {
48
49// ============================================================================
50// Rain source enumeration
51// ============================================================================
52
57enum class RainSource : int8_t {
60};
61
66enum class RainFileFormat : int8_t {
67 UNKNOWN = -1,
68 NWS_15 = 0,
77};
78
79// ============================================================================
80// GageData — SoA layout
81// ============================================================================
82
95struct GageData {
96
97 // -----------------------------------------------------------------------
98 // Static properties — set at parse time
99 // -----------------------------------------------------------------------
100
105 std::vector<int> rain_type;
106
108 std::vector<RainSource> source;
109
114 std::vector<int> ts_index;
115
123 std::vector<FilePathPair> file_path;
124
128 std::vector<std::string> ts_name;
129
136 std::vector<std::string> col_name;
137
146 std::vector<std::string> station_id;
147
154 std::vector<int> rain_units;
155
156 // -----------------------------------------------------------------------
157 // Rainfall-file summary statistics (for the "Rainfall File Summary"
158 // report section). Populated by load_external_rain_files() while it
159 // scans the external file; meaningful only when source == FILE_RAIN.
160 // -----------------------------------------------------------------------
161
163 std::vector<double> file_first_date;
164
166 std::vector<double> file_last_date;
167
169 std::vector<long> file_periods_precip;
170
180 std::vector<Table> rain_series;
181
186 std::vector<RainFileFormat> file_format;
187
192 std::vector<int> interval_sec;
193
198 std::vector<double> snow_factor;
199
211 std::vector<double> scale_factor;
212
213 // -----------------------------------------------------------------------
214 // State variables — updated each timestep
215 // -----------------------------------------------------------------------
216
221 std::vector<double> rainfall;
222
227 std::vector<double> next_rainfall;
228
233 std::vector<double> api_rainfall;
234
239 std::vector<double> next_rain_date;
240
245 std::vector<bool> is_raining;
246
247 // -----------------------------------------------------------------------
248 // Past-rain history (for control rules — GAGE_RAIN_PAST)
249 // -----------------------------------------------------------------------
250
251 static constexpr int MAXPASTRAIN = 48;
252
254 std::vector<double> past_rain;
255
257 std::vector<double> past_rain_accum;
258
260 std::vector<double> past_rain_time;
261
268 std::vector<double> cumul_rain_accum;
269
275 std::vector<std::string> comments;
276
284 std::vector<int> co_gage_index;
285
286 // -----------------------------------------------------------------------
287 // Capacity management
288 // -----------------------------------------------------------------------
289
290 int count() const noexcept { return static_cast<int>(source.size()); }
291
292 void resize(int n) {
293 const auto un = static_cast<std::size_t>(n);
294
295 rain_type.assign(un, 0);
296 source.assign(un, RainSource::TIMESERIES);
297 ts_index.assign(un, -1);
298 ts_name.assign(un, std::string{});
299 file_path.assign(un, std::string{});
300 col_name.assign(un, std::string{});
301 station_id.assign(un, std::string{});
302 rain_units.assign(un, 0);
303 file_first_date.assign(un, 0.0);
304 file_last_date.assign(un, 0.0);
305 file_periods_precip.assign(un, 0L);
306 rain_series.assign(un, Table{});
308 interval_sec.assign(un, 3600);
309 snow_factor.assign(un, 1.0);
310 scale_factor.assign(un, 1.0);
311
312 rainfall.assign(un, 0.0);
313 next_rainfall.assign(un, 0.0);
314 api_rainfall.assign(un, -1.0); // -1.0 means no API override
315 next_rain_date.assign(un, 0.0);
316 is_raining.assign(un, false);
317
318 past_rain.assign(un * MAXPASTRAIN, 0.0);
319 past_rain_accum.assign(un, 0.0);
320 past_rain_time.assign(un, 0.0);
321 cumul_rain_accum.assign(un, 0.0);
322 co_gage_index.assign(un, -1);
323 comments.assign(un, std::string{});
324 }
325
332 void grow_to(int n) {
333 if (n <= count()) return;
334 const auto un = static_cast<std::size_t>(n);
335 auto g = [&](auto& v, auto def) { v.resize(un, def); };
337 ts_name.resize(un, std::string{});
338 file_path.resize(un, std::string{});
339 col_name.resize(un, std::string{});
340 station_id.resize(un, std::string{});
341 g(rain_units, 0);
342 g(file_first_date, 0.0); g(file_last_date, 0.0); g(file_periods_precip, 0L);
343 rain_series.resize(un, Table{});
345 g(scale_factor, 1.0);
346 g(rainfall, 0.0); g(next_rainfall, 0.0);
347 g(api_rainfall, -1.0); g(next_rain_date, 0.0); g(is_raining, false);
348 // Flat 2D: [gage * MAXPASTRAIN + hour]
349 past_rain.resize(un * static_cast<std::size_t>(MAXPASTRAIN), 0.0);
350 g(past_rain_accum, 0.0); g(past_rain_time, 0.0); g(cumul_rain_accum, 0.0);
351 g(co_gage_index, -1);
352 comments.resize(un, std::string{});
353 }
354
362 void erase_at(int idx) {
363 const auto ui = static_cast<std::size_t>(idx);
364 auto e = [&](auto& v) { if (ui < v.size()) v.erase(v.begin() + static_cast<std::ptrdiff_t>(idx)); };
365
366 e(rain_type); e(source); e(ts_index); e(ts_name);
368 e(scale_factor);
371 e(comments);
372
373 // Flat 2D past_rain: [gage * MAXPASTRAIN + hour]
374 const auto base = ui * static_cast<std::size_t>(MAXPASTRAIN);
375 const auto end = base + static_cast<std::size_t>(MAXPASTRAIN);
376 if (end <= past_rain.size())
377 past_rain.erase(past_rain.begin() + static_cast<std::ptrdiff_t>(base),
378 past_rain.begin() + static_cast<std::ptrdiff_t>(end));
379 }
380
385 rain_type.shrink_to_fit();
386 source.shrink_to_fit();
387 ts_index.shrink_to_fit();
388 ts_name.shrink_to_fit();
389 file_path.shrink_to_fit();
390 col_name.shrink_to_fit();
391 file_format.shrink_to_fit();
392 interval_sec.shrink_to_fit();
393 snow_factor.shrink_to_fit();
394 scale_factor.shrink_to_fit();
395
396 rainfall.shrink_to_fit();
397 next_rainfall.shrink_to_fit();
398 api_rainfall.shrink_to_fit();
399 next_rain_date.shrink_to_fit();
400 is_raining.shrink_to_fit();
401
402 past_rain.shrink_to_fit();
403 past_rain_accum.shrink_to_fit();
404 past_rain_time.shrink_to_fit();
405 cumul_rain_accum.shrink_to_fit();
406 co_gage_index.shrink_to_fit();
407 comments.shrink_to_fit();
408 }
409
410 void reset_state() noexcept {
411 std::fill(rainfall.begin(), rainfall.end(), 0.0);
412 std::fill(next_rainfall.begin(), next_rainfall.end(), 0.0);
413 std::fill(api_rainfall.begin(), api_rainfall.end(), -1.0); // -1 = no override
414 std::fill(is_raining.begin(), is_raining.end(), false);
415 std::fill(past_rain.begin(), past_rain.end(), 0.0);
416 std::fill(past_rain_accum.begin(), past_rain_accum.end(), 0.0);
417 std::fill(past_rain_time.begin(), past_rain_time.end(), 0.0);
418 std::fill(cumul_rain_accum.begin(), cumul_rain_accum.end(), 0.0);
419 // co_gage_index is static (set at parse time), not reset between runs
420 }
421};
422
423} /* namespace openswmm */
424
425#endif /* OPENSWMM_ENGINE_GAGE_DATA_HPP */
Carrier for an external file reference in a SWMM model.
Time series and rating curve data with bidirectional cursor.
Definition NodeCoupling.cpp:16
RainFileFormat
Rain data format in an external file.
Definition GageData.hpp:66
@ HLY_PRCP
HLY_PRCP format.
Definition GageData.hpp:72
@ STAN_PRCP
Standard SWMM rain file.
Definition GageData.hpp:73
@ NWS_HOURLY
NWS hourly data.
Definition GageData.hpp:69
@ USER_CSV
Definition GageData.hpp:74
@ UNKNOWN
Definition GageData.hpp:67
@ NWS_15
NWS 15-minute data.
Definition GageData.hpp:68
@ DSI_3260
NCDC DSI 3260 15-minute.
Definition GageData.hpp:71
@ DSI_3240
NCDC DSI 3240 hourly.
Definition GageData.hpp:70
RainSource
Precipitation source type for a rain gage.
Definition GageData.hpp:57
@ FILE_RAIN
Data from an external rain file.
Definition GageData.hpp:59
@ TIMESERIES
Data from an in-file [TIMESERIES].
Definition GageData.hpp:58
Structure-of-Arrays storage for all rain gages.
Definition GageData.hpp:95
std::vector< double > api_rainfall
Current API (antecedent precipitation index) rainfall.
Definition GageData.hpp:233
std::vector< int > rain_units
Rain depth units declared in the [RAINGAGES] FILE row: 0 = IN, 1 = MM.
Definition GageData.hpp:154
std::vector< double > file_first_date
First record date found for this gage's station (OADate, 0 = none).
Definition GageData.hpp:163
std::vector< std::string > comments
Object comment from the INP file (';'-prefixed lines immediately above this gage's data row),...
Definition GageData.hpp:275
std::vector< double > file_last_date
Last record date found for this gage's station (OADate, 0 = none).
Definition GageData.hpp:166
std::vector< bool > is_raining
Current state flag (0 = no rain, 1 = raining).
Definition GageData.hpp:245
std::vector< double > snow_factor
Snow catch deficiency correction factor (1.0 = no correction).
Definition GageData.hpp:198
std::vector< int > ts_index
Time series index (when source == TIMESERIES).
Definition GageData.hpp:114
std::vector< std::string > col_name
Column name in the external multi-column file (source == FILE_RAIN).
Definition GageData.hpp:136
static constexpr int MAXPASTRAIN
Max past hours tracked per gage.
Definition GageData.hpp:251
std::vector< FilePathPair > file_path
External rain file path (when source == FILE_RAIN).
Definition GageData.hpp:123
std::vector< Table > rain_series
Resolved rainfall series for a FILE_RAIN gage (windowed to the run).
Definition GageData.hpp:180
std::vector< double > past_rain_time
Per-gage time (seconds) of last past-rain shift.
Definition GageData.hpp:260
void reset_state() noexcept
Definition GageData.hpp:410
int count() const noexcept
Definition GageData.hpp:290
std::vector< double > next_rain_date
Simulation time of the next recorded value (decimal days).
Definition GageData.hpp:239
std::vector< std::string > ts_name
Timeseries name (for deferred resolution when TS parsed after gages).
Definition GageData.hpp:128
void grow_to(int n)
Append one new gage with defaults, preserving existing data.
Definition GageData.hpp:332
std::vector< double > scale_factor
Rainfall scaling factor (1.0 = no scaling).
Definition GageData.hpp:211
std::vector< int > co_gage_index
Gap #53: Co-gage index — index of the primary gage sharing the same timeseries, or -1 if this gage re...
Definition GageData.hpp:284
std::vector< int > interval_sec
Recording interval in seconds.
Definition GageData.hpp:192
std::vector< double > past_rain_accum
Per-gage accumulator for the current partial hour.
Definition GageData.hpp:257
std::vector< double > next_rainfall
Rainfall rate at the next recorded interval (for interpolation).
Definition GageData.hpp:227
void shrink_to_fit()
Release excess vector capacity accumulated during parsing.
Definition GageData.hpp:384
std::vector< long > file_periods_precip
Number of records with precipitation > 0 for this station.
Definition GageData.hpp:169
std::vector< RainFileFormat > file_format
Rain file format.
Definition GageData.hpp:186
std::vector< int > rain_type
Rain data type: 0=INTENSITY, 1=VOLUME, 2=CUMULATIVE.
Definition GageData.hpp:105
void erase_at(int idx)
Erase the rain gage at index idx from every parallel array.
Definition GageData.hpp:362
std::vector< std::string > station_id
Station ID for a standard SWMM rain file (when source == FILE_RAIN).
Definition GageData.hpp:146
std::vector< double > cumul_rain_accum
Gap #31: Cumulative rainfall accumulator (project rain units).
Definition GageData.hpp:268
std::vector< double > rainfall
Current rainfall rate (project length/time units — inches or mm/hr).
Definition GageData.hpp:221
std::vector< RainSource > source
Precipitation source (TIMESERIES or FILE).
Definition GageData.hpp:108
void resize(int n)
Definition GageData.hpp:292
std::vector< double > past_rain
Flat 2D: [gage * MAXPASTRAIN + hour]. Hourly rain totals.
Definition GageData.hpp:254
Definition TableData.hpp:138