def test_pde_critical_input(): """test some wrong input and edge cases""" # test whether reserved symbols can be used as variables grid = grids.UnitGrid([4]) eq = PDE({"E": 1}) res = eq.solve(ScalarField(grid), t_range=2) np.testing.assert_allclose(res.data, 2) with pytest.raises(ValueError): PDE({"t": 1}) eq = PDE({"u": 1}) assert eq.expressions == {"u": "1.0"} with pytest.raises(ValueError): eq.evolution_rate(FieldCollection.scalar_random_uniform(2, grid)) eq = PDE({"u": 1, "v": 2}) assert eq.expressions == {"u": "1.0", "v": "2.0"} with pytest.raises(ValueError): eq.evolution_rate(ScalarField.random_uniform(grid)) eq = PDE({"u": "a"}) with pytest.raises(RuntimeError): eq.evolution_rate(ScalarField.random_uniform(grid)) eq = PDE({"x": "x"}) with pytest.raises(ValueError): eq.evolution_rate(ScalarField(grid))
def test_pde_wrong_input(): """test some wrong input""" with pytest.raises(ValueError): PDE({"t": 1}) with pytest.raises(ValueError): PDE({"E": 1}) with pytest.raises(ValueError): PDE({"E": 1, "t": 0}) grid = grids.UnitGrid([4]) eq = PDE({"u": 1}) assert eq.expressions == {"u": "1.0"} with pytest.raises(ValueError): eq.evolution_rate(FieldCollection.scalar_random_uniform(2, grid)) eq = PDE({"u": 1, "v": 2}) assert eq.expressions == {"u": "1.0", "v": "2.0"} with pytest.raises(ValueError): eq.evolution_rate(ScalarField.random_uniform(grid)) eq = PDE({"u": "a"}) with pytest.raises(RuntimeError): eq.evolution_rate(ScalarField.random_uniform(grid)) eq = PDE({"x": "x"}) with pytest.raises(ValueError): eq.evolution_rate(ScalarField(grid))
def test_pde_spatial_args(): """ test ScalarFieldExpression without extra dependence """ eq = PDE({"a": "x"}) field = ScalarField(grids.UnitGrid([2])) rhs = eq.evolution_rate(field) assert rhs == field.copy(data=[0.5, 1.5]) rhs = eq.make_pde_rhs(field, backend="numba") np.testing.assert_allclose(rhs(field.data, 0.0), np.array([0.5, 1.5])) eq = PDE({"a": "x + y"}) with pytest.raises(RuntimeError): eq.evolution_rate(field)
def test_pde_user_funcs(): """test user supplied functions""" # test a simple case eq = PDE({"u": "get_x(gradient(u))"}, user_funcs={"get_x": lambda arr: arr[0]}) field = ScalarField.random_colored(grids.UnitGrid([32, 32])) rhs = eq.evolution_rate(field) np.testing.assert_allclose(rhs.data, field.gradient("natural").data[0]) f = eq._make_pde_rhs_numba(field) np.testing.assert_allclose(f(field.data, 0), field.gradient("natural").data[0])
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): return lambda state: state 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
def test_wave_consistency(dim): """test some methods of the wave model""" eq = WavePDE() assert isinstance(str(eq), str) assert isinstance(repr(eq), str) # compare numba to numpy implementation grid = UnitGrid([4] * dim) state = eq.get_initial_condition(ScalarField.random_uniform(grid)) field = eq.evolution_rate(state) assert field.grid == grid rhs = eq._make_pde_rhs_numba(state) np.testing.assert_allclose(field.data, rhs(state.data, 0)) # compare to generic implementation assert isinstance(eq.expressions, dict) eq2 = PDE(eq.expressions) np.testing.assert_allclose(field.data, eq2.evolution_rate(state).data)
def test_pde_consts(): """test using the consts argument in PDE""" field = ScalarField(grids.UnitGrid([3]), 1) eq = PDE({"a": "b"}, consts={"b": 2}) np.testing.assert_allclose(eq.evolution_rate(field).data, 2) eq = PDE({"a": "b**2"}, consts={"b": field}) np.testing.assert_allclose(eq.evolution_rate(field).data, field.data) eq = PDE({"a": "laplace(b)"}, consts={"b": field}) np.testing.assert_allclose(eq.evolution_rate(field).data, 0) eq = PDE({"a": "laplace(b)"}, consts={"b": 3}) with pytest.raises(Exception): eq.evolution_rate(field) eq = PDE({"a": "laplace(b)"}, consts={"b": field.data}) with pytest.raises(Exception): eq.evolution_rate(field)