Refinement.h 14.7 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

#include "ElementFunction.h"
23
24
#include "RefineOperations.h"
#include "MeshFunction_Level.h"
25

26
namespace AMDiS { namespace extensions {
27
28
29
30
31
32
33
34
35

/** \brief
 * Base class for Refinement structure to perform local anisotropic refinement
 */
template<typename T, typename T2>
class RefinementLevel
{
public:

36
  RefinementLevel(const FiniteElemSpace *feSpace_, 
Praetorius, Simon's avatar
Praetorius, Simon committed
37
		  MeshRefinementFunction<T,T2>* refineFct_ = NULL) :
38
39
    feSpace(feSpace_), 
    refineFct(refineFct_),
Praetorius, Simon's avatar
Praetorius, Simon committed
40
41
    adaptInfo(NULL),
    refineOperation(NULL),
42
    numRefinements0(15),
43
    finished(true),
44
45
    globalRefined(false)
  {	
Praetorius, Simon's avatar
Praetorius, Simon committed
46
47
    FUNCNAME("RefinementLevel::RefinementLevel()");
    
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    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;
67
68
    
    refineOperation = new StandardRefineOperation;
69
70
  }
  
71
72
73
74
  virtual ~RefinementLevel() 
  {
    finalize();
    
75
76
    delete coarseningManager;
    delete refinementManager;
77
78
79
80
    if (refineOperation)
      delete refineOperation;
  }
  
81
82
83
84
85
86
87
88
89
90
  void finalize()
  {
    if (!finished) {
      #ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      Parallel::MeshDistributor::globalMeshDistributor->checkMeshChange();
      #endif
    }
    finished = true;
  }
  
91
92
93
94
95
96
97
98
  void setRefineOperation(AdaptInfo* adaptInfo_,
			  StandardRefineOperation* refineOperation_)
  {
    if (refineOperation)
      delete refineOperation;
    
    adaptInfo = adaptInfo_;
    refineOperation = refineOperation_;
99
100
101
102
103
104
  }

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

Praetorius, Simon's avatar
Praetorius, Simon committed
105
    if (!globalRefined && refineFct) {
106
      MSG("nr of global refinements: %d\n", refineFct->getGlobalSize());
107
108
109
110
111
112
113
114
115
      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
116
    int oldNr = 0, oldOldNr = 0;
117
118
119
    int i = 0;
    while (meshChanged && i < numRefinements) {
      markElements(markFlag);
120
      meshChanged = refineMesh(markFlag, onlyRefine);
121
      
122
      calcMeshSizes(minH, maxH, minLevel, maxLevel, false); 
123
124
125
      int nr = mesh->getNumberOfVertices();
      meshChanged = meshChanged && oldOldNr!=nr && oldNr!=nr;
      if (meshChanged) {
126
	MSG("(local) mesh sizes: [%f, %f], Vs: %d, ELs: %d\n", 
127
128
129
130
131
132
133
          minH, maxH, nr, mesh->getNumberOfElements());
      }
      i++;
			
      oldOldNr = oldNr;
      oldNr = nr; 
    }
134
135
    calcMeshSizes(minH, maxH, minLevel, maxLevel, true); 
    MSG("Final (global) mesh: [%f, %f], Vs: %d, ELs: %d, Level: [%d, %d]\n",
136
137
138
139
140
141
142
143
      minH, maxH, mesh->getNumberOfVertices(), mesh->getNumberOfElements(), minLevel, maxLevel);
  }

  void refine(int numRefinements_, bool onlyRefine= false) { 
    numRefinements = numRefinements_; 
    refine(onlyRefine);  
    numRefinements = numRefinements0; 
  }
144
145
146
147
148
149
  
  
  void initial_refine()
  {
    
  }
150
151
152
153
154

  int getNumRefinements(){
      return numRefinements;
  }

155
  void calcMeshSizes(double& minH, double& maxH, int& minLevel, int& maxLevel, bool allReduce = false) 
156
157
158
159
160
161
162
163
164
165
166
167
168
169
  {
    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;
170
171
      for (int i = 0; i < coords.getSize(); i++) {
        for (int j = 0; j < coords.getSize(); j++) {
172
173
174
175
176
177
178
179
180
181
182
	  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();
183
184
185
186
187
188
189
190
191
    
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    if (allReduce) {
      Parallel::mpi::globalMin(minH);
      Parallel::mpi::globalMax(maxH);
      Parallel::mpi::globalMin(minLevel);
      Parallel::mpi::globalMax(maxLevel);
    }
#endif
192
193
194
  }


195
  double calcMeshSize(ElInfo *elInfo, bool allReduce = false) 
196
197
198
199
200
201
202
203
204
205
  {
    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]));
      }
    }
206
207
208
209
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    if (allReduce)
      Parallel::mpi::globalMax(h);
#endif
210
211
212
213
214
215
216
217
218
219

    return h;
  }


  int calcMark(double refineH, double currentH)
  {
    return (refineH < currentH ? 
        1 : (refineH > currentH * (mesh->getDim() == 1 ? 
          2.0 : (mesh->getDim() == 2 ? 
220
221
          std::sqrt(2.0) : 
          std::sqrt(2.0)/2.0 + 0.5)) ? 
222
223
224
225
226
227
228
229
230
231
232
233
234
235
        -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) 
  {
236
237
    finished = false;
    
238
    int oldSize = mesh->getNumberOfVertices();
239
    refineOperation->beforeRefine(adaptInfo, markFlag);
240
241
    if (markFlag.isSet(1))
      refinementManager->refineMesh(mesh);
242
    refineOperation->beforeCoarsen(adaptInfo, markFlag);
243
244
    if (markFlag.isSet(2) && !onlyRefine)
      coarseningManager->coarsenMesh(mesh);
245
    refineOperation->afterCoarsen(adaptInfo, markFlag);
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
    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;
267
268
269
  
  AdaptInfo* adaptInfo;
  StandardRefineOperation* refineOperation;
270
271
272

  int numRefinements;
  int numRefinements0;
273
  
274
  bool finished;
275
276
277
  bool globalRefined;
};

278
279
280
281
282
283
284
285
286
287
288


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

  RefinementLocal(Mesh *mesh_) :
    mesh(mesh_), 
289
290
    adaptInfo(NULL),
    refineOperation(NULL),
291
    numRefinements0(15),
292
    finished(true),
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
    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;
  }
  
319
320
321
322
  virtual ~RefinementLocal() 
  {
    finalize();
    
323
324
325
326
327
328
    delete coarseningManager;
    delete refinementManager;
    if (refineOperation)
      delete refineOperation;
  }
  
329
330
331
332
333
334
335
336
337
338
  void finalize()
  {
    if (!finished) {
      #ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      Parallel::MeshDistributor::globalMeshDistributor->checkMeshChange();
      #endif
    }
    finished = true;
  }
  
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
  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
407
    Flag traverseFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS;
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
    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;
447
448
      for (int i = 0; i < coords.getSize(); i++) {
        for (int j = 0; j < coords.getSize(); j++) {
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
	  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;
477
478
    for (int i = 0; i < coords.getSize(); i++) {
      for (int j = 0; j < coords.getSize(); j++) {
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
        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 ? 
497
498
          std::sqrt(2.0) : 
          std::sqrt(2.0)/2.0 + 0.5)) ? 
499
500
501
502
503
504
505
506
507
508
509
510
511
512
        -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) 
  {
513
514
    finished = false;
    
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
    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;
  
547
  bool finished;
548
549
550
551
  bool globalRefined;
  bool onlyRefine;
};

552
553
554
555
} }

using namespace AMDiS::extensions;

556
#include "Refinement_Level.h"
557
// #include "Refinement_MeshSize.h"
558

559
#endif // EXTENSIONS_REFINEMENT_H