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
84
85#ifndef OPENSWMM_ENGINE_HOT_START_MANAGER_HPP
86#define OPENSWMM_ENGINE_HOT_START_MANAGER_HPP
87
88#include <cstdint>
89#include <functional>
90#include <string>
91#include <vector>
92
93namespace openswmm {
94
96namespace runoff {
97class RunoffSolver;
98}
99namespace groundwater {
100class GWSolver;
101}
102
103// ============================================================================
104// Per-object state records
105// ============================================================================
106
109 int layer = 0;
110 double bottom = 0.0, top = 0.0, porosity = 0.0, volume = 0.0, theta = 0.0;
111};
113 std::string id;
114 std::vector<double> lid_richards;
115 std::vector<double> lid_infiltration;
116 std::vector<double> lid_quality_mass;
117 std::vector<HotStartLidCellRecord> lid_cells;
118 double lid_treated_volume = 0.0;
119 double depth = 0.0;
120 double head = 0.0;
121 double volume = 0.0;
123 double age = -1.0;
124};
125
128 std::string id;
129 double flow = 0.0;
130 double depth = 0.0;
131 double volume = 0.0;
133 double age = -1.0;
134};
135
138 std::string id;
139 double runoff = 0.0;
140 double gwater = 0.0;
141
142 // Gap #54: infiltration model state (V2 only; -1 = not present / V1 file)
143 int infil_model = -1;
144 double infil[6] = {0, 0, 0, 0, 0, 0};
145
146 // Gap #54: groundwater zone state (V2 only; gw_theta < 0 = not present)
147 double gw_theta = -1.0;
148 double gw_lower_depth = 0.0;
149};
150
151// ============================================================================
152// File header (in-memory mirror of the on-disk header)
153// ============================================================================
154
157 uint32_t version = 1;
158 int64_t timestamp = 0;
159 double sim_time = 0.0;
160 double start_date = 0.0;
161 double end_date = 0.0;
162 std::string crs;
163};
164
165// ============================================================================
166// HotStartFile — opaque handle (cast to void* in C API)
167// ============================================================================
168
178 std::string snow_state;
179 std::string
181 std::string fv_dry_state;
182 std::string gw_transport_state;
183 std::string
186 std::vector<HotStartNodeRecord> nodes;
187 std::vector<HotStartLinkRecord> links;
188 std::vector<HotStartSubcatchRecord> subcatches;
189
197 std::vector<std::string> species;
198 std::vector<double> node_species;
199 std::vector<double> link_species;
200
214 uint32_t gw_n_cells = 0;
215 uint32_t gw_m_layers = 0;
216 std::vector<double> gw_hg;
217 std::vector<double> gw_hu;
218 std::vector<double> gw_theta_sigma;
219 std::vector<std::vector<double>>
221 std::vector<double> gw_ledger;
222
236 std::vector<std::string> gw_species;
237 std::vector<double> gw_sat_mass;
238 std::vector<double> gw_unsat_mass;
239 std::vector<double> gw_species_ledger;
243 static constexpr int kGwSpeciesLedgerTerms = 13;
244
245 std::string path;
246 bool dirty = false;
247
248 std::vector<std::string> warnings;
249
250 // -----------------------------------------------------------------------
251 // Modification helpers
252 // -----------------------------------------------------------------------
253
264 bool set_node_depth(const std::string& id, double v);
275 bool set_node_head(const std::string& id, double v);
286 bool set_link_flow(const std::string& id, double v);
297 bool set_link_depth(const std::string& id, double v);
308 bool set_subcatch_runoff(const std::string& id, double v);
309};
310
311// ============================================================================
312// HotStartManager — static methods (no state; acts as a utility namespace)
313// ============================================================================
314
334 std::vector<std::string> species;
335 int n_subcatch = 0;
336 int n_landuses = -1;
337 std::vector<double> washoff;
338 std::vector<double> ponded;
339 std::vector<double> buildup;
340};
341
343public:
347 HotStartManager() = delete;
348
349 // -----------------------------------------------------------------------
350 // Save
351 // -----------------------------------------------------------------------
352
362 static HotStartFile* save(const SimulationContext& ctx, const std::string& path);
363
379 const groundwater::GWSolver* gw, const std::string& path);
380
402 const groundwater::GWSolver* gw,
403 const std::string& path,
404 const std::string& engine_checkpoint);
405
406 // -----------------------------------------------------------------------
407 // Open
408 // -----------------------------------------------------------------------
409
419 static HotStartFile* open(const std::string& path);
420
431 static bool is_native_file(const std::string& path) noexcept;
432
433 // -----------------------------------------------------------------------
434 // Apply
435 // -----------------------------------------------------------------------
436
451 static int apply(HotStartFile& hs, SimulationContext& ctx,
452 std::function<void(const std::string&)> warn_cb = {});
453
471 std::function<void(const std::string&)> warn_cb = {});
472
473 // -----------------------------------------------------------------------
474 // Legacy EPA SWMM5 hotstart (.hsf) — read + apply routing state
475 // -----------------------------------------------------------------------
476
504 static int apply_legacy_routing(const std::string& path, SimulationContext& ctx,
505 std::function<void(const std::string&)> warn_cb = {},
506 std::vector<double>* runoff = nullptr,
507 LegacySpeciesTrailer* species = nullptr);
508
526 static bool apply_species_surface(const LegacySpeciesTrailer& tr, SimulationContext& ctx,
527 const std::function<void(const std::string&)>& warn);
528
542 static std::size_t legacy_runoff_size(const SimulationContext& ctx, int n_land);
543
570 static int save_legacy_routing(const std::string& path, const SimulationContext& ctx,
571 const std::vector<double>* runoff = nullptr);
572
573 // -----------------------------------------------------------------------
574 // Flush (write-back modifications)
575 // -----------------------------------------------------------------------
576
587 static bool flush(HotStartFile& hs);
588
589 // -----------------------------------------------------------------------
590 // Last I/O error (thread-local; for error reporting to C API layer)
591 // -----------------------------------------------------------------------
592
594 static const std::string& last_io_error() noexcept;
595
596private:
597 // -----------------------------------------------------------------------
598 // Binary I/O helpers
599 // -----------------------------------------------------------------------
600
615 static bool write_file(const HotStartFile& hs, const std::string& path);
630 static bool read_file(HotStartFile& hs, const std::string& path);
631
632 // -----------------------------------------------------------------------
633 // CRC32 (IEEE 802.3 polynomial, no external dependency)
634 // -----------------------------------------------------------------------
635
643 static uint32_t crc32(const uint8_t* data, std::size_t len) noexcept;
644};
645
646} /* namespace openswmm */
647
648#endif /* OPENSWMM_ENGINE_HOT_START_MANAGER_HPP */
HotStartManager()=delete
Prevent instantiation of the static hot-start utility class.
static bool apply_species_surface(const LegacySpeciesTrailer &tr, SimulationContext &ctx, const std::function< void(const std::string &)> &warn)
Apply a legacy trailer surface block by reaction-species name.
Definition HotStartManager.cpp:2707
static std::size_t legacy_runoff_size(const SimulationContext &ctx, int n_land)
Number of doubles in legacy's runoff section (hotstart.c saveRunoff) for the current model with n_lan...
Definition HotStartManager.cpp:2362
static const std::string & last_io_error() noexcept
Description of the most recent I/O error (empty = none).
Definition HotStartManager.cpp:120
static int save_legacy_routing(const std::string &path, const SimulationContext &ctx, const std::vector< double > *runoff=nullptr)
Write the current routing state as a legacy EPA SWMM5 .hsf (SAVE HOTSTART), byte-format-compatible wi...
Definition HotStartManager.cpp:2755
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:1974
static HotStartFile * save(const SimulationContext &ctx, const std::string &path)
Capture current engine state and write a feature-dependent native hot start.
Definition HotStartManager.cpp:1577
static int apply_legacy_routing(const std::string &path, SimulationContext &ctx, std::function< void(const std::string &)> warn_cb={}, std::vector< double > *runoff=nullptr, LegacySpeciesTrailer *species=nullptr)
Read a legacy EPA SWMM5 .hsf (USE HOTSTART) file and apply its routing state to the context,...
Definition HotStartManager.cpp:2379
static HotStartFile * open(const std::string &path)
Read and validate a hot start file.
Definition HotStartManager.cpp:1951
static bool is_native_file(const std::string &path) noexcept
Probe the native hot-start magic without parsing records.
Definition HotStartManager.cpp:1962
static HotStartFile * save_with_engine_checkpoint(const SimulationContext &ctx, const runoff::RunoffSolver *runoff, const groundwater::GWSolver *gw, const std::string &path, const std::string &engine_checkpoint)
Capture a native hot start with an optional complete engine-session checkpoint.
Definition HotStartManager.cpp:1763
static bool flush(HotStartFile &hs)
Rewrite a dirty native hot-start object to its stored path.
Definition HotStartManager.cpp:2881
Mutable two-zone groundwater state with independent scalar per-subcatchment RKF45....
Definition Groundwater.hpp:181
Owned three-pool non-LID reservoir, native soil state and completed routing history....
Definition Runoff.hpp:195
Definition HotStartManager.hpp:99
@ runoff
Definition ProcessPerf.hpp:24
Definition ExplicitInertialSolver.hpp:68
Definition ExchangeLaw.hpp:60
In-memory hot start file data.
Definition HotStartManager.hpp:177
bool set_link_depth(const std::string &id, double v)
Edit the first exact-ID stored link depth and mark the file dirty.
Definition HotStartManager.cpp:898
std::vector< double > gw_ledger
the cumulative terms (m3): 9, +link since G-X3
Definition HotStartManager.hpp:221
std::vector< double > gw_unsat_mass
column-store mass
Definition HotStartManager.hpp:238
bool set_node_depth(const std::string &id, double v)
Edit the first exact-ID stored node depth and mark the file dirty.
Definition HotStartManager.cpp:865
std::string fv_dry_state
V18: named disconnected FV scalar inventories.
Definition HotStartManager.hpp:181
std::string snow_state
V14: native degree-day and UEB accepted/held states.
Definition HotStartManager.hpp:178
HotStartHeader header
Definition HotStartManager.hpp:185
std::vector< double > link_species
Definition HotStartManager.hpp:199
bool set_subcatch_runoff(const std::string &id, double v)
Edit the first exact-ID stored subcatchment runoff and mark the file dirty.
Definition HotStartManager.cpp:909
std::vector< double > gw_species_ledger
Definition HotStartManager.hpp:239
bool dirty
True if set_*() was called.
Definition HotStartManager.hpp:246
std::vector< std::string > warnings
Populated by HotStartManager::apply()
Definition HotStartManager.hpp:248
std::vector< double > node_species
Definition HotStartManager.hpp:198
bool set_node_head(const std::string &id, double v)
Edit the first exact-ID stored node hydraulic head and mark the file dirty.
Definition HotStartManager.cpp:876
std::string surface_state
V16: subcatchment ponded water, groundwater flow and surface quality (StateAccessors::get_surface_sta...
Definition HotStartManager.hpp:184
std::vector< HotStartNodeRecord > nodes
Definition HotStartManager.hpp:186
std::string gw_transport_state
V17: canonical SIGMA chemistry, edge receipts and clocks.
Definition HotStartManager.hpp:182
std::vector< double > gw_theta_sigma
closure-B layers, layer-major
Definition HotStartManager.hpp:218
std::vector< std::string > species
Definition HotStartManager.hpp:197
std::string path
File path (for flush-on-close)
Definition HotStartManager.hpp:245
uint32_t gw_n_cells
Definition HotStartManager.hpp:214
static constexpr int kGwSpeciesLedgerTerms
Definition HotStartManager.hpp:243
std::vector< HotStartLinkRecord > links
Definition HotStartManager.hpp:187
std::vector< double > gw_hu
unsaturated storage (m of water)
Definition HotStartManager.hpp:217
std::vector< HotStartSubcatchRecord > subcatches
Definition HotStartManager.hpp:188
std::string engine_checkpoint
V15: complete accepted engine/2D state for qualified native restarts (may be empty from V16)
Definition HotStartManager.hpp:180
std::vector< std::string > gw_species
row names, in file order
Definition HotStartManager.hpp:236
std::vector< std::vector< double > > gw_interface
V12: receiving interface; V13 appends pending/cumulative ET and held surface evaporation.
Definition HotStartManager.hpp:220
std::vector< double > gw_hg
saturated thickness (m)
Definition HotStartManager.hpp:216
uint32_t gw_m_layers
Definition HotStartManager.hpp:215
bool set_link_flow(const std::string &id, double v)
Edit the first exact-ID stored link flow and mark the file dirty.
Definition HotStartManager.cpp:887
std::vector< double > gw_sat_mass
saturated-zone mass
Definition HotStartManager.hpp:237
In-memory representation of the OPENSWMM_HS_V1 header.
Definition HotStartManager.hpp:156
double start_date
options.start_date (OADate (days since 12/30/1899))
Definition HotStartManager.hpp:160
double end_date
options.end_date (OADate (days since 12/30/1899))
Definition HotStartManager.hpp:161
int64_t timestamp
Unix epoch seconds at save time.
Definition HotStartManager.hpp:158
double sim_time
Simulation elapsed time (decimal days)
Definition HotStartManager.hpp:159
uint32_t version
Definition HotStartManager.hpp:157
std::string crs
CRS string (may be empty)
Definition HotStartManager.hpp:162
Node hydraulic state at hot-start save time.
Definition HotStartManager.hpp:108
double bottom
Definition HotStartManager.hpp:110
int layer
Definition HotStartManager.hpp:109
double top
Definition HotStartManager.hpp:110
double volume
Definition HotStartManager.hpp:110
double porosity
Definition HotStartManager.hpp:110
double theta
Definition HotStartManager.hpp:110
Definition HotStartManager.hpp:112
std::vector< HotStartLidCellRecord > lid_cells
V8 retained moisture and geometry identity.
Definition HotStartManager.hpp:117
std::vector< double > lid_quality_mass
V9 retained cell pollutant mass.
Definition HotStartManager.hpp:116
std::vector< double > lid_richards
V11: per-row storage and material identity.
Definition HotStartManager.hpp:114
double age
A2a (V3): water age (seconds); -1 = not tracked / pre-V3 file.
Definition HotStartManager.hpp:123
double volume
Definition HotStartManager.hpp:121
std::string id
Definition HotStartManager.hpp:113
double lid_treated_volume
V8 clogging history (ft3)
Definition HotStartManager.hpp:118
std::vector< double > lid_infiltration
V10 S,Ks,IMDmax,IMD,F,Fu,Fumax,Lu,T,head,saturated.
Definition HotStartManager.hpp:115
double depth
Definition HotStartManager.hpp:119
double head
Definition HotStartManager.hpp:120
Subcatchment state at hot-start save time.
Definition HotStartManager.hpp:137
double gw_lower_depth
lower zone depth (ft)
Definition HotStartManager.hpp:148
double infil[6]
flat 6-elem state (model-dependent)
Definition HotStartManager.hpp:144
double runoff
Definition HotStartManager.hpp:139
int infil_model
Definition HotStartManager.hpp:143
double gw_theta
upper zone moisture content
Definition HotStartManager.hpp:147
double gwater
Definition HotStartManager.hpp:140
std::string id
Definition HotStartManager.hpp:138
Static utility class for hot start file operations.
Definition HotStartManager.hpp:333
std::vector< double > washoff
[sc * ns + s] washoff concentration
Definition HotStartManager.hpp:337
int n_landuses
-1: the file has no surface block
Definition HotStartManager.hpp:336
int n_subcatch
Definition HotStartManager.hpp:335
std::vector< std::string > species
Definition HotStartManager.hpp:334
std::vector< double > ponded
[sc * ns + s] ponded mass
Definition HotStartManager.hpp:338
std::vector< double > buildup
[(sc * nlu + lu) * ns + s], per normalizer unit
Definition HotStartManager.hpp:339
Central, reentrant simulation context.
Definition SimulationContext.hpp:390