-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathivp_binary_sensor_mask.py
More file actions
163 lines (133 loc) · 4.56 KB
/
ivp_binary_sensor_mask.py
File metadata and controls
163 lines (133 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""
Using A Binary Sensor Mask Example
Ported from: k-Wave/examples/example_ivp_binary_sensor_mask.m
Demonstrates how to use a binary sensor mask for detection of the pressure
field generated by an initial pressure distribution within a two-dimensional
homogeneous propagation medium. It builds on the Homogeneous Propagation
Medium Example.
Instead of the Cartesian sensor used in the homogeneous example, this example
uses a binary arc created with make_circle. You should observe the same two
expanding wavefronts, now sampled at grid-aligned sensor positions along a
three-quarter arc.
"""
# %%
import numpy as np
from kwave.data import Vector
from kwave.kgrid import kWaveGrid
from kwave.kmedium import kWaveMedium
from kwave.ksensor import kSensor
from kwave.ksource import kSource
from kwave.kspaceFirstOrder import kspaceFirstOrder
from kwave.utils.mapgen import make_circle, make_disc
# %%
def setup():
"""Set up the simulation physics (grid, medium, source).
Returns:
tuple: (kgrid, medium, source)
"""
# create the computational grid
Nx = 128 # number of grid points in the x direction
Ny = 128 # number of grid points in the y 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]
kgrid = kWaveGrid(Vector([Nx, Ny]), Vector([dx, dy]))
# define the properties of the propagation medium
medium = kWaveMedium(
sound_speed=1500, # [m/s]
alpha_coeff=0.75, # [dB/(MHz^y cm)]
alpha_power=1.5,
)
# create initial pressure distribution using make_disc
# -- disc 1 --
disc_1_magnitude = 5 # [Pa]
disc_1_x_pos = 50 # [grid points, 1-based]
disc_1_y_pos = 50 # [grid points, 1-based]
disc_1_radius = 8 # [grid points]
disc_1 = disc_1_magnitude * make_disc(Vector([Nx, Ny]), Vector([disc_1_x_pos, disc_1_y_pos]), disc_1_radius)
# -- disc 2 --
disc_2_magnitude = 3 # [Pa]
disc_2_x_pos = 80 # [grid points, 1-based]
disc_2_y_pos = 60 # [grid points, 1-based]
disc_2_radius = 5 # [grid points]
disc_2 = disc_2_magnitude * make_disc(Vector([Nx, Ny]), Vector([disc_2_x_pos, disc_2_y_pos]), disc_2_radius)
source = kSource()
source.p0 = (disc_1 + disc_2).astype(float)
# set time array
kgrid.makeTime(1500)
return kgrid, medium, source
# %%
def run(backend="python", device="cpu", quiet=True):
"""Run with the original binary arc sensor.
Returns:
dict: Simulation results with key 'p' (sensor_points x time_steps).
"""
kgrid, medium, source = setup()
Nx = 128 # number of grid points in the x direction
Ny = 128 # number of grid points in the y direction
# define a binary sensor mask (arc)
sensor_x_pos = Nx // 2 # [grid points, 1-based]
sensor_y_pos = Ny // 2 # [grid points, 1-based]
sensor_radius = Nx // 2 - 22 # [grid points]
sensor_arc_angle = 3 * np.pi / 2 # [radians]
sensor_mask = make_circle(
Vector([Nx, Ny]),
Vector([sensor_x_pos, sensor_y_pos]),
sensor_radius,
sensor_arc_angle,
)
sensor = kSensor(mask=sensor_mask)
sensor.record = ["p"]
return kspaceFirstOrder(
kgrid,
medium,
source,
sensor,
backend=backend,
device=device,
quiet=quiet,
pml_inside=True,
)
# %%
if __name__ == "__main__":
import matplotlib.pyplot as plt
result = run(quiet=False)
p = np.asarray(result["p"])
Nx, Ny = 128, 128
kgrid, _, source = setup()
# build sensor mask for overlay
sensor_mask = make_circle(
Vector([Nx, Ny]),
Vector([Nx // 2, Ny // 2]),
Nx // 2 - 22,
3 * np.pi / 2,
)
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
# plot the initial pressure and sensor distribution
ax = axes[0]
overlay = source.p0 + 5.0 * sensor_mask
ax.imshow(
overlay.T,
extent=[
kgrid.x_vec[0] * 1e3,
kgrid.x_vec[-1] * 1e3,
kgrid.y_vec[-1] * 1e3,
kgrid.y_vec[0] * 1e3,
],
vmin=-1,
vmax=1,
cmap="RdBu_r",
)
ax.set_ylabel("x-position [mm]")
ax.set_xlabel("y-position [mm]")
ax.set_title("Initial Pressure + Sensor Mask")
ax.set_aspect("equal")
# plot the simulated sensor data
ax = axes[1]
im = ax.imshow(p, aspect="auto", vmin=-1, vmax=1)
ax.set_ylabel("Sensor Position")
ax.set_xlabel("Time Step")
ax.set_title("Sensor Data")
fig.colorbar(im, ax=ax)
fig.suptitle("Binary Sensor Mask Example")
fig.tight_layout()
plt.show()