ElementLevelSet.h 13.8 KB
Newer Older
1
2
3
4
5
6
7
8
#ifndef AMDIS_ELEMENTLEVELSET_H
#define AMDIS_ELEMENTLEVELSET_H

#include "ElementFunction.h"
#include "FixVec.h"
#include "Parameters.h"

namespace AMDiS {
9
10
11
  class Element;
  class ElInfo;
  class Mesh;
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
}

using namespace AMDiS;
using namespace std;

// ===========================================================================
// ===== class ElementLevelSet ===============================================
// ===========================================================================
//
// Class Description:
// The class ElementLevelSet contains the functionality for
//    - calculating the intersection points resulting from an intersection
//      of the boundary (level set zero) with an element,
//    - calculating the status of an element, i.e. is the element cut
//      by the zero level set or not.
// The calculation of the intersection points is done as follows:
// The level set function is linearly approximated on the element, i.e. its 
// graph is approximated by the plane through the level set values of the 
// element vertices. We approximate the intersection points by the 
// intersection of the plane with the element edges.
// If in 3-dimensional finite element space the intersection produced 4
// intersection points S0, S1, S2, S3, the intersection points in  
// elIntersecPoints are rearranged so that S1 and S2 divides the intersection
// plane in two (dim - 1)-dimensional simplices.
//
// Constants indicating the level set status of element:
//    LEVEL_SET_INTERIOR   -  element is in domain where level set function
//                            is negative
//    LEVEL_SET_BOUNDARY   -  element is in domain where level set function
//                            is positive
//    LEVEL_SET_EXTERIOR   -  element is cut by the zero level set
//
// Main routines:
// setElementLevelSet()          - Defines the level set function for the
//                                 following calculations.
// createElementLevelSet()       - Calculates level set status of element and 
//                                 intersection points if needed.
// calculateElementLevelSetVal() - Calculates values of the level set 
//                                 function in the element vertices.
// setElement()                  - Sets elInfo.
// getElementLevelSetStatus()    - Returns status of element.
// getElementIntersecPoints()    - Returns intersection points.
// getElVertStatusVec()          - Returns vector with status information 
//                                 for each vertex.
// ===========================================================================
class ElementLevelSet
{
59
public:
60
61
62
63
64
65
66
  /**
   * Constructor.
   */
  ElementLevelSet(const char *name_,
		  ElementFunction<double> *lSFct_,
		  Mesh *mesh_) 
    : name(name_),
67
68
69
70
71
72
73
74
75
76
77
      elInfo(NULL),
      lastEl(NULL),
      level_set_domain(LEVEL_SET_UNDEFINED),
      numIntersecPoints(0),
      elStatus(LEVEL_SET_UNDEFINED),
      numElVertexInterior(0),
      numElVertexBoundary(0),
      numElVertexExterior(0),
      LS_VAL_TOL(1.e-8),
      LS_VAL_MIN(1.e-8),
      SP_BARY_TOL(1.e-7)
78
79
80
81
82
83
84
85
86
87
88
  {
    FUNCNAME("ElementLevelSet::ElementLevelSet()");

    TEST_EXIT(lSFct_ || mesh_)
      ("illegal initialization of ElementLevelSet!\n");

    lSFct = lSFct_;
    mesh = mesh_;
    dim = mesh->getDim();

    elIntersecPoints = 
Thomas Witkowski's avatar
Thomas Witkowski committed
89
      new VectorOfFixVecs<DimVec<double> >(dim, 
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
					   MAX_INTERSECTION_POINTS, 
					   NO_INIT);
    elVertexStatusVec = new int[dim+1];
    elVertexLevelSetVec = new double[dim+1];

    int setElementLevelSetTol = 0;
    GET_PARAMETER(0, name + "->set ElementLevelSet tolerances", "%d", 
		  &setElementLevelSetTol);
    if (setElementLevelSetTol) {

      GET_PARAMETER(0, name + "->LS_VAL_TOL", "%f", &LS_VAL_TOL);
      GET_PARAMETER(0, name + "->LS_VAL_MIN", "%f", &LS_VAL_MIN);
      GET_PARAMETER(0, name + "->SP_BARY_TOL", "%f", &SP_BARY_TOL);

      TEST_EXIT(LS_VAL_TOL > 0)("illegal LS_VAL_TOL\n");
      TEST_EXIT(LS_VAL_MIN > 0)("illegal LS_VAL_MIN\n");
      TEST_EXIT(SP_BARY_TOL > 0)("illegal SP_BARY_TOL\n");
    }
108
  }
109
110
111
112
113
114
115
116
117
118
119

  /**
   * Destructor.
   */
  ~ElementLevelSet()
  {
    if (elVertexStatusVec)
      delete [] elVertexStatusVec;
    if(elVertexLevelSetVec)
      delete [] elVertexLevelSetVec;
    if (elIntersecPoints)
Thomas Witkowski's avatar
Thomas Witkowski committed
120
      delete elIntersecPoints;
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

  /**
   * Calculates LevelSet-status of element and its intersection points 
   * with the zero level set if necessary.
   *
   * Result:
   *   LEVEL_SET_BOUNDARY: Element elInfo is intersected by levelSetFct.
   *   LEVEL_SET_EXTERIOR / LEVEL_SET_INTERIOR: Element lies completely on 
   *     one side of the zero level set. 
   *
   * After proceeding this function, information about the level set 
   * status is given in:
   *   elStatus: status of element (LEVEL_SET_BOUNDARY, LEVEL_SET_INTERIOR or
   *             EXTERIOR)
   *   elVertexStatusVec: stores status of each vertex of element
   *   elVertexLevelSetVec: stores level set function value of each vertex of
   *                        element
   *   numElVertexInterior: number of vertices of element with status
   *                        LEVEL_SET_INTERIOR
   *   numElVertexExterior: number of vertices of element with status
   *                        LEVEL_SET_EXTERIOR
   *   numElVertexBoundary: number of vertices of element with status
   *                        LEVEL_SET_BOUNDARY
   *   elIntersecPoints: stores the intersection points produced by the 
   *                     intersection of element with the zero level set
   *   numIntersecPoints: number of intersection points
   */
  int createElementLevelSet(const ElInfo *elInfo_, 
			    const bool doCalcIntersecPts_ = true);

152
153
  /// Gets value of level set function at point given in barycentric coordinates.
  inline const double calcLevelSetFct(const DimVec<double>& bary) {
154
    return (*lSFct)(bary);
155
  }
156

157
  /// Resets level set information on element.
158
159
160
161
162
163
164
165
  inline void resetElement() {
    FUNCNAME("ElementLevelSet::resetElement");

    numElVertexInterior = 0;
    numElVertexBoundary = 0;
    numElVertexExterior = 0;
    numIntersecPoints = 0;
    elStatus = LEVEL_SET_UNDEFINED;
166
  }
167
168
169
170
171
172
173

  /**
   * Defines current element (elInfo).
   */
  inline void setElement(const ElInfo *elInfo_) {
    elInfo = elInfo_;
    resetElement();
174
  }
175
176
177
178
179
180
181
182
183
184

  /**
   * Set level_set_domain.
   */
  inline void setLevelSetDomain(int status_) {

    TEST_EXIT(status_ == LEVEL_SET_INTERIOR ||
	      status_ == LEVEL_SET_EXTERIOR ||
	      status_ == LEVEL_SET_BOUNDARY)("illegal level set status !\n");
    level_set_domain = status_;
185
  }
186
187
188
189
190
191
192
193
194
195
196
197
198

  /**
   * Functions to set tolerances for intersection point calculation.
   */
  inline void setLsValTol(double tol) {LS_VAL_TOL = tol;};
  inline void setLsValMin(double min) {LS_VAL_MIN = min;};
  inline void setSpBaryTol(double tol) {SP_BARY_TOL = tol;};

  /**
   * Get level_set_domain.
   */
  inline const int& getLevelSetDomain() const {
    return level_set_domain;
199
  }
200
201
202
203
204
205

  /**
   * Get LevelSet-Status of element.
   */
  inline const int& getElementLevelSetStatus() const {
    return elStatus;
206
  }
207
208
209
210
211
212
213
214

  /**
   * Get number of vertices which are intersection points.
   */
  inline const int& getNumVertIntPoints() const {
    FUNCNAME("ElementLevelSet::getNumVertIntPoints");
    TEST_EXIT(numElVertexBoundary == 0)("numElVertexBoundary should be zero!\n");
    return numElVertexBoundary;
215
  };
216
217
218
219
220
221

  /**
   * Get vector elVertexStatusVec.
   */
  inline const int *getElVertStatusVec() const {
    return elVertexStatusVec;
222
  }
223
224
225
226
227
228

  /**
   * Get i-th component of vector elVertexLevelSetVec.
   */
  inline const double getElVertLevelSetVec(const int i) const {
    return elVertexLevelSetVec[i];
229
  }
230
231
232
233
234
235

  /**
   * Get vector elVertexLevelSetVec.
   */
  inline const double *getElVertLevelSetVec() const {
    return elVertexLevelSetVec;
236
  }
237
238
239
240
241
242

  /**
   * Get levelSetFct.
   */
  inline ElementFunction<double> *getLevelSetFct() const {
    return lSFct;
243
  }
244
245
246
247
248
249

  /**
   * Get mesh.
   */
  inline Mesh *getMesh() const {
    return mesh;
250
  }
251
252
253
254
255
256

  /**
   * Get dim.
   */
  inline int getDim() const {
    return dim;
257
  }
258
259
260
261
262
263

  /**
   * Get the intersection points.
   */
  inline VectorOfFixVecs<DimVec<double> > *getElIntersecPoints() const {
    return elIntersecPoints;
264
  }
265
266
267
268
269
270

  /**
   * Get number of intersection points.
   */
  inline int getNumElIntersecPoints() const {
    return numIntersecPoints;
271
  }
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

  /**
   * Calculate exterior normal to intersection plane.
   */
  void calcIntersecNormal(WorldVector<double> &normal);

  /**
   * Gets position of point in element with barycentric coordinates
   * barCoords, i.e. whether point is in the domain with positive
   * (LEVEL_SET_EXTERIOR) or negative (LEVEL_SET_INTERIOR) level set 
   * function values. Uses level set function, thus element vertices
   * may have level set function value zero.
   */
  int getElPos(const DimVec<double> barCoords);

  /**
   * Gets position of element vertex given in barycentric coordinates,
   * i.e. whether element vertex is in the domain with positive
   * (LEVEL_SET_EXTERIOR) or negative (LEVEL_SET_INTERIOR) level set 
   * function values. Uses elVertexLevelSetVec.
   */
  int getVertexPos(const DimVec<double> barCoords);

295
protected:
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
  /**
   * Calculates level set value of each vertex of element.
   */
  void calculateElementLevelSetVal();

  /**
   * Calculates the status of an element.
   *
   * Note: Uses values in elVertexLevelSetVec.
   *
   * Return value:
   *   LEVEL_SET_INTERIOR   element lies completely inside
   *   LEVEL_SET_EXTERIOR   element lies completely outside
   *   LEVEL_SET_BOUNDARY   element is cut by the zero level set
   */
  int calculateElementStatus();

  /**
   * Calculates intersection points of zero level set with element.
   *
   * Note: Uses elVertexLevelSet.
   */
  void calculateIntersecPoints();

  /**
   * Checks whether level set values of element (in elVertexLevelSetVec) 
   * are below a certain bound and corrects them if this is the case.
   *
   * Return value: number of values corrected
   */
  int checkElementLevelSetVal();

  /**
   * Checks whether barycentric coordinate of intersection point is not 
   * too small and corrects it if this is the case.
   *
   * Return value: true  - barycentric coordinate has been corrected
   *               false - barycentric coordinate is ok 
   */
  bool checkIntersecBary(double &bary);
  
  /**
   * Sort intersection points S0, S1, S2 and S3 in \ref elIntersecPoints in 
   * such a way that afterwards, a line through S1 and S2 divides the 
   * intersection plane into two (\ref dim - 1)-dimensional simplices.
   */
  void sortIntersecPoints_4IP3D();
  
  /**
   * Calculate exterior normal to intersection plane for dimension 2.
   */
  void calcIntersecNormal_2d(WorldVector<double> &normal);

  /**
   * Calculate exterior normal to intersection plane for dimension 3.
   */
  void calcIntersecNormal_3d(WorldVector<double> &normal);

354
public:
355
356
357
358
359
360
361
362
  /**
   * Constants characterizing element position with respect to zero level set.
   */
  static const int LEVEL_SET_INTERIOR = -1;
  static const int LEVEL_SET_BOUNDARY = 0;
  static const int LEVEL_SET_EXTERIOR = 1;
  static const int LEVEL_SET_UNDEFINED = -2;

363
protected:
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
  /** 
   * Name of this object.
   */
  std::string name;

  /**
   * Level set function. 
   */
  ElementFunction<double> *lSFct;

  /**
   * Mesh.
   */
  Mesh *mesh;

  /**
   * elInfo of element.
   */
  const ElInfo *elInfo;

  /**
   * Pointer to last element processed calculations on whithin this class.
   */
  Element *lastEl;

  /**
   * Indicator which can be used for example for function evaluation 
   * or integration on subelements. Indicates whether point/subelement ... 
   * is inside (LEVEL_SET_INTERIOR) or outside (LEVEL_SET_EXTERIOR) the 
   * zero level set or is cut by the zero level set (LEVEL_SET_BOUNDARY).
   */
  int level_set_domain;

  /**
   * Dimension of the problem. dim + 1 is the number of vertices 
   * of element.
   */
  int dim;

  /**
   * Vector for intersection points produced by the intersection of linearly 
   * approximated level set function with the edges of element.
   */
  VectorOfFixVecs<DimVec<double> > *elIntersecPoints;

  /**
   * Number of intersection points.
   */
  int numIntersecPoints;

  /**
   * LevelSet-Status of element.
   */
  int elStatus;

  /**
   * Holds for each vertex of element the information about the position 
   * of the vertex with respect to the zero level set.
   */
  int *elVertexStatusVec;

  /**
   * Stores for each vertex of element the level set of the vertex.
   */
  double *elVertexLevelSetVec;

  /**
   * Number of vertices in element with level set status LEVEL_SET_INTERIOR.
   */
  int numElVertexInterior;

  /**
   * Number of vertices in element with level set status LEVEL_SET_BOUNDARY.
   *
   * Note: should be zero
   */
  int numElVertexBoundary;

  /**
   * Number of vertices in element with level set status LEVEL_SET_EXTERIOR.
   */
  int numElVertexExterior;

  /**
   * Tolerance used in the calculation of the local minimal level set value.
   * The local minimal level set value depends on the gradient of the 
   * level set function.
   * Used for the calculation of intersection points.
   *
   * If intersection points are too close to vertices, they are slightly
   * moved.
   * IDEA: If d is the distance of an intersection point to vertex v, 
   *       the property
   *
   *           d > LS_VAL_TOL * h
   *
   *       must be satisfied.
   * In the implementation this results in
   *
   *           phi(v) > LS_VAL_TOL * h * grad .
   */
  double LS_VAL_TOL;

  /**
   * Lower bound for level set value on elements cut by the zero level set.
   * Used for the calculation of intersection points.
   */
  double LS_VAL_MIN;

  /**
   * Lower bound for barycentric coordinates of intersection points.
   * 
   * Each component x of the barycentric coordinates of an intersection 
   * point satisfies
   *
   *      SP_BARY_TOL < x < 1 - SP_BARY_TOL .
   */
  double SP_BARY_TOL;

  /*
   * Maximum number of intersection points.
   */
  static const int MAX_INTERSECTION_POINTS = 4;
};

#endif  // AMDIS_ELEMENTLEVELSET_H