Runge-Kutta ODE Integrator Implemented in Cython and Numba.
Reason this release was yanked:
Issues with this release
Project description
CyRK
Runge-Kutta ODE Integrator Implemented in Cython and Numba
CyRK provides fast ODE integration while still allowing for differential equations written in Python.
CyRK's numba (njit-safe) implementation is 13-25x faster than scipy's solve_ivp function. The cython implementation is about 20x faster. The cython function is also largely pre-compiled which avoids most of the initial performance hit found with the numba version.
Installation
It is recommended you use an Anaconda environment.
To install simply open a terminal (in administrator mode if using Windows) and call:
pip install CyRK
This will create a dynamic (editable) link to the CyRK directory so that future updates can be more easily installed.
During installation, CyRK will attempt to install Cython and Numpy in order to compile the cython code (unless installation is happening via a pre-compiled wheel). After the files have been compiled, cython will be uninstalled and CyRK's runtime dependencies (see setup.py for the latest list) will be installed instead.
Installation Troubleshooting
There are currently no reported installation issues. If you run into one please report it. We will work on a fix and/or add workaround information here.
Development and Testing Dependencies
If you intend to work on CyRK's code base you will want to install the following dependencies in order to run CyRK's test suite.
conda install pytest scipy matplotlib jupyter
conda install
can be replaced with pip install
if you prefer.
Using CyRK
CyRK's API is similar to SciPy's solve_ivp function. A differential equation can be defined in python such as:
import numpy as np
from numba import njit
# For even more speed up you can use numba's njit to compile the diffeq
@njit
def diffeq(t, y):
dy = list()
dy[0] = (1. - 0.01 * y[1]) * y[0]
dy[1] = (0.02 * y[0] - 1.) * y[1]
return dy
initial_conds = np.asarray((20., 20.), dtype=np.complex128)
time_span = (0., 50.)
rtol = 1.0e-7
atol = 1.0e-8
The ODE can then be solved using the numba function by calling CyRK's nbrk_ode
:
from CyRK import nbrk_ode
time_domain, y_results, success, message = \
nbrk_ode(diffeq, time_span, initial_conds, rk_method=1, rtol=rtol, atol=atol)
To call the cython version of the integrator you need to slightly edit the differential equation so that it does not return the derivative. Instead, the output is passed as an input argument (a np.ndarray) to the function.
@njit
def diffeq(t, y, dy):
dy[0] = (1. - 0.01 * y[1]) * y[0]
dy[1] = (0.02 * y[0] - 1.) * y[1]
You can then call the ODE solver in a similar fashion as the numba version.
from CyRK import cyrk_ode
time_domain, y_results, success, message = \
cyrk_ode(diffeq, time_span, initial_conds, rk_method=1, rtol=rtol, atol=atol)
Optional Inputs
Both the numba and cython versions of the ODE solver have the following optional inputs:
rtol
: Relative Tolerance (default is 1.0e-6).atol
: Absolute Tolerance (default is 1.0e-8).max_step
: Maximum step size (default is +infinity).first_step
: Initial step size (default is 0).- If 0, then the solver will try to determine an ideal value.
args
: Python tuple of additional arguments passed to thediffeq
.t_eval
: Both solvers uses an adaptive time stepping protocol based on the recent error at each step. This results in a final non-uniform time domain of variable size. If the user would like the results at specific time steps then they can provide a np.ndarray array at the desired steps tot_eval
. The solver will then interpolate the results to fit this array.rk_method
: Runge-Kutta method (default is 1; all of these methods are based off of SciPy implementations):0
- "RK23" Explicit Runge-Kutta method of order 3(2).1
- "RK45" Explicit Runge-Kutta method of order 5(4).2
- "DOP853" Explicit Runge-Kutta method of order 8.
Limitations and Known Issues
- Issue 1: Absolute tolerance can only be passed as a single value (same for all y's).
- Issue 3: Right now the cython version only allows for complex-valued y-values.
- Issue 5: The numba solver is worse than the pure python scipy solver at large timespans (high integration times).
Citing CyRK
It is great to see CyRK used in other software or in scientific studies. We ask that you cite back to CyRK's GitHub website so more users learn about this package.
In addition to citing CyRK, please consider citing SciPy and its references for the specific Runge-Kutta model that was used in your work. CyRK is largely an adaptation of SciPy's functionality. Find more details here.
Contribute to CyRK
CyRK is open-source and is distributed under the Creative Commons Attribution-ShareAlike 4.0 International license.
You are welcome to fork this repository and make any edits with attribution back to this project (please see the
Citing CyRK
section).
- We encourage users to report bugs or feature requests using GitHub Issues.
- If you would like to contribute but don't know where to start, check out the good first issue tag on GitHub.
- Users are welcome to submit pull requests and should feel free to create them before the final code is completed so that feedback and suggestions can be given early on.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for CyRK-0.1.1-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82f320c1f2e5fc6dcbed1bfeb89959f73126db9d6eab69d136b853210e56154b |
|
MD5 | 2a7c2f2d560c197f180f61b5823e54fb |
|
BLAKE2b-256 | 31a686488abcb6e46c9c36fae8550f69e2d02506ca2eadbb36f3d27a28c5958b |
Hashes for CyRK-0.1.1-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | af80948648b1ae6a1ef0cf68f656a7b32d95aa92109b4e886e4bf595a3f45365 |
|
MD5 | d13cb5ae357f61e3d48a70928e020279 |
|
BLAKE2b-256 | c618f3a58043d685b6777118aef653f72d4d9d5fba18d7255d62a4f062751aba |
Hashes for CyRK-0.1.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3355028478eb791cb784b4fe6def9d9bb3e85b3eb32ef867f10511751f7462a8 |
|
MD5 | c54b391f3d084b75cc23835f003c4936 |
|
BLAKE2b-256 | cb64ad29438c7805ff13e5754a2f427dc528bdf3ffef55b8de077ba07729e26e |
Hashes for CyRK-0.1.1-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97eb61ba9150ac4704ef7dfe17bc712e8b63fb5f2cb553822b2dfb88bbf8963c |
|
MD5 | 4b68662c47979e10dce9e9ab3ca1f569 |
|
BLAKE2b-256 | 6abac2add5366e983a614a4ee1594435d9bbcc342f5e3129b89fd1e7a58e04ac |
Hashes for CyRK-0.1.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99918bc8a0a5f7aa9c3138ab277a00a79902d9aa4fd22687f3e4eb8ec6ae43e8 |
|
MD5 | e59b0b46843fca09b6da0a0b24d3b0a5 |
|
BLAKE2b-256 | a5fc8dd5e18e6534dd7f458227ebfa06228665a4bae7b98d18ef119567e41565 |
Hashes for CyRK-0.1.1-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3645b5817fb7b162cf8a47a36aa8afb9fcbbff396bdb827e1326308a779d677 |
|
MD5 | 3cf883ef307957fad64abc232edbf6ca |
|
BLAKE2b-256 | 4e8653555075c31b04cc7b9ff03b15e4ca880ebab43a7b3163f1b4460b93bc15 |
Hashes for CyRK-0.1.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77bb7a2f5b3524add80d70314aa9a70b362078117cc7a2eb6e513994e2add77b |
|
MD5 | 0d52ece4a0912e0597a1b780127b947b |
|
BLAKE2b-256 | dc83fc74209a5826cf8f62f8c854b90f0bcd3dcd19b4fdf6c47ad00007bd017a |
Hashes for CyRK-0.1.1-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a0adf5beb827d6ca547d902936673b4d32bbf5d516c4e577d2782e140f85d8f |
|
MD5 | 59e49a146a0b561d85797fc27e025620 |
|
BLAKE2b-256 | 2cd169d18b28d0f2fca5c8df54b5a8dd841ae99e55d149ebea0590713b7102ad |