9#ifndef PECLET_FLOW_FLOW_IBM_VOF_HPP
10#define PECLET_FLOW_FLOW_IBM_VOF_HPP
20 "enable_vof_momentum and enable_vof_blocks* are not composable: momentum-consistent transport "
21 "advects rho*u with the geometric fluxes of the ONE structured colour field (at the head of "
22 "step()) and skips the colour stage that advects the block markers, so the markers would never "
23 "move and the momentum fluxes would not be theirs. Use the structured colour field with "
24 "enable_vof_momentum, or the block container without it.";
29 vofAdv_.timingOn = on;
30 vofCurv_.timingOn = on;
32 vofBlocks_->timingOn = on;
44 vofAdv_.resetTiming();
45 vofCurv_.resetTiming();
47 vofBlocks_->debrisSeconds = 0.0;
48 vofBlocks_->debrisCalls = 0;
50 tStepSum_ = tPredSum_ = tMomSum_ = tProjSum_ = 0.0;
60 return vofAdv_.timing();
65 return vofCurv_.timing();
70 vofCurv_.useWorklist = on;
75 return vofCurv_.useWorklist;
100 vofAdv_.useWorklist = on;
105 return vofAdv_.useWorklist;
110 vofAdv_.init(nx_, ny_, nz_, 1.0,
kVofG);
111 vofAdv_.cflLimit = vofCflLimit_;
112 vofAdv_.interfaceLocalCfl =
true;
113 const I3 e3 = vofAdv_.extent();
114 e3_ =
C3{e3.x, e3.y, e3.z};
116 vofAdv_.globalMax =
nullptr;
117#ifdef PECLET_FLOW_MPI
118 if (distributed_ && dec_) {
120 MPI_Comm_rank(comm_, &rank);
124 std::array<bool, 3> per{
true,
true,
true};
125 vofHalo_ = std::make_shared<GridHaloTopology<3>>();
126 vofHalo_->buildTopology(*dec_, rank,
kVofG, per, comm_);
127 vofDev_ = std::make_shared<GridHalo<double>>();
128 vofDev_->setLabel(
"vof g3");
129 vofDev_->init(*vofHalo_);
131 vofAdv_.globalMax = [cm](
double v) {
133 MPI_Allreduce(&v, &r, 1, MPI_DOUBLE, MPI_MAX, cm);
145 vofCurv_.init(nx_, ny_, nz_,
kVofG);
146 if (vofMomEnabled_) {
147 vofMom_.init(vofAdv_, vofRhoG_, vofRhoL_);
148 for (
int c = 0; c < 3; ++c)
149 if (uAdv_[c].extent(0) != n_)
150 uAdv_[c] =
CCField(
"uAdv", n_);
156 const std::size_t blockLen = (std::size_t)e3_.x * e3_.y * e3_.z;
157 if (pcAreaC_.ready() && pcAreaC_.area().extent(0) != blockLen)
158 pcAreaC_.init(nx_, ny_, nz_,
kVofG);
159 if (pcAreaMc_.ready() && pcAreaMc_.area().extent(0) != blockLen)
160 pcAreaMc_.init(nx_, ny_, nz_,
kVofG);
161 if (vofEnergy_.initialized() && vofEnergy_.temperature().extent(0) != blockLen)
162 vofEnergy_.init(vofAdv_, pcRcpG_, pcRcpL_);
168 if (!vofEnabled_ || !contactAngleSet_)
171 for (
int f = 0; f < 6; ++f)
172 if (bc_[f] == 1 || bc_[f] == 4)
180 const int gi[3] = {gx, gy, gz};
181 const int q[3] = {gs.x, gs.y, gs.z};
183 for (
int a = 0; a < 3; ++a) {
184 const double c = (double)gi[a] + 0.5;
185 if (mask & (1 << (2 * a)))
186 d = Kokkos::fmin(d, c);
187 if (mask & (1 << (2 * a + 1)))
188 d = Kokkos::fmin(d, (
double)q[a] - c);
198 if ((!hasSolid_ || !cutcellPressure_) && !wet) {
199 vofAdv_.disableGeometry();
200 vofAdv_.disableWetting();
204 if (wet && !cutcellPressure_)
205 throw std::runtime_error(
206 "set_contact_angle: a wetting DOMAIN wall (bc type 1/4) needs a cut-cell pressure "
207 "operator, because the colour transport weights its geometric fluxes with the face "
208 "openness. Build one with set_pressure_geometry(all_fluid_sdf) (all-fluid) or "
209 "set_solid(sdf, cutcell_pressure=True).");
210 vofAdv_.enableGeometry();
211 if (hasSolid_ && cutcellPressure_) {
212 if (vofCs_.extent(0) != n_)
218 CCField oa[3] = {ox_, oy_, oz_};
219 for (
int d = 0; d < 3; ++d) {
221 I3{e_.x, e_.y, e_.z},
G, d);
227 Kokkos::deep_copy(vofAdv_.epsFraction(), 1.0);
228 for (
int d = 0; d < 3; ++d)
229 Kokkos::deep_copy(vofAdv_.faceOpenness(d), 1.0);
233 vofAdv_.classifyGeometry();
242 vofAdv_.finalizeGeometry();
247 if (vofSolidG2_.extent(0) != n_)
248 vofSolidG2_ =
CCField(
"vofSolidG2", n_);
258 const int g =
kVofG, mk = mask;
259 CCField ep = vofAdv_.epsFraction();
260 CCField of[3] = {vofAdv_.faceOpenness(0), vofAdv_.faceOpenness(1), vofAdv_.faceOpenness(2)};
261 CCField ofx = of[0], ofy = of[1], ofz = of[2];
262 Kokkos::parallel_for(
264 KOKKOS_LAMBDA(
int x,
int y,
int z) {
265 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
266 const int gx = x - g + org.x, gy = y - g + org.y, gz = z - g + org.z;
267 auto inside = [&](
int ax,
int ay,
int az) {
268 const int gi[3] = {ax, ay, az};
269 const int q[3] = {gs.x, gs.y, gs.z};
270 for (
int a = 0; a < 3; ++a) {
271 if ((mk & (1 << (2 * a))) && gi[a] < 0)
273 if ((mk & (1 << (2 * a + 1))) && gi[a] >= q[a])
278 const bool in0 = inside(gx, gy, gz);
282 if (!in0 || !inside(gx + 1, gy, gz))
284 if (!in0 || !inside(gx, gy + 1, gz))
286 if (!in0 || !inside(gx, gy, gz + 1))
296 const int g =
kVofG, mk = mask;
297 CCField kd = vofAdv_.kindDouble();
298 Kokkos::parallel_for(
300 KOKKOS_LAMBDA(
int x,
int y,
int z) {
301 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
302 const int gi[3] = {x - g + org.x, y - g + org.y, z - g + org.z};
303 const int q[3] = {gs.x, gs.y, gs.z};
304 for (
int a = 0; a < 3; ++a) {
305 if (((mk & (1 << (2 * a))) && gi[a] < 0) ||
306 ((mk & (1 << (2 * a + 1))) && gi[a] >= q[a])) {
320 for (
int a = 0; a < 3; ++a)
321 for (
int sd = 0; sd < 2; ++sd) {
322 if (!(mask & (1 << (2 * a + sd))))
324 const int q = (a == 0) ? gs.x : (a == 1) ? gs.y : gs.z;
325 const int o = (a == 0) ? org.x : (a == 1) ? org.y : org.z;
327 const int i3 = (sd == 0) ? (-3 - o + g) : (q - o + g + 2);
328 const int i2 = (sd == 0) ? (-2 - o + g) : (q - o + g + 1);
329 const int dims[3] = {e.x, e.y, e.z};
330 if (i3 < 0 || i3 >= dims[a] || i2 < 0 || i2 >= dims[a])
332 const long st[3] = {1, (long)e.x, (
long)e.x * (long)e.y};
333 const int b = (a + 1) % 3, c = (a + 2) % 3;
334 const long sa = st[a], sb = st[b], sc = st[c];
336 Kokkos::parallel_for(
337 "peclet::flow::vof_extend_wall_band",
339 const long base = (long)p0 * sb + (
long)p1 * sc;
340 ff(base + (
long)i3 * sa) = ff(base + (
long)i2 * sa);
348 if (!vofEnabled_ || !vofAdv_.hasGeometry() || !vofSolidG2_.extent(0) || !vofSolidZero_)
355 const int ex = e_.x, ey = e_.y, g =
G;
356 Kokkos::parallel_for(
358 KOKKOS_LAMBDA(
int x,
int y,
int z) {
359 const long i = (long)(x + g) + (long)(y + g) * ex + (long)(z + g) * (long)ex * ey;
368 return bc_[2 * a] == 0 && bc_[2 * a + 1] == 0;
380 if (vofAdv_.hasGeometry() && f.data() == vofAdv_.colour().data()) {
387 if (vofAdv_.hasWetting()) {
388 vofAdv_.buildWettingNormals();
389 for (
int d = 0; d < 3; ++d)
397 if (vofDyn_.active() && vofDyn_.allocated()) {
400 if (!(vofDyn_.sigma > 0.0) && vofDyn_.dynamic && !contactSigmaWarned_) {
401 contactSigmaWarned_ =
true;
403 "peclet.flow: set_contact_angle_dynamic is active but sigma is 0 "
404 "(no set_surface_tension and no explicit sigma) - Ca_cl is 0 and the "
405 "Cox-Voinov correction is inert.\n");
407 vofDyn_.measure(vofAdv_);
410 vofDyn_.impose(vofAdv_);
413 vofAdv_.solidBandFill();
423#ifdef PECLET_FLOW_MPI
424 if (distributed_ && vofDev_)
425 vofDev_->exchange(f);
436 const int skip = (f.data() == vofAdv_.colour().data()) ?
vofWetWallMask() : 0;
437 vof::clampFill(f,
I3{e3_.x, e3_.y, e3_.z},
kVofG, org, gs, px, py, pz, skip);
443#ifdef PECLET_FLOW_MPI
445 return I3{gnx_, gny_, gnz_};
447 return I3{nx_, ny_, nz_};
452 return I3{og_.x, og_.y, og_.z};
462 if constexpr (Grid::collocated) {
463 CCField fa[3] = {uf_, vf_, wf_};
464 for (
int c = 0; c < 3; ++c) {
470 I3{e_.x, e_.y, e_.z},
G, c);
474 for (
int c = 0; c < 3; ++c) {
482 if (outflowCorrValid_)
490 I3{e_.x, e_.y, e_.z},
G, c);
496 std::vector<vof::VofBox> rb(
static_cast<std::size_t
>(size));
498#ifdef PECLET_FLOW_MPI
499 if (distributed_ && dec_) {
500 for (
int r = 0; r < size; ++r) {
501 const auto b = dec_->block(
static_cast<std::size_t
>(r));
502 for (
int d = 0; d < 3; ++d) {
503 rb[r].lo[d] =
static_cast<int>(b.origin[d]);
504 rb[r].hi[d] =
static_cast<int>(b.origin[d] + b.size[d]);
523#ifdef PECLET_FLOW_MPI
525 MPI_Comm_size(comm_, &size);
529 pp.
e =
I3{e3_.x, e3_.y, e3_.z};
530 pp.
n =
I3{nx_, ny_, nz_};
533 vofBlockExch_->setPatch(pp, vofAdv_.faceVel(0), vofAdv_.faceVel(1), vofAdv_.faceVel(2));
538 if (vofOverlapDensity_ && vofBlkS_.extent(0)) {
540 const long len3 =
static_cast<long>(e3_.x) * e3_.y * e3_.z;
541 if (
static_cast<long>(vofBlkCeff_.extent(0)) != len3)
542 vofBlkCeff_ =
SField(
"vof::blockcsf::ceff", len3);
543 const I3 e =
I3{e3_.x, e3_.y, e3_.z};
545 SField un = vofAdv_.colour(), sm = vofBlkS_, ce = vofBlkCeff_;
546 Kokkos::parallel_for(
548 KOKKOS_LAMBDA(
int x,
int y,
int z) {
549 const long i =
L3(x, y, z, e);
550 const double sv = sm(i);
551 ce(i) = (
sv <= 1.0 + 1e-8) ? un(i) : Kokkos::fmax(0.0, 2.0 -
sv);
564 if (!vofBlocks_ || !(vofBlocks_->csfEnabled || vofOverlapDensity_))
566 const long len3 =
static_cast<long>(e3_.x) * e3_.y * e3_.z;
567 if (
static_cast<long>(vofBlkS_.extent(0)) != len3)
568 vofBlkS_ =
SField(
"vof::blockcsf::sum", len3);
569 vofBlocks_->scatterSum(vofBlkS_);
570 const I3 e =
I3{e3_.x, e3_.y, e3_.z};
573 double mx = 0.0, ex = 0.0;
575 Kokkos::parallel_reduce(
576 "vof::block::overlap_census",
578 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& m,
double& a,
long& c) {
579 const double sv = sm(
L3(x, y, z, e));
580 m = Kokkos::fmax(m,
sv);
581 if (
sv > 1.0 + 1e-8) {
586 Kokkos::Max<double>(mx), ex, nc);
587 CCExec().fence(
"peclet::flow::vof_overlap_census");
588#ifdef PECLET_FLOW_MPI
590 double gm = 0.0, ge = 0.0;
592 MPI_Allreduce(&mx, &gm, 1, MPI_DOUBLE, MPI_MAX, comm_);
593 MPI_Allreduce(&ex, &ge, 1, MPI_DOUBLE, MPI_SUM, comm_);
594 MPI_Allreduce(&nc, &gc, 1, MPI_LONG, MPI_SUM, comm_);
600 vofOverlap_.maxSum = mx;
601 vofOverlap_.excess = ex;
602 vofOverlap_.cells = nc;
609 constexpr double kPhantomTrigger = 1e-2;
610 vofOverlap_.active = vofPhantomBound_ && vofBlocks_->csfEnabled && !vofOverlapDensity_ &&
611 mx > 1.0 + kPhantomTrigger;
616 vofPhantomBound_ = enabled;
617 vofOverlap_.active = vofOverlap_.active && enabled;
622 vofBlockDebris_ = enabled ? 1 : 0;
623 if (vofBlocks_ && vofBlocks_->csfEnabled)
624 vofBlocks_->debrisRemove = enabled;
630 throw std::runtime_error(
"set_vof_block_overlap_density: call enable_vof_blocks first");
631 vofOverlapDensity_ = enabled;
647 const int hi = faceRange ? 1 : 0;
654 const double sig = sigmaCsf_, h = 1.0 / u_.w[c];
656 Kokkos::parallel_for(
657 "csf_rhs", MD(space, {
G,
G,
G}, {e.x -
G + hi, e.y -
G + hi, e.z -
G + hi}),
658 KOKKOS_LAMBDA(
int x,
int y,
int z) {
659 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
660 const double dC = cv(i) - cv(i - strd);
677 const int hi = faceRange ? 1 : 0;
681 const double sig = sigmaCsf_, h = 1.0 / u_.w[c];
683 Kokkos::parallel_for(
684 "csf_rhs_cellinterp", MD(space, {
G,
G,
G}, {e.x -
G + hi, e.y -
G + hi, e.z -
G + hi}),
685 KOKKOS_LAMBDA(
int x,
int y,
int z) {
686 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
687 double f[2] = {0.0, 0.0};
688 for (
int q = 0; q < 2; ++q) {
689 const long j = i - (1 - q) * strd;
692 f[q] = sig * kp(j) * 0.5 * (cv(j + strd) - cv(j - strd)) / h;
695 bb(i) += rs(i) * 0.5 * (f[0] + f[1]);
697 bb(i) += 0.5 * (f[0] + f[1]);
708 Kokkos::parallel_for(
709 "csf_rhs_blocks", MD(space, {
G,
G,
G}, {e.x -
G, e.y -
G, e.z -
G}),
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 bb(i) += rs(i) *
fb(i);
718 if (!vofEnabled_ || !kappaField_.extent(0))
719 throw std::runtime_error(
720 "csf_diagnostics: needs VoF + a curvature field (call set_surface_tension and step, or "
721 "compute_vof_curvature)");
725 const double sig = sigmaCsf_, eps = csfInterfaceEps_;
727 for (
int c = 0; c < 3; ++c) {
728 const double h = 1.0 / u_.w[c];
731 long orph = 0, forced = 0;
732 Kokkos::parallel_reduce(
733 "csf_diag", MD(
CCExec(), {
G,
G,
G}, {e.x -
G, e.y -
G, e.z -
G}),
734 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& m,
long& o,
long& f) {
735 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
736 const double dC = cv(i) - cv(i - strd);
743 else if (Kokkos::fabs(dC) > eps)
747 Kokkos::Max<double>(mx), orph, forced);
749 d.orphanFaces[c] = orph;
750 d.forcedFaces[c] = forced;
767 if constexpr (Grid::collocated) {
773 throw std::runtime_error(
774 "enable_vof: geometric VoF on SolverColocated (rung V8) is ALL-FLUID only — immersed "
775 "solids on this path are the next package (doc/collocated_multiphase_solids_plan.md). "
776 "Use the staggered Solver (rung V5a supports cut cells).");
796 vofAdv_.wispEps = vofWispEps_;
802 if (!(eps >= 0.0) || eps >= 0.5)
803 throw std::runtime_error(
"set_vof_wisp_eps: the threshold must be in [0, 0.5)");
805 vofAdv_.wispEps = eps;
807 vofBlocks_->setWispEps(eps);
831 pcMaskFresh_ =
false;
842 throw std::runtime_error(
"vof_diagnostics: VoF is not enabled (call enable_vof/set_vof)");
844 auto d = vofAdv_.diagnostics();
854 if (!vofEnabled_ || !vofAdv_.hasGeometry() || !vofSolidG2_.extent(0))
857 const int ex = e_.x, ey = e_.y, g =
G;
859 Kokkos::parallel_reduce(
861 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& a) {
862 const long i = (long)(x + g) + (long)(y + g) * ex + (long)(z + g) * (long)ex * ey;
877 const double loc = vofAdv_.maxCourantInterfaceAuto(dt_ / vofAdv_.h());
878 return vofAdv_.globalMax ? vofAdv_.globalMax(loc) : loc;
883 return vofEnabled_ ? vofAdv_.lastCfl() : 0.0;
890 vofAdv_.cflLimit = v;
900 if constexpr (Grid::collocated) {
903 throw std::runtime_error(
905 ": geometric VoF on SolverColocated (rung V8) is ALL-FLUID only. The cut-cell colour "
906 "transport (rung V5a) is validated on the STAGGERED solver.");
908 if (!hasSolid_ || vofAdv_.hasGeometry())
911 m +=
": an immersed solid is present but the colour block has no cut-cell geometry. Rung V5a "
912 "needs the cut-cell openness, i.e. set_solid(sdf, cutcell_pressure=True) (the staircase "
913 "pressure operator has no face openness to weight the geometric fluxes with).";
914 throw std::runtime_error(m);
919 return vofAdv_.hasGeometry();
924 vofAdv_.cutFluxClamp = on;
929 return vofAdv_.cutFluxClamp;
939 return vofSolidZero_;
945 throw std::runtime_error(
"vof_filled_color: VoF is not enabled");
955 throw std::runtime_error(
"vof_geometry: VoF is not enabled (needs enable_vof)");
956 if (which < 0 || which > 4)
957 throw std::runtime_error(
"vof_geometry: `which` must be 0..4");
965 if (!vofAdv_.hasGeometry())
966 return std::vector<double>((std::size_t)nx_ * ny_ * nz_, which == 4 ? 0.0 : 1.0);
970 src =
CCConst(vofAdv_.epsFraction());
972 src =
CCConst(vofAdv_.faceOpenness(which - 1));
974 src =
CCConst(vofAdv_.kindDouble());
981 if (!(thetaDeg >= 0.0 && thetaDeg <= 180.0))
982 throw std::runtime_error(
"set_contact_angle: theta must be in [0, 180] degrees");
983 contactAngleDeg_ = thetaDeg;
984 contactAngleField_.clear();
985 contactAngleSet_ =
true;
995 throw std::runtime_error(
996 "set_contact_angle: this solver has no wetting wall - no immersed SDF solid (set_solid) "
997 "and no impermeable domain face (set_domain_bc(face, 1) no-slip or 4 free-slip). The "
998 "angle would be imposed nowhere. (Inflow/outflow faces carry no contact line.)");
1001template <
class Gr
id>
1003 if (thetaDeg.size() != (std::size_t)nx_ * ny_ * nz_)
1004 throw std::runtime_error(
"set_contact_angle_field: expected nx*ny*nz values");
1005 contactAngleField_ = thetaDeg;
1006 contactAngleSet_ =
true;
1012template <
class Gr
id>
1014 return contactAngleSet_;
1017template <
class Gr
id>
1019 return contactAngleDeg_;
1022template <
class Gr
id>
1024 if (mode < 0 || mode > 3)
1025 throw std::runtime_error(
"set_contact_angle_pivot: mode must be 0..3");
1026 contactPivot_ = mode;
1027 vofAdv_.wettingPivot = mode;
1030template <
class Gr
id>
1032 return contactPivot_;
1035template <
class Gr
id>
1040 const double slipInt = slipCells * u_.lenToInt();
1041 if (!(slipInt > 0.0 && slipInt < 1.0))
1042 throw std::runtime_error(
1043 "set_contact_angle_dynamic: the slip length must lie in (0, 1) cell (lambda < Delta)");
1044 if (!(muLiquid > 0.0))
1045 throw std::runtime_error(
"set_contact_angle_dynamic: mu_liquid must be positive");
1046 vofDyn_.dynamic =
true;
1047 vofDyn_.slip = slipInt;
1048 vofDyn_.muLiquid = muLiquid * u_.muToInt();
1049 contactSigmaOverride_ = sigma;
1055 slipPhys_ = slipCells;
1056 slipLambda_ = slipInt;
1057 if (wallSlip_ && hasSolid_ && !Grid::collocated) {
1064template <
class Gr
id>
1066 if (!(lambdaCells >= 0.0))
1067 throw std::runtime_error(
"set_wall_slip_length: lambda must be >= 0");
1068 const double lambdaInt = lambdaCells * u_.lenToInt();
1069 if (lambdaCells > 0.0 && Grid::collocated)
1070 throw std::runtime_error(
1071 "set_wall_slip_length: the Navier wall closure is a staggered-grid feature (the "
1072 "collocated scheme carries its own wall treatment)");
1073 slipPhys_ = lambdaCells;
1074 slipLambda_ = lambdaInt;
1075 wallSlip_ = lambdaCells > 0.0;
1076 if (vofDyn_.dynamic && lambdaCells > 0.0)
1077 vofDyn_.slip = lambdaInt;
1084template <
class Gr
id>
1086 return wallSlip_ ? slipPhys_ : 0.0;
1089template <
class Gr
id>
1091 return slipSandwich_;
1094template <
class Gr
id>
1096 if (!(thetaADeg >= thetaRDeg))
1097 throw std::runtime_error(
"set_contact_angle_hysteresis: theta_a must be >= theta_r");
1098 if (!(thetaRDeg >= 0.0 && thetaADeg <= 180.0))
1099 throw std::runtime_error(
"set_contact_angle_hysteresis: angles must lie in [0, 180]");
1100 const double toRad = 3.14159265358979323846 / 180.0;
1101 vofDyn_.hysteresis =
true;
1102 vofDyn_.thetaA = thetaADeg * toRad;
1103 vofDyn_.thetaR = thetaRDeg * toRad;
1104 if (!contactAngleSet_)
1110template <
class Gr
id>
1112 vofDyn_.dynamic =
false;
1113 vofDyn_.hysteresis =
false;
1117template <
class Gr
id>
1119 return vofDyn_.dynamic;
1122template <
class Gr
id>
1124 return vofDyn_.hysteresis;
1127template <
class Gr
id>
1129 return vofDyn_.slip;
1132template <
class Gr
id>
1134 vofDyn_.smooth = on;
1137template <
class Gr
id>
1139 if (!(loDeg > 0.0 && hiDeg < 180.0 && loDeg < hiDeg))
1140 throw std::runtime_error(
"set_contact_angle_clamp: need 0 < lo < hi < 180");
1141 const double toRad = 3.14159265358979323846 / 180.0;
1142 vofDyn_.thetaMin = loDeg * toRad;
1143 vofDyn_.thetaMax = hiDeg * toRad;
1146template <
class Gr
id>
1148 return contactSigmaOverride_ > 0.0 ? contactSigmaOverride_ : sigmaCsf_;
1151template <
class Gr
id>
1153 if (!vofEnabled_ || !vofDyn_.active() || !vofDyn_.allocated())
1154 throw std::runtime_error(
1155 "vof_dynamic_field: no dynamic contact angle (call set_contact_angle_dynamic / "
1156 "set_contact_angle_hysteresis first)");
1159 const double toDeg = 180.0 / 3.14159265358979323846;
1162 src =
CCConst(vofDyn_.imposed());
1163 else if (which == 1)
1164 src =
CCConst(vofDyn_.apparent());
1165 else if (which == 2)
1166 src =
CCConst(vofDyn_.speed());
1167 else if (which == 3)
1168 src =
CCConst(vofDyn_.capillary());
1170 src =
CCConst(vofDyn_.stateField());
1179template <
class Gr
id>
1182 throw std::runtime_error(
"contact_angle_diagnostics: VoF is not enabled");
1184 d.setAngle = contactAngleDeg_;
1185 if (!vofAdv_.hasWetting())
1188 long counts[vof::kVofWetCount];
1193 vofAdv_.wettingCensusGhost(counts, d.meanApparentAngle, nApp,
vofWetWallMask() ?
kVofG : 0);
1194 d.unfilledCells = counts[vof::kVofWetNone];
1195 d.contactCells = counts[vof::kVofWetTheta];
1196 d.neighbourCells = counts[vof::kVofWetNeighbour];
1197 d.pureCells = counts[vof::kVofWetPure];
1198 d.parallelCells = counts[vof::kVofWetParallel];
1199 d.neutralCells = counts[vof::kVofWetNeutral];
1200 if (vofDyn_.active() && vofDyn_.allocated()) {
1201 const auto cs = vofDyn_.census(vofAdv_);
1202 d.dynamicCells = cs.contactCells;
1206 d.meanImposedTheta = cs.meanImposedDeg;
1207 d.meanApparentTheta = cs.meanApparentDeg;
1208 d.maxCaCl = cs.maxCa;
1209 d.maxContactSpeed = cs.maxSpeed;
1214template <
class Gr
id>
1216 if (!contactAngleSet_ || !vofEnabled_ || !vofAdv_.hasGeometry())
1218 vofAdv_.enableWetting();
1219 vofAdv_.wettingPivot = contactPivot_;
1226 Kokkos::deep_copy(vofAdv_.wallSdf(), 1e30);
1236 const int g =
kVofG;
1237 CCField sd = vofAdv_.wallSdf();
1238 Kokkos::parallel_for(
1240 KOKKOS_LAMBDA(
int x,
int y,
int z) {
1241 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
1242 const double d =
vofWallPlaneSdf(x - g + org.x, y - g + org.y, z - g + org.z, gs, wet);
1243 sd(i) = Kokkos::fmin(sd(i), d);
1248 const double toRad = 3.14159265358979323846 / 180.0;
1249 if (contactAngleField_.empty()) {
1250 Kokkos::deep_copy(vofAdv_.contactAngle(), contactAngleDeg_ * toRad);
1253 std::vector<double> rad(contactAngleField_.size());
1254 for (std::size_t i = 0; i < rad.size(); ++i)
1255 rad[i] = contactAngleField_[i] * toRad;
1262 if (vofDyn_.active()) {
1263 vofDyn_.allocate(vofAdv_.size());
1264 Kokkos::deep_copy(vofDyn_.base(), vofAdv_.contactAngle());
1268template <
class Gr
id>
1270 if (!vofDyn_.allocated())
1272 if constexpr (Grid::collocated)
1273 throw std::runtime_error(
"set_contact_angle_dynamic: SolverColocated has no immersed solid");
1274 const long st[3] = {1, (long)e_.x, (
long)e_.x * (long)e_.y};
1275 for (
int c = 0; c < 3; ++c) {
1280 if (outflowCorrValid_)
1284 if (vofDynVel_[c].extent(0) != n_)
1285 vofDynVel_[c] =
CCField(
"vofDynVel", n_);
1288 const long sc = st[c];
1289 const int ex = e_.x, ey = e_.y, g =
G;
1290 Kokkos::parallel_for(
1292 KOKKOS_LAMBDA(
int x,
int y,
int z) {
1293 const long i = (long)(x + g) + (long)(y + g) * ex + (long)(z + g) * (long)ex * ey;
1294 t(i) = 0.5 * (u(i) + u(i + sc));
1302template <
class Gr
id>
1307template <
class Gr
id>
1311 vofBlocks_->setStep(n);
1314template <
class Gr
id>
1319template <
class Gr
id>
1321 const double dt = dtPhysArg * u_.timeToInt();
1323 throw std::runtime_error(
"advect_vof: VoF is not enabled (call enable_vof / set_vof first)");
1330 if constexpr (Grid::collocated) {
1331 if (!faceFieldValid_)
1332 throw std::runtime_error(
1333 "advect_vof: SolverColocated advects the colour with the MAC FACE field uf_/vf_/wf_ "
1334 "(the only discretely divergence-free field on this grid), and it has never been "
1335 "built — it is all zeros, which the divergence guard would certify as solenoidal and "
1336 "the advection would be a silent no-op. Call set_state()/set_velocity() (which now "
1337 "seed the face field through the same centerToFace map project() uses) or step() "
1345 if (!cutcellPressure_)
1346 throw std::runtime_error(
1347 "advect_vof: no cut-cell pressure operator, so the divergence guard cannot measure "
1348 "anything (max_open_divergence() would return 0 whatever the field is). Build one with "
1349 "set_pressure_geometry(sdf) on an all-fluid box, or set_solid(sdf, "
1350 "cutcell_pressure=True) — Weymouth-Yue conservation is conditional on "
1351 "sum_f o_f u_f = 0 per cell and a cell-centre-sampled analytic field does NOT satisfy "
1352 "it (measured on LeVeque: max|div| 0.612, 4.93 % of the liquid lost in 50 steps).");
1357 if (!(div <= 1e-10)) {
1359 std::snprintf(msg,
sizeof(msg),
1360 "advect_vof: the current face velocity is not discretely divergence-free "
1361 "(max|div(open*u)| = %.6g > 1e-10). Weymouth-Yue conservation is conditional "
1362 "on it; project the field first (step() / project()).",
1364 throw std::runtime_error(msg);
1371 vofEnergy_.advect(vofAdv_, dt, vofStep_++);
1373 scalarFillGhosts(sc);
1375 vofAdv_.advect(dt, vofStep_++);
1383template <
class Gr
id>
1386 for (
const auto& sd : seeds)
1387 vofBlocks_->seedSphere(sd[0], sd[1], sd[2], sd[3]);
1388 vofBlocks_->scatter(vofAdv_.colour());
1392template <
class Gr
id>
1395 throw std::runtime_error(
"enable_vof_blocks: VoF is not enabled (call enable_vof first)");
1399 throw std::runtime_error(
1400 "enable_vof_blocks: the block container is ALL-FLUID at rung W0 (the cut-cell block is "
1401 "rung W12). Drop set_solid, or use the structured colour field (advect_vof).");
1402 int rank = 0, size = 1;
1403#ifdef PECLET_FLOW_MPI
1405 MPI_Comm_rank(comm_, &rank);
1406 MPI_Comm_size(comm_, &size);
1409 vofBlocks_ = std::make_shared<vof::VofBlockSet>();
1416 vofBlocks_->setMetric(u_.vofMetric());
1417 vofBlocks_->cflLimit = vofCflLimit_;
1420 vofBlocks_->setWispEps(vofAdv_.wispEps);
1421 vofBlockExch_ = std::make_shared<vof::VofBlockExchange>();
1423#ifdef PECLET_FLOW_MPI
1425 vofBlockExch_->setComm(comm_);
1428 vofBlocks_->setExchange(vofBlockExch_);
1431template <
class Gr
id>
1433 vofBlocks_->assignMasters();
1434 vofBlocks_->scatter(vofAdv_.colour());
1438template <
class Gr
id>
1440 return static_cast<bool>(vofBlocks_);
1443template <
class Gr
id>
1446 vofBlockExch_.reset();
1449template <
class Gr
id>
1454template <
class Gr
id>
1457 throw std::runtime_error(
"advect_vof_blocks: call enable_vof_blocks first");
1459 if (!(div <= 1e-10)) {
1461 std::snprintf(msg,
sizeof(msg),
1462 "advect_vof_blocks: the current face velocity is not discretely "
1463 "divergence-free (max|div(open*u)| = %.6g > 1e-10). Weymouth-Yue conservation "
1464 "is conditional on it; project the field first (step() / project()).",
1466 throw std::runtime_error(msg);
1468 const double _tv0 = vofTick();
1469 vofBlocks_->timingOn = vofTiming_;
1470 const double _td0 = vofBlocks_->debrisSeconds;
1472 vofBlocks_->advect(dt, vofAdv_.colour());
1476 vofAdd(vt_.blockAdvect, _tv0);
1477 vt_.blockDebris += vofBlocks_->debrisSeconds - _td0;
1481template <
class Gr
id>
1484 throw std::runtime_error(
"vof_block_stats: call enable_vof_blocks first");
1485 return vofBlocks_->statsAll();
1488template <
class Gr
id>
1491 throw std::runtime_error(
"vof_block_imbalance: call enable_vof_blocks first");
1492 return vofBlocks_->cellImbalance();
1495template <
class Gr
id>
1498 throw std::runtime_error(
"vof_block_census: call enable_vof_blocks first");
1499 vofBlocks_->masterCensus(masters, cells);
1502template <
class Gr
id>
1505 throw std::runtime_error(
"set_vof_block_assign: call enable_vof_blocks first");
1507 vofBlocks_->reassignEvery = every;
1508 vofBlocks_->assignMasters();
1511template <
class Gr
id>
1514 throw std::runtime_error(
"vof_block_assign: call enable_vof_blocks first");
1515 return static_cast<int>(vofBlocks_->assignMode);
1518template <
class Gr
id>
1521 throw std::runtime_error(
"vof_block_imbalance_of: call enable_vof_blocks first");
1522 const auto save = vofBlocks_->assignMode;
1524 const std::vector<int> m = vofBlocks_->plannedMasters();
1525 vofBlocks_->assignMode = save;
1526 const int np = vofBlocks_->size();
1527 std::vector<long> load(
static_cast<std::size_t
>(np), 0);
1528 long tot = 0, mx = 0;
1529 for (std::size_t i = 0; i < m.size(); ++i) {
1530 const long w = vofBlocks_->blocks()[i].box.cells();
1531 load[
static_cast<std::size_t
>(m[i])] += w;
1535 mx = std::max(mx, v);
1536 return tot == 0 ? 1.0 :
static_cast<double>(mx) * np /
static_cast<double>(tot);
1539template <
class Gr
id>
1542 throw std::runtime_error(
"set_vof_block_device_staging: call enable_vof_blocks first");
1543 vofBlockExch_->deviceStaging = on;
1546template <
class Gr
id>
1549 throw std::runtime_error(
"set_vof_block_pool: call enable_vof_blocks first");
1550 vofBlocks_->usePool = on;
1552 vofBlocks_->clearPool();
1555template <
class Gr
id>
1558 throw std::runtime_error(
"vof_block_pool_stats: call enable_vof_blocks first");
1559 return {vofBlocks_->poolHits(), vofBlocks_->poolMisses()};
1562template <
class Gr
id>
1565 for (
const auto& q : boxes) {
1567 for (
int d = 0; d < 3; ++d) {
1569 bb.
hi[d] = q[3 + d];
1571 vofBlocks_->seedBox(bb);
1574 vofBlocks_->finishSeeding(vofAdv_.colour());
1578template <
class Gr
id>
1581 throw std::runtime_error(
"vof_block_color: call enable_vof_blocks first");
1582 if (
id < 0 ||
static_cast<std::size_t
>(
id) >= vofBlocks_->count())
1583 throw std::runtime_error(
"vof_block_color: no such block id");
1584 return vofBlocks_->blockColourHost(
static_cast<std::size_t
>(
id));
1587template <
class Gr
id>
1590 throw std::runtime_error(
"vof_block_kappa: call enable_vof_blocks first");
1591 if (
id < 0 ||
static_cast<std::size_t
>(
id) >= vofBlocks_->count())
1592 throw std::runtime_error(
"vof_block_kappa: no such block id");
1593 return vofBlocks_->blockKappaHost(
static_cast<std::size_t
>(
id));
1596template <
class Gr
id>
1598 const std::vector<std::vector<double>>& colours) {
1599 if (boxes.size() != colours.size())
1600 throw std::runtime_error(
"enable_vof_blocks_from_colors: one color array per box is required");
1602 for (std::size_t i = 0; i < boxes.size(); ++i) {
1604 for (
int d = 0; d < 3; ++d) {
1605 bb.
lo[d] = boxes[i][d];
1606 bb.
hi[d] = boxes[i][3 + d];
1608 vofBlocks_->seedBoxWithColour(bb, colours[i]);
1613template <
class Gr
id>
1616 throw std::runtime_error(
"enable_vof_block_csf: call enable_vof_blocks first");
1617 if (!(sigmaCsf_ > 0.0))
1618 throw std::runtime_error(
"enable_vof_block_csf: set_surface_tension(sigma) first (sigma > 0)");
1619 if constexpr (Grid::collocated)
1620 throw std::runtime_error(
1621 "enable_vof_block_csf: the block CSF is STAGGERED-only at rung W2 (the collocated face "
1622 "-acceleration form is V8's, and composing the two is not this rung).");
1631 vofBlocks_->curvProto.copyTunablesFrom(vofCurv_);
1632 vofBlocks_->curvProto.interfaceEps = csfInterfaceEps_;
1635 vofBlocks_->curvProto.pureEps = vofAdv_.wispEps;
1636 vofBlocks_->setWispEps(vofAdv_.wispEps);
1637 vofBlocks_->curvProto.kappaMax = vofBlockKappaMax_;
1638 vofBlocks_->debrisRemove = (vofBlockDebris_ != 0);
1639 vofBlocks_->enableCsf(sigmaCsf_);
1640 const long len3 =
static_cast<long>(e3_.x) * e3_.y * e3_.z;
1641 for (
int c = 0; c < 3; ++c) {
1642 vofBlkF_[c] =
SField(
"vof::blockcsf::patch", len3);
1643 csfBlkF_[c] =
CCField(
"vof::blockcsf", n_);
1649template <
class Gr
id>
1651 if (!vofBlocks_ || !vofBlocks_->csfEnabled)
1653 vofBlocks_->computeCsf(vofBlkF_[0], vofBlkF_[1], vofBlkF_[2]);
1654 for (
int c = 0; c < 3; ++c)
1658template <
class Gr
id>
1660 if (!csfBlkF_[c].extent(0))
1661 throw std::runtime_error(
"vof_block_force: call enable_vof_block_csf() first");
1665template <
class Gr
id>
1668 throw std::runtime_error(
"vof_block_curvature_stats: call enable_vof_blocks first");
1669 return vofBlocks_->csfCurvatureStats();
1672template <
class Gr
id>
1675 vofInflowSet_[f] =
true;
1676 vofInflowC_[f] =
value;
1677 vofInflowProfRaw_[f].clear();
1681template <
class Gr
id>
1684 if ((
int)
prof.size() != nb * nc)
1685 throw std::runtime_error(
"set_vof_inflow_profile: profile size != nb*nc");
1686 vofInflowSet_[f] =
true;
1687 vofInflowProfRaw_[f] =
prof;
1688 vofInflowProfNb_[f] = nb;
1689 vofInflowProfNc_[f] = nc;
1693template <
class Gr
id>
1696 vofBackflowSet_[f] =
true;
1697 vofBackflowC_[f] =
value;
1701template <
class Gr
id>
1703 return vofBcActive_;
1706template <
class Gr
id>
1708 return std::vector<double>(vofBcVol_, vofBcVol_ + 6);
1711template <
class Gr
id>
1713 return std::vector<double>(vofBcVolTotal_, vofBcVolTotal_ + 6);
1716template <
class Gr
id>
1718 for (
int f = 0; f < 6; ++f)
1719 vofBcVol_[f] = vofBcVolTotal_[f] = 0.0;
1722template <
class Gr
id>
1724 if (!vofBcActive_ || !vofEnabled_)
1726 if (f.data() != vofAdv_.colour().data())
1728 const I3 e3{e3_.x, e3_.y, e3_.z};
1729 for (
int face = 0; face < 6; ++face) {
1730 if (!touchesGlobalFace(face))
1732 const int a = face / 2, sd = face % 2;
1733 if (bc_[face] == 2 && vofInflowSet_[face]) {
1734 if (vofInflowProf3_[face].extent(0))
1738 }
else if (bc_[face] == 3 && vofBackflowSet_[face]) {
1746template <
class Gr
id>
1748 if (!vofBcActive_ || !vofEnabled_)
1750 const bool isColour = cField_.extent(0) && f.data() == cField_.data();
1751 const I3 e2{e_.x, e_.y, e_.z};
1752 for (
int face = 0; face < 6; ++face) {
1753 if (bc_[face] != 2 || !vofInflowSet_[face] || !touchesGlobalFace(face))
1755 const int a = face / 2, sd = face % 2;
1757 if (vofInflowProfG2_[face].extent(0))
1762 for (
const auto& cl : closures_)
1763 if (cl.out.data() == f.data())
1769template <
class Gr
id>
1773 const I3 e3{e3_.x, e3_.y, e3_.z};
1776 vofOutside_ =
vof::UCField(
"vof::outside", vofAdv_.size());
1778 vofAdv_.setOutsideMask(vofOutside_);
1783 for (
int face = 0; face < 6; ++face)
1784 vofAdv_.setBcFaceOwned(face, vofBcActive_ && bc_[face] != 0 && touchesGlobalFace(face));
1785 for (
int face = 0; face < 6; ++face) {
1786 vofInflowProf3_[face] =
CCField();
1787 vofInflowProfG2_[face] =
CCField();
1788 if (vofInflowProfRaw_[face].empty())
1790 vofInflowProf3_[face] =
1791 resampleFaceScalar(vofInflowProfRaw_[face], vofInflowProfNb_[face], vofInflowProfNc_[face],
1792 face, e3_,
kVofG, vofProf3Nc_[face]);
1793 vofInflowProfG2_[face] =
1794 resampleFaceScalar(vofInflowProfRaw_[face], vofInflowProfNb_[face], vofInflowProfNc_[face],
1795 face, e_,
G, vofProfG2Nc_[face]);
1799template <
class Gr
id>
1801 C3 ext,
int g,
int& outNc) {
1802 const int a = face / 2;
1803 const int dims[3] = {ext.
x, ext.
y, ext.
z};
1804 const int bax = (a + 1) % 3, cax = (a + 2) % 3;
1805 const int Lb = dims[bax], Lc = dims[cax];
1806 CCField pf(
"vof::bcprof", (std::size_t)Lb * Lc);
1807 auto h = Kokkos::create_mirror_view(
pf);
1808 auto cl = [](
int v,
int n) {
return v < 0 ? 0 : (v >= n ? n - 1 : v); };
1809 for (
int p0 = 0; p0 < Lb; ++p0)
1810 for (
int p1 = 0; p1 < Lc; ++p1)
1811 h((
long)p0 * Lc + p1) =
prof[(std::size_t)cl(p0 - g, nb) * nc + cl(p1 - g, nc)];
1812 Kokkos::deep_copy(
pf, h);
1817template <
class Gr
id>
1820 throw std::runtime_error(std::string(who) +
": face must be 0..5 (-x,+x,-y,+y,-z,+z)");
1822 if (bc_[f] != wantType)
1823 throw std::runtime_error(std::string(who) +
": face " + std::to_string(f) +
1824 " has domain BC type " + std::to_string(bc_[f]) +
", not " +
1825 std::to_string(wantType) +
" — call set_domain_bc(face, " +
1826 std::to_string(wantType) +
1827 ", ...) first (2 = inflow, "
1831template <
class Gr
id>
1833 vofBcActive_ =
true;
1839 if (vofEnabled_ && cField_.extent(0))
1843template <
class Gr
id>
1847 for (
int f = 0; f < 6; ++f) {
1848 vofBcVol_[f] = vofAdv_.bcFaceVolume(f);
1849 vofBcVolTotal_[f] += vofBcVol_[f];
1853template <
class Gr
id>
1856 throw std::runtime_error(
1857 "compute_vof_curvature: VoF is not enabled (call enable_vof / set_vof first)");
1858 if (!kappaField_.extent(0)) {
1860 kappaBranch_ =
addField(
"kappa_branch");
1867 vofCurv_.pureEps = vofAdv_.wispEps;
1868 vofCurvStats_ = vofCurv_.compute(vofAdv_.colour());
1877template <
class Gr
id>
1879 return vofCurvStats_;
1882template <
class Gr
id>
1884 if (!kappaField_.extent(0))
1885 throw std::runtime_error(
"vof_curvature: call compute_vof_curvature() first");
1886 std::vector<double> out =
gatherInner(kappaField_);
1887 const double k = u_.curvToPhys();
1889 for (
double& x : out)
1894template <
class Gr
id>
1896 if (!kappaBranch_.extent(0))
1897 throw std::runtime_error(
"vof_curvature_branch: call compute_vof_curvature() first");
1901template <
class Gr
id>
1903 vofCurv_.weightWidth = d;
1906template <
class Gr
id>
1908 return vofCurv_.weightWidth;
1911template <
class Gr
id>
1913 vofCurv_.useMixedHeightFit = on;
1916template <
class Gr
id>
1918 return vofCurv_.useMixedHeightFit;
1921template <
class Gr
id>
1923 if (!(sigma >= 0.0))
1924 throw std::runtime_error(
"set_surface_tension: sigma must be >= 0");
1927 if (!kappaField_.extent(0)) {
1929 kappaBranch_ =
addField(
"kappa_branch");
1938 vofCurv_.interfaceEps = csfInterfaceEps_;
1943 sigmaCsf_ = sigma * u_.sigmaToInt();
1947template <
class Gr
id>
1952template <
class Gr
id>
1954 csfInterfaceEps_ = eps;
1955 if (sigmaCsf_ > 0.0)
1956 vofCurv_.interfaceEps = eps;
1959template <
class Gr
id>
1961 return csfInterfaceEps_;
1964template <
class Gr
id>
1968 if (kappaMaxPhys < 0.0)
1970 else if (kappaMaxPhys > 0.0)
1971 km = kappaMaxPhys * u_.lenToPhys();
1973 throw std::runtime_error(
1974 "set_vof_block_kappa_clip: kappa_max must be > 0 (or None for 1/Delta_min)");
1976 vofBlockKappaMax_ = km;
1978 vofBlocks_->setKappaMax(km);
1981template <
class Gr
id>
1986template <
class Gr
id>
1991template <
class Gr
id>
1993 return static_cast<bool>(vofBlocks_) && vofBlocks_->csfEnabled;
1996template <
class Gr
id>
1998 return vofEnabled_ && sigmaCsf_ > 0.0 && (kappaField_.extent(0) != 0 ||
vofBlockCsf());
2001template <
class Gr
id>
2006template <
class Gr
id>
2008 return kappaFrozen_;
2011template <
class Gr
id>
2014 if (!kappaField_.extent(0)) {
2016 kappaBranch_ =
addField(
"kappa_branch");
2018 Kokkos::deep_copy(kappaField_, kappa / u_.curvToPhys());
2019 Kokkos::deep_copy(kappaBranch_, (
double)vof::kCurvHf);
2020 kappaFrozen_ =
true;
2023template <
class Gr
id>
2028template <
class Gr
id>
2030 if (!(sigmaCsf_ > 0.0))
2031 return std::numeric_limits<double>::infinity();
2034 double hpMin = u_.hp[0];
2035 for (
int a = 1; a < 3; ++a)
2036 hpMin = hpMin < u_.hp[a] ? hpMin : u_.hp[a];
2040 double lo = 0.0, hi = 0.0;
2047template <
class Gr
id>
2052template <
class Gr
id>
2054 return capillaryCfl_;
2057template <
class Gr
id>
2061 L.cflDt = (L.courant > 0.0) ? dtPhys_ * vofCflLimit_ / L.courant
2062 : std::numeric_limits<double>::infinity();
2064 const double cap = capillaryCfl_ * L.capillaryDt;
2065 L.capillaryBinds = cap < L.cflDt;
2066 L.binding = L.capillaryBinds ? cap : L.cflDt;
2070template <
class Gr
id>
2072 double lo = 0.0, hi = 0.0;
2077template <
class Gr
id>
2079 if (vofMomEnabled_) {
2080 rlo = std::fmin(vofRhoG_, vofRhoL_);
2081 rhi = std::fmax(vofRhoG_, vofRhoL_);
2091 double lo = 1e300, hi = -1e300;
2097 for (
int pass = 0; pass < 2; ++pass) {
2101 Kokkos::parallel_reduce(
2102 "rho_minmax", MD(space, {
G,
G,
G}, {e.x -
G, e.y -
G, e.z -
G}),
2103 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& mn,
double& mx) {
2104 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
2105 mn = Kokkos::fmin(mn, f(i));
2106 mx = Kokkos::fmax(mx, f(i));
2108 Kokkos::Min<double>(lo), Kokkos::Max<double>(hi));
2109 if (hi > 0.0 || pass == 1)
2113#ifdef PECLET_FLOW_MPI
2115 double g[2] = {lo, -hi}, r[2];
2116 MPI_Allreduce(g, r, 2, MPI_DOUBLE, MPI_MIN, comm_);
2125template <
class Gr
id>
2128 throw std::runtime_error(
2129 "step_adaptive: needs enable_vof() (it picks dt from the two "
2130 "explicit two-phase limits vof_step_limits() reports)");
2131 if (!(cflTarget > 0.0) || !(capillaryCflTarget > 0.0) || !(dtMax > 0.0))
2132 throw std::runtime_error(
"step_adaptive: cfl_target, capillary_cfl and dt_max must be > 0");
2134 double dt = cflTarget * L.cflDt;
2135 const double dc = capillaryCflTarget * L.capillaryDt;
2140 if (!(dt > 0.0) || !std::isfinite(dt))
2141 throw std::runtime_error(
"step_adaptive: no finite dt (cfl_dt = " + std::to_string(L.cflDt) +
2142 ", capillary_dt = " + std::to_string(L.capillaryDt) +
2143 ") - pass a finite dt_max when neither limit is active");
2147 vofPrecheckDone_ =
true;
2152template <
class Gr
id>
2154 const bool skip = vofPrecheckDone_;
2155 vofPrecheckDone_ =
false;
2156 if (!vofEnabled_ || skip)
2168 if (!(cfl <= vofCflLimit_))
2169 throw std::runtime_error(
2170 "peclet::flow::vof::WyAdvector: CFL = max|uf| dt/h = " + std::to_string(cfl) +
2171 " exceeds the Weymouth-Yue boundedness cap " + std::to_string(vofCflLimit_) +
2172 " (dt = " + std::to_string(dt_) +
", h = " + std::to_string(vofAdv_.h()) +
2173 ") - reduce dt. Rejected at the HEAD of step(): no field has been advanced, so a retry "
2174 "at a smaller dt is exact (see set_vof_cfl_limit / step_adaptive).");
2177template <
class Gr
id>
2179 const std::string rho = (vofOverlap_.active &&
vofBlockCsf())
2180 ?
"2 rho_min [block markers overlap: the phantom gas-gas interface, "
2181 "vof_overlap_design 5.6]"
2183 return "surface tension: dt = " + std::to_string(dt_) +
" exceeds the capillary limit " +
2184 std::to_string(cap) +
" (Brackbill sqrt((" + rho +
") h^3/(4 pi sigma)) = " +
2185 std::to_string(cap / (capillaryCfl_ > 0.0 ? capillaryCfl_ : 1.0)) +
" x safety factor " +
2186 std::to_string(capillaryCfl_) +
2187 "). Surface tension is EXPLICIT: this is a hard stability boundary (Denner & van "
2188 "Wachem 2015), not a margin. Reduce dt, or raise set_capillary_cfl deliberately.";
2191template <
class Gr
id>
2197 else if (!kappaFrozen_)
2204template <
class Gr
id>
2217 const double _tv0 = vofTick();
2219 const double _tb0 = vofTick();
2223 vofAdv_.resetBcFaceVolume();
2231 vofEnergy_.advect(vofAdv_, dt_, vofStep_++);
2233 scalarFillGhosts(sc);
2235 vofAdv_.advect(dt_, vofStep_++);
2242 const double _tb1 = vofTick();
2247 vofAdd(vt_.advect, _tv0);
2251template <
class Gr
id>
2253 const double rhoGas = rhoGasPhys * u_.rhoToInt(), rhoLiquid = rhoLiquidPhys * u_.rhoToInt();
2254 if constexpr (Grid::collocated)
2255 throw std::runtime_error(
2256 "enable_vof_momentum: momentum-consistent VoF transport is STAGGERED-ONLY (rung V2b); the "
2257 "collocated construction is Favre-averaged face states, rung V8.");
2260 throw std::runtime_error(
2261 "enable_vof_momentum: the volume-averaged porous momentum and the momentum-consistent VoF "
2262 "transport both own the momentum time term; they are not composable at this rung.");
2263 if (!(rhoGas > 0.0) || !(rhoLiquid > 0.0))
2264 throw std::runtime_error(
"enable_vof_momentum: both phase densities must be > 0");
2270 double rho0Phys = 0.0, rho1Phys = 0.0;
2272 auto differs = [](
double a,
double b) {
2273 return std::fabs(a - b) > 1e-9 * std::fmax(std::fabs(a), std::fabs(b));
2275 if (differs(rhoGasPhys, rho0Phys) || differs(rhoLiquidPhys, rho1Phys)) {
2279 "enable_vof_momentum(rho_c0=%.10g, rho_c1=%.10g): the pair must be the density at "
2280 "C = 0, then the density at C = 1, whichever phase each is. The 'rho' closure gives "
2281 "rho(C=0) = %.10g, "
2282 "rho(C=1) = %.10g%s",
2283 rhoGasPhys, rhoLiquidPhys, rho0Phys, rho1Phys,
2284 (!differs(rhoGasPhys, rho1Phys) && !differs(rhoLiquidPhys, rho0Phys))
2285 ?
" -- the pair is SWAPPED."
2287 throw std::runtime_error(msg);
2291 vofRhoL_ = rhoLiquid;
2292 vofMomEnabled_ =
true;
2293 for (
int c = 0; c < 3; ++c)
2294 if (uAdv_[c].extent(0) != n_)
2295 uAdv_[c] =
CCField(
"uAdv", n_);
2296 vofMom_.init(vofAdv_, vofRhoG_, vofRhoL_);
2299template <
class Gr
id>
2303 for (
const auto& c : closures_)
2304 if (c.outName ==
"rho")
2309 rho0Phys = cl->
pPhys[0];
2316 auto hx = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), cl->
tabX);
2318 auto at = [&](
double s) {
2319 const int n = cl->
nTab;
2325 while (lo + 1 < n - 1 && hx(lo + 1) <= s)
2327 return ys[lo] + (s - hx(lo)) / (hx(lo + 1) - hx(lo)) * (ys[lo + 1] - ys[lo]);
2334template <
class Gr
id>
2336 return vofMomEnabled_;
2339template <
class Gr
id>
2341 vofMom_.rhoFloorFrac = f;
2344template <
class Gr
id>
2346 return vofMom_.rhoFloorFrac;
2349template <
class Gr
id>
2351 return vofMom_.lastRhoFloor();
2354template <
class Gr
id>
2357 if (name ==
"upwind")
2358 vofMom_.momentumScheme = MS::Upwind;
2359 else if (name ==
"muscl_mixed")
2360 vofMom_.momentumScheme = MS::MusclMixed;
2361 else if (name ==
"muscl_mixed_capped")
2362 vofMom_.momentumScheme = MS::MusclMixedCapped;
2363 else if (name ==
"muscl_phase")
2364 vofMom_.momentumScheme = MS::MusclPhase;
2366 throw std::invalid_argument(
"set_vof_momentum_scheme: unknown scheme '" + name +
2367 "' (expected 'upwind', 'muscl_mixed', 'muscl_mixed_capped' or "
2371template <
class Gr
id>
2373 switch (vofMom_.momentumScheme) {
2377 return "muscl_mixed";
2379 return "muscl_mixed_capped";
2381 return "muscl_phase";
2386template <
class Gr
id>
2391template <
class Gr
id>
2393 vofMom_.useCellDilationFlag = on;
2396template <
class Gr
id>
2398 vofMom_.clampFluxes = on;
2401template <
class Gr
id>
2403 if (!vofMomEnabled_)
2404 throw std::runtime_error(
"vof_momentum_diagnostics: enable_vof_momentum was never called");
2405 return vofMom_.diagnostics();
2408template <
class Gr
id>
2410 if (!vofMomEnabled_)
2411 throw std::runtime_error(
"vof_advected_velocity: enable_vof_momentum was never called");
2415template <
class Gr
id>
2417 if (!vofMomEnabled_)
2423 throw std::runtime_error(
2424 "enable_vof_momentum requires the variable-density momentum/projection path: register a "
2425 "density closure on C (set_property_model('rho','linear','C',[rho_g, rho_l-rho_g])) or "
2426 "call set_density_mode('variable').");
2427 if (vofMom_.phaseRhoG() != vofRhoG_ || vofMom_.phaseRhoL() != vofRhoL_)
2428 vofMom_.setPhaseDensities(vofRhoG_, vofRhoL_);
2429 const double _tv0 = vofTick();
2431 const double _tb0 = vofTick();
2435 vofAdv_.resetBcFaceVolume();
2436 vofMom_.advect(vofAdv_, dt_, vofStep_++);
2440 const double _tb1 = vofTick();
2447 for (
int c = 0; c < 3; ++c)
2450 vofAdd(vt_.momAdvect, _tv0);
2451 vt_.momBridge += _tb;
2454template <
class Gr
id>
2457#ifdef PECLET_FLOW_MPI
2458 if (distributed_ && vofDev_)
2459 vofDev_->exchange(f);
2469template <
class Gr
id>
2470double Solver<Grid>::vofTick()
const {
2475 CCExec().fence(
"peclet::flow::vof_tick");
2479template <
class Gr
id>
2480void Solver<Grid>::vofAdd(
double& acc,
double t0) {
2483 CCExec().fence(
"peclet::flow::vof_add");
void imposeDomainWallKind(int mask)
void vofExchangeScalar(CCField f)
The colour block's ghost policy WITHOUT the colour-specific rules (no solid-band fill,...
void setVofMomentumMuscl(bool on)
void setVofCurvatureMixedHeightFit(bool on)
void setContactAngleClamp(double loDeg, double hiDeg)
const VofTiming & vofTimingReport() const
bool vofBlocksEnabled() const
void setVofRhoFloorFrac(double f)
void harvestVofBlockUnion()
void enableVofBlocksFromField(const std::vector< std::array< int, 6 > > &boxes)
static constexpr int kVofG
void setContactAngleSmoothing(bool on)
void vofApplyColourBc(CCField f)
The colour field's boundary rules, applied at the END of vofFillGhosts — i.e.
void fillVelGhosts(int comp, int fold)
double maxOpenDivergenceInternal()
The same diagnostic in INDEX units (per tRef), which is what the solver's own guards read.
void addCsfRhs(int c, CCField dst, bool rowScaled, bool faceRange=false)
double capillaryCfl() const
void registerBalancedForceState()
void setContactAngleDynamicOff()
void scatterInner(CCField fld, const std::vector< double > &in)
void setContactAnglePivot(int mode)
void setVofBlockPool(bool on)
bool contactAngleSet() const
ScalarField & scalarField(const std::string &name)
void setVofKappaConstant(double kappa)
kappa is a PHYSICAL curvature, 1/length (the internal field is 1/h; see vof_curvature()).
std::vector< double > getVofCurvature()
The interface curvature kappa = 2H in the caller's units, i.e.
int vofBlockAssign() const
void fillPropGhosts(CCField f, bool edges=false)
void checkVofBcFace(int f, int wantType, const char *who)
A VoF boundary colour is only meaningful on a face that already carries the matching domain BC,...
void setVofTiming(bool on)
std::vector< double > vofBlockKappa(long id)
void addCsfRhsBlocks(int c)
std::string vofMomentumScheme() const
void setVofMomentumCellFlag(bool on)
CCField addField(const std::string &name)
void vofBcArm()
Arm the rung: install the mask, rebuild the profiles, and zero the boundary volume ledger.
void computeVofCurvature()
void computeVofBlockCsf()
bool vofHasGeometry() const
double vofSolidColourSum()
bool vofRhoClosureEnds(double &rho0Phys, double &rho1Phys) const
void advectVofBlocksInternal(double dtInt, bool requireSolenoidal)
The same with dt already in INTERNAL time (step() passes its dt_).
std::vector< double > getVofFilledColour()
void enableVofBlocks(const std::vector< std::array< double, 4 > > &seeds)
void pcUpdateEnergyProps()
Refresh k(C) and (rho c_p)(C) on the G=2 block from the current colour.
std::vector< double > getVofAdvectedVelocity(int c)
void setContactAngleDynamic(double thetaEDeg, double slipCells, double muLiquid, double sigma=0.0)
void setPressureExactResidual(bool on)
WO-R2 item 3 — the exact (matrix-free, double, flux-form) level-0 operator apply in the residual and ...
std::vector< double > getVofGeometry(int which)
void vofRebuildBcBlock()
(Re)build everything that lives on the g=3 block for this rung: the out-of-domain mask and the resamp...
void fillFaceGhostsKeepBoundary(CCField f, int a)
std::vector< double > vofBcVolumes() const
Signed liquid volume that crossed each of the six domain faces during the LAST colour advection,...
double maxOpenDivergenceProjectedInternal()
The same diagnostic in INDEX units (per tRef), which is what the solver's own guards read.
std::vector< double > getVof()
double surfaceTension() const
The surface tension in the caller's units (what set_surface_tension was given).
void copyInner(CCField dst, C3 de, int dg, CCConst src, C3 se, int sg)
double capillaryDtInternal()
The same limit in the solver's index time (per tRef) — what the step's own guards compare to.
double vofTimingProjectionSeconds() const
CsfDiagnostics csfDiagnostics()
void setSurfaceTension(double sigma)
void vofHarvestBcVolumes()
Move the advector's per-face boundary volume ledger into the solver's, once per advection.
void enableVofMomentum(double rhoGasPhys, double rhoLiquidPhys)
rhoGas / rhoLiquid are PHYSICAL densities, the same numbers the rho closure is given.
std::vector< double > getVofCurvatureBranch()
void setVofInflowProfile(int f, const std::vector< double > &prof, int nb, int nc)
Per-position inflow colour on face f: prof is (nb, nc) on the INNER grid of the face's two perpendicu...
double vofBlockImbalanceOf(int mode) const
void requireVofGeometry(const char *who)
double capillaryDt()
The Brackbill capillary limit in the CALLER's time unit.
void setVof(const std::vector< double > &c)
void setContactAngleHysteresis(double thetaADeg, double thetaRDeg)
static constexpr double defaultVofWispEps()
The value enableVof starts from.
void buildVelocityOverlays(bool resetU)
Build the three per-component Robust-Scaled cut-cell overlays + solid masks from the CURRENT sdf_ (ex...
vof::VofCurvature::Stats vofBlockCurvatureStats() const
void applyDomainWallGeometry(int mask)
std::vector< double > getVofBlockForce(int c)
bool vofKappaFrozen() const
std::array< long, 2 > vofBlockPoolStats() const
void setVofCutFluxClamp(bool on)
ContactAngleDiagnostics contactAngleDiagnostics()
void setVofCurvatureWeightWidth(double d)
bool vofCutFluxClamp() const
double vofRhoFloorFrac() const
double vofTimingPredictorSeconds() const
void vofExchangeRaw(CCField f)
double wallSlipLength() const
The Navier slip length in the caller's units (0 = no-slip).
double vofInterfaceEps() const
void setVofBlockAssign(int mode, long every)
double contactAngle() const
void setWallSlipLength(double lambdaCells)
WO-V6b – the VELOCITY half of the dynamic contact line.
void updateVofCurvature()
void requireWettingWall() const
void vofBlockCensus(std::vector< long > &masters, std::vector< long > &cells) const
void setVofBackflow(int f, double value)
inletOutlet backflow colour on outflow face f (default 0 = gas): where the boundary face velocity poi...
vof::MomentumConsistentAdvector::Diagnostics vofMomentumDiagnostics()
std::vector< double > vofBlockColour(long id)
std::vector< double > getVofDynamicField(int which)
void setVofCurvatureWorklist(bool on)
VofCurvature::useWorklist — run the height-function cascade and the PV fallback over a COMPACTED list...
void setContactAngleField(const std::vector< double > &thetaDeg)
void setCapillaryCfl(double f)
const vof::VofCurvature::Timing & vofCurvatureTiming() const
CCField resampleFaceScalar(const std::vector< double > &prof, int nb, int nc, int face, C3 ext, int g, int &outNc)
Clamp-resample a per-position face scalar from the user's (nb, nc) INNER grid onto the ghost-inclusiv...
bool vofMomentumEnabled() const
vof::VofCurvature::Stats vofCurvatureStats() const
void setVofPhantomCapillaryBound(bool enabled)
void updateVofBlockOverlap()
int contactAnglePivot() const
void setVofMomentumScheme(const std::string &name)
std::vector< double > vofBcVolumesTotal() const
The same, accumulated since enable_vof() (or the last resetVofBcVolumes()).
void setVofWorklist(bool on)
WyAdvector::useWorklist — the compaction of the PLIC reconstruction pass onto the mixed cells.
void buildVofCellVelocity()
void vofExtendWallBand(CCField f, int mask)
void setVofInflow(int f, double value)
Colour of the fluid entering through inflow face f (0..5 = -x,+x,-y,+y,-z,+z), in [0,...
bool contactAngleHysteresis() const
void setVofStepParity(long n)
long strideOf(int c) const
const vof::WyAdvector::Timing & vofKernelTiming() const
void fillVelGhostsTo(CCField f, int comp, int fold, bool doOutflow=true, bool faceEdges=false)
void collocatedV8AutoFallback(const char *why)
std::array< int, 3 > wallSlipSandwichCells() const
Cut-cell axes at which a one-cell fluid gap kept the no-slip closure (per component).
void setVofBlockDeviceStaging(bool on)
void setVofKappaFrozen(bool on)
double vofRhoFloor() const
double stepAdaptive(double cflTarget, double capillaryCflTarget, double dtMax)
void bridgeVelocityToVof()
void setVofBlockOverlapDensity(bool enabled)
long vofStepParity() const
static double vofWallPlaneSdf(int gx, int gy, int gz, I3 gs, int mask)
void enableVofBlocksFromColours(const std::vector< std::array< int, 6 > > &boxes, const std::vector< std::vector< double > > &colours)
void setVofBlockDebris(bool enabled)
void advectVofKinematic(double dtPhysArg)
dt is in the caller's time unit.
void setVofInterfaceEps(double eps)
void setVofWispEps(double eps)
WO-R2 item 4 — the wisp threshold on the advector's mixed-cell predicate and on the interface Courant...
double effectiveContactSigma() const
void setVofBlockKappaClip(bool enabled, double kappaMaxPhys=-1.0)
double vofLastCourant() const
std::string capillaryThrowMessage(double cap) const
bool contactAngleDynamic() const
double vofTimingMomentumSeconds() const
double vofTimingStepSeconds() const
void setVofSolidColourZero(bool on)
std::vector< vof::VofBox > vofBlockRankBoxes(int size) const
double vofCurvatureWeightWidth() const
void phaseDensityRange(double &lo, double &hi)
void vofBcPropGhosts(CCField f)
WO-R item 5 — the Neumann property policy at an inflow face.
void vofFillGhosts(CCField f)
void addCsfRhsCellInterp(int c, CCField dst, bool rowScaled, bool faceRange=false)
const vof::WyAdvector & vofAdvector() const
vof::WyAdvector::Diagnostics vofDiagnostics()
bool vofAxisPeriodic(int a) const
void setVofCflLimit(double v)
VofStepLimits vofStepLimits()
Both limits at the current state, in the CALLER's time unit (courant is dimensionless).
std::vector< double > gatherInner(CCField fld)
double vofWispEps() const
bool vofSolidColourZero() const
bool vofCurvatureWorklist() const
double vofBlockImbalance() const
int vofWetWallMask() const
std::vector< vof::VofBlockStats > vofBlockStats() const
void advectVofBlocks(double dtPhysArg, bool requireSolenoidal=true)
dt is in the caller's time unit.
void setContactAngle(double thetaDeg)
void setVofFluxClamp(bool on)
bool vofCurvatureMixedHeightFit() const
double contactAngleSlip() const
double vofCflLimit() const
Weymouth-Yue split advection of a colour field on an extended (inner + ghost) block.
static double wallSeconds()
double capillaryDt(double rhoSum, double h, double sigma)
Brackbill capillary time-step limit sqrt((ρ₁+ρ₂) h³ / (4πσ)).
bool csfKappaDefined(double branch)
Is the curvature of a cell carrying branch code branch usable?
Kokkos::View< unsigned char *, SMem > UCField
void bcColourBackflow(SField f, I3 e, int g, int a, int s, SField un, double backflow)
inletOutlet on an outflow face: where the boundary face velocity points INTO the domain,...
void bcColourConst(SField f, I3 e, int g, int a, int s, double value)
Overwrite ALL g ghost layers of domain face (axis a, side s) with a constant colour.
void bcColourProfile(SField f, I3 e, int g, int a, int s, SField prof, int nc)
Same, with a per-position colour on the face's (b, c) plane.
void buildOutsideMask(UCField m, I3 e, int g, I3 o, I3 gs, bool px, bool py, bool pz)
1 for every ghost cell of the block that lies OUTSIDE the global domain on a non-periodic axis,...
@ kVofDynReceding
theta_app < theta_r: theta_r (+ Cox-Voinov)
@ kVofDynPinned
theta_r <= theta_app <= theta_a: the apparent angle is imposed
@ kVofDynAdvancing
theta_app > theta_a: theta_a (+ Cox-Voinov)
VofMasterAssign
How a block's MASTER rank is chosen.
void clampFill(SField f, I3 e, int g, I3 o, I3 gs, bool px, bool py, bool pz, int skip=0)
Zero-gradient (Neumann) fill of the ghost cells that lie OUTSIDE the global domain on a non-periodic ...
void copyFaceVelocity(SField dst, I3 de, int dg, SField src, I3 se, int sg, int dir)
Embed the ENTIRE source extended block (inner + its sg ghost layers) into a LARGER destination extend...
void periodicFill(SField f, I3 e, int g, bool px, bool py, bool pz)
Periodic wrap of a single-block extended field at ghost width g (the single-rank ghost path; under MP...
double csfFaceForce(double sigma, double kf, double dC, double hGrad)
The balanced-force CSF contribution to the momentum RHS at one staggered velocity unknown,...
MomentumScheme
The transported face velocity of the momentum flux (doc/vof_momentum_second_order....
bool csfFaceCurvature(double k0, double b0, double k1, double b1, double &kf)
Face curvature for the balanced-force CSF, from the two cells sharing the face.
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< double *, SMem > SField
long L3(int x, int y, int z, I3 e)
Kokkos::View< const double *, CCMem > CCConst
void buildCellFraction(CCField cs, CCConst sdf, C3 e, int g)
void applyClosureFaceGhost(const Closure &cl, C3 e, int g, int a, int side)
constexpr const char * kVofMomBlocksMsg
enable_vof_momentum x enable_vof_blocks*: refused in EITHER call order.
Kokkos::DefaultExecutionSpace SExec
Kokkos::View< double *, CCMem > CCField
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).
std::vector< double > tabYPhys
std::array< double, 4 > pPhys
This rank's own patch: the extended block the face velocity and the union colour live on.
I3 o
global index of inner cell (0,0,0)
Half-open global cell box [lo, hi).
Per-call census of the cascade over this block's inner region (LOCAL; a distributed caller sums them ...
static double value(int gx, int gy, int gz)