9#ifndef PECLET_FLOW_MAC_CUTCELL_HPP
10#define PECLET_FLOW_MAC_CUTCELL_HPP
13#include <Kokkos_Core.hpp>
14#include <Kokkos_MathematicalFunctions.hpp>
22using CCExec = Kokkos::DefaultExecutionSpace;
23using CCMem = CCExec::memory_space;
24using CCField = Kokkos::View<double*, CCMem>;
25using CCConst = Kokkos::View<const double*, CCMem>;
59 double sym,
double szp,
double szm,
int type,
60 double dx,
double dy,
double dz) {
69 double denom = (type == 1) ? (Kokkos::fabs(ny) * dy + Kokkos::fabs(nz) * dz)
70 : (type == 2) ? (Kokkos::fabs(nx) * dx + Kokkos::fabs(nz) * dz)
71 : (Kokkos::fabs(nx) * dx + Kokkos::fabs(ny) * dy);
83 const double fx = Kokkos::floor(x),
fy = Kokkos::floor(y),
fz = Kokkos::floor(z);
85 int x0 = (int)
fx, y0 = (
int)
fy,
z0 = (int)
fz;
86 auto cl = [](
int v,
int n) {
return v < 0 ? 0 : (v >= n ? n - 1 : v); };
87 const int x1 =
cl(x0 + 1, ext.
x), y1 =
cl(y0 + 1, ext.
y),
z1 =
cl(
z0 + 1, ext.
z);
91 const long sy = ext.
x, sz =
static_cast<long>(ext.
x) * ext.
y;
92 auto F = [&](
int xx,
int yy,
int zz) {
93 return sdf(
static_cast<long>(
xx) +
static_cast<long>(
yy) * sy +
static_cast<long>(
zz) * sz);
100 return c0 * (1 -
wz) + c1 *
wz;
104 int type,
double dx,
double dy,
double dz) {
108 const double e = 1.0;
110 sd,
ccSampleExt(sdf, ext,
fx + e,
fy,
fz),
ccSampleExt(sdf, ext,
fx - e,
fy,
fz),
111 ccSampleExt(sdf, ext,
fx,
fy + e,
fz),
ccSampleExt(sdf, ext,
fx,
fy - e,
fz),
112 ccSampleExt(sdf, ext,
fx,
fy,
fz + e),
ccSampleExt(sdf, ext,
fx,
fy,
fz - e), type, dx, dy,
120 const bool pa = a >= 0.0,
pb = b >= 0.0,
pc = c >= 0.0;
121 const int np = (
pa ? 1 : 0) + (
pb ? 1 : 0) + (
pc ? 1 : 0);
141 const double den = (x - y) * (x - z);
142 return den > 1e-300 ? (x * x) / den : 1.0;
158 const double den = (x - y) * (x - z);
159 return 1.0 - (den > 1e-300 ? (x * x) / den : 1.0);
176 const double e = 0.5;
181 }
else if (type == 2) {
200 const double cc =
ccg;
205 if (
frac > 1.0 - 1e-12)
214 double dy,
double dz,
int order = 1) {
218 Kokkos::parallel_for(
219 "peclet::flow::cc_open_ms",
MD(
space, {0, 0, 0}, {ext.
x, ext.
y, ext.
z}),
221 const long i =
static_cast<long>(
lx) +
static_cast<long>(
ly) * ext.
x +
222 static_cast<long>(
lz) *
static_cast<long>(ext.
x) * ext.
y;
229 Kokkos::parallel_for(
230 "peclet::flow::cc_open",
MD(
space, {0, 0, 0}, {ext.
x, ext.
y, ext.
z}),
232 const long i =
static_cast<long>(
lx) +
static_cast<long>(
ly) * ext.
x +
233 static_cast<long>(
lz) *
static_cast<long>(ext.
x) * ext.
y;
249 double dy,
double dz,
int order = 1) {
251 const int dims[3] = {ext.
x, ext.
y, ext.
z};
252 const long st[3] = {1, (
long)ext.
x, (
long)ext.
x * ext.
y};
253 const int b = (a + 1) % 3, c = (a + 2) % 3;
254 const long sa = st[a],
sb = st[b], sc = st[c];
255 const int bf =
dims[a] - g;
256 const int type = a + 1;
258 Kokkos::parallel_for(
259 "peclet::flow::cc_open_high_face",
MD(
space, {0, 0}, {
dims[b],
dims[c]}),
266 oa(i) = (order >= 2) ?
ccFaceOpenMS(sdf, ext, q[0], q[1], q[2], type)
267 :
ccFaceOpen(sdf, ext, q[0], q[1], q[2], type, dx, dy, dz);
287 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>)
310#define PECLET_FLOW_OMP_SIMD _Pragma("omp simd")
312#define PECLET_FLOW_OMP_SIMD
315template <
bool Simd,
class F>
319 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
320 const int ny = hi.
y - lo.y, nz = hi.
z - lo.z;
321 if (ny <= 0 || nz <= 0 || hi.
x <= lo.x)
323 const long rows = (
long)ny * nz;
324 auto row = [=](
long r) {
325 const int y = lo.y + (int)(r % ny), z = lo.z + (int)(r / ny);
326 if constexpr (
Simd) {
328 for (
int x = lo.x; x < hi.
x; ++x)
331 for (
int x = lo.x; x < hi.
x; ++x)
336 for (
long r = 0; r < rows; ++r)
340 Kokkos::parallel_for(name, Kokkos::RangePolicy<CCExec>(
space, 0, rows), row);
342 Kokkos::parallel_for(name,
MD(
space, {lo.x, lo.y, lo.z}, {hi.
x, hi.
y, hi.
z}), f);
349 ccdetail::ccRows3<true>(name, lo, hi, f);
354 ccdetail::ccRows3<false>(name, lo, hi, f);
366template <
class R,
bool = Kokkos::is_reducer_v<std::decay_t<R>>>
368 using type =
typename std::decay_t<R>::value_type;
372 using D = std::decay_t<R>;
373 template <
class T,
bool = Kokkos::is_view_v<T>>
379 using type =
typename T::non_const_value_type;
384template <
class F,
class... R>
388 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
389 const int ny = hi.
y - lo.y > 0 ? hi.
y - lo.y : 0, nz = hi.
z - lo.z > 0 ? hi.
z - lo.z : 0;
390 const long rows = hi.
x > lo.x ? (
long)ny * nz : 0;
391 Kokkos::parallel_reduce(
392 name, Kokkos::RangePolicy<CCExec>(
space, 0, rows),
394 const int y = lo.y + (int)(r % ny), z = lo.z + (int)(r / ny);
395 for (
int x = lo.x; x < hi.
x; ++x)
400 Kokkos::parallel_reduce(name,
MD(
space, {lo.x, lo.y, lo.z}, {hi.
x, hi.
y, hi.
z}), f,
#define PECLET_FLOW_OMP_SIMD
void ccRows3(const char *name, C3 lo, C3 hi, F f)
void ccForRows3(const char *name, C3 lo, C3 hi, F f)
CCExec::memory_space CCMem
double ccFaceOpen(CCConst sdf, C3 ext, double fx, double fy, double fz, int type, double dx, double dy, double dz)
void ccReduce3(const char *name, C3 lo, C3 hi, F f, R &&... reducers)
constexpr long kHostSerialCellCutoff
double ccFaceOpenMS(CCConst sdf, C3 ext, double fx, double fy, double fz, int type)
double ccFractionCore(double sd, double sxp, double sxm, double syp, double sym, double szp, double szm, int type, double dx, double dy, double dz)
double ccTriFrac(double a, double b, double c)
void buildOpennessHighFace(CCField oa, CCConst sdf, C3 ext, int g, int a, double dx, double dy, double dz, int order=1)
void ccFor3(const char *name, C3 lo, C3 hi, F f)
Kokkos::View< double *, CCMem > CCField
Kokkos::DefaultExecutionSpace CCExec
double ccSampleExt(CCConst sdf, C3 ext, double x, double y, double z)
void buildOpenness(CCField ox, CCField oy, CCField oz, CCConst sdf, C3 ext, double dx, double dy, double dz, int order=1)
bool hostRunSerial(long cells)
Kokkos::View< const double *, CCMem > CCConst
flow — the multidimensional iteration policies: iteration order follows storage.
typename T::non_const_value_type type
typename Pick< D >::type type
typename std::decay_t< R >::value_type type
static constexpr double F