Coverage for pySDC/helpers/visualization_tools.py: 100%
40 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 14:51 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 14:51 +0000
1import matplotlib
3matplotlib.use('Agg')
5from pySDC.helpers.stats_helper import filter_stats
7import numpy as np
9from matplotlib import rc
10import matplotlib.pyplot as plt
13# noinspection PyShadowingBuiltins
14def show_residual_across_simulation(stats, fname='residuals.png'):
15 """
16 Helper routine to visualize the residuals across the simulation (one block of PFASST)
18 Args:
19 stats (dict): statistics object from a PFASST run
20 fname (str): filename
21 """
23 # get residuals of the run
24 extract_stats = filter_stats(stats, type='residual_post_iteration')
26 # find boundaries for x-,y- and c-axis as well as arrays
27 maxprocs = 0
28 maxiter = 0
29 minres = 0
30 maxres = -99
31 for k, v in extract_stats.items():
32 maxprocs = max(maxprocs, k.process)
33 maxiter = max(maxiter, k.iter)
34 minres = min(minres, np.log10(v))
35 maxres = max(maxres, np.log10(v))
37 # grep residuals and put into array
38 residual = np.zeros((maxiter, maxprocs + 1))
39 residual[:] = -99
40 for k, v in extract_stats.items():
41 step = k.process
42 iter = k.iter
43 if iter != -1:
44 residual[iter - 1, step] = np.log10(v)
46 # Set up plotting stuff and fonts
47 rc('font', **{"sans-serif": ["Arial"], "size": 30})
48 rc('legend', fontsize='small')
49 rc('xtick', labelsize='small')
50 rc('ytick', labelsize='small')
52 # create plot and save
53 fig, ax = plt.subplots(figsize=(15, 10))
55 cmap = plt.get_cmap('Reds')
56 plt.pcolor(residual.T, cmap=cmap, vmin=minres, vmax=maxres)
58 cax = plt.colorbar()
59 cax.set_label('log10(residual)')
61 ax.set_xlabel('iteration')
62 ax.set_ylabel('process')
64 ax.set_xticks(np.arange(maxiter) + 0.5, minor=False)
65 ax.set_yticks(np.arange(maxprocs + 1) + 0.5, minor=False)
66 ax.set_xticklabels(np.arange(maxiter) + 1, minor=False)
67 ax.set_yticklabels(np.arange(maxprocs + 1), minor=False)
69 plt.savefig(fname, transparent=True, bbox_inches='tight')