Estimator.cc 15.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "Estimator.h"
#include "Operator.h"
#include "DOFMatrix.h"
#include "DOFVector.h"
#include "Assembler.h"
#include "Traverse.h"
#include "Parameters.h"

namespace AMDiS {

  void r(const ElInfo              *elInfo,
	 int                        numPoints,
	 const double              *uhIq,
	 const WorldVector<double> *grdUhIq,
	 const WorldMatrix<double> *D2UhIq,
	 const double              *uhOldIq,
	 const WorldVector<double> *grdUhOldIq,
	 const WorldMatrix<double> *D2UhOldIq,
	 DOFMatrix *A, 
	 DOFVector<double> *fh,
	 Quadrature *quad,
	 double *result)
  {
    ::std::vector<Operator*>::iterator it;
    ::std::vector<double*>::iterator fac;
    double factor;

    // lhs
    for(it = const_cast<DOFMatrix*>(A)->getOperatorsBegin(),
	  fac = const_cast<DOFMatrix*>(A)->getOperatorEstFactorBegin(); 
	it != const_cast<DOFMatrix*>(A)->getOperatorsEnd(); 
	++it, ++fac) 
      {
	factor = *fac ? **fac : 1.0;
	if (factor) {
	  if (D2UhIq) {
	    (*it)->evalSecondOrder(numPoints, uhIq, grdUhIq, D2UhIq, result, -factor);
	  }

	  if (grdUhIq) {
	    (*it)->evalFirstOrderGrdPsi(numPoints, uhIq, grdUhIq, D2UhIq, result, factor);
	    (*it)->evalFirstOrderGrdPhi(numPoints, uhIq, grdUhIq, D2UhIq, result, factor);
	  }

	  if (uhIq) {
	    (*it)->evalZeroOrder(numPoints, uhIq, grdUhIq, D2UhIq, result, factor);
	  }
	}
      }

    // 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) 
      {
	factor = *fac ? **fac : 1.0;
	if(factor) {
	  if((*it)->getUhOld()) {
	    if(D2UhOldIq) {
	      (*it)->evalSecondOrder(numPoints, 
				     uhOldIq, grdUhOldIq, D2UhOldIq, 
				     result, factor);
	    }
	    if(grdUhOldIq) {
	      (*it)->evalFirstOrderGrdPsi(numPoints, 
					  uhOldIq, grdUhOldIq, D2UhOldIq, 
					  result, -factor);
	      (*it)->evalFirstOrderGrdPhi(numPoints, 
					  uhOldIq, grdUhOldIq, D2UhOldIq, 
					  result, -factor);
	    }
	    if(uhOldIq) {
	      (*it)->evalZeroOrder(numPoints, 
				   uhOldIq, grdUhOldIq, D2UhOldIq, 
				   result, -factor);
	    }
	  } else {
	    int iq;
	    double *fx = GET_MEMORY(double, numPoints);
	    for(iq = 0; iq < numPoints; iq++) {
	      fx[iq] = 0.0;
	    }
	    (*it)->getC(elInfo, numPoints, fx);

	    for(iq = 0; iq < numPoints; iq++) {
	      result[iq] -= factor * fx[iq];
	    }
	    FREE_MEMORY(fx, double, numPoints);
	  }
	}
      }
  }

  double Estimator::estimate(double ts)
  {
    FUNCNAME("Estimator::estimate()");
  
    init(ts);
  
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, traverseFlag);
103

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    while(elInfo) {
      estimateElement(elInfo);
      elInfo = stack.traverseNext(elInfo);
    }

    exit();
    return est_sum;
  }

  void ResidualEstimator::init(double ts)
  {
    FUNCNAME("ResidualEstimator::init()");

    timestep = ts;

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

    numSystems = static_cast<int>(uh.size());
122
    TEST_EXIT_DBG(numSystems > 0)("no system set\n");
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
217
218
219
220
221
222
223
224
225
226
227
228
229

    dim = mesh->getDim();
    basFcts = GET_MEMORY(const BasisFunction*, numSystems);
    quadFast = GET_MEMORY(FastQuadrature*, numSystems);

    degree = 0;
    for(system = 0; system < numSystems; system++) {
      basFcts[system] = uh[system]->getFESpace()->getBasisFcts();
      degree = ::std::max(degree, basFcts[system]->getDegree());
    }

    degree *= 2;

    quad = Quadrature::provideQuadrature(dim, degree);
    numPoints = quad->getNumPoints();

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

    for(system = 0; system < numSystems; system++) {
      quadFast[system] = FastQuadrature::provideFastQuadrature(basFcts[system], 
							       *quad, 
							       flag);
    }
  


    uhEl = GET_MEMORY(double*, numSystems);
    uhOldEl = timestep ? GET_MEMORY(double*, numSystems) : NULL;

    for(system = 0; system < numSystems; system++) {
      uhEl[system] = GET_MEMORY(double, basFcts[system]->getNumber()); 
      if(timestep)
	uhOldEl[system] = GET_MEMORY(double, basFcts[system]->getNumber());
    }

    //const double *uhQP = NULL;
    uhQP = timestep ? GET_MEMORY(double, quad->getNumPoints()) : NULL;
    uhOldQP = 
      timestep ? GET_MEMORY(double, quad->getNumPoints()) : NULL;


    riq = GET_MEMORY(double, numPoints);

    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->getElement()->setEstimation(0.0);
      elInfo = stack.traverseNext(elInfo);
    }

    est_sum = est_max = est_t_sum = 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;
  }

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

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

    for(system = 0; system < numSystems; system++) {
      FREE_MEMORY(uhEl[system], double, basFcts[system]->getNumber());
      if(timestep)
	FREE_MEMORY(uhOldEl[system], double, basFcts[system]->getNumber());    
    }

    FREE_MEMORY(uhEl, double*, numSystems);
    if(timestep)
      FREE_MEMORY(uhOldEl, double*, numSystems);

    if(timestep) {
      FREE_MEMORY(uhQP, double, numPoints);
      FREE_MEMORY(uhOldQP, double, numPoints);
    }

    MSG("estimate   = %.8e\n", est_sum);
    if(C3)
      MSG("time estimate   = %.8e\n", est_t_sum);

    FREE_MEMORY(riq, double, numPoints);
    FREE_MEMORY(basFcts, const BasisFunction*, numSystems);
    FREE_MEMORY(quadFast, FastQuadrature*, numSystems);
  }

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

    double est_el;
    double val;
    Element *el, *neigh;
    int iq;

230
    TEST_EXIT_DBG(numSystems > 0)("no system set\n");
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

    ::std::vector<Operator*>::iterator it;

    const WorldVector<double> *grdUh_qp = NULL;
    const WorldMatrix<double> *D2uhqp = NULL; 

    el = elInfo->getElement();

    double det = elInfo->getDet();
    const DimVec<WorldVector<double> > &Lambda = elInfo->getGrdLambda();

    est_el = el->getEstimation(row);

    double h2 = h2_from_det(det, dim);

Thomas Witkowski's avatar
Thomas Witkowski committed
246
    for (iq = 0; iq < numPoints; iq++) {
247
248
249
      riq[iq] = 0.0;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
250
    for (system = 0; system < numSystems; system++) {
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

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

      // init assemblers
      ::std::vector<Operator*>::iterator it;

      for(it = const_cast<DOFMatrix*>(matrix[system])->getOperatorsBegin();
	  it != const_cast<DOFMatrix*>(matrix[system])->getOperatorsEnd(); 
	  ++it) 
	{
	  (*it)->getAssembler()->initElement(elInfo, quad);
	}

      for(it = const_cast<DOFVector<double>*>(fh[system])->getOperatorsBegin();
	  it != const_cast<DOFVector<double>*>(fh[system])->getOperatorsEnd(); 
	  ++it) 
	{
	  (*it)->getAssembler()->initElement(elInfo, quad);
	}
    
      //uh[system]->getLocalVector(el, uhEl);
272
273
      if (timestep) {
	TEST_EXIT_DBG(uhOld[system])("no uhOld\n");
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
	uhOld[system]->getLocalVector(el, uhOldEl[system]);
      }
      
      // ===== time and element residuals       
      if (timestep && (C0 || C3)) {
	//quadFast->uhAtQp(uhEl, uhQP);
	uh[system]->getVecAtQPs(elInfo, NULL, quadFast[system], uhQP);
	//quadFast->uhAtQp(uhOldEl, uhOldQP);
	uhOld[system]->getVecAtQPs(elInfo, NULL, quadFast[system], uhOldQP);
      
	if(C3 && uhOldQP && system == ::std::max(row, 0)) {
	  for (val = iq = 0; iq < numPoints; iq++) {
	    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);
	}
      }

      if (C0) {
	for(it = const_cast<DOFMatrix*>(matrix[system])->getOperatorsBegin(); 
	    it != const_cast<DOFMatrix*>(matrix[system])->getOperatorsEnd(); 
	    ++it) 
	  {
	    if((*it)->zeroOrderTerms() && !uhQP) {
	      //uhQP = const_cast<double*>
	      //  (quadFast->uhAtQp(uhEl, static_cast<double*>(NULL)));
	      uhQP = const_cast<double*>
		(uh[system]->getVecAtQPs(elInfo, NULL, quadFast[system], NULL));
	    }
	    if((*it)->firstOrderTermsGrdPsi() || (*it)->firstOrderTermsGrdPhi() 
	       && !grdUh_qp) 
	      {
		grdUh_qp = 
		  //quadFast->grdUhAtQp(Lambda, 
		  //			  const_cast<const double*>(uhEl), 
		  //		  static_cast<WorldVector<double>*>(NULL));
		  uh[system]->getGrdAtQPs(elInfo, NULL, quadFast[system], NULL);
	      }
	    if((*it)->secondOrderTerms() && !D2uhqp) {
	      if(degree > 2)
		D2uhqp = 
		  // 		quadFast->D2UhAtQp(Lambda, 
		  // 				   uhEl, 
		  // 				   static_cast<WorldMatrix<double>*>(NULL));
		  uh[system]->getD2AtQPs(elInfo, NULL, quadFast[system], NULL);
	    }
	  }
	
	r(elInfo,
	  numPoints, 
	  uhQP,
	  grdUh_qp,
	  D2uhqp,
	  uhOldQP,
	  NULL,  // grdUhOldQP 
	  NULL,  // D2UhOldQP
	  matrix[system], 
	  fh[system],
	  quad,
	  riq);	
      }
    }

    // add integral over r square
    for(val = iq = 0; iq < numPoints; iq++) {
      val += quad->getWeight(iq)*riq[iq]*riq[iq];
    }
  
    if (timestep != 0.0 || norm == NO_NORM || norm == L2_NORM) 
      val = C0*h2*h2*det*val;
    else
      val = C0*h2*det*val;

    est_el += val;

    // ===== jump residuals 
    int neighbours = Global::getGeo(NEIGH, dim);
    int face;
      
    if(dim > 1) {
      Quadrature *surfaceQuad = Quadrature::provideQuadrature(dim-1, degree);
    
      int numPointsSurface = surfaceQuad->getNumPoints();

      Vector<WorldVector<double> > jump(numPointsSurface);
    
      if (C1) {
	for (face = 0; face < neighbours; face++) {
	  if ((neigh = const_cast<Element*>(elInfo->getNeighbour(face)))) {
	    if (neigh->getMark()) {
	      ElInfo *neighInfo = mesh->createNewElInfo();
	      WorldVector<int> faceIndEl, faceIndNeigh;
	      Element *neigh = 
		const_cast<Element*>(elInfo->getNeighbour(face));
	      int oppV = elInfo->getOppVertex(face);
	      DimVec<WorldVector<double> > LambdaNeigh(dim, NO_INIT);
	      double detNeigh;
	      DimVec<double> lambda(dim, NO_INIT);
	    
	      el->sortFaceIndices(face, &faceIndEl);
	      neigh->sortFaceIndices(oppV, &faceIndNeigh);
	    
	      neighInfo->setElement(const_cast<Element*>(neigh));
	      neighInfo->setFillFlag(Mesh::FILL_COORDS);
	    
	      int dow = Global::getGeo(WORLD);
		
	      int i, j, i1, i2;

	      for (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;

394
	      if (ldp) {
395
		::std::list<LeafDataPeriodic::PeriodicInfo>::iterator it;
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
		for (it = dynamic_cast<LeafDataPeriodic*>(ldp)->getInfoList().begin();
		     it != dynamic_cast<LeafDataPeriodic*>(ldp)->getInfoList().end();
		     ++it) {

		  if (it->elementSide == face) {
		    for (i=0; i < dim; i++) {
		      i1 = faceIndEl[i];
		      i2 = faceIndNeigh[i];

		      for(j=0; j < dim; j++) {
			if (i1 == el->getVertexOfPosition(INDEX_OF_DIM(dim-1, 
								       dim),
							  face,
							  j)) {
			  break;
411
412
			}
		      }
413
414
415
416

		      TEST_EXIT_DBG(j != dim)("vertex i1 not on face ???\n");
		      
		      neighInfo->getCoord(i2) = (*(it->periodicCoords))[j];
417
		    }
418
419
		    periodicCoords = true;
		    break;
420
		  }
421
		}
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
	      }

	      if(!periodicCoords) {
		for (i = 0; i < dim; i++) {
		  i1 = faceIndEl[i];
		  i2 = faceIndNeigh[i];
		  for (j = 0; j < dow; j++)
		    neighInfo->getCoord(i2)[j] = elInfo->getCoord(i1)[j];
		}
	      }
		
	      Parametric *parametric = mesh->getParametric();
	      if(parametric) {
		neighInfo = parametric->addParametricInfo(neighInfo);
	      }
	      
	      detNeigh = abs(neighInfo->calcGrdLambda(LambdaNeigh));

	      Vector<WorldVector<double> > grdUhEl(numPointsSurface);
	      Vector<WorldVector<double> > grdUhNeigh(numPointsSurface);

	      for(iq = 0; iq < numPointsSurface; iq++) {
		jump[iq].set(0.0);
	      }

	      for(system = 0; system < numSystems; system++) {
		if(matrix[system] == NULL) continue;
	      
		uh[system]->getLocalVector(el, uhEl[system]);

		const double* uhNeigh;
		
		uhNeigh = uh[system]->getLocalVector(neigh, NULL);

		for (iq = 0; iq < numPointsSurface; iq++) {
		  lambda[face] = 0.0;
		  for (i = 0; i < dim; i++)
		    lambda[faceIndEl[i]] = surfaceQuad->getLambda(iq,i);
		  basFcts[system]->evalGrdUh(lambda, Lambda, uhEl[system], &grdUhEl[iq]);
		
		  lambda[oppV] = 0.0;
		  for (i = 0; i < dim; i++)
		    lambda[faceIndNeigh[i]] = surfaceQuad->getLambda(iq,i);
		  basFcts[system]->evalGrdUh(lambda, 
					     LambdaNeigh, 
					     uhNeigh, 
					     &grdUhNeigh[iq]);

		  grdUhEl[iq] -= grdUhNeigh[iq];
		}

		::std::vector<double*>::iterator fac;

		for(it = const_cast<DOFMatrix*>(matrix[system])->getOperatorsBegin(),
		      fac = const_cast<DOFMatrix*>(matrix[system])->getOperatorEstFactorBegin(); 
		    it != const_cast<DOFMatrix*>(matrix[system])->getOperatorsEnd(); 
		    ++it, ++fac) 
		  {
		    int i;
		    Vector<WorldVector<double> > localJump(numPointsSurface);
		    for(iq = 0; iq < numPointsSurface; iq++) {
		      localJump[iq].set(0.0);
		    }

		    (*it)->weakEvalSecondOrder(numPointsSurface,
					       grdUhEl.getValArray(),
					       localJump.getValArray());
		    double factor = *fac ? **fac : 1.0;
		    if(factor != 1.0) {
		      for(i = 0; i < numPointsSurface; i++) {
			localJump[i] *= factor;
		      }
		    }

		    for(i = 0; i < numPointsSurface; i++) {
		      jump[i] += localJump[i];
		    }
		  }
	      }

	      for(val =iq = 0; iq < numPointsSurface; iq++) {
		val += surfaceQuad->getWeight(iq)*(jump[iq]*jump[iq]);
	      }
	      
	      double d =  0.5*(det + detNeigh);

508
509
	      //	      ::std::cout << "d: " << d << ::std::endl;

510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
	      if (norm == NO_NORM || norm == L2_NORM)
		val *= C1*h2_from_det(d, dim) * d;
	      else
		val *= C1*d;
	      
	      if(parametric) {
		neighInfo = parametric->removeParametricInfo(neighInfo);
	      }

	      DELETE neighInfo;

	      neigh->setEstimation(neigh->getEstimation(row) + val, row);

	      est_el += val;
	    }
	  }
	}
	
	val = fh[::std::max(row, 0)]->
	  getBoundaryManager()->
	  boundResidual(elInfo, matrix[::std::max(row, 0)], uh[::std::max(row, 0)]);
	if (norm == NO_NORM || norm == L2_NORM)
	  val *= C1 * h2;
	else
	  val *= C1;
	
	est_el += val;
      }
    }
539
540

    //    ::std::cout << "Est: " << est_el << ::std::endl;
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
  
    el->setEstimation(est_el, row);

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

    elInfo->getElement()->setMark(0);
  }

  Estimator::Estimator(::std::string name_, int r) 
    : name(name_),
      norm(NO_NORM),
      row(r)
  {
    GET_PARAMETER(0, name + "->error norm", "%d", &norm);
  }

  ResidualEstimator::ResidualEstimator(::std::string name, int r) 
    : Estimator(name, r),
      C0(1.0), 
      C1(1.0), 
      C2(1.0), 
      C3(1.0)
  {
    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;
  }

}