forked from trixi-framework/TrixiParticles.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.jl
More file actions
409 lines (333 loc) · 18 KB
/
system.jl
File metadata and controls
409 lines (333 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
"""
ParticlePackingSystem(shape::InitialCondition;
signed_distance_field::Union{SignedDistanceField, Nothing},
smoothing_kernel=SchoenbergQuinticSplineKernel{ndims(shape)}(),
smoothing_length=shape.particle_spacing,
smoothing_length_interpolation=smoothing_length,
is_boundary=false, boundary_compress_factor=1,
neighborhood_search=GridNeighborhoodSearch{ndims(shape)}(),
background_pressure, tlsph=false, fixed_system=false)
System to generate body-fitted particles for complex shapes.
For more information on the methods, see description for [particle packing](@ref particle_packing).
# Arguments
- `shape`: [`InitialCondition`](@ref) to be packed.
# Keywords
- `background_pressure`: Constant background pressure to physically pack the particles.
A large `background_pressure` can cause high accelerations
which requires a properly adjusted time step.
- `tlsph`: With the [`TotalLagrangianSPHSystem`](@ref), particles need to be placed
on the boundary of the shape and not half a particle spacing away,
as for fluids. When `tlsph=true`, particles will be placed
on the boundary of the shape.
- `is_boundary`: When `shape` is inside the geometry that was used to create
`signed_distance_field`, set `is_boundary=false`.
Otherwise (`shape` is the sampled boundary), set `is_boundary=true`.
The thickness of the boundary is specified by creating
`signed_distance_field` with:
- `use_for_boundary_packing=true`
- `max_signed_distance=boundary_thickness`
See [`SignedDistanceField`](@ref).
- `fixed_system`: When set to `true`, the system remains static, meaning particles
will not move and the `InitialCondition` will stay unchanged.
This is useful when the system is packed together with another
(non-fixed) `ParticlePackingSystem`.
In this case, no `SignedDistanceField` is required for both
the fixed and non-fixed system (use `nothing` as signed distance field).
- `signed_distance_field`: To constrain particles onto the surface, the information about
the signed distance from a particle to a face is required.
The precalculated signed distances will be interpolated
to each particle during the packing procedure.
- `smoothing_kernel`: Smoothing kernel to be used for this system.
See [Smoothing Kernels](@ref smoothing_kernel).
- `smoothing_length`: Smoothing length to be used for the gradient estimation.
See [Smoothing Kernels](@ref smoothing_kernel).
- `smoothing_length_interpolation`: Smoothing length to be used for interpolating the `SignedDistanceField` information.
The default is `smoothing_length_interpolation = smoothing_length`.
- `boundary_compress_factor`: Factor to compress the boundary particles by reducing the boundary thickness by a factor of `boundary_compress_factor`.
The default value is `1`, which means no compression.
Compression can be useful for highly convex geometries,
where the boundary volume increases significantly while the mass of the boundary particles remains constant.
Recommended values are `0.8` or `0.9`.
"""
struct ParticlePackingSystem{S, F, NDIMS, ELTYPE <: Real, PR, C, AV,
IC, M, D, K, N, SD, UCU} <: FluidSystem{NDIMS}
initial_condition :: IC
advection_velocity :: AV
mass :: M
density :: D
particle_spacing :: ELTYPE
smoothing_kernel :: K
smoothing_length_interpolation :: ELTYPE
background_pressure :: ELTYPE
tlsph :: Bool
signed_distance_field :: S
is_boundary :: Bool
shift_length :: ELTYPE
neighborhood_search :: N
signed_distances :: SD # Only for visualization
particle_refinement :: PR
buffer :: Nothing
update_callback_used :: UCU
cache :: C
# This constructor is necessary for Adapt.jl to work with this struct.
# See the comments in general/gpu.jl for more details.
function ParticlePackingSystem(initial_condition, mass, density, particle_spacing,
smoothing_kernel, smoothing_length_interpolation,
background_pressure, tlsph, signed_distance_field,
is_boundary, shift_length, neighborhood_search,
signed_distances, particle_refinement, buffer,
update_callback_used, fixed_system, cache,
advection_velocity)
return new{typeof(signed_distance_field), fixed_system, ndims(smoothing_kernel),
eltype(density), typeof(particle_refinement), typeof(cache),
typeof(advection_velocity), typeof(initial_condition), typeof(mass),
typeof(density), typeof(smoothing_kernel), typeof(neighborhood_search),
typeof(signed_distances),
typeof(update_callback_used)}(initial_condition, advection_velocity,
mass, density, particle_spacing,
smoothing_kernel,
smoothing_length_interpolation,
background_pressure, tlsph,
signed_distance_field, is_boundary,
shift_length, neighborhood_search,
signed_distances, particle_refinement,
buffer, update_callback_used, cache)
end
end
function ParticlePackingSystem(shape::InitialCondition;
signed_distance_field::Union{SignedDistanceField, Nothing},
smoothing_kernel=SchoenbergQuinticSplineKernel{ndims(shape)}(),
smoothing_length=shape.particle_spacing,
smoothing_length_interpolation=smoothing_length,
is_boundary=false, boundary_compress_factor=1,
neighborhood_search=GridNeighborhoodSearch{ndims(shape)}(),
background_pressure, tlsph=false, fixed_system=false)
NDIMS = ndims(shape)
ELTYPE = eltype(shape)
mass = copy(shape.mass)
density = copy(shape.density)
particle_refinement = nothing
if ndims(smoothing_kernel) != NDIMS
throw(ArgumentError("smoothing kernel dimensionality must be $NDIMS for a $(NDIMS)D problem"))
end
# Create neighborhood search for `ParticlePackingSystem` with the positions
# of the `SignedDistanceField` as neighbors.
# This is an intern NHS and is thus not organized by `Semidiscretization`.
if isnothing(signed_distance_field)
nhs = nothing
@info "No `SignedDistanceField` provided. Particles will not be constraint onto a geometric surface."
else
nhs_ = isnothing(neighborhood_search) ? TrivialNeighborhoodSearch{NDIMS}() :
neighborhood_search
nhs = copy_neighborhood_search(nhs_,
compact_support(smoothing_kernel,
smoothing_length_interpolation),
length(signed_distance_field.positions))
# Initialize neighborhood search with signed distances
PointNeighbors.initialize_grid!(nhs, stack(signed_distance_field.positions))
end
# If `distance_signed >= -shift_length`, the particle position is modified
# by a surface bounding:
# `particle_position -= (distance_signed + shift_length) * normal_vector`,
# where `normal_vector` is the normal vector to the surface of the geometry
# and `distance_signed` is the level-set value at the particle position,
# which means the signed distance to the surface.
# Its value is negative if the particle is inside the geometry.
# Otherwise (if outside), the value is positive.
if is_boundary
offset = tlsph ? shape.particle_spacing : shape.particle_spacing / 2
shift_length = -boundary_compress_factor *
signed_distance_field.max_signed_distance - offset
else
shift_length = tlsph ? zero(ELTYPE) : shape.particle_spacing / 2
end
cache = (; create_cache_refinement(shape, particle_refinement, smoothing_length)...)
advection_velocity = copy(shape.velocity)
return ParticlePackingSystem(shape, mass, density, shape.particle_spacing,
smoothing_kernel, smoothing_length_interpolation,
background_pressure, tlsph, signed_distance_field,
is_boundary, shift_length, nhs,
fill(zero(ELTYPE), nparticles(shape)), particle_refinement,
nothing, Ref(false), fixed_system, cache,
advection_velocity)
end
function Base.show(io::IO, system::ParticlePackingSystem)
@nospecialize system # reduce precompilation time
print(io, "ParticlePackingSystem{", ndims(system), "}(")
print(io, "", system.smoothing_kernel)
print(io, ") with ", nparticles(system), " particles")
end
function Base.show(io::IO, ::MIME"text/plain", system::ParticlePackingSystem)
@nospecialize system # reduce precompilation time
if get(io, :compact, false)
show(io, system)
else
summary_header(io, "ParticlePackingSystem{$(ndims(system))}")
summary_line(io, "neighborhood search",
system.neighborhood_search |> typeof |> nameof)
summary_line(io, "#particles", nparticles(system))
summary_line(io, "smoothing kernel", system.smoothing_kernel |> typeof |> nameof)
summary_line(io, "tlsph", system.tlsph ? "yes" : "no")
summary_line(io, "boundary", system.is_boundary ? "yes" : "no")
summary_footer(io)
end
end
timer_name(::ParticlePackingSystem) = "packing"
@inline fixed_packing_system(::ParticlePackingSystem{<:Any, F}) where {F} = F
@inline function Base.eltype(::ParticlePackingSystem{<:Any, <:Any, <:Any, ELTYPE}) where {ELTYPE}
return ELTYPE
end
@inline function v_nvariables(system::ParticlePackingSystem)
# Don't integrate fixed systems
fixed_packing_system(system) && return 0
return ndims(system)
end
@inline function u_nvariables(system::ParticlePackingSystem)
# Don't integrate fixed systems
fixed_packing_system(system) && return 0
return ndims(system)
end
function reset_callback_flag!(system::ParticlePackingSystem)
system.update_callback_used[] = false
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)]
if write_meta_data
vtk["signed_distances"] = system.signed_distances
end
end
# Skip for fixed systems
write_u0!(u0, system::ParticlePackingSystem{<:Any, true}) = u0
# Skip for fixed systems
write_v0!(v0, system::ParticlePackingSystem{<:Any, true}) = v0
# Skip for fixed systems
@inline function current_coordinates(u, system::ParticlePackingSystem{<:Any, true})
return system.initial_condition.coordinates
end
@inline function advection_velocity(v, system::ParticlePackingSystem, particle)
return extract_svector(system.advection_velocity, system, particle)
end
write_v0!(v0, system::ParticlePackingSystem) = (v0 .= zero(eltype(system)))
# Zero for fixed systems
function kinetic_energy(system::ParticlePackingSystem{<:Any, true}, v_ode, u_ode, semi, t)
return zero(eltype(system))
end
function kinetic_energy(system::ParticlePackingSystem, v_ode, u_ode, semi, t)
(; initial_condition, is_boundary) = system
v = wrap_v(v_ode, system, semi)
# Exclude boundary packing system
is_boundary && return zero(eltype(system))
# If `each_moving_particle` is empty (no moving particles), return zero
return sum(each_moving_particle(system), init=zero(eltype(system))) do particle
velocity = advection_velocity(v, system, particle)
return initial_condition.mass[particle] * dot(velocity, velocity) / 2
end
end
@inline source_terms(system::ParticlePackingSystem) = nothing
@inline add_acceleration!(dv, particle, system::ParticlePackingSystem) = dv
# Update from `UpdateCallback` (between time steps)
update_particle_packing(system, v_ode, u_ode, semi, integrator) = system
# Update from `UpdateCallback` (between time steps)
function update_particle_packing(system::ParticlePackingSystem, v_ode, u_ode,
semi, integrator)
u = wrap_u(u_ode, system, semi)
update_position!(u, system, semi)
end
function update_position!(u, system::ParticlePackingSystem, semi)
func_name = "constrain outside particles onto surface"
@trixi_timeit timer() func_name constrain_particles_onto_surface!(u, system, 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
function constrain_particles_onto_surface!(u, system::ParticlePackingSystem, semi)
(; neighborhood_search, signed_distance_field, smoothing_length_interpolation) = system
(; positions, distances, normals) = signed_distance_field
search_radius2 = compact_support(system, system)^2
@threaded semi for particle in eachparticle(system)
particle_position = current_coords(u, system, particle)
volume = zero(eltype(system))
distance_signed = zero(eltype(system))
normal_vector = fill(volume, SVector{ndims(system), eltype(system)})
# Interpolate signed distances and normals
for neighbor in PointNeighbors.eachneighbor(particle_position, neighborhood_search)
pos_diff = positions[neighbor] - particle_position
distance2 = dot(pos_diff, pos_diff)
distance2 > search_radius2 && continue
distance = sqrt(distance2)
kernel_weight = kernel(system.smoothing_kernel, distance,
smoothing_length_interpolation)
distance_signed += distances[neighbor] * kernel_weight
normal_vector += normals[neighbor] * kernel_weight
volume += kernel_weight
end
if volume > eps()
distance_signed /= volume
normal_vector /= volume
# Store signed distance for visualization
system.signed_distances[particle] = distance_signed
constrain_particle!(u, system, particle, distance_signed, normal_vector)
end
end
return u
end
function constrain_particle!(u, system, particle, distance_signed, normal_vector)
(; shift_length) = system
# For fluid particles:
# - `tlsph = true`: `shift_length = 0`
# - `tlsph = false`: `shift_length = particle_spacing / 2`
# For boundary particles:
# `shift_length` is the thickness of the boundary.
if distance_signed >= -shift_length
# Constrain outside particles onto surface
shift = (distance_signed + shift_length) * normal_vector
for dim in 1:ndims(system)
u[dim, particle] -= shift[dim]
end
end
system.is_boundary || return u
particle_spacing = system.initial_condition.particle_spacing
shift_length_inner = system.tlsph ? particle_spacing : particle_spacing / 2
if distance_signed < shift_length_inner
shift = (distance_signed - shift_length_inner) * normal_vector
for dim in 1:ndims(system)
u[dim, particle] -= shift[dim]
end
end
return u
end
# Skip for fixed systems
@inline update_transport_velocity!(system::ParticlePackingSystem{<:Any, true}, v_ode,
semi) = system
# Update from `UpdateCallback` (between time steps)
@inline function update_transport_velocity!(system::ParticlePackingSystem, v_ode, semi)
v = wrap_v(v_ode, system, semi)
@threaded semi for particle in each_moving_particle(system)
for i in 1:ndims(system)
system.initial_condition.velocity[i, particle] = v[i, particle]
# The particle velocity is set to zero at the beginning of each time step to
# achieve a fully stationary state.
v[i, particle] = zero(eltype(system))
end
end
return system
end
# Skip for fixed systems
@inline add_velocity!(du, v, particle, system::ParticlePackingSystem{<:Any, true}) = du
# Add advection velocity.
@inline function add_velocity!(du, v, particle, system::ParticlePackingSystem)
for i in 1:ndims(system)
du[i, particle] = system.initial_condition.velocity[i, particle]
end
return du
end