Skip to content

Fix cpp source flags in new API#698

Merged
waltsims merged 5 commits intomasterfrom
fix-cpp-source-flags
Mar 30, 2026
Merged

Fix cpp source flags in new API#698
waltsims merged 5 commits intomasterfrom
fix-cpp-source-flags

Conversation

@waltsims
Copy link
Copy Markdown
Owner

@waltsims waltsims commented Mar 30, 2026

Fixes #697.

Greptile Summary

This PR fixes a bug in CppSimulation._write_hdf5 where p_source_flag, ux_source_flag, uy_source_flag, and uz_source_flag were written as plain booleans (0 or 1) instead of the number of time steps the C++ binary actually expects. Without the fix, the C++ solver would always see a count of 1 regardless of the source signal length, causing incorrect simulations for any source with more than one time step.

  • Root cause fixed: Replaces int(has_p) / int(has_ux) etc. with np.asarray(source.*).shape[-1], correctly encoding the time-step count.
  • Safer indexing: The new shape[-1] idiom works for both 1-D (Nt,) and 2-D (n_elements, Nt) source arrays, avoiding the TypeError that len(source.p[0]) would raise on a 1-D input.
  • Clarity: An inline comment is added explaining the non-obvious semantics of the *_source_flag fields, which will help future contributors.
  • No new tests are introduced, but the change is straightforward and the fix is clearly correct given the C++ binary's documented contract.

Confidence Score: 5/5

Safe to merge — the fix is a targeted correctness patch with no regressions introduced.

The single changed file contains a clearly correct bug fix: source flags now carry the actual time-step count the C++ binary requires. The safer shape[-1] indexing resolves the previously flagged 1-D array edge case. No new code paths, no API changes, and no test regressions are present.

No files require special attention.

Important Files Changed

Filename Overview
kwave/solvers/cpp_simulation.py Fixes source flags to encode the number of time steps instead of a boolean; uses safe shape[-1] indexing and adds an explanatory comment.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant CppSimulation
    participant HDF5 as HDF5 Input File
    participant Binary as C++ k-Wave Binary

    Caller->>CppSimulation: run(source, ...)
    CppSimulation->>CppSimulation: _write_hdf5()
    Note over CppSimulation: Compute p_source_flag = source.p.shape[-1]<br/>(was: int(has_p) always 1)
    CppSimulation->>HDF5: write p_source_flag = Nt_source
    CppSimulation->>HDF5: write ux/uy/uz_source_flag = Nt_source
    CppSimulation->>HDF5: write p_source_input[n_elements, Nt_source]
    Binary->>HDF5: read p_source_flag → knows Nt_source time steps
    Binary->>Binary: simulate for Nt steps using Nt_source-length signal
Loading

Reviews (2): Last reviewed commit: "Merge branch 'master' into fix-cpp-sourc..." | Re-trigger Greptile

waltsims and others added 3 commits March 30, 2026 08:51
Provides build/test commands, architecture overview, and project-specific
naming conventions so future Claude Code sessions can be productive immediately.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The C++ binary expects p_source_flag / u{x,y,z}_source_flag to contain
the number of time steps in the source signal (matching legacy
kWaveSimulation.source_p behavior), not just 0/1. Writing a boolean
caused the binary to reject valid HDF5 input files with time-varying
sources.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
These fields carry the source signal length, not a boolean — the naming
is inherited from the C++ binary's HDF5 API.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.40%. Comparing base (2ea27db) to head (aab1fea).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #698   +/-   ##
=======================================
  Coverage   74.40%   74.40%           
=======================================
  Files          56       56           
  Lines        8026     8026           
  Branches     1570     1570           
=======================================
  Hits         5972     5972           
  Misses       1437     1437           
  Partials      617      617           
Flag Coverage Δ
3.10 74.40% <ø> (ø)
3.11 74.40% <ø> (ø)
3.12 74.40% <ø> (ø)
3.13 74.40% <ø> (ø)
macos-latest 74.38% <ø> (ø)
ubuntu-latest 74.38% <ø> (ø)
windows-latest 74.39% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

waltsims and others added 2 commits March 30, 2026 09:31
Safer for both 1-D and 2-D source arrays and communicates intent
more clearly.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@waltsims
Copy link
Copy Markdown
Owner Author

@greptile-app

@waltsims waltsims merged commit 4fa9e7a into master Mar 30, 2026
31 checks passed
@waltsims waltsims deleted the fix-cpp-source-flags branch March 30, 2026 16:38
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.

[BUG] CppSimulation._write_hdf5 writes p_source_flag as boolean instead of source length

1 participant