48#ifndef OPENSWMM_ENGINE_TABLE_DATA_HPP
49#define OPENSWMM_ENGINE_TABLE_DATA_HPP
55#include <unordered_map>
137 std::vector<double>
x;
138 std::vector<double>
y;
146 std::size_t
num_rows() const noexcept {
return x.size(); }
151 bool empty() const noexcept {
return x.empty(); }
164 std::vector<double>
x;
165 std::vector<double>
y;
204 std::size_t
size() const noexcept {
return x.size(); }
207 bool empty() const noexcept {
return x.empty(); }
236 const int n =
static_cast<int>(tbl.x.size());
243 if (x_query <= tbl.x[0]) {
244 tbl.cursor.index = 0;
245 tbl.cursor.direction = +1;
253 if (x_query > tbl.x[n - 1]) {
254 tbl.cursor.index = n - 1;
255 tbl.cursor.direction = -1;
260 int idx = std::clamp(tbl.cursor.index, 0, n - 2);
263 while (idx < n - 1 && tbl.x[idx + 1] < x_query) {
265 tbl.cursor.direction = +1;
272 while (idx > 0 && tbl.x[idx] >= x_query) {
274 tbl.cursor.direction = -1;
277 tbl.cursor.index = idx;
284 const double x1 = tbl.x[idx], y1 = tbl.y[idx];
285 const double x2 = tbl.x[idx + 1], y2 = tbl.y[idx + 1];
286 const double dx = x2 - x1;
287 if (std::fabs(dx) < 1.0e-20)
288 return (y1 + y2) / 2.;
289 return y1 + (x_query - x1) * (y2 - y1) / dx;
310 const int n =
static_cast<int>(tbl.x.size());
315 if (x_query <= tbl.x[0]) {
316 tbl.cursor.index = 0;
317 tbl.cursor.direction = +1;
318 return (x_query < tbl.x[0]) ? 0.0 : tbl.y[0];
320 if (x_query >= tbl.x[n - 1]) {
321 tbl.cursor.index = n - 1;
322 tbl.cursor.direction = -1;
323 return (x_query > tbl.x[n - 1]) ? 0.0 : tbl.y[n - 1];
345 const int n =
static_cast<int>(tbl.x.size());
352 if (x_query < tbl.x[0]) {
353 tbl.cursor.index = 0;
354 tbl.cursor.direction = +1;
362 if (x_query >= tbl.x[n - 1]) {
363 tbl.cursor.index = n - 1;
364 tbl.cursor.direction = -1;
369 int idx = std::clamp(tbl.cursor.index, 0, n - 2);
371 while (idx < n - 1 && tbl.x[idx + 1] <= x_query) {
373 tbl.cursor.direction = +1;
375 while (idx > 0 && tbl.x[idx] > x_query) {
377 tbl.cursor.direction = -1;
380 tbl.cursor.index = idx;
397 const int n =
static_cast<int>(tbl.x.size());
398 if (n == 0 || depth <= 0.0)
401 double x1 = tbl.x[0];
402 double a1 = tbl.y[0];
408 return (a1 / x1) * depth * depth / 2.0;
413 double dx = 0.0, dy = 0.0;
414 for (
int i = 1; i < n; ++i) {
415 double x2 = tbl.x[i];
416 double a2 = tbl.y[i];
422 const double dxi = x2 - x1;
424 if (std::fabs(dxi) < 1.0e-20)
427 a = a1 + (depth - x1) * (a2 - a1) / dxi;
428 return v + (a1 + a) / 2.0 * (depth - x1);
432 v += (a1 + a2) / 2.0 * dx;
440 double a = a1 + s * (depth - x1);
442 v -= a1 * a1 / s / 2.0;
444 v += (a1 + a) / 2.0 * (depth - x1);
467 const int n =
static_cast<int>(tbl.x.size());
468 if (n == 0 || volume <= 0.0)
471 double x1 = tbl.x[0];
472 double a1 = tbl.y[0];
473 double v_accum = 0.0;
475 for (
int i = 1; i < n; ++i) {
476 double x2 = tbl.x[i];
477 double a2 = tbl.y[i];
478 double dv = (a1 + a2) / 2.0 * (x2 - x1);
480 if (v_accum + dv >= volume) {
486 double dv_need = volume - v_accum;
490 double s = (a2 - a1) / dx;
492 if (std::fabs(s) < 1.0e-10) {
494 depth = (a1 > 0.0) ? x1 + dv_need / a1 : x1;
497 double disc = a1 * a1 + 2.0 * s * dv_need;
500 depth = x1 + (-a1 + std::sqrt(disc)) / s;
502 return std::min(depth, x2);
511 if (tbl.x.size() >= 2) {
513 double dx = tbl.x[last] - tbl.x[last - 1];
514 double da = tbl.y[last] - tbl.y[last - 1];
515 double s = (dx > 1.0e-10) ? da / dx : 0.0;
516 double dv_need = volume - v_accum;
517 double a1_ext = tbl.y[last];
519 if (std::fabs(s) < 1.0e-10) {
520 depth = (a1_ext > 0.0) ? tbl.x[last] + dv_need / a1_ext : tbl.x[last];
522 double disc = a1_ext * a1_ext + 2.0 * s * dv_need;
525 depth = tbl.x[last] + (-a1_ext + std::sqrt(disc)) / s;
547 const int n =
static_cast<int>(tbl.x.size());
553 if (y_query <= tbl.y[0])
555 if (y_query >= tbl.y[n - 1])
558 for (
int i = 1; i < n; ++i) {
559 if (tbl.y[i] >= y_query) {
560 double dy = tbl.y[i] - tbl.y[i - 1];
563 double t = (y_query - tbl.y[i - 1]) / dy;
564 return tbl.x[i - 1] + t * (tbl.x[i] - tbl.x[i - 1]);
596 const int n =
static_cast<int>(tbl.x.size());
600 double x1 = tbl.x[0];
601 double y1 = tbl.y[0];
605 return x_query / x1 * y1;
609 for (
int i = 1; i < n; ++i) {
610 const double x2 = tbl.x[i];
611 const double y2 = tbl.y[i];
613 s = (y2 - y1) / (x2 - x1);
615 const double dx = x2 - x1;
616 if (std::fabs(dx) < 1.0e-20)
617 return (y1 + y2) / 2.;
618 return y1 + (x_query - x1) * (y2 - y1) / dx;
625 return y1 + s * (x_query - x1);
646 const int n =
static_cast<int>(tbl.x.size());
650 for (
int i = 0; i < n; ++i) {
651 if (tbl.x[i] > x_query)
673 const int n =
static_cast<int>(tbl.x.size());
678 if (x_query <= tbl.x[0]) {
679 double dx = tbl.x[1] - tbl.x[0];
680 return (dx > 0.0) ? (tbl.y[1] - tbl.y[0]) / dx : 0.0;
691 if (x_query > tbl.x[n - 1])
694 for (
int i = 1; i < n; ++i) {
695 if (tbl.x[i] >= x_query) {
696 double dx = tbl.x[i] - tbl.x[i - 1];
697 return (dx > 0.0) ? (tbl.y[i] - tbl.y[i - 1]) / dx : 0.0;
717 const int n =
static_cast<int>(tbl.x.size());
720 double ymax = tbl.y[0];
721 for (
int i = 1; i < n; ++i) {
803 tables.push_back(std::move(t));
804 const int idx =
static_cast<int>(
tables.size()) - 1;
819 for (std::size_t i = 0; i <
tables.size(); ++i)
833 int find_by_kind(std::string_view name,
bool want_timeseries)
const noexcept {
834 const auto it =
by_name.find(name);
837 for (
const int i : it->second) {
839 if (is_ts == want_timeseries)
Carrier for an external file reference in a SWMM model.
Case-insensitive string helpers matching legacy SWMM name semantics.
Definition ExchangeLaw.hpp:60
double table_getStorageVolume(Table &tbl, double depth) noexcept
Compute storage volume by trapezoidal integration of an area-vs-depth curve, matching legacy table_ge...
Definition TableData.hpp:396
TableType
Type of data stored in a Table.
Definition TableData.hpp:72
@ CURVE_PUMP5
Pump curve type 5 (head vs flow, variable speed)
Definition TableData.hpp:84
@ CURVE_PUMP1
Pump curve type 1 (ON/OFF depth)
Definition TableData.hpp:80
@ TIMESERIES
Rainfall, inflow, or other time-varying values.
Definition TableData.hpp:73
@ CURVE_PUMP4
Pump curve type 4 (depth vs speed)
Definition TableData.hpp:83
@ CURVE_RATING
Outfall/weir rating curve.
Definition TableData.hpp:76
@ CURVE_TIDAL
Tidal stage curve.
Definition TableData.hpp:79
@ CURVE_WEIR
Weir rating curve (legacy CurveTypeWords "WEIR")
Definition TableData.hpp:85
@ CURVE_SHAPE
Cross-section shape curve.
Definition TableData.hpp:77
@ CURVE_STORAGE
Storage node volume-depth curve.
Definition TableData.hpp:74
@ CURVE_DIVERSION
Diversion rating curve.
Definition TableData.hpp:75
@ CURVE_CONTROL
Control rule action curve.
Definition TableData.hpp:78
@ CURVE_PUMP2
Pump curve type 2 (head vs flow)
Definition TableData.hpp:81
@ CURVE_PUMP3
Pump curve type 3 (volume vs time)
Definition TableData.hpp:82
double table_getSlope(const Table &tbl, double x_query) noexcept
Compute the slope (dy/dx) at a given x value.
Definition TableData.hpp:672
double table_getStorageDepth(Table &tbl, double volume) noexcept
Invert a storage area-vs-depth curve to find depth from volume.
Definition TableData.hpp:466
double table_step_cursor(Table &tbl, double x_query) noexcept
Piecewise-constant (step function) table lookup with cursor.
Definition TableData.hpp:344
bool table_open_file(Table &tbl, std::size_t boundary_rows)
Open a legacy streaming table file and prepare sparse index/boundaries.
Definition TableData.cpp:473
double table_lookup_column(Table &tbl, int col_idx, double x_query)
Read and linearly interpolate a selected value column.
Definition TableData.cpp:719
TableValidation validate_table(Table &tbl)
Check table structure and derive its smallest positive x interval.
Definition TableData.cpp:344
double table_getMaxY(const Table &tbl) noexcept
Return the maximum y value in the non-decreasing leading portion.
Definition TableData.hpp:716
double table_intervalLookup(const Table &tbl, double x_query) noexcept
Step-function lookup: return y for the first x entry > x_query.
Definition TableData.hpp:645
@ TIMESERIES
Data from an in-file [TIMESERIES].
Definition GageData.hpp:58
double table_lookupEx(const Table &tbl, double x_query) noexcept
Linear-interpolating lookup with linear extrapolation outside bounds.
Definition TableData.hpp:586
double table_inverseLookup(const Table &tbl, double y_query) noexcept
Inverse lookup: given y, find corresponding x by linear interpolation.
Definition TableData.hpp:546
double table_lookup_cursor(Table &tbl, double x_query) noexcept
Look up a value in a Table using the bidirectional cursor.
Definition TableData.hpp:235
std::size_t table_load_cache(Table &tbl, std::size_t start_row)
Replace the streaming cache starting at a logical data-row offset.
Definition TableData.cpp:600
double table_tseries_lookup_cursor(Table &tbl, double x_query) noexcept
Time-series linear interpolation with out-of-range → 0 behaviour.
Definition TableData.hpp:309
double table_step_column(Table &tbl, int col_idx, double x_query)
Read and step-hold a selected value column.
Definition TableData.cpp:773
Transparent case-insensitive equality for unordered containers.
Definition StringCase.hpp:101
Transparent case-insensitive hash (FNV-1a over the uppercase fold).
Definition StringCase.hpp:80
Two-string carrier for any external file path that appears in a SWMM .inp file.
Definition FilePathPair.hpp:63
A single time series or rating curve.
Definition TableData.hpp:136
bool empty() const noexcept
Test whether a boundary/cache has no independent-variable rows.
Definition TableData.hpp:151
std::size_t num_rows() const noexcept
Count boundary/cache rows from the independent-variable vector.
Definition TableData.hpp:146
std::size_t file_row_start
Row offset in file (cache only)
Definition TableData.hpp:140
std::vector< double > x
Independent variable values.
Definition TableData.hpp:137
int num_cols
Number of value columns.
Definition TableData.hpp:139
std::vector< double > y
Dependent values (flat: row-major, num_cols per row)
Definition TableData.hpp:138
Bidirectional cursor tracking the last accessed index in a Table.
Definition TableData.hpp:103
int index
Index of the last successful lookup entry.
Definition TableData.hpp:104
int direction
Last seek direction: +1 = forward, -1 = backward.
Definition TableData.hpp:105
void reset() noexcept
Restore the lookup hint to row zero and forward direction.
Definition TableData.hpp:112
SoA collection of all time series and curves in the model.
Definition TableData.hpp:741
std::vector< Table > tables
All tables in index order.
Definition TableData.hpp:742
std::size_t count() const noexcept
Count stored time-series and curve tables.
Definition TableData.hpp:768
std::unordered_map< std::string, std::vector< int >, CiHash, CiEqual > by_name
Case-insensitive name → table indices, ascending.
Definition TableData.hpp:762
int add(const std::string &id, TableType type)
Append an empty table and index its copied name.
Definition TableData.hpp:799
void reset_cursors() noexcept
Reset all cursors (call before re-running a simulation).
Definition TableData.hpp:848
void rebuild_index()
Rebuilds by_name from tables.
Definition TableData.hpp:816
Table & operator[](int idx)
Borrow a mutable stored table.
Definition TableData.hpp:779
int find_by_kind(std::string_view name, bool want_timeseries) const noexcept
Find the first indexed table matching a case-insensitive name and kind.
Definition TableData.hpp:833
const Table & operator[](int idx) const
Borrow an immutable stored table.
Definition TableData.hpp:786
Definition TableData.hpp:154
long data_start_offset
File offset to first data row.
Definition TableData.hpp:196
std::size_t total_rows
Total data rows in file.
Definition TableData.hpp:194
TableBlock cache
Sliding cache window for file lookups.
Definition TableData.hpp:189
std::vector< long > row_offsets
Sparse byte-offset index into file.
Definition TableData.hpp:197
std::string comment
Object comment from the INP file (';'-prefixed lines immediately above the first row of this table),...
Definition TableData.hpp:163
std::size_t num_cache_rows
Cache window size (rows)
Definition TableData.hpp:195
std::unordered_map< std::string, int > column_map
Column name → index.
Definition TableData.hpp:199
TableCursor cursor
Bidirectional lookup cursor.
Definition TableData.hpp:166
TableType type
Table type (TIMESERIES, CURVE_*, etc.)
Definition TableData.hpp:156
bool empty() const noexcept
True if the table has at least one data point.
Definition TableData.hpp:207
double dx_min
Minimum inter-entry x spacing.
Definition TableData.hpp:190
double rel_anchor
start_date offset currently baked into them
Definition TableData.hpp:180
std::string id
Table identifier (from input file)
Definition TableData.hpp:155
double x_max
Maximum x value in file.
Definition TableData.hpp:192
std::vector< double > y
Dependent variable (flow, volume, etc.)
Definition TableData.hpp:165
std::FILE * file_handle
Open file handle (owned)
Definition TableData.hpp:184
FilePathPair file_path
Definition TableData.hpp:185
bool is_file_based
True if data is read from external file.
Definition TableData.hpp:183
TableBlock last_boundary
Last rows from file (for validation)
Definition TableData.hpp:188
int n_relative
Leading rows authored as elapsed time-of-start.
Definition TableData.hpp:179
static constexpr std::size_t INDEX_STRIDE
Rows between offset index entries.
Definition TableData.hpp:201
std::vector< double > x
Independent variable (time, depth, etc.)
Definition TableData.hpp:164
double x_min
Minimum x value in file.
Definition TableData.hpp:191
TableBlock first_boundary
First rows from file (for validation)
Definition TableData.hpp:187
int num_cols
Number of value columns.
Definition TableData.hpp:193
std::size_t size() const noexcept
Number of data points.
Definition TableData.hpp:204
std::vector< std::string > column_ids
Column identifiers.
Definition TableData.hpp:198
Definition TableData.hpp:858
std::vector< std::string > errors
Definition TableData.hpp:860
std::vector< std::string > warnings
Definition TableData.hpp:861
bool valid
Definition TableData.hpp:859