53 init(d, [](
const Vec<Dim>&,
int) {
return 1.0; });
62 template <
class OpenFn>
66 const auto& t = d.
local();
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);
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);
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);
86 expandFaces(ent, rfaces, coverLv, alpha);
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)];
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)];
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);
106 Lu[
static_cast<std::size_t
>(i)] = invVol_[
static_cast<std::size_t
>(i)] * acc;
111 double residual(
const std::vector<double>& u,
const std::vector<double>& rhs,
112 std::vector<double>& res)
const {
113 std::vector<double> lu;
115 res.assign(
static_cast<std::size_t
>(n_), 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)];
123 MPI_Allreduce(&s, &g, 1, MPI_DOUBLE, MPI_SUM, d_->comm());
127 double residualNorm(
const std::vector<double>& u,
const std::vector<double>& rhs)
const {
128 std::vector<double> res;
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) {
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)];
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;
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)];
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)];
171 std::array<Coord, Dim> gc{};
176 std::array<long, Dim> iLo{};
181 template <
class OpenFn>
186 double operator()(
const std::array<long, Dim>& flo,
Coord s,
int axis,
int faceDir)
const {
188 long plane = flo[axis] + (faceDir > 0 ?
static_cast<long>(s) : 0L);
189 for (
int d2 = 0; d2 < Dim; ++d2)
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);
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]);
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);
215 for (
int t = 0; t < Dim; ++t) {
218 flo[t] = clo[t] + (((k >> bit) & 1) ?
static_cast<long>(sj) : 0L);
221 for (
int d2 = 0; d2 < Dim; ++d2) {
222 long gf =
static_cast<long>(d_->globalFineSize()[d2]);
225 else if (flo[d2] >= gf)
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) {
236 const auto& t = d.local();
237 const int Li =
static_cast<int>(t.level(i));
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);
248 Index j = fi.localNb;
251 const int Lj =
static_cast<int>(t.level(j));
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});
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);
264 for (
int tt = 0; tt < Dim; ++tt) {
267 const Coord off = ((k >> bit) & 1) ? sj :
Coord(0);
268 q[tt] =
static_cast<Coord>(lo[tt] + off);
271 Index jj = t.find(M::encode(q).code());
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});
279 e.rfId =
static_cast<int>(rfaces.size());
281 RemoteFace rf{fi.gc, Li, si, axis, dir, {}};
282 rf.iLo = globalLo(bfo, lo);
283 rfaces.push_back(rf);
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())
300 Index s =
static_cast<Index
>(ghostCoords.size());
301 ghostSlot.emplace(gc, s);
302 ghostCoords.push_back(gc);
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)]) {
310 ref_.push_back(e.localRef);
312 diag_[
static_cast<std::size_t
>(i)] += e.w;
314 const RemoteFace& rf = rfaces[
static_cast<std::size_t
>(e.rfId)];
315 const int Lj = coverLv[
static_cast<std::size_t
>(e.rfId)];
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));
323 diag_[
static_cast<std::size_t
>(i)] += ww;
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);
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));
334 diag_[
static_cast<std::size_t
>(i)] += ww;
339 start_[
static_cast<std::size_t
>(i) + 1] =
static_cast<Index
>(ref_.size());
341 ghostCoords_ = std::move(ghostCoords);
345 double cellWidth(
int level,
int axis)
const {
346 return h0_[axis] *
static_cast<double>(Index(1) << level);
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);
352 for (
int d = 0; d < Dim; ++d)
357 double areaOf(
Coord s,
int axis)
const {
359 for (
int d = 0; d < Dim; ++d)
361 a *=
static_cast<double>(s) * h0_[d];
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;
369 double coeff(
Coord si,
Coord sj)
const {
return coeff(si, sj, 0); }
372 std::array<Coord, Dim> subCoord(
const std::array<Coord, Dim>& gc,
int axis,
int k,
374 std::array<Coord, Dim> q = gc;
376 for (
int tt = 0; tt < Dim; ++tt) {
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]);
384 q[tt] =
static_cast<Coord>(v);
393 std::vector<double> invVol_, diag_, w_;
394 std::vector<Index> start_, ref_;
395 std::vector<std::array<Coord, Dim>> ghostCoords_;
425 buildImpl(finest, [](
const Vec<Dim>&,
int) {
return 1.0; },
false);
432 template <
class OpenFn>
433 void build(
const DO& finest, OpenFn&& openFn) {
434 buildImpl(finest, std::forward<OpenFn>(openFn),
true);
438 template <
class OpenFn>
439 void buildImpl(
const DO& finest, OpenFn&& openFn,
bool hasOpen) {
442 const IVec<Dim> g = finest.globalRootSize();
443 const unsigned lmax = finest.lmax();
445 const auto per = finest.periodic();
446 MPI_Comm comm = finest.comm();
448 auto l0 = std::make_unique<Level>();
450 levels_.push_back(std::move(l0));
452 BO coarse = levels_.back()->d.local();
453 const Index before = coarse.numLeaves();
454 coarse.coarsenIf([](
typename DO::Code,
unsigned) {
return true; });
455 if (coarse.numLeaves() == before)
457 auto lv = std::make_unique<Level>();
458 lv->d.init(g, lmax, geo, per, comm);
459 lv->d.local() = coarse;
460 levels_.push_back(std::move(lv));
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();
479 levels_.push_back(std::move(lv));
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);
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));
508 AmrGeometry<Dim> ig = geo;
509 for (
int d = 0; d < Dim; ++d)
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));
523 std::size_t
numLevels()
const {
return levels_.size(); }
525 Index
numLeaves(std::size_t L = 0)
const {
return levels_[L]->d.local().numLeaves(); }
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()) {
535 bottomSolve(lv.op, x, b, innerCycles);
537 lv.op.jacobi(x, b, bottomSweeps_, omega);
540 lv.op.jacobi(x, b, pre, omega);
541 std::vector<double> res;
542 lv.op.residual(x, b, res);
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)];
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;
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)];
565 x[
static_cast<std::size_t
>(i)] += cx[
static_cast<std::size_t
>(p)];
567 lv.op.jacobi(x, b, post, omega);
576 const std::vector<double>& b,
int cycles) {
577 std::vector<double> 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)];
585 bi[
static_cast<std::size_t
>(m)] = res[
static_cast<std::size_t
>(i)];
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)];
592 x[
static_cast<std::size_t
>(i)] += ei[
static_cast<std::size_t
>(m)];
598 DistributedFvOperator<Dim, Bits> op;
599 std::vector<Index> c2p;
600 std::vector<double> x, b, res;
602 std::vector<std::unique_ptr<Level>> levels_;
603 std::unique_ptr<DistributedMultigrid<Dim, Bits>> inner_;
604 std::vector<Index> innerMap_;
606 bool hasOpen_ =
false;
607 int bottomSweeps_ = 400;