CFE_NormAndErrorFcts.h 13.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef AMDIS_CFE_NORMANDERRORFCTS_H
#define AMDIS_CFE_NORMANDERRORFCTS_H

#include "AbstractFunction.h"
#include "DOFVector.h"
#include "FixVec.h"
#include "MemoryManager.h"
#include "Quadrature.h"

#include "ElementLevelSet.h"
#include "ScalableQuadrature.h"
#include "SubPolytope.h"

14
15
16
namespace AMDiS {

  class ElementNorm
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
  public:
    MEMORY_MANAGED(ElementNorm);

    /**
     * Constructor.
     */
    ElementNorm(Quadrature *q_)
      : q(q_),
	nQPts(0)
    {
      if (q)
	nQPts = q->getNumPoints();
    };

    /**
     * Destructor.
     */
    virtual ~ElementNorm()
    {};

    /**
     * Calculates element norm on elInfo.
     */
    virtual double calcElNorm(ElInfo *elInfo, 
			      const double &det, 
			      const double &fac = 1.0) = 0;

    /**
     * Sets quadrature to q_.
     */
    inline void setQuadrature(Quadrature *q_) {
      q = q_;
50
      nQPts = q->getNumPoints();
51
52
53
54
55
56
57
58
59
60
61
62
    };

  protected:
    /**
     * Quadrature formula.
     */
    Quadrature *q;

    /**
     * Number of quadrature points.
     */
    int nQPts;
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
  class ElementL1Norm_Analyt : public ElementNorm
  {
  public:
    MEMORY_MANAGED(ElementL1Norm_Analyt);

    /**
     * Constructor.
     */
    ElementL1Norm_Analyt(Quadrature *q_, 
			 AbstractFunction<double, WorldVector<double> > *f_)
      : ElementNorm(q_),
	f(f_)
    {};

    /**
     * Calculates element norm on elInfo.
     */
    double calcElNorm(ElInfo *elInfo, 
		      const double &det,
		      const double &fac = 1.0);

  protected:
    /**
     * Abstract function for which norm is calculated.
     */
    AbstractFunction<double, WorldVector<double> > *f;
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
  class ElementL2Norm_Analyt : public ElementNorm
  {
  public:
    MEMORY_MANAGED(ElementL2Norm_Analyt);

    /**
     * Constructor.
     */
    ElementL2Norm_Analyt(Quadrature *q_, 
			 AbstractFunction<double, WorldVector<double> > *f_)
      : ElementNorm(q_),
	f(f_)
    {};

    /**
     * Calculates element norm on elInfo.
     */
    double calcElNorm(ElInfo *elInfo, 
		      const double &det,
		      const double &fac = 1.0);

  protected:
    /**
     * Abstract function for which norm is calculated.
     */
    AbstractFunction<double, WorldVector<double> > *f;
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

  class ElementH1Norm_Analyt : public ElementNorm
  {
  public:
    MEMORY_MANAGED(ElementH1Norm_Analyt);

    /**
     * Constructor.
     */
    ElementH1Norm_Analyt(Quadrature *q_, 
			 AbstractFunction<WorldVector<double>, WorldVector<double> > *grd_,
			 int dim_)
      : ElementNorm(q_),
	grd(grd_),
	dim(dim_)
    {};

    /**
     * Calculates element norm on elInfo.
     */
    double calcElNorm(ElInfo *elInfo, 
		      const double &det,
		      const double &fac = 1.0);

  protected:
    /**
     * Abstract function for which norm is calculated.
     */
    AbstractFunction<WorldVector<double>, WorldVector<double> > *grd; 

    /**
     * Mesh dimension.
     */
    int dim;
154
155
  };

156
  class ElementL1Norm_DOF : public ElementNorm
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
  public:
    MEMORY_MANAGED(ElementL1Norm_DOF);

    /**
     * Constructor.
     */
    ElementL1Norm_DOF(Quadrature *q_, DOFVector<double> *dofVec_)
      : ElementNorm(q_),
	dofVec(dofVec_)
    {};

    /**
     * Calculates element norm on elInfo.
     */
    double calcElNorm(ElInfo *elInfo, 
		      const double &det,
		      const double &fac = 1.0);

  protected:
    /**
     * DOF vector for which norm is calculated.
     */
    DOFVector<double> *dofVec;
  };

  class ElementL2Norm_DOF : public ElementNorm
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
  public:
    MEMORY_MANAGED(ElementL2Norm_DOF);

    /**
     * Constructor.
     */
    ElementL2Norm_DOF(Quadrature *q_, DOFVector<double> *dofVec_)
      : ElementNorm(q_),
	dofVec(dofVec_)
    {};

    /**
     * Calculates element norm on elInfo.
     */
    double calcElNorm(ElInfo *elInfo, 
		      const double &det,
		      const double &fac = 1.0);

  protected:
    /**
     * DOF vector for which norm is calculated.
     */
    DOFVector<double> *dofVec;
  };

  class ElementH1Norm_DOF : public ElementNorm
211
  {
212
213
214
215
216
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
  public:
    MEMORY_MANAGED(ElementH1Norm_DOF);

    /**
     * Constructor.
     */
    ElementH1Norm_DOF(Quadrature *q_, DOFVector<double> *dofVec_, int dim_)
      : ElementNorm(q_),
	dofVec(dofVec_),
	dim(dim_)
    {};

    /**
     * Calculates element norm on elInfo.
     */
    double calcElNorm(ElInfo *elInfo, 
		      const double &det,
		      const double &fac = 1.0);

  protected:
    /**
     * DOF vector for which norm is calculated.
     */
    DOFVector<double> *dofVec;

    /**
     * Mesh dimension.
     */
    int dim;
  };

  class ElementL2Err : public ElementNorm
244
  {
245
246
247
248
249
250
251
252
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
284
285
286
287
288
289
290
291
292
293
294
295
  public:
    MEMORY_MANAGED(ElementL2Err);

    /**
     * Constructor.
     */
    ElementL2Err(Quadrature *q_, 
		 AbstractFunction<double, WorldVector<double> > *u_,
		 DOFVector<double> *uh_,
		 int relErr_)
      : ElementNorm(q_),
	u(u_),
	uh(uh_),
	relErr(relErr_),
	nrmU(0.0)
    {};

    /**
     * Calculates element error on elInfo.
     */
    double calcElNorm(ElInfo *elInfo, 
		      const double &det,
		      const double &fac = 1.0);

    /**
     * Get norm of u.
     */
    inline double getNormU() const {
      return nrmU;
    };
  protected:
    /**
     * Abstract function for which error is calculated.
     */
    AbstractFunction<double, WorldVector<double> > *u;

    /**
     * DOF vector for which error is calculated.
     */
    DOFVector<double> *uh;

    /**
     * Indicates whether relative (1) or absolute error (0) is calculated.
     */
    int relErr;

    /**
     * Norm of u in case relative error is calculated.
     */
    double nrmU;
  };
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
  class ElementH1Err : public ElementNorm
  {
  public:
    MEMORY_MANAGED(ElementH1Err);

    /**
     * Constructor.
     */
    ElementH1Err(Quadrature *q_, 
		 AbstractFunction<WorldVector<double>, WorldVector<double> > *grdu_,
		 DOFVector<double> *uh_,
		 int relErr_,
		 int dim_)
      : ElementNorm(q_),
	grdu(grdu_),
	uh(uh_),
	relErr(relErr_),
	nrmGrdU(0.0),
	dim(dim_)
    {};

    /**
     * Calculates element error on elInfo.
     */
    double calcElNorm(ElInfo *elInfo, 
		      const double &det,
		      const double &fac = 1.0);

    /**
     * Get norm of grdu.
     */
    inline double getNormGrdU() const {
      return nrmGrdU;
    };

  protected:
    /**
     * Abstract function for which norm is calculated.
     */
    AbstractFunction<WorldVector<double>, WorldVector<double> > *grdu; 

    /**
     * DOF vector for which error is calculated.
     */
    DOFVector<double> *uh;

    /**
     * Indicates whether relative (1) or absolute error (0) is calculated.
     */
    int relErr;

    /**
     * Norm of grdu in case relative error is calculated.
     */
    double nrmGrdU;

    /**
     * Mesh dimension.
     */
    int dim;
  };

  /////////////////////////////////////////////////////////////////////////////
  ///// class CFE_NormAndErrorFcts ////////////////////////////////////////////
  /////////////////////////////////////////////////////////////////////////////
  class CFE_NormAndErrorFcts
  {
  public:
    MEMORY_MANAGED(CFE_NormAndErrorFcts);

    // ========================================================================
    //  Calculation of L1-Norm, L2-Norm and H1-Norm on domain depending
    //  on the level set function.
    //
    //  Here, the H1-norm is the H1-seminorm, i.e. you get the "common"
    //  H1-norm by
    //     H1_Norm() = sqrt(L2_Norm^2() + H1_Seminorm^2()) .
    //
    //  Parameters for domainFlag:
    //      -3:  all elements in the domain with negative level set function
    //           values which do not intersect the zero level set
    //      -2 : all elements which intersect the domain with negative level set
    //           function values, in particular all (complete) elements which
    //           cut the zero level set
    //      -1 : domain with negative level set function values
    //       0 : all elements cut by the zero level set
    //       1 : complete mesh
    // ========================================================================
    static double L1Norm_Analyt(
				AbstractFunction<double, WorldVector<double> > *f,
				ElementLevelSet *elLS,
				int domainFlag,
				int deg = 1,
				Quadrature* q = NULL);
    static double L2Norm_Analyt(
				AbstractFunction<double, WorldVector<double> > *f,
				ElementLevelSet *elLS,
				int domainFlag,
				int deg = 2,
				Quadrature* q = NULL);
    static double L2NormSquare_Analyt(
				      AbstractFunction<double, WorldVector<double> > *f,
				      ElementLevelSet *elLS,
				      int domainFlag,
				      int deg = 2,
				      Quadrature* q = NULL);
    static double H1Norm_Analyt(
				AbstractFunction<WorldVector<double>, WorldVector<double> > *grd,
				ElementLevelSet *elLS,
				int domainFlag,
				int deg = 0,
				Quadrature* q = NULL);
    static double H1NormSquare_Analyt(
				      AbstractFunction<WorldVector<double>, WorldVector<double> > *grd,
				      ElementLevelSet *elLS,
				      int domainFlag,
				      int deg = 0,
				      Quadrature* q = NULL);
    static double L1Norm_DOF(DOFVector<double> *dof,
			     ElementLevelSet *elLS,
			     int domainFlag,
			     int deg = 1,
			     Quadrature* q = NULL);
    static double L2Norm_DOF(DOFVector<double> *dof,
			     ElementLevelSet *elLS,
			     int domainFlag,
			     int deg = 2,
			     Quadrature* q = NULL);
    static double L2NormSquare_DOF(DOFVector<double> *dof,
				   ElementLevelSet *elLS,
				   int domainFlag,
				   int deg = 2,
				   Quadrature* q = NULL);
    static double H1Norm_DOF(DOFVector<double> *dof,
			     ElementLevelSet *elLS,
			     int domainFlag,
			     int deg = 0,
			     Quadrature* q = NULL);
    static double H1NormSquare_DOF(DOFVector<double> *dof,
				   ElementLevelSet *elLS,
				   int domainFlag,
				   int deg = 0,
				   Quadrature* q = NULL);

    // ========================================================================
    //  Calculation of error between
    //     -> true solution (analytically given) and
    //     -> FE solution (dof vector).
    //
    //  Here, the H1-norm is the H1-seminorm, i.e. you get the "common"
    //  H1-norm by
    //    H1_Norm() = sqrt(L2_Norm^2() + H1_Seminorm^2()) .
    //
    //  Parameter \ref domainFlag:
    //      -3:  all elements in the domain with negative level set function
    //           values which do not intersect the zero level set
    //      -2 : all elements which intersect the domain with negative level set
    //           function values, in particular all (complete) elements which
    //           cut the zero level set
    //      -1 : domain with negative level set function values
    //       0 : all elements cut by the zero level set
    //       1 : complete mesh
    // ========================================================================
    static double L2Err(AbstractFunction<double, WorldVector<double> > *u,
			DOFVector<double> *uh,
			ElementLevelSet *elLS,
			int domainFlag,
			int relErr = 0,
			int deg = 2,
			Quadrature *q = NULL);
    static double H1Err(
			AbstractFunction<WorldVector<double>, WorldVector<double> > *grdU,
			DOFVector<double> *uh,
			ElementLevelSet *elLS,
			int domainFlag,
			int relErr = 0,
			int deg = 0,
			Quadrature *q = NULL);

    /**
     * Get absolute L2 error.
     * (If relative L2 error is calculated, the absolute L2 error is also
     *  calculated and stored in L2_err_abs).
     */
    inline static double getL2ErrAbs()
    {
      return L2_err_abs;
    }

    /**
     * Get absolute H1 error.
     * (If relative H1 error is calculated, the absolute H1 error is also
     *  calculated and stored in H1_err_abs).
     */
    inline static double getH1ErrAbs()
    {
      return H1_err_abs;
    }

    /**
     * Get L2 norm of solution u.
     * (If relative L2 error is calculated, the L2 norm of the solution u is 
     *  also calculated and stored in L2_u_norm).
     */
    inline static double getL2_U_Norm()
    {
      return L2_u_norm;
    }

    /**
     * Get H1 norm of solution u.
     * (If relative H1 error is calculated, the H1 norm of the solution u is 
     *  also calculated and stored in H1_u_norm).
     */
    inline static double getH1_U_Norm()
    {
      return H1_u_norm;
    }

  protected:
    static double Norm_IntNoBound(ElementNorm *elNorm,
				  ElementLevelSet *elLS,
				  Flag fillFlag,
				  int deg,
				  Quadrature* q);
    static double Norm_IntBound(ElementNorm *elNorm,
523
524
525
526
				ElementLevelSet *elLS,
				Flag fillFlag,
				int deg,
				Quadrature* q);
527
    static double Norm_Int(ElementNorm *elNorm,
528
529
530
531
			   ElementLevelSet *elLS,
			   Flag fillFlag,
			   int deg,
			   Quadrature* q);
532
533
534
535
536
537
538
539
540
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
    static double Norm_Bound(ElementNorm *elNorm,
			     ElementLevelSet *elLS,
			     Flag fillFlag,
			     int deg,
			     Quadrature* q);
    static double Norm_Complete(ElementNorm *elNorm,
				ElementLevelSet *elLS,
				Flag fillFlag,
				int deg,
				Quadrature* q);

    /**
     * Calculate norm on subpolytope.
     */
    static double calcSubPolNorm(ElInfo *elInfo,
				 SubPolytope *subPolytope,
				 ElementNorm *elNorm,
				 ScalableQuadrature *scalQuad,
				 const double &subPolFac = 1.0);

  protected:
    /**
     * Absolute L2 error (last L2 error calculation !).
     */
    static double L2_err_abs;

    /**
     * L2 norm of correct solution u (last L2 error calculation !).
     */
    static double L2_u_norm;

    /**
     * Absolute H1 error (last H1 error calculation !).
     */
    static double H1_err_abs;

    /**
     * H1 norm of correct solution u (last H1 error calculation !).
     */
    static double H1_u_norm;
  };

}
575
576

#endif  // AMDIS_CFE_NORMANDERRORFCTS_H