OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
Default2DOutputPlugin.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
34
35#pragma once
36
37#ifdef OPENSWMM_HAS_2D
38
40
41#include <string>
42#include <vector>
43#include <hdf5.h>
44
45namespace openswmm::twoD {
46
47struct MeshData; // forward declaration
48struct SolverOptions2D; // forward declaration
49
136class Default2DOutputPlugin final : public IOutputPlugin {
137public:
142 explicit Default2DOutputPlugin(std::string h5_path);
143 ~Default2DOutputPlugin() override;
144
145 // Non-copyable
146 Default2DOutputPlugin(const Default2DOutputPlugin&) = delete;
147 Default2DOutputPlugin& operator=(const Default2DOutputPlugin&) = delete;
148
149 // -----------------------------------------------------------------------
150 // IOutputPlugin interface
151 // -----------------------------------------------------------------------
152
153 PluginState state() const noexcept override { return state_; }
154
155 int initialize(const std::vector<std::string>& init_args,
156 const IPluginComponentInfo* info) override;
157
158 int validate(const SimulationContext& ctx) override;
159
160 int prepare(const SimulationContext& ctx) override;
161
162 int update(const SimulationSnapshot& snapshot) override;
163
164 int finalize(const SimulationContext& ctx) override;
165
166 const char* last_error_message() const noexcept override {
167 return last_error_.c_str();
168 }
169
179 void prepareMeshAndDatasets(const MeshData& mesh);
180
194 void setMeshCoordinateScale(double metres_per_model_unit);
195
208 std::string configureOutput(const SolverOptions2D& opts, double report_step_sec);
209
211 unsigned reportVariables() const noexcept { return report_vars_; }
212
213private:
214 // ---- results-file size controls (configureOutput) ----
215 hid_t storage_type_ = H5T_NATIVE_DOUBLE;
216 int compression_ = 4;
217 unsigned report_vars_ = 0x7FFu;
218 std::vector<std::string> species_filter_;
219 std::vector<int> species_rows_;
220 double report_2d_step_days_ = 0.0;
221 double next_due_days_ = -1.0;
222 bool want(unsigned bit) const noexcept { return (report_vars_ & bit) != 0; }
224 void writeSpeciesRows(const double* all, hsize_t n_species_in);
225
226 std::string h5_path_;
227 PluginState state_ = PluginState::UNLOADED;
228 std::string last_error_;
229
230 std::string model_crs_;
231 double metres_per_model_unit_ = 1.0;
232
233 hid_t file_id_ = H5I_INVALID_HID;
234
235 // Dataset handles for time-varying fields (unlimited time dimension)
236 hid_t ds_time_ = H5I_INVALID_HID;
237 hid_t ds_face_depth_ = H5I_INVALID_HID;
238 hid_t ds_face_head_ = H5I_INVALID_HID;
239 hid_t ds_face_grad_hx_ = H5I_INVALID_HID;
240 hid_t ds_face_grad_hy_ = H5I_INVALID_HID;
241 hid_t ds_face_grad_hx_lim_ = H5I_INVALID_HID;
242 hid_t ds_face_grad_hy_lim_ = H5I_INVALID_HID;
243 hid_t ds_face_rainfall_ = H5I_INVALID_HID;
244 hid_t ds_face_coupling_flux_ = H5I_INVALID_HID;
245 hid_t ds_face_net_source_ = H5I_INVALID_HID;
246 hid_t ds_face_infil_rate_ = H5I_INVALID_HID;
247 hid_t ds_face_infil_cum_ = H5I_INVALID_HID;
248 hid_t ds_face_rain_cum_ = H5I_INVALID_HID;
252 hid_t ds_face_species_conc_ = H5I_INVALID_HID;
253 hsize_t n_species_ = 0;
254 hid_t ds_face_vx_ = H5I_INVALID_HID;
255 hid_t ds_face_vy_ = H5I_INVALID_HID;
256 hid_t ds_face_continuity_err_ = H5I_INVALID_HID;
257 hid_t ds_edge_flux_ = H5I_INVALID_HID;
258 hid_t ds_node_head_ = H5I_INVALID_HID;
259 hid_t ds_node_depth_ = H5I_INVALID_HID;
260
261 // Cumulative envelopes — fixed [nFace], overwritten in place each update()
262 hid_t ds_face_max_depth_ = H5I_INVALID_HID;
263 hid_t ds_face_max_velocity_ = H5I_INVALID_HID;
264 hid_t ds_face_max_continuity_err_ = H5I_INVALID_HID;
265
266 hsize_t n_faces_ = 0;
267 hsize_t n_nodes_ = 0;
268 hsize_t edge_stride_ = 3;
269 hsize_t n_steps_ = 0;
270
271 // Helpers
272 void writeMeshTopology(const SimulationContext& ctx);
274 void writeCrsVariable();
279 hid_t createUnlimitedDataset(const char* name, int rank,
280 const hsize_t* dims,
281 const hsize_t* chunk_dims,
282 hid_t type = -1);
283 void writeStringAttr(hid_t loc, const char* name, const char* value);
284 void writeDoubleAttr(hid_t loc, const char* name, double value);
285 void extendAndWrite2D(hid_t ds, const double* data, hsize_t n_cols);
286 void extendAndWrite3D(hid_t ds, const double* data, hsize_t dim1, hsize_t dim2);
288 hid_t createFaceEnvelopeDataset(const char* name, const char* long_name,
289 const char* units);
291 void writeFaceEnvelope(hid_t ds, const double* data);
292};
293
294} // namespace openswmm::twoD
295
296#endif // OPENSWMM_HAS_2D
Interface for output-writing plugins.
PluginState
Plugin lifecycle states.
Definition PluginState.hpp:83
StmtPtr prepare(sqlite3 *db, const std::string &sql)
Definition GpkgUtils.hpp:105
int validate(const std::string &expr_str, std::string &msg, int &col)
Validate a treatment expression without touching engine state.
Definition Treatment.cpp:346
Definition NodeCoupling.cpp:16
SoA storage for 2D mixed triangle/quad mesh geometry and topology.
Definition MeshData.hpp:67
Configuration for the 2D surface routing solver.
Definition SolverOptions2D.hpp:208