Skip to content

Commit

Permalink
Merge branch 'develop' into csmax-in-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Jun 29, 2022
2 parents d16b76b + 7c5b44e commit 14d4c8c
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 93 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# [Unreleased](https://github.com/pybamm-team/PyBaMM/)

# [v22.6](https://github.com/pybamm-team/PyBaMM/tree/v22.6) - 2022-06-30

## Features

- Added open-circuit potential as a separate submodel ([#2094](https://github.com/pybamm-team/PyBaMM/pull/2094))
Expand Down
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ keywords:
- "expression tree"
- "python"
- "symbolic differentiation"
version: "22.5"
version: "22.6"
repository-code: "https://github.com/pybamm-team/PyBaMM"
title: "Python Battery Mathematical Modelling (PyBaMM)"
84 changes: 0 additions & 84 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,90 +302,6 @@ pybamm.citations.register("your_paper_bibtex_identifier")

wherever code is called that uses that citation (for example, in functions or in the `__init__` method of a class such as a model or solver).

## Benchmarks

A benchmark suite is located in the `benchmarks` directory at the root of the PyBaMM project. These benchmarks can be run using [airspeed velocity](https://asv.readthedocs.io/en/stable/) (`asv`).

### Running the benchmarks
First of all, you'll need `asv` installed:
```shell
pip install asv
```

To run the benchmarks for the latest commit on the `develop` branch, simply enter the following command:
```shell
asv run
```
If it is the first time you run `asv`, you will be prompted for information about your machine (e.g. its name, operating system, architecture...).

Running the benchmarks can take a while, as all benchmarks are repeated several times to ensure statistically significant results. If accuracy isn't an issue, use the `--quick` option to avoid repeating each benchmark multiple times.
```shell
asv run --quick
```

Benchmarks can also be run over a range of commits. For instance, the following command runs the benchmark suite over every commit between version `0.3` and the tip of the `develop` branch:
```shell
asv run v0.3..develop
```
Further information on how to run benchmarks with `asv` can be found in the documentation at [Using airspeed velocity](https://asv.readthedocs.io/en/stable/using.html).

`asv` is configured using a file `asv.conf.json` located at the root of the PyBaMM repository. See the [asv reference](https://asv.readthedocs.io/en/stable/reference.html) for details on available settings and options.

Benchmark results are stored in a directory `results/` at the location of the configuration file. There is one result file per commit, per machine.

### Visualising benchmark results

`asv` is able to generate a static website with a visualisation of the benchmarks results, i.e. the benchmark's duration as a function of the commit hash.
To generate the website, use
```shell
asv publish
```
then, to view the website:
```shell
asv preview
```

Current benchmarks over PyBaMM's history can be viewed at https://pybamm-team.github.io/pybamm-bench/

### Adding benchmarks

To contribute benchmarks to PyBaMM, add a new benchmark function in one of the files in the `benchmarks/` directory.
Benchmarks are distributed across multiple files, grouped by theme. You're welcome to add a new file if none of your benchmarks fit into one of the already existing files.
Inside a benchmark file (e.g. `benchmarks/benchmarks.py`) benchmarks functions are grouped within classes.

Note that benchmark functions _must_ start with the prefix `time_`, for instance
```python3
def time_solve_SPM_ScipySolver(self):
solver = pb.ScipySolver()
solver.solve(self.model, [0, 3600])
```

In the case where some setup is necessary, but should not be timed, a `setup` function
can be defined as a method of the relevant class. For example:
```python3
class TimeSPM:
def setup(self):
model = pb.lithium_ion.SPM()
geometry = model.default_geometry

# ...

self.model = model

def time_solve_SPM_ScipySolver(self):
solver = pb.ScipySolver()
solver.solve(self.model, [0, 3600])
```

Similarly, a `teardown` method will be run after the benchmark. Note that, unless the `--quick` option is used, benchmarks are executed several times for accuracy, and both the `setup` and `teardown` function are executed before/after each repetition.

Running benchmarks can take a while, and by default encountered exceptions will not be shown. When developing benchmarks, it is often convenient to use the following command instead of `asv run`:
```shell
asv dev
```

`asv dev` implies options `--quick`, `--show-stderr`, and `--dry-run` (to avoid updating the `results` directory).

## Infrastructure

### Setuptools
Expand Down
83 changes: 83 additions & 0 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
## Benchmarks

This directory contains the benchmark suites of the PyBaMM project. These benchmarks can be run using [airspeed velocity](https://asv.readthedocs.io/en/stable/) (`asv`).

### Running the benchmarks
First of all, you'll need `asv` installed:
```shell
pip install asv
```

To run the benchmarks for the latest commit on the `develop` branch, simply enter the following command:
```shell
asv run
```
If it is the first time you run `asv`, you will be prompted for information about your machine (e.g. its name, operating system, architecture...).

Running the benchmarks can take a while, as all benchmarks are repeated several times to ensure statistically significant results. If accuracy isn't an issue, use the `--quick` option to avoid repeating each benchmark multiple times.
```shell
asv run --quick
```

Benchmarks can also be run over a range of commits. For instance, the following command runs the benchmark suite over every commit between version `0.3` and the tip of the `develop` branch:
```shell
asv run v0.3..develop
```
Further information on how to run benchmarks with `asv` can be found in the documentation at [Using airspeed velocity](https://asv.readthedocs.io/en/stable/using.html).

`asv` is configured using a file `asv.conf.json` located at the root of the PyBaMM repository. See the [asv reference](https://asv.readthedocs.io/en/stable/reference.html) for details on available settings and options.

Benchmark results are stored in a directory `results/` at the location of the configuration file. There is one result file per commit, per machine.

### Visualising benchmark results

`asv` is able to generate a static website with a visualisation of the benchmarks results, i.e. the benchmark's duration as a function of the commit hash.
To generate the website, use
```shell
asv publish
```
then, to view the website:
```shell
asv preview
```

Current benchmarks over PyBaMM's history can be viewed at https://pybamm-team.github.io/pybamm-bench/

### Adding benchmarks

To contribute benchmarks to PyBaMM, add a new benchmark function in one of the files in the `benchmarks/` directory.
Benchmarks are distributed across multiple files, grouped by theme. You're welcome to add a new file if none of your benchmarks fit into one of the already existing files.
Inside a benchmark file (e.g. `benchmarks/benchmarks.py`) benchmarks functions are grouped within classes.

Note that benchmark functions _must_ start with the prefix `time_`, for instance
```python3
def time_solve_SPM_ScipySolver(self):
solver = pb.ScipySolver()
solver.solve(self.model, [0, 3600])
```

In the case where some setup is necessary, but should not be timed, a `setup` function
can be defined as a method of the relevant class. For example:
```python3
class TimeSPM:
def setup(self):
model = pb.lithium_ion.SPM()
geometry = model.default_geometry

# ...

self.model = model

def time_solve_SPM_ScipySolver(self):
solver = pb.ScipySolver()
solver.solve(self.model, [0, 3600])
```

Similarly, a `teardown` method will be run after the benchmark. Note that, unless the `--quick` option is used, benchmarks are executed several times for accuracy, and both the `setup` and `teardown` function are executed before/after each repetition.

Running benchmarks can take a while, and by default encountered exceptions will not be shown. When developing benchmarks, it is often convenient to use the following command instead of `asv run`:
```shell
asv dev
```

`asv dev` implies options `--quick`, `--show-stderr`, and `--dry-run` (to avoid updating the `results` directory).
79 changes: 79 additions & 0 deletions benchmarks/memory_sims.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import pybamm

parameters = ["Marquis2019", "Chen2020"]


class MemSPMSimulationCCCV:
param_names = ["parameter"]
params = parameters

def mem_setup_SPM_simulationCCCV(self, parameters):
self.param = pybamm.ParameterValues(parameters)
self.model = pybamm.lithium_ion.SPM()
exp = pybamm.Experiment(
[
"Discharge at C/5 for 10 hours or until 3.3 V",
"Rest for 1 hour",
"Charge at 1 A until 4.1 V",
"Hold at 4.1 V until 10 mA",
"Rest for 1 hour",
]
)
self.sim = pybamm.Simulation(
self.model, parameter_values=self.param, experiment=exp
)
return self.sim


class MemDFNSimulationCCCV:
param_names = ["parameter"]
params = parameters

def mem_setup_DFN_simulationCCCV(self, parameters):
self.param = pybamm.ParameterValues(parameters)
self.model = pybamm.lithium_ion.DFN()
exp = pybamm.Experiment(
[
"Discharge at C/5 for 10 hours or until 3.3 V",
"Rest for 1 hour",
"Charge at 1 A until 4.1 V",
"Hold at 4.1 V until 10 mA",
"Rest for 1 hour",
]
)
self.sim = pybamm.Simulation(
self.model, parameter_values=self.param, experiment=exp
)
return self.sim


class MemSPMSimulationGITT:
param_names = ["parameter"]
params = parameters

def mem_setup_SPM_simulationGITT(self, parameters):
self.param = pybamm.ParameterValues(parameters)
self.model = pybamm.lithium_ion.SPM()
exp = pybamm.Experiment(
[("Discharge at C/20 for 1 hour", "Rest for 1 hour")] * 20
)
self.sim = pybamm.Simulation(
self.model, parameter_values=self.param, experiment=exp
)
return self.sim


class MemDFNSimulationGITT:
param_names = ["parameter"]
params = parameters

def mem_setup_DFN_simulationGITT(self, parameters):
self.param = pybamm.ParameterValues(parameters)
self.model = pybamm.lithium_ion.SPM()
exp = pybamm.Experiment(
[("Discharge at C/20 for 1 hour", "Rest for 1 hour")] * 20
)
self.sim = pybamm.Simulation(
self.model, parameter_values=self.param, experiment=exp
)
return self.sim
89 changes: 89 additions & 0 deletions benchmarks/memory_unit_benchmarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import pybamm
import numpy as np


class MemCreateExpression:
def mem_create_expression(self):
self.R = pybamm.Parameter("Particle radius [m]")
D = pybamm.Parameter("Diffusion coefficient [m2.s-1]")
j = pybamm.Parameter("Interfacial current density [A.m-2]")
F = pybamm.Parameter("Faraday constant [C.mol-1]")
c0 = pybamm.Parameter("Initial concentration [mol.m-3]")
self.model = pybamm.BaseModel()

c = pybamm.Variable("Concentration [mol.m-3]", domain="negative particle")
N = -D * pybamm.grad(c)
dcdt = -pybamm.div(N)
self.model.rhs = {c: dcdt}

lbc = pybamm.Scalar(0)
rbc = -j / F / D
self.model.boundary_conditions = {
c: {"left": (lbc, "Neumann"), "right": (rbc, "Neumann")}
}

self.model.initial_conditions = {c: c0}
self.model.variables = {
"Concentration [mol.m-3]": c,
"Surface concentration [mol.m-3]": pybamm.surf(c),
"Flux [mol.m-2.s-1]": N,
}
return self.model


class MemParameteriseModel:
def setup(self):
MemCreateExpression.mem_create_expression(self)

def mem_parameterise(self):
param = pybamm.ParameterValues(
{
"Particle radius [m]": 10e-6,
"Diffusion coefficient [m2.s-1]": 3.9e-14,
"Interfacial current density [A.m-2]": 1.4,
"Faraday constant [C.mol-1]": 96485,
"Initial concentration [mol.m-3]": 2.5e4,
}
)

self.r = pybamm.SpatialVariable(
"r", domain=["negative particle"], coord_sys="spherical polar"
)

self.geometry = {
"negative particle": {self.r: {"min": pybamm.Scalar(0), "max": self.R}}
}
param.process_model(self.model)
param.process_geometry(self.geometry)
return param


class MemDiscretiseModel:
def setup(self):
MemCreateExpression.mem_create_expression(self)
MemParameteriseModel.mem_parameterise(self)

def mem_discretise(self):
MemCreateExpression.mem_create_expression(self)
MemParameteriseModel.mem_parameterise(self)
submesh_types = {"negative particle": pybamm.Uniform1DSubMesh}
var_pts = {self.r: 20}
mesh = pybamm.Mesh(self.geometry, submesh_types, var_pts)

spatial_methods = {"negative particle": pybamm.FiniteVolume()}
disc = pybamm.Discretisation(mesh, spatial_methods)
disc.process_model(self.model)
return disc


class MemSolveModel:
def setup(self):
MemCreateExpression.mem_create_expression(self)
MemParameteriseModel.mem_parameterise(self)
MemDiscretiseModel.mem_discretise(self)

def mem_solve(self):
solver = pybamm.ScipySolver()
t = np.linspace(0, 3600, 600)
solver.solve(self.model, t)
return solver
6 changes: 3 additions & 3 deletions benchmarks/time_solve_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def solve_model_once(model, solver, t_eval):


class TimeSolveSPM:
param_names = ['solve first', 'parameter']
param_names = ["solve first", "parameter"]
params = (
[False, True],
[
Expand Down Expand Up @@ -64,7 +64,7 @@ def time_solve_model(self, solve_first, parameters):


class TimeSolveSPMe:
param_names = ['solve first', 'parameter']
param_names = ["solve first", "parameter"]
params = (
[False, True],
[
Expand Down Expand Up @@ -118,7 +118,7 @@ def time_solve_model(self, solve_first, parameters):


class TimeSolveDFN:
param_names = ['solve first', 'parameter']
param_names = ["solve first", "parameter"]
params = (
[False, True],
[
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
author = "The PyBaMM Team"

# The short X.Y version
version = "22.5"
version = "22.6"
# The full version, including alpha/beta/rc tags
release = version

Expand Down
Loading

0 comments on commit 14d4c8c

Please sign in to comment.