-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsph_kernel.html
400 lines (359 loc) · 12.7 KB
/
sph_kernel.html
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<style>
body {font-family: Helvetica, sans-serif;}
table {background-color:#CCDDEE;text-align:left}
</style>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js"],
jax: ["input/TeX", "output/HTML-CSS"],
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
processEscapes: true
},
"HTML-CSS": { fonts: ["TeX"] }
});
</script>
<script type="text/javascript" aync src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js"></script>
<script src="https://cdn.plot.ly/plotly-2.5.1.min.js"></script>
<title>SPH Kernel</title>
</head>
<body>
<main>
<h1 style="text-align:center">SPH Kernel</h1>
<table style="align_center;border-radius: 20px;padding: 20px;margin:auto">
<col width="1000">
<tr>
<td>
<div id="plotOutput" style="width: 1000px; height: 600px;border:2px solid #000000;border-radius: 0px;background-color:#EEEEEE"></div>
</td>
</tr>
<tr>
<td><table style="margin:20px">
<col width="200" style="padding-right:10px">
<col width="100">
<tr>
<td><label for="supportRadius">Support radius</label></td>
<td><input type="text" id="textInput" value="0.2" readonly></td>
</tr>
<tr>
<td></td>
<td><input onchange="document.getElementById('textInput').value=this.value;plot.reset()" id="supportRadius" value="0.2" type="range" min="0.01" max="1" step="0.01"></td>
</tr>
<tr>
<td><label for="kernelFct">Kernel function</label></td>
<td><select onchange="plot.reset()" id="kernelFct" size="1">
<option selected="selected">Cubic spline</option>
<option>Wendland C2</option>
<option>Quintic Spline</option>
</select>
</td>
</tr>
</table></td>
</tr>
<tr><td>
<h2>SPH kernel functions:</h2>
<p>In the following we investigate different SPH kernel functions and their derivatives. A kernel function $W(\mathbf{r}, h)$ with the smoothing length $h$ must have the following properties:</p>
<ul>
<li>Normalization condition: $\int_{\mathbb{R}^d} W(\mathbf x - \mathbf x^*, h) d \mathbf x^* = 1 $</li>
<li>Symmetry condition: $W(\mathbf x - \mathbf x^*, h) = W(\mathbf x^* - \mathbf x, h)$</li>
<li>Dirac-$\delta$ condition: $\lim_{h \rightarrow 0} W(\mathbf x - \mathbf x^*, h) = \delta(\mathbf x - \mathbf x^*)$</li>
<li>Non-negative condition: $W(\mathbf x - \mathbf x^*, h) \geq 0$</li>
<li>Compact support condition with support radius $r$: $W(\mathbf x - \mathbf x^*, h) = 0 \quad \text{if} \quad \|\mathbf x - \mathbf x^*\| > r$</li>
</ul>
<p>Furthermore, a kernel function should be at least twice continuously differentiable to enable a consistent discretization of 2nd-order partial
differential equations (PDEs).
The smoothing length controls defines how strongly a value at position $\mathbf x$ is influenced by the values in its neighborhood. Hence, a larger value $h$ yields a larger smoothing effect.</p>
<h3>Popular kernel functions</h3>
<h4>Cubic spline kernel [Mon92]</h4>
$$W(q) = \alpha_d \begin{cases}
\frac23 - q^2 + \frac12 q^3 & 0 \leq q < 1 \\
\frac16 (2-q)^3 & 1 \leq q < 2 \\
0 & q \geq 2,
\end{cases}$$
where $q = \frac{\|r\|}{h}$ the kernel normalization factors $\alpha_d$ for the respective
dimensions $d$ are $\alpha_1 = \frac1h$, $\alpha_2 = \frac{15}{7\pi h^2}$, and $\alpha_3 = \frac{3}{2\pi h^3}$.
<h4>Wendland C2 kernel for 1D [Wen95]</h4>
$$W(q) = \alpha_d \begin{cases}
\left (1-\frac{q}{2} \right )^3 \left (\frac32 q+1 \right ) & 0 \leq q \leq 2 \\
0 & q > 2,
\end{cases}$$
where the kernel normalization factor is $\alpha_1 = \frac{5}{8h}$.
<h4>Quintic spline kernel [LL10]</h4>
$$W(q) = \alpha_d \begin{cases}
(3-q)^5 - 6(2-q)^5 + 15 (1-q)^5 & 0 \leq q \leq 1 \\
(3-q)^5 - 6(2-q)^5 & 1 < q \leq 2 \\
(3-q)^5 & 2 < q \leq 3 \\
0 & q > 3,
\end{cases}$$
where the kernel normalization factors are $\alpha_1 = \frac{1}{120h}$, $\alpha_2 = \frac{7}{478 \pi h^2}$, and $\alpha_3 = \frac{1}{120 \pi h^3}$.
<h3>Derivatives</h3>
<p>The gradient of the kernel function is
$$\nabla W = \frac{\partial W}{\partial \mathbf r} = \frac{\partial W(q)}{\partial q} \cdot \frac{\partial q}{\partial \mathbf r} = \frac{\partial W(q)}{\partial q} \frac{\mathbf r} {h \| \mathbf r \|}$$
and the Laplacian
$$\nabla^2 W = \frac{\partial^2 W(q)}{\partial q^2} \frac{1}{h^2} + \frac{\partial W(q)}{\partial q} \nabla^2 q$$
</p>
<h3>References</h3>
<ul>
<li>[Mon92] J. Monaghan. Smoothed Particle Hydrodynamics. Annual Review of Astronomy and Astrophysics, 1992, 30, 543-574</li>
<li>[Wen95] H. Wendland. Piecewise polynomial, positive definite and compactly supported radial functions of minimal degree. Advances in computational Mathematics 4.1, 389-396, 1995</li>
<li>[LL10] M. Liu, G. Liu. Smoothed Particle Hydrodynamics (SPH): an Overview and Recent Developments. Archives of Computational Methods in Engineering, 2010, 17, 25-76</li>
</ul>
</td></tr>
</table>
</main>
<script id="simulation_code" type="text/javascript">
class Plot
{
constructor()
{
this.supportRadius = 0.1; // support radius
this.reset();
}
reset()
{
this.supportRadius = parseFloat(document.getElementById('supportRadius').value);
this.kernelFct = document.getElementById('kernelFct').value;
this.plot();
}
// Cubic spline kernel 1D
// W(r) = alpha * (2/3 - q^2 + 1/2 q^3) if 0 <= q < 1
// = alpha * 1/6 * (2-q)^3 if 1 <= q < 2
// = 0 otherwise
// q = |r|/h
cubicKernel_1D(r)
{
let h = 0.5 * this.supportRadius;
let q = Math.abs(r) / h;
let alpha = 1.0/h; // normalization factor for 1D
if ((q >= 0.0) && (q < 1.0))
return alpha * (2.0/3.0 - q*q + 0.5 * Math.pow(q,3));
else if ((q >= 1.0) && (q < 2.0))
return alpha/6.0 * Math.pow(2.0 - q, 3);
else
return 0.0;
}
// Gradient of cubic spline kernel 1D
// ∂W/∂q = alpha * (-2 q + 1.5 q^2) if 0 <= q < 1
// = -alpha * 0.5 * (2-q)^2 if 1 <= q < 2
// = 0 otherwise
// q = |r|/h
// grad W(r) = ∂W/∂q * grad q
// grad q = r / (|r| h)
gradCubicKernel_1D(r)
{
let h = 0.5 * this.supportRadius;
let q = Math.abs(r) / h;
let gradq = r / (Math.abs(r)* h);
let dW_dq = 0.0;
let alpha = 1.0/h;
if ((q >= 0.0) && (q < 1.0))
dW_dq = alpha * (-2.0*q + 1.5 * q*q);
else if ((q >= 1.0) && (q < 2.0))
dW_dq = -alpha/2.0 * Math.pow(2.0 - q, 2);
return dW_dq * gradq;
}
// Laplacian of cubic spline kernel 1D
// ∂^2 W/∂^2 q = alpha * (-2 + 3 q) if 0 <= q < 1
// = alpha * (2-q) if 1 <= q < 2
// = 0 otherwise
// q = |r|/h
// in 1D: ∂^2 W/∂^2 r = ∂^2 W/∂^2 q * 1/h^2
laplaceCubicKernel_1D(r)
{
let h = 0.5 * this.supportRadius;
let q = Math.abs(r) / h;
let d2W_d2q = 0.0;
let alpha = 1.0/h;
if ((q >= 0.0) && (q < 1.0))
d2W_d2q = alpha * (-2.0 + 3.0*q);
else if ((q >= 1.0) && (q < 2.0))
d2W_d2q = alpha * (2.0-q);
return d2W_d2q * 1.0/(h*h);
}
// Wendland C2 kernel 1D
// W(r) = alpha * (1 - 0.5 q)^3 (1.5 q + 1) if 0 <= q <= 2
// = 0 otherwise
// q = |r|/h
WendlandC2_1D(r)
{
let h = 0.5 * this.supportRadius;
let q = Math.abs(r) / h;
let alpha = 5.0/(8.0*h);
if ((q >= 0.0) && (q <= 2.0))
//return alpha * Math.pow(1.0-0.5*q, 3)*(1.5*q + 1.0);
return alpha * (1.0 - 1.5*q*q + q*q*q - 3.0/16.0*q*q*q*q);
else
return 0.0;
}
// Gradient of Wendland C2 kernel 1D
// ∂W/∂q = alpha * (-3q + 3q^2 - 0.75q^3) if 0 <= q <= 2
// = 0 otherwise
// q = |r|/h
// grad W(r) = ∂W/∂q * grad q
// grad q = r / (|r| h)
gradWendlandC2_1D(r)
{
let h = 0.5 * this.supportRadius;
let q = Math.abs(r) / h;
let alpha = 5.0/(8.0*h);
let gradq = r / (Math.abs(r)* h);
let dW_dq = 0.0;
if ((q >= 0.0) && (q <= 2.0))
//dW_dq = alpha * (-1.5 * Math.pow(1.0-0.5*q, 2)*(1.5*q + 1.0) + Math.pow(1.0-0.5*q, 3) * 1.5);
dW_dq = alpha * (-3.0*q + 3.0 * q*q - 0.75 * q*q*q);
return dW_dq * gradq;
}
// Laplacian of Wendland C2 kernel 1D
// ∂^2 W/∂^2 q = alpha * (-3 + 6q - 2.25 q^2) if 0 <= q <= 2
// = 0 otherwise
// q = |r|/h
// in 1D: ∂^2 W/∂^2 r = ∂^2 W/∂^2 q * 1/h^2
laplaceWendlandC2_1D(r)
{
let h = 0.5 * this.supportRadius;
let q = Math.abs(r) / h;
let d2W_d2q = 0.0;
let alpha = 1.0/h;
if ((q >= 0.0) && (q <= 2.0))
d2W_d2q = alpha * (-3.0 + 6.0*q - 9.0/4.0*q*q);
return d2W_d2q * 1.0/(h*h);
}
// Quintic Spline kernel 1D
// W(r) = alpha * ((3-q)^5 - (2-q)^5 + 15(1-q)^5) if 0 <= q <= 1
// = alpha * ((3-q)^5 - (2-q)^5) if 1 < q <= 2
// = alpha * (3-q)^5 if 2 < q <= 3
// = 0 otherwise
// q = |r|/h
QuinticSpline_1D(r)
{
let h = 0.5 * this.supportRadius;
let q = Math.abs(r) / h;
let alpha = 1.0/(120.0*h);
if ((q >= 0.0) && (q <= 1.0))
return alpha * (Math.pow(3.0-q, 5) - 6.0*Math.pow(2.0-q, 5) + 15.0*Math.pow(1.0-q, 5));
else if ((q > 1.0) && (q <= 2.0))
return alpha * (Math.pow(3.0-q, 5) - 6.0*Math.pow(2.0-q, 5));
else if ((q > 2.0) && (q <= 3.0))
return alpha * (Math.pow(3.0-q, 5));
else
return 0.0;
}
// Gradient of Quintic Spline kernel 1D
// ∂W/∂q = alpha * (-5(3-q)^4 + 30(2-q)^4 - 75(1-q)^4) if 0 <= q <= 1
// = alpha * (-5(3-q)^4 + 30(2-q)^4) if 1 < q <= 2
// = alpha * (-5(3-q)^4) if 2 < q <= 3
// = 0 otherwise
// q = |r|/h
// grad W(r) = ∂W/∂q * grad q
// grad q = r / (|r| h)
gradQuinticSpline_1D(r)
{
let h = 0.5 * this.supportRadius;
let q = Math.abs(r) / h;
let alpha = 1.0/(120.0*h);
let gradq = r / (Math.abs(r)* h);
let dW_dq = 0.0;
if ((q >= 0.0) && (q <= 1.0))
dW_dq = alpha * (-5.0*Math.pow(3.0-q, 4) + 30.0*Math.pow(2.0-q, 4) - 75.0*Math.pow(1.0-q, 4));
else if ((q > 1.0) && (q <= 2.0))
dW_dq = alpha * (-5.0*Math.pow(3.0-q, 4) + 30.0*Math.pow(2.0-q, 4));
else if ((q > 2.0) && (q <= 3.0))
dW_dq = alpha * (-5.0*Math.pow(3.0-q, 4));
return dW_dq * gradq;
}
// Laplacian of Quintic Spline kernel 1D
// ∂^2 W/∂^2 q = alpha * (20(3-q)^3 - 120(2-q)^3 + 300(1-q)^3) if 0 <= q <= 1
// = alpha * (20(3-q)^3 - 120(2-q)^3) if 1 < q <= 2
// = alpha * (20(3-q)^3) if 2 < q <= 3
// = 0 otherwise
// q = |r|/h
// in 1D: ∂^2 W/∂^2 r = ∂^2 W/∂^2 q * 1/h^2
laplaceQuinticSpline_1D(r)
{
let h = 0.5 * this.supportRadius;
let q = Math.abs(r) / h;
let d2W_d2q = 0.0;
let alpha = 1.0/(120.0*h);
if ((q >= 0.0) && (q <= 1.0))
d2W_d2q = alpha * (20.0*Math.pow(3.0-q, 3) - 120.0*Math.pow(2.0-q, 3) + 300.0*Math.pow(1.0-q, 3));
else if ((q > 1.0) && (q <= 2.0))
d2W_d2q = alpha * (20.0*Math.pow(3.0-q, 3) - 120.0*Math.pow(2.0-q, 3));
else if ((q > 2.0) && (q <= 3.0))
d2W_d2q = alpha * (20.0*Math.pow(3.0-q, 3));
return d2W_d2q * 1.0/(h*h);
}
plot()
{
let x = -1.1 * this.supportRadius;
let num_steps = 1000;
let stepSize = 2.2 * this.supportRadius / (num_steps-1);
let xValues = [];
let yValues = [];
let yValues_grad = [];
let yValues_lap = [];
// compute function values for num_steps
for (let i = 0; i <= num_steps; i++)
{
xValues.push(x);
if (this.kernelFct == "Wendland C2")
{
yValues.push(this.WendlandC2_1D(x));
yValues_grad.push(0.1*this.gradWendlandC2_1D(x));
yValues_lap.push(0.001 * this.laplaceWendlandC2_1D(x));
}
else if (this.kernelFct == "Quintic Spline")
{
yValues.push(this.QuinticSpline_1D(x));
yValues_grad.push(0.1*this.gradQuinticSpline_1D(x));
yValues_lap.push(0.001 * this.laplaceQuinticSpline_1D(x));
}
else
{
yValues.push(this.cubicKernel_1D(x));
yValues_grad.push(0.1*this.gradCubicKernel_1D(x));
yValues_lap.push(0.001 * this.laplaceCubicKernel_1D(x));
}
x += stepSize;
}
let trace_cubic = {
x: xValues,
y: yValues,
name: "kernel"
};
let trace_cubic_grad = {
x: xValues,
y: yValues_grad,
name: "gradient (* 0.1)"
};
let trace_cubic_lap = {
x: xValues,
y: yValues_lap,
name: "Laplacian (* 0.001)"
};
let data = [trace_cubic, trace_cubic_grad, trace_cubic_lap];
let title = this.kernelFct;
let layout = {
title: title,
width: 1000,
height: 600,
xaxis: {
range: [-0.5,0.5],
},
yaxis: {
range: [-8.6,8.6],
}
};
// plot values
Plotly.newPlot('plotOutput', data, layout);
}
}
plot = new Plot();
plot.reset();
</script>
</body>
</html>