Runge-Kutta ODE Integrator Implemented in Cython and Numba.
Project description
CyRK
Runge-Kutta ODE Integrator Implemented in Cython and Numba
CyRK provides fast integration tools to solve systems of ODEs with adaptive time stepping. CyRK can accept differential equation functions that are written in pure Python or njited numba, speeding up development time. The purpose of this package is to provide some functionality of scipy's solve_ivp with improved performance.
CyRK's numba (njit-safe) implementation is 8-20x faster than scipy's solve_ivp function (except for very long integrations). The cython implementation is 5-20x faster. The cython function is also largely pre-compiled which avoids most of the initial performance hit found with using the numba version.
Installation
It is recommended you use an Anaconda environment. CyRK has been tested on Python 3.8--3.10
To install simply open a terminal (in administrator mode if using Windows) and call:
pip install CyRK
If not installing from a wheel, CyRK will attempt to install Cython and Numpy in order to compile the cython code. After the files have been compiled, cython will be uninstalled and CyRK's runtime dependencies (see the pyproject.toml file for the latest list) will be installed instead.
CyRK requires the cython
and numpy
packages during installation. The numpy
, numba
, and scipy
packages are required for runtime.
A new installation of CyRK can be tested quickly by running the following from a python console.
from CyRK import test_cyrk, test_nbrk
test_cyrk()
# You will hopefully see the message "CyRK's cyrk_ode was tested successfully."
test_nbrk()
# You will hopefully see the message "CyRK's nbrk_ode was tested successfully."
Installation Troubleshooting
Please report installation issues. We will work on a fix and/or add workaround information here.
Due to issue 9, it is reccomended that you use the flag
--no-binary
, e.g.,pip install CyRK --no-binary="CyRK"
, to ensure that CyRK's cython code is compiled correctly (rather than pulled from a potentially broken cache).
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_nb(t, y):
dy = np.empty_like(y)
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_nb, 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_cy(t, y, dy):
dy[0] = (1. - 0.01 * y[1]) * y[0]
dy[1] = (0.02 * y[0] - 1.) * y[1]
Alternatively, you can use CyRK's conversion helper functions to automatically convert between numba/scipy and cyrk function calls.
from CyRK import nb2cy, cy2nb
@njit
def diffeq_nb(t, y):
dy = np.empty_like(y)
dy[0] = (1. - 0.01 * y[1]) * y[0]
dy[1] = (0.02 * y[0] - 1.) * y[1]
return dy
diffeq_cy = nb2cy(diffeq_nb, use_njit=True)
diffeq_nb2 = cy2nb(diffeq_cy, use_njit=True)
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_cy, 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 interested parties can learn about this package.
Renaud, Joe P. (2022). CyRK - ODE Integrator Implemented in Cython and Numba. Zenodo. https://doi.org/10.5281/zenodo.7093266
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.
Pauli Virtanen, Ralf Gommers, Travis E. Oliphant, Matt Haberland, Tyler Reddy, David Cournapeau, Evgeni Burovski, Pearu Peterson, Warren Weckesser, Jonathan Bright, Stéfan J. van der Walt, Matthew Brett, Joshua Wilson, K. Jarrod Millman, Nikolay Mayorov, Andrew R. J. Nelson, Eric Jones, Robert Kern, Eric Larson, CJ Carey, İlhan Polat, Yu Feng, Eric W. Moore, Jake VanderPlas, Denis Laxalde, Josef Perktold, Robert Cimrman, Ian Henriksen, E.A. Quintero, Charles R Harris, Anne M. Archibald, Antônio H. Ribeiro, Fabian Pedregosa, Paul van Mulbregt, and SciPy 1.0 Contributors. (2020) SciPy 1.0: Fundamental Algorithms for Scientific Computing in Python. Nature Methods, 17(3), 261-272.
Contribute to CyRK
Please look here for an up-to-date list of contributors to the CyRK package.
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.2.4-pp39-pypy39_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87b4122f7b031c191e4e3b2eb8f2e3ea56d2815c6cc92d5224aef6f99d1b44ff |
|
MD5 | 922895eb186484b9ddaece2afdd725b0 |
|
BLAKE2b-256 | 97ee055d4bed986cb344bc122199c0c0c1eca6fa103b3768aa372e208bc14926 |
Hashes for CyRK-0.2.4-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f28a7b2e678dce24d0b627316a3a48a73a600601370bca5831130b9c8f6794b8 |
|
MD5 | 195073f966ab646344639672ff77f2d7 |
|
BLAKE2b-256 | 1e146819664e636b3146021d7ec20acce9d422310fcbf61213137d31f2e8b5d3 |
Hashes for CyRK-0.2.4-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 378357bfc53f63ef9db807f4ebec8aa627bb590af4ae9bcfde4eaa5d0bba699e |
|
MD5 | c2f7faaea92e37af81913a515f9d7a9b |
|
BLAKE2b-256 | cdca5813f3d3b214af6f863904d321169f40d5c5026e0610e2144ce9dfc6a0f4 |
Hashes for CyRK-0.2.4-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff3339a60b8bd216f5b5308492d02742055c6b74ed014ccf9028434b8927ac7c |
|
MD5 | 675e7d688c7bf0f9bdabfd0ff2788918 |
|
BLAKE2b-256 | 5bb830670db2ed4a8e4e555d4c171f6d6284050daf6408c6d2cb16f0d2c351af |
Hashes for CyRK-0.2.4-pp38-pypy38_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fefdb3eec12d9cd70aeaa7b4b7fc132f8e2bfe71da88f2cbce838af4b717f832 |
|
MD5 | a272350d45e8aace0ace4274a3e79bb9 |
|
BLAKE2b-256 | 22d9b0a11eca2fec62ed857188cd0d9c20607f00a6a4d82652f7f00a4314a5d0 |
Hashes for CyRK-0.2.4-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c82f0a2e17ee58ecf37f90c69ace9ba8e9df1b3fab4009b4c4e211101b75252 |
|
MD5 | bf3909cf14a28c781e7b6e5d4dbf7a18 |
|
BLAKE2b-256 | 42f55b23d84e722fc0f57ec79bd0ef09570ae469eda701a574bdf7d53eb2483f |
Hashes for CyRK-0.2.4-pp38-pypy38_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68a8d3afa1687d46b5ea6d8c346497659fab8662d77f0414be8b5fe0c6702cae |
|
MD5 | 95cb384553d124610554b1ef55602b45 |
|
BLAKE2b-256 | 500c570808c68d43ba4fc377b871c205b52e278253ab187cfe347fa5a0065e1c |
Hashes for CyRK-0.2.4-pp38-pypy38_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a77a6012dae66596214fbd8f6fbff0b540d105278db0a8d5735af4a8ed689f1 |
|
MD5 | 9d752c1c712a53afea906d69c1eef340 |
|
BLAKE2b-256 | c2c5c274a51972734465661636be741f5202b3c5740c5f64183880b4f320ac90 |
Hashes for CyRK-0.2.4-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4dfdbccf7a9c324e1dc9ddf2df4e5c01654518bf097a301b9a920fb7656a657 |
|
MD5 | 308cd5d53265da4fab9b6947f7f7b7ae |
|
BLAKE2b-256 | 6373bf4bdca07322c3a88ed5e1b84eb19c578e8a58dd5b1e848843f7268ccf5e |
Hashes for CyRK-0.2.4-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d7778b6d25cb31b81ed5742c1173f3351035f5318b5e30b2b8426377a4e2d79 |
|
MD5 | 59cd8488fc47aa07a067e87f64789650 |
|
BLAKE2b-256 | 5ab283c605f224ec00b11b46db901318f0edb136bffb27f6532c6a0f9abb5147 |
Hashes for CyRK-0.2.4-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5080e2b5c21e138b1e3b1f478b82a47e7297941b17095dbd2d75414a730706a5 |
|
MD5 | 658b547aa08cb1d1193a38db91b65274 |
|
BLAKE2b-256 | fdc6f9852944d6db462681b71a516b41415b5ef8acc74a3354166162a9649005 |
Hashes for CyRK-0.2.4-cp310-cp310-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc179bceed62eec106f6394ca1a417132124a0e0d058303db5d797bb74bfdc98 |
|
MD5 | 441d4606fd0fee2608573610fd87a4ea |
|
BLAKE2b-256 | f47222c132dd59edd5b6b63545b2eb5e4a06324e83d6463e36a5c93f4feff4af |
Hashes for CyRK-0.2.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5b725dbe765d62716d147cf9f5cedf2e2229db3e143505c22c4e4f6a412b63b |
|
MD5 | d68165f79004d4abc154e383cbd6fc53 |
|
BLAKE2b-256 | 0a91c9e786e8981442938da6e0e428bbc82b02aade2da5644419eb16d1a6583f |
Hashes for CyRK-0.2.4-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cbbba3a3aaf2fe0ffc6fc31a34d7cf15b97ce0fb590c488ac854d39a69b3dcf |
|
MD5 | 794caa20bd1d995832679582a517362c |
|
BLAKE2b-256 | 2664dcd6a06b286ddeea26bcf30a620c8719832ac5b6316179894c752b3f5602 |
Hashes for CyRK-0.2.4-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9f9fc69762c63ca312dfd02d1716b9c74195217776908c376a4979172a0e987 |
|
MD5 | 04e2d9ad3961b472d14b541638f5004b |
|
BLAKE2b-256 | 9cf2db3d7fb4ac79149296a0bf076a13533c4284bb6bd3205dfdd45c539fedd2 |
Hashes for CyRK-0.2.4-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 405b638695491198b99482608e9db16ff074fb693ecb990848c85a26ef751c4a |
|
MD5 | a0d4dcfebf2fc2c35e274c2063466f51 |
|
BLAKE2b-256 | b5a11517c3d251c7e4f6fd05f17da731f5f3979c65c046eccaf9ba203f8e70bf |
Hashes for CyRK-0.2.4-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 865d0a4e09eb01b95acbc140bb56dc4bf6089aceba2f1725ec7ce40c19e5b370 |
|
MD5 | fc469a51d7b37829d71f902be59fe449 |
|
BLAKE2b-256 | 6b84af71cc53a1ce61cb9b1195b7e1ca518ac0dc171b0a0fad8e79ae91dfc4eb |
Hashes for CyRK-0.2.4-cp39-cp39-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a7566635b1ed1981ba6dbe242e3f9e2f1bfdd666daf9c7008c945db26542d1e |
|
MD5 | b522883535b547d07edfe8de77735f95 |
|
BLAKE2b-256 | d6bd8eafbbc6ea769f5c4cb699359e1284154e47a4b1fd998506636b207ec4f8 |
Hashes for CyRK-0.2.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c01bd88c129ac9fb6e606a97627b34e8c8fcb8f38b7aa2f2c306ec5e2a553985 |
|
MD5 | 81c94a7dfe792d8be8965269041c2b19 |
|
BLAKE2b-256 | 8c5104d3c1a1b7408d28f2dda264e57720249805e33a999efabd506870a2f639 |
Hashes for CyRK-0.2.4-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba8be22b96311688c2cf7c5386034b5c7e75d5549fea871ffda804a6b6b139e7 |
|
MD5 | 1f2f703c50d271a68c34c1f46d95c674 |
|
BLAKE2b-256 | 8bcb48b988eb59e84630a7de0cd50169a6282250db422126e2856f2658b29362 |
Hashes for CyRK-0.2.4-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d561520aa87a22f770b855cade116ecb4bd3b29f9e0c0df1f410f5206f97439 |
|
MD5 | f33dd447f52c380e721a013fd4a63103 |
|
BLAKE2b-256 | 8e0879ce52716a41dbac0fc8d28c69a8cf390b1c4b14dbcc3ad4ec6d76236eb5 |
Hashes for CyRK-0.2.4-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 315157bc1c87348f5e153e59fdc3b9b4f139016a6549de6e3674927e6301d4a3 |
|
MD5 | d28695549be059b7de2ee56644aec349 |
|
BLAKE2b-256 | 4a128f60ccfb1b6a9a08808b2a0519adddbdaac8b1e01909c4d67a3dd6760f4a |
Hashes for CyRK-0.2.4-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01b3a8fc1860491db74983cff0b9e355e56cd86fd17f7a9968895509ac01d8bc |
|
MD5 | d79313df801eb98ac50ae5ca09334159 |
|
BLAKE2b-256 | df1955b62c968f82c4059104fc9c4a775a7a2b5102f594b827d29871b2041786 |
Hashes for CyRK-0.2.4-cp38-cp38-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7e80bc0063983aef006b550f7c0b7b0fb43eff16379bad13e124bc2b0534bfd |
|
MD5 | 65bc432d9199c65ffaa1af31f2ffb816 |
|
BLAKE2b-256 | 299454c0165988ec921c472264d45a6ef415ddd4ff7467c51ced8fd2a111556d |
Hashes for CyRK-0.2.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 426dc6131b5f92be83a730004d3cdd44305288a29344ec4bbaf8919efca1ec3f |
|
MD5 | 27bedf236519277f98d6687328fa6214 |
|
BLAKE2b-256 | 4a711486571f136243c21cee1b7f0c2b496e1b99571f32842a75189dd5ac1a07 |
Hashes for CyRK-0.2.4-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66d0c24d106c57e09bbf5bd82b8efcfdd1b4b2ef15ae26d745cea539cfcf5bec |
|
MD5 | 522c6e34451fcda9e4a0d29a324c62d2 |
|
BLAKE2b-256 | 6abadda1bb68724b1f37983a2d8f893b97aaa0ce472356073dd922c5b74b66e2 |
Hashes for CyRK-0.2.4-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a343467da32a4c53c001d2c2aa71ab6cfeef7de731fafa3ce3a9a93f39388e8c |
|
MD5 | 959f4615b762d0c1255fad5749ec8e59 |
|
BLAKE2b-256 | e9904ea3299c6f1d5fdbe2e635635166396283db56a107b4aeb4c738d7fb2ae7 |