16 #ifndef _d2k_error_handler_h 17 #define _d2k_error_handler_h 19 #include <deal2lkit/config.h> 44 DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
46 DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
81 template <
int ntables=1>
88 const std::string solution_names =
"u",
89 const std::string list_of_error_norms =
"Linfty, L2, H1");
99 template<
typename DH,
typename VEC>
100 void error_from_exact(
const DH &vspace,
103 unsigned int table_no = 0,
108 template<
typename DH,
typename VEC>
113 unsigned int table_no = 0,
127 template<
typename DH>
128 void custom_error(
const std::function<
double(
const unsigned int component)> &custom_error_function,
130 const std::string &error_name=
"custom",
131 const bool add_table_extras =
false,
132 const unsigned int table_no = 0,
136 template<
typename DH,
typename VEC>
137 void difference(
const DH &,
const VEC &,
138 const DH &,
const VEC &,
139 unsigned int table_no = 0,
double dt=0.);
142 template<
typename DH,
typename VEC>
143 void difference(
const DH &,
const VEC &,
144 const VEC &,
unsigned int table_no = 0,
double dt=0.);
147 void output_table(std::ostream &out=std::cout,
const unsigned int table_no=0);
178 std::vector<std::vector<NormFlags> >
types;
200 std::vector<std::map<std::string, bool> >
extras;
222 static_cast<unsigned int> (f1) |
223 static_cast<unsigned int> (f2));
252 static_cast<unsigned int> (f1) &
253 static_cast<unsigned int> (f2));
274 template <
int ntables>
275 template<
typename DH,
typename VECTOR>
277 const VECTOR &solution1,
278 const VECTOR &solution2,
279 unsigned int table_no,
283 solution1.size(), solution2.size()));
284 VECTOR solution(solution1);
285 solution -= solution2;
286 error_from_exact(dh, solution,
292 template <
int ntables>
293 template<
typename DH,
typename VECTOR>
295 const VECTOR &solution,
297 unsigned int table_no,
301 dh, solution, exact, table_no, dt);
304 template <
int ntables>
305 template<
typename DH,
typename VECTOR>
308 const VECTOR &solution,
310 unsigned int table_no,
313 const int dim=DH::dimension;
314 const int spacedim=DH::space_dimension;
322 std::vector< std::vector<double> > error( exact.
n_components, std::vector<double>(4));
323 const unsigned int n_active_cells = dh.get_triangulation().n_global_active_cells();
324 const unsigned int n_dofs=dh.n_dofs();
326 if (extras[table_no][
"cells"])
328 tables[table_no].add_value(
"cells", n_active_cells);
329 tables[table_no].set_tex_caption(
"cells",
"\\# cells");
330 tables[table_no].set_tex_format(
"cells",
"r");
332 if (extras[table_no][
"dofs"])
334 tables[table_no].add_value(
"dofs", n_dofs);
335 tables[table_no].set_tex_caption(
"dofs",
"\\# dofs");
336 tables[table_no].set_tex_format(
"dofs",
"r");
338 if (extras[table_no][
"dt"])
340 tables[table_no].add_value(
"dt", dt);
341 tables[table_no].set_tex_caption(
"dt",
"\\Delta t");
342 tables[table_no].set_tex_format(
"dt",
"r");
345 bool compute_Linfty =
false;
346 bool compute_L2 =
false;
347 bool compute_W1infty =
false;
348 bool compute_H1 =
false;
349 bool add_this =
false;
351 unsigned int last_non_add = 0;
353 for (
unsigned int component=0; component < exact.
n_components; ++component)
360 Vector<float> difference_per_cell (dh.get_triangulation().n_global_active_cells());
365 add_this = (norm &
AddUp);
369 last_non_add = component;
370 compute_L2 = ( norm &
L2 );
371 compute_H1 = ( norm &
H1 );
372 compute_W1infty = ( norm &
W1infty ) ;
373 compute_Linfty = ( norm &
Linfty );
386 VectorTools::L2_norm,
390 const double L2_error = difference_per_cell.l2_norm();
391 difference_per_cell = 0;
402 VectorTools::H1_norm,
405 const double H1_error = difference_per_cell.l2_norm();
406 difference_per_cell = 0;
417 VectorTools::W1infty_norm,
421 const double W1inf_error = difference_per_cell.linfty_norm();
432 VectorTools::Linfty_norm,
436 const double Linf_error = difference_per_cell.linfty_norm();
442 error[last_non_add][0] = std::max(error[last_non_add][0], Linf_error);
443 error[last_non_add][1] += L2_error;
444 error[last_non_add][2] = std::max(error[last_non_add][2], W1inf_error);
445 error[last_non_add][3] += H1_error;
451 error[component][0] = Linf_error;
452 error[component][1] = L2_error;
453 error[component][2] = W1inf_error;
454 error[component][3] = H1_error;
463 if ( !(norm &
AddUp) )
467 std::string name = headers[j] +
"_Linfty";
468 std::string latex_name =
"$\\| " +
469 latex_headers[j] +
" - " +
470 latex_headers[j] +
"_h \\|_\\infty $";
471 double this_error = error[j][0];
473 tables[table_no].add_value(name, this_error);
474 tables[table_no].set_precision(name, precision);
475 tables[table_no].set_scientific(name,
true);
476 tables[table_no].set_tex_caption(name, latex_name);
481 std::string name = headers[j] +
"_L2";
482 std::string latex_name =
"$\\| " +
483 latex_headers[j] +
" - " +
484 latex_headers[j] +
"_h \\|_0 $";
485 double this_error = error[j][1];
487 tables[table_no].add_value(name, this_error);
488 tables[table_no].set_precision(name, precision);
489 tables[table_no].set_scientific(name,
true);
490 tables[table_no].set_tex_caption(name, latex_name);
494 std::string name = headers[j] +
"_W1infty";
495 std::string latex_name =
"$\\| " +
496 latex_headers[j] +
" - " +
497 latex_headers[j] +
"_h \\|_{1,\\infty} $";
498 double this_error = error[j][2];
500 tables[table_no].add_value(name, this_error);
501 tables[table_no].set_precision(name, precision);
502 tables[table_no].set_scientific(name,
true);
503 tables[table_no].set_tex_caption(name, latex_name);
507 std::string name = headers[j] +
"_H1";
508 std::string latex_name =
"$\\| " +
509 latex_headers[j] +
" - " +
510 latex_headers[j] +
"_h \\|_1 $";
511 double this_error = error[j][3];
513 tables[table_no].add_value(name, this_error);
514 tables[table_no].set_precision(name, precision);
515 tables[table_no].set_scientific(name,
true);
516 tables[table_no].set_tex_caption(name, latex_name);
525 template <
int ntables>
526 template<
typename DH>
529 const std::string &error_name,
530 const bool add_table_extras,
531 const unsigned int table_no,
539 const unsigned int n_components =
types.size();
540 std::vector<double> c_error(
types[table_no].size() );
541 const unsigned int n_active_cells = dh.get_triangulation().n_global_active_cells();
542 const unsigned int n_dofs=dh.n_dofs();
543 if (add_table_extras)
545 if (extras[table_no][
"cells"])
547 tables[table_no].add_value(
"cells", n_active_cells);
548 tables[table_no].set_tex_caption(
"cells",
"\\# cells");
549 tables[table_no].set_tex_format(
"cells",
"r");
551 if (extras[table_no][
"dofs"])
553 tables[table_no].add_value(
"dofs", n_dofs);
554 tables[table_no].set_tex_caption(
"dofs",
"\\# dofs");
555 tables[table_no].set_tex_format(
"dofs",
"r");
557 if (extras[table_no][
"dt"])
559 tables[table_no].add_value(
"dt", dt);
560 tables[table_no].set_tex_caption(
"dt",
"\\Delta t");
561 tables[table_no].set_tex_format(
"dt",
"r");
565 bool add_this =
false;
566 bool compute_Custom =
false;
568 unsigned int last_non_add = 0;
570 for (
unsigned int component=0; component < n_components; ++component)
575 add_this = (norm &
AddUp);
578 compute_Custom = ( norm &
Custom );
581 double Custom_error = 0;
584 Custom_error = custom_error_function(component);
590 c_error[last_non_add] += Custom_error;
594 c_error[last_non_add] = Custom_error;
598 for (
unsigned int j=0; j<n_components; ++j)
602 if ( !(norm &
AddUp) )
606 std::string name = headers[j] +
"_" +error_name;
607 std::string latex_name =
"$\\| " +
608 latex_headers[j] +
" - " +
609 latex_headers[j] +
"_h \\|_{\text{"+error_name+
"} $";
610 double this_error = c_error[j];
612 tables[table_no].add_value(name, this_error);
613 tables[table_no].set_precision(name, precision);
614 tables[table_no].set_scientific(name,
true);
615 tables[table_no].set_tex_caption(name, latex_name);
void custom_error(const std::function< double(const unsigned int component)> &custom_error_function, const DH &dh, const std::string &error_name="custom", const bool add_table_extras=false, const unsigned int table_no=0, const double dt=0.)
Call the given custom function to compute the custom error for the given component and store the resu...
A parameter acceptor base class.
NormFlags & operator|=(NormFlags &f1, NormFlags f2)
Global operator which sets the bits from the second argument also in the first one.
bool compute_error
Compute the error.
std::vector< ConvergenceTable > tables
Error results.
static::ExceptionBase & ExcNotInitialized()
#define AssertThrow(cond, exc)
NormFlags & operator&=(NormFlags &f1, NormFlags f2)
Global operator which clears all the bits in the first argument if they are not also set in the secon...
static::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
NormFlags operator&(NormFlags f1, NormFlags f2)
Global operator which returns an object in which all bits are set which are set in the first as well ...
void difference(const DH &, const VEC &, const DH &, const VEC &, unsigned int table_no=0, double dt=0.)
Difference between two solutions in two different vector spaces.
const unsigned int n_components
std::vector< std::vector< NormFlags > > types
Type of error to compute per components.
static::ExceptionBase & ExcMessage(std::string arg1)
const std::string list_of_error_norms
List of error norms to compute.
unsigned int precision
The precision with which the table is written.
std::string error_file_format
The error file format.
static::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
bool output_error
Output the error file also on screen.
std::vector< std::string > latex_headers
Headers for latex tables.
std::vector< std::string > headers
Headers for tables and output.
bool initialized
The parameters have been read.
std::vector< std::map< std::string, bool > > extras
The extra column to add to the tables.
double norm(const FEValuesBase< dim > &fe, const VectorSlice< const std::vector< std::vector< Tensor< 1, dim > > > > &Du)
std::vector< std::string > rate_keys
Wether or not to calculate the rates according to the given keys.
std::vector< std::string > names
Names of the tables.
void error_from_exact(const DH &vspace, const VEC &solution, const Function< DH::space_dimension > &exact, unsigned int table_no=0, double dt=0.)
Calculate the error of the numeric solution in variuous norms.
NormFlags operator|(NormFlags f1, NormFlags f2)
Global operator which returns an object in which all bits are set which are either set in the first o...
std::vector< std::string > latex_captions
Captions for latex.
std::vector< bool > add_rates
Add convergence rates.
bool write_error
Write the error files.
const std::string solution_names
Value of solution names.