# Custom termination event with discharge capacity

**URL:** <https://pybamm.discourse.group/t/custom-termination-event-with-discharge-capacity/135>\
**Category:** General\
**Created:** [31 January 2025 07:45 UTC](https://pybamm.discourse.group/t/custom-termination-event-with-discharge-capacity/135 "2025-01-31T07:45:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thiyaga](https://yyz2.discourse-cdn.com/free1/user_avatar/pybamm.discourse.group/thiyaga/32/37_2.png) [@Thiyaga](https://pybamm.discourse.group/u/Thiyaga)\
**Post date:** [31 January 2025 07:45 UTC](https://pybamm.discourse.group/t/custom-termination-event-with-discharge-capacity/135/1 "2025-01-31T07:45:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thiyaga](https://yyz2.discourse-cdn.com/free1/user_avatar/pybamm.discourse.group/thiyaga/32/37_2.png) [@Thiyaga](https://pybamm.discourse.group/u/Thiyaga)\
**Post date:** [1 February 2025 17:16 UTC](https://pybamm.discourse.group/t/custom-termination-event-with-discharge-capacity/135/2 "2025-02-01T17:16:18Z")

</div>

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](http://localhost:8888/lab/tree/Documents/Projects/pybamm-poc/pybamm-poc-cylindrical-cells/initial_exploration/DMA/Vammo/final_versions/Test_pybamm/~/.pyenv/versions/3.12.2/envs/cellmodelling/lib/python3.12/site-packages/pybamm/simulation.py#line=806), 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](http://localhost:8888/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](http://localhost:8888/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 ])

---

<div class="post-metadata">

**Author:** ![valentinsulzer](https://yyz2.discourse-cdn.com/free1/user_avatar/pybamm.discourse.group/valentinsulzer/32/9_2.png) [@valentinsulzer](https://pybamm.discourse.group/u/valentinsulzer)\
**Post date:** [3 February 2025 16:35 UTC](https://pybamm.discourse.group/t/custom-termination-event-with-discharge-capacity/135/3 "2025-02-03T16:35:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thiyaga](https://yyz2.discourse-cdn.com/free1/user_avatar/pybamm.discourse.group/thiyaga/32/37_2.png) [@Thiyaga](https://pybamm.discourse.group/u/Thiyaga)\
**Post date:** [4 February 2025 16:33 UTC](https://pybamm.discourse.group/t/custom-termination-event-with-discharge-capacity/135/4 "2025-02-04T16:33:11Z")

</div>

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”]

#
