Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added example conservation #1186

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Features

- Added an example script to check conservation of lithium ([#1186](https://github.com/pybamm-team/PyBaMM/pull/1186))

## Optimizations

Expand Down
48 changes: 48 additions & 0 deletions examples/scripts/conservation_lithium.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# Check conservation of lithium
#

import pybamm
import matplotlib.pyplot as plt

pybamm.set_logging_level("INFO")

model = pybamm.lithium_ion.DFN()

experiment = pybamm.Experiment(
[
"Discharge at 1C until 3.2 V",
"Rest for 2 hours",
"Charge at C/3 until 4 V",
"Charge at 4 V until 5 mA",
"Rest for 2 hours",
]
* 3
)

sim = pybamm.Simulation(model, experiment=experiment)
sim.solve()
solution = sim.solution

t = solution["Time [s]"].entries
Ne = solution["Total concentration in electrolyte [mol]"].entries
Np = solution["Total lithium in positive electrode [mol]"].entries
Nn = solution["Total lithium in negative electrode [mol]"].entries
Ntot = Np + Nn + Ne

fig, ax = plt.subplots(1, 2, figsize=(12, 5))

ax[0].plot(t, Ntot / Ntot[0] - 1)
ax[0].set_xlabel("Time (s)")
ax[0].set_ylabel("Variation of total lithium as fraction of initial value")

ax[1].plot(t, Np + Nn, label="total")
ax[1].plot(t, Np, label="positive")
ax[1].plot(t, Nn, label="negative")
ax[1].set_xlabel("Time (s)")
ax[1].set_ylabel("Total lithium in electrode (mol)")
ax[1].legend()

fig.tight_layout()

plt.show()