Skip to content

Commit d59d51f

Browse files
committed
Speed up continuity equation
1 parent 577f2ba commit d59d51f

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

src/schemes/fluid/fluid.jl

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,38 @@ end
136136
return dv
137137
end
138138

139-
# This formulation was chosen to be consistent with the used pressure_acceleration formulations
140-
@propagate_inbounds function continuity_equation!(dv, density_calculator::ContinuityDensity,
139+
# For compatibility with the old continuity equation function
140+
@propagate_inbounds function continuity_equation!(dv::AbstractArray,
141+
density_calculator::ContinuityDensity,
141142
particle_system::AbstractFluidSystem,
142143
neighbor_system,
143144
v_particle_system, v_neighbor_system,
144145
particle, neighbor, pos_diff, distance,
145146
m_b, rho_a, rho_b, grad_kernel)
147+
drho_particle = Ref(zero(rho_a))
146148
vdiff = current_velocity(v_particle_system, particle_system, particle) -
147149
current_velocity(v_neighbor_system, neighbor_system, neighbor)
148150

151+
continuity_equation!(drho_particle, density_calculator, particle_system,
152+
neighbor_system, v_particle_system, v_neighbor_system,
153+
particle, neighbor, pos_diff, distance,
154+
m_b, rho_a, rho_b, vdiff, grad_kernel)
155+
dv[end, particle] += drho_particle[]
156+
end
157+
158+
# This formulation was chosen to be consistent with the used pressure_acceleration formulations
159+
@propagate_inbounds function continuity_equation!(drho_particle,
160+
density_calculator::ContinuityDensity,
161+
particle_system::AbstractFluidSystem,
162+
neighbor_system,
163+
v_particle_system, v_neighbor_system,
164+
particle, neighbor, pos_diff, distance,
165+
m_b, rho_a, rho_b, vdiff, grad_kernel)
149166
vdiff += continuity_equation_shifting_term(shifting_technique(particle_system),
150167
particle_system, neighbor_system,
151168
particle, neighbor, rho_a, rho_b)
152169

153-
dv[end, particle] += rho_a / rho_b * m_b * dot(vdiff, grad_kernel)
170+
drho_particle[] += div_fast(rho_a, rho_b) * m_b * dot(vdiff, grad_kernel)
154171

155172
# Artificial density diffusion should only be applied to systems representing a fluid
156173
# with the same physical properties i.e. density and viscosity.

src/schemes/fluid/weakly_compressible_sph/rhs.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ function interact!(dv, v_particle_system, u_particle_system,
5555
v_b, rho_b = @inbounds velocity_and_density(v_neighbor_system, neighbor_system,
5656
neighbor)
5757
rho_mean = (rho_a + rho_b) / 2
58+
vdiff = v_a - v_b
5859

5960
# The following call is equivalent to
6061
# `p_b = current_pressure(v_neighbor_system, neighbor_system, neighbor)`
@@ -107,15 +108,17 @@ function interact!(dv, v_particle_system, u_particle_system,
107108

108109
# TODO If variable smoothing_length is used, this should use the neighbor smoothing length
109110
# Propagate `@inbounds` to the continuity equation, which accesses particle data
110-
@inbounds continuity_equation!(dv, density_calculator, particle_system,
111+
@inbounds continuity_equation!(drho_particle, density_calculator, particle_system,
111112
neighbor_system, v_particle_system,
112113
v_neighbor_system, particle, neighbor,
113-
pos_diff, distance, m_b, rho_a, rho_b, grad_kernel)
114+
pos_diff, distance, m_b, rho_a, rho_b, vdiff,
115+
grad_kernel)
114116
end
115117

116118
for i in eachindex(dv_particle[])
117119
@inbounds dv[i, particle] += dv_particle[][i]
118120
end
121+
@inbounds dv[end, particle] += drho_particle[]
119122
end
120123

121124
return dv

0 commit comments

Comments
 (0)