flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
block_container.hpp
Go to the documentation of this file.
1
57#ifndef PECLET_FLOW_VOF_BLOCK_CONTAINER_HPP
58#define PECLET_FLOW_VOF_BLOCK_CONTAINER_HPP
59
60#include <algorithm>
61#include <array>
62#include <cmath>
63#include <cstdio>
64#include <Kokkos_Core.hpp>
65#include <map>
66#include <memory>
67#include <stdexcept>
68#include <string>
69#include <vector>
70
71#include "mac_stencils.hpp" // peclet::flow::SExec, SField, I3, L3
72#include "peclet/core/decomp/block_decomposer.hpp"
73#include "vof/advect_wy.hpp"
74#include "vof/block_batch.hpp"
75#include "vof/colour_field.hpp"
78#include "policy.hpp"
79
80namespace peclet::flow::vof {
81
82// ---------------------------------------------------------------------------------------------
83// index boxes
84// ---------------------------------------------------------------------------------------------
85
88struct VofBox {
89 int lo[3]{0, 0, 0};
90 int hi[3]{0, 0, 0};
91
92 int n(int d) const { return hi[d] - lo[d]; }
93 long cells() const { return static_cast<long>(n(0)) * n(1) * n(2); }
94 bool empty() const { return n(0) <= 0 || n(1) <= 0 || n(2) <= 0; }
95 I3 size() const { return I3{n(0), n(1), n(2)}; }
96 I3 origin() const { return I3{lo[0], lo[1], lo[2]}; }
97
98 bool contains(const VofBox& o) const {
99 for (int d = 0; d < 3; ++d)
100 if (o.lo[d] < lo[d] || o.hi[d] > hi[d])
101 return false;
102 return true;
103 }
104 bool operator==(const VofBox& o) const {
105 for (int d = 0; d < 3; ++d)
106 if (lo[d] != o.lo[d] || hi[d] != o.hi[d])
107 return false;
108 return true;
109 }
110 bool operator!=(const VofBox& o) const { return !(*this == o); }
111
112 static VofBox grown(const VofBox& b, int m) {
113 VofBox r = b;
114 for (int d = 0; d < 3; ++d) {
115 r.lo[d] -= m;
116 r.hi[d] += m;
117 }
118 return r;
119 }
120
121 static VofBox intersect(const VofBox& a, const VofBox& b) {
122 VofBox r;
123 for (int d = 0; d < 3; ++d) {
124 r.lo[d] = a.lo[d] > b.lo[d] ? a.lo[d] : b.lo[d];
125 r.hi[d] = a.hi[d] < b.hi[d] ? a.hi[d] : b.hi[d];
126 }
127 return r;
128 }
129};
130
138struct VofRun {
139 int g0 = 0;
140 int len = 0;
141 int loc0 = 0;
142};
143
150inline int vofAxisRuns(int a, int b, int gs, bool periodic, VofRun out[5], int outside = 0) {
151 int n = 0;
152 if (b <= a)
153 return 0;
154 if (!periodic) {
155 const int s = a < -outside ? -outside : a;
156 const int e = b > gs + outside ? gs + outside : b;
157 if (e > s)
158 out[n++] = VofRun{s, e - s, s - a};
159 return n;
160 }
161 if (b - a > 3 * gs) // guarded by vofClampBox(); a block box is at most the whole axis
162 throw std::runtime_error("peclet::flow::vof: block range exceeds three periodic images");
163 int k = a;
164 while (k < b && n < 5) {
165 const int w = ((k % gs) + gs) % gs;
166 int len = gs - w;
167 if (len > b - k)
168 len = b - k;
169 out[n++] = VofRun{w, len, k - a};
170 k += len;
171 }
172 return n;
173}
174
177inline VofBox vofClampBox(VofBox b, I3 gs, const std::array<bool, 3>& per) {
178 const int g[3] = {gs.x, gs.y, gs.z};
179 for (int d = 0; d < 3; ++d) {
180 if (per[d]) {
181 if (b.n(d) > g[d]) {
182 b.lo[d] = 0;
183 b.hi[d] = g[d];
184 }
185 } else {
186 if (b.lo[d] < 0)
187 b.lo[d] = 0;
188 if (b.hi[d] > g[d])
189 b.hi[d] = g[d];
190 if (b.hi[d] <= b.lo[d]) {
191 b.lo[d] = 0;
192 b.hi[d] = g[d] < 1 ? 1 : g[d];
193 }
194 }
195 }
196 return b;
197}
198
199// ---------------------------------------------------------------------------------------------
200// master assignment (rung W1 item a)
201// ---------------------------------------------------------------------------------------------
202
207enum class VofMasterAssign {
209 Lpt = 1,
211};
212
217inline void vofAssignLpt(const std::vector<long>& w, int size, std::vector<int>& master) {
218 const std::size_t n = w.size();
219 master.assign(n, 0);
220 if (n == 0 || size <= 1)
221 return;
222 std::vector<std::size_t> ord(n);
223 for (std::size_t i = 0; i < n; ++i)
224 ord[i] = i;
225 // stable, and the comparator is a strict weak ordering on the weight alone, so equal weights
226 // keep block-id order -> with equal blocks LPT reproduces the round robin exactly.
227 std::stable_sort(ord.begin(), ord.end(),
228 [&](std::size_t a, std::size_t b) { return w[a] > w[b]; });
229 std::vector<long> load(static_cast<std::size_t>(size), 0);
230 for (std::size_t k = 0; k < n; ++k) {
231 int best = 0;
232 for (int r = 1; r < size; ++r)
233 if (load[static_cast<std::size_t>(r)] < load[static_cast<std::size_t>(best)])
234 best = r;
235 master[ord[k]] = best;
236 load[static_cast<std::size_t>(best)] += w[ord[k]];
237 }
238}
239
246inline void vofAssignOrb(const std::vector<long>& w, int size, std::vector<int>& master) {
247 const std::size_t n = w.size();
248 master.assign(n, 0);
249 if (n == 0 || size <= 1)
250 return;
251 std::vector<peclet::core::Real> wr(n);
252 for (std::size_t i = 0; i < n; ++i)
253 wr[i] = static_cast<peclet::core::Real>(w[i]);
254 peclet::core::decomp::BlockDecomposer<1> dec(
255 static_cast<std::size_t>(size),
256 peclet::core::IVec<1>{static_cast<peclet::core::Index>(n)}, wr);
257 for (int r = 0; r < size; ++r) {
258 const auto b = dec.block(static_cast<std::size_t>(r));
259 for (auto i = b.origin[0]; i < b.origin[0] + b.size[0]; ++i)
260 master[static_cast<std::size_t>(i)] = r;
261 }
262}
263
264// ---------------------------------------------------------------------------------------------
265// one block
266// ---------------------------------------------------------------------------------------------
267
270 long id = 0;
271 int master = 0;
272 int lo[3]{0, 0, 0};
273 int hi[3]{0, 0, 0};
274 double volume = 0.0;
275 double centroid[3]{0, 0, 0};
276 double velocity[3]{0, 0, 0};
278 double moment[6]{0, 0, 0, 0, 0, 0};
279 bool recentred = false;
280 long cells = 0;
284 double discarded = 0.0;
290 double area = 0.0;
298 long debrisCells = 0;
299 double debrisVolume = 0.0;
300 double debrisReturned = 0.0;
301 double debrisLost = 0.0;
305 long fullAxis = 0;
308 double residueReturned = 0.0;
309};
310
313class VofBlock {
314 public:
315 long id = 0;
316 int master = 0;
321
322 bool mine() const { return mine_; }
323 WyAdvector& advector() { return adv_; }
324 const WyAdvector& advector() const { return adv_; }
325 VofBox extended(int ghost) const { return VofBox::grown(box, ghost); }
326 const VofBlockStats& stats() const { return st_; }
331 VofCurvature& curvature() { return curv_; }
332 const VofCurvature& curvature() const { return curv_; }
333 SField csfForce(int c) const { return f_[c]; }
338 static constexpr int kAuxLen = 10;
339 void serializeAux(double out[kAuxLen]) const {
340 for (int d = 0; d < 3; ++d)
341 out[d] = prevCentroid_[d];
342 out[3] = hasPrev_ ? 1.0 : 0.0;
343 out[4] = st_.debrisReturned;
344 out[5] = st_.debrisLost;
345 out[6] = static_cast<double>(st_.debrisUnresolved); // a cell count: exact in a double
346 out[7] = st_.discarded; // §12.2: the re-centring ledger migrates too
347 out[8] = static_cast<double>(st_.fullAxis);
348 out[9] = st_.residueReturned;
349 }
350 void deserializeAux(const double in[kAuxLen]) {
351 for (int d = 0; d < 3; ++d)
352 prevCentroid_[d] = in[d];
353 hasPrev_ = (in[3] != 0.0);
354 st_.debrisReturned = in[4];
355 st_.debrisLost = in[5];
356 st_.debrisUnresolved = static_cast<long>(in[6]);
357 st_.discarded = in[7];
358 st_.fullAxis = static_cast<long>(in[8]);
359 st_.residueReturned = in[9];
360 }
361
362 friend class VofBlockSet;
363
364 private:
365 bool mine_ = false;
366 bool allocated_ = false;
367 WyAdvector adv_;
368 VofCurvature curv_;
369 SField f_[3];
370 VofBlockStats st_;
371 double prevCentroid_[3]{0, 0, 0};
372 bool hasPrev_ = false;
373};
374
375// ---------------------------------------------------------------------------------------------
376// the exchange interface (implemented in vof/block_exchange.hpp)
377// ---------------------------------------------------------------------------------------------
378
379class VofBlockSet;
380
384 virtual ~VofBlockExchangeBase() = default;
393 virtual void gatherFaceVel(std::vector<VofBlock>& blocks, int ghost) = 0;
396 virtual void scatterColourMax(std::vector<VofBlock>& blocks, SField cLocal) = 0;
398 virtual void syncTable(std::vector<VofBlock>& blocks) = 0;
402 virtual void gatherColour(std::vector<VofBlock>& blocks, SField cLocal) = 0;
406 virtual void scatterForceSum(std::vector<VofBlock>& blocks, SField fx, SField fy, SField fz) = 0;
410 virtual void scatterColourSum(std::vector<VofBlock>&, SField) {
411 throw std::runtime_error("peclet::flow::vof: this block exchange has no colour SUM scatter");
412 }
413
416 virtual void migrateColour(std::vector<VofBlock>& blocks, const std::vector<int>& oldMaster) = 0;
418 virtual long gatherBytes() const { return 0; }
419 virtual long scatterBytes() const { return 0; }
420};
421
422// ---------------------------------------------------------------------------------------------
423// the block set
424// ---------------------------------------------------------------------------------------------
425
429 public:
435 void init(I3 gs, std::array<bool, 3> per, int rank, int size, double h = 1.0) {
436 h_ = h;
437 gs_ = gs;
438 per_ = per;
439 rank_ = rank;
440 size_ = size < 1 ? 1 : size;
441 blocks_.clear();
442 statsPend_.clear();
443 step_ = 0;
444 }
445
449 void setMetric(const VofMetric& g) {
450 metric_ = g;
451 curvProto.metric = g;
452 for (auto& b : blocks_) {
453 b.adv_.metric = g;
454 b.curv_.metric = g;
455 }
456 }
457 const VofMetric& metric() const { return metric_; }
458
459 void setExchange(std::shared_ptr<VofBlockExchangeBase> x) { exch_ = std::move(x); }
460 VofBlockExchangeBase* exchange() const { return exch_.get(); }
461
462 int margin() const { return margin_; }
463 int ghost() const { return ghost_; }
467 int recentrePad = 2;
474 double cflLimit = 0.25;
480 bool batched = true;
493 bool usePool = true;
494 int poolCapacity = 8;
497 bool csfEnabled = false;
498 double sigma = 0.0;
512 VofCurvature c;
513 c.kappaMax = -1.0;
514 return c;
515 }
516
522 double bubbleEps = 1e-12;
528 double wispEps = 0.0;
530 void setWispEps(double eps) {
531 wispEps = eps;
532 curvProto.pureEps = eps; // the cascade's purity test follows the advector (review finding 1)
533 for (auto& b : blocks_)
534 if (b.allocated_) {
535 b.adv_.wispEps = eps;
536 b.curv_.pureEps = eps;
537 }
538 }
539
546 double areaEps = 1e-8;
549 bool allowShrink = true;
555 static constexpr double kDebrisMaxC = 0.5;
558 bool debrisCensus = true;
561 bool debrisRemove = false;
564 bool timingOn = false;
565 double debrisSeconds = 0.0;
566 long debrisCalls = 0;
567 static double tick_() {
568 Kokkos::fence();
570 }
571
573 static constexpr double kDebrisMinAttached = 1.0;
574 double cellSize() const { return h_; }
575
576 std::size_t count() const { return blocks_.size(); }
579 std::vector<VofBlock>& blocks() {
580 flushStats();
581 return blocks_;
582 }
583 const std::vector<VofBlock>& blocks() const { return blocks_; }
584 long step() const { return step_; }
585 void setStep(long s) { step_ = s; }
586
590 int masterOf(long id) const { return static_cast<int>(id % size_); }
591
598 void seedSphere(double cx, double cy, double cz, double r, int subLevels = 4, int extraPad = 0) {
599 flushStats(); // C2: deferred statistics are indexed by block; apply them before the table grows
600 VofBox bb;
601 const double c[3] = {cx, cy, cz};
602 for (int d = 0; d < 3; ++d) {
603 bb.lo[d] = static_cast<int>(std::floor((c[d] - r) / h_)) - 1;
604 bb.hi[d] = static_cast<int>(std::ceil((c[d] + r) / h_)) + 1;
605 }
606 VofBlock b;
607 b.id = static_cast<long>(blocks_.size());
608 b.master = masterOf(b.id);
609 b.box = vofClampBox(VofBox::grown(bb, margin_ + extraPad), gs_, per_);
610 b.mine_ = (b.master == rank_);
611 blocks_.push_back(std::move(b));
612 const std::size_t idx = blocks_.size() - 1;
613 // Re-install every earlier block's hook: `push_back` may have reallocated the table, and the
614 // hooks capture `this` + an index (so they survive it), but the advectors themselves moved.
615 for (std::size_t k = 0; k + 1 < blocks_.size(); ++k)
616 if (blocks_[k].mine_ && blocks_[k].allocated_)
617 installHook(k);
618 VofBlock& nb = blocks_[idx];
619 if (nb.mine_) {
620 allocate(idx);
621 const I3 e = nb.adv_.extent();
622 const int g = ghost_;
623 const I3 o = nb.box.origin();
624 const double hh = h_;
625 SField cf = nb.adv_.colour();
626 Kokkos::parallel_for(
627 "vof::block::seed_sphere",
628 MDRange3<SExec>(SExec(), {0, 0, 0}, {e.x, e.y, e.z}),
629 KOKKOS_LAMBDA(int x, int y, int z) {
630 const double gx = (x - g + o.x) * hh, gy = (y - g + o.y) * hh, gz = (z - g + o.z) * hh;
631 cf(L3(x, y, z, e)) = sphereCellFraction(cx, cy, cz, r, gx, gy, gz, hh, subLevels);
632 });
633 Kokkos::fence();
634 fillBlockGhosts(blocks_[idx]);
635 measure(blocks_[idx], 0.0);
636 }
637 }
638
644 void advect(double dt, SField cLocal) {
645 if (!exch_)
646 throw std::runtime_error("peclet::flow::vof::VofBlockSet: no exchange installed");
647 exch_->gatherFaceVel(blocks_, ghost_);
648 std::vector<VofBlock*> deb;
649 const bool batch = batchEligible();
650 if (batch)
651 advectBatched(dt); // read #1 also applies the previous step's deferred statistics (C2)
652 else
653 flushStats();
654 for (auto& b : blocks_) {
655 if (!b.mine_)
656 continue;
657 if (!batch)
658 b.adv_.advect(dt, step_);
659 b.st_.recentred = false;
660 // Before the re-centring, so the bubble box a re-centre measures is the one the census saw.
661 // §13: also whenever the advectors run a wisp guard -- the sub-wispEps residue must be
662 // returned on the kinematic path too (the leak is not a CSF matter).
663 if ((csfEnabled && debrisCensus) || wispEps > 0.0)
664 deb.push_back(&b);
665 }
666 // After every block has advected: each block's pass touches only its own colour, so running
667 // them together (their sums in one launch) is the per-block sequence reordered, bit for bit.
668 if (!deb.empty())
669 debrisPassBatch(deb, /*deferLedger=*/batch);
670 if (batch) {
671 // §5.9 stage 5: every bubble box in one launch, then read #2 -- the boxes, the debris sums
672 // and the WO-R ledger in ONE device->host copy; the recentre decisions exactly as before.
673 recentreBatched(!deb.empty());
674 } else {
675 for (std::size_t k = 0; k < blocks_.size(); ++k)
676 if (blocks_[k].mine_)
677 recentre(k);
678 }
679 exch_->syncTable(blocks_);
680 for (auto& b : blocks_)
681 b.mine_ = (b.master == rank_);
682 // Rung W1: re-balance the master assignment on the CURRENT boxes, before the scatter, so the
683 // step's union is produced by the new owners and there is no half-migrated state anywhere.
684 // The boxes have just been replicated, so `plannedMasters()` is the same on every rank.
685 lastReassigned_ = 0;
686 if (reassignEvery > 0 && ((step_ + 1) % reassignEvery) == 0)
687 lastReassigned_ = assignMasters();
688 exch_->scatterColourMax(blocks_, cLocal);
689 if (batch) {
690 measureBatched(dt); // C2 (§5.10): one launch, no host read; applied at the next read #1
691 } else {
692 for (auto& b : blocks_)
693 if (b.mine_)
694 measure(b, dt);
695 }
696 ++step_;
697 }
698
700 long lastReassigned() const { return lastReassigned_; }
701
702 // ---- rung W2: per-block curvature + the CSF face force ---------------------------------------
703
707 void enableCsf(double sigmaValue) {
708 csfEnabled = true;
709 sigma = sigmaValue;
710 for (std::size_t i = 0; i < blocks_.size(); ++i)
711 if (blocks_[i].mine_ && blocks_[i].allocated_)
712 allocateCsf(i);
713 }
714
717 void computeCsf(SField fx, SField fy, SField fz) {
718 if (!exch_)
719 throw std::runtime_error("peclet::flow::vof::VofBlockSet: no exchange installed");
720 curvStats_ = VofCurvature::Stats{};
721 if (csfBatchEligible()) { // C1 §5.9 stage 6: every pass for all blocks in one launch
723 exch_->scatterForceSum(blocks_, fx, fy, fz);
724 return;
725 }
726 // Tier 3 of every local block in ONE batched launch (VofCurvature::computeBegin): bit-identical
727 // to the per-block cascade, which ran the 5^3 fit's full latency once per block, serially.
728 bool batch = true;
729 for (auto& b : blocks_)
730 if (b.mine_ && !b.curv_.useWorklist)
731 batch = false;
732 if (batch) {
733 std::vector<VofCurvFallbackJob> jobs;
734 for (auto& b : blocks_)
735 if (b.mine_) {
736 b.curv_.computeBegin(b.adv_.colour());
737 jobs.push_back(b.curv_.fallbackJob(b.adv_.colour()));
738 }
740 }
741 for (auto& b : blocks_) {
742 if (!b.mine_)
743 continue;
744 const VofCurvature::Stats st =
745 batch ? b.curv_.computeEnd() : b.curv_.compute(b.adv_.colour());
746 curvStats_.interfacial += st.interfacial;
747 curvStats_.hf += st.hf;
748 curvStats_.hfMixed += st.hfMixed;
749 curvStats_.hfFit += st.hfFit;
750 curvStats_.pv += st.pv;
751 curvStats_.pvReduced += st.pvReduced;
752 curvStats_.noEstimate += st.noEstimate;
753 curvStats_.clipped += st.clipped;
754 buildCsfForce(b);
755 }
756 exch_->scatterForceSum(blocks_, fx, fy, fz);
757 }
758 VofCurvature::Stats csfCurvatureStats() const { return curvStats_; }
759
762 void setKappaMax(double km) {
763 curvProto.kappaMax = km;
764 for (auto& b : blocks_)
765 b.curv_.kappaMax = km;
766 }
767
768 void allocateCsf(std::size_t idx) {
769 VofBlock& b = blocks_[idx];
770 const I3 n = b.adv_.inner();
771 b.curv_.init(n.x, n.y, n.z, ghost_);
772 // EVERY tunable of the prototype, the metric included (review finding 1: this list once
773 // dropped `pureEps`, so a block WY at wispEps 1e-8 was judged at the 1e-10 purity floor and the
774 // HF tier decayed into the PV fallback silently, 63.8 % -> 52.3 %; and it never carried the
775 // metric, so a cascade re-allocated by a re-centring ran an anisotropic block at unit metric).
776 b.curv_.copyTunablesFrom(curvProto);
777 const long len = static_cast<long>(b.adv_.extent().x) * b.adv_.extent().y * b.adv_.extent().z;
778 for (int c = 0; c < 3; ++c)
779 b.f_[c] = SField("vof::block::csf", len);
780 }
781
789 const I3 e = b.adv_.extent(), n = b.adv_.inner();
790 const int g = ghost_;
791 // Phase 3 (V3.1): the same per-axis pressure-gradient weight `1/h_a'^2` the global
792 // `Solver::addCsfRhs` uses. `h_` stays the (index) block spacing and is not that weight.
793 const double sig = sigma;
794 const double wgt[3] = {metric_.h[0] * metric_.h[0], metric_.h[1] * metric_.h[1],
795 metric_.h[2] * metric_.h[2]};
796 SField cv = b.adv_.colour(), kp = b.curv_.kappa(), kb = b.curv_.branch();
797 const long sy = e.x, sz = static_cast<long>(e.x) * e.y;
798 for (int c = 0; c < 3; ++c) {
799 SField ff = b.f_[c];
800 const double wc = wgt[c];
801 const long strd = (c == 0) ? 1 : (c == 1 ? sy : sz);
802 Kokkos::parallel_for(
803 "vof::block::csf_force",
804 MDRange3<SExec>(SExec(), {g, g, g},
805 {g + n.x, g + n.y, g + n.z}),
806 KOKKOS_LAMBDA(int x, int y, int z) {
807 const long i = L3(x, y, z, e);
808 const double dC = cv(i) - cv(i - strd);
809 double f = 0.0;
810 if (dC != 0.0) {
811 double kf = 0.0;
812 vof::csfFaceCurvature(kp(i - strd), kb(i - strd), kp(i), kb(i), kf);
813 f = vof::csfFaceForce(sig, kf, dC, wc);
814 }
815 ff(i) = f;
816 });
817 }
818 }
819
821 void scatterSum(SField sLocal) {
822 if (!exch_)
823 throw std::runtime_error("peclet::flow::vof::VofBlockSet: no exchange installed");
824 exch_->scatterColourSum(blocks_, sLocal);
825 }
826
828 void scatter(SField cLocal) {
829 if (!exch_)
830 throw std::runtime_error("peclet::flow::vof::VofBlockSet: no exchange installed");
831 exch_->syncTable(blocks_);
832 exch_->scatterColourMax(blocks_, cLocal);
833 }
834
835 // ---- rung W1 item (a): master assignment and re-assignment ---------------------------------
836
840 std::vector<int> plannedMasters() const {
841 std::vector<long> w(blocks_.size());
842 for (std::size_t i = 0; i < blocks_.size(); ++i)
843 w[i] = blocks_[i].box.cells();
844 std::vector<int> m;
845 switch (assignMode) {
847 vofAssignLpt(w, size_, m);
848 break;
850 vofAssignOrb(w, size_, m);
851 break;
853 default:
854 m.assign(blocks_.size(), 0);
855 for (std::size_t i = 0; i < blocks_.size(); ++i)
856 m[i] = static_cast<int>(blocks_[i].id % size_);
857 break;
858 }
859 return m;
860 }
861
866 flushStats(); // C2: a migrating block carries its previous centroid (serializeAux)
867 if (blocks_.empty() || size_ <= 1)
868 return 0;
869 const std::vector<int> want = plannedMasters();
870 std::vector<int> old(blocks_.size());
871 long moved = 0;
872 for (std::size_t i = 0; i < blocks_.size(); ++i) {
873 old[i] = blocks_[i].master;
874 if (old[i] != want[i])
875 ++moved;
876 }
877 if (moved == 0)
878 return 0;
879 for (std::size_t i = 0; i < blocks_.size(); ++i) {
880 blocks_[i].master = want[i];
881 blocks_[i].st_.master = want[i];
882 }
883 // gaining ranks allocate FIRST (zeroed), so the migration has somewhere to land
884 for (std::size_t i = 0; i < blocks_.size(); ++i)
885 if (want[i] == rank_ && old[i] != rank_) {
886 blocks_[i].mine_ = true;
887 allocate(i);
888 }
889 if (exch_)
890 exch_->migrateColour(blocks_, old);
891 for (std::size_t i = 0; i < blocks_.size(); ++i) {
892 if (old[i] == rank_ && want[i] != rank_) {
893 release(i);
894 blocks_[i].mine_ = false;
895 } else if (want[i] == rank_ && old[i] != rank_) {
896 // A GAINED block only. A block this rank keeps is left completely alone -- `measure` would
897 // reset its previous centroid and blank one step of its reported velocity.
898 blocks_[i].mine_ = true;
899 fillBlockGhosts(blocks_[i]);
900 double aux[VofBlock::kAuxLen];
901 blocks_[i].serializeAux(aux); // the migrated previous centroid survives the re-measure
902 measure(blocks_[i], 0.0);
903 blocks_[i].deserializeAux(aux);
904 }
905 }
906 return moved;
907 }
908
909 // ---- general seeding: a block from an arbitrary global colour field -------------------------
910
916 void seedBox(const VofBox& bb, int extraPad = 0) {
917 flushStats(); // C2, as in seedSphere
918 VofBlock b;
919 b.id = static_cast<long>(blocks_.size());
920 b.master = static_cast<int>(b.id % size_);
921 b.box = vofClampBox(VofBox::grown(bb, margin_ + extraPad), gs_, per_);
922 b.seed_ = bb;
923 b.mine_ = (b.master == rank_);
924 blocks_.push_back(std::move(b));
925 const std::size_t idx = blocks_.size() - 1;
926 for (std::size_t k = 0; k + 1 < blocks_.size(); ++k)
927 if (blocks_[k].mine_ && blocks_[k].allocated_)
928 installHook(k);
929 if (blocks_[idx].mine_)
930 allocate(idx);
931 }
932
934 void finishSeeding(SField cLocal) {
935 if (!exch_)
936 throw std::runtime_error("peclet::flow::vof::VofBlockSet: no exchange installed");
937 flushStats(); // C2: its per-block measure() must not be overwritten by an older packet
938 exch_->gatherColour(blocks_, cLocal);
939 for (auto& b : blocks_)
940 if (b.mine_) {
941 clipToSeed(b);
943 measure(b, 0.0);
944 }
945 }
946
947 // ---- rung W3: checkpoint / restart of the block state ---------------------------------------
948 //
949 // A run longer than one job's wall clock has to write its markers out and read them back. The
950 // union colour field is NOT enough to do that: `seedBox`/`finishSeeding` gather each marker out
951 // of the UNION and clip to the seed extent, so two markers that touch each adopt a slice of the
952 // other (WO-W12 open item 5, measured -2.7 % / +7.1 %). A block's ONLY state is its own inner
953 // colour (everything else is either replicated -- the table -- or recomputed every step), so a
954 // checkpoint is exactly {box, colour} per block and the restart below is EXACT whatever the
955 // markers are doing.
956
959 std::vector<double> blockColourHost(std::size_t idx) {
960 VofBlock& b = blocks_.at(idx);
961 if (!b.mine_ || !b.allocated_)
962 return {};
963 const I3 n = b.adv_.inner();
964 SField c = b.adv_.colour();
965 auto hc = Kokkos::create_mirror_view(c);
966 Kokkos::deep_copy(hc, c);
967 std::vector<double> out(static_cast<std::size_t>(n.x) * static_cast<std::size_t>(n.y) *
968 static_cast<std::size_t>(n.z));
969 std::size_t q = 0;
970 for (int z = 0; z < n.z; ++z)
971 for (int y = 0; y < n.y; ++y)
972 for (int x = 0; x < n.x; ++x)
973 out[q++] = hc(b.adv_.index(x, y, z));
974 return out;
975 }
976
979 std::vector<double> blockKappaHost(std::size_t idx) {
980 VofBlock& b = blocks_.at(idx);
981 if (!b.mine_ || !b.allocated_ || !csfEnabled)
982 return {};
983 const I3 n = b.adv_.inner();
984 SField k = b.curv_.kappa();
985 auto hk = Kokkos::create_mirror_view(k);
986 Kokkos::deep_copy(hk, k);
987 std::vector<double> out(static_cast<std::size_t>(n.x) * static_cast<std::size_t>(n.y) *
988 static_cast<std::size_t>(n.z));
989 std::size_t q = 0;
990 for (int z = 0; z < n.z; ++z)
991 for (int y = 0; y < n.y; ++y)
992 for (int x = 0; x < n.x; ++x)
993 out[q++] = hk(b.adv_.index(x, y, z));
994 return out;
995 }
996
1000 void seedBoxWithColour(const VofBox& bb, const std::vector<double>& colour) {
1001 flushStats(); // C2, as in seedSphere
1002 VofBlock b;
1003 b.id = static_cast<long>(blocks_.size());
1004 b.master = masterOf(b.id);
1005 b.box = vofClampBox(bb, gs_, per_);
1006 b.mine_ = (b.master == rank_);
1007 blocks_.push_back(std::move(b));
1008 const std::size_t idx = blocks_.size() - 1;
1009 for (std::size_t k = 0; k + 1 < blocks_.size(); ++k)
1010 if (blocks_[k].mine_ && blocks_[k].allocated_)
1011 installHook(k);
1012 if (!blocks_[idx].mine_)
1013 return;
1014 allocate(idx);
1015 VofBlock& nb = blocks_[idx];
1016 const I3 n = nb.adv_.inner();
1017 const std::size_t want = static_cast<std::size_t>(n.x) * static_cast<std::size_t>(n.y) *
1018 static_cast<std::size_t>(n.z);
1019 if (colour.size() != want)
1020 throw std::runtime_error(
1021 "peclet::flow::vof::VofBlockSet::seedBoxWithColour: colour size does not match the box");
1022 SField c = nb.adv_.colour();
1023 auto hc = Kokkos::create_mirror_view(c);
1024 Kokkos::deep_copy(hc, c); // ghosts as allocated (zero); the fill below re-does them anyway
1025 std::size_t q = 0;
1026 for (int z = 0; z < n.z; ++z)
1027 for (int y = 0; y < n.y; ++y)
1028 for (int x = 0; x < n.x; ++x)
1029 hc(nb.adv_.index(x, y, z)) = colour[q++];
1030 Kokkos::deep_copy(c, hc);
1031 Kokkos::fence();
1032 fillBlockGhosts(nb);
1033 measure(nb, 0.0);
1034 }
1035
1043 const I3 e = b.adv_.extent(), n = b.adv_.inner(), o = b.box.origin();
1044 const int g = ghost_;
1045 const int lx = b.seed_.lo[0], hx = b.seed_.hi[0], ly = b.seed_.lo[1], hy = b.seed_.hi[1],
1046 lz = b.seed_.lo[2], hz = b.seed_.hi[2];
1047 if (hx <= lx || hy <= ly || hz <= lz)
1048 return; // no seed box recorded (a sphere seed): nothing to clip
1049 SField c = b.adv_.colour();
1050 Kokkos::parallel_for(
1051 "vof::block::clip_seed",
1052 MDRange3<SExec>(SExec(), {0, 0, 0}, {n.x, n.y, n.z}),
1053 KOKKOS_LAMBDA(int x, int y, int z) {
1054 const int gx = x + o.x, gy = y + o.y, gz = z + o.z;
1055 if (gx < lx || gx >= hx || gy < ly || gy >= hy || gz < lz || gz >= hz)
1056 c(L3(x + g, y + g, z + g, e)) = 0.0;
1057 });
1058 Kokkos::fence();
1059 }
1060
1064 const I3 e = b.adv_.extent(), n = b.adv_.inner(), o = b.box.origin();
1065 const int g = ghost_;
1066 SField f = b.adv_.colour();
1067 // 1. every ghost cell -> 0 (the block's far field: C = 0, the continuous phase, by the margin).
1068 Kokkos::parallel_for(
1069 "vof::block::ghost_zero",
1070 MDRange3<SExec>(SExec(), {0, 0, 0}, {e.x, e.y, e.z}),
1071 KOKKOS_LAMBDA(int x, int y, int z) {
1072 if (x >= g && x < g + n.x && y >= g && y < g + n.y && z >= g && z < g + n.z)
1073 return;
1074 f(L3(x, y, z, e)) = 0.0;
1075 });
1076 // 2. an axis the block spans entirely, and which is globally periodic, wraps within the block.
1077 const bool sx = per_[0] && b.box.n(0) == gs_.x;
1078 const bool sy = per_[1] && b.box.n(1) == gs_.y;
1079 const bool sz = per_[2] && b.box.n(2) == gs_.z;
1080 if (sx || sy || sz)
1081 vof::periodicFill(f, e, g, sx, sy, sz);
1082 // 3. the part of the extended box outside a NON-periodic domain takes the globally clamped
1083 // (zero-gradient) value — the same rule the structured colour field uses.
1084 if (!per_[0] || !per_[1] || !per_[2])
1085 vof::clampFill(f, e, g, o, gs_, per_[0], per_[1], per_[2]);
1086 }
1087
1089 std::vector<VofBlockStats> statsAll() {
1090 flushStats(); // C2: the last batched measure's values reach the host here or at read #1
1091 // `id`, `master` and the box come from the REPLICATED table, so they are reported on every
1092 // rank; only the measured entries (volume, centroid, velocity, moments, area) are the
1093 // master's. Filling them here rather than in `measure()` is what makes that promise true on
1094 // a rank that masters nothing.
1095 std::vector<VofBlockStats> v;
1096 v.reserve(blocks_.size());
1097 for (const auto& b : blocks_) {
1098 VofBlockStats q = b.st_;
1099 q.id = b.id;
1100 q.master = b.master;
1101 for (int d = 0; d < 3; ++d) {
1102 q.lo[d] = b.box.lo[d];
1103 q.hi[d] = b.box.hi[d];
1104 }
1105 q.cells = b.box.cells();
1106 v.push_back(q);
1107 }
1108 return v;
1109 }
1110
1114 void masterCensus(std::vector<long>& masters, std::vector<long>& cells) const {
1115 masters.assign(size_, 0);
1116 cells.assign(size_, 0);
1117 for (const auto& b : blocks_) {
1118 masters[b.master] += 1;
1119 cells[b.master] += VofBox::grown(b.box, 0).cells();
1120 }
1121 }
1122
1123 double cellImbalance() const {
1124 std::vector<long> m, c;
1125 masterCensus(m, c);
1126 long tot = 0, mx = 0;
1127 for (long v : c) {
1128 tot += v;
1129 mx = v > mx ? v : mx;
1130 }
1131 if (tot == 0)
1132 return 1.0;
1133 return static_cast<double>(mx) * size_ / static_cast<double>(tot);
1134 }
1135
1136 I3 globalSize() const { return gs_; }
1137 std::array<bool, 3> periodic() const { return per_; }
1138 int rank() const { return rank_; }
1139 int size() const { return size_; }
1140
1141 private:
1145 void installHook(std::size_t idx) {
1146 VofBlockSet* self = this;
1147 blocks_[idx].adv_.exchange = [self, idx](SField) { self->fillBlockGhosts(self->blocks_[idx]); };
1148 }
1149
1150 void allocate(std::size_t idx) {
1151 VofBlock& b = blocks_[idx];
1152 b.adv_ = acquireAdvector(b.box.n(0), b.box.n(1), b.box.n(2));
1153 installHook(idx);
1154 b.allocated_ = true;
1155 b.st_.id = b.id;
1156 b.st_.master = b.master;
1157 if (csfEnabled)
1158 allocateCsf(idx);
1159 }
1160
1163 void release(std::size_t idx) {
1164 VofBlock& b = blocks_[idx];
1165 retireAdvector(std::move(b.adv_));
1166 b.adv_ = WyAdvector();
1167 b.curv_ = VofCurvature();
1168 for (int c = 0; c < 3; ++c)
1169 b.f_[c] = SField();
1170 b.allocated_ = false;
1171 b.hasPrev_ = false;
1172 }
1173
1180 WyAdvector acquireAdvector(int nx, int ny, int nz) {
1181 const std::array<int, 3> key{nx, ny, nz};
1182 if (usePool) {
1183 auto it = pool_.find(key);
1184 if (it != pool_.end() && !it->second.empty()) {
1185 WyAdvector a = std::move(it->second.back());
1186 it->second.pop_back();
1187 ++poolHits_;
1188 // S4 (doc/vof_gpu_coarse_tail_design.md §5.5): instance-ordered fills, instance fence
1189 Kokkos::deep_copy(SExec(), a.colour(), 0.0);
1190 for (int d = 0; d < 3; ++d)
1191 Kokkos::deep_copy(SExec(), a.faceVel(d), 0.0);
1192 SExec().fence("vof::block::pool_reset");
1193 a.cflLimit = cflLimit;
1194 a.wispEps = wispEps;
1195 a.metric = metric_; // a pooled advector carries whatever metric it last had
1196 a.globalMax = nullptr;
1197 a.exchange = nullptr; // re-installed by installHook
1198 return a;
1199 }
1200 }
1201 ++poolMisses_;
1202 WyAdvector a;
1203 a.init(nx, ny, nz, h_, ghost_);
1204 a.cflLimit = cflLimit;
1205 a.wispEps = wispEps;
1206 a.metric = metric_; // a re-centred block's fresh advector keeps the anisotropic metric
1207 a.globalMax = nullptr; // the block IS the whole domain of its own advector
1208 return a;
1209 }
1210
1211 void retireAdvector(WyAdvector&& a) {
1212 if (!usePool || a.colour().extent(0) == 0)
1213 return;
1214 const I3 n = a.inner();
1215 const std::array<int, 3> key{n.x, n.y, n.z};
1216 auto& v = pool_[key];
1217 if (static_cast<int>(v.size()) >= poolCapacity)
1218 return;
1219 a.exchange = nullptr; // the hook captures a block index; never carry it into the pool
1220 v.push_back(std::move(a));
1221 }
1222
1223 public:
1224 long poolHits() const { return poolHits_; }
1225 long poolMisses() const { return poolMisses_; }
1226 void clearPool() { pool_.clear(); }
1227
1228 // NOTE: everything below is public ONLY because nvcc refuses an extended `__host__ __device__`
1229 // lambda inside a private or protected member function ("The enclosing parent function ... cannot
1230 // have private or protected access within its class"). These are implementation details of the
1231 // step; treat them as such.
1232 public:
1235 bool bubbleBox(const VofBlock& b, VofBox& out) const {
1236 const I3 e = b.adv_.extent(), n = b.adv_.inner();
1237 const int g = ghost_;
1238 const double eps = bubbleEps > wispEps ? bubbleEps : wispEps; // §12.2
1239 SField c = b.adv_.colour();
1240 int lo[3] = {n.x, n.y, n.z}, hi[3] = {-1, -1, -1};
1241 using MD = MDRange3<SExec>;
1242 MD pol(SExec(), {g, g, g}, {g + n.x, g + n.y, g + n.z});
1243 Kokkos::parallel_reduce(
1244 "vof::block::bbox_lo", pol,
1245 KOKKOS_LAMBDA(int x, int y, int z, int& lx, int& ly, int& lz) {
1246 if (!(Kokkos::fabs(c(L3(x, y, z, e))) > eps))
1247 return;
1248 const int px = x - g, py = y - g, pz = z - g;
1249 lx = px < lx ? px : lx;
1250 ly = py < ly ? py : ly;
1251 lz = pz < lz ? pz : lz;
1252 },
1253 Kokkos::Min<int>(lo[0]), Kokkos::Min<int>(lo[1]), Kokkos::Min<int>(lo[2]));
1254 Kokkos::parallel_reduce(
1255 "vof::block::bbox_hi", pol,
1256 KOKKOS_LAMBDA(int x, int y, int z, int& hx, int& hy, int& hz) {
1257 if (!(Kokkos::fabs(c(L3(x, y, z, e))) > eps))
1258 return;
1259 const int px = x - g, py = y - g, pz = z - g;
1260 hx = px > hx ? px : hx;
1261 hy = py > hy ? py : hy;
1262 hz = pz > hz ? pz : hz;
1263 },
1264 Kokkos::Max<int>(hi[0]), Kokkos::Max<int>(hi[1]), Kokkos::Max<int>(hi[2]));
1265 Kokkos::fence();
1266 if (hi[0] < lo[0])
1267 return false; // the block is empty
1268 for (int d = 0; d < 3; ++d) {
1269 out.lo[d] = lo[d] + b.box.lo[d];
1270 out.hi[d] = hi[d] + 1 + b.box.lo[d];
1271 }
1272 return true;
1273 }
1274
1277 void recentre(std::size_t idx) {
1278 VofBox bb;
1279 if (!bubbleBox(blocks_[idx], bb))
1280 return;
1281 recentreTo(idx, bb);
1282 }
1283
1284 void recentreTo(std::size_t idx, const VofBox& bb) {
1285 VofBlock& b = blocks_[idx];
1286 const VofBox need = vofClampBox(VofBox::grown(bb, margin_), gs_, per_);
1287 bool wasteful = false;
1288 if (allowShrink)
1289 for (int d = 0; d < 3; ++d)
1290 if (b.box.n(d) > need.n(d) + 2 * (margin_ + recentrePad))
1291 wasteful = true;
1292 if (b.box.contains(need) && !wasteful)
1293 return;
1294 const VofBox want = VofBox::grown(bb, margin_ + recentrePad);
1295 const VofBox nb = vofClampBox(want, gs_, per_);
1296 if (nb == b.box)
1297 return;
1298 // §12.3: an axis on which the box spans the whole periodic domain is copied WITH the wrap
1299 // (source index = destination global index mod L) -- lossless, a marker as long as the domain
1300 // is legal. Every other axis keeps the plain copy by unwrapped global index.
1301 const int L[3] = {gs_.x, gs_.y, gs_.z};
1302 bool wrap[3];
1303 bool anyWrap = false;
1304 for (int d = 0; d < 3; ++d) {
1305 wrap[d] = per_[d] && nb.n(d) == L[d];
1306 anyWrap = anyWrap || (wrap[d] && want.n(d) >= L[d]);
1307 }
1308 // What the move DROPS: the colour of old-box cells that fall outside the new box. Measured
1309 // directly rather than as `sum(old) - sum(new)`, which at |sum| ~ 1e2 is 1e-13 of summation
1310 // rounding and says nothing about the wisps (measured -5.7e-14 that way on the G1 scene).
1311 const double dropped = outOfBoxSum(b, nb, wrap);
1312 // exact copy by global index (both boxes are in the same unwrapped global frame)
1313 WyAdvector fresh = acquireAdvector(nb.n(0), nb.n(1), nb.n(2));
1314 const I3 se = b.adv_.extent(), de = fresh.extent();
1315 const I3 dn = fresh.inner();
1316 const int g = ghost_;
1317 const int sx = b.box.lo[0], sy = b.box.lo[1], sz = b.box.lo[2];
1318 const int snx = b.box.n(0), sny = b.box.n(1), snz = b.box.n(2);
1319 const int dx = nb.lo[0], dy = nb.lo[1], dz = nb.lo[2];
1320 const bool wx = wrap[0], wy = wrap[1], wz = wrap[2];
1321 const int Lx = L[0], Ly = L[1], Lz = L[2];
1322 SField src = b.adv_.colour(), dst = fresh.colour();
1323 Kokkos::parallel_for(
1324 "vof::block::recentre_copy",
1325 MDRange3<SExec>(SExec(), {0, 0, 0}, {dn.x, dn.y, dn.z}),
1326 KOKKOS_LAMBDA(int x, int y, int z) {
1327 const int gx = x + dx, gy = y + dy, gz = z + dz;
1328 int lx = gx - sx, ly = gy - sy, lz = gz - sz;
1329 if (wx)
1330 lx = ((lx % Lx) + Lx) % Lx;
1331 if (wy)
1332 ly = ((ly % Ly) + Ly) % Ly;
1333 if (wz)
1334 lz = ((lz % Lz) + Lz) % Lz;
1335 double v = 0.0;
1336 if (lx >= 0 && lx < snx && ly >= 0 && ly < sny && lz >= 0 && lz < snz)
1337 v = src(L3(lx + g, ly + g, lz + g, se));
1338 dst(L3(x + g, y + g, z + g, de)) = v;
1339 });
1340 SExec().fence("vof::block::recentre_copy"); // S4: instance, not global
1341 b.box = nb;
1342 retireAdvector(std::move(b.adv_));
1343 b.adv_ = std::move(fresh);
1344 b.adv_.cflLimit = cflLimit;
1345 b.adv_.globalMax = nullptr;
1346 installHook(idx);
1347 if (csfEnabled)
1348 allocateCsf(idx);
1349 fillBlockGhosts(blocks_[idx]);
1350 blocks_[idx].st_.recentred = true;
1351 blocks_[idx].st_.discarded += dropped;
1352 if (anyWrap)
1353 ++blocks_[idx].st_.fullAxis;
1354 }
1355
1374 std::vector<VofBlock*> one{&b};
1375 debrisPassBatch(one);
1376 }
1377
1385 void debrisPassBatch(std::vector<VofBlock*>& bs, bool deferLedger = false) {
1386 const double t0 = timingOn ? tick_() : 0.0;
1387 if (batchEligible()) {
1388 debrisBatched(bs);
1389 if (!deferLedger) {
1390 Kokkos::deep_copy(pkHost_, pk_);
1392 }
1393 } else {
1395 }
1396 if (timingOn) {
1397 Kokkos::fence();
1398 debrisSeconds += tick_() - t0;
1399 }
1400 debrisCalls += static_cast<long>(bs.size());
1401 }
1402
1403 struct DebrisJob {
1404 double* c;
1405 const long *listD, *listR, *listA;
1406 long nDc, nDr, nR, nA;
1407 };
1408 static constexpr int kDebrisBatch = 16;
1413
1416 long k, nD, nR, nA, nDr;
1418 };
1419 void debrisPassBatchImpl(std::vector<VofBlock*>& bs) {
1420 const bool doDebris = csfEnabled && debrisCensus, doResidue = wispEps > 0.0;
1421 const int g = ghost_;
1422 // The interfacial predicate of D and A is `max(interfaceEps, wispEps)` (review finding 7): R
1423 // is `0 < |C| <= wispEps`, so the three lists are DISJOINT whatever the two thresholds are --
1424 // on a kinematic block run curvProto.interfaceEps is 0 while wispEps is 1e-8, and an A/R or
1425 // D/R overlap would drop a W share or count one cell in both ledgers.
1426 const double weps = wispEps, cfull = kDebrisMaxC;
1427 const double ieps = curvProto.interfaceEps > weps ? curvProto.interfaceEps : weps;
1428 // per-block list offsets into the shared scratch
1429 std::vector<long> off(bs.size() + 1, 0);
1430 for (std::size_t k = 0; k < bs.size(); ++k) {
1431 const I3 n = bs[k]->adv_.inner();
1432 off[k + 1] = off[k] + static_cast<long>(n.x) * n.y * n.z;
1433 }
1434 auto ensure = [&](LField& f, const char* name) {
1435 if (static_cast<long>(f.extent(0)) < off.back())
1436 f = LField(Kokkos::view_alloc(std::string(name), Kokkos::WithoutInitializing), off.back());
1437 };
1438 if (doDebris)
1439 ensure(listD_, "vof::block::debris_list");
1440 if (doResidue)
1441 ensure(listR_, "vof::block::residue_list");
1442 std::vector<DebrisPending> pend;
1443 std::vector<DebrisJob> jobs;
1444 for (std::size_t k = 0; k < bs.size(); ++k) {
1445 VofBlock& b = *bs[k];
1446 b.st_.debrisCells = 0;
1447 b.st_.debrisVolume = 0.0;
1448 const I3 e = b.adv_.extent(), n = b.adv_.inner();
1449 const long region = static_cast<long>(n.x) * n.y * n.z, o = off[k];
1450 const long sy = e.x, sz = static_cast<long>(e.x) * e.y;
1451 const int nx = n.x, ny = n.y;
1452 SField c = b.adv_.colour();
1453 long nD = 0, nR = 0;
1454 if (doDebris) {
1455 LField listD = listD_;
1456 Kokkos::parallel_scan(
1457 "vof::block::debris_mark", Kokkos::RangePolicy<SExec>(SExec(), 0, region),
1458 KOKKOS_LAMBDA(const long r, long& upd, const bool final) {
1459 const int ix = static_cast<int>(r % nx);
1460 const int iy = static_cast<int>((r / nx) % ny);
1461 const int iz = static_cast<int>(r / (static_cast<long>(nx) * ny));
1462 const long i = L3(g + ix, g + iy, g + iz, e);
1463 if (!vofDebrisCell(c, i, sy, sz, ieps, cfull))
1464 return;
1465 if (final)
1466 listD(o + upd) = i;
1467 ++upd;
1468 },
1469 nD);
1470 }
1471 if (doResidue) {
1472 LField listR = listR_;
1473 Kokkos::parallel_scan(
1474 "vof::block::residue_mark", Kokkos::RangePolicy<SExec>(SExec(), 0, region),
1475 KOKKOS_LAMBDA(const long r, long& upd, const bool final) {
1476 const int ix = static_cast<int>(r % nx);
1477 const int iy = static_cast<int>((r / nx) % ny);
1478 const int iz = static_cast<int>(r / (static_cast<long>(nx) * ny));
1479 const double ci = c(L3(g + ix, g + iy, g + iz, e));
1480 if (!vofResidueCell(ci, weps))
1481 return;
1482 if (final)
1483 listR(o + upd) = L3(g + ix, g + iy, g + iz, e);
1484 ++upd;
1485 },
1486 nR);
1487 }
1488 const bool removeD = doDebris && debrisRemove && nD > 0;
1489 if (!removeD && nR == 0) {
1490 if (doDebris && nD > 0) { // census only: report what WOULD be removed
1491 LField listD = listD_;
1492 double dVd = 0.0;
1493 Kokkos::parallel_reduce(
1494 "vof::block::debris_census", Kokkos::RangePolicy<SExec>(SExec(), 0, 1),
1495 KOKKOS_LAMBDA(const long, double& acc) {
1496 double a = 0.0;
1497 for (long t = 0; t < nD; ++t)
1498 a += c(listD(o + t));
1499 acc += a;
1500 },
1501 dVd);
1502 b.st_.debrisCells = nD;
1503 b.st_.debrisVolume = dVd;
1504 }
1505 continue; // nothing to remove: nothing else runs
1506 }
1507 ensure(listA_, "vof::block::attached_list");
1508 LField listA = listA_;
1509 long nA = 0;
1510 Kokkos::parallel_scan(
1511 "vof::block::debris_attached", Kokkos::RangePolicy<SExec>(SExec(), 0, region),
1512 KOKKOS_LAMBDA(const long r, long& upd, const bool final) {
1513 const int ix = static_cast<int>(r % nx);
1514 const int iy = static_cast<int>((r / nx) % ny);
1515 const int iz = static_cast<int>(r / (static_cast<long>(nx) * ny));
1516 const long i = L3(g + ix, g + iy, g + iz, e);
1517 if (!vofAttachedCell(c, i, sy, sz, ieps, cfull))
1518 return;
1519 if (final)
1520 listA(o + upd) = i;
1521 ++upd;
1522 },
1523 nA);
1524 DebrisPending pb{&b, static_cast<long>(k), nD, nR, nA, removeD ? nD : 0, removeD};
1525 pend.push_back(pb);
1526 DebrisJob j;
1527 j.c = c.data();
1528 j.listD = doDebris ? listD_.data() + o : nullptr;
1529 j.listR = doResidue ? listR_.data() + o : nullptr;
1530 j.listA = listA_.data() + o;
1531 j.nDc = doDebris ? nD : 0;
1532 j.nDr = pb.nDr;
1533 j.nR = nR;
1534 j.nA = nA;
1535 jobs.push_back(j);
1536 }
1537 if (pend.empty())
1538 return;
1539 // [0] sum_D, [1] sum_R, [2] W, [3] VA, [4] removed volume (then `lost`), [5] acted?
1540 // The SUMS run on one thread per block in list order (a fixed summation order: the state stays
1541 // decomposition-independent bitwise); the per-cell writes are independent once dV and W are
1542 // known -- D, R and A are disjoint -- so they run in parallel and every cell receives exactly
1543 // the double the serial loop gave it (review finding 4). `lost` (a cap at 1, expected never to
1544 // fire) is summed in list order from a per-cell scratch only when some cell was capped.
1545 const long np = static_cast<long>(pend.size());
1546 debrisSums(jobs, np);
1547 debrisApply(pend, off, doDebris, np);
1548 }
1549
1551 void debrisSums(const std::vector<DebrisJob>& jobs, long np) {
1552 if (static_cast<long>(debrisOut_.extent(0)) < 6 * np) {
1553 debrisOut_ = SField("vof::block::debris_out", 6 * np);
1554 debrisOutHost_ = Kokkos::create_mirror_view(debrisOut_);
1555 }
1556 SField out = debrisOut_;
1557 const double vaMin = kDebrisMinAttached;
1558 for (long j0 = 0; j0 < np; j0 += kDebrisBatch) {
1559 DebrisTable T;
1560 T.nj = static_cast<int>(std::min<long>(kDebrisBatch, np - j0));
1561 for (int q = 0; q < T.nj; ++q)
1562 T.job[q] = jobs[j0 + q];
1563 const long base = 6 * j0;
1564 Kokkos::parallel_for(
1565 "vof::block::debris_sums", Kokkos::RangePolicy<SExec>(SExec(), 0, T.nj),
1566 KOKKOS_LAMBDA(const long q) {
1567 const DebrisJob& J = T.job[q];
1568 const VofRawField c{J.c};
1569 const long nDc = J.nDc, nDr = J.nDr, nR = J.nR, nA = J.nA;
1570 double sD = 0.0, sR = 0.0, W = 0.0, VA = 0.0;
1571 for (long t = 0; t < nDc; ++t)
1572 sD += c(J.listD[t]);
1573 for (long t = 0; t < nR; ++t)
1574 sR += c(J.listR[t]);
1575 for (long t = 0; t < nA; ++t) {
1576 const double ca = c(J.listA[t]);
1577 W += ca * (1.0 - ca);
1578 VA += ca;
1579 }
1580 const long ob = base + 6 * q;
1581 out(ob + 0) = sD;
1582 out(ob + 1) = sR;
1583 out(ob + 2) = W;
1584 out(ob + 3) = VA;
1585 out(ob + 4) = 0.0;
1586 out(ob + 5) = (VA < vaMin || !(W > 0.0)) ? 0.0 : 1.0;
1587 // the removed volume, in the fixed order D (if removing) then R -- the serial order
1588 double dV = 0.0;
1589 for (long t = 0; t < nDr; ++t)
1590 dV += c(J.listD[t]);
1591 for (long t = 0; t < nR; ++t)
1592 dV += c(J.listR[t]);
1593 out(ob + 4) = dV; // carried to the write kernel; `lost` replaces it below
1594 });
1595 }
1596 Kokkos::deep_copy(debrisOutHost_, out); // ONE device->host copy for every block
1597 }
1598
1599 void debrisApply(const std::vector<DebrisPending>& pend, const std::vector<long>& off,
1600 bool doDebris, long np) {
1601 auto hoAll = debrisOutHost_;
1602 for (long p = 0; p < np; ++p) {
1603 const DebrisPending& pb = pend[p];
1604 VofBlock& b = *pb.b;
1605 const long o = off[pb.k];
1606 const I3 n = b.adv_.inner();
1607 const long region = static_cast<long>(n.x) * n.y * n.z;
1608 double ho[6];
1609 for (int q = 0; q < 6; ++q)
1610 ho[q] = hoAll(6 * p + q);
1611 const long nD = pb.nD, nR = pb.nR, nA = pb.nA, nDr = pb.nDr;
1612 const bool removeD = pb.removeD;
1613 if (ho[5] != 0.0) {
1614 const double dV = ho[4], W = ho[2];
1615 if (static_cast<long>(debrisEx_.extent(0)) < region)
1616 debrisEx_ = SField(Kokkos::view_alloc("vof::block::debris_excess",
1617 Kokkos::WithoutInitializing),
1618 region);
1619 SField ex = debrisEx_;
1620 SField c = b.adv_.colour();
1621 LField listD = listD_, listR = listR_, listA = listA_;
1622 const long nZ = nDr + nR, nTot = nZ + nA;
1623 long capped = 0;
1624 Kokkos::parallel_reduce(
1625 "vof::block::debris_write", Kokkos::RangePolicy<SExec>(SExec(), 0, nTot),
1626 KOKKOS_LAMBDA(const long t, long& cap) {
1627 if (t < nDr) {
1628 c(listD(o + t)) = 0.0;
1629 return;
1630 }
1631 if (t < nZ) {
1632 c(listR(o + t - nDr)) = 0.0;
1633 return;
1634 }
1635 const long ta = t - nZ;
1636 const long i = listA(o + ta);
1637 const double ca = c(i);
1638 const double d = dV * (ca * (1.0 - ca)) / W;
1639 double cn = ca + d;
1640 double e = 0.0;
1641 if (cn > 1.0) {
1642 e = cn - 1.0;
1643 cn = 1.0;
1644 ++cap;
1645 }
1646 ex(ta) = e;
1647 c(i) = cn;
1648 },
1649 capped);
1650 double lost = 0.0;
1651 if (capped > 0) {
1652 Kokkos::parallel_reduce(
1653 "vof::block::debris_lost", Kokkos::RangePolicy<SExec>(SExec(), 0, 1),
1654 KOKKOS_LAMBDA(const long, double& acc) {
1655 double a = 0.0; // list order, as the serial loop accumulated it
1656 for (long t = 0; t < nA; ++t)
1657 a += ex(t);
1658 acc += a;
1659 },
1660 lost);
1661 }
1662 ho[4] = lost;
1663 }
1664 if (doDebris && !debrisRemove && nD > 0) { // census of what WOULD be removed
1665 b.st_.debrisCells = nD;
1666 b.st_.debrisVolume = ho[0];
1667 }
1668 if (ho[5] == 0.0) { // step 3: no attached interface worth a cell -- the clip bounds it
1669 if (removeD)
1670 b.st_.debrisUnresolved += nD;
1671 continue;
1672 }
1673 fillBlockGhosts(b); // the inner colour moved; the next reader expects valid ghosts
1674 if (removeD) {
1675 b.st_.debrisCells = nD;
1676 b.st_.debrisVolume = ho[0];
1677 b.st_.debrisReturned += ho[0];
1678 }
1679 b.st_.residueReturned += ho[1];
1680 b.st_.debrisLost += ho[4];
1681 }
1682 }
1683
1685 double outOfBoxSum(const VofBlock& b, const VofBox& nb) const {
1686 const bool nowrap[3] = {false, false, false};
1687 return outOfBoxSum(b, nb, nowrap);
1688 }
1689
1691 double outOfBoxSum(const VofBlock& b, const VofBox& nb, const bool wrap[3]) const {
1692 const I3 e = b.adv_.extent(), n = b.adv_.inner();
1693 const int g = ghost_;
1694 const int sx = b.box.lo[0], sy = b.box.lo[1], sz = b.box.lo[2];
1695 const int lx = nb.lo[0], ly = nb.lo[1], lz = nb.lo[2];
1696 const int hx = nb.hi[0], hy = nb.hi[1], hz = nb.hi[2];
1697 const bool wx = wrap[0], wy = wrap[1], wz = wrap[2];
1698 SField c = b.adv_.colour();
1699 double v = 0.0;
1700 Kokkos::parallel_reduce(
1701 "vof::block::out_of_box",
1702 MDRange3<SExec>(SExec(), {g, g, g},
1703 {g + n.x, g + n.y, g + n.z}),
1704 KOKKOS_LAMBDA(int x, int y, int z, double& a) {
1705 const int gx = x - g + sx, gy = y - g + sy, gz = z - g + sz;
1706 const bool inX = wx || (gx >= lx && gx < hx), inY = wy || (gy >= ly && gy < hy),
1707 inZ = wz || (gz >= lz && gz < hz);
1708 if (inX && inY && inZ)
1709 return;
1710 a += c(L3(x, y, z, e));
1711 },
1712 v);
1713 SExec().fence("vof::block::out_of_box"); // S4: instance, not global
1714 return v;
1715 }
1716
1717 double innerSum(const VofBlock& b) const {
1718 const I3 e = b.adv_.extent(), n = b.adv_.inner();
1719 const int g = ghost_;
1720 SField c = b.adv_.colour();
1721 double v = 0.0;
1722 Kokkos::parallel_reduce(
1723 "vof::block::inner_sum",
1724 MDRange3<SExec>(SExec(), {g, g, g},
1725 {g + n.x, g + n.y, g + n.z}),
1726 KOKKOS_LAMBDA(int x, int y, int z, double& a) { a += c(L3(x, y, z, e)); }, v);
1727 Kokkos::fence();
1728 return v;
1729 }
1730
1737 double interfaceArea(const VofBlock& b) const {
1738 const I3 e = b.adv_.extent(), n = b.adv_.inner();
1739 const int g = ghost_;
1740 const double hh = h_, aeps = areaEps;
1741 SField c = b.adv_.colour();
1742 const long sy = e.x, sz = static_cast<long>(e.x) * e.y;
1743 double a = 0.0;
1744 Kokkos::parallel_reduce(
1745 "vof::block::area",
1746 MDRange3<SExec>(SExec(), {g, g, g},
1747 {g + n.x, g + n.y, g + n.z}),
1748 KOKKOS_LAMBDA(int x, int y, int z, double& acc) {
1749 const long i = L3(x, y, z, e);
1750 const double ci = c(i);
1751 if (!(ci > aeps) || !(ci < 1.0 - aeps))
1752 return;
1753 double st[27];
1754 for (int dz = -1; dz <= 1; ++dz)
1755 for (int dy = -1; dy <= 1; ++dy)
1756 for (int dx = -1; dx <= 1; ++dx)
1757 st[vof::plicSt(dx + 1, dy + 1, dz + 1)] = c(i + dx + dy * sy + dz * sz);
1758 double m[3];
1759 vof::mycNormal(st, m);
1760 const double al = vof::plicAlpha(m[0], m[1], m[2], ci);
1761 double v[8][3], ctr[3], ar = 0.0;
1762 const int nv = vof::plicPolygon(m[0], m[1], m[2], al, v);
1763 vof::polygonAreaCentroid(v, nv, ctr, ar);
1764 acc += ar;
1765 },
1766 a);
1767 Kokkos::fence();
1768 (void)hh;
1769 return a; // CELL units squared, matching `volume`'s cell volumes
1770 }
1771
1773 void measure(VofBlock& b, double dt) {
1774 const I3 e = b.adv_.extent(), n = b.adv_.inner(), o = b.box.origin();
1775 const int g = ghost_;
1776 const double hh = h_;
1777 SField c = b.adv_.colour();
1778 using MD = MDRange3<SExec>;
1779 MD pol(SExec(), {g, g, g}, {g + n.x, g + n.y, g + n.z});
1780 double v = 0.0, mx = 0.0, my = 0.0, mz = 0.0;
1781 Kokkos::parallel_reduce(
1782 "vof::block::moments1", pol,
1783 KOKKOS_LAMBDA(int x, int y, int z, double& av, double& ax, double& ay, double& az) {
1784 const double q = c(L3(x, y, z, e));
1785 av += q;
1786 ax += q * (x - g + o.x + 0.5) * hh;
1787 ay += q * (y - g + o.y + 0.5) * hh;
1788 az += q * (z - g + o.z + 0.5) * hh;
1789 },
1790 v, mx, my, mz);
1791 Kokkos::fence();
1792 b.st_.id = b.id;
1793 b.st_.master = b.master;
1794 for (int d = 0; d < 3; ++d) {
1795 b.st_.lo[d] = b.box.lo[d];
1796 b.st_.hi[d] = b.box.hi[d];
1797 }
1798 b.st_.cells = b.box.cells();
1799 b.st_.volume = v;
1800 if (v <= 0.0) {
1801 b.st_.area = 0.0;
1802 for (int d = 0; d < 3; ++d)
1803 b.st_.centroid[d] = b.st_.velocity[d] = 0.0;
1804 for (int d = 0; d < 6; ++d)
1805 b.st_.moment[d] = 0.0;
1806 return;
1807 }
1808 const double cx = mx / v, cy = my / v, cz = mz / v;
1809 double sxx = 0, syy = 0, szz = 0, sxy = 0, sxz = 0, syz = 0;
1810 Kokkos::parallel_reduce(
1811 "vof::block::moments2", pol,
1812 KOKKOS_LAMBDA(int x, int y, int z, double& axx, double& ayy, double& azz, double& axy,
1813 double& axz, double& ayz) {
1814 const double q = c(L3(x, y, z, e));
1815 const double px = (x - g + o.x + 0.5) * hh - cx, py = (y - g + o.y + 0.5) * hh - cy,
1816 pz = (z - g + o.z + 0.5) * hh - cz;
1817 axx += q * px * px;
1818 ayy += q * py * py;
1819 azz += q * pz * pz;
1820 axy += q * px * py;
1821 axz += q * px * pz;
1822 ayz += q * py * pz;
1823 },
1824 sxx, syy, szz, sxy, sxz, syz);
1825 Kokkos::fence();
1826 const double nc[3] = {cx, cy, cz};
1827 for (int d = 0; d < 3; ++d) {
1828 b.st_.velocity[d] = (b.hasPrev_ && dt > 0.0) ? (nc[d] - b.prevCentroid_[d]) / dt : 0.0;
1829 b.prevCentroid_[d] = nc[d];
1830 b.st_.centroid[d] = nc[d];
1831 }
1832 b.hasPrev_ = true;
1833 b.st_.area = interfaceArea(b);
1834 const double iv = 1.0 / v;
1835 b.st_.moment[0] = sxx * iv;
1836 b.st_.moment[1] = syy * iv;
1837 b.st_.moment[2] = szz * iv;
1838 b.st_.moment[3] = sxy * iv;
1839 b.st_.moment[4] = sxz * iv;
1840 b.st_.moment[5] = syz * iv;
1841 }
1842
1843 // ---- C1: the batched container stages (`vof/block_batch.hpp`) -------------------------------
1844
1848 bool csfBatchEligible() const {
1849 if (!batchEligible())
1850 return false;
1851 for (const auto& b : blocks_) {
1852 if (!b.mine_)
1853 continue;
1854 if (!b.curv_.ready() || !b.curv_.useWorklist || b.curv_.timingOn || !b.f_[0].extent(0))
1855 return false;
1856 }
1857 return true;
1858 }
1859
1868 std::vector<VofBlock*> mb;
1869 for (auto& b : blocks_)
1870 if (b.mine_)
1871 mb.push_back(&b);
1872 const std::size_t nb = mb.size();
1873 if (nb == 0)
1874 return;
1875 std::vector<VofCurvJob> jobs(nb);
1876 bool anyClip = false;
1877 for (std::size_t k = 0; k < nb; ++k) {
1878 VofBlock& b = *mb[k];
1879 const VofCurvature& cv = b.curv_;
1880 const VofCurvFallbackJob fj = cv.fallbackJob(b.adv_.colour());
1881 VofCurvJob J;
1882 J.c = fj.c;
1883 J.mx = fj.mx;
1884 J.my = fj.my;
1885 J.mz = fj.mz;
1886 J.al = fj.al;
1887 J.kap = fj.kap;
1888 J.br = fj.br;
1889 J.slot = cv.slotMap().data();
1890 for (int c = 0; c < 3; ++c)
1891 J.f[c] = b.f_[c].data();
1892 J.e = cv.extent();
1893 J.n = cv.inner();
1894 J.g = cv.ghost();
1895 J.ieps = cv.interfaceEps;
1896 J.mtol = cv.monoTol;
1897 J.ptW = cv.ptWeightWidth * cv.metric.maxH(); // heightPass's `ptW`
1898 J.peps = cv.pureEps;
1899 J.dW = fj.dW; // fallbackPass's `weightWidth * metric.maxH()`
1900 J.cmin = fj.cmin;
1901 J.km = cv.kappaClipValue();
1905 J.gm = cv.metric;
1906 anyClip = anyClip || (J.km > 0.0);
1907 jobs[k] = J;
1908 }
1909 if (static_cast<std::size_t>(cStartG_.extent(0)) < nb) {
1910 cStartG_ = LField("vof::block::batch_curv_sG", nb);
1911 cEndG_ = LField("vof::block::batch_curv_eG", nb);
1912 cStartI_ = LField("vof::block::batch_curv_sI", nb);
1913 cEndI_ = LField("vof::block::batch_curv_eI", nb);
1914 cCnt_ = LField("vof::block::batch_curv_counts", 8 * nb);
1915 cCntHost_ = Kokkos::create_mirror_view(cCnt_);
1916 }
1917 Kokkos::deep_copy(SExec(), cCnt_, 0L);
1918 const double wgt[3] = {metric_.h[0] * metric_.h[0], metric_.h[1] * metric_.h[1],
1919 metric_.h[2] * metric_.h[2]}; // buildCsfForce's `1/h_a'^2` weights
1920 for (std::size_t j0 = 0; j0 < nb; j0 += kVofBlockBatch) {
1921 VofCurvTable T;
1922 T.nj = static_cast<int>(std::min<std::size_t>(kVofBlockBatch, nb - j0));
1923 T.base = static_cast<int>(j0);
1924 for (int k = 0; k < T.nj; ++k)
1925 T.job[k] = jobs[j0 + k];
1926 const long rg = vofSetOffsets(T, vofCurvGrown);
1927 if (static_cast<long>(cListG_.extent(0)) < rg)
1928 cListG_ = LField(
1929 Kokkos::view_alloc(std::string("vof::block::batch_curv_listG"), Kokkos::WithoutInitializing),
1930 rg);
1931 // design G §5.2: the chunk's moment cache, sized and reallocated exactly like cListG_ (the
1932 // counts stay on the device); its positions are the slots, hence the int guard
1933 if (rg >= (1L << 31))
1934 throw std::runtime_error(
1935 "peclet::flow::vof::VofBlockSet: grown list capacity >= 2^31 (the slot map is int)");
1936 if (static_cast<long>(cCacheG_.extent(0)) < rg)
1937 cCacheG_ = Kokkos::View<PvMoments*, SMem>(
1938 Kokkos::view_alloc(std::string("vof::block::batch_curv_cacheG"), Kokkos::WithoutInitializing),
1939 rg);
1940 T.cache = cCacheG_.data();
1941 vofCurvCompact(T, true, cListG_, cStartG_, cEndG_);
1943 vofCurvPlanesList(T, cListG_, cStartG_, cEndG_);
1944 const long ri = vofSetOffsets(T, vofCurvInner);
1945 if (static_cast<long>(cListI_.extent(0)) < ri)
1946 cListI_ = LField(
1947 Kokkos::view_alloc(std::string("vof::block::batch_curv_listI"), Kokkos::WithoutInitializing),
1948 ri);
1949 vofCurvCompact(T, false, cListI_, cStartI_, cEndI_);
1950 vofCurvHfReset(T);
1951 vofCurvListPasses(T, anyClip, cListI_, cStartI_, cEndI_, cCnt_); // tiers 1-3, clip, census
1952 vofCsfForceBatch(T, sigma, wgt[0], wgt[1], wgt[2]);
1953 }
1954 // the one read of the curvature stage (S4: instance-scoped copy + fence, not global)
1955 Kokkos::deep_copy(SExec(), cCntHost_, cCnt_);
1956 SExec().fence("vof::block::curv_census_read");
1957 for (std::size_t k = 0; k < nb; ++k) {
1958 const long* ct = &cCntHost_(8 * k);
1959 curvStats_.interfacial += ct[0];
1960 curvStats_.hf += ct[1];
1961 curvStats_.hfMixed += ct[2];
1962 curvStats_.hfFit += ct[3];
1963 curvStats_.pv += ct[4];
1964 curvStats_.pvReduced += ct[5];
1965 curvStats_.noEstimate += ct[6];
1966 curvStats_.clipped += ct[7];
1967 }
1968 }
1969
1974 bool batchEligible() const {
1975 if (!batched)
1976 return false;
1977 for (const auto& b : blocks_) {
1978 if (!b.mine_)
1979 continue;
1980 const WyAdvector& a = b.adv_;
1981 if (!b.allocated_ || a.hasGeometry() || a.debugRecomputeDilation || !a.useWorklist ||
1982 a.timingOn || a.globalMax || !a.exchange || a.ghost() != ghost_)
1983 return false;
1984 }
1985 return true;
1986 }
1987
1989 VofBlockJob makeJob(VofBlock& b, double dt) const {
1990 WyAdvector& a = b.adv_;
1991 VofBlockJob J;
1992 J.c = a.colour().data();
1993 J.mx = a.planeM(0).data();
1994 J.my = a.planeM(1).data();
1995 J.mz = a.planeM(2).data();
1996 J.al = a.planeAlpha().data();
1997 J.fl = a.faceFlux().data();
1998 for (int d = 0; d < 3; ++d)
1999 J.u[d] = a.faceVel(d).data();
2000 J.cc = a.dilationFlag().data();
2001 J.outside = a.hasOutsideMask() ? a.outsideMask().data() : nullptr;
2002 J.e = a.extent();
2003 J.n = a.inner();
2004 J.o = b.box.origin();
2005 J.g = a.ghost();
2006 J.dth = dt / a.h(); // WyAdvector::advect's `dth`
2007 J.weps = a.wispEps;
2008 J.interfaceCfl = a.interfaceLocalCfl;
2009 const int L[3] = {gs_.x, gs_.y, gs_.z};
2010 for (int d = 0; d < 3; ++d)
2011 J.span[d] = per_[d] && b.box.n(d) == L[d];
2012 for (int f = 0; f < 6; ++f)
2013 J.bcOwn[f] = a.bcFaceOwned(f) ? 1u : 0u;
2014 return J;
2015 }
2016
2017 static VofBlockTable batchTable(const std::vector<VofBlockJob>& jobs, std::size_t j0) {
2018 VofBlockTable T;
2019 T.nj = static_cast<int>(std::min<std::size_t>(kVofBlockBatch, jobs.size() - j0));
2020 T.base = static_cast<int>(j0);
2021 for (int k = 0; k < T.nj; ++k)
2022 T.job[k] = jobs[j0 + k];
2023 return T;
2024 }
2025
2031 for (int a = 0; a < 3; ++a) {
2032 VofBlockTable P;
2033 P.nj = 0;
2034 P.base = T.base;
2035 for (int k = 0; k < T.nj; ++k)
2036 if (T.job[k].span[a])
2037 P.job[P.nj++] = T.job[k];
2038 if (P.nj == 0)
2039 continue;
2040 const int b = (a + 1) % 3, c = (a + 2) % 3;
2041 vofSetOffsets(P, [b, c](const VofBlockJob& J) {
2042 const int dims[3] = {J.e.x, J.e.y, J.e.z};
2043 return static_cast<long>(dims[b]) * dims[c];
2044 });
2045 vofBatchPeriodic(P, a);
2046 }
2047 if (!per_[0] || !per_[1] || !per_[2])
2048 vofBatchClamp(T, gs_, per_[0], per_[1], per_[2]);
2049 }
2050
2053 void ensureRead1(std::size_t len) {
2054 if (static_cast<std::size_t>(bRead1_.extent(0)) >= len)
2055 return;
2056 if (bRead1_.extent(0) == 0)
2057 bRead1_ = SField("vof::block::batch_read1", len);
2058 else
2059 Kokkos::resize(bRead1_, len);
2060 bRead1Host_ = Kokkos::create_mirror_view(bRead1_);
2061 }
2062
2070 void measureBatched(double dt) {
2071 flushStats();
2072 std::vector<std::size_t> idx;
2073 for (std::size_t k = 0; k < blocks_.size(); ++k)
2074 if (blocks_[k].mine_)
2075 idx.push_back(k);
2076 const std::size_t nb = idx.size();
2077 if (nb == 0)
2078 return;
2079 std::vector<VofBlockJob> jobs(nb);
2080 for (std::size_t k = 0; k < nb; ++k) {
2081 VofBlock& b = blocks_[idx[k]];
2082 jobs[k] = makeJob(b, 0.0);
2083 b.st_.id = b.id;
2084 b.st_.master = b.master;
2085 for (int d = 0; d < 3; ++d) {
2086 b.st_.lo[d] = b.box.lo[d];
2087 b.st_.hi[d] = b.box.hi[d];
2088 }
2089 b.st_.cells = b.box.cells();
2090 }
2091 ensureRead1(kVofStats * nb);
2092 for (std::size_t j0 = 0; j0 < nb; j0 += kVofBlockBatch) {
2093 const VofBlockTable T = batchTable(jobs, j0);
2094 VofStatsPrev P;
2095 for (int k = 0; k < T.nj; ++k) {
2096 const VofBlock& b = blocks_[idx[j0 + k]];
2097 for (int d = 0; d < 3; ++d)
2098 P.c[k][d] = b.prevCentroid_[d];
2099 P.valid[k] = b.hasPrev_ ? 1u : 0u;
2100 }
2101 vofBatchStats(T, P, h_, areaEps, dt, bRead1_, 0, bTile_);
2102 }
2103 statsPend_ = std::move(idx);
2104 }
2105
2108 void applyStats(const double* hs) {
2109 for (std::size_t k = 0; k < statsPend_.size(); ++k) {
2110 VofBlock& b = blocks_[statsPend_[k]];
2111 const double* q = hs + kVofStats * k;
2112 b.st_.volume = q[0];
2113 if (q[0] <= 0.0) {
2114 b.st_.area = 0.0;
2115 for (int d = 0; d < 3; ++d)
2116 b.st_.centroid[d] = b.st_.velocity[d] = 0.0;
2117 for (int d = 0; d < 6; ++d)
2118 b.st_.moment[d] = 0.0;
2119 continue;
2120 }
2121 for (int d = 0; d < 3; ++d) {
2122 b.st_.velocity[d] = q[4 + d];
2123 b.prevCentroid_[d] = q[1 + d];
2124 b.st_.centroid[d] = q[1 + d];
2125 }
2126 b.hasPrev_ = true;
2127 b.st_.area = q[7];
2128 for (int d = 0; d < 6; ++d)
2129 b.st_.moment[d] = q[8 + d];
2130 }
2131 statsPend_.clear();
2132 }
2133
2136 void flushStats() {
2137 if (statsPend_.empty())
2138 return;
2139 const std::size_t len = kVofStats * statsPend_.size();
2140 Kokkos::deep_copy(Kokkos::subview(bRead1Host_, std::make_pair(std::size_t(0), len)),
2141 Kokkos::subview(bRead1_, std::make_pair(std::size_t(0), len)));
2142 applyStats(bRead1Host_.data());
2143 }
2144
2150 void advectBatched(double dt) {
2151 std::vector<VofBlock*> mb;
2152 for (auto& b : blocks_)
2153 if (b.mine_)
2154 mb.push_back(&b);
2155 if (mb.empty()) {
2156 flushStats();
2157 return;
2158 }
2159 const std::size_t nb = mb.size();
2160 std::vector<VofBlockJob> jobs(nb);
2161 bool anyMask = false;
2162 for (std::size_t k = 0; k < nb; ++k) {
2163 jobs[k] = makeJob(*mb[k], dt);
2164 anyMask = anyMask || (jobs[k].outside != nullptr);
2165 }
2166 if (static_cast<std::size_t>(bStart_.extent(0)) < nb) {
2167 bStart_ = LField("vof::block::batch_start", nb);
2168 bEnd_ = LField("vof::block::batch_end", nb);
2169 }
2170 // read #1's packet: the deferred statistics of the last batched measure (C2) first, then this
2171 // step's Courant numbers -- one device->host copy for both
2172 const std::size_t off = kVofStats * statsPend_.size();
2173 ensureRead1(off + nb);
2174 auto cfl = Kokkos::subview(bRead1_, std::make_pair(off, off + nb));
2175 ensurePacket(nb);
2176 // (1) Courant numbers: one launch, one read, the throw before any block moves (R4)
2177 for (std::size_t j0 = 0; j0 < nb; j0 += kVofBlockBatch)
2178 vofBatchCfl(batchTable(jobs, j0), cfl);
2179 // S4: instance-scoped copy + fence (an exec-less deep_copy drains every stream twice)
2180 Kokkos::deep_copy(SExec(),
2181 Kokkos::subview(bRead1Host_, std::make_pair(std::size_t(0), off + nb)),
2182 Kokkos::subview(bRead1_, std::make_pair(std::size_t(0), off + nb)));
2183 SExec().fence("vof::block::read1");
2184 applyStats(bRead1Host_.data());
2185 for (std::size_t k = 0; k < nb; ++k)
2186 mb[k]->adv_.enforceCfl(bRead1Host_(off + k), dt);
2187 Kokkos::deep_copy(SExec(), pk_, 0.0); // the WO-R ledger slots accumulate over the sweeps
2188 const int* perm = kWySweepPerm[static_cast<int>(step_ % 6)];
2189 for (std::size_t j0 = 0; j0 < nb; j0 += kVofBlockBatch) {
2190 VofBlockTable T = batchTable(jobs, j0);
2191 // (2) THE dilation flag, frozen once from C^n
2193 vofBatchFreeze(T);
2194 // (3) three directional sweeps
2195 for (int s = 0; s < 3; ++s) {
2196 const int d = perm[s];
2197 const long region = vofSetOffsets(T, vofJobGrown1);
2198 if (static_cast<long>(bList_.extent(0)) < region)
2199 bList_ = LField(Kokkos::view_alloc(std::string("vof::block::batch_list"),
2200 Kokkos::WithoutInitializing),
2201 region);
2202 vofBatchWorklist(T, bList_, bStart_, bEnd_);
2203 vofBatchPlic(T, bList_, bStart_, bEnd_);
2204 vofSetOffsets(T, [d](const VofBlockJob& J) { return vofJobFaces(J, d); });
2205 vofBatchFlux(T, d);
2206 if (anyMask)
2207 vofBatchBcLedger(T, d, pk_, kPk, kPkBcv);
2209 vofBatchUpdate(T, d);
2210 batchGhostFill(T);
2211 }
2212 }
2213 for (std::size_t k = 0; k < nb; ++k)
2214 mb[k]->adv_.noteBatchedStep();
2215 }
2216
2225 void debrisBatched(std::vector<VofBlock*>& bs) {
2226 const std::size_t nb = bs.size();
2227 if (nb == 0)
2228 return;
2230 F.doDebris = csfEnabled && debrisCensus;
2231 F.doResidue = wispEps > 0.0;
2232 F.remove = debrisRemove;
2233 F.vaMin = kDebrisMinAttached;
2234 // the predicates of debrisPassBatchImpl (review finding 7 there: the three lists are disjoint)
2235 const double weps = wispEps, cfull = kDebrisMaxC;
2236 const double ieps = curvProto.interfaceEps > weps ? curvProto.interfaceEps : weps;
2237 std::vector<VofBlockJob> jobs(nb);
2238 for (std::size_t k = 0; k < nb; ++k) {
2239 bs[k]->st_.debrisCells = 0;
2240 bs[k]->st_.debrisVolume = 0.0;
2241 jobs[k] = makeJob(*bs[k], 0.0);
2242 }
2243 if (static_cast<std::size_t>(dCap_.extent(0)) < nb) {
2244 for (int q = 0; q < 6; ++q)
2245 dRange_[q] = LField("vof::block::batch_debris_range", nb);
2246 dCap_ = Kokkos::View<int*, SMem>("vof::block::batch_debris_cap", nb);
2247 }
2248 ensurePacket(nb);
2249 Kokkos::deep_copy(SExec(), dCap_, 0);
2250 const LField sD = dRange_[0], eD = dRange_[1], sR = dRange_[2], eR = dRange_[3],
2251 sA = dRange_[4], eA = dRange_[5];
2252 for (std::size_t j0 = 0; j0 < nb; j0 += kVofBlockBatch) {
2253 VofBlockTable T = batchTable(jobs, j0);
2254 const long region = vofSetOffsets(T, vofJobInner);
2255 auto ensure = [&](LField& f, const char* name) {
2256 if (static_cast<long>(f.extent(0)) < region)
2257 f = LField(Kokkos::view_alloc(std::string(name), Kokkos::WithoutInitializing), region);
2258 };
2259 if (F.doDebris) {
2260 ensure(listD_, "vof::block::debris_list");
2261 vofBatchMark(T, 0, ieps, cfull, weps, listD_, sD, eD);
2262 }
2263 if (F.doResidue) {
2264 ensure(listR_, "vof::block::residue_list");
2265 vofBatchMark(T, 1, ieps, cfull, weps, listR_, sR, eR);
2266 }
2267 ensure(listA_, "vof::block::attached_list");
2268 vofBatchMark(T, 2, ieps, cfull, weps, listA_, sA, eA);
2269 vofBatchDebrisSums(T, F, listD_, listR_, listA_, sD, eD, sR, eR, sA, eA, pk_, kPk, 0);
2270 if (static_cast<long>(debrisEx_.extent(0)) < region)
2271 debrisEx_ = SField(
2272 Kokkos::view_alloc("vof::block::debris_excess", Kokkos::WithoutInitializing), region);
2273 vofBatchDebrisWrite(T, F, listD_, listR_, listA_, sD, sR, sA, eA, pk_, kPk, 0, debrisEx_,
2274 dCap_);
2275 vofBatchDebrisLost(T, sA, eA, pk_, kPk, 0, debrisEx_, dCap_);
2276 batchGhostFill(T); // the inner colour moved; the next reader expects valid ghosts
2277 }
2278 }
2279
2281 void applyDebrisLedger(std::vector<VofBlock*>& bs, bool doDebris) {
2282 for (std::size_t k = 0; k < bs.size(); ++k) {
2283 VofBlockStats& st = bs[k]->st_;
2284 const double* ho = &pkHost_(kPk * k);
2285 const long nD = static_cast<long>(ho[kDbND]);
2286 const bool removeD = doDebris && debrisRemove && nD > 0;
2287 const bool acted = ho[kDbActed] != 0.0;
2288 if (ho[kDbPending] == 0.0) {
2289 if (doDebris && nD > 0) { // census only: report what WOULD be removed
2290 st.debrisCells = nD;
2291 st.debrisVolume = ho[kDbSumD];
2292 }
2293 continue; // nothing to remove: nothing else ran
2294 }
2295 if (doDebris && !debrisRemove && nD > 0) { // census of what WOULD be removed
2296 st.debrisCells = nD;
2297 st.debrisVolume = ho[kDbSumD];
2298 }
2299 if (!acted) { // step 3: no attached interface worth a cell -- the clip bounds it
2300 if (removeD)
2301 st.debrisUnresolved += nD;
2302 continue;
2303 }
2304 if (removeD) {
2305 st.debrisCells = nD;
2306 st.debrisVolume = ho[kDbSumD];
2307 st.debrisReturned += ho[kDbSumD];
2308 }
2309 st.residueReturned += ho[kDbSumR];
2310 st.debrisLost += ho[kDbDV]; // `lost`, written over the dV slot by the act
2311 }
2312 }
2313
2316 static constexpr int kPkBox = kDbSlots, kPkBcv = kDbSlots + 6, kPk = kDbSlots + 12;
2317 void ensurePacket(std::size_t nb) {
2318 if (static_cast<std::size_t>(pk_.extent(0)) < kPk * nb) {
2319 pk_ = SField("vof::block::batch_packet", kPk * nb);
2320 pkHost_ = Kokkos::create_mirror_view(pk_);
2321 }
2322 }
2323
2328 void recentreBatched(bool ranDebris) {
2329 std::vector<std::size_t> idx;
2330 std::vector<VofBlock*> mb;
2331 for (std::size_t k = 0; k < blocks_.size(); ++k)
2332 if (blocks_[k].mine_) {
2333 idx.push_back(k);
2334 mb.push_back(&blocks_[k]);
2335 }
2336 const std::size_t nb = idx.size();
2337 if (nb == 0)
2338 return;
2339 std::vector<VofBlockJob> jobs(nb);
2340 for (std::size_t k = 0; k < nb; ++k)
2341 jobs[k] = makeJob(*mb[k], 0.0);
2342 ensurePacket(nb);
2343 const double eps = bubbleEps > wispEps ? bubbleEps : wispEps; // bubbleBox's threshold (§12.2)
2344 for (std::size_t j0 = 0; j0 < nb; j0 += kVofBlockBatch)
2345 vofBatchBox(batchTable(jobs, j0), eps, pk_, kPk, kPkBox, bTile_);
2346 // S4b (doc/vof_gpu_coarse_tail_design.md §5.5): read #2 as read #1 -- instance-scoped copy +
2347 // fence (an exec-less deep_copy drains every stream twice); pk_'s producers are all on SExec()
2348 Kokkos::deep_copy(SExec(), pkHost_, pk_); // read #2
2349 SExec().fence("vof::block::read2");
2350 if (ranDebris)
2352 for (std::size_t k = 0; k < nb; ++k) { // the WO-R ledger (no block installs a mask today)
2353 if (jobs[k].outside == nullptr)
2354 continue;
2355 for (int f = 0; f < 6; ++f)
2356 if (jobs[k].bcOwn[f])
2357 mb[k]->adv_.addBcFaceVolume(f, pkHost_(kPk * k + kPkBcv + f));
2358 }
2359 for (std::size_t k = 0; k < nb; ++k) {
2360 const double* hb = &pkHost_(kPk * k + kPkBox);
2361 const int lo[3] = {static_cast<int>(hb[0]), static_cast<int>(hb[1]), static_cast<int>(hb[2])};
2362 const int hi[3] = {static_cast<int>(hb[3]), static_cast<int>(hb[4]), static_cast<int>(hb[5])};
2363 if (hi[0] < lo[0])
2364 continue; // the block is empty (bubbleBox returned false)
2365 VofBlock& b = *mb[k];
2366 VofBox bb;
2367 for (int d = 0; d < 3; ++d) {
2368 bb.lo[d] = lo[d] + b.box.lo[d];
2369 bb.hi[d] = hi[d] + 1 + b.box.lo[d];
2370 }
2371 recentreTo(idx[k], bb);
2372 }
2373 }
2374
2375 private:
2376 VofMetric metric_;
2377 double h_ = 1.0;
2378 I3 gs_{0, 0, 0};
2379 std::array<bool, 3> per_{true, true, true};
2380 int rank_ = 0, size_ = 1;
2381 int margin_ =
2382 3;
2383 int ghost_ = 3;
2384 long step_ = 0;
2385 std::vector<VofBlock> blocks_;
2386 std::shared_ptr<VofBlockExchangeBase> exch_;
2387 std::map<std::array<int, 3>, std::vector<WyAdvector>> pool_;
2388 long poolHits_ = 0, poolMisses_ = 0;
2389 long lastReassigned_ = 0;
2390 VofCurvature::Stats curvStats_{};
2391 LField listD_, listA_, listR_;
2392 SField debrisEx_;
2393 SField debrisOut_;
2394 SField::host_mirror_type debrisOutHost_;
2395 // C1 batched stages: per-job device scalars (indexed by the job's place among the master blocks)
2396 SField bRead1_;
2397 SField::host_mirror_type bRead1Host_;
2398 std::vector<std::size_t> statsPend_;
2399 LField bList_, bStart_, bEnd_;
2400 LField dRange_[6];
2401 Kokkos::View<int*, SMem> dCap_;
2402 LField cListG_, cListI_, cStartG_, cEndG_, cStartI_, cEndI_;
2403 Kokkos::View<PvMoments*, SMem> cCacheG_;
2404 LField cCnt_;
2405 LField::host_mirror_type cCntHost_;
2406 SField pk_;
2407 SField::host_mirror_type pkHost_;
2408 SField bTile_;
2409};
2410
2411} // namespace peclet::flow::vof
2412
2413#endif // PECLET_FLOW_VOF_BLOCK_CONTAINER_HPP
flow — VoF rung V1: Weymouth & Yue (2010) directionally-split, exactly volume-conserving geometric ad...
flow — batched execution of the VoF block container's stages (C1, doc/vof_step_performance_design....
The container: a replicated table of blocks, the per-step orchestration (gather → advect → re-centre ...
Curvature of the colour field on an extended (inner + ghost) block.
Weymouth-Yue split advection of a colour field on an extended (inner + ghost) block.
The container: a replicated table of blocks, the per-step orchestration (gather → advect → re-centre ...
void scatterSum(SField sLocal)
S = sum_k C_k of the CURRENT (replicated) table into sLocal's inner region (§5.6).
VofBlockJob makeJob(VofBlock &b, double dt) const
The raw-pointer job of one master block for the batched launches.
VofCurvature::Stats csfCurvatureStats() const
long assignMasters()
Apply plannedMasters(): allocate on the gaining ranks, migrate the colour, free on the losing ranks.
bool bubbleBox(const VofBlock &b, VofBox &out) const
Tight bounding box of C > 0 over the inner region, in GLOBAL indices.
void recentreTo(std::size_t idx, const VofBox &bb)
recentre for a bubble box already measured (the batched path's read #2).
void enableCsf(double sigmaValue)
Turn on the block CSF.
void recentre(std::size_t idx)
Move / resize the block when the bubble has spent its margin (or when the box has grown wastefully la...
double bubbleEps
The colour threshold that defines the BUBBLE EXTENT (and hence the box).
void applyDebrisLedger(std::vector< VofBlock * > &bs, bool doDebris)
The host half of the debris pass: the ledger of debrisPassBatchImpl, from the packet.
void setExchange(std::shared_ptr< VofBlockExchangeBase > x)
VofBlockExchangeBase * exchange() const
long lastReassigned() const
Blocks that changed master at the last advect() (rung W1 item a).
static constexpr double kDebrisMaxC
Debris predicate threshold (doc/vof_overlap_design.md §11): an interfacial cell is debris when NO cel...
void debrisBatched(std::vector< VofBlock * > &bs)
Stage 4 of §5.9, the debris/residue pass of debrisPassBatchImpl on every block of bs in batched launc...
int recentrePad
Cells of slack added beyond the margin when a block is re-centred, so a translating bubble re-allocat...
void flushStats()
Bring deferred statistics to the host now (a separate read; the step itself folds it into read #1).
void debrisPassBatchImpl(std::vector< VofBlock * > &bs)
bool csfEnabled
Rung W2: form the CSF face force on each block (its own curvature cascade, the V4 balanced -force rul...
bool batched
C1 (doc/vof_step_performance_design.md §4.6, §5.9): run each stage of the container step for ALL mast...
double interfaceArea(const VofBlock &b) const
Rung W1 item (d): the marker's interface area, in CELL units squared (the same convention volume uses...
std::vector< VofBlock > & blocks()
The block table.
void debrisPassBatch(std::vector< VofBlock * > &bs, bool deferLedger=false)
The pass on several master blocks at once.
bool timingOn
Instrument: fence-bounded wall time of the debris/residue pass (all blocks, all calls) while timingOn...
void debrisPass(VofBlock &b)
Debris + residue pass on one master block (doc/vof_overlap_design.md §5.2-5.3, predicate of §11,...
double areaEps
Wisp guard on the INTERFACE-AREA predicate (VofBlockStats::area), the same threshold and the same rea...
void computeCsf(SField fx, SField fy, SField fz)
Per-block curvature cascade + the CSF face force, summed into the caller's three face-force patches.
void debrisApply(const std::vector< DebrisPending > &pend, const std::vector< long > &off, bool doDebris, long np)
debrisPassBatchImpl, steps 3-5 per acting block: the per-cell writes and the ledger.
void clipToSeed(VofBlock &b)
A seeded block's colour is its OWN bubble's: the gather copies the whole INNER box (bubble extent + m...
std::array< bool, 3 > periodic() const
VofCurvature curvProto
The curvature cascade's TUNABLES for every block, copied into each block's own VofCurvature when it i...
double sigma
surface-tension coefficient of the block CSF (cell units)
std::vector< double > blockColourHost(std::size_t idx)
One block's INNER colour, copied to the host in x-fastest order over blocks_[idx]....
void setKappaMax(double km)
Set the curvature admissibility clip (VofCurvature::kappaMax, index units) on the prototype AND on ev...
void seedSphere(double cx, double cy, double cz, double r, int subLevels=4, int extraPad=0)
Seed a spherical bubble (centre and radius in CELL units, global).
void finishSeeding(SField cLocal)
Gather the colour of every seedBox-seeded block from cLocal, fill its ghosts and measure.
bool debrisCensus
Run the debris pass in advect() (only while the block CSF is on – a kinematic block run never pays fo...
void advectBatched(double dt)
Stages 1-3 of §5.9 for every master block: the Courant numbers in one launch and ONE host read (the t...
bool debrisRemove
Remove the debris and return its volume (§5.3).
void seedBoxWithColour(const VofBox &bb, const std::vector< double > &colour)
Seed a block whose INNER box is EXACTLY bb (not grown by the margin: bb is what blockColourHost was p...
void buildCsfForce(VofBlock &b)
The V4 balanced-force CSF on the BLOCK's faces — the same csfFaceCurvature + csfFaceForce pair Solver...
void seedBox(const VofBox &bb, int extraPad=0)
Seed one block on the global index box bb (the BUBBLE extent; the inner box is bb grown by margin),...
void recentreBatched(bool ranDebris)
Stage 5 of §5.9 on the batched path: every master block's bubble box in one launch,...
const std::vector< VofBlock > & blocks() const
void init(I3 gs, std::array< bool, 3 > per, int rank, int size, double h=1.0)
void masterCensus(std::vector< long > &masters, std::vector< long > &cells) const
Load-balance census of the CURRENT master assignment: masters[r] = blocks mastered by rank r,...
void ensureRead1(std::size_t len)
Grow read #1's packet to len doubles, KEEPING its contents (deferred statistics may wait in the front...
const VofMetric & metric() const
void measureBatched(double dt)
C2 (§5.10): measure() for every master block batched (vofBatchStats), with NO host read – the values ...
double cellImbalance() const
max/mean of the per-rank block-cell load (1.0 = perfect). 0 blocks -> 1.0.
static VofCurvature blockCurvProto()
The block prototype: the class defaults, plus the admissibility clip ON at 1/Delta_min (doc/vof_overl...
void measure(VofBlock &b, double dt)
Volume, centroid, centroid velocity and the central second moments over the inner box.
void applyStats(const double *hs)
Apply the deferred statistics in hs (host, kVofStats doubles per pending block) exactly as measure() ...
void scatter(SField cLocal)
Union the current colour into cLocal without advecting (used right after seeding).
void advect(double dt, SField cLocal)
One kinematic block step: gather the face velocity, advect every local block, re-centre,...
void setMetric(const VofMetric &g)
The anisotropic cell metric (Phase 3).
std::vector< VofBlockStats > statsAll()
Per-bubble census on the master (empty entries on a non-master).
int poolCapacity
advectors kept per extent; beyond it the retired one is freed
static constexpr double kDebrisMinAttached
Guard of the volume return (§5.3 step 3): a marker whose attached interface holds less than this much...
void fillBlockGhosts(VofBlock &b)
The block's own colour ghost policy — see the file header.
bool csfBatchEligible() const
The batched cascade applies where the container step is batched and every master block's cascade runs...
double outOfBoxSum(const VofBlock &b, const VofBox &nb) const
Sum of the block's colour over inner cells whose GLOBAL index falls outside nb.
std::vector< int > plannedMasters() const
The master each block WOULD get under assignMode, from the current block cell counts.
void computeCsfBatched()
Stage 6 of §5.9: the curvature cascade (compaction, planes, tiers 1-2, tier 3, the clip,...
double cflLimit
The Weymouth-Yue cap of every block advector.
double innerSum(const VofBlock &b) const
std::vector< double > blockKappaHost(std::size_t idx)
One marker's OWN curvature over its inner box, x-fastest (empty unless this rank masters it and the b...
VofMasterAssign assignMode
Rung W1 item (a): how masters are chosen, and how often the choice is revisited.
void setWispEps(double eps)
Set wispEps on the set AND on every live advector (pooled ones take it on reuse).
void batchGhostFill(VofBlockTable T)
fillBlockGhosts of every job of T, pass by pass in the per-block pass order: zero,...
bool usePool
Rung W1 item (c): recycle the advectors a re-centring retires, keyed by the EXACT extent.
static constexpr int kPkBox
The per-job packet of read #2 (§5.9): the debris slots (VofDebrisSlot), the bubble box,...
bool batchEligible() const
The batched path applies to the plain advect() configuration: every master block an uncut all-fluid a...
double wispEps
The Weymouth-Yue wisp tolerance of every block advector (WyAdvector::wispEps); the solver sets it to ...
double outOfBoxSum(const VofBlock &b, const VofBox &nb, const bool wrap[3]) const
... where an axis flagged in wrap spans the whole periodic domain in nb and is copied with the wrap: ...
bool allowShrink
Let a block SHRINK when it has grown much larger than the bubble needs.
static VofBlockTable batchTable(const std::vector< VofBlockJob > &jobs, std::size_t j0)
int masterOf(long id) const
Master assignment: round robin by block id (VOF_PLAN §10 W0; the weighted-ORB assignment is W1).
void debrisSums(const std::vector< DebrisJob > &jobs, long np)
debrisPassBatchImpl, step 2: the sums of every acting block (np jobs) in batched launches of one thre...
void allocateCsf(std::size_t idx)
VofBox extended(int ghost) const
const VofCurvature & curvature() const
void deserializeAux(const double in[kAuxLen])
void serializeAux(double out[kAuxLen]) const
static constexpr int kAuxLen
The non-colour state a master carries between steps: the previous centroid (so a migrated block's rep...
VofBox seed_
The BUBBLE extent this block was seeded from (empty for a sphere seed, which paints its own colour).
VofCurvature & curvature()
Rung W2: the block's own curvature cascade and the CSF face force it forms.
const WyAdvector & advector() const
VofBox box
the INNER box, global
const VofBlockStats & stats() const
Curvature of the colour field on an extended (inner + ghost) block.
double interfaceEps
Wisp threshold on the interfacial predicate.
double monoTol
Tolerance of the column monotonicity test (hfColumnHeight).
static void fallbackBatch(const std::vector< VofCurvFallbackJob > &jobs)
Tier 3 of many cascades in as few launches as possible: one kernel per kFallbackBatch jobs over their...
bool debugSingleDirection
DIAGNOSTIC ONLY — disable tier 2 (the non-preferred column directions), so the branch census separate...
double pureEps
Tolerance of the PURITY test that ends a height-function column (hfIsFull/hfIsEmpty),...
VofCurvFallbackJob fallbackJob(SField c) const
This cascade's tier-3 share for fallbackBatch (worklist mode only: the job walks the compacted interf...
bool useMixedHeightFit
Tier 2b, and it ships OFF — a measured decision, see the WO-O findings entry.
VofMetric metric
The anisotropic cell metric (Phase 3).
bool debugForceFallback
DIAGNOSTIC ONLY — disable tiers 1 and 2 so every interfacial cell goes to the PV fit.
IField slotMap() const
The slot map of the moment cache (design G §5.2), for the batched cascade, which writes it with posit...
double kappaClipValue() const
The bound the clip applies this call (0 = off).
double ptWeightWidth
Wendland support width of the tier-2b height-position fit, in cell units.
Weymouth-Yue split advection of a colour field on an extended (inner + ghost) block.
SField colour() const
Colour field on the extended block (x-fastest).
flow — VoF rung V2a (WO-J): the bridge between the solver's G = 2 block and the colour field's own g ...
flow — VoF rung V3 (WO-O): the view-level driver for the curvature cascade.
flow — portable (Kokkos) MAC stencil operators: Red-Black Gauss-Seidel smoothers + divergence.
void vofBatchBox(const VofBlockTable &T, double eps, SField out, int stride, int slot0, SField &tile)
The tight box of |C| > eps over each job's inner region, in the block's LOCAL inner frame (bubbleBox'...
long vofJobGrown1(const VofBlockJob &J)
The inner region grown by one cell per side: WyAdvector::reconstructImpl's range.
void vofCurvCompact(const VofCurvTable &T, bool grown, LField list, LField start, LField end)
VofCurvature::compact: the interfacial cells of the grown (grown) or inner region,...
bool vofDebrisCell(const SF &c, long i, long sy, long sz, double ieps, double cfull)
The debris predicate of doc/vof_overlap_design.md §11 on cell i: interfacial, and NO cell of the mark...
void vofBatchFlux(const VofBlockTable &T, int d)
WyAdvector::computeFluxesImpl (uncut): one flux per d-face, wyFaceFlux (or, for a job carrying the WO...
void vofCurvListPasses(const VofCurvTable &T, bool anyClip, LField list, LField start, LField end, LField cnt)
The whole cascade list stage of a chunk: tiers 1-2, tier 3, the clip (only if some job clips),...
void vofBatchCfl(const VofBlockTable &T, SField out)
One team per job: WyAdvector::maxCourant or maxCourantInterface (per the job's interfaceCfl),...
long vofSetOffsets(VofBlockTable &T, F size)
Fill T.off from a per-job range size.
void vofBatchClamp(const VofBlockTable &T, I3 gs, bool px, bool py, bool pz)
Pass 3 (colour_field.hpp::clampFill, skip = 0): the part of each extended block outside a NON-periodi...
void vofBatchPeriodic(const VofBlockTable &T, int a)
Pass 2 on axis a (colour_field.hpp::periodicFill, one axis): only the jobs whose block spans the peri...
constexpr int kWySweepPerm[6][3]
The six permutations of (x, y, z), cycled by step index so no direction is systematically favoured (p...
void vofBatchGhostZero(const VofBlockTable &T)
Pass 1: every ghost cell of every job -> 0.
void vofAssignOrb(const std::vector< long > &w, int size, std::vector< int > &master)
Weighted ORB over a 1-D block space: the blocks are laid out as a 1-D "grid" of N cells in block-id o...
constexpr int kVofStats
Doubles per job in the statistics packet: volume, centroid[3], velocity[3], area, moment[6].
constexpr int kVofBlockBatch
Jobs per launch; a container with more master blocks runs each stage in chunks.
@ kDbND
debris count
@ kDbActed
1 when the return ran (pending and VA >= kDebrisMinAttached and W > 0)
@ kDbSumR
sum_R C in list order (signed residue)
@ kDbPending
1 when there was something to remove (removeD or nR > 0)
@ kDbSumD
sum_D C in list order (the removed / census debris volume)
@ kDbDV
the volume to return (D if removing, then R); replaced by lost once acted
void vofBatchDebrisSums(const VofBlockTable &T, VofDebrisFlags F, LField listD, LField listR, LField listA, LField sD0, LField eD0, LField sR0, LField eR0, LField sA0, LField eA0, SField out, int stride, int slot0)
Step 2 of the pass: one thread per job, every sum in list order (the per-block kernel's single thread...
long vofJobFaces(const VofBlockJob &J, int d)
A sweep's face range of one job: the inner region with one extra layer on the low side of d.
void vofCurvHfReset(const VofCurvTable &T)
heightPass (worklist mode), part 1: every inner cell to kappa = 0, branch = kCurvNone.
long vofJobInner(const VofBlockJob &J)
void vofCurvPlanesList(const VofCurvTable &T, LField list, LField start, LField end)
Part 2: wyReconstructCell over the grown interfacial list (upper bound: the grown region),...
void vofBatchMark(const VofBlockTable &T, int kind, double ieps, double cfull, double weps, LField list, LField start, LField end)
One mark scan over the concatenated inner regions (T.off = vofJobInner), in each block's inner-box in...
void vofCsfForceBatch(const VofCurvTable &T, double sig, double w0, double w1, double w2)
VofBlockSet::buildCsfForce for every job, the three components per cell: the V4 balanced-force CSF at...
VofBox vofClampBox(VofBox b, I3 gs, const std::array< bool, 3 > &per)
Clamp a block box so it is representable: at most the whole axis on a periodic axis,...
Kokkos::View< long *, SMem > LField
Definition advect_wy.hpp:90
void vofBatchPlic(const VofBlockTable &T, LField list, LField start, LField end)
wyReconstructCell over each job's compacted list: launched over the upper bound (the job's region),...
void vofAssignLpt(const std::vector< long > &w, int size, std::vector< int > &master)
Longest-processing-time greedy (Graham 1969): heaviest block first onto the currently least-loaded ra...
void vofCurvPlanesZero(const VofCurvTable &T)
reconstructPlanes (worklist mode), part 1: zero the four plane fields and clear the slot map (design ...
void vofBatchDebrisLost(const VofBlockTable &T, LField sA0, LField eA0, SField out, int stride, int slot0, SField ex, Kokkos::View< int *, SMem > capf)
lost: the capped excess of an acted job summed in list order on one thread (only where a cap fired,...
int vofAxisRuns(int a, int b, int gs, bool periodic, VofRun out[5], int outside=0)
Decompose [a, b) on one axis into global runs.
void vofBatchBcLedger(const VofBlockTable &T, int d, SField out, int stride, int slot0)
The WO-R boundary ledger (WyAdvector::accumulateBcFaceVolume) of the jobs carrying the mask: one thre...
long vofJobLen(const VofBlockJob &J)
VofMasterAssign
How a block's MASTER rank is chosen.
@ Lpt
longest-processing-time greedy on the block cell counts (rung W1 default)
@ RoundRobin
id % size — rung W0's assignment; blind to block SIZE
@ WeightedOrb
weighted ORB over a 1-D "block space" (core::decomp::BlockDecomposer<1>)
bool vofAttachedCell(const SF &c, long i, long sy, long sz, double ieps, double cfull)
The ATTACHED predicate (§5.3 step 2): interfacial with a cell at or above cfull within two.
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 vofBatchWorklist(const VofBlockTable &T, LField list, LField start, LField end)
WyAdvector::reconstructImpl's worklist scan, over the concatenated grown-by-one regions (T....
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...
long vofCurvInner(const VofCurvJob &J)
void vofBatchStats(const VofBlockTable &T, const VofStatsPrev &P, double hh, double aeps, double dt, SField out, long off, SField &tile)
VofBlockSet::measure for every job: the first moments, then the central second moments and the PLIC i...
double csfFaceForce(double sigma, double kf, double dC, double hGrad)
The balanced-force CSF contribution to the momentum RHS at one staggered velocity unknown,...
void vofBatchFreeze(const VofBlockTable &T)
WyAdvector::freezeDilationFlag over every job's extended block.
void vofBatchUpdate(const VofBlockTable &T, int d)
WyAdvector::applySweepImpl (uncut) over the inner regions (T.off = vofJobInner); the expression verba...
bool vofResidueCell(double ci, double weps)
The residue predicate (§13): 0 < |C| <= wispEps.
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.
long vofCurvGrown(const VofCurvJob &J)
The inner region grown by kPvHalf: every cell VofCurvature::reconstructPlanes writes.
void vofBatchDebrisWrite(const VofBlockTable &T, VofDebrisFlags F, LField listD, LField listR, LField listA, LField sD0, LField sR0, LField sA0, LField eA0, SField out, int stride, int slot0, SField ex, Kokkos::View< int *, SMem > capf)
Step 4, the act: removed cells -> 0, attached C += dV C(1-C)/W capped at 1 (the per-block write kerne...
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
long L3(int x, int y, int z, I3 e)
Kokkos::DefaultExecutionSpace SExec
key
Definition sweep.py:20
flow — the multidimensional iteration policies: iteration order follows storage.
Per-call census of the cascade over this block's inner region (LOCAL; a distributed caller sums them ...
Gather / scatter / table replication.
virtual void scatterForceSum(std::vector< VofBlock > &blocks, SField fx, SField fy, SField fz)=0
SUM every master block's CSF face force into the caller's three face-force patches (TBFsolver's UNPAC...
virtual void migrateColour(std::vector< VofBlock > &blocks, const std::vector< int > &oldMaster)=0
Move a block's state from oldMaster[i] to blocks[i].master after a re-assignment.
virtual void scatterColourSum(std::vector< VofBlock > &, SField)
SUM every master block's inner colour into the caller's colour patch: S = sum_k C_k,...
virtual void scatterColourMax(std::vector< VofBlock > &blocks, SField cLocal)=0
UNION the masters' inner colour into the caller's local colour patch: zero the inner region,...
virtual void syncTable(std::vector< VofBlock > &blocks)=0
Replicate the (possibly re-centred) boxes so every rank's table agrees again.
virtual void gatherColour(std::vector< VofBlock > &blocks, SField cLocal)=0
Fill every MASTER block's INNER colour from the caller's colour patch (the mirror of scatterColourMax...
virtual long gatherBytes() const
Bytes moved by the last gather / scatter on THIS rank (sent + received).
virtual void gatherFaceVel(std::vector< VofBlock > &blocks, int ghost)=0
Fill every MASTER block's faceU/V/W over its EXTENDED box, from the face velocity the ranks own.
One master block's share of a batched container launch.
unsigned char * cc
the frozen dilation flag
double dth
dt / h of this block's advector
double weps
the advector's wisp tolerance
double * u[3]
face velocities uf, vf, wf
bool interfaceCfl
interfaceLocalCfl: maxCourantInterface, else maxCourant
bool span[3]
the block spans this globally periodic axis (wrap in-block)
I3 o
extended extent, inner counts, global index of inner (0,0,0)
const unsigned char * outside
the WO-R out-of-domain mask; null when there is none
unsigned char bcOwn[6]
WO-R: the global domain faces this block owns.
One block's share of the batched sums (raw pointers: the table rides in the functor).
One acting block of debrisPassBatchImpl: its lists' lengths and whether D is removed.
Per-block Lagrangian census, computed on the master (VOF_PLAN §10 W1 outputs, available at W0).
long debrisUnresolved
cumulative debris cells left for want of an attached interface
double debrisReturned
cumulative sum of debrisVolume over the removals
double area
Interface area of the marker, in cell-size units squared: the sum of the PLIC polygon areas over the ...
double moment[6]
Central second moments Σ C (x−x̄)(y−ȳ)/Σ C, order xx, yy, zz, xy, xz, yz (the deformation).
long fullAxis
Cumulative re-centrings whose box had to span a whole periodic axis (vof_overlap_design §12....
long cells
inner-box cell count (the master's work)
double debrisLost
cumulative volume the return could not place (C capped at 1)
double debrisVolume
their colour, summed in list order (cell volumes), this step
double volume
Σ C over the inner box, in cell volumes.
double residueReturned
Cumulative SIGNED volume of the sub-wispEps residue cleared after each advection and returned to the ...
double discarded
Cumulative colour dropped by re-centrings, i.e.
bool recentred
the box moved at the end of the last step
double velocity[3]
d(centroid)/dt of the last advect
long debrisCells
Marker DEBRIS (doc/vof_overlap_design.md §5.2 as amended by §11): an interfacial cell (vofIsInterface...
The job table of one launch, by value.
Half-open global cell box [lo, hi).
bool operator!=(const VofBox &o) const
bool contains(const VofBox &o) const
static VofBox intersect(const VofBox &a, const VofBox &b)
Intersection in global coordinates (may be empty).
bool operator==(const VofBox &o) const
static VofBox grown(const VofBox &b, int m)
One block's share of a batched tier-3 launch (VofCurvature::fallbackBatch): its fields,...
One master block's cascade (VofCurvature, worklist mode) for the batched launches: its planes,...
double * f[3]
the block's CSF face force, per component
int * slot
the block's slot map (design G §5.2): absolute cListG_ positions
Per-call census of the cascade over this block's inner region (LOCAL; a distributed caller sums them ...
Flags of the pass, uniform over the blocks.
A raw-pointer field accessor with the View<double*> call syntax, for kernels that walk SEVERAL blocks...
One contiguous run of GLOBAL indices covered by a block's index range on one axis,...
int loc0
block-local index (0-based within [a, b)) the run starts at
int g0
first GLOBAL index of the run
What measure() reads besides the colour: each job's previous centroid and whether it is valid (the ho...
flow — VoF rung V4 (WO-P): balanced-force continuum surface force + the capillary Δt.
static int wrap(int a, int n)
static constexpr double F
static double perm(const std::vector< double > &gsdf, const BlockDecomposer< 3 > &dec, int rank, bool amg, int levels, MPI_Comm comm)
Kokkos::View< double *, SMem > SField
static std::vector< double > colour(int m)