Skip to main content

solve_ivp_pint: ODE solver with unit support using Pint

pre-commit static analysis workflow test workflow

Problem

If you love typing and units as we do, but need to resort to integration, this library may be for you.

The solve_ivp_pint library allows you to use the solve_ivp ODE solver from the scipy.integrate library, while using the Pint library to assign units to its variables.

Install

Install via the pypi package solve_ivp_pint. If you use pip, run the following shell command:

pip install solve_ivp_pint

Use

This library's solve_ivp function has the same structure as the one in the scipy.integrate library:

solve_ivp(fun, t_span, y0, method='RK45', t_eval=None, dense_output=False, events=None, vectorized=False, args=None, **options)

For details on the (unitless) parameters see https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html#scipy.integrate.solve_ivp

Example

Let's model throwing a ball from 2 meters height with an initial speed of 1 m/s in the x-direction and 20 m/s in the y-direction. Further, let's assume we do this on the earth with a gravitational acceleration of 9.81 in the opposite of the y-direction. We can do this with solve_ivp_pint as:

import matplotlib.pyplot as plt
import numpy as np
from pint import Quantity, UnitRegistry

from solve_ivp_pint import solve_ivp

u = UnitRegistry()


# Define the ODE
def dydt(t: Quantity, y: list[Quantity]) -> list:  # noqa: ARG001
    """
    dy/dt of a ball.

    We assume the state y to be of the form [x, y, dx/dt, dy/dt]
    """
    vx = y[2]
    vy = y[3]
    dx_dt = vx
    dy_dt = vy
    dvx_dt = 0.0 * u.m / u.s**2
    dvy_dt = -9.81 * u.m / u.s**2
    return [dx_dt, dy_dt, dvx_dt, dvy_dt]


t0 = 0 * u.seconds  # initial time
tf = 3 * u.seconds  # final time

x_0: Quantity = 0.0 * u.m
y_0: Quantity = 2.0 * u.m  # we throw from 2m
vx_0: Quantity = 1.0 * u.m / u.s
vy_0: Quantity = 20.0 * u.m / u.s
y0 = [x_0, y_0, vx_0, vy_0]

t_eval = np.linspace(0, 3, 100) * u.s

# Solving
solution = solve_ivp(dxdt, [t0, tf], y0, t_eval=t_eval)

plt.title("Throwing a ball")
plt.plot([x.to(u.m).magnitude for x in solution.y[0]], [x.to(u.m).magnitude for x in solution.y[1]], "--")
plt.show()

Metadata

Release files for solve_ivp_pint 1.0.2

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

Source distribution (sdist)

Source distribution for solve_ivp_pint 1.0.2
File Size Uploaded
solve_ivp_pint-1.0.2.tar.gz 15.6 kB Details

Built distribution (wheel)

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

Total release size: 29.4 kB

Release files / solve_ivp_pint-1.0.2.tar.gz

Download URL solve_ivp_pint-1.0.2.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
241e8c238667c88ccd452cbdb37823f8099cbfa4e6976aa16e242283255e5a14
BLAKE2b-256 checksum
How to use checksums
5f6cf7baf2ab3bd5c54430568fbce35bd5f2ac7f0493133cd0acb9e810b8c833
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release files / solve_ivp_pint-1.0.2-py3-none-any.whl

Download URL solve_ivp_pint-1.0.2-py3-none-any.whl
Size 13.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
30da5d15ddfd580d21f96835cbcbe4cb0ebf459daca2f9d9edf388275828586f
BLAKE2b-256 checksum
How to use checksums
375bb3260a7bd73c07cb705fc0e3fcb33fccd4aa7a58121335374ca640b94748
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.2

2 release files

0.0.1

1 release file

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