peclet-dem 1.1.0
Performance-portable XPBD Discrete Element Method (Kokkos + ArborX)
Loading...
Searching...
No Matches
output_sdf.hpp
Go to the documentation of this file.
1
12#ifndef DEM_OUTPUT_SDF_HPP
13#define DEM_OUTPUT_SDF_HPP
14
15#include <Kokkos_Core.hpp>
16#include <vector>
17
18#include "dem_portable.hpp" // F3/F4, invRotateVector, sdfSphere/sdfHollowCylinder, ShapeKind
19#include "narrowphase.hpp" // ShapeDesc, CpExec/CpMem, view aliases
20
21namespace peclet::dem {
22
23// Generate the SDF grid (flat, x-fastest: i = x + y*rx + z*rx*ry; negative inside solid) over
24// [min,max].
29inline std::vector<float> generateSdf(int rx, int ry, int rz, F3 dmin, F3 dmax, int numReal,
30 const BodyState& bodies,
31 Kokkos::View<const ShapeDesc*, CpMem> shapes, bool px,
32 bool py, bool pz, GridView sdfGrid = GridView{},
33 float globalScale = 1.0f) {
34 const PosView pos = bodies.pos;
35 const QuatView quat = bodies.quat;
36 const ScalarF scale = bodies.scale;
37 const ScalarI shapeId = bodies.shapeId;
38 CpExec space;
39 const long total = (long)rx * ry * rz;
40 const F3 origin = dmin;
41 const F3 vox{(dmax.x - dmin.x) / rx, (dmax.y - dmin.y) / ry, (dmax.z - dmin.z) / rz};
42
43 Kokkos::View<float*, CpMem> grid("sdf_grid", total);
44 Kokkos::View<int*, CpMem> state("sdf_state", total);
45 Kokkos::deep_copy(grid, 100.0f);
46 Kokkos::deep_copy(state, 0);
47
48 // SPLAT: each particle writes the exact analytic signed distance over its AABB band (atomic-min).
49 Kokkos::parallel_for(
50 "peclet::dem::sdf::splat", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
51 KOKKOS_LAMBDA(int i) {
52 const F3 pw = loadF3(pos, i);
53 const F4 q = loadF4(quat, i);
54 // scale * globalScale -- the SAME effective scale the narrow-phase applies. Using the
55 // bare per-particle scale here (the old behaviour) made the exported SDF disagree with the
56 // simulated geometry whenever global_scale != 1: the splat drew bodies at the wrong size
57 // and the AABB band was sized for that wrong size too.
58 const float sc = scale(i) * globalScale;
59 const ShapeDesc shp = shapes(shapeId(i));
60 float rbound = 1.0f;
61 if (shp.type == HOLLOW_CYLINDER) {
62 const float r = shp.params.x, h = shp.params.y;
63 rbound = Kokkos::sqrt(r * r + (h * 0.5f) * (h * 0.5f));
64 } else if (shp.type == SPHERE)
65 rbound = shp.params.x;
66 else if (shp.type == SHAPE_GRID_SDF || shp.type == SHAPE_SCENE)
67 rbound = shp.params.x; // canonical bounding radius stored in params.x
68 rbound *= sc;
69 rbound *= 1.2f;
70
71 int minx = (int)Kokkos::floor((pw.x - rbound - origin.x) / vox.x),
72 maxx = (int)Kokkos::ceil((pw.x + rbound - origin.x) / vox.x);
73 int miny = (int)Kokkos::floor((pw.y - rbound - origin.y) / vox.y),
74 maxy = (int)Kokkos::ceil((pw.y + rbound - origin.y) / vox.y);
75 int minz = (int)Kokkos::floor((pw.z - rbound - origin.z) / vox.z),
76 maxz = (int)Kokkos::ceil((pw.z + rbound - origin.z) / vox.z);
77 if (!px) {
78 minx = minx < 0 ? 0 : minx;
79 maxx = maxx > rx - 1 ? rx - 1 : maxx;
80 }
81 if (!py) {
82 miny = miny < 0 ? 0 : miny;
83 maxy = maxy > ry - 1 ? ry - 1 : maxy;
84 }
85 if (!pz) {
86 minz = minz < 0 ? 0 : minz;
87 maxz = maxz > rz - 1 ? rz - 1 : maxz;
88 }
89
90 for (int z = minz; z <= maxz; ++z)
91 for (int y = miny; y <= maxy; ++y)
92 for (int x = minx; x <= maxx; ++x) {
93 const F3 vp{origin.x + (x + 0.5f) * vox.x, origin.y + (y + 0.5f) * vox.y,
94 origin.z + (z + 0.5f) * vox.z};
95 const F3 prel = sub3(vp, pw);
96 const F3 plocal = scale3(invRotateVector(q, prel), 1.0f / sc);
97 float distc = 100.0f;
98 if (shp.type == HOLLOW_CYLINDER)
99 distc = sdfHollowCylinder(plocal, shp.params);
100 else if (shp.type == SPHERE)
101 distc = sdfSphere(plocal, shp.params);
102 else if (shp.type == SHAPE_GRID_SDF)
103 distc = sampleGridSdf(plocal, shp, sdfGrid);
104 else if (shp.type == SHAPE_SCENE)
105 distc = sdfEvalShape(plocal, shp, sdfGrid); // composed tree, exact
106 const float dist = distc * sc;
107 const int wx = (x % rx + rx) % rx, wy = (y % ry + ry) % ry, wz = (z % rz + rz) % rz;
108 if (!px && (x < 0 || x >= rx))
109 continue;
110 if (!py && (y < 0 || y >= ry))
111 continue;
112 if (!pz && (z < 0 || z >= rz))
113 continue;
114 const long vi = (long)wz * rx * ry + (long)wy * rx + wx;
115 Kokkos::atomic_min(&grid(vi), dist);
116 if (dist < rbound)
117 state(vi) = 1;
118 }
119 });
120 space.fence();
121
122 // EIKONAL: Jacobi fast-iterative-method (ping-pong), max(res)*4 iterations, periodic wrap.
123 Kokkos::View<float*, CpMem> grid2("sdf_grid2", total);
124 const int iters = Kokkos::max(Kokkos::max(rx, ry), rz) * 4;
125 const float h = vox.x;
126 Kokkos::View<float*, CpMem> in = grid, out = grid2;
127 for (int it = 0; it < iters; ++it) {
128 Kokkos::View<float*, CpMem> din = in, dout = out;
129 Kokkos::View<int*, CpMem> st = state;
130 Kokkos::parallel_for(
131 "peclet::dem::sdf::eikonal", Kokkos::RangePolicy<CpExec>(space, 0, total),
132 KOKKOS_LAMBDA(long idx) {
133 if (st(idx) == 1) {
134 dout(idx) = din(idx);
135 return;
136 }
137 const long sy = rx, sz = (long)rx * ry;
138 const int z = (int)(idx / sz);
139 const long rem = idx % sz;
140 const int y = (int)(rem / sy), x = (int)(rem % sy);
141 float xm = 100.0f, xp = 100.0f, ym = 100.0f, yp = 100.0f, zm = 100.0f, zp = 100.0f;
142 if (x > 0)
143 xm = din(idx - 1);
144 else if (px)
145 xm = din(idx - 1 + rx);
146 if (x < rx - 1)
147 xp = din(idx + 1);
148 else if (px)
149 xp = din(idx + 1 - rx);
150 if (y > 0)
151 ym = din(idx - sy);
152 else if (py)
153 ym = din((long)z * sz + (long)(ry - 1) * sy + x);
154 if (y < ry - 1)
155 yp = din(idx + sy);
156 else if (py)
157 yp = din((long)z * sz + x);
158 if (z > 0)
159 zm = din(idx - sz);
160 else if (pz)
161 zm = din((long)(rz - 1) * sz + (long)y * sy + x);
162 if (z < rz - 1)
163 zp = din(idx + sz);
164 else if (pz)
165 zp = din((long)y * sy + x);
166 float a = Kokkos::fmin(xm, xp), b = Kokkos::fmin(ym, yp), c = Kokkos::fmin(zm, zp);
167 if (a > b) {
168 float t = a;
169 a = b;
170 b = t;
171 }
172 if (b > c) {
173 float t = b;
174 b = c;
175 c = t;
176 }
177 if (a > b) {
178 float t = a;
179 a = b;
180 b = t;
181 }
182 float u = 100.0f, u1 = a + h;
183 if (u1 <= b)
184 u = u1;
185 else {
186 const float d2 = 2.0f * h * h - (a - b) * (a - b);
187 float u2 = (d2 < 0.0f) ? 100.0f : (a + b + Kokkos::sqrt(d2)) * 0.5f;
188 if (u2 <= c)
189 u = u2;
190 else {
191 const float s = a + b + c, qd = s * s - 3.0f * (a * a + b * b + c * c - h * h);
192 u = (qd < 0.0f) ? 100.0f : (s + Kokkos::sqrt(qd)) / 3.0f;
193 }
194 }
195 dout(idx) = u;
196 });
197 space.fence();
198 auto tmp = in;
199 in = out;
200 out = tmp;
201 }
202
203 std::vector<float> h_grid(total);
204 auto hv = Kokkos::create_mirror_view(in);
205 Kokkos::deep_copy(hv, in);
206 for (long i = 0; i < total; ++i)
207 h_grid[i] = hv(i);
208 return h_grid;
209}
210
211} // namespace peclet::dem
212
213#endif // DEM_OUTPUT_SDF_HPP
dem — portable POD types + math + analytic SDFs shared by the Kokkos kernel ports.
float sdfHollowCylinder(F3 p, F4 params)
F3 invRotateVector(F4 q, F3 v)
F3 loadF3(PosView v, int i)
float sdfEvalShape(F3 p, const ShapeDesc &d, GridView grid)
Canonical-space SDF of a shape: analytic dispatch, or a trilinear grid sample for an imported grid SD...
Kokkos::View< const float *[4], CpMem > QuatView
F3 sub3(F3 a, F3 b)
Kokkos::DefaultExecutionSpace CpExec
std::vector< float > generateSdf(int rx, int ry, int rz, F3 dmin, F3 dmax, int numReal, const BodyState &bodies, Kokkos::View< const ShapeDesc *, CpMem > shapes, bool px, bool py, bool pz, GridView sdfGrid=GridView{}, float globalScale=1.0f)
globalScale multiplies every per-particle scale, exactly as the narrow-phase does (narrowphase....
F4 loadF4(QuatView v, int i)
Kokkos::View< const int *, CpMem > ScalarI
F3 scale3(F3 a, float s)
Kokkos::View< const float *, CpMem > ScalarF
float sampleGridSdf(F3 p, const ShapeDesc &d, GridView grid)
Trilinearly sample an imported grid SDF at canonical point p.
float sdfSphere(F3 p, F4 params)
Kokkos::View< const float *[3], CpMem > PosView
Kokkos::View< const float *, CpMem > GridView
dem — portable (Kokkos) narrow-phase: SDF point-shell collision + boundary planes.
A body's state, one row per particle slot (Particles::bodyState()).
Kokkos::View< float *[3], CpMem > pos
Kokkos::View< float *, CpMem > scale
Kokkos::View< int *, CpMem > shapeId
Kokkos::View< float *[4], CpMem > quat