-
-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into csmax-in-functions
- Loading branch information
Showing
12 changed files
with
263 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.