Peano
Loading...
Searching...
No Matches
plot-dof-updates.py
Go to the documentation of this file.
1# This file is part of the ExaHyPE2 project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3import os
4import sys
5import argparse
6import numpy as np
7import matplotlib.pyplot as plt
8from matplotlib.ticker import ScalarFormatter, FuncFormatter
9from matplotlib.backends.backend_pgf import FigureCanvasPgf
10
11import exahype2.postprocessing
12
13# Define the LaTeX preamble for the output file
14preamble = r"\usepackage{amsmath,amssymb}"
15
16# Set the rcParams dictionary to use the pgf backend and include the LaTeX preamble
17plt.rcParams.update(
18 {
19 "font.family": "sans-serif",
20 "pgf.texsystem": "pdflatex",
21 "pgf.preamble": preamble,
22 }
23)
24
25if __name__ == "__main__":
26 parser = argparse.ArgumentParser(
27 description="ExaHyPE 2 Degrees-of-Freedom-Updates Plotter"
28 )
29 parser.add_argument(
30 "file",
31 nargs="+",
32 help="Filename of the file to parse. You can specify multiple files.",
33 )
34 parser.add_argument(
35 "-v",
36 "--verbose",
37 help="Increase output verbosity",
38 action="store_true",
39 default=False,
40 )
41 parser.add_argument(
42 "-log-y",
43 "--log-ysaxis",
44 help="Use log scale",
45 action="store_true",
46 default=False,
47 )
48 parser.add_argument(
49 "-l",
50 "--label",
51 nargs="*",
52 help="Label of dataset (optional; if used, you have to add --label/-l after the actual files and you have to embed the labels into hyphens)",
53 )
54 parser.add_argument(
55 "-dof",
56 "--degrees-of-freedom",
57 type=int,
58 help="Number of degrees-of-freedom one cell holds.",
59 required=True,
60 )
61 parser.add_argument(
62 "-nc", "--number-of-cells", type=int, help="Number of cells.", required=True
63 )
64 parser.add_argument(
65 "-o", "--output-file", help="Name of output file", default="output"
66 )
67 parser.add_argument(
68 "-f",
69 "--output-format",
70 help="Output format",
71 choices=["png", "pdf", "pgf"],
72 default="png",
73 )
74 args = parser.parse_args()
75
76 # If no labels are provided, generate default labels
77 if not args.label:
78 args.label = [f"Dataset {i+1}" for i in range(len(args.file))]
79
80 labels = []
81 dof_updates_per_second = []
82 for file, label in zip(args.file, args.label):
83 labels.append(label)
84 performance_data = exahype2.postprocessing.PerformanceData(
85 file_name=file, verbose=args.verbose
86 )
87 time_per_time_step_in_seconds = performance_data.time_per_time_step()
88 dof_updates_per_second.append(
89 (args.degrees_of_freedom * args.number_of_cells)
90 / time_per_time_step_in_seconds
91 )
92
93 plt.clf()
94 if args.log_ysaxis:
95 plt.yscale("log")
96
97 fig, ax = plt.subplots(figsize=(10, 6))
98
99 colors = plt.cm.viridis(np.linspace(0, 1, len(labels)))
100
101 # Creating the bar plot
102 bars = plt.bar(
103 labels,
104 dof_updates_per_second,
105 color=colors,
106 width=0.35,
107 edgecolor="black",
108 zorder=2,
109 ) # Ensure bars are in front
110
111 # Adding grid lines
112 plt.grid(
113 axis="y", linestyle="--", alpha=1, linewidth=1, zorder=1
114 ) # Increase line width and set behind bars
115
116 plt.xticks(fontsize=15)
117 plt.yticks(fontsize=15)
118
119 plt.ylabel("Degrees of Freedom Updates per Second", fontsize=15)
120
121 ax.yaxis.set_major_formatter(ScalarFormatter(useMathText=True))
122 ax.ticklabel_format(axis="y", style="sci", scilimits=(0, 0))
123 ax.yaxis.get_offset_text().set_fontsize(15)
124 # ax.set_xticks(range(len(labels)))
125 # ax.set_xticklabels(labels, fontsize=15, rotation=45, ha="right")
126 # ax.set_yticks(ax.get_yticks())
127 # ax.set_yticklabels(ax.get_yticks(), fontsize=15)
128
129 plt.title(
130 "{} Degrees of Freedom".format(args.number_of_cells * args.degrees_of_freedom),
131 fontsize=15,
132 )
133
134 if args.output_format == "pdf":
135 plt.savefig(args.output_file + ".pdf", bbox_inches="tight", transparent=False)
136 elif args.output_format == "png":
137 plt.savefig(args.output_file + ".png", bbox_inches="tight", transparent=False)
138 elif args.output_format == "pgf":
139 fig = plt.gcf()
140 fig.patch.set_alpha(0)
141 canvas = FigureCanvasPgf(fig)
142 canvas.print_figure(
143 args.output_file + ".pgf",
144 bbox_inches="tight",
145 )
146
147 if args.output_file == "output":
148 print(
149 "default file name output.{} written. Change file name with -o or --output-file".format(
150 args.output_format
151 )
152 )