Hermes2D  3.0
error_calculator.h
1 // This file is part of Hermes2D.
2 //
3 // Hermes2D is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // Hermes2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Hermes2D. If not, see <http://www.gnu.org/licenses/>.
15 
16 #ifndef __H2D_ERROR_CALCULATOR_H
17 #define __H2D_ERROR_CALCULATOR_H
18 
19 #include "../weakform/weakform.h"
20 #include "norm_form.h"
21 #include "error_thread_calculator.h"
22 
23 namespace Hermes
24 {
25  namespace Hermes2D
26  {
27  template<typename Scalar> class ErrorThreadCalculator;
28 
31  {
32  AbsoluteError,
33  RelativeErrorToElementNorm,
34  RelativeErrorToGlobalNorm
35  };
36 
38 
40  template<typename Scalar>
41  class HERMES_API ErrorCalculator :
42  public Hermes::Mixins::TimeMeasurable,
43  public Hermes::Mixins::Loggable,
45  public Hermes::Mixins::StateQueryable
46  {
47  public:
50  ErrorCalculator(CalculatedErrorType errorType);
51 
55  void calculate_errors(std::vector<MeshFunctionSharedPtr<Scalar> > coarse_solutions, std::vector<MeshFunctionSharedPtr<Scalar> > fine_solutions, bool sort_and_store = true);
56 
59  void calculate_errors(MeshFunctionSharedPtr<Scalar> coarse_solution, MeshFunctionSharedPtr<Scalar> fine_solution, bool sort_and_store = true);
60 
62  virtual ~ErrorCalculator();
63 
66  void add_error_form(NormFormVol<Scalar>* form);
67  void add_error_form(NormFormSurf<Scalar>* form);
68  void add_error_form(NormFormDG<Scalar>* form);
69 
71 
74  double get_element_error_squared(int component, int element_id) const;
75  double get_element_norm_squared(int component, int element_id) const;
76  double get_error_squared(int component) const;
77  double get_norm_squared(int component) const;
78  double get_total_error_squared() const;
79  double get_total_norm_squared() const;
80 
81  int get_component_count() const { return this->component_count; }
82 
86  ElementReference() : element_id(-1), comp(-1), error(nullptr), norm(nullptr) {};
87  ElementReference(int comp, int element_id, double* error, double* norm) : element_id(element_id), comp(comp), error(error), norm(norm) {};
88 
90  int element_id;
92  int comp;
94  double* error;
96  double* norm;
97  };
98 
100  const ElementReference& get_element_reference(unsigned int id) const;
101 
104  MeshFunctionSharedPtr<double> get_errorMeshFunction(int component = 0);
105 
107  void free();
108 
109  protected:
111  virtual bool isOkay() const;
112  inline std::string getClassName() const { return "ErrorCalculator"; }
113 
115  bool data_prepared_for_querying() const;
116 
118  void init_data_storage();
119 
122  void postprocess_error();
123 
125  std::vector<MeshFunctionSharedPtr<Scalar> > coarse_solutions;
126  std::vector<MeshFunctionSharedPtr<Scalar> > fine_solutions;
127 
130 
133 
136 
139 
141  double* errors[H2D_MAX_COMPONENTS];
142  double* norms[H2D_MAX_COMPONENTS];
143  double component_errors[H2D_MAX_COMPONENTS];
144  double component_norms[H2D_MAX_COMPONENTS];
145  int element_count[H2D_MAX_COMPONENTS];
146  double errors_squared_sum;
147  double norms_squared_sum;
148 
150  MeshFunctionSharedPtr<double> errorMeshFunction[H2D_MAX_COMPONENTS];
151 
153  std::vector<NormFormVol<Scalar> *> mfvol;
155  std::vector<NormFormSurf<Scalar> *> mfsurf;
157  std::vector<NormFormDG<Scalar> *> mfDG;
158 
161 
162  static int compareElementReference(const void * a, const void * b)
163  {
164  ElementReference* ref_a = (ElementReference*)(a);
165  ElementReference* ref_b = (ElementReference*)(b);
166  if (*((*ref_a).error) > *((*ref_b).error))
167  return -1;
168  else
169  return 1;
170  };
171 
172  friend class Adapt < Scalar > ;
173  friend class ErrorThreadCalculator < Scalar > ;
174  };
175 
176  template<typename Scalar, NormType normType>
177  class HERMES_API DefaultErrorCalculator : public ErrorCalculator < Scalar >
178  {
179  public:
180  DefaultErrorCalculator(CalculatedErrorType errorType, int component_count);
181  virtual ~DefaultErrorCalculator();
182  };
183 
184  template<typename Scalar, NormType normType>
185  class HERMES_API DefaultNormCalculator : public ErrorCalculator < Scalar >
186  {
187  public:
188  DefaultNormCalculator(int component_count);
189  virtual ~DefaultNormCalculator();
190 
192  double calculate_norms(std::vector<MeshFunctionSharedPtr<Scalar> >& solutions);
194  double calculate_norm(MeshFunctionSharedPtr<Scalar> solution);
195  };
196  }
197 }
198 #endif
std::vector< NormFormDG< Scalar > * > mfDG
Holds DG matrix forms.
ElementReference * element_references
A queue of elements which should be processes. The queue had to be filled by the method fill_regular_...
Definition: adapt.h:24
Class utilizes parallel calculation.
Definition: mixins2d.h:37
std::vector< NormFormVol< Scalar > * > mfvol
Holds volumetric matrix forms.
Containc class that calculates the norm.
std::vector< NormFormSurf< Scalar > * > mfsurf
Holds surface matrix forms.
CalculatedErrorType errorType
Absolute / Relative error.
int num_act_elems
A total number of active elements across all provided meshes.
bool elements_stored
This is for adaptivity, saying that the errors are the correct ones.
int comp
A component which this element belongs to. Invalid if below 0.
std::vector< MeshFunctionSharedPtr< Scalar > > coarse_solutions
Data.
int component_count
Number of solution components.
CalculatedErrorType
Enum passed to the class ErrorCalculator specifying the calculated errors.
Evaluation of an error between a (coarse) solution and a reference solution.
Definition: adapt.h:28
::xsd::cxx::tree::string< char, simple_type > string
C++ type corresponding to the string XML Schema built-in type.
::xsd::cxx::tree::error< char > error
Error condition.
ElementReference()
Constructor. It creates an invalid element reference.
double * error
Pointer to the final error, respecting the errorType.