deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
parsed_convergence_table.cc
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2019 - 2025 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
17
18#include <fstream>
19#include <set>
20
21
23
24namespace
25{
26 std::vector<std::string>
27 get_unique_component_names(const std::vector<std::string> &component_names)
28 {
29 auto elements = component_names;
30 elements.erase(std::unique(elements.begin(), elements.end()),
31 elements.end());
32 return elements;
33 }
34
35
36
37 std::vector<ComponentMask>
38 get_unique_component_masks(const std::vector<std::string> &component_names)
39 {
40 const auto unique_component_names =
41 get_unique_component_names(component_names);
42
43 std::vector<ComponentMask> masks;
44
45 std::vector<std::vector<bool>> bools(
46 unique_component_names.size(),
47 std::vector<bool>(component_names.size(), false));
48
49 unsigned int j = 0;
50 for (unsigned int i = 0; i < component_names.size(); ++i)
51 {
52 if (unique_component_names[j] != component_names[i])
53 masks.emplace_back(bools[j++]);
54 bools[j][i] = true;
55 }
56 masks.emplace_back(bools[j++]);
57 AssertDimension(j, unique_component_names.size());
58 return masks;
59 }
60} // namespace
61
62
63
65 const std::vector<std::string> &solution_names,
66 const std::vector<std::set<VectorTools::NormType>> &list_of_error_norms)
67 : ParsedConvergenceTable(solution_names,
68 list_of_error_norms,
69 2.0,
70 {"cells", "dofs"},
71 "dofs",
72 "reduction_rate_log2",
73 "",
74 3,
75 true)
76{}
77
78
79
81 const std::vector<std::string> &component_names,
82 const std::vector<std::set<VectorTools::NormType>> &list_of_error_norms,
83 const double exponent,
84 const std::set<std::string> &extra_columns,
85 const std::string &rate_key,
86 const std::string &rate_mode,
87 const std::string &error_file_name,
88 const unsigned int precision,
89 const bool compute_error)
90 : component_names(component_names)
91 , unique_component_names(get_unique_component_names(component_names))
92 , unique_component_masks(get_unique_component_masks(component_names))
93 , norms_per_unique_component(list_of_error_norms)
94 , exponent(exponent)
95 , extra_columns(extra_columns)
96 , rate_key(rate_key)
97 , rate_mode(rate_mode)
98 , precision(precision)
99 , error_file_name(error_file_name)
100 , compute_error(compute_error)
101{
104}
105
106
107
108void
110{
111 prm.add_parameter("Enable computation of the errors",
113 "When set to false, no computations are performed.");
114
115 prm.add_parameter("Error precision",
116 precision,
117 "Number of digits to use when printing the error.",
119
120 prm.add_parameter("Error file name",
122 "Set this to a filename with extension .txt, .gpl, .org, "
123 "or .tex to enable writing the convergence table to a "
124 "file.");
125
126 prm.add_parameter(
127 "List of error norms to compute",
129 "Each component is separated by a semicolon "
130 "and each norm by a comma. See the documentation of VectorTools::NormType "
131 "for a list of implemented norms. If you want to skip a component, leave "
132 "its entry empty.");
133
134
135 prm.add_parameter("Exponent for p-norms",
136 exponent,
137 "The exponent to use when computing p-norms.",
139
140 prm.add_parameter("Extra columns",
142 "Extra columns to add to the table. Available options "
143 "are dofs and cells.",
144 Patterns::List(Patterns::Selection("dofs|cells")));
145
146 prm.add_parameter("Rate key",
147 rate_key,
148 "Key to use when computing convergence rates. If "
149 "this is set to a column that is not present, or to the "
150 "empty string, then no error rates are computed.");
151
152 prm.add_parameter("Rate mode",
153 rate_mode,
154 "What type of error rate to compute. Available options are "
155 "reduction_rate_log2, reduction_rate, and none.",
157 "reduction_rate|reduction_rate_log2|none"));
158}
159
160
161
162void
164{
165 if (compute_error)
166 {
167 // Add convergence rates if the rate_key is not empty
168 if (rate_key != "")
169 {
170 bool has_key = false;
171 for (const auto &col : extra_columns)
172 {
173 if (rate_key == col)
174 has_key = true;
175
176 if (col != "")
178 }
179
180 for (const auto &extra_col : extra_column_functions)
181 if (extra_col.second.second == false)
182 {
183 if (rate_key == extra_col.first)
184 has_key = true;
186 extra_col.first);
187 }
188
189 if (has_key)
190 {
191 if (rate_mode == "reduction_rate_log2")
194 else if (rate_mode == "reduction_rate")
197 else
198 {
199 Assert(rate_mode != "none", ExcInternalError());
200 }
201 }
202 else
203 {
204 AssertThrow(rate_key != "",
206 "You specified the key <" + rate_key +
207 "> to compute convergence rates, but that key does "
208 "not exist in the current table."));
209 }
210 }
211 }
212}
213
214
215
216void
218{
219 if (compute_error)
220 {
222 table.write_text(out);
223 output_table();
224 }
225}
226
227
228
229void
231{
232 if (compute_error && error_file_name != "")
233 {
235
236 const std::string error_file_format =
237 error_file_name.substr(error_file_name.find_last_of('.') + 1);
238
239 std::ofstream table_file(error_file_name);
240
241 if (error_file_format == "tex")
242 table.write_tex(table_file);
243 else if (error_file_format == "txt")
244 table.write_text(table_file);
245 else if (error_file_format == "gpl")
246 table.write_text(table_file,
248 else if (error_file_format == "org")
250 else
251 {
252 AssertThrow(false,
253 ExcInternalError("Unrecognized file format: " +
254 error_file_format));
255 }
256 table_file.close();
257 }
258}
259
260
261
262void
264 const std::string &column_name,
265 const std::function<double()> &custom_function,
266 const bool compute_rate)
267{
268 extra_column_functions[column_name] = {custom_function, compute_rate};
269}
270
void evaluate_all_convergence_rates(const std::string &reference_column_key, const RateMode rate_mode)
void omit_column_from_convergence_rate_evaluation(const std::string &key)
void add_parameter(const std::string &entry, ParameterType &parameter, const std::string &documentation="", const Patterns::PatternBase &pattern= *Patterns::Tools::Convert< ParameterType >::to_pattern(), const bool has_to_be_set=false)
The ParsedConvergenceTable class.
void add_extra_column(const std::string &column_name, const std::function< double()> &custom_function, const bool compute_rate=true)
void add_parameters(ParameterHandler &prm)
std::set< std::string > extra_columns
ParsedConvergenceTable(const std::vector< std::string > &component_names={"u"}, const std::vector< std::set< VectorTools::NormType > > &list_of_error_norms={ {VectorTools::H1_norm, VectorTools::L2_norm, VectorTools::Linfty_norm}})
const std::vector< std::string > unique_component_names
std::vector< std::set< VectorTools::NormType > > norms_per_unique_component
std::map< std::string, std::pair< std::function< double()>, bool > > extra_column_functions
void write_text(std::ostream &out, const TextOutputFormat format=table_with_headers) const
@ table_with_separate_column_description
void write_tex(std::ostream &file, const bool with_header=true) const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)