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.2.tar.gz (15.6 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.2-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: solve_ivp_pint-1.0.2.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for solve_ivp_pint-1.0.2.tar.gz
Algorithm Hash digest
SHA256 241e8c238667c88ccd452cbdb37823f8099cbfa4e6976aa16e242283255e5a14
MD5 6a17d2da76806e2dca5a219ebf8b8aa8
BLAKE2b-256 5f6cf7baf2ab3bd5c54430568fbce35bd5f2ac7f0493133cd0acb9e810b8c833

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for solve_ivp_pint-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 30da5d15ddfd580d21f96835cbcbe4cb0ebf459daca2f9d9edf388275828586f
MD5 d6b590a0448da0770da03b9f4dbcc9a0
BLAKE2b-256 375bb3260a7bd73c07cb705fc0e3fcb33fccd4aa7a58121335374ca640b94748

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