Skip to content
Projects
These are the docs of the development version (the devel branch). See the docs of the latest release.

example_pkg

Type part of a name or a dotted path

A tiny package, so the example site has an API reference to render.

Classes

NameDescription
DriftMove the position at the current velocity.
GridA uniform grid of cells on [start, stop].
KickChange the velocity by the acceleration at the current position.
LeapfrogHalf a kick, a drift and half a kick: the velocity Verlet scheme.

Functions

NameDescription
linspace_midpointsThe midpoints of cells uniform cells on [start, stop].
midpoint_integralThe integral of f on [start, stop] by the composite midpoint rule.

Modules

  • gridA uniform 1D grid.
  • schemesTime steps for a particle in a force field: kick, drift and their leapfrog.

Driftclass#

class Drift(Scheme)

Bases: Scheme

Move the position at the current velocity.

Numerics

update

xn+1=xn+vn Δtx^{n+1} = x^n + v^n\,\Delta t

Assumptions:

  • velocity constant over the step

stepmethod#

def step(x, v, dt: float, acceleration: Callable) -> tuple

Advance the position only.

Gridclassdataclass#

class Grid(start: float, stop: float, cells: int)

A uniform grid of cells on [start, stop].

Attributes

NameTypeDescription
startfloatLeft end of the domain.
stopfloatRight end of the domain.
cellsintNumber of cells.

cellsattributeinstance attribute#

cells: int

startattributeinstance attribute#

start: float

stopattributeinstance attribute#

stop: float

spacingproperty#

spacing: float

The cell width h=(b−a)/nh = (b - a) / n.

nodesmethod#

def nodes() -> np.ndarray

The cells + 1 cell boundaries.

Returns

np.ndarray
The node coordinates.

Kickclass#

class Kick(Scheme)

Bases: Scheme

Change the velocity by the acceleration at the current position.

Numerics

update

vn+1=vn+a(xn) Δtv^{n+1} = v^n + a(x^n)\,\Delta t

stepmethod#

def step(x, v, dt: float, acceleration: Callable) -> tuple

Advance the velocity only.

Leapfrogclass#

class Leapfrog(Scheme)

Bases: Scheme

Half a kick, a drift and half a kick: the velocity Verlet scheme.

Numerics

half kick

vn+1/2=vn+12a(xn) Δtv^{n+1/2} = v^n + \tfrac{1}{2} a(x^n)\,\Delta t

drift

xn+1=xn+vn+1/2 Δtx^{n+1} = x^n + v^{n+1/2}\,\Delta t

kick

vn+1=vn+1/2+12a(xn+1) Δtv^{n+1} = v^{n+1/2} + \tfrac{1}{2} a(x^{n+1})\,\Delta t

energy

∣H(xn,vn)−H(x0,v0)∣=O(Δt2)for all n\left|H(x^n, v^n) - H(x^0, v^0)\right| = \mathcal{O}(\Delta t^2) \quad \text{for all } n

Order of accuracy: x: 2, v: 2.

Assumptions:

  • the acceleration depends on the position only

References:

  • Hairer, Lubich and Wanner, Geometric Numerical Integration (2006), section I.1.4

Built from:

  • example_pkg.schemes.Kick
  • example_pkg.schemes.Drift

runmethod#

def run(x, v, dt: float, steps: int, acceleration: Callable) -> np.ndarray

Take steps steps and return the positions, (steps + 1,).

stepmethod#

def step(x, v, dt: float, acceleration: Callable) -> tuple

Advance (x, v) by one leapfrog step.

linspace_midpointsfunction#

def linspace_midpoints(start: float, stop: float, cells: int) -> np.ndarray

The midpoints of cells uniform cells on [start, stop].

Parameters

NameTypeDescription
startfloatLeft end of the domain.
stopfloatRight end of the domain.
cellsintNumber of cells.

Returns

np.ndarray
An array of length cells.

midpoint_integralfunction#

def midpoint_integral(f, start: float, stop: float, cells: int) -> float

The integral of f on [start, stop] by the composite midpoint rule.

Parameters

NameTypeDescription
fThe integrand, called on an array of points.
startfloatLeft end of the domain.
stopfloatRight end of the domain.
cellsintNumber of cells.

Returns

float
The approximate integral.

Numerics

rule

∫abf(x) dx≈h∑i=0n−1f(xi+xi+12)\int_a^b f(x)\,dx \approx h \sum_{i=0}^{n-1} f\left(\frac{x_i + x_{i+1}}{2}\right)

error

∣E∣≤(b−a)h224max⁡[a,b]∣f′′∣\left|E\right| \le \frac{(b - a) h^2}{24} \max_{[a, b]} |f''|

Order of accuracy: 2.

Assumptions:

  • f twice continuously differentiable on [a, b]

References:

  • Süli and Mayers, An Introduction to Numerical Analysis (2003), section 7.2