Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/martinjrobins/diffsol_python_benchmark
https://github.com/martinjrobins/diffsol_python_benchmark
Last synced: 3 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/martinjrobins/diffsol_python_benchmark
- Owner: martinjrobins
- License: mit
- Created: 2024-12-11T19:08:01.000Z (28 days ago)
- Default Branch: main
- Last Pushed: 2024-12-13T08:37:12.000Z (27 days ago)
- Last Synced: 2024-12-13T09:28:43.033Z (27 days ago)
- Language: Python
- Size: 27.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# DiffSol Python Benchmark
This package provides a set of benchmarks to compare the performance of different solvers in Python against the [DiffSol](https://github.com/martinjrobins/diffsol) solver.
## Installation
To install the package from PyPI, run the following command:
```bash
pip install diffsol_python_benchmark
```## Usage
### Casadi
TODO
### Scipy
TODO
### Diffrax
TODO
### PyBaMM
This is a demonstration of how to use [DiffSol](https://github.com/martinjrobins/diffsol) to solve a [PyBaMM](https://github.com/pybamm-team/PyBaMM/) model.
Use the following code to solve an PyBaMM SPM model using DiffSol and PyBaMM and compare the timings. You can change the outputs and inputs to any other parameters/variables in the model.
Note that this is only a demonstration and is only tested with the SPM model. It may not work with other models.
```python
from pybamm_diffsol import PybammDiffsol, Pybamm2Diffsl
import numpy as np
import timeit
import pybammoutputs = ["Voltage [V]"]
inputs = ["Current function [A]"]# read model from spm.ds file to a string
model_str = Pybamm2Diffsl(pybamm.lithium_ion.SPM()).to_str(inputs, outputs)
model = PybammDiffsol(model_str)
t_eval = np.array([0.0, 3600.0])
t_interp = np.linspace(0.0, 3600.0, 100)
params = np.array([1.0])
n = 1000def diffsol_bench():
model.solve(params, t_interp, t_eval)diffsol_time = timeit.timeit(diffsol_bench, number=n) / n
print("Diffsol time: ", diffsol_time)# solver pybamm spm model
spm = pybamm.lithium_ion.SPM()
t_eval = np.array([0.0, 3600.0])
t_interp = np.linspace(0.0, 3600.0, 100)
params = spm.default_parameter_values
for inpt in inputs:
params[inpt] = "[input]"
geometry = spm.default_geometryparams.process_model(spm)
params.process_geometry(geometry)
mesh = pybamm.Mesh(geometry, spm.default_submesh_types, spm.default_var_pts)
disc = pybamm.Discretisation(mesh, spm.default_spatial_methods)
disc.process_model(spm)
solver = pybamm.IDAKLUSolver()
inputs = {inpt: 1.0 for inpt in inputs}
solver.solve(spm, t_eval=t_eval, inputs=inputs)def pybamm_bench():
sol = solver.solve(spm, t_eval=t_eval, inputs=inputs)
# force evalulation of the outputs
for output in outputs:
sol[output].data[0]pybamm_time = timeit.timeit(pybamm_bench, number=n) / n
print("Pybamm time: ", pybamm_time)
print("Speedup: ", pybamm_time / diffsol_time)
```