def test_findiff_cyl(): """test operator for a simple cylindrical grid. Note that we only really test the polar symmetry""" grid = CylindricalGrid(1.5, [0, 1], (3, 2), periodic_z=True) _, r1, r2 = grid.axes_coords[0] np.testing.assert_array_equal(grid.discretization, np.full(2, 0.5)) s = ScalarField(grid, [[1, 1], [2, 2], [4, 4]]) v = VectorField(grid, [[[1, 1], [2, 2], [4, 4]], [[0, 0]] * 3, [[0, 0]] * 3]) # test gradient grad = s.gradient(bc=["value", "periodic"]) np.testing.assert_allclose(grad.data[0], [[1, 1], [3, 3], [-6, -6]]) grad = s.gradient(bc=["derivative", "periodic"]) np.testing.assert_allclose(grad.data[0], [[1, 1], [3, 3], [2, 2]]) # test divergence div = v.divergence(bc=["value", "periodic"]) y1 = 3 + 2 / r1 y2 = -6 + 4 / r2 np.testing.assert_allclose(div.data, [[5, 5], [y1, y1], [y2, y2]]) div = v.divergence(bc=["derivative", "periodic"]) y2 = 2 + 4 / r2 np.testing.assert_allclose(div.data, [[5, 5], [y1, y1], [y2, y2]]) # test laplace lap = s.laplace(bc=[{"type": "value", "value": 3}, "periodic"]) y1 = 4 + 3 / r1 y2 = -16 np.testing.assert_allclose(lap.data, [[8, 8], [y1, y1], [y2, y2]]) lap = s.laplace(bc=[{"type": "derivative", "value": 3}, "periodic"]) y2 = -2 + 3.5 / r2 np.testing.assert_allclose(lap.data, [[8, 8], [y1, y1], [y2, y2]])
def test_gradient_1d(): """test specific boundary conditions for the 1d gradient""" grid = UnitGrid(5) b_l = {"type": "derivative", "value": -1} b_r = {"type": "derivative", "value": 1} bcs = grid.get_boundary_conditions([b_l, b_r]) field = ScalarField(grid, np.arange(5)) res = field.gradient(bcs) np.testing.assert_allclose(res.data, np.ones((1, 5))) b_l = {"type": "value", "value": 3} b_r = {"type": "value", "value": 3} bcs = grid.get_boundary_conditions([b_l, b_r]) field = ScalarField(grid, np.full(5, 3)) res = field.gradient(bcs) np.testing.assert_allclose(res.data, np.zeros((1, 5)))
def test_findiff_sph(): """test operator for a simple spherical grid""" grid = SphericalSymGrid(1.5, 3) _, r1, r2 = grid.axes_coords[0] assert grid.discretization == (0.5, ) s = ScalarField(grid, [1, 2, 4]) v = VectorField(grid, [[1, 2, 4], [0] * 3, [0] * 3]) # test gradient grad = s.gradient(bc="value") np.testing.assert_allclose(grad.data[0, :], [1, 3, -6]) grad = s.gradient(bc="derivative") np.testing.assert_allclose(grad.data[0, :], [1, 3, 2]) # test divergence div = v.divergence(bc="value", conservative=False) np.testing.assert_allclose(div.data, [9, 3 + 4 / r1, -6 + 8 / r2]) div = v.divergence(bc="derivative", conservative=False) np.testing.assert_allclose(div.data, [9, 3 + 4 / r1, 2 + 8 / r2])
def test_findiff(): """ test operator for a simple polar grid """ grid = PolarGrid(1.5, 3) _, _, r2 = grid.axes_coords[0] assert grid.discretization == (0.5, ) s = ScalarField(grid, [1, 2, 4]) v = VectorField(grid, [[1, 2, 4], [0] * 3]) # test gradient grad = s.gradient(bc="value") np.testing.assert_allclose(grad.data[0, :], [1, 3, -6]) grad = s.gradient(bc="derivative") np.testing.assert_allclose(grad.data[0, :], [1, 3, 2]) # test divergence div = v.divergence(bc="value") np.testing.assert_allclose(div.data, [5, 17 / 3, -6 + 4 / r2]) div = v.divergence(bc="derivative") np.testing.assert_allclose(div.data, [5, 17 / 3, 2 + 4 / r2])
def test_gradient_field_cyl(): """ test the gradient operator""" grid = CylindricalGrid(2 * np.pi, [0, 2 * np.pi], [8, 16], periodic_z=True) r, z = grid.cell_coords[..., 0], grid.cell_coords[..., 1] s = ScalarField(grid, data=np.cos(r) + np.sin(z)) v = s.gradient(bc="natural") assert v.data.shape == (3, 8, 16) np.testing.assert_allclose(v.data[0], -np.sin(r), rtol=0.1, atol=0.1) np.testing.assert_allclose(v.data[1], np.cos(z), rtol=0.1, atol=0.1) np.testing.assert_allclose(v.data[2], 0, rtol=0.1, atol=0.1)
def test_rect_div_grad(): """compare div grad to laplacian""" grid = CartesianGrid([[0, 2 * np.pi], [0, 2 * np.pi]], [16, 16], periodic=True) x, y = grid.cell_coords[..., 0], grid.cell_coords[..., 1] field = ScalarField(grid, data=np.cos(x) + np.sin(y)) bcs = grid.get_boundary_conditions("auto_periodic_neumann") a = field.laplace(bcs) b = field.gradient(bcs).divergence("auto_periodic_curvature") np.testing.assert_allclose(a.data, -field.data, rtol=0.05, atol=0.01) np.testing.assert_allclose(b.data, -field.data, rtol=0.05, atol=0.01)
def test_div_grad_const(): """compare div grad to laplace operator""" grid = CartesianGrid([[-1, 1]], 32) # test constant y = ScalarField(grid, 3) for bc in [{"type": "derivative", "value": 0}, {"type": "value", "value": 3}]: bcs = grid.get_boundary_conditions(bc) lap = y.laplace(bcs) divgrad = y.gradient(bcs).divergence("auto_periodic_curvature") np.testing.assert_allclose(lap.data, np.zeros(32)) np.testing.assert_allclose(divgrad.data, np.zeros(32))
def test_div_grad_quadratic(): """ compare div grad to laplace operator """ grid = CartesianGrid([[-1, 1]], 32) x = grid.axes_coords[0] # test simple quadratic y = ScalarField(grid, x**2) bcs = grid.get_boundary_conditions({"type": "derivative", "value": 2}) lap = y.laplace(bcs) divgrad = y.gradient(bcs).divergence(bcs.differentiated) np.testing.assert_allclose(lap.data, np.full(32, 2.0)) np.testing.assert_allclose(divgrad.data, np.full(32, 2.0))
def test_div_grad_linear(): """ compare div grad to laplace operator """ grid = CartesianGrid([[-1, 1]], 32) x = grid.axes_coords[0] # test linear f = np.random.random() + 1 y = ScalarField(grid, f * x) b1 = [{"type": "neumann", "value": -f}, {"type": "neumann", "value": f}] b2 = [{"type": "value", "value": -f}, {"type": "value", "value": f}] for bs in [b1, b2]: bcs = y.grid.get_boundary_conditions(bs) lap = y.laplace(bcs) divgrad = y.gradient(bcs).divergence(bcs.differentiated) np.testing.assert_allclose(lap.data, np.zeros(32), atol=1e-10) np.testing.assert_allclose(divgrad.data, np.zeros(32), atol=1e-10)