From 4a97b62953cd7e646e498c6d64a8867ad3c68b05 Mon Sep 17 00:00:00 2001 From: Sven Berger Date: Thu, 29 Oct 2026 15:22:44 +0100 Subject: [PATCH 01/11] Use boundary-model kernel for corrected FSI pressure --- NEWS.md | 2 ++ docs/src/systems/total_lagrangian_sph.md | 3 ++ .../boundary/wall_boundary/dummy_particles.jl | 18 ++++++++++ src/schemes/structure/structure.jl | 15 ++++++++ test/schemes/fluid/rhs.jl | 36 ++++++++++++++++++- 5 files changed, 73 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index e649c96ffb..ee2b0eaec5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -26,6 +26,8 @@ used in the Julia ecosystem. Notable changes will be documented in this file for ### Important Bugfixes +- Fixed the structure-side kernel gradient in corrected FSI pressure to use the + boundary model's kernel rather than the TLSPH self-interaction kernel (#1342). - Fixed structure-fluid reaction forces with asymmetric corrected fluid kernel gradients by evaluating the fluid-first gradient directly. The continuity equation of `RigidBodySystem` boundaries with `ContinuityDensity` now uses the kernel gradient diff --git a/docs/src/systems/total_lagrangian_sph.md b/docs/src/systems/total_lagrangian_sph.md index b41db20ba8..af790232b1 100644 --- a/docs/src/systems/total_lagrangian_sph.md +++ b/docs/src/systems/total_lagrangian_sph.md @@ -130,6 +130,9 @@ In these cases, artificial viscosity is effective at stabilizing the fluid close structure, and we recommend using it in combination with penalty force to both prevent hourglass modes and stabilize the fluid close to the fluid-structure interface. +For dummy-particle fluid coupling, corrected pressure uses the boundary model's +kernel for the structure-side gradient, independently of the TLSPH self-interaction +kernel. The boundary model and fluid must use the same smoothing kernel and smoothing length. ## [Velocity Averaging](@id velocity_averaging) diff --git a/src/schemes/boundary/wall_boundary/dummy_particles.jl b/src/schemes/boundary/wall_boundary/dummy_particles.jl index b8dd014f3a..78afa25cb3 100644 --- a/src/schemes/boundary/wall_boundary/dummy_particles.jl +++ b/src/schemes/boundary/wall_boundary/dummy_particles.jl @@ -117,6 +117,24 @@ end return ndims(boundary_model.smoothing_kernel) end +# Evaluate fluid-pressure gradients with the boundary model's kernel and correction data. +# The TLSPH self-interaction kernel and correction matrix belong to the structure. +@inline system_correction(model::BoundaryModelDummyParticles) = model.correction +@inline skip_zero_distance(model::BoundaryModelDummyParticles) = skip_zero_distance(model.correction) + +@propagate_inbounds function kernel_correction_coefficient(model::BoundaryModelDummyParticles, + particle) + return model.cache.kernel_correction_coefficient[particle] +end + +@propagate_inbounds function dw_gamma(model::BoundaryModelDummyParticles, particle) + return extract_svector(model.cache.dw_gamma, Val(ndims(model)), particle) +end + +@propagate_inbounds function correction_matrix(model::BoundaryModelDummyParticles, particle) + return extract_smatrix(model.cache.correction_matrix, Val(ndims(model)), particle) +end + @inline function clip_negative_pressure(::BoundaryModelDummyParticles{<:Any, <:Any, CLIP}) where {CLIP} return CLIP end diff --git a/src/schemes/structure/structure.jl b/src/schemes/structure/structure.jl index df3d2a2375..a6123643dd 100644 --- a/src/schemes/structure/structure.jl +++ b/src/schemes/structure/structure.jl @@ -17,6 +17,21 @@ end end end +@inline function pressure_acceleration(particle_system, + neighbor_system::Union{TotalLagrangianSPHSystem{<:BoundaryModelDummyParticles}, + RigidBodySystem{<:BoundaryModelDummyParticles}}, + particle, neighbor, m_a, m_b, p_a, p_b, rho_a, rho_b, + pos_diff, distance, W_a, + correction::Union{KernelCorrection, + GradientCorrection, + BlendedGradientCorrection, + MixedKernelGradientCorrection}) + # Use the boundary model's kernel and correction data for fluid pressure. + return pressure_acceleration(particle_system, neighbor_system.boundary_model, + particle, neighbor, m_a, m_b, p_a, p_b, rho_a, rho_b, + pos_diff, distance, W_a, correction) +end + # Both FSI directions must find the same particle pairs and use matching boundary and # fluid kernels. The TLSPH kernel for solid mechanics may differ. # Note that this is only a restriction for `BoundaryModelDummyParticles`, since diff --git a/test/schemes/fluid/rhs.jl b/test/schemes/fluid/rhs.jl index cad35f838d..86e3d7e69f 100644 --- a/test/schemes/fluid/rhs.jl +++ b/test/schemes/fluid/rhs.jl @@ -309,6 +309,7 @@ boundary_density=AdamiPressureExtrapolation(), boundary_correction=nothing, clamped=false, + structure_smoothing_kernel=smoothing_kernel, structure_smoothing_length=smoothing_length) # The material mass is twice the hydrodynamic mass, so using the wrong mass # to convert the force on the structure to an acceleration fails the test. @@ -326,7 +327,8 @@ correction=boundary_correction) if structure_type === TotalLagrangianSPHSystem - return TotalLagrangianSPHSystem(structure; smoothing_kernel, + return TotalLagrangianSPHSystem(structure; + smoothing_kernel=structure_smoothing_kernel, smoothing_length=structure_smoothing_length, young_modulus=1e5, poisson_ratio=0.3, boundary_model, @@ -685,6 +687,38 @@ @test drho ≈ expected_density_rate end + @testset "Boundary kernel in corrected pressure" begin + for structure_type in structure_types, + correction in (KernelCorrection(), MixedKernelGradientCorrection()) + fluid_system = create_fluid_system("WCSPH", (0.0, 0.0), 1005.0, nothing; + correction) + # The boundary model's kernel and the fluid kernel have compact support 2.0. + # For TLSPH, r=1.5 lies outside the self-interaction kernel's support of 2*0.4. + structure_system = create_structure_system(structure_type, nothing; + structure_smoothing_kernel=WendlandC2Kernel{2}(), + structure_smoothing_length=0.4) + # Adami extrapolation gives p=500 and rho=1005 for this pair. Use that + # density from the start so the fluid correction uses the same volume. + structure_system.boundary_model.cache.density .= 1005.0 + fluid, structure, arrays, semi, + _ = initialize(fluid_system, structure_system) + force_fluid, force_structure = pair_forces(fluid, structure, arrays, semi) + # Independent cubic-spline gradients, including the fluid's kernel + # correction. The collinear gradient-correction matrix is the identity. + grad_s = SVector(-15 / (56pi), 0.0) + gamma_f = fluid_density / 1005.0 * (10 / (7pi) + 5 / (112pi)) + dw_gamma_f = -fluid_density / 1005.0 * grad_s / gamma_f + grad_f = (-grad_s - 5 / (112pi) * dw_gamma_f) / gamma_f + expected = 500.0 * (fluid_density / 1005.0)^2 * (grad_f - grad_s) + @test force_structure ≈ expected + @test force_fluid ≈ -expected + + # The boundary model's kernel and smoothing length must match the fluid's. + structure = TrixiParticles.@set structure.boundary_model.smoothing_length = 0.5 + @test_throws ArgumentError Semidiscretization(fluid, structure) + end + end + # The extra terms of the shifting techniques in the momentum equation act on the # fluid, but must not be applied to the structure. shifting_techniques = (ParticleShiftingTechnique(), From 57cc7bbee3f21447a04d482b5d5af3f8f3c6ca05 Mon Sep 17 00:00:00 2001 From: Sven Berger Date: Mon, 9 Nov 2026 17:33:19 +0100 Subject: [PATCH 02/11] address review --- docs/src/systems/total_lagrangian_sph.md | 8 +- src/general/abstract_system.jl | 25 +- src/general/corrections.jl | 65 ++--- src/general/smoothing_kernels.jl | 40 ++- .../boundary/wall_boundary/dummy_particles.jl | 50 +++- .../fluid/entropically_damped_sph/rhs.jl | 12 +- .../fluid/weakly_compressible_sph/rhs.jl | 12 +- src/schemes/structure/structure.jl | 37 ++- .../dummy_particles/dummy_particles.jl | 80 ++++++ test/schemes/fluid/rhs.jl | 255 ++++++++++++++++-- 10 files changed, 460 insertions(+), 124 deletions(-) diff --git a/docs/src/systems/total_lagrangian_sph.md b/docs/src/systems/total_lagrangian_sph.md index af790232b1..d0862a68f9 100644 --- a/docs/src/systems/total_lagrangian_sph.md +++ b/docs/src/systems/total_lagrangian_sph.md @@ -130,9 +130,11 @@ In these cases, artificial viscosity is effective at stabilizing the fluid close structure, and we recommend using it in combination with penalty force to both prevent hourglass modes and stabilize the fluid close to the fluid-structure interface. -For dummy-particle fluid coupling, corrected pressure uses the boundary model's -kernel for the structure-side gradient, independently of the TLSPH self-interaction -kernel. The boundary model and fluid must use the same smoothing kernel and smoothing length. +For dummy-particle fluid coupling with kernel or gradient correction, the structure-side +kernel gradient in the pressure acceleration is evaluated using the boundary model's +smoothing kernel, smoothing length, and correction data, independently of the TLSPH +self-interaction kernel. The boundary model and fluid must use the same smoothing +kernel and smoothing length. ## [Velocity Averaging](@id velocity_averaging) diff --git a/src/general/abstract_system.jl b/src/general/abstract_system.jl index 1871d5779e..54ad6f9954 100644 --- a/src/general/abstract_system.jl +++ b/src/general/abstract_system.jl @@ -139,19 +139,14 @@ end return skip_zero_distance(system_correction(system)) end +@inline skip_zero_distance(system, neighbor_system) = skip_zero_distance(system) + # Robust/safe version of the function below. In performance-critical code, manually check # the kernel support, call `skip_zero_distance` and then `smoothing_kernel_grad_unsafe`. @inline function smoothing_kernel_grad(system, pos_diff, distance, particle) - h = smoothing_length(system, particle) - compact_support_ = compact_support(system_smoothing_kernel(system), h) - - # See `almostzero` for an explanation of the zero distance check - if distance >= compact_support_ || - (skip_zero_distance(system) && distance < almostzero(h)) - return zero(pos_diff) - end - - return smoothing_kernel_grad_unsafe(system, pos_diff, distance, particle) + return corrected_kernel_grad(system_smoothing_kernel(system), pos_diff, distance, + smoothing_length(system, particle), + system_correction(system), system, particle) end # Skip the zero distance and compact support checks for maximum performance. @@ -162,6 +157,16 @@ end system_correction(system), system, particle) end +# A neighbor's correction may keep a coincident pair active even though this +# system's gradient vanishes. Use the safe gradient for those interactions. +@inline function interaction_kernel_grad(system, neighbor_system, pos_diff, distance, + particle) + if skip_zero_distance(system) && !skip_zero_distance(system, neighbor_system) + return smoothing_kernel_grad(system, pos_diff, distance, particle) + end + return smoothing_kernel_grad_unsafe(system, pos_diff, distance, particle) +end + # System updates do nothing by default, but can be dispatched if needed function update_positions!(system, v, u, v_ode, u_ode, semi, t) return system diff --git a/src/general/corrections.jl b/src/general/corrections.jl index 47973e6934..ffb93b6d08 100644 --- a/src/general/corrections.jl +++ b/src/general/corrections.jl @@ -136,7 +136,7 @@ function compute_correction_values!(system::AbstractBoundarySystem, end function compute_shepard_coeff!(system, system_coords, v_ode, u_ode, semi, - kernel_correction_coefficient) + kernel_correction_coefficient, kernel_data=system) set_zero!(kernel_correction_coefficient) # Use enabled neighbor systems for the correction value. @@ -159,7 +159,7 @@ function compute_shepard_coeff!(system, system_coords, v_ode, u_ode, semi, volume = m_b / rho_b kernel_correction_coefficient[particle] += volume * - smoothing_kernel(system, + smoothing_kernel(kernel_data, distance, particle) end @@ -201,7 +201,9 @@ function compute_correction_values!(system, ::Union{KernelCorrection, MixedKernelGradientCorrection}, system_coords, v_ode, - u_ode, semi, kernel_correction_coefficient, dw_gamma) + u_ode, semi, kernel_correction_coefficient, dw_gamma, + smoothing_kernel=system_smoothing_kernel(system), + kernel_data=system) set_zero!(kernel_correction_coefficient) set_zero!(dw_gamma) @@ -217,18 +219,18 @@ function compute_correction_values!(system, neighbor_coords = current_coordinates(u_neighbor_system, neighbor_system) - h = initial_smoothing_length(system) - # Loop over all pairs of particles and neighbors within the kernel cutoff foreach_point_neighbor(system, neighbor_system, system_coords, neighbor_coords, semi) do particle, neighbor, pos_diff, distance + h = smoothing_length(kernel_data, particle) + distance >= compact_support(smoothing_kernel, h) && return + rho_b = current_density(v_neighbor_system, neighbor_system, neighbor) m_b = hydrodynamic_mass(neighbor_system, neighbor) volume = m_b / rho_b # Use uncorrected kernel to compute correction coefficients - W = kernel(system_smoothing_kernel(system), distance, - smoothing_length(system, particle)) + W = kernel_unsafe(smoothing_kernel, distance, h) kernel_correction_coefficient[particle] += volume * W @@ -237,9 +239,7 @@ function compute_correction_values!(system, if distance >= almostzero(h) # Now that we know that `distance` is not zero, we can safely call the # unsafe version of the kernel gradient to avoid redundant zero checks. - grad_W = kernel_grad_unsafe(system_smoothing_kernel(system), pos_diff, - distance, - smoothing_length(system, particle)) + grad_W = kernel_grad_unsafe(smoothing_kernel, pos_diff, distance, h) tmp = volume * grad_W for i in axes(dw_gamma, 1) dw_gamma[i, particle] += tmp[i] @@ -350,9 +350,15 @@ end function compute_gradient_correction_matrix!(corr_matrix::AbstractArray, system, coordinates, v_ode, u_ode, semi, - correction, smoothing_kernel) + correction, smoothing_kernel, + kernel_data=system) set_zero!(corr_matrix) + # Mixed correction uses kernel-corrected gradients to assemble the matrix. + # The gradient-correction matrix itself is only available after assembly. + kernel_correction = correction isa MixedKernelGradientCorrection ? KernelCorrection() : + nothing + # Loop over all pairs of particles and neighbors within the kernel cutoff @trixi_timeit timer() "compute correction matrix" begin foreach_system_wrapped(semi, v_ode, @@ -364,40 +370,13 @@ function compute_gradient_correction_matrix!(corr_matrix::AbstractArray, system, end neighbor_coords = current_coordinates(u_neighbor_system, neighbor_system) - h = initial_smoothing_length(system) - foreach_point_neighbor(system, neighbor_system, coordinates, neighbor_coords, semi) do particle, neighbor, pos_diff, distance - function kernel_grad_local(correction, smoothing_kernel, pos_diff, distance, - smoothing_length_, system, particle) - return smoothing_kernel_grad_unsafe(system, pos_diff, distance, - particle) - end - - # Compute gradient of corrected kernel - function kernel_grad_local(correction::MixedKernelGradientCorrection, - smoothing_kernel, pos_diff, distance, - smoothing_length_, system, particle) - return corrected_kernel_grad_unsafe(smoothing_kernel, pos_diff, - distance, - smoothing_length_, - KernelCorrection(), system, - particle) - end - - # Skip neighbors with (almost) the same position because the kernel gradient - # is zero, but computing it would divide by zero (see `almostzero`). - # Note that `return` only exits the closure, i.e., skips the current neighbor. - if skip_zero_distance(correction) && distance < almostzero(h) - return - end - - # Now that we know that `distance` is not zero, we can safely call the unsafe - # version of the kernel gradient to avoid redundant zero checks. - smoothing_length_ = smoothing_length(system, particle) - grad_kernel = kernel_grad_local(correction, smoothing_kernel, pos_diff, - distance, smoothing_length_, system, - particle) + grad_kernel = corrected_kernel_grad(smoothing_kernel, pos_diff, distance, + smoothing_length(kernel_data, particle), + kernel_correction, kernel_data, + particle) + iszero(grad_kernel) && return volume = hydrodynamic_mass(neighbor_system, neighbor) / current_density(v_neighbor_system, neighbor_system, neighbor) diff --git a/src/general/smoothing_kernels.jl b/src/general/smoothing_kernels.jl index 46de7246e4..7f6c53ea4f 100644 --- a/src/general/smoothing_kernels.jl +++ b/src/general/smoothing_kernels.jl @@ -65,40 +65,58 @@ end return false end +# Safe kernel-gradient evaluation with explicit kernel settings and correction data. +@inline function corrected_kernel_grad(kernel, pos_diff, distance, h, correction, + correction_data, particle) + compact_support_ = compact_support(kernel, h) + + # See `almostzero` for an explanation of the zero distance check + if distance >= compact_support_ || + (skip_zero_distance(correction) && distance < almostzero(h)) + return zero(pos_diff) + end + + return corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, correction, + correction_data, particle) +end + @inline function corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, correction, - system, particle) + correction_data, particle) return kernel_grad_unsafe(kernel, pos_diff, distance, h) end @inline function corrected_kernel_grad_unsafe(kernel_, pos_diff, distance, h, - ::KernelCorrection, system, particle) + ::KernelCorrection, correction_data, particle) return (kernel_grad(kernel_, pos_diff, distance, h) .- - kernel(kernel_, distance, h) * dw_gamma(system, particle)) / - kernel_correction_coefficient(system, particle) + kernel(kernel_, distance, h) * dw_gamma(correction_data, particle)) / + kernel_correction_coefficient(correction_data, particle) end @inline function corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, - corr::BlendedGradientCorrection, system, + corr::BlendedGradientCorrection, + correction_data, particle) (; blending_factor) = corr grad = kernel_grad_unsafe(kernel, pos_diff, distance, h) return (1 - blending_factor) * grad + - blending_factor * correction_matrix(system, particle) * grad + blending_factor * correction_matrix(correction_data, particle) * grad end @inline function corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, - ::GradientCorrection, system, particle) + ::GradientCorrection, correction_data, + particle) grad = kernel_grad_unsafe(kernel, pos_diff, distance, h) - return correction_matrix(system, particle) * grad + return correction_matrix(correction_data, particle) * grad end @inline function corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, - ::MixedKernelGradientCorrection, system, + ::MixedKernelGradientCorrection, + correction_data, particle) grad = corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, KernelCorrection(), - system, particle) - return correction_matrix(system, particle) * grad + correction_data, particle) + return correction_matrix(correction_data, particle) * grad end @doc raw""" diff --git a/src/schemes/boundary/wall_boundary/dummy_particles.jl b/src/schemes/boundary/wall_boundary/dummy_particles.jl index 78afa25cb3..2726d97094 100644 --- a/src/schemes/boundary/wall_boundary/dummy_particles.jl +++ b/src/schemes/boundary/wall_boundary/dummy_particles.jl @@ -117,10 +117,22 @@ end return ndims(boundary_model.smoothing_kernel) end -# Evaluate fluid-pressure gradients with the boundary model's kernel and correction data. -# The TLSPH self-interaction kernel and correction matrix belong to the structure. -@inline system_correction(model::BoundaryModelDummyParticles) = model.correction -@inline skip_zero_distance(model::BoundaryModelDummyParticles) = skip_zero_distance(model.correction) +# Evaluate the structure-side FSI pressure-acceleration gradient with the boundary +# model's kernel and correction data. The TLSPH self-interaction kernel and correction +# matrix are used separately for solid mechanics. +@inline function smoothing_kernel_grad(model::BoundaryModelDummyParticles, pos_diff, + distance, particle) + (; smoothing_kernel, smoothing_length, correction) = model + return corrected_kernel_grad(smoothing_kernel, pos_diff, distance, smoothing_length, + correction, model, particle) +end + +@inline function smoothing_kernel_grad_unsafe(model::BoundaryModelDummyParticles, pos_diff, + distance, particle) + (; smoothing_kernel, smoothing_length, correction) = model + return corrected_kernel_grad_unsafe(smoothing_kernel, pos_diff, distance, + smoothing_length, correction, model, particle) +end @propagate_inbounds function kernel_correction_coefficient(model::BoundaryModelDummyParticles, particle) @@ -420,7 +432,7 @@ end compute_pressure!(boundary_model, density_calculator, system, v, u, v_ode, u_ode, semi) # These are only computed when using corrections - compute_correction_values!(system, correction, u, v_ode, u_ode, semi) + compute_correction_values!(boundary_model, system, correction, u, v_ode, u_ode, semi) compute_gradient_correction_matrix!(correction, boundary_model, system, u, v_ode, u_ode, semi) # `kernel_correct_density!` only performed for `SummationDensity` @@ -430,6 +442,31 @@ end return boundary_model end +# Use the system for coordinates and neighbor traversal, and the boundary model for +# the hydrodynamic kernel and correction cache. +function compute_correction_values!(boundary_model::BoundaryModelDummyParticles, system, + correction, u, v_ode, u_ode, semi) + return system +end + +function compute_correction_values!(boundary_model::BoundaryModelDummyParticles, system, + ::ShepardKernelCorrection, u, v_ode, u_ode, semi) + (; cache) = boundary_model + return compute_shepard_coeff!(system, current_coordinates(u, system), v_ode, u_ode, + semi, cache.kernel_correction_coefficient, boundary_model) +end + +function compute_correction_values!(boundary_model::BoundaryModelDummyParticles, system, + correction::Union{KernelCorrection, + MixedKernelGradientCorrection}, u, + v_ode, u_ode, semi) + (; cache, smoothing_kernel) = boundary_model + return compute_correction_values!(system, correction, current_coordinates(u, system), + v_ode, u_ode, semi, + cache.kernel_correction_coefficient, + cache.dw_gamma, smoothing_kernel, boundary_model) +end + function kernel_correct_density!(boundary_model, v, u, v_ode, u_ode, semi, correction, density_calculator) return boundary_model @@ -456,7 +493,8 @@ function compute_gradient_correction_matrix!(corr::Union{GradientCorrection, system_coords = current_coordinates(u, system) compute_gradient_correction_matrix!(correction_matrix, system, system_coords, - v_ode, u_ode, semi, correction, smoothing_kernel) + v_ode, u_ode, semi, correction, smoothing_kernel, + boundary_model) end function compute_density!(boundary_model, ::SummationDensity, system, v, u, v_ode, u_ode, diff --git a/src/schemes/fluid/entropically_damped_sph/rhs.jl b/src/schemes/fluid/entropically_damped_sph/rhs.jl index 9350d7f354..492748bb0d 100644 --- a/src/schemes/fluid/entropically_damped_sph/rhs.jl +++ b/src/schemes/fluid/entropically_damped_sph/rhs.jl @@ -17,15 +17,13 @@ function interact!(dv, v_particle_system, u_particle_system, neighbor, pos_diff, distance - # Skip neighbors with (almost) the same position because the kernel gradient - # is zero, but computing it would divide by zero (see `almostzero`). + # Skip coincident pairs only when the active pressure gradients both vanish. # Note that `return` only exits the closure, i.e., skips the current neighbor. - skip_zero_distance(particle_system) && distance < almostzero(h) && return + skip_zero_distance(particle_system, neighbor_system) && + distance < almostzero(h) && return - # Now that we know that `distance` is not zero, we can safely call the unsafe - # version of the kernel gradient to avoid redundant zero checks. - grad_kernel = smoothing_kernel_grad_unsafe(particle_system, pos_diff, - distance, particle) + grad_kernel = interaction_kernel_grad(particle_system, neighbor_system, + pos_diff, distance, particle) # `foreach_point_neighbor` makes sure that `particle` and `neighbor` are # in bounds of the respective system. For performance reasons, we use `@inbounds` diff --git a/src/schemes/fluid/weakly_compressible_sph/rhs.jl b/src/schemes/fluid/weakly_compressible_sph/rhs.jl index 068001228b..ca0c862e6c 100644 --- a/src/schemes/fluid/weakly_compressible_sph/rhs.jl +++ b/src/schemes/fluid/weakly_compressible_sph/rhs.jl @@ -46,17 +46,15 @@ function interact!(dv, v_particle_system, u_particle_system, backend, particle; init) do particle, neighbor, pos_diff, distance - # Skip neighbors with (almost) the same position because the kernel gradient - # is zero, but computing it would divide by zero (see `almostzero`). + # Skip coincident pairs only when the active pressure gradients both vanish. # Note that `return` only exits the closure, i.e., skips the current neighbor. - if skip_zero_distance(particle_system) && distance < almostzero(h) + if skip_zero_distance(particle_system, neighbor_system) && + distance < almostzero(h) return init end - # Now that we know that `distance` is not zero, we can safely call the unsafe - # version of the kernel gradient to avoid redundant zero checks. - grad_kernel = smoothing_kernel_grad_unsafe(particle_system, pos_diff, - distance, particle) + grad_kernel = interaction_kernel_grad(particle_system, neighbor_system, + pos_diff, distance, particle) # `foreach_neighbor` makes sure that `neighbor` is in bounds of `neighbor_system` m_b = @inbounds hydrodynamic_mass(neighbor_system, neighbor) diff --git a/src/schemes/structure/structure.jl b/src/schemes/structure/structure.jl index a6123643dd..09577ddda3 100644 --- a/src/schemes/structure/structure.jl +++ b/src/schemes/structure/structure.jl @@ -26,10 +26,25 @@ end GradientCorrection, BlendedGradientCorrection, MixedKernelGradientCorrection}) - # Use the boundary model's kernel and correction data for fluid pressure. - return pressure_acceleration(particle_system, neighbor_system.boundary_model, - particle, neighbor, m_a, m_b, p_a, p_b, rho_a, rho_b, - pos_diff, distance, W_a, correction) + W_b = smoothing_kernel_grad(neighbor_system.boundary_model, -pos_diff, distance, + neighbor) + + # With correction, the kernel gradient is not necessarily symmetric, so call the + # asymmetric version of the pressure acceleration formulation. + return pressure_acceleration_formulation(particle_system)(m_a, m_b, rho_a, rho_b, + p_a, p_b, W_a, W_b) +end + +@inline function skip_zero_distance(particle_system::AbstractFluidSystem, + neighbor_system::Union{TotalLagrangianSPHSystem{<:BoundaryModelDummyParticles}, + RigidBodySystem{<:BoundaryModelDummyParticles}}) + correction = system_correction(particle_system) + # Gradient corrections use the asymmetric pressure operator. Its boundary-side + # gradient can remain nonzero at coincidence even when the fluid gradient is zero. + if correction isa Union{GradientCorrection, BlendedGradientCorrection} + return skip_zero_distance(neighbor_system.boundary_model.correction) + end + return skip_zero_distance(correction) end # Both FSI directions must find the same particle pairs and use matching boundary and @@ -71,20 +86,18 @@ function interact_structure_fluid!(dv, v_particle_system, u_particle_system, foreach_point_neighbor(particle_system, neighbor_system, system_coords, neighbor_coords, semi; points=eachparticle) do particle, neighbor, pos_diff, distance - # Skip neighbors with (almost) the same position because the kernel gradient - # is zero, but computing it would divide by zero (see `almostzero`). + # Skip coincident pairs only when the active pressure gradients both vanish. # Note that `return` only exits the closure, i.e., skips the current neighbor. - skip_zero_distance(neighbor_system) && distance < almostzero(h) && return + skip_zero_distance(neighbor_system, particle_system) && + distance < almostzero(h) && return - # Now that we know that `distance` is not zero, we can safely call the unsafe - # version of the kernel gradient to avoid redundant zero checks. # Evaluate the gradient exactly as in the fluid-structure interaction, i.e., # with the fluid particle's kernel and correction and the fluid-first displacement. # Note that we cannot just flip the sign of the structure-first gradient, since # corrections like `KernelCorrection` add a term that keeps its sign # when the displacement is reversed. - grad_kernel_fluid = smoothing_kernel_grad_unsafe(neighbor_system, -pos_diff, - distance, neighbor) + grad_kernel_fluid = interaction_kernel_grad(neighbor_system, particle_system, + -pos_diff, distance, neighbor) m_b = hydrodynamic_mass(neighbor_system, neighbor) @@ -168,7 +181,7 @@ end particle, grad_kernel_fluid) # Use the boundary model's kernel and smoothing length, which may differ from # the TLSPH kernel for solid mechanics. - # The safe version also returns zero for coincident particles. + # The safe version handles coincident particles, including kernel normalization. return smoothing_kernel_grad(boundary_model, pos_diff, distance, particle) end diff --git a/test/schemes/boundary/dummy_particles/dummy_particles.jl b/test/schemes/boundary/dummy_particles/dummy_particles.jl index ef997a1ed6..ace521775c 100644 --- a/test/schemes/boundary/dummy_particles/dummy_particles.jl +++ b/test/schemes/boundary/dummy_particles/dummy_particles.jl @@ -10,6 +10,86 @@ @test repr(boundary_model) == expected_repr end + @testset "Kernel gradients" begin + corrections = (nothing, ShepardKernelCorrection(), + AkinciFreeSurfaceCorrection(1000.0), + KernelCorrection(), GradientCorrection(), + BlendedGradientCorrection(0.5), MixedKernelGradientCorrection()) + + @testset "$NDIMS dimensions, $(nameof(typeof(correction)))" for NDIMS in (2, 3), + correction in + corrections + + smoothing_kernel = SchoenbergCubicSplineKernel{NDIMS}() + smoothing_length = 0.7 + boundary = InitialCondition(; coordinates=zeros(NDIMS, 1), density=1000.0, + mass=1.0) + model = BoundaryModelDummyParticles(boundary.density, boundary.mass, + SummationDensity(), smoothing_kernel, + smoothing_length; correction) + system = WallBoundarySystem(boundary, model) + + # Prescribe nontrivial correction data independently of the update routines. + dw_gamma = SVector(ntuple(i -> 0.1 * i, NDIMS)) + matrix = [i == j ? 1.0 + i : 0.1 for i in 1:NDIMS, j in 1:NDIMS] + if haskey(model.cache, :kernel_correction_coefficient) + model.cache.kernel_correction_coefficient .= 2.0 + end + if haskey(model.cache, :dw_gamma) + model.cache.dw_gamma[:, 1] .= dw_gamma + end + if haskey(model.cache, :correction_matrix) + model.cache.correction_matrix[:, :, 1] .= matrix + end + + pos_diff = smoothing_length * SVector(ntuple(i -> (-1)^i * 0.2 * i, NDIMS)) + kernel_correction = correction isa Union{KernelCorrection, + MixedKernelGradientCorrection} + # Differentiate a normalized kernel with a spatially varying normalization. + # At x=0, gamma=2 and grad(gamma)/gamma=dw_gamma, matching the prescribed data. + expected = TrixiParticles.ForwardDiff.gradient(zeros(NDIMS)) do x + gamma = kernel_correction ? 2 * exp(dot(dw_gamma, x)) : 1.0 + TrixiParticles.kernel(smoothing_kernel, norm(x + pos_diff), + smoothing_length) / gamma + end + expected_zero = if kernel_correction + -TrixiParticles.kernel(smoothing_kernel, 0.0, smoothing_length) / 2 * + dw_gamma + else + zero(pos_diff) + end + if correction isa Union{GradientCorrection, MixedKernelGradientCorrection} + expected = matrix * expected + expected_zero = matrix * expected_zero + elseif correction isa BlendedGradientCorrection + expected = (expected + matrix * expected) / 2 + end + + # Exercise the shared helper through both a model and an actual system. + for data in (model, system) + @test @inferred(TrixiParticles.smoothing_kernel_grad(data, pos_diff, + norm(pos_diff), 1)) ≈ + expected + @test @inferred(TrixiParticles.smoothing_kernel_grad_unsafe(data, pos_diff, + norm(pos_diff), + 1)) ≈ expected + @test TrixiParticles.smoothing_kernel_grad(data, zero(pos_diff), 0.0, 1) ≈ + expected_zero + for distance in (2 * smoothing_length, 3 * smoothing_length) + @test iszero(TrixiParticles.smoothing_kernel_grad(data, pos_diff, + distance, 1)) + end + if !kernel_correction + distance = TrixiParticles.almostzero(smoothing_length) / 2 + @test iszero(TrixiParticles.smoothing_kernel_grad(data, + distance * + normalize(pos_diff), + distance, 1)) + end + end + end + end + @testset "Pressure clipping" begin state_equation = StateEquationCole(sound_speed=10.0, reference_density=1000.0, diff --git a/test/schemes/fluid/rhs.jl b/test/schemes/fluid/rhs.jl index 86e3d7e69f..7cb214e2ea 100644 --- a/test/schemes/fluid/rhs.jl +++ b/test/schemes/fluid/rhs.jl @@ -281,8 +281,9 @@ exponent=1) function create_fluid_system(scheme, velocity, density, viscosity; - shifting_technique=nothing, correction=nothing) - fluid = InitialCondition(; coordinates=zeros(2, 1), + shifting_technique=nothing, correction=nothing, + coordinates=zeros(2, 1)) + fluid = InitialCondition(; coordinates, velocity=reshape(collect(velocity), 2, 1), mass=[fluid_density * particle_spacing^2], density=[density], pressure=state_equation(density), @@ -299,9 +300,13 @@ # Note that the average pressure reduction is enabled by default # when using shifting. average_pressure_reduction = scheme == "EDAC with average pressure reduction" + pressure_acceleration = isnothing(correction) ? + TrixiParticles.inter_particle_averaged_pressure : + nothing return EntropicallyDampedSPHSystem(fluid; smoothing_kernel, smoothing_length, sound_speed, viscosity, shifting_technique, - average_pressure_reduction, + average_pressure_reduction, correction, + pressure_acceleration, density_calculator=ContinuityDensity()) end @@ -341,22 +346,26 @@ # Run the regular update step to compute the boundary pressure, the wall velocity # for the viscosity, and the average pressure of EDAC. Return the systems # stored in the semidiscretization and the wrapped arrays. - function initialize(fluid_system, structure_system) + function initialize(fluid_system, structure_system; interaction_matrix=nothing, + auxiliary_system=nothing) semi = if structure_system isa TotalLagrangianSPHSystem @test_logs (:info, r"^To create the self-interaction neighborhood search of a `TotalLagrangianSPHSystem`") begin - Semidiscretization(fluid_system, structure_system; + Semidiscretization(fluid_system, structure_system, auxiliary_system; + interaction_matrix, parallelization_backend=SerialBackend()) end else - @test_logs Semidiscretization(fluid_system, structure_system; + @test_logs Semidiscretization(fluid_system, structure_system, + auxiliary_system; + interaction_matrix, parallelization_backend=SerialBackend()) end ode = semidiscretize(semi, (0.0, 0.01)) v_ode, u_ode = ode.u0.x TrixiParticles.update_systems_and_nhs(v_ode, u_ode, ode.p.semi, 0.0) - fluid, structure = ode.p.semi.systems + fluid, structure = ode.p.semi.systems[1:2] semi = ode.p.semi arrays = (TrixiParticles.wrap_v(v_ode, fluid, semi), TrixiParticles.wrap_u(u_ode, fluid, semi), @@ -617,6 +626,93 @@ end end + @testset "Boundary correction values are initialized" begin + boundary_configurations = ((TotalLagrangianSPHSystem, + AdamiPressureExtrapolation()), + (RigidBodySystem, AdamiPressureExtrapolation()), + (RigidBodySystem, ContinuityDensity())) + for (structure_type, boundary_density) in boundary_configurations, + correction in (ShepardKernelCorrection(), KernelCorrection()) + fluid_system = create_fluid_system("WCSPH", (0.0, 0.0), 1005.0, nothing) + structure_system = create_structure_system(structure_type, nothing; + boundary_density, + boundary_correction=correction, + structure_smoothing_kernel=WendlandC2Kernel{2}(), + structure_smoothing_length=0.4) + # Exclude structure self-interactions to isolate the fluid contribution. + fluid, structure, arrays, semi, + ode = initialize(fluid_system, structure_system; + interaction_matrix=[true true; true false]) + cache = structure.boundary_model.cache + v_ode, u_ode = ode.u0.x + + for density in (1005.0, 1010.0) + arrays[1][end, 1] = density + # Uninitialized storage must never make this test pass by chance. + cache.kernel_correction_coefficient .= NaN + if haskey(cache, :dw_gamma) + cache.dw_gamma .= NaN + end + TrixiParticles.update_systems_and_nhs(v_ode, u_ode, semi, 0.0) + + # Independent cubic-spline values at r=1.5 and h=1.0. + expected_coefficient = fluid_density / density * 5 / (112pi) + @test cache.kernel_correction_coefficient ≈ [expected_coefficient] + if haskey(cache, :dw_gamma) + @test cache.dw_gamma ≈ reshape([-6.0, 0.0], 2, 1) + end + end + end + end + + @testset "Boundary gradient correction uses the hydrodynamic kernel" begin + # Three non-collinear fluid neighbors, all outside the TLSPH kernel support. + coordinates = [0.0 1.5 2.4; 0.0 -1.0 1.2] + displacements = (SVector(1.5, 0.0), SVector(0.0, 1.0), SVector(-0.9, -1.2)) + kernel_values = (5 / (112pi), 5 / (14pi), 5 / (112pi)) + gradients = (SVector(-15 / (56pi), 0.0), SVector(0.0, -15 / (14pi)), + SVector(9 / (56pi), 12 / (56pi))) + volume = fluid_density / 1005.0 + + for structure_type in structure_types, + correction in (GradientCorrection(), BlendedGradientCorrection(0.5), + MixedKernelGradientCorrection()) + + fluid_ic = InitialCondition(; coordinates, density=1005.0, + mass=fill(fluid_density, 3), particle_spacing) + fluid_system = WeaklyCompressibleSPHSystem(fluid_ic; smoothing_kernel, + smoothing_length, state_equation, + density_calculator=ContinuityDensity()) + structure_system = create_structure_system(structure_type, nothing; + boundary_correction=correction, + structure_smoothing_kernel=WendlandC2Kernel{2}(), + structure_smoothing_length=0.4) + structure_system.boundary_model.cache.correction_matrix .= NaN + _, structure, _, _, + _ = initialize(fluid_system, structure_system; + interaction_matrix=[true true; true false]) + + # Independent cubic-spline values at r=1.0 and r=1.5 with h=1.0. + expected_gradients = gradients + if correction isa MixedKernelGradientCorrection + coefficient = volume * sum(kernel_values) + dw_gamma = volume * sum(gradients) / coefficient + expected_gradients = map(gradients, kernel_values) do gradient, value + (gradient - value * dw_gamma) / coefficient + end + end + moment = zeros(2, 2) + for (gradient, pos_diff) in zip(expected_gradients, displacements) + moment .-= volume * gradient * transpose(pos_diff) + end + matrix = structure.boundary_model.cache.correction_matrix[:, :, 1] + @test abs(det(moment)) > 1e-9 + @test !isapprox(matrix, I) + # Corrected gradients must reproduce a linear field exactly. + @test matrix * moment ≈ I + end + end + # Without a fluid correction, the pair force only uses the fluid's kernel, # so a correction of the structure's boundary model must not change it. # The continuity equation of the boundary particles must use the boundary model. @@ -687,31 +783,77 @@ @test drho ≈ expected_density_rate end - @testset "Boundary kernel in corrected pressure" begin + @testset "Boundary kernel in pressure acceleration with corrected gradients" begin + coordinates = [0.0 1.5 2.4; 0.0 -1.0 1.2] + displacements = (SVector(1.5, 0.0), SVector(0.0, 1.0), SVector(-0.9, -1.2)) + kernel_values = (5 / (112pi), 5 / (14pi), 5 / (112pi)) + gradients = (SVector(-15 / (56pi), 0.0), SVector(0.0, -15 / (14pi)), + SVector(9 / (56pi), 12 / (56pi))) + volume = fluid_density / 1005.0 + coefficient = volume * sum(kernel_values) + dw_gamma = volume * sum(gradients) / coefficient + boundary_gradients = map(gradients, kernel_values) do gradient, value + (gradient - value * dw_gamma) / coefficient + end + moment = zeros(2, 2) + for (gradient, pos_diff) in zip(boundary_gradients, displacements) + moment .-= volume * gradient * transpose(pos_diff) + end + boundary_matrix = inv(moment) + for structure_type in structure_types, correction in (KernelCorrection(), MixedKernelGradientCorrection()) - fluid_system = create_fluid_system("WCSPH", (0.0, 0.0), 1005.0, nothing; - correction) - # The boundary model's kernel and the fluid kernel have compact support 2.0. - # For TLSPH, r=1.5 lies outside the self-interaction kernel's support of 2*0.4. + fluid_ic = InitialCondition(; coordinates, density=1005.0, + mass=fill(fluid_density, 3), particle_spacing) + fluid_system = WeaklyCompressibleSPHSystem(fluid_ic; smoothing_kernel, + smoothing_length, state_equation, + density_calculator=ContinuityDensity(), + correction) + # All three fluid neighbors are inside the boundary kernel's support + # and outside the TLSPH self-interaction kernel's support of 2*0.4. structure_system = create_structure_system(structure_type, nothing; + boundary_correction=correction, structure_smoothing_kernel=WendlandC2Kernel{2}(), structure_smoothing_length=0.4) - # Adami extrapolation gives p=500 and rho=1005 for this pair. Use that - # density from the start so the fluid correction uses the same volume. - structure_system.boundary_model.cache.density .= 1005.0 + cache = structure_system.boundary_model.cache + cache.density .= 1005.0 + cache.kernel_correction_coefficient .= NaN + cache.dw_gamma .= NaN + if haskey(cache, :correction_matrix) + cache.correction_matrix .= NaN + end fluid, structure, arrays, semi, - _ = initialize(fluid_system, structure_system) + _ = initialize(fluid_system, structure_system; + interaction_matrix=[true true; true false]) + + # Prescribe fluid correction data to isolate boundary initialization. + fluid_coefficient = 2.0 + fluid_dw_gamma = SVector(0.2, -0.3) + fluid_matrix = [2.0 0.3; 0.4 1.5] + fluid.cache.kernel_correction_coefficient .= fluid_coefficient + fluid.cache.dw_gamma .= fluid_dw_gamma + if haskey(fluid.cache, :correction_matrix) + for particle in eachparticle(fluid) + fluid.cache.correction_matrix[:, :, particle] .= fluid_matrix + end + end force_fluid, force_structure = pair_forces(fluid, structure, arrays, semi) - # Independent cubic-spline gradients, including the fluid's kernel - # correction. The collinear gradient-correction matrix is the identity. - grad_s = SVector(-15 / (56pi), 0.0) - gamma_f = fluid_density / 1005.0 * (10 / (7pi) + 5 / (112pi)) - dw_gamma_f = -fluid_density / 1005.0 * grad_s / gamma_f - grad_f = (-grad_s - 5 / (112pi) * dw_gamma_f) / gamma_f - expected = 500.0 * (fluid_density / 1005.0)^2 * (grad_f - grad_s) - @test force_structure ≈ expected - @test force_fluid ≈ -expected + + # Independent pressure forces from the cubic-spline values above. + expected_forces = map(gradients, kernel_values, + boundary_gradients) do gradient, + value, + boundary_gradient + fluid_gradient = (-gradient - value * fluid_dw_gamma) / + fluid_coefficient + if correction isa MixedKernelGradientCorrection + fluid_gradient = fluid_matrix * fluid_gradient + boundary_gradient = boundary_matrix * boundary_gradient + end + 500.0 * volume^2 * (boundary_gradient - fluid_gradient) + end + @test force_fluid ≈ first(expected_forces) + @test force_structure ≈ -sum(expected_forces) # The boundary model's kernel and smoothing length must match the fluid's. structure = TrixiParticles.@set structure.boundary_model.smoothing_length = 0.5 @@ -719,6 +861,69 @@ end end + @testset "Coincident FSI pressure with boundary correction" begin + # Two auxiliary fluid particles provide a nonzero normalization gradient + # and a full-rank correction matrix without contributing to the tested pair. + auxiliary_ic = InitialCondition(; coordinates=[0.5 1.5; 0.0 -1.0], + density=1005.0, mass=fill(fluid_density, 2), + pressure=500.0, particle_spacing) + volume = fluid_density / 1005.0 + kernel_zero = 10 / (7pi) + kernel_one = 5 / (14pi) + coefficient = volume * (kernel_zero + 2 * kernel_one) + dw_gamma = SVector(-0.5, -0.5) + boundary_gradient = -kernel_zero * dw_gamma / coefficient + gradient_x = (SVector(-15 / (14pi), 0.0) - kernel_one * dw_gamma) / coefficient + gradient_y = (SVector(0.0, -15 / (14pi)) - kernel_one * dw_gamma) / coefficient + near_zero_configurations = (configurations[1:5]..., configurations[9]) + fluid_corrections = (GradientCorrection(), BlendedGradientCorrection(0.5), + nothing) + boundary_corrections = (KernelCorrection(), MixedKernelGradientCorrection()) + distances = (0.0, TrixiParticles.almostzero(smoothing_length) / 2) + + for structure_type in structure_types, + config in near_zero_configurations, + correction in fluid_corrections, + boundary_correction in boundary_corrections, + distance in distances + scheme, viscosity = config + fluid_coordinates = reshape([1.5 + distance, 0.0], 2, 1) + fluid_system = create_fluid_system(scheme, (1.0, -0.5), 1005.0, viscosity; + correction, + coordinates=fluid_coordinates) + structure_system = create_structure_system(structure_type, viscosity; + boundary_correction) + auxiliary_system = WeaklyCompressibleSPHSystem(auxiliary_ic; + smoothing_kernel, + smoothing_length, + state_equation, + density_calculator=ContinuityDensity()) + fluid, structure, arrays, semi, + _ = initialize(fluid_system, structure_system; auxiliary_system, + interaction_matrix=[true true true; true false true; + true true true]) + (force_fluid, force_structure, + dv_structure) = pair_forces(fluid, structure, arrays, semi) + + gradient = boundary_gradient + if boundary_correction isa MixedKernelGradientCorrection + moment = -volume * + (boundary_gradient * transpose(SVector(-distance, 0.0)) + + gradient_x * transpose(SVector(1.0, 0.0)) + + gradient_y * transpose(SVector(0.0, 1.0))) + gradient = inv(moment) * gradient + end + pressure = scheme == "EDAC with average pressure reduction" ? 0.0 : 500.0 + expected = isnothing(correction) ? zero(gradient) : + pressure * volume^2 * gradient + @test all(isfinite, force_fluid) + @test all(isfinite, dv_structure) + @test isapprox(force_fluid, expected, atol=sqrt(eps())) + @test isapprox(force_structure, -expected, atol=sqrt(eps())) + @test isapprox(force_structure, -force_fluid, atol=sqrt(eps())) + end + end + # The extra terms of the shifting techniques in the momentum equation act on the # fluid, but must not be applied to the structure. shifting_techniques = (ParticleShiftingTechnique(), From ea01a8c6428df5fdc9e2bdfec6d1299ae63ad933 Mon Sep 17 00:00:00 2001 From: Sven Berger Date: Tue, 10 Nov 2026 00:05:02 +0100 Subject: [PATCH 03/11] reduce tests --- .../dummy_particles/dummy_particles.jl | 80 ------- test/schemes/fluid/rhs.jl | 226 ++++++------------ 2 files changed, 71 insertions(+), 235 deletions(-) diff --git a/test/schemes/boundary/dummy_particles/dummy_particles.jl b/test/schemes/boundary/dummy_particles/dummy_particles.jl index ace521775c..ef997a1ed6 100644 --- a/test/schemes/boundary/dummy_particles/dummy_particles.jl +++ b/test/schemes/boundary/dummy_particles/dummy_particles.jl @@ -10,86 +10,6 @@ @test repr(boundary_model) == expected_repr end - @testset "Kernel gradients" begin - corrections = (nothing, ShepardKernelCorrection(), - AkinciFreeSurfaceCorrection(1000.0), - KernelCorrection(), GradientCorrection(), - BlendedGradientCorrection(0.5), MixedKernelGradientCorrection()) - - @testset "$NDIMS dimensions, $(nameof(typeof(correction)))" for NDIMS in (2, 3), - correction in - corrections - - smoothing_kernel = SchoenbergCubicSplineKernel{NDIMS}() - smoothing_length = 0.7 - boundary = InitialCondition(; coordinates=zeros(NDIMS, 1), density=1000.0, - mass=1.0) - model = BoundaryModelDummyParticles(boundary.density, boundary.mass, - SummationDensity(), smoothing_kernel, - smoothing_length; correction) - system = WallBoundarySystem(boundary, model) - - # Prescribe nontrivial correction data independently of the update routines. - dw_gamma = SVector(ntuple(i -> 0.1 * i, NDIMS)) - matrix = [i == j ? 1.0 + i : 0.1 for i in 1:NDIMS, j in 1:NDIMS] - if haskey(model.cache, :kernel_correction_coefficient) - model.cache.kernel_correction_coefficient .= 2.0 - end - if haskey(model.cache, :dw_gamma) - model.cache.dw_gamma[:, 1] .= dw_gamma - end - if haskey(model.cache, :correction_matrix) - model.cache.correction_matrix[:, :, 1] .= matrix - end - - pos_diff = smoothing_length * SVector(ntuple(i -> (-1)^i * 0.2 * i, NDIMS)) - kernel_correction = correction isa Union{KernelCorrection, - MixedKernelGradientCorrection} - # Differentiate a normalized kernel with a spatially varying normalization. - # At x=0, gamma=2 and grad(gamma)/gamma=dw_gamma, matching the prescribed data. - expected = TrixiParticles.ForwardDiff.gradient(zeros(NDIMS)) do x - gamma = kernel_correction ? 2 * exp(dot(dw_gamma, x)) : 1.0 - TrixiParticles.kernel(smoothing_kernel, norm(x + pos_diff), - smoothing_length) / gamma - end - expected_zero = if kernel_correction - -TrixiParticles.kernel(smoothing_kernel, 0.0, smoothing_length) / 2 * - dw_gamma - else - zero(pos_diff) - end - if correction isa Union{GradientCorrection, MixedKernelGradientCorrection} - expected = matrix * expected - expected_zero = matrix * expected_zero - elseif correction isa BlendedGradientCorrection - expected = (expected + matrix * expected) / 2 - end - - # Exercise the shared helper through both a model and an actual system. - for data in (model, system) - @test @inferred(TrixiParticles.smoothing_kernel_grad(data, pos_diff, - norm(pos_diff), 1)) ≈ - expected - @test @inferred(TrixiParticles.smoothing_kernel_grad_unsafe(data, pos_diff, - norm(pos_diff), - 1)) ≈ expected - @test TrixiParticles.smoothing_kernel_grad(data, zero(pos_diff), 0.0, 1) ≈ - expected_zero - for distance in (2 * smoothing_length, 3 * smoothing_length) - @test iszero(TrixiParticles.smoothing_kernel_grad(data, pos_diff, - distance, 1)) - end - if !kernel_correction - distance = TrixiParticles.almostzero(smoothing_length) / 2 - @test iszero(TrixiParticles.smoothing_kernel_grad(data, - distance * - normalize(pos_diff), - distance, 1)) - end - end - end - end - @testset "Pressure clipping" begin state_equation = StateEquationCole(sound_speed=10.0, reference_density=1000.0, diff --git a/test/schemes/fluid/rhs.jl b/test/schemes/fluid/rhs.jl index 7cb214e2ea..83c4b19dfe 100644 --- a/test/schemes/fluid/rhs.jl +++ b/test/schemes/fluid/rhs.jl @@ -346,18 +346,16 @@ # Run the regular update step to compute the boundary pressure, the wall velocity # for the viscosity, and the average pressure of EDAC. Return the systems # stored in the semidiscretization and the wrapped arrays. - function initialize(fluid_system, structure_system; interaction_matrix=nothing, - auxiliary_system=nothing) + function initialize(fluid_system, structure_system; interaction_matrix=nothing) semi = if structure_system isa TotalLagrangianSPHSystem @test_logs (:info, r"^To create the self-interaction neighborhood search of a `TotalLagrangianSPHSystem`") begin - Semidiscretization(fluid_system, structure_system, auxiliary_system; + Semidiscretization(fluid_system, structure_system; interaction_matrix, parallelization_backend=SerialBackend()) end else - @test_logs Semidiscretization(fluid_system, structure_system, - auxiliary_system; + @test_logs Semidiscretization(fluid_system, structure_system; interaction_matrix, parallelization_backend=SerialBackend()) end @@ -365,7 +363,7 @@ v_ode, u_ode = ode.u0.x TrixiParticles.update_systems_and_nhs(v_ode, u_ode, ode.p.semi, 0.0) - fluid, structure = ode.p.semi.systems[1:2] + fluid, structure = ode.p.semi.systems semi = ode.p.semi arrays = (TrixiParticles.wrap_v(v_ode, fluid, semi), TrixiParticles.wrap_u(u_ode, fluid, semi), @@ -626,97 +624,11 @@ end end - @testset "Boundary correction values are initialized" begin - boundary_configurations = ((TotalLagrangianSPHSystem, - AdamiPressureExtrapolation()), - (RigidBodySystem, AdamiPressureExtrapolation()), - (RigidBodySystem, ContinuityDensity())) - for (structure_type, boundary_density) in boundary_configurations, - correction in (ShepardKernelCorrection(), KernelCorrection()) - fluid_system = create_fluid_system("WCSPH", (0.0, 0.0), 1005.0, nothing) - structure_system = create_structure_system(structure_type, nothing; - boundary_density, - boundary_correction=correction, - structure_smoothing_kernel=WendlandC2Kernel{2}(), - structure_smoothing_length=0.4) - # Exclude structure self-interactions to isolate the fluid contribution. - fluid, structure, arrays, semi, - ode = initialize(fluid_system, structure_system; - interaction_matrix=[true true; true false]) - cache = structure.boundary_model.cache - v_ode, u_ode = ode.u0.x - - for density in (1005.0, 1010.0) - arrays[1][end, 1] = density - # Uninitialized storage must never make this test pass by chance. - cache.kernel_correction_coefficient .= NaN - if haskey(cache, :dw_gamma) - cache.dw_gamma .= NaN - end - TrixiParticles.update_systems_and_nhs(v_ode, u_ode, semi, 0.0) - - # Independent cubic-spline values at r=1.5 and h=1.0. - expected_coefficient = fluid_density / density * 5 / (112pi) - @test cache.kernel_correction_coefficient ≈ [expected_coefficient] - if haskey(cache, :dw_gamma) - @test cache.dw_gamma ≈ reshape([-6.0, 0.0], 2, 1) - end - end - end - end - - @testset "Boundary gradient correction uses the hydrodynamic kernel" begin - # Three non-collinear fluid neighbors, all outside the TLSPH kernel support. - coordinates = [0.0 1.5 2.4; 0.0 -1.0 1.2] - displacements = (SVector(1.5, 0.0), SVector(0.0, 1.0), SVector(-0.9, -1.2)) - kernel_values = (5 / (112pi), 5 / (14pi), 5 / (112pi)) - gradients = (SVector(-15 / (56pi), 0.0), SVector(0.0, -15 / (14pi)), - SVector(9 / (56pi), 12 / (56pi))) - volume = fluid_density / 1005.0 - - for structure_type in structure_types, - correction in (GradientCorrection(), BlendedGradientCorrection(0.5), - MixedKernelGradientCorrection()) - - fluid_ic = InitialCondition(; coordinates, density=1005.0, - mass=fill(fluid_density, 3), particle_spacing) - fluid_system = WeaklyCompressibleSPHSystem(fluid_ic; smoothing_kernel, - smoothing_length, state_equation, - density_calculator=ContinuityDensity()) - structure_system = create_structure_system(structure_type, nothing; - boundary_correction=correction, - structure_smoothing_kernel=WendlandC2Kernel{2}(), - structure_smoothing_length=0.4) - structure_system.boundary_model.cache.correction_matrix .= NaN - _, structure, _, _, - _ = initialize(fluid_system, structure_system; - interaction_matrix=[true true; true false]) - - # Independent cubic-spline values at r=1.0 and r=1.5 with h=1.0. - expected_gradients = gradients - if correction isa MixedKernelGradientCorrection - coefficient = volume * sum(kernel_values) - dw_gamma = volume * sum(gradients) / coefficient - expected_gradients = map(gradients, kernel_values) do gradient, value - (gradient - value * dw_gamma) / coefficient - end - end - moment = zeros(2, 2) - for (gradient, pos_diff) in zip(expected_gradients, displacements) - moment .-= volume * gradient * transpose(pos_diff) - end - matrix = structure.boundary_model.cache.correction_matrix[:, :, 1] - @test abs(det(moment)) > 1e-9 - @test !isapprox(matrix, I) - # Corrected gradients must reproduce a linear field exactly. - @test matrix * moment ≈ I - end - end - # Without a fluid correction, the pair force only uses the fluid's kernel, # so a correction of the structure's boundary model must not change it. # The continuity equation of the boundary particles must use the boundary model. - boundary_corrections = (AkinciFreeSurfaceCorrection(fluid_density), + boundary_corrections = (ShepardKernelCorrection(), + AkinciFreeSurfaceCorrection(fluid_density), KernelCorrection()) @testset "Boundary Model Correction `$(nameof(typeof(boundary_correction)))`" for boundary_correction in @@ -730,8 +642,19 @@ structure_system = create_structure_system(RigidBodySystem, viscosity; boundary_density=ContinuityDensity(), boundary_correction=boundary_correction_) + if boundary_correction_ isa ShepardKernelCorrection + structure_system.boundary_model.cache.kernel_correction_coefficient .= NaN + end (fluid, structure, arrays, - semi, _) = initialize(fluid_system, structure_system) + semi, + _) = initialize(fluid_system, structure_system; + interaction_matrix=[true true; true false]) + + if boundary_correction_ isa ShepardKernelCorrection + expected_coefficient = fluid_density / density * 5 / (112pi) + @test structure.boundary_model.cache.kernel_correction_coefficient ≈ + [expected_coefficient] + end return (pair_forces(fluid, structure, arrays, semi)..., fluid, structure) end @@ -792,17 +715,23 @@ volume = fluid_density / 1005.0 coefficient = volume * sum(kernel_values) dw_gamma = volume * sum(gradients) / coefficient - boundary_gradients = map(gradients, kernel_values) do gradient, value + normalized_gradients = map(gradients, kernel_values) do gradient, value (gradient - value * dw_gamma) / coefficient end - moment = zeros(2, 2) - for (gradient, pos_diff) in zip(boundary_gradients, displacements) - moment .-= volume * gradient * transpose(pos_diff) - end - boundary_matrix = inv(moment) for structure_type in structure_types, - correction in (KernelCorrection(), MixedKernelGradientCorrection()) + correction in (KernelCorrection(), GradientCorrection(), + BlendedGradientCorrection(0.5), MixedKernelGradientCorrection()) + + kernel_correction = correction isa Union{KernelCorrection, + MixedKernelGradientCorrection} + boundary_gradients = kernel_correction ? normalized_gradients : gradients + moment = zeros(2, 2) + for (gradient, pos_diff) in zip(boundary_gradients, displacements) + moment .-= volume * gradient * transpose(pos_diff) + end + boundary_matrix = inv(moment) + fluid_ic = InitialCondition(; coordinates, density=1005.0, mass=fill(fluid_density, 3), particle_spacing) fluid_system = WeaklyCompressibleSPHSystem(fluid_ic; smoothing_kernel, @@ -817,8 +746,10 @@ structure_smoothing_length=0.4) cache = structure_system.boundary_model.cache cache.density .= 1005.0 - cache.kernel_correction_coefficient .= NaN - cache.dw_gamma .= NaN + if kernel_correction + cache.kernel_correction_coefficient .= NaN + cache.dw_gamma .= NaN + end if haskey(cache, :correction_matrix) cache.correction_matrix .= NaN end @@ -830,8 +761,10 @@ fluid_coefficient = 2.0 fluid_dw_gamma = SVector(0.2, -0.3) fluid_matrix = [2.0 0.3; 0.4 1.5] - fluid.cache.kernel_correction_coefficient .= fluid_coefficient - fluid.cache.dw_gamma .= fluid_dw_gamma + if kernel_correction + fluid.cache.kernel_correction_coefficient .= fluid_coefficient + fluid.cache.dw_gamma .= fluid_dw_gamma + end if haskey(fluid.cache, :correction_matrix) for particle in eachparticle(fluid) fluid.cache.correction_matrix[:, :, particle] .= fluid_matrix @@ -844,83 +777,66 @@ boundary_gradients) do gradient, value, boundary_gradient - fluid_gradient = (-gradient - value * fluid_dw_gamma) / - fluid_coefficient - if correction isa MixedKernelGradientCorrection + fluid_gradient = kernel_correction ? + (-gradient - value * fluid_dw_gamma) / + fluid_coefficient : -gradient + if correction isa Union{GradientCorrection, + MixedKernelGradientCorrection} fluid_gradient = fluid_matrix * fluid_gradient boundary_gradient = boundary_matrix * boundary_gradient + elseif correction isa BlendedGradientCorrection + fluid_gradient = (fluid_gradient + fluid_matrix * fluid_gradient) / + 2 + boundary_gradient = (boundary_gradient + + boundary_matrix * boundary_gradient) / 2 end 500.0 * volume^2 * (boundary_gradient - fluid_gradient) end @test force_fluid ≈ first(expected_forces) @test force_structure ≈ -sum(expected_forces) - - # The boundary model's kernel and smoothing length must match the fluid's. - structure = TrixiParticles.@set structure.boundary_model.smoothing_length = 0.5 - @test_throws ArgumentError Semidiscretization(fluid, structure) end end @testset "Coincident FSI pressure with boundary correction" begin - # Two auxiliary fluid particles provide a nonzero normalization gradient - # and a full-rank correction matrix without contributing to the tested pair. - auxiliary_ic = InitialCondition(; coordinates=[0.5 1.5; 0.0 -1.0], - density=1005.0, mass=fill(fluid_density, 2), - pressure=500.0, particle_spacing) volume = fluid_density / 1005.0 kernel_zero = 10 / (7pi) - kernel_one = 5 / (14pi) - coefficient = volume * (kernel_zero + 2 * kernel_one) + coefficient = 2.0 dw_gamma = SVector(-0.5, -0.5) boundary_gradient = -kernel_zero * dw_gamma / coefficient - gradient_x = (SVector(-15 / (14pi), 0.0) - kernel_one * dw_gamma) / coefficient - gradient_y = (SVector(0.0, -15 / (14pi)) - kernel_one * dw_gamma) / coefficient - near_zero_configurations = (configurations[1:5]..., configurations[9]) - fluid_corrections = (GradientCorrection(), BlendedGradientCorrection(0.5), - nothing) - boundary_corrections = (KernelCorrection(), MixedKernelGradientCorrection()) + # Cover both RHS implementations, both zero-distance gradient corrections, + # and the paths that suppress the pressure force. + near_zero_configurations = (("WCSPH", GradientCorrection()), + ("EDAC", BlendedGradientCorrection(0.5)), + ("WCSPH", nothing), + ("EDAC with average pressure reduction", + GradientCorrection())) distances = (0.0, TrixiParticles.almostzero(smoothing_length) / 2) for structure_type in structure_types, config in near_zero_configurations, - correction in fluid_corrections, - boundary_correction in boundary_corrections, distance in distances - scheme, viscosity = config + scheme, correction = config fluid_coordinates = reshape([1.5 + distance, 0.0], 2, 1) - fluid_system = create_fluid_system(scheme, (1.0, -0.5), 1005.0, viscosity; + fluid_system = create_fluid_system(scheme, (1.0, -0.5), 1005.0, nothing; correction, coordinates=fluid_coordinates) - structure_system = create_structure_system(structure_type, viscosity; - boundary_correction) - auxiliary_system = WeaklyCompressibleSPHSystem(auxiliary_ic; - smoothing_kernel, - smoothing_length, - state_equation, - density_calculator=ContinuityDensity()) + structure_system = create_structure_system(structure_type, nothing; + boundary_correction=KernelCorrection()) fluid, structure, arrays, semi, - _ = initialize(fluid_system, structure_system; auxiliary_system, - interaction_matrix=[true true true; true false true; - true true true]) + _ = initialize(fluid_system, structure_system) + + # Cache assembly is covered above; prescribe a nonzero boundary gradient + # here to isolate the coincident-pair pressure and safe fluid gradient. + structure.boundary_model.cache.kernel_correction_coefficient .= coefficient + structure.boundary_model.cache.dw_gamma .= dw_gamma (force_fluid, force_structure, - dv_structure) = pair_forces(fluid, structure, arrays, semi) - - gradient = boundary_gradient - if boundary_correction isa MixedKernelGradientCorrection - moment = -volume * - (boundary_gradient * transpose(SVector(-distance, 0.0)) + - gradient_x * transpose(SVector(1.0, 0.0)) + - gradient_y * transpose(SVector(0.0, 1.0))) - gradient = inv(moment) * gradient - end + _) = pair_forces(fluid, structure, arrays, semi) + pressure = scheme == "EDAC with average pressure reduction" ? 0.0 : 500.0 - expected = isnothing(correction) ? zero(gradient) : - pressure * volume^2 * gradient - @test all(isfinite, force_fluid) - @test all(isfinite, dv_structure) + expected = isnothing(correction) ? zero(boundary_gradient) : + pressure * volume^2 * boundary_gradient @test isapprox(force_fluid, expected, atol=sqrt(eps())) @test isapprox(force_structure, -expected, atol=sqrt(eps())) - @test isapprox(force_structure, -force_fluid, atol=sqrt(eps())) end end From 68e5646709f718ae0fa0b04cb43f2b61104a48cc Mon Sep 17 00:00:00 2001 From: Sven Berger Date: Tue, 10 Nov 2026 09:39:26 +0100 Subject: [PATCH 04/11] AI review --- NEWS.md | 8 +- docs/src/systems/boundary.md | 30 +++++ docs/src/systems/total_lagrangian_sph.md | 11 +- src/general/abstract_system.jl | 10 +- src/general/corrections.jl | 26 +--- src/general/smoothing_kernels.jl | 29 ++--- .../boundary/wall_boundary/dummy_particles.jl | 22 ++-- .../fluid/entropically_damped_sph/rhs.jl | 4 +- .../fluid/weakly_compressible_sph/rhs.jl | 4 +- src/schemes/structure/structure.jl | 40 +++--- test/general/corrections.jl | 90 ++++++++++++++ test/general/general.jl | 1 + test/schemes/fluid/rhs.jl | 115 +++++++++++++++--- 13 files changed, 300 insertions(+), 90 deletions(-) create mode 100644 test/general/corrections.jl diff --git a/NEWS.md b/NEWS.md index 64ddb6fe35..b0c5478398 100644 --- a/NEWS.md +++ b/NEWS.md @@ -29,8 +29,12 @@ used in the Julia ecosystem. Notable changes will be documented in this file for ### Important Bugfixes -- Fixed the structure-side kernel gradient in corrected FSI pressure to use the - boundary model's kernel rather than the TLSPH self-interaction kernel (#1342). +- Fixed kernel gradients at structure particles in fluid–structure pressure acceleration + to use the boundary model's kernel and corrections (#1342). +- Fixed correction-matrix assembly for `GradientCorrection` and + `BlendedGradientCorrection` (#1342). +- Fixed boundary-density evolution in `RigidBodySystem` with `ContinuityDensity` to + use the boundary model's corrected kernel gradient (#1342). - Fixed structure-fluid reaction forces with asymmetric corrected fluid kernel gradients by evaluating the fluid-first gradient directly. The continuity equation of `RigidBodySystem` boundaries with `ContinuityDensity` now uses the kernel gradient diff --git a/docs/src/systems/boundary.md b/docs/src/systems/boundary.md index 195f1ae857..035be7fcef 100644 --- a/docs/src/systems/boundary.md +++ b/docs/src/systems/boundary.md @@ -177,6 +177,36 @@ condition is applied. The viscosity model [`ArtificialViscosityMonaghan`](@ref) for [`BoundaryModelDummyParticles`](@ref) has not been verified yet. +### [Corrections](@id boundary_corrections) + +When the adjacent fluid system uses a [correction](@ref corrections) with a kernel gradient +that is not antisymmetric, i.e., [`KernelCorrection`](@ref), [`GradientCorrection`](@ref), +[`BlendedGradientCorrection`](@ref) or [`MixedKernelGradientCorrection`](@ref), +the pressure acceleration cannot use +``\nabla_{r_b} W(\Vert r_b - r_a \Vert, h) = -\nabla_{r_a} W(\Vert r_a - r_b \Vert, h)``. +Instead, each of the two particles uses its own corrected kernel gradient, e.g., +for [`ContinuityDensity`](@ref), +```math +\bm{f}_{ab}^{p} += -\frac{m_a m_b}{\rho_a \rho_b} +\left( p_a \tilde{\nabla}_{r_a} W_{ab} - p_b \tilde{\nabla}_{r_b} W_{ba} \right), +``` +where ``\tilde{\nabla}_{r_a} W_{ab}`` is the corrected kernel gradient of the fluid particle ``a`` +and ``\tilde{\nabla}_{r_b} W_{ba}`` is the corrected kernel gradient of the boundary particle ``b``. +The latter is computed with the `correction` of the boundary model, which should therefore +be the same as the correction of the fluid system. +With [`ContinuityDensity`](@ref), the density of the boundary particles also evolves with the +corrected kernel gradient of the boundary particles. + +For a [`TotalLagrangianSPHSystem`](@ref) or a [`RigidBodySystem`](@ref), the kernel gradient +at each boundary particle uses the smoothing kernel, smoothing length and correction of +the boundary model. Its smoothing kernel and smoothing length must match those of the +fluid systems. The smoothing kernel of the [`TotalLagrangianSPHSystem`](@ref) is used for +solid mechanics instead. +Correction assembly still uses the existing system neighborhood searches; complete +current-configuration neighborhoods for structure corrections are addressed separately +in [#1351](https://github.com/trixi-framework/TrixiParticles.jl/pull/1351). + ## Repulsive Particles Boundaries modeled as boundary particles which exert forces on the fluid particles ([Monaghan, Kajtar, 2009](@cite Monaghan2009)). diff --git a/docs/src/systems/total_lagrangian_sph.md b/docs/src/systems/total_lagrangian_sph.md index d0862a68f9..2f3eca83c6 100644 --- a/docs/src/systems/total_lagrangian_sph.md +++ b/docs/src/systems/total_lagrangian_sph.md @@ -57,6 +57,12 @@ are the Lamé coefficients, where ``E`` is the Young's modulus and ``\nu`` is th The term ``\bm{f}_a^{PF}`` is an optional penalty force. See e.g. [`PenaltyForceGanzenmueller`](@ref). +The smoothing kernel, the smoothing length and the correction matrix ``\bm{L}_{0a}`` above +are only used for the solid mechanics of the structure. +The interaction with fluids is determined by the `boundary_model`, which has its own +smoothing kernel, smoothing length and correction +(see [Boundary Models](@ref boundary_models)). + ```@autodocs Modules = [TrixiParticles] Pages = [joinpath("schemes", "structure", "total_lagrangian_sph", "system.jl")] @@ -130,11 +136,6 @@ In these cases, artificial viscosity is effective at stabilizing the fluid close structure, and we recommend using it in combination with penalty force to both prevent hourglass modes and stabilize the fluid close to the fluid-structure interface. -For dummy-particle fluid coupling with kernel or gradient correction, the structure-side -kernel gradient in the pressure acceleration is evaluated using the boundary model's -smoothing kernel, smoothing length, and correction data, independently of the TLSPH -self-interaction kernel. The boundary model and fluid must use the same smoothing -kernel and smoothing length. ## [Velocity Averaging](@id velocity_averaging) diff --git a/src/general/abstract_system.jl b/src/general/abstract_system.jl index 54ad6f9954..a61b983904 100644 --- a/src/general/abstract_system.jl +++ b/src/general/abstract_system.jl @@ -139,6 +139,9 @@ end return skip_zero_distance(system_correction(system)) end +# Whether `system` can skip neighbors of `neighbor_system` at the same position. +# By default, this only depends on the kernel gradient of `system`. +# This can be dispatched if the kernel gradient of `neighbor_system` is also evaluated. @inline skip_zero_distance(system, neighbor_system) = skip_zero_distance(system) # Robust/safe version of the function below. In performance-critical code, manually check @@ -157,8 +160,11 @@ end system_correction(system), system, particle) end -# A neighbor's correction may keep a coincident pair active even though this -# system's gradient vanishes. Use the safe gradient for those interactions. +# Kernel gradient of `system` for an interaction with `neighbor_system`. +# Particles at the same position are skipped if `skip_zero_distance(system, neighbor_system)`. +# If the correction of `neighbor_system` requires such pairs to be processed +# (e.g., because its kernel gradient does not vanish there), even though the kernel gradient +# of `system` vanishes, use the safe gradient, which handles particles at the same position. @inline function interaction_kernel_grad(system, neighbor_system, pos_diff, distance, particle) if skip_zero_distance(system) && !skip_zero_distance(system, neighbor_system) diff --git a/src/general/corrections.jl b/src/general/corrections.jl index ffb93b6d08..325b3c8bc0 100644 --- a/src/general/corrections.jl +++ b/src/general/corrections.jl @@ -127,14 +127,9 @@ function compute_correction_values!(system, ::ShepardKernelCorrection, u, v_ode, system.cache.kernel_correction_coefficient) end -function compute_correction_values!(system::AbstractBoundarySystem, - ::ShepardKernelCorrection, u, - v_ode, u_ode, semi) - return compute_shepard_coeff!(system, current_coordinates(u, system), v_ode, u_ode, - semi, - system.boundary_model.cache.kernel_correction_coefficient) -end - +# The correction values are sums over the neighbors of `system`. `kernel_data` provides the +# smoothing kernel, the smoothing length and the correction values of the particles of `system`, +# which are stored in the system itself or in its boundary model (for structures). function compute_shepard_coeff!(system, system_coords, v_ode, u_ode, semi, kernel_correction_coefficient, kernel_data=system) set_zero!(kernel_correction_coefficient) @@ -187,16 +182,6 @@ function compute_correction_values!(system::AbstractFluidSystem, system.cache.dw_gamma) end -function compute_correction_values!(system::AbstractBoundarySystem, - correction::Union{KernelCorrection, - MixedKernelGradientCorrection}, u, - v_ode, u_ode, semi) - compute_correction_values!(system, correction, current_coordinates(u, system), v_ode, - u_ode, semi, - system.boundary_model.cache.kernel_correction_coefficient, - system.boundary_model.cache.dw_gamma) -end - function compute_correction_values!(system, ::Union{KernelCorrection, MixedKernelGradientCorrection}, system_coords, @@ -354,8 +339,9 @@ function compute_gradient_correction_matrix!(corr_matrix::AbstractArray, system, kernel_data=system) set_zero!(corr_matrix) - # Mixed correction uses kernel-corrected gradients to assemble the matrix. - # The gradient-correction matrix itself is only available after assembly. + # The matrix is assembled from kernel gradients without gradient correction, since the + # gradient-correction matrix is not available before the assembly is finished. + # `MixedKernelGradientCorrection` assembles it from the kernel-corrected gradients. kernel_correction = correction isa MixedKernelGradientCorrection ? KernelCorrection() : nothing diff --git a/src/general/smoothing_kernels.jl b/src/general/smoothing_kernels.jl index 7f6c53ea4f..e236086441 100644 --- a/src/general/smoothing_kernels.jl +++ b/src/general/smoothing_kernels.jl @@ -65,9 +65,10 @@ end return false end -# Safe kernel-gradient evaluation with explicit kernel settings and correction data. +# Safe kernel gradient with an explicit kernel and smoothing length `h`. +# `kernel_data` stores the correction values of `particle` (a system or a boundary model). @inline function corrected_kernel_grad(kernel, pos_diff, distance, h, correction, - correction_data, particle) + kernel_data, particle) compact_support_ = compact_support(kernel, h) # See `almostzero` for an explanation of the zero distance check @@ -77,46 +78,46 @@ end end return corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, correction, - correction_data, particle) + kernel_data, particle) end @inline function corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, correction, - correction_data, particle) + kernel_data, particle) return kernel_grad_unsafe(kernel, pos_diff, distance, h) end @inline function corrected_kernel_grad_unsafe(kernel_, pos_diff, distance, h, - ::KernelCorrection, correction_data, particle) + ::KernelCorrection, kernel_data, particle) return (kernel_grad(kernel_, pos_diff, distance, h) .- - kernel(kernel_, distance, h) * dw_gamma(correction_data, particle)) / - kernel_correction_coefficient(correction_data, particle) + kernel(kernel_, distance, h) * dw_gamma(kernel_data, particle)) / + kernel_correction_coefficient(kernel_data, particle) end @inline function corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, corr::BlendedGradientCorrection, - correction_data, + kernel_data, particle) (; blending_factor) = corr grad = kernel_grad_unsafe(kernel, pos_diff, distance, h) return (1 - blending_factor) * grad + - blending_factor * correction_matrix(correction_data, particle) * grad + blending_factor * correction_matrix(kernel_data, particle) * grad end @inline function corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, - ::GradientCorrection, correction_data, + ::GradientCorrection, kernel_data, particle) grad = kernel_grad_unsafe(kernel, pos_diff, distance, h) - return correction_matrix(correction_data, particle) * grad + return correction_matrix(kernel_data, particle) * grad end @inline function corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, ::MixedKernelGradientCorrection, - correction_data, + kernel_data, particle) grad = corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, KernelCorrection(), - correction_data, particle) - return correction_matrix(correction_data, particle) * grad + kernel_data, particle) + return correction_matrix(kernel_data, particle) * grad end @doc raw""" diff --git a/src/schemes/boundary/wall_boundary/dummy_particles.jl b/src/schemes/boundary/wall_boundary/dummy_particles.jl index 2726d97094..fe9811b957 100644 --- a/src/schemes/boundary/wall_boundary/dummy_particles.jl +++ b/src/schemes/boundary/wall_boundary/dummy_particles.jl @@ -25,6 +25,9 @@ Boundary model for [`WallBoundarySystem`](@ref). - `state_equation`: This should be the same as for the adjacent fluid system (see e.g. [`StateEquationCole`](@ref)). - `correction`: Correction method of the adjacent fluid system (see [Corrections](@ref corrections)). + It determines the kernel gradient at the boundary particles when the fluid + uses a correction with an asymmetric kernel gradient + (see [Corrections](@ref boundary_corrections)). - `viscosity`: Slip (default) or no-slip condition. See description below for further information. - `clip_negative_pressure=false`: Clip negative boundary pressures to avoid sticking @@ -117,9 +120,11 @@ end return ndims(boundary_model.smoothing_kernel) end -# Evaluate the structure-side FSI pressure-acceleration gradient with the boundary -# model's kernel and correction data. The TLSPH self-interaction kernel and correction -# matrix are used separately for solid mechanics. +# Kernel gradient at a boundary particle with the kernel, smoothing length, correction and +# correction values of the boundary model. This is used for the interaction with fluids, +# e.g., in the asymmetric pressure acceleration of a fluid with a corrected kernel gradient +# and in the continuity equation of the boundary particles. +# A `TotalLagrangianSPHSystem` has its own kernel and correction matrix for solid mechanics. @inline function smoothing_kernel_grad(model::BoundaryModelDummyParticles, pos_diff, distance, particle) (; smoothing_kernel, smoothing_length, correction) = model @@ -127,13 +132,6 @@ end correction, model, particle) end -@inline function smoothing_kernel_grad_unsafe(model::BoundaryModelDummyParticles, pos_diff, - distance, particle) - (; smoothing_kernel, smoothing_length, correction) = model - return corrected_kernel_grad_unsafe(smoothing_kernel, pos_diff, distance, - smoothing_length, correction, model, particle) -end - @propagate_inbounds function kernel_correction_coefficient(model::BoundaryModelDummyParticles, particle) return model.cache.kernel_correction_coefficient[particle] @@ -442,8 +440,8 @@ end return boundary_model end -# Use the system for coordinates and neighbor traversal, and the boundary model for -# the hydrodynamic kernel and correction cache. +# Use the system for the coordinates and the neighbor search, and the boundary model for +# the kernel, the smoothing length and the correction values. function compute_correction_values!(boundary_model::BoundaryModelDummyParticles, system, correction, u, v_ode, u_ode, semi) return system diff --git a/src/schemes/fluid/entropically_damped_sph/rhs.jl b/src/schemes/fluid/entropically_damped_sph/rhs.jl index 492748bb0d..30c5d2315a 100644 --- a/src/schemes/fluid/entropically_damped_sph/rhs.jl +++ b/src/schemes/fluid/entropically_damped_sph/rhs.jl @@ -17,7 +17,9 @@ function interact!(dv, v_particle_system, u_particle_system, neighbor, pos_diff, distance - # Skip coincident pairs only when the active pressure gradients both vanish. + # Skip neighbors with (almost) the same position if the kernel gradients of this + # pair vanish, because computing them would divide by zero (see `almostzero`). + # This depends on the corrections of both systems (see `skip_zero_distance`). # Note that `return` only exits the closure, i.e., skips the current neighbor. skip_zero_distance(particle_system, neighbor_system) && distance < almostzero(h) && return diff --git a/src/schemes/fluid/weakly_compressible_sph/rhs.jl b/src/schemes/fluid/weakly_compressible_sph/rhs.jl index ca0c862e6c..279b234650 100644 --- a/src/schemes/fluid/weakly_compressible_sph/rhs.jl +++ b/src/schemes/fluid/weakly_compressible_sph/rhs.jl @@ -46,7 +46,9 @@ function interact!(dv, v_particle_system, u_particle_system, backend, particle; init) do particle, neighbor, pos_diff, distance - # Skip coincident pairs only when the active pressure gradients both vanish. + # Skip neighbors with (almost) the same position if the kernel gradients of this + # pair vanish, because computing them would divide by zero (see `almostzero`). + # This depends on the corrections of both systems (see `skip_zero_distance`). # Note that `return` only exits the closure, i.e., skips the current neighbor. if skip_zero_distance(particle_system, neighbor_system) && distance < almostzero(h) diff --git a/src/schemes/structure/structure.jl b/src/schemes/structure/structure.jl index 09577ddda3..cbdc694552 100644 --- a/src/schemes/structure/structure.jl +++ b/src/schemes/structure/structure.jl @@ -26,6 +26,10 @@ end GradientCorrection, BlendedGradientCorrection, MixedKernelGradientCorrection}) + # `W_a` is the kernel gradient at the fluid particle (kernel and correction of the + # fluid). `W_b` is the kernel gradient at the structure particle, which uses the + # kernel, smoothing length and correction of the boundary model of the structure + # and not the smoothing kernel of the `TotalLagrangianSPHSystem` for solid mechanics. W_b = smoothing_kernel_grad(neighbor_system.boundary_model, -pos_diff, distance, neighbor) @@ -35,16 +39,19 @@ end p_a, p_b, W_a, W_b) end +# The interaction of a fluid particle with a structure particle evaluates the kernel +# gradient of the fluid and the kernel gradient of the boundary model of the structure +# (in the asymmetric pressure acceleration and in the continuity equation of the boundary +# particles). The gradient of a corrected kernel vanishes at zero distance for no correction, +# `ShepardKernelCorrection`, `AkinciFreeSurfaceCorrection`, `GradientCorrection` and +# `BlendedGradientCorrection`, but not for `KernelCorrection` and +# `MixedKernelGradientCorrection` (see `skip_zero_distance(correction)`). +# Particles at the same position can therefore only be skipped if both gradients vanish. @inline function skip_zero_distance(particle_system::AbstractFluidSystem, neighbor_system::Union{TotalLagrangianSPHSystem{<:BoundaryModelDummyParticles}, RigidBodySystem{<:BoundaryModelDummyParticles}}) - correction = system_correction(particle_system) - # Gradient corrections use the asymmetric pressure operator. Its boundary-side - # gradient can remain nonzero at coincidence even when the fluid gradient is zero. - if correction isa Union{GradientCorrection, BlendedGradientCorrection} - return skip_zero_distance(neighbor_system.boundary_model.correction) - end - return skip_zero_distance(correction) + return skip_zero_distance(particle_system) && + skip_zero_distance(neighbor_system.boundary_model.correction) end # Both FSI directions must find the same particle pairs and use matching boundary and @@ -86,7 +93,10 @@ function interact_structure_fluid!(dv, v_particle_system, u_particle_system, foreach_point_neighbor(particle_system, neighbor_system, system_coords, neighbor_coords, semi; points=eachparticle) do particle, neighbor, pos_diff, distance - # Skip coincident pairs only when the active pressure gradients both vanish. + # Skip neighbors with (almost) the same position if the kernel gradients of this + # pair vanish, because computing them would divide by zero (see `almostzero`). + # This depends on the corrections of the fluid and the boundary model of the + # structure (see `skip_zero_distance`). # Note that `return` only exits the closure, i.e., skips the current neighbor. skip_zero_distance(neighbor_system, particle_system) && distance < almostzero(h) && return @@ -165,7 +175,8 @@ end (; boundary_model) = particle_system # As for a `WallBoundarySystem`, the density of the boundary particles evolves with - # the boundary model's kernel gradient and not with the corrected fluid gradient. + # the kernel gradient of the boundary model (including its correction) + # and not with the corrected kernel gradient of the fluid. grad_kernel = boundary_continuity_kernel_grad(system_correction(neighbor_system), boundary_model.correction, boundary_model, pos_diff, distance, @@ -179,9 +190,10 @@ end @inline function boundary_continuity_kernel_grad(fluid_correction, boundary_correction, boundary_model, pos_diff, distance, particle, grad_kernel_fluid) - # Use the boundary model's kernel and smoothing length, which may differ from - # the TLSPH kernel for solid mechanics. - # The safe version handles coincident particles, including kernel normalization. + # Use the kernel, smoothing length and correction of the boundary model. The kernel of + # a `TotalLagrangianSPHSystem` for solid mechanics may be different. + # Use the safe version, which handles particles at the same position. The gradient of + # `KernelCorrection` and `MixedKernelGradientCorrection` does not vanish there. return smoothing_kernel_grad(boundary_model, pos_diff, distance, particle) end @@ -193,8 +205,8 @@ end ShepardKernelCorrection}, boundary_model, pos_diff, distance, particle, grad_kernel_fluid) - # Neither correction changes the gradient, and the boundary model's kernel and - # smoothing length match the fluid's (see `check_smoothing_kernel_fsi`). + # Neither correction changes the gradient, and the kernel and smoothing length of the + # boundary model match the ones of the fluid (see `check_smoothing_kernel_fsi`). # Reversing the displacement only flips the sign of the uncorrected gradient. return -grad_kernel_fluid end diff --git a/test/general/corrections.jl b/test/general/corrections.jl new file mode 100644 index 0000000000..324e829f2a --- /dev/null +++ b/test/general/corrections.jl @@ -0,0 +1,90 @@ +@testset verbose=true "Corrections" begin + @testset "Gradient correction matrix of a fluid system" begin + kernel = SchoenbergCubicSplineKernel{2}() + state_equation = StateEquationCole(; sound_speed=10.0, reference_density=1000.0, + exponent=1.0) + + # Independent formulas of the cubic spline kernel for ``h = 1`` (compact support 2) + W(r) = 10 / (7pi) * (r < 1 ? 1 - 1.5r^2 + 0.75r^3 : r < 2 ? 0.25(2 - r)^3 : 0.0) + function grad_W(pos_diff) + r = norm(pos_diff) + (iszero(r) || r >= 2) && return zero(pos_diff) + derivative = 10 / (7pi) * (r < 1 ? -3r + 2.25r^2 : -0.75(2 - r)^2) + return derivative / r * pos_diff + end + + # Irregular neighborhoods, so that the correction matrices are far from the identity. + # The first and the last particle are outside of the support of each other. + coordinates = [0.0 0.8 1.7 0.3 1.1 2.1; 0.0 0.1 0.2 0.9 1.0 1.2] + mass = [1.0, 1.1, 0.9, 1.2, 1.0, 0.8] + density = [1000.0, 1010.0, 990.0, 1005.0, 995.0, 1000.0] + volumes = mass ./ density + positions = [SVector{2}(coordinates[:, i]) for i in axes(coordinates, 2)] + + # Gradient of the kernel correction of particle `a` at displacement `pos_diff` + # to particle `b` (`k` is the kernel correction coefficient and `gamma` the vector + # ``\sum_b V_b \nabla W_b / k``). + function reference(a) + pos_diffs = [positions[a] - x for x in positions] + kernel_values = W.(norm.(pos_diffs)) + gradients = grad_W.(pos_diffs) + k = dot(volumes, kernel_values) + gamma = sum(volumes .* gradients) / k + kernel_corrected = [(g - w * gamma) / k + for (g, w) in zip(gradients, kernel_values)] + return (; pos_diffs, kernel_values, gradients, k, gamma, kernel_corrected) + end + + # ``L = (-\sum_b V_b \nabla W_b \otimes r_{ab})^{-1}`` + function inverse_moment(volumes, gradients, pos_diffs) + return inv(-sum(v * g * transpose(d) + for (v, g, d) in zip(volumes, gradients, pos_diffs))) + end + + @testset "$(nameof(typeof(correction)))" for correction in + (GradientCorrection(), + BlendedGradientCorrection(0.3), + MixedKernelGradientCorrection()) + ic = InitialCondition(; coordinates, mass, density, particle_spacing=1.0) + system = WeaklyCompressibleSPHSystem(ic; smoothing_kernel=kernel, + smoothing_length=1.0, state_equation, + density_calculator=ContinuityDensity(), + correction) + semi = Semidiscretization(system; parallelization_backend=SerialBackend()) + ode = semidiscretize(semi, (0.0, 0.01)) + v_ode, u_ode = ode.u0.x + TrixiParticles.update_systems_and_nhs(v_ode, u_ode, semi, 0.0) + + for a in axes(coordinates, 2) + (; pos_diffs, gradients, kernel_corrected) = reference(a) + + # The matrix is assembled from the gradient without gradient correction, + # but with kernel correction for `MixedKernelGradientCorrection`. + matrix_gradients = correction isa MixedKernelGradientCorrection ? + kernel_corrected : gradients + L = inverse_moment(volumes, matrix_gradients, pos_diffs) + + # The matrix must not be the identity matrix + @test !isapprox(L, I(2), atol=0.1) + @test system.cache.correction_matrix[:, :, a] ≈ L + end + + # Corrected kernel gradient of particle 1 with respect to particle 2 + (; gradients, kernel_corrected) = reference(1) + L = system.cache.correction_matrix[:, :, 1] + grad_kernel = TrixiParticles.smoothing_kernel_grad(system, + positions[1] - + positions[2], + norm(positions[1] - + positions[2]), 1) + expected = if correction isa GradientCorrection + L * gradients[2] + elseif correction isa BlendedGradientCorrection + 0.7 * gradients[2] + 0.3 * L * gradients[2] + else + L * kernel_corrected[2] + end + @test grad_kernel ≈ expected + end + end +end diff --git a/test/general/general.jl b/test/general/general.jl index 64d129da3c..e6e96b248e 100644 --- a/test/general/general.jl +++ b/test/general/general.jl @@ -1,5 +1,6 @@ include("initial_condition.jl") include("smoothing_kernels.jl") +include("corrections.jl") include("density_calculator.jl") include("semidiscretization.jl") include("interpolation.jl") diff --git a/test/schemes/fluid/rhs.jl b/test/schemes/fluid/rhs.jl index 83c4b19dfe..fc2bb39e7a 100644 --- a/test/schemes/fluid/rhs.jl +++ b/test/schemes/fluid/rhs.jl @@ -445,7 +445,7 @@ (RigidBodySystem, false, smoothing_length)) correction_methods = (nothing, AkinciFreeSurfaceCorrection(fluid_density), KernelCorrection(), GradientCorrection(), - BlendedGradientCorrection(0.5), + BlendedGradientCorrection(0.3), MixedKernelGradientCorrection()) @testset "$(config[1]), Viscosity `$(nameof(typeof(config[2])))`" for config in @@ -626,7 +626,8 @@ # Without a fluid correction, the pair force only uses the fluid's kernel, # so a correction of the structure's boundary model must not change it. - # The continuity equation of the boundary particles must use the boundary model. + # The continuity equation of the boundary particles must use the boundary model + # including its correction. boundary_corrections = (ShepardKernelCorrection(), AkinciFreeSurfaceCorrection(fluid_density), KernelCorrection()) @@ -637,13 +638,24 @@ viscosity = ViscosityAdami(nu=0.1) velocity = (1.0, 0.5) density = 1005.0 + + # Isolate the fluid contribution; complete structure neighborhoods are + # addressed separately in #1351. + volume_fluid = fluid_density * particle_spacing^2 / density + kernel_value = 5 / (112pi) # `W(1.5)` + coefficient = volume_fluid * kernel_value + prescribed_coefficient = 2.0 + prescribed_dw_gamma = SVector(0.2, 0.0) + forces = map((nothing, boundary_correction)) do boundary_correction_ fluid_system = create_fluid_system("WCSPH", velocity, density, viscosity) structure_system = create_structure_system(RigidBodySystem, viscosity; boundary_density=ContinuityDensity(), boundary_correction=boundary_correction_) - if boundary_correction_ isa ShepardKernelCorrection - structure_system.boundary_model.cache.kernel_correction_coefficient .= NaN + # Make sure that the correction values are computed + cache = structure_system.boundary_model.cache + for field in (:kernel_correction_coefficient, :dw_gamma) + haskey(cache, field) && fill!(getproperty(cache, field), NaN) end (fluid, structure, arrays, semi, @@ -651,9 +663,13 @@ interaction_matrix=[true true; true false]) if boundary_correction_ isa ShepardKernelCorrection - expected_coefficient = fluid_density / density * 5 / (112pi) @test structure.boundary_model.cache.kernel_correction_coefficient ≈ - [expected_coefficient] + [coefficient] + elseif boundary_correction_ isa KernelCorrection + # Prescribe nonzero correction data to test continuity independently + # of the single-neighbor normalization, whose gradient vanishes. + structure.boundary_model.cache.kernel_correction_coefficient .= prescribed_coefficient + structure.boundary_model.cache.dw_gamma .= prescribed_dw_gamma end return (pair_forces(fluid, structure, arrays, semi)..., fluid, structure) @@ -665,10 +681,17 @@ @test isapprox(force_structure, -force_fluid, rtol=sqrt(eps()), atol=sqrt(eps())) - grad_kernel = TrixiParticles.smoothing_kernel_grad(structure.boundary_model, - SVector(1.5, 0.0), 1.5, 1) + # Gradient of the cubic spline kernel at the displacement `(1.5, 0)` + kernel_derivative = -0.75 * (2 - 1.5)^2 * 10 / (7pi) + grad_kernel_x = kernel_derivative + if boundary_correction isa KernelCorrection + # `(∇W - W γ) / c` using the prescribed correction data + grad_kernel_x = (kernel_derivative - kernel_value * prescribed_dw_gamma[1]) / + prescribed_coefficient + @test !isapprox(grad_kernel_x, kernel_derivative, rtol=0.1) + end expected_density_rate = -fluid_density / density * fluid.mass[1] * - velocity[1] * grad_kernel[1] + velocity[1] * grad_kernel_x @test dv_structure[end, 1] ≈ expected_density_rate end @@ -713,6 +736,8 @@ gradients = (SVector(-15 / (56pi), 0.0), SVector(0.0, -15 / (14pi)), SVector(9 / (56pi), 12 / (56pi))) volume = fluid_density / 1005.0 + # Only fluid neighbors contribute in this test; complete structure + # neighborhoods are addressed separately in #1351. coefficient = volume * sum(kernel_values) dw_gamma = volume * sum(gradients) / coefficient normalized_gradients = map(gradients, kernel_values) do gradient, value @@ -721,7 +746,7 @@ for structure_type in structure_types, correction in (KernelCorrection(), GradientCorrection(), - BlendedGradientCorrection(0.5), MixedKernelGradientCorrection()) + BlendedGradientCorrection(0.3), MixedKernelGradientCorrection()) kernel_correction = correction isa Union{KernelCorrection, MixedKernelGradientCorrection} @@ -738,8 +763,9 @@ smoothing_length, state_equation, density_calculator=ContinuityDensity(), correction) - # All three fluid neighbors are inside the boundary kernel's support - # and outside the TLSPH self-interaction kernel's support of 2*0.4. + # All three fluid neighbors are inside the compact support of the boundary + # model (2) and outside the compact support of the smoothing kernel of the + # `TotalLagrangianSPHSystem` (2 * 0.4). structure_system = create_structure_system(structure_type, nothing; boundary_correction=correction, structure_smoothing_kernel=WendlandC2Kernel{2}(), @@ -785,15 +811,22 @@ fluid_gradient = fluid_matrix * fluid_gradient boundary_gradient = boundary_matrix * boundary_gradient elseif correction isa BlendedGradientCorrection - fluid_gradient = (fluid_gradient + fluid_matrix * fluid_gradient) / - 2 - boundary_gradient = (boundary_gradient + - boundary_matrix * boundary_gradient) / 2 + lambda = correction.blending_factor + fluid_gradient = (1 - lambda) * fluid_gradient + + lambda * fluid_matrix * fluid_gradient + boundary_gradient = (1 - lambda) * boundary_gradient + + lambda * boundary_matrix * boundary_gradient end 500.0 * volume^2 * (boundary_gradient - fluid_gradient) end @test force_fluid ≈ first(expected_forces) @test force_structure ≈ -sum(expected_forces) + + # The kernel gradient of the boundary model vanishes outside of its compact + # support (2), which is larger than the one of the TLSPH kernel (0.8). + @test iszero(TrixiParticles.smoothing_kernel_grad(structure.boundary_model, + SVector(2.5, 0.0), 2.5, + 1)) end end @@ -803,10 +836,15 @@ coefficient = 2.0 dw_gamma = SVector(-0.5, -0.5) boundary_gradient = -kernel_zero * dw_gamma / coefficient - # Cover both RHS implementations, both zero-distance gradient corrections, - # and the paths that suppress the pressure force. + # Cover WCSPH and EDAC, no correction and the corrections whose kernel gradient + # vanishes at zero distance (`GradientCorrection` and `BlendedGradientCorrection`), + # and the average pressure reduction, which removes the pressure of a single + # fluid particle. + # Note that EDAC does not support corrections yet (this is not part of this + # test), so only the pairs at the same position, where the fluid gradient + # vanishes, are evaluated for EDAC with correction. near_zero_configurations = (("WCSPH", GradientCorrection()), - ("EDAC", BlendedGradientCorrection(0.5)), + ("EDAC", BlendedGradientCorrection(0.3)), ("WCSPH", nothing), ("EDAC with average pressure reduction", GradientCorrection())) @@ -840,6 +878,45 @@ end end + # The kernel gradient of `KernelCorrection` does not vanish for particles at the same + # position. The continuity equation of the boundary particles must therefore be + # continuous when a fluid particle approaches a boundary particle, even if the + # correction of the fluid alone would skip pairs at the same position. + @testset "Boundary continuity near coincident particles" begin + velocity = SVector(1.0, -0.5) + density = 1005.0 + fluid_mass = fluid_density * particle_spacing^2 + coefficient = 2.0 + dw_gamma = SVector(-0.5, -0.5) + # `(∇W(0) - W(0) γ) / c` + grad_kernel = -10 / (7pi) * dw_gamma / coefficient + # The density of the boundary particle is the initial density `fluid_density` + expected_density_rate = -fluid_density / density * fluid_mass * + dot(velocity, grad_kernel) + almostzero = TrixiParticles.almostzero(smoothing_length) + + for correction in (nothing, GradientCorrection()), + distance in (0.0, almostzero / 2, 10 * almostzero) + fluid_coordinates = reshape([1.5 + distance, 0.0], 2, 1) + fluid_system = create_fluid_system("WCSPH", velocity, density, nothing; + correction, + coordinates=fluid_coordinates) + structure_system = create_structure_system(RigidBodySystem, nothing; + boundary_density=ContinuityDensity(), + boundary_correction=KernelCorrection()) + fluid, structure, arrays, semi, + _ = initialize(fluid_system, + structure_system) + + # Prescribe a nonzero boundary gradient at zero distance + structure.boundary_model.cache.kernel_correction_coefficient .= coefficient + structure.boundary_model.cache.dw_gamma .= dw_gamma + _, _, dv_structure = pair_forces(fluid, structure, arrays, semi) + + @test isapprox(dv_structure[end, 1], expected_density_rate, rtol=1e-5) + end + end + # The extra terms of the shifting techniques in the momentum equation act on the # fluid, but must not be applied to the structure. shifting_techniques = (ParticleShiftingTechnique(), From 6065414443f8d6d65e3f808d35af4bc7472d33b5 Mon Sep 17 00:00:00 2001 From: Sven Berger Date: Tue, 10 Nov 2026 10:04:34 +0100 Subject: [PATCH 05/11] review --- docs/src/general/smoothing_kernels.md | 18 +++++----- docs/src/systems/boundary.md | 3 -- src/general/abstract_system.jl | 24 +------------ src/general/smoothing_kernels.jl | 27 +++++++------- src/preprocessing/particle_packing/rhs.jl | 2 +- src/schemes/boundary/open_boundary/rhs.jl | 2 +- src/schemes/boundary/wall_boundary/rhs.jl | 4 ++- .../fluid/entropically_damped_sph/rhs.jl | 23 ++++++++---- .../fluid/implicit_incompressible_sph/rhs.jl | 2 +- .../density_diffusion.jl | 6 ++-- .../fluid/weakly_compressible_sph/rhs.jl | 24 +++++++++---- src/schemes/structure/structure.jl | 35 +++++++------------ .../structure/total_lagrangian_sph/rhs.jl | 2 +- .../structure/total_lagrangian_sph/system.jl | 2 +- 14 files changed, 81 insertions(+), 93 deletions(-) diff --git a/docs/src/general/smoothing_kernels.md b/docs/src/general/smoothing_kernels.md index 436e9c2b3e..48878b8319 100644 --- a/docs/src/general/smoothing_kernels.md +++ b/docs/src/general/smoothing_kernels.md @@ -192,18 +192,20 @@ CairoMakie.save("smoothing_kernels.png", fig) been checked. The compact support is automatically checked when inside a `foreach_point_neighbor` or `foreach_neighbor` loop. - For the zero distance check, use - ```jldoctest kernels; output = false, setup = :(almostzero = 1e-12) - TrixiParticles.skip_zero_distance(system) && distance < almostzero + For corrections whose kernel gradient vanishes at zero distance, use + ```jldoctest kernels; output = false + correction = TrixiParticles.system_correction(system) + h = TrixiParticles.smoothing_length(system, particle) + !(correction isa Union{KernelCorrection, MixedKernelGradientCorrection}) && + distance < TrixiParticles.almostzero(h) # output false ``` - where `skip_zero_distance` returns `true` if the kernel gradient is zero at zero - distance, which is not the case for some corrected kernels. - Here, `almostzero` is a small threshold, which should be chosen as `sqrt(eps(h^2))`, - where `h` is the smoothing length, since `distance^2` is in the order of `h^2`. - Note that `sqrt(eps(h^2)) != eps(h)`. + The gradients of `KernelCorrection` and `MixedKernelGradientCorrection` can be nonzero + at zero distance. Use the safe `smoothing_kernel_grad` when evaluating these pairs. + Here, `almostzero(h)` is the threshold `sqrt(eps(typeof(h))) * h`, where `h` is the + smoothing length. ```@autodocs Modules = [TrixiParticles] diff --git a/docs/src/systems/boundary.md b/docs/src/systems/boundary.md index 035be7fcef..5a8b5dd920 100644 --- a/docs/src/systems/boundary.md +++ b/docs/src/systems/boundary.md @@ -203,9 +203,6 @@ at each boundary particle uses the smoothing kernel, smoothing length and correc the boundary model. Its smoothing kernel and smoothing length must match those of the fluid systems. The smoothing kernel of the [`TotalLagrangianSPHSystem`](@ref) is used for solid mechanics instead. -Correction assembly still uses the existing system neighborhood searches; complete -current-configuration neighborhoods for structure corrections are addressed separately -in [#1351](https://github.com/trixi-framework/TrixiParticles.jl/pull/1351). ## Repulsive Particles diff --git a/src/general/abstract_system.jl b/src/general/abstract_system.jl index a61b983904..532b0cad10 100644 --- a/src/general/abstract_system.jl +++ b/src/general/abstract_system.jl @@ -135,17 +135,8 @@ end return kernel_unsafe(smoothing_kernel, distance, smoothing_length(system, particle)) end -@inline function skip_zero_distance(system::AbstractSystem) - return skip_zero_distance(system_correction(system)) -end - -# Whether `system` can skip neighbors of `neighbor_system` at the same position. -# By default, this only depends on the kernel gradient of `system`. -# This can be dispatched if the kernel gradient of `neighbor_system` is also evaluated. -@inline skip_zero_distance(system, neighbor_system) = skip_zero_distance(system) - # Robust/safe version of the function below. In performance-critical code, manually check -# the kernel support, call `skip_zero_distance` and then `smoothing_kernel_grad_unsafe`. +# the kernel support and zero distance before calling `smoothing_kernel_grad_unsafe`. @inline function smoothing_kernel_grad(system, pos_diff, distance, particle) return corrected_kernel_grad(system_smoothing_kernel(system), pos_diff, distance, smoothing_length(system, particle), @@ -160,19 +151,6 @@ end system_correction(system), system, particle) end -# Kernel gradient of `system` for an interaction with `neighbor_system`. -# Particles at the same position are skipped if `skip_zero_distance(system, neighbor_system)`. -# If the correction of `neighbor_system` requires such pairs to be processed -# (e.g., because its kernel gradient does not vanish there), even though the kernel gradient -# of `system` vanishes, use the safe gradient, which handles particles at the same position. -@inline function interaction_kernel_grad(system, neighbor_system, pos_diff, distance, - particle) - if skip_zero_distance(system) && !skip_zero_distance(system, neighbor_system) - return smoothing_kernel_grad(system, pos_diff, distance, particle) - end - return smoothing_kernel_grad_unsafe(system, pos_diff, distance, particle) -end - # System updates do nothing by default, but can be dispatched if needed function update_positions!(system, v, u, v_ode, u_ode, semi, t) return system diff --git a/src/general/smoothing_kernels.jl b/src/general/smoothing_kernels.jl index e236086441..9f9df0f03d 100644 --- a/src/general/smoothing_kernels.jl +++ b/src/general/smoothing_kernels.jl @@ -55,25 +55,14 @@ end return zero(r) end -@inline function skip_zero_distance(correction) - return true -end - -@inline function skip_zero_distance(::Union{KernelCorrection, - MixedKernelGradientCorrection}) - # For these corrections, the kernel gradient between a particle and itself is not zero - return false -end - # Safe kernel gradient with an explicit kernel and smoothing length `h`. # `kernel_data` stores the correction values of `particle` (a system or a boundary model). @inline function corrected_kernel_grad(kernel, pos_diff, distance, h, correction, kernel_data, particle) compact_support_ = compact_support(kernel, h) - # See `almostzero` for an explanation of the zero distance check - if distance >= compact_support_ || - (skip_zero_distance(correction) && distance < almostzero(h)) + # See `almostzero` for an explanation of the zero distance check. + if distance >= compact_support_ || distance < almostzero(h) return zero(pos_diff) end @@ -81,6 +70,18 @@ end kernel_data, particle) end +@inline function corrected_kernel_grad(kernel, pos_diff, distance, h, + correction::Union{KernelCorrection, + MixedKernelGradientCorrection}, + kernel_data, particle) + distance >= compact_support(kernel, h) && return zero(pos_diff) + + # At zero distance, ∇W vanishes but the normalization term -W(0) * dw_gamma / c + # need not. Mixed correction also multiplies this term by its correction matrix. + return corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, correction, + kernel_data, particle) +end + @inline function corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, correction, kernel_data, particle) return kernel_grad_unsafe(kernel, pos_diff, distance, h) diff --git a/src/preprocessing/particle_packing/rhs.jl b/src/preprocessing/particle_packing/rhs.jl index 208f7e4dcb..3490a04324 100644 --- a/src/preprocessing/particle_packing/rhs.jl +++ b/src/preprocessing/particle_packing/rhs.jl @@ -13,7 +13,7 @@ function interact!(dv, v_particle_system, u_particle_system, # Skip neighbors with (almost) the same position because the kernel gradient # is zero, but computing it would divide by zero (see `almostzero`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - skip_zero_distance(system) && distance < almostzero(h) && return + distance < almostzero(h) && return # Now that we know that `distance` is not zero, we can safely call the unsafe # version of the kernel gradient to avoid redundant zero checks. diff --git a/src/schemes/boundary/open_boundary/rhs.jl b/src/schemes/boundary/open_boundary/rhs.jl index 1385023c3f..93131a97bd 100644 --- a/src/schemes/boundary/open_boundary/rhs.jl +++ b/src/schemes/boundary/open_boundary/rhs.jl @@ -22,7 +22,7 @@ function interact!(dv, v_particle_system, u_particle_system, # Skip neighbors with (almost) the same position because the kernel gradient # is zero, but computing it would divide by zero (see `almostzero`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - skip_zero_distance(particle_system) && distance < almostzero(h) && return + distance < almostzero(h) && return # Now that we know that `distance` is not zero, we can safely call the unsafe # version of the kernel gradient to avoid redundant zero checks. diff --git a/src/schemes/boundary/wall_boundary/rhs.jl b/src/schemes/boundary/wall_boundary/rhs.jl index 17e47c035d..fe55af2936 100644 --- a/src/schemes/boundary/wall_boundary/rhs.jl +++ b/src/schemes/boundary/wall_boundary/rhs.jl @@ -27,7 +27,9 @@ function interact!(dv, v_particle_system, u_particle_system, # Skip neighbors with (almost) the same position because the kernel gradient # is zero, but computing it would divide by zero (see `almostzero`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - skip_zero_distance(particle_system) && distance < almostzero(h) && return + !(boundary_model.correction isa Union{KernelCorrection, + MixedKernelGradientCorrection}) && + distance < almostzero(h) && return # Now that we know that `distance` is not zero, we can safely call the unsafe # version of the kernel gradient to avoid redundant zero checks. diff --git a/src/schemes/fluid/entropically_damped_sph/rhs.jl b/src/schemes/fluid/entropically_damped_sph/rhs.jl index 30c5d2315a..674db0d1ee 100644 --- a/src/schemes/fluid/entropically_damped_sph/rhs.jl +++ b/src/schemes/fluid/entropically_damped_sph/rhs.jl @@ -10,6 +10,19 @@ function interact!(dv, v_particle_system, u_particle_system, h = initial_smoothing_length(particle_system) + # KernelCorrection gives ∇W_c(0) = -W(0) * dw_gamma / c, which need not vanish. + # MixedKernelGradientCorrection multiplies this gradient by its correction matrix. + # Keep coincident pairs when either the fluid or the structure's boundary model + # uses these corrections, so their pressure or boundary-density terms are not lost. + keep_coincident = correction isa + Union{KernelCorrection, MixedKernelGradientCorrection} || + (neighbor_system isa + Union{TotalLagrangianSPHSystem{<:BoundaryModelDummyParticles}, + RigidBodySystem{<:BoundaryModelDummyParticles}} && + neighbor_system.boundary_model.correction isa + Union{KernelCorrection, + MixedKernelGradientCorrection}) + # Loop over all pairs of particles and neighbors within the kernel cutoff foreach_point_neighbor(particle_system, neighbor_system, system_coords, neighbor_coords, semi; @@ -17,15 +30,11 @@ function interact!(dv, v_particle_system, u_particle_system, neighbor, pos_diff, distance - # Skip neighbors with (almost) the same position if the kernel gradients of this - # pair vanish, because computing them would divide by zero (see `almostzero`). - # This depends on the corrections of both systems (see `skip_zero_distance`). + # Skip coincident pairs only when neither correction needs them. # Note that `return` only exits the closure, i.e., skips the current neighbor. - skip_zero_distance(particle_system, neighbor_system) && - distance < almostzero(h) && return + !keep_coincident && distance < almostzero(h) && return - grad_kernel = interaction_kernel_grad(particle_system, neighbor_system, - pos_diff, distance, particle) + grad_kernel = smoothing_kernel_grad(particle_system, pos_diff, distance, particle) # `foreach_point_neighbor` makes sure that `particle` and `neighbor` are # in bounds of the respective system. For performance reasons, we use `@inbounds` diff --git a/src/schemes/fluid/implicit_incompressible_sph/rhs.jl b/src/schemes/fluid/implicit_incompressible_sph/rhs.jl index 21feadd4c8..067ec57bb8 100644 --- a/src/schemes/fluid/implicit_incompressible_sph/rhs.jl +++ b/src/schemes/fluid/implicit_incompressible_sph/rhs.jl @@ -22,7 +22,7 @@ function interact!(dv, v_particle_system, u_particle_system, # Skip neighbors with (almost) the same position because the kernel gradient # is zero, but computing it would divide by zero (see `almostzero`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - skip_zero_distance(particle_system) && distance < almostzero(h) && return + distance < almostzero(h) && return # Now that we know that `distance` is not zero, we can safely call the unsafe # version of the kernel gradient to avoid redundant zero checks. diff --git a/src/schemes/fluid/weakly_compressible_sph/density_diffusion.jl b/src/schemes/fluid/weakly_compressible_sph/density_diffusion.jl index 281f6372f5..90d1580d31 100644 --- a/src/schemes/fluid/weakly_compressible_sph/density_diffusion.jl +++ b/src/schemes/fluid/weakly_compressible_sph/density_diffusion.jl @@ -217,9 +217,9 @@ end particle, neighbor, pos_diff, distance, m_b, rho_a, rho_b, grad_kernel) # Density diffusion terms are all zero for distance zero (see `almostzero`). - # If `skip_zero_distance` is `true`, we can assume that this function isn't called - # for distance zero because these neighbors have already been skipped. - if !skip_zero_distance(particle_system) && + # Other corrections already skip these neighbors in the interaction loop. + if system_correction(particle_system) isa Union{KernelCorrection, + MixedKernelGradientCorrection} && distance < almostzero(initial_smoothing_length(particle_system)) return drho_particle end diff --git a/src/schemes/fluid/weakly_compressible_sph/rhs.jl b/src/schemes/fluid/weakly_compressible_sph/rhs.jl index 279b234650..746dd30006 100644 --- a/src/schemes/fluid/weakly_compressible_sph/rhs.jl +++ b/src/schemes/fluid/weakly_compressible_sph/rhs.jl @@ -18,6 +18,19 @@ function interact!(dv, v_particle_system, u_particle_system, h = initial_smoothing_length(particle_system) + # KernelCorrection gives ∇W_c(0) = -W(0) * dw_gamma / c, which need not vanish. + # MixedKernelGradientCorrection multiplies this gradient by its correction matrix. + # Keep coincident pairs when either the fluid or the structure's boundary model + # uses these corrections, so their pressure or boundary-density terms are not lost. + keep_coincident = correction isa + Union{KernelCorrection, MixedKernelGradientCorrection} || + (neighbor_system isa + Union{TotalLagrangianSPHSystem{<:BoundaryModelDummyParticles}, + RigidBodySystem{<:BoundaryModelDummyParticles}} && + neighbor_system.boundary_model.correction isa + Union{KernelCorrection, + MixedKernelGradientCorrection}) + @threaded semi for particle in eachparticle # We are looping over the particles of `particle_system`, so it is guaranteed # that `particle` is in bounds of `particle_system`. @@ -46,17 +59,14 @@ function interact!(dv, v_particle_system, u_particle_system, backend, particle; init) do particle, neighbor, pos_diff, distance - # Skip neighbors with (almost) the same position if the kernel gradients of this - # pair vanish, because computing them would divide by zero (see `almostzero`). - # This depends on the corrections of both systems (see `skip_zero_distance`). + # Skip coincident pairs only when neither correction needs them. # Note that `return` only exits the closure, i.e., skips the current neighbor. - if skip_zero_distance(particle_system, neighbor_system) && - distance < almostzero(h) + if !keep_coincident && distance < almostzero(h) return init end - grad_kernel = interaction_kernel_grad(particle_system, neighbor_system, - pos_diff, distance, particle) + grad_kernel = smoothing_kernel_grad(particle_system, pos_diff, distance, + particle) # `foreach_neighbor` makes sure that `neighbor` is in bounds of `neighbor_system` m_b = @inbounds hydrodynamic_mass(neighbor_system, neighbor) diff --git a/src/schemes/structure/structure.jl b/src/schemes/structure/structure.jl index cbdc694552..78f81898e9 100644 --- a/src/schemes/structure/structure.jl +++ b/src/schemes/structure/structure.jl @@ -39,21 +39,6 @@ end p_a, p_b, W_a, W_b) end -# The interaction of a fluid particle with a structure particle evaluates the kernel -# gradient of the fluid and the kernel gradient of the boundary model of the structure -# (in the asymmetric pressure acceleration and in the continuity equation of the boundary -# particles). The gradient of a corrected kernel vanishes at zero distance for no correction, -# `ShepardKernelCorrection`, `AkinciFreeSurfaceCorrection`, `GradientCorrection` and -# `BlendedGradientCorrection`, but not for `KernelCorrection` and -# `MixedKernelGradientCorrection` (see `skip_zero_distance(correction)`). -# Particles at the same position can therefore only be skipped if both gradients vanish. -@inline function skip_zero_distance(particle_system::AbstractFluidSystem, - neighbor_system::Union{TotalLagrangianSPHSystem{<:BoundaryModelDummyParticles}, - RigidBodySystem{<:BoundaryModelDummyParticles}}) - return skip_zero_distance(particle_system) && - skip_zero_distance(neighbor_system.boundary_model.correction) -end - # Both FSI directions must find the same particle pairs and use matching boundary and # fluid kernels. The TLSPH kernel for solid mechanics may differ. # Note that this is only a restriction for `BoundaryModelDummyParticles`, since @@ -89,25 +74,29 @@ function interact_structure_fluid!(dv, v_particle_system, u_particle_system, h = initial_smoothing_length(neighbor_system) + # Use the same coincident pairs as in the fluid-to-structure interaction. + keep_coincident = system_correction(neighbor_system) isa Union{KernelCorrection, + MixedKernelGradientCorrection} || + (particle_system.boundary_model isa BoundaryModelDummyParticles && + particle_system.boundary_model.correction isa + Union{KernelCorrection, + MixedKernelGradientCorrection}) + # Loop over all pairs of particles and neighbors within the kernel cutoff. foreach_point_neighbor(particle_system, neighbor_system, system_coords, neighbor_coords, semi; points=eachparticle) do particle, neighbor, pos_diff, distance - # Skip neighbors with (almost) the same position if the kernel gradients of this - # pair vanish, because computing them would divide by zero (see `almostzero`). - # This depends on the corrections of the fluid and the boundary model of the - # structure (see `skip_zero_distance`). + # Skip coincident pairs only when neither correction needs them. # Note that `return` only exits the closure, i.e., skips the current neighbor. - skip_zero_distance(neighbor_system, particle_system) && - distance < almostzero(h) && return + !keep_coincident && distance < almostzero(h) && return # Evaluate the gradient exactly as in the fluid-structure interaction, i.e., # with the fluid particle's kernel and correction and the fluid-first displacement. # Note that we cannot just flip the sign of the structure-first gradient, since # corrections like `KernelCorrection` add a term that keeps its sign # when the displacement is reversed. - grad_kernel_fluid = interaction_kernel_grad(neighbor_system, particle_system, - -pos_diff, distance, neighbor) + grad_kernel_fluid = smoothing_kernel_grad(neighbor_system, -pos_diff, distance, + neighbor) m_b = hydrodynamic_mass(neighbor_system, neighbor) diff --git a/src/schemes/structure/total_lagrangian_sph/rhs.jl b/src/schemes/structure/total_lagrangian_sph/rhs.jl index 49f29db990..e736e0f019 100644 --- a/src/schemes/structure/total_lagrangian_sph/rhs.jl +++ b/src/schemes/structure/total_lagrangian_sph/rhs.jl @@ -47,7 +47,7 @@ end # Skip neighbors with (almost) the same position because the kernel gradient # is zero, but computing it would divide by zero (see `almostzero`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - if skip_zero_distance(system) && initial_distance < almostzero(h) + if initial_distance < almostzero(h) return zero(initial_pos_diff) end diff --git a/src/schemes/structure/total_lagrangian_sph/system.jl b/src/schemes/structure/total_lagrangian_sph/system.jl index 9a345c96ca..10d3c0088e 100644 --- a/src/schemes/structure/total_lagrangian_sph/system.jl +++ b/src/schemes/structure/total_lagrangian_sph/system.jl @@ -550,7 +550,7 @@ end # Skip neighbors with (almost) the same position because the kernel gradient # is zero, but computing it would divide by zero (see `almostzero`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - if skip_zero_distance(system) && initial_distance < almostzero(h) + if initial_distance < almostzero(h) return zero(L_a) end From f710373b4a0027f2212766e3abffd46c61a18db6 Mon Sep 17 00:00:00 2001 From: Sven Berger Date: Tue, 10 Nov 2026 12:37:56 +0100 Subject: [PATCH 06/11] revert --- src/general/abstract_system.jl | 24 ++++++++++++- src/general/smoothing_kernels.jl | 27 +++++++------- src/preprocessing/particle_packing/rhs.jl | 2 +- src/schemes/boundary/open_boundary/rhs.jl | 2 +- src/schemes/boundary/wall_boundary/rhs.jl | 4 +-- .../fluid/entropically_damped_sph/rhs.jl | 23 ++++-------- .../fluid/implicit_incompressible_sph/rhs.jl | 2 +- .../density_diffusion.jl | 6 ++-- .../fluid/weakly_compressible_sph/rhs.jl | 24 ++++--------- src/schemes/structure/structure.jl | 35 ++++++++++++------- .../structure/total_lagrangian_sph/rhs.jl | 2 +- .../structure/total_lagrangian_sph/system.jl | 2 +- 12 files changed, 82 insertions(+), 71 deletions(-) diff --git a/src/general/abstract_system.jl b/src/general/abstract_system.jl index 532b0cad10..a61b983904 100644 --- a/src/general/abstract_system.jl +++ b/src/general/abstract_system.jl @@ -135,8 +135,17 @@ end return kernel_unsafe(smoothing_kernel, distance, smoothing_length(system, particle)) end +@inline function skip_zero_distance(system::AbstractSystem) + return skip_zero_distance(system_correction(system)) +end + +# Whether `system` can skip neighbors of `neighbor_system` at the same position. +# By default, this only depends on the kernel gradient of `system`. +# This can be dispatched if the kernel gradient of `neighbor_system` is also evaluated. +@inline skip_zero_distance(system, neighbor_system) = skip_zero_distance(system) + # Robust/safe version of the function below. In performance-critical code, manually check -# the kernel support and zero distance before calling `smoothing_kernel_grad_unsafe`. +# the kernel support, call `skip_zero_distance` and then `smoothing_kernel_grad_unsafe`. @inline function smoothing_kernel_grad(system, pos_diff, distance, particle) return corrected_kernel_grad(system_smoothing_kernel(system), pos_diff, distance, smoothing_length(system, particle), @@ -151,6 +160,19 @@ end system_correction(system), system, particle) end +# Kernel gradient of `system` for an interaction with `neighbor_system`. +# Particles at the same position are skipped if `skip_zero_distance(system, neighbor_system)`. +# If the correction of `neighbor_system` requires such pairs to be processed +# (e.g., because its kernel gradient does not vanish there), even though the kernel gradient +# of `system` vanishes, use the safe gradient, which handles particles at the same position. +@inline function interaction_kernel_grad(system, neighbor_system, pos_diff, distance, + particle) + if skip_zero_distance(system) && !skip_zero_distance(system, neighbor_system) + return smoothing_kernel_grad(system, pos_diff, distance, particle) + end + return smoothing_kernel_grad_unsafe(system, pos_diff, distance, particle) +end + # System updates do nothing by default, but can be dispatched if needed function update_positions!(system, v, u, v_ode, u_ode, semi, t) return system diff --git a/src/general/smoothing_kernels.jl b/src/general/smoothing_kernels.jl index 9f9df0f03d..e236086441 100644 --- a/src/general/smoothing_kernels.jl +++ b/src/general/smoothing_kernels.jl @@ -55,14 +55,25 @@ end return zero(r) end +@inline function skip_zero_distance(correction) + return true +end + +@inline function skip_zero_distance(::Union{KernelCorrection, + MixedKernelGradientCorrection}) + # For these corrections, the kernel gradient between a particle and itself is not zero + return false +end + # Safe kernel gradient with an explicit kernel and smoothing length `h`. # `kernel_data` stores the correction values of `particle` (a system or a boundary model). @inline function corrected_kernel_grad(kernel, pos_diff, distance, h, correction, kernel_data, particle) compact_support_ = compact_support(kernel, h) - # See `almostzero` for an explanation of the zero distance check. - if distance >= compact_support_ || distance < almostzero(h) + # See `almostzero` for an explanation of the zero distance check + if distance >= compact_support_ || + (skip_zero_distance(correction) && distance < almostzero(h)) return zero(pos_diff) end @@ -70,18 +81,6 @@ end kernel_data, particle) end -@inline function corrected_kernel_grad(kernel, pos_diff, distance, h, - correction::Union{KernelCorrection, - MixedKernelGradientCorrection}, - kernel_data, particle) - distance >= compact_support(kernel, h) && return zero(pos_diff) - - # At zero distance, ∇W vanishes but the normalization term -W(0) * dw_gamma / c - # need not. Mixed correction also multiplies this term by its correction matrix. - return corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, correction, - kernel_data, particle) -end - @inline function corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, correction, kernel_data, particle) return kernel_grad_unsafe(kernel, pos_diff, distance, h) diff --git a/src/preprocessing/particle_packing/rhs.jl b/src/preprocessing/particle_packing/rhs.jl index 3490a04324..208f7e4dcb 100644 --- a/src/preprocessing/particle_packing/rhs.jl +++ b/src/preprocessing/particle_packing/rhs.jl @@ -13,7 +13,7 @@ function interact!(dv, v_particle_system, u_particle_system, # Skip neighbors with (almost) the same position because the kernel gradient # is zero, but computing it would divide by zero (see `almostzero`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - distance < almostzero(h) && return + skip_zero_distance(system) && distance < almostzero(h) && return # Now that we know that `distance` is not zero, we can safely call the unsafe # version of the kernel gradient to avoid redundant zero checks. diff --git a/src/schemes/boundary/open_boundary/rhs.jl b/src/schemes/boundary/open_boundary/rhs.jl index 93131a97bd..1385023c3f 100644 --- a/src/schemes/boundary/open_boundary/rhs.jl +++ b/src/schemes/boundary/open_boundary/rhs.jl @@ -22,7 +22,7 @@ function interact!(dv, v_particle_system, u_particle_system, # Skip neighbors with (almost) the same position because the kernel gradient # is zero, but computing it would divide by zero (see `almostzero`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - distance < almostzero(h) && return + skip_zero_distance(particle_system) && distance < almostzero(h) && return # Now that we know that `distance` is not zero, we can safely call the unsafe # version of the kernel gradient to avoid redundant zero checks. diff --git a/src/schemes/boundary/wall_boundary/rhs.jl b/src/schemes/boundary/wall_boundary/rhs.jl index fe55af2936..17e47c035d 100644 --- a/src/schemes/boundary/wall_boundary/rhs.jl +++ b/src/schemes/boundary/wall_boundary/rhs.jl @@ -27,9 +27,7 @@ function interact!(dv, v_particle_system, u_particle_system, # Skip neighbors with (almost) the same position because the kernel gradient # is zero, but computing it would divide by zero (see `almostzero`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - !(boundary_model.correction isa Union{KernelCorrection, - MixedKernelGradientCorrection}) && - distance < almostzero(h) && return + skip_zero_distance(particle_system) && distance < almostzero(h) && return # Now that we know that `distance` is not zero, we can safely call the unsafe # version of the kernel gradient to avoid redundant zero checks. diff --git a/src/schemes/fluid/entropically_damped_sph/rhs.jl b/src/schemes/fluid/entropically_damped_sph/rhs.jl index 674db0d1ee..30c5d2315a 100644 --- a/src/schemes/fluid/entropically_damped_sph/rhs.jl +++ b/src/schemes/fluid/entropically_damped_sph/rhs.jl @@ -10,19 +10,6 @@ function interact!(dv, v_particle_system, u_particle_system, h = initial_smoothing_length(particle_system) - # KernelCorrection gives ∇W_c(0) = -W(0) * dw_gamma / c, which need not vanish. - # MixedKernelGradientCorrection multiplies this gradient by its correction matrix. - # Keep coincident pairs when either the fluid or the structure's boundary model - # uses these corrections, so their pressure or boundary-density terms are not lost. - keep_coincident = correction isa - Union{KernelCorrection, MixedKernelGradientCorrection} || - (neighbor_system isa - Union{TotalLagrangianSPHSystem{<:BoundaryModelDummyParticles}, - RigidBodySystem{<:BoundaryModelDummyParticles}} && - neighbor_system.boundary_model.correction isa - Union{KernelCorrection, - MixedKernelGradientCorrection}) - # Loop over all pairs of particles and neighbors within the kernel cutoff foreach_point_neighbor(particle_system, neighbor_system, system_coords, neighbor_coords, semi; @@ -30,11 +17,15 @@ function interact!(dv, v_particle_system, u_particle_system, neighbor, pos_diff, distance - # Skip coincident pairs only when neither correction needs them. + # Skip neighbors with (almost) the same position if the kernel gradients of this + # pair vanish, because computing them would divide by zero (see `almostzero`). + # This depends on the corrections of both systems (see `skip_zero_distance`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - !keep_coincident && distance < almostzero(h) && return + skip_zero_distance(particle_system, neighbor_system) && + distance < almostzero(h) && return - grad_kernel = smoothing_kernel_grad(particle_system, pos_diff, distance, particle) + grad_kernel = interaction_kernel_grad(particle_system, neighbor_system, + pos_diff, distance, particle) # `foreach_point_neighbor` makes sure that `particle` and `neighbor` are # in bounds of the respective system. For performance reasons, we use `@inbounds` diff --git a/src/schemes/fluid/implicit_incompressible_sph/rhs.jl b/src/schemes/fluid/implicit_incompressible_sph/rhs.jl index 067ec57bb8..21feadd4c8 100644 --- a/src/schemes/fluid/implicit_incompressible_sph/rhs.jl +++ b/src/schemes/fluid/implicit_incompressible_sph/rhs.jl @@ -22,7 +22,7 @@ function interact!(dv, v_particle_system, u_particle_system, # Skip neighbors with (almost) the same position because the kernel gradient # is zero, but computing it would divide by zero (see `almostzero`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - distance < almostzero(h) && return + skip_zero_distance(particle_system) && distance < almostzero(h) && return # Now that we know that `distance` is not zero, we can safely call the unsafe # version of the kernel gradient to avoid redundant zero checks. diff --git a/src/schemes/fluid/weakly_compressible_sph/density_diffusion.jl b/src/schemes/fluid/weakly_compressible_sph/density_diffusion.jl index 90d1580d31..281f6372f5 100644 --- a/src/schemes/fluid/weakly_compressible_sph/density_diffusion.jl +++ b/src/schemes/fluid/weakly_compressible_sph/density_diffusion.jl @@ -217,9 +217,9 @@ end particle, neighbor, pos_diff, distance, m_b, rho_a, rho_b, grad_kernel) # Density diffusion terms are all zero for distance zero (see `almostzero`). - # Other corrections already skip these neighbors in the interaction loop. - if system_correction(particle_system) isa Union{KernelCorrection, - MixedKernelGradientCorrection} && + # If `skip_zero_distance` is `true`, we can assume that this function isn't called + # for distance zero because these neighbors have already been skipped. + if !skip_zero_distance(particle_system) && distance < almostzero(initial_smoothing_length(particle_system)) return drho_particle end diff --git a/src/schemes/fluid/weakly_compressible_sph/rhs.jl b/src/schemes/fluid/weakly_compressible_sph/rhs.jl index 746dd30006..279b234650 100644 --- a/src/schemes/fluid/weakly_compressible_sph/rhs.jl +++ b/src/schemes/fluid/weakly_compressible_sph/rhs.jl @@ -18,19 +18,6 @@ function interact!(dv, v_particle_system, u_particle_system, h = initial_smoothing_length(particle_system) - # KernelCorrection gives ∇W_c(0) = -W(0) * dw_gamma / c, which need not vanish. - # MixedKernelGradientCorrection multiplies this gradient by its correction matrix. - # Keep coincident pairs when either the fluid or the structure's boundary model - # uses these corrections, so their pressure or boundary-density terms are not lost. - keep_coincident = correction isa - Union{KernelCorrection, MixedKernelGradientCorrection} || - (neighbor_system isa - Union{TotalLagrangianSPHSystem{<:BoundaryModelDummyParticles}, - RigidBodySystem{<:BoundaryModelDummyParticles}} && - neighbor_system.boundary_model.correction isa - Union{KernelCorrection, - MixedKernelGradientCorrection}) - @threaded semi for particle in eachparticle # We are looping over the particles of `particle_system`, so it is guaranteed # that `particle` is in bounds of `particle_system`. @@ -59,14 +46,17 @@ function interact!(dv, v_particle_system, u_particle_system, backend, particle; init) do particle, neighbor, pos_diff, distance - # Skip coincident pairs only when neither correction needs them. + # Skip neighbors with (almost) the same position if the kernel gradients of this + # pair vanish, because computing them would divide by zero (see `almostzero`). + # This depends on the corrections of both systems (see `skip_zero_distance`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - if !keep_coincident && distance < almostzero(h) + if skip_zero_distance(particle_system, neighbor_system) && + distance < almostzero(h) return init end - grad_kernel = smoothing_kernel_grad(particle_system, pos_diff, distance, - particle) + grad_kernel = interaction_kernel_grad(particle_system, neighbor_system, + pos_diff, distance, particle) # `foreach_neighbor` makes sure that `neighbor` is in bounds of `neighbor_system` m_b = @inbounds hydrodynamic_mass(neighbor_system, neighbor) diff --git a/src/schemes/structure/structure.jl b/src/schemes/structure/structure.jl index 78f81898e9..cbdc694552 100644 --- a/src/schemes/structure/structure.jl +++ b/src/schemes/structure/structure.jl @@ -39,6 +39,21 @@ end p_a, p_b, W_a, W_b) end +# The interaction of a fluid particle with a structure particle evaluates the kernel +# gradient of the fluid and the kernel gradient of the boundary model of the structure +# (in the asymmetric pressure acceleration and in the continuity equation of the boundary +# particles). The gradient of a corrected kernel vanishes at zero distance for no correction, +# `ShepardKernelCorrection`, `AkinciFreeSurfaceCorrection`, `GradientCorrection` and +# `BlendedGradientCorrection`, but not for `KernelCorrection` and +# `MixedKernelGradientCorrection` (see `skip_zero_distance(correction)`). +# Particles at the same position can therefore only be skipped if both gradients vanish. +@inline function skip_zero_distance(particle_system::AbstractFluidSystem, + neighbor_system::Union{TotalLagrangianSPHSystem{<:BoundaryModelDummyParticles}, + RigidBodySystem{<:BoundaryModelDummyParticles}}) + return skip_zero_distance(particle_system) && + skip_zero_distance(neighbor_system.boundary_model.correction) +end + # Both FSI directions must find the same particle pairs and use matching boundary and # fluid kernels. The TLSPH kernel for solid mechanics may differ. # Note that this is only a restriction for `BoundaryModelDummyParticles`, since @@ -74,29 +89,25 @@ function interact_structure_fluid!(dv, v_particle_system, u_particle_system, h = initial_smoothing_length(neighbor_system) - # Use the same coincident pairs as in the fluid-to-structure interaction. - keep_coincident = system_correction(neighbor_system) isa Union{KernelCorrection, - MixedKernelGradientCorrection} || - (particle_system.boundary_model isa BoundaryModelDummyParticles && - particle_system.boundary_model.correction isa - Union{KernelCorrection, - MixedKernelGradientCorrection}) - # Loop over all pairs of particles and neighbors within the kernel cutoff. foreach_point_neighbor(particle_system, neighbor_system, system_coords, neighbor_coords, semi; points=eachparticle) do particle, neighbor, pos_diff, distance - # Skip coincident pairs only when neither correction needs them. + # Skip neighbors with (almost) the same position if the kernel gradients of this + # pair vanish, because computing them would divide by zero (see `almostzero`). + # This depends on the corrections of the fluid and the boundary model of the + # structure (see `skip_zero_distance`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - !keep_coincident && distance < almostzero(h) && return + skip_zero_distance(neighbor_system, particle_system) && + distance < almostzero(h) && return # Evaluate the gradient exactly as in the fluid-structure interaction, i.e., # with the fluid particle's kernel and correction and the fluid-first displacement. # Note that we cannot just flip the sign of the structure-first gradient, since # corrections like `KernelCorrection` add a term that keeps its sign # when the displacement is reversed. - grad_kernel_fluid = smoothing_kernel_grad(neighbor_system, -pos_diff, distance, - neighbor) + grad_kernel_fluid = interaction_kernel_grad(neighbor_system, particle_system, + -pos_diff, distance, neighbor) m_b = hydrodynamic_mass(neighbor_system, neighbor) diff --git a/src/schemes/structure/total_lagrangian_sph/rhs.jl b/src/schemes/structure/total_lagrangian_sph/rhs.jl index e736e0f019..49f29db990 100644 --- a/src/schemes/structure/total_lagrangian_sph/rhs.jl +++ b/src/schemes/structure/total_lagrangian_sph/rhs.jl @@ -47,7 +47,7 @@ end # Skip neighbors with (almost) the same position because the kernel gradient # is zero, but computing it would divide by zero (see `almostzero`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - if initial_distance < almostzero(h) + if skip_zero_distance(system) && initial_distance < almostzero(h) return zero(initial_pos_diff) end diff --git a/src/schemes/structure/total_lagrangian_sph/system.jl b/src/schemes/structure/total_lagrangian_sph/system.jl index 10d3c0088e..9a345c96ca 100644 --- a/src/schemes/structure/total_lagrangian_sph/system.jl +++ b/src/schemes/structure/total_lagrangian_sph/system.jl @@ -550,7 +550,7 @@ end # Skip neighbors with (almost) the same position because the kernel gradient # is zero, but computing it would divide by zero (see `almostzero`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - if initial_distance < almostzero(h) + if skip_zero_distance(system) && initial_distance < almostzero(h) return zero(L_a) end From 7f4a90ca1caff64a2398a0bb2a2851d714c59bc8 Mon Sep 17 00:00:00 2001 From: Sven Berger Date: Wed, 11 Nov 2026 15:50:49 +0100 Subject: [PATCH 07/11] address review --- NEWS.md | 11 +- docs/src/general/smoothing_kernels.md | 9 +- docs/src/systems/boundary.md | 7 +- docs/src/systems/fluid.md | 6 + docs/src/systems/rigid_body.md | 5 + docs/src/systems/total_lagrangian_sph.md | 3 + src/general/abstract_system.jl | 18 -- src/general/corrections.jl | 18 +- src/general/smoothing_kernels.jl | 3 +- .../boundary/wall_boundary/dummy_particles.jl | 19 +- .../fluid/entropically_damped_sph/rhs.jl | 12 +- .../fluid/weakly_compressible_sph/rhs.jl | 12 +- .../fluid/weakly_compressible_sph/system.jl | 4 +- src/schemes/structure/rigid_body/system.jl | 4 +- src/schemes/structure/structure.jl | 45 ++--- .../structure/total_lagrangian_sph/system.jl | 4 +- test/general/corrections.jl | 5 +- test/general/semidiscretization.jl | 50 +++++ test/schemes/fluid/rhs.jl | 190 ++++++++++-------- 19 files changed, 245 insertions(+), 180 deletions(-) diff --git a/NEWS.md b/NEWS.md index b0c5478398..b95876eda0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -29,12 +29,11 @@ used in the Julia ecosystem. Notable changes will be documented in this file for ### Important Bugfixes -- Fixed kernel gradients at structure particles in fluid–structure pressure acceleration - to use the boundary model's kernel and corrections (#1342). -- Fixed correction-matrix assembly for `GradientCorrection` and - `BlendedGradientCorrection` (#1342). -- Fixed boundary-density evolution in `RigidBodySystem` with `ContinuityDensity` to - use the boundary model's corrected kernel gradient (#1342). +- Fixed fluid–structure pressure acceleration and rigid-body boundary-density evolution to + use the boundary model's kernel and corrections, and fixed correction-matrix assembly for + `GradientCorrection` and `BlendedGradientCorrection`. For TLSPH and rigid-body systems, + dummy-particle boundary corrections must now be `nothing` or match every fluid system's + correction, including its parameters (#1342). - Fixed structure-fluid reaction forces with asymmetric corrected fluid kernel gradients by evaluating the fluid-first gradient directly. The continuity equation of `RigidBodySystem` boundaries with `ContinuityDensity` now uses the kernel gradient diff --git a/docs/src/general/smoothing_kernels.md b/docs/src/general/smoothing_kernels.md index 48878b8319..d9b64157a8 100644 --- a/docs/src/general/smoothing_kernels.md +++ b/docs/src/general/smoothing_kernels.md @@ -192,18 +192,17 @@ CairoMakie.save("smoothing_kernels.png", fig) been checked. The compact support is automatically checked when inside a `foreach_point_neighbor` or `foreach_neighbor` loop. - For corrections whose kernel gradient vanishes at zero distance, use + For the zero distance check, use ```jldoctest kernels; output = false - correction = TrixiParticles.system_correction(system) h = TrixiParticles.smoothing_length(system, particle) - !(correction isa Union{KernelCorrection, MixedKernelGradientCorrection}) && + TrixiParticles.skip_zero_distance(system) && distance < TrixiParticles.almostzero(h) # output false ``` - The gradients of `KernelCorrection` and `MixedKernelGradientCorrection` can be nonzero - at zero distance. Use the safe `smoothing_kernel_grad` when evaluating these pairs. + where `skip_zero_distance` returns `true` if the kernel gradient is zero at zero + distance, which is not the case for some corrected kernels. Here, `almostzero(h)` is the threshold `sqrt(eps(typeof(h))) * h`, where `h` is the smoothing length. diff --git a/docs/src/systems/boundary.md b/docs/src/systems/boundary.md index 5a8b5dd920..4a9300c1b2 100644 --- a/docs/src/systems/boundary.md +++ b/docs/src/systems/boundary.md @@ -193,8 +193,8 @@ for [`ContinuityDensity`](@ref), ``` where ``\tilde{\nabla}_{r_a} W_{ab}`` is the corrected kernel gradient of the fluid particle ``a`` and ``\tilde{\nabla}_{r_b} W_{ba}`` is the corrected kernel gradient of the boundary particle ``b``. -The latter is computed with the `correction` of the boundary model, which should therefore -be the same as the correction of the fluid system. +The latter is computed with the `correction` of the boundary model. With `correction=nothing`, +the boundary gradient is uncorrected, even when the fluid uses a correction. With [`ContinuityDensity`](@ref), the density of the boundary particles also evolves with the corrected kernel gradient of the boundary particles. @@ -203,6 +203,9 @@ at each boundary particle uses the smoothing kernel, smoothing length and correc the boundary model. Its smoothing kernel and smoothing length must match those of the fluid systems. The smoothing kernel of the [`TotalLagrangianSPHSystem`](@ref) is used for solid mechanics instead. +The boundary model's `correction` must be `nothing` or match the correction of every fluid +system, including parameters such as the blending factor of [`BlendedGradientCorrection`](@ref). +These requirements are checked when constructing the [`Semidiscretization`](@ref). ## Repulsive Particles diff --git a/docs/src/systems/fluid.md b/docs/src/systems/fluid.md index 805c22b38e..e025c6382d 100644 --- a/docs/src/systems/fluid.md +++ b/docs/src/systems/fluid.md @@ -211,6 +211,12 @@ Pages = [joinpath("schemes", "fluid", "viscosity.jl")] ## [Corrections](@id corrections) +For fluid-structure interaction with a [`TotalLagrangianSPHSystem`](@ref) or a +[`RigidBodySystem`](@ref), the structure's [`BoundaryModelDummyParticles`](@ref) may use +`correction=nothing` or the same correction as every fluid system, including its parameters. +See [Boundary Corrections](@ref boundary_corrections) for how the fluid and boundary +gradients enter the pressure acceleration. + ```@autodocs Modules = [TrixiParticles] Pages = [joinpath("general", "corrections.jl")] diff --git a/docs/src/systems/rigid_body.md b/docs/src/systems/rigid_body.md index 27928499ce..6cd418f553 100644 --- a/docs/src/systems/rigid_body.md +++ b/docs/src/systems/rigid_body.md @@ -4,6 +4,11 @@ Rigid bodies in TrixiParticles.jl are represented by particles whose motion is e with rigid-body translation and rotation. This allows fluid-structure interaction while keeping the structure kinematics rigid. +Fluid-structure interaction is configured through `boundary_model`. When using +[`BoundaryModelDummyParticles`](@ref), its smoothing kernel and smoothing length must match +those of every fluid system. Its `correction` must be `nothing` or match the fluid correction, +including its parameters (see [Boundary Corrections](@ref boundary_corrections)). + ## API ```@autodocs diff --git a/docs/src/systems/total_lagrangian_sph.md b/docs/src/systems/total_lagrangian_sph.md index 2f3eca83c6..b5bb21f37d 100644 --- a/docs/src/systems/total_lagrangian_sph.md +++ b/docs/src/systems/total_lagrangian_sph.md @@ -62,6 +62,9 @@ are only used for the solid mechanics of the structure. The interaction with fluids is determined by the `boundary_model`, which has its own smoothing kernel, smoothing length and correction (see [Boundary Models](@ref boundary_models)). +When using [`BoundaryModelDummyParticles`](@ref), its smoothing kernel and smoothing length +must match those of every fluid system. Its `correction` must be `nothing` or match the fluid +correction, including its parameters (see [Boundary Corrections](@ref boundary_corrections)). ```@autodocs Modules = [TrixiParticles] diff --git a/src/general/abstract_system.jl b/src/general/abstract_system.jl index a61b983904..2c8a4647e0 100644 --- a/src/general/abstract_system.jl +++ b/src/general/abstract_system.jl @@ -139,11 +139,6 @@ end return skip_zero_distance(system_correction(system)) end -# Whether `system` can skip neighbors of `neighbor_system` at the same position. -# By default, this only depends on the kernel gradient of `system`. -# This can be dispatched if the kernel gradient of `neighbor_system` is also evaluated. -@inline skip_zero_distance(system, neighbor_system) = skip_zero_distance(system) - # Robust/safe version of the function below. In performance-critical code, manually check # the kernel support, call `skip_zero_distance` and then `smoothing_kernel_grad_unsafe`. @inline function smoothing_kernel_grad(system, pos_diff, distance, particle) @@ -160,19 +155,6 @@ end system_correction(system), system, particle) end -# Kernel gradient of `system` for an interaction with `neighbor_system`. -# Particles at the same position are skipped if `skip_zero_distance(system, neighbor_system)`. -# If the correction of `neighbor_system` requires such pairs to be processed -# (e.g., because its kernel gradient does not vanish there), even though the kernel gradient -# of `system` vanishes, use the safe gradient, which handles particles at the same position. -@inline function interaction_kernel_grad(system, neighbor_system, pos_diff, distance, - particle) - if skip_zero_distance(system) && !skip_zero_distance(system, neighbor_system) - return smoothing_kernel_grad(system, pos_diff, distance, particle) - end - return smoothing_kernel_grad_unsafe(system, pos_diff, distance, particle) -end - # System updates do nothing by default, but can be dispatched if needed function update_positions!(system, v, u, v_ode, u_ode, semi, t) return system diff --git a/src/general/corrections.jl b/src/general/corrections.jl index 325b3c8bc0..602cf2d34d 100644 --- a/src/general/corrections.jl +++ b/src/general/corrections.jl @@ -127,11 +127,11 @@ function compute_correction_values!(system, ::ShepardKernelCorrection, u, v_ode, system.cache.kernel_correction_coefficient) end -# The correction values are sums over the neighbors of `system`. `kernel_data` provides the -# smoothing kernel, the smoothing length and the correction values of the particles of `system`, -# which are stored in the system itself or in its boundary model (for structures). +# The correction-assembly helpers use `system` for the particles and the neighbor search. +# The `kernel_data` keyword supplies the kernel, smoothing length and any existing correction +# values needed for assembly. It defaults to `system`; boundary-model callers pass their model. function compute_shepard_coeff!(system, system_coords, v_ode, u_ode, semi, - kernel_correction_coefficient, kernel_data=system) + kernel_correction_coefficient; kernel_data=system) set_zero!(kernel_correction_coefficient) # Use enabled neighbor systems for the correction value. @@ -186,9 +186,10 @@ function compute_correction_values!(system, ::Union{KernelCorrection, MixedKernelGradientCorrection}, system_coords, v_ode, - u_ode, semi, kernel_correction_coefficient, dw_gamma, - smoothing_kernel=system_smoothing_kernel(system), + u_ode, semi, kernel_correction_coefficient, dw_gamma; kernel_data=system) + smoothing_kernel = system_smoothing_kernel(kernel_data) + set_zero!(kernel_correction_coefficient) set_zero!(dw_gamma) @@ -335,8 +336,9 @@ end function compute_gradient_correction_matrix!(corr_matrix::AbstractArray, system, coordinates, v_ode, u_ode, semi, - correction, smoothing_kernel, - kernel_data=system) + correction; kernel_data=system) + smoothing_kernel = system_smoothing_kernel(kernel_data) + set_zero!(corr_matrix) # The matrix is assembled from kernel gradients without gradient correction, since the diff --git a/src/general/smoothing_kernels.jl b/src/general/smoothing_kernels.jl index e236086441..bff121e2d2 100644 --- a/src/general/smoothing_kernels.jl +++ b/src/general/smoothing_kernels.jl @@ -113,8 +113,7 @@ end @inline function corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, ::MixedKernelGradientCorrection, - kernel_data, - particle) + kernel_data, particle) grad = corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, KernelCorrection(), kernel_data, particle) return correction_matrix(kernel_data, particle) * grad diff --git a/src/schemes/boundary/wall_boundary/dummy_particles.jl b/src/schemes/boundary/wall_boundary/dummy_particles.jl index fe9811b957..fbff6c815d 100644 --- a/src/schemes/boundary/wall_boundary/dummy_particles.jl +++ b/src/schemes/boundary/wall_boundary/dummy_particles.jl @@ -24,10 +24,14 @@ Boundary model for [`WallBoundarySystem`](@ref). # Keywords - `state_equation`: This should be the same as for the adjacent fluid system (see e.g. [`StateEquationCole`](@ref)). -- `correction`: Correction method of the adjacent fluid system (see [Corrections](@ref corrections)). +- `correction`: Correction method at the boundary particles (default: `nothing`, + see [Corrections](@ref corrections)). It determines the kernel gradient at the boundary particles when the fluid uses a correction with an asymmetric kernel gradient (see [Corrections](@ref boundary_corrections)). + For a [`TotalLagrangianSPHSystem`](@ref) or a [`RigidBodySystem`](@ref), + it must be `nothing` or match the correction of all fluid systems, + including its parameters. - `viscosity`: Slip (default) or no-slip condition. See description below for further information. - `clip_negative_pressure=false`: Clip negative boundary pressures to avoid sticking @@ -451,18 +455,19 @@ function compute_correction_values!(boundary_model::BoundaryModelDummyParticles, ::ShepardKernelCorrection, u, v_ode, u_ode, semi) (; cache) = boundary_model return compute_shepard_coeff!(system, current_coordinates(u, system), v_ode, u_ode, - semi, cache.kernel_correction_coefficient, boundary_model) + semi, cache.kernel_correction_coefficient; + kernel_data=boundary_model) end function compute_correction_values!(boundary_model::BoundaryModelDummyParticles, system, correction::Union{KernelCorrection, MixedKernelGradientCorrection}, u, v_ode, u_ode, semi) - (; cache, smoothing_kernel) = boundary_model + (; cache) = boundary_model return compute_correction_values!(system, correction, current_coordinates(u, system), v_ode, u_ode, semi, cache.kernel_correction_coefficient, - cache.dw_gamma, smoothing_kernel, boundary_model) + cache.dw_gamma; kernel_data=boundary_model) end function kernel_correct_density!(boundary_model, v, u, v_ode, u_ode, semi, @@ -485,14 +490,14 @@ function compute_gradient_correction_matrix!(corr::Union{GradientCorrection, MixedKernelGradientCorrection}, boundary_model, system, u, v_ode, u_ode, semi) - (; cache, correction, smoothing_kernel) = boundary_model + (; cache, correction) = boundary_model (; correction_matrix) = cache system_coords = current_coordinates(u, system) compute_gradient_correction_matrix!(correction_matrix, system, system_coords, - v_ode, u_ode, semi, correction, smoothing_kernel, - boundary_model) + v_ode, u_ode, semi, correction; + kernel_data=boundary_model) end function compute_density!(boundary_model, ::SummationDensity, system, v, u, v_ode, u_ode, diff --git a/src/schemes/fluid/entropically_damped_sph/rhs.jl b/src/schemes/fluid/entropically_damped_sph/rhs.jl index 30c5d2315a..d09c1a84f7 100644 --- a/src/schemes/fluid/entropically_damped_sph/rhs.jl +++ b/src/schemes/fluid/entropically_damped_sph/rhs.jl @@ -17,15 +17,13 @@ function interact!(dv, v_particle_system, u_particle_system, neighbor, pos_diff, distance - # Skip neighbors with (almost) the same position if the kernel gradients of this - # pair vanish, because computing them would divide by zero (see `almostzero`). - # This depends on the corrections of both systems (see `skip_zero_distance`). + # Skip neighbors with (almost) the same position if the kernel gradient is zero + # at zero distance, because computing it would divide by zero (see `almostzero`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - skip_zero_distance(particle_system, neighbor_system) && - distance < almostzero(h) && return + skip_zero_distance(particle_system) && distance < almostzero(h) && return - grad_kernel = interaction_kernel_grad(particle_system, neighbor_system, - pos_diff, distance, particle) + grad_kernel = smoothing_kernel_grad_unsafe(particle_system, pos_diff, distance, + particle) # `foreach_point_neighbor` makes sure that `particle` and `neighbor` are # in bounds of the respective system. For performance reasons, we use `@inbounds` diff --git a/src/schemes/fluid/weakly_compressible_sph/rhs.jl b/src/schemes/fluid/weakly_compressible_sph/rhs.jl index 279b234650..d474cb0091 100644 --- a/src/schemes/fluid/weakly_compressible_sph/rhs.jl +++ b/src/schemes/fluid/weakly_compressible_sph/rhs.jl @@ -46,17 +46,15 @@ function interact!(dv, v_particle_system, u_particle_system, backend, particle; init) do particle, neighbor, pos_diff, distance - # Skip neighbors with (almost) the same position if the kernel gradients of this - # pair vanish, because computing them would divide by zero (see `almostzero`). - # This depends on the corrections of both systems (see `skip_zero_distance`). + # Skip neighbors with (almost) the same position if the kernel gradient is zero + # at zero distance, because computing it would divide by zero (see `almostzero`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - if skip_zero_distance(particle_system, neighbor_system) && - distance < almostzero(h) + if skip_zero_distance(particle_system) && distance < almostzero(h) return init end - grad_kernel = interaction_kernel_grad(particle_system, neighbor_system, - pos_diff, distance, particle) + grad_kernel = smoothing_kernel_grad_unsafe(particle_system, pos_diff, distance, + particle) # `foreach_neighbor` makes sure that `neighbor` is in bounds of `neighbor_system` m_b = @inbounds hydrodynamic_mass(neighbor_system, neighbor) diff --git a/src/schemes/fluid/weakly_compressible_sph/system.jl b/src/schemes/fluid/weakly_compressible_sph/system.jl index eea0607d7d..67905baa0a 100644 --- a/src/schemes/fluid/weakly_compressible_sph/system.jl +++ b/src/schemes/fluid/weakly_compressible_sph/system.jl @@ -369,13 +369,13 @@ function compute_gradient_correction_matrix!(corr::Union{GradientCorrection, MixedKernelGradientCorrection}, system::WeaklyCompressibleSPHSystem, u, v_ode, u_ode, semi) - (; cache, correction, smoothing_kernel) = system + (; cache, correction) = system (; correction_matrix) = cache system_coords = current_coordinates(u, system) compute_gradient_correction_matrix!(correction_matrix, system, system_coords, - v_ode, u_ode, semi, correction, smoothing_kernel) + v_ode, u_ode, semi, correction) end function reinit_density!(vu_ode, semi) diff --git a/src/schemes/structure/rigid_body/system.jl b/src/schemes/structure/rigid_body/system.jl index 74222d7970..dd15c5b0c7 100644 --- a/src/schemes/structure/rigid_body/system.jl +++ b/src/schemes/structure/rigid_body/system.jl @@ -20,6 +20,8 @@ torque and applied consistently to all rigid particles. # Keywords - `boundary_model`: Boundary model for fluid-structure interaction (see [Boundary Models](@ref boundary_models)). + For [`BoundaryModelDummyParticles`](@ref), `correction` must be `nothing` + or match the correction of every fluid system, including its parameters. - `contact_model`: Optional rigid contact model. If specified, rigid-wall and rigid-rigid collisions are enabled. - `acceleration`: Global acceleration vector applied to all rigid particles. @@ -630,7 +632,7 @@ function check_configuration(system::RigidBodySystem, systems, nhs) "when simulating a fluid-structure interaction.")) end - check_smoothing_kernel_fsi(system, boundary_model, neighbor) + check_boundary_model_fsi(system, boundary_model, neighbor) if neighbor isa AbstractFluidSystem && neighbor.surface_normal_method isa ColorfieldSurfaceNormal diff --git a/src/schemes/structure/structure.jl b/src/schemes/structure/structure.jl index cbdc694552..0d518d446f 100644 --- a/src/schemes/structure/structure.jl +++ b/src/schemes/structure/structure.jl @@ -39,27 +39,14 @@ end p_a, p_b, W_a, W_b) end -# The interaction of a fluid particle with a structure particle evaluates the kernel -# gradient of the fluid and the kernel gradient of the boundary model of the structure -# (in the asymmetric pressure acceleration and in the continuity equation of the boundary -# particles). The gradient of a corrected kernel vanishes at zero distance for no correction, -# `ShepardKernelCorrection`, `AkinciFreeSurfaceCorrection`, `GradientCorrection` and -# `BlendedGradientCorrection`, but not for `KernelCorrection` and -# `MixedKernelGradientCorrection` (see `skip_zero_distance(correction)`). -# Particles at the same position can therefore only be skipped if both gradients vanish. -@inline function skip_zero_distance(particle_system::AbstractFluidSystem, - neighbor_system::Union{TotalLagrangianSPHSystem{<:BoundaryModelDummyParticles}, - RigidBodySystem{<:BoundaryModelDummyParticles}}) - return skip_zero_distance(particle_system) && - skip_zero_distance(neighbor_system.boundary_model.correction) -end - # Both FSI directions must find the same particle pairs and use matching boundary and # fluid kernels. The TLSPH kernel for solid mechanics may differ. +# The boundary correction must match the fluid correction or be `nothing`. Thus, if the +# fluid gradient vanishes at zero distance, the boundary gradient also vanishes there. # Note that this is only a restriction for `BoundaryModelDummyParticles`, since # `BoundaryModelMonaghanKajtar` always uses the compact support of the fluid. -function check_smoothing_kernel_fsi(system, boundary_model::BoundaryModelDummyParticles, - neighbor_system::AbstractFluidSystem) +function check_boundary_model_fsi(system, boundary_model::BoundaryModelDummyParticles, + neighbor_system::AbstractFluidSystem) smoothing_kernel_structure = boundary_model.smoothing_kernel smoothing_kernel_fluid = system_smoothing_kernel(neighbor_system) smoothing_length_structure = initial_smoothing_length(boundary_model) @@ -73,10 +60,17 @@ function check_smoothing_kernel_fsi(system, boundary_model::BoundaryModelDummyPa "as those of the fluid system `$(nameof(typeof(neighbor_system)))`.")) end + if !isnothing(boundary_model.correction) && + boundary_model.correction != system_correction(neighbor_system) + throw(ArgumentError("the correction of the boundary model of the " * + "`$(nameof(typeof(system)))` must be `nothing` or the same " * + "as that of the fluid system `$(nameof(typeof(neighbor_system)))`.")) + end + return system end -check_smoothing_kernel_fsi(system, boundary_model, neighbor_system) = system +check_boundary_model_fsi(system, boundary_model, neighbor_system) = system function interact_structure_fluid!(dv, v_particle_system, u_particle_system, v_neighbor_system, u_neighbor_system, @@ -93,12 +87,11 @@ function interact_structure_fluid!(dv, v_particle_system, u_particle_system, foreach_point_neighbor(particle_system, neighbor_system, system_coords, neighbor_coords, semi; points=eachparticle) do particle, neighbor, pos_diff, distance - # Skip neighbors with (almost) the same position if the kernel gradients of this - # pair vanish, because computing them would divide by zero (see `almostzero`). - # This depends on the corrections of the fluid and the boundary model of the - # structure (see `skip_zero_distance`). + # Skip neighbors with (almost) the same position if the fluid gradient vanishes, + # because computing it would divide by zero (see `almostzero`). The boundary + # gradient also vanishes in this case (see `check_boundary_model_fsi`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - skip_zero_distance(neighbor_system, particle_system) && + skip_zero_distance(neighbor_system) && distance < almostzero(h) && return # Evaluate the gradient exactly as in the fluid-structure interaction, i.e., @@ -106,8 +99,8 @@ function interact_structure_fluid!(dv, v_particle_system, u_particle_system, # Note that we cannot just flip the sign of the structure-first gradient, since # corrections like `KernelCorrection` add a term that keeps its sign # when the displacement is reversed. - grad_kernel_fluid = interaction_kernel_grad(neighbor_system, particle_system, - -pos_diff, distance, neighbor) + grad_kernel_fluid = smoothing_kernel_grad_unsafe(neighbor_system, -pos_diff, + distance, neighbor) m_b = hydrodynamic_mass(neighbor_system, neighbor) @@ -206,7 +199,7 @@ end boundary_model, pos_diff, distance, particle, grad_kernel_fluid) # Neither correction changes the gradient, and the kernel and smoothing length of the - # boundary model match the ones of the fluid (see `check_smoothing_kernel_fsi`). + # boundary model match the ones of the fluid (see `check_boundary_model_fsi`). # Reversing the displacement only flips the sign of the uncorrected gradient. return -grad_kernel_fluid end diff --git a/src/schemes/structure/total_lagrangian_sph/system.jl b/src/schemes/structure/total_lagrangian_sph/system.jl index 9a345c96ca..60a69690f5 100644 --- a/src/schemes/structure/total_lagrangian_sph/system.jl +++ b/src/schemes/structure/total_lagrangian_sph/system.jl @@ -33,6 +33,8 @@ See [Total Lagrangian SPH](@ref tlsph) for more details on the method. See [`PrescribedMotion`](@ref) for details. - `boundary_model`: Boundary model to compute the hydrodynamic density and pressure for fluid-structure interaction (see [Boundary Models](@ref boundary_models)). + For [`BoundaryModelDummyParticles`](@ref), `correction` must be `nothing` + or match the correction of every fluid system, including its parameters. - `penalty_force`: Penalty force to ensure regular particle position under large deformations (see [`PenaltyForceGanzenmueller`](@ref)). - `viscosity`: Artificial viscosity model to stabilize both the TLSPH and the FSI. @@ -870,6 +872,6 @@ function check_configuration(system::TotalLagrangianSPHSystem, systems, nhs) end foreach_system(systems) do neighbor - check_smoothing_kernel_fsi(system, boundary_model, neighbor) + check_boundary_model_fsi(system, boundary_model, neighbor) end end diff --git a/test/general/corrections.jl b/test/general/corrections.jl index 324e829f2a..eebeb0332d 100644 --- a/test/general/corrections.jl +++ b/test/general/corrections.jl @@ -66,7 +66,8 @@ # The matrix must not be the identity matrix @test !isapprox(L, I(2), atol=0.1) - @test system.cache.correction_matrix[:, :, a] ≈ L + @test isapprox(system.cache.correction_matrix[:, :, a], L, + rtol=1e-14, atol=eps()) end # Corrected kernel gradient of particle 1 with respect to particle 2 @@ -84,7 +85,7 @@ else L * kernel_corrected[2] end - @test grad_kernel ≈ expected + @test isapprox(grad_kernel, expected, rtol=1e-14, atol=eps()) end end end diff --git a/test/general/semidiscretization.jl b/test/general/semidiscretization.jl index fe829bf405..79dfe7babf 100644 --- a/test/general/semidiscretization.jl +++ b/test/general/semidiscretization.jl @@ -223,6 +223,56 @@ end end end + + # A boundary may omit the fluid correction, but a nontrivial boundary + # correction must match the fluid correction, including its parameters. + correction_configurations = ((KernelCorrection(), nothing, true), + (KernelCorrection(), KernelCorrection(), true), + (BlendedGradientCorrection(0.3), + BlendedGradientCorrection(0.3), true), + (nothing, KernelCorrection(), false), + (KernelCorrection(), GradientCorrection(), false), + (BlendedGradientCorrection(0.3), + BlendedGradientCorrection(0.7), false)) + @testset "Corrections, $structure_type" for structure_type in + (TotalLagrangianSPHSystem, + RigidBodySystem) + for (correction, boundary_correction, compatible) in + correction_configurations + + fluid = WeaklyCompressibleSPHSystem(ic; smoothing_kernel, + smoothing_length, state_equation, + density_calculator=ContinuityDensity(), + correction) + boundary_model = BoundaryModelDummyParticles(ic.density, ic.mass, + AdamiPressureExtrapolation(), + smoothing_kernel, + smoothing_length; + state_equation, + correction=boundary_correction) + structure = create_structure_system(structure_type, boundary_model) + error_str = "the correction of the boundary model of the " * + "`$structure_type` must be `nothing` or the same " * + "as that of the fluid system `WeaklyCompressibleSPHSystem`." + if compatible + @test_nowarn Semidiscretization(fluid, structure; + neighborhood_search=nothing) + else + @test_throws ArgumentError(error_str) Semidiscretization(fluid, + structure; + neighborhood_search=nothing) + end + + # A correction matching one fluid must also match any other fluid. + if correction isa KernelCorrection && + boundary_correction isa KernelCorrection + @test_throws ArgumentError(error_str) Semidiscretization(fluid, + fluid_systems[1], + structure; + neighborhood_search=nothing) + end + end + end end @testset verbose=true "WCSPH-Boundary Interaction" begin diff --git a/test/schemes/fluid/rhs.jl b/test/schemes/fluid/rhs.jl index fc2bb39e7a..da624a6b92 100644 --- a/test/schemes/fluid/rhs.jl +++ b/test/schemes/fluid/rhs.jl @@ -282,6 +282,7 @@ function create_fluid_system(scheme, velocity, density, viscosity; shifting_technique=nothing, correction=nothing, + density_calculator=ContinuityDensity(), coordinates=zeros(2, 1)) fluid = InitialCondition(; coordinates, velocity=reshape(collect(velocity), 2, 1), @@ -292,7 +293,7 @@ if scheme == "WCSPH" return WeaklyCompressibleSPHSystem(fluid; smoothing_kernel, smoothing_length, state_equation, - density_calculator=ContinuityDensity(), + density_calculator, viscosity, shifting_technique, correction) end @@ -300,19 +301,16 @@ # Note that the average pressure reduction is enabled by default # when using shifting. average_pressure_reduction = scheme == "EDAC with average pressure reduction" - pressure_acceleration = isnothing(correction) ? - TrixiParticles.inter_particle_averaged_pressure : - nothing return EntropicallyDampedSPHSystem(fluid; smoothing_kernel, smoothing_length, sound_speed, viscosity, shifting_technique, average_pressure_reduction, correction, - pressure_acceleration, - density_calculator=ContinuityDensity()) + density_calculator) end function create_structure_system(structure_type, viscosity; boundary_density=AdamiPressureExtrapolation(), boundary_correction=nothing, + boundary_initial_density=fluid_density, clamped=false, structure_smoothing_kernel=smoothing_kernel, structure_smoothing_length=smoothing_length) @@ -322,7 +320,7 @@ velocity=zeros(2, 1), mass=[2 * fluid_density * particle_spacing^2], density=[2 * fluid_density], particle_spacing) - boundary_model = BoundaryModelDummyParticles([fluid_density], + boundary_model = BoundaryModelDummyParticles([boundary_initial_density], [fluid_density * particle_spacing^2], boundary_density, @@ -624,7 +622,7 @@ end end - # Without a fluid correction, the pair force only uses the fluid's kernel, + # With zero boundary pressure, the pressure force only uses the fluid gradient, # so a correction of the structure's boundary model must not change it. # The continuity equation of the boundary particles must use the boundary model # including its correction. @@ -641,28 +639,33 @@ # Isolate the fluid contribution; complete structure neighborhoods are # addressed separately in #1351. - volume_fluid = fluid_density * particle_spacing^2 / density kernel_value = 5 / (112pi) # `W(1.5)` - coefficient = volume_fluid * kernel_value prescribed_coefficient = 2.0 prescribed_dw_gamma = SVector(0.2, 0.0) forces = map((nothing, boundary_correction)) do boundary_correction_ - fluid_system = create_fluid_system("WCSPH", velocity, density, viscosity) + density_calculator = boundary_correction isa ShepardKernelCorrection ? + SummationDensity() : ContinuityDensity() + fluid_system = create_fluid_system("WCSPH", velocity, density, viscosity; + correction=boundary_correction, + density_calculator) structure_system = create_structure_system(RigidBodySystem, viscosity; boundary_density=ContinuityDensity(), boundary_correction=boundary_correction_) # Make sure that the correction values are computed cache = structure_system.boundary_model.cache - for field in (:kernel_correction_coefficient, :dw_gamma) - haskey(cache, field) && fill!(getproperty(cache, field), NaN) - end + haskey(cache, :kernel_correction_coefficient) && + (cache.kernel_correction_coefficient .= NaN) + haskey(cache, :dw_gamma) && (cache.dw_gamma .= NaN) (fluid, structure, arrays, semi, _) = initialize(fluid_system, structure_system; interaction_matrix=[true true; true false]) if boundary_correction_ isa ShepardKernelCorrection + volume_fluid = fluid.mass[1] / + TrixiParticles.current_density(arrays[1], fluid, 1) + coefficient = volume_fluid * kernel_value @test structure.boundary_model.cache.kernel_correction_coefficient ≈ [coefficient] elseif boundary_correction_ isa KernelCorrection @@ -672,10 +675,13 @@ structure.boundary_model.cache.dw_gamma .= prescribed_dw_gamma end - return (pair_forces(fluid, structure, arrays, semi)..., fluid, structure) + current_fluid_density = TrixiParticles.current_density(arrays[1], fluid, 1) + return (pair_forces(fluid, structure, arrays, semi)..., fluid, structure, + current_fluid_density) end force_fluid_uncorrected = forces[1][1] - (force_fluid, force_structure, dv_structure, fluid, structure) = forces[2] + (force_fluid, force_structure, dv_structure, fluid, structure, + current_fluid_density) = forces[2] @test force_fluid == force_fluid_uncorrected @test isapprox(force_structure, -force_fluid, @@ -690,8 +696,11 @@ prescribed_coefficient @test !isapprox(grad_kernel_x, kernel_derivative, rtol=0.1) end - expected_density_rate = -fluid_density / density * fluid.mass[1] * - velocity[1] * grad_kernel_x + # With summation density, use the time derivative of the density summation. + density_factor = fluid.density_calculator isa SummationDensity ? 1.0 : + fluid_density / current_fluid_density + expected_density_rate = -density_factor * fluid.mass[1] * velocity[1] * + grad_kernel_x @test dv_structure[end, 1] ≈ expected_density_rate end @@ -729,6 +738,16 @@ @test drho ≈ expected_density_rate end + # Check that FSI pressure forces use the boundary model's kernel and correction. + # Place three fluid particles inside the boundary kernel's support but outside + # the TLSPH kernel's support. Using the TLSPH kernel would give zero boundary + # gradients for all three neighbors. + # + # The particles are not on a line, giving a non-identity correction matrix. + # Fill the boundary caches with NaN before initialization, then compare the + # fluid force and the structure's total reaction force with an independent + # calculation using cubic-spline values. Set the fluid correction data explicitly + # so this comparison checks the initialization and use of the boundary data. @testset "Boundary kernel in pressure acceleration with corrected gradients" begin coordinates = [0.0 1.5 2.4; 0.0 -1.0 1.2] displacements = (SVector(1.5, 0.0), SVector(0.0, 1.0), SVector(-0.9, -1.2)) @@ -830,90 +849,89 @@ end end - @testset "Coincident FSI pressure with boundary correction" begin - volume = fluid_density / 1005.0 + # Check the pressure forces when a fluid particle and a structure particle are + # at the same position (coincident particles), or less than `almostzero(h)` apart. + # + # The uncorrected kernel gradient is treated as zero at these distances. With + # `KernelCorrection`, the boundary gradient can still be nonzero. Set the fluid + # gradient to zero to isolate the boundary's contribution to the pressure force. + # The fluid and structure must then experience equal and opposite forces. + # For the other configurations tested here, both forces must be zero. + # + # For rigid bodies, use `ContinuityDensity` and also check the boundary-density + # derivative. With `KernelCorrection`, include a separation just above the + # zero-distance threshold to check continuity across it. + @testset "Coincident FSI pressure and boundary continuity" begin + velocity = SVector(1.0, -0.5) + density = 1005.0 + volume = fluid_density / density kernel_zero = 10 / (7pi) coefficient = 2.0 dw_gamma = SVector(-0.5, -0.5) boundary_gradient = -kernel_zero * dw_gamma / coefficient - # Cover WCSPH and EDAC, no correction and the corrections whose kernel gradient - # vanishes at zero distance (`GradientCorrection` and `BlendedGradientCorrection`), - # and the average pressure reduction, which removes the pressure of a single - # fluid particle. - # Note that EDAC does not support corrections yet (this is not part of this - # test), so only the pairs at the same position, where the fluid gradient - # vanishes, are evaluated for EDAC with correction. - near_zero_configurations = (("WCSPH", GradientCorrection()), - ("EDAC", BlendedGradientCorrection(0.3)), + # Cover matching corrections and uncorrected WCSPH and EDAC. Prescribe a zero + # fluid gradient with `KernelCorrection` to isolate the nonzero boundary + # gradient. The other configurations skip pairs at the same position. + near_zero_configurations = (("WCSPH", KernelCorrection()), + ("WCSPH", GradientCorrection()), + ("WCSPH", BlendedGradientCorrection(0.3)), ("WCSPH", nothing), - ("EDAC with average pressure reduction", - GradientCorrection())) - distances = (0.0, TrixiParticles.almostzero(smoothing_length) / 2) + ("EDAC", nothing), + ("EDAC with average pressure reduction", nothing)) + almostzero = TrixiParticles.almostzero(smoothing_length) + distances = (0.0, almostzero / 2, 10 * almostzero) + + for (structure_type, (scheme, correction), distance) in + Iterators.product(structure_types, near_zero_configurations, distances) + + # The above-threshold case only tests the boundary-density derivative. + if distance >= almostzero && + !(structure_type === RigidBodySystem && correction isa KernelCorrection) + continue + end - for structure_type in structure_types, - config in near_zero_configurations, - distance in distances - scheme, correction = config fluid_coordinates = reshape([1.5 + distance, 0.0], 2, 1) - fluid_system = create_fluid_system(scheme, (1.0, -0.5), 1005.0, nothing; + fluid_system = create_fluid_system(scheme, velocity, density, nothing; correction, coordinates=fluid_coordinates) + boundary_density = structure_type === RigidBodySystem ? + ContinuityDensity() : AdamiPressureExtrapolation() structure_system = create_structure_system(structure_type, nothing; - boundary_correction=KernelCorrection()) + boundary_density, + boundary_correction=correction, + boundary_initial_density=density) fluid, structure, arrays, semi, _ = initialize(fluid_system, structure_system) # Cache assembly is covered above; prescribe a nonzero boundary gradient - # here to isolate the coincident-pair pressure and safe fluid gradient. - structure.boundary_model.cache.kernel_correction_coefficient .= coefficient - structure.boundary_model.cache.dw_gamma .= dw_gamma + # and a zero fluid gradient to isolate pressure at coincident pairs. + if correction isa KernelCorrection + fluid.cache.kernel_correction_coefficient .= coefficient + fluid.cache.dw_gamma .= 0 + structure.boundary_model.cache.kernel_correction_coefficient .= coefficient + structure.boundary_model.cache.dw_gamma .= dw_gamma + end (force_fluid, force_structure, - _) = pair_forces(fluid, structure, arrays, semi) - - pressure = scheme == "EDAC with average pressure reduction" ? 0.0 : 500.0 - expected = isnothing(correction) ? zero(boundary_gradient) : - pressure * volume^2 * boundary_gradient - @test isapprox(force_fluid, expected, atol=sqrt(eps())) - @test isapprox(force_structure, -expected, atol=sqrt(eps())) - end - end - - # The kernel gradient of `KernelCorrection` does not vanish for particles at the same - # position. The continuity equation of the boundary particles must therefore be - # continuous when a fluid particle approaches a boundary particle, even if the - # correction of the fluid alone would skip pairs at the same position. - @testset "Boundary continuity near coincident particles" begin - velocity = SVector(1.0, -0.5) - density = 1005.0 - fluid_mass = fluid_density * particle_spacing^2 - coefficient = 2.0 - dw_gamma = SVector(-0.5, -0.5) - # `(∇W(0) - W(0) γ) / c` - grad_kernel = -10 / (7pi) * dw_gamma / coefficient - # The density of the boundary particle is the initial density `fluid_density` - expected_density_rate = -fluid_density / density * fluid_mass * - dot(velocity, grad_kernel) - almostzero = TrixiParticles.almostzero(smoothing_length) - - for correction in (nothing, GradientCorrection()), - distance in (0.0, almostzero / 2, 10 * almostzero) - fluid_coordinates = reshape([1.5 + distance, 0.0], 2, 1) - fluid_system = create_fluid_system("WCSPH", velocity, density, nothing; - correction, - coordinates=fluid_coordinates) - structure_system = create_structure_system(RigidBodySystem, nothing; - boundary_density=ContinuityDensity(), - boundary_correction=KernelCorrection()) - fluid, structure, arrays, semi, - _ = initialize(fluid_system, - structure_system) - - # Prescribe a nonzero boundary gradient at zero distance - structure.boundary_model.cache.kernel_correction_coefficient .= coefficient - structure.boundary_model.cache.dw_gamma .= dw_gamma - _, _, dv_structure = pair_forces(fluid, structure, arrays, semi) + dv_structure) = pair_forces(fluid, structure, arrays, semi) + + if distance < almostzero + # Use the EOS value to account for rounding in the initial pressure. + pressure = state_equation(density) + expected = correction isa KernelCorrection ? + pressure * volume^2 * boundary_gradient : + zero(boundary_gradient) + @test isapprox(force_fluid, expected, rtol=1e-14, atol=1e-14) + @test isapprox(force_structure, -expected, rtol=1e-14, atol=1e-14) + end - @test isapprox(dv_structure[end, 1], expected_density_rate, rtol=1e-5) + if structure isa RigidBodySystem && correction isa KernelCorrection + # Both particles have density `density`, so their density ratio is one. + # Compare against the zero-distance limit, including just above the + # threshold, where the uncorrected gradient is small but nonzero. + expected_density_rate = -fluid.mass[1] * + dot(velocity, boundary_gradient) + @test isapprox(dv_structure[end, 1], expected_density_rate, rtol=1e-5) + end end end From 8455c9ffec51c03396f6692636f0bebbe22c03ff Mon Sep 17 00:00:00 2001 From: Sven Berger Date: Wed, 11 Nov 2026 17:06:49 +0100 Subject: [PATCH 08/11] Require uncorrected structure boundary models and address review findings - Temporarily require `correction=nothing` for the `BoundaryModelDummyParticles` of a `TotalLagrangianSPHSystem` or `RigidBodySystem`. Their correction values require the complete current hydrodynamic neighborhood, which is added in #1351. - Assemble the renormalized density gradient of `DensityDiffusionAntuono` from the uncorrected kernel gradient instead of the corrected fluid gradient, whose correction values are not computed yet when the density diffusion is updated. - Allocate the dummy-particle correction caches with the element type of the density. - Compute the kernel gradient of the boundary model once for the pressure acceleration and the continuity equation in the structure-fluid interaction. - Define `system_correction` for `BoundaryModelDummyParticles` instead of a custom `smoothing_kernel_grad` method, so that the safe and unsafe gradients are consistent. --- NEWS.md | 16 +- docs/src/systems/boundary.md | 11 +- docs/src/systems/fluid.md | 7 +- docs/src/systems/rigid_body.md | 4 +- docs/src/systems/total_lagrangian_sph.md | 7 +- src/general/corrections.jl | 6 +- .../boundary/wall_boundary/dummy_particles.jl | 48 +--- .../fluid/entropically_damped_sph/rhs.jl | 7 +- .../fluid/implicit_incompressible_sph/rhs.jl | 5 +- src/schemes/fluid/pressure_acceleration.jl | 10 + .../density_diffusion.jl | 5 +- .../fluid/weakly_compressible_sph/rhs.jl | 8 +- src/schemes/structure/rigid_body/system.jl | 7 +- src/schemes/structure/structure.jl | 107 +++++++-- .../structure/total_lagrangian_sph/system.jl | 7 +- test/general/semidiscretization.jl | 65 +++--- .../dummy_particles/dummy_particles.jl | 28 +++ test/schemes/fluid/rhs.jl | 207 ++++-------------- .../density_diffusion.jl | 58 +++++ 19 files changed, 322 insertions(+), 291 deletions(-) diff --git a/NEWS.md b/NEWS.md index b95876eda0..65418f36a9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -14,6 +14,9 @@ used in the Julia ecosystem. Notable changes will be documented in this file for - The smoothing kernel and smoothing length of a `BoundaryModelDummyParticles` of a `TotalLagrangianSPHSystem` or `RigidBodySystem` must now be the same as those of all fluid systems in the simulation (#1338, #1348). +- The `correction` of a `BoundaryModelDummyParticles` of a `TotalLagrangianSPHSystem` or + `RigidBodySystem` must now be `nothing`. Previously, it was ignored or caused an error + (#1342). ### Features @@ -29,11 +32,14 @@ used in the Julia ecosystem. Notable changes will be documented in this file for ### Important Bugfixes -- Fixed fluid–structure pressure acceleration and rigid-body boundary-density evolution to - use the boundary model's kernel and corrections, and fixed correction-matrix assembly for - `GradientCorrection` and `BlendedGradientCorrection`. For TLSPH and rigid-body systems, - dummy-particle boundary corrections must now be `nothing` or match every fluid system's - correction, including its parameters (#1342). +- Fixed the fluid–structure pressure acceleration of fluids with `KernelCorrection`, + `GradientCorrection`, `BlendedGradientCorrection` or `MixedKernelGradientCorrection` to use + the smoothing kernel and smoothing length of the boundary model at the structure particles + instead of those of the `TotalLagrangianSPHSystem`. Also fixed the assembly of the + correction matrix, which made `GradientCorrection` have no effect and + `BlendedGradientCorrection` depend on the order of the neighbors, so results with these + corrections change. `DensityDiffusionAntuono` now always uses the uncorrected kernel + gradient (#1342). - Fixed structure-fluid reaction forces with asymmetric corrected fluid kernel gradients by evaluating the fluid-first gradient directly. The continuity equation of `RigidBodySystem` boundaries with `ContinuityDensity` now uses the kernel gradient diff --git a/docs/src/systems/boundary.md b/docs/src/systems/boundary.md index 4a9300c1b2..711c22a575 100644 --- a/docs/src/systems/boundary.md +++ b/docs/src/systems/boundary.md @@ -199,12 +199,11 @@ With [`ContinuityDensity`](@ref), the density of the boundary particles also evo corrected kernel gradient of the boundary particles. For a [`TotalLagrangianSPHSystem`](@ref) or a [`RigidBodySystem`](@ref), the kernel gradient -at each boundary particle uses the smoothing kernel, smoothing length and correction of -the boundary model. Its smoothing kernel and smoothing length must match those of the -fluid systems. The smoothing kernel of the [`TotalLagrangianSPHSystem`](@ref) is used for -solid mechanics instead. -The boundary model's `correction` must be `nothing` or match the correction of every fluid -system, including parameters such as the blending factor of [`BlendedGradientCorrection`](@ref). +at each boundary particle uses the smoothing kernel and smoothing length of the boundary +model, which must match those of the fluid systems. The smoothing kernel of the +[`TotalLagrangianSPHSystem`](@ref) is used for solid mechanics instead. +Corrections at the boundary particles of these structures are not supported yet, +so the boundary model's `correction` must be `nothing`. These requirements are checked when constructing the [`Semidiscretization`](@ref). ## Repulsive Particles diff --git a/docs/src/systems/fluid.md b/docs/src/systems/fluid.md index e025c6382d..8010745712 100644 --- a/docs/src/systems/fluid.md +++ b/docs/src/systems/fluid.md @@ -212,10 +212,9 @@ Pages = [joinpath("schemes", "fluid", "viscosity.jl")] ## [Corrections](@id corrections) For fluid-structure interaction with a [`TotalLagrangianSPHSystem`](@ref) or a -[`RigidBodySystem`](@ref), the structure's [`BoundaryModelDummyParticles`](@ref) may use -`correction=nothing` or the same correction as every fluid system, including its parameters. -See [Boundary Corrections](@ref boundary_corrections) for how the fluid and boundary -gradients enter the pressure acceleration. +[`RigidBodySystem`](@ref), the structure's [`BoundaryModelDummyParticles`](@ref) must use +`correction=nothing`. See [Boundary Corrections](@ref boundary_corrections) for how +the corrected fluid gradient and the boundary gradient enter the pressure acceleration. ```@autodocs Modules = [TrixiParticles] diff --git a/docs/src/systems/rigid_body.md b/docs/src/systems/rigid_body.md index 6cd418f553..6051358404 100644 --- a/docs/src/systems/rigid_body.md +++ b/docs/src/systems/rigid_body.md @@ -6,8 +6,8 @@ keeping the structure kinematics rigid. Fluid-structure interaction is configured through `boundary_model`. When using [`BoundaryModelDummyParticles`](@ref), its smoothing kernel and smoothing length must match -those of every fluid system. Its `correction` must be `nothing` or match the fluid correction, -including its parameters (see [Boundary Corrections](@ref boundary_corrections)). +those of every fluid system. Its `correction` must be `nothing` +(see [Boundary Corrections](@ref boundary_corrections)). ## API diff --git a/docs/src/systems/total_lagrangian_sph.md b/docs/src/systems/total_lagrangian_sph.md index b5bb21f37d..c5d90dfd86 100644 --- a/docs/src/systems/total_lagrangian_sph.md +++ b/docs/src/systems/total_lagrangian_sph.md @@ -60,11 +60,10 @@ The term ``\bm{f}_a^{PF}`` is an optional penalty force. See e.g. [`PenaltyForce The smoothing kernel, the smoothing length and the correction matrix ``\bm{L}_{0a}`` above are only used for the solid mechanics of the structure. The interaction with fluids is determined by the `boundary_model`, which has its own -smoothing kernel, smoothing length and correction -(see [Boundary Models](@ref boundary_models)). +smoothing kernel and smoothing length (see [Boundary Models](@ref boundary_models)). When using [`BoundaryModelDummyParticles`](@ref), its smoothing kernel and smoothing length -must match those of every fluid system. Its `correction` must be `nothing` or match the fluid -correction, including its parameters (see [Boundary Corrections](@ref boundary_corrections)). +must match those of every fluid system. Its `correction` must be `nothing` +(see [Boundary Corrections](@ref boundary_corrections)). ```@autodocs Modules = [TrixiParticles] diff --git a/src/general/corrections.jl b/src/general/corrections.jl index 602cf2d34d..28824aa614 100644 --- a/src/general/corrections.jl +++ b/src/general/corrections.jl @@ -315,7 +315,11 @@ function compute_gradient_correction_matrix!(corr_matrix, system, coordinates, d # Loop over all pairs of particles and neighbors within the kernel cutoff foreach_point_neighbor(system, system, coordinates, coordinates, semi) do particle, neighbor, pos_diff, distance - grad_kernel = smoothing_kernel_grad(system, pos_diff, distance, particle) + # Assemble the matrix from the uncorrected kernel gradient. The corrected gradient + # of a fluid would use correction values of the fluid that are computed later + # in the update and are therefore not yet available. + grad_kernel = kernel_grad(system_smoothing_kernel(system), pos_diff, distance, + smoothing_length(system, particle)) iszero(grad_kernel) && return volume = @inbounds mass[neighbor] / density_fun(neighbor) diff --git a/src/schemes/boundary/wall_boundary/dummy_particles.jl b/src/schemes/boundary/wall_boundary/dummy_particles.jl index fbff6c815d..8a6812f6e9 100644 --- a/src/schemes/boundary/wall_boundary/dummy_particles.jl +++ b/src/schemes/boundary/wall_boundary/dummy_particles.jl @@ -30,8 +30,8 @@ Boundary model for [`WallBoundarySystem`](@ref). uses a correction with an asymmetric kernel gradient (see [Corrections](@ref boundary_corrections)). For a [`TotalLagrangianSPHSystem`](@ref) or a [`RigidBodySystem`](@ref), - it must be `nothing` or match the correction of all fluid systems, - including its parameters. + it must be `nothing`, since corrections at the boundary particles + of structures are not supported yet. - `viscosity`: Slip (default) or no-slip condition. See description below for further information. - `clip_negative_pressure=false`: Clip negative boundary pressures to avoid sticking @@ -100,9 +100,11 @@ function BoundaryModelDummyParticles(initial_density, hydrodynamic_mass, @assert length(initial_density) == length(hydrodynamic_mass) n_particles = length(initial_density) + # The correction values use the same element type as the fluid's correction values, + # so that the kernel gradients at the boundary particles don't change the precision. cache = (; create_cache_model(viscosity, n_particles, NDIMS)..., create_cache_model(initial_density, density_calculator, NDIMS)..., - create_cache_model(correction, initial_density, NDIMS, n_particles)...) + create_cache_correction(correction, initial_density, NDIMS, n_particles)...) # If the `reference_density_spacing` is set calculate the `ideal_neighbor_count` if reference_particle_spacing > 0 @@ -124,17 +126,14 @@ end return ndims(boundary_model.smoothing_kernel) end -# Kernel gradient at a boundary particle with the kernel, smoothing length, correction and -# correction values of the boundary model. This is used for the interaction with fluids, -# e.g., in the asymmetric pressure acceleration of a fluid with a corrected kernel gradient -# and in the continuity equation of the boundary particles. +# The fluid-structure interaction calls `smoothing_kernel_grad(boundary_model, ...)` to +# compute the kernel gradient at the boundary particles of a structure, e.g., in the +# asymmetric pressure acceleration of a fluid with a corrected kernel gradient and in the +# continuity equation of the boundary particles. Like for a system, `smoothing_kernel_grad` +# and `smoothing_kernel_grad_unsafe` then use the smoothing kernel, smoothing length and +# correction of the boundary model together with the correction values below. # A `TotalLagrangianSPHSystem` has its own kernel and correction matrix for solid mechanics. -@inline function smoothing_kernel_grad(model::BoundaryModelDummyParticles, pos_diff, - distance, particle) - (; smoothing_kernel, smoothing_length, correction) = model - return corrected_kernel_grad(smoothing_kernel, pos_diff, distance, smoothing_length, - correction, model, particle) -end +@inline system_correction(model::BoundaryModelDummyParticles) = model.correction @propagate_inbounds function kernel_correction_coefficient(model::BoundaryModelDummyParticles, particle) @@ -266,29 +265,6 @@ struct PressureBoundaries{ELTYPE} return new{eltype(time_step)}(time_step, omega) end end -@inline create_cache_model(correction, density, NDIMS, nparticles) = (;) - -function create_cache_model(::ShepardKernelCorrection, density, NDIMS, n_particles) - return (; kernel_correction_coefficient=similar(density)) -end - -function create_cache_model(::KernelCorrection, density, NDIMS, n_particles) - dw_gamma = Array{Float64}(undef, NDIMS, n_particles) - return (; kernel_correction_coefficient=similar(density), dw_gamma) -end - -function create_cache_model(::Union{GradientCorrection, BlendedGradientCorrection}, density, - NDIMS, n_particles) - correction_matrix = Array{Float64, 3}(undef, NDIMS, NDIMS, n_particles) - return (; correction_matrix) -end - -function create_cache_model(::MixedKernelGradientCorrection, density, NDIMS, n_particles) - dw_gamma = Array{Float64}(undef, NDIMS, n_particles) - correction_matrix = Array{Float64, 3}(undef, NDIMS, NDIMS, n_particles) - return (; kernel_correction_coefficient=similar(density), dw_gamma, correction_matrix) -end - function create_cache_model(initial_density::AbstractVector, ::Union{SummationDensity, PressureMirroring, PressureZeroing}, NDIMS) diff --git a/src/schemes/fluid/entropically_damped_sph/rhs.jl b/src/schemes/fluid/entropically_damped_sph/rhs.jl index d09c1a84f7..8a9e32a320 100644 --- a/src/schemes/fluid/entropically_damped_sph/rhs.jl +++ b/src/schemes/fluid/entropically_damped_sph/rhs.jl @@ -95,13 +95,16 @@ end # Note that this function is also used for the structure-fluid interaction to compute # the exact opposite pair force. When adding new terms here, make sure that they are # also valid for structure neighbors. +# `grad_kernel_neighbor` is the kernel gradient of the neighbor for the asymmetric pressure +# acceleration if it has already been computed, or `nothing` (see `pressure_acceleration`). @propagate_inbounds function add_momentum_equation(dv_particle, particle_system::EntropicallyDampedSPHSystem, neighbor_system, v_particle_system, v_neighbor_system, particle, neighbor, pos_diff, distance, grad_kernel, sound_speed, m_a, m_b, - p_a, p_b, rho_a, rho_b, v_a, v_b) + p_a, p_b, rho_a, rho_b, v_a, v_b, + grad_kernel_neighbor=nothing) (; correction) = particle_system surface_tension_a = surface_tension_model(particle_system) @@ -119,7 +122,7 @@ end particle, neighbor, m_a, m_b, p_a - p_avg, p_b - p_avg, rho_a, rho_b, pos_diff, distance, grad_kernel, - correction) + correction, grad_kernel_neighbor) dv_particle = add_dv_viscosity(dv_particle, particle_system, neighbor_system, v_particle_system, v_neighbor_system, diff --git a/src/schemes/fluid/implicit_incompressible_sph/rhs.jl b/src/schemes/fluid/implicit_incompressible_sph/rhs.jl index 21feadd4c8..e0bd256240 100644 --- a/src/schemes/fluid/implicit_incompressible_sph/rhs.jl +++ b/src/schemes/fluid/implicit_incompressible_sph/rhs.jl @@ -71,13 +71,16 @@ end # Note that this function is also used for the structure-fluid interaction to compute # the exact opposite pair force. When adding new terms here, make sure that they are # also valid for structure neighbors. +# `grad_kernel_neighbor` is only used by systems with an asymmetric kernel gradient +# (see `pressure_acceleration`). @propagate_inbounds function add_momentum_equation(dv_particle, particle_system::ImplicitIncompressibleSPHSystem, neighbor_system, v_particle_system, v_neighbor_system, particle, neighbor, pos_diff, distance, grad_kernel, sound_speed, m_a, m_b, - p_a, p_b, rho_a, rho_b, v_a, v_b) + p_a, p_b, rho_a, rho_b, v_a, v_b, + grad_kernel_neighbor=nothing) dv_particle += pressure_acceleration(particle_system, neighbor_system, particle, neighbor, m_a, m_b, p_a, p_b, rho_a, rho_b, pos_diff, diff --git a/src/schemes/fluid/pressure_acceleration.jl b/src/schemes/fluid/pressure_acceleration.jl index b6114c0bc1..185bdcbe30 100644 --- a/src/schemes/fluid/pressure_acceleration.jl +++ b/src/schemes/fluid/pressure_acceleration.jl @@ -168,3 +168,13 @@ end return pressure_acceleration_formulation(particle_system)(m_a, m_b, rho_a, rho_b, p_a, p_b, W_a, W_b) end + +# The last argument is the kernel gradient `W_b` of the neighbor if it has already been +# computed (see `interact_structure_fluid!`), or `nothing` to compute it when required. +@inline function pressure_acceleration(particle_system, neighbor_system, particle, neighbor, + m_a, m_b, p_a, p_b, rho_a, rho_b, pos_diff, + distance, W_a, correction, W_b::Nothing) + return pressure_acceleration(particle_system, neighbor_system, particle, neighbor, + m_a, m_b, p_a, p_b, rho_a, rho_b, pos_diff, distance, + W_a, correction) +end diff --git a/src/schemes/fluid/weakly_compressible_sph/density_diffusion.jl b/src/schemes/fluid/weakly_compressible_sph/density_diffusion.jl index 281f6372f5..83acc368e3 100644 --- a/src/schemes/fluid/weakly_compressible_sph/density_diffusion.jl +++ b/src/schemes/fluid/weakly_compressible_sph/density_diffusion.jl @@ -193,7 +193,10 @@ function update!(density_diffusion::DensityDiffusionAntuono, v, u, system, semi) rho_a = @inbounds current_density(v, system, particle) rho_b = @inbounds current_density(v, system, neighbor) - grad_kernel = smoothing_kernel_grad(system, pos_diff, distance, particle) + # The renormalized density gradient uses the uncorrected kernel gradient, + # like the matrix `L` above (see the docstring of `DensityDiffusionAntuono`). + grad_kernel = kernel_grad(system_smoothing_kernel(system), pos_diff, distance, + smoothing_length(system, particle)) L = @inbounds extract_smatrix(density_diffusion_correction_matrix, system, particle) diff --git a/src/schemes/fluid/weakly_compressible_sph/rhs.jl b/src/schemes/fluid/weakly_compressible_sph/rhs.jl index d474cb0091..03357bdb78 100644 --- a/src/schemes/fluid/weakly_compressible_sph/rhs.jl +++ b/src/schemes/fluid/weakly_compressible_sph/rhs.jl @@ -122,13 +122,16 @@ end # Note that this function is also used for the structure-fluid interaction to compute # the exact opposite pair force. When adding new terms here, make sure that they are # also valid for structure neighbors. +# `grad_kernel_neighbor` is the kernel gradient of the neighbor for the asymmetric pressure +# acceleration if it has already been computed, or `nothing` (see `pressure_acceleration`). @propagate_inbounds function add_momentum_equation(dv_particle, particle_system::WeaklyCompressibleSPHSystem, neighbor_system, v_particle_system, v_neighbor_system, particle, neighbor, pos_diff, distance, grad_kernel, sound_speed, m_a, m_b, - p_a, p_b, rho_a, rho_b, v_a, v_b) + p_a, p_b, rho_a, rho_b, v_a, v_b, + grad_kernel_neighbor=nothing) (; correction) = particle_system surface_tension_a = surface_tension_model(particle_system) @@ -145,7 +148,8 @@ end dv_pressure = pressure_acceleration(particle_system, neighbor_system, particle, neighbor, m_a, m_b, p_a, p_b, rho_a, rho_b, pos_diff, - distance, grad_kernel, correction) + distance, grad_kernel, correction, + grad_kernel_neighbor) dv_particle += dv_pressure * pressure_correction dv_particle = add_dv_viscosity(dv_particle, particle_system, neighbor_system, diff --git a/src/schemes/structure/rigid_body/system.jl b/src/schemes/structure/rigid_body/system.jl index dd15c5b0c7..23535ae9cb 100644 --- a/src/schemes/structure/rigid_body/system.jl +++ b/src/schemes/structure/rigid_body/system.jl @@ -20,8 +20,9 @@ torque and applied consistently to all rigid particles. # Keywords - `boundary_model`: Boundary model for fluid-structure interaction (see [Boundary Models](@ref boundary_models)). - For [`BoundaryModelDummyParticles`](@ref), `correction` must be `nothing` - or match the correction of every fluid system, including its parameters. + For [`BoundaryModelDummyParticles`](@ref), `correction` must be `nothing`, + since corrections at the boundary particles of structures are not + supported yet. - `contact_model`: Optional rigid contact model. If specified, rigid-wall and rigid-rigid collisions are enabled. - `acceleration`: Global acceleration vector applied to all rigid particles. @@ -626,6 +627,8 @@ function check_configuration(system::RigidBodySystem, systems, nhs) end end + check_boundary_model_correction(system, boundary_model) + foreach_system(systems) do neighbor if neighbor isa AbstractFluidSystem && boundary_model === nothing throw(ArgumentError("a boundary model for `RigidBodySystem` must be specified " * diff --git a/src/schemes/structure/structure.jl b/src/schemes/structure/structure.jl index 0d518d446f..6cb0291ff4 100644 --- a/src/schemes/structure/structure.jl +++ b/src/schemes/structure/structure.jl @@ -33,16 +33,51 @@ end W_b = smoothing_kernel_grad(neighbor_system.boundary_model, -pos_diff, distance, neighbor) + return pressure_acceleration(particle_system, neighbor_system, particle, neighbor, + m_a, m_b, p_a, p_b, rho_a, rho_b, pos_diff, distance, + W_a, correction, W_b) +end + +# Same as above, but with the kernel gradient `W_b` at the structure particle already +# computed. The structure-fluid interaction computes it once for the pressure acceleration +# and the continuity equation of the boundary particles. +@inline function pressure_acceleration(particle_system, + neighbor_system::Union{TotalLagrangianSPHSystem{<:BoundaryModelDummyParticles}, + RigidBodySystem{<:BoundaryModelDummyParticles}}, + particle, neighbor, m_a, m_b, p_a, p_b, rho_a, rho_b, + pos_diff, distance, W_a, + correction::Union{KernelCorrection, + GradientCorrection, + BlendedGradientCorrection, + MixedKernelGradientCorrection}, + W_b::AbstractVector) # With correction, the kernel gradient is not necessarily symmetric, so call the # asymmetric version of the pressure acceleration formulation. return pressure_acceleration_formulation(particle_system)(m_a, m_b, rho_a, rho_b, p_a, p_b, W_a, W_b) end +# Kernel gradient at the structure particle that is required by the pressure acceleration +# of a fluid with an asymmetric corrected kernel gradient (see above). +# It is `nothing` when the pressure acceleration doesn't require it. +@inline function boundary_pressure_kernel_grad(fluid_correction, particle_system, + pos_diff, distance, particle) + return nothing +end + +@inline function boundary_pressure_kernel_grad(::Union{KernelCorrection, + GradientCorrection, + BlendedGradientCorrection, + MixedKernelGradientCorrection}, + particle_system::Union{TotalLagrangianSPHSystem{<:BoundaryModelDummyParticles}, + RigidBodySystem{<:BoundaryModelDummyParticles}}, + pos_diff, distance, particle) + return smoothing_kernel_grad(particle_system.boundary_model, pos_diff, distance, + particle) +end + # Both FSI directions must find the same particle pairs and use matching boundary and # fluid kernels. The TLSPH kernel for solid mechanics may differ. -# The boundary correction must match the fluid correction or be `nothing`. Thus, if the -# fluid gradient vanishes at zero distance, the boundary gradient also vanishes there. # Note that this is only a restriction for `BoundaryModelDummyParticles`, since # `BoundaryModelMonaghanKajtar` always uses the compact support of the fluid. function check_boundary_model_fsi(system, boundary_model::BoundaryModelDummyParticles, @@ -60,17 +95,30 @@ function check_boundary_model_fsi(system, boundary_model::BoundaryModelDummyPart "as those of the fluid system `$(nameof(typeof(neighbor_system)))`.")) end - if !isnothing(boundary_model.correction) && - boundary_model.correction != system_correction(neighbor_system) - throw(ArgumentError("the correction of the boundary model of the " * - "`$(nameof(typeof(system)))` must be `nothing` or the same " * - "as that of the fluid system `$(nameof(typeof(neighbor_system)))`.")) + return system +end + +check_boundary_model_fsi(system, boundary_model, neighbor_system) = system + +# The correction values of a boundary particle are sums over its complete hydrodynamic +# neighborhood in the current configuration, including the particles of its own structure +# within the compact support of the boundary model. The neighborhood searches of structures +# don't provide this neighborhood (the TLSPH self-interaction uses the TLSPH kernel in the +# initial configuration and rigid bodies only search for contact), so corrections of the +# boundary model of a structure are not supported yet. +# Without correction, the kernel gradient of the boundary model also vanishes at zero +# distance (see `interact_structure_fluid!`). +function check_boundary_model_correction(system, + boundary_model::BoundaryModelDummyParticles) + if !isnothing(boundary_model.correction) + throw(ArgumentError("the boundary model of a `$(nameof(typeof(system)))` does " * + "not support corrections yet. Use `correction=nothing`.")) end return system end -check_boundary_model_fsi(system, boundary_model, neighbor_system) = system +check_boundary_model_correction(system, boundary_model) = system function interact_structure_fluid!(dv, v_particle_system, u_particle_system, v_neighbor_system, u_neighbor_system, @@ -89,7 +137,7 @@ function interact_structure_fluid!(dv, v_particle_system, u_particle_system, points=eachparticle) do particle, neighbor, pos_diff, distance # Skip neighbors with (almost) the same position if the fluid gradient vanishes, # because computing it would divide by zero (see `almostzero`). The boundary - # gradient also vanishes in this case (see `check_boundary_model_fsi`). + # gradient also vanishes in this case (see `check_boundary_model_correction`). # Note that `return` only exits the closure, i.e., skips the current neighbor. skip_zero_distance(neighbor_system) && distance < almostzero(h) && return @@ -119,6 +167,16 @@ function interact_structure_fluid!(dv, v_particle_system, u_particle_system, p_a = current_pressure(v_particle_system, particle_system, particle) p_b = current_pressure(v_neighbor_system, neighbor_system, neighbor) + # The kernel gradient of the boundary model at the structure particle is required + # by the pressure acceleration of a fluid with an asymmetric corrected kernel + # gradient and by the continuity equation of the boundary particles. + # When the pressure acceleration requires it, compute it only once for both. + # This is the same gradient as in the fluid-structure interaction, since negating + # the fluid-first displacement is exact. + grad_kernel_boundary = boundary_pressure_kernel_grad(system_correction(neighbor_system), + particle_system, pos_diff, + distance, particle) + # Compute the acceleration of the fluid particle due to the structure particle # with the exact same function as in the fluid-structure interaction. # Particle and neighbor (and the corresponding systems and particle quantities) @@ -134,7 +192,7 @@ function interact_structure_fluid!(dv, v_particle_system, u_particle_system, v_neighbor_system, v_particle_system, neighbor, particle, -pos_diff, distance, grad_kernel_fluid, sound_speed, m_b, m_a, p_b, p_a, - rho_b, rho_a, v_b, v_a) + rho_b, rho_a, v_b, v_a, grad_kernel_boundary) dv_particle = -dv_fluid accumulate_structure_fluid_pair!(dv, dv_particle, particle_system, particle, m_b) @@ -143,7 +201,7 @@ function interact_structure_fluid!(dv, v_particle_system, u_particle_system, particle_system, neighbor_system, particle, neighbor, pos_diff, distance, m_b, rho_a, rho_b, v_a, v_b, - grad_kernel_fluid) + grad_kernel_fluid, grad_kernel_boundary) @inbounds write_drho_particle!(dv, particle_system, drho_particle, particle) end @@ -155,25 +213,30 @@ end particle_system::AbstractStructureSystem, neighbor_system::AbstractFluidSystem, particle, neighbor, pos_diff, distance, - m_b, rho_a, rho_b, v_a, v_b, grad_kernel_fluid) + m_b, rho_a, rho_b, v_a, v_b, grad_kernel_fluid, + grad_kernel_boundary=nothing) return drho_particle end +# `grad_kernel_boundary` is the kernel gradient of the boundary model at the structure +# particle if it has already been computed for the pressure acceleration, or `nothing`. @inline function add_continuity_equation(drho_particle, particle_system::Union{RigidBodySystem{<:BoundaryModelDummyParticles{ContinuityDensity}}, TotalLagrangianSPHSystem{<:BoundaryModelDummyParticles{ContinuityDensity}}}, neighbor_system::AbstractFluidSystem, particle, neighbor, pos_diff, distance, - m_b, rho_a, rho_b, v_a, v_b, grad_kernel_fluid) + m_b, rho_a, rho_b, v_a, v_b, grad_kernel_fluid, + grad_kernel_boundary=nothing) (; boundary_model) = particle_system # As for a `WallBoundarySystem`, the density of the boundary particles evolves with # the kernel gradient of the boundary model (including its correction) # and not with the corrected kernel gradient of the fluid. grad_kernel = boundary_continuity_kernel_grad(system_correction(neighbor_system), - boundary_model.correction, + system_correction(boundary_model), boundary_model, pos_diff, distance, - particle, grad_kernel_fluid) + particle, grad_kernel_fluid, + grad_kernel_boundary) return add_continuity_equation(drho_particle, density_calculator(neighbor_system), @@ -182,7 +245,16 @@ end @inline function boundary_continuity_kernel_grad(fluid_correction, boundary_correction, boundary_model, pos_diff, distance, - particle, grad_kernel_fluid) + particle, grad_kernel_fluid, + grad_kernel_boundary) + # Reuse the kernel gradient of the boundary model from the pressure acceleration + return grad_kernel_boundary +end + +@inline function boundary_continuity_kernel_grad(fluid_correction, boundary_correction, + boundary_model, pos_diff, distance, + particle, grad_kernel_fluid, + grad_kernel_boundary::Nothing) # Use the kernel, smoothing length and correction of the boundary model. The kernel of # a `TotalLagrangianSPHSystem` for solid mechanics may be different. # Use the safe version, which handles particles at the same position. The gradient of @@ -197,7 +269,8 @@ end AkinciFreeSurfaceCorrection, ShepardKernelCorrection}, boundary_model, pos_diff, distance, - particle, grad_kernel_fluid) + particle, grad_kernel_fluid, + grad_kernel_boundary::Nothing) # Neither correction changes the gradient, and the kernel and smoothing length of the # boundary model match the ones of the fluid (see `check_boundary_model_fsi`). # Reversing the displacement only flips the sign of the uncorrected gradient. diff --git a/src/schemes/structure/total_lagrangian_sph/system.jl b/src/schemes/structure/total_lagrangian_sph/system.jl index 60a69690f5..b319f945ab 100644 --- a/src/schemes/structure/total_lagrangian_sph/system.jl +++ b/src/schemes/structure/total_lagrangian_sph/system.jl @@ -33,8 +33,9 @@ See [Total Lagrangian SPH](@ref tlsph) for more details on the method. See [`PrescribedMotion`](@ref) for details. - `boundary_model`: Boundary model to compute the hydrodynamic density and pressure for fluid-structure interaction (see [Boundary Models](@ref boundary_models)). - For [`BoundaryModelDummyParticles`](@ref), `correction` must be `nothing` - or match the correction of every fluid system, including its parameters. + For [`BoundaryModelDummyParticles`](@ref), `correction` must be `nothing`, + since corrections at the boundary particles of structures are not + supported yet. - `penalty_force`: Penalty force to ensure regular particle position under large deformations (see [`PenaltyForceGanzenmueller`](@ref)). - `viscosity`: Artificial viscosity model to stabilize both the TLSPH and the FSI. @@ -871,6 +872,8 @@ function check_configuration(system::TotalLagrangianSPHSystem, systems, nhs) "`ContinuityDensity` is not yet supported for a `TotalLagrangianSPHSystem`")) end + check_boundary_model_correction(system, boundary_model) + foreach_system(systems) do neighbor check_boundary_model_fsi(system, boundary_model, neighbor) end diff --git a/test/general/semidiscretization.jl b/test/general/semidiscretization.jl index 79dfe7babf..0612b296cb 100644 --- a/test/general/semidiscretization.jl +++ b/test/general/semidiscretization.jl @@ -224,26 +224,32 @@ end end - # A boundary may omit the fluid correction, but a nontrivial boundary - # correction must match the fluid correction, including its parameters. - correction_configurations = ((KernelCorrection(), nothing, true), - (KernelCorrection(), KernelCorrection(), true), - (BlendedGradientCorrection(0.3), - BlendedGradientCorrection(0.3), true), - (nothing, KernelCorrection(), false), - (KernelCorrection(), GradientCorrection(), false), - (BlendedGradientCorrection(0.3), - BlendedGradientCorrection(0.7), false)) + # Corrections at the boundary particles of structures are not supported yet, + # independently of the fluid correction and of whether a fluid is present. + boundary_corrections = (ShepardKernelCorrection(), + AkinciFreeSurfaceCorrection(1000.0), + KernelCorrection(), GradientCorrection(), + BlendedGradientCorrection(0.3), + MixedKernelGradientCorrection()) @testset "Corrections, $structure_type" for structure_type in (TotalLagrangianSPHSystem, RigidBodySystem) - for (correction, boundary_correction, compatible) in - correction_configurations - - fluid = WeaklyCompressibleSPHSystem(ic; smoothing_kernel, - smoothing_length, state_equation, - density_calculator=ContinuityDensity(), - correction) + fluid = WeaklyCompressibleSPHSystem(ic; smoothing_kernel, + smoothing_length, state_equation, + density_calculator=ContinuityDensity(), + correction=KernelCorrection()) + boundary_model = BoundaryModelDummyParticles(ic.density, ic.mass, + AdamiPressureExtrapolation(), + smoothing_kernel, + smoothing_length; + state_equation) + structure = create_structure_system(structure_type, boundary_model) + @test_nowarn Semidiscretization(fluid, structure; + neighborhood_search=nothing) + + error_str = "the boundary model of a `$structure_type` does not " * + "support corrections yet. Use `correction=nothing`." + for boundary_correction in boundary_corrections boundary_model = BoundaryModelDummyParticles(ic.density, ic.mass, AdamiPressureExtrapolation(), smoothing_kernel, @@ -251,26 +257,11 @@ state_equation, correction=boundary_correction) structure = create_structure_system(structure_type, boundary_model) - error_str = "the correction of the boundary model of the " * - "`$structure_type` must be `nothing` or the same " * - "as that of the fluid system `WeaklyCompressibleSPHSystem`." - if compatible - @test_nowarn Semidiscretization(fluid, structure; - neighborhood_search=nothing) - else - @test_throws ArgumentError(error_str) Semidiscretization(fluid, - structure; - neighborhood_search=nothing) - end - - # A correction matching one fluid must also match any other fluid. - if correction isa KernelCorrection && - boundary_correction isa KernelCorrection - @test_throws ArgumentError(error_str) Semidiscretization(fluid, - fluid_systems[1], - structure; - neighborhood_search=nothing) - end + @test_throws ArgumentError(error_str) Semidiscretization(fluid, + structure; + neighborhood_search=nothing) + @test_throws ArgumentError(error_str) Semidiscretization(structure; + neighborhood_search=nothing) end end end diff --git a/test/schemes/boundary/dummy_particles/dummy_particles.jl b/test/schemes/boundary/dummy_particles/dummy_particles.jl index ef997a1ed6..b781668323 100644 --- a/test/schemes/boundary/dummy_particles/dummy_particles.jl +++ b/test/schemes/boundary/dummy_particles/dummy_particles.jl @@ -67,6 +67,34 @@ @test boundary_model_adami_no_clip.pressure[1] == -1 end + # The correction values must have the element type of the density. Otherwise, the + # kernel gradients at the boundary particles would change the precision of the + # pressure acceleration of a `Float32` simulation, e.g., on GPUs. + @testset "Correction Cache Element Type `$(nameof(typeof(correction)))`" for correction in + (ShepardKernelCorrection(), + KernelCorrection(), + GradientCorrection(), + BlendedGradientCorrection(0.5f0), + MixedKernelGradientCorrection()) + smoothing_kernel = SchoenbergCubicSplineKernel{2}() + boundary_model = BoundaryModelDummyParticles([1000.0f0, 1000.0f0], [1.0f0, 1.0f0], + AdamiPressureExtrapolation(), + smoothing_kernel, 0.1f0; correction) + (; cache) = boundary_model + + for field in (:kernel_correction_coefficient, :dw_gamma, :correction_matrix) + haskey(cache, field) || continue + @test eltype(getproperty(cache, field)) == Float32 + fill!(getproperty(cache, field), 1) + end + + pos_diff = SVector(0.05f0, 0.02f0) + grad_kernel = TrixiParticles.smoothing_kernel_grad(boundary_model, pos_diff, + norm(pos_diff), 1) + @test eltype(grad_kernel) == Float32 + @test !iszero(grad_kernel) + end + @testset "Bernoulli dynamic pressure" begin smoothing_kernel = SchoenbergCubicSplineKernel{2}() smoothing_length = 1.0 diff --git a/test/schemes/fluid/rhs.jl b/test/schemes/fluid/rhs.jl index da624a6b92..ada863fe0e 100644 --- a/test/schemes/fluid/rhs.jl +++ b/test/schemes/fluid/rhs.jl @@ -622,88 +622,6 @@ end end - # With zero boundary pressure, the pressure force only uses the fluid gradient, - # so a correction of the structure's boundary model must not change it. - # The continuity equation of the boundary particles must use the boundary model - # including its correction. - boundary_corrections = (ShepardKernelCorrection(), - AkinciFreeSurfaceCorrection(fluid_density), - KernelCorrection()) - - @testset "Boundary Model Correction `$(nameof(typeof(boundary_correction)))`" for boundary_correction in - boundary_corrections - - viscosity = ViscosityAdami(nu=0.1) - velocity = (1.0, 0.5) - density = 1005.0 - - # Isolate the fluid contribution; complete structure neighborhoods are - # addressed separately in #1351. - kernel_value = 5 / (112pi) # `W(1.5)` - prescribed_coefficient = 2.0 - prescribed_dw_gamma = SVector(0.2, 0.0) - - forces = map((nothing, boundary_correction)) do boundary_correction_ - density_calculator = boundary_correction isa ShepardKernelCorrection ? - SummationDensity() : ContinuityDensity() - fluid_system = create_fluid_system("WCSPH", velocity, density, viscosity; - correction=boundary_correction, - density_calculator) - structure_system = create_structure_system(RigidBodySystem, viscosity; - boundary_density=ContinuityDensity(), - boundary_correction=boundary_correction_) - # Make sure that the correction values are computed - cache = structure_system.boundary_model.cache - haskey(cache, :kernel_correction_coefficient) && - (cache.kernel_correction_coefficient .= NaN) - haskey(cache, :dw_gamma) && (cache.dw_gamma .= NaN) - (fluid, structure, arrays, - semi, - _) = initialize(fluid_system, structure_system; - interaction_matrix=[true true; true false]) - - if boundary_correction_ isa ShepardKernelCorrection - volume_fluid = fluid.mass[1] / - TrixiParticles.current_density(arrays[1], fluid, 1) - coefficient = volume_fluid * kernel_value - @test structure.boundary_model.cache.kernel_correction_coefficient ≈ - [coefficient] - elseif boundary_correction_ isa KernelCorrection - # Prescribe nonzero correction data to test continuity independently - # of the single-neighbor normalization, whose gradient vanishes. - structure.boundary_model.cache.kernel_correction_coefficient .= prescribed_coefficient - structure.boundary_model.cache.dw_gamma .= prescribed_dw_gamma - end - - current_fluid_density = TrixiParticles.current_density(arrays[1], fluid, 1) - return (pair_forces(fluid, structure, arrays, semi)..., fluid, structure, - current_fluid_density) - end - force_fluid_uncorrected = forces[1][1] - (force_fluid, force_structure, dv_structure, fluid, structure, - current_fluid_density) = forces[2] - - @test force_fluid == force_fluid_uncorrected - @test isapprox(force_structure, -force_fluid, - rtol=sqrt(eps()), atol=sqrt(eps())) - - # Gradient of the cubic spline kernel at the displacement `(1.5, 0)` - kernel_derivative = -0.75 * (2 - 1.5)^2 * 10 / (7pi) - grad_kernel_x = kernel_derivative - if boundary_correction isa KernelCorrection - # `(∇W - W γ) / c` using the prescribed correction data - grad_kernel_x = (kernel_derivative - kernel_value * prescribed_dw_gamma[1]) / - prescribed_coefficient - @test !isapprox(grad_kernel_x, kernel_derivative, rtol=0.1) - end - # With summation density, use the time derivative of the density summation. - density_factor = fluid.density_calculator isa SummationDensity ? 1.0 : - fluid_density / current_fluid_density - expected_density_rate = -density_factor * fluid.mass[1] * velocity[1] * - grad_kernel_x - @test dv_structure[end, 1] ≈ expected_density_rate - end - # TLSPH does not support `ContinuityDensity` boundaries yet. However, the continuity # equation of structure boundaries must already use the kernel of the boundary # model and not the TLSPH kernel for solid mechanics, which has a smaller support. @@ -738,30 +656,23 @@ @test drho ≈ expected_density_rate end - # Check that FSI pressure forces use the boundary model's kernel and correction. - # Place three fluid particles inside the boundary kernel's support but outside - # the TLSPH kernel's support. Using the TLSPH kernel would give zero boundary - # gradients for all three neighbors. + # Check that FSI pressure forces use the kernel and smoothing length of the boundary + # model at the structure particles. Place three fluid particles inside the support + # of the boundary kernel but outside the support of the TLSPH kernel. Using the + # TLSPH kernel would give zero boundary gradients for all three neighbors. # - # The particles are not on a line, giving a non-identity correction matrix. - # Fill the boundary caches with NaN before initialization, then compare the - # fluid force and the structure's total reaction force with an independent - # calculation using cubic-spline values. Set the fluid correction data explicitly - # so this comparison checks the initialization and use of the boundary data. + # The particles are not on a line. Compare the fluid force and the structure's total + # reaction force with an independent calculation using cubic-spline values. + # Set the fluid correction data explicitly, so that this comparison only checks + # the gradient of the boundary model, which is uncorrected. @testset "Boundary kernel in pressure acceleration with corrected gradients" begin coordinates = [0.0 1.5 2.4; 0.0 -1.0 1.2] - displacements = (SVector(1.5, 0.0), SVector(0.0, 1.0), SVector(-0.9, -1.2)) kernel_values = (5 / (112pi), 5 / (14pi), 5 / (112pi)) + # Kernel gradients at the structure particle for the displacements + # `(1.5, 0)`, `(0, 1)` and `(-0.9, -1.2)` to the three fluid particles gradients = (SVector(-15 / (56pi), 0.0), SVector(0.0, -15 / (14pi)), SVector(9 / (56pi), 12 / (56pi))) volume = fluid_density / 1005.0 - # Only fluid neighbors contribute in this test; complete structure - # neighborhoods are addressed separately in #1351. - coefficient = volume * sum(kernel_values) - dw_gamma = volume * sum(gradients) / coefficient - normalized_gradients = map(gradients, kernel_values) do gradient, value - (gradient - value * dw_gamma) / coefficient - end for structure_type in structure_types, correction in (KernelCorrection(), GradientCorrection(), @@ -769,12 +680,6 @@ kernel_correction = correction isa Union{KernelCorrection, MixedKernelGradientCorrection} - boundary_gradients = kernel_correction ? normalized_gradients : gradients - moment = zeros(2, 2) - for (gradient, pos_diff) in zip(boundary_gradients, displacements) - moment .-= volume * gradient * transpose(pos_diff) - end - boundary_matrix = inv(moment) fluid_ic = InitialCondition(; coordinates, density=1005.0, mass=fill(fluid_density, 3), particle_spacing) @@ -786,23 +691,12 @@ # model (2) and outside the compact support of the smoothing kernel of the # `TotalLagrangianSPHSystem` (2 * 0.4). structure_system = create_structure_system(structure_type, nothing; - boundary_correction=correction, structure_smoothing_kernel=WendlandC2Kernel{2}(), structure_smoothing_length=0.4) - cache = structure_system.boundary_model.cache - cache.density .= 1005.0 - if kernel_correction - cache.kernel_correction_coefficient .= NaN - cache.dw_gamma .= NaN - end - if haskey(cache, :correction_matrix) - cache.correction_matrix .= NaN - end fluid, structure, arrays, semi, - _ = initialize(fluid_system, structure_system; - interaction_matrix=[true true; true false]) + _ = initialize(fluid_system, structure_system) - # Prescribe fluid correction data to isolate boundary initialization. + # Prescribe fluid correction data to isolate the boundary gradient fluid_coefficient = 2.0 fluid_dw_gamma = SVector(0.2, -0.3) fluid_matrix = [2.0 0.3; 0.4 1.5] @@ -818,25 +712,20 @@ force_fluid, force_structure = pair_forces(fluid, structure, arrays, semi) # Independent pressure forces from the cubic-spline values above. - expected_forces = map(gradients, kernel_values, - boundary_gradients) do gradient, - value, - boundary_gradient + # The boundary gradient is uncorrected, the fluid gradient is corrected. + expected_forces = map(gradients, kernel_values) do gradient, value fluid_gradient = kernel_correction ? (-gradient - value * fluid_dw_gamma) / fluid_coefficient : -gradient if correction isa Union{GradientCorrection, MixedKernelGradientCorrection} fluid_gradient = fluid_matrix * fluid_gradient - boundary_gradient = boundary_matrix * boundary_gradient elseif correction isa BlendedGradientCorrection lambda = correction.blending_factor fluid_gradient = (1 - lambda) * fluid_gradient + lambda * fluid_matrix * fluid_gradient - boundary_gradient = (1 - lambda) * boundary_gradient + - lambda * boundary_matrix * boundary_gradient end - 500.0 * volume^2 * (boundary_gradient - fluid_gradient) + 500.0 * volume^2 * (gradient - fluid_gradient) end @test force_fluid ≈ first(expected_forces) @test force_structure ≈ -sum(expected_forces) @@ -852,15 +741,15 @@ # Check the pressure forces when a fluid particle and a structure particle are # at the same position (coincident particles), or less than `almostzero(h)` apart. # - # The uncorrected kernel gradient is treated as zero at these distances. With - # `KernelCorrection`, the boundary gradient can still be nonzero. Set the fluid - # gradient to zero to isolate the boundary's contribution to the pressure force. - # The fluid and structure must then experience equal and opposite forces. - # For the other configurations tested here, both forces must be zero. + # The uncorrected kernel gradient is treated as zero at these distances, which + # includes the gradient of the boundary model of the structure. With + # `KernelCorrection`, the fluid gradient can still be nonzero. Prescribe fluid + # correction data with a nonzero gradient at zero distance. The fluid and structure + # must then experience equal and opposite forces. For the other configurations + # tested here, pairs at the same position are skipped and both forces must be zero. # - # For rigid bodies, use `ContinuityDensity` and also check the boundary-density - # derivative. With `KernelCorrection`, include a separation just above the - # zero-distance threshold to check continuity across it. + # For rigid bodies, use `ContinuityDensity` and also check that the boundary-density + # derivative, which uses the uncorrected gradient of the boundary model, vanishes. @testset "Coincident FSI pressure and boundary continuity" begin velocity = SVector(1.0, -0.5) density = 1005.0 @@ -868,10 +757,8 @@ kernel_zero = 10 / (7pi) coefficient = 2.0 dw_gamma = SVector(-0.5, -0.5) - boundary_gradient = -kernel_zero * dw_gamma / coefficient - # Cover matching corrections and uncorrected WCSPH and EDAC. Prescribe a zero - # fluid gradient with `KernelCorrection` to isolate the nonzero boundary - # gradient. The other configurations skip pairs at the same position. + # Kernel-corrected fluid gradient `(∇W - W γ) / c` at zero distance + fluid_gradient = -kernel_zero * dw_gamma / coefficient near_zero_configurations = (("WCSPH", KernelCorrection()), ("WCSPH", GradientCorrection()), ("WCSPH", BlendedGradientCorrection(0.3)), @@ -879,17 +766,10 @@ ("EDAC", nothing), ("EDAC with average pressure reduction", nothing)) almostzero = TrixiParticles.almostzero(smoothing_length) - distances = (0.0, almostzero / 2, 10 * almostzero) + distances = (0.0, almostzero / 2) for (structure_type, (scheme, correction), distance) in Iterators.product(structure_types, near_zero_configurations, distances) - - # The above-threshold case only tests the boundary-density derivative. - if distance >= almostzero && - !(structure_type === RigidBodySystem && correction isa KernelCorrection) - continue - end - fluid_coordinates = reshape([1.5 + distance, 0.0], 2, 1) fluid_system = create_fluid_system(scheme, velocity, density, nothing; correction, @@ -898,39 +778,28 @@ ContinuityDensity() : AdamiPressureExtrapolation() structure_system = create_structure_system(structure_type, nothing; boundary_density, - boundary_correction=correction, boundary_initial_density=density) fluid, structure, arrays, semi, _ = initialize(fluid_system, structure_system) - # Cache assembly is covered above; prescribe a nonzero boundary gradient - # and a zero fluid gradient to isolate pressure at coincident pairs. if correction isa KernelCorrection fluid.cache.kernel_correction_coefficient .= coefficient - fluid.cache.dw_gamma .= 0 - structure.boundary_model.cache.kernel_correction_coefficient .= coefficient - structure.boundary_model.cache.dw_gamma .= dw_gamma + fluid.cache.dw_gamma .= dw_gamma end (force_fluid, force_structure, dv_structure) = pair_forces(fluid, structure, arrays, semi) - if distance < almostzero - # Use the EOS value to account for rounding in the initial pressure. - pressure = state_equation(density) - expected = correction isa KernelCorrection ? - pressure * volume^2 * boundary_gradient : - zero(boundary_gradient) - @test isapprox(force_fluid, expected, rtol=1e-14, atol=1e-14) - @test isapprox(force_structure, -expected, rtol=1e-14, atol=1e-14) - end - - if structure isa RigidBodySystem && correction isa KernelCorrection - # Both particles have density `density`, so their density ratio is one. - # Compare against the zero-distance limit, including just above the - # threshold, where the uncorrected gradient is small but nonzero. - expected_density_rate = -fluid.mass[1] * - dot(velocity, boundary_gradient) - @test isapprox(dv_structure[end, 1], expected_density_rate, rtol=1e-5) + # Use the EOS value to account for rounding in the initial pressure. + # With a vanishing boundary gradient, the pair force on the fluid is + # `-m_a m_b / (ρ_a ρ_b) p_a ∇W_a`. + pressure = state_equation(density) + expected = correction isa KernelCorrection ? + -pressure * volume^2 * fluid_gradient : zero(fluid_gradient) + @test isapprox(force_fluid, expected, rtol=1e-14, atol=1e-14) + @test isapprox(force_structure, -expected, rtol=1e-14, atol=1e-14) + + if structure isa RigidBodySystem + @test iszero(dv_structure[end, 1]) end end end diff --git a/test/schemes/fluid/weakly_compressible_sph/density_diffusion.jl b/test/schemes/fluid/weakly_compressible_sph/density_diffusion.jl index 7dafc0377e..80aca25d61 100644 --- a/test/schemes/fluid/weakly_compressible_sph/density_diffusion.jl +++ b/test/schemes/fluid/weakly_compressible_sph/density_diffusion.jl @@ -6,5 +6,63 @@ @test repr(density_diffusion) == "DensityDiffusionAntuono(0.1)" end + + # The renormalized density gradient is defined with the uncorrected kernel gradient. + # It must neither depend on the correction of the fluid nor on its correction + # values, which are only computed after the density diffusion in each update. + @trixi_testset "Uncorrected Kernel Gradient" begin + # Irregular particle positions with a nonlinear density field + coordinates = [0.0 0.8 1.7 0.3 1.1 2.1 0.6; 0.0 0.1 0.2 0.9 1.0 1.2 1.8] + density = [1000.0, 1010.0, 990.0, 1005.0, 995.0, 1000.0, 1002.0] + initial_condition = InitialCondition(; coordinates, density, mass=1.0, + particle_spacing=1.0) + state_equation = StateEquationCole(; sound_speed=10.0, + reference_density=1000.0, exponent=1) + density_diffusion = DensityDiffusionAntuono(delta=0.1) + + corrections = (nothing, KernelCorrection(), GradientCorrection(), + BlendedGradientCorrection(0.3), MixedKernelGradientCorrection()) + results = map(corrections) do correction + system = WeaklyCompressibleSPHSystem(initial_condition; + smoothing_kernel=SchoenbergCubicSplineKernel{2}(), + smoothing_length=1.0, state_equation, + density_calculator=ContinuityDensity(), + density_diffusion, correction) + ode = semidiscretize(Semidiscretization(system; + parallelization_backend=SerialBackend()), + (0.0, 0.01)) + semi = ode.p.semi + system = semi.systems[1] + v_ode, u_ode = ode.u0.x + + # The correction values of the fluid must not be used + for field in (:kernel_correction_coefficient, :dw_gamma, + :correction_matrix) + haskey(system.cache, field) || continue + fill!(getproperty(system.cache, field), NaN) + end + + TrixiParticles.update!(density_diffusion, + TrixiParticles.wrap_v(v_ode, system, semi), + TrixiParticles.wrap_u(u_ode, system, semi), + system, semi) + + return (copy(system.cache.density_diffusion_correction_matrix), + copy(system.cache.density_diffusion_normalized_density_gradient)) + end + + matrix, normalized_gradient = first(results) + @test all(isfinite, matrix) + @test all(isfinite, normalized_gradient) + @test !iszero(normalized_gradient) + + @testset "$(nameof(typeof(correction)))" for (correction, + result) in + zip(corrections[2:end], + results[2:end]) + @test result[1] == matrix + @test result[2] == normalized_gradient + end + end end end From e8a197da926cd89cc3f297a4f2f3ddf55687f9ce Mon Sep 17 00:00:00 2001 From: Sven Berger Date: Fri, 13 Nov 2026 12:24:23 +0100 Subject: [PATCH 09/11] remove support for continuitydensity in dummy bnds of tlshp structures --- docs/src/systems/boundary.md | 1 + docs/src/systems/total_lagrangian_sph.md | 2 + src/schemes/structure/structure.jl | 60 ++++--------------- .../structure/total_lagrangian_sph/rhs.jl | 3 - .../structure/total_lagrangian_sph/system.jl | 42 +++---------- test/general/semidiscretization.jl | 13 ---- test/schemes/fluid/rhs.jl | 36 +---------- test/systems/tlsph_system.jl | 13 ++++ 8 files changed, 36 insertions(+), 134 deletions(-) diff --git a/docs/src/systems/boundary.md b/docs/src/systems/boundary.md index 711c22a575..cc583121f2 100644 --- a/docs/src/systems/boundary.md +++ b/docs/src/systems/boundary.md @@ -71,6 +71,7 @@ We provide six options to compute the boundary density and pressure, determined and the pressure is computed from the density with the state equation. 4. With [`ContinuityDensity`](@ref), the density is integrated from the continuity equation, and the pressure is computed from the density with the state equation. + This option is not supported for a [`TotalLagrangianSPHSystem`](@ref). Note that this causes a gap between fluid and boundary where the boundary is initialized without any contact to the fluid. This is due to overestimation of the boundary density as soon as the fluid comes in contact with boundary particles that initially did not have diff --git a/docs/src/systems/total_lagrangian_sph.md b/docs/src/systems/total_lagrangian_sph.md index c5d90dfd86..09e078399f 100644 --- a/docs/src/systems/total_lagrangian_sph.md +++ b/docs/src/systems/total_lagrangian_sph.md @@ -64,6 +64,8 @@ smoothing kernel and smoothing length (see [Boundary Models](@ref boundary_model When using [`BoundaryModelDummyParticles`](@ref), its smoothing kernel and smoothing length must match those of every fluid system. Its `correction` must be `nothing` (see [Boundary Corrections](@ref boundary_corrections)). +[`ContinuityDensity`](@ref) is not supported for the boundary model of a +[`TotalLagrangianSPHSystem`](@ref). ```@autodocs Modules = [TrixiParticles] diff --git a/src/schemes/structure/structure.jl b/src/schemes/structure/structure.jl index 6cb0291ff4..c856142fbb 100644 --- a/src/schemes/structure/structure.jl +++ b/src/schemes/structure/structure.jl @@ -214,76 +214,36 @@ end neighbor_system::AbstractFluidSystem, particle, neighbor, pos_diff, distance, m_b, rho_a, rho_b, v_a, v_b, grad_kernel_fluid, - grad_kernel_boundary=nothing) + grad_kernel_boundary) return drho_particle end # `grad_kernel_boundary` is the kernel gradient of the boundary model at the structure # particle if it has already been computed for the pressure acceleration, or `nothing`. @inline function add_continuity_equation(drho_particle, - particle_system::Union{RigidBodySystem{<:BoundaryModelDummyParticles{ContinuityDensity}}, - TotalLagrangianSPHSystem{<:BoundaryModelDummyParticles{ContinuityDensity}}}, + particle_system::RigidBodySystem{<:BoundaryModelDummyParticles{ContinuityDensity}}, neighbor_system::AbstractFluidSystem, particle, neighbor, pos_diff, distance, m_b, rho_a, rho_b, v_a, v_b, grad_kernel_fluid, - grad_kernel_boundary=nothing) - (; boundary_model) = particle_system - - # As for a `WallBoundarySystem`, the density of the boundary particles evolves with - # the kernel gradient of the boundary model (including its correction) - # and not with the corrected kernel gradient of the fluid. - grad_kernel = boundary_continuity_kernel_grad(system_correction(neighbor_system), - system_correction(boundary_model), - boundary_model, pos_diff, distance, - particle, grad_kernel_fluid, - grad_kernel_boundary) + grad_kernel_boundary) + # The boundary model is uncorrected and its kernel and smoothing length match the + # fluid's (see `check_boundary_model_correction` and `check_boundary_model_fsi`). + # If the fluid uses an asymmetric corrected gradient, reuse the boundary gradient + # computed for the pressure acceleration. Otherwise, reverse the fluid gradient. + grad_kernel = isnothing(grad_kernel_boundary) ? -grad_kernel_fluid : + grad_kernel_boundary return add_continuity_equation(drho_particle, density_calculator(neighbor_system), m_b, rho_a, rho_b, v_a, v_b, grad_kernel, particle) end -@inline function boundary_continuity_kernel_grad(fluid_correction, boundary_correction, - boundary_model, pos_diff, distance, - particle, grad_kernel_fluid, - grad_kernel_boundary) - # Reuse the kernel gradient of the boundary model from the pressure acceleration - return grad_kernel_boundary -end - -@inline function boundary_continuity_kernel_grad(fluid_correction, boundary_correction, - boundary_model, pos_diff, distance, - particle, grad_kernel_fluid, - grad_kernel_boundary::Nothing) - # Use the kernel, smoothing length and correction of the boundary model. The kernel of - # a `TotalLagrangianSPHSystem` for solid mechanics may be different. - # Use the safe version, which handles particles at the same position. The gradient of - # `KernelCorrection` and `MixedKernelGradientCorrection` does not vanish there. - return smoothing_kernel_grad(boundary_model, pos_diff, distance, particle) -end - -@inline function boundary_continuity_kernel_grad(::Union{Nothing, - AkinciFreeSurfaceCorrection, - ShepardKernelCorrection}, - ::Union{Nothing, - AkinciFreeSurfaceCorrection, - ShepardKernelCorrection}, - boundary_model, pos_diff, distance, - particle, grad_kernel_fluid, - grad_kernel_boundary::Nothing) - # Neither correction changes the gradient, and the kernel and smoothing length of the - # boundary model match the ones of the fluid (see `check_boundary_model_fsi`). - # Reversing the displacement only flips the sign of the uncorrected gradient. - return -grad_kernel_fluid -end - @inline function write_drho_particle!(dv, ::AbstractSystem, drho_particle, particle) return dv end @propagate_inbounds function write_drho_particle!(dv, - ::Union{RigidBodySystem{<:BoundaryModelDummyParticles{ContinuityDensity}}, - TotalLagrangianSPHSystem{<:BoundaryModelDummyParticles{ContinuityDensity}}}, + ::RigidBodySystem{<:BoundaryModelDummyParticles{ContinuityDensity}}, drho_particle, particle) dv[end, particle] += drho_particle diff --git a/src/schemes/structure/total_lagrangian_sph/rhs.jl b/src/schemes/structure/total_lagrangian_sph/rhs.jl index 49f29db990..1b882f763d 100644 --- a/src/schemes/structure/total_lagrangian_sph/rhs.jl +++ b/src/schemes/structure/total_lagrangian_sph/rhs.jl @@ -93,8 +93,6 @@ end for i in 1:ndims(system) @inbounds dv[i, particle] += dv_particle_[i] end - - # TODO continuity equation for boundary model with `ContinuityDensity`? end return dv @@ -117,6 +115,5 @@ function interact!(dv, v_particle_system, u_particle_system, particle_system::TotalLagrangianSPHSystem, neighbor_system::Union{WallBoundarySystem, OpenBoundarySystem}, semi; eachparticle=each_integrated_particle(particle_system)) - # TODO continuity equation? return dv end diff --git a/src/schemes/structure/total_lagrangian_sph/system.jl b/src/schemes/structure/total_lagrangian_sph/system.jl index b319f945ab..6dba89367e 100644 --- a/src/schemes/structure/total_lagrangian_sph/system.jl +++ b/src/schemes/structure/total_lagrangian_sph/system.jl @@ -33,9 +33,9 @@ See [Total Lagrangian SPH](@ref tlsph) for more details on the method. See [`PrescribedMotion`](@ref) for details. - `boundary_model`: Boundary model to compute the hydrodynamic density and pressure for fluid-structure interaction (see [Boundary Models](@ref boundary_models)). - For [`BoundaryModelDummyParticles`](@ref), `correction` must be `nothing`, - since corrections at the boundary particles of structures are not - supported yet. + For [`BoundaryModelDummyParticles`](@ref), [`ContinuityDensity`](@ref) + is not supported and `correction` must be `nothing`, since corrections + at the boundary particles of structures are not supported yet. - `penalty_force`: Penalty force to ensure regular particle position under large deformations (see [`PenaltyForceGanzenmueller`](@ref)). - `viscosity`: Artificial viscosity model to stabilize both the TLSPH and the FSI. @@ -124,6 +124,11 @@ function TotalLagrangianSPHSystem(initial_condition; smoothing_kernel, smoothing throw(ArgumentError("smoothing kernel dimensionality must be $NDIMS for a $(NDIMS)D problem")) end + if boundary_model isa BoundaryModelDummyParticles{ContinuityDensity} + throw(ArgumentError("`BoundaryModelDummyParticles` with density calculator " * + "`ContinuityDensity` is not supported for a `TotalLagrangianSPHSystem`")) + end + # Make acceleration an SVector acceleration_ = SVector(acceleration...) if length(acceleration_) != NDIMS @@ -297,10 +302,6 @@ end return ndims(system) end -@inline function v_nvariables(system::TotalLagrangianSPHSystem{<:BoundaryModelDummyParticles{ContinuityDensity}}) - return ndims(system) + 1 -end - @inline function n_integrated_particles(system::TotalLagrangianSPHSystem) system.n_integrated_particles end @@ -631,31 +632,12 @@ function write_u0!(u0, system::TotalLagrangianSPHSystem) end function write_v0!(v0, system::TotalLagrangianSPHSystem) - (; initial_condition, boundary_model) = system + (; initial_condition) = system # This is as fast as a loop with `@inbounds`, but it's GPU-compatible indices = CartesianIndices((ndims(system), each_integrated_particle(system))) copyto!(v0, indices, initial_condition.velocity, indices) - write_v0!(v0, boundary_model, system) - - return v0 -end - -function write_v0!(v0, model, system::TotalLagrangianSPHSystem) - return v0 -end - -function write_v0!(v0, ::BoundaryModelDummyParticles{ContinuityDensity}, - system::TotalLagrangianSPHSystem) - (; cache) = system.boundary_model - (; initial_density) = cache - - for particle in each_integrated_particle(system) - # Set particle densities - v0[ndims(system) + 1, particle] = initial_density[particle] - end - return v0 end @@ -866,12 +848,6 @@ function check_configuration(system::TotalLagrangianSPHSystem, systems, nhs) end end - if boundary_model isa BoundaryModelDummyParticles && - boundary_model.density_calculator isa ContinuityDensity - throw(ArgumentError("`BoundaryModelDummyParticles` with density calculator " * - "`ContinuityDensity` is not yet supported for a `TotalLagrangianSPHSystem`")) - end - check_boundary_model_correction(system, boundary_model) foreach_system(systems) do neighbor diff --git a/test/general/semidiscretization.jl b/test/general/semidiscretization.jl index 0612b296cb..026fe1992f 100644 --- a/test/general/semidiscretization.jl +++ b/test/general/semidiscretization.jl @@ -188,19 +188,6 @@ fluid_system; neighborhood_search=nothing) - if structure_type === TotalLagrangianSPHSystem - boundary_model = BoundaryModelDummyParticles(ic.density, ic.mass, - ContinuityDensity(), - smoothing_kernel, - smoothing_length) - structure = create_structure_system(structure_type, boundary_model) - error_str = "`BoundaryModelDummyParticles` with density calculator " * - "`ContinuityDensity` is not yet supported for a `TotalLagrangianSPHSystem`" - @test_throws ArgumentError(error_str) Semidiscretization(structure, - fluid_system; - neighborhood_search=nothing) - end - @testset "$name" for (name, boundary_kernel, boundary_length, compatible) in boundary_configurations diff --git a/test/schemes/fluid/rhs.jl b/test/schemes/fluid/rhs.jl index ada863fe0e..87a1689ef4 100644 --- a/test/schemes/fluid/rhs.jl +++ b/test/schemes/fluid/rhs.jl @@ -460,7 +460,7 @@ structure_smoothing_length) in structure_configurations - # TLSPH does not yet support `ContinuityDensity` boundaries. + # TLSPH does not support `ContinuityDensity` boundaries. boundary_densities = if structure_type === TotalLagrangianSPHSystem (AdamiPressureExtrapolation(), SummationDensity()) else @@ -622,40 +622,6 @@ end end - # TLSPH does not support `ContinuityDensity` boundaries yet. However, the continuity - # equation of structure boundaries must already use the kernel of the boundary - # model and not the TLSPH kernel for solid mechanics, which has a smaller support. - @testset "TLSPH Continuity Equation with Correction `$(nameof(typeof(correction)))`" for correction in - (nothing, - GradientCorrection()) - velocity = (1.0, 0.5) - density = 1005.0 - structure = create_structure_system(TotalLagrangianSPHSystem, nothing; - boundary_density=ContinuityDensity(), - structure_smoothing_length=0.5 * - smoothing_length) - fluid = create_fluid_system("WCSPH", velocity, density, nothing; correction) - if haskey(fluid.cache, :correction_matrix) - fluid.cache.correction_matrix[:, :, 1] .= [2.0 0.3; 0.4 1.5] - end - - pos_diff = SVector(1.5, 0.0) - grad_kernel_fluid = TrixiParticles.smoothing_kernel_grad_unsafe(fluid, - -pos_diff, - 1.5, 1) - drho = TrixiParticles.add_continuity_equation(0.0, structure, fluid, 1, 1, - pos_diff, 1.5, fluid.mass[1], - fluid_density, density, - SVector(0.0, 0.0), - SVector(velocity), - grad_kernel_fluid) - - kernel_derivative = -0.75 * (2 - 1.5)^2 * 10 / (7pi) - expected_density_rate = -fluid_density / density * fluid.mass[1] * - velocity[1] * kernel_derivative - @test drho ≈ expected_density_rate - end - # Check that FSI pressure forces use the kernel and smoothing length of the boundary # model at the structure particles. Place three fluid particles inside the support # of the boundary kernel but outside the support of the TLSPH kernel. Using the diff --git a/test/systems/tlsph_system.jl b/test/systems/tlsph_system.jl index b39f0a6b2a..e346380ccc 100644 --- a/test/systems/tlsph_system.jl +++ b/test/systems/tlsph_system.jl @@ -43,6 +43,19 @@ @test TrixiParticles.initial_smoothing_length(system) == smoothing_length @test system.acceleration == [0.0 for _ in 1:NDIMS] @test system.boundary_model == boundary_model + + boundary_model = BoundaryModelDummyParticles(material_densities, mass, + ContinuityDensity(), + SchoenbergCubicSplineKernel{NDIMS}(), + smoothing_length) + error_str = "`BoundaryModelDummyParticles` with density calculator " * + "`ContinuityDensity` is not supported for a `TotalLagrangianSPHSystem`" + @test_throws ArgumentError(error_str) TotalLagrangianSPHSystem(initial_condition; + smoothing_kernel, + smoothing_length, + young_modulus=E, + poisson_ratio=nu, + boundary_model) end end From a09ff8fba5f1210d542af39a54d6ffd5198e2af4 Mon Sep 17 00:00:00 2001 From: Sven Berger Date: Fri, 13 Nov 2026 12:46:13 +0100 Subject: [PATCH 10/11] address review comments --- docs/src/general/smoothing_kernels.md | 4 ++-- src/schemes/fluid/entropically_damped_sph/rhs.jl | 2 ++ src/schemes/fluid/weakly_compressible_sph/rhs.jl | 2 ++ src/schemes/structure/structure.jl | 11 +++++++---- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/docs/src/general/smoothing_kernels.md b/docs/src/general/smoothing_kernels.md index d9b64157a8..98c9c00ae6 100644 --- a/docs/src/general/smoothing_kernels.md +++ b/docs/src/general/smoothing_kernels.md @@ -203,8 +203,8 @@ CairoMakie.save("smoothing_kernels.png", fig) ``` where `skip_zero_distance` returns `true` if the kernel gradient is zero at zero distance, which is not the case for some corrected kernels. - Here, `almostzero(h)` is the threshold `sqrt(eps(typeof(h))) * h`, where `h` is the - smoothing length. + Here, `almostzero(h)` is the threshold + `sqrt(eps(typeof(h))) * h ≈ sqrt(eps(h^2))`, where `h` is the smoothing length. ```@autodocs Modules = [TrixiParticles] diff --git a/src/schemes/fluid/entropically_damped_sph/rhs.jl b/src/schemes/fluid/entropically_damped_sph/rhs.jl index 8a9e32a320..45dabb2d8e 100644 --- a/src/schemes/fluid/entropically_damped_sph/rhs.jl +++ b/src/schemes/fluid/entropically_damped_sph/rhs.jl @@ -22,6 +22,8 @@ function interact!(dv, v_particle_system, u_particle_system, # Note that `return` only exits the closure, i.e., skips the current neighbor. skip_zero_distance(particle_system) && distance < almostzero(h) && return + # Now we can safely call the unsafe version of the kernel gradient to avoid + # redundant zero checks. grad_kernel = smoothing_kernel_grad_unsafe(particle_system, pos_diff, distance, particle) diff --git a/src/schemes/fluid/weakly_compressible_sph/rhs.jl b/src/schemes/fluid/weakly_compressible_sph/rhs.jl index 03357bdb78..db64278176 100644 --- a/src/schemes/fluid/weakly_compressible_sph/rhs.jl +++ b/src/schemes/fluid/weakly_compressible_sph/rhs.jl @@ -53,6 +53,8 @@ function interact!(dv, v_particle_system, u_particle_system, return init end + # Now we can safely call the unsafe version of the kernel gradient to avoid + # redundant zero checks. grad_kernel = smoothing_kernel_grad_unsafe(particle_system, pos_diff, distance, particle) diff --git a/src/schemes/structure/structure.jl b/src/schemes/structure/structure.jl index c856142fbb..a48d2654d3 100644 --- a/src/schemes/structure/structure.jl +++ b/src/schemes/structure/structure.jl @@ -139,9 +139,12 @@ function interact_structure_fluid!(dv, v_particle_system, u_particle_system, # because computing it would divide by zero (see `almostzero`). The boundary # gradient also vanishes in this case (see `check_boundary_model_correction`). # Note that `return` only exits the closure, i.e., skips the current neighbor. - skip_zero_distance(neighbor_system) && - distance < almostzero(h) && return + if skip_zero_distance(neighbor_system) && distance < almostzero(h) + return + end + # Now we can safely call the unsafe version of the kernel gradient to avoid + # redundant zero checks. # Evaluate the gradient exactly as in the fluid-structure interaction, i.e., # with the fluid particle's kernel and correction and the fluid-first displacement. # Note that we cannot just flip the sign of the structure-first gradient, since @@ -171,8 +174,8 @@ function interact_structure_fluid!(dv, v_particle_system, u_particle_system, # by the pressure acceleration of a fluid with an asymmetric corrected kernel # gradient and by the continuity equation of the boundary particles. # When the pressure acceleration requires it, compute it only once for both. - # This is the same gradient as in the fluid-structure interaction, since negating - # the fluid-first displacement is exact. + # Here `pos_diff` is `x_structure - x_fluid`, matching the displacement + # passed to the boundary model in the fluid-structure interaction. grad_kernel_boundary = boundary_pressure_kernel_grad(system_correction(neighbor_system), particle_system, pos_diff, distance, particle) From 2af99a208233bbe59760eb320fd5405305724729 Mon Sep 17 00:00:00 2001 From: Sven Berger Date: Fri, 13 Nov 2026 14:05:36 +0100 Subject: [PATCH 11/11] review --- docs/src/general/smoothing_kernels.md | 11 +++---- docs/src/systems/total_lagrangian_sph.md | 9 +++--- src/general/smoothing_kernels.jl | 32 +++++++++---------- .../fluid/entropically_damped_sph/rhs.jl | 12 +++---- .../fluid/weakly_compressible_sph/rhs.jl | 12 +++---- src/schemes/structure/rigid_body/system.jl | 2 +- src/schemes/structure/structure.jl | 8 ++--- .../structure/total_lagrangian_sph/system.jl | 2 +- test/schemes/boundary/dummy_particles/rhs.jl | 28 ++++++++++------ test/schemes/fluid/rhs.jl | 13 +++----- 10 files changed, 66 insertions(+), 63 deletions(-) diff --git a/docs/src/general/smoothing_kernels.md b/docs/src/general/smoothing_kernels.md index 98c9c00ae6..436e9c2b3e 100644 --- a/docs/src/general/smoothing_kernels.md +++ b/docs/src/general/smoothing_kernels.md @@ -193,18 +193,17 @@ CairoMakie.save("smoothing_kernels.png", fig) The compact support is automatically checked when inside a `foreach_point_neighbor` or `foreach_neighbor` loop. For the zero distance check, use - ```jldoctest kernels; output = false - h = TrixiParticles.smoothing_length(system, particle) - TrixiParticles.skip_zero_distance(system) && - distance < TrixiParticles.almostzero(h) + ```jldoctest kernels; output = false, setup = :(almostzero = 1e-12) + TrixiParticles.skip_zero_distance(system) && distance < almostzero # output false ``` where `skip_zero_distance` returns `true` if the kernel gradient is zero at zero distance, which is not the case for some corrected kernels. - Here, `almostzero(h)` is the threshold - `sqrt(eps(typeof(h))) * h ≈ sqrt(eps(h^2))`, where `h` is the smoothing length. + Here, `almostzero` is a small threshold, which should be chosen as `sqrt(eps(h^2))`, + where `h` is the smoothing length, since `distance^2` is in the order of `h^2`. + Note that `sqrt(eps(h^2)) != eps(h)`. ```@autodocs Modules = [TrixiParticles] diff --git a/docs/src/systems/total_lagrangian_sph.md b/docs/src/systems/total_lagrangian_sph.md index 09e078399f..2fa4d1c32a 100644 --- a/docs/src/systems/total_lagrangian_sph.md +++ b/docs/src/systems/total_lagrangian_sph.md @@ -58,11 +58,12 @@ are the Lamé coefficients, where ``E`` is the Young's modulus and ``\nu`` is th The term ``\bm{f}_a^{PF}`` is an optional penalty force. See e.g. [`PenaltyForceGanzenmueller`](@ref). The smoothing kernel, the smoothing length and the correction matrix ``\bm{L}_{0a}`` above -are only used for the solid mechanics of the structure. -The interaction with fluids is determined by the `boundary_model`, which has its own -smoothing kernel and smoothing length (see [Boundary Models](@ref boundary_models)). +are used for the solid mechanics of the structure. +The pressure forces between fluid and structure are computed with the `boundary_model` +(see [Boundary Models](@ref boundary_models)). When using [`BoundaryModelDummyParticles`](@ref), its smoothing kernel and smoothing length -must match those of every fluid system. Its `correction` must be `nothing` +are used for the kernel gradients at the structure particles and must match those of every +fluid system. Its `correction` must be `nothing` (see [Boundary Corrections](@ref boundary_corrections)). [`ContinuityDensity`](@ref) is not supported for the boundary model of a [`TotalLagrangianSPHSystem`](@ref). diff --git a/src/general/smoothing_kernels.jl b/src/general/smoothing_kernels.jl index bff121e2d2..ebf035297f 100644 --- a/src/general/smoothing_kernels.jl +++ b/src/general/smoothing_kernels.jl @@ -66,9 +66,9 @@ end end # Safe kernel gradient with an explicit kernel and smoothing length `h`. -# `kernel_data` stores the correction values of `particle` (a system or a boundary model). +# `system` stores the correction values of `particle` (a system or a boundary model). @inline function corrected_kernel_grad(kernel, pos_diff, distance, h, correction, - kernel_data, particle) + system, particle) compact_support_ = compact_support(kernel, h) # See `almostzero` for an explanation of the zero distance check @@ -78,45 +78,43 @@ end end return corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, correction, - kernel_data, particle) + system, particle) end @inline function corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, correction, - kernel_data, particle) + system, particle) return kernel_grad_unsafe(kernel, pos_diff, distance, h) end @inline function corrected_kernel_grad_unsafe(kernel_, pos_diff, distance, h, - ::KernelCorrection, kernel_data, particle) + ::KernelCorrection, system, particle) return (kernel_grad(kernel_, pos_diff, distance, h) .- - kernel(kernel_, distance, h) * dw_gamma(kernel_data, particle)) / - kernel_correction_coefficient(kernel_data, particle) + kernel(kernel_, distance, h) * dw_gamma(system, particle)) / + kernel_correction_coefficient(system, particle) end @inline function corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, - corr::BlendedGradientCorrection, - kernel_data, + corr::BlendedGradientCorrection, system, particle) (; blending_factor) = corr grad = kernel_grad_unsafe(kernel, pos_diff, distance, h) return (1 - blending_factor) * grad + - blending_factor * correction_matrix(kernel_data, particle) * grad + blending_factor * correction_matrix(system, particle) * grad end @inline function corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, - ::GradientCorrection, kernel_data, - particle) + ::GradientCorrection, system, particle) grad = kernel_grad_unsafe(kernel, pos_diff, distance, h) - return correction_matrix(kernel_data, particle) * grad + return correction_matrix(system, particle) * grad end @inline function corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, - ::MixedKernelGradientCorrection, - kernel_data, particle) + ::MixedKernelGradientCorrection, system, + particle) grad = corrected_kernel_grad_unsafe(kernel, pos_diff, distance, h, KernelCorrection(), - kernel_data, particle) - return correction_matrix(kernel_data, particle) * grad + system, particle) + return correction_matrix(system, particle) * grad end @doc raw""" diff --git a/src/schemes/fluid/entropically_damped_sph/rhs.jl b/src/schemes/fluid/entropically_damped_sph/rhs.jl index 45dabb2d8e..2f6aee2c3b 100644 --- a/src/schemes/fluid/entropically_damped_sph/rhs.jl +++ b/src/schemes/fluid/entropically_damped_sph/rhs.jl @@ -17,15 +17,15 @@ function interact!(dv, v_particle_system, u_particle_system, neighbor, pos_diff, distance - # Skip neighbors with (almost) the same position if the kernel gradient is zero - # at zero distance, because computing it would divide by zero (see `almostzero`). + # Skip neighbors with (almost) the same position because the kernel gradient + # is zero, but computing it would divide by zero (see `almostzero`). # Note that `return` only exits the closure, i.e., skips the current neighbor. skip_zero_distance(particle_system) && distance < almostzero(h) && return - # Now we can safely call the unsafe version of the kernel gradient to avoid - # redundant zero checks. - grad_kernel = smoothing_kernel_grad_unsafe(particle_system, pos_diff, distance, - particle) + # Now that we know that `distance` is not zero, we can safely call the unsafe + # version of the kernel gradient to avoid redundant zero checks. + grad_kernel = smoothing_kernel_grad_unsafe(particle_system, pos_diff, + distance, particle) # `foreach_point_neighbor` makes sure that `particle` and `neighbor` are # in bounds of the respective system. For performance reasons, we use `@inbounds` diff --git a/src/schemes/fluid/weakly_compressible_sph/rhs.jl b/src/schemes/fluid/weakly_compressible_sph/rhs.jl index db64278176..bd4e10b0db 100644 --- a/src/schemes/fluid/weakly_compressible_sph/rhs.jl +++ b/src/schemes/fluid/weakly_compressible_sph/rhs.jl @@ -46,17 +46,17 @@ function interact!(dv, v_particle_system, u_particle_system, backend, particle; init) do particle, neighbor, pos_diff, distance - # Skip neighbors with (almost) the same position if the kernel gradient is zero - # at zero distance, because computing it would divide by zero (see `almostzero`). + # Skip neighbors with (almost) the same position because the kernel gradient + # is zero, but computing it would divide by zero (see `almostzero`). # Note that `return` only exits the closure, i.e., skips the current neighbor. if skip_zero_distance(particle_system) && distance < almostzero(h) return init end - # Now we can safely call the unsafe version of the kernel gradient to avoid - # redundant zero checks. - grad_kernel = smoothing_kernel_grad_unsafe(particle_system, pos_diff, distance, - particle) + # Now that we know that `distance` is not zero, we can safely call the unsafe + # version of the kernel gradient to avoid redundant zero checks. + grad_kernel = smoothing_kernel_grad_unsafe(particle_system, pos_diff, + distance, particle) # `foreach_neighbor` makes sure that `neighbor` is in bounds of `neighbor_system` m_b = @inbounds hydrodynamic_mass(neighbor_system, neighbor) diff --git a/src/schemes/structure/rigid_body/system.jl b/src/schemes/structure/rigid_body/system.jl index 23535ae9cb..b0cb233625 100644 --- a/src/schemes/structure/rigid_body/system.jl +++ b/src/schemes/structure/rigid_body/system.jl @@ -635,7 +635,7 @@ function check_configuration(system::RigidBodySystem, systems, nhs) "when simulating a fluid-structure interaction.")) end - check_boundary_model_fsi(system, boundary_model, neighbor) + check_smoothing_kernel_fsi(system, boundary_model, neighbor) if neighbor isa AbstractFluidSystem && neighbor.surface_normal_method isa ColorfieldSurfaceNormal diff --git a/src/schemes/structure/structure.jl b/src/schemes/structure/structure.jl index a48d2654d3..f17a0bc1ee 100644 --- a/src/schemes/structure/structure.jl +++ b/src/schemes/structure/structure.jl @@ -80,8 +80,8 @@ end # fluid kernels. The TLSPH kernel for solid mechanics may differ. # Note that this is only a restriction for `BoundaryModelDummyParticles`, since # `BoundaryModelMonaghanKajtar` always uses the compact support of the fluid. -function check_boundary_model_fsi(system, boundary_model::BoundaryModelDummyParticles, - neighbor_system::AbstractFluidSystem) +function check_smoothing_kernel_fsi(system, boundary_model::BoundaryModelDummyParticles, + neighbor_system::AbstractFluidSystem) smoothing_kernel_structure = boundary_model.smoothing_kernel smoothing_kernel_fluid = system_smoothing_kernel(neighbor_system) smoothing_length_structure = initial_smoothing_length(boundary_model) @@ -98,7 +98,7 @@ function check_boundary_model_fsi(system, boundary_model::BoundaryModelDummyPart return system end -check_boundary_model_fsi(system, boundary_model, neighbor_system) = system +check_smoothing_kernel_fsi(system, boundary_model, neighbor_system) = system # The correction values of a boundary particle are sums over its complete hydrodynamic # neighborhood in the current configuration, including the particles of its own structure @@ -230,7 +230,7 @@ end m_b, rho_a, rho_b, v_a, v_b, grad_kernel_fluid, grad_kernel_boundary) # The boundary model is uncorrected and its kernel and smoothing length match the - # fluid's (see `check_boundary_model_correction` and `check_boundary_model_fsi`). + # fluid's (see `check_boundary_model_correction` and `check_smoothing_kernel_fsi`). # If the fluid uses an asymmetric corrected gradient, reuse the boundary gradient # computed for the pressure acceleration. Otherwise, reverse the fluid gradient. grad_kernel = isnothing(grad_kernel_boundary) ? -grad_kernel_fluid : diff --git a/src/schemes/structure/total_lagrangian_sph/system.jl b/src/schemes/structure/total_lagrangian_sph/system.jl index 6dba89367e..f68c35963f 100644 --- a/src/schemes/structure/total_lagrangian_sph/system.jl +++ b/src/schemes/structure/total_lagrangian_sph/system.jl @@ -851,6 +851,6 @@ function check_configuration(system::TotalLagrangianSPHSystem, systems, nhs) check_boundary_model_correction(system, boundary_model) foreach_system(systems) do neighbor - check_boundary_model_fsi(system, boundary_model, neighbor) + check_smoothing_kernel_fsi(system, boundary_model, neighbor) end end diff --git a/test/schemes/boundary/dummy_particles/rhs.jl b/test/schemes/boundary/dummy_particles/rhs.jl index 02816e489a..c848e1533e 100644 --- a/test/schemes/boundary/dummy_particles/rhs.jl +++ b/test/schemes/boundary/dummy_particles/rhs.jl @@ -76,14 +76,13 @@ v_boundary_continuity = copy(initial_condition.density') - # TLSPH system - structure_system = TotalLagrangianSPHSystem(initial_condition; smoothing_kernel, - smoothing_length, young_modulus=0.0, - poisson_ratio=0.0, - boundary_model=boundary_model_continuity) - - # Positions of the structure particles are not used here - u_structure = zeros(0, TrixiParticles.nparticles(structure_system)) + # Rigid body. A `TotalLagrangianSPHSystem` doesn't support `ContinuityDensity` + # for its boundary model. + structure_system = RigidBodySystem(initial_condition; + boundary_model=boundary_model_continuity) + + # The coordinates of a rigid body are stored in `u` + u_structure = copy(initial_condition.coordinates) v_structure = vcat(initial_condition.velocity, initial_condition.density') @@ -91,7 +90,7 @@ "Fluid-Fluid" => second_fluid_system, "Fluid-BoundaryDummyPressureZeroing" => boundary_system_zeroing, "Fluid-BoundaryDummyContinuityDensity" => boundary_system_continuity, - "Fluid-TLSPH" => structure_system + "Fluid-RigidBody" => structure_system ) if density_calculator isa SummationDensity @@ -106,7 +105,7 @@ "Fluid-BoundaryDummyContinuityDensity" => (v_boundary_continuity, u_boundary), "Fluid-BoundaryDummySummationDensity" => (v_boundary, u_boundary), - "Fluid-TLSPH" => (v_structure, u_structure) + "Fluid-RigidBody" => (v_structure, u_structure) ) return systems, vu @@ -197,6 +196,15 @@ TrixiParticles.interact!(dv_neighbor, v_neighbor, u_neighbor, v, u, neighbor_system, fluid_system, semi) + if neighbor_system isa RigidBodySystem + # A rigid body accumulates the forces on its particles instead of + # their accelerations + (; force_per_particle, boundary_model) = neighbor_system + NDIMS = ndims(neighbor_system) + mass = boundary_model.hydrodynamic_mass + dv_neighbor[1:NDIMS, :] .= force_per_particle ./ mass' + end + if neighbor_system isa WeaklyCompressibleSPHSystem # If both are fluids, neighbor-neighbor interaction is necessary # for energy preservation. diff --git a/test/schemes/fluid/rhs.jl b/test/schemes/fluid/rhs.jl index 87a1689ef4..fc4c568da2 100644 --- a/test/schemes/fluid/rhs.jl +++ b/test/schemes/fluid/rhs.jl @@ -282,7 +282,6 @@ function create_fluid_system(scheme, velocity, density, viscosity; shifting_technique=nothing, correction=nothing, - density_calculator=ContinuityDensity(), coordinates=zeros(2, 1)) fluid = InitialCondition(; coordinates, velocity=reshape(collect(velocity), 2, 1), @@ -293,7 +292,7 @@ if scheme == "WCSPH" return WeaklyCompressibleSPHSystem(fluid; smoothing_kernel, smoothing_length, state_equation, - density_calculator, + density_calculator=ContinuityDensity(), viscosity, shifting_technique, correction) end @@ -303,8 +302,8 @@ average_pressure_reduction = scheme == "EDAC with average pressure reduction" return EntropicallyDampedSPHSystem(fluid; smoothing_kernel, smoothing_length, sound_speed, viscosity, shifting_technique, - average_pressure_reduction, correction, - density_calculator) + average_pressure_reduction, + density_calculator=ContinuityDensity()) end function create_structure_system(structure_type, viscosity; @@ -344,17 +343,15 @@ # Run the regular update step to compute the boundary pressure, the wall velocity # for the viscosity, and the average pressure of EDAC. Return the systems # stored in the semidiscretization and the wrapped arrays. - function initialize(fluid_system, structure_system; interaction_matrix=nothing) + function initialize(fluid_system, structure_system) semi = if structure_system isa TotalLagrangianSPHSystem @test_logs (:info, r"^To create the self-interaction neighborhood search of a `TotalLagrangianSPHSystem`") begin Semidiscretization(fluid_system, structure_system; - interaction_matrix, parallelization_backend=SerialBackend()) end else @test_logs Semidiscretization(fluid_system, structure_system; - interaction_matrix, parallelization_backend=SerialBackend()) end ode = semidiscretize(semi, (0.0, 0.01)) @@ -443,7 +440,7 @@ (RigidBodySystem, false, smoothing_length)) correction_methods = (nothing, AkinciFreeSurfaceCorrection(fluid_density), KernelCorrection(), GradientCorrection(), - BlendedGradientCorrection(0.3), + BlendedGradientCorrection(0.5), MixedKernelGradientCorrection()) @testset "$(config[1]), Viscosity `$(nameof(typeof(config[2])))`" for config in