Skip to content

Commit 38f5f07

Browse files
committed
Merge branch 'main' into dev
2 parents c1e8589 + 8d136f2 commit 38f5f07

12 files changed

Lines changed: 134 additions & 127 deletions

File tree

src/callbacks/update.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,8 @@ end
5555
function initial_update!(cb::UpdateCallback, u, t, integrator)
5656
semi = integrator.p
5757

58-
# Tell systems that `UpdateCallback` is used
59-
foreach_system(semi) do system
60-
set_callback_flag!(system, true)
61-
end
58+
# Tell the semidiscretization that the `UpdateCallback` is used
59+
semi.update_callback_used[] = true
6260

6361
return cb(integrator)
6462
end

src/general/semidiscretization.jl

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,26 @@ semi = Semidiscretization(fluid_system, boundary_system,
4747
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
4848
```
4949
"""
50-
struct Semidiscretization{BACKEND, S, RU, RV, NS}
50+
struct Semidiscretization{BACKEND, S, RU, RV, NS, UCU}
5151
systems :: S
5252
ranges_u :: RU
5353
ranges_v :: RV
5454
neighborhood_searches :: NS
5555
parallelization_backend :: BACKEND
56+
update_callback_used :: UCU
5657

5758
# Dispatch at `systems` to distinguish this constructor from the one below when
5859
# 4 systems are passed.
5960
# This is an internal constructor only used in `test/count_allocations.jl`
6061
# and by Adapt.jl.
6162
function Semidiscretization(systems::Tuple, ranges_u, ranges_v, neighborhood_searches,
62-
parallelization_backend::PointNeighbors.ParallelizationBackend)
63+
parallelization_backend::PointNeighbors.ParallelizationBackend,
64+
update_callback_used)
6365
new{typeof(parallelization_backend), typeof(systems), typeof(ranges_u),
64-
typeof(ranges_v), typeof(neighborhood_searches)}(systems, ranges_u, ranges_v,
65-
neighborhood_searches,
66-
parallelization_backend)
66+
typeof(ranges_v), typeof(neighborhood_searches),
67+
typeof(update_callback_used)}(systems, ranges_u, ranges_v,
68+
neighborhood_searches, parallelization_backend,
69+
update_callback_used)
6770
end
6871
end
6972

@@ -92,8 +95,13 @@ function Semidiscretization(systems::Union{System, Nothing}...;
9295
for neighbor in systems)
9396
for system in systems)
9497

98+
# These will be set to true inside the `UpdateCallback`.
99+
# Some techniques require the use of this callback, and this flag can be used
100+
# to determine if the callback is used in a simulation.
101+
update_callback_used = Ref(false)
102+
95103
return Semidiscretization(systems, ranges_u, ranges_v, searches,
96-
parallelization_backend)
104+
parallelization_backend, update_callback_used)
97105
end
98106

99107
# Inline show function e.g. Semidiscretization(neighborhood_search=...)
@@ -328,7 +336,8 @@ function semidiscretize(semi, tspan; reset_threads=true)
328336
semi_new = Semidiscretization(set_system_links.(semi_.systems, Ref(semi_)),
329337
semi_.ranges_u, semi_.ranges_v,
330338
semi_.neighborhood_searches,
331-
semi_.parallelization_backend)
339+
semi_.parallelization_backend,
340+
semi_.update_callback_used)
332341
else
333342
semi_new = semi
334343
end
@@ -337,11 +346,11 @@ function semidiscretize(semi, tspan; reset_threads=true)
337346
foreach_system(semi_new) do system
338347
# Initialize this system
339348
initialize!(system, semi_new)
340-
341-
# Only for systems requiring the use of the `UpdateCallback`
342-
set_callback_flag!(system, false)
343349
end
344350

351+
# Reset callback flag that will be set by the `UpdateCallback`
352+
semi_new.update_callback_used[] = false
353+
345354
return DynamicalODEProblem(kick!, drift!, v0_ode, u0_ode, tspan, semi_new)
346355
end
347356

@@ -372,11 +381,11 @@ function restart_with!(semi, sol; reset_threads=true)
372381
u = wrap_u(sol.u[end].x[2], system, semi)
373382

374383
restart_with!(system, v, u)
375-
376-
# Only for systems requiring the use of the `UpdateCallback`
377-
set_callback_flag!(system, false)
378384
end
379385

386+
# Reset callback flag that will be set by the `UpdateCallback`
387+
semi.update_callback_used[] = false
388+
380389
return semi
381390
end
382391

@@ -887,7 +896,7 @@ end
887896
function check_update_callback(semi)
888897
foreach_system(semi) do system
889898
# This check will be optimized away if the system does not require the callback
890-
if requires_update_callback(system) && !update_callback_used(system)
899+
if requires_update_callback(system) && !semi.update_callback_used[]
891900
system_name = system |> typeof |> nameof
892901
throw(ArgumentError("`UpdateCallback` is required for `$system_name`"))
893902
end
@@ -1007,6 +1016,5 @@ function set_system_links(system::OpenBoundarySPHSystem, semi)
10071016
system.reference_pressure,
10081017
system.reference_density,
10091018
system.buffer,
1010-
system.update_callback_used,
10111019
system.cache)
10121020
end

src/general/system.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ end
149149

150150
# Only for systems requiring the use of the `UpdateCallback`
151151
@inline requires_update_callback(system) = false
152-
@inline update_callback_used(system) = false
153-
@inline set_callback_flag!(system, value) = system
154152

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

src/preprocessing/particle_packing/system.jl

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ For more information on the methods, see [particle packing](@ref particle_packin
5656
Recommended values are `0.8` or `0.9`.
5757
"""
5858
struct ParticlePackingSystem{S, F, NDIMS, ELTYPE <: Real, PR, C, AV,
59-
IC, M, D, K, N, SD, UCU} <: FluidSystem{NDIMS}
59+
IC, M, D, K, N, SD} <: FluidSystem{NDIMS}
6060
initial_condition :: IC
6161
advection_velocity :: AV
6262
mass :: M
@@ -73,7 +73,6 @@ struct ParticlePackingSystem{S, F, NDIMS, ELTYPE <: Real, PR, C, AV,
7373
signed_distances :: SD # Only for visualization
7474
particle_refinement :: PR
7575
buffer :: Nothing
76-
update_callback_used :: UCU
7776
cache :: C
7877

7978
# This constructor is necessary for Adapt.jl to work with this struct.
@@ -84,22 +83,20 @@ struct ParticlePackingSystem{S, F, NDIMS, ELTYPE <: Real, PR, C, AV,
8483
signed_distance_field,
8584
is_boundary, shift_length, neighborhood_search,
8685
signed_distances, particle_refinement, buffer,
87-
update_callback_used, fixed_system, cache,
88-
advection_velocity)
86+
fixed_system, cache, advection_velocity)
8987
return new{typeof(signed_distance_field), fixed_system, ndims(smoothing_kernel),
9088
eltype(density), typeof(particle_refinement), typeof(cache),
9189
typeof(advection_velocity), typeof(initial_condition), typeof(mass),
9290
typeof(density), typeof(smoothing_kernel), typeof(neighborhood_search),
93-
typeof(signed_distances),
94-
typeof(update_callback_used)}(initial_condition, advection_velocity,
95-
mass, density, particle_spacing,
96-
smoothing_kernel,
97-
smoothing_length_interpolation,
98-
background_pressure, place_on_shell,
99-
signed_distance_field, is_boundary,
100-
shift_length, neighborhood_search,
101-
signed_distances, particle_refinement,
102-
buffer, update_callback_used, cache)
91+
typeof(signed_distances)}(initial_condition, advection_velocity,
92+
mass, density, particle_spacing,
93+
smoothing_kernel,
94+
smoothing_length_interpolation,
95+
background_pressure, place_on_shell,
96+
signed_distance_field, is_boundary,
97+
shift_length, neighborhood_search,
98+
signed_distances, particle_refinement,
99+
buffer, cache)
103100
end
104101
end
105102

@@ -167,8 +164,7 @@ function ParticlePackingSystem(shape::InitialCondition;
167164
background_pressure, place_on_shell, signed_distance_field,
168165
is_boundary, shift_length, nhs,
169166
fill(zero(ELTYPE), nparticles(shape)), particle_refinement,
170-
nothing, Ref(false), fixed_system, cache,
171-
advection_velocity)
167+
nothing, fixed_system, cache, advection_velocity)
172168
end
173169

174170
function Base.show(io::IO, system::ParticlePackingSystem)
@@ -219,13 +215,6 @@ end
219215
end
220216

221217
@inline requires_update_callback(system::ParticlePackingSystem) = true
222-
@inline update_callback_used(system::ParticlePackingSystem) = system.update_callback_used[]
223-
224-
function set_callback_flag!(system::ParticlePackingSystem, value)
225-
system.update_callback_used[] = value
226-
227-
return system
228-
end
229218

230219
function write2vtk!(vtk, v, u, t, system::ParticlePackingSystem; write_meta_data=true)
231220
vtk["velocity"] = [advection_velocity(v, system, particle)

src/schemes/boundary/open_boundary/system.jl

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,47 +37,44 @@ Open boundary system for in- and outflow particles.
3737
It is GPU-compatible (e.g., with CUDA.jl and AMDGPU.jl), but currently **not** supported with Metal.jl.
3838
"""
3939
struct OpenBoundarySPHSystem{BM, ELTYPE, NDIMS, IC, FS, FSI, ARRAY1D, BC, FC, BZ, RV,
40-
RP, RD, B, UCU, C} <: System{NDIMS}
41-
boundary_model :: BM
42-
initial_condition :: IC
43-
fluid_system :: FS
44-
fluid_system_index :: FSI
45-
smoothing_length :: ELTYPE
46-
mass :: ARRAY1D # Array{ELTYPE, 1}: [particle]
47-
density :: ARRAY1D # Array{ELTYPE, 1}: [particle]
48-
volume :: ARRAY1D # Array{ELTYPE, 1}: [particle]
49-
pressure :: ARRAY1D # Array{ELTYPE, 1}: [particle]
50-
boundary_candidates :: BC # Array{UInt32, 1}: [particle]
51-
fluid_candidates :: FC # Array{UInt32, 1}: [particle]
52-
boundary_zone :: BZ
53-
reference_velocity :: RV
54-
reference_pressure :: RP
55-
reference_density :: RD
56-
buffer :: B
57-
update_callback_used :: UCU
58-
cache :: C
40+
RP, RD, B, C} <: System{NDIMS}
41+
boundary_model :: BM
42+
initial_condition :: IC
43+
fluid_system :: FS
44+
fluid_system_index :: FSI
45+
smoothing_length :: ELTYPE
46+
mass :: ARRAY1D # Array{ELTYPE, 1}: [particle]
47+
density :: ARRAY1D # Array{ELTYPE, 1}: [particle]
48+
volume :: ARRAY1D # Array{ELTYPE, 1}: [particle]
49+
pressure :: ARRAY1D # Array{ELTYPE, 1}: [particle]
50+
boundary_candidates :: BC # Array{UInt32, 1}: [particle]
51+
fluid_candidates :: FC # Array{UInt32, 1}: [particle]
52+
boundary_zone :: BZ
53+
reference_velocity :: RV
54+
reference_pressure :: RP
55+
reference_density :: RD
56+
buffer :: B
57+
cache :: C
5958
end
6059

6160
function OpenBoundarySPHSystem(boundary_model, initial_condition, fluid_system,
6261
fluid_system_index, smoothing_length, mass, density, volume,
6362
pressure, boundary_candidates, fluid_candidates,
6463
boundary_zone, reference_velocity,
65-
reference_pressure, reference_density, buffer,
66-
update_callback_used, cache)
64+
reference_pressure, reference_density, buffer, cache)
6765
OpenBoundarySPHSystem{typeof(boundary_model), eltype(mass), ndims(initial_condition),
6866
typeof(initial_condition), typeof(fluid_system),
6967
typeof(fluid_system_index), typeof(mass),
7068
typeof(boundary_candidates), typeof(fluid_candidates),
7169
typeof(boundary_zone), typeof(reference_velocity),
7270
typeof(reference_pressure), typeof(reference_density),
73-
typeof(buffer), typeof(update_callback_used),
71+
typeof(buffer),
7472
typeof(cache)}(boundary_model, initial_condition, fluid_system,
7573
fluid_system_index, smoothing_length, mass,
7674
density, volume, pressure, boundary_candidates,
7775
fluid_candidates, boundary_zone,
7876
reference_velocity, reference_pressure,
79-
reference_density, buffer, update_callback_used,
80-
cache)
77+
reference_density, buffer, cache)
8178
end
8279

8380
function OpenBoundarySPHSystem(boundary_zone::BoundaryZone;
@@ -149,8 +146,6 @@ function OpenBoundarySPHSystem(boundary_zone::BoundaryZone;
149146
reference_density, reference_velocity,
150147
reference_pressure)
151148

152-
# These will be set later
153-
update_callback_used = Ref(false)
154149
fluid_system_index = Ref(0)
155150

156151
smoothing_length = initial_smoothing_length(fluid_system)
@@ -162,8 +157,7 @@ function OpenBoundarySPHSystem(boundary_zone::BoundaryZone;
162157
fluid_system_index, smoothing_length, mass, density,
163158
volume, pressure, boundary_candidates, fluid_candidates,
164159
boundary_zone, reference_velocity_,
165-
reference_pressure_, reference_density_, buffer,
166-
update_callback_used, cache)
160+
reference_pressure_, reference_density_, buffer, cache)
167161
end
168162

169163
function create_cache_open_boundary(boundary_model, initial_condition,
@@ -226,14 +220,8 @@ end
226220
return ELTYPE
227221
end
228222

223+
# The `UpdateCallback` is required to update particle positions between time steps
229224
@inline requires_update_callback(system::OpenBoundarySPHSystem) = true
230-
@inline update_callback_used(system::OpenBoundarySPHSystem) = system.update_callback_used[]
231-
232-
function set_callback_flag!(system::OpenBoundarySPHSystem, value)
233-
system.update_callback_used[] = value
234-
235-
return system
236-
end
237225

238226
function corresponding_fluid_system(system::OpenBoundarySPHSystem, semi)
239227
return system.fluid_system

src/schemes/fluid/entropically_damped_sph/rhs.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ function interact!(dv, v_particle_system, u_particle_system,
2727
p_a = current_pressure(v_particle_system, particle_system, particle)
2828
p_b = current_pressure(v_neighbor_system, neighbor_system, neighbor)
2929

30-
# This technique is for a more robust `pressure_acceleration` but only with TVF.
31-
# It results only in significant improvement for EDAC and not for WCSPH.
32-
# See Ramachandran (2019) p. 582
33-
# Note that the return value is zero when not using EDAC with TVF.
30+
# This technique by Basa et al. 2017 (10.1002/fld.1927) aims to reduce numerical
31+
# errors due to large pressures by subtracting the average pressure of neighboring
32+
# particles.
33+
# It results in significant improvement for EDAC, especially with TVF,
34+
# but not for WCSPH, according to Ramachandran & Puri (2019), Section 3.2.
35+
# Note that the return value is zero when not using average pressure reduction.
3436
p_avg = average_pressure(particle_system, particle)
3537

3638
m_a = hydrodynamic_mass(particle_system, particle)

0 commit comments

Comments
 (0)