peclet-amr 0.2.0
Block-local-Morton AMR octree + collocated cut-cell Navier–Stokes solver (header-only C++20, Kokkos + MPI)
Loading...
Searching...
No Matches
distributed_fv.hpp
Go to the documentation of this file.
1// core — consistent graded FV Laplacian on a *distributed* octree.
2//
3// The distributed analog of AmrPoisson / Multigrid's consistent operator:
4// the conservative two-point FV Laplacian
5// (L u)_i = invVol_i · Σ_f w_f (u[nbr_f] − u_i), w_f = A_f / d_f,
6// on a graded DistributedOctree (refined + cross-block 2:1 balanced), with the
7// coarse side of every 2:1 interface carrying one face per fine sub-neighbour —
8// including sub-neighbours that live on another rank.
9//
10// Topology built once: each leaf's face stencil is enumerated exactly like
11// AmrPoisson::forEachFaceNeighbor (same axis/dir/sub-k order), with in-block
12// neighbours resolved locally and cross-block neighbours turned into *ghost*
13// entries. Ghost cells are discovered by one owner gather of the across-face
14// covering level (DistributedOctree::coverLevels) — enough to know whether a
15// remote face is same/coarser (one neighbour) or finer (2^(Dim-1) sub-neighbours,
16// each possibly on a different owner). Each matvec then fills the ghost values with
17// one owner gather (coverValues). Because the per-entry term w(val−u_i) is summed
18// in the same face order whether a neighbour is local or ghost, the operator is
19// bit-identical across rank counts (COMM_WORLD == COMM_SELF) and, on a single
20// block, bit-identical to host AmrPoisson::applyLaplacian.
21//
22// Scope: openness-free (w_f = A_f/d_f); the smoother is weighted Jacobi. Openness
23// (cut-cell) and a graded distributed *multigrid* hierarchy build on this.
24//
25#ifndef PECLET_AMR_DISTRIBUTED_FV_HPP
26#define PECLET_AMR_DISTRIBUTED_FV_HPP
27
28#include <array>
29#include <cmath>
30#include <map>
31#include <memory>
32#include <type_traits>
33#include <vector>
34
35#include "peclet/amr/common.hpp"
38#include "peclet/core/common/mpi.hpp"
39#include "peclet/core/common/types.hpp"
40
41namespace peclet::amr {
42
43template <int Dim, unsigned Bits = (Dim == 2 ? 32u : (Dim == 3 ? 21u : 16u))>
45 public:
47 using M = typename DO::M;
48 using Code = typename DO::Code;
49 using Coord = typename DO::Coord;
50
52 void init(DO& d) {
53 init(d, [](const Vec<Dim>&, int) { return 1.0; });
54 }
55
62 template <class OpenFn>
63 void init(DO& d, OpenFn&& openFn) {
64 d_ = &d;
65 h0_ = d.h0();
66 const auto& t = d.local();
67 const Index n = t.numLeaves();
68 const IVec<Dim> bfo = d.blockFineOrigin();
69 const FaceAlpha<std::remove_reference_t<OpenFn>> alpha{openFn, d.globalGeometry().origin, h0_};
70 invVol_.assign(static_cast<std::size_t>(n), 0.0);
71 diag_.assign(static_cast<std::size_t>(n), 0.0);
72
73 // Pass 1: per-leaf entries in face order; remote faces become markers.
74 std::vector<std::vector<FaceEntry>> ent(static_cast<std::size_t>(n));
75 std::vector<RemoteFace> rfaces;
76 for (Index i = 0; i < n; ++i)
77 appendLeafFaces(i, bfo, alpha, ent[static_cast<std::size_t>(i)], rfaces);
78
79 // One owner gather of the across-face covering level for every remote face.
80 std::vector<std::array<Coord, Dim>> coverCoords(rfaces.size());
81 for (std::size_t r = 0; r < rfaces.size(); ++r)
82 coverCoords[r] = rfaces[r].gc;
83 std::vector<int> coverLv = d.coverLevels(coverCoords);
84
85 // Pass 2: expand into the final CSR; ghost coords deduped to ghost slots.
86 expandFaces(ent, rfaces, coverLv, alpha);
87 n_ = n;
88 }
89
90 Index numLeaves() const { return n_; }
91
93 void apply(const std::vector<double>& u, std::vector<double>& Lu) const {
94 std::vector<double> g = d_->coverValues(ghostCoords_, u);
95 Lu.assign(static_cast<std::size_t>(n_), 0.0);
96 for (Index i = 0; i < n_; ++i) {
97 const double ui = u[static_cast<std::size_t>(i)];
98 double acc = 0.0;
99 for (Index k = start_[static_cast<std::size_t>(i)];
100 k < start_[static_cast<std::size_t>(i) + 1]; ++k) {
101 const Index r = ref_[static_cast<std::size_t>(k)];
102 const double val =
103 (r < n_) ? u[static_cast<std::size_t>(r)] : g[static_cast<std::size_t>(r - n_)];
104 acc += w_[static_cast<std::size_t>(k)] * (val - ui);
105 }
106 Lu[static_cast<std::size_t>(i)] = invVol_[static_cast<std::size_t>(i)] * acc;
107 }
108 }
109
111 double residual(const std::vector<double>& u, const std::vector<double>& rhs,
112 std::vector<double>& res) const {
113 std::vector<double> lu;
114 apply(u, lu);
115 res.assign(static_cast<std::size_t>(n_), 0.0);
116 double s = 0.0;
117 for (Index i = 0; i < n_; ++i) {
118 double r = rhs[static_cast<std::size_t>(i)] - lu[static_cast<std::size_t>(i)];
119 res[static_cast<std::size_t>(i)] = r;
120 s += r * r / invVol_[static_cast<std::size_t>(i)]; // V_i r²
121 }
122 double g = 0.0;
123 MPI_Allreduce(&s, &g, 1, MPI_DOUBLE, MPI_SUM, d_->comm());
124 return std::sqrt(g);
125 }
126
127 double residualNorm(const std::vector<double>& u, const std::vector<double>& rhs) const {
128 std::vector<double> res;
129 return residual(u, rhs, res);
130 }
131
135 void jacobi(std::vector<double>& u, const std::vector<double>& rhs, int sweeps,
136 double omega = 0.8) const {
137 std::vector<double> tmp(static_cast<std::size_t>(n_));
138 for (int s = 0; s < sweeps; ++s) {
139 std::vector<double> g = d_->coverValues(ghostCoords_, u);
140 for (Index i = 0; i < n_; ++i) {
141 double sumOff = 0.0;
142 for (Index k = start_[static_cast<std::size_t>(i)];
143 k < start_[static_cast<std::size_t>(i) + 1]; ++k) {
144 const Index r = ref_[static_cast<std::size_t>(k)];
145 const double val =
146 (r < n_) ? u[static_cast<std::size_t>(r)] : g[static_cast<std::size_t>(r - n_)];
147 sumOff += w_[static_cast<std::size_t>(k)] * val;
148 }
149 const double Vrhs = rhs[static_cast<std::size_t>(i)] / invVol_[static_cast<std::size_t>(i)];
150 tmp[static_cast<std::size_t>(i)] =
151 (diag_[static_cast<std::size_t>(i)] != 0.0)
152 ? (sumOff - Vrhs) / diag_[static_cast<std::size_t>(i)]
153 : u[static_cast<std::size_t>(i)];
154 }
155 for (Index i = 0; i < n_; ++i)
156 u[static_cast<std::size_t>(i)] = (1.0 - omega) * u[static_cast<std::size_t>(i)] +
157 omega * tmp[static_cast<std::size_t>(i)];
158 }
159 }
160
161 private:
162 // ---- init helpers ----------------------------------------------------------------------------
163 // Pass 1 entry of a leaf face: a weight and a local slot, or a marker of an unresolved remote
164 // face.
165 struct FaceEntry {
166 double w = 0.0;
167 Index localRef = -1; // >=0 local neighbour slot
168 int rfId = -1; // >=0 unresolved remote face
169 };
170 struct RemoteFace {
171 std::array<Coord, Dim> gc{};
172 int Li = 0;
173 Coord si = 1;
174 int axis = 0;
175 int dir = 1;
176 std::array<long, Dim> iLo{}; // requesting leaf's global fine lower corner
177 };
178
179 // α at the finer side's (sub-)face: flo = finer cell global fine lower corner,
180 // s its size (fine units), faceDir its outward normal toward the neighbour.
181 template <class OpenFn>
182 struct FaceAlpha {
183 OpenFn& openFn;
184 Vec<Dim> org;
185 Vec<Dim> h0_;
186 double operator()(const std::array<long, Dim>& flo, Coord s, int axis, int faceDir) const {
187 Vec<Dim> fc{};
188 long plane = flo[axis] + (faceDir > 0 ? static_cast<long>(s) : 0L);
189 for (int d2 = 0; d2 < Dim; ++d2)
190 fc[d2] =
191 (d2 == axis)
192 ? org[d2] + static_cast<double>(plane) * h0_[d2]
193 : org[d2] + (static_cast<double>(flo[d2]) + 0.5 * static_cast<double>(s)) * h0_[d2];
194 double a = openFn(fc, axis);
195 return a < 0.0 ? 0.0 : (a > 1.0 ? 1.0 : a);
196 }
197 };
198
200 static std::array<long, Dim> globalLo(const IVec<Dim>& bfo, const std::array<Coord, Dim>& lc) {
201 std::array<long, Dim> g{};
202 for (int d2 = 0; d2 < Dim; ++d2)
203 g[d2] = static_cast<long>(lc[d2]) + static_cast<long>(bfo[d2]);
204 return g;
205 }
206
207 // Global fine lower corner of the k-th fine sub-neighbour across a coarse cell's
208 // (axis,dir) face: clo is the coarse cell's global lo, si its size, sj=si/2. Used
209 // for the sub-face α centroid — the *finer* cell's lo, not the probe point.
210 std::array<long, Dim> fineLoGlobal(const std::array<long, Dim>& clo, Coord si, Coord sj, int axis,
211 int dir, int k) const {
212 std::array<long, Dim> flo{};
213 flo[axis] = (dir > 0) ? clo[axis] + static_cast<long>(si) : clo[axis] - static_cast<long>(sj);
214 int bit = 0;
215 for (int t = 0; t < Dim; ++t) {
216 if (t == axis)
217 continue;
218 flo[t] = clo[t] + (((k >> bit) & 1) ? static_cast<long>(sj) : 0L);
219 ++bit;
220 }
221 for (int d2 = 0; d2 < Dim; ++d2) {
222 long gf = static_cast<long>(d_->globalFineSize()[d2]);
223 if (flo[d2] < 0)
224 flo[d2] += gf;
225 else if (flo[d2] >= gf)
226 flo[d2] -= gf;
227 }
228 return flo;
229 }
230
232 template <class Alpha>
233 void appendLeafFaces(Index i, const IVec<Dim>& bfo, const Alpha& alpha,
234 std::vector<FaceEntry>& entI, std::vector<RemoteFace>& rfaces) {
235 DO& d = *d_;
236 const auto& t = d.local();
237 const int Li = static_cast<int>(t.level(i));
238 const Coord si = Coord(Coord(1) << Li);
239 invVol_[static_cast<std::size_t>(i)] = 1.0 / cellVol(Li);
240 auto b = t.bounds(i);
241 const auto& lo = b[0];
242 for (int axis = 0; axis < Dim; ++axis)
243 for (int dir = -1; dir <= 1; dir += 2) {
244 typename DO::FaceInfo fi = d.faceAcross(i, axis, dir);
245 if (fi.state == 2)
246 continue; // DomainNone
247 if (fi.state == 0) { // InBlock: enumerate locally
248 Index j = fi.localNb;
249 if (j < 0)
250 continue;
251 const int Lj = static_cast<int>(t.level(j));
252 if (Lj >= Li) { // finer side is i: α at i's face
253 const double a = alpha(globalLo(bfo, lo), si, axis, dir);
254 entI.push_back(FaceEntry{a * coeff(si, Coord(Coord(1) << Lj), axis), j, -1});
255 } else { // finer side is the sub-neighbour jj: α at jj's face (−dir)
256 const Coord sj = Coord(si >> 1);
257 const long pc = (dir > 0) ? static_cast<long>(lo[axis]) + static_cast<long>(si)
258 : static_cast<long>(lo[axis]) - 1;
259 const int nsub = 1 << (Dim - 1);
260 for (int k = 0; k < nsub; ++k) {
261 std::array<Coord, Dim> q = lo;
262 q[axis] = static_cast<Coord>(pc);
263 int bit = 0;
264 for (int tt = 0; tt < Dim; ++tt) {
265 if (tt == axis)
266 continue;
267 const Coord off = ((k >> bit) & 1) ? sj : Coord(0);
268 q[tt] = static_cast<Coord>(lo[tt] + off);
269 ++bit;
270 }
271 Index jj = t.find(M::encode(q).code());
272 const double a =
273 alpha(fineLoGlobal(globalLo(bfo, lo), si, sj, axis, dir, k), sj, axis, -dir);
274 entI.push_back(FaceEntry{a * coeff(si, sj, axis), jj, -1});
275 }
276 }
277 } else { // Remote: defer (need covering level)
278 FaceEntry e;
279 e.rfId = static_cast<int>(rfaces.size());
280 entI.push_back(e);
281 RemoteFace rf{fi.gc, Li, si, axis, dir, {}};
282 rf.iLo = globalLo(bfo, lo);
283 rfaces.push_back(rf);
284 }
285 }
286 }
287
289 template <class Alpha>
290 void expandFaces(const std::vector<std::vector<FaceEntry>>& ent,
291 const std::vector<RemoteFace>& rfaces, const std::vector<int>& coverLv,
292 const Alpha& alpha) {
293 const Index n = static_cast<Index>(ent.size());
294 std::map<std::array<Coord, Dim>, Index> ghostSlot;
295 std::vector<std::array<Coord, Dim>> ghostCoords;
296 auto ghostRef = [&](const std::array<Coord, Dim>& gc) -> Index {
297 auto it = ghostSlot.find(gc);
298 if (it != ghostSlot.end())
299 return it->second;
300 Index s = static_cast<Index>(ghostCoords.size());
301 ghostSlot.emplace(gc, s);
302 ghostCoords.push_back(gc);
303 return s;
304 };
305
306 start_.assign(static_cast<std::size_t>(n) + 1, 0);
307 for (Index i = 0; i < n; ++i) {
308 for (const FaceEntry& e : ent[static_cast<std::size_t>(i)]) {
309 if (e.rfId < 0) {
310 ref_.push_back(e.localRef);
311 w_.push_back(e.w);
312 diag_[static_cast<std::size_t>(i)] += e.w;
313 } else {
314 const RemoteFace& rf = rfaces[static_cast<std::size_t>(e.rfId)];
315 const int Lj = coverLv[static_cast<std::size_t>(e.rfId)];
316 if (Lj < 0)
317 continue; // no neighbour
318 if (Lj >= rf.Li) { // finer side is i: α at i's face
319 const double a = alpha(rf.iLo, rf.si, rf.axis, rf.dir);
320 const double ww = a * coeff(rf.si, Coord(Coord(1) << Lj), rf.axis);
321 ref_.push_back(d_->local().numLeaves() + ghostRef(rf.gc));
322 w_.push_back(ww);
323 diag_[static_cast<std::size_t>(i)] += ww;
324 } else { // finer side is each remote sub-neighbour: α at its face (−dir)
325 const Coord sj = Coord(rf.si >> 1);
326 const int nsub = 1 << (Dim - 1);
327 for (int k = 0; k < nsub; ++k) {
328 std::array<Coord, Dim> sc = subCoord(rf.gc, rf.axis, k, sj);
329 const double a =
330 alpha(fineLoGlobal(rf.iLo, rf.si, sj, rf.axis, rf.dir, k), sj, rf.axis, -rf.dir);
331 const double ww = a * coeff(rf.si, sj, rf.axis);
332 ref_.push_back(d_->local().numLeaves() + ghostRef(sc));
333 w_.push_back(ww);
334 diag_[static_cast<std::size_t>(i)] += ww;
335 }
336 }
337 }
338 }
339 start_[static_cast<std::size_t>(i) + 1] = static_cast<Index>(ref_.size());
340 }
341 ghostCoords_ = std::move(ghostCoords);
342 }
343
345 double cellWidth(int level, int axis) const {
346 return h0_[axis] * static_cast<double>(Index(1) << level);
347 }
348 double cellWidth(int level) const { return cellWidth(level, 0); }
349 double cellVol(int level) const {
350 const double f = static_cast<double>(Index(1) << level);
351 double v = 1.0;
352 for (int d = 0; d < Dim; ++d)
353 v *= h0_[d] * f;
354 return v;
355 }
357 double areaOf(Coord s, int axis) const {
358 double a = 1.0;
359 for (int d = 0; d < Dim; ++d)
360 if (d != axis)
361 a *= static_cast<double>(s) * h0_[d];
362 return a;
363 }
364 double areaOf(Coord s) const { return areaOf(s, 0); }
365 double coeff(Coord si, Coord sj, int axis) const {
366 double dist = 0.5 * (static_cast<double>(si) + static_cast<double>(sj)) * h0_[axis];
367 return areaOf(si < sj ? si : sj, axis) / dist;
368 }
369 double coeff(Coord si, Coord sj) const { return coeff(si, sj, 0); }
370
371 // tangential sub-coord for a finer 2:1 face (global, wrapped to the domain).
372 std::array<Coord, Dim> subCoord(const std::array<Coord, Dim>& gc, int axis, int k,
373 Coord sj) const {
374 std::array<Coord, Dim> q = gc;
375 int bit = 0;
376 for (int tt = 0; tt < Dim; ++tt) {
377 if (tt == axis)
378 continue;
379 const Coord off = ((k >> bit) & 1) ? sj : Coord(0);
380 long v = static_cast<long>(gc[tt]) + static_cast<long>(off);
381 const long gf = static_cast<long>(d_->globalFineSize()[tt]);
382 if (v >= gf)
383 v -= gf; // periodic tangential wrap (in-domain otherwise)
384 q[tt] = static_cast<Coord>(v);
385 ++bit;
386 }
387 return q;
388 }
389
390 DO* d_ = nullptr;
391 Vec<Dim> h0_ = detail::filledVec<Dim>(1.0);
392 Index n_ = 0;
393 std::vector<double> invVol_, diag_, w_;
394 std::vector<Index> start_, ref_; // face CSR (ref<n local, else ghost)
395 std::vector<std::array<Coord, Dim>> ghostCoords_; // dedup ghost cells (slot = ref−n)
396};
397
415template <int Dim, unsigned Bits = (Dim == 2 ? 32u : (Dim == 3 ? 21u : 16u))>
417 public:
419 using BO = typename DO::Octree;
420 using M = typename DO::M;
421
424 void build(const DO& finest) {
425 buildImpl(finest, [](const Vec<Dim>&, int) { return 1.0; }, false);
426 }
427
432 template <class OpenFn>
433 void build(const DO& finest, OpenFn&& openFn) {
434 buildImpl(finest, std::forward<OpenFn>(openFn), true);
435 }
436
437 private:
438 template <class OpenFn>
439 void buildImpl(const DO& finest, OpenFn&& openFn, bool hasOpen) {
440 hasOpen_ = hasOpen;
441 levels_.clear();
442 const IVec<Dim> g = finest.globalRootSize();
443 const unsigned lmax = finest.lmax();
444 const AmrGeometry<Dim> geo = finest.globalGeometry();
445 const auto per = finest.periodic();
446 MPI_Comm comm = finest.comm();
447
448 auto l0 = std::make_unique<Level>();
449 l0->d = finest; // copy (level 0 owned by the hierarchy)
450 levels_.push_back(std::move(l0));
451 for (;;) {
452 BO coarse = levels_.back()->d.local(); // copy
453 const Index before = coarse.numLeaves();
454 coarse.coarsenIf([](typename DO::Code, unsigned) { return true; });
455 if (coarse.numLeaves() == before)
456 break; // uniform root brick reached
457 auto lv = std::make_unique<Level>();
458 lv->d.init(g, lmax, geo, per, comm);
459 lv->d.local() = coarse; // same ORB block, coarsened local octree
460 levels_.push_back(std::move(lv));
461 }
462 // The coarsening above stops per-rank when this block reaches its root brick, so a
463 // rank owning shallow blocks builds FEWER levels than one owning deep refinement.
464 // Each level's op.init / vcycle drives a COLLECTIVE owner request/reply (coverLevels,
465 // coverValues over `comm`), so an unequal level count would mismatch the collectives
466 // and deadlock at np>1. Pad every rank up to the global max with repeats of its root
467 // brick: the extra levels are identity transfers (c2p maps a root cell to itself), a
468 // no-op in the V-cycle — and they exactly mirror what the whole-domain COMM_SELF
469 // reference does in its already-coarse regions, so the bit-exact WORLD==SELF check is
470 // preserved. (Allreduce on COMM_SELF is a no-op, leaving the serial path unchanged.)
471 {
472 int localLevels = static_cast<int>(levels_.size());
473 int globalLevels = localLevels;
474 MPI_Allreduce(&localLevels, &globalLevels, 1, MPI_INT, MPI_MAX, comm);
475 while (static_cast<int>(levels_.size()) < globalLevels) {
476 auto lv = std::make_unique<Level>();
477 lv->d.init(g, lmax, geo, per, comm);
478 lv->d.local() = levels_.back()->d.local(); // repeat the root brick (identity level)
479 levels_.push_back(std::move(lv));
480 }
481 }
482 for (auto& lv : levels_) {
483 lv->op.init(lv->d, openFn);
484 const Index n = lv->d.local().numLeaves();
485 lv->x.assign(static_cast<std::size_t>(n), 0.0);
486 lv->b.assign(static_cast<std::size_t>(n), 0.0);
487 lv->res.assign(static_cast<std::size_t>(n), 0.0);
488 }
489 // local fine→coarse maps (covering coarse leaf, same block).
490 for (std::size_t L = 0; L + 1 < levels_.size(); ++L) {
491 const BO& f = levels_[L]->d.local();
492 const BO& c = levels_[L + 1]->d.local();
493 const Index nf = f.numLeaves();
494 auto& c2p = levels_[L]->c2p;
495 c2p.assign(static_cast<std::size_t>(nf), -1);
496 for (Index i = 0; i < nf; ++i)
497 c2p[static_cast<std::size_t>(i)] = c.find(f.code(i));
498 }
499
500 // Bottom solver (openness-free only): a uniform DistributedMultigrid on the root
501 // grid, below the root brick. The coarsest graded level IS the uniform root brick
502 // (root cells at level lmax); its operator is L=∇² at spacing h0·2^lmax, the same
503 // operator the uniform MG's finest applies (DistributedPoisson is also L=∇²) on
504 // the same root grid — so the bottom solve of L e = res is inner.vcycle(e, res).
505 // With openness the uniform inner is inconsistent, so the coarsest is bottom-solved
506 // with Jacobi on its own (openness-carrying) operator instead.
507 if (!hasOpen_) {
508 AmrGeometry<Dim> ig = geo;
509 for (int d = 0; d < Dim; ++d) // root-cell width, per axis
510 ig.h0[d] = geo.h0[d] * static_cast<double>(Index(1) << lmax);
511 inner_ = std::make_unique<DistributedMultigrid<Dim, Bits>>();
512 inner_->build(g, ig, per, comm);
513 DO& coarsest = levels_.back()->d;
514 nCoarse_ = coarsest.local().numLeaves();
515 innerMap_.assign(static_cast<std::size_t>(nCoarse_), -1);
516 for (Index i = 0; i < nCoarse_; ++i)
517 innerMap_[static_cast<std::size_t>(i)] =
518 inner_->octree(0).findGlobalRoot(coarsest.globalRootOf(i));
519 }
520 }
521
522 public:
523 std::size_t numLevels() const { return levels_.size(); }
524 DistributedFvOperator<Dim, Bits>& op(std::size_t L = 0) { return levels_[L]->op; }
525 Index numLeaves(std::size_t L = 0) const { return levels_[L]->d.local().numLeaves(); }
526
530 void vcycle(std::vector<double>& x, const std::vector<double>& b, int pre = 2, int post = 2,
531 int innerCycles = 6, double omega = 0.8, std::size_t L = 0) {
532 auto& lv = *levels_[L];
533 if (L + 1 == levels_.size()) {
534 if (inner_)
535 bottomSolve(lv.op, x, b, innerCycles); // openness-free: chained uniform MG
536 else
537 lv.op.jacobi(x, b, bottomSweeps_, omega); // openness: Jacobi on the correct op
538 return;
539 }
540 lv.op.jacobi(x, b, pre, omega);
541 std::vector<double> res;
542 lv.op.residual(x, b, res);
543 // restrict residual → coarse rhs (local average over children).
544 auto& cl = *levels_[L + 1];
545 const Index nc = cl.d.local().numLeaves();
546 std::vector<double> cb(static_cast<std::size_t>(nc), 0.0),
547 cn(static_cast<std::size_t>(nc), 0.0);
548 const auto& c2p = lv.c2p;
549 const Index nf = lv.d.local().numLeaves();
550 for (Index i = 0; i < nf; ++i) {
551 Index p = c2p[static_cast<std::size_t>(i)];
552 if (p < 0)
553 continue;
554 cb[static_cast<std::size_t>(p)] += res[static_cast<std::size_t>(i)];
555 cn[static_cast<std::size_t>(p)] += 1.0;
556 }
557 for (Index p = 0; p < nc; ++p)
558 if (cn[static_cast<std::size_t>(p)] > 0.0)
559 cb[static_cast<std::size_t>(p)] /= cn[static_cast<std::size_t>(p)];
560 std::vector<double> cx(static_cast<std::size_t>(nc), 0.0);
561 vcycle(cx, cb, pre, post, innerCycles, omega, L + 1);
562 for (Index i = 0; i < nf; ++i) {
563 Index p = c2p[static_cast<std::size_t>(i)];
564 if (p >= 0)
565 x[static_cast<std::size_t>(i)] += cx[static_cast<std::size_t>(p)];
566 }
567 lv.op.jacobi(x, b, post, omega);
568 }
569
570 private:
571 // Solve L x = b on the uniform root brick via the inner uniform MG (correction
572 // scheme): res = b − Lx; solve L e = res with `cycles` inner V-cycles; x += e.
573 // Both operators are L = ∇² (same suite-wide sign), so no sign flip is needed.
574 // Index map coarsest→inner is by global root cell (identity in practice).
575 void bottomSolve(DistributedFvOperator<Dim, Bits>& op, std::vector<double>& x,
576 const std::vector<double>& b, int cycles) {
577 std::vector<double> res;
578 op.residual(x, b, res);
579 const Index ni = inner_->numLeaves(0);
580 std::vector<double> bi(static_cast<std::size_t>(ni), 0.0),
581 ei(static_cast<std::size_t>(ni), 0.0);
582 for (Index i = 0; i < nCoarse_; ++i) {
583 Index m = innerMap_[static_cast<std::size_t>(i)];
584 if (m >= 0)
585 bi[static_cast<std::size_t>(m)] = res[static_cast<std::size_t>(i)];
586 }
587 for (int c = 0; c < cycles; ++c)
588 inner_->vcycle(ei, bi);
589 for (Index i = 0; i < nCoarse_; ++i) {
590 Index m = innerMap_[static_cast<std::size_t>(i)];
591 if (m >= 0)
592 x[static_cast<std::size_t>(i)] += ei[static_cast<std::size_t>(m)];
593 }
594 }
595
596 struct Level {
597 DO d;
598 DistributedFvOperator<Dim, Bits> op;
599 std::vector<Index> c2p; // fine leaf → covering coarse leaf (local)
600 std::vector<double> x, b, res; // scratch
601 };
602 std::vector<std::unique_ptr<Level>> levels_;
603 std::unique_ptr<DistributedMultigrid<Dim, Bits>> inner_; // uniform bottom solver (openness-free)
604 std::vector<Index> innerMap_; // coarsest leaf → inner finest leaf
605 Index nCoarse_ = 0;
606 bool hasOpen_ = false;
607 int bottomSweeps_ = 400; // Jacobi sweeps on the coarsest op when openness present
608};
609
610} // namespace peclet::amr
611
612#endif // PECLET_AMR_DISTRIBUTED_FV_HPP
void jacobi(std::vector< double > &u, const std::vector< double > &rhs, int sweeps, double omega=0.8) const
sweeps weighted-Jacobi relaxations of L u = rhs (point solve u_i ← (Σ w u_nb − V_i rhs_i)/Σ w).
void init(DO &d, OpenFn &&openFn)
Build with cut-cell openness: w_f = α_f · A_f/d_f, where α_f = openFn(facecentroid,...
void init(DO &d)
Build the consistent face stencil, openness-free (w_f = A_f/d_f).
double residualNorm(const std::vector< double > &u, const std::vector< double > &rhs) const
double residual(const std::vector< double > &u, const std::vector< double > &rhs, std::vector< double > &res) const
res = rhs − L u; returns the global L2 norm (volume-weighted).
DistributedOctree< Dim, Bits > DO
void apply(const std::vector< double > &u, std::vector< double > &Lu) const
Lu = L u (consistent conservative FV Laplacian, negative-definite).
std::vector< int > coverLevels(const std::vector< std::array< Coord, Dim > > &coords) const
For each global fine coord (already wrapped into the domain), the level of the covering leaf on its o...
const Vec< Dim > & h0() const
Phase 3: the FINEST spacing per axis (docs/amr_anisotropic.md §2).
const AmrGeometry< Dim > & globalGeometry() const
const IVec< Dim > & blockFineOrigin() const
Geometric-multigrid V-cycle on a graded distributed octree, built on DistributedFvOperator.
Index numLeaves(std::size_t L=0) const
DistributedFvOperator< Dim, Bits > & op(std::size_t L=0)
void build(const DO &finest, OpenFn &&openFn)
Build with cut-cell openness openFn on every level (each level re-samples the geometry at its own fac...
void vcycle(std::vector< double > &x, const std::vector< double > &b, int pre=2, int post=2, int innerCycles=6, double omega=0.8, std::size_t L=0)
One V-cycle of L u = rhs on level L (default finest), correction scheme.
void build(const DO &finest)
Build the hierarchy from an already-graded, 2:1-balanced finest octree (openness-free; the coarsest i...
DistributedOctree< Dim, Bits > DO
Vec< Dim > filledVec(Real v)
Vec<Dim> with every component set to v (C++ has no aggregate-fill initializer).
World-space placement of a block-local octree: fine coordinate (0,..,0) sits at origin,...