Skip to content

Part 2: Operator Snapshot Layer – per-substep DW state exposure#64

Open
wiesnerfriedman wants to merge 1 commit into
HydroCouple:swmm6_relfrom
wiesnerfriedman:pr/operator-snapshot
Open

Part 2: Operator Snapshot Layer – per-substep DW state exposure#64
wiesnerfriedman wants to merge 1 commit into
HydroCouple:swmm6_relfrom
wiesnerfriedman:pr/operator-snapshot

Conversation

@wiesnerfriedman
Copy link
Copy Markdown
Collaborator

@wiesnerfriedman wiesnerfriedman commented Apr 21, 2026

  • 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

I have read the CLA Document and I hereby sign the CLA.

- 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
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants