Skip to content

Commit

Permalink
#2382 working on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Oct 21, 2022
1 parent 7881598 commit f79ae87
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 70 deletions.
4 changes: 2 additions & 2 deletions examples/scripts/experimental_protocols/cccv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pybamm
import matplotlib.pyplot as plt

# pybamm.set_logging_level("INFO")
pybamm.set_logging_level("INFO")
experiment = pybamm.Experiment(
[
(
Expand All @@ -17,7 +17,7 @@
]
* 3
)
model = pybamm.lithium_ion.SPM()
model = pybamm.lithium_ion.DFN()

sim = pybamm.Simulation(
model, experiment=experiment, solver=pybamm.CasadiSolver("fast with events")
Expand Down
77 changes: 39 additions & 38 deletions pybamm/experiments/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,23 @@ def read_string(self, cond, drive_cycles):
cond_CC, cond_CV = cond.split(" then ")
op_CC = self.read_string(cond_CC, drive_cycles)
op_CV = self.read_string(cond_CV, drive_cycles)
return {
outputs = {
"type": "CCCV",
"Current input [A]": op_CC["Current input [A]"],
"Voltage input [V]": op_CV["Voltage input [V]"],
"time": op_CV["time"],
"period": op_CV["period"],
"dc_data": None,
"events": op_CV["events"],
}
if "Current input [A]" in op_CC:
outputs["Current input [A]"] = op_CC["Current input [A]"]
else:
outputs["C-rate input [-]"] = op_CC["C-rate input [-]"]
return outputs

# Read period
if " period)" in cond:
cond, time_period = cond.split("(")
cond, time_period = cond.split(" (")
time, _ = time_period.split(" period)")
period = self.convert_time_to_seconds(time.split())
else:
Expand All @@ -188,36 +193,24 @@ def read_string(self, cond, drive_cycles):
"Type of drive cycle must be specified using '(A)', '(V)' or '(W)'."
f" For example: {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
)
# Drive cycle as numpy array
dc_name = cond_list[1] + "_ext_{}".format(end_time)
dc_data = ext_drive_cycle
# Find the type of drive cycle ("A", "V", or "W")
typ = cond_list[2][1]
electric = (dc_name, typ)
time = ext_drive_cycle[:, 0][-1]
period = np.min(np.diff(ext_drive_cycle[:, 0]))
events = None
else:
# e.g. Run US06
# Drive cycle as numpy array
dc_name = cond_list[1]
dc_data = drive_cycles[cond_list[1]]
# Find the type of drive cycle ("A", "V", or "W")
typ = cond_list[2][1]
electric = (dc_name, 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]
period = np.min(np.diff(drive_cycles[cond_list[1]][:, 0]))
events = None
# Check for Events
if "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(dc_data, end_time)
# Drive cycle as numpy array
dc_data = ext_drive_cycle
# Set time and period to 1 second for first step and
# then calculate the difference in consecutive time steps
time = dc_data[:, 0][-1]
period = np.min(np.diff(dc_data[:, 0]))
# Find the unit of drive cycle ("A", "V", or "W")
unit = cond_list[2][1]
electric = {"dc_data": dc_data, "unit": unit}
events = None
else:
dc_data = None
if "for" in cond and "or until" in cond:
Expand Down Expand Up @@ -248,6 +241,16 @@ def read_string(self, cond, drive_cycles):
f"'Run'. For example: {examples}"
)

unit = electric.pop("unit")
if unit == "C":
electric["type"] = "C-rate"
elif unit == "A":
electric["type"] = "current"
elif unit == "V":
electric["type"] = "voltage"
elif unit == "W":
electric["type"] = "power"

return {
**electric,
"time": time,
Expand Down Expand Up @@ -335,13 +338,13 @@ def convert_electric(self, electric):
value = float(value_unit[:-1])
# Read value and units
if unit == "C":
return {"C-rate input [-]": sign * float(value), "type": "C-rate"}
return {"C-rate input [-]": sign * float(value), "unit": unit}
elif unit == "A":
return {"Current input [A]": sign * float(value), "type": "current"}
return {"Current input [A]": sign * float(value), "unit": unit}
elif unit == "V":
return {"Voltage input [V]": float(value), "type": "voltage"}
return {"Voltage input [V]": float(value), "unit": unit}
elif unit == "W":
return {"Power input [W]": sign * float(value), "type": "power"}
return {"Power input [W]": sign * float(value), "unit": unit}
else:
raise ValueError(
"""units must be 'C', 'A', 'mA', 'V', 'W' or 'mW', not '{}'.
Expand Down Expand Up @@ -425,8 +428,6 @@ def is_cccv(self, step, next_step):
op = self.read_string(step, None)
next_op = self.read_string(next_step, None)
# Check that the event conditions are the same as the hold conditions
if op["events"] == {
k: v for k, v in next_op["electric"] if k in op["events"]
}:
if op["events"] == {k: v for k, v in next_op.items() if k in op["events"]}:
return True
return False
64 changes: 34 additions & 30 deletions tests/unit/test_experiments/test_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_read_strings(self):
"period": 20.0,
"dc_data": None,
"string": "Discharge at 1C for 0.5 hours",
"event": None,
"events": None,
},
{
"C-rate input [-]": 0.05,
Expand All @@ -61,7 +61,7 @@ def test_read_strings(self):
"period": 20.0,
"dc_data": None,
"string": "Discharge at C/20 for 0.5 hours",
"event": None,
"events": None,
},
{
"C-rate input [-]": -0.5,
Expand All @@ -70,7 +70,7 @@ def test_read_strings(self):
"period": 20.0,
"dc_data": None,
"string": "Charge at 0.5 C for 45 minutes",
"event": None,
"events": None,
},
{
"Current input [A]": 1,
Expand All @@ -79,16 +79,16 @@ def test_read_strings(self):
"period": 20.0,
"dc_data": None,
"string": "Discharge at 1 A for 0.5 hours",
"event": None,
"events": None,
},
{
"Current input [A]": -0.2,
"type": "current",
"time": 2700.0,
"period": 60.0,
"dc_data": None,
"string": "Charge at 200 mA for 45 minutes (1 minute period)",
"event": None,
"string": "Charge at 200 mA for 45 minutes",
"events": None,
},
{
"Power input [W]": 1,
Expand All @@ -97,7 +97,7 @@ def test_read_strings(self):
"period": 20.0,
"dc_data": None,
"string": "Discharge at 1W for 0.5 hours",
"event": None,
"events": None,
},
{
"Power input [W]": -0.2,
Expand All @@ -106,16 +106,16 @@ def test_read_strings(self):
"period": 20.0,
"dc_data": None,
"string": "Charge at 200mW for 45 minutes",
"event": None,
"events": None,
},
{
"Current input [A]": 0,
"type": "current",
"time": 600.0,
"period": 300.0,
"dc_data": None,
"string": "Rest for 10 minutes (5 minute period)",
"event": None,
"string": "Rest for 10 minutes",
"events": None,
},
{
"Voltage input [V]": 1,
Expand All @@ -124,7 +124,7 @@ def test_read_strings(self):
"period": 20.0,
"dc_data": None,
"string": "Hold at 1V for 20 seconds",
"event": None,
"events": None,
},
{
"C-rate input [-]": -1,
Expand All @@ -133,7 +133,7 @@ def test_read_strings(self):
"period": 20.0,
"dc_data": None,
"string": "Charge at 1 C until 4.1V",
"event": {"Voltage input [V]": 4.1, "type": "voltage"},
"events": {"Voltage input [V]": 4.1, "type": "voltage"},
},
{
"Voltage input [V]": 4.1,
Expand All @@ -142,7 +142,7 @@ def test_read_strings(self):
"period": 20.0,
"dc_data": None,
"string": "Hold at 4.1 V until 50mA",
"event": {"Current input [A]": 0.05, "type": "current"},
"events": {"Current input [A]": 0.05, "type": "current"},
},
{
"Voltage input [V]": 3,
Expand All @@ -151,7 +151,7 @@ def test_read_strings(self):
"period": 20.0,
"dc_data": None,
"string": "Hold at 3V until C/50",
"event": {"C-rate input [-]": 0.02, "type": "C-rate"},
"events": {"C-rate input [-]": 0.02, "type": "C-rate"},
},
{
"C-rate input [-]": 1 / 3,
Expand All @@ -160,7 +160,7 @@ def test_read_strings(self):
"period": 20.0,
"dc_data": None,
"string": "Discharge at C/3 for 2 hours or until 2.5 V",
"event": {"Voltage input [V]": 2.5, "type": "voltage"},
"events": {"Voltage input [V]": 2.5, "type": "voltage"},
},
],
)
Expand Down Expand Up @@ -210,29 +210,32 @@ def test_read_strings_cccv_combined(self):
experiment.operating_conditions,
[
{
"Current input [A]": 0.05,
"C-rate input [-]": 0.05,
"type": "C-rate",
"time": 1800.0,
"period": 60.0,
"dc_data": None,
"event": None,
"string": "Discharge at C/20 for 0.5 hours",
"events": None,
},
{
"Current input [A]": -0.5,
"Voltage input [V]": 1,
"type": "CCCV",
"Voltage input [V]": 1,
"time": None,
"period": 60.0,
"dc_data": None,
"event": {"C-rate input [-]": 0.02, "type": "C-rate"},
"string": "Charge at 0.5 C until 1V then hold at 1V until C/50",
"events": {"C-rate input [-]": 0.02, "type": "C-rate"},
"C-rate input [-]": -0.5,
},
{
"Current input [A]": 0.05,
"C-rate input [-]": 0.05,
"type": "C-rate",
"time": 1800.0,
"period": 60.0,
"dc_data": None,
"event": None,
"string": "Discharge at C/20 for 0.5 hours",
"events": None,
},
],
)
Expand All @@ -254,15 +257,15 @@ def test_read_strings_cccv_combined(self):
"time": None,
"period": 60.0,
"dc_data": None,
"event": None,
"events": None,
},
{
"Current input [A]": 1,
"type": "voltage",
"time": None,
"period": 60.0,
"dc_data": None,
"event": None,
"events": None,
},
],
)
Expand All @@ -282,15 +285,15 @@ def test_read_strings_cccv_combined(self):
"time": 120.0,
"period": 60.0,
"dc_data": None,
"event": None,
"events": None,
},
{
"Current input [A]": 1,
"type": "voltage",
"time": None,
"period": 60.0,
"dc_data": None,
"event": None,
"events": None,
},
],
)
Expand All @@ -313,15 +316,16 @@ def test_cycle_unpacking(self):
"period": 60.0,
"dc_data": None,
"string": "Discharge at C/20 for 0.5 hours",
"event": None,
"events": None,
},
{
"C-rate input [-]": -0.2,
"type": "C-rate",
"time": 2700.0,
"period": 60.0,
"dc_data": None,
"strings": "Charge at C/5 for 45 minutes",
"string": "Charge at C/5 for 45 minutes",
"events": None,
},
{
"C-rate input [-]": 0.05,
Expand All @@ -330,7 +334,7 @@ def test_cycle_unpacking(self):
"period": 60.0,
"dc_data": None,
"string": "Discharge at C/20 for 0.5 hours",
"event": None,
"events": None,
},
{
"C-rate input [-]": -0.2,
Expand All @@ -339,7 +343,7 @@ def test_cycle_unpacking(self):
"period": 60.0,
"dc_data": None,
"string": "Charge at C/5 for 45 minutes",
"event": None,
"events": None,
},
],
)
Expand Down

0 comments on commit f79ae87

Please sign in to comment.