Reference documentation for deal.II version Git 7026f387cc 2019-10-15 14:19:01 -0400
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
parsed_convergence_table.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2019 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II 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 2.1 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 deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #include <deal.II/base/parsed_convergence_table.h>
17 #include <deal.II/base/patterns.h>
18 #include <deal.II/base/utilities.h>
19 
20 DEAL_II_NAMESPACE_OPEN
21 
22 namespace
23 {
24  std::vector<std::string>
25  get_unique_component_names(const std::vector<std::string> &component_names)
26  {
27  auto elements = component_names;
28  auto last = std::unique(elements.begin(), elements.end());
29  elements.resize(last - elements.begin());
30  return elements;
31  }
32 
33 
34 
35  std::vector<ComponentMask>
36  get_unique_component_masks(const std::vector<std::string> &component_names)
37  {
38  const auto unique_component_names =
39  get_unique_component_names(component_names);
40 
41  std::vector<ComponentMask> masks;
42 
43  std::vector<std::vector<bool>> bools(
44  unique_component_names.size(),
45  std::vector<bool>(component_names.size(), false));
46 
47  unsigned int j = 0;
48  for (unsigned int i = 0; i < component_names.size(); ++i)
49  {
50  if (unique_component_names[j] != component_names[i])
51  masks.emplace_back(bools[j++]);
52  bools[j][i] = true;
53  }
54  masks.emplace_back(bools[j++]);
55  AssertDimension(j, unique_component_names.size());
56  return masks;
57  }
58 } // namespace
59 
60 
61 
63  const std::vector<std::string> & solution_names,
64  const std::vector<std::set<VectorTools::NormType>> &list_of_error_norms)
65  : ParsedConvergenceTable(solution_names,
66  list_of_error_norms,
67  2.0,
68  {"cells", "dofs"},
69  "dofs",
70  "reduction_rate_log2",
71  "",
72  3,
73  true)
74 {}
75 
76 
77 
79  const std::vector<std::string> & component_names,
80  const std::vector<std::set<VectorTools::NormType>> &list_of_error_norms,
81  const double & exponent,
82  const std::set<std::string> & extra_columns,
83  const std::string & rate_key,
84  const std::string & rate_mode,
85  const std::string & error_file_name,
86  const unsigned int & precision,
87  const bool & compute_error)
88  : component_names(component_names)
89  , unique_component_names(get_unique_component_names(component_names))
90  , unique_component_masks(get_unique_component_masks(component_names))
91  , norms_per_unique_component(list_of_error_norms)
92  , exponent(exponent)
93  , extra_columns(extra_columns)
94  , rate_key(rate_key)
95  , rate_mode(rate_mode)
96  , precision(precision)
97  , error_file_name(error_file_name)
98  , compute_error(compute_error)
99 {
101  unique_component_names.size());
102 }
103 
104 
105 
106 void
108 {
109  prm.add_parameter("Enable computation of the errors",
111  "When set to false, no computations are performed.");
112 
113  prm.add_parameter("Error precision",
114  precision,
115  "Number of digits to use when printing the error.",
116  Patterns::Integer(0));
117 
118  prm.add_parameter("Error file name",
120  "Set this to a filename with extension .txt, .gpl, .org, "
121  "or .tex to enable writing the convergence table to a "
122  "file.");
123 
124  prm.add_parameter(
125  "List of error norms to compute",
127  "Each component is separated by a semicolon "
128  "and each norm by a comma. See the documentation of VectorTools::NormType "
129  "for a list of implemented norms. If you want to skip a component, leave "
130  "its entry empty.");
131 
132 
133  prm.add_parameter("Exponent for p-norms",
134  exponent,
135  "The exponent to use when computing p-norms.",
136  Patterns::Double(1));
137 
138  prm.add_parameter("Extra columns",
140  "Extra columns to add to the table. Available options "
141  "are dofs and cells.",
142  Patterns::List(Patterns::Selection("dofs|cells")));
143 
144  prm.add_parameter("Rate key",
145  rate_key,
146  "Key to use when computing convergence rates. If "
147  "this is set to a column that is not present, or to the "
148  "empty string, then no error rates are computed.");
149 
150  prm.add_parameter("Rate mode",
151  rate_mode,
152  "What type of error rate to compute. Available options are "
153  "reduction_rate_log2, reduction_rate, and none.",
155  "reduction_rate|reduction_rate_log2|none"));
156 }
157 
158 
159 
160 void
162 {
163  if (compute_error)
164  {
165  // Add convergence rates if the rate_key is not empty
166  if (rate_key != "")
167  {
168  bool has_key = false;
169  for (const auto &col : extra_columns)
170  {
171  if (rate_key == col)
172  has_key = true;
173 
174  if (col != "")
176  }
177 
178  for (const auto &extra_col : extra_column_functions)
179  if (extra_col.second.second == false)
180  {
181  if (rate_key == extra_col.first)
182  has_key = true;
184  extra_col.first);
185  }
186 
187  if (has_key)
188  {
189  if (rate_mode == "reduction_rate_log2")
192  else if (rate_mode == "reduction_rate")
195  else
196  {
197  Assert(rate_mode != "none", ExcInternalError());
198  }
199  }
200  else
201  {
202  AssertThrow(rate_key != "",
203  ExcMessage(
204  "You specified the key <" + rate_key +
205  "> to compute convergence rates, but that key does "
206  "not exist in the current table."));
207  }
208  }
209  }
210 }
211 
212 
213 
214 void
216 {
217  if (compute_error)
218  {
220  table.write_text(out);
221  output_table();
222  }
223 }
224 
225 
226 
227 void
229 {
230  if (compute_error && error_file_name != "")
231  {
233 
234  const std::string error_file_format =
235  error_file_name.substr(error_file_name.find_last_of('.') + 1);
236 
237  std::ofstream table_file(error_file_name);
238 
239  if (error_file_format == "tex")
240  table.write_tex(table_file);
241  else if (error_file_format == "txt")
242  table.write_text(table_file);
243  else if (error_file_format == "gpl")
244  table.write_text(table_file,
246  else if (error_file_format == "org")
248  else
249  {
250  AssertThrow(false,
252  std::string("Unrecognized file format: ") +
253  error_file_format));
254  }
255  table_file.close();
256  }
257 }
258 
259 
260 
261 void
263  const std::string & column_name,
264  const std::function<double()> &custom_function,
265  const bool & compute_rate)
266 {
267  extra_column_functions[column_name] = {custom_function, compute_rate};
268 }
269 
270 DEAL_II_NAMESPACE_CLOSE
std::set< std::string > extra_columns
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1567
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}})
void add_parameters(ParameterHandler &prm)
#define AssertThrow(cond, exc)
Definition: exceptions.h:1519
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())
void evaluate_all_convergence_rates(const std::string &reference_column_key, const RateMode rate_mode)
std::vector< std::set< VectorTools::NormType > > norms_per_unique_component
static ::ExceptionBase & ExcMessage(std::string arg1)
void write_text(std::ostream &out, const TextOutputFormat format=table_with_headers) const
#define Assert(cond, exc)
Definition: exceptions.h:1407
void write_tex(std::ostream &file, const bool with_header=true) const
const std::vector< ComponentMask > unique_component_masks
const std::vector< std::string > unique_component_names
The ParsedConvergenceTable class.
void add_extra_column(const std::string &column_name, const std::function< double()> &custom_function, const bool &compute_rate=true)
std::map< std::string, std::pair< std::function< double()>, bool > > extra_column_functions
static ::ExceptionBase & ExcInternalError()