34#ifndef PECLET_FLOW_GHOST_PROJECTION_DEBUG_HPP
35#define PECLET_FLOW_GHOST_PROJECTION_DEBUG_HPP
42#include <Kokkos_Core.hpp>
52 const char* s = std::getenv(
"PECLET_FLOW_GP_DEBUG");
53 return s ? std::atoi(s) : 0;
57inline constexpr const char*
gpStateName[5] = {
"COUPLED",
"QUAD",
"LIN",
"BC_ONLY",
"EXPLICIT"};
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);
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));
78 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Kokkos::subview(ov.
wm_n1, sub6));
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));
92 long rhoDec[7] = {0, 0, 0, 0, 0, 0, 0};
93 long wDec[8] = {0, 0, 0, 0, 0, 0, 0, 0};
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)
108 const float th = h.th(s * 6 + k);
111 if (th <= (
float)(1e-4 * 1.0000001))
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;
128 c.rowMaxW.assign((std::size_t)nRows, 0.0f);
129 for (
int s = 0; s < nRows; ++s) {
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)]++;
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)]++;
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)
164 const int s = hId(c);
165 return s >= 0 ? h.rescale(s) : 1.0f;
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));
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;
179 m.rowMismatch[(std::size_t)s] = (
float)worst;
180 m.mmMax = worst > m.mmMax ? worst : m.mmMax;
182 m.mmDec[b < 0 ? 0 : (b > 5 ? 5 : b)]++;
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);
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],
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);
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));
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");
244 std::printf(
"[gp] COULD NOT OPEN %s for the per-row dump\n", path.c_str());
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);
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);
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);
273 std::printf(
"[gp] per-row dump -> %s (%d records)\n", path.c_str(), nRows);
282 Kokkos::View<int*, CCMem> idMap,
int rank = 0) {
284 if (level <= 0 || nRows <= 0)
287 const Kokkos::View<int*, Kokkos::HostSpace> hId =
288 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), idMap);
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,...
std::vector< float > rowMismatch
Row/face census of the overlay: face-state counts, the theta and rho histograms and the max |closure ...
std::vector< float > rowMaxW
static double rhoAt(int, int, int z)