waltsims / k-wave-python

A Python interface to k-Wave GPU accelerated binaries
https://k-wave-python.readthedocs.io/en/latest/
GNU General Public License v3.0
100 stars 27 forks source link

[BUG] Question about exponentially increasing pressure when adding heterogenous density #353

Closed ivanmkc closed 4 months ago

ivanmkc commented 5 months ago

Describe the bug In some situations, adding heterogenous density results in greatly increasing pressure.

I'm new to ultrasound and k-wave so perhaps this is user error.

To Reproduce See attached notebook.

Adding a tiny area of heterogenous density is enough to reproduce this behaviour.

Ultrasound_Simulation2D(k_wave_python) (1).ipynb.zip

Expected behavior I won't expect a tiny area of different density to lead to enormous increases in pressure.

Screenshots Increasing pressure over time

Screenshot 2024-03-25 at 5 19 43 AM

Desktop (please complete the following information):

Colab

Additional context Add any other context about the problem here.

djps commented 5 months ago

Can you reproduce this in matlab? I can reproduce this in your notebook.

I get sensible results when I lower the cfl number to 0.03 when looking at the p_max. The further post-processing crashes for me though with colab as it runs out of memory.

There is a check_stability function in utils.checks which will shorten the time step if needed.

Does this help?

ivanmkc commented 4 months ago

Thanks @djps. I haven't been using Matlab but made some progress in Python.

  1. Changing cfl to 0.03 doesn't change anything for me fixes it for me as well.
  2. check_stability returns inf regardless of if the line is commented out or not, indicating that the "model is unconditionally stable". However, the function help mentions:
    The time steps given are accurate when the medium properties are
    homogeneous. For a heterogeneous media they give a useful, but not
    exact, estimate.

I'll do more reading on how to set the cfl. Is that just a hyper-parameter to tweak and test?

ivanmkc commented 4 months ago

Here is some additional testing. It seems like there are variations (see the diagonal artifacts) even at lower cfl values. If anyone knows a principled way to determine the correct cfl, please let me know.

Are lower cfl values strictly more "correct" than higher cfl values?

Screenshot 2024-04-09 at 3 41 03 PM
djps commented 4 months ago

The two-points-per-wavelength rule is to be able to resolve the wave.

The simulations seem to be unstable above the a cfl of 0.045, given the huge ´p_max´.

In general I think the lower the cfl the better. There will be textbooks on the numerical solutions of partial differential equations, with chapters on the wave equation. Also Fornberg's "A Practical Guide to Pseudospectral Methods"

More correct is tricky - my feeling is that the diagonal lines are not spurious, but due to the faint inference from the one source and reflection from the interface due to the other source which are only seen at higher resolutions.

waltsims commented 4 months ago

@djps Thanks for looking into this.

@ivanmkc Running with a CFL number of 0.03 I was able to produce stable simulations using your notebook.

Another good resource for you could be Section 2.7 of the k-Wave manual if you haven't already found it. pressure

I hope that's helpful.

waltsims commented 4 months ago

Closing for now