Am Montag, 13. Mai 2022, finden Wartungsarbeiten am Gitlab-Server (Update auf neue Version statt). Der Dienst wird daher am Montag für einige Zeit nicht verfügbar sein.
On Monday, May 13th 2022, the Gitlab server will be updated. The service will therefore not be accessible for some time on Monday.

Refinement.h 15.4 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
  virtual ~RefinementLevel() 
  {
73
//     finalize();
74
    
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
  void refine(bool onlyRefine= false, bool meshRepartation = false) 
102
103
104
  {
    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
      int nr = mesh->getNumberOfVertices();
      meshChanged = meshChanged && oldOldNr!=nr && oldNr!=nr;
125
126
127
128
129
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      int sendValue = static_cast<int>(meshChanged);
      Parallel::mpi::globalAdd(MPI::COMM_WORLD, sendValue);
      meshChanged = static_cast<bool>(sendValue);
#endif
130
      if (meshChanged) {
131
	MSG("(local) mesh sizes: [%f, %f], Vs: %d, ELs: %d\n", 
132
133
134
135
136
137
138
          minH, maxH, nr, mesh->getNumberOfElements());
      }
      i++;
			
      oldOldNr = oldNr;
      oldNr = nr; 
    }
139
140
141
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    Parallel::MeshDistributor::globalMeshDistributor->checkMeshChange(meshRepartation);
#endif
142
143
    calcMeshSizes(minH, maxH, minLevel, maxLevel, true); 
    MSG("Final (global) mesh: [%f, %f], Vs: %d, ELs: %d, Level: [%d, %d]\n",
144
145
146
      minH, maxH, mesh->getNumberOfVertices(), mesh->getNumberOfElements(), minLevel, maxLevel);
  }

147
  void refine(int numRefinements_, bool onlyRefine= false, bool repartation = false) { 
148
    numRefinements = numRefinements_; 
149
    refine(onlyRefine, repartation);  
150
151
    numRefinements = numRefinements0; 
  }
152
153
154
155
156
157
  
  
  void initial_refine()
  {
    
  }
158
159
160
161
162

  int getNumRefinements(){
      return numRefinements;
  }

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


203
  double calcMeshSize(ElInfo *elInfo, bool allReduce = false) 
204
205
206
207
  {
    FixVec<WorldVector<double>, VERTEX> coords(mesh->getDim(), NO_INIT);
    coords = elInfo->getCoords();
    double h = 0.0;
Praetorius, Simon's avatar
Praetorius, Simon committed
208
209
    for (int i = 0; i < coords.getSize(); i++) {
      for (int j = 0; j < coords.getSize(); j++) {
210
211
212
213
        if (i != j)
          h = std::max(h,norm(coords[i]-coords[j]));
      }
    }
214
215
216
217
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    if (allReduce)
      Parallel::mpi::globalMax(h);
#endif
218
219
220
221
222
223
224
225
226
227

    return h;
  }


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

  int numRefinements;
  int numRefinements0;
281
  
282
  bool finished;
283
284
285
  bool globalRefined;
};

286
287
288
289
290
291
292
293
294
295
296


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

  RefinementLocal(Mesh *mesh_) :
    mesh(mesh_), 
297
298
    adaptInfo(NULL),
    refineOperation(NULL),
299
    numRefinements0(15),
300
    finished(true),
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
    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;
  }
  
327
328
  virtual ~RefinementLocal() 
  {
329
//     finalize();
330
    
331
332
333
334
335
336
    delete coarseningManager;
    delete refinementManager;
    if (refineOperation)
      delete refineOperation;
  }
  
337
338
339
340
341
342
343
344
345
346
  void finalize()
  {
    if (!finished) {
      #ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      Parallel::MeshDistributor::globalMeshDistributor->checkMeshChange();
      #endif
    }
    finished = true;
  }
  
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
  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;
388
389
390
391
392
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      int sendValue = static_cast<int>(meshChanged);
      Parallel::mpi::globalAdd(MPI::COMM_WORLD, sendValue);
      meshChanged = static_cast<bool>(sendValue);
#endif
393
394
395
396
397
398
399
400
401
      if (meshChanged) {
	MSG("(local) mesh sizes: [%f, %f], Vs: %d, ELs: %d\n", 
          minH, maxH, nr, mesh->getNumberOfElements());
      }
      i++;
			
      oldOldNr = oldNr;
      oldNr = nr; 
    }
402
403
404
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    Parallel::MeshDistributor::globalMeshDistributor->checkMeshChange();
#endif
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
    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
423
    Flag traverseFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS;
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
    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;
463
464
      for (int i = 0; i < coords.getSize(); i++) {
        for (int j = 0; j < coords.getSize(); j++) {
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
492
	  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;
493
494
    for (int i = 0; i < coords.getSize(); i++) {
      for (int j = 0; j < coords.getSize(); j++) {
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
        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 ? 
513
514
          std::sqrt(2.0) : 
          std::sqrt(2.0)/2.0 + 0.5)) ? 
515
516
517
518
519
520
521
522
523
524
525
526
527
528
        -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) 
  {
529
530
    finished = false;
    
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
    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;
  
563
  bool finished;
564
565
566
567
  bool globalRefined;
  bool onlyRefine;
};

568
569
570
571
} }

using namespace AMDiS::extensions;

572
#include "Refinement_Level.h"
573
// #include "Refinement_MeshSize.h"
574

575
#endif // EXTENSIONS_REFINEMENT_H