Skip to content

Commit

Permalink
#1726 make summary variables an attribute of the model
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Oct 25, 2021
1 parent 9b5d303 commit e092ef8
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 55 deletions.
16 changes: 11 additions & 5 deletions examples/scripts/experimental_protocols/cccv.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@
experiment = pybamm.Experiment(
[
(
"Discharge at C/5 for 10 hours or until 3.3 V",
"Discharge at C/5 for 10 hours or until 3.5 V",
"Rest for 1 hour",
"Charge at 1 A until 4.1 V",
"Hold at 4.1 V until 10 mA",
"Rest for 1 hour",
# "Hold at 4.1 V until 10 mA",
# "Rest for 1 hour",
),
]
* 3
)
model = pybamm.lithium_ion.DFN()

options = {"working electrode": "positive"}
model = pybamm.lithium_ion.DFN(options=options)
chemistry = pybamm.parameter_sets.Xu2019
param = pybamm.ParameterValues(chemistry=chemistry)
sim = pybamm.Simulation(
model, experiment=experiment, solver=pybamm.CasadiSolver("fast with events")
model,
experiment=experiment,
solver=pybamm.CasadiSolver("fast with events"),
parameter_values=param,
)
sim.solve()

Expand Down
25 changes: 25 additions & 0 deletions pybamm/models/full_battery_models/base_battery_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ def build_model(self):

pybamm.logger.debug("Setting degradation variables ({})".format(self.name))
self.set_degradation_variables()
self.set_summary_variables()

# Massive hack for consistent delta_phi = phi_s - phi_e with SPMe
# This needs to be corrected
Expand All @@ -741,6 +742,30 @@ def new_empty_copy(self):
new_model.length_scales = self.length_scales
return new_model

@property
def summary_variables(self):
return self._summary_variables

@summary_variables.setter
def summary_variables(self, value):
"""
Set summary variables
Parameters
----------
value : list of strings
Names of the summary variables. Must all be in self.variables.
"""
for var in value:
if var not in self.variables:
raise KeyError(
f"No cycling variable defined for summary variable '{var}'"
)
self._summary_variables = value

def set_summary_variables(self):
self._summary_variables = []

def set_external_circuit_submodel(self):
"""
Define how the external circuit defines the boundary conditions for the model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,44 @@ def set_degradation_variables(self):
}
)

def set_summary_variables(self):
"""
Sets the default summary variables.
"""
summary_variables = [
"Positive electrode capacity [A.h]",
# LAM, LLI
"Loss of active material in positive electrode [%]",
"Loss of lithium inventory [%]",
"Loss of lithium inventory, including electrolyte [%]",
# Total lithium
"Total lithium [mol]",
"Total lithium in electrolyte [mol]",
"Total lithium in positive electrode [mol]",
"Total lithium in particles [mol]",
# Lithium lost
"Total lithium lost [mol]",
"Total lithium lost from particles [mol]",
"Total lithium lost from electrolyte [mol]",
"Loss of lithium to SEI [mol]",
"Loss of lithium to lithium plating [mol]",
"Loss of capacity to SEI [A.h]",
"Loss of capacity to lithium plating [A.h]",
"Total lithium lost to side reactions [mol]",
"Total capacity lost to side reactions [A.h]",
# Resistance
"Local ECM resistance [Ohm]",
]

if not self.half_cell:
summary_variables += [
"Negative electrode capacity [A.h]",
"Loss of active material in negative electrode [%]",
"Total lithium in negative electrode [mol]",
]

self.summary_variables = summary_variables

def set_sei_submodel(self):
if self.half_cell:
reaction_loc = "interface"
Expand Down
85 changes: 35 additions & 50 deletions pybamm/solvers/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,45 +794,28 @@ def make_cycle_solution(step_solutions, esoh_sim=None, save_this_cycle=True):


def get_cycle_summary_variables(cycle_solution, esoh_sim):
Q = cycle_solution["Discharge capacity [A.h]"].data
min_Q = np.min(Q)
max_Q = np.max(Q)

cycle_summary_variables = pybamm.FuzzyDict(
{
"Minimum measured discharge capacity [A.h]": min_Q,
"Maximum measured discharge capacity [A.h]": max_Q,
"Measured capacity [A.h]": max_Q - min_Q,
}
)
cycle_summary_variables = {}

# Measured capacity variables
try:
Q = cycle_solution["Discharge capacity [A.h]"].data
except KeyError:
Q = None
if Q is not None:
min_Q = np.min(Q)
max_Q = np.max(Q)

cycle_summary_variables = pybamm.FuzzyDict(
{
"Minimum measured discharge capacity [A.h]": min_Q,
"Maximum measured discharge capacity [A.h]": max_Q,
"Measured capacity [A.h]": max_Q - min_Q,
}
)

degradation_variables = [
"Negative electrode capacity [A.h]",
"Positive electrode capacity [A.h]",
# LAM, LLI
"Loss of active material in negative electrode [%]",
"Loss of active material in positive electrode [%]",
"Loss of lithium inventory [%]",
"Loss of lithium inventory, including electrolyte [%]",
# Total lithium
"Total lithium [mol]",
"Total lithium in electrolyte [mol]",
"Total lithium in positive electrode [mol]",
"Total lithium in negative electrode [mol]",
"Total lithium in particles [mol]",
# Lithium lost
"Total lithium lost [mol]",
"Total lithium lost from particles [mol]",
"Total lithium lost from electrolyte [mol]",
"Loss of lithium to SEI [mol]",
"Loss of lithium to lithium plating [mol]",
"Loss of capacity to SEI [A.h]",
"Loss of capacity to lithium plating [A.h]",
"Total lithium lost to side reactions [mol]",
"Total capacity lost to side reactions [A.h]",
# Resistance
"Local ECM resistance [Ohm]",
]
# Degradation variables
model = cycle_solution.all_models[0]
degradation_variables = model.summary_variables
first_state = cycle_solution.first_state
last_state = cycle_solution.last_state
for var in degradation_variables:
Expand All @@ -844,7 +827,12 @@ def get_cycle_summary_variables(cycle_solution, esoh_sim):
data_last[0] - data_first[0]
)

if esoh_sim is not None:
# eSOH variables (full-cell lithium-ion model only, for now)
if (
esoh_sim is not None
and isinstance(model, pybamm.lithium_ion.BaseModel)
and model.half_cell is False
):
V_min = esoh_sim.parameter_values["Lower voltage cut-off [V]"]
V_max = esoh_sim.parameter_values["Upper voltage cut-off [V]"]
C_n = last_state["Negative electrode capacity [A.h]"].data[0]
Expand Down Expand Up @@ -885,19 +873,16 @@ def get_cycle_summary_variables(cycle_solution, esoh_sim):
esoh_sim.built_model.set_initial_conditions_from(
{"x_100": x_100_init, "C": C_init}
)
inputs = {
"V_min": V_min,
"V_max": V_max,
"C_n": C_n,
"C_p": C_p,
"n_Li": n_Li,
}

try:
esoh_sol = esoh_sim.solve(
[0],
inputs={
"V_min": V_min,
"V_max": V_max,
"C_n": C_n,
"C_p": C_p,
"n_Li": n_Li,
},
solver=solver,
)
esoh_sol = esoh_sim.solve([0], inputs=inputs, solver=solver)
except pybamm.SolverError: # pragma: no cover
raise pybamm.SolverError(
"Could not solve for summary variables, run "
Expand Down

0 comments on commit e092ef8

Please sign in to comment.