8#ifndef PECLET_FLOW_FLOW_IBM_CLOSURES_HPP
9#define PECLET_FLOW_FLOW_IBM_CLOSURES_HPP
16 for (
int face = 0; face < 6; ++face)
17 if (bc_[face] != 0 && touchesGlobalFace(face))
33 for (
int c = 0; c < 3; ++c)
47 for (
int face = 0; face < 6; ++face) {
48 const int t = bc_[face];
49 if (t == 0 || !touchesGlobalFace(face))
60 return varRho_ || (porous_ && porousCons_);
65 return varRho_ ? rhoField_ : epsRho_;
73 const double rho = rho_;
75 "peclet::flow::eps_rho", Kokkos::RangePolicy<CCExec>(space, 0, n_),
76 KOKKOS_LAMBDA(std::size_t i) { er(i) = ep(i) * rho; });
106 if (form ==
"auto") {
107 viscousStressAuto_ =
true;
111 if (form ==
"laplacian") {
112 viscousStressAuto_ =
false;
114 fullStressAuto_ =
false;
118 throw std::invalid_argument(
119 "set_viscous_stress: form must be 'auto', 'full' or 'laplacian', got '" + form +
"'");
120 if constexpr (Grid::collocated)
121 throw std::runtime_error(
122 "SolverColocated.set_viscous_stress('full'): the full viscous stress is staggered-only in "
123 "v1 (doc/full_viscous_stress.md §9); use peclet.flow.Solver");
124 viscousStressAuto_ =
false;
125 fullStressAuto_ =
false;
131 return (fullStress_ || fullStressAuto_) && varProps_ && !Grid::collocated;
143 fullStressAuto_ =
false;
146 const char* why =
nullptr;
147 if constexpr (Grid::collocated)
148 why =
"the collocated grid (staggered-only in v1, §9)";
149 else if (harmonicMu_)
150 why =
"the harmonic face viscosity";
152 why =
"porous (eps-weighted) momentum";
154 why =
"moving scene instances";
157 "an immersed solid (the staircase drops the transpose flux, an O(gamma h) near-wall error "
158 "on oblique walls; repair open)";
159 if (why ==
nullptr) {
160 fullStressAuto_ =
true;
163 if (!fullStressAutoNotice_) {
164 fullStressAutoNotice_ =
true;
166 "peclet::flow %s: AUTO viscous stress fell back to the Laplacian form div(mu grad u) "
167 "(%s is outside the full stress's v1 scope, doc/full_viscous_stress.md §5.6). Select "
168 "explicitly with set_viscous_stress to silence this notice.\n",
169 Grid::collocated ?
"SolverColocated" :
"Solver", why);
175 const auto refuse = [&](
const char* why) {
176 throw std::runtime_error(std::string(who) +
": set_viscous_stress('full') " + why +
177 " (doc/full_viscous_stress.md §5.6); call "
178 "set_viscous_stress('laplacian') for this configuration");
182 "does not support the harmonic face viscosity (the series direction is "
183 "component-dependent and breaks the symmetry of the transpose term)");
186 "does not support porous (eps-weighted) momentum: the eps-weighted stress is a "
187 "different equation");
190 "does not support moving scene instances (the regular-node masks would have to follow "
202 double lo = 0.0, hi = 0.0;
204#ifdef PECLET_FLOW_MPI
206 double g[2] = {lo, -hi}, r[2];
207 MPI_Allreduce(g, r, 2, MPI_DOUBLE, MPI_MIN, comm_);
212 muUniform_ = (lo == hi);
218 for (
int c = 0; c < 3; ++c)
221 if (hasSolid_ && regDirty_)
228 const std::size_t n = (std::size_t)n_;
232 Kokkos::view_alloc(space,
"peclet::flow::full_stress_reg_tmp", Kokkos::WithoutInitializing),
234 for (
int c = 0; c < 3; ++c) {
235 if (fsReg_[c].extent(0) != n)
237 Kokkos::view_alloc(space,
"peclet::flow::full_stress_reg", Kokkos::WithoutInitializing),
240 Kokkos::parallel_for(
241 "peclet::flow::full_stress_reg_mask", Kokkos::RangePolicy<CCExec>(space, 0, n),
242 KOKKOS_LAMBDA(std::size_t i) { tmp(i) = (m(i) == 0.0) ? 1.0 : 0.0; });
243 const auto ov = C[c].ov;
244 Kokkos::parallel_for(
245 "peclet::flow::full_stress_reg_cut", Kokkos::RangePolicy<CCExec>(space, 0, C[c].nCut),
246 KOKKOS_LAMBDA(
int k) { tmp(ov.cell_index(k)) = 0.0; });
249 Kokkos::parallel_for(
250 "peclet::flow::full_stress_reg_pack", Kokkos::RangePolicy<CCExec>(space, 0, n),
251 KOKKOS_LAMBDA(std::size_t i) { r(i) = (tmp(i) != 0.0) ? 1 : 0; });
260 const bool skipLo = bc_[2 * c] != 0 && touchesGlobalFace(2 * c);
268 u_.w[c], skipLo, svRhsList_, svRhsN_);
272 const RegConst reg[3] = {fsReg_[0], fsReg_[1], fsReg_[2]};
274 G, u_.w[c], skipLo, reg);
288 Kokkos::parallel_for(
289 "peclet::flow::div_adv",
290 MD(space, {
G - 1,
G - 1,
G - 1}, {e.x -
G + 1, e.y -
G + 1, e.z -
G + 1}),
291 KOKKOS_LAMBDA(
int x,
int y,
int z) {
292 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
293 const long i = (long)x + (
long)y * sy + (long)z * sz;
294 dv(i) = (
U(i + sx) -
U(i)) + (V(i + sy) - V(i)) + (W(i + sz) - W(i));
300 const std::string& in0,
const std::string& in1,
301 const std::vector<double>& params) {
302 if (target ==
"mu" && vmgExplicit_ && useVelocityMg_)
306 cl.
out = ensureTarget(target);
322 for (
int k = 0; k < 4 && k < (int)params.size(); ++k)
323 cl.
pPhys[k] = params[k];
326 closures_.push_back(cl);
339 if (fields_.has(
"rho"))
340 rhoField_ = fields_.at(
"rho").data;
343 Kokkos::deep_copy(rhoField_, rho_);
345 if (rho1_.extent(0) == 0) {
351 ensureCellForceAll();
352 useVelocityMg_ =
false;
365 useChebyshev_ =
true;
366 chebBoundsSet_ =
false;
375 return cutcellPressure_;
380 if constexpr (Grid::collocated) {
382 throw std::runtime_error(
"set_porous_continuity: staggered-only (v1)");
386 if (fields_.has(
"eps"))
387 epsField_ = fields_.at(
"eps").data;
390 Kokkos::deep_copy(epsField_, 1.0);
392 if (epsPrev_.extent(0) == 0) {
393 epsPrev_ =
CCField(
"epsPrev", n_);
394 depsdt_ =
CCField(
"depsdt", n_);
396 if (divAdv_.extent(0) == 0)
397 divAdv_ =
CCField(
"divAdv", n_);
398 if (epsRho_.extent(0) == 0)
399 epsRho_ =
CCField(
"epsRho", n_);
404 ensureCellForceAll();
405 Kokkos::deep_copy(epsPrev_, epsField_);
406 if (eps1_.extent(0) == 0)
408 if (beta1_.extent(0) == 0)
409 beta1_ =
CCField(
"beta1", n1_);
410 if (rho1_.extent(0) == 0) {
425 useChebyshev_ =
true;
426 chebBoundsSet_ =
false;
437 Kokkos::deep_copy(epsPrev_, epsField_);
452 pressUnderRelax_ = w;
459 if (variable && vmgExplicit_ && useVelocityMg_)
461 varProps_ = variable;
462 harmonicMu_ = harmonic;
464 if (fields_.has(
"mu"))
465 muField_ = fields_.at(
"mu").data;
468 Kokkos::deep_copy(muField_,
478 varRotMode_ = mode < 0 ? 0 : (mode > 2 ? 2 : mode);
479 varRotChi_ = chi < 0.0 ? 0.0 : chi;
484 const std::vector<double>& xs,
const std::vector<double>& ys) {
487 cl.
out = ensureTarget(target);
491 cl.
nTab = (int)std::min(xs.size(), ys.size());
494 auto hx = Kokkos::create_mirror_view(cl.
tabX);
495 auto hy = Kokkos::create_mirror_view(cl.
tabY);
496 for (
int k = 0; k < cl.
nTab; ++k) {
500 Kokkos::deep_copy(cl.
tabX, hx);
501 Kokkos::deep_copy(cl.
tabY, hy);
505 closures_.push_back(cl);
511 return u_.rhoToInt();
514 if (target ==
"force_x" || target ==
"force_y" || target ==
"force_z")
515 return u_.forceToInt(target[6] -
'x');
523 "peclet.flow set_property_model NOTICE: a physical domain is armed and target "
524 "'%s' is not rho/mu/force_x/y/z, so this closure's parameters are taken in "
525 "that field's INTERNAL units (see the `unit_scales` property).\n",
537 auto hy = Kokkos::create_mirror_view(cl.
tabY);
538 for (
int k = 0; k < cl.
nTab; ++k)
539 hy(k) = cl.
tabYPhys[(std::size_t)k] * kp;
540 Kokkos::deep_copy(cl.
tabY, hy);
545 for (
int k = 0; k < 4; ++k)
546 cl.
p[k] = (k < nScaled) ? cl.
pPhys[k] * kp : cl.
pPhys[k];
551 for (
auto& cl : closures_)
557 ensureCellForceAll();
562 if (!fields_.has(
"drag_beta"))
565 dragBeta_ = fields_.at(
"drag_beta").data;
566 ensureCellForceAll();
573 if (!(porous_ && hasDrag_))
575 pressGraphAmg_ =
true;
578 if (cutcellPressure_)
579 mg_.setAgglomerationMode(1);
580 useChebyshev_ =
false;
581 chebBoundsSet_ =
false;
607 Kokkos::parallel_for(
608 "peclet::flow::add_drag_diag", MD(space, {
G,
G,
G}, {e.x -
G, e.y -
G, e.z -
G}),
609 KOKKOS_LAMBDA(
int x,
int y,
int z) {
610 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
611 const double bd = Grid::atVelocity(beta, i, sc);
620 if (mode ==
"auto") {
621 sharpMode_ = SharpMode::kAuto;
624 if (mode ==
"sharp") {
625 if constexpr (Grid::collocated)
626 throw std::runtime_error(
627 "SolverColocated.set_vof_viscous_interface('sharp'): the sharp viscous interface is "
628 "staggered-only (doc/vof_sharp_viscous_stress.md §8; 'auto' resolves to 'averaged' "
629 "here); use peclet.flow.Solver");
630 sharpMode_ = SharpMode::kSharp;
633 if (mode ==
"averaged") {
634 sharpMode_ = SharpMode::kAveraged;
635 sharpActive_ =
false;
638 throw std::invalid_argument(
"set_vof_viscous_interface: unknown mode '" + mode +
639 "' (expected 'auto', 'sharp' or 'averaged')");
644 switch (sharpMode_) {
645 case SharpMode::kSharp:
647 case SharpMode::kAveraged:
660 sharpActive_ =
false;
661 if (sharpMode_ == SharpMode::kAveraged)
663 const bool expl = sharpMode_ == SharpMode::kSharp;
664 const char* why =
nullptr;
666 for (
const auto& c : closures_)
667 if (c.outName ==
"mu")
672 why =
"needs the VoF colour field (enable_vof)";
673 }
else if (expl && harmonicMu_)
674 why =
"replaces the harmonic face viscosity; it does not combine with it";
677 else if (Grid::collocated)
678 why =
"is staggered-only (doc/vof_sharp_viscous_stress.md §8)";
679 else if (harmonicMu_)
680 why =
"replaces the harmonic face viscosity; it does not combine with it";
682 why =
"does not support immersed solids (v1)";
684 why =
"does not support porous (eps-weighted) momentum";
686 why =
"does not support moving scene instances";
688 why =
"does not support phase change ([u] != 0 breaks the kink model)";
690 why =
"needs the full viscous stress (set_viscous_stress('auto' or 'full'))";
693 "supports mu = LinearMix(C) only, set_property_model('mu', 'linear', 'C', [mu_0, mu_1 - "
694 "mu_0]) (v1, §12 Q7)";
696 if (why ==
nullptr) {
703 if (!(g.muA > 0.0 && g.muB > 0.0))
704 why =
"needs positive phase viscosities";
706 if (why !=
nullptr) {
708 throw std::runtime_error(std::string(
"set_vof_viscous_interface('sharp') ") + why +
709 " (doc/vof_sharp_viscous_stress.md §1, §9); call "
710 "set_vof_viscous_interface('averaged') for this configuration");
711 if (!sharpAutoNotice_) {
712 sharpAutoNotice_ =
true;
714 "peclet::flow %s: AUTO viscous interface fell back to 'averaged' (the sharp "
715 "interface %s; doc/vof_sharp_viscous_stress.md §9). Select explicitly with "
716 "set_vof_viscous_interface to silence this notice.\n",
717 Grid::collocated ?
"SolverColocated" :
"Solver", why);
721 g.lowA = g.muA < g.muB;
722 g.mulo = g.lowA ? g.muA : g.muB;
723 g.r = g.lowA ? g.muA / g.muB : g.muB / g.muA;
735 if (vofCurv_.useWorklist)
736 vofCurv_.compact(vofAdv_.colour());
737 vofCurv_.reconstructPlanes(vofAdv_.colour());
739 const std::size_t n = (std::size_t)n_;
740 if (svCls_.extent(0) != n) {
743 Kokkos::view_alloc(space,
"peclet::flow::sharp_cls", Kokkos::WithoutInitializing), n);
744 svFb_ =
vof::SvByte(Kokkos::view_alloc(space,
"peclet::flow::sharp_fb"), n);
745 svFit_ =
CCField(Kokkos::view_alloc(space,
"peclet::flow::sharp_fit"), 4 * n);
746 for (
int a = 0; a < 3; ++a)
747 svTau_[a] = Kokkos::subview(svFit_, std::make_pair(a * n, (a + 1) * n));
748 svDnn_ = Kokkos::subview(svFit_, std::make_pair(3 * n, 4 * n));
749#ifdef PECLET_FLOW_MPI
753 Kokkos::view_alloc(space,
"peclet::flow::sharp_list", Kokkos::WithoutInitializing), n);
755 Kokkos::view_alloc(space,
"peclet::flow::sharp_rhs_list", Kokkos::WithoutInitializing), n);
758 const int ee[3] = {e_.x, e_.y, e_.z};
764 g.st[2] = (long)e_.x * e_.y;
767 g.st3[2] = (long)e3_.x * e3_.y;
769 g.col =
CCConst(vofAdv_.colour());
770 for (
int a = 0; a < 3; ++a)
771 g.pm[a] =
CCConst(vofCurv_.planeM(a));
772 g.pa =
CCConst(vofCurv_.planeAlpha());
776 constexpr int kOpen = 1 << 28;
777 for (
int a = 0; a < 3; ++a) {
778 g.dlo[a] = (bc_[2 * a] != 0 && touchesGlobalFace(2 * a)) ?
G : -kOpen;
779 g.dhi[a] = (bc_[2 * a + 1] != 0 && touchesGlobalFace(2 * a + 1)) ? ee[a] -
G : kOpen;
787 g.eps = csfInterfaceEps_;
788 const vof::VofMetric gm = u_.vofMetric();
790 for (
int a = 0; a < 3; ++a)
801 vof::sharpFit(sharpGeom_, v, svTau_[0], svTau_[1], svTau_[2], svDnn_, svFb_,
C3{
G,
G,
G},
802 C3{e_.x -
G, e_.y -
G, e_.z -
G}, svList_);
804#ifdef PECLET_FLOW_MPI
808 MPI_Comm_rank(comm_, &rank);
809 svHalo_ = std::make_shared<GridHaloTopology<3>>();
810 svHalo_->buildTopology(*dec_, rank,
G, {
true,
true,
true}, comm_,
811 peclet::core::halo::Neighborhood::Full, kSvHaloDepth);
812 svDev_ = std::make_shared<GridHalo<double>>();
813 svDev_->setLabel(
"sharp fit");
814 svDev_->init(*svHalo_, 4, (
long)n_);
816 svDev_->exchange(svFit_);
820 for (
int a = 0; a < 3; ++a)
837 if (!sharpActive_ || svCls_.extent(0) != (std::size_t)n_)
844 std::array<std::vector<double>, 5> out;
845 if (!sharpActive_ || svCls_.extent(0) != (std::size_t)n_)
847 for (
int a = 0; a < 3; ++a)
851 CCField f(Kokkos::view_alloc(space,
"peclet::flow::sharp_fb_out", Kokkos::WithoutInitializing),
854 Kokkos::parallel_for(
855 "peclet::flow::sharp_fb_out", Kokkos::RangePolicy<CCExec>(space, 0, (
long)n_),
856 KOKKOS_LAMBDA(
long i) { f(i) =
fb(i) ? 1.0 : 0.0; });
bool hasCutcellPressure() const
static constexpr int kVofG
void registerBalancedForceState()
void fillDragBetaGhosts()
void fillGhosts(CCField f)
void resolveSharpViscous()
void fillPropGhosts(CCField f, bool edges=false)
void buildFullStressRegMasks()
void setPropertyMode(bool variable, bool harmonic)
CCField addField(const std::string &name)
std::string vofViscousInterface() const
void requireFullStressScope(const char *who) const
void addFullStressRhsComp(int c)
void sharpViscousGeometry()
vof::SharpCensus vofViscousInterfaceCensus() const
void setPropertyModel(const std::string &target, ClosureKind kind, const std::string &in0, const std::string &in1, const std::vector< double > ¶ms)
void applyScalarBcFace(CCField c, int a, int side, int type, double val, bool edges=false)
void addDragDiagonal(int c)
void setVariableRotational(int mode, double chi)
void setPressureUnderRelax(double w)
void closureUnitsNotice(const std::string &target) const
Under a physical domain, say on stderr that a closure on a target of unknown dimension takes its para...
void resolveViscousStressAuto()
void setPorousDepsDt(bool on)
bool fullStressActive() const
void closureRefreshUnits(Closure &cl)
Re-derive a closure's internal p / tabY from its physical pPhys / tabYPhys with the current reference...
void setPorousConservative(bool on)
std::array< std::vector< double >, 5 > vofViscousInterfaceFit()
long strideOf(int c) const
void fillVelGhostsTo(CCField f, int comp, int fold, bool doOutflow=true, bool faceEdges=false)
void collocatedV8AutoFallback(const char *why)
void setVofViscousInterface(const std::string &mode)
void setViscousStress(const std::string &form)
void throwVelocityMgVariableMu(const char *who) const
void setDensityMode(bool variable)
void fillCellForceGhosts()
Kokkos::View< MReal *, CCMem > FV
void setPropertyTable(const std::string &target, const std::string &in0, const std::vector< double > &xs, const std::vector< double > &ys)
void configurePorousDragSolver()
double closureTargetToInt(const std::string &target) const
The internal-per-physical factor of a closure TARGET: rho, mu, force_x/y/z; 0.0 for any other field (...
vof::SharpFaceProps makeSharpFaceProps(int c, vof::SvRole role)
VarFaceProps makeFaceProps(int c)
void vofBcPropGhosts(CCField f)
WO-R item 5 — the Neumann property policy at an inflow face.
void setPorousContinuity(bool on)
std::vector< double > gatherInner(CCField fld)
void fillPorousEpsGhosts()
void sharpFit(const SharpGeom &gg, const CCConst(&v)[3], CCField t0, CCField t1, CCField t2, CCField dnn, SvByte fb, C3 lo, C3 hi, SvList list)
The τ̂/D̂_nn fit (§5.3) over the cells [lo, hi) of the solver block: interfacial cells get their fit ...
SharpCensus sharpCensus(const SharpGeom &gg, SvByteConst fb, C3 lo, C3 hi)
Kokkos::View< std::uint8_t *, CCMem > SvByte
Per-cell byte fields on the solver block: the band class and the fit-fallback flag.
Kokkos::View< long *, CCMem > SvList
The compacted interfacial cells of a fit (local linear indices), the device fit's worklist.
void sharpClassify(const SharpGeom &gg, SvByte cls)
The band classes over the WHOLE solver block (§5.2, §5.7 Q-B).
void addSharpViscousRhs(int c, CCField bb, CCConst rs, const CCConst(&v)[3], const SharpGeom &gg, const CCConst(&tau)[3], CCConst dnn, int g, double wc, bool skipLoC, SvList list, long nl)
The explicit band terms of the u_c rows over the inner cells [g, e−g)³ (§5.4): the τ-terms of the fou...
long sharpRhsCompact(const SharpGeom &gg, int g, SvList list)
The cells of the inner box [g, e−g)³ whose band terms (addSharpViscousRhs) can be nonzero for some co...
SvRole
Which half of the time split a SharpFaceProps feeds (§6.1, WO-5b): the implicit band (ibmBuildDiffusi...
Kokkos::DefaultExecutionSpace CCExec
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).
Kokkos::View< const double *, CCMem > CCConst
Kokkos::View< const std::uint8_t *, CCMem > RegConst
void addFullStressRhs(int c, CCField bb, CCConst rs, const CCConst(&v)[3], FaceProps fp, C3 e, int g, double wc, bool skipLoC, const RegConst(®)[3]={RegConst(), RegConst(), RegConst()})
Add rs_c · T_c(uⁿ) to the momentum RHS bb of component c over the inner cells [g, e−g)³.
Kokkos::View< double *, CCMem > CCField
void muRangeInner(CCConst mu, C3 e, int g, double &lo, double &hi)
The local min and max of mu over [g−1, e−g+1)³ — the inner cells plus the one ghost ring the transpos...
void applyClosure(const Closure &cl, C3 e, int g)
Kokkos::View< std::uint8_t *, CCMem > RegMask
Per-component regular-node mask (§5.4): 1 = a velocity node with neither a solid nor a cut row.
std::array< double, 4 > p
std::vector< double > tabYPhys
std::array< double, 4 > pPhys
The census of the band (§9, G-SV10) over [lo, hi), LOCAL to this block: interfacial cells,...
The face viscosity under the sharp interface (§5.4 a, §6): a band edge across a (a ≠ c) takes μ̄_c,...
The band geometry and the closure constants, by value into every kernel.
static constexpr double AC
static constexpr double U