-
-
Notifications
You must be signed in to change notification settings - Fork 134
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
Add the Störmer-Verlet method + symplectic test changes #303
Open
packquickly
wants to merge
1
commit into
patrick-kidger:main
Choose a base branch
from
packquickly:stormer-verlet
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.
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,7 @@ | |
Ralston, | ||
ReversibleHeun, | ||
SemiImplicitEuler, | ||
StormerVerlet, | ||
Sil3, | ||
StratonovichMilstein, | ||
Tsit5, | ||
|
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,78 @@ | ||
from typing import Tuple | ||
|
||
from equinox.internal import ω | ||
|
||
from ..custom_types import Bool, DenseInfo, PyTree, Scalar | ||
from ..local_interpolation import LocalLinearInterpolation | ||
from ..solution import RESULTS | ||
from ..term import AbstractTerm | ||
from .base import AbstractSolver | ||
|
||
_ErrorEstimate = None | ||
_SolverState = None | ||
|
||
class StormerVerlet(AbstractSolver): | ||
""" Störmer-Verlet method. | ||
|
||
Symplectic method. Does not support adaptive step sizing. Uses 1st order local | ||
linear interpolation for dense/ts output. | ||
""" | ||
|
||
term_structure = (AbstractTerm, AbstractTerm) | ||
interpolation_cls = LocalLinearInterpolation | ||
|
||
def order(self, terms): | ||
return 2 | ||
|
||
def init( | ||
self, | ||
terms: Tuple[AbstractTerm, AbstractTerm], | ||
t0: Scalar, | ||
t1: Scalar, | ||
y0: PyTree, | ||
args: PyTree, | ||
) -> _SolverState: | ||
return None | ||
|
||
def step( | ||
self, | ||
terms: Tuple[AbstractTerm, AbstractTerm], | ||
t0: Scalar, | ||
t1: Scalar, | ||
y0: Tuple[PyTree, PyTree], | ||
args: PyTree, | ||
solver_state: _SolverState, | ||
made_jump: Bool, | ||
) -> Tuple[Tuple[PyTree, PyTree], _ErrorEstimate, DenseInfo, _SolverState, RESULTS]: | ||
del solver_state, made_jump | ||
|
||
term_1, term_2 = terms | ||
y0_1, y0_2 = y0 | ||
midpoint = (t1 + t0)/2 | ||
|
||
control1_half_1 = term_1.contr(t0, midpoint) | ||
control1_half_2 = term_1.contr(midpoint, t1) | ||
control2 = term_2.contr(t0, t1) | ||
|
||
yhalf_1 = (y0_1 ** ω + term_1.vf_prod(t0, y0_2, args, control1_half_1) ** ω).ω | ||
y1_2 = (y0_2 ** ω + term_2.vf_prod(midpoint, yhalf_1, args, control2) ** ω).ω | ||
y1_1 = (yhalf_1 ** ω + term_1.vf_prod(t1, y1_2, args, control1_half_2 ** ω)).ω | ||
|
||
y1 = (y1_1, y1_2) | ||
dense_info = dict(y0=y0, y1=y1) | ||
return y1, None, dense_info, None, RESULTS.successful | ||
|
||
def func( | ||
self, | ||
terms: Tuple[AbstractTerm, AbstractTerm], | ||
t0: Scalar, | ||
y0: Tuple[PyTree, PyTree], | ||
args: PyTree | ||
) -> Tuple[PyTree, PyTree]: | ||
term_1, term_2 = terms | ||
y0_1, y0_2 = y0 | ||
f1 = term_1.func(t0, y0_2, args) | ||
f2 = term_2.func(t0, y0_1, args) | ||
return (f1, f2) | ||
|
||
|
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
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.
Isn't this just semi-implicit Euler written in kick-drift-kick form? (I.e. offset by half a step.) Justification: it looks to me like
y1_2
on this step isy0_2
on the next step, so theterm_1.vf_prod(...)
evaluations happen at the same point twice. (Which also means that this is increasing runtime/compiletime.)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.
This is pretty subtle actually. long story short: the half step difference has an impact, and they are indeed different (you can check numerically, Störmer-Verlet is order 2, symplectic Euler is order 1.)
Störmer-Verlet is the composition of the symplectic euler method and it's adjoint (reverse method,) ie. it is both variants of symplectic Euler stacked with step-size$h/2$ :
The implementation in non kick-drift-kick form, ie.
is distinct from symplectic Euler primarily because of the initialization. Looking at the second-order diffeq case ($g(q) = q$ ) the initial $p_1$ for symplectic Euler is
and for Störmer-Verlet it's:
This is pretty much the only difference for these two though, and the non kick-drift-kick (often called the leapfrog-Verlet implementation, ugh) is definitely the better one when we don't care about knowing$q_{n+1}$ . However, I assumed we needed to be able to return the tuple $(p_{n+1}, q_{n+1})$ in each call to
step
, hence the more expensive kick-drift-kick implementation.If you see a way to switch to the leapfrog-Verlet implementation though by all means I'll do that instead.
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, you might find it interesting that Hairer wrote a long article about Störmer-Verlet as a precursor to "Geometric Numerical Integration," where he used the method to demonstrate a bunch of the ideas later expanded on in the book