<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>k-Wave User Forum &#187; Topic: Unexpected instability</title>
		<link>http://www.k-wave.org/forum/topic/unexpected-instability</link>
		<description>Support for the k-Wave MATLAB toolbox</description>
		<language>en-US</language>
		<pubDate>Wed, 13 May 2026 06:07:24 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://www.k-wave.org/forum/search.php</link>
		</textInput>
		<atom:link href="http://www.k-wave.org/forum/rss/topic/unexpected-instability" rel="self" type="application/rss+xml" />

		<item>
			<title>Foufour on "Unexpected instability"</title>
			<link>http://www.k-wave.org/forum/topic/unexpected-instability#post-6538</link>
			<pubDate>Fri, 27 Jul 2018 20:16:42 +0000</pubDate>
			<dc:creator>Foufour</dc:creator>
			<guid isPermaLink="false">6538@http://www.k-wave.org/forum/</guid>
			<description>&#60;p&#62;Hi Brad,&#60;/p&#62;
&#60;p&#62;I didn't find a solution in the meantime. I even switched off smoothing, and smoothed manually. Same result. I replaced the external source file by a simple ball, and got the same instability again. So yes, I can create an example that doesn't depend on external functions or data that has the same problem. The only thing you'll have to change is the path of the created HDF5 input file (if you want to create one at all). The code follows...&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;%% Input parameters
grid_length = 0.1; % length of kgrid in m
grid_width = 0.1; % width of kgrid in m
axial_spacing = 250e-6;
% spacing between neighbouring points in axial direction (dx) in m
lateral_spacing = 250e-6;
% spacing between neighbouring points in lateral direction (dy,dz) in m
time_step = 25e-9; % simulation time step in s
total_time = 35e-6; % total simulation time in s

T = 25; % temperature in °C

d_geant_grid = 100e-6; % grid spacing of the above source file

smooth_p0 = true;
% Boolean controlling whether initial pressure distribution gets smoothed automatically
smooth_density = true;
% Boolean controlling whether density distribution gets smoothed automatically
smooth_soundspeed = true;
% Boolean controlling whether sound speed distribution gets smoothed automatically
StreamToDisk = true;
% Boolean controlling whether simulation data are stored on disk during simulation
DataCast = &#38;#39;gpuArray-double&#38;#39;;
% Variable precision &#38;amp; simulation mode. Possible: &#38;#39;single&#38;#39;, &#38;#39;double&#38;#39;, &#38;#39;gpuArray-double&#38;#39;, &#38;#39;gpuArray-single&#38;#39;
SaveToDisk = &#38;#39;sirmiobeam-double-250-in&#38;#39;;
% File name of the HDF5 input file
PMLInside = false;

%% Creating kWaveGrid
% In all dimensions, the grid should have as small prime factors as
% possible. That&#38;#39;s why I chose to increase the grid to the next power of 2.
% The 20 points subtracted will get re-added in the end as PML.
Nx = 2^nextpow2(grid_length/axial_spacing)-20;
Ny = 2^nextpow2(grid_width/lateral_spacing)-20;
Nz = Ny;

kgrid = kWaveGrid(Nx,axial_spacing,Ny,lateral_spacing,Nz,lateral_spacing);

kgrid.dt = time_step;
kgrid.Nt = round(total_time/time_step);

%% Creating medium
sound_speedwater = 1.40238677e3 + 5.03798765*T - 5.80980033e-2*T.^2 ...
+ 3.34296650e-4*T.^3 - 1.47936902e-6*T.^4 + 3.14893508e-9*T.^5; %[m/s]

densitywater = 999.9 + T.*(0.06079 + T.*(-0.008313 + T.*(6.595e-5 + T.* ...
    (-4.437e-7 + 1.369e-9.*T))));%[kg/m^3]

medium = struct(&#38;#39;sound_speed&#38;#39;, sound_speedwater,...
    &#38;#39;density&#38;#39;, densitywater, &#38;#39;sound_speed_ref&#38;#39;, sound_speedwater);

%% Creating source
raw_source.p0 = makeBall(500,1000,1000,200,500,500,50);
% This is similar to what my function new_read_Geant does - it creates a
% 500x1000x1000 source matrix. I simplified this to a ball source instead of
% a Geant4 simulation output for you. The behaviour in terms of instablilty
% is the same.

%% Adjusting the source to the spacing of the k-Wave grid
% This part corresponds to my adjust_grids function, in an attempt to
% become independent of the inherent grid spacing of the source file.
[x_rawlength, y_rawlength, z_rawlength] = size(raw_source.p0);

x_newlength = ceil(x_rawlength * d_geant_grid / kgrid.dx);
y_newlength = ceil(y_rawlength * d_geant_grid / kgrid.dy);
z_newlength = ceil(z_rawlength * d_geant_grid / kgrid.dz);

[y_geant, x_geant, z_geant] = meshgrid((1 : y_rawlength) * d_geant_grid,...
    (1 : x_rawlength) * d_geant_grid, (1 : z_rawlength) * d_geant_grid);
[y_kgrid, x_kgrid, z_kgrid] = meshgrid((1 : y_newlength) * kgrid.dy,...
    (1 : x_newlength) * kgrid.dx, (1 : z_newlength) * kgrid.dz);
raw_source.p0 = interp3(y_geant, x_geant, z_geant, raw_source.p0,...
    y_kgrid, x_kgrid, z_kgrid);

%% Put the source into the kgrid
% This is similar to my makeSource_spatial3D function. It creates an empty
% container of the size of the k-Wave grid, and puts the raw source into
% this container.

source.p0 = zeros(kgrid.Nx, kgrid.Ny, kgrid.Nz);

y_shift = floor((kgrid.Ny - y_newlength) / 2);
z_shift = floor((kgrid.Nz - z_newlength) / 2);

source.p0(1 : x_newlength, y_shift + (1 : y_newlength),...
    z_shift + (1 : z_newlength)) = raw_source.p0;

%% Creating sensor

y_bragg = floor(kgrid.Ny/2)+1; % index
z_bragg = floor(kgrid.Nz/2)+1; % index
[~,x_bragg] = max(source.p0(:,y_bragg,z_bragg)); %index

sensor.mask = zeros(Nx, Ny, Nz);
sensor.mask(x_bragg + round(0.025/kgrid.dx), y_bragg, z_bragg) = 1;

%% Additional input parameters

input_fullpath = strcat(&#38;#39;/project/med4/IONOAKUSTIK/SimulationAndreas/&#38;#39;,...
    SaveToDisk,&#38;#39;.h5&#38;#39;);

cvsmooth = [smooth_p0, smooth_soundspeed, smooth_density];

input_args={&#38;#39;PMLInside&#38;#39;, PMLInside, &#38;#39;Smooth&#38;#39;, cvsmooth, &#38;#39;DataCast&#38;#39;,...
    DataCast, &#38;#39;StreamToDisk&#38;#39;, StreamToDisk, &#38;#39;SaveToDisk&#38;#39;, input_fullpath};

kspaceFirstOrder3D(kgrid,medium,source,sensor,input_args{:});&#60;/code&#62;&#60;/pre&#62;</description>
		</item>
		<item>
			<title>Bradley Treeby on "Unexpected instability"</title>
			<link>http://www.k-wave.org/forum/topic/unexpected-instability#post-6517</link>
			<pubDate>Fri, 13 Jul 2018 21:54:49 +0000</pubDate>
			<dc:creator>Bradley Treeby</dc:creator>
			<guid isPermaLink="false">6517@http://www.k-wave.org/forum/</guid>
			<description>&#60;p&#62;Hi Foufour,&#60;/p&#62;
&#60;p&#62;Can you create an example that doesn't depend on external functions or data that has the same problem? Then I can take a look.&#60;/p&#62;
&#60;p&#62;Brad.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Foufour on "Unexpected instability"</title>
			<link>http://www.k-wave.org/forum/topic/unexpected-instability#post-6507</link>
			<pubDate>Thu, 28 Jun 2018 10:29:00 +0000</pubDate>
			<dc:creator>Foufour</dc:creator>
			<guid isPermaLink="false">6507@http://www.k-wave.org/forum/</guid>
			<description>&#60;p&#62;Here comes the matlab code...&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;%% Input parameters
grid_length = 0.1; % length of kgrid in m
grid_width = 0.1; % width of kgrid in m
axial_spacing = 250e-6; % spacing between neighbouring points in axial direction (dx) in m
lateral_spacing = 250e-6; % spacing between neighbouring points in lateral direction (dy,dz) in m
time_step = 25e-9; % simulation time step in s
total_time = 35e-6; % total simulation time in s

T = 25; % temperature in °C

source_filename = &#38;#39;/project/med4/IONOAKUSTIK/SimulationAndreas/Marco_SIRMIO_Geant4data/TestSignalForIonoacoustics_50MeV_SigmaEnergy_1MeV_voxelSize_0.1x0.1x0.1mm_FWHM_2mm.txt&#38;#39;;
geant_grid_spacing = 100e-6; % grid spacing of the above source file

detector_shape = &#38;#39;point&#38;#39;; % String. Possible values: &#38;#39;planar&#38;#39;, &#38;#39;spherical&#38;#39;, &#38;#39;point&#38;#39;, &#38;#39;rahmen&#38;#39;, &#38;#39;multiple_points&#38;#39;, &#38;#39;triple_planar&#38;#39;

smooth_p0 = true; % Boolean controlling whether initial pressure distribution gets smoothed automatically
smooth_density = true; % Boolean controlling whether density distribution gets smoothed automatically
smooth_soundspeed = true; % Boolean controlling whether sound speed distribution gets smoothed automatically
StreamToDisk = true; % Boolean controlling whether simulation data are stored on disk during simulation
DataCast = &#38;#39;gpuArray-double&#38;#39;; % Variable precision &#38;amp; simulation mode. Possible: &#38;#39;single&#38;#39;, &#38;#39;double&#38;#39;, &#38;#39;gpuArray-double&#38;#39;, &#38;#39;gpuArray-single&#38;#39;
SaveToDisk = &#38;#39;sirmiobeam-double-250-in&#38;#39;; % File name of the HDF5 input file
PMLInside = false;

%% Creating kWaveGrid
% In all dimensions, the grid should have as small prime factors as
% possible. That&#38;#39;s why I chose to increase the grid to the next power of 2.
% The 20 points subtracted will get re-added in the end as PML.
Nx = 2^nextpow2(grid_length/axial_spacing)-20;
Ny = 2^nextpow2(grid_width/lateral_spacing)-20;
Nz = Ny;

kgrid = kWaveGrid(Nx,axial_spacing,Ny,lateral_spacing,Nz,lateral_spacing);

kgrid.dt = time_step;
kgrid.Nt = round(total_time/time_step);

%% Creating medium
sound_speedwater=1.40238677e3 + 5.03798765*T -5.80980033e-2*T.^2 + 3.34296650e-4*T.^3 -1.47936902e-6*T.^4 + 3.14893508e-9*T.^5; %[m/s]

densitywater = 999.9+T.*(0.06079+T.*(-0.008313+T.*(6.595e-5+T.*(-4.437e-7+1.369e-9.*T))));%[kg/m^3]

medium = struct(&#38;#39;sound_speed&#38;#39;, sound_speedwater, &#38;#39;density&#38;#39;, densitywater, &#38;#39;sound_speed_ref&#38;#39;, sound_speedwater);

%% Crating source
source = new_Read_Geant(source_filename,T); % Get the data from *.dat format into matlab
[source, ~] = adjust_grids(kgrid,source,geant_grid_spacing,0);
% I added this because I wanted to become independent of the grid spacing
% of the Geant4 files. So I decided to transform the source file from its
% original grid spacing into that of my kgrid
source = makeSource_spatial3D(source,kgrid.Nx,kgrid.Ny,kgrid.Nz,kgrid.dx,0,0,0,0,0);
% Put the source into the kgrid.

%% Creating sensor

y_bragg = floor(kgrid.Ny/2)+1; % index
z_bragg = floor(kgrid.Nz/2)+1; % index
[~,x_bragg] = max(source.p0(:,y_bragg,z_bragg)); %index

sensor.mask = zeros(Nx, Ny, Nz);
sensor.mask(x_bragg + round(0.025/kgrid.dx), y_bragg, z_bragg) = 1;

%% Additional input parameters

input_fullpath = strcat(&#38;#39;/project/med4/IONOAKUSTIK/SimulationAndreas/&#38;#39;,SaveToDisk,&#38;#39;.h5&#38;#39;);

cvsmooth = [smooth_p0, smooth_soundspeed, smooth_density];

input_args={&#38;#39;PMLInside&#38;#39;, PMLInside, &#38;#39;Smooth&#38;#39;, cvsmooth, &#38;#39;DataCast&#38;#39;, DataCast, &#38;#39;StreamToDisk&#38;#39;, StreamToDisk, &#38;#39;SaveToDisk&#38;#39;, input_fullpath};

kspaceFirstOrder3D(kgrid,medium,source,sensor,input_args{:});&#60;/code&#62;&#60;/pre&#62;</description>
		</item>
		<item>
			<title>Foufour on "Unexpected instability"</title>
			<link>http://www.k-wave.org/forum/topic/unexpected-instability#post-6506</link>
			<pubDate>Thu, 28 Jun 2018 09:45:10 +0000</pubDate>
			<dc:creator>Foufour</dc:creator>
			<guid isPermaLink="false">6506@http://www.k-wave.org/forum/</guid>
			<description>&#60;p&#62;Hi,&#60;/p&#62;
&#60;p&#62;I'm facing instability issues I don't understand. I'm simulating in 3D in homogeneous water. On coarse grids (1 mm and 500 µm spacings, with time steps of 100 and 50 ns, respectively), everything behaves exactly as I expect. However, when I set the spacing to 250 µm, the simulation is no longer stable, but blows up immediately, no matter how small I choose the time step (even tried 1 ns).&#60;/p&#62;
&#60;p&#62;The medium doesn't change (since it's homogeneous), so I checked the source. I'm using the same source file for all grids and match it onto the kgrid. This happens automatically, the only code lines I change are really the grid spacing and the time step.&#60;/p&#62;
&#60;p&#62;I re-imported the source from the HDF5 input file to investigate it. But it looks fine. No steeper gradients than on the coarse grid (max. 25 Pa/m, corresponding to 6 mPa between two grid points), and the shape looks fine as well.&#60;/p&#62;
&#60;p&#62;I'm running out of explanations... I'll extract the matlab code lines for you. Could you please give me an idea of what went wrong?&#60;/p&#62;
&#60;p&#62;Kind regards,&#60;br /&#62;
Foufour
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
