Repository: pybamm-team/PyBaMM
Pull request #3756 (2024-01-22 08:03:51)

Added more information to error when drive cycle starts at t>0
# Description

When a drive cycle starts at t>0, the current error thrown seems confusing. The changes made gives drive cycle a name and throws relevant exception when t>0. The documentation is changed accordingly to reflect this fact

Fixes #3612 

## Type of change

Please add a line in the relevant section of [CHANGELOG.md](https://github.com/pybamm-team/PyBaMM/blob/develop/CHANGELOG.md) to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

- [ ] New feature (non-breaking change which adds functionality)
- [ ] Optimization (back-end change that speeds up the code)
- [x] Bug fix (non-breaking change which fixes an issue)

# Key checklist:

- [x] No style issues: `$ pre-commit run` (or `$ nox -s pre-commit`) (see [CONTRIBUTING.md](https://github.com/pybamm-team/PyBaMM/blob/develop/CONTRIBUTING.md#installing-and-using-pre-commit) for how to set this up to run automatically when committing locally, in just two lines of code)
- [x] All tests pass: `$ python run-tests.py --all` (or `$ nox -s tests`)
- [x] The documentation builds: `$ python run-tests.py --doctest` (or `$ nox -s doctests`)

You can run integration tests, unit tests, and doctests together at once, using `$ python run-tests.py --quick` (or `$ nox -s quick`).

## Further checks:

- [x] Code is commented, particularly in hard-to-understand areas
- [ ] Tests added that prove fix is effective or that feature works



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8d793bd77..7d36a0c4b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
 
 ## Bug Fixes
 
+- Updated `_steps_util.py` to throw a specific exception when drive cycle starts at t>0 ([#3756](https://github.com/pybamm-team/PyBaMM/pull/3756))
 - Updated `plot_voltage_components.py` to support both `Simulation` and `Solution` objects. Added new methods in both `Simulation` and `Solution` classes for allow the syntax `simulation.plot_voltage_components` and `solution.plot_voltage_components`. Updated `test_plot_voltage_components.py` to reflect these changes ([#3723](https://github.com/pybamm-team/PyBaMM/pull/3723)).
 - The SEI thickness decreased at some intervals when the 'electron-migration limited' model was used. It has been corrected ([#3622](https://github.com/pybamm-team/PyBaMM/pull/3622))
 
diff --git a/docs/source/api/experiment/experiment_steps.rst b/docs/source/api/experiment/experiment_steps.rst
index 6a2e2abc3..61d800d92 100644
--- a/docs/source/api/experiment/experiment_steps.rst
+++ b/docs/source/api/experiment/experiment_steps.rst
@@ -1,7 +1,7 @@
 Experiment step functions
 =========================
 
-The following functions can be used to define steps in an experiment.
+The following functions can be used to define steps in an experiment. Note that the drive cycle must start at t=0
 
 .. autofunction:: pybamm.step.string
 
diff --git a/pybamm/experiment/step/_steps_util.py b/pybamm/experiment/step/_steps_util.py
index f44cf5211..6bf137ed1 100644
--- a/pybamm/experiment/step/_steps_util.py
+++ b/pybamm/experiment/step/_steps_util.py
@@ -120,8 +120,15 @@ class _Step:
                 t, y = value[:, 0], value[:, 1]
                 self.duration = t.max()
 
+            # Check that drive cycle starts at t=0
+            if t[0] > 0:
+                raise ValueError("Drive cycle must start at t=0")
+
             self.value = pybamm.Interpolant(
-                t, y, pybamm.t - pybamm.InputParameter("start time")
+                t,
+                y,
+                pybamm.t - pybamm.InputParameter("start time"),
+                name="Drive Cycle",
             )
             self.period = np.diff(t).min()
         else:


Test patch:
diff --git a/tests/unit/test_experiments/test_experiment_steps.py b/tests/unit/test_experiments/test_experiment_steps.py
index b99ae2239..7fc21bc14 100644
--- a/tests/unit/test_experiments/test_experiment_steps.py
+++ b/tests/unit/test_experiments/test_experiment_steps.py
@@ -5,6 +5,7 @@ import pybamm
 import unittest
 import numpy as np
 from datetime import datetime
+from pybamm.experiment.step import _Step
 
 
 class TestExperimentSteps(unittest.TestCase):
@@ -276,6 +277,13 @@ class TestExperimentSteps(unittest.TestCase):
         self.assertEqual(event.name, "Negative stoichiometry cut-off [experiment]")
         self.assertEqual(event.expression, 2)
 
+    def test_drive_cycle_start_time(self):
+        # An example where start_time t>0
+        t = np.array([[1, 1], [2, 2], [3, 3]])
+
+        with self.assertRaisesRegex(ValueError, "Drive cycle must start at t=0"):
+            _Step("current", t)
+
 
 if __name__ == "__main__":
     print("Add -v for more debug output")