Error.hh 7.53 KB
Newer Older
1
2
3
#include "Mesh.h"
#include "Parametric.h"
#include "Quadrature.h"
4
#include "Traverse.h"
5
6
7
8

namespace AMDiS {

  template<typename T>
9
  T Error<T>::errUFct(const DimVec<double>& lambda)
10
11
  {
    WorldVector<double> x;
12
13
    elinfo->coordToWorld(lambda, x);
    return ((*pU)(x));
14
15
16
  }

  template<typename T>
17
  WorldVector<T> Error<T>::grdErrUFct(const DimVec<double>& lambda)
18
19
  {
    WorldVector<double> x;
20
21
    elinfo->coordToWorld(lambda, x);
    return ((*pGrdU)(x));
22
23
24
25
26
27
28
  }

  template<typename T>
  double Error<T>::maxErrAtQp(const AbstractFunction<T, WorldVector<double> >& u,
			      const DOFVector<T>& uh,
			      const Quadrature* q)
  {
29
    FUNCNAME("Error<T>::maxErrAtQp()");
30

31
    const FiniteElemSpace *fe_space;
32
33
34
35
    if (!(pU = &u)) {
      ERROR("no function u specified; doing nothing\n");
      return(-1.0);
    }
36
37
    if (!(errUh = &uh) || !(fe_space = uh->getFESpace())) {
      ERROR("no discrete function or no fe_space for it; doing nothing\n");
38
39
      return(-1.0);
    }
40
    if (!(basFct = fe_space->getBasisFcts())) {
41
42
43
      ERROR("no basis functions at discrete solution ; doing nothing\n");
      return(-1.0);
    }
44
45

    if (!q)
46
47
      q = Quadrature::provideQuadrature(fe_space->getMesh()->getDim(),
					2 * fe_space->getBasisFcts()->getDegree() -  2);
48

49
50
51
    quadFast = FastQuadrature::provideFastQuadrature(basFct, *q, INIT_PHI);
    double maxErr = 0.0;
    TraverseStack stack;
52
53
    ElInfo *elInfo = stack.traverseFirst(fe_space->getMesh(), -1,
					 Mesh::FILL_COORDS | Mesh::CALL_LEAF_EL);
54
    while (elInfo) {
Thomas Witkowski's avatar
Thomas Witkowski committed
55
      elinfo = elInfo;
56
57
58
59
60
61
62
63
64
65
      double err = 0.0;
      const double *u_vec, *uh_vec;
      
      u_vec = quadFast->getQuadrature()->fAtQp(errU, NULL);
      uh_vec = errUh->getVecAtQPs(elInfo, NULL, quadFast, NULL);
      
      int nPoints = quadFast->getNumPoints();
      for (int i = 0; i < nPoints; i++) {
	err = u_vec[i] > uh_vec[i] ? u_vec[i] - uh_vec[i] : uh_vec[i] - u_vec[i];
	maxErr = max(maxErr, err);
66
67
      }

68
      elInfo = stack.traverseNext(elInfo);
69
70
    }

71
    return maxErr;
72
73
74
  }

  template<typename T>
75
76
77
78
  double Error<T>::H1Err(const AbstractFunction<WorldVector<T>, WorldVector<double> > &grdU,
			 const DOFVector<T> &uh,
			 int relErr,
			 double* max,
79
80
81
			 bool writeLeafData,
			 int comp)
  {
82
83
    FUNCNAME("Error<T>::H1Err()");

84
    const FiniteElemSpace *fe_space;
85
86
87
88
89
90
    writeInLeafData = writeLeafData;
    component = comp;
    Quadrature *q = NULL;
    pGrdU = &grdU;
    errUh = &uh;

91
92
    if (!(fe_space = uh.getFESpace())) {
      ERROR("no fe_space for uh; doing nothing\n");
93
94
      return(0.0);
    }
95
    if (!(basFct = fe_space->getBasisFcts())) {
96
97
98
      ERROR("no basis functions at discrete solution ; doing nothing\n");
      return(0.0);
    }
99

100
    int dim = fe_space->getMesh()->getDim();
101
    int deg = grdU.getDegree();
102
    int degree = deg ? deg : 2 * fe_space->getBasisFcts()->getDegree() - 2;
103
104
105
106
107
108

    q = Quadrature::provideQuadrature(dim, degree);
    quadFast = FastQuadrature::provideFastQuadrature(basFct, 
						     *q, 
						     INIT_GRD_PHI);

109
110
111
112
113
    double relative = relErr;
    double maxErr = 0.0, h1Err2 = 0.0, h1Norm2 = 0.0;


    TraverseStack stack;
114
    ElInfo *elInfo = stack.traverseFirst(fe_space->getMesh(), -1,
115
116
117
118
119
					 Mesh::FILL_COORDS |
					 Mesh::CALL_LEAF_EL |
					 Mesh::FILL_DET | 
					 Mesh::FILL_GRD_LAMBDA);
    while (elInfo) {
Thomas Witkowski's avatar
Thomas Witkowski committed
120
      elinfo = elInfo;
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
      int i, j;
      double err, err_2, h1_err_el, norm_el, norm2, det, exact;
      const WorldVector<double> *grdu_vec, *grduh_vec;
      
      grdu_vec = quadFast->getQuadrature()->grdFAtQp(grdErrU, NULL);
      det = elInfo->getDet();
      grduh_vec = errUh->getGrdAtQPs(elinfo, NULL, quadFast, NULL);
      
      int nPoints = quadFast->getNumPoints();
      int dow = Global::getGeo(WORLD);
      
      for (h1_err_el = i = 0; i < nPoints; i++) {
	for (err_2 = j = 0; j < dow; j++) {
	  err = grdu_vec[i][j] - grduh_vec[i][j];
	  err_2 += sqr(err);
	}
	h1_err_el += quadFast->getWeight(i)*err_2;
      }
      
      exact = det*h1_err_el;
      h1Err2 += exact;
      maxErr = std::max(maxErr, exact);
      
      if (writeInLeafData)
	elInfo->getElement()->setEstimation(exact, component);
      
      if (relative) {
	for (norm_el = i = 0; i < nPoints; i++) {
	  for (norm2 = j = 0; j < dow; j++) 
	    norm2 += sqr(grdu_vec[i][j]);
	  norm_el += quadFast->getWeight(i)*norm2;
	}
	h1Norm2 += det*norm_el;
      }
155

156
157
      elInfo = stack.traverseNext(elInfo);
    }
158
159

    if (relative) {
160
161
      double relNorm2 = h1Norm2 + 1.e-15;

162
      elInfo = stack.traverseFirst(fe_space->getMesh(), -1, Mesh::CALL_LEAF_EL);
163
164
165
166
167
168
      while (elInfo) {
	double exact = elInfo->getElement()->getEstimation(component) / relNorm2;
	if (writeInLeafData)
	  elinfo->getElement()->setEstimation(exact, component);
	elInfo = stack.traverseNext(elInfo);
      }
169
170
171
172
173

      h1Err2 /= relNorm2;
      maxErr /= relNorm2;
    }

174
175
    if (max)
      *max = maxErr;
176

177
    return sqrt(h1Err2);
178
179
180
181
182
183
184
185
186
187
  }

  template<typename T>
  double Error<T>::L2Err(const AbstractFunction<T, WorldVector<double> >& u,
			 const DOFVector<T>& uh,
			 int relErr,
			 double* max,
			 bool writeLeafData,
			 int comp)
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
188
    FUNCNAME("Error<T>::L2Err()");
189

190
    const FiniteElemSpace *fe_space;
191
192
193
194
195
196
197
198
199
    Quadrature *q = NULL;
    writeInLeafData = writeLeafData;
    component = comp;

    if (!(pU = &u)) {
      ERROR("no function u specified; doing nothing\n");
      return(0.0);
    }
    
200
201
    if (!(errUh = &uh)  ||  !(fe_space = uh.getFESpace())) {
      ERROR("no discrete function or no fe_space for it; doing nothing\n");
202
203
204
      return(0.0);
    }

205
    if (!(basFct = fe_space->getBasisFcts())) {
206
207
208
209
      ERROR("no basis functions at discrete solution ; doing nothing\n");
      return(0.0);
    }

210
    int dim = fe_space->getMesh()->getDim();
211
    int deg = u.getDegree();
212
    int degree = deg ? deg :  2 * fe_space->getBasisFcts()->getDegree() - 2;
213
214
215
216

    q = Quadrature::provideQuadrature(dim, degree);
    quadFast = FastQuadrature::provideFastQuadrature(basFct, *q, INIT_PHI);

217
218
    double relative = relErr;
    double maxErr = 0.0, l2Err2 = 0.0, l2Norm2 = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
219
    double *u_vec = new double[quadFast->getQuadrature()->getNumPoints()];
220
221

    TraverseStack stack;
222
    ElInfo *elInfo = stack.traverseFirst(fe_space->getMesh(), -1,
223
224
225
					 Mesh::FILL_COORDS |
					 Mesh::CALL_LEAF_EL |
					 Mesh::FILL_DET | 
226
					 Mesh::FILL_GRD_LAMBDA);
227
    while (elInfo) {
Thomas Witkowski's avatar
Thomas Witkowski committed
228
229
      elinfo = elInfo;
      const double *uh_vec;
230
      
Thomas Witkowski's avatar
Thomas Witkowski committed
231
      quadFast->getQuadrature()->fAtQp(errU, u_vec);
232
      uh_vec = errUh->getVecAtQPs(elInfo, NULL, quadFast, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
233
      double det = elInfo->getDet(); 
234
      int nPoints = quadFast->getNumPoints();
Thomas Witkowski's avatar
Thomas Witkowski committed
235
236
237
      double l2_err_el = 0.0, err = 0.0;

      for (int i = 0; i < nPoints; i++) {
238
239
240
241
	err = u_vec[i] - uh_vec[i];
	l2_err_el += quadFast->getWeight(i) * sqr(err);
      }
      
Thomas Witkowski's avatar
Thomas Witkowski committed
242
      double exact = det * l2_err_el;
243
244
245
246
247
248
249
      l2Err2 += exact;
      maxErr = std::max(maxErr, exact);
      
      if (writeInLeafData)
	elInfo->getElement()->setEstimation(exact, component);
      
      if (relative) {
Thomas Witkowski's avatar
Thomas Witkowski committed
250
251
	double norm_el = 0.0;
	for (int i = 0; i < nPoints; i++)
252
253
254
	  norm_el += quadFast->getWeight(i) * sqr(u_vec[i]);
	l2Norm2 += det * norm_el;
      }
255

256
257
      elInfo = stack.traverseNext(elInfo);
    }
258

Thomas Witkowski's avatar
Thomas Witkowski committed
259
260
    delete [] u_vec;

261
    if (relative) {
262
263
      double relNorm2 = l2Norm2 + 1.e-15;

264
      elInfo = stack.traverseFirst(fe_space->getMesh(), -1, Mesh::CALL_LEAF_EL);
265
266
267
268
269
270
271
      while (elInfo) {
	double exact = elInfo->getElement()->getEstimation(component) / relNorm2;
	if (writeInLeafData)
	  elInfo->getElement()->setEstimation(exact, component);
	elInfo = stack.traverseNext(elInfo);
      }

272
273
274
275
276
277
278
279
280
281
      l2Err2 /= relNorm2;
    }

    if (max)  
      *max = maxErr;

    return (sqrt(l2Err2));
  }

}