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

Mixed kernel gradient #261

Merged
merged 106 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
106 commits
Select commit Hold shift + click to select a range
57382e2
add gradient correction stuff
svchb Nov 1, 2023
81449fb
impl
svchb Nov 1, 2023
63f5ac9
up
svchb Nov 2, 2023
cfaf96c
finish impl
svchb Nov 2, 2023
4bf647e
update
svchb Nov 2, 2023
e937e15
fix testcases
svchb Nov 3, 2023
7c131db
fix
svchb Nov 3, 2023
1f53431
fix tests
svchb Nov 3, 2023
af8af78
format
svchb Nov 3, 2023
901d189
Merge branch 'main' into mixed-kernel-gradient
svchb Nov 3, 2023
182a872
working
svchb Nov 22, 2023
3cf34f4
format
svchb Nov 22, 2023
2e67efe
Merge branch 'main'
svchb Nov 22, 2023
027b1a4
format
svchb Nov 22, 2023
5f4a925
fix
svchb Nov 22, 2023
ad42394
working trixi_include
svchb Nov 24, 2023
8612c82
Merge branch 'main'
svchb Nov 24, 2023
09a1ea4
fix
svchb Nov 24, 2023
cadbbba
cleanup
svchb Nov 28, 2023
01a4173
format
svchb Nov 28, 2023
b506359
fix
svchb Nov 28, 2023
89cb149
fix tests
svchb Nov 28, 2023
f1537c4
Merge branch 'main'
svchb Nov 28, 2023
2cae13f
fix
svchb Nov 28, 2023
36ee614
Merge branch 'main' into mixed-kernel-gradient
svchb Nov 28, 2023
1cffc70
ups
svchb Nov 28, 2023
5d57d97
Merge branch 'mixed-kernel-gradient' of github.com:svchb/TrixiParticl…
svchb Nov 28, 2023
0e75ef2
typo
svchb Nov 28, 2023
2b03fe9
up
svchb Nov 29, 2023
1548433
fix
svchb Nov 30, 2023
ae78667
format
svchb Nov 30, 2023
54e7c16
update fix errors
svchb Dec 4, 2023
47ed721
fix
svchb Dec 5, 2023
57c55f1
update
svchb Dec 6, 2023
76aefea
up
svchb Dec 6, 2023
5c4d20d
fix tests
svchb Dec 6, 2023
c7976f9
fix tests
svchb Dec 6, 2023
aa66268
np
svchb Dec 6, 2023
369cbc9
remove comment
svchb Dec 6, 2023
7799f62
revert some changes
svchb Dec 6, 2023
d7932ca
up
svchb Dec 6, 2023
cc075f5
Merge branch 'main'
svchb Dec 6, 2023
e5e1ed9
up
svchb Dec 6, 2023
001668a
Merge branch 'main' into mixed-kernel-gradient
svchb Dec 6, 2023
40c567f
update
svchb Dec 7, 2023
95dcc62
format
svchb Dec 7, 2023
c8a67e7
Merge branch 'mixed-kernel-gradient' of github.com:svchb/TrixiParticl…
svchb Dec 7, 2023
30f7c32
cleanup
svchb Dec 7, 2023
ac76b86
revert
svchb Dec 7, 2023
67ea62d
revert
svchb Dec 7, 2023
ae6b3eb
implement correction on wall
svchb Dec 11, 2023
057adbe
fix tests
svchb Dec 11, 2023
bc9b87e
fix pressure_acceleration for summation
svchb Dec 12, 2023
21b83d5
format
svchb Dec 12, 2023
96b18c9
revert
svchb Dec 13, 2023
10d1a47
fix
svchb Dec 13, 2023
ca7f106
Merge branch 'main'
svchb Dec 13, 2023
7126cce
fixes
svchb Dec 13, 2023
f5469da
fix
svchb Dec 13, 2023
47f824d
fix pressure_acceleration dispatches
svchb Dec 14, 2023
3af1309
typo
svchb Dec 14, 2023
1b5d137
fix
svchb Dec 14, 2023
acafbe1
rename
svchb Dec 14, 2023
6ae8528
Merge branch 'main' into mixed-kernel-gradient
svchb Dec 22, 2023
1e47bc9
Merge branch 'main' into mixed-kernel-gradient
svchb Jan 2, 2024
d9d9562
some review fixes
svchb Jan 3, 2024
be802d4
review
svchb Jan 3, 2024
9278edb
more fixes
svchb Jan 3, 2024
f54c891
add solid system type
svchb Jan 3, 2024
3ed56be
review
svchb Jan 3, 2024
72d293f
fix tlsph call
svchb Jan 3, 2024
7b0c438
fix comment
svchb Jan 3, 2024
1a4803c
review
svchb Jan 3, 2024
3aea7ae
review
svchb Jan 3, 2024
2316101
format
svchb Jan 3, 2024
b4c3ad6
review
svchb Jan 3, 2024
2ab5853
move
svchb Jan 3, 2024
49b72f3
fix doc
svchb Jan 3, 2024
e2b9574
review fix
svchb Jan 3, 2024
148c0e9
more review
svchb Jan 3, 2024
2c86a03
review fix
svchb Jan 4, 2024
54b3fa0
format
svchb Jan 4, 2024
26e90b1
change parameter list
svchb Jan 4, 2024
824a054
reorder params
svchb Jan 4, 2024
76a5673
Merge branch 'main' into mixed-kernel-gradient
svchb Jan 4, 2024
390e0ed
format
svchb Jan 4, 2024
b3d3924
fix
svchb Jan 4, 2024
33ce59a
fix
svchb Jan 4, 2024
3346c63
Merge branch 'mixed-kernel-gradient' of github.com:svchb/TrixiParticl…
svchb Jan 4, 2024
22a6ff5
fix
svchb Jan 4, 2024
40805b5
remove dispatched functions
svchb Jan 4, 2024
ea17c79
format
svchb Jan 4, 2024
9e162ca
fix comments
svchb Jan 4, 2024
ae069d2
fix
svchb Jan 4, 2024
8fb3323
remove system
svchb Jan 4, 2024
c5803fd
format
svchb Jan 4, 2024
4f5223f
change parameter list
svchb Jan 4, 2024
111c53c
review
svchb Jan 4, 2024
be15f26
unused variable
svchb Jan 4, 2024
7886cf9
break
svchb Jan 4, 2024
2a332b9
edit todo
svchb Jan 4, 2024
240f1c8
remove breaks
svchb Jan 4, 2024
0b98a52
format
svchb Jan 4, 2024
2e6fcd7
Merge branch 'main' into mixed-kernel-gradient
svchb Jan 4, 2024
451f3d4
rename grad_kernel to W_a
svchb Jan 5, 2024
1c152c9
Merge branch 'mixed-kernel-gradient' of github.com:svchb/TrixiParticl…
svchb Jan 5, 2024
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
2 changes: 1 addition & 1 deletion examples/fluid/dam_break_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fluid_density = 1000.0
# Simulation settings
fluid_particle_spacing = 0.02
smoothing_length = 1.2 * fluid_particle_spacing
boundary_layers = 3
boundary_layers = 4
# Change spacing ratio to 3 and boundary layers to 1 when using Monaghan-Kajtar boundary model
spacing_ratio = 1
boundary_particle_spacing = fluid_particle_spacing / spacing_ratio
Expand Down
26 changes: 22 additions & 4 deletions examples/fluid/dam_break_2d_corrections.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
using TrixiParticles

fluid_density = 1000.0
atmospheric_pressure = 100000.0
gravity = 9.81

initial_fluid_size = (2.0, 1.0)
sound_speed = 20 * sqrt(gravity * initial_fluid_size[2])

particle_spacing = 0.05
smoothing_length = 1.15 * particle_spacing
tspan = (0.0, 5.7 / sqrt(gravity))

boundary_density_calculator = SummationDensity()

gravity = 9.81
tspan = (0.0, 5.7 / sqrt(gravity))

correction_dict = Dict(
"no_correction" => Nothing(),
"shepard_kernel_correction" => ShepardKernelCorrection(),
"akinci_free_surf_correction" => AkinciFreeSurfaceCorrection(fluid_density),
"kernel_gradient_summation_correction" => KernelGradientCorrection(),
"kernel_gradient_continuity_correction" => KernelGradientCorrection(),
"blended_gradient_summation_correction" => BlendedGradientCorrection(0.5),
"blended_gradient_continuity_correction" => BlendedGradientCorrection(0.5),
)

density_calculator_dict = Dict(
Expand All @@ -24,6 +29,18 @@ density_calculator_dict = Dict(
"akinci_free_surf_correction" => SummationDensity(),
"kernel_gradient_summation_correction" => SummationDensity(),
"kernel_gradient_continuity_correction" => ContinuityDensity(),
"blended_gradient_summation_correction" => SummationDensity(),
"blended_gradient_continuity_correction" => ContinuityDensity(),
)

smoothing_kernel_dict = Dict(
"no_correction" => SchoenbergCubicSplineKernel{2}(),
"shepard_kernel_correction" => SchoenbergCubicSplineKernel{2}(),
"akinci_free_surf_correction" => SchoenbergCubicSplineKernel{2}(),
"kernel_gradient_summation_correction" => SchoenbergCubicSplineKernel{2}(),
"kernel_gradient_continuity_correction" => SchoenbergCubicSplineKernel{2}(),
"blended_gradient_summation_correction" => SchoenbergQuinticSplineKernel{2}(),
"blended_gradient_continuity_correction" => SchoenbergQuinticSplineKernel{2}(),
)

trixi_include(@__MODULE__, joinpath(examples_dir(), "fluid", "dam_break_2d.jl"),
Expand All @@ -41,6 +58,7 @@ state_equation = StateEquationCole(sound_speed, 7, fluid_density, atmospheric_pr
for correction_name in keys(correction_dict)
local fluid_density_calculator = density_calculator_dict[correction_name]
local correction = correction_dict[correction_name]
local smoothing_kernel = smoothing_kernel_dict[correction_name]

println("="^100)
println("fluid/dam_break_2d.jl with ", correction_name)
Expand All @@ -51,6 +69,6 @@ for correction_name in keys(correction_dict)
boundary_density_calculator=boundary_density_calculator,
fluid_density_calculator=fluid_density_calculator,
correction=correction, use_reinit=false,
state_equation=state_equation,
state_equation=state_equation, smoothing_kernel=smoothing_kernel,
file_prefix="$(correction_name)", tspan=tspan)
end
4 changes: 2 additions & 2 deletions examples/fluid/rectangular_tank_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ callbacks = CallbackSet(info_callback, saving_callback)
# become extremely large when fluid particles are very close to boundary particles,
# and the time integration method interprets this as an instability.
sol = solve(ode, RDPK3SpFSAL49(),
abstol=1e-5, # Default abstol is 1e-6 (may need to be tuned to prevent boundary penetration)
reltol=1e-3, # Default reltol is 1e-3 (may need to be tuned to prevent boundary penetration)
abstol=1e-6, # Default abstol is 1e-6 (may need to be tuned to prevent boundary penetration)
reltol=1e-5, # Default reltol is 1e-3 (may need to be tuned to prevent boundary penetration)
dtmax=1e-2, # Limit stepsize to prevent crashing
save_everystep=false, callback=callbacks);
6 changes: 3 additions & 3 deletions examples/fsi/dam_break_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ beta = 1
boundary_layers = 3

water_width = 0.146
water_height = 2water_width
water_height = 2 * water_width
water_density = 1000.0

tank_width = 4water_width
tank_height = 4water_width
tank_width = 4 * water_width
tank_height = 4 * water_width

sound_speed = 20 * sqrt(9.81 * water_height)

Expand Down
3 changes: 2 additions & 1 deletion src/TrixiParticles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export examples_dir, trixi_include
export trixi2vtk
export RectangularTank, RectangularShape, SphereShape
export VoxelSphere, RoundSphere, reset_wall!
export ShepardKernelCorrection, KernelGradientCorrection, AkinciFreeSurfaceCorrection
export ShepardKernelCorrection, KernelGradientCorrection, AkinciFreeSurfaceCorrection,
GradientCorrection, BlendedGradientCorrection, MixedKernelGradientCorrection
export nparticles

end # module
118 changes: 102 additions & 16 deletions src/general/corrections.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Sorted in order of computational cost
using LinearAlgebra
svchb marked this conversation as resolved.
Show resolved Hide resolved

# Sorted in order of computational cost
@doc raw"""
AkinciFreeSurfaceCorrection(rho0)

Expand All @@ -11,6 +12,16 @@ near free surfaces to counter this effect.
It's important to note that this correlation is unphysical and serves as an approximation.
The computation time added by this method is about 2-3%.

Mathematically the idea is quite simple if we have a SPH particle in the middle of a volume
at rest its density will be identical to the rest density `rho0`. If we now consider a SPH
particle at a free surface at rest it will have neighbor missing in the direction normal to
the surface, which will result in a lower density, if we calculate the correction factor `k`
```math
k = rho_0/rho_{mean}
```
this value will be about ~1.5 for particles at the free surface and can than be used to increase
the pressure and viscosity accordingly.
svchb marked this conversation as resolved.
Show resolved Hide resolved

# Arguments
- `rho0`: Reference density.

Expand Down Expand Up @@ -119,6 +130,33 @@ especially for free surfaces.
"""
struct KernelGradientCorrection end

@doc raw"""
MixedKernelGradientCorrection()

Combines 'GradientCorrection' and 'KernelGradientCorrection' which results in a 1st order accurate SPH method.
svchb marked this conversation as resolved.
Show resolved Hide resolved

# Notes:
- Stability issues as especially when particles separate into small clusters.
svchb marked this conversation as resolved.
Show resolved Hide resolved
- Doubles the computational effort.

## References:
- J. Bonet, T.-S.L. Lok.
"Variational and momentum preservation aspects of Smooth Particle Hydrodynamic formulations".
In: Computer Methods in Applied Mechanics and Engineering 180 (1999), pages 97-115.
svchb marked this conversation as resolved.
Show resolved Hide resolved
[doi: 10.1016/S0045-7825(99)00051-1](https://doi.org/10.1016/S0045-7825(99)00051-1)
- Mihai Basa, Nathan Quinlan, Martin Lastiwka.
"Robustness and accuracy of SPH formulations for viscous flow".
In: International Journal for Numerical Methods in Fluids 60 (2009), pages 1127-1148.
svchb marked this conversation as resolved.
Show resolved Hide resolved
[doi: 10.1002/fld.1927](https://doi.org/10.1002/fld.1927)
"""
struct MixedKernelGradientCorrection
use_factorization::Bool
svchb marked this conversation as resolved.
Show resolved Hide resolved

function MixedKernelGradientCorrection(; use_factorization=false)
return new{}(use_factorization)
end
end

function kernel_correction_coefficient(system, particle)
return system.cache.kernel_correction_coefficient[particle]
end
Expand Down Expand Up @@ -175,7 +213,8 @@ end

function compute_correction_values!(system, system_index, v, u, v_ode, u_ode, semi,
::Union{SummationDensity, ContinuityDensity},
::KernelGradientCorrection)
::Union{KernelGradientCorrection,
MixedKernelGradientCorrection})
(; systems, neighborhood_searches) = semi
(; cache) = system
(; kernel_correction_coefficient, dw_gamma) = cache
Expand Down Expand Up @@ -249,6 +288,8 @@ aiming to make the corrected gradient more accurate, especially near domain boun
# Notes:
- Stability issues as especially when particles separate into small clusters.
- Doubles the computational effort.
- Better stability with smoothing Kernels with larger support and Continuity e.g. 'SchoenbergQuinticSplineKernel' or 'WendlandC6Kernel'.
svchb marked this conversation as resolved.
Show resolved Hide resolved
- Set dt_max =< 1e-3
svchb marked this conversation as resolved.
Show resolved Hide resolved

## References:
- J. Bonet, T.-S.L. Lok.
Expand All @@ -260,11 +301,41 @@ aiming to make the corrected gradient more accurate, especially near domain boun
In: International Journal for Numerical Methods in Fluids 60 (2009), pages 1127-1148.
[doi: 10.1002/fld.1927](https://doi.org/10.1002/fld.1927)
"""
struct GradientCorrection end
struct GradientCorrection
use_factorization::Bool

function GradientCorrection(; use_factorization=false)
return new{}(use_factorization)
end
end

@doc raw"""
BlendedGradientCorrection()
svchb marked this conversation as resolved.
Show resolved Hide resolved

To increase stability this calculates a blended gradient, which reduces the stability issues of the 'GradientCorrection' method.
svchb marked this conversation as resolved.
Show resolved Hide resolved

This calculates the following
svchb marked this conversation as resolved.
Show resolved Hide resolved
```math
\nabla^\tilde A_i = (1-\lambda) \nabla A_i + \lambda L_i \nabla A_i
svchb marked this conversation as resolved.
Show resolved Hide resolved

function compute_gradient_correction_matrix!(corr_matrix, neighborhood_search, system,
coordinates)
(; mass, material_density) = system
```
with $\lambda$ being the blending factor.
svchb marked this conversation as resolved.
Show resolved Hide resolved

"""
struct BlendedGradientCorrection{ELTYPE <: Real}
blending_factor :: ELTYPE
use_factorization :: Bool
svchb marked this conversation as resolved.
Show resolved Hide resolved

function BlendedGradientCorrection(blending_factor; use_factorization=false)
return new{eltype(blending_factor)}(blending_factor, use_factorization)
end
end

function compute_gradient_correction_matrix!(corr_matrix::AbstractArray,
neighborhood_search, system,
coordinates, density_fun;
use_factorization=false)
(; mass, smoothing_length, smoothing_kernel, correction) = system

set_zero!(corr_matrix)

Expand All @@ -278,22 +349,37 @@ function compute_gradient_correction_matrix!(corr_matrix, neighborhood_search, s
# Only consider particles with a distance > 0.
distance < sqrt(eps()) && return

volume = mass[neighbor] / material_density[neighbor]
volume = mass[neighbor] / density_fun(neighbor)

grad_kernel = smoothing_kernel_grad(system, pos_diff, distance)
result = volume * grad_kernel * pos_diff'
grad_kernel = nothing
if correction isa MixedKernelGradientCorrection
grad_kernel = corrected_kernel_grad(smoothing_kernel, pos_diff, distance,
smoothing_length,
KernelGradientCorrection(), system,
particle)
else
grad_kernel = smoothing_kernel_grad(system, pos_diff, distance)
end

L = volume * grad_kernel * pos_diff'

@inbounds for j in 1:ndims(system), i in 1:ndims(system)
corr_matrix[i, j, particle] -= result[i, j]
corr_matrix[i, j, particle] -= L[i, j]
end
end

@threaded for particle in eachparticle(system)
L = correction_matrix(system, particle)
result = inv(L)

@inbounds for j in 1:ndims(system), i in 1:ndims(system)
corr_matrix[i, j, particle] = result[i, j]
if use_factorization
@threaded for particle in eachparticle(system)
L = correction_matrix(system, particle)
invert_factorization(corr_matrix, L, particle, system)
end
else
@threaded for particle in eachparticle(system)
L = correction_matrix(system, particle)
if cond(L) > 1e10
throw(ModelError("Correction Matrix 'L' is not well conditioned."))
end
invert(corr_matrix, L, particle, system)
svchb marked this conversation as resolved.
Show resolved Hide resolved
end
end

Expand Down
15 changes: 0 additions & 15 deletions src/general/density_calculators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,6 @@ end
return v[end, particle]
end

# *Note* that these functions are intended to internally set the density for buffer particles
# and density correction. It cannot be used to set up an initial condition,
# as the particle density depends on the particle positions.
@inline function set_particle_density(particle, v, system, density)
set_particle_density(particle, v, system.density_calculator, system, density)
end

@inline function set_particle_density(particle, v, ::SummationDensity, system, density)
system.cache.density[particle] = density
end

@inline function set_particle_density(particle, v, ::ContinuityDensity, system, density)
v[end, particle] = density
end

function summation_density!(system, system_index, semi, u, u_ode, density;
particles=each_moving_particle(system))
(; systems, neighborhood_searches) = semi
Expand Down
34 changes: 34 additions & 0 deletions src/general/general.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,40 @@
return du
end

@inline function invert(inverse, A, particle, system)
A_inv = inv(A)
@inbounds for j in 1:ndims(system), i in 1:ndims(system)
inverse[i, j, particle] = A_inv[i, j]
end
end

@inline function invert_factorization(inverse, A, particle, system)
A_factored = lu(A)
A_inv = similar(A)

n = ndims(system)
y = similar(A_inv[:, 1])

# Inverting by solving linear system for each column of the identity
@inbounds for j in 1:n
e_j = zeros(eltype(A), n)
e_j[j] = 1.0
y .= A_factored.L \ e_j
A_inv[:, j] .= A_factored.U \ y
end
@inbounds for j in 1:n, i in 1:n
inverse[i, j, particle] = A_inv[i, j]
end
end

struct SimulationDiverged <: Exception
msg::AbstractString
end

struct ModelError <: Exception
msg::AbstractString
end

# Note that `semidiscretization.jl` depends on the system types and has to be
# included later.
# `density_calculators.jl` needs to be included before `corrections.jl`.
Expand Down
20 changes: 20 additions & 0 deletions src/general/smoothing_kernels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ end
kernel_correction_coefficient(system, particle)
end

@inline function corrected_kernel_grad(kernel, pos_diff, distance, h,
corr::BlendedGradientCorrection, system, particle)
grad = kernel_grad(kernel, pos_diff, distance, h)
factor = corr.blending_factor
svchb marked this conversation as resolved.
Show resolved Hide resolved
return (1 - factor) * grad + factor * correction_matrix(system, particle) * grad
end

@inline function corrected_kernel_grad(kernel, pos_diff, distance, h,
::GradientCorrection, system, particle)
grad = kernel_grad(kernel, pos_diff, distance, h)
return correction_matrix(system, particle) * grad
end

@inline function corrected_kernel_grad(kernel, pos_diff, distance, h,
::MixedKernelGradientCorrection, system, particle)
grad = corrected_kernel_grad(kernel, pos_diff, distance, h, KernelGradientCorrection(),
system, particle)
return correction_matrix(system, particle) * grad
end

@doc raw"""
SchoenbergCubicSplineKernel{NDIMS}()

Expand Down
Loading