ElementLevelSet.h 14.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ==  http://www.amdis-fem.org                                              ==
// ==                                                                        ==
// ============================================================================
//
// 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.




22
23
24
#ifndef AMDIS_ELEMENTLEVELSET_H
#define AMDIS_ELEMENTLEVELSET_H

25
#include "AMDiS_fwd.h"
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
#include "ElementFunction.h"
#include "FixVec.h"
#include "Parameters.h"

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
{
 public:
  ElementLevelSet(const char *name_,
		  ElementFunction<double> *lSFct_,
		  Mesh *mesh_) 
    : name(name_),
80
81
82
83
84
85
86
87
88
89
90
      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)
91
92
93
94
95
96
97
98
99
100
  {
    FUNCNAME("ElementLevelSet::ElementLevelSet()");

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
101
102
103
104
105
    elIntersecPoints = new VectorOfFixVecs<DimVec<double> >(dim, 
							    MAX_INTERSECTION_POINTS, 
							    NO_INIT);
    elVertexStatusVec = new int[dim + 1];
    elVertexLevelSetVec = new double[dim + 1];
106
107
108
109
110
111
112
113
114
115
116
117
118
119

    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");
    }
120
  }
121
122
123
124
125
126
127
128

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

  /**
   * 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);

Thomas Witkowski's avatar
Thomas Witkowski committed
161
162
163
  /// Gets value of level set function at point given in barycentric coordinates.
  inline double calcLevelSetFct(const DimVec<double>& bary) 
  {
164
    return (*lSFct)(bary);
Thomas Witkowski's avatar
Thomas Witkowski committed
165
  }
166

Thomas Witkowski's avatar
Thomas Witkowski committed
167
168
169
  /// Resets level set information on element.
  inline void resetElement() 
  {
170
171
172
173
174
175
176
    FUNCNAME("ElementLevelSet::resetElement");

    numElVertexInterior = 0;
    numElVertexBoundary = 0;
    numElVertexExterior = 0;
    numIntersecPoints = 0;
    elStatus = LEVEL_SET_UNDEFINED;
Thomas Witkowski's avatar
Thomas Witkowski committed
177
  }
178

Thomas Witkowski's avatar
Thomas Witkowski committed
179
180
181
  /// Defines current element (elInfo).
  inline void setElement(const ElInfo *elInfo_) 
  {
182
183
    elInfo = elInfo_;
    resetElement();
Thomas Witkowski's avatar
Thomas Witkowski committed
184
  }
185

Thomas Witkowski's avatar
Thomas Witkowski committed
186
187
188
  /// Set level_set_domain.
  inline void setLevelSetDomain(int status_) 
  {
189
190
191
192
    TEST_EXIT(status_ == LEVEL_SET_INTERIOR ||
	      status_ == LEVEL_SET_EXTERIOR ||
	      status_ == LEVEL_SET_BOUNDARY)("illegal level set status !\n");
    level_set_domain = status_;
Thomas Witkowski's avatar
Thomas Witkowski committed
193
  }
194

Thomas Witkowski's avatar
Thomas Witkowski committed
195
196
197
198
199
  /// Functions to set tolerances for intersection point calculation.
  inline void setLsValTol(double tol) 
  {
    LS_VAL_TOL = tol;
  }
200

Thomas Witkowski's avatar
Thomas Witkowski committed
201
202
203
204
205
206
207
208
209
210
211
212
213
  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 
  {
214
    return level_set_domain;
Thomas Witkowski's avatar
Thomas Witkowski committed
215
  }
216

Thomas Witkowski's avatar
Thomas Witkowski committed
217
218
219
  /// Get LevelSet-Status of element.
  inline const int& getElementLevelSetStatus() const 
  {
220
    return elStatus;
Thomas Witkowski's avatar
Thomas Witkowski committed
221
  }
222

Thomas Witkowski's avatar
Thomas Witkowski committed
223
224
225
  /// Get number of vertices which are intersection points.
  inline const int& getNumVertIntPoints() const 
  {
226
227
228
    FUNCNAME("ElementLevelSet::getNumVertIntPoints");
    TEST_EXIT(numElVertexBoundary == 0)("numElVertexBoundary should be zero!\n");
    return numElVertexBoundary;
Thomas Witkowski's avatar
Thomas Witkowski committed
229
  };
230

Thomas Witkowski's avatar
Thomas Witkowski committed
231
232
233
  /// Get vector elVertexStatusVec.
  inline const int *getElVertStatusVec() const 
  {
234
    return elVertexStatusVec;
Thomas Witkowski's avatar
Thomas Witkowski committed
235
  }
236

Thomas Witkowski's avatar
Thomas Witkowski committed
237
238
239
  /// Get i-th component of vector elVertexLevelSetVec.
  inline const double getElVertLevelSetVec(const int i) const 
  {
240
    return elVertexLevelSetVec[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
241
  }
242

Thomas Witkowski's avatar
Thomas Witkowski committed
243
244
245
  /// Get vector elVertexLevelSetVec.
  inline const double *getElVertLevelSetVec() const 
  {
246
    return elVertexLevelSetVec;
Thomas Witkowski's avatar
Thomas Witkowski committed
247
  }
248

Thomas Witkowski's avatar
Thomas Witkowski committed
249
250
251
  /// Get levelSetFct.
  inline ElementFunction<double> *getLevelSetFct() const 
  {
252
    return lSFct;
Thomas Witkowski's avatar
Thomas Witkowski committed
253
  }
254

Thomas Witkowski's avatar
Thomas Witkowski committed
255
256
257
  /// Get mesh.
  inline Mesh *getMesh() const 
  {
258
    return mesh;
Thomas Witkowski's avatar
Thomas Witkowski committed
259
  }
260

Thomas Witkowski's avatar
Thomas Witkowski committed
261
262
263
  /// Get dim.
  inline int getDim() const 
  {
264
    return dim;
Thomas Witkowski's avatar
Thomas Witkowski committed
265
  }
266

Thomas Witkowski's avatar
Thomas Witkowski committed
267
268
269
  /// Get the intersection points.
  inline VectorOfFixVecs<DimVec<double> > *getElIntersecPoints() const 
  {
270
    return elIntersecPoints;
Thomas Witkowski's avatar
Thomas Witkowski committed
271
  }
272

Thomas Witkowski's avatar
Thomas Witkowski committed
273
274
275
  /// Get number of intersection points.
  inline int getNumElIntersecPoints() const 
  {
276
    return numIntersecPoints;
Thomas Witkowski's avatar
Thomas Witkowski committed
277
  }
278

Thomas Witkowski's avatar
Thomas Witkowski committed
279
  /// Calculate exterior normal to intersection plane.
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
  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);

 protected:
Thomas Witkowski's avatar
Thomas Witkowski committed
300
  /// Calculates level set value of each vertex of element.
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
  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);

 public:
  /**
   * 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;

 protected:
  /** 
   * 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