Micha
commited on
Commit
·
c5973f1
1
Parent(s):
0325897
Added generating code for simulations
Browse files- code/KwaveUpdateDomainParams.py +274 -0
- code/reference_file.h5 +3 -0
code/KwaveUpdateDomainParams.py
ADDED
@@ -0,0 +1,274 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
import numpy as np
|
5 |
+
import h5py
|
6 |
+
import argparse
|
7 |
+
import random
|
8 |
+
import shutil
|
9 |
+
import os
|
10 |
+
import scipy.interpolate as si
|
11 |
+
|
12 |
+
from scipy.signal import gaussian
|
13 |
+
from itertools import product
|
14 |
+
|
15 |
+
from typing import Tuple
|
16 |
+
|
17 |
+
def generate_sound_speed(shape = [1152, 512],
|
18 |
+
c_ref: float = 1500., c_min: float = 1300., c_max: float = 1800.,
|
19 |
+
atn_min: float = 0.05, atn_max: float = 0.15,
|
20 |
+
max_ell: int = 6, max_lines: int = 4) -> Tuple[np.ndarray, np.ndarray]:
|
21 |
+
"""[summary]
|
22 |
+
Generate a random speed of sound and attenuation map with up to max_ell ellipses and max_lines straight reflectors
|
23 |
+
|
24 |
+
Args:
|
25 |
+
shape (list, optional): [description]. Defaults to [1152, 512].
|
26 |
+
c_ref (float, optional): [description]. Defaults to 1500..
|
27 |
+
c_min (float, optional): [description]. Defaults to 1300..
|
28 |
+
c_max (float, optional): [description]. Defaults to 1800..
|
29 |
+
atn_min (float, optional): [description]. Defaults to 0.05.
|
30 |
+
atn_max (float, optional): [description]. Defaults to 0.15.
|
31 |
+
max_ell (int, optional): [description]. Defaults to 6.
|
32 |
+
max_lines (int, optional): [description]. Defaults to 4.
|
33 |
+
|
34 |
+
Returns:
|
35 |
+
[type]: [description]
|
36 |
+
"""
|
37 |
+
|
38 |
+
while True:
|
39 |
+
n_ell = np.random.randint(max_ell + 1)
|
40 |
+
n_lines = np.random.randint(max_lines + 1)
|
41 |
+
|
42 |
+
if n_ell + n_lines > 0:
|
43 |
+
break
|
44 |
+
|
45 |
+
ell_type = np.array([0,] * n_ell + [1,] * n_lines)[np.random.permutation(n_ell + n_lines)]
|
46 |
+
c = np.random.rand(n_ell + n_lines + 1) * (c_max - c_min) + c_min
|
47 |
+
atn = np.random.rand(n_ell + n_lines + 1) * (atn_max - atn_min) + atn_min
|
48 |
+
|
49 |
+
ell_params = np.random.rand(5, n_ell); # x0, y0, a, b, \theta in columns
|
50 |
+
|
51 |
+
ell_params[0, :] = ell_params[0, :] * shape[-1]
|
52 |
+
ell_params[1, :] = ell_params[1, :] * shape[-2]
|
53 |
+
ell_params[2, :] = ell_params[2, :] * shape[-1];
|
54 |
+
ell_params[3, :] = ell_params[3, :] * shape[-2];
|
55 |
+
ell_params[4, :] = (ell_params[4, :] - 0.5) * np.pi / 4;
|
56 |
+
|
57 |
+
line_params = np.random.rand(2, n_lines); # Angle and offset
|
58 |
+
|
59 |
+
line_params[0, :] = (line_params[0, :] - 0.5) * np.pi / 6;
|
60 |
+
line_params[1, :] = line_params[1, :] * (shape[-2] - 68) + 68
|
61 |
+
|
62 |
+
sound_speed = np.empty(shape[-2:], dtype=np.single)
|
63 |
+
sound_speed[...] = c[-1]
|
64 |
+
alpha_coeff = np.empty(shape[-2:], dtype=np.single)
|
65 |
+
alpha_coeff[...] = atn[-1]
|
66 |
+
|
67 |
+
i_ellp = 0
|
68 |
+
i_lines = 0
|
69 |
+
|
70 |
+
X, Y = np.meshgrid(range(shape[-1]), range(shape[-2]))
|
71 |
+
|
72 |
+
for i,t in enumerate(ell_type):
|
73 |
+
if t == 0:
|
74 |
+
# elipse
|
75 |
+
x0 = np.array([ell_params[0, i_ellp], ell_params[1, i_ellp]])
|
76 |
+
a = np.array([ell_params[2, i_ellp], ell_params[3, i_ellp]])
|
77 |
+
|
78 |
+
th = ell_params[4, i_ellp]
|
79 |
+
ath = np.abs(th)
|
80 |
+
|
81 |
+
R = np.array([[np.cos(th), -np.sin(th)], [np.sin(th), np.cos(th)]])
|
82 |
+
aR = np.array([[np.cos(ath), -np.sin(ath)], [np.sin(ath), np.cos(ath)]])
|
83 |
+
|
84 |
+
toff = aR.dot(a)[1]
|
85 |
+
|
86 |
+
if x0[1] - toff < 68:
|
87 |
+
x0[1] = 68 + toff
|
88 |
+
|
89 |
+
x = X - x0[0]
|
90 |
+
y = Y - x0[1]
|
91 |
+
|
92 |
+
u = np.cos(th) * x - np.sin(th) * y
|
93 |
+
v = np.sin(th) * x + np.cos(th) * y
|
94 |
+
|
95 |
+
sound_speed[(u / ell_params[2, i_ellp]) ** 2 + (v / min(ell_params[3, i_ellp], ell_params[1, i_ellp] - 68)) ** 2 < 1] = c[i]
|
96 |
+
alpha_coeff[(u / ell_params[2, i_ellp]) ** 2 + (v / min(ell_params[3, i_ellp], ell_params[1, i_ellp] - 68)) ** 2 < 1] = atn[i]
|
97 |
+
|
98 |
+
i_ellp += 1
|
99 |
+
else:
|
100 |
+
# plane
|
101 |
+
th = line_params[0, i_lines]
|
102 |
+
off = line_params[1, i_lines]
|
103 |
+
|
104 |
+
if off - shape[-1] * np.tan(np.abs(th)) < 68:
|
105 |
+
off = shape[-1] * np.tan(np.abs(th)) + 68
|
106 |
+
|
107 |
+
sound_speed[X * np.sin(th) + (Y - off) * np.cos(th) > 0] = c[i]
|
108 |
+
alpha_coeff[X * np.sin(th) + (Y - off) * np.cos(th) > 0] = atn[i]
|
109 |
+
|
110 |
+
i_lines += 1
|
111 |
+
|
112 |
+
return sound_speed, alpha_coeff
|
113 |
+
|
114 |
+
|
115 |
+
def generate_density(shape: tuple = [1152, 1152],
|
116 |
+
d_ref: float = 900.,
|
117 |
+
n_spec: float = -1.,
|
118 |
+
spec_amp: float = 0.1) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
|
119 |
+
"""
|
120 |
+
Generate a speckle map in the density domain, including the x and y derivatives
|
121 |
+
|
122 |
+
|
123 |
+
Args:
|
124 |
+
shape (_type_, optional): shape of output map. Defaults to [1152, 1152]
|
125 |
+
d_ref (float, optional): reference density (kg / m^3). Defaults to 900
|
126 |
+
n_spec (float, optional): speckle density. Defaults to -1
|
127 |
+
spec_amp (float, optional): speckle amplitude. Defaults to 0.1
|
128 |
+
|
129 |
+
Returns:
|
130 |
+
_type_: _description_
|
131 |
+
"""
|
132 |
+
|
133 |
+
if n_spec < 0:
|
134 |
+
n_spec = shape[-1] * shape[-2] * (2. / 64.)
|
135 |
+
|
136 |
+
t_n_spec = int(np.round(n_spec * (10 - np.random.rand() * 8) * 0.1))
|
137 |
+
|
138 |
+
speckle = np.zeros((shape[-2] - 48 - 68, shape[-1] - 48*2), np.single)
|
139 |
+
speckle.reshape(-1,1)[np.random.permutation(speckle.size)[:t_n_spec]] = d_ref * (np.random.rand(t_n_spec, 1) - 1/3) * spec_amp
|
140 |
+
|
141 |
+
rho0 = np.empty(shape[-2:], dtype=np.single)
|
142 |
+
rho0[...] = d_ref
|
143 |
+
|
144 |
+
rho0[68:-48, 48:-48] += speckle
|
145 |
+
|
146 |
+
rho0_sgx = np.empty_like(rho0)
|
147 |
+
rho0_sgx[:,:-1] = (rho0[:,:-1] + rho0[:,1:]) * 0.5
|
148 |
+
rho0_sgx[:,-1] = rho0[:,-1]
|
149 |
+
|
150 |
+
rho0_sgy = np.empty_like(rho0)
|
151 |
+
rho0_sgy[:-1,:] = (rho0[:-1,:] + rho0[1:,:]) * 0.5
|
152 |
+
rho0_sgy[-1,:] = rho0[-1,:]
|
153 |
+
|
154 |
+
return rho0, rho0_sgx, rho0_sgy
|
155 |
+
|
156 |
+
|
157 |
+
def generate_pulse(center_freq=5e6, n_periods=4.4, sampling_freq=40e6):
|
158 |
+
samples_per_cycle = sampling_freq / center_freq
|
159 |
+
n_samples = np.ceil(samples_per_cycle * n_periods + 1)
|
160 |
+
|
161 |
+
signal = np.sin(np.arange(n_samples, dtype=np.float32) / samples_per_cycle * 2 * np.pi) * gaussian(n_samples, (n_samples - 1) / 6).astype(np.single)
|
162 |
+
|
163 |
+
return signal
|
164 |
+
|
165 |
+
|
166 |
+
if __name__ == "__main__":
|
167 |
+
parser = argparse.ArgumentParser()
|
168 |
+
parser.add_argument('input', help='reference file')
|
169 |
+
parser.add_argument('--device', '-d', type=int, default=0, help='cuda device to use')
|
170 |
+
parser.add_argument('--frequencies', '-f', type=float, nargs='+', default=[5e6, 2.5e6],
|
171 |
+
help='at which frequencies to run the simulation')
|
172 |
+
parser.add_argument('--offsets', '-o', type=int, nargs='+', default=[-32, -24, -16, -8, 0, 8, 16, 24, 32],
|
173 |
+
help='list of (signed) offsets to use with respect to center, in elements. Angles will be calculated from offsets')
|
174 |
+
parser.add_argument('--n_periods', '-p', type=float, default=5.0,
|
175 |
+
help='how many periods to use for the pulse')
|
176 |
+
parser.add_argument('--ref_offset', '-r', type=int, default=59 * 1152 + (1152 - (128 + 127) * 4) // 2 + 32 * 2 * 4,
|
177 |
+
help='reference offset in entries (where to start offset 0) - default: line 60, element 33, remember the kerf')
|
178 |
+
parser.add_argument('--ref_speed', '-s', type=float, default=1540,
|
179 |
+
help='reference speed under which to computer angle')
|
180 |
+
|
181 |
+
args = parser.parse_args()
|
182 |
+
|
183 |
+
hash = f'{random.getrandbits(128):x}'
|
184 |
+
|
185 |
+
tmp_input = f'tmp_input_{hash}.h5'
|
186 |
+
tmp_output = f'tmp_output_{hash}.h5'
|
187 |
+
sim_result = f'sim_result_{hash}.h5'
|
188 |
+
|
189 |
+
# costs an additional copy but solves problem of opening file multiple times in parallel
|
190 |
+
shutil.copyfile(args.input, tmp_input)
|
191 |
+
|
192 |
+
try:
|
193 |
+
with h5py.File(tmp_input, 'r+') as f_i:
|
194 |
+
shape = np.squeeze(f_i['c0'][()]).shape
|
195 |
+
dt = f_i['dt'][0,0,0]
|
196 |
+
dx = f_i['dx'][0,0,0]
|
197 |
+
|
198 |
+
c0, alpha_coeff = generate_sound_speed(shape)
|
199 |
+
rho0, rho0_sgx, rho0_sgy = generate_density(shape)
|
200 |
+
|
201 |
+
f_i['c0'][...] = c0
|
202 |
+
f_i['alpha_coeff'][...] = alpha_coeff
|
203 |
+
|
204 |
+
f_i['rho0'][...] = rho0
|
205 |
+
f_i['rho0_sgx'][...] = rho0_sgx
|
206 |
+
f_i['rho0_sgy'][...] = rho0_sgy
|
207 |
+
|
208 |
+
# # Also record receive traces
|
209 |
+
# smi = np.tile(np.array(f_i['sensor_mask_index']), (1,1,2))
|
210 |
+
# smi[...,smi.shape[-1]//2:] += 1152 * (1152 - 60)
|
211 |
+
# del f_i['sensor_mask_index']
|
212 |
+
# f_i["sensor_mask_index"] = smi
|
213 |
+
# f_i["sensor_mask_index"].attrs["data_type"] = np.string_(b"long")
|
214 |
+
# f_i["sensor_mask_index"].attrs["domain_type"] = np.string_(b"real")
|
215 |
+
|
216 |
+
with h5py.File(sim_result, 'w') as f_o:
|
217 |
+
f_o.create_dataset(f'c0', data=c0, compression="gzip", compression_opts=9)
|
218 |
+
f_o.create_dataset(f'alpha_coeff', data=alpha_coeff, compression="gzip", compression_opts=9)
|
219 |
+
f_o.create_dataset(f'f', data=np.array(args.frequencies))
|
220 |
+
f_o.create_dataset(f'offsets', data=np.array(args.offsets))
|
221 |
+
|
222 |
+
for frequency, offset in product(args.frequencies, args.offsets):
|
223 |
+
# TODO: deal with offsets / angles
|
224 |
+
w = 127 * 4
|
225 |
+
o = offset * 8
|
226 |
+
dn = w * o / np.sqrt(64 * o ** 2 + 4 * w ** 2) * (dx / dt) / args.ref_speed
|
227 |
+
|
228 |
+
pulse = generate_pulse(center_freq=frequency, n_periods=args.n_periods, sampling_freq=1.0/dt) * 1e-7
|
229 |
+
|
230 |
+
signal = np.zeros((1, int(np.ceil(np.abs(dn))) + len(pulse), 127 * 4), dtype=np.single)
|
231 |
+
for i in range(127 * 4):
|
232 |
+
signal[0, int(np.round(-dn * i / 507 if o < 0 else dn - dn * i / 507)) + np.arange(len(pulse)), i] = pulse
|
233 |
+
|
234 |
+
with h5py.File(tmp_input, 'r+') as f_i:
|
235 |
+
del f_i["u_source_index"]
|
236 |
+
f_i["u_source_index"] = (np.arange((64 + 63) * 4, dtype=np.uint64) + args.ref_offset + offset * 8).reshape(1, 1, -1).astype(np.uint64)
|
237 |
+
f_i["u_source_index"].attrs["data_type"] = np.string_(b"long")
|
238 |
+
f_i["u_source_index"].attrs["domain_type"] = np.string_(b"real")
|
239 |
+
|
240 |
+
del f_i["uy_source_input"]
|
241 |
+
f_i["uy_source_input"] = signal
|
242 |
+
f_i["uy_source_input"].attrs["data_type"] = np.string_(b"float")
|
243 |
+
f_i["uy_source_input"].attrs["domain_type"] = np.string_(b"real")
|
244 |
+
|
245 |
+
f_i["uy_source_flag"][:] = signal.shape[1]
|
246 |
+
|
247 |
+
os.system(f'../../scripts/kspaceFirstOrder-CUDA -r 20 -t 8 -g {args.device} -i {tmp_input} -o {tmp_output}')
|
248 |
+
|
249 |
+
with h5py.File(tmp_output, 'r+') as f_t:
|
250 |
+
p = np.squeeze(f_t['p'][()]).T
|
251 |
+
|
252 |
+
p = 0.35 * (p[0::4,:] + p[3::4,:]) + 0.95 * (p[1::4,:] + p[2::4,:])
|
253 |
+
T = (p.shape[1] - 1) * dt
|
254 |
+
|
255 |
+
x_orig = np.linspace(0,T,p.shape[1])
|
256 |
+
x_out = np.arange(0,T,1/40e6)
|
257 |
+
|
258 |
+
p_out = si.interp1d(x_orig, p, kind='linear', copy=False, bounds_error=False, fill_value=0, assume_sorted=True)(x_out).astype(np.float32)
|
259 |
+
|
260 |
+
with h5py.File(sim_result, 'r+') as f_o:
|
261 |
+
f_o.create_dataset(f'p_f{frequency/1e6}_o{offset}', data=p_out, compression="gzip", compression_opts=9)
|
262 |
+
except Exception as e:
|
263 |
+
print(f"Error : {e.args[0]}")
|
264 |
+
os.system(f'rm {sim_result}')
|
265 |
+
|
266 |
+
try:
|
267 |
+
os.system(f'rm {tmp_input}')
|
268 |
+
except Exception as e:
|
269 |
+
print(f"Error removing {tmp_input} : {e.args[0]}")
|
270 |
+
|
271 |
+
try:
|
272 |
+
os.system(f'rm {tmp_output}')
|
273 |
+
except Exception as e:
|
274 |
+
print(f"Error removing {tmp_output} : {e.args[0]}")
|
code/reference_file.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:32fdd3e2950b5a13b02d6e98c2e9a55d229df362bb7a6822f9da7f0e2dcae65e
|
3 |
+
size 31711238
|