Skip to content

Commit 379db88

Browse files
authored
Make continuity_equation! and density_diffusion! work on Ref values (#1130)
* Make `continuity_equation!` and `density_diffusion!` work on Ref values * Fix writing drho to dv for summation density * Implement copilot suggestions * Fix density diffusion smoothing length * Fix missing suggestion from #1043 * Fix * Fix `continuity_equation!` functions
1 parent 419d5b9 commit 379db88

File tree

8 files changed

+136
-85
lines changed

8 files changed

+136
-85
lines changed

src/schemes/boundary/open_boundary/rhs.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,17 @@ function interact!(dv, v_particle_system, u_particle_system,
7777
@inbounds dv[i, particle] += dv_particle[i]
7878
end
7979

80-
v_diff = current_velocity(v_particle_system, particle_system, particle) -
81-
current_velocity(v_neighbor_system, neighbor_system, neighbor)
80+
v_a = current_velocity(v_particle_system, particle_system, particle)
81+
v_b = current_velocity(v_neighbor_system, neighbor_system, neighbor)
82+
v_diff = v_a - v_b
8283

8384
# Propagate `@inbounds` to the continuity equation, which accesses particle data
84-
@inbounds continuity_equation!(dv, particle_system, neighbor_system,
85-
v_particle_system, v_neighbor_system, particle,
86-
neighbor, pos_diff, distance, m_b, rho_a, rho_b,
87-
grad_kernel)
85+
drho_particle = Ref(zero(rho_a))
86+
@inbounds continuity_equation!(drho_particle,
87+
particle_system, neighbor_system,
88+
particle, neighbor, pos_diff, distance,
89+
m_b, rho_a, rho_b, v_a, v_b, grad_kernel)
90+
dv[end, particle] += drho_particle[]
8891

8992
# Open boundary pressure evolution matches the corresponding fluid system:
9093
# - EDAC: Compute pressure evolution like the fluid system

src/schemes/boundary/wall_boundary/rhs.jl

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,14 @@ function interact!(dv, v_particle_system, u_particle_system,
4545
rho_a = current_density(v_particle_system, particle_system, particle)
4646
rho_b = current_density(v_neighbor_system, neighbor_system, neighbor)
4747

48-
v_diff = current_velocity(v_particle_system, particle_system, particle) -
49-
current_velocity(v_neighbor_system, neighbor_system, neighbor)
48+
v_a = current_velocity(v_particle_system, particle_system, particle)
49+
v_b = current_velocity(v_neighbor_system, neighbor_system, neighbor)
5050

51-
continuity_equation!(dv, density_calculator(neighbor_system),
52-
m_b, rho_a, rho_b, v_diff, grad_kernel, particle)
51+
drho_particle = Ref(zero(rho_a))
52+
continuity_equation!(drho_particle, density_calculator(neighbor_system),
53+
m_b, rho_a, rho_b, v_a, v_b, grad_kernel, particle)
54+
55+
dv[end, particle] += drho_particle[]
5356
end
5457

5558
return dv
@@ -58,13 +61,19 @@ end
5861
# This is the derivative of the density summation, which is compatible with the
5962
# `SummationDensity` pressure acceleration.
6063
# Energy preservation tests will fail with the other formulation.
61-
function continuity_equation!(dv, fluid_density_calculator::SummationDensity,
62-
m_b, rho_a, rho_b, v_diff, grad_kernel, particle)
63-
dv[end, particle] += m_b * dot(v_diff, grad_kernel)
64+
@propagate_inbounds function continuity_equation!(drho_particle, ::SummationDensity,
65+
m_b, rho_a, rho_b, v_a, v_b,
66+
grad_kernel, particle)
67+
drho_particle[] += m_b * dot(v_a - v_b, grad_kernel)
68+
69+
return drho_particle
6470
end
6571

6672
# This is identical to the continuity equation of the fluid
67-
function continuity_equation!(dv, fluid_density_calculator::ContinuityDensity,
68-
m_b, rho_a, rho_b, v_diff, grad_kernel, particle)
69-
dv[end, particle] += rho_a / rho_b * m_b * dot(v_diff, grad_kernel)
73+
@propagate_inbounds function continuity_equation!(drho_particle, ::ContinuityDensity,
74+
m_b, rho_a, rho_b, v_a, v_b,
75+
grad_kernel, particle)
76+
drho_particle[] += rho_a / rho_b * m_b * dot(v_a - v_b, grad_kernel)
77+
78+
return drho_particle
7079
end

src/schemes/fluid/entropically_damped_sph/rhs.jl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,24 @@ function interact!(dv, v_particle_system, u_particle_system,
9393
@inbounds dv[i, particle] += dv_particle[][i]
9494
end
9595

96-
v_diff = current_velocity(v_particle_system, particle_system, particle) -
97-
current_velocity(v_neighbor_system, neighbor_system, neighbor)
96+
v_a = current_velocity(v_particle_system, particle_system, particle)
97+
v_b = current_velocity(v_neighbor_system, neighbor_system, neighbor)
98+
v_diff = v_a - v_b
9899

99100
pressure_evolution!(dv, particle_system, neighbor_system, v_diff, grad_kernel,
100101
particle, neighbor, pos_diff, distance,
101102
sound_speed, m_a, m_b, p_a, p_b, rho_a, rho_b, nu_edac)
102103

104+
drho_particle = Ref(zero(rho_a))
105+
103106
# TODO If variable smoothing_length is used, this should use the neighbor smoothing length
104107
# Propagate `@inbounds` to the continuity equation, which accesses particle data
105-
@inbounds continuity_equation!(dv, density_calculator, particle_system,
106-
neighbor_system, v_particle_system,
107-
v_neighbor_system, particle, neighbor,
108-
pos_diff, distance, m_b, rho_a, rho_b, grad_kernel)
108+
@inbounds continuity_equation!(drho_particle, density_calculator,
109+
particle_system, neighbor_system,
110+
particle, neighbor, pos_diff, distance,
111+
m_b, rho_a, rho_b, v_a, v_b, grad_kernel)
112+
113+
@inbounds write_drho_particle!(dv, density_calculator, drho_particle, particle)
109114
end
110115

111116
return dv

src/schemes/fluid/fluid.jl

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -128,51 +128,62 @@ function compute_density!(system, u, u_ode, semi, ::SummationDensity)
128128
end
129129

130130
# With 'SummationDensity', density is calculated in wcsph/system.jl:compute_density!
131-
@inline function continuity_equation!(dv, density_calculator::SummationDensity,
131+
@inline function continuity_equation!(drho_particle, ::SummationDensity,
132132
particle_system, neighbor_system,
133-
v_particle_system, v_neighbor_system,
134133
particle, neighbor, pos_diff, distance,
135-
m_b, rho_a, rho_b, grad_kernel)
136-
return dv
134+
m_b, rho_a, rho_b, v_a, v_b, grad_kernel)
135+
return drho_particle
137136
end
138137

139138
# This formulation was chosen to be consistent with the used pressure_acceleration formulations
140-
@propagate_inbounds function continuity_equation!(dv, density_calculator::ContinuityDensity,
139+
@propagate_inbounds function continuity_equation!(drho_particle,
140+
::ContinuityDensity,
141141
particle_system::AbstractFluidSystem,
142142
neighbor_system,
143-
v_particle_system, v_neighbor_system,
144143
particle, neighbor, pos_diff, distance,
145-
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)
144+
m_b, rho_a, rho_b, v_a, v_b, grad_kernel)
145+
continuity_equation!(drho_particle, particle_system, neighbor_system,
146+
particle, neighbor, pos_diff, distance,
147+
m_b, rho_a, rho_b, v_a, v_b, grad_kernel)
149148
end
150149

151-
@propagate_inbounds function continuity_equation!(dv, particle_system, neighbor_system,
152-
v_particle_system, v_neighbor_system,
150+
@propagate_inbounds function continuity_equation!(drho_particle,
151+
particle_system, neighbor_system,
153152
particle, neighbor, pos_diff, distance,
154-
m_b, rho_a, rho_b, grad_kernel)
155-
vdiff = current_velocity(v_particle_system, particle_system, particle) -
156-
current_velocity(v_neighbor_system, neighbor_system, neighbor)
153+
m_b, rho_a, rho_b, v_a, v_b, grad_kernel)
154+
v_diff = v_a - v_b
157155

158-
vdiff += continuity_equation_shifting_term(shifting_technique(particle_system),
156+
v_diff = continuity_equation_shifting_term(v_diff,
157+
shifting_technique(particle_system),
159158
particle_system, neighbor_system,
160159
particle, neighbor, rho_a, rho_b)
161160

162161
# Since this is one of the most performance critical functions, using fast divisions
163162
# here gives a significant speedup on GPUs.
164163
# See the docs page "Development" for more details on `div_fast`.
165-
dv[end, particle] += div_fast(rho_a, rho_b) * m_b * dot(vdiff, grad_kernel)
164+
drho_particle[] += div_fast(rho_a, rho_b) * m_b * dot(v_diff, grad_kernel)
166165

167166
# Artificial density diffusion should only be applied to systems representing a fluid
168167
# with the same physical properties i.e. density and viscosity.
169168
# TODO: shouldn't be applied to particles on the interface (depends on PR #539)
170169
if particle_system === neighbor_system
171-
density_diffusion!(dv, density_diffusion(particle_system),
172-
v_particle_system, particle, neighbor,
173-
pos_diff, distance, m_b, rho_a, rho_b, particle_system,
174-
grad_kernel)
170+
density_diffusion!(drho_particle, density_diffusion(particle_system),
171+
particle_system, particle, neighbor,
172+
pos_diff, distance, m_b, rho_a, rho_b, grad_kernel)
175173
end
174+
175+
return drho_particle
176+
end
177+
178+
@inline function write_drho_particle!(dv, density_calculator, drho_particle, particle)
179+
return dv
180+
end
181+
182+
@propagate_inbounds function write_drho_particle!(dv, ::ContinuityDensity,
183+
drho_particle, particle)
184+
dv[end, particle] += drho_particle[]
185+
186+
return dv
176187
end
177188

178189
function calculate_dt(v_ode, u_ode, cfl_number, system::AbstractFluidSystem, semi)

src/schemes/fluid/shifting_techniques.jl

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ end
4848
return dv_particle
4949
end
5050

51-
# Additional term(s) in the continuity equation due to the shifting technique
52-
@inline function continuity_equation_shifting_term(shifting, particle_system,
51+
# Add additional term(s) in the continuity equation due to the shifting technique
52+
# and return the modified term.
53+
@inline function continuity_equation_shifting_term(v_diff, shifting, particle_system,
5354
neighbor_system,
5455
particle, neighbor, rho_a, rho_b)
55-
return zero(SVector{ndims(particle_system), eltype(particle_system)})
56+
return v_diff
5657
end
5758

5859
@doc raw"""
@@ -356,7 +357,8 @@ end
356357
end
357358

358359
# `ParticleShiftingTechnique{<:Any, <:Any, true}` means `modify_continuity_equation=true`
359-
@propagate_inbounds function continuity_equation_shifting_term(shifting::ParticleShiftingTechnique{<:Any,
360+
@propagate_inbounds function continuity_equation_shifting_term(v_diff,
361+
shifting::ParticleShiftingTechnique{<:Any,
360362
<:Any,
361363
true},
362364
system, neighbor_system,
@@ -366,9 +368,10 @@ end
366368
delta_v_b = delta_v(neighbor_system, neighbor)
367369
delta_v_diff = delta_v_a - delta_v_b
368370

369-
second_term = second_continuity_equation_term(shifting.second_continuity_equation_term,
370-
delta_v_a, delta_v_b, rho_a, rho_b)
371-
return delta_v_diff + second_term
371+
shifting_term = second_continuity_equation_term(delta_v_diff,
372+
shifting.second_continuity_equation_term,
373+
delta_v_a, delta_v_b, rho_a, rho_b)
374+
return v_diff + shifting_term
372375
end
373376

374377
"""
@@ -381,15 +384,16 @@ See [`ParticleShiftingTechnique`](@ref).
381384
"""
382385
struct ContinuityEquationTermSun2019 end
383386

384-
@propagate_inbounds function second_continuity_equation_term(::ContinuityEquationTermSun2019,
387+
@propagate_inbounds function second_continuity_equation_term(v_diff,
388+
::ContinuityEquationTermSun2019,
385389
delta_v_a, delta_v_b,
386390
rho_a, rho_b)
387-
return delta_v_a + rho_b / rho_a * delta_v_b
391+
return v_diff + delta_v_a + rho_b / rho_a * delta_v_b
388392
end
389393

390-
@inline function second_continuity_equation_term(second_continuity_equation_term,
394+
@inline function second_continuity_equation_term(v_diff, second_continuity_equation_term,
391395
delta_v_a, delta_v_b, rho_a, rho_b)
392-
return zero(delta_v_a)
396+
return v_diff
393397
end
394398

395399
# `ParticleShiftingTechnique{<:Any, true}` means `update_everystage=true`
@@ -606,15 +610,16 @@ end
606610
return pressure_acceleration_continuity_density(m_a, m_b, rho_a, rho_b, p_a, p_b, W_a)
607611
end
608612

609-
@propagate_inbounds function continuity_equation_shifting_term(::TransportVelocityAdami{true},
613+
@propagate_inbounds function continuity_equation_shifting_term(v_diff,
614+
::TransportVelocityAdami{true},
610615
particle_system,
611616
neighbor_system,
612617
particle, neighbor,
613618
rho_a, rho_b)
614619
delta_v_diff = delta_v(particle_system, particle) -
615620
delta_v(neighbor_system, neighbor)
616621

617-
return delta_v_diff
622+
return v_diff + delta_v_diff
618623
end
619624

620625
function update_shifting!(system, shifting::TransportVelocityAdami, v, u, v_ode,

src/schemes/fluid/weakly_compressible_sph/density_diffusion.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,12 @@ function update!(density_diffusion::DensityDiffusionAntuono, v, u, system, semi)
213213
return density_diffusion
214214
end
215215

216-
@propagate_inbounds function density_diffusion!(dv,
216+
@propagate_inbounds function density_diffusion!(drho_particle,
217217
density_diffusion::AbstractDensityDiffusion,
218-
v_particle_system, particle, neighbor,
219-
pos_diff, distance, m_b, rho_a, rho_b,
220218
particle_system::Union{AbstractFluidSystem,
221219
OpenBoundarySystem{<:BoundaryModelDynamicalPressureZhang}},
222-
grad_kernel)
220+
particle, neighbor, pos_diff, distance,
221+
m_b, rho_a, rho_b, grad_kernel)
223222
# Density diffusion terms are all zero for distance zero.
224223
# If `skip_zero_distance` is `true`, we can assume that this function isn't called
225224
# for distance zero because these neighbors have already been skipped.
@@ -240,12 +239,12 @@ end
240239

241240
(; delta) = density_diffusion
242241
sound_speed = system_sound_speed(particle_system)
243-
dv[end, particle] += delta * smoothing_length_avg * sound_speed * density_diffusion_term
242+
drho_particle[] += delta * smoothing_length_avg * sound_speed * density_diffusion_term
244243
end
245244

246245
# Density diffusion `nothing` or interaction other than fluid-fluid
247-
@inline function density_diffusion!(dv, density_diffusion, v_particle_system, particle,
248-
neighbor, pos_diff, distance, m_b, rho_a, rho_b,
249-
particle_system, grad_kernel)
250-
return dv
246+
@inline function density_diffusion!(drho_particle, density_diffusion, particle_system,
247+
particle, neighbor, pos_diff, distance,
248+
m_b, rho_a, rho_b, grad_kernel)
249+
return drho_particle
251250
end

src/schemes/fluid/weakly_compressible_sph/rhs.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ function interact!(dv, v_particle_system, u_particle_system,
5151
rho_b = @inbounds current_density(v_neighbor_system, neighbor_system, neighbor)
5252
rho_mean = (rho_a + rho_b) / 2
5353

54+
v_a = @inbounds current_velocity(v_particle_system, particle_system, particle)
55+
v_b = @inbounds current_velocity(v_neighbor_system, neighbor_system, neighbor)
56+
5457
# Determine correction factors.
5558
# This can be ignored, as these are all 1 when no correction is used.
5659
(viscosity_correction, pressure_correction,
@@ -114,12 +117,16 @@ function interact!(dv, v_particle_system, u_particle_system,
114117
# debug_array[i, particle] += dv_pressure[i]
115118
end
116119

120+
drho_particle = Ref(zero(rho_a))
121+
117122
# TODO If variable smoothing_length is used, this should use the neighbor smoothing length
118123
# Propagate `@inbounds` to the continuity equation, which accesses particle data
119-
@inbounds continuity_equation!(dv, density_calculator, particle_system,
120-
neighbor_system, v_particle_system,
121-
v_neighbor_system, particle, neighbor,
122-
pos_diff, distance, m_b, rho_a, rho_b, grad_kernel)
124+
@inbounds continuity_equation!(drho_particle, density_calculator,
125+
particle_system, neighbor_system,
126+
particle, neighbor, pos_diff, distance,
127+
m_b, rho_a, rho_b, v_a, v_b, grad_kernel)
128+
129+
@inbounds write_drho_particle!(dv, density_calculator, drho_particle, particle)
123130
end
124131
# Debug example
125132
# periodic_box = neighborhood_search.periodic_box

0 commit comments

Comments
 (0)