56#ifndef PECLET_AMR_CF_SCHEME_HPP
57#define PECLET_AMR_CF_SCHEME_HPP
67#include "peclet/core/common/host_parallel.hpp"
68#include "peclet/core/common/types.hpp"
100template <
unsigned Bits,
class Ent,
class Flu
idFn>
102 Index fine,
int axis,
double scale, FluidFn&& fluidOk,
CfScheme scheme,
103 const Ent& proto = Ent{}) {
112 const std::array<long, 3> bc = ap.
loOf(coarse);
113 const std::array<long, 3> bf = ap.
loOf(fine);
114 const unsigned Lc = ap.
levelOf(coarse);
115 const double sc =
static_cast<double>(Index(1) << Lc);
116 const double sf =
static_cast<double>(Index(1) << ap.
levelOf(fine));
117 auto push = [&](Index cell,
double w) {
123 for (
int tt = 0; tt < 3; ++tt) {
128 const double H = ap.
cellWidth(coarse, tt);
131 ((
static_cast<double>(bf[tt]) + 0.5 * sf) - (
static_cast<double>(bc[tt]) + 0.5 * sc)) *
146 auto sampleTangential = [&](
int dir) {
151 const unsigned Lnb = ap.
levelOf(nb);
172 const long scL = 1L << Lc, shL = scL >> 1;
173 std::array<long, 3> lo = bc;
174 lo[tt] += (dir > 0) ? scL : -scL;
175 for (
int oct2 = 0; oct2 < (1 << 3); ++oct2) {
176 std::array<long, 3> q = lo;
177 for (
int d = 0; d < 3; ++d)
181 if (ch < 0 || Lch + 1 != Lc || !fluidOk(ch))
184 sm.w[sm.n] = 1.0 /
static_cast<double>(1 << 3);
190 const Samp sp = sampleTangential(+1);
191 const Samp smi = sampleTangential(-1);
192 const bool okP = sp.ok && ap.
faceOpenness(coarse, tt, +1) >= 0.5;
193 const bool okM = smi.ok && ap.
faceOpenness(coarse, tt, -1) >= 0.5;
196 const double cUp = dt / (2.0 * H) + 0.5 * dt * dt / (H * H);
197 const double cUm = -dt / (2.0 * H) + 0.5 * dt * dt / (H * H);
198 const double cUc = -dt * dt / (H * H);
199 push(coarse, scale * cUc);
200 for (
int k2 = 0; k2 < sp.n; ++k2)
201 push(sp.cell[k2], scale * cUp * sp.w[k2]);
202 for (
int k2 = 0; k2 < smi.n; ++k2)
203 push(smi.cell[k2], scale * cUm * smi.w[k2]);
212 const Samp& s1 = okP ? sp : smi;
213 const double sgn = okP ? 1.0 : -1.0;
214 push(coarse, -scale * sgn * dt / H);
215 for (
int k2 = 0; k2 < s1.n; ++k2)
216 push(s1.cell[k2], scale * sgn * (dt / H) * s1.w[k2]);
268template <
unsigned Bits,
class Ent,
class Flu
idFn>
270 Index coarse, Index fine,
int axis,
double dist,
double scale,
271 FluidFn&& fluidOk,
CfScheme scheme,
const Ent& proto = Ent{}) {
273 const double wF = (0.5 * H) / dist, wC = (0.5 * h) / dist;
274 Ent eF = proto, eC = proto;
276 eF.w = scale * (wF - 0.5);
278 eC.w = scale * (wC - 0.5);
281 cfAppendStencil(ap, out, coarse, fine, axis, scale * wC, fluidOk, scheme, proto);
284template <
class Ent,
class Csr>
285inline void compactCsr(
const std::vector<std::vector<Ent>>& per,
Csr& out, Index n) {
286 out.
start.assign(
static_cast<std::size_t
>(n) + 1, 0);
287 for (Index i = 0; i < n; ++i)
288 out.
start[
static_cast<std::size_t
>(i) + 1] =
289 out.
start[
static_cast<std::size_t
>(i)] +
290 static_cast<Index
>(per[
static_cast<std::size_t
>(i)].size());
291 const Index nz = out.
start[
static_cast<std::size_t
>(n)];
292 out.slot.resize(
static_cast<std::size_t
>(nz));
293 out.
coef.resize(
static_cast<std::size_t
>(nz));
295 for (Index i = 0; i < n; ++i)
296 for (
const auto& e : per[
static_cast<std::size_t
>(i)]) {
297 out.slot[
static_cast<std::size_t
>(k)] = e.cell;
298 out.
coef[
static_cast<std::size_t
>(k)] = e.w;
310template <
unsigned Bits,
class RowFn,
class Flu
idFn>
312 double factor, RowFn&& rowOk, FluidFn&& fluidOk,
CfScheme scheme) {
314 std::vector<std::vector<detail::ScalarEnt>> per(
static_cast<std::size_t
>(n));
318 hostParFor(n, [&](Index i) {
321 const unsigned Li = t.
level(i);
324 const unsigned Lj = ap.
levelOf(j);
327 const Index coarse = (Lj > Li) ? j : i;
328 const Index fine = (Lj > Li) ? i : j;
329 const double scale = factor * invV * (a * c) * ((Lj > Li) ? 1.0 : -1.0);
349template <
unsigned Bits,
class RegularFn,
class Flu
idFn>
351 RegularFn&& regularOk, FluidFn&& fluidOk,
CfScheme scheme) {
353 std::vector<std::vector<detail::CompEnt>> per(
static_cast<std::size_t
>(n));
355 hostParFor(n, [&](Index i) {
358 const unsigned Li = t.
level(i);
361 i, [&](Index j,
int axis,
int dir,
double area,
double dist,
double alpha) {
362 const unsigned Lj = ap.
levelOf(j);
364 if (Lj == Li || !regularOk(j))
366 const Index coarse = (Lj > Li) ? j : i;
367 const Index fine = (Lj > Li) ? i : j;
368 const double scale = invV * alpha * area *
static_cast<double>(dir);
370 proto.
comp =
static_cast<int8_t
>(axis);
372 dist, scale, fluidOk, scheme, proto);
380 for (Index i = 0; i < n; ++i)
381 for (
const auto& e : per[
static_cast<std::size_t
>(i)])
382 csr.
comp[
static_cast<std::size_t
>(k++)] = e.comp;
396template <
unsigned Bits,
class RegularFn,
class Flu
idFn>
399 FluidFn&& fluidOk,
CfScheme scheme) {
401 std::array<std::vector<std::vector<detail::ScalarEnt>>, 3> per;
402 for (
int a = 0; a < 3; ++a)
403 per[
static_cast<std::size_t
>(a)].resize(
static_cast<std::size_t
>(n));
405 hostParFor(n, [&](Index i) {
408 const unsigned Li = t.
level(i);
412 double sdist[3][2] = {};
414 ap.
forEachFaceFull(i, [&](Index j,
int axis,
int dir,
double,
double dist,
double alpha) {
417 const int s = (dir > 0) ? 0 : 1;
419 sdist[axis][s] = dist;
424 if (ap.
levelOf(j) != Li && regularOk(j))
427 for (
int a = 0; a < 3; ++a) {
428 if (!cf[a][0] && !cf[a][1])
430 if (cnt[a][0] == 0 || cnt[a][1] == 0)
432 const double dp = sdist[a][0], dm = sdist[a][1];
433 const double wp = dm / (dp + dm), wm = dp / (dp + dm);
434 auto& row = per[
static_cast<std::size_t
>(a)][
static_cast<std::size_t
>(i)];
435 ap.
forEachFaceFull(i, [&](Index j,
int axis,
int dir,
double,
double dist,
double alpha) {
436 if (axis != a || alpha <= 1e-12)
438 const int s = (dir > 0) ? 0 : 1;
439 const double w = (s == 0) ? wp : wm;
440 const double inv = 1.0 /
static_cast<double>(cnt[a][s]);
441 const double gsgn = (dir > 0) ? 1.0 : -1.0;
443 const double rw = (w - 0.5) * inv * gsgn / dist;
445 row.push_back({j, rw});
446 row.push_back({i, -rw});
449 const unsigned Lj = ap.
levelOf(j);
450 if (Lj == Li || !regularOk(j))
452 const Index coarse = (Lj > Li) ? j : i;
453 const Index fine = (Lj > Li) ? i : j;
454 const double ssgn = (Lj > Li) ? gsgn : -gsgn;
460 std::array<CfCsr, 3> out;
461 for (
int a = 0; a < 3; ++a)
462 detail::compactCsr(per[
static_cast<std::size_t
>(a)], out[
static_cast<std::size_t
>(a)], n);
491template <
unsigned Bits,
class RegularFn,
class Flu
idFn>
493 RegularFn&& regularOk, FluidFn&& fluidOk,
CfScheme scheme) {
498 std::vector<Index> slotCnt(
static_cast<std::size_t
>(n), 0);
499 hostParFor(n, [&](Index i) {
501 ap.
forEachFaceFull(i, [&](Index,
int,
int,
double,
double,
double) { ++c; });
502 slotCnt[
static_cast<std::size_t
>(i)] = c;
504 std::vector<Index> slotBase(
static_cast<std::size_t
>(n) + 1, 0);
505 hostParScan(n, [&](Index i, Index& acc,
bool final) {
507 slotBase[
static_cast<std::size_t
>(i)] = acc;
508 acc += slotCnt[
static_cast<std::size_t
>(i)];
511 (n > 0) ? slotBase[
static_cast<std::size_t
>(n) - 1] + slotCnt[
static_cast<std::size_t
>(n) - 1]
513 slotBase[
static_cast<std::size_t
>(n)] = nSlots;
515 d.
vel.
start.assign(
static_cast<std::size_t
>(nSlots) + 1, 0);
521 std::vector<std::vector<detail::CompEnt>> velPer(
static_cast<std::size_t
>(n));
522 hostParFor(n, [&](Index i) {
523 const unsigned Li = t.
level(i);
524 Index slot = slotBase[
static_cast<std::size_t
>(i)];
525 auto& vrow = velPer[
static_cast<std::size_t
>(i)];
526 ap.
forEachFaceFull(i, [&](Index j,
int axis,
int,
double,
double dist,
double) {
527 const unsigned Lj = ap.
levelOf(j);
528 if (Lj != Li && regularOk(i) && regularOk(j)) {
529 const Index coarse = (Lj > Li) ? j : i;
530 const Index fine = (Lj > Li) ? i : j;
531 std::vector<detail::CompEnt> ve;
533 proto.
comp =
static_cast<int8_t
>(axis);
536 for (
const auto& e : ve)
538 d.vel.start[
static_cast<std::size_t
>(slot) + 1] =
static_cast<Index
>(ve.size());
545 for (Index s = 0; s < nSlots; ++s)
546 d.vel.start[
static_cast<std::size_t
>(s) + 1] += d.vel.start[
static_cast<std::size_t
>(s)];
547 d.vel.slot.reserve(
static_cast<std::size_t
>(d.vel.start[
static_cast<std::size_t
>(nSlots)]));
548 d.vel.coef.reserve(
static_cast<std::size_t
>(d.vel.start[
static_cast<std::size_t
>(nSlots)]));
549 d.vel.comp.reserve(
static_cast<std::size_t
>(d.vel.start[
static_cast<std::size_t
>(nSlots)]));
550 for (Index i = 0; i < n; ++i)
551 for (
const auto& e : velPer[
static_cast<std::size_t
>(i)]) {
552 d.vel.slot.push_back(e.cell);
553 d.vel.coef.push_back(e.w);
554 d.vel.comp.push_back(e.comp);
569template <
unsigned Bits,
class RegularFn,
class Flu
idFn>
571 RegularFn&& regularOk, FluidFn&& fluidOk) {
575 std::vector<Index> per(
static_cast<std::size_t
>(n), 0);
576 hostParFor(n, [&](Index i) {
579 const unsigned Li = t.
level(i);
582 if (ap.
levelOf(j) == Li || !fluidOk(j))
584 if (regularOk(i) && regularOk(j))
588 per[
static_cast<std::size_t
>(i)] = c;
591 for (Index i = 0; i < n; ++i)
592 cnt += per[
static_cast<std::size_t
>(i)];
599inline void cfApplyHost(
const CfCsr& c,
const std::vector<double>& f, std::vector<double>& out) {
600 const Index n =
static_cast<Index
>(c.
start.size()) - 1;
601 for (Index i = 0; i < n; ++i) {
603 for (Index k = c.
start[
static_cast<std::size_t
>(i)];
604 k < c.
start[
static_cast<std::size_t
>(i) + 1]; ++k)
605 acc += c.
coef[
static_cast<std::size_t
>(k)] *
606 f[
static_cast<std::size_t
>(c.
slot[
static_cast<std::size_t
>(k)])];
607 out[
static_cast<std::size_t
>(i)] += acc;
613 std::vector<double>& out) {
614 const Index n =
static_cast<Index
>(c.
start.size()) - 1;
615 for (Index i = 0; i < n; ++i) {
617 for (Index k = c.
start[
static_cast<std::size_t
>(i)];
618 k < c.
start[
static_cast<std::size_t
>(i) + 1]; ++k)
619 acc += c.
coef[
static_cast<std::size_t
>(k)] *
620 u[
static_cast<std::size_t
>(c.
comp[
static_cast<std::size_t
>(k)])]
621 [
static_cast<std::size_t
>(c.
slot[
static_cast<std::size_t
>(k)])];
622 out[
static_cast<std::size_t
>(i)] += acc;
627#ifdef KOKKOS_INLINE_FUNCTION
644 d.
n =
static_cast<Index
>(h.
start.size()) - 1;
645 d.
nz =
static_cast<Index
>(h.
slot.size());
657 d.
n =
static_cast<Index
>(h.
start.size()) - 1;
658 d.
nz =
static_cast<Index
>(h.
slot.size());
677 Kokkos::parallel_for(
678 "amr::cf_apply", c.
n, KOKKOS_LAMBDA(
const Index i) {
680 for (Index k = st(i); k < st(i + 1); ++k)
681 acc += w(k) * f(sl(k));
688 View<const double> u2, View<double> out) {
695 Kokkos::parallel_for(
696 "amr::cf_apply_comp", c.
n, KOKKOS_LAMBDA(
const Index i) {
698 for (Index k = st(i); k < st(i + 1); ++k) {
700 const double v = (a == 0) ? u0(sl(k)) : (a == 1) ? u1(sl(k)) : u2(sl(k));
Cell-centered FV Poisson operator on one (periodic) block octree.
unsigned levelOf(Index slot) const
Octree level of an extended slot (local leaf or declared ghost).
Real cellWidth(Index i, int axis) const
Cell width along axis (Phase 3: per axis).
void forEachFaceFull(Index i, Fn &&fn) const
Like forEachFaceNeighbor but exposes geometry for a consistent FV divergence/gradient: fn(neighbour,...
const Vec< Dim > & h0() const
void forEachFaceNeighbor(Index i, Fn &&fn) const
Visit each face neighbour of leaf i: fn(neighbourSlot, coeff, axis, alpha) where coeff = A_f / d_f (p...
std::pair< Index, unsigned > probeSlot(const std::array< long, Dim > &p) const
Covering slot + level of a block-local probe (longs, possibly outside the block).
double faceOpenness(Index i, int axis, int dir) const
Openness of leaf i's face on (axis,dir); 1 if no openness has been set.
Index periodicNeighbor(Index i, int axis, int dir) const
Periodic face neighbour leaf (covering the cell just across the face).
std::array< long, Dim > loOf(Index slot) const
Block-local lo corner of an extended slot as longs (ghosts may lie outside the block).
Real cellVolume(Index i) const
Per-block adaptive octree over block-local Morton codes.
unsigned level(Index i) const
void cfAppendStencil(const AmrPoisson< 3, Bits > &ap, std::vector< Ent > &out, Index coarse, Index fine, int axis, double scale, FluidFn &&fluidOk, CfScheme scheme, const Ent &proto=Ent{})
Append the scheme's (coarse* − coarse_raw) substitution stencil for ONE directed C/F sub-face to out,...
void cfAppendFaceValueDelta(const AmrPoisson< 3, Bits > &ap, std::vector< Ent > &out, Index coarse, Index fine, int axis, double dist, double scale, FluidFn &&fluidOk, CfScheme scheme, const Ent &proto=Ent{})
THE face-value delta for ONE directed 2:1 sub-face, ×scale — the single source of truth shared by the...
void compactCsr(const std::vector< std::vector< Ent > > &per, Csr &out, Index n)
Index countCfCutFaces(const AmrPoisson< 3, Bits > &ap, const BlockOctree< 3, Bits > &t, RegularFn &®ularOk, FluidFn &&fluidOk)
The C/F CENSUS (docs/amr_cf_flux_gate.md §6.5): how many OWNED forEachFaceFull slots (i,...
CfCsrDev uploadCfCsr(const CfCsr &h, const char *name)
CfCompCsr buildCfDivDelta(const AmrPoisson< 3, Bits > &ap, const BlockOctree< 3, Bits > &t, RegularFn &®ularOk, FluidFn &&fluidOk, CfScheme scheme)
(D_scheme − D_std) for the face-average divergence div_i = invV·Σ α·A·dir·(face value).
void cfApply(const CfCsrDev &c, View< const double > f, View< double > out)
out(i) += Σ coef·f(slot).
CfCompCsrDev uploadCfCompCsr(const CfCompCsr &h, const char *name)
CfUfDelta buildCfUfDelta(const AmrPoisson< 3, Bits > &ap, const BlockOctree< 3, Bits > &t, RegularFn &®ularOk, FluidFn &&fluidOk, CfScheme scheme)
void cfApplyCompHost(const CfCompCsr &c, const std::array< std::vector< double >, 3 > &u, std::vector< double > &out)
out(i) += Σ coef·u[comp](slot) (the divergence overlay).
CfCsr buildCfLapDelta(const AmrPoisson< 3, Bits > &ap, const BlockOctree< 3, Bits > &t, double factor, RowFn &&rowOk, FluidFn &&fluidOk, CfScheme scheme)
(∇²_scheme − ∇²_std) as a scalar CSR, ×factor (pass μ for the momentum deferred-correction RHS,...
void cfApplyHost(const CfCsr &c, const std::vector< double > &f, std::vector< double > &out)
out(i) += Σ coef·f(slot) (the scalar overlay: momentum ∇² delta, gradient delta per axis).
std::array< CfCsr, 3 > buildCfGradDelta(const AmrPoisson< 3, Bits > &ap, const BlockOctree< 3, Bits > &t, RegularFn &®ularOk, FluidFn &&fluidOk, CfScheme scheme)
(G_scheme − G_std) for the ABC cell gradient gradOf/grad3.
void cfApplyComp(const CfCompCsrDev &c, View< const double > u0, View< const double > u1, View< const double > u2, View< double > out)
out(i) += Σ coef·u[comp](slot).
CfScheme
Coarse/fine interface scheme for the collocated flow operators.
@ standard
raw coarse value (two-point flux; 1st-order at 2:1 faces)
@ quadratic
Martin–Cartwright tangential quadratic (2nd-order at 2:1 faces).
Component-tagged CSR overlay (the divergence delta): out(i) += Σ_k coef·u[comp](slot).
std::vector< Index > slot
std::vector< Index > start
std::vector< int8_t > comp
std::vector< double > coef
Device mirror of a CfCsr (empty when the scheme is standard / no C/F rows).
Scalar-input CSR overlay: out(i) += Σ_k coef·f(slot) over rows with C/F faces.
std::vector< double > coef
std::vector< Index > slot
std::vector< Index > start
size n+1
(uf_scheme − uf_std) for the div-free FACE field uf_k = ½(u_i+u_j) − (φ₊−φ₋)/d, one delta row per for...
CfCompCsr vel
reads the velocity components, rows = face slots
An assembled face-CSR: row offsets (size n+1), neighbour index + coefficient per face (size nFaces).
View< double > coef
coefficient per face, size nFaces
View< Index > start
CSR row offsets, size n+1; start(n) == nFaces.