8#ifndef PECLET_FLOW_FLOW_IBM_SCALARS_HPP
9#define PECLET_FLOW_FLOW_IBM_SCALARS_HPP
19 sc.cOld =
CCField(name +
"_old", n_);
20 sc.b =
CCField(name +
"_b", n_);
21 sc.AC =
CCField(name +
"_AC", n_);
22 sc.AW =
CCField(name +
"_AW", n_);
23 sc.AE =
CCField(name +
"_AE", n_);
24 sc.AS =
CCField(name +
"_AS", n_);
25 sc.AN =
CCField(name +
"_AN", n_);
26 sc.AB =
CCField(name +
"_AB", n_);
27 sc.AT =
CCField(name +
"_AT", n_);
33 sc.D = D * u_.diffToInt();
35 sc.iters = iters < 1 ? 1 : iters;
38 sc.cut = std::make_shared<ScalarCutState>();
40 scalars_.push_back(sc);
45 for (
const auto& sc : scalars_)
53 for (
auto& sc : scalars_)
54 if (sc.name == name) {
56 sc.bcVal[face] =
value;
58 sc.cut->hasProfile[face] =
false;
61 throw std::runtime_error(
"set_scalar_bc: no scalar named '" + name +
"'");
68 const double idt = 1.0 / dt_;
70 if constexpr (Grid::collocated) {
87 const vof::VofMetric gmS = u_.vofMetric();
88 for (
auto& sc : scalars_) {
97 const bool energy = sc.energy && sc.kcell.extent(0) == n_;
98 const bool hasMask = sc.dmask.extent(0) == n_;
102 CCConst(sc.dmask), idt, e_,
G, u_.w[0], u_.w[1], u_.w[2]);
103 applyScalarBcStencilVar(sc);
108 applyScalarBcStencil(sc);
112 const bool gfm = hasMask && pcPlaneDir_ && sc.gfmB.extent(0) == n_ && pcGphi_.extent(0) == n_;
116 const bool gfm2 = gfm && (pcGfmOrder_ >= 2 || (pcCurvDist_ && pcKappa_.extent(0) == n_));
121 CCConst(pcCurvDist_ && pcKappa_.extent(0) == n_ ? pcKappa_ : pcGphi_),
122 CCConst(sc.kcell), sc.D, energy, pcGfmThMin_, pcGfmThMax_, pcGfmOrder_,
123 pcCurvDist_ && pcKappa_.extent(0) == n_, e_,
G, u_.w[0], u_.w[1], u_.w[2],
129 CCConst(sc.kcell), sc.D, energy, pcGfmThMin_, pcGfmThMax_, e_,
G, u_.w[0],
130 u_.w[1], u_.w[2], gmS);
143 scalarFillGhosts(sc);
144 Kokkos::deep_copy(sc.cOld, sc.c);
146 if (hasMask && pcCarryConserve_ && pcCarrySrc_.extent(0) == n_)
148 scalarInflowGhosts(sc);
159 if (hasMask && pcBudgetOn_ && pcClsPrev_.extent(0) == n_)
164 for (
int it = 0; it < sc.iters; ++it) {
165 scalarFillGhosts(sc);
166 cutcellSmoothColor(sc.c,
CCConst(sc.b), sc.AC, sc.AW, sc.AE, sc.AS, sc.AN, sc.AB, sc.AT, e_,
168 scalarFillGhosts(sc);
169 cutcellSmoothColor(sc.c,
CCConst(sc.b), sc.AC, sc.AW, sc.AE, sc.AS, sc.AN, sc.AB, sc.AT, e_,
172 scalarFillGhosts(sc);
173 if (hasMask && pcBudgetOn_ && pcClsPrev_.extent(0) == n_)
180 for (
auto& sc : scalars_)
183 throw std::runtime_error(
"no scalar named '" + name +
"'");
190 throw std::invalid_argument(
191 "the per-cell Dirichlet mask is not available on the cut-cell "
193 name +
"' (doc/scalar_ibm_design.md §8.1)");
194 if (sc.dmask.extent(0) != n_) {
195 sc.dmask =
CCField(name +
"_dmask", n_);
196 sc.dval =
CCField(name +
"_dval", n_);
197 sc.gfmB =
CCField(name +
"_gfmb", n_);
199 if (pcGphi_.extent(0) != n_) {
200 pcTgam_ =
CCField(
"pc_tgam", n_);
201 pcGn_[0] =
CCField(
"pc_gnx", n_);
202 pcGn_[1] =
CCField(
"pc_gny", n_);
203 pcGn_[2] =
CCField(
"pc_gnz", n_);
204 pcGphi_ =
CCField(
"pc_gphi", n_);
209void Solver<Grid>::scalarFillGhosts(
ScalarField& sc) {
215void Solver<Grid>::applyScalarBc(ScalarField& sc) {
216 for (
int f = 0; f < 6; ++f)
217 if (sc.bc[f] != 0 && touchesGlobalFace(f))
218 applyScalarBcFace(sc.c, f / 2, f % 2, sc.bc[f], sc.bcVal[f]);
222void Solver<Grid>::scalarInflowGhosts(ScalarField& sc) {
223 for (
int f = 0; f < 6; ++f) {
226 bc_[f] == 2 && (bcProf_[f].extent(0) > 0 || std::fabs(bcVel_[f][a]) > 1e-12);
227 if (sc.bc[f] == 2 && inflow && touchesGlobalFace(f))
228 applyScalarBcFace(sc.cOld, a, f % 2, 3, sc.bcVal[f]);
233bool Solver<Grid>::touchesGlobalFace(
int f)
const {
234#ifdef PECLET_FLOW_MPI
237 const int o = (
a == 0) ? og_.x : (
a == 1) ? og_.
y : og_.
z;
238 const int n = (
a == 0) ? nx_ : (
a == 1) ? ny_ : nz_;
239 const int gn = (
a == 0) ? gnx_ : (
a == 1) ? gny_ : gnz_;
240 return (f % 2 == 0) ? (o == 0) : (o + n == gn);
248void Solver<Grid>::applyScalarBcStencilVar(
ScalarField& sc) {
249 for (
int f = 0;
f < 6; ++
f) {
250 if (
sc.bc[f] != 2 || !touchesGlobalFace(f))
252 const int a =
f / 2,
side =
f % 2;
254 : (
a == 1) ? (side == 0 ?
sc.AS :
sc.AN)
256 patchScalarDirichletFaceVar(
sc.AC, band,
sc.kcell, a, side);
261void Solver<Grid>::applyScalarBcStencil(
ScalarField& sc) {
262 for (
int f = 0;
f < 6; ++
f) {
263 if (
sc.bc[f] != 2 || !touchesGlobalFace(f))
265 const int a =
f / 2,
side =
f % 2;
267 : (
a == 1) ? (side == 0 ?
sc.AS :
sc.AN)
269 patchScalarDirichletFace(
sc.AC, band,
sc.D, a, side);
276 const double wa = u_.w[a];
277 const int t1 = (a + 1) % 3, t2 = (a + 2) % 3;
278 const int nt1 = (t1 == 0) ? nx_ : (t1 == 1) ? ny_ : nz_;
279 const int nt2 = (t2 == 0) ? nx_ : (t2 == 1) ? ny_ : nz_;
280 const int na = (a == 0) ? nx_ : (a == 1) ? ny_ : nz_;
281 const long sx = 1, sy = e_.x, sz = (long)e_.x * e_.y;
282 const long sa = (a == 0) ? sx : (a == 1) ? sy : sz;
283 const long st1 = (t1 == 0) ? sx : (t1 == 1) ? sy : sz;
284 const long st2 = (t2 == 0) ? sx : (t2 == 1) ? sy : sz;
285 const int aInner = (side == 0) ?
G : (
G + na - 1);
287 Kokkos::parallel_for(
289 KOKKOS_LAMBDA(
int j1,
int j2) {
290 const long i = (long)aInner * sa + (
long)j1 * st1 + (long)j2 * st2;
291 const double D = kc(i) * wa;
292 AC(i) += D + band(i);
299 const double D = Din * u_.w[a];
300 const int t1 = (a + 1) % 3, t2 = (a + 2) % 3;
301 const int nt1 = (t1 == 0) ? nx_ : (t1 == 1) ? ny_ : nz_;
302 const int nt2 = (t2 == 0) ? nx_ : (t2 == 1) ? ny_ : nz_;
303 const int na = (a == 0) ? nx_ : (a == 1) ? ny_ : nz_;
304 const long sx = 1, sy = e_.x, sz = (long)e_.x * e_.y;
305 const long sa = (a == 0) ? sx : (a == 1) ? sy : sz;
306 const long st1 = (t1 == 0) ? sx : (t1 == 1) ? sy : sz;
307 const long st2 = (t2 == 0) ? sx : (t2 == 1) ? sy : sz;
308 const int aInner = (side == 0) ?
G : (
G + na - 1);
310 Kokkos::parallel_for(
312 KOKKOS_LAMBDA(
int j1,
int j2) {
313 const long i = (long)aInner * sa + (
long)j1 * st1 + (long)j2 * st2;
316 AC(i) += D + band(i);
323 const int t1 = (a + 1) % 3, t2 = (a + 2) % 3;
324 const int nt1 = (t1 == 0) ? nx_ : (t1 == 1) ? ny_ : nz_;
325 const int nt2 = (t2 == 0) ? nx_ : (t2 == 1) ? ny_ : nz_;
326 const int na = (a == 0) ? nx_ : (a == 1) ? ny_ : nz_;
327 const long sx = 1, sy = e_.x, sz = (long)e_.x * e_.y;
328 const long sa = (a == 0) ? sx : (a == 1) ? sy : sz;
329 const long st1 = (t1 == 0) ? sx : (t1 == 1) ? sy : sz;
330 const long st2 = (t2 == 0) ? sx : (t2 == 1) ? sy : sz;
331 const int aInner = (side == 0) ?
G : (
G + na - 1);
332 const int dir = (side == 0) ? -1 : +1;
335 const int t0 = edges ? 0 :
G, t1e = edges ? nt1 + 2 *
G :
G + nt1,
336 t2e = edges ? nt2 + 2 *
G :
G + nt2;
338 Kokkos::parallel_for(
339 "peclet::flow::scalar_bc_face",
MDRange2<CCExec>(space, {t0, t0}, {t1e, t2e}),
340 KOKKOS_LAMBDA(
int j1,
int j2) {
341 const long base = (long)aInner * sa + (
long)j1 * st1 + (long)j2 * st2;
342 for (
int L = 1; L <= 2; ++L) {
343 const long gcell = base + (long)dir * L * sa;
344 const long icell = base - (long)dir * (L - 1) * sa;
347 c(gcell) = (type == 2) ? (2.0 *
val - c(icell)) : (type == 3) ?
val : c(icell);
ScalarField & scalarField(const std::string &name)
void patchScalarDirichletFaceVar(CCField AC, CCField band, CCField kc, int a, int side)
CCField addField(const std::string &name)
void advanceScalarCutCell(ScalarField &sc)
void addScalar(const std::string &name, double D, int scheme, int iters, bool cutcell=false)
void fillFaceGhostsKeepBoundary(CCField f, int a)
void applyScalarBcFace(CCField c, int a, int side, int type, double val, bool edges=false)
void setScalarBc(const std::string &name, int face, int type, double value)
void pcBudgetPost(ScalarField &sc)
WO-P3f, part 2: the state AFTER the energy solve, the heat the plane-anchored rows actually delivered...
void pcBudgetPre(ScalarField &sc)
WO-P3f, the energy-budget instrument, part 1: the state BEFORE the energy solve, and the class-change...
bool hasScalar(const std::string &name) const
void pcCarryApply(ScalarField &sc)
WO-P3f: consume pcCarrySrc_ into the energy solve's time base.
void patchScalarDirichletFace(CCField AC, CCField band, double Din, int a, int side)
void scalarDirichletMask(const std::string &name)
Allocate (idempotently) the per-cell Dirichlet mask + value of a registered scalar.
void scalarMaskRhs(CCField b, CCField c, CCConst mask, CCConst val, C3 e, int g)
Kokkos::DefaultExecutionSpace CCExec
void scalarMaskGfm2(CCField AC, CCField AW, CCField AE, CCField AS, CCField AN, CCField AB, CCField AT, CCField gfmB, CCConst ox, CCConst oy, CCConst oz, CCConst mask, CCConst tgam, CCConst gnx, CCConst gny, CCConst gnz, CCConst gphi, CCConst kap, CCConst kc, double Dconst, bool useK, double thMin, double thMax, int order, bool useKappa, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0, vof::VofMetric gm=vof::VofMetric{})
void scalarBuildDiffusionOpen(CCField AC, CCField AW, CCField AE, CCField AS, CCField AN, CCField AB, CCField AT, CCConst ox, CCConst oy, CCConst oz, double D, double idt, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0)
Kokkos::View< const double *, CCMem > CCConst
void scalarAddGfmRhs(CCField b, CCConst gfmB, CCConst mask, C3 e, int g)
void scalarMaskGfm(CCField AC, CCField AW, CCField AE, CCField AS, CCField AN, CCField AB, CCField AT, CCField gfmB, CCConst ox, CCConst oy, CCConst oz, CCConst mask, CCConst tgam, CCConst gnx, CCConst gny, CCConst gnz, CCConst gphi, CCConst kc, double Dconst, bool useK, double thMin, double thMax, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0, vof::VofMetric gm=vof::VofMetric{})
Kokkos::View< double *, CCMem > CCField
void scalarMaskStencil(CCField AC, CCField AW, CCField AE, CCField AS, CCField AN, CCField AB, CCField AT, CCConst mask, C3 e, int g)
void cutcellSmoothColor(CCField phi, CCConst b, OpV AC, OpV AW, OpV AE, OpV AS, OpV AN, OpV AB, OpV AT, C3 e, C3 og, int g, int color)
void scalarBuildDiffusionVarK(CCField AC, CCField AW, CCField AE, CCField AS, CCField AN, CCField AB, CCField AT, CCConst ox, CCConst oy, CCConst oz, CCConst kc, CCConst rcp, CCConst mask, double idt, C3 e, int g, double wx=1.0, double wy=1.0, double wz=1.0)
void scalarBuildRhsHeat(CCField b, CCConst cOld, CCConst rcp, double idt, C3 e, int g)
Kokkos::MDRangePolicy< Exec, Kokkos::Rank< 2, Kokkos::Iterate::Left, Kokkos::Iterate::Left > > MDRange2
2-D range, first index fastest (both the tile order and the order within a tile).
void scalarBuildRhs(CCField b, CCConst cOld, CCConst U, CCConst V, CCConst W, CCConst ox, CCConst oy, CCConst oz, double idt, int scheme, C3 e, int g)
static double val(long gi)
static constexpr double AC
static double value(int gx, int gy, int gz)