Skip to content

Restructure TVF #864

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
28f0c19
Restructure TVF
efaulhaber Jul 21, 2025
f42b0fa
Remove dv term from newer paper
efaulhaber Jul 31, 2025
cdb0770
Eliminate time step from spatial discretization
efaulhaber Jul 31, 2025
4ea7395
Rename updates
efaulhaber Jul 31, 2025
03037e7
Merge branch 'main' into revise-tvf
efaulhaber Jul 31, 2025
4aaf014
Fix EDAC
efaulhaber Jul 31, 2025
16f38d2
Fix `current_density`
efaulhaber Jul 31, 2025
f9456d6
Fix sign of background pressure
efaulhaber Jul 31, 2025
793bfa5
Merge branch 'revise-tvf' of github.com:efaulhaber/TrixiParticles.jl …
efaulhaber Jul 31, 2025
3157178
Add reference
efaulhaber Jul 31, 2025
5725d90
Implement suggestions
efaulhaber Aug 1, 2025
42fc6d6
Remove `update_from_callback`
efaulhaber Aug 1, 2025
23c63d7
Implement suggestions
efaulhaber Aug 1, 2025
49417b5
Fix tests
efaulhaber Aug 1, 2025
11978d1
Fix background pressure factor
efaulhaber Aug 1, 2025
a1344f9
Improve comments
efaulhaber Aug 1, 2025
b3402ff
Fix boundary bug
efaulhaber Aug 1, 2025
4c6d5c4
Merge branch 'main' into revise-tvf
efaulhaber Aug 1, 2025
5c6c8e5
Make N-Body system type-stable
efaulhaber Aug 2, 2025
ca0a2f6
Fix packing
efaulhaber Aug 2, 2025
a32a2f9
Merge branch 'revise-tvf' of https://github.com/efaulhaber/TrixiParti…
efaulhaber Aug 2, 2025
2444ab5
Merge branch 'main' into revise-tvf
efaulhaber Aug 2, 2025
ea63b5f
Fix update callback
efaulhaber Aug 2, 2025
88371fa
Merge branch 'revise-tvf' of https://github.com/efaulhaber/TrixiParti…
efaulhaber Aug 2, 2025
90865e0
Update src/schemes/fluid/transport_velocity.jl
efaulhaber Aug 3, 2025
a5e93e7
Merge branch 'main' into revise-tvf
svchb Aug 13, 2025
d084d6c
Fix open boundaries
efaulhaber Aug 17, 2025
92e9e7a
Fix open boundaries
efaulhaber Aug 17, 2025
7c3b234
Merge branch 'main' into revise-tvf
LasNikas Aug 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/n_body/n_body_system.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using TrixiParticles
using LinearAlgebra

struct NBodySystem{NDIMS, ELTYPE <: Real} <: TrixiParticles.System{NDIMS}
initial_condition :: InitialCondition{ELTYPE}
struct NBodySystem{NDIMS, ELTYPE <: Real, IC} <: TrixiParticles.System{NDIMS}
initial_condition :: IC
mass :: Array{ELTYPE, 1} # [particle]
G :: ELTYPE
buffer :: Nothing
Expand All @@ -11,13 +11,13 @@ struct NBodySystem{NDIMS, ELTYPE <: Real} <: TrixiParticles.System{NDIMS}
mass = copy(initial_condition.mass)

new{size(initial_condition.coordinates, 1),
eltype(mass)}(initial_condition, mass, G, nothing)
eltype(mass), typeof(initial_condition)}(initial_condition, mass, G, nothing)
end
end

TrixiParticles.timer_name(::NBodySystem) = "nbody"

@inline Base.eltype(system::NBodySystem) = eltype(system.initial_condition.coordinates)
@inline Base.eltype(system::NBodySystem{NDIMS, ELTYPE}) where {NDIMS, ELTYPE} = ELTYPE

function TrixiParticles.write_u0!(u0, system::NBodySystem)
u0 .= system.initial_condition.coordinates
Expand Down
2 changes: 1 addition & 1 deletion src/callbacks/density_reinit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function initialize_reinit_cb!(cb::DensityReinitializationCallback, u, t, integr
# Update systems to compute quantities like density and pressure.
semi = integrator.p
v_ode, u_ode = u.x
update_systems_and_nhs(v_ode, u_ode, semi, t; update_from_callback=true)
update_systems_and_nhs(v_ode, u_ode, semi, t)

# Apply the callback.
cb(integrator)
Expand Down
3 changes: 1 addition & 2 deletions src/callbacks/particle_shifting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ function particle_shifting!(integrator)
# still have the values from the last stage of the previous step if not updated here.
@trixi_timeit timer() "update systems and nhs" begin
# Don't create sub-timers here to avoid cluttering the timer output
@notimeit timer() update_systems_and_nhs(v_ode, u_ode, semi, t;
update_from_callback=true)
@notimeit timer() update_systems_and_nhs(v_ode, u_ode, semi, t)
end

@trixi_timeit timer() "particle shifting" foreach_system(semi) do system
Expand Down
3 changes: 1 addition & 2 deletions src/callbacks/post_process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ function (pp::PostprocessCallback)(integrator)
# still have the values from the last stage of the previous step if not updated here.
@trixi_timeit timer() "update systems and nhs" begin
# Don't create sub-timers here to avoid cluttering the timer output
@notimeit timer() update_systems_and_nhs(v_ode, u_ode, semi, t;
update_from_callback=true)
@notimeit timer() update_systems_and_nhs(v_ode, u_ode, semi, t)
end

foreach_system(semi) do system
Expand Down
7 changes: 3 additions & 4 deletions src/callbacks/update.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function initial_update!(cb::UpdateCallback, u, t, integrator)

# Tell systems that `UpdateCallback` is used
foreach_system(semi) do system
update_callback_used!(system)
set_callback_flag!(system, true)
end

return cb(integrator)
Expand All @@ -78,7 +78,7 @@ function (update_callback!::UpdateCallback)(integrator)

# Update quantities that are stored in the systems. These quantities (e.g. pressure)
# still have the values from the last stage of the previous step if not updated here.
update_systems_and_nhs(v_ode, u_ode, semi, t; update_from_callback=true)
update_systems_and_nhs(v_ode, u_ode, semi, t)

# Update open boundaries first, since particles might be activated or deactivated
@trixi_timeit timer() "update open boundary" foreach_system(semi) do system
Expand All @@ -89,6 +89,7 @@ function (update_callback!::UpdateCallback)(integrator)
update_particle_packing(system, v_ode, u_ode, semi, integrator)
end

# This is only used by the particle packing system and should be removed in the future
@trixi_timeit timer() "update TVF" foreach_system(semi) do system
update_transport_velocity!(system, v_ode, semi)
end
Expand Down Expand Up @@ -141,5 +142,3 @@ function Base.show(io::IO, ::MIME"text/plain",
summary_box(io, "UpdateCallback", setup)
end
end

update_callback_used!(system) = system
54 changes: 45 additions & 9 deletions src/general/semidiscretization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ function semidiscretize(semi, tspan; reset_threads=true)
# Initialize this system
initialize!(system, semi_new)

# Only for systems requiring a mandatory callback
reset_callback_flag!(system)
# Only for systems requiring the use of the `UpdateCallback`
set_callback_flag!(system, false)
end

return DynamicalODEProblem(kick!, drift!, v0_ode, u0_ode, tspan, semi_new)
Expand Down Expand Up @@ -373,8 +373,8 @@ function restart_with!(semi, sol; reset_threads=true)

restart_with!(system, v, u)

# Only for systems requiring a mandatory callback
reset_callback_flag!(system)
# Only for systems requiring the use of the `UpdateCallback`
set_callback_flag!(system, false)
end

return semi
Expand Down Expand Up @@ -464,17 +464,33 @@ function drift!(du_ode, v_ode, u_ode, semi, t)
end

@inline function add_velocity!(du, v, particle, system)
# Generic fallback for all systems that don't define this function
for i in 1:ndims(system)
du[i, particle] = v[i, particle]
@inbounds du[i, particle] = v[i, particle]
end

return du
end

@inline add_velocity!(du, v, particle, system::BoundarySPHSystem) = du
# Boundary systems don't integrate the particle positions
@inline add_velocity!(du, v, particle, system::BoundarySystem) = du

@inline function add_velocity!(du, v, particle, system::FluidSystem)
# This is zero unless a transport velocity is used
delta_v_ = delta_v(system, particle)

for i in 1:ndims(system)
@inbounds du[i, particle] = v[i, particle] + delta_v_[i]
end

return du
end

function kick!(dv_ode, v_ode, u_ode, semi, t)
@trixi_timeit timer() "kick!" begin
# Check that the `UpdateCallback` is used if required
check_update_callback(semi)

@trixi_timeit timer() "reset ∂v/∂t" set_zero!(dv_ode)

@trixi_timeit timer() "update systems and nhs" update_systems_and_nhs(v_ode, u_ode,
Expand All @@ -490,8 +506,9 @@ function kick!(dv_ode, v_ode, u_ode, semi, t)
return dv_ode
end

# Update the systems and neighborhood searches (NHS) for a simulation before calling `interact!` to compute forces
function update_systems_and_nhs(v_ode, u_ode, semi, t; update_from_callback=false)
# Update the systems and neighborhood searches (NHS) for a simulation
# before calling `interact!` to compute forces.
function update_systems_and_nhs(v_ode, u_ode, semi, t)
# First update step before updating the NHS
# (for example for writing the current coordinates in the solid system)
foreach_system(semi) do system
Expand Down Expand Up @@ -523,12 +540,21 @@ function update_systems_and_nhs(v_ode, u_ode, semi, t; update_from_callback=fals
update_pressure!(system, v, u, v_ode, u_ode, semi, t)
end

# This update depends on the computed quantities of the fluid system and therefore
# needs to be after `update_quantities!`.
foreach_system(semi) do system
v = wrap_v(v_ode, system, semi)
u = wrap_u(u_ode, system, semi)

update_boundary_interpolation!(system, v, u, v_ode, u_ode, semi, t)
end

# Final update step for all remaining systems
foreach_system(semi) do system
v = wrap_v(v_ode, system, semi)
u = wrap_u(u_ode, system, semi)

update_final!(system, v, u, v_ode, u_ode, semi, t; update_from_callback)
update_final!(system, v, u, v_ode, u_ode, semi, t)
end
end

Expand Down Expand Up @@ -858,6 +884,16 @@ function update!(neighborhood_search, x, y, semi; points_moving=(true, false),
parallelization_backend=semi.parallelization_backend)
end

function check_update_callback(semi)
foreach_system(semi) do system
# This check will be optimized away if the system does not require the callback
if requires_update_callback(system) && !update_callback_used(system)
system_name = system |> typeof |> nameof
throw(ArgumentError("`UpdateCallback` is required for `$system_name`"))
end
end
end

function check_configuration(systems,
nhs::Union{Nothing, PointNeighbors.AbstractNeighborhoodSearch})
foreach_system(systems) do system
Expand Down
22 changes: 14 additions & 8 deletions src/general/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ end
system_correction(system), system, particle)
end

# System update orders. This can be dispatched if needed.
# 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
end
Expand All @@ -139,21 +139,27 @@ function update_pressure!(system, v, u, v_ode, u_ode, semi, t)
return system
end

function update_final!(system, v, u, v_ode, u_ode, semi, t; update_from_callback=false)
function update_boundary_interpolation!(system, v, u, v_ode, u_ode, semi, t)
return system
end

# Only for systems requiring a mandatory callback
reset_callback_flag!(system) = system
function update_final!(system, v, u, v_ode, u_ode, semi, t)
return system
end

# Only for systems requiring the use of the `UpdateCallback`
@inline requires_update_callback(system) = false
@inline update_callback_used(system) = false
@inline set_callback_flag!(system, value) = system

initial_smoothing_length(system) = smoothing_length(system, nothing)
@inline initial_smoothing_length(system) = smoothing_length(system, nothing)

function smoothing_length(system, particle)
@inline function smoothing_length(system, particle)
return system.smoothing_length
end

system_smoothing_kernel(system) = system.smoothing_kernel
system_correction(system) = nothing
@inline system_smoothing_kernel(system) = system.smoothing_kernel
@inline system_correction(system) = nothing

@inline particle_spacing(system, particle) = system.initial_condition.particle_spacing

Expand Down
3 changes: 1 addition & 2 deletions src/io/write_vtk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ function trixi2vtk(vu_ode, semi, t; iter=nothing, output_directory="out", prefix
# still have the values from the last stage of the previous step if not updated here.
@trixi_timeit timer() "update systems" begin
# Don't create sub-timers here to avoid cluttering the timer output
@notimeit timer() update_systems_and_nhs(v_ode, u_ode, semi, t;
update_from_callback=true)
@notimeit timer() update_systems_and_nhs(v_ode, u_ode, semi, t)
end

filenames = system_names(systems)
Expand Down
22 changes: 9 additions & 13 deletions src/preprocessing/particle_packing/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,15 @@ end
return ndims(system)
end

function reset_callback_flag!(system::ParticlePackingSystem)
system.update_callback_used[] = false
@inline requires_update_callback(system::ParticlePackingSystem) = true
@inline update_callback_used(system::ParticlePackingSystem) = system.update_callback_used[]

function set_callback_flag!(system::ParticlePackingSystem, value)
system.update_callback_used[] = value

return system
end

update_callback_used!(system::ParticlePackingSystem) = system.update_callback_used[] = true

function write2vtk!(vtk, v, u, t, system::ParticlePackingSystem; write_meta_data=true)
vtk["velocity"] = [advection_velocity(v, system, particle)
for particle in active_particles(system)]
Expand Down Expand Up @@ -289,15 +290,6 @@ function update_position!(u, system::ParticlePackingSystem, semi)
return u
end

function update_final!(system::ParticlePackingSystem, v, u, v_ode, u_ode, semi, t;
update_from_callback=false)
if !update_from_callback && !(system.update_callback_used[])
throw(ArgumentError("`UpdateCallback` is required when using `ParticlePackingSystem`"))
end

return system
end

# Skip for systems without `SignedDistanceField`
constrain_particles_onto_surface!(u, system::ParticlePackingSystem{Nothing}, semi) = u

Expand Down Expand Up @@ -398,6 +390,10 @@ end
return system
end

@inline function update_transport_velocity!(system, v_ode, semi)
return system
end

# Skip for fixed systems
@inline add_velocity!(du, v, particle, system::ParticlePackingSystem{<:Any, true}) = du

Expand Down
7 changes: 3 additions & 4 deletions src/schemes/boundary/dummy_particles/dummy_particles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,17 +311,16 @@ end
system, v, u, v_ode, u_ode, semi)
(; correction, density_calculator) = boundary_model

compute_correction_values!(system, correction, u, v_ode, u_ode, semi)
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_gradient_correction_matrix!(correction, boundary_model, system, u, v_ode, u_ode,
semi)

# `kernel_correct_density!` only performed for `SummationDensity`
kernel_correct_density!(boundary_model, v, u, v_ode, u_ode, semi, correction,
density_calculator)

compute_pressure!(boundary_model, density_calculator, system, v, u, v_ode, u_ode, semi)

return boundary_model
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ end
end

# Called from semidiscretization
function update_final!(system, ::BoundaryModelLastiwka, v, u, v_ode, u_ode, semi, t)
function update_boundary_model!(system, ::BoundaryModelLastiwka, v, u, v_ode, u_ode,
semi, t)
@trixi_timeit timer() "evaluate characteristics" begin
evaluate_characteristics!(system, v, u, v_ode, u_ode, semi, t)
end
Expand Down
2 changes: 1 addition & 1 deletion src/schemes/boundary/open_boundary/mirroring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function update_boundary_quantities!(system, boundary_model::BoundaryModelTafuni
end
end

update_final!(system, ::BoundaryModelTafuni, v, u, v_ode, u_ode, semi, t) = system
update_boundary_model!(system, ::BoundaryModelTafuni, v, u, v_ode, u_ode, semi, t) = system

function extrapolate_values!(system,
mirror_method::Union{FirstOrderMirroring, SimpleMirroring},
Expand Down
19 changes: 8 additions & 11 deletions src/schemes/boundary/open_boundary/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,15 @@ end
return ELTYPE
end

function reset_callback_flag!(system::OpenBoundarySPHSystem)
system.update_callback_used[] = false
@inline requires_update_callback(system::OpenBoundarySPHSystem) = true
@inline update_callback_used(system::OpenBoundarySPHSystem) = system.update_callback_used[]

function set_callback_flag!(system::OpenBoundarySPHSystem, value)
system.update_callback_used[] = value

return system
end

update_callback_used!(system::OpenBoundarySPHSystem) = system.update_callback_used[] = true

function corresponding_fluid_system(system::OpenBoundarySPHSystem, semi)
return system.fluid_system
end
Expand All @@ -252,13 +253,9 @@ end
return system.pressure
end

function update_final!(system::OpenBoundarySPHSystem, v, u, v_ode, u_ode, semi, t;
update_from_callback=false)
if !update_from_callback && !(system.update_callback_used[])
throw(ArgumentError("`UpdateCallback` is required when using `OpenBoundarySPHSystem`"))
end

update_final!(system, system.boundary_model, v, u, v_ode, u_ode, semi, t)
function update_boundary_interpolation!(system::OpenBoundarySPHSystem, v, u, v_ode, u_ode,
semi)
update_boundary_model!(system, system.boundary_model, v, u, v_ode, u_ode, semi, t)
end

# This function is called by the `UpdateCallback`, as the integrator array might be modified
Expand Down
9 changes: 5 additions & 4 deletions src/schemes/boundary/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,13 @@ function update_quantities!(system::BoundarySPHSystem, v, u, v_ode, u_ode, semi,
end

# This update depends on the computed quantities of the fluid system and therefore
# has to be in `update_final!` after `update_quantities!`.
function update_final!(system::BoundarySPHSystem, v, u, v_ode, u_ode, semi, t;
update_from_callback=false)
# has to be in `update_boundary_interpolation!` after `update_quantities!`.
function update_boundary_interpolation!(system::BoundarySPHSystem, v, u, v_ode, u_ode,
semi, t)
(; boundary_model) = system

# Note that `update_pressure!(::BoundarySPHSystem, ...)` is empty
# Note that `update_pressure!(::BoundarySPHSystem, ...)` is empty,
# so no pressure is updated in the previous update steps.
update_pressure!(boundary_model, system, v, u, v_ode, u_ode, semi)

return system
Expand Down
Loading
Loading