forked from AMReX-Codes/amrex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAMReX_EB2_Level.H
396 lines (338 loc) · 15.1 KB
/
AMReX_EB2_Level.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
89
90
91
92
93
94
95
96
97
98
99
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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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
256
257
258
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
286
287
288
289
290
291
292
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
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
#ifndef AMREX_EB2_LEVEL_H_
#define AMREX_EB2_LEVEL_H_
#include <AMReX_Geometry.H>
#include <AMReX_MultiFab.H>
#include <AMReX_LayoutData.H>
#include <AMReX_VisMF.H>
#include <AMReX_Array.H>
#include <AMReX_EBCellFlag.H>
#include <AMReX_MultiCutFab.H>
#include <AMReX_EB2_MultiGFab.H>
#include <AMReX_EB2_F.H>
#include <AMReX_EB2_IF_AllRegular.H>
#include <unordered_map>
#include <limits>
#include <cmath>
#include <type_traits>
#ifdef _OPENMP
#include <omp.h>
#endif
namespace amrex { namespace EB2 {
class IndexSpace;
class Level
{
public:
bool isAllRegular () const noexcept { return m_allregular; }
bool isOK () const noexcept { return m_ok; }
void fillEBCellFlag (FabArray<EBCellFlagFab>& cellflag, const Geometry& geom) const;
void fillVolFrac (MultiFab& vfrac, const Geometry& geom) const;
void fillCentroid (MultiCutFab& centroid, const Geometry& geom) const;
void fillCentroid ( MultiFab& centroid, const Geometry& geom) const;
void fillBndryArea (MultiCutFab& bndryarea, const Geometry& geom) const;
void fillBndryArea ( MultiFab& bndryarea, const Geometry& geom) const;
void fillBndryCent (MultiCutFab& bndrycent, const Geometry& geom) const;
void fillBndryCent ( MultiFab& bndrycent, const Geometry& geom) const;
void fillBndryNorm (MultiCutFab& bndrynorm, const Geometry& geom) const;
void fillBndryNorm ( MultiFab& bndrynorm, const Geometry& geom) const;
void fillAreaFrac (Array<MultiCutFab*,AMREX_SPACEDIM> const& areafrac, const Geometry& geom) const;
void fillAreaFrac (Array< MultiFab*,AMREX_SPACEDIM> const& areafrac, const Geometry& geom) const;
void fillFaceCent (Array<MultiCutFab*,AMREX_SPACEDIM> const& facefrac, const Geometry& geom) const;
void fillFaceCent (Array< MultiFab*,AMREX_SPACEDIM> const& facefrac, const Geometry& geom) const;
void fillLevelSet (MultiFab& levelset, const Geometry& geom) const;
const BoxArray& boxArray () const noexcept { return m_grids; }
const DistributionMapping& DistributionMap () const noexcept { return m_dmap; }
Level (IndexSpace const* is, const Geometry& geom) : m_geom(geom), m_parent(is) {}
void prepareForCoarsening (const Level& rhs, int max_grid_size, IntVect ngrow);
const Geometry& Geom () const noexcept { return m_geom; }
IndexSpace const* getEBIndexSpace () const noexcept { return m_parent; }
protected:
Level (Level && rhs) = default;
Level (Level const& rhs) = delete;
Level& operator=(Level const& rhs) = delete;
Level& operator=(Level && rhs) = delete;
int coarsenFromFine (Level& fineLevel, bool fill_boundary);
void buildCellFlag ();
Geometry m_geom;
IntVect m_ngrow;
BoxArray m_grids;
BoxArray m_covered_grids;
DistributionMapping m_dmap;
MultiGFab m_mgf;
MultiFab m_levelset;
FabArray<EBCellFlagFab> m_cellflag;
MultiFab m_volfrac;
MultiFab m_centroid;
MultiFab m_bndryarea;
MultiFab m_bndrycent;
MultiFab m_bndrynorm;
Array<MultiFab,AMREX_SPACEDIM> m_areafrac;
Array<MultiFab,AMREX_SPACEDIM> m_facecent;
bool m_allregular = false;
bool m_ok = false;
IndexSpace const* m_parent;
};
template <typename G>
class GShopLevel
: public Level
{
public:
GShopLevel (IndexSpace const* is, G const& gshop, const Geometry& geom, int max_grid_size, int ngrow);
GShopLevel (IndexSpace const* is, int ilev, int max_grid_size, int ngrow,
const Geometry& geom, GShopLevel<G>& fineLevel);
};
template <typename G>
GShopLevel<G>::GShopLevel (IndexSpace const* is, G const& gshop, const Geometry& geom,
int max_grid_size, int ngrow)
: Level(is, geom)
{
if (std::is_same<typename G::FunctionType, AllRegularIF>::value) {
m_allregular = true;
m_ok = true;
return;
}
BL_PROFILE("EB2::GShopLevel()-fine");
// make sure ngrow is multiple of 16
m_ngrow = IntVect{static_cast<int>(std::ceil(ngrow/16.)) * 16};
Box domain_grown = geom.Domain();
for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
if (geom.isPeriodic(idim)) {
m_ngrow[idim] = 0;
} else {
m_ngrow[idim] = std::min(m_ngrow[idim], domain_grown.length(idim));
}
}
domain_grown.grow(m_ngrow);
m_grids.define(domain_grown);
m_grids.maxSize(max_grid_size);
m_dmap.define(m_grids);
Vector<Box> cut_boxes;
Vector<Box> covered_boxes;
for (MFIter mfi(m_grids, m_dmap); mfi.isValid(); ++mfi)
{
const Box& vbx = mfi.validbox();
const Box& gbx = amrex::surroundingNodes(amrex::grow(vbx,1));
int box_type = gshop.getBoxType(gbx, geom);
if (box_type == gshop.allcovered) {
covered_boxes.push_back(vbx);
} else if (box_type == gshop.mixedcells) {
cut_boxes.push_back(vbx);
}
}
amrex::AllGatherBoxes(cut_boxes);
amrex::AllGatherBoxes(covered_boxes);
if ( cut_boxes.empty() &&
!covered_boxes.empty())
{
amrex::Abort("AMReX_EB2_Level.H: Domain is completely covered");
}
if (!covered_boxes.empty()) {
m_covered_grids = BoxArray(BoxList(std::move(covered_boxes)));
}
if (cut_boxes.empty()) {
m_grids = BoxArray();
m_dmap = DistributionMapping();
m_allregular = true;
m_ok = true;
return;
}
m_grids = BoxArray(BoxList(std::move(cut_boxes)));
m_dmap = DistributionMapping(m_grids);
m_mgf.define(m_grids, m_dmap);
const int ng = 2;
MFInfo mf_info;
mf_info.SetTag("EB2::Level");
m_cellflag.define(m_grids, m_dmap, 1, ng, mf_info);
m_volfrac.define(m_grids, m_dmap, 1, ng, mf_info);
m_centroid.define(m_grids, m_dmap, AMREX_SPACEDIM, ng, mf_info);
m_bndryarea.define(m_grids, m_dmap, 1, ng, mf_info);
m_bndrycent.define(m_grids, m_dmap, AMREX_SPACEDIM, ng, mf_info);
m_bndrynorm.define(m_grids, m_dmap, AMREX_SPACEDIM, ng, mf_info);
for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
m_areafrac[idim].define(amrex::convert(m_grids, IntVect::TheDimensionVector(idim)),
m_dmap, 1, ng, mf_info);
m_facecent[idim].define(amrex::convert(m_grids, IntVect::TheDimensionVector(idim)),
m_dmap, AMREX_SPACEDIM-1, ng, mf_info);
}
const auto dx = geom.CellSizeArray();
const auto dxinv = geom.InvCellSizeArray();
const auto problo = geom.ProbLoArray();
#ifdef _OPENMP
#pragma omp parallel if (Gpu::notInLaunchRegion())
#endif
{
Array<BaseFab<Real>, AMREX_SPACEDIM> intercept;
Array<BaseFab<Real>, AMREX_SPACEDIM> M2;
for (MFIter mfi(m_mgf); mfi.isValid(); ++mfi)
{
auto& gfab = m_mgf[mfi];
const Box& vbx = gfab.validbox();
auto& levelset = gfab.getLevelSet();
gshop.fillFab(levelset, geom, RunOn::Gpu);
auto& cellflag = m_cellflag[mfi];
gfab.buildTypes(cellflag);
Gpu::streamSynchronize(); // needed until the whole loop is on gpu
#if (AMREX_SPACEDIM == 3)
auto& facetype = gfab.getFaceType();
auto& edgetype = gfab.getEdgeType();
for (int idim = 0; idim < AMREX_SPACEDIM; ++idim)
{
const auto& edgetype_fab = edgetype[idim];
const Box& b = edgetype_fab.box();
auto& inter = intercept[idim];
inter.resize(b);
inter.setVal(std::numeric_limits<Real>::quiet_NaN());
edgetype_fab.ForEachIV(b, 0, 1,
[&inter, &gshop, &geom, &idim] (const Type_t& t, const IntVect& iv) {
if (t == Type::irregular) {
inter(iv) = gshop.getIntercept(idim, iv, geom);
}
});
}
for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
const Box& b = facetype[idim].box();
M2[idim].resize(b,3);
}
// regular by default
for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
m_areafrac[idim][mfi].setVal(1.0);
m_facecent[idim][mfi].setVal(0.0);
}
amrex_eb2_build_faces(BL_TO_FORTRAN_BOX(vbx),
BL_TO_FORTRAN_ANYD(cellflag),
BL_TO_FORTRAN_ANYD(facetype[0]),
BL_TO_FORTRAN_ANYD(facetype[1]),
BL_TO_FORTRAN_ANYD(facetype[2]),
BL_TO_FORTRAN_ANYD(edgetype[0]),
BL_TO_FORTRAN_ANYD(edgetype[1]),
BL_TO_FORTRAN_ANYD(edgetype[2]),
BL_TO_FORTRAN_ANYD(levelset),
BL_TO_FORTRAN_ANYD(intercept[0]),
BL_TO_FORTRAN_ANYD(intercept[1]),
BL_TO_FORTRAN_ANYD(intercept[2]),
BL_TO_FORTRAN_ANYD(m_areafrac[0][mfi]),
BL_TO_FORTRAN_ANYD(m_areafrac[1][mfi]),
BL_TO_FORTRAN_ANYD(m_areafrac[2][mfi]),
BL_TO_FORTRAN_ANYD(m_facecent[0][mfi]),
BL_TO_FORTRAN_ANYD(m_facecent[1][mfi]),
BL_TO_FORTRAN_ANYD(m_facecent[2][mfi]),
BL_TO_FORTRAN_ANYD(M2[0]),
BL_TO_FORTRAN_ANYD(M2[1]),
BL_TO_FORTRAN_ANYD(M2[2]),
dx.data(), dxinv.data(), problo.data());
// regular by default
m_volfrac[mfi].setVal(1.0);
m_centroid[mfi].setVal(0.0);
m_bndryarea[mfi].setVal(0.0);
m_bndrycent[mfi].setVal(-1.0);
m_bndrynorm[mfi].setVal(0.0);
amrex_eb2_build_cells(BL_TO_FORTRAN_BOX(vbx),
BL_TO_FORTRAN_ANYD(cellflag),
BL_TO_FORTRAN_ANYD(facetype[0]),
BL_TO_FORTRAN_ANYD(facetype[1]),
BL_TO_FORTRAN_ANYD(facetype[2]),
BL_TO_FORTRAN_ANYD(m_areafrac[0][mfi]),
BL_TO_FORTRAN_ANYD(m_areafrac[1][mfi]),
BL_TO_FORTRAN_ANYD(m_areafrac[2][mfi]),
BL_TO_FORTRAN_ANYD(m_facecent[0][mfi]),
BL_TO_FORTRAN_ANYD(m_facecent[1][mfi]),
BL_TO_FORTRAN_ANYD(m_facecent[2][mfi]),
BL_TO_FORTRAN_ANYD(M2[0]),
BL_TO_FORTRAN_ANYD(M2[1]),
BL_TO_FORTRAN_ANYD(M2[2]),
BL_TO_FORTRAN_ANYD(m_volfrac[mfi]),
BL_TO_FORTRAN_ANYD(m_centroid[mfi]),
BL_TO_FORTRAN_ANYD(m_bndryarea[mfi]),
BL_TO_FORTRAN_ANYD(m_bndrycent[mfi]),
BL_TO_FORTRAN_ANYD(m_bndrynorm[mfi]));
#elif (AMREX_SPACEDIM == 2)
auto& facetype = gfab.getFaceType();
for (int idim = 0; idim < AMREX_SPACEDIM; ++idim)
{
const auto& facetype_fab = facetype[idim];
const Box& b = facetype_fab.box();
auto& inter = intercept[idim];
inter.resize(b);
inter.setVal(std::numeric_limits<Real>::quiet_NaN());
facetype_fab.ForEachIV(b, 0, 1,
[&inter, &gshop, &geom, &idim] (const Type_t& t, const IntVect& iv) {
if (t == Type::irregular) {
inter(iv) = gshop.getIntercept(1-idim, iv, geom);
}
});
}
// regular by default
for (int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
m_areafrac[idim][mfi].setVal(1.0);
m_facecent[idim][mfi].setVal(0.0);
}
amrex_eb2_build_faces(BL_TO_FORTRAN_BOX(vbx),
BL_TO_FORTRAN_ANYD(cellflag),
BL_TO_FORTRAN_ANYD(facetype[0]),
BL_TO_FORTRAN_ANYD(facetype[1]),
BL_TO_FORTRAN_ANYD(levelset),
BL_TO_FORTRAN_ANYD(intercept[0]),
BL_TO_FORTRAN_ANYD(intercept[1]),
BL_TO_FORTRAN_ANYD(m_areafrac[0][mfi]),
BL_TO_FORTRAN_ANYD(m_areafrac[1][mfi]),
BL_TO_FORTRAN_ANYD(m_facecent[0][mfi]),
BL_TO_FORTRAN_ANYD(m_facecent[1][mfi]),
dx.data(), dxinv.data(), problo.data());
// regular by default
m_volfrac[mfi].setVal(1.0);
m_centroid[mfi].setVal(0.0);
m_bndryarea[mfi].setVal(0.0);
m_bndrycent[mfi].setVal(-1.0);
m_bndrynorm[mfi].setVal(0.0);
amrex_eb2_build_cells(BL_TO_FORTRAN_BOX(vbx),
BL_TO_FORTRAN_ANYD(cellflag),
BL_TO_FORTRAN_ANYD(facetype[0]),
BL_TO_FORTRAN_ANYD(facetype[1]),
BL_TO_FORTRAN_ANYD(m_areafrac[0][mfi]),
BL_TO_FORTRAN_ANYD(m_areafrac[1][mfi]),
BL_TO_FORTRAN_ANYD(m_volfrac[mfi]),
BL_TO_FORTRAN_ANYD(m_centroid[mfi]),
BL_TO_FORTRAN_ANYD(m_bndryarea[mfi]),
BL_TO_FORTRAN_ANYD(m_bndrycent[mfi]),
BL_TO_FORTRAN_ANYD(m_bndrynorm[mfi]));
#endif
}
}
m_levelset = m_mgf.getLevelSet();
m_ok = true;
}
template <typename G>
GShopLevel<G>::GShopLevel (IndexSpace const* is, int ilev, int max_grid_size, int /*ngrow*/,
const Geometry& geom, GShopLevel<G>& fineLevel)
: Level(is, geom)
{
if (fineLevel.isAllRegular()) {
m_allregular = true;
m_ok = true;
return;
}
BL_PROFILE("EB2::GShopLevel()-coarse");
const BoxArray& fine_grids = fineLevel.m_grids;
const BoxArray& fine_covered_grids = fineLevel.m_covered_grids;
const int coarse_ratio = 2;
const int min_width = 8;
bool coarsenable = fine_grids.coarsenable(coarse_ratio, min_width)
&& (fine_covered_grids.empty() || fine_covered_grids.coarsenable(coarse_ratio));
m_ngrow = amrex::coarsen(fineLevel.m_ngrow,2);
if (amrex::scale(m_ngrow,2) != fineLevel.m_ngrow) {
m_ngrow = IntVect::TheZeroVector();
}
if (coarsenable)
{
int ierr = coarsenFromFine(fineLevel, true);
m_ok = (ierr == 0);
}
else
{
Level fine_level_2(is, fineLevel.m_geom);
fine_level_2.prepareForCoarsening(fineLevel, max_grid_size, amrex::scale(m_ngrow,2));
int ierr = coarsenFromFine(fine_level_2, false);
m_ok = (ierr == 0);
}
}
}}
#endif