Hermes2D  2.0
proj_based_selector.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_REFINEMENT_PROJ_BASED_SELECTOR_H
17 #define __H2D_REFINEMENT_PROJ_BASED_SELECTOR_H
18 
19 #include "../global.h"
20 #include "optimum_selector.h"
21 
22 using namespace Hermes::Algebra::DenseMatrixOperations;
23 namespace Hermes
24 {
25  namespace Hermes2D
26  {
27  namespace RefinementSelectors {
29 
32 
34 
48  template<typename Scalar>
49  class HERMES_API ProjBasedSelector : public OptimumSelector<Scalar> {
50  protected:
51  class TrfShapeExp;
52 
53  public: //API
55  virtual ~ProjBasedSelector();
56 
58 
64  void set_error_weights(double weight_h = H2DRS_DEFAULT_ERR_WEIGHT_H, double weight_p = H2DRS_DEFAULT_ERR_WEIGHT_P, double weight_aniso = H2DRS_DEFAULT_ERR_WEIGHT_ANISO);
65 
66  double get_error_weight_h() const;
67  double get_error_weight_p() const;
68  double get_error_weight_aniso() const;
69 
71  typedef Hermes::vector<TrfShapeExp> TrfShape[H2D_TRF_NUM];
72 
75  TrfShape* cached_shape_vals;
76 
77  protected: //evaluated shape basis
79 
88  class TrfShapeExp {
89  public:
91  TrfShapeExp();
92 
94  virtual ~TrfShapeExp();
95 
97  const TrfShapeExp& operator = (const TrfShapeExp& other)
98  {
99  delete [] values; values = NULL;
100  if(other.values == NULL)
101  throw Exceptions::Exception("Unable to assign a non-empty values. Use references instead.");
102  return *this;
103  }
104  private:
105  int num_gip;
106  int num_expansion;
107  double** values;
108 
110 
112  void allocate(int num_expansion, int num_gip);
113 
115 
117  inline double* operator[](int inx_expansion);
118 
120 
121  inline bool empty() const;
122 
123  template<typename T> friend class ProjBasedSelector;
124  template<typename T> friend class L2ProjBasedSelector;
125  template<typename T> friend class H1ProjBasedSelector;
126  template<typename T> friend class HcurlProjBasedSelector;
127  template<typename T> friend class Adapt;
128  };
129 
131 
145  virtual void precalc_shapes(const double3* gip_points, const int num_gip_points, const Trf* trfs, const int num_noni_trfs, const Hermes::vector<typename OptimumSelector<Scalar>::ShapeInx>& shapes, const int max_shape_inx, TrfShape& svals, ElementMode2D mode) {};
146 
148 
165  virtual void precalc_ortho_shapes(const double3* gip_points, const int num_gip_points, const Trf* trfs, const int num_noni_trfs, const Hermes::vector<typename OptimumSelector<Scalar>::ShapeInx>& shapes, const int max_shape_inx, TrfShape& ortho_svals, ElementMode2D mode) {};
166 
167  protected:
169 
176  ProjBasedSelector(CandList cand_list, double conv_exp, int max_order, Shapeset* shapeset, const typename OptimumSelector<Scalar>::Range& vertex_order, const typename OptimumSelector<Scalar>::Range& edge_bubble_order);
177 
178  protected: //internal logic
180 
185 
186  protected: //error evaluation
187  static const int H2DRS_VALCACHE_INVALID = 0;
188  static const int H2DRS_VALCACHE_VALID = 1;
189  static const int H2DRS_VALCACHE_USER = 2;
190 
192  template<typename T>
193  struct ValueCacheItem {
195 
196  bool is_valid() const { return state != H2DRS_VALCACHE_INVALID; };
197 
199 
200  void mark(int new_state = H2DRS_VALCACHE_VALID) { state = new_state; };
201 
203 
204  void set(T new_value) { value = new_value; };
205 
207 
208  T get() const { return value; };
209 
211 
214  ValueCacheItem(const T& value = 0, const int state = H2DRS_VALCACHE_INVALID) : value(value), state(state) {};
215  private:
216  T value;
217  int state;
218  };
220 
221  typedef double** ProjMatrixCache[H2DRS_MAX_ORDER + 2][H2DRS_MAX_ORDER + 2];
222 
224 
229  ProjMatrixCache proj_matrix_cache[H2D_NUM_MODES];
230 
232 
237  Hermes::vector< ValueCacheItem<Scalar> > nonortho_rhs_cache;
238  Hermes::vector< ValueCacheItem<Scalar> > ortho_rhs_cache;
239 
240  double error_weight_h;
241  double error_weight_p;
243 
245 
246  virtual void evaluate_cands_error(Element* e, Solution<Scalar>* rsln, double* avg_error, double* dev_error);
247 
249 
262  virtual void calc_projection_errors(Element* e, const typename OptimumSelector<Scalar>::CandsInfo& info_h, const typename OptimumSelector<Scalar>::CandsInfo& info_p, const typename OptimumSelector<Scalar>::CandsInfo& info_aniso, Solution<Scalar>* rsln, CandElemProjError herr[H2D_MAX_ELEMENT_SONS], CandElemProjError perr, CandElemProjError anisoerr[H2D_MAX_ELEMENT_SONS]);
263 
265 
279  void calc_error_cand_element(const ElementMode2D mode, double3* gip_points, int num_gip_points, const int num_sub, Element** sub_domains, Trf** sub_trfs, Scalar*** sub_rvals, Hermes::vector<TrfShapeExp>** sub_nonortho_svals, Hermes::vector<TrfShapeExp>** sub_ortho_svals, const typename OptimumSelector<Scalar>::CandsInfo& info, CandElemProjError errors_squared);
280 
281  protected: //projection
283  struct ElemProj {
284  int* shape_inxs;
286  Hermes::vector<TrfShapeExp>& svals;
287  Scalar* shape_coeffs;
289  };
290 
292 
293  struct ElemGIP {
294  double3* gip_points;
296  Scalar** rvals;
297  };
298 
300  struct ElemSubTrf {
301  Trf* trf;
302  double coef_mx;
303  double coef_my;
304  };
305 
308  int inx;
310  };
311 
313 
325  virtual Scalar** precalc_ref_solution(int inx_son, Solution<Scalar>* rsln, Element* element, int intr_gip_order) = 0;
326 
328 
334  virtual double** build_projection_matrix(double3* gip_points, int num_gip_points, const int* shape_inx, const int num_shapes, ElementMode2D) = 0;
335 
337 
343  virtual Scalar evaluate_rhs_subdomain(Element* sub_elem, const ElemGIP& sub_gip, const ElemSubTrf& sub_trf, const ElemSubShapeFunc& sub_shape) = 0;
344 
346 
352  virtual double evaluate_error_squared_subdomain(Element* sub_elem, const ElemGIP& sub_gip, const ElemSubTrf& sub_trf, const ElemProj& elem_proj) = 0;
353  };
354  }
355  }
356 }
357 #endif