ElementLevelSet.cc 10.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// 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.


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
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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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
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
#include "ElementLevelSet.h"
#include "ElInfo.h"

int
ElementLevelSet::createElementLevelSet(const ElInfo *elInfo_,
				       const bool doCalcIntersecPts_)
{
  Element *el = elInfo_->getElement();

  if (elInfo == NULL  ||  el != lastEl) {
    /**
     * Element has changed. New calculation.
     */

    // Set new element.
    lastEl = el;

    // Set information for element and reset data.
    setElement(elInfo_);

    // Calculate level set function values for each vertex of element.
    calculateElementLevelSetVal();

    // Check whether level set function values are not too small.
    checkElementLevelSetVal();

    // Calculate status of element and element vertices.
    calculateElementStatus();

    // Calculate intersection points with zero level set if necessary.
    if (doCalcIntersecPts_ && elStatus == LEVEL_SET_BOUNDARY) {
      calculateIntersecPoints();

      // dim == 3: sort intersection points if necessary
      if (dim == 3 && numIntersecPoints == 4) 
	sortIntersecPoints_4IP3D();	
    }
  }

//   else {
    /**
     * LevelSet-Status for element has already been calculated.
     */
//   }

  return elStatus;
}

void
ElementLevelSet::calculateElementLevelSetVal()
{
  DimVec<double> lambda(dim, DEFAULT_VALUE, 0.0);

  for (int i = 0; i <= dim; i++) {

    lambda[i] = 1.0;
    lSFct->setElInfo(elInfo);
    elVertexLevelSetVec[i] = (*lSFct)((const DimVec<double>) lambda);
    lambda[i] = 0.0;
  }
}

int
ElementLevelSet::calculateElementStatus()
{
  for (int i=0; i<=dim; ++i) {
    if (elVertexLevelSetVec[i] < 0) {
      elVertexStatusVec[i] = LEVEL_SET_INTERIOR;
      numElVertexInterior++;
    }    
    else {
      elVertexStatusVec[i] = LEVEL_SET_EXTERIOR;
      numElVertexExterior++;
    }
  }

  /**
   * Calculate level set status of element.
   */
  if (numElVertexInterior == dim+1) {
    elStatus = LEVEL_SET_INTERIOR;
    return LEVEL_SET_INTERIOR;
  }
  if (numElVertexExterior == dim+1) {
    elStatus = LEVEL_SET_EXTERIOR;
    return LEVEL_SET_EXTERIOR;
  }
  elStatus = LEVEL_SET_BOUNDARY;
  return LEVEL_SET_BOUNDARY;
}

void
ElementLevelSet::calculateIntersecPoints()
{
  //**************************************************************************
  // The level set function is linearly approximated by a hyperplane through 
  // the points of the graph of the level set function in the vertices 
  // of the element.
  // This routine calculates the intersection points of the hyperplane 
  // with the edges of the element.
  //**************************************************************************
  DimVec<double> tempPoint(dim);
  DimVec<double> zeroVec(dim, DEFAULT_VALUE, 0.0);
  int i, j;

  /**
   * Get intersection points (in barycentric coordinates).
   *
   * An edge is intersected if its vertices have level sets with
   * different sign.
   */
  for (i = 0; i <= dim; i++) {
    for (j = i+1; j <= dim; j++) {

      if (elVertexStatusVec[i] * elVertexStatusVec[j] < 0.0) {

	  tempPoint = zeroVec;
	  tempPoint[j] = elVertexLevelSetVec[i] / 
	    (elVertexLevelSetVec[i] - elVertexLevelSetVec[j]);
	  checkIntersecBary(tempPoint[j]);
	  tempPoint[i] = 1 - tempPoint[j];

	  (*elIntersecPoints)[numIntersecPoints] = tempPoint;
	  ++numIntersecPoints;
      }

    } // for(j ...
  } // for(i ...
}

int
ElementLevelSet::checkElementLevelSetVal()
{
  int changed = 0;
  double abs_grd_val = 0.0;
  double abs_min_val = 0.0;
  double comp = 0.0;

  for (int i=0; i<dim; ++i) {
    comp = elVertexLevelSetVec[i]-elVertexLevelSetVec[dim];
    abs_grd_val += comp*comp;
  }
  abs_grd_val = sqrt(abs_grd_val);

  abs_min_val = LS_VAL_TOL*abs_grd_val;
  abs_min_val = (abs_min_val > LS_VAL_MIN) ? abs_min_val : LS_VAL_MIN;

  for (int i=0; i<=dim; ++i) {
    if (fabs(elVertexLevelSetVec[i]) < abs_min_val) {
//       elVertexLevelSetVec[i] = abs_min_val;
      elVertexLevelSetVec[i] = (elVertexLevelSetVec[i] < 0) ?
	-abs_min_val : abs_min_val;
      ++changed;
    }
  }

  return changed;
}

bool
ElementLevelSet::checkIntersecBary(double &bary)
{
  if (bary < SP_BARY_TOL) {
    bary = SP_BARY_TOL;
    return true;
  }
  if (bary > 1-SP_BARY_TOL) {
    bary = 1-SP_BARY_TOL;
    return true;
  }

  return false;
}

void
ElementLevelSet::sortIntersecPoints_4IP3D()
{
  FUNCNAME("sortIntersecPoints_4IP3D");

  int indexFace1 = 0; 
  int indexFace2 = 0; 
  int indexOpVert = 0;
  DimVec<double> tempPoint(dim);
  int i,j;

  /**
   * Consider the 4 intersection points S0, S1, S2 and S3. If the components 
   * of S0 indexFace1 and indexFace2 are not zero, i.e. S0 lies in 
   * the element faces indexFace1 and indexFace2, the intersection 
   * point with zero components indexFace1 and indexFace2 is
   * opposite to S0 in the intersection plane. Move this vertex to the end
   * of the intersection point list elIntersecPoints.
   */ 

  // Get indexFace1 and indexFace2.
  for (i = 0; i < numIntersecPoints; i++) {
    if (fabs((*elIntersecPoints)[0][i]) > 1.e-15) {
      indexFace1 = i;
      break;
    }
  }
  for (j = i+1; j < numIntersecPoints; j++) {
    if (fabs((*elIntersecPoints)[0][j]) > 1.e-15) {
      indexFace2 = j;
      break;
    }
  }

  // Get index of vertex opposite to S0.
  for (i = 1; i < numIntersecPoints; i++) {
    if (fabs((*elIntersecPoints)[i][indexFace1]) <= 1.e-15  && 
	fabs((*elIntersecPoints)[i][indexFace2]) <= 1.e-15) {
      indexOpVert = i;
      break;
    }
  }

  // Move vertex to the end of \ref elIntersecPoints.
  if (indexOpVert != numIntersecPoints-1) {
    tempPoint = (*elIntersecPoints)[indexOpVert];
    (*elIntersecPoints)[indexOpVert] = (*elIntersecPoints)[numIntersecPoints-1];
    (*elIntersecPoints)[numIntersecPoints-1] = tempPoint;
  }

  return;
}

void 
ElementLevelSet::calcIntersecNormal(WorldVector<double> &normalVec)
{
  FUNCNAME("ElementLevelSet::calcIntersecNormal");
  
  switch(dim) {
  case 2: calcIntersecNormal_2d(normalVec);
    break;
  case 3: calcIntersecNormal_3d(normalVec);
    break;
  default: ERROR_EXIT("illegal dimension !\n");
  }
};

void 
ElementLevelSet::calcIntersecNormal_2d(WorldVector<double> &normalVec)
{
  FUNCNAME("ElementLevelSet::calcIntersecNormal_2d");

  TEST_EXIT(numIntersecPoints == 2)("illegal number of intersection points !\n");

  // ===== Get world coordinates of intersection points. =====
  WorldVector<double> sP1;
  WorldVector<double> sP2;
  elInfo->coordToWorld((*elIntersecPoints)[0], sP1);
  elInfo->coordToWorld((*elIntersecPoints)[1], sP2);

  // ===== Calculate normal vector. =====
  double norm2 = 0.0;
  double val;
  for (int i=0; i<dim; ++i){
    val = sP1[i] - sP2[i];
    norm2 += val*val;
    normalVec[dim-1-i] = val;
  } 
  normalVec[0] *= -1;
  double norm = sqrt(norm2);
  for(int i=0; i<dim; ++i) {
    normalVec[i] = 1/norm * normalVec[i];
  }

  // ===== Set correct orientation (exterior normal vector). =====

  // Calculate barycenter.
  WorldVector<double> baryc;
  for (int i=0; i<dim; ++i) {
    baryc[i] = 0.5*sP1[i] + 0.5*sP2[i];
  }
  double d = sqrt((sP1-sP2)*(sP1-sP2));

  // Barycenter + factor*normalVec.
  double elSize = sqrt(fabs(elInfo->getDet()));
  double factor = 0.01*d/elSize;
  WorldVector<double> tmpPoint;
  int cntr = 0;
  DimVec<double> lambda(dim, NO_INIT);
  
  while (1) {
    ++cntr;

    for(int i=0; i<dim; ++i) {
      tmpPoint[i] = baryc[i] + factor*normalVec[i];
    }

    elInfo->worldToCoord(tmpPoint, &lambda);
    for (int i=0; i<=dim; ++i) {
      if (lambda[i] < 0) {
	factor *= 0.1;

	if (cntr == 10) {
	  WARNING("inefficient normal vector calculation !\n");
	}
	if (cntr == 100) {
	  ERROR_EXIT("infinite loop !\n");
	}

	continue;
      }
    }

    break;
  }

  if (ElementLevelSet::calcLevelSetFct(lambda) < 0) {
    for (int i=0; i<dim; ++i) {
      normalVec[i] *= -1;
    }
  }
};

void 
ElementLevelSet::calcIntersecNormal_3d(WorldVector<double> &normalVec)
{
  FUNCNAME("ElementLevelSet::calcIntersecNormal_3d");

  TEST_EXIT(numIntersecPoints == 3 || numIntersecPoints == 4)("illegal number of intersection points !\n");

  // ===== Get world coordinates of intersection points. =====
  WorldVector<double> sP1;
  WorldVector<double> sP2;
  WorldVector<double> sP3;
  elInfo->coordToWorld((*elIntersecPoints)[0], sP1);
  elInfo->coordToWorld((*elIntersecPoints)[1], sP2);
  elInfo->coordToWorld((*elIntersecPoints)[2], sP3);

  // ===== Calculate normal vector. =====
  WorldVector<double> A = sP2-sP1;
  WorldVector<double> B = sP3-sP1;
  vectorProduct(A, B, normalVec);
  
  double norm = sqrt(normalVec * normalVec);
  for(int i=0; i<dim; ++i) {
    normalVec[i] = 1/(norm+1.e-4) * normalVec[i];
  }

  // ===== Set correct orientation (exterior normal vector). =====

  // Calculate barycenter.
  WorldVector<double> baryc;
  for (int i=0; i<dim; ++i) {
    baryc[i] = 1.0/3*sP1[i] + 1.0/3*sP2[i] + 1.0/3*sP3[i];
  }
  double d = sqrt((sP1-sP2)*(sP1-sP2));

  // Barycenter + factor*normalVec.
  double elSize = pow(fabs(elInfo->getDet()), 1.0/3);
  double factor = 0.01*d/elSize;
  WorldVector<double> tmpPoint;
  int cntr = 0;
  DimVec<double> lambda(dim, NO_INIT);
  
  while (1) {
    ++cntr;

    for(int i=0; i<dim; ++i) {
      tmpPoint[i] = baryc[i] + factor*normalVec[i];
    }

    elInfo->worldToCoord(tmpPoint, &lambda);
    for (int i=0; i<=dim; ++i) {
      if (lambda[i] < 0) {
	factor *= 0.1;

	if (cntr == 10) {
	  WARNING("inefficient normal vector calculation !\n");
	}
	if (cntr == 100) {
	  ERROR_EXIT("infinite loop !\n");
	}

	continue;
      }
    }

    break;
  }

  if (ElementLevelSet::calcLevelSetFct(lambda) < 0) {
    for (int i=0; i<dim; ++i) {
      normalVec[i] *= -1;
    }
  }
};

int 
ElementLevelSet::getVertexPos(const DimVec<double> barCoords)
{
  double vertex_val;

  for (int i=0; i<=dim; ++i) {
    if (barCoords[i] > 1-1.e-15) {
      vertex_val = elVertexLevelSetVec[i];
      break;
    }
  }

  if (vertex_val > 0) {
    return LEVEL_SET_EXTERIOR;
  }
  else {
    return LEVEL_SET_INTERIOR;
  }
}

int
ElementLevelSet::getElPos(const DimVec<double> barCoords)
{
  double ls_val = calcLevelSetFct(barCoords);
  if (ls_val > 0) {
    return LEVEL_SET_EXTERIOR;
  }
  else {
    return LEVEL_SET_INTERIOR;
  }
}