Coverage for pySDC/tutorial/step_8/A_visualize_residuals.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-20 14:51 +0000

1import os 

2from pathlib import Path 

3 

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 

8 

9 

10def main(): 

11 """ 

12 A simple test program to demonstrate residual visualization 

13 """ 

14 

15 # get parameters from Step 6, Part A 

16 description, controller_params, t0, Tend = set_parameters_ml() 

17 

18 # use 8 processes here 

19 num_proc = 8 

20 

21 # instantiate controller 

22 controller = controller_nonMPI(num_procs=num_proc, controller_params=controller_params, description=description) 

23 

24 # get initial values on finest level 

25 P = controller.MS[0].levels[0].prob 

26 uinit = P.u_exact(t0) 

27 

28 # call main function to get things done... 

29 uend, stats = controller.run(u0=uinit, t0=t0, Tend=Tend) 

30 

31 # compute exact solution and compare (for testing purposes only) 

32 uex = P.u_exact(Tend) 

33 err = abs(uex - uend) 

34 

35 # filter statistics by type (number of iterations) 

36 iter_counts = get_sorted(stats, type='niter', sortby='time') 

37 

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() 

50 

51 # call helper routine to produce residual plot 

52 

53 fname = 'data/step_8_residuals.png' 

54 show_residual_across_simulation(stats=stats, fname=fname) 

55 

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 ) 

62 

63 

64if __name__ == "__main__": 

65 main()