def test_spherical_to_cartesian_single_3D_points(self): x_convert = sp.spherical_to_cartesian(x_s) y_convert = sp.spherical_to_cartesian(y_s) z_convert = sp.spherical_to_cartesian(z_s) minus_x_convert = sp.spherical_to_cartesian(minus_x_s) # Test Float Equality to 7 Decimal Places for each Component for xc, xe in zip(x_convert, x_c): self.assertAlmostEqual(xc, xe) for yc, ye in zip(y_convert, y_c): self.assertAlmostEqual(yc, ye) for zc, ze in zip(z_convert, z_c): self.assertAlmostEqual(zc, ze) for xc, xe in zip(minus_x_convert, minus_x_c): self.assertAlmostEqual(xc, xe)
def update(ff): ax.clear() format_3d_axes(ax, font_kwargs=font_kwargs, **ax_kwargs) for fgs, level, tri_kwarg, cont_kwarg in zip(function_grid_series, levels, tri_kwargs, cont_kwargs): func = fgs[ff][0].data r, theta, phi = fgs[ff][0].grid.coordinates # Calculate and Plot Isosurface # Factor of 2 for consistency with bloch sphere convention del_r, del_theta, del_phi = 2 * (r[1] - r[0]), (theta[1] - theta[0]), (phi[1] - phi[0]) r_min, theta_min, phi_min = 2 * np.min(r), np.min(theta), np.min(phi) if len(level) == 1: lvl = level[0] else: lvl = level[ff] if normalize_level: lvl = lvl * np.max(func) verts, faces = measure.marching_cubes_classic(volume=func, level=lvl) v = [] v.append(verts[:, 0] * del_r + r_min) v.append(verts[:, 1] * del_theta + theta_min) v.append(verts[:, 2] * del_phi + phi_min) v = sp.spherical_to_cartesian(tuple(v)) verts[:, 0] = v[0] verts[:, 1] = v[1] verts[:, 2] = v[2] tri = ax.plot_trisurf(verts[:, 0], verts[:, 1], faces, verts[:, 2], zorder=100., **tri_kwarg) return tri,
def test_spherical_to_cartesian_one_4D_complex_point(self): x_iy_spherical = (1. + 1.j, 1. + 1.j, pi / 2 + 1.j * pi / 2, 1.j * pi / 2) x_iy_convert = sp.spherical_to_cartesian(x_iy_spherical) x_iy_cart = (1. + 1.j, 1., 1.j, 0.) self.assertTrue( np.array_equal(np.round(x_iy_convert, 7), np.round(x_iy_cart, 7)))
def test_spherical_grid_init_equal_length_inputs_cartesian_equality(self): # Initialize Grids with equal length coordinate arrays grid = gr.SphericalGrid((r, t, p)) log_grid = gr.SphericalGrid((r_log, t_log, p_log)) # Define Expected Behavior expected_cart = sp.spherical_to_cartesian(grid.mesh) expected_cart = np.round(expected_cart, 7) expected_log_cart = sp.spherical_to_cartesian(log_grid.mesh) expected_log_cart = np.round(expected_log_cart, 7) cart_equality = np.array_equal(np.round(grid.cartesian, 7), expected_cart) log_cart_equality = np.array_equal(np.round(log_grid.cartesian, 7), expected_log_cart) self.assertTrue(cart_equality) self.assertTrue(log_cart_equality)
def test_spherical_grid_iter_lists(self): grid = gr.SphericalGrid([r, t, p]) xm, ym, zm = sp.spherical_to_cartesian( np.meshgrid(r, t, p, indexing='ij')) x_expected = xm.flatten() y_expected = ym.flatten() z_expected = zm.flatten() for c, xe, ye, ze in zip(grid, x_expected, y_expected, z_expected): self.assertEqual(c, [xe, ye, ze])
def test_spherical_to_cartesian_one_4D_point(self): x_spherical = (1., 1., pi / 2, 0.) y_spherical = (1., 1., pi / 2, pi / 2) z_spherical = (1., 1., 0., 0.) x_convert = sp.spherical_to_cartesian(x_spherical) y_convert = sp.spherical_to_cartesian(y_spherical) z_convert = sp.spherical_to_cartesian(z_spherical) x_cart = (1., 1., 0., 0.) y_cart = (1., 0., 1., 0.) z_cart = (1., 0., 0., 1.) for xconv, xcart in zip(x_convert, x_cart): self.assertAlmostEqual(xconv, xcart) for yconv, ycart in zip(y_convert, y_cart): self.assertAlmostEqual(yconv, ycart) for zconv, zcart in zip(z_convert, z_cart): self.assertAlmostEqual(zconv, zcart)
def test_spherical_grid_init_unequal_length_inputs_cartesian_equality( self): # Initialize Grids with equal length coordinate arrays mixed_grid = gr.SphericalGrid((r, t_short, p_short)) # Define Expected Behavior expected_mixed_cart = sp.spherical_to_cartesian(mixed_grid.mesh) expected_mixed_cart = np.round(expected_mixed_cart, 7) # Check Equality mixed_cart_eq = np.array_equal(np.round(mixed_grid.cartesian, 7), expected_mixed_cart) self.assertTrue(mixed_cart_eq)
def plot_spherical_isosurface(function_grids, levels, tri_kwargs=[{'cmap': 'Spectral', 'lw': 1}], fig_kwargs={'figsize': [12., 15.]}, ax_kwargs={}, font_kwargs={'name': 'serif', 'size': 30}, show_plot=True, fname=None, fig_num=None): # Repeat axis and line formats if only one value is given if len(tri_kwargs) == 1: tri_kwargs = tri_kwargs * len(function_grids) if len(levels) == 1: levels = levels * len(function_grids) # Checks that all inputs are now the same length assert len(function_grids) == len(tri_kwargs) == len(levels) # Format Figure and Axes fig = plt.figure(fig_num, **fig_kwargs) ax = fig.add_axes([0.025, 0.15, 0.8, 0.85], projection='3d') # Format Axes format_3d_axes(ax, font_kwargs=font_kwargs, **ax_kwargs) for fg, level, tri_kwarg in zip(function_grids, levels, tri_kwargs): func = fg.data r, theta, phi = fg.grid.coordinates # Calculate and Plot Isosurface # Factor of 2 for consistency with bloch sphere convention del_r, del_theta, del_phi = 2 * (r[1] - r[0]), (theta[1] - theta[0]), (phi[1] - phi[0]) r_min, theta_min, phi_min = 2 * np.min(r), np.min(theta), np.min(phi) verts, faces = measure.marching_cubes_classic(volume=func, level=level) v = [] v.append(verts[:, 0] * del_r + r_min) v.append(verts[:, 1] * del_theta + theta_min) v.append(verts[:, 2] * del_phi + phi_min) v = sp.spherical_to_cartesian(tuple(v)) verts[:, 0] = v[0] verts[:, 1] = v[1] verts[:, 2] = v[2] tri = ax.plot_trisurf(verts[:, 0], verts[:, 1], faces, verts[:, 2], zorder=100., **tri_kwarg) # Draw and Save Figure if show_plot: plt.show() if fname is not None: fig.savefig(fname) return fig, tri
def test_spherical_to_cartesian_3D_complex_array(self): r_array = (1. + 1.j, 0.5, 0.0) theta_array = (0.0, pi / 2 + 1.j * pi / 2) phi_array = (0.0, pi / 2 + 1.j * pi / 2) spherical_grid = np.meshgrid(r_array, theta_array, phi_array, indexing='ij') x_conv, y_conv, z_conv = sp.spherical_to_cartesian(spherical_grid) x_grid = np.array([[[0., 0.], [1. + 1.j, 0.]], [[0., 0.], [0.5, 0.]], [[0., 0.], [0., 0.]]]) y_grid = np.array([[[0., 0.], [0., 1. + 1.j]], [[0., 0.], [0., 0.5]], [[0., 0.], [0., 0.]]]) z_grid = np.array([[[1. + 1.j, 1. + 1.j], [0., 0.]], [[0.5, 0.5], [0., 0.]], [[0., 0.], [0., 0.]]]) self.assertTrue(np.array_equal(np.round(x_conv, 7), x_grid)) self.assertTrue(np.array_equal(np.round(y_conv, 7), y_grid)) self.assertTrue(np.array_equal(np.round(z_conv, 7), z_grid))
def convert_cartesian(self): return spherical_to_cartesian(self.vector)
def test_spherical_grid_getitem_unequal_length_arrays(self): grid = gr.SphericalGrid((r_short, t, p)) expected_000 = sp.spherical_to_cartesian((r_short[0], t[0], p[0])) expected_210 = sp.spherical_to_cartesian((r_short[2], t[1], p[0])) self.assertEqual(grid[0, 0, 0], expected_000) self.assertEqual(grid[2, 1, 0], expected_210)
def _calculate_cartesian_mesh(mesh): return sp.spherical_to_cartesian(mesh)