-
Notifications
You must be signed in to change notification settings - Fork 12
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
Implement the time integration method used in DualSPHysics #693
Draft
efaulhaber
wants to merge
3
commits into
trixi-framework:main
Choose a base branch
from
efaulhaber:dualsphysics-time-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+200
−0
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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,3 @@ | ||
```@docs | ||
SymplecticPositionVerlet | ||
``` |
This file contains 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,175 @@ | ||
module TrixiParticlesOrdinaryDiffEqExt | ||
|
||
using TrixiParticles | ||
|
||
# This is needed because `TrixiParticles.@threaded` translates | ||
# to `PointNeighbors.parallel_foreach`, so `PointNeighbors` must be available. | ||
const PointNeighbors = TrixiParticles.PointNeighbors | ||
|
||
using OrdinaryDiffEqCore: @.., @muladd, @cache, OrdinaryDiffEqCore, | ||
OrdinaryDiffEqPartitionedAlgorithm, OrdinaryDiffEqMutableCache | ||
|
||
struct SymplecticPositionVerlet <: OrdinaryDiffEqPartitionedAlgorithm end | ||
|
||
TrixiParticles.SymplecticPositionVerlet() = SymplecticPositionVerlet() | ||
|
||
OrdinaryDiffEqCore.default_linear_interpolation(alg::SymplecticPositionVerlet, prob) = true | ||
|
||
@cache struct SymplecticPositionVerletCache{uType, rateType, uEltypeNoUnits} <: | ||
OrdinaryDiffEqMutableCache | ||
u::uType | ||
uprev::uType | ||
tmp::uType | ||
k::rateType | ||
fsalfirst::rateType | ||
half::uEltypeNoUnits | ||
end | ||
|
||
function OrdinaryDiffEqCore.get_fsalfirstlast(cache::SymplecticPositionVerletCache, u) | ||
return (cache.fsalfirst, cache.k) | ||
end | ||
|
||
# Copied from OrdinaryDiffEqSymplecticRK | ||
# | ||
# provide the mutable uninitialized objects to keep state and derivative in case of mutable caches | ||
# no such objects are required for constant caches | ||
function alloc_symp_state(integrator) | ||
(integrator.u.x..., integrator.cache.tmp.x...) | ||
end | ||
|
||
# load state and derivatives at begin of symplectic iteration steps | ||
function load_symp_state(integrator) | ||
(integrator.uprev.x..., integrator.fsallast.x...) | ||
end | ||
|
||
# store state and derivatives at the end of symplectic iteration steps | ||
function store_symp_state!(integrator, cache, kdu, ku) | ||
copyto!(integrator.k[1].x[1], integrator.k[2].x[1]) | ||
copyto!(integrator.k[1].x[2], integrator.k[2].x[2]) | ||
copyto!(integrator.k[2].x[2], ku) | ||
copyto!(integrator.k[2].x[1], kdu) | ||
nothing | ||
end | ||
|
||
function OrdinaryDiffEqCore.alg_cache(alg::SymplecticPositionVerlet, u, rate_prototype, | ||
::Type{uEltypeNoUnits}, | ||
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, | ||
uprev, | ||
uprev2, f, t, | ||
dt, reltol, p, calck, | ||
::Val{true}) where {uEltypeNoUnits, | ||
uBottomEltypeNoUnits, | ||
tTypeNoUnits} | ||
tmp = zero(u) | ||
k = zero(rate_prototype) | ||
fsalfirst = zero(rate_prototype) | ||
half = uEltypeNoUnits(1 // 2) | ||
SymplecticPositionVerletCache(u, uprev, k, tmp, fsalfirst, half) | ||
end | ||
|
||
function OrdinaryDiffEqCore.alg_cache(alg::SymplecticPositionVerlet, u, rate_prototype, | ||
::Type{uEltypeNoUnits}, | ||
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, | ||
uprev, | ||
uprev2, f, t, | ||
dt, reltol, p, calck, | ||
::Val{false}) where {uEltypeNoUnits, | ||
uBottomEltypeNoUnits, | ||
tTypeNoUnits} | ||
error("`SymplecticPositionVerlet` only supports inplace functions") | ||
end | ||
|
||
function OrdinaryDiffEqCore.initialize!(integrator, | ||
cache::C) where {C <: SymplecticPositionVerletCache} | ||
integrator.kshortsize = 2 | ||
resize!(integrator.k, integrator.kshortsize) | ||
integrator.k[1] = integrator.fsalfirst | ||
integrator.k[2] = integrator.fsallast | ||
|
||
duprev, uprev = integrator.uprev.x | ||
integrator.f.f1(integrator.k[2].x[1], duprev, uprev, integrator.p, integrator.t) | ||
# verify_f2(integrator.f.f2, integrator.k[2].x[2], duprev, uprev, integrator.p, | ||
# integrator.t, integrator, cache) | ||
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) | ||
integrator.stats.nf2 += 1 | ||
end | ||
|
||
@muladd function OrdinaryDiffEqCore.perform_step!(integrator, | ||
cache::SymplecticPositionVerletCache, | ||
repeat_step=false) | ||
(; t, dt, f, p) = integrator | ||
duprev, uprev, _, _ = load_symp_state(integrator) | ||
du, u, kdu, ku = alloc_symp_state(integrator) | ||
|
||
# update position half step | ||
half = cache.half | ||
f.f2(ku, duprev, uprev, p, t) | ||
@.. broadcast=false u=uprev + dt * half * ku | ||
|
||
# update velocity half step | ||
f.f1(kdu, duprev, uprev, p, t) | ||
@.. broadcast=false du=duprev + dt * half * kdu | ||
|
||
# update velocity (add to previous full step velocity) | ||
f.f1(kdu, du, u, p, t + half * dt) | ||
# The following is equivalent to `du = duprev + dt * kdu` for the velocity, but when | ||
# the density is integrated, a different update is used for the density. | ||
semi = p | ||
TrixiParticles.foreach_system(semi) do system | ||
kdu_system = TrixiParticles.wrap_v(kdu, system, semi) | ||
du_system = TrixiParticles.wrap_v(du, system, semi) | ||
duprev_system = TrixiParticles.wrap_v(duprev, system, semi) | ||
|
||
update_velocity!(du_system, kdu_system, duprev_system, system, dt) | ||
update_density!(du_system, kdu_system, duprev_system, system, dt) | ||
end | ||
|
||
# update position (add to half step position) | ||
f.f2(ku, du, u, p, t + dt) | ||
@.. broadcast=false u=u + dt * half * ku | ||
|
||
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 2) | ||
integrator.stats.nf2 += 2 | ||
store_symp_state!(integrator, cache, kdu, ku) | ||
end | ||
|
||
@muladd function update_velocity!(du_system, kdu_system, duprev_system, system, dt) | ||
@.. broadcast=false du_system=duprev_system + dt * kdu_system | ||
end | ||
|
||
@inline function update_density!(du_system, kdu_system, duprev_system, system, dt) | ||
return du_system | ||
end | ||
|
||
@muladd function update_velocity!(du_system, kdu_system, duprev_system, | ||
system::WeaklyCompressibleSPHSystem, dt) | ||
TrixiParticles.@threaded system for particle in TrixiParticles.each_moving_particle(system) | ||
for i in 1:ndims(system) | ||
du_system[i, particle] = duprev_system[i, particle] + | ||
dt * kdu_system[i, particle] | ||
end | ||
end | ||
end | ||
|
||
@inline function update_density!(du_system, kdu_system, duprev_system, | ||
system::WeaklyCompressibleSPHSystem, dt) | ||
update_density!(du_system, kdu_system, duprev_system, | ||
system.density_calculator, system, dt) | ||
end | ||
|
||
@inline function update_density!(du_system, kdu_system, duprev_system, | ||
density_calculator, system, dt) | ||
return du_system | ||
end | ||
|
||
@muladd function update_density!(du_system, kdu_system, duprev_system, | ||
::ContinuityDensity, system, dt) | ||
TrixiParticles.@threaded system for particle in TrixiParticles.each_moving_particle(system) | ||
density_prev = duprev_system[end, particle] | ||
density_half = du_system[end, particle] | ||
epsilon = -kdu_system[end, particle] / density_half * dt | ||
du_system[end, particle] = density_prev * (2 - epsilon) / (2 + epsilon) | ||
end | ||
end | ||
|
||
end # module |
This file contains 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 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 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,13 @@ | ||||||
# Time integration is handles by the package OrdinaryDiffEq.jl. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
# See the docs for more details. | ||||||
# In this file, we define the structs for extra time integration schemes that | ||||||
# are implemented in the package extension TrixiParticlesOrdinaryDiffEqExt.jl. | ||||||
""" | ||||||
SymplecticPositionVerlet() | ||||||
|
||||||
Modified leapfrog integration scheme for Weakly Compressible SPH (WCSPH) when integrating | ||||||
the density with [`ContinuityDensity`](@ref). | ||||||
This scheme is used by DualSPHysics: | ||||||
https://github.com/DualSPHysics/DualSPHysics/wiki/3.-SPH-formulation#372-symplectic-position-verlet-scheme | ||||||
""" | ||||||
SymplecticPositionVerlet(_) = nothing |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunatly,
using TrixiParticles, OrdinaryDiffEq
doesn't make this extension available. I have to explicitly add and import
OrdinaryDiffEqCore
.How do I make this work with just
using OrdinaryDiffEq
?In the extension, I need to import
OrdinaryDiffEqCore
.@sloede ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, then you should have OrdinaryDiffEq as the trigger for the pkgext. I am sure you can finagle OrdinaryDiffEq from it, either by then doing
or if this does not work, I am sure one of the dependencies of OrdinaryDiffEq will have the Core package available as a name.