k-Wave Toolbox |

## Simulations In Three Dimensions Example

On this page…
Overview Creating the k-space grid and defining the medium properties Defining the initial pressure distribution Defining the sensor mask Running the simulation A note on Cartesian sensor masks

### Overview

This example provides a simple demonstration of using k-Wave for the simulation and detection of the pressure field generated by an initial pressure distribution within a three-dimensional heterogeneous propagation medium. It builds on the Homogeneous Propagation Medium and Heterogeneous Propagation Medium examples.

- open the file in the MATLAB Editor
- run the file in MATLAB

Back to Top

### Creating the k-space grid and defining the medium properties

Simulations in three-dimensions are performed in an analogous fashion to those in one- and two-dimensions. The medium discretisation is again performed by `makeGrid` with two additional inputs for the z-dimension (note the ordering of the inputs). Similarly, matrices for the properties of the acoustic propagation medium also have an extra dimension. The matrix indexing convention (x, y, z) is an extension to that used in two-dimensions (x, y) where matrix row and column data correspond to the x- and y- axes, respectively.

    % create the computational grid
    Nx = 64;            % number of grid points in the x direction
    Ny = 64;            % number of grid points in the y direction
    Nz = 64;            % number of grid points in the z direction
    dx = 0.1e-3;        % grid point spacing in the x direction [m]
    dy = 0.1e-3;        % grid point spacing in the y direction [m]
    dz = 0.1e-3;        % grid point spacing in the z direction [m]
    kgrid = makeGrid(Nx, dx, Ny, dy, Nz, dz);

    % define the properties of the propagation medium
    medium.sound_speed = 1500*ones(Nx, Ny, Nz);	% [m/s]
    medium.sound_speed(1:Nx/2, :, :) = 1800;    % [m/s]
    medium.density = 1000*ones(Nx, Ny, Nz);     % [kg/m^3]
    medium.density(:, Ny/4:end, :) = 1200;      % [kg/m^3]

Back to Top

### Defining the initial pressure distribution

As in one- and two-dimensions, the initial pressure distribution is simply a matrix of arbitrary numeric values over the medium discretisation given by the k-Wave grid. Here `makeBall` is used to create an initial pressure distribution of two small filled balls with different diameters.

    % create initial pressure distribution using makeBall
    ball_magnitude = 10;    % [au]
    ball_x_pos = 36;        % [grid points]
    ball_y_pos = 36;        % [grid points]
    ball_z_pos = 36;        % [grid points]
    ball_radius = 5;        % [grid points]
    ball_1 = ball_magnitude*makeBall(Nx, Ny, Nz, ball_x_pos, ball_y_pos, ball_z_pos, ball_radius);

    ball_magnitude = 10;    % [au]
    ball_x_pos = 20;        % [grid points]
    ball_y_pos = 20;        % [grid points]
    ball_z_pos = 20;        % [grid points]
    ball_radius = 3;        % [grid points]
    ball_2 = ball_magnitude*makeBall(Nx, Ny, Nz, ball_x_pos, ball_y_pos, ball_z_pos, ball_radius);

    source.p0 = ball_1 + ball_2;

Back to Top

### Defining the sensor mask

Again both Cartesian and binary sensor masks can be used to define the locations where the pressure-field is recorded at each time-step. Several functions for three-dimensional geometry creation are included in the k-Wave toolbox, including `makeSphere` which returns a binary map of single pixel spherical shell created using the midpoint circle algorithm, and `makeCartSphere` which returns the Cartesian location of an evenly distributed array of an arbitrary number of points on the surface of a sphere using the Golden Section Spiral method. Here a linear sensor array is explicitly created.

    % define a series of Cartesian points to collect the data
    x = (-22:2:22)*dx;          % [m]
    y = 22*dy*ones(size(x));    % [m]
    z = (-22:2:22)*dz;          % [m]
    sensor.mask = [x; y; z];

A visualisation of the initial pressure distribution and the sensor mask using `cart2grid` and `voxelPlot` is given below.

Back to Top

### Running the simulation

The computation is invoked by running `kspaceFirstOrder3D` with the inputs defined above. By default, a visualisation of the propagating wave-field (through the horizontal, median, and frontal planes) and a status bar are displayed.

    % input arguments
    input_args = {'PlotLayout', true, 'PlotPML', false, 'DataCast', 'single'};

    % run the simulation
    sensor_data = kspaceFirstOrder3D(kgrid, medium, source, sensor, input_args{:});

By setting the optional input parameter `'PlotLayout'` to `true`, a plot of the initial pressure, and sound speed and density (if heterogenous) are produced. To remove the PML from the display, the optional input `'PlotPML'` is also set to `false`.

As the function runs, status updates and computational parameters are printed to the command line. By setting the optional input `'DataCast'` to `'single'`, the computational time is decreased (see the Optimising k-Wave Performance Example for more details).

    Running k-Wave simulation...
      start time: 18-Oct-2011 09:47:56
      reference sound speed: 1800m/s
      dt: 16.6667ns, t_end: 6.15us, time steps: 370
      input grid size: 64 by 64 by 64 pixels (6.4 by 6.4 by 6.4mm)
      maximum supported frequency: 7.5MHz
      smoothing p0 distribution...
      casting variables to single type...
      precomputation completed in 1.2331s
      starting time loop...
      estimated simulation time 23.6016s...
      simulation completed in 26.9362s
      reordering Cartesian measurement data...
      total computation time 28.205s

When the time loop has completed, the function returns the recorded time series at each of sensor locations defined by `sensor.mask`. The ordering is again dependent on whether a Cartesian or binary sensor mask is used. A visualisation is given below.

Back to Top

### A note on Cartesian sensor masks

As with the two-dimensional simulations, the interpolation mode when using a Cartesian sensor mask may be set to `'linear'` or `'nearest'` (the default in three-dimensions). If `'CartInterp'` is set to `'linear'`, `TriScatteredInterp` is used to compute the Delaunay triangulation during the precomputation (note, this was only introduced in MATLAB R2009a). For large 3D matrices, this can significantly add to the precomputation time (see the output below for the same example run with `'CartInterp'` set to `'linear'`). Note, the inbuilt function `TriScatteredInterp` only accepts inputs of type `'double'`, thus `'DataCast'` must be set to `'off'` (the default value) if using linear interpolation.

    Running k-Wave simulation...
      start time: 18-Oct-2011 10:08:44
      reference sound speed: 1800m/s
      dt: 16.6667ns, t_end: 6.15us, time steps: 370
      input grid size: 64 by 64 by 64 pixels (6.4 by 6.4 by 6.4mm)
      maximum supported frequency: 7.5MHz
      smoothing p0 distribution...
      calculating Delaunay triangulation (TriScatteredInterp)...
      precomputation completed in 25.1272s
      starting time loop...
      estimated simulation time 44.8s...
      simulation completed in 50.4407s
      total computation time 1min 15.598s

In both two- and three- dimensional simulations, if the optional input `'CartInterp'` is set to `'nearest'`, the Cartesian points are internally mapped onto a binary grid. If more than one Cartesian sensor point maps to the same location, the duplicated points are discarded. Consequently, if the Cartesian sensor mask has a dense array of points, the number of returned time-series may not correspond to the original number of Cartesian sensor points. Details of discarded duplicate points are printed to the command line. This situation can be avoided by increasing the resolution of the computational grid, or by reducing the packing of the Cartesian sensor points.

Back to Top

© 2009, 2010, 2011 Bradley Treeby and Ben Cox.