def test_complex_operator(example_grid): """test using a complex operator on grid""" r = ScalarField.random_normal(example_grid) i = ScalarField.random_normal(example_grid) c = r + 1j * i assert c.is_complex assert np.iscomplexobj(c) c_lap = c.laplace("natural").data np.testing.assert_allclose(c_lap.real, r.laplace("natural").data) np.testing.assert_allclose(c_lap.imag, i.laplace("natural").data)
def test_smoothing(): """test smoothing on different grids""" for grid in [ CartesianGrid([[-2, 3]], 4), UnitGrid(7, periodic=False), UnitGrid(7, periodic=True), ]: f1 = ScalarField.random_uniform(grid) sigma = 0.5 + np.random.random() # this assumes that the grid periodicity is the same for all axes mode = "wrap" if grid.periodic[0] else "reflect" s = sigma / grid.typical_discretization expected = ndimage.gaussian_filter(f1.data, sigma=s, mode=mode) out = f1.smooth(sigma) np.testing.assert_allclose(out.data, expected) out.data = 0 # reset data f1.smooth(sigma, out=out).data np.testing.assert_allclose(out.data, expected) # test one simple higher order smoothing tf = Tensor2Field.random_uniform(grid) assert tf.data.shape == tf.smooth(1).data.shape # test in-place smoothing g = UnitGrid([8, 8]) f1 = ScalarField.random_normal(g) f2 = f1.smooth(3) f1.smooth(3, out=f1) np.testing.assert_allclose(f1.data, f2.data)
def test_pde_bcs_error(bc): """test PDE with wrong boundary conditions""" eq = PDE({"u": "laplace(u)"}, bc=bc) grid = grids.UnitGrid([8, 8]) field = ScalarField.random_normal(grid) for backend in ["numpy", "numba"]: with pytest.raises(BCDataError): eq.solve(field, t_range=1, dt=0.01, backend=backend, tracker=None)
def test_random_normal_types(): """test whether random normal fields behave correctly for different types""" grid = UnitGrid([8]) for dtype in [bool, int, float, complex]: field = VectorField.random_normal(grid, dtype=dtype) assert field.dtype == np.dtype(dtype) assert isinstance(field.data.flat[0].item(), dtype) assert ScalarField.random_normal(grid, 0, 1).dtype == np.dtype(float) assert ScalarField.random_normal(grid, mean=0 + 0j).dtype == np.dtype(complex) assert ScalarField.random_normal(grid, std=1 + 0j).dtype == np.dtype(complex) assert ScalarField.random_normal(grid, 0 + 0j, 1 + 0j).dtype == np.dtype(complex) m = complex(np.random.random(), np.random.random()) s = complex(1 + np.random.random(), 1 + np.random.random()) grid = UnitGrid([256, 256]) field = field.random_normal(grid, m, s) assert np.mean(field.average) == pytest.approx(m, rel=0.1, abs=0.1) assert np.std(field.data.real) == pytest.approx(s.real, rel=0.1, abs=0.1) assert np.std(field.data.imag) == pytest.approx(s.imag, rel=0.1, abs=0.1)
def test_pde_scalar(): """test PDE with a single scalar field""" eq = PDE({"u": "laplace(u) + exp(-t) + sin(t)"}) assert eq.explicit_time_dependence assert not eq.complex_valued grid = grids.UnitGrid([8]) field = ScalarField.random_normal(grid) res_a = eq.solve(field, t_range=1, dt=0.01, backend="numpy", tracker=None) res_b = eq.solve(field, t_range=1, dt=0.01, backend="numba", tracker=None) res_a.assert_field_compatible(res_b) np.testing.assert_allclose(res_a.data, res_b.data)
def test_pde_bcs(bc): """test PDE with boundary conditions""" eq = PDE({"u": "laplace(u)"}, bc=bc) assert not eq.explicit_time_dependence assert not eq.complex_valued grid = grids.UnitGrid([8]) field = ScalarField.random_normal(grid) res_a = eq.solve(field, t_range=1, dt=0.01, backend="numpy", tracker=None) res_b = eq.solve(field, t_range=1, dt=0.01, backend="numba", tracker=None) res_a.assert_field_compatible(res_b) np.testing.assert_allclose(res_a.data, res_b.data)
def test_custom_operators(): """test using a custom operator""" grid = grids.UnitGrid([32]) field = ScalarField.random_normal(grid) eq = PDE({"u": "undefined(u)"}) with pytest.raises(NameError): eq.evolution_rate(field) def make_op(state): def op(arr, out): out[:] = arr[1:-1] # copy valid part of the array return op grids.UnitGrid.register_operator("undefined", make_op) eq._cache = {} # reset cache res = eq.evolution_rate(field) np.testing.assert_allclose(field.data, res.data) del grids.UnitGrid._operators["undefined"] # reset original state