Skip to content

Commit

Permalink
Merge pull request #1457 from alibh95/develop
Browse files Browse the repository at this point in the history
Type of Driving Cycle ('A', 'V', 'W') in Experiment Class
  • Loading branch information
valentinsulzer authored Apr 9, 2021
2 parents 4820ceb + 5264d4c commit e025ced
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
36 changes: 25 additions & 11 deletions pybamm/experiments/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
Charge at 1 C until 4.1V,
Hold at 4.1 V until 50 mA,
Hold at 3V until C/50,
Run US06,
Run US06 for 20 seconds,
Run US06 for 45 minutes,
Run US06 for 2 hours,
Run US06 until 4.1V,
Run US06 until 50 mA,
Run US06 until C/50,
Run US06 (A),
Run US06 (A) for 20 seconds,
Run US06 (V) for 45 minutes,
Run US06 (W) for 2 hours,
"""


Expand Down Expand Up @@ -170,20 +167,37 @@ def read_string(self, cond, drive_cycles):
examples
)
)
# Check for Events
dc_types = ["(A)", "(V)", "(W)"]
if all(x not in cond for x in dc_types):
raise ValueError(
"""Type of drive cycle must be
specified using '(A)', '(V)' or '(W)'.
For example: {}""".format(
examples
)
)
# Check for Events
elif "for" in cond:
# e.g. for 3 hours
idx = cond_list.index("for")
end_time = self.convert_time_to_seconds(cond_list[idx + 1 :])
ext_drive_cycle = self.extend_drive_cycle(drive_cycles[cond_list[1]],
end_time)
electric = (ext_drive_cycle[:, 1], "Drive")
# Drive cycle as numpy array
dc_data = ext_drive_cycle
# Find the type of drive cycle ("A", "V", or "W")
typ = cond_list[2][1]
electric = (dc_data, typ)
time = ext_drive_cycle[:, 0][-1]
period = np.min(np.diff(ext_drive_cycle[:, 0]))
events = None
else:
# e.g. Run US06
electric = (drive_cycles[cond_list[1]][:, 1], "Drive")
# Drive cycle as numpy array
dc_data = drive_cycles[cond_list[1]]
# Find the type of drive cycle ("A", "V", or "W")
typ = cond_list[2][1]
electric = (dc_data, typ)
# Set time and period to 1 second for first step and
# then calculate the difference in consecutive time steps
time = drive_cycles[cond_list[1]][:, 0][-1]
Expand Down Expand Up @@ -277,7 +291,7 @@ def convert_electric(self, electric):
sign = -1
else:
raise ValueError(
"""instruction must be 'discharge', 'charge', 'rest', 'hold' or 'Run'.
"""Instruction must be 'discharge', 'charge', 'rest', 'hold' or 'Run'.
For example: {}""".format(
examples
)
Expand Down
32 changes: 22 additions & 10 deletions tests/unit/test_experiments/test_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ def test_read_strings(self):
"Discharge at 1 A for 0.5 hours",
"Charge at 200 mA for 45 minutes (1 minute period)",
"Discharge at 1W for 0.5 hours",
"Charge at 200 mW for 45 minutes",
"Charge at 200mW for 45 minutes",
"Rest for 10 minutes (5 minute period)",
"Hold at 1V for 20 seconds",
"Charge at 1 C until 4.1V",
"Hold at 4.1 V until 50mA",
"Hold at 3V until C/50",
"Discharge at C/3 for 2 hours or until 2.5 V",
"Run US06",
"Run US06 for 5 minutes",
"Run US06 for 0.5 hours",
"Run US06 (A)",
"Run US06 (V) for 5 minutes",
"Run US06 (W) for 0.5 hours",
],
{"test": "test"}, drive_cycles={"US06": drive_cycle},
period="20 seconds",
Expand Down Expand Up @@ -74,16 +74,16 @@ def test_read_strings(self):
)
# Check drive cycle operating conditions
self.assertTrue(
((experiment.operating_conditions[-3][0] == drive_cycle[:, 1]).all() & (
experiment.operating_conditions[-3][1] == "Drive") & (
((experiment.operating_conditions[-3][0] == drive_cycle).all() & (
experiment.operating_conditions[-3][1] == "A") & (
experiment.operating_conditions[-3][2] == time_0).all() & (
experiment.operating_conditions[-3][3] == period_0).all() & (
experiment.operating_conditions[-2][0] == drive_cycle_1[:, 1]).all() & (
experiment.operating_conditions[-2][1] == "Drive") & (
experiment.operating_conditions[-2][0] == drive_cycle_1).all() & (
experiment.operating_conditions[-2][1] == "V") & (
experiment.operating_conditions[-2][2] == time_1).all() & (
experiment.operating_conditions[-2][3] == period_1).all() & (
experiment.operating_conditions[-1][0] == drive_cycle_2[:, 1]).all() & (
experiment.operating_conditions[-1][1] == "Drive") & (
experiment.operating_conditions[-1][0] == drive_cycle_2).all() & (
experiment.operating_conditions[-1][1] == "W") & (
experiment.operating_conditions[-1][2] == time_2).all() & (
experiment.operating_conditions[-1][3] == period_2).all())
)
Expand Down Expand Up @@ -170,10 +170,22 @@ def test_bad_strings(self):
pybamm.Experiment(["Discharge at 1 A at 2 hours"])
with self.assertRaisesRegex(ValueError, "Instruction must be"):
pybamm.Experiment(["Run at 1 A for 2 hours"])
with self.assertRaisesRegex(
ValueError, "Type of drive cycle must be"
):
pybamm.Experiment(["Run US06 for 2 hours"])
with self.assertRaisesRegex(
ValueError, "Instruction must be"
):
pybamm.Experiment(["Run at at 1 A for 2 hours"])
with self.assertRaisesRegex(
ValueError, "Instruction must be"
):
pybamm.Experiment(["Play at 1 A for 2 hours"])
with self.assertRaisesRegex(
ValueError, "Instruction"
):
pybamm.Experiment(["Cell Charge at 1 A for 2 hours"])
with self.assertRaisesRegex(ValueError, "units must be"):
pybamm.Experiment(["Discharge at 1 B for 2 hours"])
with self.assertRaisesRegex(ValueError, "time units must be"):
Expand Down

0 comments on commit e025ced

Please sign in to comment.