8#ifndef PECLET_FLOW_FLOW_IBM_PROJECT_HPP
9#define PECLET_FLOW_FLOW_IBM_PROJECT_HPP
30 const double ts0 = phaseTick();
31 tPredictor_ = tMomentum_ = tProjection_ = 0.0;
32 lastMomentumSweeps_ = 0;
33 lastMomentumResid_ = -1.0;
34 mg_.resetAllreduceCounters();
35 const bool hoisted = stepHeadFields();
37 const bool fullStress = stepTimeBase();
39 stepBalancedForce(hoisted);
41 for (
int outer = 0; outer < outerIters_; ++outer) {
42 const double tp0 = phaseTick();
43 lastOuterIters_ = outer + 1;
45 for (
int c = 0; c < 3; ++c)
46 Kokkos::deep_copy(prev_[c], C[c].u);
47 stepPredictorInputs();
48 const bool coloFF = stepPredictorRhs();
49 stepPredictorForces(coloFF, fullStress);
50 stepPredictorOperator();
51 stepPredictorCoarseAdvection();
52 const double tp1 = phaseTick();
53 tPredictor_ += tp1 - tp0;
55 const double tp2 = phaseTick();
56 tMomentum_ += tp2 - tp1;
58 tProjection_ += phaseTick() - tp2;
62 for (
int c = 0; c < 3; ++c)
64 lastOuterCorr_ = corr;
70 stepBackflowWarning();
71 tStep_ = phaseTick() - ts0;
74 tPredSum_ += tPredictor_;
75 tMomSum_ += tMomentum_;
76 tProjSum_ += tProjection_;
82void Solver<Grid>::stepPrechecks() {
93 superficialVelocityPrecheck();
98 if (colocatedFaceForce())
99 requireCollocatedFaceForceScope(
"step");
100 if (balancedForceActive())
101 requireBalancedForceScope(
"step",
true);
105 if (viscousStressAuto_)
106 resolveViscousStressAuto();
107 else if (fullStress_ && varProps_)
108 requireFullStressScope(
"step");
109 resolveSharpViscous();
113bool Solver<Grid>::stepHeadFields() {
125 const double _t0 = vofTick();
127 vofAdd(vt_.phaseChange, _t0);
136 const bool hoisted = prefactorHoistEligible();
138 projectBuildCoefficients();
139 mg_.prefactorBottom();
145 fillCellForceGhosts();
149 fillDragBetaGhosts();
153 const double _t0 = vofTick();
154 updateVofCurvature();
155 vofAdd(vt_.curvature, _t0);
163 if (porous_ && porousCons_)
169void Solver<Grid>::stepStencils() {
172 sharpViscousGeometry();
176 if (((varProps_ || varRho_ || hasDrag_ || (porous_ && porousCons_)) || dtDirty_) &&
183bool Solver<Grid>::stepTimeBase() {
186 for (
int c = 0;
c < 3; ++
c)
187 Kokkos::deep_copy(
CCExec(), old_[c], C[c].u);
190 const bool fullStress = fullStressActive();
193 if (sharpActive_ && fullStress)
200 if (sharpActive_ && fullStress && muUniform_) {
201 sharpActive_ =
false;
209void Solver<Grid>::stepPressureGhosts() {
210 if (cutcellPressure_ && incremental_) {
216 if (colocatedFaceForce()) {
223 fillPropGhosts(effRhoField());
228void Solver<Grid>::stepBalancedForce(
bool hoisted) {
231 coeffBuiltThisStep_ = hoisted;
232 if (pWritten_ || pbWritten_) {
233 pbValid_ = pWritten_ && pbWritten_;
234 pWritten_ = pbWritten_ =
false;
236 if (balancedForceActive()) {
237 applyBalancedForceProjection();
240 if (!bfpSet_ && !bfpOpenNotice_ && colocatedFaceForce() && hasOpenDomainFace()) {
243 bfpOpenNotice_ =
true;
245#ifdef PECLET_FLOW_MPI
247 MPI_Comm_rank(comm_, &r);
251 "peclet::flow SolverColocated: the balanced-force projection (default ON on "
252 "variable density / surface tension) is OFF: it does not support "
253 "inflow/outflow domain faces (v1). Call set_balanced_force_projection(False) "
254 "to silence this notice.\n");
260void Solver<Grid>::stepPredictorInputs() {
261 if (advectsMomentum() || hasBc_ || (Grid::collocated && faceInterp_ >= 5 && faceInterp_ <= 7))
262 for (
int c = 0;
c < 3; ++
c)
278 if (porous_ && advectsMomentum())
283bool Solver<Grid>::stepPredictorRhs() {
289 const bool coloFF = colocatedFaceForce();
290 for (
int c = 0;
c < 3; ++
c)
291 coloFF ? buildRhsColoVar(c)
292 : (vofMomEnabled_ ? buildRhsVarMom(c)
293 : (effVarRho() ? buildRhsVar(
c)
294 : (hasCellForce_ ? buildRhsForced(
c) :
buildRhs(
c))));
299void Solver<Grid>::stepPredictorForces(
bool coloFF,
bool fullStress) {
304 if (csfActive() && !coloFF) {
305 const double _t0 = vofTick();
306 for (
int c = 0;
c < 3; ++
c)
309 : (csfMode_ == 0 ? addCsfRhs(
c,
C[
c].
b, true) : addCsfRhsCellInterp(
c,
C[
c].
b, true));
310 vofAdd(vt_.csf, _t0);
315 if (fullStress && !muUniform_)
316 for (
int c = 0;
c < 3; ++
c)
317 addFullStressRhsComp(c);
321void Solver<Grid>::stepPredictorOperator() {
327 if (implicitAdv() && (!hasBc_ || !useVelocityMg_ || mixedVelocityMg()))
328 for (
int c = 0;
c < 3; ++
c)
329 (varProps_ || effVarRho()) ? buildAdvStencilVar(c) : buildAdvStencil(
c);
333 if (bcStencilPath() && backflowBeta_ > 0.0 && hasOutflow_)
334 for (
int c = 0;
c < 3; ++
c)
335 applyBackflowStab(c);
339void Solver<Grid>::stepPredictorCoarseAdvection() {
343 if (useVelocityMg_ && advectsMomentum() &&
344 ((implicitFou_ && !hasBc_) || (mixedVelocityMg() && implicitAdv())))
345 vmg_.restrictAdvVelocities(
349 ufAdvVelocity() ? openFaceView(0) : advVelView(0),
350 ufAdvVelocity() ? openFaceView(1) : advVelView(1),
351 ufAdvVelocity() ? openFaceView(2) : advVelView(2));
355void Solver<Grid>::stepMomentumSolve() {
356 for (
int c = 0;
c < 3; ++
c)
363 if constexpr (!Grid::collocated) {
365 for (
int c = 0;
c < 3; ++
c) {
366 if (uStar_[c].extent(0) != n_)
368 Kokkos::deep_copy(uStar_[c], C[c].u);
376void Solver<Grid>::stepProjection() {
382 if (porous_ && !cutcellPressure_)
383 throw std::runtime_error(
384 "set_porous_continuity(True) requires the cut-cell pressure operator: call "
385 "set_solid(...) or set_pressure_geometry(all-fluid SDF) before stepping (a "
386 "domain-BC-only box otherwise runs with NO continuity constraint at all)");
387 if (cutcellPressure_)
392void Solver<Grid>::stepPostProjection() {
394 applyVelocityBcAll(0,
false);
397 if (cutcellPressure_)
398 scalarCaptureOpenFaceFlux();
402void Solver<Grid>::stepTransport() {
413 pcUpdateEnergyProps();
416 pcUpdateThermalMask();
424 applySuperficialVelocity();
428void Solver<Grid>::stepBackflowWarning() {
433 if (hasOutflow_ && backflowBeta_ <= 0.0 && !backflowWarned_) {
434 const OutflowBackflow
ob = outflowBackflow();
435 if (
ob.reversed > 0) {
436 backflowWarned_ =
true;
438#ifdef PECLET_FLOW_MPI
440 MPI_Comm_rank(comm_, &r);
444 "peclet::flow WARNING: reversed flow on an OUTFLOW face (max u.n = -%.3e, "
445 "%.1f%% of the outlet area, kinetic-energy influx %.3e) with the backflow "
446 "stabilization OFF. The zero-gradient outflow is only conditionally "
447 "energy-stable under reversal (the re-entering flux carries the boundary "
448 "cell's own velocity back in with no sink); this is the configuration in "
449 "which the literature's backflow divergence occurs. Call "
450 "set_backflow_stabilization(beta) (default 0.2; beta >= 0.5 is the "
451 "unconditional bound), or lengthen the domain so the recirculation closes "
452 "before the outlet. Monitor with outflow_backflow().\n",
453 ob.maxReverse, 100.0 *
ob.fraction,
ob.energyInflux);
460 if (superficialAxis_ < 0)
462 const int a = superficialAxis_;
463 if (bc_[2 * a] != 0 || bc_[2 * a + 1] != 0)
464 throw std::runtime_error(
465 "set_superficial_velocity: the axis carries a domain boundary condition; a uniform "
466 "shift of the normal velocity is divergence-free only on a PERIODIC axis");
468 throw std::runtime_error(
469 "set_superficial_velocity: an immersed solid is present; a uniform shift would move fluid "
470 "through the cut faces (all-fluid domains only)");
475 if (superficialAxis_ < 0)
477 const int a = superficialAxis_;
481 long ncell =
static_cast<long>(nx_) * ny_ * nz_;
482#ifdef PECLET_FLOW_MPI
484 ncell =
static_cast<long>(gnx_) * gny_ * gnz_;
486 const double target = superficialVelPhys_ * u_.velToInt(a), dn =
static_cast<double>(ncell);
487 Kokkos::View<double, CCMem> shift = superficialShift_;
490 Kokkos::parallel_reduce(
491 "superficial_velocity_sum", inner,
492 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& acc) {
493 acc += f((
long)x + (
long)y * e.x + (
long)z * (
long)e.x * e.y);
495 Kokkos::Sum<double, CCMem>(shift));
496#ifdef PECLET_FLOW_MPI
499 double loc = 0.0, glob = 0.0;
500 Kokkos::deep_copy(loc, shift);
501 MPI_Allreduce(&loc, &glob, 1, MPI_DOUBLE, MPI_SUM, comm_);
502 Kokkos::deep_copy(shift, glob);
505 Kokkos::parallel_for(
506 "superficial_velocity_to_shift", Kokkos::RangePolicy<CCExec>(space, 0, 1),
507 KOKKOS_LAMBDA(
int) { shift() = target - shift() / dn; });
508 Kokkos::parallel_for(
509 "superficial_velocity_shift", inner, KOKKOS_LAMBDA(
int x,
int y,
int z) {
510 f((
long)x + (
long)y * e.x + (
long)z * (
long)e.x * e.y) += shift();
519 return Grid::collocated && (varRho_ ||
csfActive());
524 if constexpr (Grid::collocated) {
525 if (colSchemeAuto_ && ghostProjection_) {
526 ghostProjection_ =
false;
530 "peclet::flow SolverColocated: AUTO scheme fell back to gauge-exact (%s is rung V8 "
531 "and the ghost projection v1 does not support it). Select explicitly with "
532 "set_collocated_scheme to silence this notice.\n",
540 for (
int c = 0; c < 3; ++c)
541 if (faceAcc_[c].extent(0) != n_)
542 faceAcc_[c] =
CCField(
"faceAcc", n_);
551 throw std::runtime_error(
553 ": variable density / surface tension on SolverColocated is rung V8 and is ALL-FLUID "
554 "only (set_pressure_geometry). Immersed solids on this path are the next package "
555 "(doc/collocated_multiphase_solids_plan.md) — not this rung.");
556 if (ghostProjection_)
557 throw std::runtime_error(
559 ": the ghost projection (v1) does not support variable density; select "
560 "set_collocated_scheme(\"gauge-exact\") (the AUTO fallback already does).");
561 if (rhoFaceHarmonic_)
562 throw std::runtime_error(
564 ": set_rho_face_harmonic is not wired into the collocated variable-density pair (the "
565 "pressure force and the face coefficient would use different rho_f). Staggered only.");
567 throw std::runtime_error(
569 ": variable density / surface tension on SolverColocated needs the incremental pressure "
570 "(diagnostics.set_incremental_pressure(True)): the pressure force lives inside the "
571 "implicit predictor, and the non-incremental (Chorin) step has none, so its steady state "
572 "would depend on dt.");
597 return lastBalancedForceIters_;
602 return lastBalancedForceFailed_;
607 return balancedForceFailures_;
624 for (
int f = 0; f < 6; ++f)
625 if (bc_[f] == 2 || bc_[f] == 3)
632 const std::string m = std::string(who) +
": the balanced-force projection ";
634 throw std::runtime_error(m +
635 "does not support the porous (volume-averaged) continuity: the "
636 "VANS operator carries eps and the drag relaxation (CFD-DEM is out "
637 "of scope). Call set_balanced_force_projection(False).");
638 bool autoGhost =
false;
639 if constexpr (Grid::collocated)
640 autoGhost = colSchemeAuto_;
641 if (ghostProjection_ && (atStep || !autoGhost))
642 throw std::runtime_error(m +
643 "does not support the ghost projection (its overlay does not reach "
644 "the balanced face force yet). Call set_balanced_force_projection"
646 if (fluidOnlyMode_ == 2)
647 throw std::runtime_error(m +
"does not support set_fluid_only_constraint(2).");
649 throw std::runtime_error(m +
"does not support the block CSF (enable_vof_block_csf).");
650 for (
int f = 0; f < 6; ++f)
651 if (bc_[f] == 2 || bc_[f] == 3)
652 throw std::runtime_error(
654 "does not support inflow/outflow domain faces (v1: the operator-vs-flux openness rule "
655 "at inflow and the outflow planes are not wired). Call "
656 "set_balanced_force_projection(False).");
658 throw std::runtime_error(m +
"needs the incremental pressure (it re-splits P).");
660 if (!cutcellPressure_)
661 throw std::runtime_error(
662 m +
"needs the cut-cell pressure operator (set_solid or set_pressure_geometry).");
663 if constexpr (Grid::collocated)
665 throw std::runtime_error(
667 "has nothing to balance on the constant-density collocated path without surface "
668 "tension (its pressure force is the gauge-exact cell gradient, not the face "
669 "integral). It applies to variable density / surface tension on SolverColocated "
670 "and to every Solver configuration. Call set_balanced_force_projection(False).");
681 const double fc = f_[c];
683 const bool haveFb = hasCellForce_;
688 Kokkos::parallel_for(
689 "peclet::flow::bfp_face_force", MD(space, {
G,
G,
G}, {e.x -
G + 1, e.y -
G + 1, e.z -
G + 1}),
690 KOKKOS_LAMBDA(
int x,
int y,
int z) {
691 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
692 af(i) = fc + (haveFb ? 0.5 * ((double)
fb(i) + (double)
fb(i - s)) : 0.0);
697 if (Grid::collocated || csfMode_ == 0)
705 const double rho0 = rho_;
706 const bool harm = rhoFaceHarmonic_;
707 Kokkos::parallel_for(
708 "peclet::flow::bfp_face_coeff",
709 MD(space, {
G,
G,
G}, {e.x -
G + 1, e.y -
G + 1, e.z -
G + 1}),
710 KOKKOS_LAMBDA(
int x,
int y,
int z) {
711 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
712 const double cf = harm ? rho0 * (rho(i) + rho(i - s)) / (2.0 * rho(i) * rho(i - s))
713 : rho0 / (0.5 * (rho(i) + rho(i - s)));
721 return prefactorHoist_ && varRho_ && !porous_ && outerIters_ == 1 && !
balancedForceActive() &&
722 !useChebyshev_ && !ghostProjection_ && mg_.directPrefactorEligible();
731 coeffBuiltThisStep_ =
true;
732 if (prefactorHoist_ && !useChebyshev_ && !ghostProjection_)
733 mg_.prefactorBottom();
736 if (pb1_.extent(0) != n1_)
741 for (
int c = 0; c < 3; ++c)
753#ifdef PECLET_FLOW_MPI
756 MPI_Allreduce(&bmax, &g, 1, MPI_DOUBLE, MPI_MAX, comm_);
761 Kokkos::deep_copy(pb1_, 0.0);
767 Kokkos::parallel_for(
768 "peclet::flow::bfp_negdiv", Kokkos::RangePolicy<CCExec>(space, 0, n1_),
769 KOKKOS_LAMBDA(std::size_t i) { r(i) = -r(i); });
772 const double rtol = useChebyshev_ ? chebRtol_ : pcgRtol_;
773 const double bref = std::max(mg_.rhsNorm(rhs1_, r_), 1e-300);
774 const double r0 = mg_.residualNorm(rhs1_, pb1_, r_, Ap_);
775 if (r0 > rtol * bref) {
779 int bad = mg_.lastSolveFailed() ? 1 : 0;
785 Kokkos::parallel_reduce(
786 "peclet::flow::bfp_nonfinite",
788 KOKKOS_LAMBDA(
int x,
int y,
int z,
long& acc) {
789 const long i = (long)x + (
long)y * e1.
x + (long)z * (
long)e1.
x * e1.
y;
790 if (!Kokkos::isfinite(x1(i)))
794 bad = (bad || nf > 0) ? 1 : 0;
796#ifdef PECLET_FLOW_MPI
799 MPI_Allreduce(&bad, &g, 1, MPI_INT, MPI_MAX, comm_);
806 lastBalancedForceIters_ = iters;
807 lastBalancedForceFailed_ = failed;
812 ++balancedForceFailures_;
813 if (!bfpFailWarned_) {
814 bfpFailWarned_ =
true;
816#ifdef PECLET_FLOW_MPI
818 MPI_Comm_rank(comm_, &r);
822 "peclet::flow WARNING: the balanced-force pre-projection solve failed (driver "
823 "breakdown or a non-finite result, %ld iterations); P and p_balanced were "
824 "kept for this step. Further failures are counted silently in "
825 "diagnostics.balanced_force_failures().\n",
839 const C3 e1 = e1_, e2 = e_;
840 const bool resplit = !pbValid_;
841 Kokkos::parallel_for(
842 "peclet::flow::bfp_split",
MDRange3<CCExec>(space, {0, 0, 0}, {nx_, ny_, nz_}),
843 KOKKOS_LAMBDA(
int x,
int y,
int z) {
844 const long i1 = (long)(x + 1) + (long)(y + 1) * e1.
x + (long)(z + 1) * (long)e1.
x * e1.
y;
845 const long i2 = (long)(x +
G) + (long)(y +
G) * e2.x + (long)(z +
G) * (long)e2.x * e2.y;
846 const double xv = x1(i1);
848 P(i2) += xv - pb(i2);
869 struct StopRefGuard {
873 mg_.setStopReference(bref);
876 mg_.estimateEigenvalues(
CCConst(rhs1_), bfpChebA_, bfpChebB_, 15, 2, 2, 12);
881 long it = mg_.solveChebyshev(rhs1_, pb1_, chebMaxit_, chebRtol_, 2, 2, 12, bfpChebA_, bfpChebB_,
883 if (mg_.chebyshevGuardTripped()) {
885 mg_.estimateEigenvalues(
CCConst(rhs1_), bfpChebA_, bfpChebB_, 15, 2, 2, 12,
887 if (bfpChebB_ < bfpLmaxFloor_)
888 bfpChebB_ = bfpLmaxFloor_;
889 bfpLmaxFloor_ = bfpChebB_;
891 it += mg_.solveChebyshev(rhs1_, pb1_, chebMaxit_, chebRtol_, 2, 2, 12, bfpChebA_, bfpChebB_);
892 }
else if (it >= chebMaxit_) {
893 const double rfin = mg_.residualNorm(rhs1_, pb1_, r_, Ap_);
895 mg_.estimateEigenvalues(
CCConst(rhs1_), bfpChebA_, bfpChebB_, 15, 2, 2, 12);
896 if (bfpChebB_ < bfpLmaxFloor_)
897 bfpChebB_ = bfpLmaxFloor_;
900 mg_.solveChebyshev(rhs1_, pb1_, chebMaxit_, chebRtol_, 2, 2, 12, bfpChebA_, bfpChebB_);
907 if (zp1_.extent(0) != n1_)
909 it = mg_.solveFCG(rhs1_, pb1_, r_, pp_, z_, zp1_, Ap_, pcgMaxit_, pcgRtol_, 2, 2, 12,
nullptr,
910 0,
C3{nx_, ny_, nz_});
912 it = mg_.solvePCG(rhs1_, pb1_, r_, pp_, z_, Ap_, pcgMaxit_, pcgRtol_, 2, 2, 12,
nullptr, 0,
915 checkCgExit(rhs1_, pb1_, it, useFcg_ ?
"balanced-force flexible-CG" :
"balanced-force MG-PCG",
923 const double idiag = rho_ / dt_, fc = f_[c], rho = rho_, wc = u_.w[c];
925 CCField bb = C[c].b, rs = C[c].rscale, P = P_, brhs = bcBrhs_[c], inh = C[c].inhom;
936 const long strd = (c == 0) ? 1 : (c == 1) ? e_.x : (long)e_.x * e_.y;
940 const bool pureFou =
implicitAdv() && !deferredCorr_;
941 const bool incr = cutcellPressure_ && incremental_, adv =
advectsMomentum() && !pureFou,
949 const int sch = advScheme_;
959 const bool tg = Grid::collocated && faceInterp_ == 5 && incr;
961 const bool wg = Grid::collocated && (faceInterp_ == 6 || faceInterp_ == 7) && incr;
966 const bool gg = Grid::collocated && (ghostProjection_ || faceInterp_ == 9) && incr;
967 if constexpr (Grid::collocated) {
974 CCField xcs[3] = {xcx_, xcy_, xcz_};
975 CCField oax[3] = {ox_, oy_, oz_};
977 true, c, e_,
G, u_.w[c]);
979 CCField oax[3] = {ox_, oy_, oz_};
994 const bool wd = Grid::collocated && faceInterp_ >= 5 && faceInterp_ <= 7;
995 if constexpr (Grid::collocated)
998 FPC(C[c].AN),
FPC(C[c].AB),
FPC(C[c].AT), e_,
G);
1001 u_.w[2], u_.hp[0], u_.hp[1], u_.hp[2]);
1008 "rhs",
C3{
G,
G,
G},
C3{e.x -
G, e.y -
G, e.z -
G}, KOKKOS_LAMBDA(
int x,
int y,
int z) {
1009 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
1010 double aK = 0.0, aF = 0.0;
1012 sadv::ViewAcc Ua{
U, e.x, e.y}, Va{V, e.x, e.y}, Wa{W, e.x, e.y}, Fa{aP, e.x, e.y};
1013 aK = (sch == 0) ? Grid::advect_sou(c, x, y, z, Ua, Va, Wa, Fa, ufa)
1014 : Grid::advect(c, x, y, z, Ua, Va, Wa, Fa, ufa);
1016 aF = Grid::advect_fou(c, x, y, z, Ua, Va, Wa, Fa, ufa);
1019 ar(i) = rho * (aF - aK);
1029 ? ((tg || wg || gg) ? gpw(i) : wc * (0.5 * (P((
long)i + strd) - P((
long)i - strd))))
1030 : wc * (P(i) - P((
long)i - strd));
1033 const double bfv = idiag * cs(i) * un(i) + cs(i) * (fc - gp);
1034 bb(i) = fvM(i) - rs(i) * (fvL(i) - bfv);
1036 const double comp = pc ? rho * uu(i) * 0.5 * (dv(i) + dv((
long)i - strd)) : 0.0;
1037 bb(i) = rs(i) * (idiag * un(i) + fc - rho * aK + rho * aF + comp - gp) +
1038 (bc ? brhs(i) : -inh(i));
1043template <
class Gr
id>
1046 const double idiag = rho_ / dt_, fc = f_[c], rho = rho_, wc = u_.w[c];
1048 CCField bb = C[c].b, rs = C[c].rscale, P = P_, brhs = bcBrhs_[c], inh = C[c].inhom;
1060 const long strd = (c == 0) ? 1 : (c == 1) ? e_.x : (long)e_.x * e_.y;
1061 const bool pureFou =
implicitAdv() && !deferredCorr_;
1062 const bool incr = cutcellPressure_ && incremental_, adv =
advectsMomentum() && !pureFou,
1065 const int sch = advScheme_;
1071 const bool tg = Grid::collocated && faceInterp_ == 5 && incr;
1072 const bool gg = Grid::collocated && (ghostProjection_ || faceInterp_ == 9) &&
1074 if constexpr (Grid::collocated) {
1078 CCField xcs[3] = {xcx_, xcy_, xcz_};
1079 CCField oax[3] = {ox_, oy_, oz_};
1081 true, c, e_,
G, u_.w[c]);
1086 Kokkos::parallel_for(
1087 "rhs_forced", MD(space, {
G,
G,
G}, {e.x -
G, e.y -
G, e.z -
G}),
1088 KOKKOS_LAMBDA(
int x,
int y,
int z) {
1089 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
1090 double aK = 0.0, aF = 0.0;
1092 sadv::ViewAcc Ua{
U, e.x, e.y}, Va{V, e.x, e.y}, Wa{W, e.x, e.y}, Fa{aP, e.x, e.y};
1093 aK = (sch == 0) ? Grid::advect_sou(c, x, y, z, Ua, Va, Wa, Fa, ufa)
1094 : Grid::advect(c, x, y, z, Ua, Va, Wa, Fa, ufa);
1096 aF = Grid::advect_fou(c, x, y, z, Ua, Va, Wa, Fa, ufa);
1099 ar(i) = rho * (aF - aK);
1103 ? ((tg || gg) ? gpw(i) : wc * (0.5 * (P((
long)i + strd) - P((
long)i - strd))))
1104 : wc * (P(i) - P((
long)i - strd));
1105 const double comp = pc ? rho * uu(i) * 0.5 * (dv(i) + dv((
long)i - strd)) : 0.0;
1108 const double fbU = Grid::atVelocity(
fb, i, strd);
1109 bb(i) = rs(i) * (idiag * un(i) + fc + fbU - rho * aK + rho * aF + comp - gp) +
1110 (bc ? brhs(i) : -inh(i));
1114template <
class Gr
id>
1116 const double idt = 1.0 / dt_, fc = f_[c], wc = u_.w[c];
1118 CCField bb = C[c].b, rs = C[c].rscale, P = P_, brhs = bcBrhs_[c], inh = C[c].inhom;
1132 const bool pureFou =
implicitAdv() && !deferredCorr_;
1133 const bool incr = cutcellPressure_ && incremental_, adv =
advectsMomentum() && !pureFou,
1136 const int sch = advScheme_;
1145 "rhs_var",
C3{
G,
G,
G},
C3{e.x -
G, e.y -
G, e.z -
G}, KOKKOS_LAMBDA(
int x,
int y,
int z) {
1146 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
1147 const double rhoF = 0.5 * (rf(i) + rf(i - strd));
1148 double aK = 0.0, aF = 0.0;
1150 sadv::ViewAcc Ua{
U, e.x, e.y}, Va{V, e.x, e.y}, Wa{W, e.x, e.y}, Fa{aP, e.x, e.y};
1151 aK = (sch == 0) ? Grid::advect_sou(c, x, y, z, Ua, Va, Wa, Fa, ufa)
1152 : Grid::advect(c, x, y, z, Ua, Va, Wa, Fa, ufa);
1154 aF = Grid::advect_fou(c, x, y, z, Ua, Va, Wa, Fa, ufa);
1156 const double gp = !incr ? 0.0
1157 : Grid::collocated ? wc * (0.5 * (P((
long)i + strd) - P((
long)i - strd)))
1158 : wc * (P(i) - P((
long)i - strd));
1159 const double fbF = Grid::atVelocity(
fb, i, strd);
1160 const double comp = pc ? rhoF * uu(i) * 0.5 * (dv(i) + dv((
long)i - strd)) : 0.0;
1161 bb(i) = rs(i) * (rhoF * idt * un(i) + fc + fbF - rhoF * aK + rhoF * aF + comp - gp) +
1162 (bc ? brhs(i) : -inh(i));
1166template <
class Gr
id>
1169 const double idt = 1.0 / dt_, fc = f_[c], wc = u_.w[c];
1171 CCField bb = C[c].b, rs = C[c].rscale, P = P_, brhs = bcBrhs_[c], inh = C[c].inhom;
1176 const bool incr = cutcellPressure_ && incremental_;
1179 Kokkos::parallel_for(
1180 "rhs_var_mom", MD(space, {
G,
G,
G}, {e.x -
G, e.y -
G, e.z -
G}),
1181 KOKKOS_LAMBDA(
int x,
int y,
int z) {
1182 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
1183 const double rhoF = 0.5 * (rf(i) + rf(i - strd));
1184 const double gp = !incr ? 0.0
1185 : Grid::collocated ? wc * (0.5 * (P((
long)i + strd) - P((
long)i - strd)))
1186 : wc * (P(i) - P((
long)i - strd));
1187 const double fbF = Grid::atVelocity(
fb, i, strd);
1188 bb(i) = rs(i) * (rhoF * idt * ua(i) + fc + fbF - gp) + (bc ? brhs(i) : -inh(i));
1202template <
class Gr
id>
1209 const bool incr = cutcellPressure_ && incremental_;
1214 rho_, f_[c], incr, 1.0, sc, e_,
G, u_.w[c]);
1217 haveRho, rho_, sigmaCsf_, 1.0 / u_.w[c], 1.0, sc, e_,
G);
1222 const double idt = 1.0 / dt_, rhoIdtC = rho_ / dt_, rhoK = rho_;
1224 CCField bb = C[c].b, rs = C[c].rscale, brhs = bcBrhs_[c], inh = C[c].inhom;
1225 CCField oax[3] = {ox_, oy_, oz_};
1227 const bool haveFb = hasCellForce_;
1232 const bool pureFou =
implicitAdv() && !deferredCorr_;
1235 const int sch = advScheme_;
1237 Kokkos::parallel_for(
1238 "rhs_colo_var", MD(space, {
G,
G,
G}, {e.x -
G, e.y -
G, e.z -
G}),
1239 KOKKOS_LAMBDA(
int x,
int y,
int z) {
1240 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
1241 const double rhoC = haveRho ? rf(i) : rhoK;
1242 const double dg = haveRho ? rhoC * idt : rhoIdtC;
1243 double aK = 0.0, aF = 0.0;
1245 sadv::ViewAcc Ua{
U, e.x, e.y}, Va{V, e.x, e.y}, Wa{W, e.x, e.y}, Fa{aP, e.x, e.y};
1246 aK = (sch == 0) ? Grid::advect_sou(c, x, y, z, Ua, Va, Wa, Fa, ufa)
1247 : Grid::advect(c, x, y, z, Ua, Va, Wa, Fa, ufa);
1249 aF = Grid::advect_fou(c, x, y, z, Ua, Va, Wa, Fa, ufa);
1251 const long j = i + (long)((c == 0) ? 1 : (c == 1) ? e.x : (
long)e.x * e.y);
1252 const double oLo = o(i), oHi = o(j);
1254 const double fcell = haveFb ? wsum *
fb(i) : 0.0;
1257 rs(i) * (dg * un(i) - rhoC * aK + rhoC * aF + fcell +
pf) + (bc ? brhs(i) : -inh(i));
1261template <
class Gr
id>
1264 Kokkos::deep_copy(tgp_, f);
1268 const double eps = rotFilterEps_;
1271 Kokkos::parallel_for(
1272 "peclet::flow::rot_filter", MD(space, {
G,
G,
G}, {e.x -
G, e.y -
G, e.z -
G}),
1273 KOKKOS_LAMBDA(
int x,
int y,
int z) {
1274 const long sy = e.x, sz = (long)e.x * e.y;
1275 const long i = (long)x + (
long)y * sy + (long)z * sz;
1276 const long sa = (axis == 0) ? 1 : (axis == 1) ? sy : sz;
1279 const bool am = sd(i - sa) >= 0.0, ap = sd(i + sa) >= 0.0;
1282 sm = 0.25 * (src(i - sa) + 2.0 * src(i) + src(i + sa));
1284 sm = 0.5 * (src(i) + src(i + sa));
1286 sm = 0.5 * (src(i) + src(i - sa));
1294 f(i) = eps * src(i) + (1.0 - eps) * sm;
1298template <
class Gr
id>
1301 if (!coeffBuiltThisStep_)
1308template <
class Gr
id>
1313 if constexpr (Grid::collocated) {
1318 for (
int c = 0; c < 3; ++c)
1329 }
else if (faceInterp_ == 5 || faceInterp_ == 7)
1335 faceFieldValid_ =
true;
1336 if (ghostProjection_) {
1342 throw std::runtime_error(
"ghost projection: call set_solid after set_ghost_projection");
1343 if (porous_ || varRho_ || useChebyshev_)
1344 throw std::runtime_error(
1345 "ghost projection: porous/variable-rho/Chebyshev unsupported (v1)");
1349 C3{nx_, ny_, nz_}, e_,
G, distributed_);
1353 for (
int c = 0; c < 3; ++c)
1362 }
else if (ghostProjection_) {
1367 throw std::runtime_error(
"ghost projection: call set_solid after set_ghost_projection");
1368 if (porous_ || varRho_ || useChebyshev_)
1369 throw std::runtime_error(
1370 "ghost projection: porous/variable-rho/Chebyshev unsupported (v1)");
1374 C3{nx_, ny_, nz_}, e_,
G, distributed_);
1391 CCField d = div_, dd = depsdt_, ep = epsField_, epp = epsPrev_;
1392 const double idt = 1.0 / dt_;
1393 const bool useDt = porousDepsDt_;
1395 Kokkos::parallel_for(
1396 "peclet::flow::deps_dt", MD(space, {
G,
G,
G}, {e.x -
G, e.y -
G, e.z -
G}),
1397 KOKKOS_LAMBDA(
int x,
int y,
int z) {
1398 const long i = (long)x + (
long)y * e.x + (long)z * e.x * e.y;
1399 dd(i) = (ep(i) - epp(i)) * idt;
1410 Kokkos::parallel_for(
1411 "negdiv", Kokkos::RangePolicy<CCExec>(space, 0, n1_),
1412 KOKKOS_LAMBDA(std::size_t i) { r(i) = -r(i); });
1414 if (fluidOnlyMode_ == 2) {
1418 throw std::runtime_error(
"set_fluid_only_constraint(2): Chebyshev unsupported (v1)");
1422 const C3 e1 = e1_, e2 = e_;
1423 Kokkos::parallel_for(
1424 "peclet::flow::star_mask_rhs",
MDRange3<CCExec>(space, {0, 0, 0}, {nx_, ny_, nz_}),
1425 KOKKOS_LAMBDA(
int x,
int y,
int z) {
1426 const long i2 = (long)(x +
G) + (long)(y +
G) * e2.x + (long)(z +
G) * (long)e2.x * e2.y;
1428 r((
long)(x + 1) + (
long)(y + 1) * e1.
x + (
long)(z + 1) * (
long)e1.
x * e1.
y) = 0.0;
1433template <
class Gr
id>
1446 const bool inPlace = !mg_.distributed() && !hasOutflow_;
1447 CCField cx = inPlace ? mg_.level0Coefficient(0) : cx1_;
1448 CCField cy = inPlace ? mg_.level0Coefficient(1) : cy1_;
1449 CCField cz = inPlace ? mg_.level0Coefficient(2) : cz1_;
1456 if (rhoFaceHarmonic_)
1469 CCField oo[3] = {ox1_, oy1_, oz1_};
1470 for (
int a = 0; a < 3; ++a)
1471 if (bc_[2 * a + 1] == 3 && touchesGlobalFace(2 * a + 1))
1475 mg_.setBoundaryConditions(bc_);
1476 mg_.setOutflowCoefficient(hasOutflow_ && outflowOpCoeff_);
1478 mg_.setOutflowCoefficient(
false);
1481 chebWarmOk_ = chebWarmOk_ || chebBoundsSet_;
1482 chebBoundsSet_ =
false;
1498 }
else if (porousCons_) {
1499 Kokkos::deep_copy(beta1_, 0.0);
1506 CCConst(beta1_), hasDrag_, rho_ / dt_, e1_, 1);
1507 }
else if (hasDrag_) {
1509 CCConst(beta1_), rho_ / dt_, e1_, 1);
1514 mg_.setBoundaryConditions(bc_);
1518 mg_.setOutflowCoefficient(hasOutflow_ && outflowOpCoeff_);
1520 mg_.setOutflowCoefficient(
false);
1521 chebWarmOk_ = chebWarmOk_ || chebBoundsSet_;
1522 chebBoundsSet_ =
false;
1526template <
class Gr
id>
1535 if (pguessMode_ != 1) {
1537 if (why && pguessMode_ == 2)
1538 throw std::runtime_error(
1539 std::string(
"set_pressure_initial_guess('projected'): not eligible here: ") + why);
1540 guess = why ==
nullptr;
1545 Kokkos::deep_copy(
CCExec(), phi1_, 0.0);
1549 struct ZeroGuessScope {
1552 } zeroGuessScope{mg_};
1553 mg_.setZeroInitialGuess(!pwarm_);
1559 const int driver = useFcg_ ? 2 : 1;
1560 if (phiPrev1_.extent(0) != n1_) {
1561 phiPrev1_ =
CCField(
"phiPrev1", n1_);
1564 if (driver != pguessDriver_)
1566 pguessDriver_ = driver;
1567 const int hist = std::min(pguessCount_, pguessHistMax_);
1576 Kokkos::deep_copy(phiPrev1_, 0.0);
1577 mg_.setZeroInitialGuess(
true);
1579 mg_.setProjectedGuess(phi1_, hist);
1581 std::swap(phi1_, phiPrev1_);
1582 pguessCount_ = std::min(pguessCount_ + 1, pguessHistMax_);
1583 if (mg_.lastSolveFailed() || lastPressureIters_ >= pcgMaxit_)
1590 lastPressureFailed_ = mg_.lastSolveFailed();
1591 if (fluidOnlyMode_ == 2) {
1598 const C3 e1 = e1_, e2 = e_;
1599 Kokkos::parallel_for(
1600 "peclet::flow::star_pin_solid",
MDRange3<CCExec>(space, {0, 0, 0}, {nx_, ny_, nz_}),
1601 KOKKOS_LAMBDA(
int x,
int y,
int z) {
1602 const long i2 = (long)(x +
G) + (long)(y +
G) * e2.x + (long)(z +
G) * (long)e2.x * e2.y;
1604 ph((
long)(x + 1) + (
long)(y + 1) * e1.
x + (
long)(z + 1) * (
long)e1.
x * e1.
y) = 0.0;
1611 B3 e{e_.x, e_.y, e_.z};
1612 for (
int a = 0; a < 3; ++a)
1613 for (
int s = 0; s < 2; ++s)
1614 if (bc_[2 * a + s] == 3 && touchesGlobalFace(2 * a + s))
1619template <
class Gr
id>
1622 if (chebGrowthWarned_)
1624 chebGrowthWarned_ =
true;
1626#ifdef PECLET_FLOW_MPI
1628 MPI_Comm_rank(comm_, &r);
1631 std::fprintf(stderr,
1632 "peclet::flow notice: a Chebyshev pressure solve on cold spectral bounds saw its "
1633 "residual grow (iteration %ld): lambda_max was underestimated; the bounds were "
1634 "re-estimated with a bracketing margin and the solve redone. Further cases are "
1635 "counted silently in diagnostics.num_pressure_chebyshev_growth_trips.\n",
1639template <
class Gr
id>
1642 if (useChebyshev_) {
1653 auto ratchet = [&]() {
1654 if (chebB_ < chebLmaxFloor_)
1655 chebB_ = chebLmaxFloor_;
1658 if (!chebBoundsSet_) {
1660 chebLmaxFloor_ = 0.0;
1661 warm = chebWarmOk_ && mg_.eigenWarmReady();
1662 mg_.estimateEigenvalues(
CCConst(rhs1), chebA_, chebB_, warm ? 5 : 15, 2, 2, 12,
1663 warm ? ES::Warm : ES::ColdKeep);
1664 if (warm && !mg_.eigenWarmReady()) {
1666 mg_.estimateEigenvalues(
CCConst(rhs1), chebA_, chebB_, 15, 2, 2, 12, ES::ColdKeep);
1669 chebBoundsSet_ =
true;
1673 auto keepStart = [&]() {
1675 if (chebX0_.extent(0) != x1.extent(0))
1676 chebX0_ =
CCField(
"chebX0", x1.extent(0));
1677 Kokkos::deep_copy(
CCExec(), chebX0_, x1);
1680 auto restoreStart = [&]() {
1682 Kokkos::deep_copy(
CCExec(), x1, chebX0_);
1684 Kokkos::deep_copy(
CCExec(), x1, 0.0);
1691 auto coldSolve = [&]() {
1693 long it = mg_.solveChebyshev(rhs1, x1, chebMaxit_, chebRtol_, 2, 2, 12, chebA_, chebB_,
false,
1695 if (mg_.chebyshevGuardTripped()) {
1697 mg_.estimateEigenvalues(
CCConst(rhs1), chebA_, chebB_, 15, 2, 2, 12, ES::ColdKeep,
1700 chebLmaxFloor_ = chebB_;
1702 it += mg_.solveChebyshev(rhs1, x1, chebMaxit_, chebRtol_, 2, 2, 12, chebA_, chebB_);
1707 iters = coldSolve();
1710 iters = mg_.solveChebyshev(rhs1, x1, chebMaxit_, chebRtol_, 2, 2, 12, chebA_, chebB_,
true);
1711 if (iters >= chebMaxit_ || mg_.chebyshevGuardTripped()) {
1712 ++chebColdRestarts_;
1713 mg_.estimateEigenvalues(
CCConst(rhs1), chebA_, chebB_, 15, 2, 2, 12, ES::ColdKeep);
1716 iters += coldSolve();
1719 }
else if (ghostProjection_) {
1725#ifdef PECLET_FLOW_MPI
1728 mg_.solveBiCGStab(rhs1, x1, r_, gpRh_, pp_, Ap_, gpT_, z_, gpZ2_, pcgMaxit_, pcgRtol_, 2,
1729 2, 12, gpOv_, gpNRows_,
C3{nx_, ny_, nz_}, gpX2_, velDev_.get(), e_);
1732 iters = mg_.solveBiCGStab(rhs1, x1, r_, gpRh_, pp_, Ap_, gpT_, z_, gpZ2_, pcgMaxit_, pcgRtol_,
1733 2, 2, 12, gpOv_, gpNRows_,
C3{nx_, ny_, nz_});
1746 auto idMap = gpIdMap_;
1748 const C3 e1 = e1_, e2 = e_;
1749 const int lnx = nx_, lny = ny_;
1750 Kokkos::parallel_for(
1751 "peclet::flow::gp_pin_decoupled",
MDRange3<CCExec>(space, {0, 0, 0}, {nx_, ny_, nz_}),
1752 KOKKOS_LAMBDA(
int x,
int y,
int z) {
1754 (long)(x + 1) + (long)(y + 1) * e1.
x + (long)(z + 1) * (long)e1.
x * e1.
y;
1756 (long)(x +
G) + (long)(y +
G) * e2.x + (long)(z +
G) * (long)e2.x * e2.y;
1757 bool dec = sg(i2) < 0.0;
1759 const int s = idMap((
long)x + (
long)y * lnx + (
long)z * (
long)lnx * lny);
1760 if (s >= 0 && ov.coupled(s) == 0)
1767 }
else if (useFcg_) {
1772 if (zp1_.extent(0) != n1_)
1774 iters = mg_.solveFCG(rhs1, x1, r_, pp_, z_, zp1_, Ap_, pcgMaxit_, pcgRtol_, 2, 2, 12,
1775 fluidOnlyMode_ == 2 ? &starOv_ :
nullptr, nStar_,
C3{nx_, ny_, nz_});
1776 checkCgExit(rhs1, x1, iters,
"flexible-CG", fluidOnlyMode_ == 2 ? &starOv_ :
nullptr);
1778 iters = mg_.solvePCG(rhs1, x1, r_, pp_, z_, Ap_, pcgMaxit_, pcgRtol_, 2, 2, 12,
1779 fluidOnlyMode_ == 2 ? &starOv_ :
nullptr, nStar_,
C3{nx_, ny_, nz_});
1780 checkCgExit(rhs1, x1, iters,
"MG-PCG", fluidOnlyMode_ == 2 ? &starOv_ :
nullptr);
1785template <
class Gr
id>
1792 const bool failed = mg_.lastSolveFailed();
1793 lastPressureTrueResidual_ =
1794 mg_.cgTrueResidual(rhs1, x1, r_, Ap_, star, star ? nStar_ : 0,
C3{nx_, ny_, nz_});
1795 const bool capped = iters >= pcgMaxit_ || failed;
1796 const double limit = std::max(kCgGuardFactor, kCgGuardFloor / pcgRtol_);
1797 if (!capped && lastPressureTrueResidual_ <= limit)
1799 ++numPressureUnconvergedSolves_;
1800 if (numPressureUnconvergedSolves_ < cgGuardNextReport_)
1802 cgGuardNextReport_ *= 10;
1804#ifdef PECLET_FLOW_MPI
1806 MPI_Comm_rank(comm_, &r);
1811 "peclet::flow: the %s pressure solve did not converge (%s; true residual "
1812 "max|b - A x| = %.3g x the stop tolerance rtol*r0, limit %.3g, rtol %.1e) -- the "
1813 "field it returned is not a projection. %ld such solve%s so far "
1814 "(diagnostics.num_pressure_unconverged_solves; next report at %ld). Chebyshev, the "
1815 "variable-density default, is healthy at high density contrast "
1816 "(set_pressure_chebyshev).\n",
1818 failed ?
"broke down"
1819 : iters >= pcgMaxit_ ?
"capped"
1820 :
"stopped on its recurrence residual",
1821 lastPressureTrueResidual_, limit, pcgRtol_, numPressureUnconvergedSolves_,
1822 numPressureUnconvergedSolves_ == 1 ?
"" :
"s", cgGuardNextReport_);
1825template <
class Gr
id>
1832 if (!cgContrastPending_ || !varRho_ || rhoField_.extent(0) == 0 || mg_.symmetricVcycle())
1834 cgContrastPending_ =
false;
1837 double ext[2] = {-1e300, -1e300};
1838 for (
int k = 0; k < 2; ++k) {
1839 const double sg = k == 0 ? 1.0 : -1.0;
1840 Kokkos::parallel_reduce(
1841 "peclet::flow::cg_rho_extrema",
1843 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& acc) {
1844 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
1845 acc = Kokkos::fmax(acc, sg * f(i));
1847 Kokkos::Max<double>(ext[k]));
1849 double mx = ext[0], mn = -ext[1];
1851#ifdef PECLET_FLOW_MPI
1853 double g[2] = {-mn, mx}, o[2];
1854 MPI_Allreduce(g, o, 2, MPI_DOUBLE, MPI_MAX, comm_);
1857 MPI_Comm_rank(comm_, &r);
1860 if (r == 0 && mn > 0.0 && mx >= 1e3 * mn)
1863 "peclet::flow: set_pressure_%s selected under variable density with "
1864 "rho_max/rho_min = %.3g and the trilinear V-cycle (set_pressure_vcycle_symmetric(False)). "
1865 "Both CG drivers fail above a density ratio of ~1e3 with that V-cycle (the "
1866 "preconditioner turns indefinite); Chebyshev, the variable-density "
1867 "default, is healthy there (set_pressure_chebyshev). Unconverged CG solves are "
1868 "reported and counted (diagnostics.num_pressure_unconverged_solves).\n",
1869 useFcg_ ?
"fcg" :
"pcg", mx / mn);
1872template <
class Gr
id>
1877template <
class Gr
id>
1879 if constexpr (Grid::collocated) {
1880 projectPhiWallGhosts();
1881 projectCorrectFaceField();
1882 projectCorrectFaceFieldOutflow();
1887 projectCorrectCellField();
1889 projectCorrectStaggered();
1890 projectCorrectStaggeredOutflow();
1895 for (
int c = 0; c < 3; ++c)
1899template <
class Gr
id>
1900void Solver<Grid>::projectPhiWallGhosts() {
1906 B3 e{e_.x, e_.y, e_.z};
1907 for (
int a = 0; a < 3; ++a)
1908 for (
int s = 0; s < 2; ++s) {
1909 const int t = bc_[2 * a + s];
1910 if (t != 0 && t != 3 && touchesGlobalFace(2 * a + s))
1916template <
class Gr
id>
1917void Solver<Grid>::projectCorrectFaceField() {
1929 if (fluidOnlyMode_ == 2)
1931 C3{nx_, ny_, nz_}, e_,
G, e_,
G, exactResidual_, u_.w[0], u_.w[1], u_.w[2]);
1936 fillFaceGhostsKeepBoundary(uf_, 0);
1937 fillFaceGhostsKeepBoundary(vf_, 1);
1938 fillFaceGhostsKeepBoundary(wf_, 2);
1941template <
class Gr
id>
1942void Solver<Grid>::projectCorrectFaceFieldOutflow() {
1945 B3 e{e_.x, e_.y, e_.z};
1946 CCField fa[3] = {uf_, vf_, wf_};
1949 const bool var = varRho_ && !porous_ && outflowRhoCorr_;
1950 for (
int a = 0;
a < 3; ++
a)
1951 if (bc_[2 * a + 1] == 3 && touchesGlobalFace(2 * a + 1)) {
1960template <
class Gr
id>
1961void Solver<Grid>::projectCorrectCellField() {
1962 if (colocatedFaceForce()) {
1967 CCField oax[3] = {ox_, oy_, oz_};
1968 for (
int cc = 0; cc < 3; ++cc)
1970 CCConst(oax[cc]), varRho_, rho_, strideOf(cc), e_,
G, u_.w[cc]);
1971 }
else if (ghostProjection_ || faceInterp_ == 9) {
1977 for (
int cc = 0; cc < 3; ++cc) {
1982 }
else if (faceInterp_ == 5) {
1984 CCField xcs[3] = {xcx_, xcy_, xcz_};
1985 CCField oax[3] = {ox_, oy_, oz_};
1986 for (
int cc = 0; cc < 3; ++cc) {
1988 true, cc, e_,
G, u_.w[cc]);
1991 }
else if (faceInterp_ == 6 || faceInterp_ == 7) {
1994 CCConst(oz_), e_,
G, u_.w[0], u_.w[1], u_.w[2]);
1997 CCConst(oz_), e_,
G, u_.w[0], u_.w[1], u_.w[2]);
2001template <
class Gr
id>
2002void Solver<Grid>::projectCorrectStaggered() {
2003 if (porous_ && porousCons_)
2006 CCConst(dragBeta_), hasDrag_, rho_ / dt_, e_,
G, u_.w[0], u_.w[1],
2011 e_,
G, u_.w[0], u_.w[1], u_.w[2]);
2012 else if (varRho_ && rhoFaceHarmonic_)
2014 u_.w[0], u_.w[1], u_.w[2]);
2017 u_.w[0], u_.w[1], u_.w[2]);
2022template <
class Gr
id>
2023void Solver<Grid>::projectCorrectStaggeredOutflow() {
2026 B3 e{e_.x, e_.y, e_.z};
2036 const bool var = varRho_ && !porous_ && outflowRhoCorr_;
2037 for (
int a = 0;
a < 3; ++
a)
2038 if (bc_[2 * a + 1] == 3 && touchesGlobalFace(2 * a + 1)) {
2044 outflowCorrValid_ =
true;
2048template <
class Gr
id>
2054 if (rotFilter_ && rotationalP_)
2055 for (
int a = 0; a < 3; ++a)
2058 CCField P = P_, ph = phi_, d = div_;
2064 const double ct = pressUnderRelax_ * rho_ / dt_, mu = rotationalP_ ? rotWeight_ * mu_ : 0.0;
2070 if (varRotMode_ == 1) {
2072 const double chi = varRotChi_;
2073 Kokkos::parallel_for(
2074 "press_var_full", Kokkos::RangePolicy<CCExec>(space, 0, n_),
2075 KOKKOS_LAMBDA(std::size_t i) { P(i) += ct * ph(i) - chi * mf(i) * d(i); });
2077 const double muRot = (varRotMode_ == 2) ? 0.0 : varRotChi_ *
minMuInner();
2078 Kokkos::parallel_for(
2079 "press_var_min", Kokkos::RangePolicy<CCExec>(space, 0, n_),
2080 KOKKOS_LAMBDA(std::size_t i) { P(i) += ct * ph(i) - muRot * d(i); });
2082 }
else if (rotWallW_ > 0.0 && rotationalP_) {
2092 const double w0 = rotWallW_, muF = rotWeight_ * mu_;
2094 Kokkos::parallel_for(
2095 "press_wallblend", Kokkos::RangePolicy<CCExec>(space, 0, n_),
2096 KOKKOS_LAMBDA(std::size_t i) {
2097 const long sy = e.x, sz = (long)e.x * e.y;
2098 const int x = (int)(i % e.x), y = (int)((i / e.x) % e.y), z = (int)(i / sz);
2100 if (x > 0 && y > 0 && z > 0 && x < e.x - 1 && y < e.y - 1 && z < e.z - 1 &&
2102 (sd(i - 1) < 0.0 || sd(i + 1) < 0.0 || sd(i - sy) < 0.0 || sd(i + sy) < 0.0 ||
2103 sd(i - sz) < 0.0 || sd(i + sz) < 0.0))
2105 P(i) += (ct + w * muF) * ph(i) - (1.0 - w) * muF * d(i);
2108 Kokkos::parallel_for(
2109 "press", Kokkos::RangePolicy<CCExec>(space, 0, n_),
2110 KOKKOS_LAMBDA(std::size_t i) { P(i) += ct * ph(i) - mu * d(i); });
2115 Kokkos::deep_copy(epsPrev_, epsField_);
void setZeroInitialGuess(bool on)
void clearProjectedGuess()
static constexpr double kChebBracket
void setStopReference(double ref)
void projectAssembleDivergence()
void noteChebyshevGrowthTrip(long iters)
void projectPressureUpdate()
void fillVelGhosts(int comp, int fold)
void addCsfRhs(int c, CCField dst, bool rowScaled, bool faceRange=false)
void registerBalancedForceState()
void buildRhsVarMom(int c)
bool advectsMomentum() const
void checkCgExit(CCField rhs1, CCField x1, long iters, const char *which, const StarOverlay *star)
bool balancedForceProjection() const
void buildOpenFaceField()
Build ufOpen_ = the face field with each open (outflow) domain face overwritten by its zero-gradient ...
long solveBalancedForceSystem(double bref, double r0)
void fillGhosts(CCField f)
void fillPropGhosts(CCField f, bool edges=false)
void requireCollocatedFaceForceScope(const char *who)
void pcApplyDivergenceSource(CCField div)
Subtract the phase-change (and any prescribed) divergence source from div_ so the deflated pressure s...
bool prefactorHoistEligible() const
CCField addField(const std::string &name)
void buildRhsColoVar(int c)
CCConst advVelView(int c) const
The velocity view the advection operators must read for component c: the wall-corrected scratch while...
void copyInner(CCField dst, C3 de, int dg, CCConst src, C3 se, int sg)
bool ufAdvVelocity() const
Are the advection operators reading the projected face field this step?
bool lastBalancedForceFailed() const
CCConst openFaceView(int c) const
Component c of the face field AS THE ADVECTION AND THE DIVERGENCE DIAGNOSTIC MUST READ IT: advFaceVie...
void addWallFluxDivergence(CCField d)
long lastBalancedForceIterations() const
bool hasOpenDomainFace() const
void constraintDivergence(CCConst fx, CCConst fy, CCConst fz, CCField out)
void projectBuildCoefficients()
long balancedForceFailures() const
void applySuperficialVelocity()
void applyBalancedForceProjection()
long strideOf(int c) const
void collocatedV8AutoFallback(const char *why)
long solvePressureSystem(CCField rhs1, CCField x1)
const char * projectedGuessIneligible() const
void superficialVelocityPrecheck() const
bool balancedForceActive() const
void setSolidVelocityMgAuto()
void requireBalancedForceScope(const char *who, bool atStep) const
void filterCellField(CCField f, int axis)
bool ensureAdvStash(int c, bool adv)
bool bcStencilPath() const
void addCsfRhsCellInterp(int c, CCField dst, bool rowScaled, bool faceRange=false)
void setBalancedForceProjection(bool enabled)
void buildRhsForced(int c)
void copyBlockShifted(CCField dst, C3 de, CCConst src, C3 se, int off)
void buildBalancedFaceForce(int c)
void fillPorousEpsGhosts()
bool colocatedFaceForce() const
void projectCorrectVelocities()
void buildRhs(CCField b, CCConst cOld, CCConst kappa, CCConst unk, double idt, double srcConst, CCConst src, bool useField, double srcFactor, C3 e, int g)
Base right-hand side: b = kappa (idt c^n + S'), identity rows 0.
double weightSum(double oLo, double oHi)
double cellFromFaces(double aLo, double aHi, double oLo, double oHi)
void stencilMatvec(CCField y, CCConst u, MC AC, MC AW, MC AE, MC AS, MC AN, MC AB, MC AT, C3 e, int g)
Kokkos::DefaultExecutionSpace CCExec
void bcCorrectOutflow(BField f, BField phi, B3 ext, int g, int a, double wa=1.0)
void buildRhoCoeffOutflowFace(CCField ca, CCConst oa, CCConst rho, double rho0, C3 e, int g, int a, bool harmonic)
void gpDivergDelta(CCField d, CCConst u, CCConst v, CCConst w, const GpOverlayReal< Real > &ov, int nOv, C3 nn, C3 extb, int gb, bool useGhost=false)
Overlay divergence correction: d(r) = rho_r * (d(r) + closure/BC/explicit face values),...
void buildPorousCoeffDrag(CCField cx, CCField cy, CCField cz, CCConst ox, CCConst oy, CCConst oz, CCConst eps, CCConst beta, double idt, C3 e, int g)
Kokkos::MDRangePolicy< Exec, Kokkos::Rank< 3, Kokkos::Iterate::Left, Kokkos::Iterate::Left > > MDRange3
3-D range, x (the first index) fastest (both the tile order and the order within a tile).
void centerGradOpen(CCField out, CCConst p, CCConst o, int axis, C3 e, int g, double wa=1.0)
void projectCorrectPorousDrag(CCField u, CCField v, CCField w, CCConst phi, CCConst beta, double idt, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0)
void buildRhoCoeff(CCField cx, CCField cy, CCField cz, CCConst ox, CCConst oy, CCConst oz, CCConst rho, double rho0, C3 e, int g)
void buildPorousCoeffCons(CCField cx, CCField cy, CCField cz, CCConst ox, CCConst oy, CCConst oz, CCConst eps, CCConst beta, bool useBeta, double rhoidt, C3 e, int g)
void bcZeroPressureGhost(BField phi, B3 ext, int g, int a, int s)
void subtractField(CCField u, CCConst d, C3 e, int g)
Kokkos::View< const double *, CCMem > CCConst
void transposeGradWallAware(CCField out, CCConst p, CCConst sdf, CCConst o, CCConst xc, bool useCen, int axis, C3 e, int g, double wa=1.0)
void divergOpen(CCConst u, CCConst v, CCConst w, CCConst ox, CCConst oy, CCConst oz, CCField d, C3 e, int g)
void projectCorrect(CCField u, CCField v, CCField w, CCConst phi, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0)
void projectCorrectCenterOpen(CCField u, CCField v, CCField w, CCConst phi, CCConst ox, CCConst oy, CCConst oz, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0)
void correctCellFaceAverageVar(CCField u, CCConst phi, CCConst rho, CCConst o, bool haveRho, double rho0, long s, C3 e, int g, double wa=1.0)
Kokkos::View< const MReal *, CCMem > FPC
void projectCorrectPorousCons(CCField u, CCField v, CCField w, CCConst phi, CCConst eps, CCConst beta, bool useBeta, double rhoidt, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0)
void buildFaceAccelVar(CCField af, CCConst P, CCConst rho, CCConst fb, bool haveFb, bool haveRho, double rhoC, double fc, bool incr, double scale, long s, C3 e, int g, double wa=1.0)
void centerToFaceWallAware(CCField uf, CCField vf, CCField wf, CCConst U, CCConst V, CCConst W, CCConst sdf, CCConst xcx, CCConst xcy, CCConst xcz, bool useCen, C3 e, int g)
double maxAbsInner(CCConst a, C3 e, int g)
void starCorrectFaces(CCField uf, CCField vf, CCField wf, CCConst phi, const StarOverlay &ov, int nOv, C3 nn, C3 ext, int g, C3 extP, int gP, bool exact, double wx=1.0, double wy=1.0, double wz=1.0)
Fix the face correction at fluid|solid faces: projectCorrect applied -(phi_hi - phi_lo) with the soli...
void projectCorrectVar(CCField u, CCField v, CCField w, CCConst phi, CCConst rho, double rho0, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0)
Kokkos::View< double *, CCMem > CCField
void ccFor3(const char *name, C3 lo, C3 hi, F f)
void centerToFace(CCField uf, CCField vf, CCField wf, CCConst U, CCConst V, CCConst W, C3 e, int g)
void embedViscousApply(CCField Lu, CCConst U, CCConst sdf, CCConst cs, CCConst ox, CCConst oy, CCConst oz, double mu, double idt, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0, double hpx=1.0, double hpy=1.0, double hpz=1.0)
double maxAbsDiffInner(CCConst a, CCConst b, C3 e, int g)
void buildPorousCoeff(CCField cx, CCField cy, CCField cz, CCConst ox, CCConst oy, CCConst oz, CCConst eps, C3 e, int g)
void buildRhoCoeffHarm(CCField cx, CCField cy, CCField cz, CCConst ox, CCConst oy, CCConst oz, CCConst rho, double rho0, C3 e, int g)
void projectCorrectCenter(CCField u, CCField v, CCField w, CCConst phi, CCConst ox, CCConst oy, CCConst oz, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0)
void centerToFaceMassWeighted(CCField uf, CCField vf, CCField wf, CCConst U, CCConst V, CCConst W, CCConst rho, C3 e, int g)
void divergOpenEps(CCConst u, CCConst v, CCConst w, CCConst ox, CCConst oy, CCConst oz, CCConst eps, CCField d, C3 e, int g)
void projectCorrectVarHarm(CCField u, CCField v, CCField w, CCConst phi, CCConst rho, double rho0, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0)
void addFaceAccelCsf(CCField af, CCConst cv, CCConst kp, CCConst kb, CCConst rho, bool haveRho, double rhoC, double sigma, double hGrad, double scale, long s, C3 e, int g)
void gpCenterGrad(CCField out, CCConst p, CCConst sdf, int axis, C3 e, int g, double wa=1.0)
Directional cell-center gradient (collocated ghost path) of a cell field p whose solid-centered rows ...
void bcCorrectOutflowVar(BField f, BField phi, BField rho, double rho0, B3 ext, int g, int a, bool harmonic, double wa=1.0)
void bcNeumannGhost(BField f, B3 ext, int g, int a, int s)
One entry per eliminated solid-centered cell: packed INNER flat index + the apertures of its (up to 6...
static constexpr double AC
static constexpr double U