Coverage for pySDC/tutorial/step_8/A_visualize_residuals.py: 100%
32 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-16 14:51 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-16 14:51 +0000
1import os
2from pathlib import Path
4from pySDC.helpers.stats_helper import get_sorted
5from pySDC.helpers.visualization_tools import show_residual_across_simulation
6from pySDC.implementations.controller_classes.controller_nonMPI import controller_nonMPI
7from pySDC.tutorial.step_6.A_run_non_MPI_controller import set_parameters_ml
10def main():
11 """
12 A simple test program to demonstrate residual visualization
13 """
15 # get parameters from Step 6, Part A
16 description, controller_params, t0, Tend = set_parameters_ml()
18 # use 8 processes here
19 num_proc = 8
21 # instantiate controller
22 controller = controller_nonMPI(num_procs=num_proc, controller_params=controller_params, description=description)
24 # get initial values on finest level
25 P = controller.MS[0].levels[0].prob
26 uinit = P.u_exact(t0)
28 # call main function to get things done...
29 uend, stats = controller.run(u0=uinit, t0=t0, Tend=Tend)
31 # compute exact solution and compare (for testing purposes only)
32 uex = P.u_exact(Tend)
33 err = abs(uex - uend)
35 # filter statistics by type (number of iterations)
36 iter_counts = get_sorted(stats, type='niter', sortby='time')
38 # compute and print statistics
39 min_iter = 99
40 max_iter = 0
41 Path("data").mkdir(parents=True, exist_ok=True)
42 f = open('data/step_8_A_out.txt', 'w')
43 for item in iter_counts:
44 out = 'Number of iterations for time %4.2f: %1i' % item
45 f.write(out + '\n')
46 print(out)
47 min_iter = min(min_iter, item[1])
48 max_iter = max(max_iter, item[1])
49 f.close()
51 # call helper routine to produce residual plot
53 fname = 'data/step_8_residuals.png'
54 show_residual_across_simulation(stats=stats, fname=fname)
56 assert err < 6.1555e-05, 'ERROR: error is too large, got %s' % err
57 assert os.path.isfile(fname), 'ERROR: residual plot has not been created'
58 assert min_iter == 7 and max_iter == 7, "ERROR: number of iterations not as expected, got %s and %s" % (
59 min_iter,
60 max_iter,
61 )
64if __name__ == "__main__":
65 main()