def test_rotate_gsm_vector(self): time = 20 nmax = 1 kmax = 1 radius = R_REF theta_geo = 90/6 phi_geo = 90/8 g_gsm = np.array([1, 2, -1]) base_1, base_2, base_3 = c.basevectors_gsm(time) matrix = c.rotate_gauss(nmax, kmax, base_1, base_2, base_3) g_geo = np.matmul(matrix, g_gsm) B_geo_1 = m.synth_values(g_geo, radius, theta_geo, phi_geo) B_geo_1 = np.array(B_geo_1) theta_gsm, phi_gsm, R = c.matrix_geo_to_base(theta_geo, phi_geo, base_1, base_2, base_3) B_gsm = m.synth_values(g_gsm, radius, theta_gsm, phi_gsm) B_gsm = np.array(B_gsm) B_geo_2 = np.matmul(R.transpose(), B_gsm) self.assertIsNone(np.testing.assert_allclose( B_geo_1, B_geo_2, rtol=1e-5))
def test_synth_values_mmax(self): theta = 79. phi = 13. radius = R_REF # function for quick testing with "true" solution self.assertIsNone( np.testing.assert_allclose( m.synth_values([1., 2., 3., 4., 5., 6., 7., 8.], radius, theta, phi), m.synth_values([1., 2., 3., 4., 5., 6., 7., 8.], radius, theta, phi, nmax=None, mmax=None))) self.assertIsNone( np.testing.assert_allclose( m.synth_values([1., 2., 3., 0., 0., 0., 0., 0.], radius, theta, phi), m.synth_values([1., 2., 3., 4., 5., 6., 7., 8.], radius, theta, phi, nmax=1, mmax=None))) self.assertIsNone( np.testing.assert_allclose( m.synth_values([1., 2., 3., 4., 5., 6., 0., 0.], radius, theta, phi), m.synth_values([1., 2., 3., 4., 5., 6.], radius, theta, phi, nmax=2, mmax=1))) self.assertIsNone( np.testing.assert_allclose( m.synth_values([1., 0., 0., 0., 0., 0., 0., 0.], radius, theta, phi), m.synth_values([1.], radius, theta, phi, nmax=1, mmax=0))) self.assertIsNone( np.testing.assert_allclose( m.synth_values([1., 2., 3., 4., 5., 6., 0., 0.], radius, theta, phi), m.synth_values([1., 2., 3., 4., 5., 6.], radius, theta, phi, nmax=None, mmax=1)))
def test_synth_values_poles(self): radius = 6371.2 theta = np.array([0., 180.]) phi = np.linspace(0., 360., num=10, endpoint=False) # dipole aligned with x-axis, B = -grad(V) # North pole: Bx = -1, Bt = -1 # South pole: Bx = -1, Bt = 1 Br, Bt, Bp = m.synth_values([0., 1., 0], radius, theta, phi, grid=True) # north pole (theta, phi) = (0., 0.) npole = (0, 0) np.testing.assert_allclose(Br[npole], 0.) np.testing.assert_allclose(Bt[npole], -1.) np.testing.assert_allclose(Bp[npole], 0.) Bx = (np.cos(np.radians(0.)) * np.cos(np.radians(phi)) * Bt[0, :] - np.sin(np.radians(phi)) * Bp[0, :]) np.testing.assert_allclose(Bx, -np.ones((10, ))) # south pole (theta, phi) = (180., 0.) spole = (1, 0) np.testing.assert_allclose(Br[spole], 0.) np.testing.assert_allclose(Bt[spole], 1.) np.testing.assert_allclose(Bp[spole], 0.) Bx = (np.cos(np.radians(180.)) * np.cos(np.radians(phi)) * Bt[1, :] - np.sin(np.radians(phi)) * Bp[1, :]) np.testing.assert_allclose(Bx, -np.ones((10, )))
def test_synth_values_nmin(self): theta = 79. phi = 13. radius = R_REF theta_ls = theta * np.ones((15, )) phi_ls = phi * np.ones((27, )) phi_grid, theta_grid = np.meshgrid(phi_ls, theta_ls) # 2-D radius_grid = R_REF * np.ones(phi_grid.shape) # 2-D coeffs_grid = np.random.random() * np.ones(phi_grid.shape + (24, )) # 3-D coeffs_grid_min = np.copy(coeffs_grid) coeffs_grid_min[..., :8] = 0. # nmax=4 # exhaustive inputs B = m.synth_values(coeffs_grid_min, radius_grid, theta_grid, phi_grid) # function for quick testing with "true" solution def test(field): self.assertIsNone(np.testing.assert_allclose(B, field)) test( m.synth_values(coeffs_grid[..., 8:], radius, theta_grid, phi_grid, nmin=3, nmax=4)) test( m.synth_values(coeffs_grid_min[..., 8:], radius, theta_grid, phi_grid, nmin=3, nmax=4))
def test_synth_values_grid(self): radius = R_REF theta = np.linspace(0., 180., num=4000) phi = np.linspace(-180., 180., num=3000) phi_grid, theta_grid = np.meshgrid(phi, theta) coeffs = np.random.random((24, )) s = timer() B_grid = m.synth_values(coeffs, radius, theta_grid, phi_grid, grid=False) e = timer() print(" Time for 'grid=False' computation: ", e - s) s = timer() B_grid2 = m.synth_values(coeffs, radius, theta[..., None], phi[None, ...], grid=False) e = timer() print(" Time for broadcasted 'grid=False' computation: ", e - s) s = timer() B = m.synth_values(coeffs, radius, theta, phi, grid=True) e = timer() print(" Time for 'grid=True' computation: ", e - s) for comp, comp_grid in zip(B_grid, B_grid2): np.testing.assert_allclose(comp, comp_grid) for comp, comp_grid in zip(B_grid2, B): np.testing.assert_allclose(comp, comp_grid)
def test_synth_values_inputs(self): theta = 79. phi = 13. radius = R_REF theta_ls = theta * np.ones((15, )) phi_ls = phi * np.ones((27, )) phi_grid, theta_grid = np.meshgrid(phi_ls, theta_ls) # 2-D radius_grid = R_REF * np.ones(phi_grid.shape) # 2-D coeffs_grid = np.random.random() * np.ones(phi_grid.shape + (24, )) # 3-D # exhaustive inputs B = m.synth_values(coeffs_grid, radius_grid, theta_grid, phi_grid) # function for quick testing with "true" solution def test(field): self.assertIsNone(np.testing.assert_allclose(B, field)) # one input () dimension: float test(m.synth_values(coeffs_grid, radius, theta_grid, phi_grid)) test(m.synth_values(coeffs_grid, radius_grid, theta, phi_grid)) test(m.synth_values(coeffs_grid, radius_grid, theta_grid, phi)) # one input (1,) dimension: one element array test( m.synth_values(coeffs_grid[0, 0], radius_grid, theta_grid, phi_grid)) test( m.synth_values(coeffs_grid, radius_grid[0, 0], theta_grid, phi_grid)) test( m.synth_values(coeffs_grid, radius_grid, theta_grid[0, 0], phi_grid)) test( m.synth_values(coeffs_grid, radius_grid, theta_grid, phi_grid[0, 0])) # GRID MODE test( m.synth_values(coeffs_grid, radius_grid, theta_ls, phi_ls, grid=True)) test(m.synth_values(coeffs_grid, radius, theta_ls, phi_ls, grid=True)) test( m.synth_values(coeffs_grid[0, 0], radius_grid, theta_ls, phi_ls, grid=True)) test( m.synth_values(coeffs_grid[0, 0], radius_grid[0, 0], theta_ls, phi_ls, grid=True)) # test multi-dimensional grid # full grid coeffs_grid = np.random.random((24, )) radius_grid = radius * np.ones((2, 5, 6, 4)) phi_grid = phi * np.ones((2, 5, 6, 4)) theta_grid = theta * np.ones((2, 5, 6, 4)) B = m.synth_values(coeffs_grid, radius_grid, theta_grid, phi_grid) # reduced grid radius_grid2 = radius * np.ones((2, 1, 6, 4)) phi_grid2 = phi * np.ones((2, 5, 1, 1)) theta_grid2 = theta * np.ones((1, 5, 6, 4)) test(m.synth_values(coeffs_grid, radius_grid2, theta_grid2, phi_grid2))