Beispiel #1
0
 def test_cartesian_grid_eq_very_different_equal_length_grids_return_false(
         self):
     x2 = 2. * x
     assert not np.array_equal(x, x2)
     grid = gr.CartesianGrid((x, y, z))
     grid2 = gr.CartesianGrid((x2, y, z))
     self.assertNotEqual(grid, grid2)
Beispiel #2
0
 def test_cartesian_grid_eq_nearly_equal_length_grids_return_false(self):
     x2 = x + 1E-7 * np.ones_like(x)
     assert x2 is not x
     assert not np.array_equal(x, x2)
     grid = gr.CartesianGrid((x, y, z))
     grid2 = gr.CartesianGrid((x2, y, z))
     self.assertNotEqual(grid, grid2)
Beispiel #3
0
 def test_cartesian_grid_eq_equal_length_grids_return_true(self):
     x2 = 1. * x  # Use this so that x2 == x but is not x
     assert x2 is not x
     assert np.array_equal(x, x2)
     grid = gr.CartesianGrid((x, y, z))
     grid2 = gr.CartesianGrid((x2, y, z))
     self.assertEqual(grid, grid2)
Beispiel #4
0
 def test_cartesian_grid_gradient_container_type(self):
     xm, ym, zm = np.meshgrid(x, y, z, indexing='ij')
     tuple_grid = gr.CartesianGrid((x, y, z))
     list_grid = gr.CartesianGrid([x, y, z])
     tuple_grad = tuple_grid.gradient(xm)
     list_grad = list_grid.gradient(ym)
     self.assertIs(type(tuple_grad), tuple)
     self.assertIs(type(list_grad), list)
Beispiel #5
0
 def test_grid_getitem_override(self):
     x1 = (1., 2., 3.)
     x2 = (100, 200, 300)
     grid1d = gr.CartesianGrid((x1, ))
     grid2d = gr.CartesianGrid((x1, x2))
     self.assertEqual(grid1d[0], (1., ))
     self.assertEqual(grid1d[2], (3., ))
     self.assertEqual(grid2d[0, 0], (1., 100))
     self.assertEqual(grid2d[0, 1], (1., 200))
Beispiel #6
0
 def test_grid_iter_method(self):
     x1 = (1., 2., 3.)
     x1repeat = (1., 1., 1., 2., 2., 2., 3., 3., 3.)
     x2 = (100, 200, 300)
     x2repeat = (100, 200, 300, 100, 200, 300, 100, 200, 300)
     grid1d = gr.CartesianGrid((x1, ))
     grid2d = gr.CartesianGrid((x1, x2))
     for gt, xt in zip(grid1d, x1):
         self.assertEqual(gt, (xt, ))
     for g2, x1t, x2t in zip(grid2d, x1repeat, x2repeat):
         self.assertEqual(g2, (x1t, x2t))
Beispiel #7
0
 def test_grid_equality_override(self):
     x = (0., 1., 2.)
     x2 = (0., 1., 3.)
     x3 = (0., 2.)
     xprime = (0., 1., 4. / 2)
     y = (0., 5.)
     z = (1., 2., 3., 4.)
     grid1 = gr.CartesianGrid((x, y, z))
     grid1priime = gr.CartesianGrid((xprime, y, z))
     grid2 = gr.CartesianGrid((x2, y, z))
     grid3 = gr.CartesianGrid((x3, y, z))
     self.assertEqual(grid1, grid1priime)
     self.assertNotEqual(grid1, grid2)
     self.assertNotEqual(grid1, grid3)
     self.assertNotEqual(grid2, grid3)
Beispiel #8
0
 def test_spherical_grid_eq_very_different_equal_length_grids_return_false(
         self):
     r2 = 2. * r
     assert not np.array_equal(r, r2)
     grid = gr.SphericalGrid((r, t, p))
     grid2 = gr.CartesianGrid((r2, t, p))
     self.assertNotEqual(grid, grid2)
Beispiel #9
0
 def test_cartesian_grid_iter_lists(self):
     grid = gr.CartesianGrid([x, y, z])
     xm, ym, zm = np.meshgrid(x, y, z, 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])
Beispiel #10
0
 def test_grid_data_getitem_overload_2D_data(self):
     x1 = (1., 2.)
     x2 = (100, 200)
     grid2d = gr.CartesianGrid((x1, x2))
     data2d = (np.array([['a', 'b'], ['c', 'd']]), np.array([[1, 2], [3,
                                                                      4]]))
     g_d = gr.GridData(data2d, grid2d)
     self.assertEqual((('a', 1), (1., 100)), g_d[0, 0])
Beispiel #11
0
    def test_cartesian_grid_init_equal_length_inputs_mesh_equality(self):
        # Initialize Grids with equal length coordinate arrays
        short_grid = gr.CartesianGrid((x_short, y_short, z_short))
        long_grid = gr.CartesianGrid((x, y, z))

        # Define Expected Behavior
        expected_short_mesh = np.meshgrid(x_short,
                                          y_short,
                                          z_short,
                                          indexing='ij')
        expected_long_mesh = np.meshgrid(x, y, z, indexing='ij')

        # Check Equality
        short_mesh_eq = np.array_equal(short_grid.mesh, expected_short_mesh)
        long_mesh_eq = np.array_equal(long_grid.mesh, expected_long_mesh)
        self.assertTrue(short_mesh_eq)
        self.assertTrue(long_mesh_eq)
Beispiel #12
0
 def test_grid_data_rmul_1D_data(self):
     x1 = (1., 2.)
     x2 = (10, 20)
     grid = gr.CartesianGrid((x1, x2))
     d1 = np.array([[1., 2.], [3., 4.]])
     d_sum = 2 * d1
     expected = gr.GridData(d_sum, grid)
     actual = 2 * gr.GridData(d1, grid)
     self.assertEqual(expected, actual)
Beispiel #13
0
 def test_cartesian_grid_mesh_iter_tuples(self):
     grid = gr.CartesianGrid((x, y, z))
     xm, ym, zm = np.meshgrid(x, y, z, indexing='ij')
     x_expected = xm.flatten()
     y_expected = ym.flatten()
     z_expected = zm.flatten()
     for c, xe, ye, ze in zip(grid.mesh_iter(), x_expected, y_expected,
                              z_expected):
         self.assertEqual(c, (xe, ye, ze))
Beispiel #14
0
    def test_cartesian_grid_init_unequal_length_inputs_mesh_equality(self):
        # Initialize Grids with equal length coordinate arrays
        mixed_grid = gr.CartesianGrid((x, y_short, z_short))

        # Define Expected Behavior
        expected_mixed_mesh = np.meshgrid(x, y_short, z_short, indexing='ij')

        # Check Equality
        mixed_mesh_eq = np.array_equal(mixed_grid.mesh, expected_mixed_mesh)
        self.assertTrue(mixed_mesh_eq)
Beispiel #15
0
 def test_cartesian_grid_init_list_implicit_container_type(self):
     grid = gr.CartesianGrid([x_short, y_short, z_short])
     container_type = grid._container_type
     coordinate_type = type(grid.coordinates)
     mesh_type = type(grid.mesh)
     cartesian_type = type(grid.cartesian)
     self.assertIs(container_type, list)
     self.assertIs(coordinate_type, list)
     self.assertIs(mesh_type, list)
     self.assertIs(cartesian_type, list)
Beispiel #16
0
 def test_grid_data_diff_1D_data(self):
     x1 = (1., 2.)
     x2 = (10, 20)
     grid = gr.CartesianGrid((x1, x2))
     d1 = np.array([[1., 2.], [3., 4.]])
     d2 = np.array([[10., 20.], [30., 40.]])
     d_sum = d1 - d2
     expected = gr.GridData(d_sum, grid)
     actual = gr.GridData(d1, grid) - gr.GridData(d2, grid)
     self.assertEqual(expected, actual)
Beispiel #17
0
 def test_grid_data_iter_overload_2D_data(self):
     x1 = (1., 2.)
     x2 = (100, 200)
     data2d = (np.array([['a', 'b'], ['c', 'd']]), np.array([[1, 2], [3,
                                                                      4]]))
     data1f = data2d[0].flatten()
     data2f = data2d[1].flatten()
     grid2d = gr.CartesianGrid((x1, x2))
     g_d = gr.GridData(data2d, grid2d)
     for gdt, de1, de2, ge in zip(g_d, data1f, data2f, grid2d):
         self.assertEqual(((de1, de2), ge), gdt)
Beispiel #18
0
 def test_grid_data_rmul_2D_data(self):
     x1 = (1., 2.)
     x2 = (10, 20)
     grid = gr.CartesianGrid((x1, x2))
     d1 = np.array([[1., 2.], [3., 4.]])
     d2 = np.array([[10., 20.], [30., 40.]])
     d_sum_0 = 2 * d1
     d_sum_1 = 2 * d2
     expected = gr.GridData((d_sum_0, d_sum_1), grid)
     actual = 2 * gr.GridData((d1, d2), grid)
     self.assertEqual(expected, actual)
Beispiel #19
0
 def test_grid_data_diff_2D_data(self):
     x1 = (1., 2.)
     x2 = (10, 20)
     grid = gr.CartesianGrid((x1, x2))
     d1 = np.array([[1., 2.], [3., 4.]])
     d2 = np.array([[10., 20.], [30., 40.]])
     d_sum_0 = d1 - d2
     d_sum_1 = -2 * d2 - d1
     expected = gr.GridData((d_sum_0, d_sum_1), grid)
     actual = gr.GridData((d1, -1 * d1), grid) - gr.GridData(
         (d2, 2 * d2), grid)
     self.assertEqual(expected, actual)
Beispiel #20
0
 def test_grid_data_like(self):
     x1 = (1., 2.)
     x2 = (100, 200)
     data2d = (np.array([['a', 'b'], ['c', 'd']]), np.array([[1, 2], [3,
                                                                      4]]))
     grid2d = gr.CartesianGrid((x1, x2))
     g_d = gr.GridData(data2d, grid2d)
     data2d_other = (np.array([['e', 'f'],
                               ['g', 'h']]), np.array([[10, 20], [30, 40]]))
     g_expected = gr.GridData(data2d_other, grid2d)
     g_test = g_d.like(data2d_other)
     self.assertEqual(g_test, g_expected)
Beispiel #21
0
    def test_cartesian_grid_divergence_equal_length_arrays_azimuthal_vectors(
            self):
        xm, ym, zm = np.meshgrid(x_short, y, z, indexing='ij')
        vector_field = (-ym, xm, np.zeros_like(zm))
        divergence_expected = 3. * np.zeros_like(xm)

        grid = gr.CartesianGrid((x_short, y, z))
        divergence_calculated = grid.divergence(vector_field)

        # Test Float Equality
        divergence_calculated = np.round(divergence_calculated, 7)
        self.assertTrue(
            np.array_equal(divergence_calculated, divergence_expected))
Beispiel #22
0
 def test_grid_data_iter_overload_1D_data(self):
     x1 = (1., 2.)
     x1r = (1., 1., 2., 2.)
     x2 = (100, 200)
     x2r = (100, 200, 100, 200)
     data1d = np.array([['a', 'b'], ['c', 'd']])
     data1d_flattened = data1d.flatten()
     grid2d = gr.CartesianGrid((x1, x2))
     g_d = gr.GridData(data1d, grid2d)
     for gdt, de, ge in zip(g_d, data1d_flattened, grid2d):
         self.assertEqual((de, ge), gdt)
     for gdt, de, ge1, ge2 in zip(g_d, data1d_flattened, x1r, x2r):
         self.assertEqual((de, (ge1, ge2)), gdt)
Beispiel #23
0
    def test_spherical_grid_init_equal_length_inputs_mesh_equality(self):
        # Initialize Grids with equal length coordinate arrays
        grid = gr.SphericalGrid((r, t, p))
        log_grid = gr.CartesianGrid((r_log, t_log, p_log))

        # Define Expected Behavior
        expected_mesh = np.meshgrid(r, t, p, indexing='ij')
        expected_log_mesh = np.meshgrid(r_log, t_log, p_log, indexing='ij')

        mesh_equality = np.array_equal(grid.mesh, expected_mesh)
        log_mesh_equality = np.array_equal(log_grid.mesh, expected_log_mesh)
        self.assertTrue(mesh_equality)
        self.assertTrue(log_mesh_equality)
Beispiel #24
0
 def test_grid_data_getitem_overload_1D_data(self):
     x1 = (1., 2.)
     x2 = (100, 200)
     grid2d = gr.CartesianGrid((x1, x2))
     data1d = np.array([['a', 'b'], ['c', 'd']])
     d1d2 = grid2d.grid[0] + grid2d.grid[1]
     g_d = gr.GridData(data1d, grid2d)
     g_d2 = gr.GridData(d1d2, grid2d)
     self.assertEqual(('a', (1., 100)), g_d[0, 0])
     self.assertEqual(('c', (2., 100)), g_d[1, 0])
     self.assertEqual(('b', (1., 200)), g_d[0, 1])
     self.assertEqual(('d', (2., 200)), g_d[1, 1])
     self.assertEqual((101, (1., 100)), g_d2[0, 0])
     self.assertEqual((102, (2., 100)), g_d2[1, 0])
     self.assertEqual((201, (1., 200)), g_d2[0, 1])
     self.assertEqual((202, (2., 200)), g_d2[1, 1])
Beispiel #25
0
    def test_cartesian_grid_gradient_equal_length_arrays_linear(self):
        xf, yf, zf = np.meshgrid(x, y, z, indexing='ij')
        grid = gr.CartesianGrid((x, y, z))
        grad_x = grid.gradient(xf)
        grad_y = grid.gradient(yf)
        grad_z = grid.gradient(zf)

        grad_xe = (np.ones_like(xf), np.zeros_like(yf), np.zeros_like(zf))
        grad_ye = (np.zeros_like(xf), np.ones_like(yf), np.zeros_like(zf))
        grad_ze = (np.zeros_like(xf), np.zeros_like(yf), np.ones_like(zf))
        grad_xc = np.round(grad_x, 7)
        grad_yc = np.round(grad_y, 7)
        grad_zc = np.round(grad_z, 7)

        self.assertTrue(np.array_equal(grad_xc, grad_xe))
        self.assertTrue(np.array_equal(grad_yc, grad_ye))
        self.assertTrue(np.array_equal(grad_zc, grad_ze))
Beispiel #26
0
    def test_cartesian_grid_volume_unequal_length_arrays(self):
        dx, dy, dz = 0.2 * np.ones_like(x_short), 0.1 * np.ones_like(
            y), 0.1 * np.ones_like(z)
        dx[0] = 0.5 * dx[0]
        dx[-1] = 0.5 * dx[1]
        dy[0] = 0.5 * dy[0]
        dy[-1] = 0.5 * dy[-1]
        dz[0] = 0.5 * dz[0]
        dz[-1] = 0.5 * dz[-1]
        dxg, dyg, dzg = np.meshgrid(dx, dy, dz, indexing='ij')
        v_e = dxg * dyg * dzg

        # Round For float equality check
        grid = gr.CartesianGrid((x_short, y, z))
        v_e = np.round(v_e, 7)
        v_c = np.round(grid.volume, 7)
        self.assertTrue(np.array_equal(v_e, v_c))
Beispiel #27
0
    def test_unvectorize_cartesian(self):
        si = la.CartesianSpinOperator((1, 0, 0, 0))
        sx = la.CartesianSpinOperator((0, 1, 0, 0))
        sy = la.CartesianSpinOperator((0, 0, 1, 0))
        sz = la.CartesianSpinOperator((0, 0, 0, 1))
        ops = np.array([[si, sx], [sy, sz]])

        x1 = (0, 1)
        x2 = (0, 1)
        grid = gr.CartesianGrid((x1, x2))

        op_grid = gr.GridData(ops, grid)
        expected = op_grid
        calculated = gr.vectorize_operator_grid(op_grid)
        calculated = gr.unvectorize_operator_grid(calculated,
                                                  la.CartesianSpinOperator)

        self.assertEqual(expected, calculated)
Beispiel #28
0
    def test_vectorize_spherical(self):
        si = la.SphericalSpinOperator((1, 0, 0, 0))
        sx = la.SphericalSpinOperator((0, 1, pi / 2, 0))
        sy = la.SphericalSpinOperator((0, 1, pi / 2, pi / 2))
        sz = la.SphericalSpinOperator((0, 1, 0, 0))
        ops = np.array([[si, sx], [sy, sz]])
        expected = (np.array([[1, 0], [0, 0]]), np.array([[0, 1], [0, 0]]),
                    np.array([[0, 0], [1, 0]]), np.array([[0, 0], [0, 1]]))

        x1 = (0, 1)
        x2 = (0, 1)
        grid = gr.CartesianGrid((x1, x2))

        op_grid = gr.GridData(ops, grid)
        expected = gr.GridData(expected, grid)
        calculated = gr.vectorize_operator_grid(op_grid)

        self.assertAlmostEqual(expected, calculated)
Beispiel #29
0
 def test_nonzero_unitary_dynamics(self):
     x1 = (1., 2.)
     x2 = (10., 20.)
     grid = gr.CartesianGrid((x1, x2))
     rhoi = la.CartesianSpinOperator((0.5, 0., 0., 0.))
     rhox = la.CartesianSpinOperator((0.5, 0.5, 0., 0.))
     rhoy = la.CartesianSpinOperator((0.5, 0., 0.5, 0.))
     rhoz = la.CartesianSpinOperator((0.5, 0., 0., 0.5))
     rhos = np.array([[rhoi, rhox], [rhoy, rhoz]])
     rho_grid = gr.GridData(rhos, grid)
     rhoidot_expected = 1 / hbar * la.CartesianSpinOperator((0., 0., 0., 0.))
     rhoxdot_expected = 1 / hbar * la.CartesianSpinOperator((0., 0., 1., 0.))
     rhoydot_expected = 1 / hbar * la.CartesianSpinOperator((0., -1., 0., 0.))
     rhozdot_expected = 1 / hbar * la.CartesianSpinOperator((0., 0., 0., 0.))
     rhodots_expected = np.array([[rhoidot_expected, rhoxdot_expected], [rhoydot_expected, rhozdot_expected]])
     rhodot_expected_grid = rho_grid.like(rhodots_expected)
     ham = la.CartesianSpinOperator((0, 0, 0, 1))
     rhodots_calc_grid = grid_unitary_derivative(rho_grid, ham)
     self.assertEqual(rhodot_expected_grid, rhodots_calc_grid)
Beispiel #30
0
 def test_initialization(self):
     x = (0., 1.)
     y = (0., 1.)
     z = (0., 1.)
     grid = gr.CartesianGrid((x, y, z))
     s000 = la.CartesianSpinOperator((0., 0., 0., 0.))
     s001 = la.CartesianSpinOperator((0., 0., 0., 1.))
     s010 = la.CartesianSpinOperator((0., 0., 1., 0.))
     s011 = la.CartesianSpinOperator((0., 0., 1., 1.))
     s100 = la.CartesianSpinOperator((0., 1., 0., 0.))
     s101 = la.CartesianSpinOperator((0., 1., 0., 1.))
     s110 = la.CartesianSpinOperator((0., 1., 1., 0.))
     s111 = la.CartesianSpinOperator((0., 1., 1., 1.))
     ops = np.array([[[s000, s001], [s010, s011]],
                     [[s100, s101], [s110, s111]]])
     o_grid_ex = gr.GridData(ops, grid)
     o_grid_ac = gr.initialize_operator_grid(grid,
                                             la.CartesianSpinOperator,
                                             i_coord=0.)
     self.assertEqual(o_grid_ac, o_grid_ex)