OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
OutputReader.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
40
41#ifndef OPENSWMM_ENGINE_OUTPUT_READER_HPP
42#define OPENSWMM_ENGINE_OUTPUT_READER_HPP
43
44#include <cstdio>
45#include <cstdint>
46#include <string>
47#include <vector>
48
57
58namespace openswmm {
59
69public:
71 OutputReader() = default;
74
76 OutputReader(const OutputReader&) = delete;
79
90 bool open(const char* path);
91
109 bool openLive(const char* path);
110
125 int refresh();
126
130 bool is_live() const noexcept { return live_; }
131
138 void close();
139
142 bool is_open() const noexcept { return file_ != nullptr; }
143
144 // -- Metadata ---------------------------------------------------------
145
149 int version() const noexcept { return version_; }
153 int flow_units() const noexcept { return flow_units_; }
157 int subcatch_count() const noexcept { return n_subcatch_; }
161 int node_count() const noexcept { return n_nodes_; }
165 int link_count() const noexcept { return n_links_; }
169 int pollut_count() const noexcept { return n_polluts_; }
173 int period_count() const noexcept { return n_periods_; }
177 double start_date() const noexcept { return start_date_; }
181 int report_step() const noexcept { return report_step_; }
185 int error_code() const noexcept { return error_code_; }
186
191 bool has_subcatch_variable(int code) const {
192 const int i = subcatchColumn(code);
193 return i >= 0 && i < n_subcatch_vars_;
194 }
195
198 int subcatch_var_count() const noexcept { return n_subcatch_vars_; }
202 int node_var_count() const noexcept { return n_node_vars_; }
206 int link_var_count() const noexcept { return n_link_vars_; }
210 int system_var_count() const noexcept { return n_system_vars_; }
211
212 // -- Object IDs -------------------------------------------------------
213
218 const char* subcatch_id(int index) const;
223 const char* node_id(int index) const;
228 const char* link_id(int index) const;
229
247 const char* pollut_id(int index) const;
252 int pollut_units(int index) const;
253
254 // -- Per-period results (all objects, one variable) --------------------
255
263 bool get_subcatch_result(int period, int var, float* values) const;
264
273 bool get_node_result(int period, int var, float* values) const;
274
283 bool get_link_result(int period, int var, float* values) const;
284
293 bool get_system_result(int period, int var, float* value) const;
294
304 bool get_node_block(int period, std::vector<float>& values) const;
311 bool get_link_block(int period, std::vector<float>& values) const;
312
313 // -- Time series (one object, one variable, period range) --------------
314
324 bool get_subcatch_series(int obj_idx, int var, int start_period, int end_period,
325 float* values) const;
326
336 bool get_node_series(int obj_idx, int var, int start_period, int end_period,
337 float* values) const;
338
348 bool get_link_series(int obj_idx, int var, int start_period, int end_period,
349 float* values) const;
350
359 bool get_system_series(int var, int start_period, int end_period, float* values) const;
360
361 // -- Per-object all-variables at one period ----------------------------
362
372 bool get_subcatch_attribute(int obj_idx, int period, float* values, int* count) const;
373
382 bool get_node_attribute(int obj_idx, int period, float* values, int* count) const;
383
392 bool get_link_attribute(int obj_idx, int period, float* values, int* count) const;
393
394 // -- Period time -------------------------------------------------------
395
402 bool get_period_time(int period, double* time) const;
403
404 // -- Per-node summary statistics — Slice QA-01 -------------------------
405 //
406 // Computed on-demand from the per-period node results stored in the
407 // file. See openswmm_output.h for unit conventions + the report-step
408 // vs. routing-step precision caveat. Each method is O(n_periods).
416 bool get_node_stat_max_depth(int node_idx, double* value) const;
424 bool get_node_stat_max_overflow(int node_idx, double* value) const;
434 bool get_node_stat_vol_flooded(int node_idx, double* value) const;
442 bool get_node_stat_time_flooded(int node_idx, double* value) const;
443
444private:
445 static constexpr int32_t MAGIC_NUMBER = 516114522;
446
447 FILE* file_ = nullptr;
448
449 // Header fields
450 int version_ = 0;
451 int flow_units_ = 0;
452 int n_subcatch_ = 0;
453 int n_nodes_ = 0;
454 int n_links_ = 0;
455 int n_polluts_ = 0;
456
457 // Variable counts (read from file)
458 int n_subcatch_vars_ = 0;
459 std::vector<int> subcatch_codes_;
463 int subcatchColumn(int code) const;
464 int n_node_vars_ = 0;
465 int n_link_vars_ = 0;
466 int n_system_vars_ = 0;
467
468 // Report metadata
469 double start_date_ = 0.0;
470 int report_step_ = 0;
471
472 // Footer fields
473 long id_start_pos_ = 0;
474 long input_start_pos_ = 0;
475 long output_start_pos_ = 0;
476 int n_periods_ = 0;
477 int error_code_ = 0;
478
479 // Computed: bytes per period record
480 long bytes_per_period_ = 0;
481
482 // openLive(): period count comes from the file size until the footer lands
483 bool live_ = false;
484
485 // Object IDs
486 std::vector<std::string> subcatch_ids_;
487 std::vector<std::string> node_ids_;
488 std::vector<std::string> link_ids_;
489 std::vector<std::string> pollut_ids_;
490 std::vector<int> pollut_units_;
491
492 // -- Helpers ----------------------------------------------------------
493
503 bool readResultBlock(int period, int objectType, int count, int variables,
504 std::vector<float>& values) const;
509 bool readFooter();
513 bool readHeader();
519 bool readIDs();
527 bool readVariableCodes();
528
533 bool readInt4(int32_t& value) const;
538 bool readReal4(float& value) const;
543 bool readReal8(double& value) const;
544
549 long periodOffset(int period) const;
550
559 bool seekToVar(int period, int obj_type, int obj_idx, int var) const;
560};
561
562} /* namespace openswmm */
563
564#endif /* OPENSWMM_ENGINE_OUTPUT_READER_HPP */
bool get_subcatch_attribute(int obj_idx, int period, float *values, int *count) const
All variables for one subcatchment at one period.
Definition OutputReader.cpp:422
int node_var_count() const noexcept
Read retained parsed metadata without performing IO.
Definition OutputReader.hpp:202
bool get_subcatch_series(int obj_idx, int var, int start_period, int end_period, float *values) const
Read an inclusive report-period series for one subcatch variable.
Definition OutputReader.cpp:339
OutputReader()=default
Construct an unopened reader with zero metadata and empty identifier arrays.
bool is_live() const noexcept
True while opened via openLive() and the footer has not yet been seen.
Definition OutputReader.hpp:130
bool get_node_stat_max_depth(int node_idx, double *value) const
Compute a node summary from its complete stored report-period series.
Definition OutputReader.cpp:810
OutputReader & operator=(const OutputReader &)=delete
Disable assignment of the owned FILE cursor and buffers.
int system_var_count() const noexcept
Read retained parsed metadata without performing IO.
Definition OutputReader.hpp:210
int subcatch_var_count() const noexcept
Read retained parsed metadata without performing IO.
Definition OutputReader.hpp:198
bool get_node_stat_max_overflow(int node_idx, double *value) const
Compute a node summary from its complete stored report-period series.
Definition OutputReader.cpp:833
bool get_node_stat_vol_flooded(int node_idx, double *value) const
Compute a node summary from its complete stored report-period series.
Definition OutputReader.cpp:856
const char * subcatch_id(int index) const
Borrow one subcatch identifier stored by the file.
Definition OutputReader.cpp:205
bool get_link_result(int period, int var, float *values) const
Read one link variable for all links at a period.
Definition OutputReader.cpp:282
double start_date() const noexcept
Read retained parsed metadata without performing IO.
Definition OutputReader.hpp:177
bool get_link_attribute(int obj_idx, int period, float *values, int *count) const
All variables for one link at one period.
Definition OutputReader.cpp:464
int node_count() const noexcept
Read retained parsed metadata without performing IO.
Definition OutputReader.hpp:161
const char * pollut_id(int index) const
Species (pollutant) ID at a column index, or nullptr.
Definition OutputReader.cpp:223
void close()
Close the file and release resources.
Definition OutputReader.cpp:187
const char * link_id(int index) const
Borrow one link identifier stored by the file.
Definition OutputReader.cpp:217
bool get_node_stat_time_flooded(int node_idx, double *value) const
Compute a node summary from its complete stored report-period series.
Definition OutputReader.cpp:885
const char * node_id(int index) const
Borrow one node identifier stored by the file.
Definition OutputReader.cpp:211
bool get_node_series(int obj_idx, int var, int start_period, int end_period, float *values) const
Read an inclusive report-period series for one node variable.
Definition OutputReader.cpp:360
bool get_link_series(int obj_idx, int var, int start_period, int end_period, float *values) const
Read an inclusive report-period series for one link variable.
Definition OutputReader.cpp:380
bool get_node_result(int period, int var, float *values) const
Read one node variable for all nodes at a period.
Definition OutputReader.cpp:265
int pollut_count() const noexcept
Read retained parsed metadata without performing IO.
Definition OutputReader.hpp:169
int flow_units() const noexcept
Read retained parsed metadata without performing IO.
Definition OutputReader.hpp:153
int link_count() const noexcept
Read retained parsed metadata without performing IO.
Definition OutputReader.hpp:165
bool open(const char *path)
Open and parse the header/footer of a binary output file.
Definition OutputReader.cpp:51
bool get_system_result(int period, int var, float *value) const
Read one system variable at a period.
Definition OutputReader.cpp:299
bool has_subcatch_variable(int code) const
Test presence of a stored subcatchment variable code.
Definition OutputReader.hpp:191
int report_step() const noexcept
Read retained parsed metadata without performing IO.
Definition OutputReader.hpp:181
int link_var_count() const noexcept
Read retained parsed metadata without performing IO.
Definition OutputReader.hpp:206
int refresh()
Re-count periods on a live file.
Definition OutputReader.cpp:145
bool get_period_time(int period, double *time) const
Get the simulation time for a given period.
Definition OutputReader.cpp:487
int version() const noexcept
Read retained parsed metadata without performing IO.
Definition OutputReader.hpp:149
int subcatch_count() const noexcept
Read retained parsed metadata without performing IO.
Definition OutputReader.hpp:157
bool is_open() const noexcept
Whether the reader has a file open and valid.
Definition OutputReader.hpp:142
bool get_node_attribute(int obj_idx, int period, float *values, int *count) const
All variables for one node at one period.
Definition OutputReader.cpp:445
bool openLive(const char *path)
Open a file that is still being written (no footer yet).
Definition OutputReader.cpp:100
OutputReader(const OutputReader &)=delete
Disable copying of the owned FILE cursor and buffers.
bool get_node_block(int period, std::vector< float > &values) const
Read all node/link variables at one period with one contiguous read. Values are object-major: values[...
Definition OutputReader.cpp:327
int error_code() const noexcept
Read retained parsed metadata without performing IO.
Definition OutputReader.hpp:185
~OutputReader()
Close the file and destroy all reader-owned ID and code buffers.
Definition OutputReader.cpp:47
bool get_link_block(int period, std::vector< float > &values) const
Read the contiguous object-major link result block.
Definition OutputReader.cpp:331
bool get_subcatch_result(int period, int var, float *values) const
Read one subcatch variable at one report period.
Definition OutputReader.cpp:247
int pollut_units(int index) const
Read a stored pollutant concentration-unit code.
Definition OutputReader.cpp:229
bool get_system_series(int var, int start_period, int end_period, float *values) const
Read an inclusive report-period series for one system variable.
Definition OutputReader.cpp:400
int period_count() const noexcept
Read retained parsed metadata without performing IO.
Definition OutputReader.hpp:173
Definition ExchangeLaw.hpp:60