diff --git a/docs/src/systems/weakly_compressible_sph.md b/docs/src/systems/weakly_compressible_sph.md index 9b1b47082b..7dcb261222 100644 --- a/docs/src/systems/weakly_compressible_sph.md +++ b/docs/src/systems/weakly_compressible_sph.md @@ -170,6 +170,16 @@ The constants are fixed to ``R = 0.2`` and ``n = 4``. The ``\delta``-SPH method (WCSPH with density diffusion) together with this formulation of PST is commonly referred to as ``\delta^+``-SPH. +With `integrate_shifting_velocity=false`, the particle positions are shifted in a callback +after each time step, but the particle properties are kept by default. +With `correct_properties=true`, the velocity is corrected with a first-order Taylor expansion +```math +\bm{v}_a' = \bm{v}_a + \sum_b \frac{m_b}{\rho_b} + \left( \delta \bm{r}_a \cdot \nabla_a W_{ab} \right) \left( \bm{v}_b - \bm{v}_a \right). +``` +Walls without a viscosity in their boundary model (free-slip) are not included in this sum. +Density and pressure are currently not corrected. + To apply particle shifting, use the keyword argument `shifting_technique` in the constructor of a system that supports it. diff --git a/src/schemes/boundary/wall_boundary/system.jl b/src/schemes/boundary/wall_boundary/system.jl index 3a83fc1958..e95ccca53b 100644 --- a/src/schemes/boundary/wall_boundary/system.jl +++ b/src/schemes/boundary/wall_boundary/system.jl @@ -162,6 +162,29 @@ end return extract_svector(system.boundary_model.cache.wall_velocity, system, particle) end +# Neighbor velocity in the velocity gradient of the property correction after shifting +# (see `correct_properties!`) +@propagate_inbounds function correction_velocity(v_neighbor, + neighbor_system::WallBoundarySystem, + neighbor, v_a) + return correction_velocity(v_neighbor, neighbor_system, + neighbor_system.boundary_model.viscosity, neighbor, v_a) +end + +# Free-slip wall: Return `v_a`, so that the wall does not contribute to the gradient. +# The tangential velocity of the fluid does not change towards a free-slip wall, but the +# wall particles carry the wall velocity (zero for resting walls). Using this velocity +# would produce a nonphysical gradient that slows down particles shifted towards the wall. +@inline function correction_velocity(v_neighbor, neighbor_system, ::Nothing, neighbor, v_a) + return v_a +end + +# No-slip wall: The velocity gradient towards the wall is physical +@propagate_inbounds function correction_velocity(v_neighbor, neighbor_system, viscosity, + neighbor, v_a) + return current_velocity(v_neighbor, neighbor_system, neighbor) +end + @inline function current_density(v, system::WallBoundarySystem) return current_density(v, system.boundary_model, system) end diff --git a/src/schemes/fluid/shifting_techniques.jl b/src/schemes/fluid/shifting_techniques.jl index 6130137bff..0a699f6bd1 100644 --- a/src/schemes/fluid/shifting_techniques.jl +++ b/src/schemes/fluid/shifting_techniques.jl @@ -16,13 +16,16 @@ particle_shifting_from_callback!(u_ode, shifting, system, v_ode, semi, integrato create_cache_shifting(initial_condition, ::Nothing) = (;) -function create_cache_shifting(initial_condition, ::AbstractShiftingTechnique) +function create_cache_shifting(initial_condition, shifting::AbstractShiftingTechnique) delta_v = zeros(eltype(initial_condition), ndims(initial_condition), nparticles(initial_condition)) - return (; delta_v) + return (; delta_v, create_cache_property_correction(initial_condition, shifting)...) end +# No correction of the particle properties by default +create_cache_property_correction(initial_condition, shifting) = (;) + # `δv` is the correction to the particle velocity due to the shifting. # Particles are advected with the velocity `v + δv`. @propagate_inbounds function delta_v(system, particle) @@ -64,7 +67,8 @@ end modify_continuity_equation=true, second_continuity_equation_term=ContinuityEquationTermSun2019(), momentum_equation_term=MomentumEquationTermSun2019(), - v_max_factor=1, sound_speed_factor=0) + v_max_factor=1, sound_speed_factor=0, + correct_properties=false) Particle Shifting Technique by [Sun et al. (2017)](@cite Sun2017) and [Sun et al. (2019)](@cite Sun2019). @@ -133,6 +137,14 @@ We provide the following convenience constructors for common variants of the met `sound_speed_factor * c`, where `c` is the speed of sound. Only one of `v_max_factor` and `sound_speed_factor` can be non-zero. +- `correct_properties`: If `true`, the particle properties are corrected with a + first-order Taylor expansion to account for the change of + the particle positions when the shifting is applied. + Currently, only the velocity is corrected. + Density and pressure are not corrected. + This requires `integrate_shifting_velocity=false`. + With `integrate_shifting_velocity=true`, use + `momentum_equation_term` instead. !!! warning The Particle Shifting Technique needs to be disabled close to the free surface @@ -143,6 +155,7 @@ struct ParticleShiftingTechnique{integrate_shifting_velocity, update_everystage, modify_continuity_equation, compute_v_max, + correct_properties, ELTYPE, S, M} <: AbstractShiftingTechnique v_factor :: ELTYPE second_continuity_equation_term :: S @@ -153,7 +166,8 @@ struct ParticleShiftingTechnique{integrate_shifting_velocity, modify_continuity_equation=true, second_continuity_equation_term=ContinuityEquationTermSun2019(), momentum_equation_term=MomentumEquationTermSun2019(), - v_max_factor=1, sound_speed_factor=0) + v_max_factor=1, sound_speed_factor=0, + correct_properties=false) if !integrate_shifting_velocity && update_everystage throw(ArgumentError("ParticleShiftingTechnique: " * "integrate_shifting_velocity=false requires " * @@ -178,6 +192,12 @@ struct ParticleShiftingTechnique{integrate_shifting_velocity, "integrate_shifting_velocity=true")) end + if integrate_shifting_velocity && correct_properties + throw(ArgumentError("ParticleShiftingTechnique: " * + "correct_properties=true requires " * + "integrate_shifting_velocity=false")) + end + if v_max_factor > 0 && sound_speed_factor > 0 throw(ArgumentError("ParticleShiftingTechnique: " * "Only one of v_max_factor and sound_speed_factor " * @@ -196,7 +216,8 @@ struct ParticleShiftingTechnique{integrate_shifting_velocity, new{integrate_shifting_velocity, update_everystage, modify_continuity_equation, - compute_v_max, typeof(v_factor), + compute_v_max, + correct_properties, typeof(v_factor), typeof(second_continuity_equation_term), typeof(momentum_equation_term)}(v_factor, second_continuity_equation_term, @@ -218,10 +239,11 @@ ParticleShiftingTechnique(integrate_shifting_velocity=false, modify_continuity_equation=false, second_continuity_equation_term=nothing, momentum_equation_term=nothing, - v_max_factor=1, sound_speed_factor=0) + v_max_factor=1, sound_speed_factor=0, + correct_properties=false) # output -ParticleShiftingTechnique{false, false, false, true, Int64, Nothing, Nothing}(1, nothing, nothing) +ParticleShiftingTechnique{false, false, false, true, false, Int64, Nothing, Nothing}(1, nothing, nothing) ``` See [ParticleShiftingTechnique](@ref ParticleShiftingTechnique) for all available options. @@ -234,7 +256,7 @@ See [ParticleShiftingTechnique](@ref ParticleShiftingTechnique) for all availabl shifting_technique = ParticleShiftingTechniqueSun2017() # output -ParticleShiftingTechnique{false, false, false, true, Int64, Nothing, Nothing}(1, nothing, nothing) +ParticleShiftingTechnique{false, false, false, true, false, Int64, Nothing, Nothing}(1, nothing, nothing) ``` !!! warning @@ -249,6 +271,7 @@ function ParticleShiftingTechniqueSun2017(; kwargs...) second_continuity_equation_term=nothing, momentum_equation_term=nothing, v_max_factor=1, sound_speed_factor=0, + correct_properties=false, kwargs...) end @@ -264,10 +287,11 @@ ParticleShiftingTechnique(integrate_shifting_velocity=true, modify_continuity_equation=true, second_continuity_equation_term=ContinuityEquationTermSun2019(), momentum_equation_term=MomentumEquationTermSun2019(), - v_max_factor=0, sound_speed_factor=0.1f0) + v_max_factor=0, sound_speed_factor=0.1f0, + correct_properties=false) # output -ParticleShiftingTechnique{true, true, true, false, Float32, ContinuityEquationTermSun2019, MomentumEquationTermSun2019}(0.1f0, ContinuityEquationTermSun2019(), MomentumEquationTermSun2019()) +ParticleShiftingTechnique{true, true, true, false, false, Float32, ContinuityEquationTermSun2019, MomentumEquationTermSun2019}(0.1f0, ContinuityEquationTermSun2019(), MomentumEquationTermSun2019()) ``` See [ParticleShiftingTechnique](@ref ParticleShiftingTechnique) for all available options. @@ -287,7 +311,7 @@ See [ParticleShiftingTechnique](@ref ParticleShiftingTechnique) for all availabl shifting_technique = ConsistentShiftingSun2019() # output -ParticleShiftingTechnique{true, true, true, false, Float32, ContinuityEquationTermSun2019, MomentumEquationTermSun2019}(0.1f0, ContinuityEquationTermSun2019(), MomentumEquationTermSun2019()) +ParticleShiftingTechnique{true, true, true, false, false, Float32, ContinuityEquationTermSun2019, MomentumEquationTermSun2019}(0.1f0, ContinuityEquationTermSun2019(), MomentumEquationTermSun2019()) ``` !!! warning @@ -302,6 +326,7 @@ function ConsistentShiftingSun2019(; kwargs...) second_continuity_equation_term=ContinuityEquationTermSun2019(), momentum_equation_term=MomentumEquationTermSun2019(), v_max_factor=0, sound_speed_factor=0.1f0, + correct_properties=false, kwargs...) end @@ -309,6 +334,17 @@ end # so the `UpdateCallback` is required to update the shifting velocity. requires_update_callback(::ParticleShiftingTechnique{<:Any, false}) = true +# `ParticleShiftingTechnique{<:Any, <:Any, <:Any, <:Any, true}` +# means `correct_properties=true` +function create_cache_property_correction(initial_condition, + ::ParticleShiftingTechnique{<:Any, <:Any, <:Any, + <:Any, true}) + velocity_correction = zeros(eltype(initial_condition), ndims(initial_condition), + nparticles(initial_condition)) + + return (; velocity_correction) +end + # `ParticleShiftingTechnique{false}` means `integrate_shifting_velocity=false`. # Zero if PST is applied in a callback as a position correction # and not with a shifting velocity in the time integration stages @@ -522,6 +558,10 @@ function particle_shifting_from_callback!(u_ode, # Update the shifting velocity update_shifting_from_callback!(system, shifting, v_ode, u_ode, semi) + # Correct the particle properties for the shift that is applied below. + # The neighborhood searches and densities are only valid for the unshifted positions. + correct_properties!(v_ode, u_ode, shifting, system, semi, integrator.dt) + @trixi_timeit timer() "apply particle shifting" begin # Update the particle positions with the shifting velocity apply_particle_shifting!(u_ode, shifting, system, semi, integrator.dt) @@ -559,6 +599,98 @@ function apply_particle_shifting!(u_ode, ::ParticleShiftingTechnique{true}, return u_ode end +# No correction of the particle properties by default +@inline correct_properties!(v_ode, u_ode, shifting, system, semi, dt) = v_ode + +# `ParticleShiftingTechnique{<:Any, <:Any, <:Any, <:Any, true}` +# means `correct_properties=true`, which requires `integrate_shifting_velocity=false`. +# +# The properties of the shifted particle are approximated with a first-order +# Taylor expansion around its unshifted position, +# ϕ(r_a + δr_a) ≈ ϕ_a + (∇ϕ)_a ⋅ δr_a, +# where δr_a = Δt δv_a is the shift and +# (∇ϕ)_a = \sum_b m_b / ρ_b (ϕ_b - ϕ_a) ∇W_ab. +# The neighbors `b` in this sum are the particles of +# - this system and all other fluid systems, with their particle velocity, +# - open boundaries, with their particle velocity, +# - structures (TLSPH and rigid bodies), with their particle velocity, +# both with and without a viscosity in their boundary model, +# - no-slip walls (with a viscosity in the boundary model), with the physical wall velocity +# (zero for resting walls) and not with the extrapolated velocity used in the viscosity. +# Free-slip walls (without a viscosity in the boundary model) do not contribute +# (see `correction_velocity`). +# Systems for which `has_system_interaction` is `false` do not contribute either. +# The sum uses the mass and density of the boundary model for walls and structures, +# and the kernel gradient of this system (including its kernel gradient correction, if any) +# for all neighbors. +# +# TODO: Only the velocity is corrected so far. Correct density and pressure as well. +function correct_properties!(v_ode, u_ode, + ::ParticleShiftingTechnique{false, <:Any, <:Any, <:Any, true}, + system, semi, dt) + (; velocity_correction) = system.cache + + v = wrap_v(v_ode, system, semi) + u = wrap_u(u_ode, system, semi) + + @trixi_timeit timer() "correct particle properties" begin + # Compute (∇v)_a ⋅ δv_a for all particles before changing any velocity + set_zero!(velocity_correction) + + foreach_system_wrapped(semi, v_ode, + u_ode) do neighbor_system, v_neighbor, u_neighbor + if !has_system_interaction(system, neighbor_system, semi) + # No interaction between these systems. + return + end + + system_coords = current_coordinates(u, system) + neighbor_coords = current_coordinates(u_neighbor, neighbor_system) + + foreach_point_neighbor(system, neighbor_system, system_coords, neighbor_coords, + semi; + points=each_integrated_particle(system)) do particle, + neighbor, + pos_diff, + distance + m_b = @inbounds hydrodynamic_mass(neighbor_system, neighbor) + rho_b = @inbounds current_density(v_neighbor, neighbor_system, neighbor) + v_a = @inbounds current_velocity(v, system, particle) + v_b = @inbounds correction_velocity(v_neighbor, neighbor_system, + neighbor, v_a) + + # The function `delta_v` returns zero with `integrate_shifting_velocity=false` + delta_v_a = @inbounds extract_svector(system.cache.delta_v, system, + particle) + + grad_kernel = smoothing_kernel_grad(system, pos_diff, distance, particle) + + correction = m_b / rho_b * dot(grad_kernel, delta_v_a) * (v_b - v_a) + + for i in eachindex(correction) + @inbounds velocity_correction[i, particle] += correction[i] + end + end + end + + @threaded semi for particle in each_integrated_particle(system) + # The velocity is stored in the first `ndims(system)` rows of `v` + for i in axes(velocity_correction, 1) + @inbounds v[i, particle] += dt * velocity_correction[i, particle] + end + end + end + + return v_ode +end + +# The neighbor velocity `v_b` in the velocity gradient used in `correct_properties!`. +# This is the particle velocity for all systems except for free-slip walls +# (see the methods for `WallBoundarySystem`). +@propagate_inbounds function correction_velocity(v_neighbor, neighbor_system, neighbor, v_a) + return current_velocity(v_neighbor, neighbor_system, neighbor) +end + """ TransportVelocityAdami(; background_pressure::Real, modify_continuity_equation=false) diff --git a/test/examples/examples_fluid.jl b/test/examples/examples_fluid.jl index eb9aa573ec..c3e8d8f211 100644 --- a/test/examples/examples_fluid.jl +++ b/test/examples/examples_fluid.jl @@ -393,6 +393,17 @@ @test count_rhs_allocations(sol) == 0 end + @trixi_testset "fluid/periodic_channel_2d.jl with PST and property correction" begin + @trixi_test_nowarn trixi_include(@__MODULE__, + joinpath(examples_dir(), "fluid", + "periodic_channel_2d.jl"), + tspan=(0.0, 0.2), + shifting_technique=ParticleShiftingTechniqueSun2017(correct_properties=true), + extra_callback=UpdateCallback()) + @test sol.retcode == ReturnCode.Success + @test count_rhs_allocations(sol) == 0 + end + @trixi_testset "fluid/periodic_channel_2d.jl with TVF" begin @trixi_test_nowarn trixi_include(@__MODULE__, joinpath(examples_dir(), "fluid", diff --git a/test/schemes/fluid/shifting_techniques.jl b/test/schemes/fluid/shifting_techniques.jl index 20ef2372fa..2959c370d9 100644 --- a/test/schemes/fluid/shifting_techniques.jl +++ b/test/schemes/fluid/shifting_techniques.jl @@ -27,5 +27,138 @@ @test_throws ArgumentError ParticleShiftingTechnique(integrate_shifting_velocity=true, modify_continuity_equation=false, second_continuity_equation_term=ContinuityEquationTermSun2019()) + # Can't correct properties if integrating shifting velocity + @test_throws ArgumentError ParticleShiftingTechnique(correct_properties=true) + @test_throws ArgumentError ConsistentShiftingSun2019(correct_properties=true) + @test_nowarn ParticleShiftingTechniqueSun2017(correct_properties=true) + end + + @testset verbose=true "Property Correction" begin + particle_spacing = 0.1 + smoothing_kernel = WendlandC2Kernel{2}() + smoothing_length = 1.5 * particle_spacing + state_equation = StateEquationCole(; sound_speed=10.0, reference_density=1000.0, + exponent=1) + dt = 0.01 + + function create_fluid(velocity, shifting_technique; edac=false, + density_calculator=ContinuityDensity()) + fluid = RectangularShape(particle_spacing, (15, 15), (0.0, 0.0); + density=1000.0, velocity) + if edac + return EntropicallyDampedSPHSystem(fluid; smoothing_kernel, + smoothing_length, sound_speed=10.0, + density_calculator, + average_pressure_reduction=false, + shifting_technique) + end + + return WeaklyCompressibleSPHSystem(fluid; smoothing_kernel, smoothing_length, + density_calculator, state_equation, + shifting_technique) + end + + # Return `v` and `u` before and after applying the shifting in the callback + function apply_shifting(system, systems...; delta_v=nothing) + semi = Semidiscretization(system, systems...) + ode = semidiscretize(semi, (0.0, 1.0)) + v_ode, u_ode = ode.u0.x + TrixiParticles.update_systems_and_nhs(v_ode, u_ode, semi, 0.0) + + # `wrap_v` and `wrap_u` return unsafe pointer arrays + GC.@preserve v_ode u_ode begin + v0 = copy(TrixiParticles.wrap_v(v_ode, system, semi)) + u0 = copy(TrixiParticles.wrap_u(u_ode, system, semi)) + shifting = system.shifting_technique + + if isnothing(delta_v) + TrixiParticles.update_shifting_from_callback!(system, shifting, v_ode, + u_ode, semi) + else + # Prescribe a constant shifting velocity + system.cache.delta_v .= delta_v + end + TrixiParticles.correct_properties!(v_ode, u_ode, shifting, system, semi, dt) + TrixiParticles.apply_particle_shifting!(u_ode, shifting, system, semi, dt) + + v = copy(TrixiParticles.wrap_v(v_ode, system, semi)) + u = copy(TrixiParticles.wrap_u(u_ode, system, semi)) + end + + return v0, u0, v, u + end + + pst = ParticleShiftingTechniqueSun2017(correct_properties=true) + gradient = [1.0 2.0; -3.0 -1.0] + velocity(x) = gradient * x + delta_v = [0.3, -0.2] + + # Particles with full kernel support + is_inside(x) = all(coord -> 0.35 < coord < 1.15, x) + + density_calculators = (ContinuityDensity(), SummationDensity()) + @testset "Linear Velocity Field, EDAC: $edac" for edac in (false, true) + @testset "$density_calculator" for density_calculator in density_calculators + system = create_fluid(velocity, pst; edac, density_calculator) + v0, u0, v, u = apply_shifting(system; delta_v) + inside = findall(is_inside, eachcol(u0)) + @test length(inside) == 64 + + # The corrected velocity is the velocity field at the shifted positions + change = v[1:2, inside] - v0[1:2, inside] + expected = dt * gradient * delta_v + @test isapprox(change, repeat(expected, 1, 64), rtol=0.02) + @test isapprox(v[1:2, inside], gradient * u[:, inside], rtol=1e-3) + + # All other properties are unchanged + @test v[3:end, :] == v0[3:end, :] + end + end + + @testset "Computed Shifting Velocity" begin + # The shifting velocity is nonzero at the surface of the fluid + system = create_fluid(velocity, pst) + v0, u0, v, u = apply_shifting(system) + @test !iszero(system.cache.delta_v) + @test v[1:2, :] != v0[1:2, :] + @test u ≈ u0 + dt * system.cache.delta_v + + # Without the correction, the positions are the same, but the velocity is kept + system = create_fluid(velocity, ParticleShiftingTechniqueSun2017()) + v0_, u0_, v_, u_ = apply_shifting(system) + @test !haskey(system.cache, :velocity_correction) + @test v_ == v0_ + @test u_ == u + end + + @testset "Walls" begin + function shift_towards_wall(viscosity) + system = create_fluid((1.0, 0.0), pst) + # Wall below the fluid + wall = RectangularShape(particle_spacing, (15, 4), (0.0, -0.4); + density=1000.0) + boundary_model = BoundaryModelDummyParticles(wall.density, wall.mass, + AdamiPressureExtrapolation(), + smoothing_kernel, + smoothing_length; + state_equation, viscosity) + boundary = WallBoundarySystem(wall, boundary_model) + + return apply_shifting(system, boundary; delta_v=[0.0, -0.3]) + end + + # Free-slip walls don't change the uniform velocity + v0, u0, v, u = shift_towards_wall(nothing) + @test v[1:2, :] == v0[1:2, :] + + # No-slip walls slow down the particles that are shifted towards the wall + v0, u0, v, u = shift_towards_wall(ViscosityAdami(nu=0.01)) + first_row = findall(y -> y < 0.1, u0[2, :]) + @test length(first_row) == 15 + @test all(v[1, first_row] .< v0[1, first_row]) + # Particles that don't see the wall are unchanged + far = findall(y -> y > 0.4, u0[2, :]) + @test v[1:2, far] == v0[1:2, far] + end end end