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)

Gaussian 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.

Note about graphics:

When using the code with passagemath/ipython, graphics -if any- cannot be displayed. But in any case (using sage ou passagemath/sage) they can be saved in png files (see for example PythonExamples/Gaussian.py).

Metadata

Release files for RKkit 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for RKkit 0.1.8
File Size Uploaded
rkkit-0.1.8.tar.gz 30.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for RKkit 0.1.8
File Interpreter ABI Platform
rkkit-0.1.8-py3-none-any.whl Python 3 none any Details

Total release size: 58.1 kB

Release files / rkkit-0.1.8.tar.gz

Download URL rkkit-0.1.8.tar.gz
Size 30.4 kB
Tags Source
SHA-256 checksum
How to use checksums
16e105150ed90db6d3a0cb5c1fc4b748a8e6f0a8127f2410ceaadabe5104d46e
BLAKE2b-256 checksum
How to use checksums
3253e2746e40660a157485e7898b35516b73642f3a6ba52a3106e6cb56c5b692
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / rkkit-0.1.8-py3-none-any.whl

Download URL rkkit-0.1.8-py3-none-any.whl
Size 27.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d47a71df8695874a0b4210559f72811008eb0cc08236fb0e97c61b7ed37ae7ea
BLAKE2b-256 checksum
How to use checksums
e68d1296e9ba8155ef41d07d57b2e735a6bcbf0714e64bfb415fc5697bf0e799
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release 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