flow 1.3.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
voro_capture.cpp
Go to the documentation of this file.
1// Rung-4 relocation oracle: evaluate voro's SDF providers over a deterministic sweep and dump raw
2// bits. Run before and after the port; must be byte-identical. Covers the three analytic providers
3// being ported AND SdfGrid/SdfSpheres, which are NOT being ported -- so the capture also proves
4// they were left alone.
5#include <cstdint>
6#include <cstdio>
7#include <cstring>
8#include <Kokkos_Core.hpp>
9#include <vector>
10
11#include "peclet/voro/sdf.hpp"
12
13using namespace peclet::voro;
14
15int main(int argc, char** argv) {
16 Kokkos::initialize(argc, argv);
17 std::vector<std::uint32_t> bits;
18 {
19 const int NP = 3000;
20 std::uint32_t st = 0xBEEF01u;
21 auto u = [&](double lo, double hi) {
22 st = st * 1664525u + 1013904223u;
23 return lo + (hi - lo) * ((double)((st >> 8) & 0xFFFFFFu) / (double)0x1000000u);
24 };
25 std::vector<double> px(NP), py(NP), pz(NP);
26 for (int i = 0; i < NP; ++i) {
27 px[i] = u(-3, 3);
28 py[i] = u(-3, 3);
29 pz[i] = u(-3, 3);
30 }
31 Kokkos::View<double*> X("x", NP), Y("y", NP), Z("z", NP);
32 auto hx = Kokkos::create_mirror_view(X), hy = Kokkos::create_mirror_view(Y),
33 hz = Kokkos::create_mirror_view(Z);
34 for (int i = 0; i < NP; ++i) {
35 hx(i) = px[i];
36 hy(i) = py[i];
37 hz(i) = pz[i];
38 }
39 Kokkos::deep_copy(X, hx);
40 Kokkos::deep_copy(Y, hy);
41 Kokkos::deep_copy(Z, hz);
42
43 // sphere / box / hollow-cylinder sweeps (several parameter sets incl. all three axes)
44 const int NS = 12;
45 Kokkos::View<double*> out("out", (std::size_t)NP * NS);
46 Kokkos::parallel_for(
47 "cap", NP, KOKKOS_LAMBDA(int i) {
48 const double x = X(i), y = Y(i), z = Z(i);
49 int s = 0;
50 SdfSphere<double> a{0.1, -0.2, 0.3, 1.25};
51 SdfSphere<double> a2{0, 0, 0, 0.5};
52 out(i * NS + s++) = a.eval(x, y, z);
53 out(i * NS + s++) = a2.eval(x, y, z);
54 SdfBox<double> b{0.2, 0.1, -0.3, 0.8, 1.1, 0.5};
55 SdfBox<double> b2{0, 0, 0, 0.4, 0.4, 0.4};
56 out(i * NS + s++) = b.eval(x, y, z);
57 out(i * NS + s++) = b2.eval(x, y, z);
58 for (int ax = 0; ax < 3; ++ax) {
59 SdfHollowCylinder<double> c{0.05, -0.1, 0.15, 1.3, 0.7, 1.6, ax};
60 out(i * NS + s++) = c.eval(x, y, z);
61 }
62 SdfHollowCylinder<double> c2{0, 0, 0, 1.0, 0.0, 2.0, 2};
63 out(i * NS + s++) = c2.eval(x, y, z);
64 // gradients through the shared central-difference helper
65 double g[3];
66 sdfGradient<double>(a, x, y, z, g);
67 out(i * NS + s++) = g[0];
68 out(i * NS + s++) = g[1];
69 out(i * NS + s++) = g[2];
70 out(i * NS + s++) = a.gradH() + b.gradH() + c2.gradH();
71 });
72 Kokkos::fence();
73 auto ho = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, out);
74 for (std::size_t i = 0; i < ho.extent(0); ++i) {
75 std::uint64_t b64;
76 double v = ho(i);
77 std::memcpy(&b64, &v, 8);
78 bits.push_back((std::uint32_t)(b64 & 0xFFFFFFFFu));
79 bits.push_back((std::uint32_t)(b64 >> 32));
80 }
81
82 // SdfSpheres + SdfGrid (NOT ported -- capture proves they are untouched)
83 const int M = 24;
84 Kokkos::View<double*> cen("cen", 3 * M), rad("rad", M);
85 auto hc = Kokkos::create_mirror_view(cen), hr = Kokkos::create_mirror_view(rad);
86 for (int i = 0; i < M; ++i) {
87 hc(3 * i) = u(-1, 1);
88 hc(3 * i + 1) = u(-1, 1);
89 hc(3 * i + 2) = u(-1, 1);
90 hr(i) = u(0.1, 0.4);
91 }
92 Kokkos::deep_copy(cen, hc);
93 Kokkos::deep_copy(rad, hr);
94 const int G = 8;
95 Kokkos::View<float*> gv("gv", (std::size_t)G * G * G);
96 auto hg = Kokkos::create_mirror_view(gv);
97 for (int k = 0; k < G; ++k)
98 for (int j = 0; j < G; ++j)
99 for (int i2 = 0; i2 < G; ++i2) {
100 const double xx = -1 + 0.3 * i2, yy = -1 + 0.3 * j, zz = -1 + 0.3 * k;
101 hg(i2 + G * (j + G * k)) = (float)(std::sqrt(xx * xx + yy * yy + zz * zz) - 0.6);
102 }
103 Kokkos::deep_copy(gv, hg);
104 Kokkos::View<double*> out2("out2", (std::size_t)NP * 2);
105 Kokkos::parallel_for(
106 "cap2", NP, KOKKOS_LAMBDA(int i) {
107 SdfSpheres<double> sp{cen, rad, M, 2.0};
108 auto gr =
109 SdfGrid<double>::fromSpacing(Kokkos::View<const float*, peclet::core::MemSpace>(gv),
110 G, G, G, -1.0, -1.0, -1.0, 0.3, 0.3, 0.3);
111 out2(2 * i) = sp.eval(X(i), Y(i), Z(i));
112 out2(2 * i + 1) = gr.eval(X(i), Y(i), Z(i));
113 });
114 Kokkos::fence();
115 auto ho2 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, out2);
116 for (std::size_t i = 0; i < ho2.extent(0); ++i) {
117 std::uint64_t b64;
118 double v = ho2(i);
119 std::memcpy(&b64, &v, 8);
120 bits.push_back((std::uint32_t)(b64 & 0xFFFFFFFFu));
121 bits.push_back((std::uint32_t)(b64 >> 32));
122 }
123 }
124 Kokkos::finalize();
125 std::FILE* f = std::fopen(argv[1], "wb");
126 std::fwrite(bits.data(), 4, bits.size(), f);
127 std::fclose(f);
128 std::printf("wrote %zu words\n", bits.size());
129 return 0;
130}
int main()
Definition dbg_scene.cpp:13
static constexpr int G