Skip to content

Commit

Permalink
Merge pull request #3573 from pybamm-team/issue-2188-evaluate-at
Browse files Browse the repository at this point in the history
#2188 implement evaluate at in 1D
  • Loading branch information
rtimms authored Dec 5, 2023
2 parents cd39f6e + b2848f5 commit 9c85dd7
Show file tree
Hide file tree
Showing 19 changed files with 517 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Features

- Added a new unary operator, `EvaluateAt`, that evaluates a spatial variable at a given position ([#3573](https://github.com/pybamm-team/PyBaMM/pull/3573))
- Added a method, `insert_reference_electrode`, to `pybamm.lithium_ion.BaseModel` that insert a reference electrode to measure the electrolyte potential at a given position in space and adds new variables that mimic a 3E cell setup. ([#3573](https://github.com/pybamm-team/PyBaMM/pull/3573))
- Serialisation added so models can be written to/read from JSON ([#3397](https://github.com/pybamm-team/PyBaMM/pull/3397))
- Mechanical parameters are now a function of stoichiometry and temperature ([#3576](https://github.com/pybamm-team/PyBaMM/pull/3576))

Expand Down
6 changes: 6 additions & 0 deletions docs/source/api/expression_tree/unary_operator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ Unary Operators
.. autoclass:: pybamm.Mass
:members:

.. autoclass:: pybamm.BoundaryMass
:members:

.. autoclass:: pybamm.Integral
:members:

Expand All @@ -58,6 +61,9 @@ Unary Operators
.. autoclass:: pybamm.BoundaryGradient
:members:

.. autoclass:: pybamm.EvaluateAt
:members:

.. autoclass:: pybamm.UpwindDownwind
:members:

Expand Down
1 change: 1 addition & 0 deletions docs/source/examples/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ The notebooks are organised into subfolders, and can be viewed in the galleries
notebooks/models/rate-capability.ipynb
notebooks/models/saving_models.ipynb
notebooks/models/SEI-on-cracks.ipynb
notebooks/models/simulate-3E-cell.ipynb
notebooks/models/simulating-ORegan-2022-parameter-set.ipynb
notebooks/models/SPM.ipynb
notebooks/models/SPMe.ipynb
Expand Down
210 changes: 210 additions & 0 deletions docs/source/examples/notebooks/models/simulate-3E-cell.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Simulating a 3E cell\n",
"\n",
"In this notebook we show how to insert a reference electrode to mimic a 3E cell."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install \"pybamm[plot,cite]\" -q # install PyBaMM if it is not installed\n",
"import pybamm"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"We first load a model"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"model = pybamm.lithium_ion.DFN()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we use the helper function `insert_reference_electrode` to insert a reference electrode into the model. This function takes the position of the reference electrode as an optional argument. If no position is given, the reference electrode is inserted at the midpoint of the separator. The helper function adds the new variables \"Reference electrode potential [V]\", \"Negative electrode 3E potential [V]\" and \"Positive electrode 3E potential [V]\" to the model.\n",
"\n",
"In this example we will explicitly pass a position to show how it is done"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"L_n = model.param.n.L # Negative electrode thickness [m]\n",
"L_s = model.param.s.L # Separator thickness [m]\n",
"L_ref = L_n + L_s / 2 # Reference electrode position [m]\n",
"\n",
"model.insert_reference_electrode(L_ref)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we can set up a simulation and solve the model as usual"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<pybamm.solvers.solution.Solution at 0x169dc5950>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sim = pybamm.Simulation(model)\n",
"sim.solve([0, 3600])"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's plot a comparison of the 3E potentials and the potential difference between the solid and electrolyte phases at the electrode/separator interfaces"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d1f4e1ed03764660b87ee56b135b24a7",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=0.0, description='t', max=1.0, step=0.01), Output()), _dom_classes=('w…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<pybamm.plotting.quick_plot.QuickPlot at 0x169816a90>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sim.plot(\n",
" [\n",
" [\n",
" \"Negative electrode surface potential difference at separator interface [V]\",\n",
" \"Negative electrode 3E potential [V]\",\n",
" ],\n",
" [\n",
" \"Positive electrode surface potential difference at separator interface [V]\",\n",
" \"Positive electrode 3E potential [V]\",\n",
" ],\n",
" \"Voltage [V]\",\n",
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] Joel A. E. Andersson, Joris Gillis, Greg Horn, James B. Rawlings, and Moritz Diehl. CasADi – A software framework for nonlinear optimization and optimal control. Mathematical Programming Computation, 11(1):1–36, 2019. doi:10.1007/s12532-018-0139-4.\n",
"[2] Marc Doyle, Thomas F. Fuller, and John Newman. Modeling of galvanostatic charge and discharge of the lithium/polymer/insertion cell. Journal of the Electrochemical society, 140(6):1526–1533, 1993. doi:10.1149/1.2221597.\n",
"[3] Charles R. Harris, K. Jarrod Millman, Stéfan J. van der Walt, Ralf Gommers, Pauli Virtanen, David Cournapeau, Eric Wieser, Julian Taylor, Sebastian Berg, Nathaniel J. Smith, and others. Array programming with NumPy. Nature, 585(7825):357–362, 2020. doi:10.1038/s41586-020-2649-2.\n",
"[4] Scott G. Marquis, Valentin Sulzer, Robert Timms, Colin P. Please, and S. Jon Chapman. An asymptotic derivation of a single particle model with electrolyte. Journal of The Electrochemical Society, 166(15):A3693–A3706, 2019. doi:10.1149/2.0341915jes.\n",
"[5] Valentin Sulzer, Scott G. Marquis, Robert Timms, Martin Robinson, and S. Jon Chapman. Python Battery Mathematical Modelling (PyBaMM). Journal of Open Research Software, 9(1):14, 2021. doi:10.5334/jors.309.\n",
"\n"
]
}
],
"source": [
"pybamm.print_citations()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "9ff3d0c7e37de5f5aa47f4f719e4c84fc6cba7b39c571a05173422444e82fa58"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.7.4 ('dev': venv)",
"display_name": "dev",
"language": "python",
"name": "python3"
},
Expand All @@ -177,7 +177,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
"version": "3.9.16"
},
"toc": {
"base_numbering": 1,
Expand All @@ -194,7 +194,7 @@
},
"vscode": {
"interpreter": {
"hash": "0f0e5a277ebcf03e91e138edc3d4774b5dee64e7d6640c0d876f99a9f6b2a4dc"
"hash": "bca2b99bfac80e18288b793d52fa0653ab9b5fe5d22e7b211c44eb982a41c00c"
}
}
},
Expand Down
1 change: 1 addition & 0 deletions pybamm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
from .logger import logger, set_logging_level, get_new_logger
from .settings import settings
from .citations import Citations, citations, print_citations

#
# Classes for the Expression Tree
#
Expand Down
4 changes: 4 additions & 0 deletions pybamm/discretisations/discretisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,10 @@ def _process_symbol(self, symbol):
return child_spatial_method.boundary_value_or_flux(
symbol, disc_child, self.bcs
)
elif isinstance(symbol, pybamm.EvaluateAt):
return child_spatial_method.evaluate_at(
symbol, disc_child, symbol.position
)
elif isinstance(symbol, pybamm.UpwindDownwind):
direction = symbol.name # upwind or downwind
return spatial_method.upwind_or_downwind(
Expand Down
Loading

0 comments on commit 9c85dd7

Please sign in to comment.