Programming tasks to Scientific Computing I
errorEstimator.h
Go to the documentation of this file.
1 #ifndef __ERRORESTIMATOR_H
2 #define __ERRORESTIMATOR_H
3 
11 #include <typeinfo>
12 #include <cxxabi.h>
13 
14 #include <Eigen/Dense>
15 
19 template<typename ConfiguratorType>
21 {
22 public:
25  // Strangely, lpNorm fails if this is set to
26  // "typename ConfiguratorType::VectorType". Bug?
27  typedef typename Eigen::VectorXd VectorType;
28  typedef typename Eigen::Matrix<RealType, 3, 1> VecType;
29 
30  ErrorEstimator(MeshType &grid, RealType gamma = 0.4)
31  : _mesh(grid), _gamma(gamma)
32  {}
33 
35  RealType err = markTriangles();
36  _mesh.refineMarkedTriangles();
37  return err;
38  }
39 
40  RealType markTriangles()
41  {
42  VectorType errEstimates(_mesh.getNumTriangs());
43 
44  computeLocalErrorEstimates(errEstimates);
45  RealType refineAbove = errEstimates.lpNorm<Eigen::Infinity>() * _gamma;
46 
47  for (int t = 0; t < _mesh.getNumTriangs(); ++t)
48  if (errEstimates[t] > refineAbove)
49  _mesh.mark(t);
50 
51  return errEstimates.norm();
52  }
53 
54 protected:
55  RealType f (const typename ConfiguratorType::Point3DType &x) const
56  {
57  RealType a = 0.8;
58  RealType b = 0.0;
59  int r = 0;
60 
61  for(int i = 0; i < 4; i++) {
62  r = r || ( (x[1] > b) && (fabs(x[0]) + fabs(x[1]-b) < a) );
63  b += a *= .75;
64  }
65 
66  return 2.0 * static_cast<RealType>(r);
67  }
68 
69  void computeLocalErrorEstimates(VectorType &errEstimates) const
70  {
71  for (int t = 0; t < _mesh.getNumTriangs(); ++t) {
72  RealType h = _mesh.getTriang(t).getEdge(_mesh.getLongestEdgeIndex(t)).norm();
73  RealType J_t = jumpResidual(t);
74  errEstimates[t] = sqrt(h) * J_t;
75  }
76  }
77 
78  RealType jumpResidual(int t) const
79  {
80  RealType Jt = 0.0;
81 
82  RealType val = getMidpointValue(t);
83  for (int e = 0; e < 3; ++e) {
84  int neighbour = _mesh.getNeighbour(t, e);
85  // If no neighbour
86  if (neighbour == -1)
87  continue;
88 
89  RealType nVal = getMidpointValue(neighbour);
90  Jt += fabs(val - nVal);
91  }
92 
93  return Jt;
94  }
95 
96  RealType getMidpointValue(int t) const {
98 
99  RealType res = 0.0;
100  for (int i = 0; i < 3; ++i) {
101  res += f(el.getNode(i));
102  }
103  res /= 3.0;
104 
105  return res;
106  }
107 
108  MeshType &_mesh;
109  RealType _gamma;
110 };
111 
112 #endif /* __ERRORESTIMATOR_H */
RealType f(const typename ConfiguratorType::Point3DType &x) const
DataTypeContainer::Point3DType Point3DType
const TriangleType & getTriang(const int num) const
Definition: triangMesh.h:87
ErrorEstimator(MeshType &grid, RealType gamma=0.4)
RealType jumpResidual(int t) const
Class for local error estimation.
int getNeighbour(const int elementID, const int acrossLocalEdge) const
Get neighbor on edge.
Definition: triangMesh.h:76
DataTypeContainer::RealType RealType
ConfiguratorType::InitType MeshType
RealType markTriangles()
RealType getMidpointValue(int t) const
int getNumTriangs() const
Definition: triangMesh.h:46
Eigen::VectorXd VectorType
RealType markAndRefineTriangles()
const Point3DType & getNode(int i) const
Access node i.
MeshType & _mesh
ConfiguratorType::RealType RealType
Eigen::Matrix< RealType, 3, 1 > VecType
Triangle which has a tangent space at each node.
const TangentVecType & getEdge(int i) const
Access edge i.
void computeLocalErrorEstimates(VectorType &errEstimates) const