Repository navigation
Conversation
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.
Added methods to check uniformity of diffusion and perfusion coefficients, and updated the source setup to handle material properties more robustly.
| solver = kWaveDiffusion( | ||
| grid, | ||
| DiffusionMedium( | ||
| diffusion_coeff=np.full(N, D) | ||
| ), | ||
| DiffusionSource(T0=T0), | ||
| display_updates=False, | ||
| ) |
There was a problem hiding this comment.
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!
| max_rate = float( | ||
| self.xp.max( | ||
| diffusion_coeff * self.k_squared | ||
| + P | ||
| ).item() | ||
| ) | ||
|
|
||
| if max_rate <= 0: | ||
| return np.inf |
There was a problem hiding this comment.
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 pull requests suggests adding kWaveDiffusion to k-wave-python, ported from the MATLAB k-Wave toolbox. Please provide any feedback and suggestions.
Fix the heterogeneous
dt_limitcalculation before merging.Findings
Summary
Adds
kWaveDiffusionfor periodic thermal simulations, with optional CuPy execution, heat sources, perfusion, sensor readings, and thermal dose.Reviews (3) · Last reviewed commit: "Implement uniformity checks for diffusio..." · Reviewed by Greptile