32#ifndef OPENSWMM_ENGINE_TABLE_DATA_HPP
33#define OPENSWMM_ENGINE_TABLE_DATA_HPP
105 std::vector<double>
x;
106 std::vector<double>
y;
110 std::size_t
size() const noexcept {
return x.size(); }
113 bool empty() const noexcept {
return x.empty(); }
142 const int n =
static_cast<int>(tbl.x.size());
143 if (n == 0)
return 0.0;
144 if (n == 1)
return tbl.y[0];
147 if (x_query <= tbl.x[0]) {
148 tbl.cursor.index = 0;
149 tbl.cursor.direction = +1;
154 if (x_query >= tbl.x[n - 1]) {
155 tbl.cursor.index = n - 1;
156 tbl.cursor.direction = -1;
161 int idx = std::clamp(tbl.cursor.index, 0, n - 2);
164 while (idx < n - 1 && tbl.x[idx + 1] < x_query) {
166 tbl.cursor.direction = +1;
170 while (idx > 0 && tbl.x[idx] > x_query) {
172 tbl.cursor.direction = -1;
175 tbl.cursor.index = idx;
178 const double dx = tbl.x[idx + 1] - tbl.x[idx];
179 if (dx <= 0.0)
return tbl.y[idx];
180 const double t = (x_query - tbl.x[idx]) / dx;
181 return tbl.y[idx] + t * (tbl.y[idx + 1] - tbl.y[idx]);
199 const int n =
static_cast<int>(tbl.x.size());
200 if (n == 0)
return 0.0;
201 if (n == 1)
return tbl.y[0];
204 if (x_query < tbl.x[0]) {
205 tbl.cursor.index = 0;
206 tbl.cursor.direction = +1;
214 if (x_query >= tbl.x[n - 1]) {
215 tbl.cursor.index = n - 1;
216 tbl.cursor.direction = -1;
221 int idx = std::clamp(tbl.cursor.index, 0, n - 2);
223 while (idx < n - 1 && tbl.x[idx + 1] <= x_query) {
225 tbl.cursor.direction = +1;
227 while (idx > 0 && tbl.x[idx] > x_query) {
229 tbl.cursor.direction = -1;
232 tbl.cursor.index = idx;
261 tables.push_back({id, type, {}, {}, {}});
262 return static_cast<int>(
tables.size()) - 1;
269 for (
auto& t :
tables) t.cursor.reset();
Definition Controls.cpp:24
TableType
Type of data stored in a Table.
Definition TableData.hpp:51
@ CURVE_PUMP1
Pump curve type 1 (ON/OFF depth)
@ CURVE_PUMP4
Pump curve type 4 (depth vs speed)
@ CURVE_RATING
Outfall/weir rating curve.
@ CURVE_TIDAL
Tidal stage curve.
@ CURVE_SHAPE
Cross-section shape curve.
@ CURVE_STORAGE
Storage node volume-depth curve.
@ CURVE_DIVERSION
Diversion rating curve.
@ CURVE_CONTROL
Control rule action curve.
@ CURVE_PUMP2
Pump curve type 2 (head vs flow)
@ CURVE_PUMP3
Pump curve type 3 (volume vs time)
double table_step_cursor(Table &tbl, double x_query) noexcept
Piecewise-constant (step function) table lookup with cursor.
Definition TableData.hpp:198
@ TIMESERIES
Data from an in-file [TIMESERIES].
double table_lookup_cursor(Table &tbl, double x_query) noexcept
Look up a value in a Table using the bidirectional cursor.
Definition TableData.hpp:141
Bidirectional cursor tracking the last accessed index in a Table.
Definition TableData.hpp:80
int index
Index of the last successful lookup entry.
Definition TableData.hpp:81
int direction
Last seek direction: +1 = forward, -1 = backward.
Definition TableData.hpp:82
void reset() noexcept
Definition TableData.hpp:84
SoA collection of all time series and curves in the model.
Definition TableData.hpp:248
std::vector< Table > tables
All tables in index order.
Definition TableData.hpp:249
std::size_t count() const noexcept
Definition TableData.hpp:251
int add(const std::string &id, TableType type)
Add a new empty table with the given ID and type.
Definition TableData.hpp:260
void reset_cursors() noexcept
Reset all cursors (call before re-running a simulation).
Definition TableData.hpp:268
Table & operator[](int idx)
Definition TableData.hpp:253
const Table & operator[](int idx) const
Definition TableData.hpp:254
A single time series or rating curve.
Definition TableData.hpp:102
TableCursor cursor
Bidirectional lookup cursor.
Definition TableData.hpp:107
TableType type
Table type (TIMESERIES, CURVE_*, etc.)
Definition TableData.hpp:104
bool empty() const noexcept
True if the table has at least one data point.
Definition TableData.hpp:113
std::string id
Table identifier (from input file)
Definition TableData.hpp:103
std::vector< double > y
Dependent variable (flow, volume, etc.)
Definition TableData.hpp:106
std::vector< double > x
Independent variable (time, depth, etc.)
Definition TableData.hpp:105
std::size_t size() const noexcept
Number of data points.
Definition TableData.hpp:110