Skip to main content

_

pre-commit CI Publish documentation License: MIT Code style: black CodSpeed Badge status DOI

gotranx

gotranx is the next generation General ODE translator. The general idea is that you write your ODE in a high level markup language and use gotranx to generate code for solving the ODE in different programming languages. gotranx uses sympy to create a symbolic representation of the ODE which is used to generate the jacobian and numerical schemes.

gotranx makes it also possible generate code from e.g CellML models using conversion tools from myokit.

Install

Install with pip

python3 -m pip install gotranx

or for the development version

python3 -m pip install git+https://github.com/finsberg/gotranx

You can also install gotranx using conda

conda install -c conda-forge gotranx

Quick start

Define your ODE in a .ode file, e.g file.ode with the content

states(x=1, y=0)
parameters(a=1.0)

dx_dt = a * y
dy_dt = -x

which defines the ODE system

$$ \begin{align} \frac{dx}{dt} &= ay \ \frac{dy}{dt} &= -x \end{align} $$

with the initial conditions $x(0) = 1$ and $y(0) = 0$ and the parameter $a$ with a value of 1.0. Now generate code in python for solving this ODE with the explicit euler scheme using the command

gotranx ode2py file.ode --scheme explicit_euler -o file.py

which will create a file file.py containing functions for solving the ODE. Now you can solve the ode using the following code snippet

import file as model
import numpy as np
import matplotlib.pyplot as plt

s = model.init_state_values()
p = model.init_parameter_values()
dt = 1e-4  # 0.1 ms
T = 2 * np.pi
t = np.arange(0, T, dt)

x_index = model.state_index("x")
x = [s[x_index]]
y_index = model.state_index("y")
y = [s[y_index]]

for ti in t[1:]:
    s = model.explicit_euler(s, ti, dt, p)
    x.append(s[x_index])
    y.append(s[y_index])

plt.plot(t, x, label="x")
plt.plot(t, y, label="y")
plt.legend()
plt.show()

_

Alternatively, you can use a third-party ODE solver, e.g scipy.integrate.solve_ivp to solve the ODE by passing in the right-hand side function

import file as model
from scipy.integrate import solve_ivp
import numpy as np
import matplotlib.pyplot as plt

s = model.init_state_values()
p = model.init_parameter_values()
dt = 1e-4  # 0.1 ms
T = 2 * np.pi
t = np.arange(0, T, dt)

res = solve_ivp(
    model.rhs,
    (0, T),
    s,
    method="RK45",
    t_eval=t,
    args=(p,),
)

plt.plot(res.t, res.y.T)
plt.legend()
plt.show()

Note that this is a rather artificial example, so check out the demos in the documentation for more elaborate examples.

Syntax highlighting in VS Code

If you would like to have syntax highlighting in VS code for the .ode files, then you can check out the vscode extension found here: https://github.com/finsberg/gotranx-ode-vscode

FAQ

Why should I use gotranx? The main reasons to use gotranx are

  1. You want to solve your model using different programming languages (e.g python and C)
  2. You want to create a custom numerical scheme that can utilize the symbolic representation of the ODE
  3. You would like to share your model in a high level representation (i.e a markup language)

How does it differ from scipy.integrate.solve_ivp? scipy.integrate.solve_ivp is an ODE solver which takes as input a function defining the right-hand side. gotranx takes a high level representation of the ODE and can generate code for the right hand side. In other words, you can use scipy.integrate.solve_ivp to solve the ODE and use gotranx to generate the right hand side.

Automated tests

Unit tests

Automated tests can be found in the test folder. To run the tests please install the test dependencies

python3 -m pip install "gotranx[test]"

or if you have cloned the repo locally you can do

python3 -m pip install ".[test]"

To run the tests you should execute the following command

python3 -m pytest

Also note that the tests are run on every push and pull request to main using GitHub actions.

Linting and formatting

We use pre-commit to run a set of linters and formatters in order to ensure consistent code style. Developers should install the pre-commit hooks by first installing pre-commit

python3 -m pip install pre-commit

and then install the pre-commit hooks

pre-commit install

To run the hooks on all the files you can do

pre-commit run --all

For further instructions see the contributing guide.

Note also that we run all hooks as a part of our continuous integration, and we are also using pre-commit.ci to update branches automatically that can fix issues automatically.

Performance monitoring

We have defined a set of benchmarks that run on every push to the main branch using codspeed. To monitor the performance over time you can check out the performance report.

To run the benchmarks locally you can install the pytest-codspeed plugin

python3 -m pip install pytest-codspeed

and run

python3 -m pytest tests/ --codspeed

You can find more info at https://docs.codspeed.io/benchmarks/python

Citing

If you use gotranx in your research project we would appreciate if you could use the following citation

@article{Finsberg2024,
    doi = {10.21105/joss.07063},
    url = {https://doi.org/10.21105/joss.07063},
    year = {2024},
    publisher = {The Open Journal},
    volume = {9},
    number = {102},
    pages = {7063},
    author = {Henrik Finsberg and Johan Hake},
    title = {gotranx: General ODE translator},
    journal = {Journal of Open Source Software}
}

License

MIT

Contributing

Contributions are very welcomed, but please read the contributing guide first

Download files

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

Source Distribution

gotranx-2.1.0.tar.gz (120.4 kB view details)

Uploaded Source

Built Distribution

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

gotranx-2.1.0-py3-none-any.whl (88.5 kB view details)

Uploaded Python 3

File details

Details for the file gotranx-2.1.0.tar.gz.

File metadata

  • Download URL: gotranx-2.1.0.tar.gz
  • Upload date:
  • Size: 120.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for gotranx-2.1.0.tar.gz
Algorithm Hash digest
SHA256 6370e92b9ef475ddc8407ba957b17dafac760bc140e033a418eba21b54090c2d
MD5 4d805ce4e0fee422cd363ff8169de922
BLAKE2b-256 0d2f67a51cfa3adbc5af5aab97d94157b437de0650c44cf70c22a08caee8c960

See more details on using hashes here.

Provenance

The following attestation bundles were made for gotranx-2.1.0.tar.gz:

Publisher: pypi.yml on finsberg/gotranx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gotranx-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: gotranx-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 88.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for gotranx-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cf0376521cfbadf8e2825b97e61793b276382763b0dce6cdca58c62ee62970e1
MD5 03fb14ab7e35f3a18213907a2ddca5e1
BLAKE2b-256 80f80b543f811a5b3cb44d75d3a682935a71a98545773faaa5713ce78f28576b

See more details on using hashes here.

Provenance

The following attestation bundles were made for gotranx-2.1.0-py3-none-any.whl:

Publisher: pypi.yml on finsberg/gotranx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

2.1.0 This release

2 files

2.0.0

2 files

1.8.0

2 files

1.7.0

2 files

1.6.1

2 files

1.6.0

2 files

1.5.1

2 files

1.5.0

2 files

1.4.0

2 files

1.3.6

2 files

1.3.5

2 files

1.3.4

2 files

1.3.3

2 files

1.3.2

2 files

1.3.1

2 files

1.3.0

2 files

1.2.2

2 files

1.2.1

2 files

1.2.0

2 files

1.1.5

2 files

1.1.4

2 files

1.1.3

2 files

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

1.0.0

2 files

0.3.8

2 files

0.3.7

2 files

0.3.6

2 files

0.3.5

2 files

0.3.4

2 files

0.3.3

2 files

0.3.0

2 files

0.2.4

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.1

2 files

0.1.0

2 files

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