Refinement.h 16.9 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_H
#define EXTENSIONS_REFINEMENT_H
21
22
23

#include "ElementFunction.h"

24
namespace AMDiS { namespace extensions {
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45


/** \brief
 * Abstract class that can be passed to RefinementLevel* as indicator where
 * to refine the mesh up to which level. It is an AbstractFunction that 
 * overloads the operator() method to return a level or a meshsize depending
 * on the coords/data passed to the operator.
 * You can switch between meshsize and level with the methods hToLevel(double) and 
 * levelToH(int)
 **/
template<typename T, typename T2>
class MeshRefinementFunction : public AbstractFunction<T2, T>
{
public:

  MeshRefinementFunction(Mesh* mesh_) :
    AbstractFunction<T2, T>(0),
    mesh(mesh_), 
    globalSize(0)
  {
      h0 = getMacroMeshSize(mesh);
46
      reduction = 1.0 / std::sqrt(2.0); // if dim==2
47
48
49
50
51
  }

  int getGlobalSize() { return globalSize; }

  double meshSize() { return h0; }
52
53
  
  virtual void init(ElInfo* elInfo) {}
54
55

  virtual T2 operator()(const T &value) const { return globalSize; }
56
  virtual T2 getLevel() const { return globalSize; }
57
58
59
60
61
62
  
  virtual double indicator(const T &value) const { return 1.0; }

protected:

  int hToLevel(double h) {
63
      int level = static_cast<int>(std::floor(std::log(h / h0) / std::log(reduction)));
64
65
66
67
      return level;
  }

  double levelToH(int level) {
68
      double h = std::pow(reduction,level)*h0;
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
      return h;
  }

  double getMacroMeshSize(Mesh* mesh) {
      FixVec<WorldVector<double>, VERTEX> coords = mesh->getMacroElement(0)->getCoord();
      double h = 0.0;
      for (int i = 0; i < coords.size(); ++i)
          for (int j = i + 1; j < coords.size(); ++j)
              h = std::max(h, norm(coords[i]-coords[j]));
      return h;
  }

protected:

  Mesh* mesh;

  int globalSize;

  double h0;
  double reduction;
};

91
/// Operations performed before and after refinement/ coarsening
92
93
struct StandardRefineOperation
{
94
  virtual ~StandardRefineOperation() {};
95
96
97
98
99
  virtual void beforeRefine(AdaptInfo* adaptInfo, Flag markFlag) {}
  virtual void beforeCoarsen(AdaptInfo* adaptInfo, Flag markFlag) {}
  virtual void afterCoarsen(AdaptInfo* adaptInfo, Flag markFlag) {}  
};

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
/// vector of operations performed before and after refinement/ coarsening
struct CoupledRefineOperation : public StandardRefineOperation
{
  CoupledRefineOperation(std::vector<StandardRefineOperation*> operations_) 
    : operations(operations_) {}
    
  void beforeRefine(AdaptInfo* adaptInfo, Flag markFlag) override 
  {
    for (size_t i = 0; i < operations.size(); i++)
	operations[i]->beforeRefine(adaptInfo, markFlag);
  }
  
  void beforeCoarsen(AdaptInfo* adaptInfo, Flag markFlag) override
  {
    for (size_t i = 0; i < operations.size(); i++)
      operations[i]->beforeCoarsen(adaptInfo, markFlag);
  }
  void afterCoarsen(AdaptInfo* adaptInfo, Flag markFlag) override
  {
    for (size_t i = 0; i < operations.size(); i++)
      operations[i]->afterCoarsen(adaptInfo, markFlag);
  }

protected:
   std::vector<StandardRefineOperation*> operations;
}; 

127

128
129
130
131
132
133
134
135
/** \brief
 * Base class for Refinement structure to perform local anisotropic refinement
 */
template<typename T, typename T2>
class RefinementLevel
{
public:

136
  RefinementLevel(const FiniteElemSpace *feSpace_, 
Praetorius, Simon's avatar
Praetorius, Simon committed
137
		  MeshRefinementFunction<T,T2>* refineFct_ = NULL) :
138
139
    feSpace(feSpace_), 
    refineFct(refineFct_),
Praetorius, Simon's avatar
Praetorius, Simon committed
140
141
    adaptInfo(NULL),
    refineOperation(NULL),
142
143
144
    numRefinements0(15),
    globalRefined(false)
  {	
Praetorius, Simon's avatar
Praetorius, Simon committed
145
146
    FUNCNAME("RefinementLevel::RefinementLevel()");
    
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    mesh = feSpace->getMesh();
    switch (mesh->getDim()) {
    case 1:
      coarseningManager = new CoarseningManager1d();
      refinementManager = new RefinementManager1d();
      break;
    case 2:
      coarseningManager = new CoarseningManager2d();
      refinementManager = new RefinementManager2d();
      break;
    case 3:
      coarseningManager = new CoarseningManager3d();
      refinementManager = new RefinementManager3d();
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }

    numRefinements = numRefinements0;
166
167
    
    refineOperation = new StandardRefineOperation;
168
169
  }
  
Praetorius, Simon's avatar
Praetorius, Simon committed
170
  virtual ~RefinementLevel() {
171
172
    delete coarseningManager;
    delete refinementManager;
173
174
175
176
177
178
179
180
181
182
183
184
    if (refineOperation)
      delete refineOperation;
  }
  
  void setRefineOperation(AdaptInfo* adaptInfo_,
			  StandardRefineOperation* refineOperation_)
  {
    if (refineOperation)
      delete refineOperation;
    
    adaptInfo = adaptInfo_;
    refineOperation = refineOperation_;
185
186
187
188
189
190
  }

  void refine(bool onlyRefine= false) 
  {
    FUNCNAME("RefinementLevel::refine()");

Praetorius, Simon's avatar
Praetorius, Simon committed
191
    if (!globalRefined && refineFct) {
192
      MSG("nr of global refinements: %d\n", refineFct->getGlobalSize());
193
194
195
196
197
198
199
200
201
      refinementManager->globalRefine(mesh, refineFct->getGlobalSize());
      globalRefined = true;
    }
    double minH = 0.0, maxH = 1.0;
    int minLevel = 100, maxLevel = 0;
    
    // build mesh for phasefield-function
    bool meshChanged = true;
    Flag markFlag;
Praetorius, Simon's avatar
Praetorius, Simon committed
202
    int oldNr = 0, oldOldNr = 0;
203
204
205
    int i = 0;
    while (meshChanged && i < numRefinements) {
      markElements(markFlag);
206
      meshChanged = refineMesh(markFlag, onlyRefine);
207
      
208
      calcMeshSizes(minH, maxH, minLevel, maxLevel, false); 
209
210
211
      int nr = mesh->getNumberOfVertices();
      meshChanged = meshChanged && oldOldNr!=nr && oldNr!=nr;
      if (meshChanged) {
212
	MSG("(local) mesh sizes: [%f, %f], Vs: %d, ELs: %d\n", 
213
214
215
216
217
218
219
          minH, maxH, nr, mesh->getNumberOfElements());
      }
      i++;
			
      oldOldNr = oldNr;
      oldNr = nr; 
    }
220
221
    calcMeshSizes(minH, maxH, minLevel, maxLevel, true); 
    MSG("Final (global) mesh: [%f, %f], Vs: %d, ELs: %d, Level: [%d, %d]\n",
222
223
224
225
226
227
228
229
      minH, maxH, mesh->getNumberOfVertices(), mesh->getNumberOfElements(), minLevel, maxLevel);
  }

  void refine(int numRefinements_, bool onlyRefine= false) { 
    numRefinements = numRefinements_; 
    refine(onlyRefine);  
    numRefinements = numRefinements0; 
  }
230
231
232
233
234
235
  
  
  void initial_refine()
  {
    
  }
236
237
238
239
240

  int getNumRefinements(){
      return numRefinements;
  }

241
  void calcMeshSizes(double& minH, double& maxH, int& minLevel, int& maxLevel, bool allReduce = false) 
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
  {
    FixVec<WorldVector<double>, VERTEX> coords(mesh->getDim(), NO_INIT);

    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS);
    minH = 1e15; maxH = 0.0;
    int k = 0;
    minLevel = 100;
    maxLevel = 0;
    while (elInfo) {
      maxLevel = std::max(maxLevel,elInfo->getLevel());
      minLevel = std::min(minLevel,elInfo->getLevel());
      coords = elInfo->getCoords();
      double h = 0.0;
      for (int i = 0; i < coords.size(); i++) {
        for (int j = 0; j < coords.size(); j++) {
	  if (i != j)
            h = std::max(h,norm(coords[i]-coords[j]));
        }
      }
      minH = std::min(h, minH);
      maxH = std::max(h, maxH);
      elInfo = stack.traverseNext(elInfo);
      k++;
    }
    minLevel += mesh->getMacroElementLevel();
    maxLevel += mesh->getMacroElementLevel();
269
270
271
272
273
274
275
276
277
    
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    if (allReduce) {
      Parallel::mpi::globalMin(minH);
      Parallel::mpi::globalMax(maxH);
      Parallel::mpi::globalMin(minLevel);
      Parallel::mpi::globalMax(maxLevel);
    }
#endif
278
279
280
  }


281
  double calcMeshSize(ElInfo *elInfo, bool allReduce = false) 
282
283
284
285
286
287
288
289
290
291
  {
    FixVec<WorldVector<double>, VERTEX> coords(mesh->getDim(), NO_INIT);
    coords = elInfo->getCoords();
    double h = 0.0;
    for (int i = 0; i < coords.size(); i++) {
      for (int j = 0; j < coords.size(); j++) {
        if (i != j)
          h = std::max(h,norm(coords[i]-coords[j]));
      }
    }
292
293
294
295
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    if (allReduce)
      Parallel::mpi::globalMax(h);
#endif
296
297
298
299
300
301
302
303
304
305

    return h;
  }


  int calcMark(double refineH, double currentH)
  {
    return (refineH < currentH ? 
        1 : (refineH > currentH * (mesh->getDim() == 1 ? 
          2.0 : (mesh->getDim() == 2 ? 
306
307
          std::sqrt(2.0) : 
          std::sqrt(2.0)/2.0 + 0.5)) ? 
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
        -1 : 
        0));
  }


  virtual int calcMark(int refineLevel, int currentLevel)
  {
    int levelDiff = refineLevel - currentLevel;
    return (levelDiff > 0 ? 1 : (levelDiff < 0 ? -1 : 0));
  }

	
  bool refineMesh(Flag markFlag, bool onlyRefine) 
  {
    int oldSize = mesh->getNumberOfVertices();
323
    refineOperation->beforeRefine(adaptInfo, markFlag);
324
325
    if (markFlag.isSet(1))
      refinementManager->refineMesh(mesh);
326
    refineOperation->beforeCoarsen(adaptInfo, markFlag);
327
328
    if (markFlag.isSet(2) && !onlyRefine)
      coarseningManager->coarsenMesh(mesh);
329
    refineOperation->afterCoarsen(adaptInfo, markFlag);
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
    if (markFlag.isSet(1) || markFlag.isSet(2)) {
      int newSize = mesh->getNumberOfVertices();
      if (oldSize != newSize) 
        return true;
    }
    return false;
  }

  virtual void markElements(Flag &markFlag) = 0;

  void setGlobalRefined(bool refined) { globalRefined = refined; }
  RefinementManager* getRefinementManager() { return refinementManager; }
  CoarseningManager* getCoarseningManager() { return coarseningManager; }

protected:

  const FiniteElemSpace *feSpace;
  Mesh* mesh;
  MeshRefinementFunction<T,T2>* refineFct;
  RefinementManager* refinementManager;
  CoarseningManager* coarseningManager;
351
352
353
  
  AdaptInfo* adaptInfo;
  StandardRefineOperation* refineOperation;
354
355
356

  int numRefinements;
  int numRefinements0;
357
  
358
359
360
  bool globalRefined;
};

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


/** \brief
 * Base class for Refinement structure to perform local anisotropic refinement
 */
class RefinementLocal
{
public:

  RefinementLocal(Mesh *mesh_) :
    mesh(mesh_), 
    adaptInfo(nullptr),
    refineOperation(nullptr),
    numRefinements0(15),
    globalRefined(false)
  {	
    FUNCNAME("RefinementLevel::RefinementLevel()");
    
    switch (mesh->getDim()) {
    case 1:
      coarseningManager = new CoarseningManager1d();
      refinementManager = new RefinementManager1d();
      break;
    case 2:
      coarseningManager = new CoarseningManager2d();
      refinementManager = new RefinementManager2d();
      break;
    case 3:
      coarseningManager = new CoarseningManager3d();
      refinementManager = new RefinementManager3d();
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }

    numRefinements = numRefinements0;
    
    refineOperation = new StandardRefineOperation;
  }
  
  virtual ~RefinementLocal() {
    delete coarseningManager;
    delete refinementManager;
    if (refineOperation)
      delete refineOperation;
  }
  
  void setRefineOperation(AdaptInfo* adaptInfo_,
			  StandardRefineOperation* refineOperation_)
  {
    if (refineOperation)
      delete refineOperation;
    
    adaptInfo = adaptInfo_;
    refineOperation = refineOperation_;
  }

  void setOnlyRefine(bool onlyRefine_)
  {
    onlyRefine = onlyRefine_;
  }
  
  template<typename MeshRefFunc>
  void refine(MeshRefFunc* refineFct) 
  {
    FUNCNAME("RefinementLevel::refine()");

    refineFct->init(mesh);
    if (!globalRefined) {
      MSG("nr of global refinements: %d\n", refineFct->getGlobalSize());
      refinementManager->globalRefine(mesh, refineFct->getGlobalSize());
      globalRefined = true;
    }
    double minH = 0.0, maxH = 1.0;
    int minLevel = 100, maxLevel = 0;
    
    // build mesh for phasefield-function
    bool meshChanged = true;
    Flag markFlag;
    int oldNr = 0, oldOldNr = 0;
    int i = 0;
    while (meshChanged && i < numRefinements) {
      markElements(refineFct, markFlag);
      meshChanged = refineMesh(markFlag, onlyRefine);
      
      calcMeshSizes(minH, maxH, minLevel, maxLevel, false); 
      int nr = mesh->getNumberOfVertices();
      meshChanged = meshChanged && oldOldNr!=nr && oldNr!=nr;
      if (meshChanged) {
	MSG("(local) mesh sizes: [%f, %f], Vs: %d, ELs: %d\n", 
          minH, maxH, nr, mesh->getNumberOfElements());
      }
      i++;
			
      oldOldNr = oldNr;
      oldNr = nr; 
    }
    calcMeshSizes(minH, maxH, minLevel, maxLevel, true); 
    MSG("Final (global) mesh: [%f, %f], Vs: %d, ELs: %d, Level: [%d, %d]\n",
      minH, maxH, mesh->getNumberOfVertices(), mesh->getNumberOfElements(), minLevel, maxLevel);
  }

  template<typename MeshRefFunc>
  void refine(int numRefinements_, MeshRefFunc* refineFct, bool onlyRefine_= false) { 
    numRefinements = numRefinements_; 
    setOnlyRefine(onlyRefine_);
    refine(refineFct);  
    numRefinements = numRefinements0; 
  }
  
  template<typename MeshRefFunc>
  void markElements(MeshRefFunc* refineFct, Flag &markFlag) 
  {    
    bool elMarkRefine = false, elMarkCoarsen = false;

Praetorius, Simon's avatar
Praetorius, Simon committed
476
    Flag traverseFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS;
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
523
524
525
526
527
528
529
530
531
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
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, traverseFlag);
    while(elInfo) {
      refineFct->init(elInfo);
      int refineLevel = refineFct->getLevel();
      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;
  }

  int getNumRefinements() {
      return numRefinements;
  }

  void calcMeshSizes(double& minH, double& maxH, int& minLevel, int& maxLevel, bool allReduce = false) 
  {
    FixVec<WorldVector<double>, VERTEX> coords(mesh->getDim(), NO_INIT);

    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS);
    minH = 1e15; maxH = 0.0;
    int k = 0;
    minLevel = 100;
    maxLevel = 0;
    while (elInfo) {
      maxLevel = std::max(maxLevel,elInfo->getLevel());
      minLevel = std::min(minLevel,elInfo->getLevel());
      coords = elInfo->getCoords();
      double h = 0.0;
      for (int i = 0; i < coords.size(); i++) {
        for (int j = 0; j < coords.size(); j++) {
	  if (i != j)
            h = std::max(h,norm(coords[i]-coords[j]));
        }
      }
      minH = std::min(h, minH);
      maxH = std::max(h, maxH);
      elInfo = stack.traverseNext(elInfo);
      k++;
    }
    minLevel += mesh->getMacroElementLevel();
    maxLevel += mesh->getMacroElementLevel();
    
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    if (allReduce) {
      Parallel::mpi::globalMin(minH);
      Parallel::mpi::globalMax(maxH);
      Parallel::mpi::globalMin(minLevel);
      Parallel::mpi::globalMax(maxLevel);
    }
#endif
  }


  double calcMeshSize(ElInfo *elInfo, bool allReduce = false) 
  {
    FixVec<WorldVector<double>, VERTEX> coords(mesh->getDim(), NO_INIT);
    coords = elInfo->getCoords();
    double h = 0.0;
    for (int i = 0; i < coords.size(); i++) {
      for (int j = 0; j < coords.size(); j++) {
        if (i != j)
          h = std::max(h,norm(coords[i]-coords[j]));
      }
    }
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    if (allReduce)
      Parallel::mpi::globalMax(h);
#endif

    return h;
  }


  int calcMark(double refineH, double currentH)
  {
    return (refineH < currentH ? 
        1 : (refineH > currentH * (mesh->getDim() == 1 ? 
          2.0 : (mesh->getDim() == 2 ? 
566
567
          std::sqrt(2.0) : 
          std::sqrt(2.0)/2.0 + 0.5)) ? 
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
        -1 : 
        0));
  }


  virtual int calcMark(int refineLevel, int currentLevel)
  {
    int levelDiff = refineLevel - currentLevel;
    return (levelDiff > 0 ? 1 : (levelDiff < 0 ? -1 : 0));
  }

	
  bool refineMesh(Flag markFlag, bool onlyRefine) 
  {
    int oldSize = mesh->getNumberOfVertices();
    refineOperation->beforeRefine(adaptInfo, markFlag);
    if (markFlag.isSet(1))
      refinementManager->refineMesh(mesh);
    refineOperation->beforeCoarsen(adaptInfo, markFlag);
    if (markFlag.isSet(2) && !onlyRefine)
      coarseningManager->coarsenMesh(mesh);
    refineOperation->afterCoarsen(adaptInfo, markFlag);
    if (markFlag.isSet(1) || markFlag.isSet(2)) {
      int newSize = mesh->getNumberOfVertices();
      if (oldSize != newSize) 
        return true;
    }
    return false;
  }

  void setGlobalRefined(bool refined) { globalRefined = refined; }
  RefinementManager* getRefinementManager() { return refinementManager; }
  CoarseningManager* getCoarseningManager() { return coarseningManager; }

protected:

  Mesh* mesh;
  RefinementManager* refinementManager;
  CoarseningManager* coarseningManager;
  
  AdaptInfo* adaptInfo;
  StandardRefineOperation* refineOperation;

  int numRefinements;
  int numRefinements0;
  
  bool globalRefined;
  bool onlyRefine;
};

618
619
620
621
} }

using namespace AMDiS::extensions;

622
#include "Refinement_Level.h"
623
// #include "Refinement_MeshSize.h"
624

625
#endif // EXTENSIONS_REFINEMENT_H