Refinement_Level.h 13.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/******************************************************************************
 *
 * Extension of AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: Simon Praetorius et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/


#ifndef EXTENSIONS_REFINEMENT_LEVEL_PHASEFIELD_H
#define EXTENSIONS_REFINEMENT_LEVEL_PHASEFIELD_H
21
22
23
24
25

#include "ElementFunction.h"

using namespace AMDiS;

Praetorius, Simon's avatar
Praetorius, Simon committed
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

class RefinementLevelSimple : public RefinementLevel<WorldVector<double>, int >
{
public:
  RefinementLevelSimple(const FiniteElemSpace *feSpace_, int level_) :
    RefinementLevel<WorldVector<double>, int >(feSpace_), refineLevel(level_) { }
    
  ~RefinementLevelSimple() {}

  void markElements(Flag &markFlag) 
  {
    bool elMarkRefine = false, elMarkCoarsen = false;

    Flag traverseFlag = Mesh::CALL_LEAF_EL;
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, traverseFlag);
    while (elInfo) {
      int oldLevel = elInfo->getLevel();
      elInfo->getElement()->setMark( calcMark(refineLevel, oldLevel) );

      elMarkRefine |= elInfo->getElement()->getMark() == 1;
      elMarkCoarsen |= elInfo->getElement()->getMark() == -1;
      elInfo = stack.traverseNext(elInfo);
    }
    
    markFlag = 0;
    if (elMarkRefine) 
      markFlag = 1;
    if (elMarkCoarsen) 
      markFlag |= 2;
  }
  
protected:
  int refineLevel;
};

62
63
64
65
66
67
68
69
70
/** \brief
 * Refinement structure to perform local anisotropic refinement depending
 * on a refineFunction that defines the local refinement level to given
 * coordinates.
 **/
class RefinementLevelCoords : public RefinementLevel<WorldVector<double>, int >
{
public:
  RefinementLevelCoords(const FiniteElemSpace *feSpace_, MeshRefinementFunction<WorldVector<double>, int >* refineFct_) :
Praetorius, Simon's avatar
Praetorius, Simon committed
71
72
73
    RefinementLevel<WorldVector<double>, int >(feSpace_, refineFct_) { }
    
  ~RefinementLevelCoords() {}
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

  void markElements(Flag &markFlag) 
  {
    FixVec<WorldVector<double>, VERTEX> coords(mesh->getDim(), NO_INIT);
    WorldVector<double> center;

    bool elMarkRefine = false, elMarkCoarsen = false;

    Flag traverseFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS;
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, traverseFlag);
    while (elInfo) {
      coords = elInfo->getCoords();
      center.set(0.0);
      for (int i = 0; i < coords.size(); ++i) {
	center += coords[i];
      }
      center *= 1.0 / static_cast<double>(coords.size());
      int refineLevel = (*refineFct)(center);
      int oldLevel = elInfo->getLevel();
      elInfo->getElement()->setMark( calcMark(refineLevel, oldLevel) );

      elMarkRefine |= elInfo->getElement()->getMark() == 1;
      elMarkCoarsen |= elInfo->getElement()->getMark() == -1;
      elInfo = stack.traverseNext(elInfo);
    }
    
    markFlag = 0;
    if (elMarkRefine) 
      markFlag = 1;
    if (elMarkCoarsen) 
      markFlag |= 2;
Praetorius, Simon's avatar
Praetorius, Simon committed
106
  }
107
108
109
110
111
112
113
114
115
116
117
118
};


/** \brief
 * Refinement structure to perform local anisotropic refinement depending
 * on a refineFunction that defines the local refinement level to given
 * coordinates.
 **/
class RefinementLevelCoords2 : public RefinementLevel<double, int >
{
public:
  RefinementLevelCoords2(const FiniteElemSpace *feSpace_, MeshRefinementFunction<double, int >* refineFct_, AbstractFunction<double, WorldVector<double> > *fct_) :
Praetorius, Simon's avatar
Praetorius, Simon committed
119
120
121
    RefinementLevel<double, int >(feSpace_, refineFct_), fct(fct_) { }
    
  ~RefinementLevelCoords2() {}
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

  void markElements(Flag &markFlag)
  {
    FixVec<WorldVector<double>, VERTEX> coords(mesh->getDim(), NO_INIT);
    WorldVector<double> center;

    bool elMarkRefine = false, elMarkCoarsen = false;

    Flag traverseFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS;
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, traverseFlag);
    while (elInfo) {
      coords = elInfo->getCoords();
      center.set(0.0);
      for (int i = 0; i < coords.size(); ++i) {
	center += coords[i];
      }
      center *= 1.0 / static_cast<double>(coords.size());
      double value = (*fct)(center);
      int refineLevel = (*refineFct)(value);
      int oldLevel = elInfo->getLevel();
      elInfo->getElement()->setMark( calcMark(refineLevel, oldLevel) );

      elMarkRefine |= elInfo->getElement()->getMark() == 1;
      elMarkCoarsen |= elInfo->getElement()->getMark() == -1;
      elInfo = stack.traverseNext(elInfo);
    }

    markFlag = 0;
    if (elMarkRefine)
      markFlag = 1;
    if (elMarkCoarsen)
      markFlag |= 2;
Praetorius, Simon's avatar
Praetorius, Simon committed
155
  }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

private:
  AbstractFunction<double, WorldVector<double> > *fct;
};


/** \brief
 * Refinement structure to perform local anisotropic refinement depending
 * on a refineFunction that defines the local refinement level to a given
 * value, like phase-value or distance-value.
 **/
class RefinementLevelDOF : public RefinementLevel<double, int>
{
public:
  RefinementLevelDOF(const FiniteElemSpace *feSpace_, MeshRefinementFunction<double, int>* refineFct_, DOFVector<double> *vec_) :
    RefinementLevel<double, int>(feSpace_, refineFct_), 
Praetorius, Simon's avatar
Praetorius, Simon committed
172
173
174
    vec(vec_) { }
    
  ~RefinementLevelDOF() {}
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

  void markElements(Flag &markFlag) 
  {
    double meanValue;

    ElementFunctionDOFVec<double> elFct(vec);
    DimVec<double> lambda(mesh->getDim(), NO_INIT);
    for (int i = 0; i < lambda.getSize(); ++i)
      lambda[i] = 1.0 / lambda.getSize();

    bool elMarkRefine = false, elMarkCoarsen = false;

    Flag traverseFlag = Mesh::CALL_LEAF_EL;
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, traverseFlag);
    while (elInfo) {
      elFct.setElInfo(elInfo);
      meanValue = elFct(lambda);

      int refineLevel = (*refineFct)(meanValue);
      int oldLevel = elInfo->getLevel();
      elInfo->getElement()->setMark( calcMark(refineLevel, oldLevel) );

      elMarkRefine |= elInfo->getElement()->getMark() == 1;
      elMarkCoarsen |= elInfo->getElement()->getMark() == -1;
      elInfo = stack.traverseNext(elInfo);
    }
		
    markFlag = 0;
    if (elMarkRefine) 
      markFlag = 1;
    if (elMarkCoarsen) 
      markFlag |= 2;
Praetorius, Simon's avatar
Praetorius, Simon committed
208
  }
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

private:

  DOFVector<double>* vec;
};


/** \brief
 * Refinement structure to perform local anisotropic refinement depending
 * on a refineFunction that defines the local refinement level to a given
 * value, like phase-value or distance-value, and the center coords of the
 * element.
 **/
class RefinementLevelCoordsDOF : public RefinementLevel<std::pair<WorldVector<double>, double>, int >
{
public:

  RefinementLevelCoordsDOF(const FiniteElemSpace *feSpace_, MeshRefinementFunction<std::pair<WorldVector<double>, double>, int >* refineFct_, DOFVector<double> *vec_) :
Praetorius, Simon's avatar
Praetorius, Simon committed
227
228
229
    RefinementLevel<std::pair<WorldVector<double>, double>, int >(feSpace_, refineFct_), vec(vec_) { }
    
  ~RefinementLevelCoordsDOF() {}
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

  void markElements(Flag &markFlag) 
  {
    double meanValue;
    FixVec<WorldVector<double>, VERTEX> coords(mesh->getDim(), NO_INIT);
    WorldVector<double> center;

    ElementFunctionDOFVec<double> elFct(vec);
    DimVec<double> lambda(mesh->getDim(), NO_INIT);
    for (int i= 0; i<lambda.getSize(); ++i)
      lambda[i]= 1.0/lambda.getSize();

    bool elMarkRefine= false, elMarkCoarsen= false;

    Flag traverseFlag= Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS;
    TraverseStack stack;
    ElInfo *elInfo= stack.traverseFirst(mesh, -1, traverseFlag);
    while (elInfo) {
      elFct.setElInfo(elInfo);
      meanValue = elFct(lambda);
      coords = elInfo->getCoords();
      center.set(0.0);
      for (int i = 0; i < coords.size(); ++i) {
        center += coords[i];
      }
      center *= 1.0 / static_cast<double>(coords.size());
256
      int refineLevel = (*refineFct)(std::make_pair(center, meanValue));
257
258
259
260
261
262
263
264
265
266
267
268
269
      int oldLevel = elInfo->getLevel();
      elInfo->getElement()->setMark( calcMark(refineLevel, oldLevel) );

      elMarkRefine |= elInfo->getElement()->getMark() == 1;
      elMarkCoarsen |= elInfo->getElement()->getMark() == -1;
      elInfo = stack.traverseNext(elInfo);
    }
                
    markFlag = 0;
    if (elMarkRefine) 
      markFlag = 1;
    if (elMarkCoarsen) 
      markFlag |= 2;
Praetorius, Simon's avatar
Praetorius, Simon committed
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

private:

  DOFVector<double>* vec;
};


/** \brief
 * Refinement structure to perform local anisotropic refinement depending
 * on a refineFunction that defines the local refinement level to a given
 * list of values, like phase-values or distance-values.
 **/
class RefinementLevelDOFList : public RefinementLevel< std::vector<double>, int >
{
public:
  RefinementLevelDOFList(
      const FiniteElemSpace *feSpace_, 
      MeshRefinementFunction< std::vector<double>, int >* refineFct_, 
      DOFVector<double>* vec1_, 
      DOFVector<double>* vec2_ = NULL,
      DOFVector<double>* vec3_ = NULL) : 
    RefinementLevel< std::vector<double>, int >(feSpace_, refineFct_)
  {
    vecs.push_back(vec1_);
    if(vec2_ != NULL) vecs.push_back(vec2_);
    if(vec3_ != NULL) vecs.push_back(vec3_);			
Praetorius, Simon's avatar
Praetorius, Simon committed
297
298
299
  }
    
  ~RefinementLevelDOFList() {}
300
301

  void markElements(Flag &markFlag) 
Praetorius, Simon's avatar
Praetorius, Simon committed
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
    bool elMarkRefine = false, elMarkCoarsen = false;

    std::vector<ElementFunctionDOFVec<double>*> elFcts;
    for (unsigned i = 0; i < vecs.size(); ++i) {
      elFcts.push_back(new ElementFunctionDOFVec<double>(vecs[i]));
    }
    DimVec<double> lambda(mesh->getDim(), NO_INIT);
    for (int i= 0; i < lambda.getSize(); ++i)
      lambda[i] = 1.0 / lambda.getSize();

      Flag traverseFlag = Mesh::CALL_LEAF_EL;
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, traverseFlag);
      while(elInfo) {
	std::vector<double> meanValues;
	for (unsigned v = 0; v < vecs.size(); ++v) {
          elFcts[v]->setElInfo(elInfo);
	  meanValues.push_back( (*(elFcts[v]))(lambda) );
	}
	
        int refineLevel = (*refineFct)(meanValues);
        int oldLevel = elInfo->getLevel();
        elInfo->getElement()->setMark( calcMark(refineLevel, oldLevel) );

	elMarkRefine |= elInfo->getElement()->getMark() == 1;
	elMarkCoarsen |= elInfo->getElement()->getMark() == -1;
	elInfo = stack.traverseNext(elInfo);
    }
      
    markFlag= 0;
    if(elMarkRefine) 
	    markFlag= 1;
    if(elMarkCoarsen) 
	    markFlag|= 2;
Praetorius, Simon's avatar
Praetorius, Simon committed
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
private:
	std::vector<DOFVector<double>*> vecs;
};



/** \brief
 * Refinement structure to perform local anisotropic refinement depending
 * on a refineFunction that defines the local refinement level to a given
 * list of values, like phase-values or distance-values.
 **/
class RefinementLevelES : public RefinementLevel< std::vector<double>, int >
{
public:
  RefinementLevelES(
      const FiniteElemSpace *feSpace_, 
      MeshRefinementFunction< std::vector<double>, int >* refineFct_, 
      DOFVector<double>* vec1_, 
      DOFVector<double>* vec2_, 
      DOFVector<double>* phase_,
      double tol_) : 
    RefinementLevel< std::vector<double>, int >(feSpace_, refineFct_),
    phase(phase_),
    tol(tol_)
  {
    vecs.push_back(vec1_);
    vecs.push_back(vec2_);
Praetorius, Simon's avatar
Praetorius, Simon committed
366
367
368
  }
    
  ~RefinementLevelES() {}
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

  void markElements(Flag &markFlag) 
  {
    FUNCNAME("RefinementLevelDOF::markElements()");

    const BasisFunction *basFcts = feSpace->getBasisFcts();
    int numBasFcts = basFcts->getNumber();

    DegreeOfFreedom *localIndices = new DegreeOfFreedom[numBasFcts];
    FixVec<WorldVector<double>, VERTEX> coords(mesh->getDim(), NO_INIT);

    bool elMarkRefine = false, elMarkCoarsen = false;

    std::vector<ElementFunctionDOFVec<double>*> elFcts;
    for (unsigned i = 0; i < vecs.size(); ++i) {
      elFcts.push_back(new ElementFunctionDOFVec<double>(vecs[i]));
    }
    ElementFunctionDOFVec<double>* elFct_phase = new ElementFunctionDOFVec<double>(phase);
    DimVec<double> lambda(mesh->getDim(), NO_INIT);
    for (int i= 0; i < lambda.getSize(); ++i)
      lambda[i] = 1.0 / lambda.getSize();

    Flag traverseFlag = Mesh::CALL_LEAF_EL;
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, traverseFlag);

//     int n = 0;
//     double mean = 0.0;
//     while(elInfo) {
//       elFct_phase->setElInfo(elInfo);
//       if ((*elFct_phase)(lambda) > 0.001) {
//         std::vector<double> meanValues;
//         meanValues.push_back(0.0);
//         for (unsigned v = 0; v < vecs.size(); ++v) {
//           elFcts[v]->setElInfo(elInfo);
//           meanValues.push_back( (*(elFcts[v]))(lambda) );
//         }
//         
//         double value = refineFct->indicator(meanValues);
//         mean += value;
//         n++;
//       }
//       elInfo = stack.traverseNext(elInfo);
//     }
//     mean /= n;
//     MSG("mean value = %e\n", mean);

    elInfo = stack.traverseFirst(mesh, -1, traverseFlag);
    int nCoarse = 0;
    while(elInfo) {
      elFct_phase->setElInfo(elInfo);
      if ((*elFct_phase)(lambda) > 0.0001) {
        std::vector<double> meanValues;
        meanValues.push_back(tol);
        for (unsigned v = 0; v < vecs.size(); ++v) {
          elFcts[v]->setElInfo(elInfo);
          meanValues.push_back( (*(elFcts[v]))(lambda) );
        }
        
        int refineLevel = (*refineFct)(meanValues);
        nCoarse += refineLevel;
        elInfo->getElement()->setMark( refineLevel );
      } else {
        elInfo->getElement()->setMark( -1 );
      }

      elMarkRefine |= elInfo->getElement()->getMark() == 1;
      elMarkCoarsen |= elInfo->getElement()->getMark() == -1;
      elInfo = stack.traverseNext(elInfo);
    }
    MSG("number of elements marked for coarsening: %d\n", abs(nCoarse));
      
    markFlag= 0;
    if(elMarkRefine) 
            markFlag= 1;
    if(elMarkCoarsen) 
            markFlag|= 2;
    
    delete [] localIndices;
Praetorius, Simon's avatar
Praetorius, Simon committed
448
449
  }
  
450
451
452
453
454
private:
        std::vector<DOFVector<double>*> vecs;
        DOFVector<double>* phase;
        double tol;
};
455
#endif // EXTENSIONS_REFINEMENT_LEVEL_PHASEFIELD_H
456