def test_rotated_spheroid_equals_rotated_light(material, atol): """cross-sections are the same if a spheroid is rotated or if the light is rotated""" theta = 1.3 phi = 0.7 source = miepy.sources.plane_wave([1,0], theta=theta, phi=phi) z_oriented = miepy.cluster(particles=miepy.spheroid([0,0,0], radius, 2*radius, material), source=source, wavelength=wavelength, lmax=lmax, medium=medium) C1 = z_oriented.cross_sections() source = miepy.sources.plane_wave([1,0]) q = miepy.quaternion.from_spherical_coords(theta, phi).inverse() oriented = miepy.cluster(particles=miepy.spheroid([0,0,0], radius, 2*radius, material, orientation=q), source=source, wavelength=wavelength, lmax=lmax, medium=medium) C2 = oriented.cross_sections() assert np.allclose(C1, C2, rtol=0, atol=atol)
def test_ellipsoid_equal_spheroid_z_oriented(): """An (rx, ry, rz) ellipsoid should equal a z-oriented (rxy, rz) spheroid""" e = miepy.spheroid([0, 0, 0], rx, rz, material) T1 = e.compute_tmatrix(lmax, wavelength, 1.0) e = miepy.ellipsoid([0, 0, 0], rx, ry, rz, material) T2 = e.compute_tmatrix(lmax, wavelength, 1.0) assert np.allclose(T1, T2, atol=2e-5)
def test_tmatrix_spheroid_is_sphere(material, atol): """tmatrix of spheroid with aspect ratio 1 is equal to tmatrix of sphere""" sphere = miepy.sphere([0,0,0], radius, material) spheroid = miepy.spheroid([0,0,0], radius, radius, material, tmatrix_lmax=4) T1 = sphere.compute_tmatrix(lmax, wavelength, eps_b) T2 = spheroid.compute_tmatrix(lmax, wavelength, eps_b) print(np.max(np.abs(T1-T2))) assert np.allclose(T1, T2, rtol=0, atol=atol)
def test_ellipsoid_equal_spheroid_y_oriented(): """An (rx, rz, ry) ellipsoid should equal a y-oriented (rxy, rz) spheroid""" q = miepy.quaternion.from_spherical_coords(np.pi / 2, np.pi / 2) e = miepy.spheroid([0, 0, 0], rx, rz, material, orientation=q) T1 = e.compute_tmatrix(lmax, wavelength, 1.0) e = miepy.ellipsoid([0, 0, 0], rx, rz, ry, material) T2 = e.compute_tmatrix(lmax, wavelength, 1.0) assert np.allclose(T1, T2, atol=5e-5)
def tests(Nmax, step=1): Nparticles = np.arange(1, Nmax+1, step) t_force, t_flux, t_build, t_solve, t_expand, t_tmatrix = [np.zeros_like(Nparticles, dtype=float) for i in range(6)] for i,N in enumerate(Nparticles): print(N, Nmax) positions = [[n*separation, 0, 0] for n in range(N)] particles = [miepy.spheroid(pos, radius, 0.5*radius, Ag) for pos in positions] mie = miepy.cluster(particles=particles, source=source, wavelength=600*nm, lmax=2) t_force[i] = time_function(mie.force) t_flux[i] = time_function(mie.cross_sections) t_build[i] = time_function(partial(miepy.interactions.particle_aggregate_tmatrix, mie.position, mie.tmatrix, mie.material_data.k_b)) A = miepy.interactions.particle_aggregate_tmatrix(mie.position, mie.tmatrix, k=mie.material_data.k_b) t_solve[i] = time_function(partial(solve_linear_system, A, mie.p_src, method=miepy.solver.bicgstab)) t_tmatrix[i] = time_function(partial(tmatrix_time, mie)) x = np.linspace(0, N*separation, 1) y = 2*radius*np.ones_like(x) z = np.zeros_like(x) t_expand[i] = time_function(partial(mie.E_field, x, y, z)) fig, ax = plt.subplots() ax.plot(Nparticles, t_force, label='force') ax.plot(Nparticles, t_flux, label='flux') ax.plot(Nparticles, t_build, label='build') ax.plot(Nparticles, t_solve, label='solve') ax.plot(Nparticles, t_expand, label='expand') ax.plot(Nparticles, t_tmatrix, label='tmatrix') ax.legend() ax.set(xlabel='number of particles', ylabel='runtime (s)') plt.show()
import miepy S = miepy.spheroid([0, 0, 0], 1, 1, miepy.constant_material(2)) T = S.compute_tmatrix(2, 5, 1) print(T)
dr_phi = -rad**3 * np.sin(phi) * np.cos(phi) * np.sin(theta) * (1 / b**2 - 1 / a**2) rhat, that, phat = miepy.coordinates.sph_basis_vectors(theta, phi) sigma = (rhat - 1 / rad * dr_theta * that - 1 / rad * dr_phi * phat) * rad**2 * np.sin(theta) return rad, sigma rad, dS = ellipsoid_dS(1 * radius, 1.3 * radius, 1.6 * radius, THETA, PHI) T = get_tmatrix(rad, dS, eps, 1, wavelength, lmax) print(T[1, :, 0, 0]) T1 = np.copy(T) spheroid = miepy.spheroid([0, 0, 0], radius, 1.3 * radius, material) spheroid = miepy.ellipsoid([0, 0, 0], radius, 1.3 * radius, 1.6 * radius, material) T = spheroid.compute_tmatrix(lmax, wavelength, 1) print(T[1, :, 0, 0]) T2 = np.copy(T) import matplotlib.pyplot as plt rmax = lmax * (lmax + 2) T1 = np.reshape(T1, [2 * rmax, 2 * rmax]) T2 = np.reshape(T2, [2 * rmax, 2 * rmax]) y = np.abs(T1 - T2) y /= np.max(y) plt.pcolormesh(y, vmax=.1) plt.gca().set_aspect('equal')