Python wrapper of LSODA (solving ODEs) which can be called from within numba functions.
Project description
numbalsoda
numbalsoda
is a python wrapper to the LSODA method in ODEPACK, which is for solving ordinary differential equation initial value problems. LSODA was originally written in Fortran. numbalsoda
is a wrapper to a C++ re-write of the original code: https://github.com/dilawar/libsoda
numbalsoda
also wraps the dop853
explicit Runge-Kutta method from this repository: https://github.com/jacobwilliams/dop853
This package is very similar to scipy.integrate.solve_ivp
(see here), when you set method = 'LSODA'
or method = DOP853
. But, scipy.integrate.solve_ivp
invokes the python interpreter every time step which can be slow. Also, scipy.integrate.solve_ivp
can not be used within numba jit-compiled python functions. In contrast, numbalsoda
never invokes the python interpreter during integration and can be used within a numba compiled function which makes numbalsoda
a lot faster than scipy for most problems, and achieves similar performance to Julia's DifferentialEquations.jl in some cases (see benchmark
folder).
Installation
Conda:
conda install -c conda-forge numbalsoda
Pip:
python -m pip install numbalsoda
Basic usage
from numbalsoda import lsoda_sig, lsoda, dop853
from numba import njit, cfunc
import numpy as np
@cfunc(lsoda_sig)
def rhs(t, u, du, p):
du[0] = u[0]-u[0]*u[1]
du[1] = u[0]*u[1]-u[1]*p[0]
funcptr = rhs.address # address to ODE function
u0 = np.array([5.,0.8]) # Initial conditions
data = np.array([1.0]) # data you want to pass to rhs (data == p in the rhs).
t_eval = np.linspace(0.0,50.0,1000) # times to evaluate solution
# integrate with lsoda method
usol, success = lsoda(funcptr, u0, t_eval, data = data)
# integrate with dop853 method
usol1, success1 = dop853(funcptr, u0, t_eval, data = data)
# usol = solution
# success = True/False
The variables u
, du
and p
in the rhs
function are pointers to an array of floats. Therefore, operations like np.sum(u)
or len(u)
will not work. However, you can use the function nb.carray()
to make a numpy array out of the pointers. For example:
import numba as nb
@cfunc(lsoda_sig)
def rhs(t, u, du, p):
u_ = nb.carray(u, (2,))
p_ = nb.carray(p, (1,))
# ... rest of rhs goes here using u_ and p_
Above, u_
and p_
are numpy arrays build out of u
and p
, and so functions like np.sum(u_)
will work.
Also, note lsoda
can be called within a jit-compiled numba function (see below). This makes it much faster than scipy if a program involves many integrations in a row.
@njit
def test():
usol, success = lsoda(funcptr, u0, t_eval, data = data)
return usol
usol = test() # this works!
@njit
def test_sp():
sol = solve_ivp(f_scipy, t_span, u0, t_eval = t_eval, method='LSODA')
return sol
sol = test_sp() # this does not work :(
Passing data to the right-hand-side function
In the examples shown above, we passed a an single array of floats to the right-hand-side function:
# ...
data = np.array([1.0])
usol, success = lsoda(funcptr, u0, t_eval, data = data)
However, sometimes you might want to pass more data types than just floats. For example, you might want to pass several integers, an array of floats, and an array of integers. One way to achieve this is with generating the cfunc
using a function like this:
def make_lsoda_func(param1, param2, param3):
@cfunc(lsoda_sig)
def rhs(t, x, du, p):
# Here param1, param2, and param3
# can be accessed.
du[0] = param1*t
# etc...
return rhs
rhs = make_lsoda_func(10.0, 5, 10000)
funcptr = rhs.address
# etc...
The only drawback of this approach is if you want to do many successive integrations where the parameters change because it would required re-compiling the cfunc
between each integration. This could be slow.
But! It is possible to pass arbitrary parameters without re-compiling the cfunc
, but it is a little tricky. The notebook passing_data_to_rhs_function.ipynb
gives an example that explains how.
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 numbalsoda-0.3.4-pp37-pypy37_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fdd9080cbcb330b72d643a52af8e627d269c524772afcacc54129295baeac912 |
|
MD5 | 25f878081e86840e3438aa462676206a |
|
BLAKE2b-256 | 4c599b22e289f7a8c69aa7698decc5bce0780446ceeb09c2857e9270794dfb7f |
Hashes for numbalsoda-0.3.4-pp37-pypy37_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06198f8ae6b5c81b84309386c3fbe383e05c0af3488ba542700ce1105f7c27d0 |
|
MD5 | 6b78da3e2d75250620621a2546a7273a |
|
BLAKE2b-256 | d56788e1ec87fbff58ea58e44a60a51a48818cde429d4efbd0a85aff4e082647 |
Hashes for numbalsoda-0.3.4-pp37-pypy37_pp73-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ecb81f44675bf6a53eaff6eabcab6797d81bbbc83f2429c5b0ddfe9e93b101f |
|
MD5 | f516c42c4be415886f382f8c4310317f |
|
BLAKE2b-256 | 8ae6a7996acee95bc27fcb2ea78ed329f570c2f6a8bfd04e1e91f2fb9d01b006 |
Hashes for numbalsoda-0.3.4-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc607de8bbebc3cb70f212b77431203b6ae2be55df819f5ed9d138bfa95cc27f |
|
MD5 | 81c4a273f47c358cce03a3dbdc344259 |
|
BLAKE2b-256 | 240b93fdee9b1c1a30e75ae66ed93de7a97895148fe021070988f5d2562884cc |
Hashes for numbalsoda-0.3.4-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 104285586ed426294845699de55625dd8a2ae8cb02960e21b15faee8b601d2f6 |
|
MD5 | 5de15b1a9276a116288d24307ab35d49 |
|
BLAKE2b-256 | ad8f79211651176f94bea45bf1d93e631d42ddff168b248445313939764a7976 |
Hashes for numbalsoda-0.3.4-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 944946e3f20f5fb67645220611575f12b6c742a292f6c7f5cb92faaafff56c2b |
|
MD5 | d90df49eb25972381e61cdfd7f2a2437 |
|
BLAKE2b-256 | a660ebc4fe8bada46d35d48f65a92843743625b3405e722f245ea4721ee7bc80 |
Hashes for numbalsoda-0.3.4-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3fcdf41928ab8bb1384d76455f922df27565a02bf7c4ece95788e58fecf9127f |
|
MD5 | 7a2d2e5d3048c9fe7e489eb77cd880c7 |
|
BLAKE2b-256 | 03115b5a534513d18abcae8d603f3bacf84ed23e4755f04b64e82089826521e6 |
Hashes for numbalsoda-0.3.4-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d8093988473e44059f34a443307df2f73880a93f6e51ca4ff109685f46f2576 |
|
MD5 | c42ef8a2c4933a6e612c7c38272179eb |
|
BLAKE2b-256 | 05334ef52040bf3098498a976c55f58fce5aa84947e8ba4d72a9a91999479ddd |
Hashes for numbalsoda-0.3.4-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 448c9d41c7a845b1f3368f56891245de49985b6002de03f8c7aa1e64a77bb185 |
|
MD5 | 762bed9964bb31a0ce7eca9b4fbb0b3a |
|
BLAKE2b-256 | bc7585a198a63aba6add4269ff7b9a9d03cfd6d6c49c4e0a7c3ca700e2d5f21d |
Hashes for numbalsoda-0.3.4-cp39-cp39-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28e74a4d0e4a5211898465f9a8635b987d70cd86cf58b63457669a52ed4d9c5c |
|
MD5 | 8982b6dcf2b3b4bcc685168b7fcc5810 |
|
BLAKE2b-256 | f43f3d0a65070f358b5a2811e2f60d91b11c82298eb97597bddb36363543c3e3 |
Hashes for numbalsoda-0.3.4-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e4aa2d99f0e23d9454289498a1500bd346965cd04cdfd9d748562344dc5bc6a |
|
MD5 | 758b3095cbb5d2554a29542f7de03016 |
|
BLAKE2b-256 | 2244e76cbf009995e8b98f41e595e5df8f6237a194dad6c6365ea9d2dd40bfed |
Hashes for numbalsoda-0.3.4-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 940ee9105fe3d3e0d6f8a94fca6dcc33aa00780b10705c93266198118f8d03f1 |
|
MD5 | 119dcd9d26e371340aec590739cb4bd9 |
|
BLAKE2b-256 | 4b87a4cf6a0963f9bc771a09f26940c7d0ecd0f8610549db2a7496e5eb3c0da0 |
Hashes for numbalsoda-0.3.4-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62420453dfc4d4b9f8984e6936bc0a03616b24ee50d2d5d5efd7dca8c46dfca7 |
|
MD5 | 4bae7e7b797dc86dd2f43bd0a7377109 |
|
BLAKE2b-256 | dab97134a73953af22f5b8bcebbac760c1aeb5051c61469e39c857e21ec9c0df |
Hashes for numbalsoda-0.3.4-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29d5e400eb234344a20aadd5a0e299bb13d6017bc3bd0990d078beb182d8ede1 |
|
MD5 | 336c71dfd811f5369c68e9b872c39cf3 |
|
BLAKE2b-256 | 520bf4ddd40b3fe8146a0be298c6d5e0847df673201ce7d09b81762614b4580f |
Hashes for numbalsoda-0.3.4-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e20be130b06b7849fa4240cf9d1f5033db062ba085bf750de4d4d4c34497625 |
|
MD5 | 11380d54509e77f31a1a39ffb418a846 |
|
BLAKE2b-256 | 2536f129ef86c671e2cdb26246614aa2928cdf50a65de17bea1c2c42bd66e347 |
Hashes for numbalsoda-0.3.4-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17452df9b7ea03bd7ffe6a4d395967d5ac599cbfe7da6d1ba52c4352713c98e8 |
|
MD5 | 9ba6811bd376b196db4151b1eb2b94c0 |
|
BLAKE2b-256 | eff83dd2c79e243dd0825469d9350b8e699d19416459f07d9c01ed91ce7de0c1 |
Hashes for numbalsoda-0.3.4-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51a185400c970da9459c0c498995c35236cb7a0607f7a06cac08cd6048023b38 |
|
MD5 | 6ff08ab26c74ac90e208a245f58660ab |
|
BLAKE2b-256 | 0906e05b617c1c190b15ddb0959117d9557b5def56b8d5f225dc6c10cfe48bf8 |
Hashes for numbalsoda-0.3.4-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a77ba80c4af74e0e272679185c874fe4bc3be0def045bfdf73bf7be1c93bc6e |
|
MD5 | 3c87959613de731a35b8ede3bd9211a7 |
|
BLAKE2b-256 | a90271d06eb6c77a0dabc8ac1fe33dd4ef92511b19103e44492b06aad1c29145 |
Hashes for numbalsoda-0.3.4-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90230aa5bb5cf83100d07bde63528150f8e09a41f9197f3a8d57150e23b8f30f |
|
MD5 | a668d56c0e787e1808de1c414b519732 |
|
BLAKE2b-256 | 3a2315d70a97aabf424a7543ea87137c1b1be458f0830e6fd92d9145a00f6ce3 |
Hashes for numbalsoda-0.3.4-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bcf69473673123837bb76863c11e0fd843ddf9b4650040bf79c67bebd76297a1 |
|
MD5 | d46c8249ffbd8b8db2b35f197a2a58e0 |
|
BLAKE2b-256 | ccdac8f511aa666e2afb3c7ce986c5ab1a7d76a5cb9ea33e4c0439e71ec33c72 |
Hashes for numbalsoda-0.3.4-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 550e52534215f17e92b85f61fa2bd1726e688c6d3fdba2725d0ca5bfc8dc040e |
|
MD5 | cfa415356c28ff9464584d0f7dfb10fc |
|
BLAKE2b-256 | d95a13f5e255acca18933c37badcba6b7a237d17b00112af85a853de7b9a89ab |
Hashes for numbalsoda-0.3.4-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7aa01bd4b46d6b8a1f518fe37430d041da0c397cce47f738c98e938c8dca6915 |
|
MD5 | 350cd8a6cfcf870757468e79d5e87263 |
|
BLAKE2b-256 | c3f4eefa4c43c70df98b24d98ecaa37ad6426288f2bfad33e243c0dcbfce3cf1 |
Hashes for numbalsoda-0.3.4-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c705dd08412e0d21a850535161678fc9c6da341145f1ea5658412b2c08cdce25 |
|
MD5 | f340389cc3b9f716b07549e7e616e9e6 |
|
BLAKE2b-256 | 7d09109c46be20e6de656d61550d39eed2dd4b993dbc0d27018c0662287aece5 |
Hashes for numbalsoda-0.3.4-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 465adcd1b03eaa7cd1c689b87a503349c24f053bf595474fdddc942872ac90fb |
|
MD5 | 6792e238b50889deda0ab46c9c492c5c |
|
BLAKE2b-256 | bbb9db51cde7efcbbafb20915005529dafabcc839fc0b0c50736925f7534e690 |
Hashes for numbalsoda-0.3.4-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4751bfbce14a14a7ea47316f5a1c905bf3ac44ed810762172f9b042d3ffdc8e3 |
|
MD5 | 2b9a0be89480385a339c9355917b3794 |
|
BLAKE2b-256 | 7a8099fda7a8526d6530869923b3f18f9807e556c9c9556cbd41998882a85cb6 |
Hashes for numbalsoda-0.3.4-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b19013f02d0b507827e3ccf3d784f7d481a61b9f8ccf593a98b03c434d6b1459 |
|
MD5 | 454f410566aa5f72e14f3e228d9df4ec |
|
BLAKE2b-256 | 06d184b92e666f80715647c54505db3cbf5d6d6fbee943a6fc8ffba8a6d728f5 |
Hashes for numbalsoda-0.3.4-cp36-cp36m-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 21d23b36a3b3754aa01e314291c9a7a294db303b8fbee655bbca445b60d6bdd4 |
|
MD5 | d939022ee4c090848d47608eda6c2b5a |
|
BLAKE2b-256 | 0e910a977b2c9f93c8aa62f2375546444b4d5896b97bbaece0db0472ed0e42cb |
Hashes for numbalsoda-0.3.4-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e66bc4d7fed26c98394759de997c347f2efe8585215cb0d0c2befa3ae19f39cf |
|
MD5 | 3cd50d395074544fa50cfc12ac50e157 |
|
BLAKE2b-256 | fce68d1328f0d918735e959447eb067d4a08d6e564e092f2046a351442d5e0e1 |