Skip to content

Commit 92bb2b8

Browse files
authored
Fix drive cycle duration bug (#5153)
* ensure drive cycles end at `tf` * better error message for `t_eval`s * test * Update CHANGELOG.md
1 parent 69bb924 commit 92bb2b8

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
- Adds a composite electrode electrode soh model ([#5160](https://github.com/pybamm-team/PyBaMM/pull/5129))
88
- Generalises `set_initial_soc` to `set_initial_state` and adds Thevenin initial state setting. ([#5129](https://github.com/pybamm-team/PyBaMM/pull/5129))
99

10+
## Bug fixes
11+
12+
- Fixed a bug where the final duration of a drive cycle would not be inferred correctly. ([#5153](https://github.com/pybamm-team/PyBaMM/pull/5153))
13+
1014
# [v25.8.0](https://github.com/pybamm-team/PyBaMM/tree/v25.8.0) - 2025-08-04
1115

1216
## Features

src/pybamm/experiment/step/base_step.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ def _setup_timestepping(self, solver, tf, t_interp):
346346
# then truncate the drive cycle
347347
if t_eval[-1] > tf:
348348
t_eval = t_eval[t_eval <= tf]
349+
if t_eval[-1] != tf:
350+
t_eval = np.append(t_eval, tf)
349351
else:
350352
t_eval = np.array([0, tf])
351353

src/pybamm/solvers/base_solver.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,9 +1216,15 @@ def step(
12161216
t_eval = np.linspace(0, dt, npts)
12171217
elif t_eval is None:
12181218
t_eval = np.array([0, dt])
1219-
elif t_eval[0] != 0 or t_eval[-1] != dt:
1219+
elif t_eval[0] != 0:
12201220
raise pybamm.SolverError(
1221-
"Elements inside array t_eval must lie in the closed interval 0 to dt"
1221+
f"The first `t_eval` value ({t_eval[0]}) must be 0."
1222+
"Please correct your `t_eval` array."
1223+
)
1224+
elif t_eval[-1] != dt:
1225+
raise pybamm.SolverError(
1226+
f"The final `t_eval` value ({t_eval[-1]}) must be equal "
1227+
f"to the step time `dt` ({dt}). Please correct your `t_eval` array."
12221228
)
12231229
else:
12241230
pass

tests/integration/test_experiments.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,29 @@ def test_drive_cycle(self):
101101
# Add a small tolerance to account for numerical errors
102102
V_max = 4.00 - 1e-4
103103
assert np.all(sol["Terminal voltage [V]"].entries >= V_max)
104+
105+
def test_drive_cycle_duration(self):
106+
# Test that the drive cycle is truncated to duration timespan
107+
tf = 1
108+
drive_cycle = np.column_stack(
109+
(
110+
np.array([0, tf + 1]),
111+
np.array([0, -1 / 100]),
112+
)
113+
)
114+
c_step = pybamm.step.current(value=drive_cycle, duration=tf)
115+
experiment = pybamm.Experiment(
116+
[
117+
c_step,
118+
],
119+
)
120+
model = pybamm.lithium_ion.SPM()
121+
param = pybamm.ParameterValues("Chen2020")
122+
sim = pybamm.Simulation(
123+
model,
124+
experiment=experiment,
125+
parameter_values=param,
126+
)
127+
sol = sim.solve()
128+
129+
assert sol.t[-1] == tf

tests/unit/test_solvers/test_base_solver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ def test_nonmonotonic_teval(self):
9393
t_eval = np.array([0, 1])
9494
with pytest.raises(
9595
pybamm.SolverError,
96-
match="Elements inside array t_eval must lie in the closed interval 0 to dt",
96+
match="The final `t_eval` value \\(1\\) must be equal to the step time `dt` \\(2\\)",
9797
):
9898
solver.step(None, model, dt, t_eval=t_eval)
9999

100100
t_eval = np.array([1, dt])
101101
with pytest.raises(
102102
pybamm.SolverError,
103-
match="Elements inside array t_eval must lie in the closed interval 0 to dt",
103+
match="The first `t_eval` value \\(1\\) must be 0",
104104
):
105105
solver.step(None, model, dt, t_eval=t_eval)
106106

0 commit comments

Comments
 (0)