Error.hh 7.39 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
38
39
40
41
42
43
    if (!(errUh = &uh) || !(fe_space = uh->getFESpace())) {
      ERROR("no discrete function or no fe_space for it; doing nothing\n");
      return(-1.0);
    }
    if (!(basFct = fe_space->getBasisFcts())) {
      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
52
53
54
55
56
57
58
59
60
61
62
63
64
    quadFast = FastQuadrature::provideFastQuadrature(basFct, *q, INIT_PHI);
    double maxErr = 0.0;
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(fe_space->getMesh(), -1,
					 Mesh::FILL_COORDS | Mesh::CALL_LEAF_EL);
    while (elInfo) {
      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);
65
66
      }

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

70
    return maxErr;
71
72
73
  }

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

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

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

    int dim = fe_space->getMesh()->getDim();
    int deg = grdU.getDegree();
    int degree = deg ? deg : 2 * fe_space->getBasisFcts()->getDegree() - 2;

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

108
109
110
111
112
113
114
115
116
117
118
119
120
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
    double relative = relErr;
    double maxErr = 0.0, h1Err2 = 0.0, h1Norm2 = 0.0;


    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(fe_space->getMesh(), -1,
					 Mesh::FILL_COORDS |
					 Mesh::CALL_LEAF_EL |
					 Mesh::FILL_DET | 
					 Mesh::FILL_GRD_LAMBDA);
    while (elInfo) {
      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;
      }
153

154
155
      elInfo = stack.traverseNext(elInfo);
    }
156
157

    if (relative) {
158
159
160
161
162
163
164
165
166
      double relNorm2 = h1Norm2 + 1.e-15;

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

      h1Err2 /= relNorm2;
      maxErr /= relNorm2;
    }

172
173
    if (max)
      *max = maxErr;
174

175
    return sqrt(h1Err2);
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
  }

  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)
  {
    FUNCNAME("Error<T>::L2Err");
    const FiniteElemSpace *fe_space;

    Quadrature *q = NULL;

    writeInLeafData = writeLeafData;
  
    component = comp;

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

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

    int dim = fe_space->getMesh()->getDim();
    int deg = u.getDegree();
    int degree = deg ? deg :  2 * fe_space->getBasisFcts()->getDegree() - 2;

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

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
    double relative = relErr;
    double maxErr = 0.0, l2Err2 = 0.0, l2Norm2 = 0.0;

    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(fe_space->getMesh(), -1,
					 Mesh::FILL_COORDS |
					 Mesh::CALL_LEAF_EL |
					 Mesh::FILL_DET | 
					 Mesh::FILL_GRD_LAMBDA);
    while (elInfo) {
      int i;
      double err, det, l2_err_el, norm_el, exact;
      const double *u_vec, *uh_vec;
      
      u_vec = quadFast->getQuadrature()->fAtQp(errU, NULL);
      uh_vec = errUh->getVecAtQPs(elInfo, NULL, quadFast, NULL);
      det = elInfo->getDet();
  
      int nPoints = quadFast->getNumPoints();
      
      for (l2_err_el = i = 0; i < nPoints; i++) {
	err = u_vec[i] - uh_vec[i];
	l2_err_el += quadFast->getWeight(i) * sqr(err);
      }
      
      exact = det * l2_err_el;
      l2Err2 += exact;
      maxErr = std::max(maxErr, exact);
      
      if (writeInLeafData)
	elInfo->getElement()->setEstimation(exact, component);
      
      if (relative) {
	for (norm_el = i = 0; i < nPoints; i++)
	  norm_el += quadFast->getWeight(i) * sqr(u_vec[i]);
	l2Norm2 += det * norm_el;
      }
254

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

    if (relative) {
259
260
261
262
263
264
265
266
267
268
      double relNorm2 = l2Norm2 + 1.e-15;

      elInfo = stack.traverseFirst(fe_space->getMesh(), -1, Mesh::CALL_LEAF_EL);
      while (elInfo) {
	double exact = elInfo->getElement()->getEstimation(component) / relNorm2;
	if (writeInLeafData)
	  elInfo->getElement()->setEstimation(exact, component);
	elInfo = stack.traverseNext(elInfo);
      }

269
270
271
272
273
274
275
276
277
278
      l2Err2 /= relNorm2;
    }

    if (max)  
      *max = maxErr;

    return (sqrt(l2Err2));
  }

}