8#ifndef PECLET_FLOW_FLOW_IBM_BC_HPP
9#define PECLET_FLOW_FLOW_IBM_BC_HPP
15 if (face < 0 || face > 5)
16 throw std::invalid_argument(
"set_domain_bc: face must be 0..5 (-x,+x,-y,+y,-z,+z)");
17 if (type < 0 || type > 4)
18 throw std::invalid_argument(
19 "set_domain_bc: type must be 0 periodic / 1 wall / 2 inflow / 3 outflow / 4 free-slip");
20 const bool valueUpdateOnly = geometryBuilt_ && type == bc_[face];
21 if (geometryBuilt_ && !valueUpdateOnly)
22 throw std::runtime_error(
23 "set_domain_bc: call BEFORE the geometry (set_solid / set_pressure_geometry / "
24 "set_solid_from_scene) to CHANGE a face's TYPE -- the type is folded into the "
25 "operators when the geometry is built. A VALUE update (vx, vy, vz) on a face whose "
26 "type is unchanged is allowed after the geometry.");
31 bcVelPhys_[face][0] = vx;
32 bcVelPhys_[face][1] = vy;
33 bcVelPhys_[face][2] = vz;
34 for (
int a = 0; a < 3; ++a)
35 bcVel_[face][a] = bcVelPhys_[face][a] * u_.velToInt(a);
38 for (
int i = 0; i < 6; ++i) {
55 if (valueUpdateOnly && hasBc_ && !Grid::collocated)
61 if (geometryBuilt_ && bc_[face] != 2)
62 throw std::runtime_error(
63 "set_domain_bc_profile: call BEFORE the geometry (set_solid / set_pressure_geometry / "
64 "set_solid_from_scene) to make a face inflow -- the type is folded into the operators "
65 "when the geometry is built. Updating the profile on a face that is ALREADY inflow is "
66 "allowed after the geometry.");
70 bcProfRaw_[face] =
prof;
72 bcProfRawNc_[face] = nc;
80 if (bcProfRaw_[face].empty())
82 const std::vector<double>&
prof = bcProfRaw_[face];
83 const int nb = bcProfNb_[face], nc = bcProfRawNc_[face];
84 const int a = face / 2;
85 const int dims[3] = {e_.x, e_.y, e_.z};
86 const int bax = (a + 1) % 3, cax = (a + 2) % 3;
87 const int Lb = dims[bax], Lc = dims[cax];
88 CCField pf(
"bcprof", (std::size_t)Lb * Lc * 3);
89 auto h = Kokkos::create_mirror_view(
pf);
90 auto cl = [](
int v,
int n) {
return v < 0 ? 0 : (v >= n ? n - 1 : v); };
91 for (
int p0 = 0; p0 < Lb; ++p0)
92 for (
int p1 = 0; p1 < Lc; ++p1) {
93 const int ib = cl(p0 -
G, nb), ic = cl(p1 -
G, nc);
96 for (
int k = 0; k < 3; ++k)
97 h(((
long)p0 * Lc + p1) * 3 + k) =
98 prof[((std::size_t)ib * nc + ic) * 3 + k] * u_.velToInt(k);
100 Kokkos::deep_copy(
pf, h);
102 bcProfNc_[face] = Lc;
107 if (backflowBeta_ <= 0.0 || !hasOutflow_)
110 const double beta = backflowBeta_, rho = rho_;
112 int dims[3] = {e.x, e.y, e.z};
113 long st[3] = {1, e.x, (long)e.x * e.y};
117 for (
int s = 0; s < 2; ++s) {
118 if (bc_[2 * a + s] != 3 || !touchesGlobalFace(2 * a + s))
120 const long sa = st[a];
121 const int na = dims[a];
122 const int bic = (s == 0) ?
G : (na -
G - 1);
123 const double sgn = (s == 0) ? 1.0 : -1.0;
124 const int b = (a + 1) % 3, cc = (a + 2) % 3;
125 const long sb = st[b], sc = st[cc];
127 Kokkos::parallel_for(
128 "peclet::flow::backflow", MD2(space, {
G,
G}, {dims[b] -
G, dims[cc] -
G}),
129 KOKKOS_LAMBDA(
int p0,
int p1) {
130 const long i = (long)p0 * sb + (
long)p1 * sc + (long)bic * sa;
131 const double back = sgn * u(i);
133 AC(i) += (
MReal)(beta * rho * back);
143 int dims[3] = {e.x, e.y, e.z};
144 long st[3] = {1, e.x, (long)e.x * e.y};
145 for (
int a = 0; a < 3; ++a)
146 for (
int s = 0; s < 2; ++s) {
147 if (bc_[2 * a + s] == 0 || !touchesGlobalFace(2 * a + s))
149 const int b = (a + 1) % 3, c = (a + 2) % 3;
150 const long sa = st[a], sb = st[b], sc = st[c];
151 const int na = dims[a];
152 const int bic = (s == 0) ?
G : (na -
G - 1);
153 const int lo = (s == 0) ? 0 : (na -
G), hi = (s == 0) ? (
G - 1) : (na - 1);
154 Kokkos::parallel_for(
156 KOKKOS_LAMBDA(
int p0,
int p1) {
157 const long base = (long)p0 * sb + (
long)p1 * sc;
158 const double pin = P(base + (
long)bic * sa);
159 for (
int ia = lo; ia <= hi; ++ia)
160 P(base + (
long)ia * sa) = pin;
170 outflowCorrValid_ =
false;
181#ifdef PECLET_FLOW_MPI
204 const bool keep = !Grid::collocated && !doOutflow && hasOutflow_ && a >= 0 && a < 3 &&
205 bc_[2 * a + 1] == 3 && touchesGlobalFace(2 * a + 1);
206 B3 eb{e_.x, e_.y, e_.z};
208 const int b = (a + 1) % 3, c = (a + 2) % 3;
209 const int d[3] = {e_.x, e_.y, e_.z};
210 const std::size_t np = (std::size_t)(d[b] - 2 *
G) * (d[c] - 2 *
G);
211 if (outflowPlane_[a].extent(0) != np)
213 Kokkos::view_alloc(
"peclet::flow::outflow_face_plane", Kokkos::WithoutInitializing),
219 const bool edges = faceEdges && !Grid::collocated && !doOutflow && hasOutflow_ && a >= 0 &&
220 a < 3 && bc_[2 * a + 1] == 3;
221 velDev_->exchange(f);
225 if (outflowEdgeScratch_.extent(0) != f.extent(0))
227 Kokkos::view_alloc(
"peclet::flow::outflow_edge_scratch", Kokkos::WithoutInitializing),
231 velDev_->exchange(outflowEdgeScratch_);
251 hasBc_ && a >= 0 && a < 3 && bc_[2 * a + 1] != 0 && touchesGlobalFace(2 * a + 1);
252 B3 eb{e_.x, e_.y, e_.z};
254 const int b = (a + 1) % 3, c = (a + 2) % 3;
255 const int d[3] = {e_.x, e_.y, e_.z};
256 const std::size_t np = (std::size_t)(d[b] - 2 *
G) * (d[c] - 2 *
G);
257 if (outflowPlane_[a].extent(0) != np)
259 Kokkos::view_alloc(
"peclet::flow::outflow_face_plane", Kokkos::WithoutInitializing), np);
271 B3 e{e_.x, e_.y, e_.z};
272 if constexpr (Grid::collocated) {
277 for (
int a = 0; a < 3; ++a)
278 for (
int s = 0; s < 2; ++s) {
279 const int ff = 2 * a + s;
280 const int t = bc_[ff];
281 if (t == 0 || !touchesGlobalFace(ff))
297 if (bcProf_[ff].extent(0) >
306 for (
int a = 0; a < 3; ++a) {
309 for (
int s = 0; s < 2; ++s) {
310 const int ff = 2 * a + s;
311 J.
j[J.
n++] =
BcVelFace{f, bcProf_[ff], bcVel_[ff][comp], comp, s, bcProfNc_[ff]};
316 for (
int s = 0; s < 2; ++s)
323 const int e[3] = {e_.x, e_.y, e_.z};
324 for (
int s = 0; s < 2; ++s) {
325 const int t = bc_[2 * a + s];
326 if (t == 0 || t == 3 || t == 4 || !touchesGlobalFace(2 * a + s))
335 B3 e{e_.x, e_.y, e_.z};
336 const int ff = 2 * a + s;
337 const int t = bc_[ff];
338 if (t == 0 || !touchesGlobalFace(ff))
349 if (bcProf_[ff].extent(0) > 0)
350 bcVelocityComp(f, e,
G, a, s, comp, 0.0, fold, bcProf_[ff], bcProfNc_[ff]);
357 if (Grid::collocated || !hasBc_) {
358 for (
int c = 0; c < 3; ++c)
364 B3 e{e_.x, e_.y, e_.z};
365 for (
int a = 0; a < 3; ++a) {
368 for (
int c = 0; c < 3; ++c)
369 for (
int s = 0; s < 2; ++s) {
370 const int ff = 2 * a + s;
371 J.
j[J.
n++] =
BcVelFace{C[c].u, bcProf_[ff], bcVel_[ff][c], c, s, bcProfNc_[ff]};
376 for (
int c = 0; c < 3; ++c)
377 for (
int s = 0; s < 2; ++s)
384 B3 e{e_.x, e_.y, e_.z};
385 for (
int c = 0; c < 3; ++c) {
386 Kokkos::deep_copy(bcDcorr_[c], 0.0);
387 Kokkos::deep_copy(bcBrhs_[c], 0.0);
388 for (
int a = 0; a < 3; ++a) {
391 const double beta = mu_ * u_.w[a];
392 for (
int s = 0; s < 2; ++s) {
393 const int t = bc_[2 * a + s];
394 if (!touchesGlobalFace(2 * a + s))
400 }
else if (t == 4 && c != a) {
403 }
else if (t != 0 && c != a) {
405 bval = 2.0 * beta * bcVel_[2 * a + s][c];
void applyVelocityBcAll(int fold, bool doOutflow)
void fillVelGhosts(int comp, int fold)
void fillGhosts(CCField f)
void fillDomainPeriodic(CCField f)
void setDomainBc(int face, int type, double vx, double vy, double vz)
void applyBackflowStab(int c)
void applyVelocityBcComp(int comp, int fold, bool doOutflow)
void applyVelocityBcFace(CCField f, int comp, int a, int s, int fold, bool doOutflow)
void fillFaceGhostsKeepBoundary(CCField f, int a)
void applyVelocityBcCompTo(CCField f, int comp, int fold, bool doOutflow)
bool bcVelFacePair(int a) const
void resampleBcProfile(int face)
void fillVelGhostsTo(CCField f, int comp, int fold, bool doOutflow=true, bool faceEdges=false)
void setDomainBcProfile(int face, const std::vector< double > &prof, int nb, int nc)
Kokkos::View< MReal *, CCMem > FV
Kokkos::DefaultExecutionSpace CCExec
void bcRestoreHighFacePlaneInner(BField dst, BField src, B3 ext, int g, int a)
Kokkos::View< const double *, CCMem > CCConst
void bcDiffusionFold(BField dcorr, BField brhs, B3 ext, int g, int a, int s, double dval, double bval)
void bcVelocityColocated(BField f, B3 ext, int g, int a, int s, double wall, int comp=0, BField prof=BField(), int prof_nc=0)
void bcSaveHighFacePlaneInner(BField dst, BField src, B3 ext, int g, int a)
void bcVelocityComp(BField f, B3 ext, int g, int a, int s, int comp, double wall, int fold, BField prof=BField(), int prof_nc=0)
Kokkos::View< double *, CCMem > CCField
void bcMirrorGhost(BField f, B3 ext, int g, int a, int s)
void bcVelocityFaces(const BcVelFaces &J, B3 ext, int g, int a, int fold)
void bcHighFacePlaneEdgesFromLastInner(BField dst, BField scratch, B3 ext, int g, int a)
void bcOutflowComp(BField f, B3 ext, int g, int a, int s, int comp, int fold)
void bcNeumannGhost(BField f, B3 ext, int g, int a, int s)
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 bcHighFacePlaneToLastInner(BField scratch, BField src, B3 ext, int g, int a)
void bcSlipComp(BField f, B3 ext, int g, int a, int s, int comp, int fold)
static constexpr double AC