OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
HotStartManager.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
78
79#ifndef OPENSWMM_ENGINE_HOT_START_MANAGER_HPP
80#define OPENSWMM_ENGINE_HOT_START_MANAGER_HPP
81
82#include <cstdint>
83#include <functional>
84#include <string>
85#include <vector>
86
87namespace openswmm {
88
90namespace runoff { class RunoffSolver; }
91namespace groundwater { class GWSolver; }
92
93// ============================================================================
94// Per-object state records
95// ============================================================================
96
99 std::string id;
100 double depth = 0.0;
101 double head = 0.0;
102 double volume = 0.0;
104 double age = -1.0;
105};
106
109 std::string id;
110 double flow = 0.0;
111 double depth = 0.0;
112 double volume = 0.0;
114 double age = -1.0;
115};
116
119 std::string id;
120 double runoff = 0.0;
121 double gwater = 0.0;
122
123 // Gap #54: infiltration model state (V2 only; -1 = not present / V1 file)
124 int infil_model = -1;
125 double infil[6] = {0, 0, 0, 0, 0, 0};
126
127 // Gap #54: groundwater zone state (V2 only; gw_theta < 0 = not present)
128 double gw_theta = -1.0;
129 double gw_lower_depth = 0.0;
130};
131
132// ============================================================================
133// File header (in-memory mirror of the on-disk header)
134// ============================================================================
135
138 uint32_t version = 1;
139 int64_t timestamp = 0;
140 double sim_time = 0.0;
141 double start_date = 0.0;
142 double end_date = 0.0;
143 std::string crs;
144};
145
146// ============================================================================
147// HotStartFile — opaque handle (cast to void* in C API)
148// ============================================================================
149
160 std::vector<HotStartNodeRecord> nodes;
161 std::vector<HotStartLinkRecord> links;
162 std::vector<HotStartSubcatchRecord> subcatches;
163
171 std::vector<std::string> species;
172 std::vector<double> node_species;
173 std::vector<double> link_species;
174
188 uint32_t gw_n_cells = 0;
189 uint32_t gw_m_layers = 0;
190 std::vector<double> gw_hg;
191 std::vector<double> gw_hu;
192 std::vector<double> gw_theta_sigma;
193 std::vector<double> gw_ledger;
194
195 std::string path;
196 bool dirty = false;
197
198 std::vector<std::string> warnings;
199
200 // -----------------------------------------------------------------------
201 // Modification helpers
202 // -----------------------------------------------------------------------
203
205 bool set_node_depth (const std::string& id, double v);
207 bool set_node_head (const std::string& id, double v);
209 bool set_link_flow (const std::string& id, double v);
211 bool set_link_depth (const std::string& id, double v);
213 bool set_subcatch_runoff(const std::string& id, double v);
214};
215
216// ============================================================================
217// HotStartManager — static methods (no state; acts as a utility namespace)
218// ============================================================================
219
230public:
231 HotStartManager() = delete;
232
233 // -----------------------------------------------------------------------
234 // Save
235 // -----------------------------------------------------------------------
236
245 static HotStartFile* save(const SimulationContext& ctx,
246 const std::string& path);
247
260 static HotStartFile* save(const SimulationContext& ctx,
262 const groundwater::GWSolver* gw,
263 const std::string& path);
264
265 // -----------------------------------------------------------------------
266 // Open
267 // -----------------------------------------------------------------------
268
278 static HotStartFile* open(const std::string& path);
279
280 // -----------------------------------------------------------------------
281 // Apply
282 // -----------------------------------------------------------------------
283
296 static int apply(HotStartFile& hs,
298 std::function<void(const std::string&)> warn_cb = {});
299
313 static int apply(HotStartFile& hs,
317 std::function<void(const std::string&)> warn_cb = {});
318
319 // -----------------------------------------------------------------------
320 // Legacy EPA SWMM5 hotstart (.hsf) — read + apply routing state
321 // -----------------------------------------------------------------------
322
342 static int apply_legacy_routing(const std::string& path,
343 SimulationContext& ctx,
344 std::function<void(const std::string&)> warn_cb = {});
345
364 static int save_legacy_routing(const std::string& path,
365 const SimulationContext& ctx);
366
367 // -----------------------------------------------------------------------
368 // Flush (write-back modifications)
369 // -----------------------------------------------------------------------
370
376 static bool flush(HotStartFile& hs);
377
378 // -----------------------------------------------------------------------
379 // Last I/O error (thread-local; for error reporting to C API layer)
380 // -----------------------------------------------------------------------
381
383 static const std::string& last_io_error() noexcept;
384
385private:
386 // -----------------------------------------------------------------------
387 // Binary I/O helpers
388 // -----------------------------------------------------------------------
389
390 static bool write_file(const HotStartFile& hs, const std::string& path);
391 static bool read_file (HotStartFile& hs, const std::string& path);
392
393 // -----------------------------------------------------------------------
394 // CRC32 (IEEE 802.3 polynomial, no external dependency)
395 // -----------------------------------------------------------------------
396
397 static uint32_t crc32(const uint8_t* data, std::size_t len) noexcept;
398};
399
400} /* namespace openswmm */
401
402#endif /* OPENSWMM_ENGINE_HOT_START_MANAGER_HPP */
static int save_legacy_routing(const std::string &path, const SimulationContext &ctx)
Write the current routing state as a legacy EPA SWMM5 .hsf (SAVE HOTSTART), byte-format-compatible wi...
Definition HotStartManager.cpp:1209
static int apply_legacy_routing(const std::string &path, SimulationContext &ctx, std::function< void(const std::string &)> warn_cb={})
Read a legacy EPA SWMM5 .hsf (USE HOTSTART) file and apply its routing state to the context,...
Definition HotStartManager.cpp:1029
static const std::string & last_io_error() noexcept
Description of the most recent I/O error (empty = none).
Definition HotStartManager.cpp:67
static int apply(HotStartFile &hs, SimulationContext &ctx, std::function< void(const std::string &)> warn_cb={})
Apply hot start records to a simulation context.
Definition HotStartManager.cpp:882
static HotStartFile * save(const SimulationContext &ctx, const std::string &path)
Capture current engine state and write to a hot start file (V1 format).
Definition HotStartManager.cpp:690
static HotStartFile * open(const std::string &path)
Read and validate a hot start file.
Definition HotStartManager.cpp:867
static bool flush(HotStartFile &hs)
If the file is dirty, rewrite it to disk.
Definition HotStartManager.cpp:1285
Definition Groundwater.hpp:173
Definition Runoff.hpp:114
Definition HotStartManager.hpp:91
Definition HotStartManager.hpp:90
Definition NodeCoupling.cpp:16
In-memory hot start file data.
Definition HotStartManager.hpp:158
bool set_link_depth(const std::string &id, double v)
Set depth for a stored link. Returns false if id not found.
Definition HotStartManager.cpp:447
std::vector< double > gw_ledger
the 9 cumulative terms (m3)
Definition HotStartManager.hpp:193
bool set_node_depth(const std::string &id, double v)
Set depth for a stored node. Returns false if id not found.
Definition HotStartManager.cpp:426
HotStartHeader header
Definition HotStartManager.hpp:159
std::vector< double > link_species
Definition HotStartManager.hpp:173
bool set_subcatch_runoff(const std::string &id, double v)
Set runoff for a stored subcatchment. Returns false if id not found.
Definition HotStartManager.cpp:454
bool dirty
True if set_*() was called.
Definition HotStartManager.hpp:196
std::vector< std::string > warnings
Populated by HotStartManager::apply()
Definition HotStartManager.hpp:198
std::vector< double > node_species
Definition HotStartManager.hpp:172
bool set_node_head(const std::string &id, double v)
Set head for a stored node. Returns false if id not found.
Definition HotStartManager.cpp:433
std::vector< HotStartNodeRecord > nodes
Definition HotStartManager.hpp:160
std::vector< double > gw_theta_sigma
closure-B layers, layer-major
Definition HotStartManager.hpp:192
std::vector< std::string > species
Definition HotStartManager.hpp:171
std::string path
File path (for flush-on-close)
Definition HotStartManager.hpp:195
uint32_t gw_n_cells
Definition HotStartManager.hpp:188
std::vector< HotStartLinkRecord > links
Definition HotStartManager.hpp:161
std::vector< double > gw_hu
unsaturated storage (m of water)
Definition HotStartManager.hpp:191
std::vector< HotStartSubcatchRecord > subcatches
Definition HotStartManager.hpp:162
std::vector< double > gw_hg
saturated thickness (m)
Definition HotStartManager.hpp:190
uint32_t gw_m_layers
Definition HotStartManager.hpp:189
bool set_link_flow(const std::string &id, double v)
Set flow for a stored link. Returns false if id not found.
Definition HotStartManager.cpp:440
In-memory representation of the OPENSWMM_HS_V1 header.
Definition HotStartManager.hpp:137
double start_date
options.start_date (OADate (days since 12/30/1899))
Definition HotStartManager.hpp:141
double end_date
options.end_date (OADate (days since 12/30/1899))
Definition HotStartManager.hpp:142
int64_t timestamp
Unix epoch seconds at save time.
Definition HotStartManager.hpp:139
double sim_time
Simulation elapsed time (decimal days)
Definition HotStartManager.hpp:140
uint32_t version
Definition HotStartManager.hpp:138
std::string crs
CRS string (may be empty)
Definition HotStartManager.hpp:143
Node hydraulic state at hot-start save time.
Definition HotStartManager.hpp:98
double age
A2a (V3): water age (seconds); -1 = not tracked / pre-V3 file.
Definition HotStartManager.hpp:104
double volume
Definition HotStartManager.hpp:102
std::string id
Definition HotStartManager.hpp:99
double depth
Definition HotStartManager.hpp:100
double head
Definition HotStartManager.hpp:101
Subcatchment state at hot-start save time.
Definition HotStartManager.hpp:118
double gw_lower_depth
lower zone depth (ft)
Definition HotStartManager.hpp:129
double infil[6]
flat 6-elem state (model-dependent)
Definition HotStartManager.hpp:125
double runoff
Definition HotStartManager.hpp:120
int infil_model
Definition HotStartManager.hpp:124
double gw_theta
upper zone moisture content
Definition HotStartManager.hpp:128
double gwater
Definition HotStartManager.hpp:121
std::string id
Definition HotStartManager.hpp:119
Central, reentrant simulation context.
Definition SimulationContext.hpp:353