Skip to content

Commit

Permalink
Merge pull request #265 from pybamm-team/fix_protocols
Browse files Browse the repository at this point in the history
Update to PyBaMM 23.5
  • Loading branch information
TomTranter committed Jul 5, 2023
2 parents 4e4e13e + c800e9c commit 193483c
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 191 deletions.
144 changes: 24 additions & 120 deletions docs/examples/03 Experiments.ipynb

Large diffs are not rendered by default.

117 changes: 60 additions & 57 deletions docs/examples/05 Drive cycles.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies:
- sympy
- pip
- pip:
- pybamm>=23.3
- pybamm>=23.5
- ipdb
- ruff
- mkdocstrings-python-legacy
Expand Down
70 changes: 70 additions & 0 deletions examples/mixed_capacity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""
Example of running a simulation with batteries of different size.
"""

import liionpack as lp
import pybamm
import numpy as np

lp.logger.setLevel("NOTICE")

# Define parameters
Np = 2
Ns = 3
Iapp = 10

# Generate the netlist and output variables
netlist = lp.setup_circuit(
Np=Np,
Ns=Ns,
Rb=1.5e-3,
Rc=1e-2,
Ri=5e-2,
V=4.0,
I=Iapp,
configuration="series-groups",
)

lp.draw_circuit(netlist)

# Cycling experiment
experiment = pybamm.Experiment(
[
f"Discharge at {Iapp} A for 30 minutes",
"Rest for 30 minutes",
],
period="10 seconds",
)

# PyBaMM parameters
param = pybamm.ParameterValues("Chen2020")

w_original = param["Electrode width [m]"]

param.update(
{
"Electrode width [m]": "[input]",
}
)

new_widths = np.ones(Np * Ns) * w_original

# Divide the capacity by 2 for 1 half of the batteries
new_widths[:3] = w_original / 2

inputs = {
"Electrode width [m]": new_widths,
}

# Solve pack
output = lp.solve(
netlist=netlist,
parameter_values=param,
experiment=experiment,
initial_soc=None,
inputs=inputs,
)

# Plot results
lp.plot_output(output)
lp.show_plots()
17 changes: 8 additions & 9 deletions liionpack/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,25 @@ def generate_protocol_from_experiment(experiment, flatten=True):
"""
protocol = []
for i, op in enumerate(experiment.operating_conditions):
for i, step in enumerate(experiment.operating_conditions_steps):
proto = []
t = op["time"]
dt = op["period"]
t = step.duration
dt = step.period
if t % dt != 0:
raise ValueError("Time must be an integer multiple of the period")
typ = op["type"]
typ = step.type
if typ not in ["current"]:
raise ValueError("Only constant current operations are supported")
else:
if typ == "current":
if "Current input [A]" in op.keys():
I = op["Current input [A]"]
if not step.is_drive_cycle:
I = step.value
proto.extend([I] * int(t / dt))
if i == 0:
# Include initial state when not drive cycle, first op
proto = [proto[0]] + proto
elif "dc_data" in op.keys():
dc_data = op["dc_data"]
proto.extend(dc_data[:, 1].tolist())
else:
proto.extend(step.value.y.tolist())

if flatten:
protocol.extend(proto)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies = [
"openpyxl",
"pandas",
"plotly",
"pybamm>=23.3",
"pybamm>=23.5",
"ray",
"redis",
"scikit-spatial",
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/test_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ def test_generate_protocol_from_drive_cycle(self):
).to_numpy()

experiment = pybamm.Experiment(
operating_conditions=["Run US06 (A)"],
period="1 minute",
drive_cycles={"US06": drive_cycle},
[pybamm.step.current(drive_cycle)], period="1 second"
)
p = lp.generate_protocol_from_experiment(experiment)
assert len(p) == 601
Expand Down

0 comments on commit 193483c

Please sign in to comment.