hyper.deal
Loading...
Searching...
No Matches
dynamic_convergence_table.h
1// ---------------------------------------------------------------------
2//
3// Copyright (C) 2020 by the hyper.deal authors
4//
5// This file is part of the hyper.deal library.
6//
7// The hyper.deal library is free software; you can use it, redistribute
8// it, and/or modify it under the terms of the GNU Lesser General
9// Public License as published by the Free Software Foundation; either
10// version 3.0 of the License, or (at your option) any later version.
11// The full text of the license can be found in the file LICENSE.MD at
12// the top level directory of hyper.deal.
13//
14// ---------------------------------------------------------------------
15
16#ifndef HYPERDEAL_DYNAMIC_CONVERGENCE_TABLE
17#define HYPERDEAL_DYNAMIC_CONVERGENCE_TABLE
18
19#include <hyper.deal/base/config.h>
20
21#include <deal.II/base/timer.h>
22
23#include <algorithm>
24
25namespace hyperdeal
26{
33 {
34 public:
36 {
37 this->add_new_row();
38 }
39
40 void
41 start(std::string label, bool reset = false)
42 {
43 auto it = timers.find(label);
44 if (it != timers.end())
45 {
46 if (reset)
47 it->second.reset();
48 it->second.start();
49 }
50 else
51 {
52 timers[label] = dealii::Timer();
53 timers[label].start();
54 }
55 }
56
57 double
58 stop(std::string label)
59 {
60 return timers[label].stop();
61 }
62
63 void
64 stop_and_set(std::string label)
65 {
66 double value = stop(label);
67 set(label, value);
68 }
69
70 void
71 stop_and_put(std::string label)
72 {
73 double value = stop(label);
74 put(label, value);
75 }
76
77 void
78 add_new_row()
79 {
80 vec.push_back(std::map<std::string, double>());
81 }
82
83 void
84 put(std::string label, double value) const
85 {
86 auto &map = vec.back();
87 auto it = map.find(label);
88 if (it != map.end())
89 it->second += value;
90 else
91 map[label] = value;
92 }
93
94 double
95 get(std::string label) const
96 {
97 auto &map = vec.back();
98 auto it = map.find(label);
99 if (it != map.end())
100 return it->second;
101 else
102 return 0;
103 }
104
105 void
106 set(std::string label, double value) const
107 {
108 auto &map = vec.back();
109 auto it = map.find(label);
110 if (it != map.end())
111 it->second = value;
112 else
113 map[label] = value;
114 }
115
116 void
117 print(FILE *f, bool do_horizontal = true) const
118 {
119 int rank;
120 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
121
122 if (rank)
123 return;
124
125 std::vector<std::string> header;
126
127 for (auto &map : vec)
128 for (auto &it : map)
129 if (std::find(header.begin(), header.end(), it.first) == header.end())
130 header.push_back(it.first);
131
132 std::sort(header.begin(), header.end());
133
134 if (do_horizontal)
135 {
136 for (auto it : header)
137 fprintf(f, "%12s", it.c_str());
138 fprintf(f, "\n");
139
140 for (auto &map : vec)
141 {
142 if (map.size() == 0)
143 continue;
144 for (auto h : header)
145 {
146 auto it = map.find(h);
147 if (it == map.end())
148 fprintf(f, "%12.4e", 0.0);
149 else
150 fprintf(f, "%12.4e", it->second);
151 }
152 fprintf(f, "\n");
153 }
154 }
155 else
156 {
157 size_t length = 0;
158 for (auto head : header)
159 {
160 length = std::max(length, head.size());
161 }
162
163 const auto hline = [&]() {
164 // hline
165 printf("+%s+", std::string((int)length + 5, '-').c_str());
166 for (auto &map : vec)
167 {
168 if (map.size() == 0)
169 continue;
170 fprintf(f, "--------------+");
171 }
172 fprintf(f, "\n");
173 };
174
175 hline();
176
177 // header
178 printf("| %-*s|", (int)length + 4, "category");
179 int counter = 0;
180 for (auto &map : vec)
181 {
182 if (map.size() == 0)
183 continue;
184 fprintf(f, " item %2d |", counter++);
185 }
186 fprintf(f, "\n");
187
188 hline();
189
190 // categories
191 for (auto head : header)
192 {
193 fprintf(f, "| %-*s|", (int)length + 4, head.c_str());
194 for (auto &map : vec)
195 {
196 if (map.size() == 0)
197 continue;
198 auto it = map.find(head);
199 if (it == map.end())
200 fprintf(f, " %12.4e |", 0.0);
201 else
202 fprintf(f, " %12.4e |", it->second);
203 }
204
205 fprintf(f, "\n");
206 }
207 hline();
208
209 fprintf(f, "\n");
210 }
211 }
212
213 void
214 print(bool do_horizontal = true) const
215 {
216 this->print(stdout, do_horizontal);
217 }
218
219
220 void
221 print(std::string filename) const
222 {
223 FILE *f = fopen(filename.c_str(), "w");
224 this->print(f);
225 fclose(f);
226 }
227
228 private:
229 mutable std::vector<std::map<std::string, double>> vec;
230 std::map<std::string, dealii::Timer> timers;
231 };
232
233} // namespace hyperdeal
234
235#endif
Definition dynamic_convergence_table.h:33