Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1826 add voltage termination #1832

Merged
merged 4 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pybamm/experiments/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,13 @@ def read_termination(self, termination):
"Capacity termination must be given in the form "
"'80%', '4Ah', or '4A.h'"
)
elif term.endswith("V"):
end_discharge_V = term.split("V")[0]
termination_dict["voltage"] = (float(end_discharge_V), "V")
else:
raise ValueError(
"Only capacity can be provided as a termination reason, "
"e.g. '80% capacity' or '4 Ah capacity'"
"Only capacity or voltage can be provided as a termination reason, "
"e.g. '80% capacity', '4 Ah capacity', or '2.5 V'"
)
return termination_dict

Expand Down
18 changes: 18 additions & 0 deletions pybamm/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,8 @@ def solve(
else:
esoh_sim = None

voltage_stop = self.experiment.termination.get("voltage")

idx = 0
num_cycles = len(self.experiment.cycle_lengths)
feasible = True # simulation will stop if experiment is infeasible
Expand Down Expand Up @@ -983,6 +985,22 @@ def solve(
)
break

# Check voltage stop
if voltage_stop is not None:
min_voltage = np.min(cycle_solution["Battery voltage [V]"].data)
if min_voltage > voltage_stop[0]:
pybamm.logger.notice(
f"Minimum voltage is now {min_voltage:.3f} V "
f"(will stop at {voltage_stop[0]:.3f} V)"
)
else:
pybamm.logger.notice(
"Stopping experiment since minimum voltage "
f"({min_voltage:.3f} V) "
f"is below stopping voltage ({voltage_stop[0]:.3f} V)."
)
break

if self.solution is not None and len(all_cycle_solutions) > 0:
self.solution.cycles = all_cycle_solutions
self.solution.set_summary_variables(all_summary_variables)
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_experiments/test_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ def test_termination(self):
["Discharge at 1 C for 20 seconds"], termination="80.7% capacity"
)
self.assertEqual(experiment.termination, {"capacity": (80.7, "%")})

experiment = pybamm.Experiment(
["Discharge at 1 C for 20 seconds"], termination="80.7 % capacity"
)
Expand All @@ -348,11 +349,24 @@ def test_termination(self):
["Discharge at 1 C for 20 seconds"], termination="4.1Ah capacity"
)
self.assertEqual(experiment.termination, {"capacity": (4.1, "Ah")})

experiment = pybamm.Experiment(
["Discharge at 1 C for 20 seconds"], termination="4.1 A.h capacity"
)
self.assertEqual(experiment.termination, {"capacity": (4.1, "Ah")})

experiment = pybamm.Experiment(
["Discharge at 1 C for 20 seconds"], termination="3V"
)
self.assertEqual(experiment.termination, {"voltage": (3, "V")})

experiment = pybamm.Experiment(
["Discharge at 1 C for 20 seconds"], termination=["3V", "4.1Ah capacity"]
)
self.assertEqual(
experiment.termination, {"voltage": (3, "V"), "capacity": (4.1, "Ah")}
)

with self.assertRaisesRegex(ValueError, "Only capacity"):
experiment = pybamm.Experiment(
["Discharge at 1 C for 20 seconds"], termination="bla bla capacity bla"
Expand Down
24 changes: 23 additions & 1 deletion tests/unit/test_experiments/test_simulation_with_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def test_run_experiment_breaks_early(self):
pybamm.set_logging_level("WARNING")
self.assertEqual(sim._solution, None)

def test_run_experiment_termination(self):
def test_run_experiment_termination_capacity(self):
# with percent
experiment = pybamm.Experiment(
[
Expand Down Expand Up @@ -247,6 +247,28 @@ def test_run_experiment_termination(self):
# all but the last value should be above the termination condition
np.testing.assert_array_less(5.04, C[:-1])

def test_run_experiment_termination_voltage(self):
# with percent
experiment = pybamm.Experiment(
[
("Discharge at 0.5C for 10 minutes", "Rest for 10 minutes"),
]
* 5,
termination="4V",
)
model = pybamm.lithium_ion.SPM()
param = pybamm.ParameterValues("Chen2020")
sim = pybamm.Simulation(model, experiment=experiment, parameter_values=param)
sol = sim.solve()
# Only two cycles should be completed, only 2nd cycle should go below 4V
np.testing.assert_array_less(
4, np.min(sol.cycles[0]["Terminal voltage [V]"].data)
)
np.testing.assert_array_less(
np.min(sol.cycles[1]["Terminal voltage [V]"].data), 4
)
self.assertEqual(len(sol.cycles), 2)

def test_save_at_cycles(self):
experiment = pybamm.Experiment(
[
Expand Down