18#ifndef PECLET_FLOW_MAC_VELOCITY_MG_HPP
19#define PECLET_FLOW_MAC_VELOCITY_MG_HPP
22#include <Kokkos_Core.hpp>
38 const bool hasPin = (pin.extent(0) != 0);
40 "peclet::flow::vmg_resid",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
41 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
42 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
43 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
44 if (hasPin && pin(i) > 0.5) {
48 const double Ax = (double)
AC(i) * x(i) + (double)AE(i) * x(i + sx) +
49 (double)AW(i) * x(i - sx) + (double)AN(i) * x(i + sy) +
50 (double)AS(i) * x(i - sy) + (double)AT(i) * x(i + sz) +
51 (double)AB(i) * x(i - sz);
63 FPC AT,
CCConst pin,
C3 e,
int g,
int holdAxis,
int holdIdx,
64 double& rmax,
double& bdmax,
double& bmax) {
65 const bool hasPin = (pin.extent(0) != 0);
67 "peclet::flow::vmg_resid_max",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
68 KOKKOS_LAMBDA(
int lx,
int ly,
int lz,
double& m,
double& d,
double& bm) {
69 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
70 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
72 if (hasPin && pin(i) > 0.5) {
75 const double Ax = (double)
AC(i) * x(i) + (double)AE(i) * x(i + sx) +
76 (double)AW(i) * x(i - sx) + (double)AN(i) * x(i + sy) +
77 (double)AS(i) * x(i - sy) + (double)AT(i) * x(i + sz) +
78 (double)AB(i) * x(i - sz);
81 if (holdAxis >= 0 && (holdAxis == 0 ? lx : (holdAxis == 1 ? ly : lz)) == holdIdx)
83 const double ar = Kokkos::fabs(r);
86 const double ad = Kokkos::fabs(b(i) - r);
89 const double ab = Kokkos::fabs(b(i));
93 Kokkos::Max<double>(rmax), Kokkos::Max<double>(bdmax), Kokkos::Max<double>(bmax));
100 C3 finner,
C3 ratio,
double eps) {
103 Kokkos::parallel_for(
104 "peclet::flow::vmg_prolong_masked", MD(space, {0, 0, 0}, {finner.
x, finner.
y, finner.
z}),
105 KOKKOS_LAMBDA(
int ifx,
int ify,
int ifz) {
107 (long)(ifx + g) + (long)(ify + g) * fext.
x + (long)(ifz + g) * (long)fext.
x * fext.
y;
110 const double cx = (ratio.x == 2) ? 0.5 * ifx - 0.25 + g : ifx + g;
111 const double cy = (ratio.y == 2) ? 0.5 * ify - 0.25 + g : ify + g;
112 const double cz = (ratio.z == 2) ? 0.5 * ifz - 0.25 + g : ifz + g;
113 const double fxw = Kokkos::floor(cx), fyw = Kokkos::floor(cy), fzw = Kokkos::floor(cz);
114 const double wx = cx - fxw, wy = cy - fyw, wz = cz - fzw;
115 const int x0 = (int)fxw, y0 = (
int)fyw, z0 = (int)fzw;
116 const long sy = cext.
x, sz = (long)cext.
x * cext.
y;
117 auto C = [&](
int xx,
int yy,
int zz) {
118 return coarse((
long)xx + (
long)yy * sy + (
long)zz * sz);
120 const double c00 = C(x0, y0, z0) * (1 - wx) + C(x0 + 1, y0, z0) * wx;
121 const double c10 = C(x0, y0 + 1, z0) * (1 - wx) + C(x0 + 1, y0 + 1, z0) * wx;
122 const double c01 = C(x0, y0, z0 + 1) * (1 - wx) + C(x0 + 1, y0, z0 + 1) * wx;
123 const double c11 = C(x0, y0 + 1, z0 + 1) * (1 - wx) + C(x0 + 1, y0 + 1, z0 + 1) * wx;
124 const double c0 = c00 * (1 - wy) + c10 * wy, c1 = c01 * (1 - wy) + c11 * wy;
125 fine(fi) += c0 * (1 - wz) + c1 * wz;
133 CCConst theta,
C3 e,
int g,
double bx,
double by,
double bz,
134 double thresh,
double idiag) {
137 Kokkos::parallel_for(
138 "peclet::flow::vmg_staircase", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
139 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
140 const long i = (long)lx + (
long)ly * e.x + (long)lz * (
long)e.x * e.y;
144 AW(i) = AE(i) = AS(i) = AN(i) = AB(i) = AT(i) = (
MReal)0.0;
147 AC(i) = (
MReal)(idiag + 2.0 * (bx + by + bz));
148 AW(i) = (
MReal)(-bx);
149 AE(i) = (
MReal)(-bx);
150 AS(i) = (
MReal)(-by);
151 AN(i) = (
MReal)(-by);
152 AB(i) = (
MReal)(-bz);
153 AT(i) = (
MReal)(-bz);
160 C3 e,
int g,
double thresh) {
163 Kokkos::parallel_for(
164 "peclet::flow::vmg_pin_rows", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
165 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
166 const long i = (long)lx + (
long)ly * e.x + (long)lz * (
long)e.x * e.y;
167 if (theta(i) < thresh) {
169 AW(i) = AE(i) = AS(i) = AN(i) = AB(i) = AT(i) = (
MReal)0.0;
181 double bz,
double fouw,
double sx,
double sy,
double sz,
double idiag) {
184 Kokkos::parallel_for(
185 "peclet::flow::vmg_adv_coarse", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
186 KOKKOS_LAMBDA(
int x,
int y,
int z) {
187 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
188 double cC = idiag + 2.0 * (bx + by + bz), cxm = -bx, cxp = -bx, cym = -by, cyp = -by,
189 czm = -bz, czp = -bz;
191 sadv::fou_operator_aniso(comp, x, y, z, Ua, Va, Wa, fouw, sx, sy, sz, cC, cxm, cxp, cym,
207 double by,
double bz,
double idiag) {
209 const std::size_t n = (std::size_t)e.x * e.y * e.z;
212 Kokkos::parallel_for(
213 "peclet::flow::vmg_const_aniso", Kokkos::RangePolicy<CCExec>(space, 0, n),
214 KOKKOS_LAMBDA(std::size_t i) {
231 int dims[3] = {e.x, e.y, e.z};
232 long st[3] = {1, e.x, (long)e.x * e.y};
233 const int b = (a + 1) % 3, c = (a + 2) % 3;
234 const long sa = st[a], sb = st[b], sc = st[c];
235 const int bic = (s == 0) ? g : (dims[a] - g - 1);
236 Kokkos::parallel_for(
237 "peclet::flow::vmg_bc_fold",
MDRange2<CCExec>(space, {0, 0}, {dims[b], dims[c]}),
238 KOKKOS_LAMBDA(
int p0,
int p1) {
239 const long i = (long)p0 * sb + (
long)p1 * sc + (long)bic * sa;
249 int dims[3] = {e.x, e.y, e.z};
250 long st[3] = {1, e.x, (long)e.x * e.y};
251 const int b = (a + 1) % 3, c = (a + 2) % 3;
252 const long sa = st[a], sb = st[b], sc = st[c];
253 const int na = dims[a];
254 Kokkos::parallel_for(
255 "peclet::flow::vmg_fill_bc_ghost",
MDRange2<CCExec>(space, {0, 0}, {dims[b], dims[c]}),
256 KOKKOS_LAMBDA(
int p0,
int p1) {
257 const long base = (long)p0 * sb + (
long)p1 * sc;
259 const double v = dirichlet ? 0.0 : x(base + (
long)g * sa);
260 for (
int ia = 0; ia < g; ++ia)
261 x(base + (
long)ia * sa) = v;
263 const double v = dirichlet ? 0.0 : x(base + (
long)(na - g - 1) * sa);
264 for (
int ia = na - g; ia < na; ++ia)
265 x(base + (
long)ia * sa) = v;
274 int dims[3] = {e.x, e.y, e.z};
275 long st[3] = {1, e.x, (long)e.x * e.y};
276 const int b = (axis + 1) % 3, c = (axis + 2) % 3;
277 const long sa = st[axis], sb = st[b], sc = st[c];
278 Kokkos::parallel_for(
279 "peclet::flow::vmg_zero_plane",
MDRange2<CCExec>(space, {0, 0}, {dims[b], dims[c]}),
280 KOKKOS_LAMBDA(
int p0,
int p1) { m((
long)p0 * sb + (
long)p1 * sc + (
long)idx * sa) = 0.0; });
286 std::size_t n = m.extent(0);
289 Kokkos::parallel_for(
290 "peclet::flow::vmg_threshold", Kokkos::RangePolicy<CCExec>(space, 0, n),
291 KOKKOS_LAMBDA(std::size_t i) { mm(i) = (th(i) < thresh) ? 1.0 : 0.0; });
298 Kokkos::parallel_reduce(
299 "peclet::flow::vmg_maxabs", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
300 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& acc) {
301 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
302 const double d = Kokkos::fabs(a(i));
306 Kokkos::Max<double>(m));
314 Kokkos::parallel_reduce(
315 "peclet::flow::vmg_maxabsdiff", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
316 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& acc) {
317 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
318 const double d = Kokkos::fabs(a(i) - b(i));
322 Kokkos::Max<double>(m));
327 std::size_t n = r.extent(0);
330 Kokkos::parallel_for(
331 "peclet::flow::vmg_mulmask", Kokkos::RangePolicy<CCExec>(space, 0, n),
332 KOKKOS_LAMBDA(std::size_t i) { rr(i) *= mm(i); });
339 static constexpr int G = 2;
340#ifdef PECLET_FLOW_MPI
341 using Comm = MPI_Comm;
342 static MPI_Comm
nullComm() {
return MPI_COMM_NULL; }
357#ifdef PECLET_FLOW_MPI
358 std::shared_ptr<GridHaloTopology<3>> halo;
359 std::shared_ptr<GridHalo<double>> dev;
370 for (
int a = 0; a < 3; ++a) {
374 aniso_ = !(hp_[0] == 1.0 && hp_[1] == 1.0 && hp_[2] == 1.0);
380 r.reserve(lv_.size());
381 for (
const auto& v : lv_)
382 r.push_back(v.ratio);
389 C3 inner{nx, ny, nz}, cf{1, 1, 1};
390 for (
int L = 0; L <
nLevels; ++L) {
394 v.ext =
C3{inner.x + 2 *
G, inner.y + 2 *
G, inner.z + 2 *
G};
396 v.n = (std::size_t)v.ext.x * v.ext.y * v.ext.z;
397 auto can = [&](
int d) {
return (d % 2 == 0) && (d / 2 >= 2); };
403 const bool canA[3] = {can(inner.x), can(inner.y), can(inner.z)};
404 const double H[3] = {hp_[0] * (double)cf.x, hp_[1] * (
double)cf.y, hp_[2] * (double)cf.z};
407 next.
x = inner.x / 2;
409 next.
y = inner.y / 2;
411 next.
z = inner.z / 2;
415 v.rhs =
CCField(
"vmg_rhs", v.n);
416 v.res =
CCField(
"vmg_res", v.n);
417 v.theta =
CCField(
"vmg_th", v.n);
418 v.pin =
CCField(
"vmg_pin", v.n);
420 v.advU =
CCField(
"vmg_au", v.n);
421 v.advV =
CCField(
"vmg_av", v.n);
422 v.advW =
CCField(
"vmg_aw", v.n);
424 for (
FPV* p : {&v.AC, &v.AW, &v.AE, &v.AS, &v.AN, &v.AB, &v.AT})
425 *p =
FPV(
"vmg_A", v.n);
427 if (next.
x == inner.x && next.
y == inner.y && next.
z == inner.z)
430 cf =
C3{cf.x * ratio.x, cf.y * ratio.y, cf.z * ratio.z};
433 CCField(
"vmg_resmask0", lv_[0].n);
435#ifdef PECLET_FLOW_MPI
440 void initMpi(
int gnx,
int gny,
int gnz,
int nLevels, MPI_Comm comm) {
442 MPI_Comm_size(comm, &size);
443 initMpi(peclet::core::decomp::BlockDecomposer<3>(
static_cast<std::size_t
>(size),
444 peclet::core::IVec<3>{gnx, gny, gnz}),
453 void initMpi(
const peclet::core::decomp::BlockDecomposer<3>& dec0,
int nLevels, MPI_Comm comm,
454 bool inPlace =
true) {
457 int rank = 0, size = 1;
458 MPI_Comm_rank(comm, &rank);
459 MPI_Comm_size(comm, &size);
460 std::array<bool, 3> per{
true,
true,
true};
461 const auto& g0 = dec0.globalSize();
462 C3
gs{(
int)g0[0], (
int)g0[1], (
int)g0[2]},
cf{1, 1, 1};
463 auto can = [&](
int d) {
return (d % 2 == 0) && (
d / 2 >= 2); };
464 auto evenOn = [](
const peclet::core::decomp::BlockDecomposer<3>&
d,
int ax) {
465 for (std::size_t b = 0;
b <
d.numBlocks(); ++
b) {
466 const auto blk =
d.block(b);
467 if (blk.origin[ax] % 2 != 0 || blk.size[ax] % 2 != 0)
472 peclet::core::decomp::BlockDecomposer<3> dec = dec0;
475 v.halo = std::make_shared<GridHaloTopology<3>>();
476 if (!inPlace && L > 0)
477 dec = peclet::core::decomp::BlockDecomposer<3>(
static_cast<std::size_t
>(size),
478 peclet::core::IVec<3>{
gs.x,
gs.y,
gs.z});
479 v.halo->buildTopology(dec, rank,
G, per, comm);
480 v.dev = std::make_shared<GridHalo<double>>();
481 v.dev->init(*
v.halo);
482 v.dev->setLabel(
"vmg L" + std::to_string(L));
483 const auto& idx =
v.halo->indexer();
484 const auto eg = idx.sizeInclGhost(), ino = idx.sizeInner(), oig = idx.originInclGhost();
485 v.ext = {(
int)eg[0], (
int)
eg[1], (
int)eg[2]};
486 v.inner = {(
int)ino[0], (
int)ino[1], (
int)ino[2]};
487 v.og = {(
int)oig[0] +
G, (
int)oig[1] +
G, (
int)oig[2] +
G};
490 v.n = idx.numCellsInclGhost();
495 const bool canA[3] = {can(
gs.x) && (!inPlace || evenOn(dec, 0)),
496 can(
gs.y) && (!inPlace || evenOn(dec, 1)),
497 can(
gs.z) && (!inPlace || evenOn(dec, 2))};
498 const double H[3] = {hp_[0] * (double)
cf.x, hp_[1] * (
double)
cf.y, hp_[2] * (double)
cf.z};
518 for (
FPV* p : {&
v.AC, &
v.AW, &
v.AE, &
v.AS, &
v.AN, &
v.AB, &
v.AT})
519 *p =
FPV(
"vmg_A",
v.n);
521 if (next.
x ==
gs.x && next.
y ==
gs.y && next.
z ==
gs.z)
528 lv_[0].resMask =
CCField(
"vmg_resmask0", lv_[0].n);
536 const int o = (a == 0) ? lv.og.x : (a == 1) ? lv.og.y : lv.og.z;
537 const int n = (a == 0) ? lv.inner.x : (a == 1) ? lv.inner.y : lv.inner.z;
538 const int gn = (a == 0) ? lv.gdim.x : (a == 1) ? lv.gdim.y : lv.gdim.z;
539 return (f % 2 == 0) ? (o == 0) : (o + n == gn);
541 int nLevels()
const {
return (
int)lv_.size(); }
545 int levels()
const {
return (
int)lv_.size(); }
550 Kokkos::deep_copy(f.AC,
AC);
551 Kokkos::deep_copy(f.AW, AW);
552 Kokkos::deep_copy(f.AE, AE);
553 Kokkos::deep_copy(f.AS, AS);
554 Kokkos::deep_copy(f.AN, AN);
555 Kokkos::deep_copy(f.AB, AB);
556 Kokkos::deep_copy(f.AT, AT);
567 Kokkos::deep_copy(f.theta, theta0);
568 Kokkos::deep_copy(f.pin, solid0);
569 Kokkos::deep_copy(f.resMask, resmask0);
570 for (
int L = 1; L < (int)lv_.size(); ++L) {
572 Level& fin = lv_[L - 1];
576 const double bx = nu_dt * w_[0] / (double)(c.cfac.x * c.cfac.x),
577 by = nu_dt * w_[1] / (double)(c.cfac.y * c.cfac.y),
578 bz = nu_dt * w_[2] / (double)(c.cfac.z * c.cfac.z);
579 buildVelocityStaircase(c.AC, c.AW, c.AE, c.AS, c.AN, c.AB, c.AT,
CCConst(c.theta), c.ext,
G,
580 bx, by, bz, thresh, idiag);
595 double idiag,
double thresh,
bool upwind =
false,
double fouw = 0.0) {
597 setStaircase(theta0, solid0, resmask0, nu_dt, idiag, thresh);
599 for (
int L = 1; L < (int)lv_.size(); ++L) {
601 const double bx = nu_dt * w_[0] / (double)(c.cfac.x * c.cfac.x),
602 by = nu_dt * w_[1] / (double)(c.cfac.y * c.cfac.y),
603 bz = nu_dt * w_[2] / (double)(c.cfac.z * c.cfac.z);
605 CCConst(c.advW), comp, c.ext,
G, bx, by, bz, fouw, 1.0 / c.cfac.x,
606 1.0 / c.cfac.y, 1.0 / c.cfac.z, idiag);
607 pinSolidRows(c.AC, c.AW, c.AE, c.AS, c.AN, c.AB, c.AT,
CCConst(c.theta), c.ext,
G, thresh);
611 const int t = bc_[2 * comp];
612 if ((t == 1 || t == 2 || t == 4) &&
touches(f, 2 * comp))
615 for (
int L = 1; L < (int)lv_.size(); ++L) {
617 const double bx = nu_dt * w_[0] / (double)(c.cfac.x * c.cfac.x),
618 by = nu_dt * w_[1] / (double)(c.cfac.y * c.cfac.y),
619 bz = nu_dt * w_[2] / (double)(c.cfac.z * c.cfac.z);
620 for (
int ff = 0; ff < 6; ++ff) {
621 const int a = ff / 2, sd = ff % 2;
622 const double ba = (a == 0) ? bx : (a == 1) ? by : bz;
626 else if ((bc_[ff] == 1 || bc_[ff] == 2) && a != comp)
643 for (
int L = 1; L < (int)lv_.size(); ++L) {
645 Level& fin = lv_[L - 1];
663 for (
int L = 1; L < (int)lv_.size(); ++L) {
665 const double bx = nu_dt * w_[0] / (double)(c.cfac.x * c.cfac.x),
666 by = nu_dt * w_[1] / (double)(c.cfac.y * c.cfac.y),
667 bz = nu_dt * w_[2] / (double)(c.cfac.z * c.cfac.z);
668 const double sx = 1.0 / (double)c.cfac.x, sy = 1.0 / (
double)c.cfac.y,
669 sz = 1.0 / (double)c.cfac.z;
671 CCConst(c.advW), comp, c.ext,
G, bx, by, bz, fouw, sx, sy, sz, idiag);
681 for (
int i = 0; i < 6; ++i) {
702 chebDegree_ = degree;
703 chebEigRatio_ = eigRatio > 1.0 ? eigRatio : 10.0;
719 Kokkos::deep_copy(f.resMask, 1.0);
721 for (
int s = 0; s < 1;
723 const int t = bc_[2 * comp + s];
724 if (t == 1 || t == 2 || t == 4) {
730 for (
int L = 0; L < (int)lv_.size(); ++L) {
732 const double bx = nu_dt * w_[0] / (double)(c.cfac.x * c.cfac.x),
733 by = nu_dt * w_[1] / (double)(c.cfac.y * c.cfac.y),
734 bz = nu_dt * w_[2] / (double)(c.cfac.z * c.cfac.z);
735 buildConstAniso(c.AC, c.AW, c.AE, c.AS, c.AN, c.AB, c.AT, c.ext, bx, by, bz, idiag);
736 for (
int f = 0; f < 6; ++f) {
737 const int a = f / 2, s = f % 2;
738 const double ba = (a == 0) ? bx : (a == 1) ? by : bz;
742 else if ((bc_[f] == 1 || bc_[f] == 2) && a != comp)
744 else if (bc_[f] == 4 && a != comp && L == 0)
768#ifdef PECLET_FLOW_MPI
769 if (distributed_ && comm != MPI_COMM_NULL) {
771 MPI_Allreduce(&v, &g, 1, MPI_DOUBLE, MPI_MAX, comm);
782 for (
Level& lv : lv_) {
783 double lo = 0.0, hi = 0.0;
785 FPC(lv.AT), usePin_ ?
CCConst(lv.pin) : empty_, lv.ext,
G, lo, hi);
801 if (bcMode_ && heldComp_ >= 0) {
802 const int t = bc_[2 * heldComp_];
803 if ((t == 1 || t == 2 || t == 4) &&
touches(l0, 2 * heldComp_))
836 } restore{l0, l0.
rhs, l0.
x,
false};
837 if (!distributed_ && b.extent(0) == l0.
n && x.extent(0) == l0.
n) {
839 l0.
rhs =
CCField(
const_cast<double*
>(b.data()), l0.
n);
842 Kokkos::deep_copy(l0.
rhs, b);
843 Kokkos::deep_copy(l0.
x, x);
850 const bool useRes = resTol > 0.0;
851 const bool useDu = !useRes && tol > 0.0;
852 if (useDu && prev_.extent(0) != l0.
n)
854 double du0 = 0.0, bnorm = 0.0;
856 lastResRatio_ = -1.0;
862 lastResRatio_ = bnorm > 0.0 ? r0 / bnorm : 0.0;
865 for (
int v = 0; v < nvc; ++v) {
867 Kokkos::deep_copy(prev_, l0.
x);
871 lastResRatio_ = bnorm > 0.0 ? r / bnorm : 0.0;
873 const bool floor = r <= 1e-14 * bnorm || (rPrev >= 0.0 && r >= rPrev);
875 if (r <= resTol * bnorm || floor) {
883 if (du <= tol * du0) {
890 Kokkos::deep_copy(x, l0.
x);
898 const bool l0 = (L == 0);
899 if (L + 1 == (
int)lv_.size()) {
905 if (l0 && bcApplyL0_)
909 usePin_ ?
CCConst(lv.pin) : empty_, lv.ext,
G);
911 useResMask_ && (lv.resMask.extent(0) == lv.n);
914 Level& cs = lv_[L + 1];
916 Kokkos::deep_copy(cs.x, 0.0);
931 if (chebSmooth_ && lv.chebHi > 0.0) {
932 chebSmooth(lv, chebDegree_ > 0 ? chebDegree_ : sweeps, isL0, pin);
935 for (
int k = 0; k < sweeps; ++k)
936 for (
int color = 0; color < 2; ++color) {
937#ifdef PECLET_FLOW_MPI
938 if (distributed_ && !bcMode_) {
942 const C3 lo{
G + 1,
G + 1,
G + 1};
943 const C3 hi{lv.ext.x -
G - 1, lv.ext.y -
G - 1, lv.ext.z -
G - 1};
944 lv.dev->exchangeBegin(lv.x);
946 FPC(lv.AS),
FPC(lv.AN),
FPC(lv.AB),
FPC(lv.AT), pin, lv.ext, og,
947 color, lo, hi,
C3{0, 0, 0},
C3{0, 0, 0});
948 lv.dev->exchangeEnd(lv.x);
950 FPC(lv.AS),
FPC(lv.AN),
FPC(lv.AB),
FPC(lv.AT), pin, lv.ext, og,
951 color,
C3{G, G, G},
C3{lv.ext.x - G, lv.ext.y - G, lv.ext.z - G},
957 if (isL0 && bcApplyL0_)
960 FPC(lv.AN),
FPC(lv.AB),
FPC(lv.AT), pin, lv.ext, og,
G, color);
972 if (lv.cd.extent(0) != lv.n)
973 lv.cd =
CCField(
"vmg_cd", lv.n);
974 Kokkos::deep_copy(lv.cd, 0.0);
975 ChebCoeffs cb(lv.chebHi / chebEigRatio_, lv.chebHi);
976 for (
int k = 0; k < degree; ++k) {
978 if (isL0 && bcApplyL0_)
981 FPC(lv.AS),
FPC(lv.AN),
FPC(lv.AB),
FPC(lv.AT), pin, lv.ext,
G);
982 double alpha = 0.0, beta = 0.0;
983 cb.
next(k, alpha, beta);
992#ifdef PECLET_FLOW_MPI
1000 for (
int a = 0; a < 3; ++a)
1001 if (!bcMode_ || (bc_[2 * a] == 0 && bc_[2 * a + 1] == 0))
1008 for (
int a = 0; a < 3; ++a)
1009 for (
int s = 0; s < 2; ++s)
1010 if (bc_[2 * a + s] != 0 &&
touches(lv, 2 * a + s))
1018 for (
int a = 0; a < 3; ++a)
1019 for (
int s = 0; s < 2; ++s) {
1020 const int t = bc_[2 * a + s];
1021 if (t == 0 || !
touches(lv, 2 * a + s))
1029 int N3[3] = {lv.inner.x, lv.inner.y, lv.inner.z};
1030 int dims[3] = {e.x, e.y, e.z};
1031 long st[3] = {1, e.x, (long)e.x * e.y};
1032 const int a = axis, b = (axis + 1) % 3, c = (axis + 2) % 3;
1033 const long sa = st[a], sb = st[b], sc = st[c];
1034 const int N = N3[a];
1036 Kokkos::parallel_for(
1037 "peclet::flow::vmg_pfill",
MDRange2<CCExec>(space, {0, 0}, {dims[b], dims[c]}),
1038 KOKKOS_LAMBDA(
int p0,
int p1) {
1039 const long base = (long)p0 * sb + (
long)p1 * sc;
1040 for (
int gl = 0; gl <
G; ++gl) {
1041 ff(base + (
long)gl * sa) = ff(base + (
long)(gl +
N) * sa);
1042 ff(base + (
long)(
G +
N + gl) * sa) = ff(base + (
long)(
G + gl) * sa);
1048 std::vector<Level> lv_;
1049 double w_[3] = {1.0, 1.0, 1.0};
1050 double hp_[3] = {1.0, 1.0, 1.0};
1051 bool aniso_ =
false;
1052 double aspectTheta_ = 2.0;
1053 int pre_ = 2, post_ = 2, bottom_ = 8;
1054 bool chebSmooth_ =
false;
1055 int chebDegree_ = 0;
1056 double chebEigRatio_ = 10.0;
1057 bool usePin_ =
true,
1059 bool bcMode_ =
false;
1060 int bc_[6] = {0, 0, 0, 0, 0, 0};
1061 std::function<void(
CCField)> bcApplyL0_;
1063 bool distributed_ =
false;
1065 double lastResRatio_ = -1.0;
static C3 mgChooseRatio(const double H[3], const bool canA[3], bool aniso, double theta)
bool chebyshevSmoother() const
void setFineStencil(FPC AC, FPC AW, FPC AE, FPC AS, FPC AN, FPC AB, FPC AT)
void fillProlongBcGhosts(Level &lv)
void buildUpwindCoarse(int comp, double nu_dt, double idiag, double fouw)
void zeroBcGhosts(Level &lv, CCField f)
void setChebyshevSmoother(bool on, int degree=0, double eigRatio=10.0)
bool touches(const Level &lv, int f) const
double solveMax(double v, Comm comm) const
void setStaircaseBc(int comp, CCConst theta0, CCConst solid0, CCConst resmask0, double nu_dt, double idiag, double thresh, bool upwind=false, double fouw=0.0)
int solve(CCConst b, CCField x, int nvc, int pre, int post, int bottom, double tol=0.0, Comm comm=nullComm(), double resTol=0.0)
void fillAxis(Level &lv, CCField f, int axis)
double level0Residual(Comm comm)
void setBcApplyL0(std::function< void(CCField)> fn)
void setDomainBcOp(int comp, double nu_dt, double idiag)
void smooth(Level &lv, int sweeps, bool isL0)
void chebSmooth(Level &lv, int degree, bool isL0, CCConst pin)
void restrictAdvVelocities(CCConst u0, CCConst v0, CCConst w0)
void fill(Level &lv, CCField f)
void setAspectThreshold(double theta)
Anisotropic-coarsening aspect threshold theta (doc/anisotropic_metric.md §5.1).
void chebLevelBounds(Comm comm)
void setStaircase(CCConst theta0, CCConst solid0, CCConst resmask0, double nu_dt, double idiag, double thresh)
void setBC(const int bc[6])
void init(int nx, int ny, int nz, int nLevels)
double lastResidualRatio() const
std::vector< C3 > levelRatios() const
The per-level coarsening ratio actually chosen (the level table of §5; the two hierarchies share one ...
void setMetric(const double w[3], const double hp[3])
Per-axis metric of the physical domain (doc/anisotropic_metric.md §1.1): the weights w_a = 1/h_a'^2,...
flow — Chebyshev semi-iteration as a standalone momentum (implicit-diffusion) solver,...
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
flow — portable (Kokkos) IBM geometric fields + variable-coefficient RB-GS smoother.
void zeroPlane(CCField m, C3 e, int axis, int idx)
Kokkos::DefaultExecutionSpace CCExec
void residualVarPinMax(CCConst x, CCConst b, FPC AC, FPC AW, FPC AE, FPC AS, FPC AN, FPC AB, FPC AT, CCConst pin, C3 e, int g, int holdAxis, int holdIdx, double &rmax, double &bdmax, double &bmax)
void buildConstAniso(FPV AC, FPV AW, FPV AE, FPV AS, FPV AN, FPV AB, FPV AT, C3 e, double bx, double by, double bz, double idiag)
Kokkos::MDRangePolicy< Exec, Kokkos::Rank< 3, Kokkos::Iterate::Left, Kokkos::Iterate::Left > > MDRange3
3-D range, x (the first index) fastest (both the tile order and the order within a tile).
void buildVelocityStaircase(FPV AC, FPV AW, FPV AE, FPV AS, FPV AN, FPV AB, FPV AT, CCConst theta, C3 e, int g, double bx, double by, double bz, double thresh, double idiag)
void thresholdMask(CCField m, CCConst theta, double thresh)
void ibmChebUpdate(CCField x, CCField d, CCConst r, FPC AC, CCConst solidmask, C3 e, int g, double alpha, double beta)
One Chebyshev update on the Jacobi-preconditioned system: d <- beta*d + alpha*r/AC ; x += d.
void prolongAdd(CCField fine, CCConst coarse, C3 fext, C3 cext, int gf, int gc, C3 finner, C3 ratio)
Kokkos::View< const double *, CCMem > CCConst
void ibmStencilJacobiBounds(MC AC, MC AW, MC AE, MC AS, MC AN, MC AB, MC AT, CCConst solidmask, C3 e, int g, double &lo, double &hi)
Gershgorin bounds [lo, hi] on the spectrum of the Jacobi-preconditioned cut-cell Helmholtz diag(AC)^-...
void ccReduce3(const char *name, C3 lo, C3 hi, F f, R &&... reducers)
void residualVarPin(CCField r, CCConst x, CCConst b, FPC AC, FPC AW, FPC AE, FPC AS, FPC AN, FPC AB, FPC AT, CCConst pin, C3 e, int g)
void boundaryFold(FPV AC, C3 e, int g, int a, int s, double beta)
Kokkos::View< const MReal *, CCMem > FPC
void fillBcGhost(CCField x, C3 e, int g, int a, int s, int dirichlet)
double maxAbsInner(CCConst a, C3 e, int g)
Kokkos::View< double *, CCMem > CCField
void ccFor3(const char *name, C3 lo, C3 hi, F f)
Kokkos::View< MReal *, CCMem > FPV
void restrictAvg(CCField coarse, CCConst fine, C3 cext, C3 fext, int gc, int gf, C3 cinner, C3 ratio)
double maxAbsDiffInner(CCConst a, CCConst b, C3 e, int g)
void ibmRbgsStencilColorBox(CCField x, CCConst b, MC AC, MC AW, MC AE, MC AS, MC AN, MC AB, MC AT, CCConst solidmask, C3 ext, C3 og, int color, C3 rlo, C3 rhi, C3 slo, C3 shi)
void mulMask(CCField r, CCConst m)
void pinSolidRows(FPV AC, FPV AW, FPV AE, FPV AS, FPV AN, FPV AB, FPV AT, CCConst theta, C3 e, int g, double thresh)
void prolongMasked(CCField fine, CCConst coarse, CCConst mask, C3 fext, C3 cext, int g, C3 finner, C3 ratio, double eps)
void ibmRbgsStencilColor(CCField x, CCConst b, MC AC, MC AW, MC AE, MC AS, MC AN, MC AB, MC AT, CCConst solidmask, C3 ext, C3 og, int g, int color)
void buildAdvCoarse(FPV AC, FPV AW, FPV AE, FPV AS, FPV AN, FPV AB, FPV AT, CCConst U, CCConst V, CCConst W, int comp, C3 e, int g, double bx, double by, double bz, double fouw, double sx, double sy, double sz, double idiag)
Kokkos::MDRangePolicy< Exec, Kokkos::Rank< 2, Kokkos::Iterate::Left, Kokkos::Iterate::Left > > MDRange2
2-D range, first index fastest (both the tile order and the order within a tile).
void fou_operator_aniso(int comp, int x, int y, int z, A U, A V, A W, double dt, double sx, double sy, double sz, double &cC, double &cxm, double &cxp, double &cym, double &cyp, double &czm, double &czp)
flow — the multidimensional iteration policies: iteration order follows storage.
flow — portable (Kokkos) staggered MAC momentum advection (Koren TVD + FOU).
The Chebyshev coefficient recurrence (Golub-Varga; the form multigrid Chebyshev smoothers use).
void next(int it, double &alpha, double &beta)
Coefficients for iteration it (0-based). Advances the recurrence.
static constexpr double AC
static constexpr double U