OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
DefaultOutputPlugin.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
38
39#ifndef OPENSWMM_ENGINE_DEFAULT_OUTPUT_PLUGIN_HPP
40#define OPENSWMM_ENGINE_DEFAULT_OUTPUT_PLUGIN_HPP
41
43
46#include <string>
47#include <atomic>
48#include <cstdio>
49#include <vector>
50#include <memory>
51
52namespace openswmm {
53
62class DefaultOutputPlugin final : public IOutputPlugin {
63public:
66 explicit DefaultOutputPlugin(std::string out_path);
70 ~DefaultOutputPlugin() override = default;
71
72 // -----------------------------------------------------------------------
73 // IOutputPlugin interface
74 // -----------------------------------------------------------------------
75
79 PluginState state() const noexcept override { return state_; }
80
85 int initialize(const std::vector<std::string>& init_args,
86 const IPluginComponentInfo* info) override;
87
91 int validate(const SimulationContext& ctx) override;
92
101 int prepare(const SimulationContext& ctx) override;
102
112 int update(const SimulationSnapshot& snapshot) override;
113
119 int finalize(const SimulationContext& ctx) override;
120
124 const char* last_error_message() const noexcept override { return last_error_.c_str(); }
125
126private:
127 friend class SWMMEngine;
128 struct PreparedCheckpoint;
129 bool checkpoint_fresh_ = false;
130 std::string checkpoint_header_;
137 std::string checkpointBytes(const SimulationContext& ctx, double next_report_ms) const;
147 std::shared_ptr<PreparedCheckpoint> prepareCheckpoint(const SimulationContext& ctx,
148 double next_report_ms, std::string bytes);
154 void installCheckpoint(PreparedCheckpoint& prepared);
161 checkpointPublication(const PreparedCheckpoint& prepared) const;
167 void adoptCheckpoint(PreparedCheckpoint& prepared) noexcept;
176 void validateCheckpoint(const SimulationContext& ctx, double next_report_ms, int periods,
177 const std::string& bytes) const;
183 std::string readPrefix() const;
194 std::string captureFileCheckpoint(const SimulationContext& ctx, double next,
195 const std::filesystem::path& base,
196 std::shared_ptr<io::OwnedCheckpointFile>& artifact) const;
206 std::shared_ptr<PreparedCheckpoint> prepareFileCheckpoint(const SimulationContext& ctx,
207 double next, std::string bytes);
218 void validateFileCheckpoint(FILE* file, uint64_t bytes, const SimulationContext& ctx,
219 double next, int periods) const;
220
221 std::string out_path_;
223 std::string last_error_;
224 int step_count_ = 0;
225
226 FILE* out_file_ = nullptr;
227 int n_subcatch_ = 0;
228 int n_nodes_ = 0;
229 int n_links_ = 0;
230 int n_polluts_ = 0;
231 int n_subcatch_vars_ = 0;
232 bool snow_process_ = false;
233 int n_node_vars_ = 0;
234 int n_link_vars_ = 0;
235 int flow_units_code_ = 0;
236 long id_start_pos_ = 0;
237 long input_start_pos_ = 0;
238 long output_start_pos_ = 0;
239 int n_periods_ = 0;
240
241 // Static-object header is written from the SimulationContext (internal
242 // units), so it still needs this length factor. Subcatchment area is
243 // already held in display units and needs none. Per-timestep results
244 // arrive pre-converted by the engine boundary and need no plugin-side
245 // conversion.
246 double ucf_length_ = 1.0;
247
248 // Per-object report flags, captured during prepare() from SimulationContext
249 std::vector<char> subcatch_rpt_flag_;
250 std::vector<char> node_rpt_flag_;
251 std::vector<char> link_rpt_flag_;
252
253 static constexpr int MAGIC_NUMBER = 516114522;
254 static constexpr int VERSION = 60000; // SWMM 6.0.000
255 static constexpr int MAX_SYS_RESULTS = 15;
256
262 void writeHeader(const SimulationContext& ctx);
266 void writeID(const char* id);
270 void writeInt4(int value);
274 void writeReal4(float value);
278 void writeReal8(double value);
279};
280
281} /* namespace openswmm */
282
283#endif /* OPENSWMM_ENGINE_DEFAULT_OUTPUT_PLUGIN_HPP */
Versioned checkpoint file serialization and integrity checks.
Publication of outputs associated with restored checkpoint sessions.
Interface for output-writing plugins.
friend class SWMMEngine
Definition DefaultOutputPlugin.hpp:127
DefaultOutputPlugin(std::string out_path)
Store a binary output destination and start in LOADED state.
Definition DefaultOutputPlugin.cpp:56
int update(const SimulationSnapshot &snapshot) override
Append and flush one complete report-period record, then increment counters.
Definition DefaultOutputPlugin.cpp:158
const char * last_error_message() const noexcept override
Borrow the stored diagnostic text without clearing it.
Definition DefaultOutputPlugin.hpp:124
~DefaultOutputPlugin() override=default
Destroy member buffers without implicitly closing out_file_. Caller must finalize() before deletion o...
int initialize(const std::vector< std::string > &init_args, const IPluginComponentInfo *info) override
Reset write counters and mark INITIALIZED.
Definition DefaultOutputPlugin.cpp:59
PluginState state() const noexcept override
Read the last assigned lifecycle marker without synchronization.
Definition DefaultOutputPlugin.hpp:79
int prepare(const SimulationContext &ctx) override
Open/truncate the binary output and write its reporting layout/header.
Definition DefaultOutputPlugin.cpp:114
int finalize(const SimulationContext &ctx) override
Write a footer with current counters, close the output and mark FINALIZED.
Definition DefaultOutputPlugin.cpp:312
int validate(const SimulationContext &ctx) override
Mark VALIDATED without checking path or model arrays.
Definition DefaultOutputPlugin.cpp:67
Interface for output-writing plugins.
Definition IOutputPlugin.hpp:77
Describes a plugin component: metadata, capabilities, and factory methods.
Definition IPluginComponentInfo.hpp:201
PluginState
Plugin lifecycle states.
Definition PluginState.hpp:86
@ UNLOADED
Library not yet loaded (or was closed).
Definition PluginState.hpp:87
Definition ExchangeLaw.hpp:60
Definition DefaultOutputCheckpoint.cpp:53
Central, reentrant simulation context.
Definition SimulationContext.hpp:390
Complete simulation state snapshot at one output time step.
Definition SimulationSnapshot.hpp:123
Definition CheckpointOutputPublication.hpp:64