ResidualEstimator.cc 14.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#include "ResidualEstimator.h"
#include "Operator.h"
#include "DOFMatrix.h"
#include "DOFVector.h"
#include "Assembler.h"
#include "Traverse.h"
#include "Parameters.h"

namespace AMDiS {

11
  ResidualEstimator::ResidualEstimator(std::string name, int r) 
12
13
14
    : Estimator(name, r),
      C0(1.0), 
      C1(1.0), 
15
      C2(0.0), 
16
17
      C3(1.0)
  {
18
19
    FUNCNAME("ResidualEstimator::ResidualEstimator()");

20
21
22
23
24
25
26
27
28
    GET_PARAMETER(0, name + "->C0", "%f", &C0);
    GET_PARAMETER(0, name + "->C1", "%f", &C1);
    GET_PARAMETER(0, name + "->C2", "%f", &C2);
    GET_PARAMETER(0, name + "->C3", "%f", &C3);

    C0 = C0 > 1.e-25 ? sqr(C0) : 0.0;
    C1 = C1 > 1.e-25 ? sqr(C1) : 0.0;
    C2 = C2 > 1.e-25 ? sqr(C2) : 0.0;
    C3 = C3 > 1.e-25 ? sqr(C3) : 0.0;
29
30

    TEST_EXIT(C2 == 0.0)("C2 is not used! Please remove it or set it to 0.0!\n");
31
32
33
34
35
  }

  void ResidualEstimator::init(double ts)
  {
    FUNCNAME("ResidualEstimator::init()");
Thomas Witkowski's avatar
Thomas Witkowski committed
36
    
37
38
39
40
    timestep = ts;

    mesh = uh[row == -1 ? 0 : row]->getFESpace()->getMesh();

Thomas Witkowski's avatar
Thomas Witkowski committed
41
42
    nSystems = static_cast<int>(uh.size());
    TEST_EXIT_DBG(nSystems > 0)("no system set\n");
43
44

    dim = mesh->getDim();
45
46
    basFcts = new const BasisFunction*[nSystems];
    quadFast = new FastQuadrature*[nSystems];
47
48

    degree = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
49
    for (int system = 0; system < nSystems; system++) {
50
      basFcts[system] = uh[system]->getFESpace()->getBasisFcts();
51
      degree = std::max(degree, basFcts[system]->getDegree());
52
53
54
55
    }
    degree *= 2;

    quad = Quadrature::provideQuadrature(dim, degree);
Thomas Witkowski's avatar
Thomas Witkowski committed
56
    nPoints = quad->getNumPoints();
57
58

    Flag flag = INIT_PHI | INIT_GRD_PHI;
59
60
    if (degree > 2)
      flag |= INIT_D2_PHI;    
61

62
    for (int system = 0; system < nSystems; system++)
63
64
      quadFast[system] = FastQuadrature::provideFastQuadrature(basFcts[system], 
							       *quad, 
65
							       flag);    
66
  
67
68
69
    uhEl = new double*[nSystems];
    uhNeigh = new double*[nSystems];
    uhOldEl = timestep ? new double*[nSystems] : NULL;
70

Thomas Witkowski's avatar
Thomas Witkowski committed
71
    for (int system = 0; system < nSystems; system++) {
72
73
      uhEl[system] = new double[basFcts[system]->getNumber()]; 
      uhNeigh[system] = new double[basFcts[system]->getNumber()];
74
      if (timestep)
75
	uhOldEl[system] = new double[basFcts[system]->getNumber()];
76
77
    }

78
79
80
    uhQP = timestep ? new double[nPoints] : NULL;
    uhOldQP = timestep ? new double[nPoints] : NULL;
    riq = new double[nPoints];
81
82
83
    grdUh_qp = NULL;
    D2uhqp = NULL;

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    TraverseStack stack;
    ElInfo *elInfo = NULL;

    // clear error indicators and mark elements for jumpRes
    elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
    while (elInfo) {
      elInfo->getElement()->setEstimation(0.0, row);
      elInfo->getElement()->setMark(1);
      elInfo = stack.traverseNext(elInfo);
    }

    est_sum = 0.0;
    est_max = 0.0;
    est_t_sum = 0.0;
    est_t_max = 0.0;

    traverseFlag = 
      Mesh::FILL_NEIGH      |
      Mesh::FILL_COORDS     |
      Mesh::FILL_OPP_COORDS |
      Mesh::FILL_BOUND      |
      Mesh::FILL_GRD_LAMBDA |
      Mesh::FILL_DET        |
      Mesh::CALL_LEAF_EL;
108
109
110
111

    neighInfo = mesh->createNewElInfo();

    // prepare date for computing jump residual
112
113
114
115
116
117
118
119
120
121
    if (C1 > 0.0 && dim > 1) {
      surfaceQuad = Quadrature::provideQuadrature(dim - 1, degree);
      nPointsSurface = surfaceQuad->getNumPoints();
      grdUhEl.resize(nPointsSurface);
      grdUhNeigh.resize(nPointsSurface);
      jump.resize(nPointsSurface);
      localJump.resize(nPointsSurface);
      nNeighbours = Global::getGeo(NEIGH, dim);
      lambdaNeigh = new DimVec<WorldVector<double> >(dim, NO_INIT);
      lambda = new DimVec<double>(dim, NO_INIT);
122
    }
123
124
125
126
127
128
129
130
131
  }

  void ResidualEstimator::exit(bool output)
  {
    FUNCNAME("ResidualEstimator::exit()");

    est_sum = sqrt(est_sum);
    est_t_sum = sqrt(est_t_sum);

Thomas Witkowski's avatar
Thomas Witkowski committed
132
    for (int system = 0; system < nSystems; system++) {
133
134
      delete [] uhEl[system];
      delete [] uhNeigh[system];
135
      if (timestep)
136
	delete [] uhOldEl[system];
137
138
    }

139
140
    delete [] uhEl;
    delete [] uhNeigh;
141
142

    if (timestep) {
143
144
145
      delete [] uhOldEl;
      delete [] uhQP;
      delete [] uhOldQP;
146
    } else {
147
148
      if (uhQP != NULL)
	delete [] uhQP;
149
150
151
152
    }

    if (output) {
      MSG("estimate   = %.8e\n", est_sum);
153
      if (C3)
154
155
156
	MSG("time estimate   = %.8e\n", est_t_sum);
    }

157
158
159
    delete [] riq;
    delete [] basFcts;
    delete [] quadFast;
160

161
162
163
164
    if (grdUh_qp != NULL)
      delete [] grdUh_qp;
    if (D2uhqp != NULL)
      delete [] D2uhqp;
165

166
    if (C1 && (dim > 1)) {
167
168
      delete lambdaNeigh;
      delete lambda;
169
170
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
171
    delete neighInfo;
172
173
174
  }

  void ResidualEstimator::estimateElement(ElInfo *elInfo)
175
  {    
176
177
    FUNCNAME("ResidualEstimator::estimateElement()");

Thomas Witkowski's avatar
Thomas Witkowski committed
178
    TEST_EXIT_DBG(nSystems > 0)("no system set\n");
179

Thomas Witkowski's avatar
Thomas Witkowski committed
180
    double val = 0.0;
181
    std::vector<Operator*>::iterator it;
182
    std::vector<double*>::iterator itfac;
Thomas Witkowski's avatar
Thomas Witkowski committed
183
    Element *el = elInfo->getElement();
184
    double det = elInfo->getDet();
185
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
Thomas Witkowski's avatar
Thomas Witkowski committed
186
    double est_el = el->getEstimation(row);
187
188
    double h2 = h2_from_det(det, dim);

189
    for (int iq = 0; iq < nPoints; iq++)
190
191
      riq[iq] = 0.0;

Thomas Witkowski's avatar
Thomas Witkowski committed
192
    for (int system = 0; system < nSystems; system++) {
193
194
195
196

      if (matrix[system] == NULL) 
	continue;

197
198
      // === init assemblers ===

199
200
      for (it = const_cast<DOFMatrix*>(matrix[system])->getOperatorsBegin(),
	   itfac = const_cast<DOFMatrix*>(matrix[system])->getOperatorEstFactorBegin();
201
	   it != const_cast<DOFMatrix*>(matrix[system])->getOperatorsEnd(); 
202
203
	   ++it, ++itfac)
	if (*itfac == NULL || **itfac != 0.0)
204
	  (*it)->getAssembler(omp_get_thread_num())->initElement(elInfo, NULL, quad);
205

206
      if (C0 > 0.0)
Thomas Witkowski's avatar
Thomas Witkowski committed
207
208
	for (it = const_cast<DOFVector<double>*>(fh[system])->getOperatorsBegin();
	     it != const_cast<DOFVector<double>*>(fh[system])->getOperatorsEnd(); 
209
	     ++it)
210
	  (*it)->getAssembler(omp_get_thread_num())->initElement(elInfo, NULL, quad);	
Naumann, Andreas's avatar
Naumann, Andreas committed
211
	
212
      if (timestep && uhOld[system]) {
213
214
215
	TEST_EXIT_DBG(uhOld[system])("no uhOld\n");
	uhOld[system]->getLocalVector(el, uhOldEl[system]);
  
216
217
218
	// === Compute time error. ===

	if (C0 > 0.0 || C3 > 0.0) {   
219
220
221
	  uh[system]->getVecAtQPs(elInfo, NULL, quadFast[system], uhQP);
	  uhOld[system]->getVecAtQPs(elInfo, NULL, quadFast[system], uhOldQP);
	  
222
	  if (C3 > 0.0 && uhOldQP && system == std::max(row, 0)) {
223
	    val = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
224
	    for (int iq = 0; iq < nPoints; iq++) {
225
226
227
228
229
230
231
232
233
	      double tiq = (uhQP[iq] - uhOldQP[iq]);
	      val += quad->getWeight(iq) * tiq * tiq;
	    }
	    double v = C3 * det * val;
	    est_t_sum += v;
	    est_t_max = max(est_t_max, v);
	  }
	}
      }
234
           
235
236
237
      // === Compute element residual. ===

      if (C0 > 0.0) {  
238
239
	for (it = const_cast<DOFMatrix*>(matrix[system])->getOperatorsBegin(),
	     itfac = const_cast<DOFMatrix*>(matrix[system])->getOperatorEstFactorBegin(); 
240
	     it != const_cast<DOFMatrix*>(matrix[system])->getOperatorsEnd(); 
241
	     ++it, ++itfac) {
242
	  if (*itfac == NULL || **itfac != 0.0) {
243
	    if (uhQP == NULL && (*it)->zeroOrderTerms()) {
244
	      uhQP = new double[nPoints];
245
246
	      uh[system]->getVecAtQPs(elInfo, NULL, quadFast[system], uhQP);
	    }
247
248
	    if (grdUh_qp == NULL && 
		((*it)->firstOrderTermsGrdPsi() || (*it)->firstOrderTermsGrdPhi())) {
249
250
251
	      grdUh_qp = new WorldVector<double>[nPoints];
	      uh[system]->getGrdAtQPs(elInfo, NULL, quadFast[system], grdUh_qp);
	    }
252
	    if (D2uhqp == NULL && degree > 2 && (*it)->secondOrderTerms()) { 
253
254
255
	      D2uhqp = new WorldMatrix<double>[nPoints];
	      uh[system]->getD2AtQPs(elInfo, NULL, quadFast[system], D2uhqp);	    
	    }
256
257
258
	  }
	}
	
259
260
	// === Compute the element residual and store it in irq. ===

261
	r(elInfo,
Thomas Witkowski's avatar
Thomas Witkowski committed
262
	  nPoints, 
263
264
265
266
267
268
269
270
271
	  uhQP,
	  grdUh_qp,
	  D2uhqp,
	  uhOldQP,
	  NULL,  // grdUhOldQP 
	  NULL,  // D2UhOldQP
	  matrix[system], 
	  fh[system],
	  quad,
272
	  riq);
273
274
275
276
      }     
    }

    // add integral over r square
277
    val = 0.0;
278
    for (int iq = 0; iq < nPoints; iq++)
279
      val += quad->getWeight(iq) * riq[iq] * riq[iq];
280
   
281
    if (timestep != 0.0 || norm == NO_NORM || norm == L2_NORM)
282
      val = C0 * h2 * h2 * det * val;
283
    else
284
      val = C0 * h2 * det * val;
285
    
286
287
    est_el += val;

288
289
290

    // === Compute jump residuals. ===

291
    if (C1 && (dim > 1)) {
292
      int dow = Global::getGeo(WORLD);
293

294
      for (int face = 0; face < nNeighbours; face++) {  
Thomas Witkowski's avatar
Thomas Witkowski committed
295
	Element *neigh = const_cast<Element*>(elInfo->getNeighbour(face));
296
297
298
	if (neigh && neigh->getMark()) {      
	  int oppV = elInfo->getOppVertex(face);
	      
299
300
	  el->sortFaceIndices(face, &faceIndEl);
	  neigh->sortFaceIndices(oppV, &faceIndNeigh);
301
302
303
	    
	  neighInfo->setElement(const_cast<Element*>(neigh));
	  neighInfo->setFillFlag(Mesh::FILL_COORDS);
304
	      	
305
306
307
308
309
310
311
312
313
	  for (int i = 0; i < dow; i++)
	    neighInfo->getCoord(oppV)[i] = elInfo->getOppCoord(face)[i];
		
	  // periodic leaf data ?
	  ElementData *ldp = el->getElementData()->getElementData(PERIODIC);

	  bool periodicCoords = false;

	  if (ldp) {
314
315
	    std::list<LeafDataPeriodic::PeriodicInfo>::iterator it;
	    std::list<LeafDataPeriodic::PeriodicInfo>& infoList = 
316
		dynamic_cast<LeafDataPeriodic*>(ldp)->getInfoList();
317

318
	    for (it = infoList.begin(); it != infoList.end(); ++it) {
319
320
	      if (it->elementSide == face) {
		for (int i = 0; i < dim; i++) {
321
322
		  int i1 = faceIndEl[i];
		  int i2 = faceIndNeigh[i];
323

Thomas Witkowski's avatar
Thomas Witkowski committed
324
325
		  int j = 0;
		  for (; j < dim; j++) {
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
		    if (i1 == el->getVertexOfPosition(INDEX_OF_DIM(dim - 1, 
								   dim),
						      face,
						      j)) {
		      break;
		    }
		  }

		  TEST_EXIT_DBG(j != dim)("vertex i1 not on face ???\n");
		      
		  neighInfo->getCoord(i2) = (*(it->periodicCoords))[j];
		}
		periodicCoords = true;
		break;
	      }
	    }
	  }
      
	  if (!periodicCoords) {
	    for (int i = 0; i < dim; i++) {
346
347
	      int i1 = faceIndEl[i];
	      int i2 = faceIndNeigh[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
348
	      for (int j = 0; j < dow; j++)
349
350
351
352
353
		neighInfo->getCoord(i2)[j] = elInfo->getCoord(i1)[j];
	    }
	  }
	      
	  Parametric *parametric = mesh->getParametric();
354
355
	  if (parametric)
	    neighInfo = parametric->addParametricInfo(neighInfo);	  
356
	      
357
	  double detNeigh = abs(neighInfo->calcGrdLambda(*lambdaNeigh));
358
	      
359
360
	  for (int iq = 0; iq < nPointsSurface; iq++)
	    jump[iq].set(0.0);     
361

Thomas Witkowski's avatar
Thomas Witkowski committed
362
	  for (int system = 0; system < nSystems; system++) {	
363
364
365
	    if (matrix[system] == NULL) 
	      continue;
	      
366
367
	    uh[system]->getLocalVector(el, uhEl[system]);	
	    uh[system]->getLocalVector(neigh, uhNeigh[system]);
368
			
369
370
371
372
	    for (int iq = 0; iq < nPointsSurface; iq++) {
	      (*lambda)[face] = 0.0;
	      for (int i = 0; i < dim; i++)
		(*lambda)[faceIndEl[i]] = surfaceQuad->getLambda(iq, i);
373
		  
374
	      basFcts[system]->evalGrdUh(*lambda, 
375
					 grdLambda, 
376
					 uhEl[system], 
377
					 &grdUhEl[iq]);
378
		  
379
380
381
	      (*lambda)[oppV] = 0.0;
	      for (int i = 0; i < dim; i++)
		(*lambda)[faceIndNeigh[i]] = surfaceQuad->getLambda(iq, i);
382
		  
383
384
	      basFcts[system]->evalGrdUh(*lambda, 
					 *lambdaNeigh, 
385
					 uhNeigh[system], 
386
					 &grdUhNeigh[iq]);
387
		  
388
	      grdUhEl[iq] -= grdUhNeigh[iq];
389
390
	    }				

391
	    std::vector<double*>::iterator fac;
392
393
394
395
396

	    for (it = const_cast<DOFMatrix*>(matrix[system])->getOperatorsBegin(),
		   fac = const_cast<DOFMatrix*>(matrix[system])->getOperatorEstFactorBegin(); 
		 it != const_cast<DOFMatrix*>(matrix[system])->getOperatorsEnd(); 
		 ++it, ++fac) {
397

398
	      if (*fac == NULL || **fac != 0.0) {
399
400
		for (int iq = 0; iq < nPointsSurface; iq++)
		  localJump[iq].set(0.0);
401
		
402
403
404
		(*it)->weakEvalSecondOrder(nPointsSurface,
					   grdUhEl.getValArray(),
					   localJump.getValArray());
405
		double factor = *fac ? **fac : 1.0;
406
407
408
		if (factor != 1.0)
		  for (int i = 0; i < nPointsSurface; i++)
		    localJump[i] *= factor;
409
		
410
411
		for (int i = 0; i < nPointsSurface; i++)
		  jump[i] += localJump[i];
412
413
	      }		
	    }
414
415
	  }
	      
416
	  val = 0.0;
417
418
	  for (int iq = 0; iq < nPointsSurface; iq++)
	    val += surfaceQuad->getWeight(iq) * (jump[iq] * jump[iq]);
419
420
421
422
423
424
425
426
	      
	  double d = 0.5 * (det + detNeigh);

	  if (norm == NO_NORM || norm == L2_NORM)
	    val *= C1 * h2_from_det(d, dim) * d;
	  else
	    val *= C1 * d;
	      
427
	  if (parametric)
428
429
430
431
432
433
434
	    neighInfo = parametric->removeParametricInfo(neighInfo);

	  neigh->setEstimation(neigh->getEstimation(row) + val, row);
	  est_el += val;
	} 
      } 
       
435
      val = fh[std::max(row, 0)]->
436
	getBoundaryManager()->
437
	boundResidual(elInfo, matrix[std::max(row, 0)], uh[std::max(row, 0)]);
438
439
440
441
442
443
444
445
446
447
448
449
450
451
      if (norm == NO_NORM || norm == L2_NORM)
	val *= C1 * h2;
      else
	val *= C1;
	
      est_el += val;
    } 
  

    el->setEstimation(est_el, row);

    est_sum += est_el;
    est_max = max(est_max, est_el);

452
    elInfo->getElement()->setMark(0);  
453
454
  }

455
  void r(const ElInfo *elInfo,
Thomas Witkowski's avatar
Thomas Witkowski committed
456
	 int nPoints,
457
	 const double *uhIq,
458
459
	 const WorldVector<double> *grdUhIq,
	 const WorldMatrix<double> *D2UhIq,
460
	 const double *uhOldIq,
461
462
463
464
465
466
467
	 const WorldVector<double> *grdUhOldIq,
	 const WorldMatrix<double> *D2UhOldIq,
	 DOFMatrix *A, 
	 DOFVector<double> *fh,
	 Quadrature *quad,
	 double *result)
  {
468
469
    std::vector<Operator*>::iterator it;
    std::vector<double*>::iterator fac;
470
471
472
473
474
475

    // lhs
    for (it = const_cast<DOFMatrix*>(A)->getOperatorsBegin(),
	   fac = const_cast<DOFMatrix*>(A)->getOperatorEstFactorBegin(); 
	 it != const_cast<DOFMatrix*>(A)->getOperatorsEnd(); 
	 ++it, ++fac) {
476
477
478
     
      double factor = *fac ? **fac : 1.0;

479
480
      if (factor) {
	if (D2UhIq) {
Thomas Witkowski's avatar
Thomas Witkowski committed
481
	  (*it)->evalSecondOrder(nPoints, uhIq, grdUhIq, D2UhIq, result, -factor);
482
483
484
	}

	if (grdUhIq) {
Thomas Witkowski's avatar
Thomas Witkowski committed
485
486
	  (*it)->evalFirstOrderGrdPsi(nPoints, uhIq, grdUhIq, D2UhIq, result, factor);
	  (*it)->evalFirstOrderGrdPhi(nPoints, uhIq, grdUhIq, D2UhIq, result, factor);
487
488
489
	}
	
	if (uhIq) {
Thomas Witkowski's avatar
Thomas Witkowski committed
490
	  (*it)->evalZeroOrder(nPoints, uhIq, grdUhIq, D2UhIq, result, factor);
491
492
493
494
495
496
497
498
499
500
	}
      }
    }
    
    // rhs
    for (it = const_cast<DOFVector<double>*>(fh)->getOperatorsBegin(),
	 fac = const_cast<DOFVector<double>*>(fh)->getOperatorEstFactorBegin(); 
	 it != const_cast<DOFVector<double>*>(fh)->getOperatorsEnd(); 
	 ++it, ++fac) {

501
502
      double factor = *fac ? **fac : 1.0;

503
504
505
      if (factor) {
	if ((*it)->getUhOld()) {
	  if (D2UhOldIq) {
Thomas Witkowski's avatar
Thomas Witkowski committed
506
	    (*it)->evalSecondOrder(nPoints, 
507
508
509
510
				   uhOldIq, grdUhOldIq, D2UhOldIq, 
				   result, factor);
	  }
	  if (grdUhOldIq) {
Thomas Witkowski's avatar
Thomas Witkowski committed
511
	    (*it)->evalFirstOrderGrdPsi(nPoints, 
512
513
					uhOldIq, grdUhOldIq, D2UhOldIq, 
					result, -factor);
Thomas Witkowski's avatar
Thomas Witkowski committed
514
	    (*it)->evalFirstOrderGrdPhi(nPoints, 
515
516
517
518
					uhOldIq, grdUhOldIq, D2UhOldIq, 
					result, -factor);
	  }
	  if (uhOldIq) {
Thomas Witkowski's avatar
Thomas Witkowski committed
519
	    (*it)->evalZeroOrder(nPoints, 
520
521
522
523
				 uhOldIq, grdUhOldIq, D2UhOldIq, 
				 result, -factor);
	  }
	} else {
524
	  std::vector<double> fx(nPoints, 0.0);
Thomas Witkowski's avatar
Thomas Witkowski committed
525
	  (*it)->getC(elInfo, nPoints, fx);
526

527
	  for (int iq = 0; iq < nPoints; iq++)
528
529
530
531
532
533
534
535
	    result[iq] -= factor * fx[iq];
	}
      }
    }    
  }


}