def test_stochastic_adaptive_solver(caplog): """test using an adaptive, stochastic solver""" field = ScalarField.random_uniform(UnitGrid([16]), -1, 1) eq = DiffusionPDE(noise=1e-6) with caplog.at_level(logging.WARNING): solver = ExplicitSolver(eq, backend="numpy", adaptive=True) c = Controller(solver, t_range=1, tracker=None) c.run(field, dt=1e-2) assert "fixed" in caplog.text
def test_no_dt(): """ test scipy solver without timestep """ grid = UnitGrid([16]) field = ScalarField.random_uniform(grid, -1, 1) eq = DiffusionPDE() c1 = Controller(ScipySolver(eq), t_range=1, tracker=None) s1 = c1.run(field, dt=1e-3) c2 = Controller(ScipySolver(eq), t_range=1, tracker=None) s2 = c2.run(field) np.testing.assert_allclose(s1.data, s2.data, rtol=1e-3, atol=1e-3)
def test_compare_solvers(solver_class): """compare several solvers""" field = ScalarField.random_uniform(UnitGrid([8, 8]), -1, 1) eq = DiffusionPDE() # ground truth c1 = Controller(ExplicitSolver(eq, scheme="runge-kutta"), t_range=0.1, tracker=None) s1 = c1.run(field, dt=5e-3) c2 = Controller(solver_class(eq), t_range=0.1, tracker=None) with np.errstate(under="ignore"): s2 = c2.run(field, dt=5e-3) np.testing.assert_allclose(s1.data, s2.data, rtol=1e-2, atol=1e-2)
def test_compare_explicit(): """ test explicit solvers """ grid = UnitGrid([16, 16]) field = ScalarField.random_uniform(grid, -1, 1) eq = DiffusionPDE() c1 = Controller(ExplicitSolver(eq), t_range=0.1, tracker=None) s1 = c1.run(field, dt=2e-3) c2 = Controller(ExplicitSolver(eq, scheme="runge-kutta"), t_range=0.1, tracker=None) with np.errstate(under="ignore"): s2 = c2.run(field, dt=2e-3) np.testing.assert_allclose(s1.data, s2.data, rtol=1e-2, atol=1e-2)
def test_stochastic_solvers(backend): """ test simple version of the stochastic solver """ field = ScalarField.random_uniform(UnitGrid([16]), -1, 1) eq = DiffusionPDE() seq = DiffusionPDE(noise=1e-6) solver1 = ExplicitSolver(eq, backend=backend) c1 = Controller(solver1, t_range=1, tracker=None) s1 = c1.run(field, dt=1e-3) solver2 = ExplicitSolver(seq, backend=backend) c2 = Controller(solver2, t_range=1, tracker=None) s2 = c2.run(field, dt=1e-3) np.testing.assert_allclose(s1.data, s2.data, rtol=1e-4, atol=1e-4) assert not solver1.info["stochastic"] assert solver2.info["stochastic"]
def test_solvers_simple_ode(scheme, adaptive): """test explicit solvers with a simple ODE""" grid = UnitGrid([1]) field = ScalarField(grid, 1) eq = PDE({"y": "2*sin(t) - y"}) dt = 1e-3 if scheme == "euler" else 1e-2 storage = MemoryStorage() solver = ExplicitSolver(eq, scheme=scheme, adaptive=adaptive) controller = Controller(solver, t_range=20.0, tracker=storage.tracker(1)) controller.run(field, dt=dt) ts = np.ravel(storage.times) expect = 2 * np.exp(-ts) - np.cos(ts) + np.sin(ts) np.testing.assert_allclose(np.ravel(storage.data), expect, atol=0.05) if adaptive: assert solver.info["steps"] < 20 / dt else: assert solver.info["steps"] == pytest.approx(20 / dt, abs=1)
def test_solvers_simple_example(scheme, adaptive): """test explicit solvers""" grid = UnitGrid([4]) field = ScalarField(grid, 1) eq = PDE({"c": "c"}) dt = 1e-3 if scheme == "euler" else 1e-2 solver = ExplicitSolver(eq, scheme=scheme, adaptive=adaptive) controller = Controller(solver, t_range=10.0, tracker=None) res = controller.run(field, dt=dt) np.testing.assert_allclose(res.data, np.exp(10), rtol=0.1) if adaptive: assert solver.info["steps"] != pytest.approx(10 / dt, abs=1) else: assert solver.info["steps"] == pytest.approx(10 / dt, abs=1)
def test_solvers_complex(backend): """test solvers with a complex PDE""" r = FieldCollection.scalar_random_uniform(2, UnitGrid([3]), labels=["a", "b"]) c = r["a"] + 1j * r["b"] assert c.is_complex # assume c = a + i * b eq_c = PDE({"c": "-I * laplace(c)"}) eq_r = PDE({"a": "laplace(b)", "b": "-laplace(a)"}) res_r = eq_r.solve(r, t_range=1e-2, dt=1e-3, backend="numpy", tracker=None) exp_c = res_r[0].data + 1j * res_r[1].data for solver_class in [ExplicitSolver, ImplicitSolver, ScipySolver]: solver = solver_class(eq_c, backend=backend) controller = Controller(solver, t_range=1e-2, tracker=None) res_c = controller.run(c, dt=1e-3) np.testing.assert_allclose(res_c.data, exp_c, rtol=1e-3, atol=1e-3)