Skip to main content

A python package to solve first order ODEs

Project description

PyODESolver

Python package for solving ODEs

Installation

To install PyODESolver execute:

python3 -m pip install PyODESolver

This will make the package pyodesolver available for import. To this this launch python3

python3

and import the new package pyodesolver

import pyodesolver

Quickstart guide:

To get started with using PyODESolver include a solver method. In this example we will use the explicit euler method to solve the differential equation y'(t) = -2 * y(t), y(0) = pi. This differential equation has the exact solution

y(t) = pi * exp(-2 * t).

The right hand side in the expression

y'(t) = -2*y(t),
y(0) = pi

is called a RHSFunction. This example has been implemented in ExampleFunc01 in the file rhs_function.py. To use the explicit euler solver for this ODE we first import the required files

from method_explicit_euler import ExplicitEuler
from rhs_function import ExampleFunc01
import numpy as np

we can than construct a method object in our main function and call generate on it to get the solver object.

if __name__ == "__main__":
    N = 10**3
    t = np.linspace(0, 1, num=N)
    y0 = np.pi
    ee_solver = ExplicitEuler(N, y0, [0, 1], ExampleFunc01())
    solution = ee_solver.generate()

The generator object is useful in this instance as it very lean in term of required memory and puts full control to the user at it allows to generate new time steps as required. For example one thread can be used for updating a plot and a different thread can call next(solution) to match the framerate of the plot.

To extract the whole solution we can use:

numericSol = []
for (time, val) in solution:
    numericSol.append(val)

To plot the solution in this example we can use

import matplotlib.pyplot as plt
plt.plot(t, np.array(numericSol))
plt.show()

Solving arbitrary ODEs

To solve a customised ode the only two components required are the new RHSFunction and the Jacobian of this function. In cases where the Jacobian is unknown explicit methods will still function.

To implement a new RHSFunction the user can inherit from RHSFunction and define a new subclass

class NewFunction(RHSFunction):
...

RHSFunction has exposes three methods that raise NotImplementedErrors. The user can then implement these methods by providing

def eval(self, y_vec, time):
    ...

and

def jacobian(self, y_vec, time):
    ...

to provide everything required for explicit and implicit methods. The solution can be obtained as described in the Quick-start-guide.

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

PyODESolver-0.1.2.dev0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

PyODESolver-0.1.2.dev0-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file PyODESolver-0.1.2.dev0.tar.gz.

File metadata

  • Download URL: PyODESolver-0.1.2.dev0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.27.0 CPython/3.6.6

File hashes

Hashes for PyODESolver-0.1.2.dev0.tar.gz
Algorithm Hash digest
SHA256 b4c46a1913cb91afed2e16feea38ff48991fd7883cc6511883b742524b62fae8
MD5 2884240c73a3a6e33a33118af32b3087
BLAKE2b-256 417390dbe77c2c35df7c1a5747ea706612f09419fc10b512d76af72b4bd6970e

See more details on using hashes here.

File details

Details for the file PyODESolver-0.1.2.dev0-py3-none-any.whl.

File metadata

  • Download URL: PyODESolver-0.1.2.dev0-py3-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.27.0 CPython/3.6.6

File hashes

Hashes for PyODESolver-0.1.2.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 bde2b63bac79cdeb9ebba3ff9fe436195d8087ae08b704db181c67cc98060a4c
MD5 761ba5d274ac970f407bddc4e3f0aab1
BLAKE2b-256 62fdf57f35af5fbd55d38162053d144edd4c22a28bd8cfe4aa0a4b10836c2a4a

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