Skip to main content

Playing with Runge-Kutta methods and SageMath.

Installation

You can install this package in sage and in passagemath.

With sage do:

sage -pip install RKkit

With passagemath:

1- Install passagemath in a virtualenv

2- Once passagemath in installed, activate the virtualenv if necessary and then do:

pip install RKkit

Then if you clone this repository you can run the python scripts in PythonExamples/ with sage or passagemath/sage and, il you use passagemath you can run them with ipython or in a python script.

To run the jupyter notebooks of Notebooks/ with passagemath/sage you need to install jupyter or jupyterlab (using pip install).

Goal of the code:

The goal of this code is to verify different properties of a given Runge--Kutta method, defined by it's Butcher array (all the notions used here are defined in the Bible, see below).

  • With SageMath, it is possible to make exact computations in some useful sets of numbers, and thus we make the hypothesis that the coefficients of the methods live in the set of (real) algebraic numbers (called AA in SageMath); this is not a really restrictive hypothesis. But then, the result obtained are proofs (if both my codes and SageMath are correct!).

  • To compute the order of a Runge-Kutta method, one use the so called rooted trees for which we have a SageMath implementation, coded by Florent Hivert.

  • We also provide a function to compute the Butcher array of a method defined by collocation. A classical application is the set of Gaussian Runge-Kutta methods.

Some sage/jupyter notebooks are provided: they probably provide the best way to learn and to test this code.

Note that the cost of the computation grows very fast with tne number of steps of the method; also, the rooted_tree machinery in sage is quite slow.

References:

  • To learn about Ordinary Differential Equations solvers, you should read the Bible:

1- Solving Ordinary Differential Equations I, by Hairer, Nørsett,, Wanner (HNW),

2- Solving Ordinary Differential Equations II Stiff and Differential-Algebraic Problems by Hairer and Wanner (HW),

3- Geometric Numerical Integration by Hairer, Lubich and Wanner (HLW).

If you want to learn and to understand rooted trees and B-series, I recommend to start first by reading reference 3. Being the latest book, things have become much easier to understand than in reference 1.

  • If you want to learn SageMath, you can read the book Mathematical Computation with Sage (which now is available in French, English and German), and for which freely available pdf files can be downloaded here and there.

Using the code

  • First, you must define a Runge--Kutta method. To do this you must write a (simple) python class: have a look at formulas.py in RKkit/ to better undestand what to do.

    The class must derive from the "RungeKutta" class and include a constructor.

The constructor must:

1- Define the arrays A and B of the Butcher array (the C part is generally not necessary).

2- Give a title.

3- Call the base RungeKutta class constructor.

Remember that the "formula.py" file in RKkit/ gives examples of such classes.

Be careful: The coefficients of A and B must be algebraic numbers (AA or QQbar) or rational numbers (QQ). This is not absolutely trivial since, in sage:

2/3, for example, is directly evaluated as a float (0.66666...). This is a Python intrinsic, and cannot be avoided. So as you need to enter an algebraic (real) number or a rational, you must write QQ(2/3) or AA(2/3) or 2/QQ(3) or 2/AA(3) instead of 2/3. Again, have a look at RKkit/formula.py.

Have a look at "GoodAndBad.ipynb": this notebook shows this more in details.

  • Then, the best is to look at the notebook Example1.ipynb and adapt it to your needs. For this, launch sage like this:

>sage -n jupyter

or

>sage -n jupyterlab

and then, launch the notebook _Exemple1.ipynb_from Notebooks.

Gaussian formulas (and other methods obtained by collocation)

Gausian formulae with n steps are obtained by collocation at the roots of the Legendre P polynomials of degree n, shifted from [-1,1] to [0,1].

RKcolloc.colloc computes the Butcher arrays, given collocation points in [0,1] and returns a Runge-Kutta method class (note that collocation points are not necessary Gaussian points).

See the notebook Gaussian.ipynb.

For the fun, formulas obtained by collocation at roots of Tchebychev polynomials are computed in "Tchebychev.ipynb" (they certainly have no interesting application).

Notebooks

All are Sage/Jupyter notebooks. Launch Sage by typing:

sage -n jupyter

or

sage -n jupyterlab

if you use passagemath, you can install jupyter in the virtualenv:

pip install notebook (once the vitualenv is activated).

  1. Example.ipynb : a tour of the system.

  2. Gaussian.ipynb: construct and test some of the Gauss methods.

  3. AllProperties.ipynb: show how to compute all possible properties of a method.

  4. Test.ipynb : just testing that everything works fine on a set of formulas.

  5. RadauByCollocation.ipynb : Radau methods are collocation methods. Play with this.

  6. GoodAndBad.ipynb : is supposed to show what to do and what not to do when coding a Runge-Kutta formula.

Implementation:

  • The code uses a decorator @_persistance to avoid recomputing known properties (which is often expensive).

A precedent version was using Sage's @lazy_attribute decorator, which could be disturbing.

Download files

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

Source Distribution

rkkit-0.1.7.tar.gz (30.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rkkit-0.1.7-py3-none-any.whl (27.7 kB view details)

Uploaded Python 3

File details

Details for the file rkkit-0.1.7.tar.gz.

File metadata

  • Download URL: rkkit-0.1.7.tar.gz
  • Upload date:
  • Size: 30.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.7

File hashes

Hashes for rkkit-0.1.7.tar.gz
Algorithm Hash digest
SHA256 32ff9d63d7895e60c1892eb64843e33749c1fb40059be7ee934756f8b09f5efe
MD5 05d8179d5824255d3b38bc96e3022b50
BLAKE2b-256 2b7681c937fc1a1402e35ffeb0263c12a82a54a2fa3b793d6148bf66f8ce78d9

See more details on using hashes here.

File details

Details for the file rkkit-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: rkkit-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 27.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.7

File hashes

Hashes for rkkit-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 ba3444df95d86279199cb86f03d8d6cab1127a01fd3d1d69a25702071bb1df4c
MD5 f3635baab78ccbdb94e48ca0de4fab2a
BLAKE2b-256 5b6c6c10634df2a6dcc980450cb2ae97467feba739afc2fbf80c56318ceacbd4

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.7 This release

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page