25#ifndef PECLET_AMR_MULTIGRID_HPP
26#define PECLET_AMR_MULTIGRID_HPP
40#include "peclet/core/common/host_parallel.hpp"
41#include "peclet/core/common/view.hpp"
46inline void restrictField(View<const Index> childStart, View<const Index> childIdx,
47 View<const double> fine, View<double> coarse, Index nCoarse) {
49 "amr::device_restrict", nCoarse, KOKKOS_LAMBDA(
const Index p) {
50 const Index a = childStart(p), z = childStart(p + 1);
52 for (Index k = a; k < z; ++k)
53 s += fine(childIdx(k));
54 coarse(p) = (z > a) ? s /
static_cast<double>(z - a) : 0.0;
64inline void restrictKappa(View<const Index> childStart, View<const Index> childIdx,
65 View<const double> fine, View<const double> kappa, View<double> coarse,
68 "amr::device_restrict_kappa", nCoarse, KOKKOS_LAMBDA(
const Index p) {
69 const Index a = childStart(p), z = childStart(p + 1);
70 double sw = 0.0, swv = 0.0;
71 for (Index k = a; k < z; ++k) {
72 const Index ch = childIdx(k);
73 const double w = kappa(ch);
77 coarse(p) = (sw > 0.0) ? swv / sw : 0.0;
82inline void prolongAdd(View<const Index> c2p, View<const double> coarse, View<double> fine,
85 "amr::device_prolong", nFine, KOKKOS_LAMBDA(
const Index i) {
86 const Index p = c2p(i);
95 View<const char> excl, View<double> fine, Index nFine) {
97 "amr::device_prolong_masked", nFine, KOKKOS_LAMBDA(
const Index i) {
100 const Index p = c2p(i);
102 fine(i) += coarse(p);
109inline void zeroMasked(View<double> v, View<const char> excl, Index n) {
110 Kokkos::parallel_for(
111 "amr::device_zero_masked", n, KOKKOS_LAMBDA(
const Index i) {
117template <
int Dim,
unsigned Bits = (Dim == 2 ? 32u : (Dim == 3 ? 21u : 16u))>
142 void build(
const Octree& finest,
const Vec<Dim>& h0,
bool liftRoot =
true,
145 hmg_ = std::make_unique<AmrMultigrid<Dim, Bits>>();
154 template <
class OpenFn>
155 void build(
const Octree& finest,
double h0, OpenFn&& openFn,
bool periodic =
true,
156 bool immersedWall =
false,
bool liftRoot =
true, Index
bottomExtent = 4) {
164 template <
class OpenFn>
165 void build(
const Octree& finest,
const Vec<Dim>& h0, OpenFn&& openFn,
bool periodic =
true,
166 bool immersedWall =
false,
bool liftRoot =
true, Index
bottomExtent = 4) {
168 hmg_ = std::make_unique<AmrMultigrid<Dim, Bits>>();
170 hmg_->setOpenness(std::forward<OpenFn>(openFn));
171 hmg_->setPeriodic(periodic);
172 hmg_->setImmersedWall(immersedWall);
182 void buildRaw(
const Octree& finest,
const Vec<Dim>& h0, std::vector<double> alphaRaw,
183 bool periodic =
true,
bool liftRoot =
true, Index
bottomExtent = 4) {
185 hmg_ = std::make_unique<AmrMultigrid<Dim, Bits>>();
187 hmg_->setOpennessRaw(std::move(alphaRaw));
188 hmg_->setPeriodic(periodic);
209 for (
auto& lv : levels_) {
216 if (c0 != 0.0 || cD != 1.0)
233 std::string
bottomName()
const {
return amgOn_ ?
"amg" :
"jacobi"; }
254 std::size_t
numLevels()
const {
return levels_.size(); }
255 Index
numLeaves(std::size_t L = 0)
const {
return levels_[L].n; }
256 Code octreeCode(std::size_t L, Index i)
const {
return hmg_->op(L).octree().code(i); }
257 View<double>
x(std::size_t L = 0) {
return levels_[L].x; }
258 View<double>
b(std::size_t L = 0) {
return levels_[L].b; }
259 const FvOp&
op(std::size_t L = 0)
const {
return levels_[L].op; }
266 void vcycle(
int pre = 2,
int post = 2,
int bottom = 40,
double omega = 0.8, std::size_t L = 0) {
267 Level& lv = levels_[L];
268 View<const double> bc(lv.b);
269 if (L + 1 == levels_.size()) {
273 for (
int s = 0; s <
bottom; ++s)
274 jacobiFv(lv.op, lv.x, bc, lv.tmp, omega);
280 for (
int s = 0; s < pre; ++s)
281 jacobiFv(lv.op, lv.x, bc, lv.tmp, omega);
282 residualFv(lv.op, View<const double>(lv.x), bc, lv.res);
283 Level& cl = levels_[L + 1];
285 restrictKappa(lv.childStart, lv.childIdx, View<const double>(lv.res),
286 View<const double>(lv.kappa), cl.b, cl.n);
288 restrictField(lv.childStart, lv.childIdx, View<const double>(lv.res), cl.b, cl.n);
289 Kokkos::deep_copy(cl.x, 0.0);
291 prolongAdd(lv.c2p, View<const double>(cl.x), lv.x, lv.n);
292 for (
int s = 0; s < post; ++s)
293 jacobiFv(lv.op, lv.x, bc, lv.tmp, omega);
303 double solveQuad(
int outer = 20,
int cyclesPerOuter = 1,
int pre = 2,
int post = 2,
304 int bottom = 40,
double omega = 0.8) {
305 Level& lv = levels_[0];
306 Kokkos::deep_copy(b0true_, lv.b);
311 for (
int o = 0; o < outer; ++o) {
312 quadDelta(View<const Index>(qStart_), View<const Index>(qSlot_), View<const double>(qCoef_),
313 View<const double>(lv.x), dq_, lv.n);
314 Kokkos::parallel_for(
315 "amr::quad_rhsp", lv.n, KOKKOS_LAMBDA(
const Index i) { bb(i) = b0(i) - dq(i); });
316 for (
int c = 0; c < cyclesPerOuter; ++c)
320 residualFv(lv.op, View<const double>(lv.x), View<const double>(b0true_), lv.res);
321 quadDelta(View<const Index>(qStart_), View<const Index>(qSlot_), View<const double>(qCoef_),
322 View<const double>(lv.x), dq_, lv.n);
325 Kokkos::parallel_reduce(
326 "amr::quad_resnorm", lv.n,
327 KOKKOS_LAMBDA(
const Index i,
double& acc) {
328 double rr = res(i) - dq(i);
333 Kokkos::deep_copy(lv.b, b0true_);
352 void buildFromHostMg() {
353 const std::size_t nl = hmg_->numLevels();
356 for (std::size_t L = 0; L < nl; ++L) {
357 const Poisson& ap = hmg_->op(L);
359 Level& lv = levels_[L];
361 buildFaceCsr(ap, oct, lv);
364 std::vector<double> kap(
static_cast<std::size_t
>(lv.
n));
365 hostParFor(lv.
n, [&](Index i) {
367 for (int axis = 0; axis < Dim; ++axis)
368 for (int dir = -1; dir <= 1; dir += 2)
369 s += ap.faceOpenness(i, axis, dir);
370 kap[static_cast<std::size_t>(i)] = s / static_cast<double>(2 * Dim);
372 lv.
kappa = toDevice(kap,
"mg_kappa");
373 lv.
x = View<double>(
"mg_x",
static_cast<std::size_t
>(lv.
n));
374 lv.
b = View<double>(
"mg_b",
static_cast<std::size_t
>(lv.
n));
375 lv.
res = View<double>(
"mg_res",
static_cast<std::size_t
>(lv.
n));
376 lv.
tmp = View<double>(
"mg_tmp",
static_cast<std::size_t
>(lv.
n));
378 for (std::size_t L = 0; L + 1 < nl; ++L) {
379 const Octree& f = hmg_->op(L).octree();
380 const Octree& c = hmg_->op(L + 1).octree();
381 const Index nf = f.numLeaves(), nc = c.numLeaves();
382 std::vector<Index> c2p(
static_cast<std::size_t
>(nf));
383 std::vector<Index> cnt(
static_cast<std::size_t
>(nc), 0);
391 hostParFor(nf, [&](Index i) { c2p[
static_cast<std::size_t
>(i)] = c.find(f.code(i)); });
392 for (Index i = 0; i < nf; ++i) {
393 const Index p = c2p[
static_cast<std::size_t
>(i)];
395 ++cnt[
static_cast<std::size_t
>(p)];
397 std::vector<Index> start(
static_cast<std::size_t
>(nc) + 1, 0);
398 for (Index p = 0; p < nc; ++p)
399 start[
static_cast<std::size_t
>(p) + 1] =
400 start[
static_cast<std::size_t
>(p)] + cnt[
static_cast<std::size_t
>(p)];
401 std::vector<Index> idx(
static_cast<std::size_t
>(nf));
402 std::vector<Index> cur(start.begin(), start.end() - 1);
403 for (Index i = 0; i < nf; ++i) {
404 Index p = c2p[
static_cast<std::size_t
>(i)];
406 idx[
static_cast<std::size_t
>(cur[
static_cast<std::size_t
>(p)]++)] = i;
408 levels_[L].c2p = toDevice(c2p,
"mg_c2p");
409 levels_[L].childStart = toDevice(start,
"mg_cstart");
410 levels_[L].childIdx = toDevice(idx,
"mg_cidx");
414 buildQuadCsr(hmg_->op(0), hmg_->op(0).octree());
415 const Index n0 = levels_[0].n;
416 dq_ = View<double>(
"mg_dq",
static_cast<std::size_t
>(n0));
417 b0true_ = View<double>(
"mg_b0",
static_cast<std::size_t
>(n0));
434 if (!hmg_ || levels_.empty())
436 const std::size_t Lc = levels_.size() - 1;
437 const Poisson& ap = hmg_->op(Lc);
439 for (
int d = 0; d < Dim; ++d)
440 E = std::max(E, ap.octree().brick()[d]);
447 const auto A = ap.assembleFv();
448 bool singular =
true;
449 for (
double d : A.bcDiag)
454 amg_ = std::make_unique<AmgBottom<Dim, Bits>>();
455 amg_->build(ap, singular);
463 amg_->solve(View<const double>(lv.b), lv.x);
466 residualFv(lv.op, View<const double>(lv.x), View<const double>(lv.b), lv.res);
469 double rm = 0.0, bm = 0.0;
470 Kokkos::parallel_reduce(
471 "amr::bottom_check", lv.n,
472 KOKKOS_LAMBDA(
const Index i,
double& a,
double& c) {
473 const double r = res(i) < 0.0 ? -res(i) : res(i);
474 const double b = bb(i) < 0.0 ? -bb(i) : bb(i);
480 Kokkos::Max<double>(rm), Kokkos::Max<double>(bm));
481 bottomRel_ = (bm > 0.0) ? rm / bm : 0.0;
486 const double c0 = lv.op.c0, cD = lv.op.cD;
501 const std::size_t nl = hmg_->numLevels();
502 for (std::size_t L = 0; L < nl && L < levels_.size(); ++L)
503 buildFaceCsr(hmg_->op(L), hmg_->op(L).octree(), levels_[L]);
511 const Index n = t.numLeaves();
512 std::vector<std::vector<std::pair<Index, double>>> per(
static_cast<std::size_t
>(n));
513 const Vec<Dim>& h0 = ap.h0();
514 hostParFor(n, [&](Index i) {
515 const unsigned Li = t.level(i);
516 const double invV = 1.0 / ap.cellVolume(i);
517 ap.forEachFaceNeighbor(i, [&](Index j, Real c,
int axis,
double a) {
518 const unsigned Lj = t.level(j);
521 const Index coarse = (Lj > Li) ? j : i;
522 const Index fine = (Lj > Li) ? i : j;
523 const double scale = invV * (a * c) * ((Lj > Li) ? 1.0 : -1.0);
524 addCoarseStarStencil(ap, t, per[
static_cast<std::size_t
>(i)], coarse, fine, axis, scale,
528 std::vector<Index> qs(
static_cast<std::size_t
>(n) + 1, 0);
529 for (Index i = 0; i < n; ++i)
530 qs[
static_cast<std::size_t
>(i) + 1] =
531 qs[
static_cast<std::size_t
>(i)] +
532 static_cast<Index
>(per[
static_cast<std::size_t
>(i)].size());
533 const Index nq = qs[
static_cast<std::size_t
>(n)];
534 std::vector<Index> slot(
static_cast<std::size_t
>(nq));
535 std::vector<double> coef(
static_cast<std::size_t
>(nq));
537 for (Index i = 0; i < n; ++i)
538 for (
auto& e : per[
static_cast<std::size_t
>(i)]) {
539 slot[
static_cast<std::size_t
>(k)] = e.first;
540 coef[
static_cast<std::size_t
>(k)] = e.second;
543 qStart_ = toDevice(qs,
"mg_qstart");
544 qSlot_ = toDevice(slot,
"mg_qslot");
545 qCoef_ = toDevice(coef,
"mg_qcoef");
551 static void addCoarseStarStencil(
const Poisson& ap,
const Octree& t,
552 std::vector<std::pair<Index, double>>& out, Index coarse,
553 Index fine,
int axis,
double scale,
const Vec<Dim>& h0) {
554 auto bc = t.bounds(coarse);
555 auto bf = t.bounds(fine);
556 const double sc =
static_cast<double>(Index(1) << t.level(coarse));
557 const double sf =
static_cast<double>(Index(1) << t.level(fine));
558 for (
int tt = 0; tt < Dim; ++tt) {
563 const double H = ap.cellWidth(coarse, tt);
564 const double dt = ((
static_cast<double>(bf[0][tt]) + 0.5 * sf) -
565 (
static_cast<double>(bc[0][tt]) + 0.5 * sc)) *
567 Index cp = ap.periodicNeighbor(coarse, tt, +1);
568 Index cm = ap.periodicNeighbor(coarse, tt, -1);
569 if (cp < 0 || cm < 0)
571 if (t.level(cp) != t.level(coarse) || t.level(cm) != t.level(coarse))
573 if (ap.faceOpenness(coarse, tt, +1) < 0.5 || ap.faceOpenness(coarse, tt, -1) < 0.5)
575 const double cUp = dt / (2.0 * H) + 0.5 * dt * dt / (H * H);
576 const double cUm = -dt / (2.0 * H) + 0.5 * dt * dt / (H * H);
577 const double cUc = -dt * dt / (H * H);
578 out.emplace_back(coarse, scale * cUc);
579 out.emplace_back(cp, scale * cUp);
580 out.emplace_back(cm, scale * cUm);
587 std::unique_ptr<AmrMultigrid<Dim, Bits>> hmg_;
588 std::vector<Level> levels_;
589 bool kappaRestrict_ =
false;
590 bool removeMean_ =
false;
592 Index bottomExtent_ = 4;
593 bool amgOn_ =
false, bottomCheck_ =
false;
594 double bottomRel_ = 0.0;
595 std::unique_ptr<AmgBottom<Dim, Bits>> amg_;
596 View<Index> qStart_, qSlot_;
598 View<double> dq_, b0true_;
Cell-centered FV Poisson operator on one (periodic) block octree.
const Octree & octree() const
Per-block adaptive octree over block-local Morton codes.
morton::Morton< Dim, Bits > M
typename M::code_type Code
BlockOctree< Dim, Bits > Octree
const FvOp & op(std::size_t L=0) const
View< Index > quadSlot() const
AmrPoisson< Dim, Bits > Poisson
View< Index > quadStart() const
void setHelmholtz(double c0, double cD)
Turn every level's operator into the Helmholtz form H = c0·I + cD·L (default c0=0,...
Code octreeCode(std::size_t L, Index i) const
int bottomIters() const
Inner CG iterations of the last exact bottom solve (0 without it).
Bottom bottom() const
The selection in force (Auto unless setBottom said otherwise).
void reassembleOperators()
Re-assemble every level's operator ON THE DEVICE from the (host) hierarchy's current geometry — the d...
Index numLeaves(std::size_t L=0) const
Index bottomExtent() const
int bottomComponents() const
Connected fluid components of the coarsest level (0 without the exact bottom).
View< double > b(std::size_t L=0)
double solveQuad(int outer=20, int cyclesPerOuter=1, int pre=2, int post=2, int bottom=40, double omega=0.8)
Solve L_quad u = rhs (the 2nd-order graded operator) by deferred correction: each outer step solves L...
typename Octree::Code Code
void build(const Octree &finest, const Vec< Dim > &h0, OpenFn &&openFn, bool periodic=true, bool immersedWall=false, bool liftRoot=true, Index bottomExtent=4)
The per-axis form of the openness build (see the scalar-h0 overload above).
void vcycle(int pre=2, int post=2, int bottom=40, double omega=0.8, std::size_t L=0)
One V-cycle on level L (default finest) of the standard (consistent conservative) operator,...
double bottomResidual() const
max|b − L x| / max|b| of the last checked bottom solve (0 unless setBottomCheck(true)).
void build(const Octree &finest, const Vec< Dim > &h0, bool liftRoot=true, Index bottomExtent=4)
The per-axis form: h0 is the finest spacing (dx, dy, dz); every level inherits the root aspect ratio ...
void build(const Octree &finest, double h0, bool liftRoot=true, Index bottomExtent=4)
Build + upload the hierarchy from a finest octree (uniform coarsening), openness- free.
void build(const Octree &finest, double h0, OpenFn &&openFn, bool periodic=true, bool immersedWall=false, bool liftRoot=true, Index bottomExtent=4)
Build with cut-cell openness openFn(faceCentreWorld, axis) → [0,1], coarsened to every level by area-...
Bottom
What solves the coarsest level (docs/amr_mg_depth.md §6.6).
void bottomSolveExact(Level &lv)
One exact bottom solve.
void setBottom(Bottom b)
Select what solves the coarsest level (§6.6).
View< double > x(std::size_t L=0)
void setBottomCheck(bool on)
Opt-in §6.6 consistency gate (flow's AGMG_DEBUG): after every exact bottom solve, recompute |b − L x|...
void buildRaw(const Octree &finest, const Vec< Dim > &h0, std::vector< double > alphaRaw, bool periodic=true, bool liftRoot=true, Index bottomExtent=4)
Build from a finest octree whose face openness is handed over ALREADY EVALUATED — alphaRaw in the Amr...
std::string bottomName() const
"jacobi" | "amg" — what the coarsest level ACTUALLY runs.
void setRemoveMean(bool on)
Opt-in (default off): project the correction to mean-zero over fluid cells at every V-cycle level — t...
Index bottomIdentityRows() const
Fully closed coarsest cells the exact bottom solves as identity rows (0 without it).
bool bottomOnDevice() const
Whether the exact bottom's per-V-cycle solve runs on the device (§5.5's 10^4-row rule).
std::size_t numLevels() const
View< double > quadCoef() const
Index bottomSize() const
Rows of the exact bottom's matrix (0 when the sweeps run instead).
void setKappaRestrict(bool on)
Opt-in: use κ-weighted (fluid-fraction) restriction instead of the default plain volume-average.
Vec< Dim > filledVec(Real v)
Vec<Dim> with every component set to v (C++ has no aggregate-fill initializer).
Csr buildFaceCsr(Index n, const Emit &emit)
Build a face-CSR on device from a per-cell emit functor.
void zeroMasked(View< double > v, View< const char > excl, Index n)
Zero v at excluded cells (excl != 0).
void removeMeanFv(const FvOp &op, View< double > u)
Project u to volume-weighted-mean-zero over the ACTIVE (fluid) cells of the operator — the constant-n...
void residualFv(const FvOp &op, View< const double > u, View< const double > rhs, View< double > res)
res = rhs − H u.
FvOp assembleFv(const AmrPoisson< Dim, Bits > &ap, const BlockOctreeView< Dim, Bits > &ov)
Assemble the FvOp entirely on the device from a host AmrPoisson's geometry (h0, periodicity,...
void restrictKappa(View< const Index > childStart, View< const Index > childIdx, View< const double > fine, View< const double > kappa, View< double > coarse, Index nCoarse)
Restrict, κ-weighted: coarse(p) = Σ_child κ_c·fine_c / Σ_child κ_c, with κ the fine cell's fluid-frac...
void jacobiFv(const FvOp &op, View< double > u, View< const double > rhs, View< double > tmp, double omega)
One weighted-Jacobi sweep of H u = rhs (in place).
void prolongAddMasked(View< const Index > c2p, View< const double > coarse, View< const char > excl, View< double > fine, Index nFine)
Masked piecewise-constant prolong + correct: fine(i) += coarse(c2p(i)) only on non-excluded fine cell...
void quadDelta(View< const Index > qStart, View< const Index > qSlot, View< const double > qCoef, View< const double > u, View< double > dq, Index n)
dq = (L_quad − L_std) u, the quadratic coarse-fine correction as its own SpMV over a precomputed CSR ...
void restrictField(View< const Index > childStart, View< const Index > childIdx, View< const double > fine, View< double > coarse, Index nCoarse)
Restrict: coarse(p) = mean over p's children (CSR fixed order ⇒ deterministic).
void prolongAdd(View< const Index > c2p, View< const double > coarse, View< double > fine, Index nFine)
Prolong (piecewise-constant) + correct: fine(i) += coarse(c2p(i)).
Device mirror of a BlockOctree's leaf arrays + device-callable queries.
void upload(const Host &t)
(Re)upload the host octree's current leaf set to the device.