flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_mg_vcycle_symmetry.cpp
Go to the documentation of this file.
1
26#include <cmath>
27#include <cstdio>
28#include <Kokkos_Core.hpp>
29#include <string>
30#include <vector>
31
32#include "mac_cutcell_mg.hpp"
33#include "mg_fp32_problems.hpp"
34
35using namespace peclet::flow;
36using namespace peclet_flow_fp32_problems;
38
39namespace {
40
41int fails = 0;
42void check(bool ok, const std::string& what) {
43 std::printf(" %-90s %s\n", what.c_str(), ok ? "PASS" : "FAIL");
44 std::fflush(stdout);
45 if (!ok)
46 ++fails;
47}
48std::string num(double v) {
49 char b[32];
50 std::snprintf(b, sizeof b, "%.3e", v);
51 return b;
52}
53
54constexpr double kBoundSmoothed = 1e-14, kBoundAgglomerated = 1e-8;
55constexpr int kCap = 500;
56constexpr double kRtol = 1e-10;
57
58// Deterministic pseudo-random value in [-1, 1) of a global cell key.
59double val(long key) {
60 unsigned long s = (unsigned long)key * 6364136223846793005ul + 1442695040888963407ul;
61 s ^= s >> 33;
62 s *= 0xff51afd7ed558ccdul;
63 s ^= s >> 33;
64 return (double)(s >> 11) / (double)(1ul << 52) - 1.0;
65}
66// A random level-0 vector (inner cells; ghosts 0), closed cells (AC <= 1e-30) zero, mean-free.
67CCField randomVector(CutcellMG& mg, int seed) {
68 Level& l = mg.level(0);
69 CCField f("v", l.n);
70 auto h = Kokkos::create_mirror_view(f);
71 auto ac = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), l.AC);
72 for (std::size_t i = 0; i < h.extent(0); ++i)
73 h(i) = 0.0;
74 for (int z = 0; z < l.inner.z; ++z)
75 for (int y = 0; y < l.inner.y; ++y)
76 for (int x = 0; x < l.inner.x; ++x) {
77 const long i =
78 (long)(x + l.g) + (long)(y + l.g) * l.ext.x + (long)(z + l.g) * (long)l.ext.x * l.ext.y;
79 h(i) = (double)ac(i) > 1e-30 ? val(seed * 7919L + x + 1000L * y + 1000000L * z) : 0.0;
80 }
81 Kokkos::deep_copy(f, h);
82 mg.removeMean(l, f);
83 return f;
84}
85// The drivers' smoothing schedule (2, 2, 12): a zero-iteration solve sets it.
86void setSchedule(CutcellMG& mg) {
87 const std::size_t n = mg.level(0).n;
88 CCField s0("s0", n), s1("s1", n), s2("s2", n), s3("s3", n), s4("s4", n), s5("s5", n);
89 mg.solvePCG(s0, s1, s2, s3, s4, s5, 0, 1e-8, 2, 2, 12);
90}
91// a of the V-cycle as `mg` is configured (FP32 when its precision is forced 'fp32').
92double asym(CutcellMG& mg, bool fp32) {
93 Level& l = mg.level(0);
94 setSchedule(mg);
95 CCField x = randomVector(mg, 11), y = randomVector(mg, 12), Mx("Mx", l.n), My("My", l.n);
96 if (fp32) {
97 mg.precondVcycleFp32(Mx, x, std::ilogb(mg.maxabs(l, x)), -1, CCField());
98 mg.precondVcycleFp32(My, y, std::ilogb(mg.maxabs(l, y)), -1, CCField());
99 } else {
100 mg.precondVcycle(Mx, x);
101 mg.precondVcycle(My, y);
102 }
103 const double a = mg.dot(l, x, My), b = mg.dot(l, y, Mx);
104 return std::abs(a - b) / (std::sqrt(mg.dot(l, x, x)) * std::sqrt(mg.dot(l, My, My)));
105}
106// Production solvePCG on rhsOf(P) (closed cells zero), zero guess.
107int pcgIters(CutcellMG& mg, const Problem& P) {
108 Level& l = mg.level(0);
109 const std::size_t n = l.n;
110 std::vector<double> hb = rhsOf(P);
111 auto ac = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), l.AC);
112 for (std::size_t i = 0; i < n; ++i)
113 if (!((double)ac(i) > 1e-30))
114 hb[i] = 0.0;
115 CCField b = toDevice(hb, "b"), x("x", n), r("r", n), p("p", n), z("z", n), Ap("Ap", n);
116 return mg.solvePCG(b, x, r, p, z, Ap, kCap, kRtol, 2, 2, 12);
117}
118
119Problem thinProblem(bool walls) { // gpu_diag/mgd/a3/symdiag.cpp presets thinW / slabP
120 const int w = walls ? 1 : 0;
121 const int bc[6] = {0, 0, 0, 0, w, w};
122 auto rho = [](double x, double y, double z) {
123 const double dx = x - 24, dy = y - 40, dz = (z - 2.0) * 3.0;
124 return dx * dx + dy * dy + dz * dz < 144.0 ? 1e2 : 1.0;
125 };
126 return makeProblem(
127 walls ? "thinW" : "slabP", 64, 64, 4, 5, bc, [](double, double, double) { return 1.0; }, rho);
128}
129Problem blobProblem(bool walls, int levels) { // symdiag.cpp presets iso64p / iso64w
130 const int w = walls ? 1 : 0;
131 const int bc[6] = {w, w, w, w, w, w};
132 auto rho = [](double x, double y, double z) {
133 const double dx = x - 20, dy = y - 36, dz = z - 28;
134 return dx * dx + dy * dy + dz * dz < 100.0 ? 1e2 : 1.0;
135 };
136 return makeProblem(
137 std::string(walls ? "iso64w" : "iso64p") + " D" + std::to_string(levels), 64, 64, 64, levels,
138 bc, [](double, double, double) { return 1.0; }, rho);
139}
140Problem scalarProblem() { // the state_hash `scalar` case's pressure operator
141 const int bc[6] = {1, 1, 1, 1, 0, 0};
142 auto one = [](double, double, double) { return 1.0; };
143 return makeProblem("scalar", 16, 16, 4, 0, bc, one, one);
144}
145Problem channelProblem() { // outflow-anchored: -x inflow, +x outflow (Dirichlet), y/z walls
146 const int bc[6] = {2, 3, 1, 1, 1, 1};
147 auto one = [](double, double, double) { return 1.0; };
148 return makeProblem("channel", 32, 16, 16, 4, bc, one, one);
149}
150
151std::string hierarchy(CutcellMG& mg) {
152 std::string s = std::to_string(mg.nLevels()) + " levels, bottom ";
153 const Level& b = mg.level(mg.nLevels() - 1);
154 s += std::to_string(b.inner.x) + "x" + std::to_string(b.inner.y) + "x" +
155 std::to_string(b.inner.z) + (mg.agglomerateBottom() ? " agglomerated" : " smoothed");
156 return s;
157}
158
159// a on P with the switch on (gated) and off (recorded); lr: line relaxation; agglom: forced.
160void symCase(const Problem& P, bool lr, bool agglom) {
161 CutcellMG mg;
163 mg.setLineRelaxation(lr);
164 if (agglom)
166 const std::string tag = P.name + (lr ? "" : " LR off");
167 std::printf("%s: %s\n", tag.c_str(), hierarchy(mg).c_str());
168 mg.setSymmetricVcycle(false);
169 const double aOff = asym(mg, false);
170 mg.setSymmetricVcycle(true);
171 const double aOn = asym(mg, false);
172 std::printf(" a: trilinear/forward %s (recorded), symmetric %s\n", num(aOff).c_str(),
173 num(aOn).c_str());
174 check(mg.agglomerateBottom() == agglom,
175 tag + ": the bottom is " + (agglom ? "agglomerated" : "smoothed"));
176 const double bound = agglom ? kBoundAgglomerated : kBoundSmoothed;
177 check(aOn <= bound, tag + ": a " + num(aOn) + " <= " + num(bound));
178}
179
180// The guard: production PCG iterations with the switch on (and off, recorded).
181void guardCase(const Problem& P, bool lr, int bound) {
182 CutcellMG mg;
184 mg.setLineRelaxation(lr);
185 const std::string tag = P.name + (lr ? "" : " LR off");
186 mg.setSymmetricVcycle(false);
187 const int off = pcgIters(mg, P);
188 mg.setSymmetricVcycle(true);
189 const int on = pcgIters(mg, P);
190 std::printf(
191 "%s: production PCG rhsOf rtol 1e-10: trilinear/forward %d (recorded), symmetric %d\n",
192 tag.c_str(), off, on);
193 check(on <= bound, tag + ": PCG " + std::to_string(on) + " <= " + std::to_string(bound));
194}
195
196// The FP32 V-cycle (forced 'fp32'): a within 100x of its recorded value, PCG on thinW <= 18.
197// Positive control (doc/mg_depth_policy_log.md 2026-10-30): with the symmetric FP32 branch's
198// prolongation swapped to the trilinear prolongFp32 this case FAILS on both checks (host: a
199// 1.345e-4, PCG 140).
200void fp32Case(const Problem& P) {
201 CutcellMG mg;
203 mg.setSymmetricVcycle(true);
204 if (const char* why = mg.fp32VcycleIneligible(true)) {
205 std::printf("%s FP32: not eligible (%s) -- n/a\n", P.name.c_str(), why);
206 check(CutcellMG::kHostMemory, P.name + " FP32: eligible on a device backend");
207 return;
208 }
209 const double a = asym(mg, true);
210 const int it = pcgIters(mg, P);
211 // Recorded at gate mgsym4 (thinW, 2026-10-29): host 3.582e-10, CUDA 1.154e-9; PCG 10 on both.
212 const double aRec = CutcellMG::kHostMemory ? 3.582e-10 : 1.154e-9;
213 std::printf("%s FP32 V-cycle (forced 'fp32', symmetric): a %s (recorded %s); PCG %d%s\n",
214 P.name.c_str(), num(a).c_str(), num(aRec).c_str(), it,
215 mg.lastVcycleFp32() ? "" : " (not FP32)");
216 check(mg.lastVcycleFp32(), P.name + " FP32: the V-cycles ran in FP32");
217 check(a <= 100.0 * aRec, P.name + " FP32: a " + num(a) + " <= 100 x " + num(aRec));
218 check(it <= 18 && !mg.lastSolveFailed(), P.name + " FP32: PCG " + std::to_string(it) + " <= 18");
219}
220
221} // namespace
222
223int main(int argc, char** argv) {
224 Kokkos::initialize(argc, argv);
225 {
226 const Problem thinW = thinProblem(true), slabP = thinProblem(false);
227 symCase(thinW, true, false);
228 symCase(thinW, false, false);
229 symCase(slabP, true, false);
230 symCase(scalarProblem(), true, false);
231 symCase(blobProblem(false, 5), true, false);
232 symCase(blobProblem(true, 5), true, false);
233 symCase(cylProblem(), true, false);
234 symCase(channelProblem(), true, false);
235 symCase(blobProblem(false, 4), true, true);
236 fp32Case(thinW);
237 guardCase(thinW, true, 16);
238 guardCase(thinW, false, 16);
239 guardCase(slabP, true, 14);
240 }
241 Kokkos::finalize();
242 std::printf("mg_vcycle_symmetry: %s (%d failure%s)\n", fails ? "FAIL" : "PASS", fails,
243 fails == 1 ? "" : "s");
244 return fails ? 1 : 0;
245}
static constexpr bool kHostMemory
void removeMean(Level &lv, CCField f, bool stopGuard=false)
int solvePCG(CCField b, CCField x, CCField r, CCField p, CCField z, CCField Ap, int maxit, double rtol, int pre, int post, int bottom, const StarOverlay *star=nullptr, int nStar=0, C3 nnStar=C3{0, 0, 0})
void precondVcycleFp32(CCField zz, CCField rr, int e, int k, CCField p)
const char * fp32VcycleIneligible(bool krylov) const
void setSymmetricVcycle(bool on)
A3 (doc/mg_depth_policy.md Amendment A3, WO-S1): the symmetric pressure V-cycle – piecewise-constant ...
double maxabs(Level &lv, CCField a)
void precondVcycle(CCField zz, CCField rr)
double dot(Level &lv, CCField a, CCField b)
void setAgglomerationMode(int mode)
void setLineRelaxation(bool on)
A2.4 (doc/mg_depth_policy.md, WO-7): zebra line relaxation along the blocked strong axis on the trigg...
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
The FP32 V-cycle test problems (column, cyl, slab, pack) and their right-hand side,...
Kokkos::View< double *, CCMem > CCField
std::vector< double > rhsOf(const Problem &P)
CCField toDevice(const std::vector< double > &v, const char *name)
void setUp(CutcellMG &mg, const Problem &P, int mode)
Problem makeProblem(const std::string &name, int nx, int ny, int nz, int levels, const int bc[6], Open open, Rho rho)
static double val(long gi)
Definition symconv.cpp:27
static void check(bool ok, const std::string &what)
static int fails