flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
flow_ibm_phase_change.hpp
Go to the documentation of this file.
1
8#ifndef PECLET_FLOW_FLOW_IBM_PHASE_CHANGE_HPP
9#define PECLET_FLOW_FLOW_IBM_PHASE_CHANGE_HPP
10
11namespace peclet::flow {
12
13template <class Grid>
14void Solver<Grid>::enablePhaseChange(double rhoG, double rhoL, double hlv) {
15 if constexpr (Grid::collocated)
16 throw std::runtime_error(
17 "enable_phase_change: rungs P0/P1 are STAGGERED-ONLY (the collocated variable-density "
18 "pair has not been composed with the divergence-source deposit).");
19 enableVof();
20 if (hasSolid_)
21 throw std::runtime_error(
22 "enable_phase_change: an immersed solid is out of scope at rungs P0/P1 (the source "
23 "deposit and the regression would need the solid-clipped flux polygons of rung V5a's "
24 "follow-on). Use an all-fluid set_pressure_geometry.");
25 if (vofMomEnabled_)
26 throw std::runtime_error(
27 "enable_phase_change is not composable with enable_vof_momentum at this rung: both own "
28 "the head of the step, and momentum consistency would have to carry the interfacial mass "
29 "transfer in its own fluxes.");
30 if (!(rhoG > 0.0) || !(rhoL > 0.0))
31 throw std::runtime_error("enable_phase_change: both phase densities must be > 0");
32 if (!(hlv > 0.0))
33 throw std::runtime_error("enable_phase_change: the latent heat h_lv must be > 0");
34 pcRhoGPhys_ = rhoG;
35 pcRhoLPhys_ = rhoL;
36 pcHlvPhys_ = hlv;
38 // WO-P23: phase change and WO-R2 item 4's wisp guard are NOT compatible, and the measurement
39 // is in the findings. `enable_vof` sets `WyAdvector::wispEps = 1e-8`, which makes the advector
40 // treat a cell with `C <= 1e-8` as a PURE phase for reconstruction and flux. The phase-change
41 // driver needs the colour it reconstructs a plane from, deposits a source behind and pins a
42 // Dirichlet row in to be the colour that is actually advected; over the band
43 // `1e-12 < C < 1e-8` the two disagree, and on a CURVED interface that diverges the run — the
44 // P3 Scriven bubble at Ja = 0.5 reads `R(t)` error 48 % and trips the study's dt-collapse guard
45 // at step 34, against 2.002 % and 80 clean steps with the guard off. Sharing the tolerance
46 // (`pcEffInterfaceEps` / `pcEffPureEps`) is necessary but not sufficient: it fixes the planar
47 // gates and moves the Scriven blow-up from step 34 to step ~200 of 80-worth of physical time,
48 // and only `wispEps = 0` removes it. So phase change turns the guard off, loudly, and
49 // `set_vof_wisp_eps` after `enable_phase_change` is the deliberate override.
50 setVofWispEps(0.0);
51 pcMdot_ = addField("mdot");
52 pcSrc_ = addField("pc_source");
53 if (pcArea_.extent(0) != n_) {
54 pcArea_ = CCField("pc_area", n_);
55 pcNrm_[0] = CCField("pc_nx", n_);
56 pcNrm_[1] = CCField("pc_ny", n_);
57 pcNrm_[2] = CCField("pc_nz", n_);
58 pcDep_ = CCField("pc_dep", n_);
59 pcTgt_ = CCField("pc_tgt", n_);
60 pcCnew_ = CCField("pc_cnew", n_);
61 pcDefic_ = CCField("pc_defic", n_);
62 }
63 pcEnabled_ = true;
64}
65
66template <class Grid>
68 return pcEnabled_;
69}
70
71template <class Grid>
73 requirePhaseChange("set_mass_flux_uniform");
74 requireScalesPinned("set_mass_flux_uniform", true);
75 Kokkos::deep_copy(pcMdot_, v * u_.mdotToInt()); // `v * 1.0` in cell units
76 pcThermal_ = false;
77}
78
79template <class Grid>
80void Solver<Grid>::setMassFlux(const std::vector<double>& v) {
81 requirePhaseChange("set_mass_flux");
82 requireScalesPinned("set_mass_flux", true);
83 const double k = u_.mdotToInt();
84 if (k != 1.0) {
85 std::vector<double> vi(v.size());
86 for (std::size_t i = 0; i < v.size(); ++i)
87 vi[i] = v[i] * k;
88 scatterInner(pcMdot_, vi);
89 } else {
90 scatterInner(pcMdot_, v);
91 }
92 fillPropGhosts(pcMdot_);
93 pcThermal_ = false;
94}
95
96template <class Grid>
97void Solver<Grid>::requireScalesPinned(const char* who, bool needRho) const {
98 if (!u_.physical)
99 return;
100 if (!u_.tRefSet || (needRho && !u_.rhoRefSet))
101 throw std::runtime_error(
102 std::string(who) +
103 ": under a physical domain this writes a dimensional value into a field, converted once "
104 "with the reference scales; call set_rho and set_dt first (the first of each pins the "
105 "scale, and a field converted before that would be left in the wrong units)");
106}
107
108template <class Grid>
110 // Every factor is exactly 1.0 in cell units, so each line is `x = x_phys * 1.0` there.
111 pcRhoG_ = pcRhoGPhys_ * u_.rhoToInt();
112 pcRhoL_ = pcRhoLPhys_ * u_.rhoToInt();
113 pcHlv_ = pcHlvPhys_ * u_.latentToInt();
114 pcKg_ = pcKgPhys_ * u_.condToInt();
115 pcKl_ = pcKlPhys_ * u_.condToInt();
116 pcRint_ = pcRintPhys_ * u_.rintToInt();
117 pcRcpG_ = pcRcpGPhys_ * u_.rcpToInt();
118 pcRcpL_ = pcRcpLPhys_ * u_.rcpToInt();
119 if (pcEnergy_) { // the consumers that cached the old internal values
120 if (vofEnergy_.initialized())
121 vofEnergy_.setPhaseRcp(pcRcpG_, pcRcpL_);
123 }
124}
125
126template <class Grid>
127void Solver<Grid>::setPhaseChangeThermal(const std::string& tname, double Tsat, double kg,
128 double kl, double Rint) {
129 requirePhaseChange("set_phase_change_thermal");
130 if (!hasScalar(tname))
131 throw std::runtime_error("set_phase_change_thermal: no scalar named '" + tname +
132 "' (call add_scalar first)");
133 if (isCutcellScalar(tname))
134 throw std::invalid_argument("set_phase_change_thermal: '" + tname +
135 "' is a cut-cell scalar; the phase-change energy path takes a "
136 "legacy scalar only (doc/scalar_ibm_design.md §8.1)");
137 pcTName_ = tname;
138 pcTsat_ = Tsat; // a temperature: never rescaled
139 pcKgPhys_ = kg;
140 pcKlPhys_ = kl;
141 pcRintPhys_ = Rint;
143 pcThermal_ = true;
144 scalarDirichletMask(tname); // allocate the per-cell Dirichlet mask + value fields
146}
147
148template <class Grid>
150 pcThermal_ = false;
151}
152
153template <class Grid>
155 pcPlaneDir_ = on;
156}
157
158template <class Grid>
160 return pcPlaneDir_;
161}
162
163template <class Grid>
165 pcQuadFit_ = on;
166}
167
168template <class Grid>
170 return pcQuadFit_;
171}
172
173template <class Grid>
175 if (mode < 0 || mode > 7)
176 throw std::runtime_error(
177 "set_phase_change_area: mode must be 0 (PLIC/MYC), 1 (cascade metric), 2 (cascade "
178 "normal), 3 (cascade footprint) or 4-7 (the joined marching-tetrahedra sheet: 4 colour/"
179 "centroid, 5 colour/split, 6 PLIC-distance/centroid, 7 PLIC-distance/split)");
180 pcAreaMode_ = mode;
181}
182
183template <class Grid>
185 return pcAreaMode_;
186}
187
188template <class Grid>
190 if (!vofEnabled_)
191 throw std::runtime_error("vof_interface_area: VoF is not enabled (call enable_vof first)");
193 double a = 0.0;
194 if (pcAreaMode_ == vof::kAreaPlic) {
195 const I3 e3 = I3{e3_.x, e3_.y, e3_.z};
196 const int g = kVofG;
197 const long sy = e3.x, sz = (long)e3.x * e3.y;
198 CCConst c = CCConst(vofAdv_.colour());
199 const double eps = pcEffInterfaceEps();
200 // The PHYSICAL polygon area in hRef^2 (V5.1), as the phase-change build takes it: on box cells
201 // the unit-cube `plicArea` is not an area at all. Bit-identical on cubic cells (det = s = 1).
202 const vof::VofMetric gme = u_.vofMetric();
203 Kokkos::parallel_reduce(
204 "peclet::flow::vof_area_plic",
205 MDRange3<CCExec>(CCExec(), {g, g, g}, {g + nx_, g + ny_, g + nz_}),
206 KOKKOS_LAMBDA(int x, int y, int z, double& acc) {
207 const long i = (long)x + (long)y * e3.x + (long)z * sz;
208 if (!vof::pcIsInterfacial(c(i), eps))
209 return;
210 double st[27];
211 for (int kk = -1; kk <= 1; ++kk)
212 for (int jj = -1; jj <= 1; ++jj)
213 for (int ii = -1; ii <= 1; ++ii)
214 st[vof::plicSt(ii + 1, jj + 1, kk + 1)] = c(i + ii + jj * sy + kk * sz);
215 double m[3];
216 vof::mycNormal(st, m);
217 acc += vof::plicAreaMetric(m[0], m[1], m[2], vof::plicAlpha(m[0], m[1], m[2], c(i)), gme);
218 },
219 a);
220 Kokkos::fence();
221 } else {
222 a = pcAreaCascadeCompute().area;
223 }
224#ifdef PECLET_FLOW_MPI
225 if (distributed_) {
226 double gsum = 0.0;
227 MPI_Allreduce(&a, &gsum, 1, MPI_DOUBLE, MPI_SUM, comm_);
228 a = gsum;
229 }
230#endif
231 return a * u_.areaToPhys(); // hRef^2 -> the caller's area (exactly 1.0 in cell units)
232}
233
234template <class Grid>
236 if (pcAreaMode_ >= vof::kAreaMcColour) {
237 if (!pcAreaMc_.ready())
238 pcAreaMc_.init(nx_, ny_, nz_, kVofG);
239 pcAreaMc_.interfaceEps = pcEffInterfaceEps();
240 const auto m = pcAreaMc_.compute(vofAdv_.colour(), pcAreaMode_);
242 s.interfacial = m.cells;
243 s.hf = m.cells - m.orphanCells; // the cells the flux integral can actually use
244 s.hfMixed = 0;
245 s.pv = 0;
246 s.noEstimate = m.orphanCells;
247 s.area = m.area;
248 pcMcOrphanArea_ = m.orphanArea;
249 return s;
250 }
251 pcMcOrphanArea_ = 0.0;
252 if (!pcAreaC_.ready())
253 pcAreaC_.init(nx_, ny_, nz_, kVofG);
254 pcAreaC_.interfaceEps = pcEffInterfaceEps();
255 pcAreaC_.pureEps = vofAdv_.wispEps;
256 return pcAreaC_.compute(vofAdv_.colour(), pcAreaMode_);
257}
258
259template <class Grid>
261 return (pcAreaMode_ >= vof::kAreaMcColour) ? pcAreaMc_.area() : pcAreaC_.area();
262}
263
264template <class Grid>
265void Solver<Grid>::setPhaseChangeEnergy(double rcpGas, double rcpLiquid) {
266 requirePhaseChange("set_phase_change_energy");
267 if (!pcThermal_)
268 throw std::runtime_error(
269 "set_phase_change_energy: call set_phase_change_thermal first (the consistent transport "
270 "is for the energy scalar it names)");
271 if (!(rcpGas > 0.0) || !(rcpLiquid > 0.0))
272 throw std::runtime_error("set_phase_change_energy: both phase rho*c_p must be > 0");
273 pcRcpGPhys_ = rcpGas;
274 pcRcpLPhys_ = rcpLiquid;
275 pcRefreshUnits(); // pcEnergy_ is not yet set here, so this only derives the constants
276 if (pcKcell_.extent(0) != n_) {
277 pcKcell_ = CCField("pc_k", n_);
278 pcRcp_ = CCField("pc_rcp", n_);
279 }
280 ScalarField& sc = scalarField(pcTName_);
281 sc.energy = true;
282 sc.kcell = pcKcell_;
283 sc.rcp = pcRcp_;
284 if (!vofEnergy_.initialized())
285 vofEnergy_.init(vofAdv_, pcRcpG_, pcRcpL_);
286 else
287 vofEnergy_.setPhaseRcp(pcRcpG_, pcRcpL_);
288 vofEnergy_.exchange = [this](CCField f) { this->vofExchangeScalar(f); };
289 pcEnergy_ = true;
291}
292
293template <class Grid>
295 vofEnergy_.energyMuscl = on;
296}
297
298template <class Grid>
300 return vofEnergy_.energyMuscl;
301}
302
303template <class Grid>
305 pcEnergy_ = false;
306 if (!pcTName_.empty() && hasScalar(pcTName_))
307 scalarField(pcTName_).energy = false;
308}
309
310template <class Grid>
312 return pcEnergy_;
313}
314
315template <class Grid>
317 requirePhaseChange("set_phase_change_budget");
318 pcBudgetOn_ = on;
319 if (on && pcClsPrev_.extent(0) != n_) {
320 pcClsPrev_ = CCField("pc_cls_prev", n_);
321 Kokkos::deep_copy(pcClsPrev_, -1.0); // "no previous step": the first call counts no changes
322 }
323}
324
325template <class Grid>
327 return pcBudgetOn_;
328}
329
330template <class Grid>
332 requirePhaseChange("set_phase_change_carry_conserve");
333 pcCarryConserve_ = on;
334 if (on && pcCarrySrc_.extent(0) != n_) {
335 pcCarrySrc_ = CCField("pc_carry_src", n_);
336 Kokkos::deep_copy(pcCarrySrc_, 0.0);
337 }
338}
339
340template <class Grid>
342 return pcCarryConserve_;
343}
344
345template <class Grid>
347 requirePhaseChange("set_phase_change_fit_curvature");
348 pcFitKappa_ = kappa / u_.curvToPhys();
349}
350
351template <class Grid>
353 return pcFitKappa_ * u_.curvToPhys();
354}
355
356template <class Grid>
358 requirePhaseChange("set_phase_change_energy_order");
359 if (order != 1 && order != 2)
360 throw std::runtime_error("set_phase_change_energy_order: order must be 1 or 2");
361 setPhaseChangeMdotOperator(order == 2);
364 setPhaseChangeCarryConserve(order == 2);
365}
366
367template <class Grid>
369 return (pcMdotOperator_ && pcGfmOrder_ == 2 && pcCurvDist_ && pcCarryConserve_) ? 2 : 1;
370}
371
372template <class Grid>
374 requirePhaseChange("set_phase_change_mdot_operator");
375 if (on && pcRint_ != 0.0 && !pcThermal_)
376 throw std::runtime_error("set_phase_change_mdot_operator: needs the thermal mass flux");
377 pcMdotOperator_ = on;
378 if (on && pcMdotFit_.extent(0) != n_)
379 pcMdotFit_ = CCField("pc_mdot_fit", n_);
380}
381
382template <class Grid>
384 return pcMdotOperator_;
385}
386
387template <class Grid>
389 requirePhaseChange("set_phase_change_gfm_order");
390 if (order != 1 && order != 2)
391 throw std::runtime_error("set_phase_change_gfm_order: order must be 1 or 2");
392 pcGfmOrder_ = order;
393}
394
395template <class Grid>
397 return pcGfmOrder_;
398}
399
400template <class Grid>
402 requirePhaseChange("set_phase_change_curvature_distance");
403 pcCurvDist_ = on;
404 if (on && pcKappa_.extent(0) != n_)
405 pcKappa_ = CCField("pc_kappa", n_);
406 pcMaskFresh_ = false; // the row geometry has to be rebuilt with (or without) the curvature
407}
408
409template <class Grid>
411 return pcCurvDist_;
412}
413
414template <class Grid>
416 requirePhaseChange("set_phase_change_deposit_fallback");
417 pcDepositFallback_ = on;
418}
419
420template <class Grid>
422 return pcDepositFallback_;
423}
424
425template <class Grid>
427 return pcQOperator_ * u_.powerToPhys();
428}
429
430template <class Grid>
432 return pcQOrphan_ * u_.powerToPhys();
433}
434
435template <class Grid>
437 return pcCarryDeposited_ * pcLedgerEnergyToPhys();
438}
439
440template <class Grid>
442 return pcCarryLost_ * pcLedgerEnergyToPhys();
443}
444
445template <class Grid>
447 // A per-cell sum of rcp' (T - T_ref), cell volume left out: an energy with the consistent
448 // transport (internal energy unit x the cell volume in hRef^3), and with the constant-
449 // diffusivity scalar — whose ledgers weight by 1 instead of rho c_p — a temperature x volume.
450 return (pcEnergy_ ? u_.energyToPhys() : u_.volToPhys()) * u_.vol;
451}
452
453template <class Grid>
455 PhaseChangeBudget b = pcBudget_;
456 const double ke = pcLedgerEnergyToPhys();
457 // the GFM fluxes are sums of FACE fluxes k' o (dT)/theta: a power (per time: divide the
458 // energy unit by tRef), with no cell-volume factor
459 const double kq = pcEnergy_ ? u_.powerToPhys() : u_.volToPhys() * u_.divToPhys();
460 b.hOpen *= ke;
461 b.hOpenNew *= ke;
462 b.hLiquid *= ke;
463 b.hMasked *= ke;
464 b.dEoverwrite *= ke;
465 b.dEoverwriteNew *= ke;
466 b.eEnter *= ke;
467 b.eLeave *= ke;
468 b.qGfm *= kq;
469 b.qBehind *= kq;
470 return b;
471}
472
473template <class Grid>
475 const C3 e = e_;
476 const long sx = 1, sy = e_.x, sz = (long)e_.x * e_.y;
477 CCConst c = CCConst(cField_), U = CCConst(C[0].u), V = CCConst(C[1].u), W = CCConst(C[2].u);
478 CCConst ox = CCConst(ox_), oy = CCConst(oy_), oz = CCConst(oz_);
479 const double eps = pcEffInterfaceEps();
480 double m = 0.0;
481 Kokkos::parallel_reduce(
482 "peclet::flow::pc_band_div",
483 MDRange3<CCExec>(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
484 KOKKOS_LAMBDA(int x, int y, int z, double& acc) {
485 const long i = (long)x + (long)y * sy + (long)z * sz;
486 if (!vof::pcIsInterfacial(c(i), eps))
487 return;
488 const double d = (ox(i + sx) * U(i + sx) - ox(i) * U(i)) +
489 (oy(i + sy) * V(i + sy) - oy(i) * V(i)) +
490 (oz(i + sz) * W(i + sz) - oz(i) * W(i));
491 acc = Kokkos::fmax(acc, Kokkos::fabs(d));
492 },
493 Kokkos::Max<double>(m));
494 Kokkos::fence();
495 return Kokkos::fmax(m, 0.0);
496}
497
498template <class Grid>
500 if (!pcEnergy_)
501 return;
502 const C3 e = e_;
503 CCField kf = pcKcell_, rf = pcRcp_;
504 CCConst c = CCConst(cField_);
505 const double kg = pcKg_, kl = pcKl_, rg = pcRcpG_, rl = pcRcpL_;
506 Kokkos::parallel_for(
507 "peclet::flow::pc_energy_props",
508 MDRange3<CCExec>(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
509 KOKKOS_LAMBDA(int x, int y, int z) {
510 const long i = (long)x + (long)y * e.x + (long)z * (long)e.x * e.y;
511 const double cl = Kokkos::fmin(Kokkos::fmax(c(i), 0.0), 1.0);
512 kf(i) = vof::pcPhaseMix(kg, kl, cl);
513 rf(i) = vof::pcPhaseMix(rg, rl, cl);
514 });
515 Kokkos::fence();
516 fillPropGhosts(pcKcell_);
517 fillPropGhosts(pcRcp_);
518}
519
520template <class Grid>
521void Solver<Grid>::setDivergenceSource(const std::vector<double>& v) {
522 requireScalesPinned("set_divergence_source", false);
523 if (pcUser_.extent(0) != n_)
524 pcUser_ = addField("div_source");
525 const double k = u_.divToInt(); // S' = S * tRef (exactly 1.0 in cell units)
526 if (k != 1.0) {
527 std::vector<double> vi(v.size());
528 for (std::size_t i = 0; i < v.size(); ++i)
529 vi[i] = v[i] * k;
530 scatterInner(pcUser_, vi);
531 } else {
532 scatterInner(pcUser_, v);
533 }
534 fillPropGhosts(pcUser_);
535 pcHasUser_ = true;
536}
537
538template <class Grid>
540 pcHasUser_ = false;
541}
542
543template <class Grid>
544void Solver<Grid>::setDivergenceSink(const std::vector<double>& w) {
545 requirePhaseChange("set_divergence_sink");
546 if (pcSink_.extent(0) != n_)
547 pcSink_ = addField("div_sink");
548 scatterInner(pcSink_, w);
549 fillPropGhosts(pcSink_);
550 const C3 e = e_;
551 CCConst wv = CCConst(pcSink_);
552 double acc = 0.0;
553 Kokkos::parallel_reduce(
554 "peclet::flow::pc_sink_weight",
555 MDRange3<CCExec>(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
556 KOKKOS_LAMBDA(int x, int y, int z, double& a) {
557 a += wv((long)x + (long)y * e.x + (long)z * (long)e.x * e.y);
558 },
559 acc);
560 Kokkos::fence();
561#ifdef PECLET_FLOW_MPI
562 if (distributed_) {
563 double g = 0;
564 MPI_Allreduce(&acc, &g, 1, MPI_DOUBLE, MPI_SUM, comm_);
565 acc = g;
566 }
567#endif
568 if (!(acc > 0.0))
569 throw std::runtime_error("set_divergence_sink: the weights must have a positive global sum");
570 pcSinkW_ = acc;
571 pcHasSink_ = true;
572}
573
574template <class Grid>
576 pcHasSink_ = false;
577}
578
579template <class Grid>
580void Solver<Grid>::applyPhaseChange(double dtPhysArg) {
581 const double dt = dtPhysArg * u_.timeToInt();
582 requirePhaseChange("apply_phase_change");
585 pcRegress(dt);
588 // WO-P3f: the a-priori half of the energy-budget instrument. With no energy solve to bracket,
589 // `pcBudgetPost` on the CURRENT temperature and the just-rebuilt plane geometry answers the
590 // one question an exact-state probe can ask of the energy side: how much heat do the
591 // plane-anchored rows draw across this interface, against the `mdot h_lv A_Gamma` the SAME
592 // fields make the regression book? Inert unless `set_phase_change_budget(true)` ran.
593 if (pcBudgetOn_ && pcThermal_ && pcClsPrev_.extent(0) == n_) {
594 ScalarField& sc = scalarField(pcTName_);
595 if (sc.dmask.extent(0) == n_)
596 pcBudgetPost(sc);
597 }
598}
599
600template <class Grid>
602 if (!pcEnabled_)
603 return;
606 pcRegress(dt_);
607}
608
609template <class Grid>
611 requirePhaseChange("phase_change_diagnostics");
612 PhaseChangeDiagnostics d = pcDiag_;
613 // colour extrema of the CURRENT field (the regression's own boundedness read-out)
614 CCConst c = CCConst(cField_);
615 const C3 e = e_;
616 double mn = 1e300, mx = -1e300;
617 Kokkos::parallel_reduce(
618 "peclet::flow::pc_extrema",
619 MDRange3<CCExec>(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
620 KOKKOS_LAMBDA(int x, int y, int z, double& lo, double& hi) {
621 const long i = (long)x + (long)y * e.x + (long)z * (long)e.x * e.y;
622 lo = Kokkos::fmin(lo, c(i));
623 hi = Kokkos::fmax(hi, c(i));
624 },
625 Kokkos::Min<double>(mn), Kokkos::Max<double>(mx));
626 Kokkos::fence();
627 d.minC = mn;
628 d.maxC = mx;
629 d.bandDiv = pcBandDivergence();
630 if (pcEnergy_ && vofEnergy_.initialized())
631 vofEnergy_.extrema(d.Tmin, d.Tmax);
632 // To the caller's units (every factor exactly 1.0 in cell units; counts, colours and
633 // temperatures are unit-free). The colour ledger is a liquid VOLUME: colour x cell volume.
634 const double km = u_.mdotToPhys(), ka = u_.areaToPhys(), kq = u_.powerToPhys(),
635 kv = u_.volToPhys() * u_.vol, kd = u_.divToPhys();
636 d.mdotMin *= km;
637 d.mdotMax *= km;
638 d.mdotMean *= km;
639 d.mdotFit *= km;
640 d.area *= ka;
641 d.areaOrphan *= ka;
642 d.qOperator *= kq;
643 d.qOrphan *= kq;
644 d.removedVolume *= kv;
645 d.redistributed *= kv;
646 d.unresolved *= kv;
647 d.sourceSum *= kd;
648 d.bandDiv *= kd;
649 return d;
650}
651
652template <class Grid>
653bool Solver<Grid>::pcBuildInputs() {
654 // WO-P23 (a defect found in the P0/P1 code): the gradient fit reads the temperature at +-2, so
655 // the energy scalar's ghost band has to be VALID here. `set_field` and the coupling drivers
656 // write inner cells only, and `advanceScalars` fills the ghosts at its END — so the FIRST
657 // `apply_phase_change` / `step` of every run fitted its one-sided gradients against a band of
658 // zeros. Measured on the P2 sucking-interface kernel probe (an exact analytic state, no time
659 // stepping, quasi-2D so the y/z ghosts are inside the 5^3 stencil): mdot came out
660 // **8.16 against the exact 18.48**, a 56 % error that did NOT converge under refinement,
661 // because the transverse ghost samples are counted as pure liquid at T = 0 and pull the fit
662 // towards zero. One line, and it is a correctness fix, not a tolerance.
663 if (pcThermal_)
664 scalarFillGhosts(scalarField(pcTName_));
665 // WO-P3g item 1: the operator-flux `mdot` reads the very rows the energy solve uses, so the
666 // Dirichlet mask, the plane geometry and (item 3) the curvature must belong to the colour this
667 // build reconstructs from. In the COUPLED step they already do — `pcUpdateThermalMask` runs at
668 // the bottom of `step()` on exactly this colour, and nothing has touched it since — so this
669 // costs nothing there. After `set_vof` / `set_field`, i.e. in the one-shot `apply_phase_change`
670 // probe, they do not, and rebuilding here is what makes the probe measure the same operator the
671 // run does.
672 if (pcMdotOperator_ && pcThermal_ && !pcMaskFresh_) {
673 pcUpdateEnergyProps();
674 pcUpdateThermalMask();
675 }
676 // WO-P3c: the cascade-consistent interfacial area, computed on the colour field's OWN g = 3
677 // block (the height columns reach +-3, which the G = 2 phase-change block does not carry) and
678 // copied back onto the inner region. Inert at `pcAreaMode_ == kAreaPlic`: the kernel below then
679 // takes the branch it always took, and `pcAreaCg2_` is never even allocated.
680 const bool cascadeArea = (pcAreaMode_ != vof::kAreaPlic);
681 if (cascadeArea) {
682 if (pcAreaCg2_.extent(0) != n_)
683 pcAreaCg2_ = CCField("pc_area_cascade", n_);
684 bridgeColourToVof();
685 const auto as = pcAreaCascadeCompute();
686 pcDiag_.areaHf = as.hf + as.hfMixed;
687 pcDiag_.areaPv = as.pv;
688 pcDiag_.areaNone = as.noEstimate;
689 pcDiag_.areaOrphan = pcMcOrphanArea_;
690 copyInner(pcAreaCg2_, e_, G, CCConst(pcAreaCascadeField()), e3_, kVofG);
691 }
692 return cascadeArea;
693}
694
695template <class Grid>
696void Solver<Grid>::pcOperatorDomain(bool opMode, bool opEnergy) {
697 // UNITS. Without the consistent energy transport the operator flux below is built from the
698 // scalar's DIFFUSIVITY, i.e. it is the heat divided by an implicit rho c_p = 1 — one unit of
699 // the solver's internal system, which is a different physical heat capacity in every unit
700 // system. In cell units that is the shipped (and gated) behaviour and stays; under an armed
701 // extent it has no physical meaning, so the combination is refused rather than guessed.
702 if (opMode && !opEnergy && u_.physical)
703 throw std::runtime_error(
704 "set_phase_change_mdot_operator / set_phase_change_energy_order(2) under a physical "
705 "domain need set_phase_change_energy(True, rho_cp_gas, rho_cp_liquid): without it the "
706 "operator's interfacial heat is a diffusivity flux with an implicit rho*c_p of one "
707 "internal unit, which is not a physical heat capacity");
708 if (opMode)
709 pcBuildInDomain();
710}
711
712template <class Grid>
714 const C3 e = e_;
715 const long sz = (long)e_.x * e_.y;
716 // extrema of mdot over interfacial cells
717 double mn = 0.0, mx = 0.0;
718 if (nIface > 0) {
719 mn = 1e300;
720 mx = -1e300;
721 CCConst md = CCConst(pcMdot_), ar = CCConst(pcArea_);
722 Kokkos::parallel_reduce(
723 "peclet::flow::pc_mdot_extrema",
724 MDRange3<CCExec>(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
725 KOKKOS_LAMBDA(int x, int y, int z, double& lo, double& hi) {
726 const long i = (long)x + (long)y * e.x + (long)z * sz;
727 if (ar(i) > 0.0) {
728 lo = Kokkos::fmin(lo, md(i));
729 hi = Kokkos::fmax(hi, md(i));
730 }
731 },
732 Kokkos::Min<double>(mn), Kokkos::Max<double>(mx));
733 Kokkos::fence();
734 }
735 pcDiag_.mdotMin = mn;
736 pcDiag_.mdotMax = mx;
737}
738
739template <class Grid>
740void Solver<Grid>::pcExchangeInterfaceGhosts() {
741 // The exchange that makes the two consumers decomposition-independent. `fillGhosts` is the
742 // halo/periodic base; on a NON-periodic domain face the periodic wrap would import the far
743 // side's interface as a phantom source/deficit donor, so those ghosts are zeroed.
744 fillGhosts(pcMdot_);
745 fillGhosts(pcArea_);
746 fillGhosts(pcDep_);
747 fillGhosts(pcTgt_);
748 for (int d = 0; d < 3; ++d)
749 fillGhosts(pcNrm_[d]);
750 pcZeroDomainGhosts(pcMdot_);
751 pcZeroDomainGhosts(pcArea_);
752 pcZeroDomainGhosts(pcDep_);
753 pcZeroDomainGhosts(pcTgt_);
754 for (int d = 0; d < 3; ++d)
755 pcZeroDomainGhosts(pcNrm_[d]);
756}
757
758template <class Grid>
760 const bool cascadeArea = pcBuildInputs();
761 CCConst areaCasc = CCConst(cascadeArea ? pcAreaCg2_ : pcMdot_);
762 const C3 e = e_;
763 const long sy = e_.x, sz = (long)e_.x * e_.y;
764 CCField mdot = pcMdot_, area = pcArea_, dep = pcDep_, tgt = pcTgt_;
765 CCField nx = pcNrm_[0], ny = pcNrm_[1], nz = pcNrm_[2];
766 CCConst c = CCConst(cField_);
767 const bool thermal = pcThermal_;
768 CCConst T = thermal ? CCConst(scalarField(pcTName_).c) : CCConst(cField_);
769 const double eps = pcEffInterfaceEps(), pureEps = pcEffPureEps();
770 const bool quad = pcQuadFit_;
771 const bool depFallback = pcDepositFallback_;
772 const double Tsat = pcTsat_, kg = pcKg_, kl = pcKl_, hlv = pcHlv_, Rint = pcRint_;
773 const double kapPresc = pcFitKappa_; // WO-P3f, 0 = the shipped (tangent-plane) distance
774 const double rhoG = pcRhoG_, rhoL = pcRhoL_;
775 // WO-P3g. `curvDist` (item 3) replaces the prescribed curvature by the V3 cascade's per-cell
776 // one; `opMode` (item 1) replaces the least-squares fit by the energy operator's own flux, for
777 // which the whole Dirichlet row set has to be readable inside the kernel.
778 const bool curvDist = pcCurvDist_ && pcKappa_.extent(0) == n_;
779 CCConst kapF = CCConst(curvDist ? pcKappa_ : pcMdot_);
780 ScalarField* scT = thermal ? &scalarField(pcTName_) : nullptr;
781 const bool opMode =
782 pcMdotOperator_ && thermal && pcGphi_.extent(0) == n_ && scT->dmask.extent(0) == n_;
783 const bool opEnergy = opMode && scT->energy && scT->kcell.extent(0) == n_;
784 pcOperatorDomain(opMode, opEnergy);
785 CCConst mkF = CCConst(opMode ? scT->dmask : pcMdot_);
786 CCConst inD = CCConst(opMode ? pcInDomain_ : pcMdot_);
787 CCConst kcF = CCConst(opEnergy ? scT->kcell : pcMdot_);
788 CCConst oxF = CCConst(ox_), oyF = CCConst(oy_), ozF = CCConst(oz_);
789 const double Dconst = thermal ? scT->D : 0.0, thMin = pcGfmThMin_, thMax = pcGfmThMax_;
790 const int gfmOrder = pcGfmOrder_;
791 // Phase 3 (V5, `flow/doc/anisotropic_vof.md` §7): the phase-change layer is where lengths,
792 // areas and the cell VOLUME all enter at once. `gme` is the per-axis metric; `vCell` the cell
793 // volume in hRef^3. Both are exactly 1 on every isotropic run.
794 const vof::VofMetric gme = u_.vofMetric();
795 const double vCell = u_.vol;
796 CCField mdotFit = opMode ? pcMdotFit_ : pcMdot_;
797 long nIface = 0, nFallback = 0;
798 double sumArea = 0.0, sumMdot = 0.0, sumQ = 0.0, sumQorph = 0.0, sumMdotFitA = 0.0;
799 Kokkos::parallel_reduce(
800 "peclet::flow::pc_build", MDRange3<CCExec>(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
801 KOKKOS_LAMBDA(int x, int y, int z, long& nif, long& nfb, double& aacc, double& macc,
802 double& qacc, double& qorph, double& mfacc) {
803 const long i = (long)x + (long)y * e.x + (long)z * sz;
804 area(i) = 0.0;
805 dep(i) = 0.0;
806 tgt(i) = 0.0;
807 nx(i) = 0.0;
808 ny(i) = 0.0;
809 nz(i) = 0.0;
810 if (!vof::pcIsInterfacial(c(i), eps)) {
811 if (thermal)
812 mdot(i) = 0.0;
813 return;
814 }
815 double st[27];
816 for (int kk = -1; kk <= 1; ++kk)
817 for (int jj = -1; jj <= 1; ++jj)
818 for (int ii = -1; ii <= 1; ++ii)
819 st[vof::plicSt(ii + 1, jj + 1, kk + 1)] = c(i + ii + jj * sy + kk * sz);
820 double m[3];
821 vof::mycNormal(st, m);
822 const double al = vof::plicAlpha(m[0], m[1], m[2], c(i));
823 // PHYSICAL area (V5.1) — `areaCasc` already carries the metric from its own driver.
824 const double A = cascadeArea ? areaCasc(i) : vof::plicAreaMetric(m[0], m[1], m[2], al, gme);
825 // The PHYSICAL unit normal and the PHYSICAL centre distance (V5.2): `s(m)` is the shape
826 // factor of §2, exactly 1.0 at equal spacings, so both are today's values there.
827 double n[3] = {1.0, 0.0, 0.0};
828 const double sMet = vof::vofPhysNormal(m, gme, n);
829 if (!(sMet > 0.0))
830 return;
831 const double phic = vof::pcCentreDistance(m[0], m[1], m[2], al) / sMet;
832 // **WO-P3g item 1 — the area is a UNIT CONVERSION, not a term in the mass balance.**
833 // The regression removes `dV = mdot A dt/rho_l` and the source deposits
834 // `S = mdot A (1/rho_g - 1/rho_l)`, and with `mdot = q/(h_lv A)` BOTH are `A`-free:
835 // `dV = q dt/(h_lv rho_l)`, `S = q (1/rho_g - 1/rho_l)/h_lv`. So a cell the AREA
836 // ESTIMATOR gave nothing still has to evaporate the heat its Dirichlet rows draw, or
837 // that heat is simply destroyed. Measured on the a-priori probe (exact sphere, R = 20,
838 // Ja 0.5, area mode 6): **27.6 % of the operator's total interfacial heat sits on
839 // interfacial cells with A = 0** -- the joined marching-tetrahedra sheet deposits each
840 // triangle to the cell holding its centroid, so a band cell can carry an interface and
841 // no area. `Aeff` is the unit conversion those cells use; the DIAGNOSTIC area
842 // (`interface_area`) stays the geometric one, so `mdot_area` is unaffected.
843 double Aeff = A;
844 double md = mdot(i);
845 if (thermal) {
846 const double Tg = vof::pcInterfaceTemperature(Tsat, md, Rint);
847 // WO-P3g item 3: the sample distance is measured to the CURVED interface, with the
848 // curvature taken per cell from the V3 cascade (`curvDist`) or, as WO-P3f shipped it,
849 // prescribed for the whole field. Bitwise unchanged when both are 0.
850 const double kapFit = curvDist ? kapF(i) : kapPresc;
851 vof::PcGradFit fg, fl;
852 for (int dz = -2; dz <= 2; ++dz)
853 for (int dy = -2; dy <= 2; ++dy)
854 for (int dx = -2; dx <= 2; ++dx) {
855 if (dx == 0 && dy == 0 && dz == 0)
856 continue;
857 // V5.2: the sample offset is PHYSICAL — `delta = H d` — because the fit models
858 // T against the physical normal distance and returns a physical dT/dn. With
859 // index offsets the fitted gradient would be off by the shape factor `s(m)`.
860 // `g.h = {1,1,1}` multiplies each component by 1.0.
861 const double di[3] = {(double)dx, (double)dy, (double)dz};
862 double dp[3];
863 gme.toPhys(di, dp);
864 const double w = vof::pcGradWeight(dp[0], dp[1], dp[2], n);
865 if (!(w > 0.0))
866 continue;
867 const long j = i + dx + dy * sy + dz * sz;
868 const double cj = c(j);
869 double phi = vof::pcOffsetDistance(phic, n, dp[0], dp[1], dp[2]);
870 // WO-P3f: bitwise unchanged at kappaFit == 0 (the shipped default)
871 if (kapFit != 0.0)
872 phi = vof::pcCurvedDistance(phi, dp[0], dp[1], dp[2], n, kapFit);
873 if (cj <= pureEps && phi > 0.0)
874 vof::pcGradAdd(fg, w, phi, T(j), Tg);
875 else if (cj >= 1.0 - pureEps && phi < 0.0)
876 vof::pcGradAdd(fl, w, phi, T(j), Tg);
877 }
878 md = quad ? vof::pcMassFlux(kg, vof::pcGradSolve2(fg), kl, vof::pcGradSolve2(fl), hlv)
879 : vof::pcMassFlux(kg, vof::pcGradSolve(fg), kl, vof::pcGradSolve(fl), hlv);
880 // **WO-P3g item 1 — `mdot` from the ENERGY OPERATOR's own interfacial flux.**
881 //
882 // The rows the energy solve runs are `scalarMaskGfm2`'s, one per (pure cell, masked
883 // neighbour) face; this gathers the SAME rows from the masked side and asks how much
884 // heat they draw. Every ingredient is read from the state that solve used — the
885 // Dirichlet mask, the face openness, `k(C)` and the plane geometry — and the plane this
886 // kernel just reconstructed is bitwise the plane `pcUpdateThermalMask` stored (same
887 // colour, same `mycNormal`/`plicAlpha`), so `theta` is identical to the row's own.
888 //
889 // What it buys: `mdot h_lv A = q` makes the heat the energy equation loses and the mass
890 // the regression produces ONE discrete quantity (WO-P3f measured the shipped pair
891 // disagreeing by 0.95…1.04), and `A` then cancels out of both the plane shift
892 // `dV = mdot A dt/rho_l` and the divergence source — the interfacial area stops being a
893 // term in the mass balance at all.
894 if (opMode) {
895 const long stq[3] = {1, sy, sz};
896 double qsum = 0.0, csum = 0.0;
897 for (int d = 0; d < 3; ++d)
898 for (int sg = -1; sg <= 1; sg += 2) {
899 const long pc = i + (long)sg * stq[d];
900 if (mkF(pc) > 0.5)
901 continue; // an identity row: it carries no Dirichlet coupling
902 if (!(inD(pc) > 0.5))
903 continue; // outside the global domain: no row exists there at all
904 const double of = (d == 0) ? ((sg > 0) ? oxF(i + 1) : oxF(i))
905 : (d == 1) ? ((sg > 0) ? oyF(i + sy) : oyF(i))
906 : ((sg > 0) ? ozF(i + sz) : ozF(i));
907 if (!(of > 0.0))
908 continue;
909 const double ofB = (d == 0) ? ((sg > 0) ? oxF(pc + 1) : oxF(pc))
910 : (d == 1) ? ((sg > 0) ? oyF(pc + sy) : oyF(pc))
911 : ((sg > 0) ? ozF(pc + sz) : ozF(pc));
912 const long jb = pc + (long)sg * stq[d];
913 const bool behind = !(mkF(jb) > 0.5) && ofB > 0.0;
914 // the step from the PURE cell `pc` to this (masked) cell is -sg
915 // V5.3: `theta` is a distance ALONG a grid line, i.e. an axis ratio — it takes
916 // the INDEX centre distance and the INDEX normal component, both of which the
917 // metric-aware overload reconstructs from the physical pair.
918 const double th = vof::pcGfmThetaKAniso(
919 phic, n, d, (double)(-sg), curvDist ? kapF(i) : 0.0, thMin, thMax, gme);
920 const vof::PcGfmRow row = vof::pcGfmRow(th, behind, gfmOrder);
921 const double cf = (opEnergy ? kcF(pc) : Dconst) * of * row.aGamma;
922 qsum += cf * (T(pc) - Tsat);
923 csum += cf;
924 // The three-point row also RESCALES `pc`'s band toward the cell BEHIND it, and
925 // that rescaling is one-sided (cell `jb`'s own row does not mirror it), so it is
926 // part of the heat the interface removes from the solved set. Booking it here is
927 // what makes `sum_j Q_j` the EXACT total the operator transfers -- and, on a
928 // 1-D quadratic, the exact conductive flux through the interfacial face at every
929 // theta (gate (a); with the Dirichlet coupling alone it is a factor 2/(1+theta)
930 // off, which is the whole point of the second-order row).
931 if (behind && row.aBehind != 1.0) {
932 const double kb = opEnergy ? 0.5 * (kcF(pc) + kcF(jb)) : Dconst;
933 qsum += (row.aBehind - 1.0) * kb * ofB * (T(pc) - T(jb));
934 }
935 }
936 mdotFit(i) = md; // the least-squares estimator, kept as a diagnostic
937 Aeff = (A > 0.0) ? A : 1.0;
938 md = vof::pcOperatorMassFlux(qsum, csum, Aeff, hlv, Rint);
939 qacc += qsum;
940 if (!(A > 0.0))
941 qorph += qsum; // reported, but no longer dropped
942 mfacc += mdotFit(i) * A;
943 }
944 mdot(i) = md;
945 }
946 area(i) = Aeff;
947 nx(i) = n[0];
948 ny(i) = n[1];
949 nz(i) = n[2];
950 ++nif;
951 aacc += A; // the GEOMETRIC area, for the diagnostics; `Aeff` is the unit conversion
952 macc += md;
953 // the divergence source and the pure-gas cell that will carry it
954 // V5.5: `S` is a volumetric source, so the interfacial flux is divided by the cell
955 // VOLUME `V' = hp_x hp_y hp_z` (exactly 1.0 isotropic: `x / 1.0 == x`).
956 const double S = vof::pcDivSource(md, Aeff, rhoG, rhoL) / vCell;
957 if (S != 0.0) {
958 // WO-P23: the receiving PURE GAS cell is the BEST cell of the 5^3 neighbourhood on
959 // the `+n` side, scored by Malan's own collinearity weight `(d.n)^2/|d|^3` — closest
960 // and most nearly along the normal wins, so it returns the `d = round(n)` cell whenever
961 // that one is pure and degrades gracefully when it is not. The rung P0/P1 rule tried
962 // exactly two candidates (`round(k n)`, k = 1, 2) and left the source IN the
963 // interfacial cell when both were still interfacial — which is what happens on a
964 // CURVED interface: measured 48 … 262 such cells on the P3 Scriven bubble, and each of
965 // them then carries `div(open u) = S` on its OWN faces, i.e. Weymouth-Yue advects it
966 // with a field that is not the liquid velocity (that is exactly what
967 // `phase_change_diagnostics()['band_div']` reports). The search order is fixed and the
968 // comparison strict, so the choice is deterministic and decomposition-independent.
969 int tx = 0, ty = 0, tz = 0;
970 bool found = false;
971 for (int k = 1; k <= 2 && !found; ++k) { // the rung P0/P1 rule, FIRST and unchanged
972 const int ox = (int)Kokkos::round(k * n[0]);
973 const int oy = (int)Kokkos::round(k * n[1]);
974 const int oz = (int)Kokkos::round(k * n[2]);
975 if (ox == 0 && oy == 0 && oz == 0)
976 continue;
977 if (c(i + ox + oy * sy + oz * sz) <= pureEps) {
978 tx = ox;
979 ty = oy;
980 tz = oz;
981 found = true;
982 }
983 }
984 if (!found && depFallback) {
985 // Only where that rule FAILS does the deposit fall back to the best cell of the `+n`
986 // half of the 5^3 box, scored by Malan's collinearity weight. Those are the cells the
987 // P0/P1 code left the source in (48 … 262 of them on the Scriven bubble), and each
988 // one then carries `div(open u) = S` on its OWN faces, i.e. Weymouth-Yue advecting
989 // the colour with a field that is not the liquid velocity.
990 //
991 // The order matters and the measurement says so: making the search the PRIMARY rule
992 // (best-`w` over the whole box) DIVERGES the Scriven bubble — `max|uf|` runs away and
993 // the study's dt-collapse guard trips at step 316 of the Ja = 0.5 run with the
994 // Weymouth-Yue Courant number pinned at 0.38 however small dt is. The planar gates
995 // (P0a/P0b/P1/P2) never saw it, because there the two rules choose the same cell.
996 // Preferring the along-the-normal candidates keeps the validated behaviour wherever
997 // it existed and only fills the holes.
998 double best = 0.0;
999 for (int dz = -2; dz <= 2; ++dz)
1000 for (int dy = -2; dy <= 2; ++dy)
1001 for (int dx = -2; dx <= 2; ++dx) {
1002 if (dx == 0 && dy == 0 && dz == 0)
1003 continue;
1004 // V5.5: the candidate is scored on its PHYSICAL offset, so on a stretched
1005 // grid the search still prefers the cell that is nearest AND most along the
1006 // normal. `g.h = {1,1,1}` multiplies each component by 1.0.
1007 const double di[3] = {(double)dx, (double)dy, (double)dz};
1008 double dp[3];
1009 gme.toPhys(di, dp);
1010 if (!(dp[0] * n[0] + dp[1] * n[1] + dp[2] * n[2] > 0.0))
1011 continue; // the deposit goes BEHIND the interface, into the gas
1012 const double w = vof::pcGradWeight(dp[0], dp[1], dp[2], n);
1013 if (!(w > best))
1014 continue;
1015 if (c(i + dx + dy * sy + dz * sz) > pureEps)
1016 continue;
1017 best = w;
1018 tx = dx;
1019 ty = dy;
1020 tz = dz;
1021 }
1022 if (!(best > 0.0))
1023 ++nfb; // no pure gas cell in the 5^3 box on the +n side: the source stays put
1024 } else if (!found) {
1025 ++nfb; // the default: no pure gas cell within two cells, the source stays put
1026 }
1027 dep(i) = S;
1028 tgt(i) = (double)((tx + 2) + 5 * (ty + 2) + 25 * (tz + 2));
1029 }
1030 },
1031 nIface, nFallback, sumArea, sumMdot, sumQ, sumQorph, sumMdotFitA);
1032 Kokkos::fence();
1033 pcQOperator_ = sumQ;
1034 pcQOrphan_ = sumQorph;
1035 pcMdotFitMean_ = sumArea > 0.0 ? sumMdotFitA / sumArea : 0.0;
1036 pcDiag_.qOperator = sumQ;
1037 pcDiag_.qOrphan = sumQorph;
1038 pcDiag_.mdotFit = pcMdotFitMean_;
1039 pcDiag_.interfaceCells = nIface;
1040 pcDiag_.fallbackCells = nFallback;
1041 pcDiag_.area = sumArea;
1042 pcDiag_.mdotMean = nIface > 0 ? sumMdot / (double)nIface : 0.0;
1043 pcMdotExtrema(nIface);
1044 pcExchangeInterfaceGhosts();
1045}
1046
1047template <class Grid>
1049 const C3 e = e_;
1050 const long sy = e_.x, sz = (long)e_.x * e_.y;
1051 CCField src = pcSrc_;
1052 CCConst dep = CCConst(pcDep_), tgt = CCConst(pcTgt_);
1053 double sum = 0.0;
1054 long ncell = 0;
1055 Kokkos::parallel_reduce(
1056 "peclet::flow::pc_source_gather",
1057 MDRange3<CCExec>(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
1058 KOKKOS_LAMBDA(int x, int y, int z, double& acc, long& nc) {
1059 const long i = (long)x + (long)y * e.x + (long)z * sz;
1060 double s = 0.0;
1061 for (int dz = -2; dz <= 2; ++dz)
1062 for (int dy = -2; dy <= 2; ++dy)
1063 for (int dx = -2; dx <= 2; ++dx) {
1064 const long j = i + dx + dy * sy + dz * sz;
1065 const double dj = dep(j);
1066 if (dj == 0.0)
1067 continue;
1068 const int code = (int)tgt(j);
1069 const int tx = code % 5 - 2, ty = (code / 5) % 5 - 2, tz = code / 25 - 2;
1070 if (tx + dx == 0 && ty + dy == 0 && tz + dz == 0)
1071 s += dj;
1072 }
1073 src(i) = s;
1074 acc += s;
1075 if (s != 0.0)
1076 ++nc;
1077 },
1078 sum, ncell);
1079 Kokkos::fence();
1080#ifdef PECLET_FLOW_MPI
1081 if (distributed_) {
1082 double g = 0;
1083 MPI_Allreduce(&sum, &g, 1, MPI_DOUBLE, MPI_SUM, comm_);
1084 sum = g;
1085 }
1086#endif
1087 // WO-P23: the auto-balanced sink. Subtract the GLOBAL deposited source, spread over the sink
1088 // weights, so a closed domain's Poisson RHS is compatible by construction.
1089 if (pcHasSink_) {
1090 const double f = sum / pcSinkW_;
1091 CCConst wv = CCConst(pcSink_);
1092 Kokkos::parallel_for(
1093 "peclet::flow::pc_sink_apply",
1094 MDRange3<CCExec>(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
1095 KOKKOS_LAMBDA(int x, int y, int z) {
1096 const long i = (long)x + (long)y * e.x + (long)z * sz;
1097 src(i) -= f * wv(i);
1098 });
1099 Kokkos::fence();
1100 }
1101 pcDiag_.sourceSum = sum;
1102 pcDiag_.sourceCells = ncell;
1103 fillPropGhosts(pcSrc_);
1104}
1105
1106template <class Grid>
1108 const C3 e = e_;
1109 const long sy = e_.x, sz = (long)e_.x * e_.y;
1110 CCField Cf = cField_, cnew = pcCnew_, defic = pcDefic_;
1111 CCConst md = CCConst(pcMdot_), ar = CCConst(pcArea_);
1112 const double rhoL = pcRhoL_;
1113 // V5.5: `mdot A dt/rho_l` is a liquid VOLUME; the colour it removes is that volume divided by
1114 // the CELL volume `V' = hp_x hp_y hp_z` (exactly 1.0 on every isotropic run).
1115 const double vCell = u_.vol;
1116 double removed = 0.0;
1117 long ndef = 0, nexc = 0;
1118 double redist = 0.0;
1119 Kokkos::parallel_reduce(
1120 "peclet::flow::pc_regress_raw", MDRange3<CCExec>(CCExec(), {0, 0, 0}, {e.x, e.y, e.z}),
1121 KOKKOS_LAMBDA(int x, int y, int z, double& rem, long& nd, long& ne, double& rd) {
1122 const long i = (long)x + (long)y * e.x + (long)z * sz;
1123 const double A = ar(i);
1124 if (!(A > 0.0)) {
1125 cnew(i) = Cf(i);
1126 defic(i) = 0.0;
1127 return;
1128 }
1129 const double dV = vof::pcRegressVolume(md(i), A, dt, rhoL) / vCell;
1130 const double raw = Cf(i) - dV;
1131 const double cl = Kokkos::fmin(Kokkos::fmax(raw, 0.0), 1.0);
1132 cnew(i) = cl;
1133 defic(i) = raw - cl;
1134 const bool inner =
1135 (x >= G && x < e.x - G && y >= G && y < e.y - G && z >= G && z < e.z - G);
1136 if (inner) {
1137 rem += dV;
1138 if (raw < 0.0)
1139 ++nd;
1140 if (raw > 1.0)
1141 ++ne;
1142 rd += Kokkos::fabs(raw - cl);
1143 }
1144 },
1145 removed, ndef, nexc, redist);
1146 Kokkos::fence();
1147 CCConst cn = CCConst(pcCnew_), df = CCConst(pcDefic_);
1148 CCConst nxv = CCConst(pcNrm_[0]), nyv = CCConst(pcNrm_[1]), nzv = CCConst(pcNrm_[2]);
1149 Kokkos::parallel_for(
1150 "peclet::flow::pc_regress_apply",
1151 MDRange3<CCExec>(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
1152 KOKKOS_LAMBDA(int x, int y, int z) {
1153 const long i = (long)x + (long)y * e.x + (long)z * sz;
1154 double v = cn(i);
1155 const long st[3] = {1, sy, sz};
1156 for (int d = 0; d < 3; ++d)
1157 for (int s = -1; s <= 1; s += 2) {
1158 const long j = i + (long)s * st[d]; // the neighbour that might push into i
1159 const double dj = df(j);
1160 if (dj == 0.0)
1161 continue;
1162 // Recompute j's WHOLE allocation here (not just i's share): every receiver runs the
1163 // identical arithmetic on the identical inputs, so the sum each cell forms has a
1164 // fixed order and is bitwise independent of the decomposition.
1165 const double sgn = dj < 0.0 ? -1.0 : 1.0;
1166 double n[3] = {nxv(j), nyv(j), nzv(j)};
1167 int step[3];
1168 double w[3];
1169 bool avail[3];
1170 for (int q = 0; q < 3; ++q) {
1171 const double p = sgn * n[q];
1172 const int sq = (p > 0.0) ? 1 : ((p < 0.0) ? -1 : 0);
1173 const double ct = sq == 0 ? 0.0 : cn(j + (long)sq * st[q]);
1174 avail[q] = sq != 0 && (dj < 0.0 ? (ct > 0.0) : (ct < 1.0));
1175 }
1176 vof::pcPushWeights(n, sgn, avail, step, w);
1177 // j pushes into j + step[d]*e_d; that is i iff step[d] == -s
1178 if (step[d] == -s)
1179 v += dj * w[d];
1180 }
1181 Cf(i) = v;
1182 });
1183 Kokkos::fence();
1184 pcDiag_.removedVolume = removed;
1185 pcDiag_.deficitCells = ndef;
1186 pcDiag_.excessCells = nexc;
1187 pcDiag_.redistributed = redist;
1188 // How much residue found no neighbour able to absorb it (pushed anyway, on the unrestricted
1189 // weights, so conservation holds and the colour goes slightly out of [0,1] instead).
1190 double unres = 0.0;
1191 Kokkos::parallel_reduce(
1192 "peclet::flow::pc_unresolved",
1193 MDRange3<CCExec>(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
1194 KOKKOS_LAMBDA(int x, int y, int z, double& acc) {
1195 const long i = (long)x + (long)y * e.x + (long)z * sz;
1196 const double dj = df(i);
1197 if (dj == 0.0)
1198 return;
1199 const double sgn = dj < 0.0 ? -1.0 : 1.0;
1200 const long st[3] = {1, sy, sz};
1201 double n[3] = {nxv(i), nyv(i), nzv(i)};
1202 bool avail[3];
1203 for (int q = 0; q < 3; ++q) {
1204 const double p = sgn * n[q];
1205 const int sq = (p > 0.0) ? 1 : ((p < 0.0) ? -1 : 0);
1206 const double ct = sq == 0 ? 0.0 : cn(i + (long)sq * st[q]);
1207 avail[q] = sq != 0 && (dj < 0.0 ? (ct > 0.0) : (ct < 1.0));
1208 }
1209 int step[3];
1210 double w[3];
1211 if (!vof::pcPushWeights(n, sgn, avail, step, w))
1212 acc += Kokkos::fabs(dj);
1213 },
1214 unres);
1215 Kokkos::fence();
1216 pcDiag_.unresolved = unres;
1217 fillPropGhosts(cField_);
1218}
1219
1220template <class Grid>
1222 if (pcKappa_.extent(0) != n_)
1223 pcKappa_ = CCField("pc_kappa", n_);
1225 vofCurv_.compute(vofAdv_.colour());
1226 copyInner(pcKappa_, e_, G, CCConst(vofCurv_.kappa()), e3_, kVofG);
1227 // read at depth 1 (the GFM row asks for the INTERFACIAL neighbour's curvature), so exchange;
1228 // a periodic wrap on a non-periodic domain face would import a phantom curvature, so zero it.
1229 fillGhosts(pcKappa_);
1230 pcZeroDomainGhosts(pcKappa_);
1231}
1232
1233template <class Grid>
1235 if (pcInDomain_.extent(0) == n_)
1236 return;
1237 pcInDomain_ = CCField("pc_indomain", n_);
1238 Kokkos::deep_copy(pcInDomain_, 0.0);
1239 const C3 e = e_;
1240 CCField f = pcInDomain_;
1241 Kokkos::parallel_for(
1242 "peclet::flow::pc_indomain",
1243 MDRange3<CCExec>(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
1244 KOKKOS_LAMBDA(int x, int y, int z) {
1245 f((long)x + (long)y * e.x + (long)z * (long)e.x * e.y) = 1.0;
1246 });
1247 Kokkos::fence();
1248 fillGhosts(pcInDomain_);
1249 pcZeroDomainGhosts(pcInDomain_);
1250}
1251
1252template <class Grid>
1254 if (!pcThermal_)
1255 return;
1256 if (pcCurvDist_)
1257 pcUpdateCurvature(); // WO-P3g item 3: kappa for the row's theta and for the carried-value fit
1258 ScalarField& sc = scalarField(pcTName_);
1259 scalarFillGhosts(sc); // the carried-value refit reads T at +-2 (see pcBuildInterface)
1260 const C3 e = e_;
1261 const long sy = e_.x, sz = (long)e_.x * e_.y;
1262 CCField mk = sc.dmask, dv = sc.dval, tg = pcTgam_;
1263 CCField gn0 = pcGn_[0], gn1 = pcGn_[1], gn2 = pcGn_[2], gph = pcGphi_;
1264 CCConst c = CCConst(cField_), md = CCConst(pcMdot_), T = CCConst(sc.c);
1265 const double eps = pcEffInterfaceEps(), pureEps = pcEffPureEps(), Tsat = pcTsat_, Rint = pcRint_;
1266 const double kapPresc = pcFitKappa_; // WO-P3f
1267 const vof::VofMetric gme = u_.vofMetric(); // Phase 3 (V5.2/V5.3)
1268 const bool curvDist = pcCurvDist_ && pcKappa_.extent(0) == n_; // WO-P3g item 3
1269 CCConst kapF = CCConst(curvDist ? pcKappa_ : pcMdot_);
1270 const bool carry = pcPlaneDir_, quad = pcQuadFit_;
1271 const long syl = sy, szl = sz;
1272 Kokkos::parallel_for(
1273 "peclet::flow::pc_thermal_mask",
1274 MDRange3<CCExec>(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
1275 KOKKOS_LAMBDA(int x, int y, int z) {
1276 const long i = (long)x + (long)y * e.x + (long)z * sz;
1277 const bool on = vof::pcIsInterfacial(c(i), eps);
1278 const double Tgam = vof::pcInterfaceTemperature(Tsat, md(i), Rint);
1279 mk(i) = on ? 1.0 : 0.0;
1280 tg(i) = Tgam;
1281 dv(i) = Tgam;
1282 gn0(i) = 0.0;
1283 gn1(i) = 0.0;
1284 gn2(i) = 0.0;
1285 gph(i) = 0.0;
1286 if (!on)
1287 return;
1288 // WO-P23: the plane geometry the PLANE-ANCHORED (ghost-fluid) Dirichlet rows need — the
1289 // unit normal and the signed centre distance of THIS cell's PLIC plane, rebuilt from the
1290 // colour the energy solve is about to run with (the head-of-step values belong to C^n and
1291 // the interface has moved since).
1292 double st[27];
1293 for (int kk = -1; kk <= 1; ++kk)
1294 for (int jj = -1; jj <= 1; ++jj)
1295 for (int ii = -1; ii <= 1; ++ii)
1296 st[vof::plicSt(ii + 1, jj + 1, kk + 1)] = c(i + ii + jj * sy + kk * sz);
1297 double m[3];
1298 vof::mycNormal(st, m);
1299 // V5.2/V5.3: the stored normal and centre distance are PHYSICAL — the plane-anchored
1300 // rows and the one-sided fit both measure in physical lengths, and the axis pullback
1301 // happens in `pcGfmThetaKAniso`. Identity at the unit metric.
1302 double n[3] = {0.0, 0.0, 0.0};
1303 const double sMet = vof::vofPhysNormal(m, gme, n);
1304 if (!(sMet > 0.0))
1305 return;
1306 const double al = vof::plicAlpha(m[0], m[1], m[2], c(i));
1307 gn0(i) = n[0];
1308 gn1(i) = n[1];
1309 gn2(i) = n[2];
1310 const double phic = vof::pcCentreDistance(m[0], m[1], m[2], al) / sMet;
1311 gph(i) = phic;
1312 if (!carry)
1313 return;
1314 // The value this cell CARRIES until the interface sweeps past it and it becomes pure.
1315 // Nothing reads it across a face (the plane-anchored rows use `tg`), so it is free to be
1316 // the one-sided extrapolation of the profile on the side the cell CENTRE lies on.
1317 const bool gasSide = phic > 0.0;
1318 const double kapFit = curvDist ? kapF(i) : kapPresc; // WO-P3g item 3
1320 for (int dz = -2; dz <= 2; ++dz)
1321 for (int dy = -2; dy <= 2; ++dy)
1322 for (int dx = -2; dx <= 2; ++dx) {
1323 if (dx == 0 && dy == 0 && dz == 0)
1324 continue;
1325 const double di[3] = {(double)dx, (double)dy, (double)dz};
1326 double dp[3];
1327 gme.toPhys(di, dp); // V5.2: PHYSICAL sample offsets
1328 const double w = vof::pcGradWeight(dp[0], dp[1], dp[2], n);
1329 if (!(w > 0.0))
1330 continue;
1331 const long j = i + dx + dy * syl + dz * szl;
1332 const double cj = c(j);
1333 double phi = vof::pcOffsetDistance(phic, n, dp[0], dp[1], dp[2]);
1334 if (kapFit != 0.0) // WO-P3f, bitwise unchanged at 0
1335 phi = vof::pcCurvedDistance(phi, dp[0], dp[1], dp[2], n, kapFit);
1336 if (gasSide) {
1337 if (cj <= pureEps && phi > 0.0)
1338 vof::pcGradAdd(f, w, phi, T(j), Tgam);
1339 } else if (cj >= 1.0 - pureEps && phi < 0.0) {
1340 vof::pcGradAdd(f, w, phi, T(j), Tgam);
1341 }
1342 }
1343 dv(i) = vof::pcCarriedValue(Tgam, quad ? vof::pcGradSolve2(f) : vof::pcGradSolve(f), phic);
1344 });
1345 Kokkos::fence();
1346 // The GFM rows read mask / dval / n / phi of a FACE NEIGHBOUR, i.e. at depth 1: exchange, then
1347 // kill the periodic wrap on non-periodic domain faces (a wrapped "interfacial" ghost would turn
1348 // a domain-boundary face into a phantom Dirichlet).
1349 fillGhosts(sc.dmask);
1350 fillGhosts(sc.dval);
1351 fillGhosts(pcTgam_);
1352 fillGhosts(pcGn_[0]);
1353 fillGhosts(pcGn_[1]);
1354 fillGhosts(pcGn_[2]);
1355 fillGhosts(pcGphi_);
1356 pcZeroDomainGhosts(sc.dmask);
1357 pcZeroDomainGhosts(sc.dval);
1358 pcZeroDomainGhosts(pcTgam_);
1359 pcZeroDomainGhosts(pcGn_[0]);
1360 pcZeroDomainGhosts(pcGn_[1]);
1361 pcZeroDomainGhosts(pcGn_[2]);
1362 pcZeroDomainGhosts(pcGphi_);
1363 // WO-P3f: the enthalpy the overwrite is about to destroy, returned to the phase it came from.
1364 // Runs AFTER the exchanges above (it reads the donor's mask/normal/T_Gamma at depth 1 and the
1365 // donor's own neighbours at depth 2). Inert -- no kernel, no allocation -- when the option
1366 // is off.
1367 if (pcCarryConserve_ && pcCarrySrc_.extent(0) == n_)
1368 pcCarryDeposit(sc);
1369 // WO-P3g item 1: the Dirichlet rows now describe THIS colour field, which is what the
1370 // operator-flux `mdot` needs before it may read them (see the note in `pcBuildInterface`).
1371 pcMaskFresh_ = true;
1372}
1373
1374template <class Grid>
1376 for (int face = 0; face < 6; ++face) {
1377 if (bc_[face] == 0 || !touchesGlobalFace(face))
1378 continue;
1379 const int a = face / 2, side = face % 2;
1380 const int t1 = (a + 1) % 3, t2 = (a + 2) % 3;
1381 const int nt1 = (t1 == 0) ? nx_ : (t1 == 1) ? ny_ : nz_;
1382 const int nt2 = (t2 == 0) ? nx_ : (t2 == 1) ? ny_ : nz_;
1383 const int na = (a == 0) ? nx_ : (a == 1) ? ny_ : nz_;
1384 const long sx = 1, sy = e_.x, sz = (long)e_.x * e_.y;
1385 const long sa = (a == 0) ? sx : (a == 1) ? sy : sz;
1386 const long st1 = (t1 == 0) ? sx : (t1 == 1) ? sy : sz;
1387 const long st2 = (t2 == 0) ? sx : (t2 == 1) ? sy : sz;
1388 const int aInner = (side == 0) ? G : (G + na - 1);
1389 const int dir = (side == 0) ? -1 : +1;
1390 Kokkos::parallel_for(
1391 "peclet::flow::pc_zero_ghosts", MDRange2<CCExec>(CCExec(), {G, G}, {G + nt1, G + nt2}),
1392 KOKKOS_LAMBDA(int j1, int j2) {
1393 const long base = (long)aInner * sa + (long)j1 * st1 + (long)j2 * st2;
1394 for (int L = 1; L <= 2; ++L)
1395 f(base + (long)dir * L * sa) = 0.0;
1396 });
1397 }
1398 Kokkos::fence();
1399}
1400
1401template <class Grid>
1403 const C3 e = e_;
1404 const long sx = 1, sy = e_.x, sz = (long)e_.x * e_.y;
1405 CCConst c = CCConst(cField_), T = CCConst(sc.c), dv = CCConst(sc.dval), mk = CCConst(sc.dmask),
1406 tg = CCConst(pcTgam_), gn0 = CCConst(pcGn_[0]), gn1 = CCConst(pcGn_[1]),
1407 gn2 = CCConst(pcGn_[2]);
1408 CCField srcF = pcCarrySrc_;
1409 const bool useRcp = pcEnergy_;
1410 const double rcpG = pcRcpG_, rcpL = pcRcpL_;
1411 double dep = 0.0, lost = 0.0;
1412 using MD = MDRange3<CCExec>;
1413 Kokkos::parallel_reduce(
1414 "peclet::flow::pc_carry_deposit", MD(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
1415 KOKKOS_LAMBDA(int x, int y, int z, double& acc) {
1416 const long st[3] = {sx, sy, sz};
1417 const long i = (long)x + (long)y * sy + (long)z * sz;
1418 if (mk(i) > 0.5)
1419 return; // masked cells are the identity rows; they receive nothing
1420 double add = 0.0;
1421 for (int d = 0; d < 3; ++d)
1422 for (int sg = -1; sg <= 1; sg += 2) {
1423 const long j = i + (long)sg * st[d];
1424 if (!(mk(j) > 0.5))
1425 continue;
1426 const double rj = useRcp ? vof::pcPhaseMix(rcpG, rcpL, c(j)) : 1.0;
1427 const double dE = rj * (T(j) - dv(j));
1428 if (!(dE != 0.0))
1429 continue;
1430 const double nn[3] = {gn0(j), gn1(j), gn2(j)};
1431 const double devj = T(j) - tg(j);
1432 int chosen[3] = {0, 0, 0};
1433 double w[3] = {0.0, 0.0, 0.0}, wsum = 0.0;
1434 for (int dd = 0; dd < 3; ++dd) {
1435 if (nn[dd] == 0.0)
1436 continue;
1437 const int sL = (nn[dd] > 0.0) ? -1 : +1; // n points into the GAS
1438 const double dl = T(j + (long)sL * st[dd]) - tg(j);
1439 const double dg = T(j - (long)sL * st[dd]) - tg(j);
1440 const int s = (dl * devj >= dg * devj) ? sL : -sL;
1441 if (mk(j + (long)s * st[dd]) > 0.5)
1442 continue; // that neighbour is itself an identity row: it cannot absorb
1443 chosen[dd] = s;
1444 w[dd] = nn[dd] * nn[dd];
1445 wsum += w[dd];
1446 }
1447 if (!(wsum > 0.0))
1448 continue; // no neighbour left in the solve; the residual is booked below
1449 if (chosen[d] != -sg)
1450 continue;
1451 add += (w[d] / wsum) * dE;
1452 }
1453 if (add == 0.0)
1454 return;
1455 const double ri = useRcp ? vof::pcPhaseMix(rcpG, rcpL, c(i)) : 1.0;
1456 srcF(i) += add / ri; // stored as a TEMPERATURE increment for cell i
1457 acc += add;
1458 },
1459 dep);
1460 // The donor total, so `lost` is the EXACT unplaced remainder rather than a census of one
1461 // failure mode: an interfacial cell whose whole `n_d^2` allocation lands on cells that are
1462 // themselves identity rows keeps its discarded enthalpy, and on a curved interface the band is
1463 // thicker than one cell, so that is not a rare event. Report it, never hide it.
1464 Kokkos::parallel_reduce(
1465 "peclet::flow::pc_carry_total", MD(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
1466 KOKKOS_LAMBDA(int x, int y, int z, double& tot) {
1467 const long i = (long)x + (long)y * sy + (long)z * sz;
1468 if (!(mk(i) > 0.5))
1469 return;
1470 const double ri = useRcp ? vof::pcPhaseMix(rcpG, rcpL, c(i)) : 1.0;
1471 tot += ri * (T(i) - dv(i));
1472 },
1473 lost);
1474 Kokkos::fence();
1475 pcCarryDeposited_ = dep;
1476 pcCarryLost_ = lost - dep;
1477}
1478
1479template <class Grid>
1481 const C3 e = e_;
1482 const long sy = e_.x, sz = (long)e_.x * e_.y;
1483 CCField cOld = sc.cOld, srcF = pcCarrySrc_;
1484 CCConst mk = CCConst(sc.dmask);
1485 Kokkos::parallel_for(
1486 "peclet::flow::pc_carry_apply",
1487 MDRange3<CCExec>(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
1488 KOKKOS_LAMBDA(int x, int y, int z) {
1489 const long i = (long)x + (long)y * sy + (long)z * sz;
1490 if (!(mk(i) > 0.5))
1491 cOld(i) += srcF(i);
1492 srcF(i) = 0.0;
1493 });
1494 Kokkos::fence();
1495}
1496
1497template <class Grid>
1499 const C3 e = e_;
1500 const long sy = e_.x, sz = (long)e_.x * e_.y;
1501 CCConst c = CCConst(cField_), T = CCConst(sc.cOld), dvl = CCConst(sc.dval),
1502 mk = CCConst(sc.dmask), cls = CCConst(pcClsPrev_);
1503 const bool useRcp = sc.energy && sc.rcp.extent(0) == n_;
1504 CCConst rcp = useRcp ? CCConst(sc.rcp) : CCConst(sc.c);
1505 const double Tsat = pcTsat_;
1506 double hOpen = 0, hLiq = 0, hMask = 0, dEo = 0, dEoN = 0, eEnt = 0, eLev = 0;
1507 long nEL = 0, nEG = 0, nLL = 0, nLG = 0, nM = 0;
1508 using MD = MDRange3<CCExec>;
1509 Kokkos::parallel_reduce(
1510 "peclet::flow::pc_budget_pre", MD(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
1511 KOKKOS_LAMBDA(int x, int y, int z, double& a1, double& a2, double& a3, double& a4, double& a5,
1512 double& a6, double& a7) {
1513 const long i = (long)x + (long)y * sy + (long)z * sz;
1514 const double r = useRcp ? rcp(i) : 1.0;
1515 const double h = r * (T(i) - Tsat);
1516 const bool msk = mk(i) > 0.5;
1517 if (msk) {
1518 a3 += h;
1519 a4 += r * (dvl(i) - T(i));
1520 } else {
1521 a1 += h;
1522 if (c(i) >= 0.5)
1523 a2 += h;
1524 }
1525 const double cp = cls(i);
1526 if (!(cp >= 0.0))
1527 return; // the first instrumented step has no previous class
1528 const bool wasMsk = (cp > 0.5 && cp < 1.5);
1529 if (msk && !wasMsk) {
1530 a6 += h;
1531 a5 += r * (dvl(i) - T(i));
1532 } else if (!msk && wasMsk) {
1533 a7 += h;
1534 }
1535 },
1536 hOpen, hLiq, hMask, dEo, dEoN, eEnt, eLev);
1537 Kokkos::parallel_reduce(
1538 "peclet::flow::pc_budget_counts", MD(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
1539 KOKKOS_LAMBDA(int x, int y, int z, long& c1, long& c2, long& c3, long& c4, long& c5) {
1540 const long i = (long)x + (long)y * sy + (long)z * sz;
1541 const bool msk = mk(i) > 0.5;
1542 if (msk)
1543 c5 += 1;
1544 const double cp = cls(i);
1545 if (!(cp >= 0.0))
1546 return;
1547 const bool wasMsk = (cp > 0.5 && cp < 1.5);
1548 if (msk && !wasMsk) {
1549 if (cp > 1.5)
1550 c1 += 1;
1551 else
1552 c2 += 1;
1553 } else if (!msk && wasMsk) {
1554 if (c(i) >= 0.5)
1555 c3 += 1;
1556 else
1557 c4 += 1;
1558 }
1559 },
1560 nEL, nEG, nLL, nLG, nM);
1561 Kokkos::fence();
1562 pcBudget_.hOpen = hOpen;
1563 pcBudget_.hLiquid = hLiq;
1564 pcBudget_.hMasked = hMask;
1565 pcBudget_.dEoverwrite = dEo;
1566 pcBudget_.dEoverwriteNew = dEoN;
1567 pcBudget_.eEnter = eEnt;
1568 pcBudget_.eLeave = eLev;
1569 pcBudget_.nEnterLiquid = nEL;
1570 pcBudget_.nEnterGas = nEG;
1571 pcBudget_.nLeaveLiquid = nLL;
1572 pcBudget_.nLeaveGas = nLG;
1573 pcBudget_.nMasked = nM;
1574}
1575
1576template <class Grid>
1578 const C3 e = e_;
1579 const long sx = 1, sy = e_.x, sz = (long)e_.x * e_.y;
1580 CCConst c = CCConst(cField_), T = CCConst(sc.c), mk = CCConst(sc.dmask), tg = CCConst(pcTgam_),
1581 gnx = CCConst(pcGn_[0]), gny = CCConst(pcGn_[1]), gnz = CCConst(pcGn_[2]),
1582 gph = CCConst(pcGphi_);
1583 CCConst ox = CCConst(ox_), oy = CCConst(oy_), oz = CCConst(oz_);
1584 const bool useRcp = sc.energy && sc.rcp.extent(0) == n_;
1585 const bool useK = sc.energy && sc.kcell.extent(0) == n_;
1586 CCConst rcp = useRcp ? CCConst(sc.rcp) : CCConst(sc.c);
1587 CCConst kc = useK ? CCConst(sc.kcell) : CCConst(sc.c);
1588 const double Tsat = pcTsat_, Dc = sc.D, thMin = pcGfmThMin_, thMax = pcGfmThMax_;
1589 const bool gfm = pcPlaneDir_ && pcGphi_.extent(0) == n_;
1590 // WO-P3g: the instrument has to mirror the ROW, or it measures a scheme nobody ran.
1591 const int gfmOrder = pcGfmOrder_;
1592 const bool curvDist = pcCurvDist_ && pcKappa_.extent(0) == n_;
1593 CCConst kapF = CCConst(curvDist ? pcKappa_ : pcGphi_);
1594 const vof::VofMetric gme = u_.vofMetric(); // Phase 3 (V5.3)
1595 CCField clsOut = pcClsPrev_;
1596 double hOpen = 0, q = 0, qb = 0;
1597 using MD = MDRange3<CCExec>;
1598 Kokkos::parallel_reduce(
1599 "peclet::flow::pc_budget_post", MD(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
1600 KOKKOS_LAMBDA(int x, int y, int z, double& a1, double& a2, double& a3) {
1601 const long i = (long)x + (long)y * sy + (long)z * sz;
1602 const double r = useRcp ? rcp(i) : 1.0;
1603 const bool msk = mk(i) > 0.5;
1604 clsOut(i) = msk ? 1.0 : (c(i) >= 0.5 ? 2.0 : 0.0);
1605 if (msk)
1606 return;
1607 a1 += r * (T(i) - Tsat);
1608 if (!gfm)
1609 return;
1610 const long st[3] = {sx, sy, sz};
1611 const double kd = useK ? kc(i) : Dc;
1612 for (int d = 0; d < 3; ++d)
1613 for (int sgn = -1; sgn <= 1; sgn += 2) {
1614 const long j = i + (long)sgn * st[d];
1615 if (!(mk(j) > 0.5))
1616 continue;
1617 const double of = (d == 0) ? ((sgn < 0) ? ox(i) : ox(i + sx))
1618 : (d == 1) ? ((sgn < 0) ? oy(i) : oy(i + sy))
1619 : ((sgn < 0) ? oz(i) : oz(i + sz));
1620 const double nvec[3] = {gnx(j), gny(j), gnz(j)};
1621 const double th = vof::pcGfmThetaKAniso(gph(j), nvec, d, (double)sgn,
1622 curvDist ? kapF(j) : 0.0, thMin, thMax, gme);
1623 const long jb = i - (long)sgn * st[d];
1624 const double ofB = (d == 0) ? ((sgn < 0) ? ox(i + sx) : ox(i))
1625 : (d == 1) ? ((sgn < 0) ? oy(i + sy) : oy(i))
1626 : ((sgn < 0) ? oz(i + sz) : oz(i));
1627 const bool behind = !(mk(jb) > 0.5) && ofB > 0.0;
1628 const vof::PcGfmRow row = vof::pcGfmRow(th, behind, gfmOrder);
1629 a2 += kd * of * row.aGamma * (tg(j) - T(i));
1630 if (behind && row.aBehind != 1.0) {
1631 const double kb = useK ? 0.5 * (kc(i) + kc(jb)) : Dc;
1632 a3 += (row.aBehind - 1.0) * kb * ofB * (T(jb) - T(i));
1633 }
1634 }
1635 },
1636 hOpen, q, qb);
1637 Kokkos::fence();
1638 pcBudget_.hOpenNew = hOpen;
1639 pcBudget_.qGfm = q;
1640 pcBudget_.qBehind = qb;
1641 pcBudget_.calls += 1;
1642}
1643
1644template <class Grid>
1646 if (!pcEnabled_ && !pcHasUser_)
1647 return;
1648 const C3 e = e_;
1649 const bool hasPc = pcEnabled_, hasUser = pcHasUser_;
1650 CCConst sp = hasPc ? CCConst(pcSrc_) : CCConst(div);
1651 CCConst su = hasUser ? CCConst(pcUser_) : CCConst(div);
1652 Kokkos::parallel_for(
1653 "peclet::flow::pc_div_source",
1654 MDRange3<CCExec>(CCExec(), {G, G, G}, {e.x - G, e.y - G, e.z - G}),
1655 KOKKOS_LAMBDA(int x, int y, int z) {
1656 const long i = (long)x + (long)y * e.x + (long)z * (long)e.x * e.y;
1657 double s = 0.0;
1658 if (hasPc)
1659 s += sp(i);
1660 if (hasUser)
1661 s += su(i);
1662 div(i) -= s;
1663 });
1664}
1665
1666template <class Grid>
1668 return Kokkos::fmax(pcInterfaceEps_, vofAdv_.wispEps);
1669}
1670
1671template <class Grid>
1673 return Kokkos::fmax(pcPureEps_, vofAdv_.wispEps);
1674}
1675
1676template <class Grid>
1677void Solver<Grid>::requirePhaseChange(const char* who) const {
1678 if (!pcEnabled_)
1679 throw std::runtime_error(std::string(who) +
1680 ": phase change is not enabled (call "
1681 "enable_phase_change first)");
1682}
1683
1684template <class Grid>
1685double Solver<Grid>::phaseTick() {
1686 // S4 (doc/vof_gpu_coarse_tail_design.md §5.5): the step's device work is all on the default
1687 // instance, so its fence times it; a global fence would also wait on the side factor (X).
1688 CCExec().fence("peclet::flow::phase_tick");
1689 return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count();
1690}
1691
1692} // namespace peclet::flow
1693
1694#endif // PECLET_FLOW_FLOW_IBM_PHASE_CHANGE_HPP
void vofExchangeScalar(CCField f)
The colour block's ghost policy WITHOUT the colour-specific rules (no solid-band fill,...
void setPhaseChangeGfmOrder(int order)
WO-P3g item 2: the order of the one-sided (ghost-fluid) Dirichlet row.
void setPhaseChangeEnergyMuscl(bool on)
MinMod-limited donor reconstruction of the face temperature in the consistent energy flux (vof/energy...
static constexpr int kVofG
void pcZeroDomainGhosts(CCField f)
Zero the two ghost layers on every NON-periodic domain face this rank owns.
void pcCarryDeposit(ScalarField &sc)
WO-P3f — the enthalpy the per-cell Dirichlet overwrite destroys, returned to the phase it came from.
void scatterInner(CCField fld, const std::vector< double > &in)
void phaseChangeStep()
The in-step driver: everything applyPhaseChange does, at the head of step().
ScalarField & scalarField(const std::string &name)
PhaseChangeDiagnostics phaseChangeDiagnostics()
void fillGhosts(CCField f)
void setMassFluxUniform(double v)
Prescribe a UNIFORM mass flux (P0), in mass per area per time.
void setPhaseChangeFitCurvature(double kappa)
WO-P3f INSTRUMENT: prescribe the interface curvature kappa = div(n) the one-sided fits use to correct...
void fillPropGhosts(CCField f, bool edges=false)
void pcScatterSource()
(2) Deposit each interfacial cell's source into its chosen pure-gas cell, as a GATHER (each receiving...
void pcApplyDivergenceSource(CCField div)
Subtract the phase-change (and any prescribed) divergence source from div_ so the deflated pressure s...
void setPhaseChangeMdotOperator(bool on)
WO-P3g item 1: take mdot from the energy operator's own interfacial flux instead of the one-sided lea...
double phaseChangeCarryDeposited() const
The carry ledger, physical: an energy with the consistent transport on, and a temperature times a vol...
CCField addField(const std::string &name)
void pcRegress(double dt)
(3) The regression: two Jacobi passes over the exchanged per-cell data, so the clip deficit is redist...
vof::VofInterfaceArea::Stats pcAreaCascadeCompute()
Run the cascade area driver on the (already bridged) g = 3 colour block.
SField pcAreaCascadeField() const
The area field the last pcAreaCascadeCompute filled (either driver).
double pcEffPureEps() const
…and the mirror statement: a cell the ADVECTOR treats as a pure phase is a pure phase here too,...
void pcUpdateEnergyProps()
Refresh k(C) and (rho c_p)(C) on the G=2 block from the current colour.
void requireScalesPinned(const char *who, bool needRho) const
Under an armed extent, refuse a call that writes a dimensional value into a FIELD before the referenc...
double phaseChangeQOperator() const
The operator's interfacial heat (power), physical.
void copyInner(CCField dst, C3 de, int dg, CCConst src, C3 se, int sg)
void setDivergenceSink(const std::vector< double > &w)
WO-P23: an AUTO-BALANCED sink region for the phase-change divergence source.
void setPhaseChangeThermal(const std::string &tname, double Tsat, double kg, double kl, double Rint)
P1: compute mdot each step from the registered scalar tname by the one-sided pure-cell weighted least...
void pcBudgetPost(ScalarField &sc)
WO-P3f, part 2: the state AFTER the energy solve, the heat the plane-anchored rows actually delivered...
void setPhaseChangePlaneDirichlet(bool on)
The PLANE-ANCHORED (ghost-fluid) Dirichlet condition, ON by default.
double vofInterfaceArea()
The summed interfacial area over the inner region, PHYSICAL (globally reduced under MPI) — the E7 gal...
void setPhaseChangeQuadraticFit(bool on)
The QUADRATIC one-sided gradient fit (T - T_G = G phi + Q phi^2) instead of the linear one.
double pcBandDivergence()
The BAND-EXTENDED LIQUID VELOCITY of VOF_PLAN §9 item 3, as a MEASUREMENT rather than a switch.
void pcBudgetPre(ScalarField &sc)
WO-P3f, the energy-budget instrument, part 1: the state BEFORE the energy solve, and the class-change...
void applyPhaseChange(double dtPhysArg)
Kinematic entry point (the P0a/P1 driver): build mdot/A_G/n from the current colour and temperature,...
void setPhaseChangeCarryConserve(bool on)
WO-P3f: make the per-cell Dirichlet overwrite ENTHALPY-CONSERVING.
static constexpr int G
Definition flow_ibm.hpp:82
void pcBuildInterface()
(1) The interface build: for every inner interfacial cell reconstruct the PLIC plane from the canonic...
double pcLedgerEnergyToPhys() const
The factor from an internal phase-change ledger entry (a per-cell sum of rcp' dT) to the caller's uni...
void enablePhaseChange(double rhoG, double rhoL, double hlv)
Turn on phase change.
bool hasScalar(const std::string &name) const
void pcBuildInDomain()
WO-P3g — 1 on every cell that CARRIES A ROW in the energy solve, 0 otherwise.
void pcCarryApply(ScalarField &sc)
WO-P3f: consume pcCarrySrc_ into the energy solve's time base.
void pcUpdateCurvature()
The per-cell Dirichlet mask of the energy scalar: T = T_sat + mdot R_int in every interfacial cell,...
double pcEffInterfaceEps() const
The colour tolerance that decides which cells are INTERFACIAL, and it has to be at least the colour a...
void setVofWispEps(double eps)
WO-R2 item 4 — the wisp threshold on the advector's mixed-cell predicate and on the interface Courant...
void setPhaseChangeCurvatureDistance(bool on)
WO-P3g item 3: measure the GFM row's theta and the one-sided fits' sample distances to the CURVED int...
void setPhaseChangeEnergyOrder(int order)
WO-P3g — the SECOND-ORDER interfacial energy operator, as one package.
void requirePhaseChange(const char *who) const
void setMassFlux(const std::vector< double > &v)
Prescribe a per-cell mass flux (P0), x-fastest over the inner region; units and call-order rule as se...
void setPhaseChangeEnergy(double rcpGas, double rcpLiquid)
Turn on the CONSISTENT energy transport (VOF_PLAN §9 item 6) for the scalar set_phase_change_thermal ...
void scalarDirichletMask(const std::string &name)
Allocate (idempotently) the per-cell Dirichlet mask + value of a registered scalar.
void pcRefreshUnits()
Re-derive the INTERNAL phase-change constants (pcRhoG_ ... pcRcpL_) from the caller's physical ones (...
void setPhaseChangeBudget(bool on)
WO-P3f: turn the ENERGY BUDGET instrument on.
PhaseChangeBudget phaseChangeBudgetValues() const
The budget, physical: energies and powers with the consistent transport on, temperature x volume (and...
void setDivergenceSource(const std::vector< double > &v)
A PRESCRIBED extra divergence source (1/time, physical; set_dt must precede it under an extent,...
void setPhaseChangeDepositFallback(bool on)
WO-P3f open item 6 / WO-P3g — the divergence source's 5^3 fallback target, as a setter (it was only r...
bool isCutcellScalar(const std::string &name) const
void setPhaseChangeArea(int mode)
WO-P3c — which geometry the interfacial AREA comes from.
double pcGradSolve2(const PcGradFit &f)
The QUADRATIC one-sided fit (WO-P23): T - T_G = G phi + Q phi^2, returning G.
double pcMassFlux(double kg, double gradG, double kl, double gradL, double hlv)
mdot = (k_g dT_g/dn - k_l dT_l/dn)/h_lv with n the PLIC normal (liquid -> gas).
void pcGradAdd(PcGradFit &f, double w, double phi, double T, double Tg)
Add one pure-phase sample at normal distance phi with temperature T.
double pcCentreDistance(double mx, double my, double mz, double alpha)
Signed distance (in cells) from the CELL CENTRE to the PLIC plane, measured along n and POSITIVE ON T...
double pcRegressVolume(double mdot, double area, double dt, double rhoL)
Liquid volume (fraction of a cell) removed from an interfacial cell in one step: dV = mdot A_G dt / r...
bool pcPushWeights(const double n[3], double sgn, const bool avail[3], int step[3], double w[3])
The clip-and-redistribute allocation: which face neighbours a clipped cell's residue is pushed into,...
double pcDivSource(double mdot, double area, double rhoG, double rhoL)
The volumetric divergence source of one interfacial cell (1/s, cell volume = 1): S = mdot A_G (1/rho_...
PcGfmRow pcGfmRow(double theta, bool behind, int order)
double pcCurvedDistance(double phi, double dx, double dy, double dz, const double n[3], double kappa)
WO-P3f — the sample's distance to the CURVED interface, not to the tangent plane.
double pcCarriedValue(double Tg, double grad, double phic)
The plane-anchored (ghost-fluid) interfacial Dirichlet condition (WO-P23, the first item).
bool pcIsInterfacial(double c, double eps)
Is this a cell the regression acts on?
double pcInterfaceTemperature(double Tsat, double mdot, double Rint)
Interfacial temperature under the Schrage-derived interfacial heat-transfer resistance (Bureš & Sato)...
double pcOffsetDistance(double phic, const double n[3], double dx, double dy, double dz)
Signed distance from the plane of the neighbour at INTEGER cell offset d, given the centre distance p...
double pcGfmThetaKAniso(double phiC, const double n[3], int d, double s, double kappa, double thetaMin, double thetaMax, const VofMetric &g)
The same on an ANISOTROPIC cell (Phase 3, flow/doc/anisotropic_vof.md §7, V5.3).
double pcOperatorMassFlux(double q, double coefSum, double area, double hlv, double Rint)
WO-P3g item 1 — mdot from the ENERGY OPERATOR's own interfacial flux.
double pcPhaseMix(double vg, double vl, double c)
Linear-in-C phase property (k, rho c_p), written so that C = 0 and C = 1 return the endpoint EXACTLY ...
@ kAreaMcColour
the C = 1/2 level set, whole triangles to the centroid's cell
@ kAreaPlic
rung P0/P1: plicArea on the MYC normal. The old numbers.
double plicAreaMetric(double mx, double my, double mz, double alpha, const VofMetric &g)
The PHYSICAL area of the PLIC polygon of an ANISOTROPIC cell, in hRef^2 (flow/doc/anisotropic_vof....
double pcGradSolve(const PcGradFit &f)
The fitted normal derivative dT/dn (0 if the side carried no usable sample).
double pcGradWeight(double dx, double dy, double dz, const double n[3])
Malan's collinearity weight for a sample at cell offset d: w = xi / |d| with xi = (d....
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).
Definition policy.hpp:36
Kokkos::View< double *, SMem > SField
Kokkos::View< const double *, CCMem > CCConst
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).
Definition policy.hpp:31
WO-P3f — the ENERGY BUDGET of the phase-change energy solve.
WO-P3g item 2 — the Gibou–Fedkiw second-order one-sided (ghost-fluid) row.
Accumulator for dT/dn fitted through the interface value: T - T_G = G * phi, so G = sum_j w_j phi_j (...
Per-call census over the inner region (LOCAL; a distributed caller sums it).
static constexpr int G
static constexpr double U