def test_runtime_tracker(): """ test the RuntimeTracker """ s = ScalarField.random_uniform(UnitGrid([128])) tracker = trackers.RuntimeTracker("0:01") sol = ExplicitSolver(DiffusionPDE()) con = Controller(sol, t_range=1e4, tracker=["progress", tracker]) con.run(s, dt=1e-3)
def test_consistency_tracker(): """ test the ConsistencyTracker """ s = ScalarField.random_uniform(UnitGrid([128])) sol = ExplicitSolver(DiffusionPDE(1e3)) con = Controller(sol, t_range=1e5, tracker=["consistency"]) with np.errstate(all="ignore"): con.run(s, dt=1) assert con.info["t_final"] < con.info["t_end"]
def main(equation: str = "cahn-hilliard", t_range: float = 100, size: int = 32): """main routine testing the performance Args: equation (str): Chooses the equation to consider t_range (float): Sets the total duration that should be solved for size (int): The number of grid points along each axis """ print("Reports duration in seconds (smaller is better)\n") # determine grid and initial state grid = UnitGrid([size, size], periodic=False) field = ScalarField.random_uniform(grid) print(f"GRID: {grid}") # determine the equation to solve if equation == "diffusion": eq = DiffusionPDE() elif equation == "cahn-hilliard": eq = CahnHilliardPDE() else: raise ValueError(f"Undefined equation `{equation}`") print(f"EQUATION: ∂c/∂t = {eq.expression}") print("\nSOLVER PERFORMANCE:") expected = eq.solve(field, t_range=t_range, dt=1e-5, tracker=None) solvers = { "Euler, fixed": (1e-3, ExplicitSolver(eq, scheme="euler", adaptive=False)), "Euler, adaptive": (1e-3, ExplicitSolver(eq, scheme="euler", adaptive=True)), "Runge-Kutta, fixed": (1e-3, ExplicitSolver(eq, scheme="rk", adaptive=False)), "Runge-Kutta, adaptive": (1e-3, ExplicitSolver(eq, scheme="rk", adaptive=True)), "implicit": (1e-3, ImplicitSolver(eq)), "scipy": (None, ScipySolver(eq)), } for name, (dt, solver) in solvers.items(): solver.backend = "numba" controller = Controller(solver, t_range=t_range, tracker=None) result = controller.run(field, dt=dt) # call once to pre-compile and test result if np.allclose(result.data, expected.data, atol=1e-2): # report the duration print(f"{name:>21s}: {controller.info['profiler']['solver']:.3g}") else: # report the mismatch print(f"{name:>21s}: MISMATCH")
def test_material_conservation_tracker(): """ test the MaterialConservationTracker """ state = ScalarField.random_uniform(UnitGrid([8, 8]), 0, 1) solver = ExplicitSolver(CahnHilliardPDE()) controller = Controller(solver, t_range=1, tracker=["material_conservation"]) controller.run(state, dt=1e-3) assert controller.info["t_final"] >= 1 solver = ExplicitSolver(AllenCahnPDE()) controller = Controller(solver, t_range=1, tracker=["material_conservation"]) controller.run(state, dt=1e-3) assert controller.info["t_final"] <= 1
This example shows how to set up solvers explicitly and how to extract diagnostic information. """ from pde import (UnitGrid, ScalarField, FieldCollection, DiffusionPDE, ExplicitSolver, ScipySolver, Controller) # initialize the grid, an initial condition, and the PDE grid = UnitGrid([32, 32]) field = ScalarField.random_uniform(grid, -1, 1) eq = DiffusionPDE() # try the explicit solver solver1 = ExplicitSolver(eq) controller1 = Controller(solver1, t_range=1, tracker=None) sol1 = controller1.run(field, dt=1e-3) sol1.label = 'explicit solver' print('Diagnostic information from first run:') print(controller1.diagnostics) print() # try the standard scipy solver solver2 = ScipySolver(eq) controller2 = Controller(solver2, t_range=1, tracker=None) sol2 = controller2.run(field) sol2.label = 'scipy solver' print('Diagnostic information from second run:') print(controller2.diagnostics) print()