-
Notifications
You must be signed in to change notification settings - Fork 18
Implement the Particle Shifting Technique (PST) #735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b112344
First prototype of PST
efaulhaber dd1cb74
Fix plotting of periodic examples
efaulhaber ba64d5c
Merge branch 'main' into particle-shifting
efaulhaber dca50a8
Merge branch 'main' into particle-shifting
efaulhaber 81b0419
Fix merge
efaulhaber 3fd2aba
Enable `particle_spacing` functions
efaulhaber fc21995
Update
efaulhaber d6da0fc
Cleanup
efaulhaber 0f38a52
Merge branch 'main' into particle-shifting
efaulhaber 8aef554
Fix typo
efaulhaber 3bfd73e
Fix factor
efaulhaber 8daf24f
Merge branch 'main' into particle-shifting
LasNikas d333804
Implement suggestions
efaulhaber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| @doc raw""" | ||
| ParticleShiftingCallback() | ||
|
|
||
| Callback to apply the Particle Shifting Technique by [Sun et al. (2017)](@cite Sun2017). | ||
| Following the original paper, the callback is applied in every time step and not | ||
| in every stage of a multi-stage time integration method to reduce the computational | ||
| cost and improve the stability of the scheme. | ||
|
|
||
| ## References | ||
| [Sun2017](@cite) | ||
| """ | ||
| function ParticleShiftingCallback() | ||
| # The first one is the `condition`, the second the `affect!` | ||
| return DiscreteCallback((particle_shifting_condition), particle_shifting!, | ||
| save_positions=(false, false)) | ||
| end | ||
|
|
||
| # `condition` | ||
| function particle_shifting_condition(u, t, integrator) | ||
| return true | ||
| end | ||
|
|
||
| # `affect!` | ||
| function particle_shifting!(integrator) | ||
| t = integrator.t | ||
| semi = integrator.p | ||
| v_ode, u_ode = integrator.u.x | ||
| dt = integrator.dt | ||
| # Internal cache vector, which is safe to use as temporary array | ||
| u_cache = first(get_tmp_cache(integrator)) | ||
|
|
||
| # Update quantities that are stored in the systems. These quantities (e.g. pressure) | ||
| # still have the values from the last stage of the previous step if not updated here. | ||
| update_systems_and_nhs(v_ode, u_ode, semi, t; update_from_callback=true) | ||
|
|
||
| @trixi_timeit timer() "particle shifting" foreach_system(semi) do system | ||
| u = wrap_u(u_ode, system, semi) | ||
| v = wrap_v(v_ode, system, semi) | ||
| particle_shifting!(u, v, system, v_ode, u_ode, semi, u_cache, dt) | ||
| end | ||
|
|
||
| # Tell OrdinaryDiffEq that `u` has been modified | ||
| u_modified!(integrator, true) | ||
|
|
||
| return integrator | ||
| end | ||
|
|
||
| function particle_shifting!(u, v, system, v_ode, u_ode, semi, u_cache, dt) | ||
| return u | ||
| end | ||
|
|
||
| function particle_shifting!(u, v, system::WeaklyCompressibleSPHSystem, v_ode, u_ode, semi, | ||
| u_cache, dt) | ||
| # Wrap the cache vector to an NDIMS x NPARTICLES matrix. | ||
| # We need this buffer because we cannot safely update `u` while iterating over it. | ||
| delta_r = wrap_u(u_cache, system, semi) | ||
| set_zero!(delta_r) | ||
|
|
||
| v_max = maximum(particle -> norm(current_velocity(v, system, particle)), | ||
| eachparticle(system)) | ||
|
|
||
| # TODO this needs to be adapted to multi-resolution. | ||
| # Section 3.2 explains what else needs to be changed. | ||
| Wdx = smoothing_kernel(system, particle_spacing(system, 1), 1) | ||
| h = smoothing_length(system, 1) | ||
|
|
||
| foreach_system(semi) do neighbor_system | ||
| u_neighbor = wrap_u(u_ode, neighbor_system, semi) | ||
| v_neighbor = wrap_v(v_ode, neighbor_system, semi) | ||
|
|
||
| system_coords = current_coordinates(u, system) | ||
| neighbor_coords = current_coordinates(u_neighbor, neighbor_system) | ||
|
|
||
| foreach_point_neighbor(system, neighbor_system, system_coords, neighbor_coords, | ||
| semi) do particle, neighbor, pos_diff, distance | ||
| m_b = hydrodynamic_mass(neighbor_system, neighbor) | ||
| rho_a = particle_density(v, system, particle) | ||
| rho_b = particle_density(v_neighbor, neighbor_system, neighbor) | ||
|
|
||
| kernel = smoothing_kernel(system, distance, particle) | ||
| grad_kernel = smoothing_kernel_grad(system, pos_diff, distance, particle) | ||
|
|
||
| # According to p. 29 below Eq. 9 | ||
| R = 0.2 | ||
| n = 4 | ||
|
|
||
| # Eq. 7 in Sun et al. (2017). | ||
| # CFL * Ma can be rewritten as Δt * v_max / h (see p. 29, right above Eq. 9). | ||
| delta_r_ = -dt * v_max * 4 * h * (1 + R * (kernel / Wdx)^n) * | ||
| m_b / (rho_a + rho_b) * grad_kernel | ||
|
|
||
| # Write into the buffer | ||
| for i in eachindex(delta_r_) | ||
| @inbounds delta_r[i, particle] += delta_r_[i] | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Add δ_r from the buffer to the current coordinates | ||
| @threaded semi for particle in eachparticle(system) | ||
| for i in 1:ndims(system) | ||
efaulhaber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @inbounds u[i, particle] += delta_r[i, particle] | ||
| end | ||
| end | ||
|
|
||
| return u | ||
| end | ||
|
|
||
| function Base.show(io::IO, cb::DiscreteCallback{<:Any, typeof(particle_shifting!)}) | ||
| @nospecialize cb # reduce precompilation time | ||
| print(io, "ParticleShiftingCallback()") | ||
| end | ||
|
|
||
| function Base.show(io::IO, ::MIME"text/plain", | ||
| cb::DiscreteCallback{<:Any, typeof(particle_shifting!)}) | ||
| @nospecialize cb # reduce precompilation time | ||
|
|
||
| if get(io, :compact, false) | ||
| show(io, cb) | ||
| else | ||
| summary_box(io, "ParticleShiftingCallback") | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.