Skip to content

Commit 52e4659

Browse files
authored
AMRErrorTag: Add PARSER (#4534)
1 parent 449ff2f commit 52e4659

File tree

5 files changed

+157
-15
lines changed

5 files changed

+157
-15
lines changed

Src/AmrCore/AMReX_ErrorList.H

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
#include <AMReX_Config.H>
44

55
#include <AMReX_Array.H>
6-
#include <AMReX_Vector.H>
7-
#include <AMReX_REAL.H>
86
#include <AMReX_ArrayLim.H>
7+
#include <AMReX_Geometry.H>
98
#include <AMReX_MultiFab.H>
9+
#include <AMReX_Parser.H>
10+
#include <AMReX_REAL.H>
1011
#include <AMReX_TagBox.H>
11-
#include <AMReX_Geometry.H>
12+
#include <AMReX_Vector.H>
1213

1314
#include <string>
1415
#include <memory>
@@ -426,7 +427,7 @@ std::ostream& operator << (std::ostream& os, const ErrorList& elst);
426427
{
427428
public:
428429

429-
enum TEST {GRAD=0, RELGRAD, LESS, GREATER, VORT, BOX, USER};
430+
enum TEST {GRAD=0, RELGRAD, LESS, GREATER, VORT, BOX, USER, PARSER};
430431

431432
struct UserFunc
432433
{
@@ -482,6 +483,12 @@ std::ostream& operator << (std::ostream& os, const ErrorList& elst);
482483
const AMRErrorTagInfo& info = AMRErrorTagInfo()) noexcept
483484
: m_userfunc(userfunc), m_field(std::move(field)), m_info(info), m_ngrow(ngrow) {}
484485

486+
//! Construct AMRErrorTag from a Parser that returns Real for given
487+
//! (x,y,z,t). This class's operator() can then be used to tag regions
488+
//! where the Parser's return value is greater than 0.
489+
explicit AMRErrorTag (Parser parser,
490+
const AMRErrorTagInfo& info = AMRErrorTagInfo());
491+
485492
void operator() (amrex::TagBoxArray& tb,
486493
const amrex::MultiFab* mf,
487494
char clearval,
@@ -503,9 +510,11 @@ std::ostream& operator << (std::ostream& os, const ErrorList& elst);
503510
Vector<Real> m_value;
504511
TEST m_test{BOX};
505512
UserFunc* m_userfunc = nullptr;
513+
std::unique_ptr<Parser> m_parser;
514+
ParserExecutor<4> m_parser_exe; // 4: (x,y,z,t)
506515
std::string m_field;
507516
AMRErrorTagInfo m_info;
508-
int m_ngrow;
517+
int m_ngrow = 0;
509518
};
510519
}
511520

Src/AmrCore/AMReX_ErrorList.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,21 @@ operator << (std::ostream& os,
214214
return os;
215215
}
216216

217+
AMRErrorTag::AMRErrorTag (Parser parser, const AMRErrorTagInfo& info)
218+
: m_test(PARSER),
219+
m_parser(std::make_unique<Parser>(std::move(parser))),
220+
m_parser_exe(m_parser->compile<4>()),
221+
m_info(info)
222+
{}
223+
217224
int
218225
AMRErrorTag::SetNGrow () const noexcept
219226
{
220-
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_test != USER, "Do not call SetNGrow with USER test");
221-
static std::map<TEST,int> ng = { {GRAD,1}, {RELGRAD,1}, {LESS,0}, {GREATER,0}, {VORT,0}, {BOX,0} };
222-
return ng[m_test];
227+
if (m_test == GRAD || m_test == RELGRAD) {
228+
return 1;
229+
} else {
230+
return 0;
231+
}
223232
}
224233

225234
void
@@ -272,6 +281,30 @@ AMRErrorTag::operator() (TagBoxArray& tba,
272281
}
273282
});
274283
}
284+
else if (m_test == PARSER)
285+
{
286+
const auto& plo = geom.ProbLoArray();
287+
const auto& dx = geom.CellSizeArray();
288+
auto tag_update = m_info.m_derefine ? clearval : tagval;
289+
auto fn = m_parser_exe;
290+
ParallelFor(tba, [=] AMREX_GPU_DEVICE (int bi, int i, int j, int k) noexcept
291+
{
292+
auto x = plo[0]+(Real(i)+Real(0.5))*dx[0];
293+
#if (AMREX_SPACEDIM > 1)
294+
auto y = plo[1]+(Real(j)+Real(0.5))*dx[1];
295+
#else
296+
auto y = Real(0);
297+
#endif
298+
#if (AMREX_SPACEDIM == 3)
299+
auto z = plo[2]+(Real(k)+Real(0.5))*dx[2];
300+
#else
301+
auto z = Real(0);
302+
#endif
303+
if (fn(x,y,z,time) > 0) {
304+
tagma[bi](i,j,k) = tag_update;
305+
}
306+
});
307+
}
275308
else
276309
{
277310
auto const& datma = mf->const_arrays();
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# *****************************************************************
2+
# Run until nsteps == max_step or time == stop_time,
3+
# whichever comes first
4+
# *****************************************************************
5+
max_step = 1000
6+
stop_time = 2.0
7+
8+
# *****************************************************************
9+
# Are we restarting from an existing checkpoint file?
10+
# *****************************************************************
11+
#amr.restart = chk00060 # restart from this checkpoint file
12+
13+
# *****************************************************************
14+
# Problem size and geometry
15+
# *****************************************************************
16+
geometry.prob_lo = 0.0 0.0 0.0
17+
geometry.prob_hi = 1.0 1.0 0.125
18+
geometry.is_periodic = 1 1 1
19+
20+
# *****************************************************************
21+
# VERBOSITY
22+
# *****************************************************************
23+
amr.v = 1 # verbosity in Amr
24+
25+
# *****************************************************************
26+
# Resolution and refinement
27+
# *****************************************************************
28+
amr.n_cell = 64 64 8
29+
amr.max_level = 2 # maximum level number allowed --
30+
# number of levels = max_level + 1
31+
32+
amr.ref_ratio = 2 2 2 2 # refinement ratio between levels
33+
34+
# *****************************************************************
35+
# Control of grid creation
36+
# *****************************************************************
37+
# Blocking factor for grid creation in each dimension --
38+
# this ensures that every grid is coarsenable by a factor of 8 --
39+
# this is mostly relevant for multigrid performance
40+
amr.blocking_factor_x = 8
41+
amr.blocking_factor_y = 8
42+
amr.blocking_factor_z = 8
43+
44+
amr.max_grid_size = 16
45+
46+
amr.regrid_int = 2 # how often to regrid
47+
48+
# *****************************************************************
49+
# Time step control
50+
# *****************************************************************
51+
adv.cfl = 0.7 # CFL constraint for explicit advection
52+
53+
adv.do_subcycle = 1 # Do we subcycle in time?
54+
55+
# *****************************************************************
56+
# Should we reflux at coarse-fine boundaries?
57+
# *****************************************************************
58+
adv.do_reflux = 1
59+
60+
# *****************************************************************
61+
# Tagging - if phi > 1.01 at level 0, then refine
62+
# if phi > 1.1 at level 1, then refine
63+
# if phi > 1.5 at level 2, then refine
64+
# *****************************************************************
65+
adv.phierr = 1.01 1.1 1.5
66+
adv.errfn = "1.e-3-((x-0.2*t)**2+(y-0.3*t)**2+z**2)" # tag moving sphere
67+
68+
# *****************************************************************
69+
# Plotfile name and frequency
70+
# *****************************************************************
71+
amr.plot_file = plt # root name of plot file
72+
amr.plot_int = 10 # number of timesteps between plot files
73+
# if negative then no plot files will be written
74+
75+
# *****************************************************************
76+
# Checkpoint name and frequency
77+
# *****************************************************************
78+
amr.chk_file = chk # root name of checkpoint file
79+
amr.chk_int = -1 # number of timesteps between checkpoint files
80+
# if negative then no checkpoint files will be written
81+
82+
# *****************************************************************
83+
# Particles
84+
# *****************************************************************
85+
amr.do_tracers = 1 # Turn tracer particles on or off

Tests/Amr/Advection_AmrCore/Source/AmrCoreAdv.H

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <AMReX_AmrCore.H>
1111
#include <AMReX_BCRec.H>
12+
#include <AMReX_ErrorList.H>
1213
#include <AMReX_FluxRegister.H>
1314
#include <AMReX_FillPatcher.H>
1415

@@ -215,6 +216,9 @@ private:
215216
//number of ghost cells on facevel
216217
int nghost = 2;
217218

219+
std::string error_fn;
220+
amrex::Vector<amrex::AMRErrorTag> error_tag;
221+
218222
#ifdef AMREX_PARTICLES
219223
void init_particles ();
220224
int do_tracers = 0;

Tests/Amr/Advection_AmrCore/Source/AmrCoreAdv.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ AmrCoreAdv::AmrCoreAdv ()
9090
// fillpatcher[lev] is for filling data on level lev using the data on
9191
// lev-1 and lev.
9292
fillpatcher.resize(nlevs_max+1);
93+
94+
// amrex::AMRErrorTag supports a number of common tagging
95+
// approaches. Here we set it up to use amrex::Parser.
96+
if (! error_fn.empty()) {
97+
amrex::Parser parser(error_fn);
98+
parser.registerVariables({"x","y","z","t"});
99+
error_tag.emplace_back(std::move(parser));
100+
}
93101
}
94102

95103
AmrCoreAdv::~AmrCoreAdv () = default;
@@ -349,7 +357,7 @@ void AmrCoreAdv::MakeNewLevelFromScratch (int lev, Real time, const BoxArray& ba
349357
// tag all cells for refinement
350358
// overrides the pure virtual function in AmrCore
351359
void
352-
AmrCoreAdv::ErrorEst (int lev, TagBoxArray& tags, Real /*time*/, int /*ngrow*/)
360+
AmrCoreAdv::ErrorEst (int lev, TagBoxArray& tags, Real time, int /*ngrow*/)
353361
{
354362
static bool first = true;
355363
static Vector<Real> phierr;
@@ -370,18 +378,16 @@ AmrCoreAdv::ErrorEst (int lev, TagBoxArray& tags, Real /*time*/, int /*ngrow*/)
370378
}
371379
}
372380

373-
if (lev >= phierr.size()) { return; }
374-
375-
// const int clearval = TagBox::CLEAR;
381+
const int clearval = TagBox::CLEAR;
376382
const int tagval = TagBox::SET;
377383

378-
const MultiFab& state = phi_new[lev];
384+
if (lev < phierr.size())
385+
{
386+
const MultiFab& state = phi_new[lev];
379387

380388
#ifdef AMREX_USE_OMP
381389
#pragma omp parallel if(Gpu::notInLaunchRegion())
382390
#endif
383-
{
384-
385391
for (MFIter mfi(state,TilingIfNotGPU()); mfi.isValid(); ++mfi)
386392
{
387393
const Box& bx = mfi.tilebox();
@@ -396,6 +402,10 @@ AmrCoreAdv::ErrorEst (int lev, TagBoxArray& tags, Real /*time*/, int /*ngrow*/)
396402
});
397403
}
398404
}
405+
406+
for (auto const& etag : error_tag) {
407+
etag(tags, nullptr, clearval, tagval, time, lev, geom[lev]);
408+
}
399409
}
400410

401411
// read in some parameters from inputs file
@@ -425,6 +435,7 @@ AmrCoreAdv::ReadParameters ()
425435
pp.query("cfl", cfl);
426436
pp.query("do_reflux", do_reflux);
427437
pp.query("do_subcycle", do_subcycle);
438+
pp.query("errfn", error_fn);
428439
}
429440

430441
#ifdef AMREX_PARTICLES

0 commit comments

Comments
 (0)