peclet-dem
Performance-portable XPBD Discrete Element Method (Kokkos + ArborX)
Loading...
Searching...
No Matches
dem_portable.hpp
Go to the documentation of this file.
1
8
#ifndef DEM_PORTABLE_HPP
9
#define DEM_PORTABLE_HPP
10
11
#include <Kokkos_Core.hpp>
12
#include <Kokkos_MathematicalFunctions.hpp>
13
14
namespace
peclet::dem
{
15
16
// Portable mirrors of CUDA float3/float4 (POD, trivially copyable).
17
struct
F3
{
18
float
x
,
y
,
z
;
19
};
20
struct
F4
{
21
float
x
,
y
,
z
,
w
;
22
};
23
24
// --- vector ops ---
25
KOKKOS_INLINE_FUNCTION
F3
add3
(
F3
a,
F3
b) {
26
return
F3
{a.
x
+ b.
x
, a.
y
+ b.
y
, a.
z
+ b.
z
};
27
}
28
KOKKOS_INLINE_FUNCTION
F3
sub3
(
F3
a,
F3
b) {
29
return
F3
{a.
x
- b.
x
, a.
y
- b.
y
, a.
z
- b.
z
};
30
}
31
KOKKOS_INLINE_FUNCTION
F3
scale3
(
F3
a,
float
s) {
32
return
F3
{a.
x
* s, a.
y
* s, a.
z
* s};
33
}
34
KOKKOS_INLINE_FUNCTION
float
dot3
(
F3
a,
F3
b) {
35
return
a.
x
* b.
x
+ a.
y
* b.
y
+ a.
z
* b.
z
;
36
}
37
KOKKOS_INLINE_FUNCTION
float
len3
(
F3
v) {
38
return
Kokkos::sqrt(
dot3
(v, v));
39
}
40
KOKKOS_INLINE_FUNCTION
F3
cross3v
(
F3
a,
F3
b) {
41
return
F3
{a.
y
* b.
z
- a.
z
* b.
y
, a.
z
* b.
x
- a.
x
* b.
z
, a.
x
* b.
y
- a.
y
* b.
x
};
42
}
43
KOKKOS_INLINE_FUNCTION
F4
cross3
(
F4
a,
F4
b) {
44
return
F4
{a.
y
* b.
z
- a.
z
* b.
y
, a.
z
* b.
x
- a.
x
* b.
z
, a.
x
* b.
y
- a.
y
* b.
x
, 0.0f};
45
}
46
47
// Load an F3 / F4 from a multidimensional Kokkos View row (templated => no per-header duplication).
48
template
<
class
V>
49
KOKKOS_INLINE_FUNCTION
F3
ldF3
(
const
V& v,
int
i) {
50
return
F3
{v(i, 0), v(i, 1), v(i, 2)};
51
}
52
template
<
class
V>
53
KOKKOS_INLINE_FUNCTION
F4
ldF4
(
const
V& v,
int
i) {
54
return
F4
{v(i, 0), v(i, 1), v(i, 2), v(i, 3)};
55
}
56
57
// --- quaternion rotate (q = {x,y,z,w}) — copy of math_utils.cuh rotate_vector/inv_rotate_vector
58
// ---
59
KOKKOS_INLINE_FUNCTION
F3
rotateVector
(
F4
q,
F3
v) {
60
const
F3
qv{q.
x
, q.
y
, q.
z
};
61
const
F3
t =
scale3
(
cross3v
(qv, v), 2.0f);
62
return
add3
(
add3
(v,
scale3
(t, q.
w
)),
cross3v
(qv, t));
63
}
64
KOKKOS_INLINE_FUNCTION
F3
invRotateVector
(
F4
q,
F3
v) {
65
return
rotateVector
(
F4
{-q.
x
, -q.
y
, -q.
z
, q.
w
}, v);
66
}
67
KOKKOS_INLINE_FUNCTION
F4
quatInverse
(
F4
q) {
68
return
F4
{-q.
x
, -q.
y
, -q.
z
, q.
w
};
69
}
70
KOKKOS_INLINE_FUNCTION
F4
quatMult
(
F4
a,
F4
b) {
71
return
F4
{
72
a.
w
* b.
x
+ a.
x
* b.
w
+ a.
y
* b.
z
- a.
z
* b.
y
, a.
w
* b.
y
- a.
x
* b.
z
+ a.
y
* b.
w
+ a.
z
* b.
x
,
73
a.
w
* b.
z
+ a.
x
* b.
y
- a.
y
* b.
x
+ a.
z
* b.
w
, a.
w
* b.
w
- a.
x
* b.
x
- a.
y
* b.
y
- a.
z
* b.
z
};
74
}
75
76
// --- analytic SDFs (canonical/unit space) — copy of shapes/sdf_analytic.cuh ---
77
enum
ShapeKind
{
SHAPE_GRID_SDF
= 0,
SPHERE
= 1,
HOLLOW_CYLINDER
= 2,
BOX
= 3 };
78
79
KOKKOS_INLINE_FUNCTION
float
sdfSphere
(
F3
p,
F4
params) {
80
return
len3
(p) - params.
x
;
81
}
82
83
KOKKOS_INLINE_FUNCTION
float
sdfHollowCylinder
(
F3
p,
F4
params) {
84
const
float
r_outer = params.
x
, h = params.
y
, thick = params.
z
;
85
const
float
r = Kokkos::sqrt(p.
x
* p.
x
+ p.
z
* p.
z
);
86
const
float
r_mid = r_outer - thick * 0.5f;
87
const
float
dx = Kokkos::fabs(r - r_mid) - thick * 0.5f;
88
const
float
dy = Kokkos::fabs(p.
y
) - h * 0.5f;
89
const
float
ox = Kokkos::fmax(dx, 0.0f), oy = Kokkos::fmax(dy, 0.0f);
90
const
float
outside = Kokkos::sqrt(ox * ox + oy * oy);
91
const
float
inside = Kokkos::fmin(Kokkos::fmax(dx, dy), 0.0f);
92
return
outside + inside;
93
}
94
95
KOKKOS_INLINE_FUNCTION
float
sdfBox
(
F3
p,
F4
params) {
96
const
float
dx = Kokkos::fabs(p.
x
) - params.
x
;
97
const
float
dy = Kokkos::fabs(p.
y
) - params.
y
;
98
const
float
dz = Kokkos::fabs(p.
z
) - params.
z
;
99
const
float
ox = Kokkos::fmax(dx, 0.0f), oy = Kokkos::fmax(dy, 0.0f), oz = Kokkos::fmax(dz, 0.0f);
100
const
float
outside = Kokkos::sqrt(ox * ox + oy * oy + oz * oz);
101
const
float
inside = Kokkos::fmin(Kokkos::fmax(dx, Kokkos::fmax(dy, dz)), 0.0f);
102
return
outside + inside;
103
}
104
105
KOKKOS_INLINE_FUNCTION
float
sdfEval
(
F3
p,
int
type,
F4
params) {
106
if
(type ==
SPHERE
)
107
return
sdfSphere
(p, params);
108
if
(type ==
HOLLOW_CYLINDER
)
109
return
sdfHollowCylinder
(p, params);
110
if
(type ==
BOX
)
111
return
sdfBox
(p, params);
112
return
1e9f;
// grid SDF (texture) not yet ported
113
}
114
115
}
// namespace peclet::dem
116
117
#endif
// DEM_PORTABLE_HPP
peclet::dem
Definition
broadphase_arborx.hpp:19
peclet::dem::quatMult
F4 quatMult(F4 a, F4 b)
Definition
dem_portable.hpp:70
peclet::dem::quatInverse
F4 quatInverse(F4 q)
Definition
dem_portable.hpp:67
peclet::dem::sdfEval
float sdfEval(F3 p, int type, F4 params)
Definition
dem_portable.hpp:105
peclet::dem::sdfHollowCylinder
float sdfHollowCylinder(F3 p, F4 params)
Definition
dem_portable.hpp:83
peclet::dem::cross3v
F3 cross3v(F3 a, F3 b)
Definition
dem_portable.hpp:40
peclet::dem::sdfBox
float sdfBox(F3 p, F4 params)
Definition
dem_portable.hpp:95
peclet::dem::invRotateVector
F3 invRotateVector(F4 q, F3 v)
Definition
dem_portable.hpp:64
peclet::dem::ldF3
F3 ldF3(const V &v, int i)
Definition
dem_portable.hpp:49
peclet::dem::ShapeKind
ShapeKind
Definition
dem_portable.hpp:77
peclet::dem::HOLLOW_CYLINDER
@ HOLLOW_CYLINDER
Definition
dem_portable.hpp:77
peclet::dem::BOX
@ BOX
Definition
dem_portable.hpp:77
peclet::dem::SPHERE
@ SPHERE
Definition
dem_portable.hpp:77
peclet::dem::SHAPE_GRID_SDF
@ SHAPE_GRID_SDF
Definition
dem_portable.hpp:77
peclet::dem::rotateVector
F3 rotateVector(F4 q, F3 v)
Definition
dem_portable.hpp:59
peclet::dem::dot3
float dot3(F3 a, F3 b)
Definition
dem_portable.hpp:34
peclet::dem::sub3
F3 sub3(F3 a, F3 b)
Definition
dem_portable.hpp:28
peclet::dem::ldF4
F4 ldF4(const V &v, int i)
Definition
dem_portable.hpp:53
peclet::dem::scale3
F3 scale3(F3 a, float s)
Definition
dem_portable.hpp:31
peclet::dem::len3
float len3(F3 v)
Definition
dem_portable.hpp:37
peclet::dem::add3
F3 add3(F3 a, F3 b)
Definition
dem_portable.hpp:25
peclet::dem::sdfSphere
float sdfSphere(F3 p, F4 params)
Definition
dem_portable.hpp:79
peclet::dem::cross3
F4 cross3(F4 a, F4 b)
Definition
dem_portable.hpp:43
peclet::dem::F3
Definition
dem_portable.hpp:17
peclet::dem::F3::y
float y
Definition
dem_portable.hpp:18
peclet::dem::F3::z
float z
Definition
dem_portable.hpp:18
peclet::dem::F3::x
float x
Definition
dem_portable.hpp:18
peclet::dem::F4
Definition
dem_portable.hpp:20
peclet::dem::F4::y
float y
Definition
dem_portable.hpp:21
peclet::dem::F4::w
float w
Definition
dem_portable.hpp:21
peclet::dem::F4::x
float x
Definition
dem_portable.hpp:21
peclet::dem::F4::z
float z
Definition
dem_portable.hpp:21
src
dem_portable.hpp
Generated by
1.9.8