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

#include "ElementFunction.h"

using namespace AMDiS;


/** \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);
      reduction = 1.0 / sqrt(2.0); // if dim==2
  }

  int getGlobalSize() { return globalSize; }

  double meshSize() { return h0; }

  virtual T2 operator()(const T &value) const { return globalSize; }
  
  virtual double indicator(const T &value) const { return 1.0; }

protected:

  int hToLevel(double h) {
      int level = static_cast<int>(floor(log(h / h0) / log(reduction)));
      return level;
  }

  double levelToH(int level) {
      double h = pow(reduction,level)*h0;
      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;
};


89
90
91
92
93
94
95
96
struct StandardRefineOperation
{
  virtual void beforeRefine(AdaptInfo* adaptInfo, Flag markFlag) {}
  virtual void beforeCoarsen(AdaptInfo* adaptInfo, Flag markFlag) {}
  virtual void afterCoarsen(AdaptInfo* adaptInfo, Flag markFlag) {}  
};


97
98
99
100
101
102
103
104
/** \brief
 * Base class for Refinement structure to perform local anisotropic refinement
 */
template<typename T, typename T2>
class RefinementLevel
{
public:

105
106
  RefinementLevel(const FiniteElemSpace *feSpace_, 
		  MeshRefinementFunction<T,T2>* refineFct_) :
107
108
    feSpace(feSpace_), 
    refineFct(refineFct_),
109
110
    adaptInfo(nullptr),
    refineOperation(nullptr),
111
112
113
    numRefinements0(15),
    globalRefined(false)
  {	
Praetorius, Simon's avatar
Praetorius, Simon committed
114
115
    FUNCNAME("RefinementLevel::RefinementLevel()");
    
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    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;
135
136
    
    refineOperation = new StandardRefineOperation;
137
138
  }
  
Praetorius, Simon's avatar
Praetorius, Simon committed
139
  virtual ~RefinementLevel() {
140
141
    delete coarseningManager;
    delete refinementManager;
142
143
144
145
146
147
148
149
150
151
152
153
    if (refineOperation)
      delete refineOperation;
  }
  
  void setRefineOperation(AdaptInfo* adaptInfo_,
			  StandardRefineOperation* refineOperation_)
  {
    if (refineOperation)
      delete refineOperation;
    
    adaptInfo = adaptInfo_;
    refineOperation = refineOperation_;
154
155
156
157
158
159
160
  }

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

    if (!globalRefined) {
161
      MSG("nr of global refinements: %d\n", refineFct->getGlobalSize());
162
163
164
165
166
167
168
169
170
      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
171
    int oldNr = 0, oldOldNr = 0;
172
173
174
    int i = 0;
    while (meshChanged && i < numRefinements) {
      markElements(markFlag);
175
      meshChanged = refineMesh(adaptInfo, markFlag, onlyRefine);
176
      
177
      calcMeshSizes(minH, maxH, minLevel, maxLevel, false); 
178
179
180
      int nr = mesh->getNumberOfVertices();
      meshChanged = meshChanged && oldOldNr!=nr && oldNr!=nr;
      if (meshChanged) {
181
	MSG("(local) mesh sizes: [%f, %f], Vs: %d, ELs: %d\n", 
182
183
184
185
186
187
188
          minH, maxH, nr, mesh->getNumberOfElements());
      }
      i++;
			
      oldOldNr = oldNr;
      oldNr = nr; 
    }
189
190
    calcMeshSizes(minH, maxH, minLevel, maxLevel, true); 
    MSG("Final (global) mesh: [%f, %f], Vs: %d, ELs: %d, Level: [%d, %d]\n",
191
192
193
194
195
196
197
198
199
200
201
202
203
      minH, maxH, mesh->getNumberOfVertices(), mesh->getNumberOfElements(), minLevel, maxLevel);
  }

  void refine(int numRefinements_, bool onlyRefine= false) { 
    numRefinements = numRefinements_; 
    refine(onlyRefine);  
    numRefinements = numRefinements0; 
  }

  int getNumRefinements(){
      return numRefinements;
  }

204
  void calcMeshSizes(double& minH, double& maxH, int& minLevel, int& maxLevel, bool allReduce = false) 
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
  {
    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();
232
233
234
235
236
237
238
239
240
    
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    if (allReduce) {
      Parallel::mpi::globalMin(minH);
      Parallel::mpi::globalMax(maxH);
      Parallel::mpi::globalMin(minLevel);
      Parallel::mpi::globalMax(maxLevel);
    }
#endif
241
242
243
  }


244
  double calcMeshSize(ElInfo *elInfo, bool allReduce = false) 
245
246
247
248
249
250
251
252
253
254
  {
    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]));
      }
    }
255
256
257
258
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    if (allReduce)
      Parallel::mpi::globalMax(h);
#endif
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

    return h;
  }


  int calcMark(double refineH, double currentH)
  {
    return (refineH < currentH ? 
        1 : (refineH > currentH * (mesh->getDim() == 1 ? 
          2.0 : (mesh->getDim() == 2 ? 
          sqrt(2.0) : 
          sqrt(2.0)/2.0 + 0.5)) ? 
        -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();
286
    refineOperation->beforeRefine(adaptInfo, markFlag);
287
288
    if (markFlag.isSet(1))
      refinementManager->refineMesh(mesh);
289
    refineOperation->beforeCoarsen(adaptInfo, markFlag);
290
291
    if (markFlag.isSet(2) && !onlyRefine)
      coarseningManager->coarsenMesh(mesh);
292
    refineOperation->afterCoarsen(adaptInfo, markFlag);
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
    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;
314
315
316
  
  AdaptInfo* adaptInfo;
  StandardRefineOperation* refineOperation;
317
318
319

  int numRefinements;
  int numRefinements0;
320
  
321
322
323
324
  bool globalRefined;
};

#include "Refinement_Level.h"
325
// #include "Refinement_MeshSize.h"
326

327
#endif // EXTENSIONS_REFINEMENT_H