From db4b04d2673220af72a2d3413d882070693d1ae2 Mon Sep 17 00:00:00 2001 From: Ali Hussain Umar Bhatti Date: Thu, 8 Apr 2021 15:27:55 +0500 Subject: [PATCH 1/5] Type of Driving Cycle ('A', 'V', 'W') Modified the experiment.py file to process the type of driving cycle ('A', 'V', 'W') in the operating condition string as suggested by @tinosulzer . --- pybamm/experiments/experiment.py | 19 ++++++++++--------- .../unit/test_experiments/test_experiment.py | 12 ++++++------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/pybamm/experiments/experiment.py b/pybamm/experiments/experiment.py index 147c928130..54c9dca1b6 100644 --- a/pybamm/experiments/experiment.py +++ b/pybamm/experiments/experiment.py @@ -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, """ @@ -177,13 +174,17 @@ def read_string(self, cond, drive_cycles): 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") + interp = ext_drive_cycle[:, 1] # Electric Part of the Drive Cycle as Numpy Array + typ = cond_list[2][1] # Find the Type of Drive Cycle ("A", "V", or "W") + electric = (interp, 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") + interp = drive_cycles[cond_list[1]][:, 1] # Electric Part of the Drive Cycle as Numpy Array + typ = cond_list[2][1] # Find the Type of Drive Cycle ("A", "V", or "W") + electric = (interp, 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] diff --git a/tests/unit/test_experiments/test_experiment.py b/tests/unit/test_experiments/test_experiment.py index 874e35846a..b8e13f4346 100644 --- a/tests/unit/test_experiments/test_experiment.py +++ b/tests/unit/test_experiments/test_experiment.py @@ -34,9 +34,9 @@ def test_read_strings(self): "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", @@ -75,15 +75,15 @@ 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][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][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][1] == "W") & ( experiment.operating_conditions[-1][2] == time_2).all() & ( experiment.operating_conditions[-1][3] == period_2).all()) ) From b3869e677fa8c9c10c86ad5ab60186cda9c8402d Mon Sep 17 00:00:00 2001 From: Ali Hussain Umar Bhatti <65511923+alibh95@users.noreply.github.com> Date: Thu, 8 Apr 2021 15:51:46 +0500 Subject: [PATCH 2/5] Fix Style Issue --- pybamm/experiments/experiment.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pybamm/experiments/experiment.py b/pybamm/experiments/experiment.py index 54c9dca1b6..72afc183d7 100644 --- a/pybamm/experiments/experiment.py +++ b/pybamm/experiments/experiment.py @@ -174,16 +174,20 @@ def read_string(self, cond, drive_cycles): 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) - interp = ext_drive_cycle[:, 1] # Electric Part of the Drive Cycle as Numpy Array - typ = cond_list[2][1] # Find the Type of Drive Cycle ("A", "V", or "W") + # Electric Part of the Drive Cycle as Numpy Array + interp = ext_drive_cycle[:, 1] + # Find the Type of Drive Cycle ("A", "V", or "W") + typ = cond_list[2][1] electric = (interp, typ) time = ext_drive_cycle[:, 0][-1] period = np.min(np.diff(ext_drive_cycle[:, 0])) events = None else: # e.g. Run US06 - interp = drive_cycles[cond_list[1]][:, 1] # Electric Part of the Drive Cycle as Numpy Array - typ = cond_list[2][1] # Find the Type of Drive Cycle ("A", "V", or "W") + # Electric Part of the Drive Cycle as Numpy Array + interp = drive_cycles[cond_list[1]][:, 1] + # Find the Type of Drive Cycle ("A", "V", or "W") + typ = cond_list[2][1] electric = (interp, typ) # Set time and period to 1 second for first step and # then calculate the difference in consecutive time steps From a99d14643703552db60f61151c4eb8e95943fb6d Mon Sep 17 00:00:00 2001 From: Ali Hussain Umar Bhatti <65511923+alibh95@users.noreply.github.com> Date: Fri, 9 Apr 2021 07:21:13 +0500 Subject: [PATCH 3/5] DC Data as ndarray and added tests --- pybamm/experiments/experiment.py | 29 ++++++++++++------- .../unit/test_experiments/test_experiment.py | 16 +++++++--- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/pybamm/experiments/experiment.py b/pybamm/experiments/experiment.py index 72afc183d7..c4e987dff9 100644 --- a/pybamm/experiments/experiment.py +++ b/pybamm/experiments/experiment.py @@ -167,28 +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 diving 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 Part of the Drive Cycle as Numpy Array - interp = ext_drive_cycle[:, 1] - # Find the Type of Drive Cycle ("A", "V", or "W") + # 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 = (interp, typ) + 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 Part of the Drive Cycle as Numpy Array - interp = drive_cycles[cond_list[1]][:, 1] - # Find the Type of Drive Cycle ("A", "V", or "W") + # 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 = (interp, typ) + 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] @@ -282,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 ) diff --git a/tests/unit/test_experiments/test_experiment.py b/tests/unit/test_experiments/test_experiment.py index b8e13f4346..175863d12c 100644 --- a/tests/unit/test_experiments/test_experiment.py +++ b/tests/unit/test_experiments/test_experiment.py @@ -27,7 +27,7 @@ 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", @@ -74,15 +74,15 @@ 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][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][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][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()) @@ -174,6 +174,14 @@ def test_bad_strings(self): 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"): From 1c9e447e8b0972f642185d1e5e817d8e10c23bd3 Mon Sep 17 00:00:00 2001 From: Ali Hussain Umar Bhatti <65511923+alibh95@users.noreply.github.com> Date: Fri, 9 Apr 2021 07:42:51 +0500 Subject: [PATCH 4/5] Resolved Typo --- pybamm/experiments/experiment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pybamm/experiments/experiment.py b/pybamm/experiments/experiment.py index c4e987dff9..724d913565 100644 --- a/pybamm/experiments/experiment.py +++ b/pybamm/experiments/experiment.py @@ -170,7 +170,7 @@ def read_string(self, cond, drive_cycles): dc_types = ["(A)", "(V)", "(W)"] if all(x not in cond for x in dc_types): raise ValueError( - """Type of diving cycle must be + """Type of drive cycle must be specified using '(A)', '(V)' or '(W)'. For example: {}""".format( examples From 5264d4cd431ab13ca8d25564affc7bf637187a59 Mon Sep 17 00:00:00 2001 From: Ali Hussain Umar Bhatti <65511923+alibh95@users.noreply.github.com> Date: Fri, 9 Apr 2021 11:39:55 +0500 Subject: [PATCH 5/5] Added Test. --- tests/unit/test_experiments/test_experiment.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/unit/test_experiments/test_experiment.py b/tests/unit/test_experiments/test_experiment.py index 175863d12c..4a39436fe2 100644 --- a/tests/unit/test_experiments/test_experiment.py +++ b/tests/unit/test_experiments/test_experiment.py @@ -170,6 +170,10 @@ 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" ):