Hermes2D  2.0
adapt.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_ADAPT_H
17 #define __H2D_ADAPT_H
18 
19 #include "../forms.h"
20 #include "../space/space.h"
21 #include "../weakform/weakform.h"
22 #include "../integrals/h1.h"
23 #include "../integrals/hcurl.h"
24 #include "../integrals/hdiv.h"
25 #include "../integrals/l2.h"
26 #include "../mesh/element_to_refine.h"
27 #include "../refinement_selectors/selector.h"
28 #include "exceptions.h"
29 #include "../global.h"
30 
31 namespace Hermes
32 {
33  namespace Hermes2D
34  {
49  template<typename Scalar> class Global;
50 
52 
56  template<typename Scalar>
57  class HERMES_API Adapt : public Hermes::Mixins::TimeMeasurable, public Hermes::Mixins::Loggable
58  {
59  public:
62  Adapt(Hermes::vector<Space<Scalar>*> spaces, Hermes::vector<ProjNormType> proj_norms = Hermes::vector<ProjNormType>());
63  Adapt(Space<Scalar>* space, ProjNormType proj_norm = HERMES_UNSET_NORM);
64  virtual ~Adapt();
65 
67  class HERMES_API MatrixFormVolError : public MatrixFormVol<Scalar>
68  {
69  public:
71  MatrixFormVolError(int i, int j);
73  MatrixFormVolError(int i, int j, ProjNormType type);
74 
76  virtual Scalar value(int n, double *wt, Func<Scalar> *u_ext[],
78  Func<Scalar> **ext) const;
79 
81  virtual Hermes::Ord ord(int n, double *wt, Func<Hermes::Ord> *u_ext[],
83  Func<Ord> **ext) const;
84 
85  virtual MatrixFormVol<Scalar>* clone() const;
86 
87  protected:
90 
92  template<typename TestFunctionDomain, typename SolFunctionDomain>
93  static SolFunctionDomain l2_error_form(int n, double *wt, Func<SolFunctionDomain> *u_ext[], Func<SolFunctionDomain> *u,
95 
97  template<typename TestFunctionDomain, typename SolFunctionDomain>
98  static SolFunctionDomain h1_error_form(int n, double *wt, Func<SolFunctionDomain> *u_ext[], Func<SolFunctionDomain> *u,
100 
102  template<typename TestFunctionDomain, typename SolFunctionDomain>
103  static SolFunctionDomain h1_error_semi_form(int n, double *wt, Func<SolFunctionDomain> *u_ext[], Func<SolFunctionDomain> *u,
105 
107  template<typename TestFunctionDomain, typename SolFunctionDomain>
108  static SolFunctionDomain hdiv_error_form(int n, double *wt, Func<SolFunctionDomain> *u_ext[], Func<SolFunctionDomain> *u,
110 
112  template<typename TestFunctionDomain, typename SolFunctionDomain>
113  static SolFunctionDomain hcurl_error_form(int n, double *wt, Func<SolFunctionDomain> *u_ext[], Func<SolFunctionDomain> *u,
115  };
116 
118 
124  void set_error_form(int i, int j, MatrixFormVolError* form);
125  void set_error_form(MatrixFormVolError* form);
126 
127  void set_norm_form(int i, int j, MatrixFormVolError* form);
128  void set_norm_form(MatrixFormVolError* form);
129 
132  double calc_err_est(Solution<Scalar>*sln, Solution<Scalar>*rsln, bool solutions_for_adapt = true,
133  unsigned int error_flags = HERMES_TOTAL_ERROR_REL | HERMES_ELEMENT_ERROR_REL);
134 
139  double calc_err_est(Hermes::vector<Solution<Scalar>*> slns, Hermes::vector<Solution<Scalar>*> rslns,
140  Hermes::vector<double>* component_errors = NULL, bool solutions_for_adapt = true,
141  unsigned int error_flags = HERMES_TOTAL_ERROR_REL | HERMES_ELEMENT_ERROR_REL);
142 
145  double calc_err_exact(Solution<Scalar>*sln, Solution<Scalar>*rsln, bool solutions_for_adapt = true,
146  unsigned int error_flags = HERMES_TOTAL_ERROR_REL | HERMES_ELEMENT_ERROR_REL);
147 
150  double calc_err_exact(Hermes::vector<Solution<Scalar>*> slns, Hermes::vector<Solution<Scalar>*> rslns,
151  Hermes::vector<double>* component_errors = NULL, bool solutions_for_adapt = true,
152  unsigned int error_flags = HERMES_TOTAL_ERROR_REL | HERMES_ELEMENT_ERROR_REL);
153 
155 
163  bool adapt(Hermes::vector<RefinementSelectors::Selector<Scalar>*> refinement_selectors, double thr, int strat = 0,
164  int regularize = -1, double to_be_processed = 0.0);
165 
167 
175  bool adapt(RefinementSelectors::Selector<Scalar>* refinement_selector, double thr, int strat = 0,
176  int regularize = -1, double to_be_processed = 0.0);
177 
179 
182  double get_element_error_squared(int component, int id) const;
183  protected:
184 
185  Exceptions::Exception* caughtException;
186 
189  int id;
190  int comp;
191  ElementReference(int id = -1, int comp = -1) : id(id), comp(comp) {};
192  };
193 
195 
196  const Hermes::vector<ElementReference>& get_regular_queue() const;
197 
199 
200  void apply_refinement(const ElementToRefine& elem_ref);
201 
203 
204  virtual void apply_refinements(std::vector<ElementToRefine>& elems_to_refine);
205 
207 
208  const std::vector<ElementToRefine>& get_last_refinements() const;
209 
210  std::queue<ElementReference> priority_queue;
211  Hermes::vector<ElementReference> regular_queue;
212  std::vector<ElementToRefine> last_refinements;
213 
215 
220  void fix_shared_mesh_refinements(Mesh** meshes, std::vector<ElementToRefine>& elems_to_refine, int** idx,
221  RefinementSelectors::Selector<Scalar>*** refinement_selectors);
222 
224 
225  void homogenize_shared_mesh_orders(Mesh** meshes);
226 
227  int num;
228  Hermes::vector<Space<Scalar>*> spaces;
229  bool **own_forms;
231  Solution<Scalar>* sln[H2D_MAX_COMPONENTS];
232  Solution<Scalar>* rsln[H2D_MAX_COMPONENTS];
233  bool have_errors;
236 
237  double* errors[H2D_MAX_COMPONENTS];
238 
240 
241  double error_time;
242 
243  static const unsigned char HERMES_TOTAL_ERROR_MASK = 0x0F;
244  static const unsigned char HERMES_ELEMENT_ERROR_MASK = 0xF0;
245 
246  MatrixFormVolError* error_form[H2D_MAX_COMPONENTS][H2D_MAX_COMPONENTS];
247  MatrixFormVolError* norm_form[H2D_MAX_COMPONENTS][H2D_MAX_COMPONENTS];
248 
250 
253  virtual double calc_err_internal(Hermes::vector<Solution<Scalar>*> slns, Hermes::vector<Solution<Scalar>*> rslns,
254  Hermes::vector<double>* component_errors, bool solutions_for_adapt, unsigned int error_flags);
255 
257  virtual double calc_err_internal(Solution<Scalar>* sln, Solution<Scalar>* rsln,
258  Hermes::vector<double>* component_errors, bool solutions_for_adapt, unsigned int error_flags);
259 
261 
274  virtual double eval_error(MatrixFormVolError* form,
276 
278 
286  virtual double eval_error_norm(MatrixFormVolError* form,
288 
290 
295  virtual void fill_regular_queue(const Mesh** meshes);
296 
297  private:
299  class CompareElements
300  {
301  private:
302  double** errors;
303  public:
304  CompareElements(double** errors);
305 
306 
309  bool operator ()(const ElementReference& e1, const ElementReference& e2) const;
310  };
311  };
312  }
313 }
314 #endif