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
176 std::vector<double> file_start_date;
177
187 std::vector<Table> rain_series;
188
193 std::vector<RainFileFormat> file_format;
194
199 std::vector<int> interval_sec;
200
205 std::vector<double> snow_factor;
206
218 std::vector<double> scale_factor;
219
220 // -----------------------------------------------------------------------
221 // State variables — updated each timestep
222 // -----------------------------------------------------------------------
223
228 std::vector<double> rainfall;
229
234 std::vector<double> next_rainfall;
235
240 std::vector<double> api_rainfall;
241
246 std::vector<double> next_rain_date;
247
252 std::vector<bool> is_raining;
253
280 std::vector<int> st_cur;
281 std::vector<int> st_next;
282 std::vector<double> st_rain;
283 std::vector<uint8_t> st_init;
284 std::vector<uint8_t> st_used;
285
286 // -----------------------------------------------------------------------
287 // Past-rain history (for control rules — GAGE_RAIN_PAST)
288 // -----------------------------------------------------------------------
289
290 static constexpr int MAXPASTRAIN = 48;
291
293 std::vector<double> past_rain;
294
296 std::vector<double> past_rain_accum;
297
299 std::vector<double> past_rain_interval;
300
307 std::vector<double> cumul_rain_accum;
308
314 std::vector<std::string> comments;
315
323 std::vector<int> co_gage_index;
324
325 // -----------------------------------------------------------------------
326 // Capacity management
327 // -----------------------------------------------------------------------
328
333 int count() const noexcept { return static_cast<int>(source.size()); }
334
344 void resize(int n) {
345 const auto un = static_cast<std::size_t>(n);
346
347 rain_type.assign(un, 0);
348 source.assign(un, RainSource::TIMESERIES);
349 ts_index.assign(un, -1);
350 ts_name.assign(un, std::string{});
351 file_path.assign(un, std::string{});
352 col_name.assign(un, std::string{});
353 station_id.assign(un, std::string{});
354 rain_units.assign(un, 0);
355 file_first_date.assign(un, 0.0);
356 file_last_date.assign(un, 0.0);
357 file_periods_precip.assign(un, 0L);
358 file_start_date.assign(un, 0.0);
359 rain_series.assign(un, Table{});
361 interval_sec.assign(un, 3600);
362 snow_factor.assign(un, 1.0);
363 scale_factor.assign(un, 1.0);
364
365 rainfall.assign(un, 0.0);
366 next_rainfall.assign(un, 0.0);
367 api_rainfall.assign(un, -1.0); // -1.0 means no API override
368 next_rain_date.assign(un, 0.0);
369 is_raining.assign(un, false);
370 st_cur.assign(un, -2);
371 st_next.assign(un, -1);
372 st_rain.assign(un, 0.0);
373 st_init.assign(un, 0);
374 st_used.assign(un, 0);
375
376 past_rain.assign(un * MAXPASTRAIN, 0.0);
377 past_rain_accum.assign(un, 0.0);
378 past_rain_interval.assign(un, 0.0);
379 cumul_rain_accum.assign(un, 0.0);
380 co_gage_index.assign(un, -1);
381 comments.assign(un, std::string{});
382 }
383
393 void grow_to(int n) {
394 if (n <= count())
395 return;
396 const auto un = static_cast<std::size_t>(n);
397 auto g = [&](auto& v, auto def) { v.resize(un, def); };
398 g(rain_type, 0);
400 g(ts_index, -1);
401 ts_name.resize(un, std::string{});
402 file_path.resize(un, std::string{});
403 col_name.resize(un, std::string{});
404 station_id.resize(un, std::string{});
405 g(rain_units, 0);
406 g(file_first_date, 0.0);
407 g(file_last_date, 0.0);
408 g(file_periods_precip, 0L);
409 g(file_start_date, 0.0);
410 rain_series.resize(un, Table{});
412 g(interval_sec, 3600);
413 g(snow_factor, 1.0);
414 g(scale_factor, 1.0);
415 g(rainfall, 0.0);
416 g(next_rainfall, 0.0);
417 g(api_rainfall, -1.0);
418 g(next_rain_date, 0.0);
419 g(is_raining, false);
420 g(st_cur, -2);
421 g(st_next, -1);
422 g(st_rain, 0.0);
423 g(st_init, static_cast<uint8_t>(0));
424 g(st_used, static_cast<uint8_t>(0));
425 // Flat 2D: [gage * MAXPASTRAIN + hour]
426 past_rain.resize(un * static_cast<std::size_t>(MAXPASTRAIN), 0.0);
427 g(past_rain_accum, 0.0);
428 g(past_rain_interval, 0.0);
429 g(cumul_rain_accum, 0.0);
430 g(co_gage_index, -1);
431 comments.resize(un, std::string{});
432 }
433
445 void erase_at(int idx) {
446 const auto ui = static_cast<std::size_t>(idx);
447 auto e = [&](auto& v) {
448 if (ui < v.size())
449 v.erase(v.begin() + static_cast<std::ptrdiff_t>(idx));
450 };
451
452 e(rain_type);
453 e(source);
454 e(ts_index);
455 e(ts_name);
456 e(file_path);
457 e(col_name);
458 e(file_format);
459 e(interval_sec);
460 e(snow_factor);
461 e(scale_factor);
463 e(rainfall);
464 e(next_rainfall);
465 e(api_rainfall);
467 e(is_raining);
468 e(st_cur);
469 e(st_next);
470 e(st_rain);
471 e(st_init);
472 e(st_used);
476 e(co_gage_index);
477 e(comments);
478
479 // Flat 2D past_rain: [gage * MAXPASTRAIN + hour]
480 const auto base = ui * static_cast<std::size_t>(MAXPASTRAIN);
481 const auto end = base + static_cast<std::size_t>(MAXPASTRAIN);
482 if (end <= past_rain.size())
483 past_rain.erase(past_rain.begin() + static_cast<std::ptrdiff_t>(base),
484 past_rain.begin() + static_cast<std::ptrdiff_t>(end));
485 }
486
491 rain_type.shrink_to_fit();
492 source.shrink_to_fit();
493 ts_index.shrink_to_fit();
494 ts_name.shrink_to_fit();
495 file_path.shrink_to_fit();
496 col_name.shrink_to_fit();
497 file_format.shrink_to_fit();
498 interval_sec.shrink_to_fit();
499 snow_factor.shrink_to_fit();
500 scale_factor.shrink_to_fit();
501
502 rainfall.shrink_to_fit();
503 next_rainfall.shrink_to_fit();
504 api_rainfall.shrink_to_fit();
505 next_rain_date.shrink_to_fit();
506 is_raining.shrink_to_fit();
507 st_cur.shrink_to_fit();
508 st_next.shrink_to_fit();
509 st_rain.shrink_to_fit();
510 st_init.shrink_to_fit();
511 st_used.shrink_to_fit();
512
513 past_rain.shrink_to_fit();
514 past_rain_accum.shrink_to_fit();
515 past_rain_interval.shrink_to_fit();
516 cumul_rain_accum.shrink_to_fit();
517 co_gage_index.shrink_to_fit();
518 comments.shrink_to_fit();
519 }
520
528 void reset_state() noexcept {
529 std::fill(rainfall.begin(), rainfall.end(), 0.0);
530 std::fill(next_rainfall.begin(), next_rainfall.end(), 0.0);
531 std::fill(api_rainfall.begin(), api_rainfall.end(), -1.0); // -1 = no override
532 std::fill(is_raining.begin(), is_raining.end(), false);
533 std::fill(st_cur.begin(), st_cur.end(), -2);
534 std::fill(st_next.begin(), st_next.end(), -1);
535 std::fill(st_rain.begin(), st_rain.end(), 0.0);
536 std::fill(st_init.begin(), st_init.end(), static_cast<uint8_t>(0));
537 std::fill(st_used.begin(), st_used.end(), static_cast<uint8_t>(0));
538 std::fill(past_rain.begin(), past_rain.end(), 0.0);
539 std::fill(past_rain_accum.begin(), past_rain_accum.end(), 0.0);
540 std::fill(past_rain_interval.begin(), past_rain_interval.end(), 0.0);
541 std::fill(cumul_rain_accum.begin(), cumul_rain_accum.end(), 0.0);
542 // co_gage_index is static (set at parse time), not reset between runs
543 }
544};
545
546} /* namespace openswmm */
547
548#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 ExchangeLaw.hpp:60
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:240
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:314
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:252
std::vector< double > snow_factor
Snow catch deficiency correction factor (1.0 = no correction).
Definition GageData.hpp:205
std::vector< double > file_start_date
[RAINGAGES] FILE StartDate (OADate day, 0 = none / *).
Definition GageData.hpp:176
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:290
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 (full file record).
Definition GageData.hpp:187
void reset_state() noexcept
Clear dynamic rain values, state-machine cursors and antecedent history.
Definition GageData.hpp:528
int count() const noexcept
Count rain gages from the source-type column.
Definition GageData.hpp:333
std::vector< double > next_rain_date
Simulation time of the next recorded value (decimal days).
Definition GageData.hpp:246
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)
Extend rain-gage arrays to a requested total while preserving their prefixes.
Definition GageData.hpp:393
std::vector< double > scale_factor
Rainfall scaling factor (1.0 = no scaling).
Definition GageData.hpp:218
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:323
std::vector< double > st_rain
Definition GageData.hpp:282
std::vector< uint8_t > st_used
Definition GageData.hpp:284
std::vector< int > interval_sec
Recording interval in seconds.
Definition GageData.hpp:199
std::vector< double > past_rain_accum
Per-gage accumulator for the current partial hour.
Definition GageData.hpp:296
std::vector< int > st_next
Definition GageData.hpp:281
std::vector< double > next_rainfall
Rainfall rate at the next recorded interval (for interpolation).
Definition GageData.hpp:234
void shrink_to_fit()
Release excess vector capacity accumulated during parsing.
Definition GageData.hpp:490
std::vector< uint8_t > st_init
Definition GageData.hpp:283
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:193
std::vector< double > past_rain_interval
Per-gage seconds in the current partial hour (0..3600).
Definition GageData.hpp:299
std::vector< int > rain_type
Rain data type: 0=INTENSITY, 1=VOLUME, 2=CUMULATIVE.
Definition GageData.hpp:105
std::vector< int > st_cur
Legacy rain-gage state machine (gage_initState / gage_setState / getNextRainfall),...
Definition GageData.hpp:280
void erase_at(int idx)
Erase a gage from the columns visited by this helper.
Definition GageData.hpp:445
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:307
std::vector< double > rainfall
Current rainfall rate (project length/time units — inches or mm/hr).
Definition GageData.hpp:228
std::vector< RainSource > source
Precipitation source (TIMESERIES or FILE).
Definition GageData.hpp:108
void resize(int n)
Replace rain-gage configuration and runtime arrays with defaults.
Definition GageData.hpp:344
std::vector< double > past_rain
Flat 2D: [gage * MAXPASTRAIN + hour]. Hourly rain totals.
Definition GageData.hpp:293
Definition TableData.hpp:154