Custom termination event with discharge capacity

Hi,

I am doing a discharge event with a drive cycle current profile. Trying to terminate the event when the discharge capacity is closer to zero as voltage fluctuates. how to do that. thanks for your time.

regards

Capacity based termination works for charging but it gives me error for discharge, not sure why,

code snippet,

import pybamm
model = pybamm.lithium_ion.SPM()
parameter_values = pybamm.ParameterValues(“OKane2022”)

model.variables[“d_capacity [A.h]”] = model.variables[‘Discharge capacity [A.h]’]

def instantaneous_capacity(variables):
return variables[“d_capacity [A.h]”] - 3.0

capacity_termination = pybamm.step.CustomTermination(
name=“instantaneous capacity [A.h]”, event_function=instantaneous_capacity
)
terminations = [capacity_termination, “3 V”]

exp_dc = pybamm.Experiment(
[
(
pybamm.step.current(5.0, termination=terminations),
)
]
)

sim = pybamm.Simulation(model, parameter_values=parameter_values, experiment=exp_dc)
sol = sim.solve()

error message
--------------------------------------------------------------------------- SolverError Traceback (most recent call last) Cell In[352], line 3 1 sim = pybamm.Simulation(model, parameter_values=parameter_values, experiment=exp_dc) 2 # ----> 3 sol = sim.solve() File ~/.pyenv/versions/3.12.2/envs/cellmodelling/lib/python3.12/site-packages/pybamm/simulation.py:807, in Simulation.solve(self, t_eval, solver, save_at_cycles, calc_esoh, starting_solution, initial_soc, callbacks, showprogress, inputs, **kwargs) 805 if all(isinstance(step, pybamm.EmptySolution) for step in steps): 806 if len(steps) == 1: → 807 raise pybamm.SolverError( 808 f"Step ‘{step_str}’ is infeasible " 809 "due to exceeded bounds at initial conditions. " 810 "If this step is part of a longer cycle, " 811 "round brackets should be used to indicate this, " 812 “e.g.:\n pybamm.Experiment([(\n” 813 “\tDischarge at C/5 for 10 hours or until 3.3 V,\n” 814 “\tCharge at 1 A until 4.1 V,\n” 815 “\tHold at 4.1 V until 10 mA\n” 816 “])” 817 ) 818 else: 819 this_cycle = self.experiment.cycles[cycle_num - 1] SolverError: Step ‘Step(5.0, duration=86400, termination=[<pybamm.experiment.step.step_termination.CustomTermination object at 0x30e5c4c80>, <pybamm.experiment.step.step_termination.VoltageTermination object at 0x30e5c6840>], direction=Discharge)’ is infeasible due to exceeded bounds at initial conditions. If this step is part of a longer cycle, round brackets should be used to indicate this, e.g.: pybamm.Experiment([( Discharge at C/5 for 10 hours or until 3.3 V, Charge at 1 A until 4.1 V, Hold at 4.1 V until 10 mA ])

Discharge capacity starts at 0 then increases as you discharge, so the custom termination event is negative at the initial condition which is why you are getting the error. “3 - capacity” should work instead

1 Like

hi @valentinsulzer ,

thanks a lot for the kind reply. it works now.

model.variables[“d_capacity [A.h]”] = model.variables[‘Discharge capacity [A.h]’]

def instantaneous_capacity(variables):
return -0.25 - variables[“d_capacity [A.h]”]
#

The CustomTermination class takes a name and function

capacity_termination = pybamm.step.CustomTermination(
name=“instantaneous capacity [A.h]”, event_function=instantaneous_capacity
)

terminations = [capacity_termination, “3.0 V”]