SimpleResidualEstimator.cc 11.8 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
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


#include "est/SimpleResidualEstimator.h"
#include "Operator.h"
#include "DOFMatrix.h"
#include "DOFVector.h"
#include "Assembler.h"
#include "Traverse.h"
#include "Initfile.h"

namespace AMDiS {

  SimpleResidualEstimator::SimpleResidualEstimator(std::string name) 
    : Estimator(name, 0),
      C0(0.0), 
      C1(0.0)
  {
    FUNCNAME("SimpleResidualEstimator::SimpleResidualEstimator()");

    // === Read parameters C0 and C1 from init file. ===

    Parameters::get(name + "->C0", C0);
    Parameters::get(name + "->C1", C1);

    C0 = C0 > 1.e-25 ? sqr(C0) : 0.0;
    C1 = C1 > 1.e-25 ? sqr(C1) : 0.0;
  }


  void SimpleResidualEstimator::init(double)
  {
    FUNCNAME("SimpleResidualEstimator::init()");

44
45
46
47
    double kappa = 0.0;
    Parameters::get("kappa", kappa);
    kappa_inv = 1.0 / kappa;

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
103
104
105
106
107
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
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
    // === Create data structures. ===

    basFcts = uh[0]->getFeSpace()->getBasisFcts();
    dim = mesh->getDim();
    degree = basFcts->getDegree() * 2;
    quad = Quadrature::provideQuadrature(dim, degree);
    nPoints = quad->getNumPoints();

    Flag flag = INIT_PHI;
    if (degree > 2)
      flag |= INIT_D2_PHI;    
    quadFast = FastQuadrature::provideFastQuadrature(basFcts, *quad, flag);
  
    uhEl.change_dim(basFcts->getNumber()); 
    uhNeigh.change_dim(basFcts->getNumber());
    riq.change_dim(nPoints);

    // === Clear error indicators and mark elements for jumpRes. ===

    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
    while (elInfo) {
      elInfo->getElement()->setEstimation(0.0, 0);
      elInfo->getElement()->setMark(1);
      elInfo = stack.traverseNext(elInfo);
    }

    est_sum = 0.0;
    est_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;
    neighInfo = mesh->createNewElInfo();


    // === Prepare date for computing jump residual. ===

    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);
    }
  }


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

    // === Calculate the root of the estimations and make output. ===

    est_sum = sqrt(est_sum);

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

    /// === Delete data structures. ===

    if (C1 && (dim > 1)) {
      delete lambdaNeigh;
      delete lambda;
    }

    delete neighInfo;
  }


  void SimpleResidualEstimator::estimateElement(ElInfo *elInfo, DualElInfo *)
  {    
    FUNCNAME("SimpleResidualEstimator::estimateElement()");

    TEST_EXIT(matrix[0])("Should not happen!\n");

    // Get pointer to element object.
    Element *el = elInfo->getElement();
    // Get error estimation of this element.
    double est_el = el->getEstimation(0);
    // Shortcut for the system matrix.
    DOFMatrix *dofMat = const_cast<DOFMatrix*>(matrix[0]);
    // Shortcut for the right hand side DOF vector
    DOFVector<double> *dofVec = const_cast<DOFVector<double>*>(fh[0]);
    
    // === Init assembler ===

    std::vector<Operator*>::iterator it;
    std::vector<double*>::iterator itfac;
    // Matrix assembler are only initialized with the corresponding multiply
    // factors are != 0
    for (it = dofMat->getOperatorsBegin(), itfac = dofMat->getOperatorEstFactorBegin();
	 it != dofMat->getOperatorsEnd(); ++it, ++itfac)
      if (*itfac == NULL || **itfac != 0.0)
	(*it)->getAssembler()->initElement(elInfo, NULL, quad);	  


    // Vector assembler are only initialized if C0 is set. Note that the jump 
    // residual (thus C1) does not contain the right hand side.
    if (C0 > 0.0)
      for (it = dofVec->getOperatorsBegin(); it != dofVec->getOperatorsEnd(); ++it)
	(*it)->getAssembler()->initElement(elInfo, NULL, quad);	  


    // === Compute element residuals and time error estimation. ===
    if (C0 > 0.0)
      est_el += computeElementResidual(elInfo);

    // === Compute jump residuals. ===
    if (C1 > 0.0 && dim > 1)
      est_el += computeJumpResidual(elInfo);

    // === Update global residual variables. ===
    el->setEstimation(est_el, 0);
    el->setMark(0);
    est_sum += est_el;
    est_max = std::max(est_max, est_el);
  }


  double SimpleResidualEstimator::computeElementResidual(ElInfo *elInfo)
  {
    FUNCNAME("SimpleResidualEstimator::computeElementResidual()");

    double det = elInfo->getDet();
    double h2 = h2_from_det(det, dim);
    riq = 0.0;
    DOFMatrix *dofMat = const_cast<DOFMatrix*>(matrix[0]);
    DOFVector<double> *dofVec = const_cast<DOFVector<double>*>(fh[0]);

    // === If there is a valid left hand side operator get the solution  ===
    // === vector or its derivations on the quadrature points of the     ===
    // === element.                                                      ===
    std::vector<Operator*>::iterator it;
    std::vector<double*>::iterator itfac;      
    for (it = dofMat->getOperatorsBegin(), itfac = dofMat->getOperatorEstFactorBegin();
	 it != dofMat->getOperatorsEnd();  ++it, ++itfac) {
      if (*itfac == NULL || **itfac != 0.0) {
195
	if ((*it)->zeroOrderTerms()) {
196
197
198
199
	  uhQP.change_dim(nPoints);
	  uh[0]->getVecAtQPs(elInfo, NULL, quadFast, uhQP);
	}

200
	if (degree > 2 && (*it)->secondOrderTerms()) { 
201
202
	  D2UhQp.change_dim(nPoints);
	  uh[0]->getD2AtQPs(elInfo, NULL, quadFast, D2UhQp);
203
204
205
206
207
	}
      }
    }
    
    // === Compute the element residual and store it in irq. ===    
208
    r(elInfo, nPoints, uhQP, D2UhQp, dofMat, dofVec, quad, riq);
209
210
211
212
213
214
    
    
    // === Add integral over r square. ===
    double result = 0.0;
    for (int iq = 0; iq < nPoints; iq++)
      result += quad->getWeight(iq) * riq[iq] * riq[iq];
215
216

    double alpha0 = std::min(h2, kappa_inv);
217
218
   
    if (norm == NO_NORM || norm == L2_NORM)
219
      result = C0 * alpha0 * alpha0 * det * result;
220
    else
221
222
      result = C0 * alpha0 * det * result;

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
    return result;
  }


  double SimpleResidualEstimator::computeJumpResidual(ElInfo *elInfo)
  {
    FUNCNAME("SimpleResidualEstimator::computeJumpResidual()");

    // === Init temporary variables. ===
    double result = 0.0;
    int dow = Global::getGeo(WORLD);
    Element *el = elInfo->getElement();
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
    double det = elInfo->getDet();

    /// === Compute jump on all faces of the current element. ===
    for (int face = 0; face < nNeighbours; face++) {
      // Pointer to neighbouring element.
      Element *neigh = const_cast<Element*>(elInfo->getNeighbour(face));

      // If there is no neighbour, or we have already visited this pair, 
      // then continue with next one.
      if (!(neigh && neigh->getMark()))
	continue;

      // === The next lines are only to set all information about the ===
      // === neighbouring element.                                    ===
      int oppV = elInfo->getOppVertex(face);	
251
252
      el->sortFaceIndices(face, faceIndEl);
      neigh->sortFaceIndices(oppV, faceIndNeigh);	
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
      neighInfo->setElement(const_cast<Element*>(neigh));
      neighInfo->setFillFlag(Mesh::FILL_COORDS);
      
      for (int i = 0; i < dow; i++)
	neighInfo->getCoord(oppV)[i] = elInfo->getOppCoord(face)[i];
      
      for (int i = 0; i < dim; i++) {
	int i1 = faceIndEl[i];
	int i2 = faceIndNeigh[i];
	for (int j = 0; j < dow; j++)
	  neighInfo->getCoord(i2)[j] = elInfo->getCoord(i1)[j];
      }      
	
      // Compute determinant of the neighbouring element.
      double detNeigh = abs(neighInfo->calcGrdLambda(*lambdaNeigh));
           
      // Reset data.
      for (int iq = 0; iq < nPointsSurface; iq++)
	jump[iq].set(0.0);     
      
      // Get solution vector on the nodes of the current element and 
      // its neighbour.
      uh[0]->getLocalVector(el, uhEl);
      uh[0]->getLocalVector(neigh, uhNeigh);

      // Compute the jump of the gradients of the solution over the face.
      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);
	
284
	basFcts->evalGrdUh(*lambda, grdLambda, uhEl, grdUhEl[iq]);
285
286
287
288
289
	
	(*lambda)[oppV] = 0.0;
	for (int i = 0; i < dim; i++)
	  (*lambda)[faceIndNeigh[i]] = surfaceQuad->getLambda(iq, i);
	
290
	basFcts->evalGrdUh(*lambda, *lambdaNeigh, uhNeigh, grdUhNeigh[iq]);
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
	
	grdUhEl[iq] -= grdUhNeigh[iq];
      } // for iq				
      

      // === Compute the action of the second order operators on the jump. ===
      std::vector<double*>::iterator fac;
      std::vector<Operator*>::iterator it;
      DOFMatrix *mat = const_cast<DOFMatrix*>(matrix[0]);
      for (it = mat->getOperatorsBegin(), fac = mat->getOperatorEstFactorBegin(); 
	   it != mat->getOperatorsEnd(); ++it, ++fac) {
	
	if (*fac == NULL || **fac != 0.0) {
	  for (int iq = 0; iq < nPointsSurface; iq++)
	    localJump[iq].set(0.0);
	  
	  (*it)->weakEvalSecondOrder(grdUhEl, localJump);
	  
	  double factor = *fac ? **fac : 1.0;
	  if (factor != 1.0)
	    for (int i = 0; i < nPointsSurface; i++)
	      localJump[i] *= factor;
	  
	  for (int i = 0; i < nPointsSurface; i++)
	    jump[i] += localJump[i];
	}		
      } // for (it = ...
      

      // === Compute the squared integral of the jump and multiply with ===
      // === the appropriate constants.                                 ===
      
      double val = 0.0;
      for (int iq = 0; iq < nPointsSurface; iq++)
	val += surfaceQuad->getWeight(iq) * (jump[iq] * jump[iq]);

      double d = 0.5 * (det + detNeigh);
328
329
330
      double h2 = h2_from_det(d, dim);

      double alpha0 = std::min(h2, kappa_inv);
331
332
   
      if (norm == NO_NORM || norm == L2_NORM)
333
	val *= C1 * alpha0 * d;
334
335
      else
	val *= C1 * d;
336

337
338
339
340
      neigh->setEstimation(neigh->getEstimation(0) + val, 0);
      result += val;
    } // for face
    
341
342
343
    /*
    double val = 
      fh[0]->getBoundaryManager()->boundResidual(elInfo, matrix[0], uh[0]);
344
345
346
347
348
    if (norm == NO_NORM || norm == L2_NORM)
      val *= C1 * h2;
    else
      val *= C1;   
    result += val;
349
350
    */

351
352
353
354
355
356
357

    return result;
  }


  void SimpleResidualEstimator::r(const ElInfo *elInfo,
				  int nPoints,
358
359
				  const mtl::dense_vector<double>& uhIq,
				  const mtl::dense_vector<WorldMatrix<double> > &D2UhIq,
360
361
362
				  DOFMatrix *A, 
				  DOFVector<double> *fh,
				  Quadrature *quad,
363
				  mtl::dense_vector<double>& result)
364
365
366
367
368
369
370
371
372
373
374
375
376
  {
    FUNCNAME("SimpleResidualEstimator::r()");

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

    // === Get left hand side on quadrature points. ===
    for (it = A->getOperatorsBegin(), fac = A->getOperatorEstFactorBegin(); 
	 it != A->getOperatorsEnd(); ++it, ++fac) {
     
      double factor = *fac ? **fac : 1.0;

      if (factor) {
377
378
	if (num_rows(D2UhIq) > 0)
	  (*it)->evalSecondOrder(nPoints, uhIq, grdUhQp, D2UhIq, result, -factor);
379
380

	if (num_rows(uhIq) > 0)
381
	  (*it)->evalZeroOrder(nPoints, uhIq, grdUhQp, D2UhIq, result, factor);
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
      }
    }
    

    // === Get right hand side on quadrature points. ===
    for (it = fh->getOperatorsBegin(), fac = fh->getOperatorEstFactorBegin(); 
	 it != fh->getOperatorsEnd(); ++it, ++fac) {

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

      if (factor) {
	ElementVector fx(nPoints, 0.0);
	(*it)->getC(elInfo, nPoints, fx);
	
	for (int iq = 0; iq < nPoints; iq++)
	  result[iq] -= factor * fx[iq];
      }
    }    
  }


}