Skip to content

Commit

Permalink
#2382 merge issue-2382-simulation-only
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Oct 21, 2022
2 parents 6266e87 + 0df3a59 commit 4ec21f3
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 170 deletions.
98 changes: 49 additions & 49 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("NOTICE")
experiment = pybamm.Experiment(
[
(
Expand All @@ -24,55 +24,55 @@
)
sim.solve()

# # Plot voltages from the discharge segments only
# fig, ax = plt.subplots()
# for i in range(3):
# # Extract sub solutions
# sol = sim.solution.cycles[i]
# # Extract variables
# t = sol["Time [h]"].entries
# V = sol["Terminal voltage [V]"].entries
# # Plot
# ax.plot(t - t[0], V, label="Discharge {}".format(i + 1))
# ax.set_xlabel("Time [h]")
# ax.set_ylabel("Voltage [V]")
# ax.set_xlim([0, 10])
# ax.legend(loc="lower left")
# Plot voltages from the discharge segments only
fig, ax = plt.subplots()
for i in range(3):
# Extract sub solutions
sol = sim.solution.cycles[i]
# Extract variables
t = sol["Time [h]"].entries
V = sol["Terminal voltage [V]"].entries
# Plot
ax.plot(t - t[0], V, label="Discharge {}".format(i + 1))
ax.set_xlabel("Time [h]")
ax.set_ylabel("Voltage [V]")
ax.set_xlim([0, 10])
ax.legend(loc="lower left")

# # Save time, voltage, current, discharge capacity, temperature, and electrolyte
# # concentration to csv and matlab formats
# sim.solution.save_data(
# "output.mat",
# [
# "Time [h]",
# "Current [A]",
# "Terminal voltage [V]",
# "Discharge capacity [A.h]",
# "X-averaged cell temperature [K]",
# "Electrolyte concentration [mol.m-3]",
# ],
# to_format="matlab",
# short_names={
# "Time [h]": "t",
# "Current [A]": "I",
# "Terminal voltage [V]": "V",
# "Discharge capacity [A.h]": "Q",
# "X-averaged cell temperature [K]": "T",
# "Electrolyte concentration [mol.m-3]": "c_e",
# },
# )
# # We can only save 0D variables to csv
# sim.solution.save_data(
# "output.csv",
# [
# "Time [h]",
# "Current [A]",
# "Terminal voltage [V]",
# "Discharge capacity [A.h]",
# "X-averaged cell temperature [K]",
# ],
# to_format="csv",
# )
# Save time, voltage, current, discharge capacity, temperature, and electrolyte
# concentration to csv and matlab formats
sim.solution.save_data(
"output.mat",
[
"Time [h]",
"Current [A]",
"Terminal voltage [V]",
"Discharge capacity [A.h]",
"X-averaged cell temperature [K]",
"Electrolyte concentration [mol.m-3]",
],
to_format="matlab",
short_names={
"Time [h]": "t",
"Current [A]": "I",
"Terminal voltage [V]": "V",
"Discharge capacity [A.h]": "Q",
"X-averaged cell temperature [K]": "T",
"Electrolyte concentration [mol.m-3]": "c_e",
},
)
# We can only save 0D variables to csv
sim.solution.save_data(
"output.csv",
[
"Time [h]",
"Current [A]",
"Terminal voltage [V]",
"Discharge capacity [A.h]",
"X-averaged cell temperature [K]",
],
to_format="csv",
)

# Show all plots
sim.plot()
31 changes: 20 additions & 11 deletions pybamm/experiments/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def __init__(
next_step = None
finished = True
if self.is_cccv(step, next_step):
processed_cycle.append(step + " then " + next_step)
processed_cycle.append(
f"{step} then {next_step[0].lower()}{next_step[1:]}"
)
idx += 2
else:
processed_cycle.append(step)
Expand Down Expand Up @@ -167,6 +169,7 @@ def read_string(self, cond, drive_cycles):
"time": op_CV["time"],
"period": op_CV["period"],
"dc_data": None,
"string": cond,
"events": op_CV["events"],
}
if "Current input [A]" in op_CC:
Expand Down Expand Up @@ -241,15 +244,8 @@ 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"
self.unit_to_type(electric)
self.unit_to_type(events)

return {
**electric,
Expand All @@ -260,6 +256,19 @@ def read_string(self, cond, drive_cycles):
"events": events,
}

def unit_to_type(self, electric):
if electric is not None:
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

def extend_drive_cycle(self, drive_cycle, end_time):
"Extends the drive cycle to enable for event"
temp_time = []
Expand Down Expand Up @@ -287,7 +296,7 @@ def convert_electric(self, electric):
"""Convert electrical instructions to consistent output"""
# Rest == zero current
if electric[0].lower() == "rest":
return {"Current input [A]": 0, "type": "current"}
return {"Current input [A]": 0, "unit": "A"}
else:
if len(electric) in [3, 4]:
if len(electric) == 4:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,7 @@ class PowerFunctionControl(FunctionControl):
"""External circuit with power control."""

def __init__(self, param, options, control="algebraic"):
super().__init__(
param,
self.constant_power,
options,
control=control,
)
super().__init__(param, self.constant_power, options, control=control)

def constant_power(self, variables):
I = variables["Current [A]"]
Expand Down
Loading

0 comments on commit 4ec21f3

Please sign in to comment.