Skip to content

Add kwavediffusion - #789

Open
rhh006 wants to merge 5 commits into
waltsims:masterfrom
rhh006:add-kwavediffusion
Open

rhh006 wants to merge 5 commits into
waltsims:masterfrom
rhh006:add-kwavediffusion

Conversation

@rhh006

@rhh006 rhh006 commented Nov 6, 2026 •

Copy link
Copy Markdown

This pull requests suggests adding kWaveDiffusion to k-wave-python, ported from the MATLAB k-Wave toolbox. Please provide any feedback and suggestions.

RetriggerView in GreptileConfidence Score: 4/5

Fix the heterogeneous dt_limit calculation before merging.

Findings

  1. P1 Unsafe steps look safe ▶
  2. P2 Test skips changed derivatives ▶

Summary

Adds kWaveDiffusion for periodic thermal simulations, with optional CuPy execution, heat sources, perfusion, sensor readings, and thermal dose.

  • Temperature simulations now run from material and heat inputs on a grid.
  • The solver tracks thermal dose and marks affected grid cells.
  • The solver reports a time-step limit for its diffusion setup.

Reviews (3) · Last reviewed commit: "Implement uniformity checks for diffusio..." · Reviewed by Greptile

This PR adds a Python port of the kWaveDiffusion class from the MATLAB k-Wave toolbox to k-Wave-Python, supporting 1D, 2D, and 3D thermal diffusion and perfusion simulations in homogeneous and heterogeneous media, with optional GPU acceleration.
Add a 3D thermal diffusion test for kWaveDiffusion function demonstrating localized heating, perfusion, and CPU/CUDA device selection. Reports temperature, CEM43 thermal dose, and lesion volume.
Comment thread tests/test_kwaveDiffusion.py Resolved
Comment thread tests/test_kwaveDiffusion.py Resolved
Comment thread kwave/kwaveDiffusion.py Resolved
Comment thread kwave/kwaveDiffusion.py Resolved
Comment thread kwave/kwaveDiffusion.py Resolved
Comment thread kwave/kwaveDiffusion.py Resolved
Comment thread kwave/kwaveDiffusion.py Resolved
Comment thread tests/test_kwaveDiffusion.py Resolved
Added methods to check uniformity of diffusion and perfusion coefficients, and updated the source setup to handle material properties more robustly.
Comment on lines +237 to +244
solver = kWaveDiffusion(
grid,
DiffusionMedium(
diffusion_coeff=np.full(N, D)
),
DiffusionSource(T0=T0),
display_updates=False,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Test skips changed derivatives

test_nyquist_mode_decay never reaches the heterogeneous code changed to fix alternating temperature patterns. Its np.full(N, D) coefficient now passes _is_uniform, so the test uses _homogeneous_diffusion_update. Restoring the old, broken first derivatives would still pass this test.

Add an expected-value test that reaches _heterogeneous_diffusion_update and includes an alternating temperature component.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment thread kwave/kwaveDiffusion.py
Comment on lines +1513 to +1521
max_rate = float(
self.xp.max(
diffusion_coeff * self.k_squared
+ P
).item()
)

if max_rate <= 0:
return np.inf

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Unsafe steps look safe

With use_kspace=False, the new dt_limit calculation multiplies spatial coefficients by Fourier frequencies at matching array positions. Those positions do not describe the same thing.

For example, with no perfusion and diffusion_coeff positive only at index zero, the product is zero everywhere because k_squared[0] is zero. The property returns infinity, but _heterogeneous_diffusion_update still applies diffusion and needs a finite step limit. Users relying on this value can choose steps that make temperature errors grow. Bound the actual update instead of multiplying these arrays elementwise.

This branch has not been deployed

No deployments
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.

1 participant