Name | poincare JSON |
Version |
0.5.0
JSON |
| download |
home_page | |
Summary | Simulation of dynamical systems. |
upload_time | 2024-01-19 11:52:49 |
maintainer | |
docs_url | None |
author | |
requires_python | >=3.7 |
license | MIT License Copyright (c) 2023 Mauro Silberberg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
keywords |
dynamical systems
differential equations
ode
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
![Package](https://img.shields.io/pypi/v/poincare?label=poincare)
![CodeStyle](https://img.shields.io/badge/code%20style-black-000000.svg)
![License](https://img.shields.io/pypi/l/poincare?label=license)
![PyVersion](https://img.shields.io/pypi/pyversions/poincare?label=python)
[![CI](https://github.com/maurosilber/poincare/actions/workflows/ci.yml/badge.svg)](https://github.com/maurosilber/poincare/actions/workflows/ci.yml)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/maurosilber/poincare/main.svg)](https://results.pre-commit.ci/latest/github/maurosilber/poincare/main)
# Poincaré: simulation of dynamical systems
Poincaré allows to define and simulate dynamical systems in Python.
### Definition
To define the system
$$ \\frac{dx}{dt} = -x \\quad \\text{with} \\quad x(0) = 1 $$
we write can:
```python
>>> from poincare import Variable, System, initial
>>> class Model(System):
... # Define a variable with name `x` with an initial value (t=0) of `1``.
... x: Variable = initial(default=1)
... # The rate of change of `x` (i.e. velocity) is assigned (<<) to `-x`.
... # This relation is assigned to a Python variable (`eq`)
... eq = x.derive() << -x
...
```
### Simulation
To simulate that system,
we do:
```python
>>> from poincare import Simulator
>>> sim = Simulator(Model)
>>> sim.solve(save_at=range(3))
x
time
0 1.000000
1 0.368139
2 0.135501
```
The output is a `pandas.DataFrame`,
which can be plotted with `.plot()`.
### Changing initial conditions
To change the initial condition,
we have two options.
1. Passing a dictionary to the \`solve\`\` method:
```python
>>> sim.solve(values={Model.x: 2}, save_at=range(3))
x
time
0 2.000000
1 0.736278
2 0.271002
```
which reuses the previously compiled model in the `Simulator` instance.
2. Instantiating the model with other values:
```python
>>> Simulator(Model(x=2)).solve(save_at=range(3))
x
time
0 2.000000
1 0.736278
2 0.271002
```
This second option allows to compose systems
into bigger systems.
See the example in [examples/oscillators.py](https://github.com/maurosilber/poincare/blob/main/examples/oscillators.py).
### Transforming the output
We can compute transformations of the output
by passing a dictionary of expressions:
```python
>>> Simulator(Model, transform={"x": Model.x, "2x": 2 * Model.x}).solve(save_at=range(3))
x 2x
time
0 1.000000 2.000000
1 0.368139 0.736278
2 0.135501 0.271002
```
### Higher-order systems
To define a higher-order system,
we have to assign an initial condition to the derivative of a variable:
```python
>>> from poincare import Derivative
>>> class Oscillator(System):
... x: Variable = initial(default=1)
... v: Derivative = x.derive(initial=0)
... eq = v.derive() << -x
...
>>> Simulator(Oscillator).solve(save_at=range(3))
x v
time
0 1.000000 0.000000
1 0.540366 -0.841561
2 -0.416308 -0.909791
```
### Constants, Parameters, and functions
Besides variables,
we can define parameters and constants,
and use functions from [symbolite](https://github.com/hgrecco/symbolite).
#### Constants
Constants allow to define common initial conditions for Variables and Derivatives:
```python
>>> from poincare import assign, Constant
>>> class Model(System):
... c: Constant = assign(default=1, constant=True)
... x: Variable = initial(default=c)
... y: Variable = initial(default=2 * c)
... eq_x = x.derive() << -x
... eq_y = y.derive() << -y
...
>>> Simulator(Model).solve(save_at=range(3))
x y
time
0 1.000000 2.000000
1 0.368139 0.736278
2 0.135501 0.271002
```
Now, we can vary their initial conditions jointly:
```python
>>> Simulator(Model(c=2)).solve(save_at=range(3))
x y
time
0 2.000000 4.000000
1 0.736278 1.472556
2 0.271001 0.542003
```
But we can break that connection by passing `y` initial value directly:
```python
>>> Simulator(Model(c=2, y=2)).solve(save_at=range(3))
x y
time
0 2.000000 2.000000
1 0.736278 0.736278
2 0.271002 0.271002
```
#### Parameters
Parameters are like Variables,
but their time evolution is given directly as a function of time,
Variables, Constants and other Parameters:
```python
>>> from poincare import Parameter
>>> class Model(System):
... p: Parameter = assign(default=1)
... x: Variable = initial(default=1)
... eq = x.derive() << -p * x
...
>>> Simulator(Model).solve(save_at=range(3))
x
time
0 1.000000
1 0.368139
2 0.135501
```
#### Functions
Symbolite functions are accessible from the `symbolite.scalar` module:
```python
>>> from symbolite import scalar
>>> class Model(System):
... x: Variable = initial(default=1)
... eq = x.derive() << scalar.sin(x)
...
>>> Simulator(Model).solve(save_at=range(3))
x
time
0 1.000000
1 1.951464
2 2.654572
```
### Units
poincaré also supports functions through
[`pint`](https://github.com/hgrecco/pint)
and [`pint-pandas`](https://github.com/hgrecco/pint-pandas).
```python
>>> import pint
>>> unit = pint.get_application_registry()
>>> class Model(System):
... x: Variable = initial(default=1 * unit.m)
... v: Derivative = x.derive(initial=0 * unit.m/unit.s)
... w: Parameter = assign(default=1 * unit.Hz)
... eq = v.derive() << -w**2 * x
...
>>> result = Simulator(Model).solve(save_at=range(3))
```
The columns have units of m and m/s, respectively.
`pint` raises a `DimensionalityError` if we try to add them:
```python
>>> result["x"] + result["v"]
Traceback (most recent call last):
...
pint.errors.DimensionalityError: Cannot convert from 'meter' ([length]) to 'meter / second' ([length] / [time])
```
We can remove the units and set them as string metadata with:
```python
>>> result.pint.dequantify()
x v
unit meter meter / second
time
0 1.000000 0.000000
1 0.540366 -0.841561
2 -0.416308 -0.909791
```
which allows to plot the DataFrame with `.plot()`.
## Installation
```bash
pip install -U poincare
```
Raw data
{
"_id": null,
"home_page": "",
"name": "poincare",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "dynamical systems,differential equations,ODE",
"author": "",
"author_email": "\"Hern\u00e1n E. Grecco\" <hernan.grecco@gmail.com>, Mauro Silberberg <maurosilber@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/db/ac/699ec3e67d9852f704addfacffbbce67a1e8a069aa56849c2096cf370d44/poincare-0.5.0.tar.gz",
"platform": null,
"description": "![Package](https://img.shields.io/pypi/v/poincare?label=poincare)\n![CodeStyle](https://img.shields.io/badge/code%20style-black-000000.svg)\n![License](https://img.shields.io/pypi/l/poincare?label=license)\n![PyVersion](https://img.shields.io/pypi/pyversions/poincare?label=python)\n[![CI](https://github.com/maurosilber/poincare/actions/workflows/ci.yml/badge.svg)](https://github.com/maurosilber/poincare/actions/workflows/ci.yml)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/maurosilber/poincare/main.svg)](https://results.pre-commit.ci/latest/github/maurosilber/poincare/main)\n\n# Poincar\u00e9: simulation of dynamical systems\n\nPoincar\u00e9 allows to define and simulate dynamical systems in Python.\n\n### Definition\n\nTo define the system\n\n$$ \\\\frac{dx}{dt} = -x \\\\quad \\\\text{with} \\\\quad x(0) = 1 $$\n\nwe write can:\n\n```python\n>>> from poincare import Variable, System, initial\n>>> class Model(System):\n... # Define a variable with name `x` with an initial value (t=0) of `1``.\n... x: Variable = initial(default=1)\n... # The rate of change of `x` (i.e. velocity) is assigned (<<) to `-x`.\n... # This relation is assigned to a Python variable (`eq`)\n... eq = x.derive() << -x\n...\n```\n\n### Simulation\n\nTo simulate that system,\nwe do:\n\n```python\n>>> from poincare import Simulator\n>>> sim = Simulator(Model)\n>>> sim.solve(save_at=range(3))\n x\ntime\n0 1.000000\n1 0.368139\n2 0.135501\n```\n\nThe output is a `pandas.DataFrame`,\nwhich can be plotted with `.plot()`.\n\n### Changing initial conditions\n\nTo change the initial condition,\nwe have two options.\n\n1. Passing a dictionary to the \\`solve\\`\\` method:\n\n```python\n>>> sim.solve(values={Model.x: 2}, save_at=range(3))\n x\ntime\n0 2.000000\n1 0.736278\n2 0.271002\n```\n\nwhich reuses the previously compiled model in the `Simulator` instance.\n\n2. Instantiating the model with other values:\n\n```python\n>>> Simulator(Model(x=2)).solve(save_at=range(3))\n x\ntime\n0 2.000000\n1 0.736278\n2 0.271002\n```\n\nThis second option allows to compose systems\ninto bigger systems.\nSee the example in [examples/oscillators.py](https://github.com/maurosilber/poincare/blob/main/examples/oscillators.py).\n\n### Transforming the output\n\nWe can compute transformations of the output\nby passing a dictionary of expressions:\n\n```python\n>>> Simulator(Model, transform={\"x\": Model.x, \"2x\": 2 * Model.x}).solve(save_at=range(3))\n x 2x\ntime\n0 1.000000 2.000000\n1 0.368139 0.736278\n2 0.135501 0.271002\n```\n\n### Higher-order systems\n\nTo define a higher-order system,\nwe have to assign an initial condition to the derivative of a variable:\n\n```python\n>>> from poincare import Derivative\n>>> class Oscillator(System):\n... x: Variable = initial(default=1)\n... v: Derivative = x.derive(initial=0)\n... eq = v.derive() << -x\n...\n>>> Simulator(Oscillator).solve(save_at=range(3))\n x v\ntime\n0 1.000000 0.000000\n1 0.540366 -0.841561\n2 -0.416308 -0.909791\n```\n\n### Constants, Parameters, and functions\n\nBesides variables,\nwe can define parameters and constants,\nand use functions from [symbolite](https://github.com/hgrecco/symbolite).\n\n#### Constants\n\nConstants allow to define common initial conditions for Variables and Derivatives:\n\n```python\n>>> from poincare import assign, Constant\n>>> class Model(System):\n... c: Constant = assign(default=1, constant=True)\n... x: Variable = initial(default=c)\n... y: Variable = initial(default=2 * c)\n... eq_x = x.derive() << -x\n... eq_y = y.derive() << -y\n...\n>>> Simulator(Model).solve(save_at=range(3))\n x y\ntime\n0 1.000000 2.000000\n1 0.368139 0.736278\n2 0.135501 0.271002\n```\n\nNow, we can vary their initial conditions jointly:\n\n```python\n>>> Simulator(Model(c=2)).solve(save_at=range(3))\n x y\ntime\n0 2.000000 4.000000\n1 0.736278 1.472556\n2 0.271001 0.542003\n```\n\nBut we can break that connection by passing `y` initial value directly:\n\n```python\n>>> Simulator(Model(c=2, y=2)).solve(save_at=range(3))\n x y\ntime\n0 2.000000 2.000000\n1 0.736278 0.736278\n2 0.271002 0.271002\n```\n\n#### Parameters\n\nParameters are like Variables,\nbut their time evolution is given directly as a function of time,\nVariables, Constants and other Parameters:\n\n```python\n>>> from poincare import Parameter\n>>> class Model(System):\n... p: Parameter = assign(default=1)\n... x: Variable = initial(default=1)\n... eq = x.derive() << -p * x\n...\n>>> Simulator(Model).solve(save_at=range(3))\n x\ntime\n0 1.000000\n1 0.368139\n2 0.135501\n```\n\n#### Functions\n\nSymbolite functions are accessible from the `symbolite.scalar` module:\n\n```python\n>>> from symbolite import scalar\n>>> class Model(System):\n... x: Variable = initial(default=1)\n... eq = x.derive() << scalar.sin(x)\n...\n>>> Simulator(Model).solve(save_at=range(3))\n x\ntime\n0 1.000000\n1 1.951464\n2 2.654572\n```\n\n### Units\n\npoincar\u00e9 also supports functions through\n[`pint`](https://github.com/hgrecco/pint)\nand [`pint-pandas`](https://github.com/hgrecco/pint-pandas).\n\n```python\n>>> import pint\n>>> unit = pint.get_application_registry()\n>>> class Model(System):\n... x: Variable = initial(default=1 * unit.m)\n... v: Derivative = x.derive(initial=0 * unit.m/unit.s)\n... w: Parameter = assign(default=1 * unit.Hz)\n... eq = v.derive() << -w**2 * x\n...\n>>> result = Simulator(Model).solve(save_at=range(3))\n```\n\nThe columns have units of m and m/s, respectively.\n`pint` raises a `DimensionalityError` if we try to add them:\n\n```python\n>>> result[\"x\"] + result[\"v\"]\nTraceback (most recent call last):\n...\npint.errors.DimensionalityError: Cannot convert from 'meter' ([length]) to 'meter / second' ([length] / [time])\n```\n\nWe can remove the units and set them as string metadata with:\n\n```python\n>>> result.pint.dequantify()\n x v\nunit meter meter / second\ntime\n0 1.000000 0.000000\n1 0.540366 -0.841561\n2 -0.416308 -0.909791\n```\n\nwhich allows to plot the DataFrame with `.plot()`.\n\n## Installation\n\n```bash\npip install -U poincare\n```\n",
"bugtrack_url": null,
"license": "MIT License Copyright (c) 2023 Mauro Silberberg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
"summary": "Simulation of dynamical systems.",
"version": "0.5.0",
"project_urls": {
"Bug Tracker": "https://github.com/maurosilber/poincare/issues",
"Homepage": "https://github.com/maurosilber/poincare"
},
"split_keywords": [
"dynamical systems",
"differential equations",
"ode"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "7526262fc6fc0090117138de7301258c2685ab9fd697ad23a0f892e861f4d616",
"md5": "8468b9db7c8e0c2ad54007db1d341fc0",
"sha256": "cdd54a4dc9a71dc4a62bc2100a3717ebe5f49c4a226aed3d1fde129e1cf047ab"
},
"downloads": -1,
"filename": "poincare-0.5.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8468b9db7c8e0c2ad54007db1d341fc0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 37187,
"upload_time": "2024-01-19T11:52:48",
"upload_time_iso_8601": "2024-01-19T11:52:48.305156Z",
"url": "https://files.pythonhosted.org/packages/75/26/262fc6fc0090117138de7301258c2685ab9fd697ad23a0f892e861f4d616/poincare-0.5.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "dbac699ec3e67d9852f704addfacffbbce67a1e8a069aa56849c2096cf370d44",
"md5": "425de8248a022713676c4ff69b7cca18",
"sha256": "95476ec9e9f88946c97a5e7bdbd4e7535038c597ef2924c2c36f7e4a8e0d2040"
},
"downloads": -1,
"filename": "poincare-0.5.0.tar.gz",
"has_sig": false,
"md5_digest": "425de8248a022713676c4ff69b7cca18",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 35730,
"upload_time": "2024-01-19T11:52:49",
"upload_time_iso_8601": "2024-01-19T11:52:49.464000Z",
"url": "https://files.pythonhosted.org/packages/db/ac/699ec3e67d9852f704addfacffbbce67a1e8a069aa56849c2096cf370d44/poincare-0.5.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-01-19 11:52:49",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "maurosilber",
"github_project": "poincare",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"tox": true,
"lcname": "poincare"
}