flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
ghost_projection_debug.hpp
Go to the documentation of this file.
1
34#ifndef PECLET_FLOW_GHOST_PROJECTION_DEBUG_HPP
35#define PECLET_FLOW_GHOST_PROJECTION_DEBUG_HPP
36
37#include <algorithm>
38#include <cmath>
39#include <cstdint>
40#include <cstdio>
41#include <cstdlib>
42#include <Kokkos_Core.hpp>
43#include <string>
44#include <vector>
45
46#include "ghost_projection.hpp"
47
48namespace peclet::flow {
49
51inline int gpDebugLevel() {
52 const char* s = std::getenv("PECLET_FLOW_GP_DEBUG");
53 return s ? std::atoi(s) : 0;
54}
55
57inline constexpr const char* gpStateName[5] = {"COUPLED", "QUAD", "LIN", "BC_ONLY", "EXPLICIT"};
58
61template <class Real>
63
64template <class Real>
67 h.cell = Kokkos::create_mirror_view_and_copy(
68 Kokkos::HostSpace(), Kokkos::subview(ov.cell, Kokkos::make_pair(0, nRows)));
69 h.rescale = Kokkos::create_mirror_view_and_copy(
70 Kokkos::HostSpace(), Kokkos::subview(ov.rescale, Kokkos::make_pair(0, nRows)));
71 h.coupled = Kokkos::create_mirror_view_and_copy(
72 Kokkos::HostSpace(), Kokkos::subview(ov.coupled, Kokkos::make_pair(0, nRows)));
73 const auto sub6 = Kokkos::make_pair(0, 6 * nRows);
74 h.state =
75 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Kokkos::subview(ov.state, sub6));
76 h.th = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Kokkos::subview(ov.th, sub6));
77 h.wm_n1 =
78 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Kokkos::subview(ov.wm_n1, sub6));
79 h.wm_n2 =
80 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Kokkos::subview(ov.wm_n2, sub6));
81 h.w_n1 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Kokkos::subview(ov.w_n1, sub6));
82 h.w_n2 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Kokkos::subview(ov.w_n2, sub6));
83 return h;
84}
85
89 long stCount[5] = {0, 0, 0, 0, 0};
91 // rho decades: [1e-5,1e-4) ... [1e-1,1) and == 1
92 long rhoDec[7] = {0, 0, 0, 0, 0, 0, 0};
93 long wDec[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // max |w| decades 1e0..1e7
94 long decoupled = 0;
95 double rhoMin = 1.0, wMax = 0.0;
96 std::vector<float> rowMaxW;
97};
98
100template <class Real>
101inline float gpRowFaceCensus(const GpOverlayHost<Real>& h, int s, GpRowCensus& c) {
102 float mw = 0.0f;
103 for (int k = 0; k < 6; ++k) {
104 const int8_t st = h.state(s * 6 + k);
105 c.stCount[st < 0 || st > 4 ? 0 : st]++;
106 if (st != GP_QUAD && st != GP_LIN)
107 continue;
108 const float th = h.th(s * 6 + k);
109 if (th > 1.0f)
110 ++c.extendedTh;
111 if (th <= (float)(1e-4 * 1.0000001))
112 ++c.clampedThLo;
113 if (th >= 2.0f)
114 ++c.clampedThHi;
115 const float a1 = std::fabs(h.wm_n1(s * 6 + k)), a2 = std::fabs(h.wm_n2(s * 6 + k));
116 mw = mw > a1 ? mw : a1;
117 mw = mw > a2 ? mw : a2;
118 const float b1 = std::fabs(h.w_n1(s * 6 + k)), b2 = std::fabs(h.w_n2(s * 6 + k));
119 mw = mw > b1 ? mw : b1;
120 mw = mw > b2 ? mw : b2;
121 }
122 return mw;
123}
124
125template <class Real>
126inline GpRowCensus gpRowCensus(const GpOverlayHost<Real>& h, int nRows) {
127 GpRowCensus c;
128 c.rowMaxW.assign((std::size_t)nRows, 0.0f);
129 for (int s = 0; s < nRows; ++s) {
130 if (!h.coupled(s))
131 ++c.decoupled;
132 const float rho = h.rescale(s);
133 c.rhoMin = rho < c.rhoMin ? rho : c.rhoMin;
134 int d = rho >= 1.0f ? 6 : (int)std::floor(std::log10((double)rho)) + 6;
135 c.rhoDec[d < 0 ? 0 : (d > 6 ? 6 : d)]++;
136 const float mw = gpRowFaceCensus(h, s, c);
137 c.rowMaxW[(std::size_t)s] = mw;
138 c.wMax = mw > c.wMax ? mw : c.wMax;
139 int wd = mw <= 1.0f ? 0 : (int)std::floor(std::log10((double)mw));
140 c.wDec[wd < 0 ? 0 : (wd > 7 ? 7 : wd)]++;
141 }
142 return c;
143}
144
149 long mmDec[6] = {0, 0, 0, 0, 0, 0}; // |log10 ratio| in [0,1),[1,2),...,[5,inf)
150 double mmMax = 0.0;
151 std::vector<float> rowMismatch;
152};
153
154template <class Real>
155inline GpRhoMismatch gpRhoMismatch(const GpOverlayHost<Real>& h, int nRows, C3 nn,
156 Kokkos::View<int*, Kokkos::HostSpace> hId) {
157 const long nInner = (long)nn.x * nn.y * nn.z;
158 auto rhoAt = [&](int x, int y, int z) -> float {
159 const int ix = ((x % nn.x) + nn.x) % nn.x, iy = ((y % nn.y) + nn.y) % nn.y,
160 iz = ((z % nn.z) + nn.z) % nn.z;
161 const long c = (long)ix + (long)iy * nn.x + (long)iz * (long)nn.x * nn.y;
162 if (c < 0 || c >= nInner)
163 return 1.0f;
164 const int s = hId(c);
165 return s >= 0 ? h.rescale(s) : 1.0f;
166 };
168 m.rowMismatch.assign((std::size_t)nRows, 0.0f);
169 for (int s = 0; s < nRows; ++s) {
170 const int c = h.cell(s);
171 const int x = c % nn.x, y = (c / nn.x) % nn.y, z = c / (nn.x * nn.y);
172 const double lr = std::log10((double)h.rescale(s));
173 double worst = 0.0;
174 const int off[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};
175 for (auto& o : off) {
176 const double d = std::fabs(lr - std::log10((double)rhoAt(x + o[0], y + o[1], z + o[2])));
177 worst = d > worst ? d : worst;
178 }
179 m.rowMismatch[(std::size_t)s] = (float)worst;
180 m.mmMax = worst > m.mmMax ? worst : m.mmMax;
181 int b = (int)worst;
182 m.mmDec[b < 0 ? 0 : (b > 5 ? 5 : b)]++;
183 }
184 return m;
185}
186
187inline void gpPrintCensus(int rank, int nRows, long nInner, const GpRowCensus& c,
188 const GpRhoMismatch& m) {
189 std::printf("[gp] rank %d overlay census: %d rows of %ld inner cells (%.2f %%), %ld decoupled\n",
190 rank, nRows, nInner, 100.0 * nRows / (double)nInner, c.decoupled);
191 std::printf("[gp] face states:");
192 for (int i = 0; i < 5; ++i)
193 std::printf(" %s=%ld", gpStateName[i], c.stCount[i]);
194 std::printf("\n[gp] theta: EXTENDED(1,2)=%ld clamped-low(1e-4)=%ld clamped-high(2)=%ld\n",
195 c.extendedTh, c.clampedThLo, c.clampedThHi);
196 std::printf(
197 "[gp] rho decades [1e-5,1e-4)=%ld [1e-4,1e-3)=%ld [1e-3,1e-2)=%ld "
198 "[1e-2,1e-1)=%ld [1e-1,1)=%ld ==1:%ld min=%.3e\n",
199 c.rhoDec[0] + c.rhoDec[1], c.rhoDec[2], c.rhoDec[3], c.rhoDec[4], c.rhoDec[5], c.rhoDec[6],
200 c.rhoMin);
201 std::printf("[gp] max|w| decades");
202 for (int i = 0; i < 8; ++i)
203 std::printf(" 1e%d:%ld", i, c.wDec[i]);
204 std::printf(" max=%.3e\n", c.wMax);
205 std::printf("[gp] neighbour rho mismatch |log10| bins");
206 for (int i = 0; i < 6; ++i)
207 std::printf(" %d:%ld", i, m.mmDec[i]);
208 std::printf(" max=%.2f decades\n", m.mmMax);
209}
210
212template <class Real>
213inline void gpPrintWorstRows(const GpOverlayHost<Real>& h, int nRows, C3 nn, const GpRowCensus& c,
214 const GpRhoMismatch& m) {
215 std::vector<int> ord((std::size_t)nRows);
216 for (int i = 0; i < nRows; ++i)
217 ord[(std::size_t)i] = i;
218 const int show = nRows < 20 ? nRows : 20;
219 std::partial_sort(ord.begin(), ord.begin() + show, ord.end(),
220 [&](int a, int b) { return h.rescale(a) < h.rescale(b); });
221 std::printf("[gp] worst %d rows by rho:\n", show);
222 for (int i = 0; i < show; ++i) {
223 const int s = ord[(std::size_t)i];
224 const int cl = h.cell(s);
225 std::printf("[gp] (%4d,%4d,%4d) rho=%.4e maxw=%.3e mism=%.2f |", cl % nn.x,
226 (cl / nn.x) % nn.y, cl / (nn.x * nn.y), h.rescale(s), c.rowMaxW[(std::size_t)s],
227 m.rowMismatch[(std::size_t)s]);
228 for (int k = 0; k < 6; ++k)
229 std::printf(" %s(th=%.3e,wm=%.3e/%.3e)", gpStateName[h.state(s * 6 + k)], h.th(s * 6 + k),
230 h.wm_n1(s * 6 + k), h.wm_n2(s * 6 + k));
231 std::printf("\n");
232 }
233}
234
237template <class Real>
238inline void gpDumpRows(const GpOverlayHost<Real>& h, int nRows, C3 nn, int rank) {
239 const char* fenv = std::getenv("PECLET_FLOW_GP_DEBUG_FILE");
240 std::string path = fenv ? std::string(fenv) : std::string("gp_rows");
241 path += "_rank" + std::to_string(rank) + ".bin";
242 std::FILE* f = std::fopen(path.c_str(), "wb");
243 if (!f) {
244 std::printf("[gp] COULD NOT OPEN %s for the per-row dump\n", path.c_str());
245 return;
246 }
247 for (int s = 0; s < nRows; ++s) {
248 const int c = h.cell(s);
249 const int32_t xyz[3] = {c % nn.x, (c / nn.x) % nn.y, c / (nn.x * nn.y)};
250 const float rho = h.rescale(s);
251 const int8_t cpl = h.coupled(s);
252 int8_t st[6];
253 float th[6], wm1[6], wm2[6], w1[6], w2[6];
254 for (int k = 0; k < 6; ++k) {
255 st[k] = h.state(s * 6 + k);
256 th[k] = h.th(s * 6 + k);
257 wm1[k] = h.wm_n1(s * 6 + k);
258 wm2[k] = h.wm_n2(s * 6 + k);
259 w1[k] = h.w_n1(s * 6 + k);
260 w2[k] = h.w_n2(s * 6 + k);
261 }
262 std::fwrite(xyz, sizeof(int32_t), 3, f);
263 std::fwrite(&rho, sizeof(float), 1, f);
264 std::fwrite(&cpl, sizeof(int8_t), 1, f);
265 std::fwrite(st, sizeof(int8_t), 6, f);
266 std::fwrite(th, sizeof(float), 6, f);
267 std::fwrite(wm1, sizeof(float), 6, f);
268 std::fwrite(wm2, sizeof(float), 6, f);
269 std::fwrite(w1, sizeof(float), 6, f);
270 std::fwrite(w2, sizeof(float), 6, f);
271 }
272 std::fclose(f);
273 std::printf("[gp] per-row dump -> %s (%d records)\n", path.c_str(), nRows);
274 std::fflush(stdout);
275}
276
280template <class Real>
281inline void gpDebugReport(const GpOverlayReal<Real>& ov, int nRows, C3 nn,
282 Kokkos::View<int*, CCMem> idMap, int rank = 0) {
283 const int level = gpDebugLevel();
284 if (level <= 0 || nRows <= 0)
285 return;
286 const GpOverlayHost<Real> h = gpOverlayRowsToHost(ov, nRows);
287 const Kokkos::View<int*, Kokkos::HostSpace> hId =
288 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), idMap);
289 const GpRowCensus census = gpRowCensus(h, nRows);
290 const GpRhoMismatch mismatch = gpRhoMismatch(h, nRows, nn, hId);
291 gpPrintCensus(rank, nRows, (long)nn.x * nn.y * nn.z, census, mismatch);
292 gpPrintWorstRows(h, nRows, nn, census, mismatch);
293 std::fflush(stdout);
294 if (level >= 2)
295 gpDumpRows(h, nRows, nn, rank);
296}
297
298} // namespace peclet::flow
299
300#endif // PECLET_FLOW_GHOST_PROJECTION_DEBUG_HPP
flow — directional ghost-cell IBM projection overlay (experimental second staggered IBM).
void gpDumpRows(const GpOverlayHost< Real > &h, int nRows, C3 nn, int rank)
PECLET_FLOW_GP_DEBUG=2: every overlay row to <PECLET_FLOW_GP_DEBUG_FILE>_rank<r>.bin (record layout i...
void gpPrintWorstRows(const GpOverlayHost< Real > &h, int nRows, C3 nn, const GpRowCensus &c, const GpRhoMismatch &m)
The 20 worst rows by rho, with their full face anatomy.
GpRhoMismatch gpRhoMismatch(const GpOverlayHost< Real > &h, int nRows, C3 nn, Kokkos::View< int *, Kokkos::HostSpace > hId)
int gpDebugLevel()
0 = off (default). Read once per call; cheap enough, and keeps the flag hot-swappable in tests.
GpOverlayHost< Real > gpOverlayRowsToHost(const GpOverlayReal< Real > &ov, int nRows)
float gpRowFaceCensus(const GpOverlayHost< Real > &h, int s, GpRowCensus &c)
Face census of row s into c; returns the row's max |closure weight|.
GpRowCensus gpRowCensus(const GpOverlayHost< Real > &h, int nRows)
GpOverlayT< CCMem, Real > GpOverlayReal
constexpr const char * gpStateName[5]
Face-state names, indexed by GpOverlayT::state (GP_COUPLED .. GP_EXPLICIT).
void gpPrintCensus(int rank, int nRows, long nInner, const GpRowCensus &c, const GpRhoMismatch &m)
GpOverlayT< Kokkos::HostSpace, Real > GpOverlayHost
The report's host view of an overlay: the first nRows rows of every array it reads (w_bc is not read ...
void gpDebugReport(const GpOverlayReal< Real > &ov, int nRows, C3 nn, Kokkos::View< int *, CCMem > idMap, int rank=0)
Census + optional per-row dump of the built overlay.
Kokkos::View< Real *, Space > th
Kokkos::View< int8_t *, Space > state
Kokkos::View< Real *, Space > w_n2
Kokkos::View< int8_t *, Space > coupled
Kokkos::View< int *, Space > cell
Kokkos::View< Real *, Space > wm_n2
Kokkos::View< Real *, Space > rescale
Kokkos::View< Real *, Space > w_n1
Kokkos::View< Real *, Space > wm_n1
Neighbour rho mismatch: the row scaling is a LEFT diagonal scaling of a nonsymmetric operator,...
Row/face census of the overlay: face-state counts, the theta and rho histograms and the max |closure ...
static double rhoAt(int, int, int z)