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
44#include <string>
45#include <atomic>
46#include <cstdio>
47#include <vector>
48
49namespace openswmm {
50
59class DefaultOutputPlugin final : public IOutputPlugin {
60public:
65 explicit DefaultOutputPlugin(std::string out_path);
66 ~DefaultOutputPlugin() override = default;
67
68 // -----------------------------------------------------------------------
69 // IOutputPlugin interface
70 // -----------------------------------------------------------------------
71
72 PluginState state() const noexcept override { return state_; }
73
74 int initialize(const std::vector<std::string>& init_args,
75 const IPluginComponentInfo* info) override;
76
77 int validate(const SimulationContext& ctx) override;
78
79 int prepare(const SimulationContext& ctx) override;
80
81 int update(const SimulationSnapshot& snapshot) override;
82
83 int finalize(const SimulationContext& ctx) override;
84
85 const char* last_error_message() const noexcept override {
86 return last_error_.c_str();
87 }
88
89private:
90 std::string out_path_;
92 std::string last_error_;
93 int step_count_ = 0;
94
95 FILE* out_file_ = nullptr;
96 int n_subcatch_ = 0;
97 int n_nodes_ = 0;
98 int n_links_ = 0;
99 int n_polluts_ = 0;
100 int n_subcatch_vars_ = 0;
101 int n_node_vars_ = 0;
102 int n_link_vars_ = 0;
103 int flow_units_code_ = 0;
104 long id_start_pos_ = 0;
105 long input_start_pos_ = 0;
106 long output_start_pos_ = 0;
107 int n_periods_ = 0;
108
109 // Static-object header is written from the SimulationContext (internal
110 // units), so it still needs these two factors. Per-timestep results arrive
111 // pre-converted by the engine boundary and need no plugin-side conversion.
112 double ucf_length_ = 1.0;
113 double ucf_landarea_ = 1.0;
114
115 // Per-object report flags, captured during prepare() from SimulationContext
116 std::vector<char> subcatch_rpt_flag_;
117 std::vector<char> node_rpt_flag_;
118 std::vector<char> link_rpt_flag_;
119
120 static constexpr int MAGIC_NUMBER = 516114522;
121 static constexpr int VERSION = 60000; // SWMM 6.0.000
122 static constexpr int MAX_SYS_RESULTS = 15;
123
124 void writeHeader(const SimulationContext& ctx);
125 void writeID(const char* id);
126 void writeInt4(int value);
127 void writeReal4(float value);
128 void writeReal8(double value);
129};
130
131} /* namespace openswmm */
132
133#endif /* OPENSWMM_ENGINE_DEFAULT_OUTPUT_PLUGIN_HPP */
Interface for output-writing plugins.
DefaultOutputPlugin(std::string out_path)
Construct with the output file path.
Definition DefaultOutputPlugin.cpp:50
int update(const SimulationSnapshot &snapshot) override
Write one output snapshot. Called from the IO thread.
Definition DefaultOutputPlugin.cpp:111
const char * last_error_message() const noexcept override
Get the last error message from this plugin instance.
Definition DefaultOutputPlugin.hpp:85
~DefaultOutputPlugin() override=default
int initialize(const std::vector< std::string > &init_args, const IPluginComponentInfo *info) override
Definition DefaultOutputPlugin.cpp:55
PluginState state() const noexcept override
Query the current plugin state.
Definition DefaultOutputPlugin.hpp:72
int prepare(const SimulationContext &ctx) override
Open output file(s) and write headers.
Definition DefaultOutputPlugin.cpp:75
int finalize(const SimulationContext &ctx) override
Flush and close output file(s). Called from main thread.
Definition DefaultOutputPlugin.cpp:223
int validate(const SimulationContext &ctx) override
Validate plugin configuration against the simulation model.
Definition DefaultOutputPlugin.cpp:63
Interface for output-writing plugins.
Definition IOutputPlugin.hpp:73
Describes a plugin component: metadata, capabilities, and factory methods.
Definition IPluginComponentInfo.hpp:195
#define MAX_SYS_RESULTS
Maximum number of system results.
Definition enums.h:412
#define VERSION
SWMM version number.
Definition consts.h:39
PluginState
Plugin lifecycle states.
Definition PluginState.hpp:83
@ UNLOADED
Library not yet loaded (or was closed).
Definition PluginState.hpp:84
Definition NodeCoupling.cpp:16
Central, reentrant simulation context.
Definition SimulationContext.hpp:353
Complete simulation state snapshot at one output time step.
Definition SimulationSnapshot.hpp:117