Skip to content

Commit 26d0109

Browse files
authored
Merge branch 'main' into optimize-kernels
2 parents 2ae1f15 + 3a5900a commit 26d0109

File tree

14 files changed

+230
-176
lines changed

14 files changed

+230
-176
lines changed

src/general/buffer.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ end
4040
# Dispatch by system type to handle systems that provide a buffer.
4141
@inline buffer(system) = nothing
4242

43-
@inline update_system_buffer!(buffer::Nothing, semi) = buffer
43+
@inline update_system_buffer!(buffer::Nothing) = buffer
4444

4545
# TODO `resize` allocates. Find a non-allocating version
46-
@inline function update_system_buffer!(buffer::SystemBuffer, semi)
46+
@inline function update_system_buffer!(buffer::SystemBuffer)
4747
(; active_particle) = buffer
4848

4949
# TODO: Parallelize (see https://github.com/trixi-framework/TrixiParticles.jl/issues/810)
@@ -64,7 +64,7 @@ end
6464
return view(buffer.eachparticle, 1:buffer.active_particle_count[])
6565
end
6666

67-
@inline function deactivate_particle!(system, particle, u)
67+
@inline function deactivate_particle!(system, particle, v, u)
6868
(; active_particle) = system.buffer
6969

7070
# Set particle far away from simulation domain
@@ -73,6 +73,14 @@ end
7373
u[dim, particle] = eltype(system)(1e16)
7474
end
7575

76+
# To ensure that the velocity of an inactive particle does not dominate the time step
77+
# in adaptive time integrators, set this velocity to zero.
78+
# Additionally, this enables map-reduce operations for `v_max` computation
79+
# without having to distinguish inactive particles.
80+
for dim in 1:ndims(system)
81+
v[dim, particle] = 0
82+
end
83+
7684
# `deactivate_particle!` and `active_particle[particle] = true`
7785
# are never called on the same buffer inside a kernel,
7886
# so we don't have any race conditions on this `active_particle` vector.

src/general/custom_quantities.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ Returns the total kinetic energy of all particles in a system.
66
function kinetic_energy(system, dv_ode, du_ode, v_ode, u_ode, semi, t)
77
v = wrap_v(v_ode, system, semi)
88

9-
# TODO: `current_velocity` should only contain active particles
10-
# (see https://github.com/trixi-framework/TrixiParticles.jl/issues/850)
119
velocity = reinterpret(reshape, SVector{ndims(system), eltype(v)},
1210
view(current_velocity(v, system), :,
1311
each_active_particle(system)))

src/io/write_vtk.jl

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,13 @@ function write2vtk!(vtk, v, u, t, system::AbstractFluidSystem)
350350
rho_b = current_density(v, system, neighbor)
351351
grad_kernel = smoothing_kernel_grad(system, pos_diff, distance, particle)
352352

353-
surface_tension[1:ndims(system),
354-
particle] .+= surface_tension_force(surface_tension_a,
355-
surface_tension_b,
356-
system,
357-
system,
358-
particle,
359-
neighbor,
360-
pos_diff,
361-
distance,
362-
rho_a,
363-
rho_b,
364-
grad_kernel)
353+
dv_surface_tension = Ref(zero(pos_diff))
354+
surface_tension_force!(dv_surface_tension,
355+
surface_tension_a, surface_tension_b,
356+
system, system, particle, neighbor,
357+
pos_diff, distance, rho_a, rho_b, grad_kernel, 1)
358+
359+
surface_tension[1:ndims(system), particle] .+= dv_surface_tension[]
365360
end
366361
vtk["surface_tension"] = surface_tension
367362

src/schemes/boundary/open_boundary/dynamical_pressure.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ end
5454
end
5555

5656
@inline function density_calculator(system::OpenBoundarySystem{<:BoundaryModelDynamicalPressureZhang})
57-
return system.cache.density_calculator
57+
return ContinuityDensity()
5858
end
5959

6060
@inline impose_rest_density!(v, system, particle, boundary_model) = v

src/schemes/boundary/open_boundary/rhs.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function interact!(dv, v_particle_system, u_particle_system,
33
v_neighbor_system, u_neighbor_system,
44
particle_system::OpenBoundarySystem{<:BoundaryModelDynamicalPressureZhang},
55
neighbor_system, semi)
6-
(; fluid_system, cache, boundary_model) = particle_system
6+
(; fluid_system, cache) = particle_system
77

88
sound_speed = system_sound_speed(fluid_system)
99

@@ -75,13 +75,11 @@ function interact!(dv, v_particle_system, u_particle_system,
7575
v_diff = current_velocity(v_particle_system, particle_system, particle) -
7676
current_velocity(v_neighbor_system, neighbor_system, neighbor)
7777

78-
# Continuity equation
79-
@inbounds dv[end, particle] += rho_a / rho_b * m_b * dot(v_diff, grad_kernel)
80-
81-
density_diffusion!(dv, density_diffusion(particle_system),
82-
v_particle_system, particle, neighbor,
83-
pos_diff, distance, m_b, rho_a, rho_b,
84-
particle_system, grad_kernel)
78+
# Propagate `@inbounds` to the continuity equation, which accesses particle data
79+
@inbounds continuity_equation!(dv, particle_system, neighbor_system,
80+
v_particle_system, v_neighbor_system, particle,
81+
neighbor, pos_diff, distance, m_b, rho_a, rho_b,
82+
grad_kernel)
8583

8684
# Open boundary pressure evolution matches the corresponding fluid system:
8785
# - EDAC: Compute pressure evolution like the fluid system

src/schemes/boundary/open_boundary/system.jl

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ function OpenBoundarySystem(boundary_zones::Union{BoundaryZone, Nothing}...;
7575
pressure_acceleration=fluid_system.pressure_acceleration_formulation,
7676
shifting_technique=boundary_model isa
7777
BoundaryModelDynamicalPressureZhang ?
78-
shifting_technique(fluid_system) : nothing)
78+
shifting_technique(fluid_system) : nothing,
79+
density_diffusion=boundary_model isa
80+
BoundaryModelDynamicalPressureZhang ?
81+
density_diffusion(fluid_system) : nothing)
7982
boundary_zones_ = filter(bz -> !isnothing(bz), boundary_zones)
8083

8184
initial_conditions = union((bz.initial_condition for bz in boundary_zones_)...)
@@ -90,7 +93,8 @@ function OpenBoundarySystem(boundary_zones::Union{BoundaryZone, Nothing}...;
9093
cache = (;
9194
create_cache_shifting(initial_conditions, shifting_technique)...,
9295
create_cache_open_boundary(boundary_model, fluid_system, initial_conditions,
93-
calculate_flow_rate, boundary_zones_)...)
96+
density_diffusion, calculate_flow_rate,
97+
boundary_zones_)...)
9498

9599
if any(pr -> isa(pr, RCRWindkesselModel), cache.pressure_reference_values)
96100
calculate_flow_rate = true
@@ -130,7 +134,7 @@ function initialize!(system::OpenBoundarySystem, semi)
130134
end
131135

132136
function create_cache_open_boundary(boundary_model, fluid_system, initial_condition,
133-
calculate_flow_rate, boundary_zones)
137+
density_diffusion, calculate_flow_rate, boundary_zones)
134138
reference_values = map(bz -> bz.reference_values, boundary_zones)
135139
ELTYPE = eltype(initial_condition)
136140

@@ -174,15 +178,14 @@ function create_cache_open_boundary(boundary_model, fluid_system, initial_condit
174178
# as it was already verified in `allocate_buffer` that the density array is constant.
175179
density_rest = first(initial_condition.density)
176180

177-
dd = density_diffusion(fluid_system)
178-
if dd isa DensityDiffusionAntuono
179-
density_diffusion_ = DensityDiffusionAntuono(initial_condition; delta=dd.delta)
181+
if density_diffusion isa DensityDiffusionAntuono
182+
density_diffusion_ = DensityDiffusionAntuono(initial_condition;
183+
delta=density_diffusion.delta)
180184
else
181-
density_diffusion_ = dd
185+
density_diffusion_ = density_diffusion
182186
end
183187

184-
cache = (; density_calculator=ContinuityDensity(),
185-
density_diffusion=density_diffusion_,
188+
cache = (; density_diffusion=density_diffusion_,
186189
pressure_boundary=pressure_boundary,
187190
density_rest=density_rest, cache...)
188191

@@ -261,6 +264,10 @@ system_sound_speed(system::OpenBoundarySystem) = system_sound_speed(system.fluid
261264

262265
@inline hydrodynamic_mass(system::OpenBoundarySystem, particle) = system.mass[particle]
263266

267+
@propagate_inbounds function current_velocity(v, system::OpenBoundarySystem)
268+
return view(v, 1:ndims(system), :)
269+
end
270+
264271
@inline function current_density(v, system::OpenBoundarySystem)
265272
return system.cache.density
266273
end
@@ -306,6 +313,9 @@ function update_boundary_interpolation!(system::OpenBoundarySystem, v, u, v_ode,
306313
semi, t)
307314
update_boundary_model!(system, system.boundary_model, v, u, v_ode, u_ode, semi, t)
308315
update_shifting!(system, shifting_technique(system), v, u, v_ode, u_ode, semi)
316+
317+
@trixi_timeit timer() "update density diffusion" update!(density_diffusion(system),
318+
v, u, system, semi)
309319
end
310320

311321
# This function is called by the `UpdateCallback`, as the integrator array might be modified
@@ -397,8 +407,8 @@ function check_domain!(system, v, u, v_ode, u_ode, semi)
397407
v, u, v_fluid, u_fluid)
398408
end
399409

400-
update_system_buffer!(system.buffer, semi)
401-
update_system_buffer!(fluid_system.buffer, semi)
410+
update_system_buffer!(system.buffer)
411+
update_system_buffer!(fluid_system.buffer)
402412

403413
fluid_candidates .= false
404414

@@ -428,8 +438,8 @@ function check_domain!(system, v, u, v_ode, u_ode, semi)
428438
v, u, v_fluid, u_fluid)
429439
end
430440

431-
update_system_buffer!(system.buffer, semi)
432-
update_system_buffer!(fluid_system.buffer, semi)
441+
update_system_buffer!(system.buffer)
442+
update_system_buffer!(fluid_system.buffer)
433443

434444
# Since particles have been transferred, the neighborhood searches must be updated
435445
update_nhs!(semi, u_ode)
@@ -453,7 +463,7 @@ end
453463
# to determine if it exited the boundary zone through the free surface (outflow).
454464
if dot(relative_position, boundary_zone.face_normal) < 0
455465
# Particle is outside the fluid domain
456-
deactivate_particle!(system, particle, u)
466+
deactivate_particle!(system, particle, v, u)
457467

458468
return system
459469
end
@@ -475,7 +485,7 @@ end
475485
# Verify the particle remains inside the boundary zone after the reset; deactivate it if not.
476486
particle_coords = current_coords(u, system, particle)
477487
if !is_in_boundary_zone(boundary_zone, particle_coords)
478-
deactivate_particle!(system, particle, u)
488+
deactivate_particle!(system, particle, v, u)
479489

480490
return system
481491
end
@@ -494,7 +504,7 @@ end
494504
transfer_particle!(system, fluid_system, particle, particle_new, v, u, v_fluid, u_fluid)
495505

496506
# Deactivate particle in interior domain
497-
deactivate_particle!(fluid_system, particle, u_fluid)
507+
deactivate_particle!(fluid_system, particle, v_fluid, u_fluid)
498508

499509
return fluid_system
500510
end

src/schemes/fluid/entropically_damped_sph/rhs.jl

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,27 @@ function interact!(dv, v_particle_system, u_particle_system,
6868
sound_speed, m_a, m_b, rho_a, rho_b,
6969
grad_kernel)
7070

71-
# Extra terms in the momentum equation when using a shifting technique
72-
dv_tvf = @inbounds dv_shifting(shifting_technique(particle_system),
73-
particle_system, neighbor_system,
74-
v_particle_system, v_neighbor_system,
75-
particle, neighbor, m_a, m_b, rho_a, rho_b,
76-
pos_diff, distance, grad_kernel, correction)
77-
78-
dv_surface_tension = surface_tension_force(surface_tension_a, surface_tension_b,
79-
particle_system, neighbor_system,
80-
particle, neighbor, pos_diff, distance,
81-
rho_a, rho_b, grad_kernel)
71+
dv_particle = Ref(dv_pressure + dv_viscosity_)
8272

83-
dv_adhesion = adhesion_force(surface_tension_a, particle_system, neighbor_system,
84-
particle, neighbor, pos_diff, distance)
85-
86-
dv_particle = dv_pressure + dv_viscosity_ + dv_tvf + dv_surface_tension +
87-
dv_adhesion
73+
# Extra terms in the momentum equation when using a shifting technique
74+
@inbounds dv_shifting!(dv_particle, shifting_technique(particle_system),
75+
particle_system, neighbor_system,
76+
v_particle_system, v_neighbor_system,
77+
particle, neighbor, m_a, m_b, rho_a, rho_b,
78+
pos_diff, distance, grad_kernel, correction)
79+
80+
@inbounds surface_tension_force!(dv_particle, surface_tension_a,
81+
surface_tension_b,
82+
particle_system, neighbor_system,
83+
particle, neighbor, pos_diff, distance,
84+
rho_a, rho_b, grad_kernel, 1)
85+
86+
@inbounds adhesion_force!(dv_particle, surface_tension_a, particle_system,
87+
neighbor_system,
88+
particle, neighbor, pos_diff, distance)
8889

8990
for i in 1:ndims(particle_system)
90-
@inbounds dv[i, particle] += dv_particle[i]
91+
@inbounds dv[i, particle] += dv_particle[][i]
9192
end
9293

9394
v_diff = current_velocity(v_particle_system, particle_system, particle) -

src/schemes/fluid/fluid.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ end
143143
v_particle_system, v_neighbor_system,
144144
particle, neighbor, pos_diff, distance,
145145
m_b, rho_a, rho_b, grad_kernel)
146+
return continuity_equation!(dv, particle_system, neighbor_system, v_particle_system,
147+
v_neighbor_system, particle, neighbor, pos_diff, distance,
148+
m_b, rho_a, rho_b, grad_kernel)
149+
end
150+
151+
@propagate_inbounds function continuity_equation!(dv, particle_system, neighbor_system,
152+
v_particle_system, v_neighbor_system,
153+
particle, neighbor, pos_diff, distance,
154+
m_b, rho_a, rho_b, grad_kernel)
146155
vdiff = current_velocity(v_particle_system, particle_system, particle) -
147156
current_velocity(v_neighbor_system, neighbor_system, neighbor)
148157

0 commit comments

Comments
 (0)