src.acoustools.SphericalHarmonics.SphericalHarmonics
1import scipy, torch, math 2import acoustools.Constants as c 3from acoustools.Utilities import DTYPE, device, forward_model_batched 4 5from acoustools.SphericalHarmonics.Bessel import spherical_bessel_h1,spherical_bessel_h1_grad, spherical_bessel_j, spherical_bessel_j_grad 6 7from line_profiler import profile 8 9_CACHE_Pmn = {} 10 11 12def convert_to_spherical(points, origin): 13 vecs = origin-points 14 15 distance = torch.norm(vecs, dim=1, p=2) 16 17 vecs_x = vecs[:,0] 18 vecs_y = vecs[:,1] 19 vecs_z = vecs[:,2] 20 21 theta = torch.acos(vecs_z / distance).to(DTYPE) 22 phi = torch.atan2(vecs_y.real, vecs_x.real).to(DTYPE) 23 24 theta[theta.isnan()] = -1*c.pi/2 25 26 # print(theta.real.min(), theta.real.max()) 27 # print(phi.real.min(), phi.real.max()) 28 # print() 29 30 31 # xy_distance = torch.sqrt(vecs_y**2 + vecs_x**2) 32 # phi = torch.sgn(vecs_y.real) * torch.arccos(vecs_x / xy_distance) 33 34 # print(distance, theta, phi) 35 36 return distance, theta, phi 37 38 39@profile 40def associated_legendre_polynomial(z,n,m): 41 ''' 42 For now uses SciPy - Later come back and write this in torch so Autograd works. 43 ''' 44 45 hash_z = hash(str(torch.hash_tensor(z.real).item()) + str(z.shape)) 46 # hash_z = hash(tuple(z.reshape(-1).real.tolist())) 47 # print(hash_z) 48 49 if (n,m,hash_z) in _CACHE_Pmn: 50 P = _CACHE_Pmn[(n,m,hash_z)] 51 return P 52 else: 53 # print(( (n-1,m,hash_z) in _CACHE_Pmn), ((n-2,m,hash_z) in _CACHE_Pmn)) 54 if( (n-1,m,hash_z) in _CACHE_Pmn) and ((n-2,m,hash_z) in _CACHE_Pmn): 55 Pn1 = _CACHE_Pmn[(n-1,m,hash_z)] 56 Pn2 = _CACHE_Pmn[(n-2,m,hash_z)] 57 58 Pn1 = z*(2*n - 1) * Pn1 59 Pn2 = (n + m - 1) * Pn2 60 61 P = (Pn1 - Pn2) / (n-m) 62 else: 63 P = scipy.special.assoc_legendre_p(n,m,z) 64 P = torch.tensor(P, device=device, dtype=DTYPE) 65 66 _CACHE_Pmn[(n,m,hash_z)] = P 67 return P 68 69@profile 70def spherical_harmonic_Y(theta, phi,n,m): 71 72 cos_theta = torch.cos(theta) 73 P_n_m = associated_legendre_polynomial(cos_theta, n, m) 74 exp = torch.exp(1j * m * phi) 75 # exit() 76 77 factorial_minus = math.factorial(n - m) 78 factorial_plus = math.factorial(n + m) 79 80 constant = math.sqrt(((2*n+1) / (4*c.pi)) * (factorial_minus / factorial_plus) ) 81 82 return constant * P_n_m * exp 83 84@profile 85def transducer_expansion_coef_Snm(activations, board, centre_of_mass, n, m, transducer_norms = None, k=c.k, transducer_radius=c.radius, sum_j=True, p_ref = c.P_ref, directivity=None, distance=None, theta=None, phi=None): 86 87 if directivity is None: 88 directivity = forward_model_batched(centre_of_mass, board, p_ref=1, norms = transducer_norms, transducer_radius= transducer_radius, k=k, only_directivity=True) * p_ref 89 90 if distance is None and theta is None and phi is None: 91 distance, theta, phi = convert_to_spherical(board.unsqueeze(0).permute(0,2,1), centre_of_mass) 92 93 Y = spherical_harmonic_Y(theta, phi, n, m).permute(0,2,1) 94 # print(scipy.special.sph_harm_y(n,m,theta.real, phi.real).squeeze() / Y.squeeze()) 95 # print(Y.shape) 96 97 h = spherical_bessel_h1(k * distance, n).unsqueeze(2) 98 99 # hnm = lambda x, n: scipy.special.spherical_jn(n,x) + 1j * scipy.special.spherical_yn(n,x) 100 # print(h.squeeze() / hnm(k*distance, n).squeeze()) 101 102 S_jnm = activations * 4 * c.pi * 1j * k * h * Y.conj() * directivity 103 104 105 if sum_j: return torch.sum(S_jnm, dim=1) 106 return S_jnm 107 108def mie_coef(n,particle_radius, medium_density=c.p_0, medium_speed=c.c_0, particle_density=c.p_p, particle_speed=c.c_p, k=c.k): 109 110 ka= k * particle_radius 111 ka_p = (medium_speed / particle_speed) * ka 112 113 jn = spherical_bessel_j(ka, n) 114 jn_grad = spherical_bessel_j_grad(ka, n) 115 116 hn = spherical_bessel_h1(ka, n) 117 # print((scipy.special.spherical_jn(n, ka) + 1j * scipy.special.spherical_yn(n, ka)) / hn) 118 hn_grad = spherical_bessel_h1_grad(ka, n) 119 120 jn_p = spherical_bessel_j(ka_p, n) 121 jn_grad_p = spherical_bessel_j_grad(ka_p, n) 122 123 Z = (particle_density * particle_speed) / (medium_density * medium_speed) 124 # Z = 1 125 126 127 cn = ((jn*jn_grad_p) - Z * (jn_grad * jn_p)) / (hn*jn_grad_p - Z * (hn_grad * jn_p)) 128 # 129 cn = cn * -1 130 131 return cn 132 133
def
convert_to_spherical(points, origin):
14def convert_to_spherical(points, origin): 15 vecs = origin-points 16 17 distance = torch.norm(vecs, dim=1, p=2) 18 19 vecs_x = vecs[:,0] 20 vecs_y = vecs[:,1] 21 vecs_z = vecs[:,2] 22 23 theta = torch.acos(vecs_z / distance).to(DTYPE) 24 phi = torch.atan2(vecs_y.real, vecs_x.real).to(DTYPE) 25 26 theta[theta.isnan()] = -1*c.pi/2 27 28 # print(theta.real.min(), theta.real.max()) 29 # print(phi.real.min(), phi.real.max()) 30 # print() 31 32 33 # xy_distance = torch.sqrt(vecs_y**2 + vecs_x**2) 34 # phi = torch.sgn(vecs_y.real) * torch.arccos(vecs_x / xy_distance) 35 36 # print(distance, theta, phi) 37 38 return distance, theta, phi
@profile
def
associated_legendre_polynomial(z, n, m):
41@profile 42def associated_legendre_polynomial(z,n,m): 43 ''' 44 For now uses SciPy - Later come back and write this in torch so Autograd works. 45 ''' 46 47 hash_z = hash(str(torch.hash_tensor(z.real).item()) + str(z.shape)) 48 # hash_z = hash(tuple(z.reshape(-1).real.tolist())) 49 # print(hash_z) 50 51 if (n,m,hash_z) in _CACHE_Pmn: 52 P = _CACHE_Pmn[(n,m,hash_z)] 53 return P 54 else: 55 # print(( (n-1,m,hash_z) in _CACHE_Pmn), ((n-2,m,hash_z) in _CACHE_Pmn)) 56 if( (n-1,m,hash_z) in _CACHE_Pmn) and ((n-2,m,hash_z) in _CACHE_Pmn): 57 Pn1 = _CACHE_Pmn[(n-1,m,hash_z)] 58 Pn2 = _CACHE_Pmn[(n-2,m,hash_z)] 59 60 Pn1 = z*(2*n - 1) * Pn1 61 Pn2 = (n + m - 1) * Pn2 62 63 P = (Pn1 - Pn2) / (n-m) 64 else: 65 P = scipy.special.assoc_legendre_p(n,m,z) 66 P = torch.tensor(P, device=device, dtype=DTYPE) 67 68 _CACHE_Pmn[(n,m,hash_z)] = P 69 return P
For now uses SciPy - Later come back and write this in torch so Autograd works.
@profile
def
spherical_harmonic_Y(theta, phi, n, m):
71@profile 72def spherical_harmonic_Y(theta, phi,n,m): 73 74 cos_theta = torch.cos(theta) 75 P_n_m = associated_legendre_polynomial(cos_theta, n, m) 76 exp = torch.exp(1j * m * phi) 77 # exit() 78 79 factorial_minus = math.factorial(n - m) 80 factorial_plus = math.factorial(n + m) 81 82 constant = math.sqrt(((2*n+1) / (4*c.pi)) * (factorial_minus / factorial_plus) ) 83 84 return constant * P_n_m * exp
@profile
def
transducer_expansion_coef_Snm( activations, board, centre_of_mass, n, m, transducer_norms=None, k=732.7329804081634, transducer_radius=0.0045, sum_j=True, p_ref=3.4000000000000004, directivity=None, distance=None, theta=None, phi=None):
86@profile 87def transducer_expansion_coef_Snm(activations, board, centre_of_mass, n, m, transducer_norms = None, k=c.k, transducer_radius=c.radius, sum_j=True, p_ref = c.P_ref, directivity=None, distance=None, theta=None, phi=None): 88 89 if directivity is None: 90 directivity = forward_model_batched(centre_of_mass, board, p_ref=1, norms = transducer_norms, transducer_radius= transducer_radius, k=k, only_directivity=True) * p_ref 91 92 if distance is None and theta is None and phi is None: 93 distance, theta, phi = convert_to_spherical(board.unsqueeze(0).permute(0,2,1), centre_of_mass) 94 95 Y = spherical_harmonic_Y(theta, phi, n, m).permute(0,2,1) 96 # print(scipy.special.sph_harm_y(n,m,theta.real, phi.real).squeeze() / Y.squeeze()) 97 # print(Y.shape) 98 99 h = spherical_bessel_h1(k * distance, n).unsqueeze(2) 100 101 # hnm = lambda x, n: scipy.special.spherical_jn(n,x) + 1j * scipy.special.spherical_yn(n,x) 102 # print(h.squeeze() / hnm(k*distance, n).squeeze()) 103 104 S_jnm = activations * 4 * c.pi * 1j * k * h * Y.conj() * directivity 105 106 107 if sum_j: return torch.sum(S_jnm, dim=1) 108 return S_jnm
def
mie_coef( n, particle_radius, medium_density=1.2, medium_speed=343, particle_density=29.36, particle_speed=1052, k=732.7329804081634):
110def mie_coef(n,particle_radius, medium_density=c.p_0, medium_speed=c.c_0, particle_density=c.p_p, particle_speed=c.c_p, k=c.k): 111 112 ka= k * particle_radius 113 ka_p = (medium_speed / particle_speed) * ka 114 115 jn = spherical_bessel_j(ka, n) 116 jn_grad = spherical_bessel_j_grad(ka, n) 117 118 hn = spherical_bessel_h1(ka, n) 119 # print((scipy.special.spherical_jn(n, ka) + 1j * scipy.special.spherical_yn(n, ka)) / hn) 120 hn_grad = spherical_bessel_h1_grad(ka, n) 121 122 jn_p = spherical_bessel_j(ka_p, n) 123 jn_grad_p = spherical_bessel_j_grad(ka_p, n) 124 125 Z = (particle_density * particle_speed) / (medium_density * medium_speed) 126 # Z = 1 127 128 129 cn = ((jn*jn_grad_p) - Z * (jn_grad * jn_p)) / (hn*jn_grad_p - Z * (hn_grad * jn_p)) 130 # 131 cn = cn * -1 132 133 return cn