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
49namespace openswmm {
50
56public:
57 OutputReader() = default;
59
60 OutputReader(const OutputReader&) = delete;
62
68 bool open(const char* path);
69
82 bool openLive(const char* path);
83
94 int refresh();
95
98 bool is_live() const noexcept { return live_; }
99
103 void close();
104
106 bool is_open() const noexcept { return file_ != nullptr; }
107
108 // -- Metadata ---------------------------------------------------------
109
110 int version() const noexcept { return version_; }
111 int flow_units() const noexcept { return flow_units_; }
112 int subcatch_count() const noexcept { return n_subcatch_; }
113 int node_count() const noexcept { return n_nodes_; }
114 int link_count() const noexcept { return n_links_; }
115 int pollut_count() const noexcept { return n_polluts_; }
116 int period_count() const noexcept { return n_periods_; }
117 double start_date() const noexcept { return start_date_; }
118 int report_step() const noexcept { return report_step_; }
119 int error_code() const noexcept { return error_code_; }
120
121 int subcatch_var_count() const noexcept { return n_subcatch_vars_; }
122 int node_var_count() const noexcept { return n_node_vars_; }
123 int link_var_count() const noexcept { return n_link_vars_; }
124 int system_var_count() const noexcept { return n_system_vars_; }
125
126 // -- Object IDs -------------------------------------------------------
127
128 const char* subcatch_id(int index) const;
129 const char* node_id(int index) const;
130 const char* link_id(int index) const;
131
145 const char* pollut_id(int index) const;
146
147 // -- Per-period results (all objects, one variable) --------------------
148
154 bool get_subcatch_result(int period, int var, float* values) const;
155
157 bool get_node_result(int period, int var, float* values) const;
158
160 bool get_link_result(int period, int var, float* values) const;
161
163 bool get_system_result(int period, int var, float* value) const;
164
165 // -- Time series (one object, one variable, period range) --------------
166
167 bool get_subcatch_series(int obj_idx, int var,
168 int start_period, int end_period,
169 float* values) const;
170
171 bool get_node_series(int obj_idx, int var,
172 int start_period, int end_period,
173 float* values) const;
174
175 bool get_link_series(int obj_idx, int var,
176 int start_period, int end_period,
177 float* values) const;
178
179 bool get_system_series(int var,
180 int start_period, int end_period,
181 float* values) const;
182
183 // -- Per-object all-variables at one period ----------------------------
184
186 bool get_subcatch_attribute(int obj_idx, int period,
187 float* values, int* count) const;
188
190 bool get_node_attribute(int obj_idx, int period,
191 float* values, int* count) const;
192
194 bool get_link_attribute(int obj_idx, int period,
195 float* values, int* count) const;
196
197 // -- Period time -------------------------------------------------------
198
200 bool get_period_time(int period, double* time) const;
201
202 // -- Per-node summary statistics — Slice QA-01 -------------------------
203 //
204 // Computed on-demand from the per-period node results stored in the
205 // file. See openswmm_output.h for unit conventions + the report-step
206 // vs. routing-step precision caveat. Each method is O(n_periods).
207 bool get_node_stat_max_depth(int node_idx, double* value) const;
208 bool get_node_stat_max_overflow(int node_idx, double* value) const;
209 bool get_node_stat_vol_flooded(int node_idx, double* value) const;
210 bool get_node_stat_time_flooded(int node_idx, double* value) const;
211
212private:
213 static constexpr int32_t MAGIC_NUMBER = 516114522;
214
215 FILE* file_ = nullptr;
216
217 // Header fields
218 int version_ = 0;
219 int flow_units_ = 0;
220 int n_subcatch_ = 0;
221 int n_nodes_ = 0;
222 int n_links_ = 0;
223 int n_polluts_ = 0;
224
225 // Variable counts (read from file)
226 int n_subcatch_vars_ = 0;
227 int n_node_vars_ = 0;
228 int n_link_vars_ = 0;
229 int n_system_vars_ = 0;
230
231 // Report metadata
232 double start_date_ = 0.0;
233 int report_step_ = 0;
234
235 // Footer fields
236 long id_start_pos_ = 0;
237 long input_start_pos_ = 0;
238 long output_start_pos_ = 0;
239 int n_periods_ = 0;
240 int error_code_ = 0;
241
242 // Computed: bytes per period record
243 long bytes_per_period_ = 0;
244
245 // openLive(): period count comes from the file size until the footer lands
246 bool live_ = false;
247
248 // Object IDs
249 std::vector<std::string> subcatch_ids_;
250 std::vector<std::string> node_ids_;
251 std::vector<std::string> link_ids_;
252 std::vector<std::string> pollut_ids_;
253
254 // -- Helpers ----------------------------------------------------------
255
256 bool readFooter();
257 bool readHeader();
258 bool readIDs();
259 bool readVariableCodes();
260
261 bool readInt4(int32_t& value) const;
262 bool readReal4(float& value) const;
263 bool readReal8(double& value) const;
264
266 long periodOffset(int period) const;
267
274 bool seekToVar(int period, int obj_type, int obj_idx, int var) const;
275};
276
277} /* namespace openswmm */
278
279#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:314
int node_var_count() const noexcept
Definition OutputReader.hpp:122
bool get_subcatch_series(int obj_idx, int var, int start_period, int end_period, float *values) const
Definition OutputReader.cpp:251
bool is_live() const noexcept
True while opened via openLive() and the footer has not yet been seen.
Definition OutputReader.hpp:98
bool get_node_stat_max_depth(int node_idx, double *value) const
Definition OutputReader.cpp:653
OutputReader & operator=(const OutputReader &)=delete
int system_var_count() const noexcept
Definition OutputReader.hpp:124
int subcatch_var_count() const noexcept
Definition OutputReader.hpp:121
bool get_node_stat_max_overflow(int node_idx, double *value) const
Definition OutputReader.cpp:670
bool get_node_stat_vol_flooded(int node_idx, double *value) const
Definition OutputReader.cpp:687
const char * subcatch_id(int index) const
Definition OutputReader.cpp:177
bool get_link_result(int period, int var, float *values) const
Read one link variable for all links at a period.
Definition OutputReader.cpp:226
double start_date() const noexcept
Definition OutputReader.hpp:117
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:342
int node_count() const noexcept
Definition OutputReader.hpp:113
const char * pollut_id(int index) const
Species (pollutant) ID at a column index, or nullptr.
Definition OutputReader.cpp:192
void close()
Close the file and release resources.
Definition OutputReader.cpp:161
const char * link_id(int index) const
Definition OutputReader.cpp:187
bool get_node_stat_time_flooded(int node_idx, double *value) const
Definition OutputReader.cpp:710
const char * node_id(int index) const
Definition OutputReader.cpp:182
bool get_node_series(int obj_idx, int var, int start_period, int end_period, float *values) const
Definition OutputReader.cpp:266
bool get_link_series(int obj_idx, int var, int start_period, int end_period, float *values) const
Definition OutputReader.cpp:281
bool get_node_result(int period, int var, float *values) const
Read one node variable for all nodes at a period.
Definition OutputReader.cpp:214
int pollut_count() const noexcept
Definition OutputReader.hpp:115
int flow_units() const noexcept
Definition OutputReader.hpp:111
int link_count() const noexcept
Definition OutputReader.hpp:114
bool open(const char *path)
Open and parse the header/footer of a binary output file.
Definition OutputReader.cpp:50
bool get_system_result(int period, int var, float *value) const
Read one system variable at a period.
Definition OutputReader.cpp:238
int report_step() const noexcept
Definition OutputReader.hpp:118
int link_var_count() const noexcept
Definition OutputReader.hpp:123
int refresh()
Re-count periods on a live file.
Definition OutputReader.cpp:120
bool get_period_time(int period, double *time) const
Get the simulation time for a given period.
Definition OutputReader.cpp:360
int version() const noexcept
Definition OutputReader.hpp:110
int subcatch_count() const noexcept
Definition OutputReader.hpp:112
bool is_open() const noexcept
Whether the reader has a file open and valid.
Definition OutputReader.hpp:106
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:328
bool openLive(const char *path)
Open a file that is still being written (no footer yet).
Definition OutputReader.cpp:84
OutputReader(const OutputReader &)=delete
int error_code() const noexcept
Definition OutputReader.hpp:119
~OutputReader()
Definition OutputReader.cpp:46
bool get_subcatch_result(int period, int var, float *values) const
Read one subcatch variable for all subcatchments at a period.
Definition OutputReader.cpp:202
bool get_system_series(int var, int start_period, int end_period, float *values) const
Definition OutputReader.cpp:296
int period_count() const noexcept
Definition OutputReader.hpp:116
Definition NodeCoupling.cpp:16