Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add all the other typically used smoothing kernels for SPH #237

Merged
merged 41 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
f90f017
Merge remote-tracking branch 'origin/main' into main
svchb Sep 5, 2023
372d67c
Merge branch 'trixi-framework:main' into main
svchb Sep 7, 2023
f564478
Merge branch 'main' of github.com:svchb/TrixiParticles.jl
svchb Sep 20, 2023
9c9e182
Merge branch 'trixi-framework:main' into main
svchb Sep 25, 2023
90897f6
Merge branch 'main' of github.com:svchb/TrixiParticles.jl
svchb Sep 29, 2023
1df6912
Merge branch 'main' of github.com:svchb/TrixiParticles.jl
svchb Oct 5, 2023
f09d7ae
add gaussian kernels
svchb Oct 7, 2023
546d0db
add all the other ones
svchb Oct 8, 2023
ded8954
format
svchb Oct 8, 2023
134ccc9
Merge branch 'main' into add_kernels
svchb Oct 8, 2023
a9238b2
fix
svchb Oct 9, 2023
cffd7b2
format
svchb Oct 9, 2023
85fdb41
Merge branch 'add_kernels' of github.com:svchb/TrixiParticles.jl into…
svchb Oct 9, 2023
7cd3785
fixes
svchb Oct 10, 2023
c4136a9
fix
svchb Oct 10, 2023
2748762
add testcase and fix wendlandc4
svchb Oct 10, 2023
73016ad
format
svchb Oct 10, 2023
f87d4e3
add QuadGK
svchb Oct 10, 2023
2bbccb0
Merge branch 'main' into add_kernels
svchb Oct 18, 2023
868712b
Merge branch 'main' into add_kernels
svchb Oct 23, 2023
6d678ae
Merge remote-tracking branch 'refs/remotes/origin/main'
svchb Oct 26, 2023
bd4d11b
update
svchb Oct 27, 2023
85f8ee9
format
svchb Oct 27, 2023
cebca60
Merge branch 'main' into add_kernels
svchb Oct 27, 2023
68a0c98
Merge branch 'main'
svchb Oct 31, 2023
e9ca620
Merge branch 'main' into add_kernels
svchb Nov 1, 2023
b22d6c2
Fix tests
svchb Nov 8, 2023
09f9b38
review
svchb Nov 8, 2023
8f64949
Merge branch 'main' into add_kernels
svchb Nov 8, 2023
3a800ee
format
svchb Nov 8, 2023
2c0b5fb
Merge branch 'add_kernels' of github.com:svchb/TrixiParticles.jl into…
svchb Nov 8, 2023
ab3b5f4
[skip ci] add information regarding smoothing L
svchb Nov 10, 2023
c125996
fix docs
svchb Nov 10, 2023
e15f2b0
code review
svchb Nov 10, 2023
05aa84b
Merge branch 'main' into add_kernels
svchb Nov 10, 2023
72eebf8
[skip ci] fix table
svchb Nov 10, 2023
9e8a4a5
Merge branch 'add_kernels' of github.com:svchb/TrixiParticles.jl into…
svchb Nov 10, 2023
3b62cfa
[skip ci] change table
svchb Nov 10, 2023
3132665
revert change
svchb Nov 10, 2023
b975d79
[skip ci]
svchb Nov 10, 2023
a2a6778
[skip ci] review
svchb Nov 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 102 additions & 55 deletions src/general/smoothing_kernels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ of dimensions.

We provide the following smoothing kernels:

| Smoothing Kernel | Compact Support |
| :---------------------------------------- | :---------------- |
| [`SchoenbergCubicSplineKernel`](@ref) | $[0, 2h]$ |
| [`SchoenbergQuarticSplineKernel`](@ref) | $[0, 2.5h]$ |
| [`SchoenbergQuinticSplineKernel`](@ref) | $[0, 3h]$ |
| Smoothing Kernel | Compact Support | Typ. Smoothing Length | Recommended Application | Stability |
| :---------------------------------------- | :---------------- | :-------------------- | :---------------------- | :-------- |
| [`SchoenbergCubicSplineKernel`](@ref) | $[0, 2h]$ | $1.1 to 1.3$ | General + sharp waves | ++ |
svchb marked this conversation as resolved.
Show resolved Hide resolved
| [`SchoenbergQuarticSplineKernel`](@ref) | $[0, 2.5h]$ | $1.1 to 1.5$ | General | +++ |
| [`SchoenbergQuinticSplineKernel`](@ref) | $[0, 3h]$ | $1.1 to 1.5$ | General | ++++ |
| [`GaussianKernel`](@ref) | $[0, 3h]$ | $1.0 to 1.5$ | Literature | +++++ |
| [`WendlandC2Kernel`](@ref) | $[0, 1h]$ | $2.5 to 4.0$ | **Try first** | ++++ |
| [`WendlandC4Kernel`](@ref) | $[0, 1h]$ | $3.0 to 4.5$ | General | +++++ |
| [`WendlandC6Kernel`](@ref) | $[0, 1h]$ | $3.5 to 5.0$ | General | +++++ |
| [`Poly6Kernel`](@ref) | $[0, 1h]$ | $1.5 to 2.5$ | Literature | + |
| [`SpikyKernel`](@ref) | $[0, 1h]$ | $1.5 to 3.0$ | Sharp corners + waves | + |
svchb marked this conversation as resolved.
Show resolved Hide resolved

!!! note "Usage"
The kernel can be called as
Expand Down Expand Up @@ -67,15 +73,19 @@ W(r, h) = \frac{\sigma_d}{h^d} e^{-r^2/h^2}

where ``d`` is the number of dimensions and

- `` \sigma_2 = \frac{1}{\pi} `` for 2D
- `` \sigma_3 = \frac{1}{\pi^{3/2}} `` for 3D
- `` \sigma_2 = \frac{1}{\pi} `` for 2D,
- `` \sigma_3 = \frac{1}{\pi^{3/2}} `` for 3D.

This kernel function has an infinite support, but in practice,
it's often truncated at a certain multiple of ``h``, such as ``3h``.

In this implementation, the kernel is truncated at `3h`,
In this implementation, the kernel is truncated at ``3h``,
so this kernel function has a compact support of ``[0, 3h]``.

The smoothing length is typically in range `[1.0 : 1.5] * particle_spacing`.

For general information and usage see [`SmoothingKernel`](@ref).

Note:
This truncation makes this Kernel not conservative,
which is beneficial in regards to stability but makes it less accurate.
svchb marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -134,6 +144,8 @@ For an analytic formula for higher order Schoenberg kernels, see (Monaghan, 1985
The largest disadvantage of Schoenberg Spline Kernel is the rather non-smooth first derivative,
which can lead to increased noise compared to other kernel variants.

The smoothing length is typically in range `[1.1 : 1.3] * particle_spacing`.
svchb marked this conversation as resolved.
Show resolved Hide resolved

For general information and usage see [`SmoothingKernel`](@ref).

## References:
Expand Down Expand Up @@ -213,6 +225,9 @@ For an analytic formula for higher order Schoenberg kernels, see (Monaghan, 1985

The largest disadvantage of Schoenberg Spline Kernel are the rather non-smooth first derivative,
which can lead to increased noise compared to other kernel variants.

The smoothing length is typically in range `[1.1 : 1.5] * particle_spacing`.

For general information and usage see [`SmoothingKernel`](@ref).

## References:
Expand Down Expand Up @@ -306,6 +321,9 @@ For an analytic formula for higher order Schoenberg kernels, see (Monaghan, 1985

The largest disadvantage of Schoenberg Spline Kernel are the rather non-smooth first derivative,
which can lead to increased noise compared to other kernel variants.

The smoothing length is typically in range `[1.1 : 1.5] * particle_spacing`.

For general information and usage see [`SmoothingKernel`](@ref).

## References:
Expand Down Expand Up @@ -376,16 +394,16 @@ abstract type WendlandKernel{NDIMS} <: SmoothingKernel{NDIMS} end
Wendland C2 kernel (Wendland, 1995), a piecewise polynomial function designed to have compact support and to
be twice continuously differentiable everywhere. Given by

\[ W(r, h) = \frac{1}{h^d} w(r/h) \]
`` W(r, h) = \frac{1}{h^d} w(r/h) ``
svchb marked this conversation as resolved.
Show resolved Hide resolved

with

\[
```math
w(q) = \sigma \begin{cases}
(1 - q)^4 (4q + 1) & \text{if } 0 \leq q < 1, \\
0 & \text{if } q \geq 1,
\end{cases}
\]
```

where `` d `` is the number of dimensions and `` \sigma `` is a normalization factor dependent on the dimension.
The normalization factor `` \sigma `` is `` 40/7\pi `` in two dimensions or `` 21/2\pi `` in three dimensions.
Expand All @@ -395,40 +413,47 @@ This kernel function has a compact support of `` [0, h] ``.
For a detailed discussion on Wendland functions and their applications in SPH, see (Dehnen & Aly, 2012).
The smoothness of these functions is also the largest disadvantage as they lose details at sharp corners.

The smoothing length is typically in range `[2.5 : 4.0] * particle_spacing`.

For general information and usage see [`SmoothingKernel`](@ref).

## References:
- Walter Dehnen & Hassan Aly.
"Improving convergence in smoothed particle hydrodynamics simulations without pairing instability".
In: Monthly Notices of the Royal Astronomical Society 425.2 (2012), pages 1068-1082.
[doi: 10.1111/j.1365-2966.2012.21439.x](https://doi.org/10.1111/j.1365-2966.2012.21439.x)
"Improving convergence in smoothed particle hydrodynamics simulations without pairing instability".
In: Monthly Notices of the Royal Astronomical Society 425.2 (2012), pages 1068-1082.
[doi: 10.1111/j.1365-2966.2012.21439.x](https://doi.org/10.1111/j.1365-2966.2012.21439.x)

svchb marked this conversation as resolved.
Show resolved Hide resolved
- Holger Wendland.
"Piecewise polynomial, positive definite and compactly supported radial functions of minimal degree."
In: Advances in computational Mathematics 4 (1995): 389-396.
In: Advances in computational Mathematics 4 (1995), pages 389-396.
[doi: 10.1007/BF02123482](https://doi.org/10.1007/BF02123482)

"""
struct WendlandC2Kernel{NDIMS} <: WendlandKernel{NDIMS} end

@fastpow @inline function kernel(kernel::WendlandC2Kernel, r::Real, h)
q = r / h

result = (1 - q)^4 * (4q + 1)

# Zero out result if q >= 1
result = ifelse(q < 1, normalization_factor(kernel, h) * (1 - q)^4 * (4q + 1), zero(q))
result = ifelse(q < 1, normalization_factor(kernel, h) * result, zero(q))

return result
end

@fastpow @inline function kernel_deriv(kernel::WendlandC2Kernel, r::Real, h)
@fastpow @muladd @inline function kernel_deriv(kernel::WendlandC2Kernel, r::Real, h)
inner_deriv = 1 / h
q = r * inner_deriv

q1_3 = (1 - q)^3
q1_4 = (1 - q)^4

result = -4 * q1_3 * (4q + 1)
result = result + q1_4 * 4

# Zero out result if q >= 1
result = ifelse(q < 1,
normalization_factor(kernel, h) * (-4 * q1_3 * (4q + 1) + q1_4 * 4) *
inner_deriv, zero(q))
normalization_factor(kernel, h) * result * inner_deriv, zero(q))

return result
end
Expand All @@ -442,30 +467,34 @@ end
Wendland C4 kernel, a piecewise polynomial function designed to have compact support and to
be four times continuously differentiable everywhere. Given by

\[ W(r, h) = \frac{1}{h^d} w(r/h) \]
`` W(r, h) = \frac{1}{h^d} w(r/h) ``

with

\[
```math
w(q) = \sigma \begin{cases}
(1 - q)^6 * (35q^2 / 3 + 6q + 1) & \text{if } 0 \leq q < 1, \\
svchb marked this conversation as resolved.
Show resolved Hide resolved
0 & \text{if } q \geq 1,
\end{cases}
\]
```

where `` d `` is the number of dimensions and `` \sigma `` is a normalization factor dependent
on the dimension. The exact value of `` \sigma `` needs to be determined based on the dimension to ensure proper normalization.
on the dimension. The normalization factor `` \sigma `` is `` 9 / \pi `` in two dimensions or `` 495 / 32\pi `` in three dimensions.

This kernel function has a compact support of `` [0, h] ``.

For a detailed discussion on Wendland functions and their applications in SPH, see (Dehnen & Aly, 2012).
The smoothness of these functions is also the largest disadvantage as they loose details at sharp corners.

The smoothing length is typically in range `[3.0 : 4.5] * particle_spacing`.

For general information and usage see [`SmoothingKernel`](@ref).

## References:
- Walter Dehnen & Hassan Aly.
"Improving convergence in smoothed particle hydrodynamics simulations without pairing instability".
In: Monthly Notices of the Royal Astronomical Society 425.2 (2012), pages 1068-1082.
[doi: 10.1111/j.1365-2966.2012.21439.x](https://doi.org/10.1111/j.1365-2966.2012.21439.x)
"Improving convergence in smoothed particle hydrodynamics simulations without pairing instability".
In: Monthly Notices of the Royal Astronomical Society 425.2 (2012), pages 1068-1082.
[doi: 10.1111/j.1365-2966.2012.21439.x](https://doi.org/10.1111/j.1365-2966.2012.21439.x)

- Holger Wendland.
"Piecewise polynomial, positive definite and compactly supported radial functions of minimal degree."
Expand All @@ -477,10 +506,10 @@ struct WendlandC4Kernel{NDIMS} <: WendlandKernel{NDIMS} end
@fastpow @inline function kernel(kernel::WendlandC4Kernel, r::Real, h)
q = r / h

result = (1 - q)^6 * (35q^2 / 3 + 6q + 1)

# Zero out result if q >= 1
result = ifelse(q < 1,
normalization_factor(kernel, h) * (1 - q)^6 * (35q^2 / 3 + 6q + 1),
zero(q))
result = ifelse(q < 1, normalization_factor(kernel, h) * result, zero(q))

return result
end
Expand All @@ -506,30 +535,34 @@ end

Wendland C6 kernel, a piecewise polynomial function designed to have compact support and to be six times continuously differentiable everywhere. Given by:

\[ W(r, h) = \frac{1}{h^d} w(r/h) \]
`` W(r, h) = \frac{1}{h^d} w(r/h) ``

with:

\[
```math
w(q) = \sigma \begin{cases}
(1 - q)^8 (32q^3 + 25q^2 + 8q + 1) & \text{if } 0 \leq q < 1, \\
0 & \text{if } q \geq 1,
\end{cases}
\]
```

where `` d `` is the number of dimensions and `` \sigma `` is a normalization factor dependent
on the dimension. The exact value of `` \sigma `` needs to be determined based on the dimension to ensure proper normalization.
on the dimension. The normalization factor `` \sigma `` is `` 78 / 7 \pi`` in two dimensions or `` 1365 / 64\pi`` in three dimensions.

This kernel function has a compact support of `` [0, h] ``.

For a detailed discussion on Wendland functions and their applications in SPH, see (Dehnen & Aly, 2012).
The smoothness of these functions is also the largest disadvantage as they loose details at sharp corners.

The smoothing length is typically in range `[3.5 : 5.0] * particle_spacing`.

For general information and usage see [`SmoothingKernel`](@ref).

## References:
- Walter Dehnen & Hassan Aly.
"Improving convergence in smoothed particle hydrodynamics simulations without pairing instability".
In: Monthly Notices of the Royal Astronomical Society 425.2 (2012), pages 1068-1082.
[doi: 10.1111/j.1365-2966.2012.21439.x](https://doi.org/10.1111/j.1365-2966.2012.21439.x)
"Improving convergence in smoothed particle hydrodynamics simulations without pairing instability".
In: Monthly Notices of the Royal Astronomical Society 425.2 (2012), pages 1068-1082.
[doi: 10.1111/j.1365-2966.2012.21439.x](https://doi.org/10.1111/j.1365-2966.2012.21439.x)

- Holger Wendland.
"Piecewise polynomial, positive definite and compactly supported radial functions of minimal degree."
Expand All @@ -541,10 +574,10 @@ struct WendlandC6Kernel{NDIMS} <: WendlandKernel{NDIMS} end
@fastpow @inline function kernel(kernel::WendlandC6Kernel, r::Real, h)
q = r / h

result = (1 - q)^8 * (32q^3 + 25q^2 + 8q + 1)

# Zero out result if q >= 1
result = ifelse(q < 1,
normalization_factor(kernel, h) * (1 - q)^8 * (32q^3 + 25q^2 + 8q + 1),
zero(q))
result = ifelse(q < 1, normalization_factor(kernel, h) * result, zero(q))

return result
end
Expand All @@ -570,19 +603,19 @@ end

Poly6 kernel, a commonly used kernel in SPH literature, especially in computer graphics contexts. It is defined as

\[ W(r, h) = \frac{1}{h^d} w(r/h) \]
`` W(r, h) = \frac{1}{h^d} w(r/h) ``

with

\[
```math
w(q) = \sigma \begin{cases}
(1 - q^2)^3 & \text{if } 0 \leq q < 1, \\
0 & \text{if } q \geq 1,
\end{cases}
\]
```

where `` d `` is the number of dimensions and `` \sigma `` is a normalization factor that depends
on the dimension. The exact value of `` \sigma `` needs to be determined based on the dimension to ensure proper normalization.
on the dimension. The normalization factor `` \sigma `` is `` 4 / \pi`` in two dimensions or `` 315 / 64\pi`` in three dimensions.

This kernel function has a compact support of `` [0, h] ``.

Expand All @@ -591,6 +624,10 @@ other kernels that might offer better accuracy for hydrodynamic simulations. Fur
its derivatives are not that smooth, which can lead to stability problems.
It is also susceptible to clumping.

The smoothing length is typically in range `[1.5 : 2.5] * particle_spacing`.

For general information and usage see [`SmoothingKernel`](@ref).

## References:
- Matthias Müller, David Charypar, and Markus Gross. "Particle-based fluid simulation for interactive applications".
In: Proceedings of the 2003 ACM SIGGRAPH/Eurographics symposium on Computer animation. Eurographics Association. 2003, pages 154-159.
Expand All @@ -601,8 +638,10 @@ struct Poly6Kernel{NDIMS} <: SmoothingKernel{NDIMS} end
@inline function kernel(kernel::Poly6Kernel, r::Real, h)
q = r / h

result = (1 - q^2)^3

# Zero out result if q >= 1
result = ifelse(q < 1, normalization_factor(kernel, h) * (1 - q^2)^3, zero(q))
result = ifelse(q < 1, normalization_factor(kernel, h) * result, zero(q))

return result
end
Expand All @@ -611,10 +650,11 @@ end
inner_deriv = 1 / h
q = r * inner_deriv

result = -6 * q * (1 - q^2)^2

# Zero out result if q >= 1
result = ifelse(q < 1,
-6 * q * (1 - q^2)^2 * normalization_factor(kernel, h) * inner_deriv,
zero(q))
result * normalization_factor(kernel, h) * inner_deriv, zero(q))
return result
end

Expand All @@ -629,26 +669,30 @@ end
The Spiky kernel is another frequently used kernel in SPH, especially due to its desirable
properties in preserving features near boundaries in fluid simulations. It is defined as:

\[ W(r, h) = \frac{1}{h^d} w(r/h) \]
`` W(r, h) = \frac{1}{h^d} w(r/h) ``

with:

\[
```math
w(q) = \sigma \begin{cases}
(1 - q)^3 & \text{if } 0 \leq q < 1, \\
0 & \text{if } q \geq 1,
\end{cases}
\]
```

where `` d `` is the number of dimensions and `` \sigma `` is a normalization factor, which
depends on the dimension and ensures the kernel integrates to 1 over its support.
where `` d `` is the number of dimensions and the normalization factor `` \sigma `` is `` 10 / \pi``
in two dimensions or `` 15 / \pi`` in three dimensions.

This kernel function has a compact support of `` [0, h] ``.

The Spiky kernel is particularly known for its sharp gradients, which can help to preserve
sharp features in fluid simulations, especially near solid boundaries.
These sharp gradients at the boundary are also the largest disadvantage as they can lead to instability.

The smoothing length is typically in range `[1.5 : 3.0] * particle_spacing`.

For general information and usage see [`SmoothingKernel`](@ref).

## References:
- Matthias Müller, David Charypar, and Markus Gross. "Particle-based fluid simulation for interactive applications".
In: Proceedings of the 2003 ACM SIGGRAPH/Eurographics symposium on Computer animation. Eurographics Association. 2003, pages 154-159.
Expand All @@ -659,8 +703,10 @@ struct SpikyKernel{NDIMS} <: SmoothingKernel{NDIMS} end
@inline function kernel(kernel::SpikyKernel, r::Real, h)
q = r / h

result = (1 - q)^3

# Zero out result if q >= 1
result = ifelse(q < 1, normalization_factor(kernel, h) * (1 - q)^3, zero(q))
result = ifelse(q < 1, normalization_factor(kernel, h) * result, zero(q))

return result
end
Expand All @@ -669,9 +715,10 @@ end
inner_deriv = 1 / h
q = r * inner_deriv

result = -3 * (1 - q)^2

# Zero out result if q >= 1
result = ifelse(q < 1, -3 * (1 - q)^2 * normalization_factor(kernel, h) * inner_deriv,
zero(q))
result = ifelse(q < 1, result * normalization_factor(kernel, h) * inner_deriv, zero(q))

return result
end
Expand Down
4 changes: 2 additions & 2 deletions test/general/smoothing_kernels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ using QuadGK
return 4 * π * integral_3d_radial
end

# All smoothing kernels should integrate to something close to 1
# Don't show all kernel tests in the final overview
# All smoothing kernels should integrate to something close to 1.
# Don't show all kernel tests in the final overview.
@testset verbose=false "Integral" begin
# Treat the truncated Gaussian kernel separately
@testset "GaussianKernel" begin
Expand Down