All Projects → Nicholaswogan → NumbaLSODA

Nicholaswogan / NumbaLSODA

Licence: MIT License
Python wrapper of LSODA (solving ODEs) which can be called from within numba functions.

Programming Languages

Jupyter Notebook
11667 projects
C++
36643 projects - #6 most used programming language
python
139335 projects - #7 most used programming language
julia
2034 projects
CMake
9771 projects

Projects that are alternatives of or similar to NumbaLSODA

odepack
Work in Progress to refactor and modernize the ODEPACK Library
Stars: ✭ 30 (+0%)
Mutual labels:  ode-solver, odepack
heyoka
C++ library for ODE integration via Taylor's method and LLVM
Stars: ✭ 151 (+403.33%)
Mutual labels:  ode-solver
QUB DW HighPerformancePython
Code and more for the QUB Development Weeks event 'High Performance Python'
Stars: ✭ 79 (+163.33%)
Mutual labels:  numba
transonic
🚀 Make your Python code fly at transonic speeds!
Stars: ✭ 93 (+210%)
Mutual labels:  numba
dataiter
Python classes for data manipulation
Stars: ✭ 25 (-16.67%)
Mutual labels:  numba
numba-dppy
Numba extension for Intel(R) XPUs
Stars: ✭ 26 (-13.33%)
Mutual labels:  numba
heyoka.py
Python library for ODE integration via Taylor's method and LLVM
Stars: ✭ 45 (+50%)
Mutual labels:  ode-solver
ode-solvers
Numerical methods to solve ordinary differential equations in Rust.
Stars: ✭ 19 (-36.67%)
Mutual labels:  ode-solver
antropy
AntroPy: entropy and complexity of (EEG) time-series in Python
Stars: ✭ 111 (+270%)
Mutual labels:  numba
Python-Complementary-Languages
Just a small test to see which language is better for extending python when using lists of lists
Stars: ✭ 32 (+6.67%)
Mutual labels:  numba
gpu mandelbrot
Interactive Mandelbrot set on GPU with Python
Stars: ✭ 33 (+10%)
Mutual labels:  numba
eom
Configurable ODE/PDE solver
Stars: ✭ 50 (+66.67%)
Mutual labels:  ode-solver
NCCV
Short course on computer vision and image processing using Numba+CUDA+OpenCV
Stars: ✭ 22 (-26.67%)
Mutual labels:  numba
odex-js
Bulirsch-Stoer integration of systems of ordinary differential equations in JavaScript
Stars: ✭ 52 (+73.33%)
Mutual labels:  ode-solver
grblas
Python wrapper around GraphBLAS
Stars: ✭ 22 (-26.67%)
Mutual labels:  numba
codex-africanus
Radio Astronomy Algorithms Library
Stars: ✭ 13 (-56.67%)
Mutual labels:  numba
PyBox
A box-model that automatically creates and solves equations used to describe the evolution in atmospheric composition using Python with Numba and, optionally, Fortran.
Stars: ✭ 30 (+0%)
Mutual labels:  numba
Batch-First
A JIT compiled chess engine which traverses the search tree in batches in a best-first manner, allowing for neural network batching, asynchronous GPU use, and vectorized CPU computations.
Stars: ✭ 27 (-10%)
Mutual labels:  numba
numbsql
Run Numba compiled functions into SQLite
Stars: ✭ 34 (+13.33%)
Mutual labels:  numba
PySDM
Pythonic particle-based (super-droplet) warm-rain/aqueous-chemistry cloud microphysics package with box, parcel & 1D/2D prescribed-flow examples in Python, Julia and Matlab
Stars: ✭ 26 (-13.33%)
Mutual labels:  numba

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

This package is very similar to scipy.integrate.solve_ivp (see here), when you set method = 'LSODA'. 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 (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
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

usol, success = lsoda(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. This is possible, but a little tricky. The notebook passing_data_to_rhs_function.ipynb gives an example that explains how.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].