11#ifndef PECLET_FLOW_MAC_STENCILS_HPP
12#define PECLET_FLOW_MAC_STENCILS_HPP
14#include <Kokkos_Core.hpp>
21using SExec = Kokkos::DefaultExecutionSpace;
22using SMem = SExec::memory_space;
23using SField = Kokkos::View<double*, SMem>;
24using SConst = Kokkos::View<const double*, SMem>;
30KOKKOS_INLINE_FUNCTION
long L3(
int x,
int y,
int z,
I3 e) {
31 return static_cast<long>(x) +
static_cast<long>(y) * e.x +
32 static_cast<long>(z) *
static_cast<long>(e.x) * e.y;
47 double bz,
double Ac,
int color,
SConst dcorr) {
49 const bool hasD = (dcorr.extent(0) != 0);
50 if constexpr (std::is_same_v<typename SExec::memory_space, Kokkos::HostSpace>) {
51 const int nyi = e.y - 2 * g, nzi = e.z - 2 * g;
53 "peclet::flow::diff", Kokkos::RangePolicy<SExec>(space, 0, (
long)nyi * nzi),
54 KOKKOS_LAMBDA(
long t) {
55 const int y = g + (int)(t % nyi), z = g + (int)(t / nyi);
56 const long sx = 1, sy = e.x, sz =
static_cast<long>(e.x) * e.y;
57 const int P = (color + og.x + og.y + y + og.z + z) & 1;
58 for (
int x = g + ((P ^ (g & 1)) & 1); x < e.x - g; x += 2) {
59 const long i =
L3(x, y, z, e);
63 const double b0 = bx, b1 = by, b2 = bz;
64 const double nE = c(i + sx), nW = c(i - sx), nN = c(i + sy), nS = c(i - sy),
65 nT = c(i + sz), nB = c(i - sz);
68 s = b0 * (nE + nW) + b1 * (nN + nS) + b2 * (nT + nB);
70 s = b0 * (nE + nW + nN + nS + nT + nB);
71 c(i) = (b(i) + s) / (Ac + (hasD ? dcorr(i) : 0.0));
78 "peclet::flow::diff", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
79 KOKKOS_LAMBDA(
int x,
int y,
int z) {
80 if ((((x + og.x) + (y + og.y) + (z + og.z)) & 1) != color)
82 const long i =
L3(x, y, z, e), sx = 1, sy = e.x, sz =
static_cast<long>(e.x) * e.y;
83 const double b0 = bx, b1 = by, b2 = bz;
84 const double nE = c(i + sx), nW = c(i - sx), nN = c(i + sy), nS = c(i - sy), nT = c(i + sz),
88 s = b0 * (nE + nW) + b1 * (nN + nS) + b2 * (nT + nB);
90 s = b0 * (nE + nW + nN + nS + nT + nB);
91 c(i) = (b(i) + s) / (Ac + (hasD ? dcorr(i) : 0.0));
97 double Ac,
int color,
SConst dcorr,
bool aniso) {
99 diffSmoothColorT<true>(c, b, e, og, g, bx, by, bz, Ac, color, dcorr);
101 diffSmoothColorT<false>(c, b, e, og, g, bx, by, bz, Ac, color, dcorr);
106 int color,
SConst dcorr) {
107 diffSmoothColorT<false>(c, b, e, og, g, beta, beta, beta, Ac, color, dcorr);
115 double bz,
double Ac,
SConst dcorr) {
117 const bool hasD = (dcorr.extent(0) != 0);
119 Kokkos::parallel_for(
120 "peclet::flow::diff_resid", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
121 KOKKOS_LAMBDA(
int x,
int y,
int z) {
122 const long i =
L3(x, y, z, e), sx = 1, sy = e.x, sz =
static_cast<long>(e.x) * e.y;
123 const double b0 = bx, b1 = by, b2 = bz;
124 const double nE = c(i + sx), nW = c(i - sx), nN = c(i + sy), nS = c(i - sy), nT = c(i + sz),
128 sum = b0 * (nE + nW) + b1 * (nN + nS) + b2 * (nT + nB);
130 sum = b0 * (nE + nW + nN + nS + nT + nB);
131 r(i) = b(i) - ((Ac + (hasD ? dcorr(i) : 0.0)) * c(i) - sum);
141 double Ac,
SConst dcorr,
int holdAxis,
int holdIdx,
double& rmax,
142 double& bdmax,
double& bmax) {
144 const bool hasD = (dcorr.extent(0) != 0);
146 Kokkos::parallel_reduce(
147 "peclet::flow::diff_resid_max", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
148 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& m,
double& d,
double& bm) {
149 const long i =
L3(x, y, z, e), sx = 1, sy = e.x, sz =
static_cast<long>(e.x) * e.y;
150 const double b0 = bx, b1 = by, b2 = bz;
151 const double nE = c(i + sx), nW = c(i - sx), nN = c(i + sy), nS = c(i - sy), nT = c(i + sz),
155 sum = b0 * (nE + nW) + b1 * (nN + nS) + b2 * (nT + nB);
157 sum = b0 * (nE + nW + nN + nS + nT + nB);
158 double r = b(i) - ((Ac + (hasD ? dcorr(i) : 0.0)) * c(i) - sum);
159 if (holdAxis >= 0 && (holdAxis == 0 ? x : (holdAxis == 1 ? y : z)) == holdIdx)
161 const double ar = Kokkos::fabs(r);
164 const double ad = Kokkos::fabs(b(i) - r);
167 const double ab = Kokkos::fabs(b(i));
171 Kokkos::Max<double>(rmax), Kokkos::Max<double>(bdmax), Kokkos::Max<double>(bmax));
174 double Ac,
SConst dcorr,
bool aniso,
int holdAxis,
int holdIdx,
175 double& rmax,
double& bdmax,
double& bmax) {
177 diffResidualMaxT<true>(c, b, e, g, bx, by, bz, Ac, dcorr, holdAxis, holdIdx, rmax, bdmax, bmax);
179 diffResidualMaxT<false>(c, b, e, g, bx, by, bz, Ac, dcorr, holdAxis, holdIdx, rmax, bdmax,
185 double Ac,
SConst dcorr,
bool aniso) {
202 double bz,
double Ac,
int color,
SConst dcorr) {
204 const bool hasD = (dcorr.extent(0) != 0);
206 if constexpr (std::is_same_v<typename SExec::memory_space, Kokkos::HostSpace>) {
207 const int nyi = e.y - 2 * g, nzi = e.z - 2 * g;
208 Kokkos::parallel_reduce(
209 "peclet::flow::diff_du", Kokkos::RangePolicy<SExec>(space, 0, (
long)nyi * nzi),
210 KOKKOS_LAMBDA(
long t,
double& m) {
211 const int y = g + (int)(t % nyi), z = g + (int)(t / nyi);
212 const long sx = 1, sy = e.x, sz =
static_cast<long>(e.x) * e.y;
213 const int P = (color + og.x + og.y + y + og.z + z) & 1;
214 for (
int x = g + ((P ^ (g & 1)) & 1); x < e.x - g; x += 2) {
215 const long i =
L3(x, y, z, e);
219 const double b0 = bx, b1 = by, b2 = bz;
220 const double nE = c(i + sx), nW = c(i - sx), nN = c(i + sy), nS = c(i - sy),
221 nT = c(i + sz), nB = c(i - sz);
224 s = b0 * (nE + nW) + b1 * (nN + nS) + b2 * (nT + nB);
226 s = b0 * (nE + nW + nN + nS + nT + nB);
227 const double cn = (b(i) + s) / (Ac + (hasD ? dcorr(i) : 0.0));
228 const double d = Kokkos::fabs(cn - c(i));
234 Kokkos::Max<double>(du));
238 Kokkos::parallel_reduce(
239 "peclet::flow::diff_du", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
240 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& m) {
241 if ((((x + og.x) + (y + og.y) + (z + og.z)) & 1) != color)
243 const long i =
L3(x, y, z, e), sx = 1, sy = e.x, sz =
static_cast<long>(e.x) * e.y;
244 const double b0 = bx, b1 = by, b2 = bz;
245 const double nE = c(i + sx), nW = c(i - sx), nN = c(i + sy), nS = c(i - sy), nT = c(i + sz),
249 s = b0 * (nE + nW) + b1 * (nN + nS) + b2 * (nT + nB);
251 s = b0 * (nE + nW + nN + nS + nT + nB);
252 const double cn = (b(i) + s) / (Ac + (hasD ? dcorr(i) : 0.0));
253 const double d = Kokkos::fabs(cn - c(i));
258 Kokkos::Max<double>(du));
264 double bz,
double Ac,
int color,
SConst dcorr,
bool aniso) {
265 return aniso ?
diffSmoothColorDuT<true>(c, b, e, og, g, bx, by, bz, Ac, color, dcorr)
266 :
diffSmoothColorDuT<false>(c, b, e, og, g, bx, by, bz, Ac, color, dcorr);
271 int color,
SConst dcorr) {
272 return diffSmoothColorDuT<false>(c, b, e, og, g, beta, beta, beta, Ac, color, dcorr);
279 Kokkos::parallel_for(
280 "peclet::flow::pois", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
281 KOKKOS_LAMBDA(
int x,
int y,
int z) {
282 if ((((x + og.x) + (y + og.y) + (z + og.z)) & 1) != color)
284 const long i =
L3(x, y, z, e), sx = 1, sy = e.x, sz =
static_cast<long>(e.x) * e.y;
286 phi(i + sx) + phi(i - sx) + phi(i + sy) + phi(i - sy) + phi(i + sz) + phi(i - sz);
287 phi(i) = (s - d(i)) / 6.0;
295 Kokkos::parallel_for(
296 "peclet::flow::diverg", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
297 KOKKOS_LAMBDA(
int x,
int y,
int z) {
298 const long i =
L3(x, y, z, e), sx = 1, sy = e.x, sz =
static_cast<long>(e.x) * e.y;
299 d(i) = (u(i + sx) - u(i)) + (v(i + sy) - v(i)) + (w(i + sz) - w(i));
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).
Kokkos::View< double *, SMem > SField
void diffResidualT(SField r, SConst c, SConst b, I3 e, int g, double bx, double by, double bz, double Ac, SConst dcorr)
long L3(int x, int y, int z, I3 e)
void divergence(SConst u, SConst v, SConst w, SField d, I3 e, int g)
void diffSmoothColorT(SField c, SConst b, I3 e, I3 og, int g, double bx, double by, double bz, double Ac, int color, SConst dcorr)
double diffSmoothColorDu(SField c, SConst b, I3 e, I3 og, int g, double bx, double by, double bz, double Ac, int color, SConst dcorr, bool aniso)
double diffSmoothColorDuT(SField c, SConst b, I3 e, I3 og, int g, double bx, double by, double bz, double Ac, int color, SConst dcorr)
void diffResidual(SField r, SConst c, SConst b, I3 e, int g, double bx, double by, double bz, double Ac, SConst dcorr, bool aniso)
Kokkos::DefaultExecutionSpace SExec
void poisSmoothColor(SField phi, SConst d, I3 e, I3 og, int g, int color)
Kokkos::View< const double *, SMem > SConst
void diffResidualMax(SConst c, SConst b, I3 e, int g, double bx, double by, double bz, double Ac, SConst dcorr, bool aniso, int holdAxis, int holdIdx, double &rmax, double &bdmax, double &bmax)
void diffSmoothColor(SField c, SConst b, I3 e, I3 og, int g, double bx, double by, double bz, double Ac, int color, SConst dcorr, bool aniso)
void diffResidualMaxT(SConst c, SConst b, I3 e, int g, double bx, double by, double bz, double Ac, SConst dcorr, int holdAxis, int holdIdx, double &rmax, double &bdmax, double &bmax)
void poisSweep(SField phi, SConst d, I3 e, I3 og, int g)
flow — the multidimensional iteration policies: iteration order follows storage.