-
Notifications
You must be signed in to change notification settings - Fork 3
/
Kernels.hpp
258 lines (223 loc) · 7.64 KB
/
Kernels.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#pragma once
#include "Math.hpp"
#include "Utils.hpp"
#include "Vector.hpp"
namespace Pvl {
/// \brief Base class for all SPH kernels.
///
/// Provides an interface for computing kernel values and gradients. All derived class must implement method
/// <code>valueImpl</code> and <code>gradImpl</code>. Both function take SQUARED value of dimensionless
/// distance q as a parameter. Function value returns the kernel value, grad returns gradient DIVIDED BY q.
template <typename TDerived, typename Float, int Dim>
class Kernel : public Noncopyable {
public:
using Vec = Vector<Float, Dim>;
Kernel() = default;
/// Value of kernel at given point
/// this should be called only once for a pair of particles as there is expensive division
/// \todo Potentially precompute the 3rd power ...
Float value(const Vec& r, const Float h) const noexcept {
PVL_ASSERT(h > 0.f);
const Float hInv = 1.f / h;
return pow<Dim>(hInv) * impl().valueImpl(normSqr(r) * sqr(hInv));
}
Vec grad(const Vec& r, const Float h) const noexcept {
PVL_ASSERT(h > 0.f);
const Float hInv = 1.f / h;
return r * pow<Dim + 2>(hInv) * impl().gradImpl(normSqr(r) * sqr(hInv));
}
private:
const TDerived& impl() const noexcept {
return static_cast<const TDerived&>(*this);
}
};
/// \brief A look-up table approximation of the kernel.
///
/// Can be constructed from any SPH kernel. Use this class exclusively for any high-performance computations,
/// it is always faster than using kernel functions directly (except for trivial kerneles, such as \ref
/// TriangleKernel). The precision difference is about 1.e-6.
template <typename Float, int Dim>
class LutKernel : public Kernel<LutKernel<Float, Dim>, Float, Dim> {
private:
static constexpr int NEntries = 40000;
/// \todo replace with StaticArray?
Float values[NEntries];
Float grads[NEntries];
Float rad = 0.f;
Float qSqrToIdx = 0.f;
public:
LutKernel() {
/// \todo initialize, otherwise compiler complains about using uninitialized values
qSqrToIdx = NAN;
for (Float& v : values) {
v = NAN;
}
for (Float& g : grads) {
g = NAN;
}
}
LutKernel(const LutKernel& other) {
*this = other;
}
LutKernel& operator=(const LutKernel& other) {
rad = other.rad;
qSqrToIdx = other.qSqrToIdx;
for (int i = 0; i < NEntries; ++i) {
values[i] = other.values[i];
grads[i] = other.grads[i];
}
return *this;
}
/// \brief Constructs LUT kernel given an exact SPH kernel.
template <typename TKernel,
typename = std::enable_if_t<!std::is_same<std::decay_t<TKernel>, LutKernel>::value>>
LutKernel(TKernel&& source) {
rad = source.radius();
PVL_ASSERT(rad > 0.f);
const Float radInvSqr = 1.f / (rad * rad);
qSqrToIdx = Float(NEntries) * radInvSqr;
for (int i = 0; i < NEntries; ++i) {
const Float qSqr = Float(i) / qSqrToIdx;
values[i] = source.valueImpl(qSqr);
grads[i] = source.gradImpl(qSqr);
}
/// \todo re-normalize?
}
bool initialized() const {
return rad > 0.f;
}
Float radius() const noexcept {
return rad;
}
Float valueImpl(const Float qSqr) const noexcept {
PVL_ASSERT(qSqr >= 0.f);
PVL_ASSERT(initialized());
if (qSqr >= sqr(rad)) {
// outside of kernel support
return 0.f;
}
// linear interpolation of stored values
const Float floatIdx = qSqrToIdx * qSqr;
const int idx1 = Size(floatIdx);
PVL_ASSERT(idx1 < NEntries);
const int idx2 = idx1 + 1;
const Float ratio = floatIdx - Float(idx1);
PVL_ASSERT(ratio >= 0.f && ratio < 1.f);
return values[idx1] * (1.f - ratio) + (int(idx2 < NEntries) * values[idx2]) * ratio;
}
Float gradImpl(const Float qSqr) const noexcept {
PVL_ASSERT(qSqr >= 0.f);
PVL_ASSERT(initialized());
if (qSqr >= sqr(rad)) {
// outside of kernel support
return 0.f;
}
const Float floatIdx = qSqrToIdx * qSqr;
const int idx1 = Size(floatIdx);
PVL_ASSERT(unsigned(idx1) < unsigned(NEntries));
const int idx2 = idx1 + 1;
const Float ratio = floatIdx - Float(idx1);
PVL_ASSERT(ratio >= 0.f && ratio < 1.f);
return grads[idx1] * (1.f - ratio) + (int(idx2 < NEntries) * grads[idx2]) * ratio;
}
};
/// \brief Cubic spline (M4) kernel
template <typename Float, int Dim>
class CubicSpline : public Kernel<CubicSpline<Float, Dim>, Float, Dim> {
private:
const Float normalization[3] = { 2.f / 3.f, 10.f / (7.f * PI), 1.f / PI };
public:
Float radius() const {
return 2.f;
}
// template <bool TApprox = false>
Float valueImpl(const Float qSqr) const {
const Float q = sqrt(qSqr);
PVL_ASSERT(q >= 0);
if (q < 1.f) {
return normalization[Dim - 1] * (0.25f * pow<3>(2.f - q) - pow<3>(1.f - q));
}
if (q < 2.f) {
return normalization[Dim - 1] * (0.25f * pow<3>(2.f - q));
}
return 0.f; // compact within 2r radius
}
// template <bool TApprox = false>
Float gradImpl(const Float qSqr) const {
const Float q = sqrt(qSqr);
if (q == 0.f) {
// gradient of kernel is 0 at q = 0, but here we divide by q,
// the expression grad/q has a finite limit for q->0
return -3.f * normalization[Dim - 1];
}
if (q < 1.f) {
return (1.f / q) * normalization[Dim - 1] * (-0.75f * pow<2>(2.f - q) + 3.f * pow<2>(1.f - q));
}
if (q < 2.f) {
return (1.f / q) * normalization[Dim - 1] * (-0.75f * pow<2>(2.f - q));
}
return 0.f;
}
};
/*
/// \brief Gaussian kernel
///
/// Clamped to zero at radius 5, the error is therefore about exp(-5^2) = 10^-11.
template <Size D>
class Gaussian : public Kernel<Gaussian<D>, D> {
private:
const Float normalization[3] = { 1.f / sqrt(PI), 1.f / PI, 1.f / (PI * sqrt(PI)) };
public:
Float radius() const {
return 5.f;
}
Float valueImpl(const Float qSqr) const {
if (qSqr >= sqr(radius())) {
return 0.f;
}
return normalization[D - 1] * exp(-qSqr);
}
Float gradImpl(const Float qSqr) const {
if (qSqr >= sqr(radius())) {
return 0.f;
}
if (qSqr == 0.f) {
return -2.f * normalization[D - 1];
}
const Float q = sqrt(qSqr);
return normalization[D - 1] / q * exp(-qSqr) * (-2.f * q);
}
};
/// \brief Triangular (piecewise linear) kernel.
///
/// Does not have continuous derivatives, mainly for testing purposes and non-SPH applications.
template <Size D>
class TriangleKernel : public Kernel<TriangleKernel<D>, D> {
private:
const Float normalization[3] = { 1.f, 3.f / PI, 3.f / PI };
public:
Float radius() const {
return 1.f;
}
Float valueImpl(const Float qSqr) const {
if (qSqr >= sqr(radius())) {
return 0.f;
}
const Float q = sqrt(qSqr);
return normalization[D - 1] * (1.f - q);
}
Float gradImpl(const Float qSqr) const {
if (qSqr >= sqr(radius())) {
return 0.f;
}
// unfortunately this gradient is nonzero at q->0, so grad/q diverges;
// let's return a reasonable value to avoid numerical problems
if (qSqr == 0.f) {
return -100.f;
}
const Float q = sqrt(qSqr);
return -normalization[D - 1] / q;
}
};
*/
} // namespace Pvl