48#ifndef OPENSWMM_ENGINE_TABLE_DATA_HPP
49#define OPENSWMM_ENGINE_TABLE_DATA_HPP
55#include <unordered_map>
129 std::vector<double>
x;
130 std::vector<double>
y;
134 std::size_t
num_rows() const noexcept {
return x.size(); }
135 bool empty() const noexcept {
return x.empty(); }
148 std::vector<double>
x;
149 std::vector<double>
y;
188 std::size_t
size() const noexcept {
return x.size(); }
191 bool empty() const noexcept {
return x.empty(); }
220 const int n =
static_cast<int>(tbl.x.size());
221 if (n == 0)
return 0.0;
222 if (n == 1)
return tbl.y[0];
225 if (x_query <= tbl.x[0]) {
226 tbl.cursor.index = 0;
227 tbl.cursor.direction = +1;
235 if (x_query > tbl.x[n - 1]) {
236 tbl.cursor.index = n - 1;
237 tbl.cursor.direction = -1;
242 int idx = std::clamp(tbl.cursor.index, 0, n - 2);
245 while (idx < n - 1 && tbl.x[idx + 1] < x_query) {
247 tbl.cursor.direction = +1;
254 while (idx > 0 && tbl.x[idx] >= x_query) {
256 tbl.cursor.direction = -1;
259 tbl.cursor.index = idx;
266 const double x1 = tbl.x[idx], y1 = tbl.y[idx];
267 const double x2 = tbl.x[idx + 1], y2 = tbl.y[idx + 1];
268 const double dx = x2 - x1;
269 if (std::fabs(dx) < 1.0e-20)
return (y1 + y2) / 2.;
270 return y1 + (x_query - x1) * (y2 - y1) / dx;
291 const int n =
static_cast<int>(tbl.x.size());
292 if (n == 0)
return 0.0;
295 if (x_query <= tbl.x[0]) {
296 tbl.cursor.index = 0;
297 tbl.cursor.direction = +1;
298 return (x_query < tbl.x[0]) ? 0.0 : tbl.y[0];
300 if (x_query >= tbl.x[n - 1]) {
301 tbl.cursor.index = n - 1;
302 tbl.cursor.direction = -1;
303 return (x_query > tbl.x[n - 1]) ? 0.0 : tbl.y[n - 1];
325 const int n =
static_cast<int>(tbl.x.size());
326 if (n == 0)
return 0.0;
327 if (n == 1)
return tbl.y[0];
330 if (x_query < tbl.x[0]) {
331 tbl.cursor.index = 0;
332 tbl.cursor.direction = +1;
340 if (x_query >= tbl.x[n - 1]) {
341 tbl.cursor.index = n - 1;
342 tbl.cursor.direction = -1;
347 int idx = std::clamp(tbl.cursor.index, 0, n - 2);
349 while (idx < n - 1 && tbl.x[idx + 1] <= x_query) {
351 tbl.cursor.direction = +1;
353 while (idx > 0 && tbl.x[idx] > x_query) {
355 tbl.cursor.direction = -1;
358 tbl.cursor.index = idx;
375 const int n =
static_cast<int>(tbl.x.size());
376 if (n == 0 || depth <= 0.0)
return 0.0;
378 double x1 = tbl.x[0];
379 double a1 = tbl.y[0];
383 if (x1 < 1.0e-6)
return 0.0;
384 return (a1 / x1) * depth * depth / 2.0;
389 double dx = 0.0, dy = 0.0;
390 for (
int i = 1; i < n; ++i) {
391 double x2 = tbl.x[i];
392 double a2 = tbl.y[i];
398 const double dxi = x2 - x1;
400 if (std::fabs(dxi) < 1.0e-20) a = (a1 + a2) / 2.;
401 else a = a1 + (depth - x1) * (a2 - a1) / dxi;
402 return v + (a1 + a) / 2.0 * (depth - x1);
406 v += (a1 + a2) / 2.0 * dx;
414 double a = a1 + s * (depth - x1);
416 v -= a1 * a1 / s / 2.0;
418 v += (a1 + a) / 2.0 * (depth - x1);
441 const int n =
static_cast<int>(tbl.x.size());
442 if (n == 0 || volume <= 0.0)
return 0.0;
444 double x1 = tbl.x[0];
445 double a1 = tbl.y[0];
446 double v_accum = 0.0;
448 for (
int i = 1; i < n; ++i) {
449 double x2 = tbl.x[i];
450 double a2 = tbl.y[i];
451 double dv = (a1 + a2) / 2.0 * (x2 - x1);
453 if (v_accum + dv >= volume) {
459 double dv_need = volume - v_accum;
460 if (dx < 1.0e-10)
return x1;
462 double s = (a2 - a1) / dx;
464 if (std::fabs(s) < 1.0e-10) {
466 depth = (a1 > 0.0) ? x1 + dv_need / a1 : x1;
469 double disc = a1 * a1 + 2.0 * s * dv_need;
470 if (disc < 0.0) disc = 0.0;
471 depth = x1 + (-a1 + std::sqrt(disc)) / s;
473 return std::min(depth, x2);
482 if (tbl.x.size() >= 2) {
484 double dx = tbl.x[last] - tbl.x[last - 1];
485 double da = tbl.y[last] - tbl.y[last - 1];
486 double s = (dx > 1.0e-10) ? da / dx : 0.0;
487 double dv_need = volume - v_accum;
488 double a1_ext = tbl.y[last];
490 if (std::fabs(s) < 1.0e-10) {
491 depth = (a1_ext > 0.0) ? tbl.x[last] + dv_need / a1_ext : tbl.x[last];
493 double disc = a1_ext * a1_ext + 2.0 * s * dv_need;
494 if (disc < 0.0) disc = 0.0;
495 depth = tbl.x[last] + (-a1_ext + std::sqrt(disc)) / s;
517 const int n =
static_cast<int>(tbl.x.size());
518 if (n == 0)
return 0.0;
519 if (n == 1)
return tbl.x[0];
521 if (y_query <= tbl.y[0])
return tbl.x[0];
522 if (y_query >= tbl.y[n - 1])
return tbl.x[n - 1];
524 for (
int i = 1; i < n; ++i) {
525 if (tbl.y[i] >= y_query) {
526 double dy = tbl.y[i] - tbl.y[i - 1];
527 if (dy <= 0.0)
return tbl.x[i - 1];
528 double t = (y_query - tbl.y[i - 1]) / dy;
529 return tbl.x[i - 1] + t * (tbl.x[i] - tbl.x[i - 1]);
561 const int n =
static_cast<int>(tbl.x.size());
562 if (n == 0)
return 0.0;
564 double x1 = tbl.x[0];
565 double y1 = tbl.y[0];
568 if (x1 > 0.0)
return x_query / x1 * y1;
571 for (
int i = 1; i < n; ++i) {
572 const double x2 = tbl.x[i];
573 const double y2 = tbl.y[i];
574 if (x2 != x1) s = (y2 - y1) / (x2 - x1);
576 const double dx = x2 - x1;
577 if (std::fabs(dx) < 1.0e-20)
return (y1 + y2) / 2.;
578 return y1 + (x_query - x1) * (y2 - y1) / dx;
583 if (s < 0.0) s = 0.0;
584 return y1 + s * (x_query - x1);
605 const int n =
static_cast<int>(tbl.x.size());
606 if (n == 0)
return 0.0;
608 for (
int i = 0; i < n; ++i) {
609 if (tbl.x[i] > x_query)
return tbl.y[i];
630 const int n =
static_cast<int>(tbl.x.size());
631 if (n < 2)
return 0.0;
634 if (x_query <= tbl.x[0]) {
635 double dx = tbl.x[1] - tbl.x[0];
636 return (dx > 0.0) ? (tbl.y[1] - tbl.y[0]) / dx : 0.0;
640 if (x_query >= tbl.x[n - 1]) {
641 double dx = tbl.x[n - 1] - tbl.x[n - 2];
642 return (dx > 0.0) ? (tbl.y[n - 1] - tbl.y[n - 2]) / dx : 0.0;
645 for (
int i = 1; i < n; ++i) {
646 if (tbl.x[i] >= x_query) {
647 double dx = tbl.x[i] - tbl.x[i - 1];
648 return (dx > 0.0) ? (tbl.y[i] - tbl.y[i - 1]) / dx : 0.0;
668 const int n =
static_cast<int>(tbl.x.size());
669 if (n == 0)
return 0.0;
670 double ymax = tbl.y[0];
671 for (
int i = 1; i < n; ++i) {
672 if (tbl.y[i] < ymax)
break;
726 tables.push_back(std::move(t));
727 const int idx =
static_cast<int>(
tables.size()) - 1;
742 for (std::size_t i = 0; i <
tables.size(); ++i)
750 int find_by_kind(std::string_view name,
bool want_timeseries)
const noexcept {
751 const auto it =
by_name.find(name);
752 if (it ==
by_name.end())
return -1;
753 for (
const int i : it->second) {
756 if (is_ts == want_timeseries)
return i;
765 for (
auto& t :
tables) t.cursor.reset();
Carrier for an external file reference in a SWMM model.
Case-insensitive string helpers matching legacy SWMM name semantics.
Definition NodeCoupling.cpp:16
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:374
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:629
double table_getStorageDepth(Table &tbl, double volume) noexcept
Invert a storage area-vs-depth curve to find depth from volume.
Definition TableData.hpp:440
double table_step_cursor(Table &tbl, double x_query) noexcept
Piecewise-constant (step function) table lookup with cursor.
Definition TableData.hpp:324
double table_lookup_column(Table &tbl, int col_idx, double x_query)
Definition TableData.cpp:666
TableValidation validate_table(Table &tbl)
Definition TableData.cpp:298
double table_getMaxY(const Table &tbl) noexcept
Return the maximum y value in the non-decreasing leading portion.
Definition TableData.hpp:667
bool table_open_file(Table &tbl, std::size_t boundary_rows)
Definition TableData.cpp:426
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:604
@ 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:551
double table_inverseLookup(const Table &tbl, double y_query) noexcept
Inverse lookup: given y, find corresponding x by linear interpolation.
Definition TableData.hpp:516
double table_lookup_cursor(Table &tbl, double x_query) noexcept
Look up a value in a Table using the bidirectional cursor.
Definition TableData.hpp:219
std::size_t table_load_cache(Table &tbl, std::size_t start_row)
Definition TableData.cpp:551
double table_tseries_lookup_cursor(Table &tbl, double x_query) noexcept
Time-series linear interpolation with out-of-range → 0 behaviour.
Definition TableData.hpp:290
double table_step_column(Table &tbl, int col_idx, double x_query)
Definition TableData.cpp:719
Transparent case-insensitive equality for unordered containers.
Definition StringCase.hpp:79
Transparent case-insensitive hash (FNV-1a over the uppercase fold).
Definition StringCase.hpp:65
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:128
bool empty() const noexcept
Definition TableData.hpp:135
std::size_t num_rows() const noexcept
Definition TableData.hpp:134
std::size_t file_row_start
Row offset in file (cache only)
Definition TableData.hpp:132
std::vector< double > x
Independent variable values.
Definition TableData.hpp:129
int num_cols
Number of value columns.
Definition TableData.hpp:131
std::vector< double > y
Dependent values (flat: row-major, num_cols per row)
Definition TableData.hpp:130
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
Definition TableData.hpp:107
SoA collection of all time series and curves in the model.
Definition TableData.hpp:690
std::vector< Table > tables
All tables in index order.
Definition TableData.hpp:691
std::size_t count() const noexcept
Definition TableData.hpp:713
std::unordered_map< std::string, std::vector< int >, CiHash, CiEqual > by_name
Case-insensitive name → table indices, ascending.
Definition TableData.hpp:711
int add(const std::string &id, TableType type)
Add a new empty table with the given ID and type.
Definition TableData.hpp:722
void reset_cursors() noexcept
Reset all cursors (call before re-running a simulation).
Definition TableData.hpp:764
void rebuild_index()
Rebuilds by_name from tables.
Definition TableData.hpp:739
Table & operator[](int idx)
Definition TableData.hpp:715
int find_by_kind(std::string_view name, bool want_timeseries) const noexcept
Lowest-indexed table with this name and kind; -1 if none.
Definition TableData.hpp:750
const Table & operator[](int idx) const
Definition TableData.hpp:716
Definition TableData.hpp:138
long data_start_offset
File offset to first data row.
Definition TableData.hpp:180
std::size_t total_rows
Total data rows in file.
Definition TableData.hpp:178
TableBlock cache
Sliding cache window for file lookups.
Definition TableData.hpp:173
std::vector< long > row_offsets
Sparse byte-offset index into file.
Definition TableData.hpp:181
std::string comment
Object comment from the INP file (';'-prefixed lines immediately above the first row of this table),...
Definition TableData.hpp:147
std::size_t num_cache_rows
Cache window size (rows)
Definition TableData.hpp:179
std::unordered_map< std::string, int > column_map
Column name → index.
Definition TableData.hpp:183
TableCursor cursor
Bidirectional lookup cursor.
Definition TableData.hpp:150
TableType type
Table type (TIMESERIES, CURVE_*, etc.)
Definition TableData.hpp:140
bool empty() const noexcept
True if the table has at least one data point.
Definition TableData.hpp:191
double dx_min
Minimum inter-entry x spacing.
Definition TableData.hpp:174
double rel_anchor
start_date offset currently baked into them
Definition TableData.hpp:164
std::string id
Table identifier (from input file)
Definition TableData.hpp:139
double x_max
Maximum x value in file.
Definition TableData.hpp:176
std::vector< double > y
Dependent variable (flow, volume, etc.)
Definition TableData.hpp:149
std::FILE * file_handle
Open file handle (owned)
Definition TableData.hpp:168
FilePathPair file_path
Definition TableData.hpp:169
bool is_file_based
True if data is read from external file.
Definition TableData.hpp:167
TableBlock last_boundary
Last rows from file (for validation)
Definition TableData.hpp:172
int n_relative
Leading rows authored as elapsed time-of-start.
Definition TableData.hpp:163
static constexpr std::size_t INDEX_STRIDE
Rows between offset index entries.
Definition TableData.hpp:185
std::vector< double > x
Independent variable (time, depth, etc.)
Definition TableData.hpp:148
double x_min
Minimum x value in file.
Definition TableData.hpp:175
TableBlock first_boundary
First rows from file (for validation)
Definition TableData.hpp:171
int num_cols
Number of value columns.
Definition TableData.hpp:177
std::size_t size() const noexcept
Number of data points.
Definition TableData.hpp:188
std::vector< std::string > column_ids
Column identifiers.
Definition TableData.hpp:182
Definition TableData.hpp:773
std::vector< std::string > errors
Definition TableData.hpp:775
std::vector< std::string > warnings
Definition TableData.hpp:776
bool valid
Definition TableData.hpp:774