31#ifndef PECLET_AMR_FLOW_ORACLE_HPP
32#define PECLET_AMR_FLOW_ORACLE_HPP
52#include "peclet/core/common/types.hpp"
59template <
unsigned Bits = 21u>
64 void init(
const Octree& t, Real h0, Vec<3> origin = Vec<3>{}) {
67 void init(
const Octree& t,
const Vec<3>& h0, Vec<3> origin = Vec<3>{}) {
75 void setDt(
double dt) { dt_ = dt; }
76 void setBodyForce(
double fx,
double fy,
double fz) { f_ = {fx, fy, fz}; }
111 ghostProjReq_ = on ? 1 : 0;
112 gpMatrixOrder_ = matrixOrder;
113 gpRhsOrder_ = rhsOrder;
133 double advectTerm(
int comp, Index i)
const {
return advectHO(comp, i); }
136 template <
class SdfFn>
138 mom_.init(*t_, h0_, origin_);
140 for (
int d = 0; d < 3; ++d)
141 betaA[d] = mu_ / (h0_[d] * h0_[d]);
142 mom_.build(sdfFn, rho_ / dt_, betaA);
143 pres_.init(*t_, h0_);
144 pres_.setOrigin(origin_);
145 presMG_.build(*t_, h0_);
149 ghostProj_ = (ghostProjReq_ != 0);
150 ghostSampled_ = ghostProj_ && (ghostSampledReq_ == 1);
151 if (ghostProj_ && !ghostSampled_) {
153 gpOv_ =
buildGhostOverlay(*t_, pres_, mom_.sdfCRaw(), gpMatrixOrder_, gpRhsOrder_, &viol);
155 if (ghostProjReq_ == 1)
156 throw std::runtime_error(
157 "amr ghost projection: an overlay row's ±2 closure reach crosses a 2:1 level "
158 "boundary — widen the refineToSdf band margin");
161 "peclet::core AmrFlow(oracle): AUTO scheme fell back to the aperture projection "
162 "(the finest band is too thin for the ghost overlay).\n");
171 buildMomSeamDelta(gpOvS_, *t_, pres_, mom_, sdfFn, origin_, rho_ / dt_, mu_, &momSkipped);
174 "[peclet.amr] mom seam delta: %ld raw-regular/virtually-ghost rows "
175 "skipped (rung-1 scope)\n",
183 auto sdfW = [&](
const Vec<3>& p) {
return sdfFn(p); };
188 pres_.buildOpenness(binFn);
189 presMG_.setOpenness(binFn);
192 pres_.buildOpenness(binFn);
193 presMG_.setOpenness(binFn);
198 maskC_.assign(
static_cast<std::size_t
>(t_->numLeaves()), 0.0);
199 for (Index i = 0; i < t_->numLeaves(); ++i)
200 maskC_[
static_cast<std::size_t
>(i)] =
201 (mom_.isFluid(i) && !(!gpPocket_.empty() && gpPocket_[
static_cast<std::size_t
>(i)]))
204 const GhostOverlay& rows = ghostSampled_ ? gpOvS_.base : gpOv_;
205 for (Index r = 0; r < rows.
n; ++r)
206 if (!rows.
coupled[
static_cast<std::size_t
>(r)])
207 maskC_[
static_cast<std::size_t
>(rows.
cell[
static_cast<std::size_t
>(r)])] = 0.0;
210 pres_.buildOpenness([&](
const Vec<3>& fc,
int axis) {
return faceFrac(sdfFn, fc, axis); });
211 presMG_.setOpenness([&](
const Vec<3>& fc,
int axis) {
return faceFrac(sdfFn, fc, axis); });
222 const Index nl = t_->numLeaves();
223 cfRegular_.assign(
static_cast<std::size_t
>(nl), 0);
224 for (Index i = 0; i < nl; ++i)
225 cfRegular_[
static_cast<std::size_t
>(i)] = (mom_.isFluid(i) && !mom_.isCut(i)) ? 1 : 0;
227 auto regularOk = [&](Index s) {
228 if (s < 0 || s >=
static_cast<Index
>(cfRegular_.size()))
229 throw std::logic_error(
"peclet::amr oracle: C/F regular flag queried outside [0, n)");
230 return cfRegular_[
static_cast<std::size_t
>(s)] != 0;
232 cfCutFaces_ =
countCfCutFaces(pres_, *t_, regularOk, [&](Index s) {
return mom_.isFluid(s); });
234 auto fluidOk = [&](Index j) {
return mom_.isFluid(j); };
235 auto rowRegular = [&](Index i) {
return mom_.isFluid(i) && !mom_.isCut(i); };
236 cfMom_ =
buildCfLapDelta(mom_.lap(), *t_, mu_, rowRegular, fluidOk, cfScheme_);
239 cfUf_ =
buildCfUfDelta(pres_, *t_, regularOk, fluidOk, cfScheme_);
246 const Index n = t_->numLeaves();
247 for (
int c = 0; c < 3; ++c)
248 u_[c].assign(
static_cast<std::size_t
>(n), 0.0);
249 phi_.assign(
static_cast<std::size_t
>(n), 0.0);
250 p_.assign(
static_cast<std::size_t
>(n), 0.0);
253 faceStart_.assign(
static_cast<std::size_t
>(n) + 1, 0);
254 for (Index i = 0; i < n; ++i) {
256 pres_.forEachFaceFull(i, [&](Index,
int,
int,
double,
double,
double) { ++cnt; });
257 faceStart_[
static_cast<std::size_t
>(i) + 1] = faceStart_[
static_cast<std::size_t
>(i)] + cnt;
259 uf_.assign(
static_cast<std::size_t
>(faceStart_[
static_cast<std::size_t
>(n)]), 0.0);
260 faceFieldBuilt_ =
false;
263 const std::vector<double>&
velocity(
int c)
const {
return u_[c]; }
265 bool isFluid(Index i)
const {
return mom_.isFluid(i); }
266 bool isCut(Index i)
const {
return mom_.isCut(i); }
272 adaptOldT_ = std::make_unique<Octree>(*t_);
276 template <
class SdfFn>
279 throw std::runtime_error(
"oracle::AmrFlow::finishAdapt called without beginAdapt");
280 std::array<std::vector<double>, 3> nu;
281 for (
int c = 0; c < 3; ++c)
282 nu[
static_cast<std::size_t
>(c)] =
283 transferField(*adaptOldT_, adaptU_[
static_cast<std::size_t
>(c)], *t_,
true);
284 std::vector<double> np =
transferField(*adaptOldT_, adaptP_, *t_,
true);
286 for (Index i = 0; i < t_->numLeaves(); ++i) {
287 const bool fl = mom_.isFluid(i);
288 for (
int c = 0; c < 3; ++c)
289 u_[
static_cast<std::size_t
>(c)][
static_cast<std::size_t
>(i)] =
290 fl ? nu[
static_cast<std::size_t
>(c)][
static_cast<std::size_t
>(i)] : 0.0;
291 p_[
static_cast<std::size_t
>(i)] = fl ? np[
static_cast<std::size_t
>(i)] : 0.0;
298 void step(
int momSweeps = 200,
int presIters = 60,
int presSweeps = 4) {
299 const Index n = t_->numLeaves();
304 std::array<std::vector<double>, 3> adv;
307 mom_.buildAdvectionFou(u_, rho_, uf_, faceStart_,
309 for (
int c = 0; c < 3; ++c) {
310 adv[c].assign(
static_cast<std::size_t
>(n), 0.0);
311 for (Index i = 0; i < n; ++i)
312 if (mom_.isFluid(i)) {
313 double term = rho_ * advectHO(c, i);
315 term -= mom_.fouApply(i, u_[c]);
316 adv[c][
static_cast<std::size_t
>(i)] = term;
322 for (
int c = 0; c < 3; ++c) {
325 std::vector<double> cfm;
327 cfm.assign(
static_cast<std::size_t
>(n), 0.0);
332 if (ghostSampled_ && !gpsMomDelta_.start.empty()) {
334 cfm.assign(
static_cast<std::size_t
>(n), 0.0);
337 std::vector<double> src(
static_cast<std::size_t
>(n), 0.0);
338 for (Index i = 0; i < n; ++i)
339 if (mom_.isFluid(i)) {
341 double s = (rho_ / dt_) * u_[c][
static_cast<std::size_t
>(i)] + f_[c] - gradP(p_, i, c);
343 s -= adv[c][
static_cast<std::size_t
>(i)];
345 s += cfm[
static_cast<std::size_t
>(i)];
346 src[
static_cast<std::size_t
>(i)] = s;
348 std::vector<double> b = mom_.makeRhs(src, 0.0);
349 mom_.gaussSeidel(u_[c], b, momSweeps);
351 project(presIters, presSweeps);
356 void project(
int presIters = 60,
int presSweeps = 4) {
357 const Index n = t_->numLeaves();
358 std::vector<double> div(
static_cast<std::size_t
>(n), 0.0);
359 for (Index i = 0; i < n; ++i)
361 div[
static_cast<std::size_t
>(i)] =
divergence(u_, i);
371 std::fill(phi_.begin(), phi_.end(), 0.0);
376 solveGhostBiCGStab(phi_, div, presIters);
382 for (
int it = 0; it < presIters; ++it)
383 presMG_.vcycle(0, phi_, div);
389 for (
int c = 0; c < 3; ++c)
390 for (Index i = 0; i < n; ++i)
392 u_[c][
static_cast<std::size_t
>(i)] -= gradP(phi_, i, c);
397 for (Index i = 0; i < n; ++i)
399 p_[
static_cast<std::size_t
>(i)] += (rho_ / dt_) * phi_[
static_cast<std::size_t
>(i)] -
400 mu_ * div[
static_cast<std::size_t
>(i)];
406 double divergence(
const std::array<std::vector<double>, 3>& vel, Index i)
const {
408 pres_.forEachFaceFull(i, [&](Index j,
int axis,
int dir,
double area,
double,
double alpha) {
409 double ui = vel[axis][
static_cast<std::size_t
>(i)];
410 double uj = vel[axis][
static_cast<std::size_t
>(j)];
411 d += alpha * area * dir * 0.5 * (ui + uj);
413 return d / pres_.cellVolume(i);
416 double divNormL2(
const std::array<std::vector<double>, 3>& vel)
const {
418 const Index n = t_->numLeaves();
419 for (Index i = 0; i < n; ++i)
420 if (mom_.isFluid(i)) {
431 const Index n = t_->numLeaves();
432 for (Index i = 0; i < n; ++i)
433 if (mom_.isFluid(i) && !mom_.isCut(i)) {
450 const Index n = t_->numLeaves();
451 for (Index i = 0; i < n; ++i) {
452 Index s = faceStart_[
static_cast<std::size_t
>(i)];
453 if (!mom_.isFluid(i) || mom_.isCut(i)) {
454 pres_.forEachFaceFull(i, [&](Index,
int,
int,
double,
double,
double) { ++s; });
458 pres_.forEachFaceFull(i, [&](Index,
int,
int dir,
double area,
double,
double alpha) {
459 d += alpha * area * dir * uf_[
static_cast<std::size_t
>(s++)];
461 d /= pres_.cellVolume(i);
464 return std::sqrt(tot);
477 const Index n = t_->numLeaves();
478 std::vector<double> dcell(
static_cast<std::size_t
>(n), 0.0);
480 const std::size_t nSlots =
481 cfUf_.vel.start.empty() ? 0u : cfUf_.vel.start.size() - 1;
482 std::vector<double> duf(nSlots, 0.0);
484 double err = 0.0, ref = 0.0;
485 for (Index i = 0; i < n; ++i) {
487 Index s = faceStart_[
static_cast<std::size_t
>(i)];
488 pres_.forEachFaceFull(i, [&](Index,
int,
int dir,
double area,
double,
double alpha) {
489 if (
static_cast<std::size_t
>(s) < nSlots)
490 d += alpha * area * dir * duf[
static_cast<std::size_t
>(s)];
493 d /= pres_.cellVolume(i);
494 const double r = d - dcell[
static_cast<std::size_t
>(i)];
496 ref += dcell[
static_cast<std::size_t
>(i)] * dcell[
static_cast<std::size_t
>(i)];
498 return {std::sqrt(err), std::sqrt(ref)};
510 const Index n = t_->numLeaves();
511 for (Index i = 0; i < n; ++i) {
512 Index s = faceStart_[
static_cast<std::size_t
>(i)];
513 pres_.forEachFaceFull(i, [&](Index j,
int axis,
int dir,
double,
double dist,
double) {
515 0.5 * (u_[axis][
static_cast<std::size_t
>(i)] + u_[axis][
static_cast<std::size_t
>(j)]);
519 ? (phi_[
static_cast<std::size_t
>(j)] - phi_[
static_cast<std::size_t
>(i)]) / dist
520 : (phi_[
static_cast<std::size_t
>(i)] - phi_[
static_cast<std::size_t
>(j)]) / dist;
521 uf_[
static_cast<std::size_t
>(s++)] = uface - gphi;
532 faceFieldBuilt_ =
true;
540 const Index n = t_->numLeaves();
541 for (Index i = 0; i < n; ++i) {
542 if (!mom_.isFluid(i))
544 Index s = faceStart_[
static_cast<std::size_t
>(i)];
546 pres_.forEachFaceFull(i, [&](Index,
int,
int dir,
double area,
double,
double alpha) {
547 d += alpha * area * dir * uf_[
static_cast<std::size_t
>(s++)];
549 d /= pres_.cellVolume(i);
552 return std::sqrt(tot);
557 const std::vector<double>&
faceField()
const {
return uf_; }
558 const std::vector<Index>&
faceStart()
const {
return faceStart_; }
562 static double koren(
double up_m1,
double up,
double down,
double vel) {
563 const double num = up - up_m1, den = down - up;
564 double r = (std::fabs(den) < 1e-10) ? 0.0 : num / den;
565 double psi = std::fmax(0.0, std::fmin(2.0 * r, std::fmin((1.0 + 2.0 * r) / 3.0, 2.0)));
566 return vel * (up + 0.5 * psi * (down - up));
568 static double tvd(
double LL,
double L,
double R,
double RR,
double vel) {
569 return (vel > 0.0) ? koren(LL, L, R, vel) : koren(RR, R, L, vel);
572 double hoFace(
double upup,
double up,
double down)
const {
583 double advectHO(
int comp, Index i)
const {
585 Index s = faceFieldBuilt_ ? faceStart_[
static_cast<std::size_t
>(i)] : 0;
586 pres_.
forEachFaceFull(i, [&](Index j,
int axis,
int dir,
double area,
double,
double) {
592 const double uface = faceFieldBuilt_ ? uf_[
static_cast<std::size_t
>(s)]
593 : 0.5 * (u_[axis][static_cast<std::size_t>(i)] +
594 u_[axis][static_cast<std::size_t>(j)]);
596 if (!mom_.isFluid(j))
598 double velOut = dir * uface;
599 Index up = (velOut > 0.0) ? i : j, down = (velOut > 0.0) ? j : i;
600 int upDir = (up == i) ? -dir : dir;
602 double phiUp = u_[comp][
static_cast<std::size_t
>(up)];
604 (upup >= 0 && mom_.isFluid(upup)) ? u_[comp][
static_cast<std::size_t
>(upup)] : phiUp;
605 double phiFace = hoFace(phiUpUp, phiUp, u_[comp][
static_cast<std::size_t
>(down)]);
606 out += area * velOut * phiFace;
608 return out / pres_.cellVolume(i);
614 void gpProject(std::vector<double>& v)
const {
615 const Index n = t_->numLeaves();
616 double su = 0.0, sv = 0.0;
617 for (Index i = 0; i < n; ++i) {
618 const std::size_t s =
static_cast<std::size_t
>(i);
620 if (maskC_[s] > 0.0) {
621 const double V = pres_.cellVolume(i);
626 const double m = (sv > 0.0) ? su / sv : 0.0;
627 for (Index i = 0; i < n; ++i)
628 v[
static_cast<std::size_t
>(i)] -= maskC_[
static_cast<std::size_t
>(i)] * m;
636 void solveGhostBiCGStab(std::vector<double>& x,
const std::vector<double>& b,
int maxIters,
637 double tol = 1e-10) {
638 const Index n = t_->numLeaves();
639 const std::size_t ns =
static_cast<std::size_t
>(n);
640 auto applyA = [&](
const std::vector<double>& v, std::vector<double>& y) {
641 pres_.applyLaplacian(v, y);
648 auto prec = [&](
const std::vector<double>& r, std::vector<double>& z) {
649 std::fill(z.begin(), z.end(), 0.0);
650 presMG_.vcycle(0, z, r);
651 presMG_.vcycle(0, z, r);
654 auto dot = [&](
const std::vector<double>& a,
const std::vector<double>& c) {
656 for (std::size_t i = 0; i < ns; ++i)
660 std::vector<double> r(ns), rhat(ns), p(ns, 0.0), phat(ns), v(ns, 0.0), s(ns), shat(ns), t(ns);
662 for (std::size_t i = 0; i < ns; ++i)
666 const double res0 = std::sqrt(dot(r, r));
669 double rho = 1, alpha = 1, omega = 1, best = res0;
671 for (
int it = 0; it < maxIters; ++it) {
672 const double rhoNew = dot(rhat, r);
675 const double beta = (rhoNew / rho) * (alpha / omega);
676 for (std::size_t i = 0; i < ns; ++i)
677 p[i] = r[i] + beta * (p[i] - omega * v[i]);
680 const double rhatV = dot(rhat, v);
683 alpha = rhoNew / rhatV;
684 for (std::size_t i = 0; i < ns; ++i)
685 s[i] = r[i] - alpha * v[i];
686 double snorm = std::sqrt(dot(s, s));
687 if (snorm <= tol * res0) {
688 for (std::size_t i = 0; i < ns; ++i)
689 x[i] += alpha * phat[i];
694 const double tt = dot(t, t);
695 omega = (tt != 0.0) ? dot(t, s) / tt : 0.0;
696 for (std::size_t i = 0; i < ns; ++i) {
697 x[i] += alpha * phat[i] + omega * shat[i];
698 r[i] = s[i] - omega * t[i];
700 const double rnorm = std::sqrt(dot(r, r));
701 if (rnorm <= tol * res0)
703 if (rnorm < 0.999 * best) {
706 }
else if (++noImprove >= 6) {
720 double gradP(
const std::vector<double>& fld, Index i,
int c)
const {
721 if (ghostGrad_ && mom_.isCut(i)) {
726 const Index r = gpOvS_.rowOf[
static_cast<std::size_t
>(i)];
728 return gpsDirGrad(gpOvS_, r, fld, c, 1.0 / pres_.cellWidth(i));
730 return gradOfDir(fld, i, c);
732 double g = gradOf(fld, i, c);
734 if (cfScheme_ !=
CfScheme::standard && !cfGrad_[
static_cast<std::size_t
>(c)].start.empty()) {
735 const CfCsr& cs = cfGrad_[
static_cast<std::size_t
>(c)];
736 for (Index k = cs.start[
static_cast<std::size_t
>(i)];
737 k < cs.start[
static_cast<std::size_t
>(i) + 1]; ++k)
738 g += cs.coef[
static_cast<std::size_t
>(k)] *
739 fld[
static_cast<std::size_t
>(cs.slot[
static_cast<std::size_t
>(k)])];
750 double gradOfDir(
const std::vector<double>& fld, Index i,
int c)
const {
751 const double h = pres_.cellWidth(i);
752 auto F = [&](Index j) {
return fld[
static_cast<std::size_t
>(j)]; };
754 auto ok = [&](Index j) {
755 return j >= 0 && mom_.isFluid(j) && t_->level(j) == t_->level(i) &&
756 !(!gpPocket_.empty() && gpPocket_[
static_cast<std::size_t
>(j)]);
758 const Index jp = pres_.periodicNeighbor(i, c, +1);
759 const Index jm = pres_.periodicNeighbor(i, c, -1);
760 const bool ap = ok(jp), am = ok(jm);
762 return (F(jp) - F(jm)) / (2.0 * h);
764 const Index jpp = pres_.periodicNeighbor(jp, c, +1);
765 return ok(jpp) ? (-3.0 * F(i) + 4.0 * F(jp) - F(jpp)) / (2.0 * h) : (F(jp) - F(i)) / h;
768 const Index jmm = pres_.periodicNeighbor(jm, c, -1);
769 return ok(jmm) ? (3.0 * F(i) - 4.0 * F(jm) + F(jmm)) / (2.0 * h) : (F(i) - F(jm)) / h;
781 double gradOf(
const std::vector<double>& fld, Index i,
int c)
const {
782 const double pi = fld[
static_cast<std::size_t
>(i)];
783 double gp = 0, gm = 0;
787 pres_.forEachFaceFull(i, [&](Index j,
int axis,
int dir,
double,
double dist,
double alpha) {
788 if (axis != c || alpha <= 1e-12)
790 double g = (dir > 0) ? (fld[
static_cast<std::size_t
>(j)] - pi) / dist
791 : (pi - fld[
static_cast<std::size_t
>(j)]) / dist;
800 double gpa = np ? gp / np : 0.0, gma = nm ? gm / nm : 0.0;
801 return 0.5 * (gpa + gma);
811 static double triFrac(
double a,
double b,
double c) {
812 const bool pa = a >= 0.0, pb = b >= 0.0, pc = c >= 0.0;
813 const int np = (pa ? 1 : 0) + (pb ? 1 : 0) + (pc ? 1 : 0);
833 const double den = (x - y) * (x - z);
834 return den > 1e-300 ? (x * x) / den : 1.0;
849 const double den = (x - y) * (x - z);
850 return 1.0 - (den > 1e-300 ? (x * x) / den : 1.0);
852 template <
class SdfFn>
853 double faceFrac(SdfFn&& sdfFn,
const Vec<3>& fc,
int axis)
const {
854 if (apertureOrder_ >= 2) {
855 if (sdfFn(fc) <= 0.0)
857 const int t1 = (axis + 1) % 3, t2 = (axis + 2) % 3;
858 const double e1 = 0.5 * h0_[t1], e2 = 0.5 * h0_[t2];
859 auto at = [&](
double d1,
double d2) {
865 const double c00 = at(-e1, -e2), c10 = at(e1, -e2), c11 = at(e1, e2), c01 = at(-e1, e2);
866 const double cc = sdfFn(fc);
867 const double frac = 0.25 * (triFrac(c00, c10, cc) + triFrac(c10, c11, cc) +
868 triFrac(c11, c01, cc) + triFrac(c01, c00, cc));
869 return frac < 1e-3 ? 0.0 : (frac > 1.0 - 1e-12 ? 1.0 : frac);
871 double sd = sdfFn(fc);
875 for (
int d = 0; d < 3; ++d) {
876 Vec<3> pp = fc, pm = fc;
879 g[d] = (sdfFn(pp) - sdfFn(pm)) / (2.0 * h0_[d]);
881 double gmag = std::sqrt(g[0] * g[0] + g[1] * g[1] + g[2] * g[2]);
884 int t1 = (axis + 1) % 3, t2 = (axis + 2) % 3;
885 double denom = (std::fabs(g[t1]) * h0_[t1] + std::fabs(g[t2]) * h0_[t2]) / gmag;
888 double frac = 0.5 + sd / denom;
889 return frac < 0.0 ? 0.0 : (frac > 1.0 ? 1.0 : frac);
892 const Octree* t_ =
nullptr;
895 double rho_ = 1.0, mu_ = 1.0, dt_ = 1e6;
897 bool ghostGrad_ =
true;
898 bool ghostProj_ =
false;
899 int8_t ghostProjReq_ = -1;
901 int apertureOrder_ = 2;
902 int gpMatrixOrder_ = 2, gpRhsOrder_ = 2;
905 bool ghostSampled_ =
false;
906 int8_t ghostSampledReq_ = 0;
907 GhostOverlaySampled gpOvS_;
909 std::vector<double> maskC_;
910 std::vector<char> gpPocket_;
926 std::vector<char> cfRegular_;
927 Index cfCutFaces_ = 0;
929 std::array<CfCsr, 3> cfGrad_;
931 bool implicitFou_ =
true;
934 AmrCutCell<Bits> mom_;
935 AmrPoisson<3, Bits> pres_;
936 AmrMultigrid<3, Bits> presMG_;
937 std::array<std::vector<double>, 3> u_;
938 std::vector<double> phi_;
939 std::vector<double> p_;
946 std::vector<Index> faceStart_;
947 std::vector<double> uf_;
948 std::unique_ptr<Octree> adaptOldT_;
949 std::array<std::vector<double>, 3> adaptU_;
950 std::vector<double> adaptP_;
951 bool faceFieldBuilt_ =
void forEachFaceFull(Index i, Fn &&fn) const
Like forEachFaceNeighbor but exposes geometry for a consistent FV divergence/gradient: fn(neighbour,...
Index periodicNeighbor(Index i, int axis, int dir) const
Periodic face neighbour leaf (covering the cell just across the face).
Per-block adaptive octree over block-local Morton codes.
void project(int presIters=60, int presSweeps=4)
Pressure projection of the current velocity in place: solve ∇²φ = ∇·u and correct u -= ∇φ.
std::array< std::vector< double >, 3 > & velocityRef()
void setCfScheme(int scheme)
Coarse/fine (2:1) interface scheme (cf_scheme.hpp): 0 = standard two-point flux (default,...
void setAdvection(bool on)
const std::vector< Index > & faceStart() const
void setBodyForce(double fx, double fy, double fz)
double advectTerm(int comp, Index i) const
Conservative Koren-TVD advection term ∇·(u u_comp) at leaf i (physical units; the explicit momentum a...
double divNormFace() const
L2 norm of the divergence of the FACE field uf_ (the div-free flux).
void setGhostProjection(bool on, int matrixOrder=2, int rhsOrder=2)
bool isCut(Index i) const
BlockOctree< 3, Bits > Octree
void setGhostGradient(bool on)
Directional ghost cell-gradient for the −∇pⁿ predictor and the projection's cell correction (the AMR ...
bool isFluid(Index i) const
void setImplicitAdvection(bool on)
Implicit-FOU deferred-correction advection (default ON): the first-order-upwind part is solved implic...
void init(const Octree &t, Real h0, Vec< 3 > origin=Vec< 3 >{})
void setDensity(double rho)
void setGhostSampled(bool on)
SAMPLED ghost projection: the mixed-level cut-band prototype (the D1 machinery of docs/amr_mixed_leve...
void setViscosity(double mu)
double divNormFaceRegular() const
‖D_std(uf)‖₂ over REGULAR fluid rows only.
void setAdvectionScheme(int s)
High-order advection scheme: 0 = second-order upwind (SOU, default), 1 = Koren TVD.
void init(const Octree &t, const Vec< 3 > &h0, Vec< 3 > origin=Vec< 3 >{})
std::pair< double, double > cfFluxIdentity() const
Rule (I) of docs/amr_cf_flux_gate.md §4, measured (§6.6): the advecting face field's C/F delta and th...
void step(int momSweeps=200, int presIters=60, int presSweeps=4)
One Stokes projection step.
const std::vector< double > & velocity(int c) const
void setApertureOrder(int order)
FULL directional ghost-cell projection (the AMR port of flow's collocated set_ghost_projection): the ...
double divNormL2Regular(const std::array< std::vector< double >, 3 > &vel) const
divNormL2 restricted to REGULAR fluid rows (fluid and not cut) — the companion of divNormFaceRegular ...
void finishAdapt(SdfFn &&sdfFn)
void buildFaceField()
Build the ABC/Basilisk divergence-free FACE field from the current cell velocity u* and the projectio...
void setSolid(SdfFn &&sdfFn)
Build the cut-cell operators from an SDF callable sdfFn(worldPoint) (>0 fluid).
double divergence(const std::array< std::vector< double >, 3 > &vel, Index i) const
Openness-weighted FV divergence at leaf i, C/F-consistent: sum over (sub)faces of α·area·(outward fac...
Index numCfCutFaces() const
The C/F census — see AmrFlow::numCfCutFaces (docs/amr_cf_flux_gate.md §6.5).
const std::vector< double > & faceField() const
The divergence-free face field (read-only): uf[faceStart()[i]+s] for cell i's s-th forEachFaceFull fa...
double divNormL2(const std::array< std::vector< double >, 3 > &vel) const
Vec< Dim > filledVec(Real v)
Vec<Dim> with every component set to v (C++ has no aggregate-fill initializer).
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,...
auto makeBinaryOpenFnMixed(const BlockOctree< 3, Bits > &t, const AmrPoisson< 3, Bits > &pres, SdfFn sdfFn, const Vec< 3 > &h0, Vec< 3 > origin, std::array< long, 3 > frameShift={})
The canonical (sub)face openness for mixed-level cut bands: open iff both adjacent ACTUAL leaf center...
void ghostApplyDeltaHost(const GhostOverlay &ov, const std::vector< double > &x, std::vector< double > &y)
Matrix overlay: y currently holds the BINARY L matvec; overwrite the overlay rows with y = rho·(y + i...
GhostOverlaySampled buildGhostOverlaySampled(const BlockOctree< 3, Bits > &t, const AmrPoisson< 3, Bits > &pres, SdfFn &&sdf, int matrixOrder, int rhsOrder, Vec< 3 > origin=Vec< 3 >{}, const std::array< long, 3 > *globalFine=nullptr, std::array< long, 3 > frameShift={}, bool discovery=false, double rhoFactor=2.2, long maxSamples=0)
Build the sampled overlay.
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).
double hoFaceValue(double upup, double up, double down, int scheme)
High-order advected face value from the two upwind cells (upup, up) and the downwind cell (down).
CfUfDelta buildCfUfDelta(const AmrPoisson< 3, Bits > &ap, const BlockOctree< 3, Bits > &t, RegularFn &®ularOk, FluidFn &&fluidOk, CfScheme scheme)
std::vector< char > findPocketCells(const BlockOctree< 3, Bits > &t, const AmrPoisson< 3, Bits > &pres, const std::vector< double > &sdfC)
Fragmentation guard (the AMR port of flow's host-BFS pocket guard): the BINARY coupled-face graph (a ...
std::vector< double > transferField(const BlockOctree< Dim, Bits > &oldT, const std::vector< double > &oldF, const BlockOctree< Dim, Bits > &newT, bool linear=true, const std::type_identity_t< std::vector< std::array< double, Dim > > > *gradIn=nullptr)
Conservative remap of a leaf field from oldT to newT (same domain).
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).
void ghostDivergDeltaSampledHost(const GhostOverlaySampled &ov, const std::array< std::vector< double >, 3 > &u, std::vector< double > &d)
Divergence overlay (sampled): the sampled analog of ghostDivergDeltaHost (u_bc = 0).
void ghostDivergDeltaHost(const GhostOverlay &ov, const std::array< std::vector< double >, 3 > &u, std::vector< double > &d)
Divergence overlay: d currently holds the BINARY divergence (physical); overwrite overlay rows with d...
auto makeBinaryOpenFn(SdfFn sdfFn, const Vec< 3 > &h0)
Binary openness callable factory for the MG surrogate: a face is open iff both adjacent centers (prob...
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.
double gpsDirGrad(const GhostOverlaySampled &ov, Index r, const std::vector< double > &fld, int c, double invh)
Directional ghost cell-gradient on a sampled row (the mixed-level gradOfDir): the same cascade — cent...
CfCsr buildMomSeamDelta(const GhostOverlaySampled &ov, const BlockOctree< 3, Bits > &t, const AmrPoisson< 3, Bits > &pres, const Mom &mom, SdfFn &&sdf, Vec< 3 > origin, double idiag, double mu, long *nSkipped=nullptr, std::array< long, 3 > frameShift={})
Momentum ξ-overlay seam correction (plan §6.2, rung 1): at overlay rows with a different-level ±1 nei...
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).
GhostOverlay buildGhostOverlay(const BlockOctree< 3, Bits > &t, const AmrPoisson< 3, Bits > &pres, const std::vector< double > &sdfC, int matrixOrder, int rhsOrder, bool *bandViolation=nullptr)
Build the overlay from the octree + the cell-centered SDF samples (AmrCutCell::sdfCRaw — EXTENDED ove...
void ghostApplyDeltaSampledHost(const GhostOverlaySampled &ov, const std::vector< double > &x, std::vector< double > &y)
Matrix overlay (sampled): y currently holds the BINARY L matvec; overwrite overlay rows with y = rho·...
Component-tagged CSR overlay (the divergence delta): out(i) += Σ_k coef·u[comp](slot).
Scalar-input CSR overlay: out(i) += Σ_k coef·f(slot) over rows with C/F faces.
(uf_scheme − uf_std) for the div-free FACE field uf_k = ½(u_i+u_j) − (φ₊−φ₋)/d, one delta row per for...
Host ghost-projection overlay: one row per non-clean fluid leaf (== cut cell: some ±1 center sample s...
std::vector< int8_t > coupled
[n] 1 if the row has any phi coupling at all
std::vector< Index > cell
[n] leaf index