-
Notifications
You must be signed in to change notification settings - Fork 258
Adding Symplectic Integrators #1411
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
Open
JacobHass8
wants to merge
12
commits into
boostorg:develop
Choose a base branch
from
JacobHass8:symplectic-integrators
base: develop
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
Show all changes
12 commits
Select commit
Hold shift + click to select a range
eb64a06
Initial symplectic integrator
JacobHass8 62c80b7
Generalized to floats
JacobHass8 45ebaff
Added driver function
JacobHass8 34d3cd8
Added error handling and default arguments
JacobHass8 098ab7d
Initial testing
JacobHass8 fd343b7
Added SHO test
JacobHass8 63eac98
Switched to map functionality
JacobHass8 a3a916f
Added documentation
JacobHass8 1281885
Fixed typo
JacobHass8 5e7705b
Updated docs and max function
JacobHass8 e31880b
Changed functions to only accept vectors
JacobHass8 645aa1a
Completely eliminated valarrays
JacobHass8 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,103 @@ | ||
| [/ | ||
| Copyright (c) 2026 Jacob Hass | ||
| Use, modification and distribution are subject to the | ||
| Boost Software License, Version 1.0. (See accompanying file | ||
| LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| ] | ||
|
|
||
| [section:symplectic Symplectic Integration] | ||
|
|
||
| [heading Synopsis] | ||
|
|
||
| #include <boost/math/quadrature/symmetric.hpp> | ||
| namespace boost{ namespace math{ namespace quadrature { | ||
|
|
||
| template <class ReturnType, class RealType, class Func> | ||
| std::pair<std::vector<ReturnType>, std::vector<ReturnType> > integrate_hamiltonian(const ReturnType p0, | ||
| const ReturnType q0, | ||
| const RealType dt, | ||
| const unsigned steps, | ||
| Func dHdp, | ||
| Func dHdq, | ||
| std::string method = "Y6") | ||
|
|
||
| template <class ReturnType, class RealType, class Func, class ``__Policy``> | ||
| std::pair<std::vector<ReturnType>, std::vector<ReturnType> > integrate_hamiltonian(const ReturnType p0, | ||
| const ReturnType q0, | ||
| const RealType dt, | ||
| const unsigned steps, | ||
| Func dHdp, | ||
| Func dHdq, | ||
| std::string method, | ||
| const ``__Policy``& pol) | ||
|
|
||
| }}} // namespaces | ||
|
|
||
| [heading Description] | ||
|
|
||
| The functional `integrate_hamiltonian` calculates the phase space trajectory for a given Hamiltonian. | ||
| The trajectories are calculated using symplectic integration which preserves the energy of | ||
| a system. Even higher order traditional numerical integration algorithms will gain or lose | ||
| energy at long times. The functional implements the methods in [@https://www.sciencedirect.com/science/article/abs/pii/0375960190900923 Yoshida's] | ||
| landmark paper. We assume that the Hamiltonian is separable so that it can be written as `H = T(p) + V(q)`. | ||
|
|
||
| We now give an example for a simple harmonic oscillator with the Hamiltonian | ||
| [/ $H = \frac{p^2}{2m} + \frac{1}{2}kx^2$ ] | ||
| [equation harmonic_oscillator] | ||
|
|
||
| For simplicity we will assume `k = m = 1`. Then the partial derivatives of the Hamiltonian | ||
| with respect to `p` and `q` are | ||
|
|
||
| double dHdp(double p) | ||
| { | ||
| return p; | ||
| } | ||
|
|
||
| double dHdq(double q) | ||
| { | ||
| return q; | ||
| } | ||
|
|
||
| Note that the functional can be readily generalized to multiple coordinates by changing the | ||
| signature of `dHdp` to | ||
|
|
||
| std::valarray<double> dHdp(std::valarray<double> p) | ||
| { | ||
| // calculate the partial derivatives with respect to each p_i | ||
| } | ||
|
|
||
| The function must return a `valarray` type, as opposed to `std::vector`, because we must be | ||
| able to perform arithmetic operations on the return type. We then define the timestep size | ||
| and number of steps of the algorithm to go from `t=0` to `t=100` | ||
|
|
||
| RealType dt = 0.05; | ||
| RealType t_end = 100; | ||
| unsigned int steps = t_end / dt; | ||
|
|
||
| Lastly, we define the initial conditions so that the oscillator starts from rest at `x=1` | ||
|
|
||
| RealType q0 = 1; | ||
| RealType p0 = 0; | ||
|
|
||
| We now evolve the system using the following | ||
|
|
||
| std::vector<RealType> p; | ||
| std::vector<RealType> q; | ||
|
|
||
| std::tie(p, q) = boost::math::quadrature::integrate_hamiltonian(p0, q0, dt, steps, dHdp, dHdq, "Y6"); | ||
|
|
||
| The ouput vectors `q, p` are the position and momentum of the system at each time. In | ||
| higher dimensions the output will be a vector of vectors. | ||
|
|
||
| The last argument that we pass to `integrate_hamiltonian`, "Y6" here, sets the integration | ||
| method to use. The string "Y6" stands for Yoshida's 6th order integrator. Yoshida's 2nd and | ||
| 4th order methods are also available by passing the string "Y2", or "Y4" respectively. | ||
|
|
||
| [optional_policy] | ||
|
|
||
| References: | ||
|
|
||
| Yoshida, Haruo. [`Construction of higher order symplectic integrators], Physics Letters A, 150.5-7 (1990): 262-268. | ||
|
|
||
| [endsect] [/section:symplectic Symplectic Integration] | ||
|
|
||
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.
@JacobHass8 : I believe that
std::valarrayhas been essentially deprecated in the C++ community. @mborland : Is this correct?In general, we like to use
template typename <RandomAccessContainer>.Uh oh!
There was an error while loading. Please reload this page.
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.
I only used
std::valarraybecause it supports vectorized arithmetic. I think Eigen vectors would be okay too.Maybe this shouldn't be a requirement though? I would need to either convert to an
std::valarrayor just write for loops. The for loops option isn't bad but I thought it would obfuscate the algorithm.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.
Yeah, I'd personally just leave it generic and do a for loop.
Uh oh!
There was an error while loading. Please reload this page.
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.
Hmmm, I can't figure out how to allow the function to accept both numeric types and iterators. I might just require the user to always input an iterator. I can't figure out how to use for loops for iterators and none for numeric types.