def test_trackers(): """ test whether simple trackers can be used """ times = [] def store_time(state, t): times.append(t) def get_data(state): return {"integral": state.integral} devnull = open(os.devnull, "w") data = trackers.DataTracker(get_data, interval=0.1) tracker_list = [ trackers.PrintTracker(interval=0.1, stream=devnull), trackers.CallbackTracker(store_time, interval=0.1), None, # should be ignored data, ] if module_available("matplotlib"): tracker_list.append(trackers.PlotTracker(interval=0.1, show=False)) grid = UnitGrid([16, 16]) state = ScalarField.random_uniform(grid, 0.2, 0.3) pde = DiffusionPDE() pde.solve(state, t_range=1, dt=0.005, tracker=tracker_list) devnull.close() assert times == data.times if module_available("pandas"): df = data.dataframe np.testing.assert_allclose(df["time"], times) np.testing.assert_allclose(df["integral"], state.integral)
def test_plot_movie_tracker(tmp_path): """ test whether the plot tracker creates files without errors """ output_file = tmp_path / "movie.mov" grid = UnitGrid([4, 4]) state = ScalarField.random_uniform(grid) pde = DiffusionPDE() tracker = trackers.PlotTracker(movie=output_file, interval=0.1, show=False) pde.solve(state, t_range=0.5, dt=0.005, tracker=tracker, backend="numpy") assert output_file.stat().st_size > 0
def test_plot_tracker(tmp_path): """ test whether the plot tracker creates files without errors """ output_file = tmp_path / "img.png" def get_title(state, t): return f"{state.integral:g} at {t:g}" grid = UnitGrid([4, 4]) state = ScalarField.random_uniform(grid) pde = DiffusionPDE() tracker = trackers.PlotTracker( output_file=output_file, title=get_title, interval=0.1, show=False ) pde.solve(state, t_range=0.5, dt=0.005, tracker=tracker, backend="numpy") assert output_file.stat().st_size > 0