Skip to main content

Some tools for Hamiltonian systems

Project description

pyHamSys

pyHamSys is a Python package for scientific computing involving Hamiltonian systems

PyPI License

Installation:

pip install pyhamsys

Symplectic Integrators

pyHamSys includes a class SymplecticIntegrator containing the following symplectic splitting integrators:

All purpose integrators are for any splitting of the Hamiltonian H=∑k Ak in any order of the functions Ak. Otherwise, the order of the operators is specified for each integrator.

Usage: integrator = SymplecticIntegrator(name) where name is one of the names listed above.

The functions solve_ivp_symp and solve_ivp_sympext solve an initial value problem for a Hamiltonian system using an element of the class SymplecticIntegrator, an explicit symplectic splitting scheme (see [1]). These functions numerically integrate a system of ordinary differential equations given an initial value:
  dy / dt = {y, H(t, y)}
  y(t0) = y0
Here t is a 1-D independent variable (time), y(t) is an N-D vector-valued function (state). A Hamiltonian H(t, y) and a Poisson bracket {. , .} determine the differential equations. The goal is to find y(t) approximately satisfying the differential equations, given an initial value y(t0) = y0.

The function solve_ivp_symp solves an initial value problem using an explicit symplectic integration. The Hamiltonian flow is defined by two functions chi and chi_star of (h, t, y) (see [2]).

The function solve_ivp_sympext solves an initial value problem using an explicit symplectic approximation obtained by an extension in phase space (see [3]). The Hamiltonian flow is defined by one function fun of (t, y) and one coupling parameter omega.

Parameters:

  • chi (for solve_ivp_symp) : callable
    Function of (h, t, y) returning exp(h Xn)...exp(h X1) y at time t. If the selected integrator is not all purpose, refer to the list above for the specific ordering of the operators. The operator Xk is the Liouville operator associated with the function Ak, i.e., for Hamiltonian flows Xk = {Ak , ·} where {· , ·} is the Poisson bracket. chi must return an array of the same shape as y.
  • chi_star (for solve_ivp_symp) : callable
    Function of (h, t, y) returning exp(h X1)...exp(h Xn) y at time t. chi_star must return an array of the same shape as y.
  • fun (for solve_ivp_sympext) : callable
    Right-hand side of the system: the time derivative of the state y at time t. i.e., {y, H(t, y)}. The calling signature is fun(t, y), where t is a scalar and y is an ndarray with len(y) = len(y0). fun must return an array of the same shape as y.
  • t_span : 2-member sequence
    Interval of integration (t0, tf). The solver starts with t=t0 and integrates until it reaches t=tf. Both t0 and tf must be floats or values interpretable by the float conversion function.
  • y0 : array_like, shape (n,)
    Initial state.
  • step : float
    Step size.
  • t_eval : array_like or None, optional
    Times at which to store the computed solution, must be sorted and equally spaced, and lie within t_span. If None (default), use points selected by the solver.
  • method : string, optional
    Integration methods are listed on pyhamsys.
    'BM4' is the default.
  • omega (for solve_ivp_sympext) : float, optional
    Coupling parameter in the extended phase space (see [3]). Default = 10.
  • command : function of (t, y)
    Function to be run at each step size (e.g., plotting an observable associated with the state vector y, or register specific events).

Remarks:

  • Use solve_ivp_symp is the Hamiltonian can be split and if each partial flow exp(h Xk) can be easily computed. Otherwise use solve_ivp_sympext.
  • If t_eval is a linearly spaced list or array, or if t_eval is None (default), the step size is slightly readjusted so that the output times contain the values in t_eval, or the final time tf corresponds to an integer number of step sizes.

Returns:

  Bunch object with the following fields defined:

  • t : ndarray, shape (n_points,)
    Time points.
  • y : ndarray, shape (n, n_points)
    Values of the solution at t.
  • step : step size used in the computation.

References:

  • [1] Hairer, Lubich, Wanner, 2003, Geometric Numerical Integration: Structure-Preserving Algorithms for Ordinary Differential Equations (Springer)
  • [2] McLachlan, Tuning symplectic integrators is easy and worthwhile, Commun. Comput. Phys. 31, 987 (2022); arxiv:2104.10269
  • [3] Tao, M., Explicit symplectic approximation of nonseparable Hamiltonians: Algorithm and long time performance, Phys. Rev. E 94, 043303 (2016)

Example

>>> import numpy as xp
>>> import matplotlib.pyplot as plt
>>> from pyhamsys import solve_ivp_sympext
>>> def fun(t,y):
	x, p = xp.split(y, 2)
	return xp.concatenate((p, -xp.sin(x)), axis=None)
>>> sol = solve_ivp_sympext(fun, (0, 20), xp.asarray([3, 0]), step=1e-1)
>>> plt.plot(sol.y[0], sol.y[1])
>>> plt.show()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyhamsys-0.1.0.tar.gz (10.6 kB view hashes)

Uploaded Source

Built Distribution

pyhamsys-0.1.0-py3-none-any.whl (11.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page