Part 2: Operator Snapshot Layer – per-substep DW state exposure#64
Open
wiesnerfriedman wants to merge 1 commit into
Open
Part 2: Operator Snapshot Layer – per-substep DW state exposure#64wiesnerfriedman wants to merge 1 commit into
wiesnerfriedman wants to merge 1 commit into
Conversation
- Add openswmm_operator_snapshot.h: C API for snapshot callback and poll-mode - Add OperatorSnapshotState.hpp: per-instance snapshot storage and staging buffers - Wire snapshot populate + callback into SWMMEngine::stepRouting() - Add DWSolver::populateSnapshot() with zero-copy pointers into solver buffers - Wire iteration history (resetIterCount/recordResidual) into Picard loop - Add C API: swmm_set_operator_snapshot_callback, swmm_get_operator_snapshot, swmm_enable_iteration_history, swmm_get_iteration_residual - Fix error code redefinition conflict (SWMM_ERR_WRONG_STATE alias) - Add IntelliSense workaround for dllimport false positives - Add test_operator_snapshot.cpp (4 tests) and bench_operator_snapshot.cpp - Register test and benchmark in CMakeLists.txt
There was a problem hiding this comment.
Pull request overview
Adds an “operator snapshot” facility to expose per-routing-substep Dynamic Wave (DW) internal state (topology, linearization terms, flags, and Picard telemetry) via a zero-copy C snapshot struct, with both callback- and poll-based access. This integrates snapshot population into SWMMEngine::stepRouting() and adds optional Picard iteration residual history recording, plus unit tests and a benchmark to validate/measure the feature.
Changes:
- Introduces
SWMM_OperatorSnapshot+ C APIs for callback registration, poll-mode snapshot retrieval, and iteration residual history access. - Wires DW solver snapshot population and iteration residual capture into the routing step / Picard loop.
- Adds new unit tests and a benchmark, and registers them in CMake.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/unit/engine/test_operator_snapshot.cpp | Adds unit tests for snapshot callback/poll behavior and basic field sanity checks. |
| tests/unit/engine/CMakeLists.txt | Registers the new unit test target. |
| tests/benchmarks/bench_operator_snapshot.cpp | Adds benchmark cases for snapshot overhead (baseline/no-op/copy). |
| tests/benchmarks/CMakeLists.txt | Registers the new benchmark target. |
| src/engine/hydraulics/DynamicWave.hpp | Declares snapshot population API and iteration-history hooks on DWSolver. |
| src/engine/hydraulics/DynamicWave.cpp | Implements snapshot population and Picard residual recording. |
| src/engine/core/OperatorSnapshotState.hpp | Adds per-engine snapshot storage, staging buffers, and iteration-history buffer. |
| src/engine/core/SWMMEngine.hpp | Stores OperatorSnapshotState per engine instance. |
| src/engine/core/SWMMEngine.cpp | Populates/fires snapshot in routing; allocates staging; resets snapshot state on close. |
| src/engine/core/openswmm_engine_impl.cpp | Implements new C APIs + IntelliSense dllimport workaround. |
| include/openswmm/engine/openswmm_operator_snapshot.h | New public C header for snapshot struct and APIs. |
| include/openswmm/engine/openswmm_engine.h | Includes the new snapshot header from the master engine header. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+45
to
+52
| #ifndef OPENSWMM_OPERATOR_SNAPSHOT_H | ||
| #define OPENSWMM_OPERATOR_SNAPSHOT_H | ||
|
|
||
| #include "openswmm_engine.h" | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif |
Comment on lines
+59
to
+66
| * @brief Per-substep operator snapshot — read-only view of DW solver state. | ||
| * | ||
| * @details All pointer fields reference per-instance solver buffers. | ||
| * They are valid only inside the operator snapshot callback. | ||
| * Array sizes are given by `n_nodes` and `n_links`. | ||
| * | ||
| * Optional sections (Anderson acceleration, dynamic slot) have their | ||
| * pointer fields set to NULL when the corresponding feature is |
|
|
||
| // Reset transient operator snapshot state so reopen starts clean | ||
| op_snap_.resetTransientState(); | ||
|
|
Comment on lines
+120
to
+129
| * @param out Output buffer of size n_nodes_hist_. | ||
| * @param n Size of out buffer. | ||
| * @returns true on success, false if out-of-range. | ||
| */ | ||
| bool getResidual(int iter, double* out, int n) const { | ||
| if (iter < 0 || iter >= iter_count_ || !out) return false; | ||
| int count = (n < n_nodes_hist_) ? n : n_nodes_hist_; | ||
| auto offset = static_cast<size_t>(iter) * static_cast<size_t>(n_nodes_hist_); | ||
| std::memcpy(out, iter_history_.data() + offset, | ||
| static_cast<size_t>(count) * sizeof(double)); |
Comment on lines
+5
to
+15
| * @details Verifies that: | ||
| * 1. Callback fires each routing substep with valid snapshot data. | ||
| * 2. Poll mode (swmm_get_operator_snapshot) returns populated snapshot. | ||
| * 3. Snapshot dimensions match engine model counts. | ||
| * 4. Topology pointers (node1, node2, link_type) are non-null. | ||
| * 5. Picard telemetry (iterations, routing_dt) is reasonable. | ||
| * 6. Flow/depth/head arrays are non-null and plausible. | ||
| * 7. dqdh array is non-null. | ||
| * 8. Node converged/surcharged flags are 0 or 1. | ||
| * 9. Two independent engines receive independent snapshots. | ||
| * 10. No callback → zero overhead (snapshot not populated). |
Comment on lines
+259
to
+262
| // Poll should return null since no callback means snapshot is never populated | ||
| const SWMM_OperatorSnapshot* snap = nullptr; | ||
| ASSERT_EQ(SWMM_OK, swmm_get_operator_snapshot(engine, &snap)); | ||
| EXPECT_EQ(snap, nullptr) << "Snapshot should not be populated without a callback"; |
Comment on lines
+30
to
+35
| std::string benchModel() { | ||
| // Resolve relative to source tree — same model as unit tests | ||
| namespace fs = std::filesystem; | ||
| fs::path p = fs::path(__FILE__).parent_path().parent_path() | ||
| / "unit" / "engine" / "data" / "site_drainage_model.inp"; | ||
| return p.string(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I have read the CLA Document and I hereby sign the CLA.