Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Benchmarks #326

Draft
wants to merge 53 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
ee058eb
Create planar.py
djps Feb 16, 2024
d9f35dc
Add files via upload
djps Feb 16, 2024
e5a092f
Create README.md
djps Feb 16, 2024
24d12ed
remove unused imports
djps Feb 16, 2024
7db75e4
tx types
djps Feb 16, 2024
4b0d0fd
notebook
djps Feb 19, 2024
f61175d
Created using Colaboratory
djps Feb 19, 2024
fa20560
Created using Colaboratory
djps Feb 19, 2024
108b132
Created using Colaboratory
djps Feb 19, 2024
546197f
Merge branch 'waltsims:master' into benchmarks
djps Feb 23, 2024
9bdc8f1
Created using Colaboratory
djps Feb 25, 2024
98c2205
remove broken part
djps Feb 25, 2024
9be9572
Merge branch 'waltsims:master' into benchmarks
djps Feb 25, 2024
c1ea609
Merge branch 'waltsims:master' into benchmarks
djps Feb 28, 2024
4bacb39
Created using Colaboratory
djps Feb 28, 2024
8b5cf9c
Created using Colaboratory
djps Feb 28, 2024
3bf6f01
Created using Colaboratory
djps Feb 28, 2024
65304c5
Created using Colaboratory
djps Feb 28, 2024
a004e88
Created using Colaboratory - WIP
djps Feb 29, 2024
c8cb457
Update ph1-bm7-sc2.py
djps Feb 29, 2024
fa92384
Created using Colaboratory
djps Feb 29, 2024
89c82bf
Merge branch 'waltsims:master' into benchmarks
djps Feb 29, 2024
225f931
Create ph1-bm8-sc1.py
djps Feb 29, 2024
50b40c9
Add files via upload
djps Feb 29, 2024
2113f68
Create ph1-bm8-sc2.py
djps Feb 29, 2024
05206af
Created using Colaboratory
djps Feb 29, 2024
8b246c8
Created using Colaboratory
djps Feb 29, 2024
b6be079
Created using Colaboratory
djps Mar 4, 2024
8335afe
Created using Colaboratory - not enough memory
djps Mar 4, 2024
6f6928d
sync old changes
djps Mar 6, 2024
b548012
Create ph1-bm3-sc1.py
djps Mar 12, 2024
6a6c1b4
[WIP] bm3
djps Mar 12, 2024
1b08b5c
with graph
djps Mar 12, 2024
4f9c01b
sc1 and sc2
djps Mar 13, 2024
928cf30
Created using Colaboratory
djps Mar 13, 2024
8ce9468
Merge branch 'waltsims:master' into benchmarks
djps Mar 13, 2024
6f97763
first pass at bm4
djps Mar 15, 2024
4ba54b0
Add files via upload
djps Mar 17, 2024
d1254a1
Rename Untitled.ipynb to ph1-bm4-sc1.ipynb
djps Mar 17, 2024
c2604c3
Create ph1-bm5-sc1.py
djps Mar 18, 2024
85d3993
Update README.md
djps Mar 18, 2024
bad1213
Create ph1-bm6-sc1.py
djps Mar 18, 2024
9bf51b8
Create ph1-bm1-sc1.py
djps Mar 18, 2024
a7a3e36
Create ph1-bm2-sc1.py
djps Mar 18, 2024
48cfb5b
add pots to bm6, trim medium memory on bm1,2
djps Mar 18, 2024
993ad45
remove unsused imports
djps Mar 19, 2024
a587e50
add planar element simulations
djps Mar 19, 2024
d95baa6
Update pyproject.toml
djps Mar 19, 2024
a2f35d1
Update pyproject.toml
djps Mar 19, 2024
084d00b
Update pyproject.toml - revert
djps Mar 19, 2024
5c78305
Update filters.py - now with single precision ffts
djps Mar 20, 2024
c819b31
Merge branch 'waltsims:master' into benchmarks
djps Mar 20, 2024
d36f55d
Merge branch 'waltsims:master' into benchmarks
djps Sep 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
258 changes: 258 additions & 0 deletions examples/benchmarks/1/ph1-bm1-sc1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
import numpy as np

import matplotlib.pyplot as plt

from kwave.data import Vector
from kwave.utils.kwave_array import kWaveArray
from kwave.utils.checks import check_stability
from kwave.kgrid import kWaveGrid
from kwave.kmedium import kWaveMedium
from kwave.ksource import kSource
from kwave.ksensor import kSensor
from kwave.utils.signals import create_cw_signals
from kwave.utils.filters import extract_amp_phase
from kwave.kspaceFirstOrder2D import kspace_first_order_2d_gpu

from kwave.options.simulation_options import SimulationOptions
from kwave.options.simulation_execution_options import SimulationExecutionOptions

useMaxTimeStep: bool = True

Nx: int = 141
Nz: int = 241

centre: int = (Nx - 1) // 2

dx: float = 0.5e-3
dz: float = dx

focus: int = 128

focus_coords = [centre, focus]

bowl_coords = [centre, 0]

# =========================================================================
# DEFINE THE MATERIAL PROPERTIES
# =========================================================================

# water
sound_speed = 1500.0
density = 1000.0
alpha_coeff = 0.0

# non-dispersive
alpha_power = 2.0

medium = kWaveMedium(sound_speed=sound_speed,
density=density,
alpha_coeff=alpha_coeff,
alpha_power=alpha_power)

# =========================================================================
# DEFINE THE TRANSDUCER SETUP
# =========================================================================

# bowl radius of curvature [m]
source_roc: float = 64.0e-3

# bowl openning [m]
diameters: float = 64.0e-3

# frequency [Hz]
freq = 500e3

# source pressure [Pa]
source_amp = np.array([60e3])

# phase [rad]
source_phase = np.array([0.0])


# =========================================================================
# DEFINE COMPUTATIONAL PARAMETERS
# =========================================================================

# wavelength
k_min: float = c0 / freq

# points per wavelength
ppw: float = k_min / dx

# number of periods to record
record_periods: int = 3

# compute points per period
ppp: int = 60

# CFL number determines time step
cfl: float = (ppw / ppp)


# =========================================================================
# DEFINE THE KGRID
# =========================================================================

grid_size_points = Vector([Nx, Nz])

grid_spacing_meters = Vector([dx, dz])

# create the k-space grid
kgrid = kWaveGrid(grid_size_points, grid_spacing_meters)


# =========================================================================
# DEFINE THE TIME VECTOR
# =========================================================================

# compute corresponding time stepping
dt = 1.0 / (ppp * freq)

dt_stability_limit = check_stability(kgrid, medium)

if (useMaxTimeStep and (not np.isfinite(dt_stability_limit)) and (dt_stability_limit < dt)):
dt_old = dt
ppp = np.ceil(1.0 / (dt_stability_limit * freq))
dt = 1.0 / (ppp * freq)

# calculate the number of time steps to reach steady state
t_end = np.sqrt(kgrid.x_size**2 + kgrid.y_size**2) / c0_min

# create the time array using an integer number of points per period
Nt = round(t_end / dt)

# make time array
kgrid.setTime(Nt, dt)

# calculate the actual CFL after adjusting for dt
cfl_actual = 1.0 / (dt * freq)


# =========================================================================
# DEFINE THE SOURCE PARAMETERS
# =========================================================================

# create empty kWaveArray this specfies the transducer properties
karray = kWaveArray(bli_tolerance=0.01,
upsampling_rate=16,
single_precision=True)

# set bowl position and orientation
bowl_pos = [kgrid.x_vec[bowl_coords[0]].item(),
kgrid.y_vec[bowl_coords[1]].item()]

focus_pos = [kgrid.x_vec[focus_coords[0]].item(),
kgrid.y_vec[focus_coords[1]].item()]

# add bowl shaped element
karray.add_arc_element(bowl_pos, source_roc, diameters, focus_pos)

# create time varying source
source_sig = create_cw_signals(np.squeeze(kgrid.t_array),
freq,
source_amp,
source_phase)

# make a source object.
source = kSource()

# assign binary mask using the karray
source.p_mask = karray.get_array_binary_mask(kgrid)

# assign source pressure output in time
source.p = karray.get_distributed_source_signal(kgrid, source_sig)


# =========================================================================
# DEFINE THE SENSOR PARAMETERS
# =========================================================================

sensor = kSensor()

# set sensor mask: the mask says at which points data should be recorded
sensor.mask = np.ones((Nx, Nz), dtype=bool)

# set the record type: record the pressure waveform
sensor.record = ['p']

# record the final few periods when the field is in steady state
sensor.record_start_index = kgrid.Nt - (record_periods * ppp) + 1


# =========================================================================
# DEFINE THE SIMULATION PARAMETERS
# =========================================================================

DATA_CAST = 'single'
DATA_PATH = './'

input_filename = 'ph1_bm1_sc1_input.h5'
output_filename = 'ph1_bm1_sc1_output.h5'

# options for writing to file, but not doing simulations
simulation_options = SimulationOptions(
data_cast=DATA_CAST,
data_recast=True,
save_to_disk=True,
input_filename=input_filename,
output_filename=output_filename,
save_to_disk_exit=False,
data_path=DATA_PATH,
pml_inside=False)

execution_options = SimulationExecutionOptions(
is_gpu_simulation=True,
delete_data=False,
verbose_level=2)

# =========================================================================
# RUN THE SIMULATION
# =========================================================================

sensor_data = kspace_first_order_2d_gpu(
medium=medium,
kgrid=kgrid,
source=source,
sensor=sensor,
simulation_options=simulation_options,
execution_options=execution_options)


# =========================================================================
# VISUALIZATION
# =========================================================================

# sampling frequency
fs = 1.0 / kgrid.dt

# get Fourier coefficients
amp, _, _ = extract_amp_phase(sensor_data['p'].T, fs, freq, dim=1, fft_padding=1,
window='Rectangular')

# reshape to array
p = np.reshape(amp, (Nx, Nz), order='F')

# axes for plotting
x_vec = kgrid.x_vec
y_vec = kgrid.y_vec[0] - kgrid.y_vec

fig1, ax1 = plt.subplots(1, 1)
p1 = ax1.pcolormesh(1e3 * np.squeeze(x_vec),
1e3 * np.squeeze(y_vec),
np.flip(p.T, axis=1) / 1e3,
shading='gouraud', cmap='viridis')
ax1.set(xlabel='Lateral Position [mm]',
ylabel='Axial Position [mm]',
title='PH1-BM1-SC1')
ax1.set_aspect('equal')
cbar1 = fig1.colorbar(p1, ax=ax1)
_ = cbar1.ax.set_title('[kPa]', fontsize='small')

fig2, ax2 = plt.subplots(1, 1)
ax2.plot(-1e3 * y_vec, p[(Nx - 1) // 2, :] / 1e3 )
ax2.set(xlabel='Axial Position [mm]',
ylabel='Pressure [kPa]',
title='PH1-BM1-SC1')
ax2.grid(True)

plt.show()
Loading
Loading