Skip to main content

ODE solver processing pint (using 'solve_ivp' function)

Project description

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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

solve_ivp_pint-1.0.1.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

solve_ivp_pint-1.0.1-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file solve_ivp_pint-1.0.1.tar.gz.

File metadata

  • Download URL: solve_ivp_pint-1.0.1.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for solve_ivp_pint-1.0.1.tar.gz
Algorithm Hash digest
SHA256 1a61984b388e91fe26d5ba5bc26d105773ce988e7950a12a809a097fcd661337
MD5 46778841248f62fc39ccef48229e84f0
BLAKE2b-256 cd90dd54fee74327257bb6879d25171aacc09a030d5f39335a278b2c2943576b

See more details on using hashes here.

File details

Details for the file solve_ivp_pint-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: solve_ivp_pint-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for solve_ivp_pint-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6720ebe7844d0e23a85e0d46522fa3c9a8f95692d9a6f96f14f49aece1d8cef0
MD5 29d7752cb70755c1ad1da45eaae32aae
BLAKE2b-256 817938e81ab459dadf3daec3dc925aa2548fdd13772179424037915a894a7ff9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page