31#ifndef PECLET_PNM_PORE_KERNELS_HPP
32#define PECLET_PNM_PORE_KERNELS_HPP
38#include <Kokkos_Core.hpp>
51using Exec = Kokkos::DefaultExecutionSpace;
52using Mem = Exec::memory_space;
59inline void uploadVec(
const std::vector<T>& h,
const Kokkos::View<T*, Mem>& d) {
63 d, Kokkos::View<
const T*, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>(
70inline std::vector<typename V::value_type>
downloadN(
const V& d, std::size_t count) {
71 std::vector<typename V::value_type> out(count);
73 Kokkos::deep_copy(Kokkos::View<
typename V::value_type*, Kokkos::HostSpace,
74 Kokkos::MemoryTraits<Kokkos::Unmanaged>>(out.data(), count),
75 Kokkos::subview(d, Kokkos::make_pair(std::size_t(0), count)));
82 auto h = Kokkos::create_mirror_view(d);
83 Kokkos::deep_copy(h, d);
87KOKKOS_INLINE_FUNCTION
int get_idx(
int x,
int y,
int z,
I3 res) {
88 x = (x % res.x + res.x) % res.x;
89 y = (y % res.y + res.y) % res.y;
90 z = (z % res.z + res.z) % res.z;
91 return z * res.y * res.x + y * res.x + x;
94KOKKOS_INLINE_FUNCTION
int wrapc(
int a,
int n) {
96 return r < 0 ? r + n : r;
106 KOKKOS_INLINE_FUNCTION
Index cell(
int lx,
int ly,
int lz)
const {
109 KOKKOS_INLINE_FUNCTION
Index gid(
int lx,
int ly,
int lz)
const {
return cell(lx, ly, lz); }
110 KOKKOS_INLINE_FUNCTION
void gcoord(
int lx,
int ly,
int lz,
int& gx,
int& gy,
int& gz)
const {
115 KOKKOS_INLINE_FUNCTION
bool inOwned(
int,
int,
int)
const {
return true; }
116 KOKKOS_INLINE_FUNCTION
Index owned(
int ix,
int iy,
int iz)
const {
return cell(ix, iy, iz); }
119 iy = int((o /
nx) %
ny);
124 return cell(gx, gy, gz);
139 KOKKOS_INLINE_FUNCTION
Index lidx(
int lx,
int ly,
int lz)
const {
142 KOKKOS_INLINE_FUNCTION
Index cell(
int lx,
int ly,
int lz)
const {
return lidx(lx, ly, lz); }
144 KOKKOS_INLINE_FUNCTION
Index gidAt(
int lx,
int ly,
int lz)
const {
148 KOKKOS_INLINE_FUNCTION
Index gid(
int lx,
int ly,
int lz)
const {
return gidAt(lx, ly, lz); }
149 KOKKOS_INLINE_FUNCTION
void gcoord(
int lx,
int ly,
int lz,
int& gx,
int& gy,
int& gz)
const {
154 KOKKOS_INLINE_FUNCTION
bool inOwned(
int ix,
int iy,
int iz)
const {
155 return ix >= 0 && ix < nx && iy >= 0 && iy < ny && iz >= 0 && iz <
nz;
157 KOKKOS_INLINE_FUNCTION
Index owned(
int ix,
int iy,
int iz)
const {
162 iy = int((o /
nx) %
ny);
169 const int gx = int(
gid %
gnx);
171 const int gy = int(t %
gny), gz = int(t /
gny);
181 return lidx(lx, ly, lz);
187 KOKKOS_INLINE_FUNCTION
bool onSurface(
int ix,
int iy,
int iz)
const {
188 return !(ix > 0 && ix <
nx - 1 && iy > 0 && iy <
ny - 1 && iz > 0 && iz <
nz - 1);
196using MD3 = Kokkos::MDRangePolicy<Exec, Kokkos::Rank<3>>;
197using R1 = Kokkos::RangePolicy<Exec>;
202 return MD3(space, {0, 0, 0}, {geo.nx, geo.ny, geo.nz});
213template <
class Geo,
class SdfView>
214KOKKOS_INLINE_FUNCTION
bool isPeak(
const Geo& g,
const SdfView& sdf,
int lx,
int ly,
int lz,
215 float cv,
Index cgid) {
216 for (
int dz = -1; dz <= 1; ++dz)
217 for (
int dy = -1; dy <= 1; ++dy)
218 for (
int dx = -1; dx <= 1; ++dx) {
219 if (dx == 0 && dy == 0 && dz == 0)
221 const float nv = sdf(g.cell(lx + dx, ly + dy, lz + dz));
222 const Index ngid = g.gid(lx + dx, ly + dy, lz + dz);
223 if (nv > cv || (nv == cv && ngid > cgid))
231template <
class Geo,
class SdfView>
232KOKKOS_INLINE_FUNCTION
Pore poreAt(
const Geo& g,
const SdfView& sdf,
int lx,
int ly,
int lz,
233 float ox,
float oy,
float oz,
float sx,
float sy,
float sz) {
234 float sw = 0.0f, px = 0.0f, py = 0.0f, pz = 0.0f;
235 for (
int dz = -1; dz <= 1; ++dz)
236 for (
int dy = -1; dy <= 1; ++dy)
237 for (
int dx = -1; dx <= 1; ++dx) {
238 const float v = sdf(g.cell(lx + dx, ly + dy, lz + dz));
239 float w = v > 0.0f ? v : 0.0f;
246 float fx = 0, fy = 0, fz = 0;
253 g.gcoord(lx, ly, lz, gx, gy, gz);
254 return Pore{ox + (gx + fx) * sx, oy + (gy + fy) * sy, oz + (gz + fz) * sz,
255 sdf(g.cell(lx, ly, lz))};
261template <
class Geo,
class SdfView>
262KOKKOS_INLINE_FUNCTION
Index steepest(
const Geo& g,
const SdfView& sdf,
int lx,
int ly,
int lz,
263 Index cgid,
bool ascent) {
265 float bv = sdf(g.cell(lx, ly, lz));
266 for (
int dz = -1; dz <= 1; ++dz)
267 for (
int dy = -1; dy <= 1; ++dy)
268 for (
int dx = -1; dx <= 1; ++dx) {
269 if (dx == 0 && dy == 0 && dz == 0)
271 const Index ni = g.gid(lx + dx, ly + dy, lz + dz);
272 const float nv = sdf(g.cell(lx + dx, ly + dy, lz + dz));
277 }
else if (nv == bv && ni > best)
283 }
else if (nv == bv && ni > best)
291template <
class Geo,
class FieldView>
292KOKKOS_INLINE_FUNCTION
double trilinear(
const Geo& g,
const FieldView& p,
const double gp[3]) {
295 for (
int a = 0; a < 3; ++a) {
296 const double fl = Kokkos::floor(gp[a]);
297 b[a] =
static_cast<int>(fl);
301 for (
int dz = 0; dz < 2; ++dz)
302 for (
int dy = 0; dy < 2; ++dy)
303 for (
int dx = 0; dx < 2; ++dx) {
305 (dx ? f[0] : 1.0 - f[0]) * (dy ? f[1] : 1.0 - f[1]) * (dz ? f[2] : 1.0 - f[2]);
306 acc += wt * p(g.cellAtGlobal(b[0] + dx, b[1] + dy, b[2] + dz));
315KOKKOS_INLINE_FUNCTION
bool facesAdjacent(
int dx,
int dy,
int dz,
int d,
int d2) {
316 const int Dx = 2 * dx + (d2 == 0) - (d == 0);
317 const int Dy = 2 * dy + (d2 == 1) - (d == 1);
318 const int Dz = 2 * dz + (d2 == 2) - (d == 2);
319 return Dx * Dx + Dy * Dy + Dz * Dz <= 8;
323template <
class KeyView>
325 typename KeyView::non_const_value_type key) {
326 Index lo = 0, hi = nkeys - 1;
328 const Index mid = lo + (hi - lo) / 2;
329 const auto k = keys(mid);
352 return d == 0 ?
v0(e) : (d == 1 ?
v1(e) :
v2(e));
362template <
class Geo,
class SdfView,
class PoreView,
class CountView>
364 std::array<float, 3> origin, std::array<float, 3> spacing,
365 const PoreView& pores,
const CountView& counter,
int maxPores) {
367 const float ox = origin[0], oy = origin[1], oz = origin[2];
368 const float sx = spacing[0], sy = spacing[1], sz = spacing[2];
369 Kokkos::parallel_for(
370 "pnm::extract_pores",
ownedRange(space, g), KOKKOS_LAMBDA(
int ix,
int iy,
int iz) {
371 const int lx = ix + g.g, ly = iy + g.g, lz = iz + g.g;
372 const float cv = sdf(g.cell(lx, ly, lz));
375 if (!
isPeak(g, sdf, lx, ly, lz, cv, g.gid(lx, ly, lz)))
377 const int slot = Kokkos::atomic_fetch_add(&counter(), 1);
379 pores(slot) =
poreAt(g, sdf, lx, ly, lz, ox, oy, oz, sx, sy, sz);
389template <
class Geo,
class SdfView,
class ParentView>
390inline void initMarkers(
const Exec& space,
const Geo& geo,
const SdfView& sdf,
float thr,
391 const ParentView& parent) {
392 using L =
typename ParentView::non_const_value_type;
394 Kokkos::parallel_for(
395 "pnm::init_markers",
ownedRange(space, g), KOKKOS_LAMBDA(
int ix,
int iy,
int iz) {
396 const Index o = g.owned(ix, iy, iz);
397 parent(o) = (sdf(g.cell(ix + g.g, iy + g.g, iz + g.g)) < thr) ? L(o) : L(-1);
403template <
class ParentView>
404inline void cclFlatten(
const Exec& space,
const ParentView& parent, std::size_t n) {
405 using L =
typename ParentView::non_const_value_type;
406 Kokkos::parallel_for(
407 "pnm::flatten",
R1(space, 0, n), KOKKOS_LAMBDA(std::size_t idx) {
410 while (l != parent(l))
421template <
class Geo,
class ParentView,
class FlagView>
423 const FlagView& changed) {
424 using L =
typename ParentView::non_const_value_type;
426 Kokkos::parallel_for(
427 "pnm::merge_markers",
ownedRange(space, g), KOKKOS_LAMBDA(
int ix,
int iy,
int iz) {
428 const Index o = g.owned(ix, iy, iz);
429 const L my = parent(o);
432 const int dz_l[13] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
433 const int dy_l[13] = {-1, -1, -1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0};
434 const int dx_l[13] = {-1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, 1};
435 for (
int k = 0; k < 13; ++k) {
436 const int jx = ix + dx_l[k], jy = iy + dy_l[k], jz = iz + dz_l[k];
437 if (!g.inOwned(jx, jy, jz))
439 const L nl = parent(g.owned(jx, jy, jz));
440 if (nl != -1 && my != nl) {
442 while (rm != parent(rm))
445 while (rn != parent(rn))
448 const L small = rm < rn ? rm : rn, large = rm < rn ? rn : rm;
449 Kokkos::atomic_min(&parent(large), small);
460template <
class ParentView,
class FlagView,
class MergeFn>
462 const FlagView& changed,
const MergeFn& merge) {
465 Kokkos::deep_copy(changed, 0);
475template <
class Geo,
class SdfView,
class LabelView,
class FlagView>
477 const LabelView& labels,
const LabelView& labelsN,
const FlagView& changed) {
478 using L =
typename LabelView::non_const_value_type;
480 Kokkos::parallel_for(
481 "pnm::flood",
ownedRange(space, g), KOKKOS_LAMBDA(
int ix,
int iy,
int iz) {
482 const int lx = ix + g.g, ly = iy + g.g, lz = iz + g.g;
483 const Index e = g.cell(lx, ly, lz);
489 for (
int dz = -1; dz <= 1; ++dz)
490 for (
int dy = -1; dy <= 1; ++dy)
491 for (
int dx = -1; dx <= 1; ++dx) {
492 if (dx == 0 && dy == 0 && dz == 0)
494 const L nl = labels(g.cell(lx + dx, ly + dy, lz + dz));
495 if (nl != -1 && (best == -1 || nl < best))
512template <
class Geo,
class SdfView,
class LabelView,
class FlagView,
class SyncFn>
513inline void floodFixpoint(
const Exec& space,
const Geo& geo,
const SdfView& sdf, LabelView& labels,
514 LabelView& labelsN,
const FlagView& changed,
const SyncFn& sync) {
517 Kokkos::deep_copy(changed, 0);
518 Kokkos::deep_copy(labelsN, labels);
519 floodSweep(space, geo, sdf, labels, labelsN, changed);
520 std::swap(labels, labelsN);
534template <
class SdfView,
class EmitFn>
536 const EmitFn& emit) {
538 Kokkos::parallel_for(
539 "pnm::gradient_path",
R1(space, 0, g.numOwned()), KOKKOS_LAMBDA(std::size_t i0) {
541 const bool ascent = ascentAll || (sdf(ci) > 0.0f);
543 const int MAX_STEPS = 512;
544 for (
int s = 0; s < MAX_STEPS; ++s) {
546 g.ownedCoords(walker, wx, wy, wz);
547 const Index best =
steepest(g, sdf, wx, wy, wz, walker, ascent);
561template <
class Geo,
class SdfView,
class TargetView,
class ChaseFn,
class FinalFn,
class PeakFn>
563 const TargetView& target,
const ChaseFn& chase,
const FinalFn& finalOf,
564 const PeakFn& peakOf) {
566 Kokkos::parallel_for(
567 "pnm::grad_step",
ownedRange(space, g), KOKKOS_LAMBDA(
int ix,
int iy,
int iz) {
568 const int lx = ix + g.g, ly = iy + g.g, lz = iz + g.g;
569 const Index ci = g.cell(lx, ly, lz);
570 const Index cgid = g.gid(lx, ly, lz);
572 target(ci) = finalOf(ci, cgid);
576 target(ci) = (best == cgid) ? peakOf(ci, cgid) : best;
585template <
class Geo,
class TargetView>
589 Kokkos::parallel_reduce(
590 "pnm::resolve_roots",
R1(space, 0, g.numOwned()),
591 KOKKOS_LAMBDA(std::size_t o,
int& pend) {
593 g.ownedCoords(
Index(o), ix, iy, iz);
594 const Index ci = g.cell(ix + g.g, iy + g.g, iz + g.g);
595 Index t = target(ci);
598 for (
int s = 0; s < 64; ++s) {
599 const Index tl = g.localOf(t);
602 const Index tv = target(tl);
607 const Index nl = g.localOf(tv);
629template <
class Geo,
class SegView,
class PairView,
class CountView>
631 const PairView& pairs,
const CountView& cnt,
Index maxPairs) {
633 Kokkos::parallel_for(
634 "pnm::boundary_pairs",
ownedRange(space, g), KOKKOS_LAMBDA(
int ix,
int iy,
int iz) {
635 const int lx = ix + g.g, ly = iy + g.g, lz = iz + g.g;
636 const int my = seg(g.cell(lx, ly, lz));
637 const int dx_l[3] = {1, 0, 0}, dy_l[3] = {0, 1, 0}, dz_l[3] = {0, 0, 1};
638 for (
int k = 0; k < 3; ++k) {
639 const int nl = seg(g.cell(lx + dx_l[k], ly + dy_l[k], lz + dz_l[k]));
641 const int slot = Kokkos::atomic_fetch_add(&cnt(), 1);
642 if (slot < maxPairs) {
643 pairs(2 * slot) = my < nl ? my : nl;
644 pairs(2 * slot + 1) = my < nl ? nl : my;
653template <
class PairView>
654inline std::vector<std::pair<int, int>>
uniquePairs(
const PairView& pairs, std::size_t count) {
655 std::vector<int> flat =
downloadN(pairs, 2 * count);
656 std::vector<std::pair<int, int>> result(count);
657 for (std::size_t i = 0; i < count; ++i)
658 result[i] = {flat[2 * i], flat[2 * i + 1]};
659 std::sort(result.begin(), result.end());
660 result.erase(std::unique(result.begin(), result.end()), result.end());
665template <
class Geo,
class SegView>
666inline int maxLabel(
const Exec& space,
const Geo& geo,
const SegView& seg) {
669 Kokkos::parallel_reduce(
670 "pnm::max_label",
R1(space, 0, g.numOwned()),
671 KOKKOS_LAMBDA(std::size_t o,
int& m) {
673 g.ownedCoords(
Index(o), ix, iy, iz);
674 const int sv = seg(g.cell(ix + g.g, iy + g.g, iz + g.g));
678 Kokkos::Max<int>(np));
692template <
class Geo,
class LabView,
class SdfView,
class OpenView,
class CoreView,
class ParentView,
694inline void faceInit(
const Exec& space,
const Geo& geo,
const LabView& flowLab,
const SdfView& sdf,
695 const OpenView& ox,
const OpenView& oy,
const OpenView& oz,
bool hasOpen,
696 const CoreView& core,
const ParentView& parent,
const PairView& fpair) {
697 using L =
typename ParentView::non_const_value_type;
699 const bool ho = hasOpen;
700 Kokkos::parallel_for(
701 "pnm::face_init",
ownedRange(space, g), KOKKOS_LAMBDA(
int ix,
int iy,
int iz) {
702 const int lx = ix + g.g, ly = iy + g.g, lz = iz + g.g;
703 const Index o = g.owned(ix, iy, iz);
704 const Index ce = g.cell(lx, ly, lz);
705 const int a = flowLab(ce);
706 for (
int d = 0; d < 3; ++d) {
707 const Index f = 3 * o + d;
708 const Index nb = g.cell(lx + (d == 0), ly + (d == 1), lz + (d == 2));
709 const int b = flowLab(nb);
710 const double opn = ho ? (d == 0 ? ox(nb) : (d == 1 ? oy(nb) : oz(nb))) : 1.0;
711 const bool itf = (a != b && a > 0 && b > 0 && opn > 0.0);
712 core(f) = (itf && sdf(ce) > 0.0f && sdf(nb) > 0.0f) ? 1 : 0;
713 parent(f) = core(f) ? L(f) : L(-1);
714 fpair(f) = itf ? ((
Index(a < b ? a : b) << 32) | (a < b ? b : a)) :
Index(-1);
722template <
class Geo,
class ParentView,
class PairView,
class FlagView>
723inline void faceMerge(
const Exec& space,
const Geo& geo,
const ParentView& parent,
724 const PairView& fpair,
const FlagView& changed) {
725 using L =
typename ParentView::non_const_value_type;
727 Kokkos::parallel_for(
728 "pnm::face_merge",
ownedRange(space, g), KOKKOS_LAMBDA(
int ix,
int iy,
int iz) {
729 const Index o = g.owned(ix, iy, iz);
730 for (
int d = 0; d < 3; ++d) {
731 const Index f = 3 * o + d;
734 const Index pk = fpair(f);
735 for (
int dz = -1; dz <= 1; ++dz)
736 for (
int dy = -1; dy <= 1; ++dy)
737 for (
int dx = -1; dx <= 1; ++dx) {
738 const int jx = ix + dx, jy = iy + dy, jz = iz + dz;
739 if (!g.inOwned(jx, jy, jz))
741 const Index o2 = g.owned(jx, jy, jz);
742 for (
int d2 = 0; d2 < 3; ++d2) {
743 const Index f2 = 3 * o2 + d2;
744 if (f2 == f || parent(f2) < 0 || fpair(f2) != pk)
749 while (rm != parent(rm))
752 while (rn != parent(rn))
755 const L sml = rm < rn ? rm : rn, lrg = rm < rn ? rn : rm;
756 Kokkos::atomic_min(&parent(lrg), sml);
768template <
class Geo,
class ParentView,
class MinView>
769inline void faceMinGid(
const Exec& space,
const Geo& geo,
const ParentView& parent,
772 Kokkos::parallel_for(
773 "pnm::face_mingid",
ownedRange(space, g), KOKKOS_LAMBDA(
int ix,
int iy,
int iz) {
774 const Index o = g.owned(ix, iy, iz);
775 const Index gid = g.gid(ix + g.g, iy + g.g, iz + g.g);
776 for (
int d = 0; d < 3; ++d)
777 if (parent(3 * o + d) >= 0)
778 Kokkos::atomic_min(&mg(parent(3 * o + d)), 3 * gid + d);
786template <
class ParentView,
class PairView,
class LabView,
class RootFn>
788 const PairView& fpair,
const LabView& faceLab,
const RootFn& rootOf) {
789 Kokkos::parallel_for(
790 "pnm::face_label",
R1(space, 0, nf), KOKKOS_LAMBDA(std::size_t f) {
791 faceLab(f) = parent(f) >= 0 ? rootOf(parent(f)) : (fpair(f) >= 0 ?
kSent :
Index(-1));
797template <
class ParentView,
class LabView,
class RootFn>
799 const LabView& faceLab,
const RootFn& rootOf) {
800 Kokkos::parallel_for(
801 "pnm::leftover_label",
R1(space, 0, nf), KOKKOS_LAMBDA(std::size_t f) {
802 if (faceLab(f) ==
kSent)
803 faceLab(f) = rootOf(parent(f));
809template <
class ParentView,
class LabView>
811 const ParentView& parent) {
812 using L =
typename ParentView::non_const_value_type;
813 Kokkos::parallel_for(
814 "pnm::leftover_init",
R1(space, 0, nf),
815 KOKKOS_LAMBDA(std::size_t f) { parent(f) = (faceLab(f) ==
kSent) ? L(f) : L(-1); });
823template <
class Geo,
class LabView,
class PairView,
class CoreView,
class NbLab,
class NbPair,
826 const PairView& fpair,
const CoreView& core,
const NbLab& nbLab,
827 const NbPair& nbPair,
const FlagView& changed) {
829 Kokkos::parallel_for(
830 "pnm::film_attach",
ownedRange(space, g), KOKKOS_LAMBDA(
int ix,
int iy,
int iz) {
831 const Index o = g.owned(ix, iy, iz);
832 for (
int d = 0; d < 3; ++d) {
833 const Index f = 3 * o + d;
834 if (core(f) || faceLab(f) < 0)
836 Index best = faceLab(f);
837 const Index pk = fpair(f);
838 for (
int dz = -1; dz <= 1; ++dz)
839 for (
int dy = -1; dy <= 1; ++dy)
840 for (
int dx = -1; dx <= 1; ++dx) {
841 const Index e2 = g.cell(ix + g.g + dx, iy + g.g + dy, iz + g.g + dz);
842 for (
int d2 = 0; d2 < 3; ++d2) {
843 const Index l2 = nbLab(e2, d2);
844 if (l2 < 0 || l2 >=
kSent || l2 >= best)
846 if (nbPair(e2, d2) != pk)
853 if (best < faceLab(f)) {
870 std::sort(rootPair.begin(), rootPair.end(),
871 [](
const std::pair<Index, Index>& x,
const std::pair<Index, Index>& y) {
872 return x.second != y.second ? x.second < y.second : x.first < y.first;
875 std::vector<Index> rootF;
876 for (
const auto& rp : rootPair) {
877 ts.
throats.push_back({int(rp.second >> 32), int(rp.second & 0x7fffffff)});
878 rootF.push_back(rp.first);
882 ts.
slotOf.resize(rootF.size());
883 for (std::size_t t = 0; t < rootF.size(); ++t) {
901template <
class Geo,
class FieldView>
902KOKKOS_INLINE_FUNCTION
void faceVelocity(
const Geo& g,
const FieldView& u,
const FieldView& v,
903 const FieldView& w,
int lx,
int ly,
int lz,
int d,
904 double un,
double uf[3]) {
905 for (
int d2 = 0; d2 < 3; ++d2) {
910 const FieldView& f = d2 == 0 ? u : (d2 == 1 ? v : w);
911 const int ex = (d2 == 0), ey = (d2 == 1), ez = (d2 == 2);
912 const int nx = lx + (d == 0), ny = ly + (d == 1), nz = lz + (d == 2);
913 uf[d2] = 0.25 * ((f(g.cell(lx, ly, lz)) + f(g.cell(lx + ex, ly + ey, lz + ez))) +
914 (f(g.cell(nx, ny, nz)) + f(g.cell(nx + ex, ny + ey, nz + ez))));
929template <
class Geo,
class LabView,
class FieldView,
class OpenView,
class FaceLabView,
930 class KeyView,
class SlotView,
class AncView,
class AccView>
932 const FieldView& u,
const FieldView& v,
const FieldView& w,
933 const FieldView& p,
const OpenView& ox,
const OpenView& oy,
934 const OpenView& oz,
bool hasOpen,
const FaceLabView& faceLab,
935 const KeyView& keyD,
const SlotView& slotD, std::size_t nt,
936 const AncView& ancX,
const AncView& ancY,
const AncView& ancZ,
937 std::array<float, 3> origin, std::array<float, 3> spacing,
938 std::array<int, 3> gdims,
const AccView& Q,
const AccView& A,
939 const AccView& Cx,
const AccView& Cy,
const AccView& Cz,
940 const AccView& resid,
const AccView& ext) {
942 const double Ax = double(spacing[1]) * spacing[2], Ay = double(spacing[0]) * spacing[2],
943 Az = double(spacing[0]) * spacing[1];
944 const std::int64_t ntl =
static_cast<std::int64_t
>(nt);
945 const bool ho = hasOpen;
946 const float oxo = origin[0], oyo = origin[1], ozo = origin[2];
947 const float sx = spacing[0], sy = spacing[1], sz = spacing[2];
948 const double Lx = double(gdims[0]) * sx, Ly = double(gdims[1]) * sy, Lz = double(gdims[2]) * sz;
949 Kokkos::parallel_for(
950 "pnm::throat_flux",
ownedRange(space, g), KOKKOS_LAMBDA(
int ix,
int iy,
int iz) {
951 const int lx = ix + g.g, ly = iy + g.g, lz = iz + g.g;
952 const Index o = g.owned(ix, iy, iz);
953 const int a = flowLab(g.cell(lx, ly, lz));
954 for (
int d = 0; d < 3; ++d) {
955 const Index nb = g.cell(lx + (d == 0), ly + (d == 1), lz + (d == 2));
956 const int b = flowLab(nb);
960 const double vel = d == 0 ? u(nb) : (d == 1 ? v(nb) : w(nb));
961 const double opn = ho ? (d == 0 ? ox(nb) : (d == 1 ? oy(nb) : oz(nb))) : 1.0;
962 const double area = d == 0 ? Ax : (d == 1 ? Ay : Az);
963 const double q = opn * vel * area;
965 Kokkos::atomic_add(&resid(a - 1), q);
967 Kokkos::atomic_add(&resid(b - 1), -q);
968 const Index rt = faceLab(3 * o + d);
971 const int slot = k >= 0 ? slotD(k) : -1;
972 const int lo = a < b ? a : b;
974 const double w0 = opn * area;
975 Kokkos::atomic_add(&Q(slot), a < b ? q : -q);
976 Kokkos::atomic_add(&A(slot), w0);
979 g.gcoord(lx, ly, lz, gx, gy, gz);
980 double fp[3] = {oxo + (gx + (d == 0 ? 0.5 : 0.0)) *
double(sx),
981 oyo + (gy + (d == 1 ? 0.5 : 0.0)) *
double(sy),
982 ozo + (gz + (d == 2 ? 0.5 : 0.0)) *
double(sz)};
983 const double pc[3] = {ancX(lo - 1), ancY(lo - 1), ancZ(lo - 1)};
984 const double Lw[3] = {Lx, Ly, Lz};
985 for (
int a2 = 0; a2 < 3; ++a2) {
986 double dv = fp[a2] - pc[a2];
987 dv -= Lw[a2] * Kokkos::round(dv / Lw[a2]);
990 Kokkos::atomic_add(&Cx(slot), w0 * fp[0]);
991 Kokkos::atomic_add(&Cy(slot), w0 * fp[1]);
992 Kokkos::atomic_add(&Cz(slot), w0 * fp[2]);
994 const double sgn = a < b ? 1.0 : -1.0;
995 const double un = sgn * vel;
998 const double wun = w0 * un;
999 for (
int a2 = 0; a2 < 3; ++a2)
1000 Kokkos::atomic_add(&ext(a2 * ntl + slot), wun * uf[a2]);
1001 const double ke = 0.5 * (uf[0] * uf[0] + uf[1] * uf[1] + uf[2] * uf[2]);
1002 Kokkos::atomic_add(&ext(3 * ntl + slot), wun * ke);
1003 Kokkos::atomic_add(&ext((4 + d) * ntl + slot), w0 * sgn);
1004 const double pf = 0.5 * (p(g.cell(lx, ly, lz)) + p(nb));
1005 Kokkos::atomic_add(&ext(7 * ntl + slot), wun * pf);
1024template <
class Geo,
class SegView,
class SdfView,
class FieldView,
class PkView,
class AccView>
1025inline void poreVolume(
const Exec& space,
const Geo& geo,
const SegView& seg,
const SdfView& sdf,
1026 const FieldView& u,
const FieldView& v,
const FieldView& w,
1027 const FieldView& p,
const PkView& pkX,
const PkView& pkY,
const PkView& pkZ,
1028 int np, std::array<int, 3> gdims,
const AccView& acc) {
1030 const std::int64_t npl = np;
1031 const int gnx = gdims[0], gny = gdims[1], gnz = gdims[2];
1032 Kokkos::parallel_for(
1033 "pnm::pore_volume",
ownedRange(space, g), KOKKOS_LAMBDA(
int ix,
int iy,
int iz) {
1034 const int lx = ix + g.g, ly = iy + g.g, lz = iz + g.g;
1035 const Index ce = g.cell(lx, ly, lz);
1036 const int k = seg(ce) - 1;
1037 if (!(sdf(ce) > 0.0f) || k < 0)
1039 const double uc[3] = {0.5 * (u(ce) + u(g.cell(lx + 1, ly, lz))),
1040 0.5 * (v(ce) + v(g.cell(lx, ly + 1, lz))),
1041 0.5 * (w(ce) + w(g.cell(lx, ly, lz + 1)))};
1043 g.gcoord(lx, ly, lz, gx, gy, gz);
1044 const int off[3] = {
wrapc(gx - pkX(k) + gnx / 2, gnx) - gnx / 2,
1045 wrapc(gy - pkY(k) + gny / 2, gny) - gny / 2,
1046 wrapc(gz - pkZ(k) + gnz / 2, gnz) - gnz / 2};
1047 Kokkos::atomic_add(&acc(k), 1.0);
1048 Kokkos::atomic_add(&acc(npl + k),
double(p(ce)));
1049 Kokkos::atomic_add(&acc(2 * npl + k),
1050 0.5 * (uc[0] * uc[0] + uc[1] * uc[1] + uc[2] * uc[2]));
1051 for (
int a = 0; a < 3; ++a) {
1052 Kokkos::atomic_add(&acc((3 + a) * npl + k), uc[a]);
1053 Kokkos::atomic_add(&acc((6 + a) * npl + k),
double(off[a]));
1069 const std::vector<double>& ext,
1070 const std::vector<double>& throatArea,
1071 const std::vector<double>& Cx,
const std::vector<double>& Cy,
1072 const std::vector<double>& Cz,
const std::vector<double>& ancX,
1073 const std::vector<double>& ancY,
const std::vector<double>& ancZ) {
1074 const std::size_t nt = throats.size();
1081 for (std::size_t t = 0; t < nt; ++t) {
1082 const int lo = throats[t].first - 1;
1083 const double aw = throatArea[t];
1084 const double c[3] = {ancX[lo] + Cx[t] / aw, ancY[lo] + Cy[t] / aw, ancZ[lo] + Cz[t] / aw};
1085 for (
int a = 0; a < 3; ++a) {
1087 te.
normal_zyx[3 * t + 2 - a] = ext[(4 + a) * nt + t] / aw;
1105 std::array<float, 3> spacing,
1106 const std::vector<double>& ancX,
1107 const std::vector<double>& ancY,
1108 const std::vector<double>& ancZ) {
1109 const std::size_t n = std::size_t(np);
1119 const double cellVol = double(spacing[0]) * double(spacing[1]) * double(spacing[2]);
1120 for (std::size_t k = 0; k < n; ++k) {
1121 const double cnt = acc[k];
1122 ps.
volume[k] = cnt * cellVol;
1125 const double anc[3] = {ancX[k], ancY[k], ancZ[k]};
1127 for (
int a = 0; a < 3; ++a) {
1129 c[a] = anc[a] + (acc[(6 + a) * n + k] / cnt) *
double(spacing[a]);
1147 const std::vector<std::pair<int, int>>& throats,
const std::vector<double>& posX,
1148 const std::vector<double>& posY,
const std::vector<double>& posZ,
1149 const std::vector<double>& porePressure,
const std::vector<double>& throatArea,
1150 const std::vector<double>& Cx,
const std::vector<double>& Cy,
const std::vector<double>& Cz,
1151 const std::vector<double>& ancX,
const std::vector<double>& ancY,
1152 const std::vector<double>& ancZ, std::array<float, 3> spacing, std::array<int, 3> gdims,
1153 std::array<double, 3> grad_p) {
1154 const std::size_t nt = throats.size();
1155 std::vector<double> dp(nt);
1156 const double L[3] = {double(gdims[0]) * spacing[0], double(gdims[1]) * spacing[1],
1157 double(gdims[2]) * spacing[2]};
1158 for (std::size_t t = 0; t < nt; ++t) {
1159 const int li = throats[t].first, lj = throats[t].second;
1160 const double aw = throatArea[t];
1161 const double anc[3] = {ancX[li - 1], ancY[li - 1], ancZ[li - 1]};
1162 const double ancj[3] = {ancX[lj - 1], ancY[lj - 1], ancZ[lj - 1]};
1164 const double pip[3] = {posX[li - 1], posY[li - 1], posZ[li - 1]};
1165 const double pjp[3] = {posX[lj - 1], posY[lj - 1], posZ[lj - 1]};
1166 for (
int a = 0; a < 3; ++a) {
1167 const double ct = (Cx[t] * (a == 0) + Cy[t] * (a == 1) + Cz[t] * (a == 2)) / aw;
1168 const long long Dc = llround((ancj[a] - anc[a]) /
double(spacing[a])) -
1169 llround(ct /
double(spacing[a]) + 1e-6);
1170 const long long k = llround(
double(Dc) / gdims[a]);
1171 macro += grad_p[a] * ((pjp[a] - pip[a]) - L[a] * double(k));
1173 dp[t] = (porePressure[li - 1] - porePressure[lj - 1]) - macro;
1180 const std::vector<std::pair<int, int>>& throats,
const std::vector<Pore>& pores,
1181 const std::vector<double>& porePressure,
const std::vector<double>& throatArea,
1182 const std::vector<double>& Cx,
const std::vector<double>& Cy,
const std::vector<double>& Cz,
1183 const std::vector<double>& ancX,
const std::vector<double>& ancY,
1184 const std::vector<double>& ancZ, std::array<float, 3> spacing, std::array<int, 3> gdims,
1185 std::array<double, 3> grad_p) {
1186 std::vector<double> px(pores.size()), py(pores.size()), pz(pores.size());
1187 for (std::size_t k = 0; k < pores.size(); ++k) {
1188 px[k] = double(pores[k].x);
1189 py[k] = double(pores[k].y);
1190 pz[k] = double(pores[k].z);
1192 return throatPressureDrops(throats, px, py, pz, porePressure, throatArea, Cx, Cy, Cz, ancX, ancY,
1193 ancZ, spacing, gdims, grad_p);
void forestInit(const Exec &space, const Geo &geo, const SdfView &sdf, const TargetView &target, const ChaseFn &chase, const FinalFn &finalOf, const PeakFn &peakOf)
Steepest-neighbour forest for the hold-at-ghost root resolution.
Kokkos::RangePolicy< Exec > R1
void poreVolume(const Exec &space, const Geo &geo, const SegView &seg, const SdfView &sdf, const FieldView &u, const FieldView &v, const FieldView &w, const FieldView &p, const PkView &pkX, const PkView &pkY, const PkView &pkZ, int np, std::array< int, 3 > gdims, const AccView &acc)
Per-pore volume sums over the owned FLUID cells (sdf > 0) of each watershed segment (seg,...
void floodSweep(const Exec &space, const Geo &geo, const SdfView &sdf, const LabelView &labels, const LabelView &labelsN, const FlagView &changed)
One Jacobi flood sweep: every unlabelled solid voxel takes the smallest labelled 26-neighbour (reads ...
Index steepest(const Geo &g, const SdfView &sdf, int lx, int ly, int lz, Index cgid, bool ascent)
Steepest 26-neighbour under the (sdf, gid) lexicographic order: ascent (pores) or descent (solids); r...
void boundaryPairs(const Exec &space, const Geo &geo, const SegView &seg, const PairView &pairs, const CountView &cnt, Index maxPairs)
Boundary pairs: for every owned cell, the (min,max) label pairs across its +x/+y/+z faces where the l...
void leftoverInit(const Exec &space, std::size_t nf, const LabView &faceLab, const ParentView &parent)
Leftover tier init: parent = f for the films no core patch reached, -1 otherwise.
void faceLabelLeftover(const Exec &space, std::size_t nf, const ParentView &parent, const LabView &faceLab, const RootFn &rootOf)
Leftover-tier labels: the still-kSent films (their own CCL tier) get rootOf(parent).
MD3 ownedRange(const Exec &space, const Geo &geo)
The owned box of a geometry as an MDRange policy.
Kokkos::MDRangePolicy< Exec, Kokkos::Rank< 3 > > MD3
bool isPeak(const Geo &g, const SdfView &sdf, int lx, int ly, int lz, float cv, Index cgid)
Local SDF maximum under the global (sdf, gid) lexicographic order (26-neighbourhood).
void faceMerge(const Exec &space, const Geo &geo, const ParentView &parent, const PairView &fpair, const FlagView &changed)
One union sweep of the face CCL over the faces with parent >= 0 (same pair key + adjacency; owned-own...
void throatFlux(const Exec &space, const Geo &geo, const LabView &flowLab, const FieldView &u, const FieldView &v, const FieldView &w, const FieldView &p, const OpenView &ox, const OpenView &oy, const OpenView &oz, bool hasOpen, const FaceLabView &faceLab, const KeyView &keyD, const SlotView &slotD, std::size_t nt, const AncView &ancX, const AncView &ancY, const AncView &ancZ, std::array< float, 3 > origin, std::array< float, 3 > spacing, std::array< int, 3 > gdims, const AccView &Q, const AccView &A, const AccView &Cx, const AccView &Cy, const AccView &Cz, const AccView &resid, const AccView &ext)
Accumulate over every owned +face where the flow-basin label changes: the openness-weighted MAC flux ...
void faceLabelCore(const Exec &space, std::size_t nf, const ParentView &parent, const PairView &fpair, const LabView &faceLab, const RootFn &rootOf)
Core-tier labels: faceLab = rootOf(parent) for the CCL'd faces, kSent for the remaining interface (fi...
Index bsearchKey(const KeyView &keys, Index nkeys, typename KeyView::non_const_value_type key)
Device binary search over a sorted key view; returns the position or -1.
void cclFixpoint(const Exec &space, const ParentView &parent, std::size_t n, const FlagView &changed, const MergeFn &merge)
Union-find to fixpoint: merge() launches one union sweep (setting changed), then a flatten; repeat un...
ThroatSlots throatSlots(std::vector< std::pair< Index, Index > > rootPair)
int maxLabel(const Exec &space, const Geo &geo, const SegView &seg)
The largest label over the owned box (= the number of pore labels).
void faceMinGid(const Exec &space, const Geo &geo, const ParentView &parent, const MinView &mg)
Per-component minimum GLOBAL face id (3*gid+d) of the local face CCL, keyed by the component root: th...
void filmAttachSweep(const Exec &space, const Geo &geo, const LabView &faceLab, const PairView &fpair, const CoreView &core, const NbLab &nbLab, const NbPair &nbPair, const FlagView &changed)
One Jacobi min-propagation sweep of the film attachment: every film face (interface,...
void faceInit(const Exec &space, const Geo &geo, const LabView &flowLab, const SdfView &sdf, const OpenView &ox, const OpenView &oy, const OpenView &oz, bool hasOpen, const CoreView &core, const ParentView &parent, const PairView &fpair)
Interface predicate + CORE tier per owned face: an interface face separates two different positive fl...
constexpr Index kSent
Film-face sentinel of the per-patch throat labelling: "interface face awaiting attachment".
void faceVelocity(const Geo &g, const FieldView &u, const FieldView &v, const FieldView &w, int lx, int ly, int lz, int d, double un, double uf[3])
Full velocity vector at the centre of the +d face of field cell (lx,ly,lz) (the shared face with the ...
void detectPores(const Exec &space, const Geo &geo, const SdfView &sdf, std::array< float, 3 > origin, std::array< float, 3 > spacing, const PoreView &pores, const CountView &counter, int maxPores)
Emit the owned peaks (local SDF maxima, sdf > 0) as Pore records into pores (atomic slot counter coun...
double trilinear(const Geo &g, const FieldView &p, const double gp[3])
Trilinear interpolation of a cell-centered periodic field at grid position gp (in cells).
void floodFixpoint(const Exec &space, const Geo &geo, const SdfView &sdf, LabelView &labels, LabelView &labelsN, const FlagView &changed, const SyncFn &sync)
Jacobi flood to fixpoint.
Pore poreAt(const Geo &g, const SdfView &sdf, int lx, int ly, int lz, float ox, float oy, float oz, float sx, float sy, float sz)
The pore record at a peak: sub-voxel position by the SDF^2-weighted centroid of the 3^3 neighbourhood...
void gradientWalk(const Exec &space, const GridGeo &geo, const SdfView &sdf, bool ascentAll, const EmitFn &emit)
Single-rank gradient walk: from every owned cell follow the steepest neighbour (ascent for pores,...
std::vector< std::pair< int, int > > uniquePairs(const PairView &pairs, std::size_t count)
Interleaved device pairs -> host (l1,l2) list, sorted and unique.
bool facesAdjacent(int dx, int dy, int dz, int d, int d2)
Face adjacency of the per-patch throat CCL: two faces (cell offset (dx,dy,dz), directions d and d2) c...
void cclFlatten(const Exec &space, const ParentView &parent, std::size_t n)
Path compression: every labelled entry points at its root.
constexpr int kPoreVol
Number of per-pore volume sums of poreVolume (layout [c*np + k]): c = 0 fluid cell count; 1 sum of p;...
ThroatExtras throatExtras(const std::vector< std::pair< int, int > > &throats, const std::vector< double > &ext, const std::vector< double > &throatArea, const std::vector< double > &Cx, const std::vector< double > &Cy, const std::vector< double > &Cz, const std::vector< double > &ancX, const std::vector< double > &ancY, const std::vector< double > &ancZ)
void initMarkers(const Exec &space, const Geo &geo, const SdfView &sdf, float thr, const ParentView &parent)
Deep-solid markers: parent(o) = o where sdf < thr, else -1 (owned-dense).
void cclMergeMarkers(const Exec &space, const Geo &geo, const ParentView &parent, const FlagView &changed)
One union sweep of the marker CCL (26-connectivity, 13 forward neighbours, union-by-min via atomic_mi...
constexpr int kThroatExt
Number of per-throat momentum/energy sums of throatFlux (ext, layout [c*nt + slot]): c = 0....
int resolveHoldAtGhost(const Exec &space, const Geo &geo, const TargetView &target)
One round of pointer jumping with the hold-at-ghost invariant: chase within the field,...
PoreVolumeStats poreVolumeStats(const std::vector< double > &acc, int np, std::array< float, 3 > spacing, const std::vector< double > &ancX, const std::vector< double > &ancY, const std::vector< double > &ancZ)
std::vector< double > throatPressureDrops(const std::vector< std::pair< int, int > > &throats, const std::vector< double > &posX, const std::vector< double > &posY, const std::vector< double > &posZ, const std::vector< double > &porePressure, const std::vector< double > &throatArea, const std::vector< double > &Cx, const std::vector< double > &Cy, const std::vector< double > &Cz, const std::vector< double > &ancX, const std::vector< double > &ancY, const std::vector< double > &ancZ, std::array< float, 3 > spacing, std::array< int, 3 > gdims, std::array< double, 3 > grad_p)
Total-pressure drop per throat: periodic parts + the macroscopic gradient along the throat- anchored ...
int get_idx(int x, int y, int z, I3 res)
V::value_type readScalar(const V &d)
Read a device scalar (the changed / counter flags) back to the host.
Kokkos::DefaultExecutionSpace Exec
void uploadVec(const std::vector< T > &h, const Kokkos::View< T *, Mem > &d)
Bulk host->device upload of a whole std::vector via one deep_copy over an unmanaged host view.
std::vector< typename V::value_type > downloadN(const V &d, std::size_t count)
Download the first count elements of a device view into a fresh std::vector via one deep_copy — only ...
std::int64_t Index
Global voxel / face id (== peclet::core::Index; asserted where core is included).
Extended-block geometry policy (the MPI pipeline): ghost-inclusive local box <-> global periodic grid...
Index cellAtGlobal(int gx, int gy, int gz) const
bool inOwned(int ix, int iy, int iz) const
std::size_t numExt() const
Index localOf(Index gid) const
gid -> extended-local linear index if the cell has an image in this extended box, else -1.
Index cell(int lx, int ly, int lz) const
bool onSurface(int ix, int iy, int iz) const
Is the owned cell within one cell of a block face (the only cells that can see a ghost)?
Index gid(int lx, int ly, int lz) const
void ownedCoords(Index o, int &ix, int &iy, int &iz) const
Index owned(int ix, int iy, int iz) const
static int wrapc(int a, int n)
void gcoord(int lx, int ly, int lz, int &gx, int &gy, int &gz) const
Index lidx(int lx, int ly, int lz) const
std::size_t numOwned() const
Index gidAt(int lx, int ly, int lz) const
gid of the (periodically wrapped) global cell under extended-local coords.
Single-rank geometry policy: the field IS the whole periodic grid, owned == field coords,...
Index cellAtGlobal(int gx, int gy, int gz) const
void ownedCoords(Index o, int &ix, int &iy, int &iz) const
bool inOwned(int, int, int) const
Index gid(int lx, int ly, int lz) const
Index owned(int ix, int iy, int iz) const
Index localOf(Index gid) const
Index cell(int lx, int ly, int lz) const
void gcoord(int lx, int ly, int lz, int &gx, int &gy, int &gz) const
std::size_t numOwned() const
static GridGeo of(I3 res)
Per-face field accessors for the film-attachment sweep: label / pair key of face (cell e,...
Index operator()(Index e, int d) const
Host finish of the per-pore volume sums (both pipelines): fluid volume (count * cell volume),...
std::vector< double > centroid_zyx
std::vector< double > mean_kinetic_energy
std::vector< double > mean_pressure
std::vector< double > mean_velocity_zyx
std::vector< double > volume
Index operator()(Index e, int d) const
Global unique (patch root, pair key) list -> the throat list ordered by (pair, root fid),...
std::vector< Index > keySorted
std::vector< int > slotOf
std::vector< std::pair< int, int > > throats