-
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 and add docs for time integration #716
Open
efaulhaber
wants to merge
11
commits into
main
Choose a base branch
from
ef/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.
+442
−5
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b00d796
Implement the time integration method used in DualSPHysics
efaulhaber 7338228
Add source
efaulhaber 479874e
Add docstring to docs
efaulhaber 12e7dbd
Revise package extension
efaulhaber 1b5b4e2
Add docs for time integration
efaulhaber 0c23b00
Merge branch 'main' into ef/dualsphysics-time-integration
efaulhaber 429b0a1
Add tests
efaulhaber 4b6f146
Implement suggestions
efaulhaber f218b7b
Merge branch 'main' into ef/dualsphysics-time-integration
efaulhaber 9235998
Merge branch 'main' into ef/dualsphysics-time-integration
efaulhaber 5927b88
Update NEWS.md
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 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
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,143 @@ | ||
# [Time integration](@id time_integration) | ||
|
||
TrixiParticles.jl uses a modular approach where time integration is just another module | ||
that can be customized and exchanged. | ||
The function [`semidiscretize`](@ref) returns an `ODEProblem` | ||
(see [the OrdinaryDiffEq.jl docs](https://docs.sciml.ai/DiffEqDocs/stable/types/ode_types/)), | ||
which can be integrated with [OrdinaryDiffEq.jl](https://github.com/SciML/OrdinaryDiffEq.jl). | ||
|
||
In particular, a [`DynamicalODEProblem`](https://docs.sciml.ai/DiffEqDocs/stable/types/dynamical_types/) | ||
is returned, where the right-hand side is split into two functions, the `kick!`, which | ||
computes the derivative of the particle velocities and the `drift!`, which computes | ||
the derivative of the particle positions. | ||
This approach allows us to use [specialized time integration methods that do not work with | ||
general `ODEProblem`s](@ref symplectic_schemes). | ||
Note that this is not a true `DynamicalODEProblem` where the kick does not depend | ||
on the velocity. Therefore, not all integrators designed for `DynamicalODEProblem`s | ||
will work (properly) (see [below](@ref kick_drift_kick)). | ||
However, all integrators designed for general `ODEProblem`s can be used. | ||
|
||
## Usage | ||
|
||
After obtaining an `ODEProblem` from [`semidiscretize`](@ref), let us call it `ode`, | ||
we can pass it to the function `solve` of OrdinaryDiffEq.jl. | ||
For most schemes, we do the following: | ||
```julia | ||
using OrdinaryDiffEq | ||
sol = solve(ode, Euler(), | ||
dt=1.0, | ||
save_everystep=false, callback=callbacks); | ||
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. Mention why we set |
||
``` | ||
Here, `Euler()` should in practice be replaced by a more useful scheme. | ||
`callbacks` should be a `CallbackSet` containing callbacks like the [`InfoCallback`](@ref). | ||
For callbacks, please refer to [the docs](@ref Callbacks) and the example files. | ||
In this case, we need to either set a reasonable, problem- and resolution-dependent | ||
step size `dt`, or use the [`StepsizeCallback`](@ref), which overwrites the step size | ||
dynamically during the simulation based on a CFL-number. | ||
|
||
Some schemes, e.g. the two schemes `RDPK3SpFSAL35` and `RDPK3SpFSAL49` mentioned below, | ||
support automatic time stepping, where the step size is determined automatically based on | ||
error estimates during the simulation. | ||
These schemes do not use the keyword argument `dt` and will ignore the step size set by | ||
the [`StepsizeCallback`](@ref). | ||
Instead, they will work with the tolerances `abstol` and `reltol`, which can be passed as | ||
keyword arguments to `solve`. The default tolerances are `abstol=1e-6` and `reltol=1e-3`. | ||
|
||
## Recommended schemes | ||
|
||
A list of schemes for general `ODEProblem`s can be found | ||
[here](https://docs.sciml.ai/DiffEqDocs/stable/solvers/ode_solve/). | ||
We commonly use the following three schemes: | ||
- `CarpenterKennedy2N54(williamson_condition=false)`: A five-stage, fourth order | ||
low-storage Runge-Kutta method designed by [Carpenter and Kennedy](@cite CarpenterKennedy) | ||
for hyperbolic problems. | ||
- `RDPK3SpFSAL35()`: A 5-stage, third order low-storage Runge-Kutta scheme with embedded | ||
error estimator, optimized for compressible fluid mechanics [Ranocha2022](@cite). | ||
- `RDPK3SpFSAL49()`: A 9-stage, fourth order low-storage Runge-Kutta scheme with embedded | ||
error estimator, optimized for compressible fluid mechanics [Ranocha2022](@cite). | ||
|
||
## [Symplectic schemes](@id symplectic_schemes) | ||
|
||
Symplectic schemes like the leapfrog method are often used for SPH. | ||
|
||
### [Leapfrog kick-drift-kick](@id kick_drift_kick) | ||
|
||
The kick-drift-kick scheme of the leapfrog method, updating positions ``u`` | ||
and velocities ``v`` with the functions ``\operatorname{kick}`` and ``\operatorname{drift}``, | ||
svchb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
reads: | ||
```math | ||
\begin{align*} | ||
v^{1/2} &= v^0 + \frac{1}{2} \Delta t\, \operatorname{kick}(u^0, t^0), \\ | ||
u^1 &= u^0 + \Delta t\, \operatorname{drift} \left( v^{1/2}, t^0 + \frac{1}{2} \Delta t \right), \\ | ||
v^1 &= v^{1/2} + \frac{1}{2} \Delta t\, \operatorname{kick}(u^{1}, t^0 + \Delta t). | ||
\end{align*} | ||
``` | ||
In this form, it is also identical to the velocity Verlet scheme. | ||
Note that this only works as long as ``\operatorname{kick}`` does not depend on ``v``, i.e., | ||
in the inviscid case. | ||
Once we add viscosity, ``\operatorname{kick}`` depends on both ``u`` and ``v``. | ||
Then, the calculation of ``v^1`` requires ``v^1`` and becomes implicit. | ||
|
||
The way this scheme is implemented in OrdinaryDiffEq.jl as `VerletLeapfrog`, | ||
the intermediate velocity ``v^{1/2}`` is passed to ``\operatorname{kick}`` in the last stage, | ||
resulting in first-order convergence when the scheme is used in the viscid case. | ||
|
||
!!! warning | ||
Please do not use `VelocityVerlet` and `VerletLeapfrog` with TrixiParticles.jl. | ||
They will require very small time steps due to first-order convergence in the viscid case. | ||
|
||
### Leapfrog drift-kick-drift | ||
|
||
The drift-kick-drift scheme of the leapfrog method reads: | ||
```math | ||
\begin{align*} | ||
u^{1/2} &= u^0 + \frac{1}{2} \Delta t\, \operatorname{drift}(v^0, t^0), \\ | ||
v^1 &= v^0 + \Delta t\, \operatorname{kick} \left( u^{1/2}, t^0 + \frac{1}{2} \Delta t \right), \\ | ||
u^1 &= u^{1/2} + \frac{1}{2} \Delta t\, \operatorname{drift}(v^{1}, t^0 + \Delta t). | ||
\end{align*} | ||
``` | ||
In the viscid case where ``\operatorname{kick}`` depends on ``v``, we can add another | ||
half step for ``v``, yielding | ||
```math | ||
\begin{align*} | ||
u^{1/2} &= u^0 + \frac{1}{2} \Delta t\, \operatorname{drift}(v^0, u^0, t^0), \\ | ||
v^{1/2} &= v^0 + \frac{1}{2} \Delta t\, \operatorname{kick}(v^0, u^0, t^0), \\ | ||
v^1 &= v^0 + \Delta t\, \operatorname{kick} \left( v^{1/2}, u^{1/2}, t^0 + \frac{1}{2} \Delta t \right), \\ | ||
u^1 &= u^{1/2} + \frac{1}{2} \Delta t\, \operatorname{drift}(v^{1}, u^{1}, t^0 + \Delta t). | ||
\end{align*} | ||
``` | ||
This scheme is implemented in OrdinaryDiffEq.jl as `LeapfrogDriftKickDrift` and yields | ||
the desired second order as long as ``\operatorname{drift}`` does not depend on ``u``, | ||
which is always the case. | ||
|
||
### Symplectic position Verlet | ||
|
||
When the density is integrated (with [`ContinuityDensity`](@ref)), the density is appended | ||
to ``v`` as an additional dimension, so all previously mentioned schemes treat the density | ||
similar to the velocity. | ||
The SPH code [DualSPHysics](https://github.com/DualSPHysics/DualSPHysics) implements | ||
a variation of the drift-kick-drift scheme where the density is updated separately. | ||
See [Domínguez et al. 2022, Section 2.5.2](@cite Dominguez2022). | ||
In the following, we will call the derivative of the density ``R(v, u, t)``, | ||
even though it is actually included in the ``\operatorname{kick}`` as an additional dimension. | ||
|
||
This scheme reads | ||
```math | ||
\begin{align*} | ||
u^{1/2} &= u^0 + \frac{1}{2} \Delta t\, \operatorname{drift}(v^0, u^0, t^0), \\ | ||
v^{1/2} &= v^0 + \frac{1}{2} \Delta t\, \operatorname{kick}(v^0, u^0, t^0), \\ | ||
\rho^{1/2} &= \rho^0 + \frac{1}{2} \Delta t\, R(v^0, u^0, t^0), \\ | ||
v^1 &= v^0 + \Delta t\, \operatorname{kick} \left( v^{1/2}, u^{1/2}, t^0 + \frac{1}{2} \Delta t \right), \\ | ||
\rho^1 &= \rho^0 \frac{2 - \varepsilon^{1/2}}{2 + \varepsilon^{1/2}}, \\ | ||
u^1 &= u^{1/2} + \frac{1}{2} \Delta t\, \operatorname{drift}(v^{1}, u^{1}, t^0 + \Delta t), | ||
\end{align*} | ||
``` | ||
where | ||
```math | ||
\varepsilon^{1/2} = - \frac{R(v^{1/2}, u^{1/2}, t^0 + \frac{1}{2} \Delta t)}{\rho^{1/2}} \Delta t. | ||
``` | ||
This scheme is implemented in TrixiParticles.jl as [`SymplecticPositionVerlet`](@ref). | ||
|
||
```@docs | ||
SymplecticPositionVerlet | ||
``` |
Oops, something went wrong.
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.
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.
Better maybe?
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.
Also fine