Skip to content

Commit

Permalink
#1082 cccv now working well for dfn and spm
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Mar 16, 2021
1 parent 7efc9ad commit 68bb06f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
8 changes: 4 additions & 4 deletions examples/scripts/DFN.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@
disc.process_model(model)

# solve model
t_eval = np.linspace(0, 4000, 100)
# solver = pybamm.CasadiSolver(mode="safe", atol=1e-3, rtol=1e-3)
# solution1 = solver.solve(model, t_eval)
t_eval = np.linspace(0, 5000, 100)
solver = pybamm.CasadiSolver(mode="safe", atol=1e-3, rtol=1e-3)
solution1 = solver.solve(model, t_eval)
# solver = pybamm.CasadiSolver(mode="fast", atol=1e-3, rtol=1e-3)
# solution1 = solver.solve(model, t_eval)
solver = pybamm.CasadiSolver(mode="fast with events", atol=1e-3, rtol=1e-3)
solution2 = solver.solve(model, t_eval)

# plot
plot = pybamm.QuickPlot(
solution2,
[solution1, solution2],
[
# "Negative particle concentration [mol.m-3]",
"Electrolyte concentration [mol.m-3]",
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/experimental_protocols/cccv.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
]
* 3
)
model = pybamm.lithium_ion.DFN()
model = pybamm.lithium_ion.SPM()
var = pybamm.standard_spatial_vars
var_pts = {var.x_n: 30, var.x_s: 30, var.x_p: 30, var.r_n: 10, var.r_p: 10}

Expand Down
32 changes: 17 additions & 15 deletions pybamm/models/full_battery_models/base_battery_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,21 +916,23 @@ def set_voltage_variables(self):

# Cut-off open-circuit voltage (for event switch with casadi 'fast with events'
# mode)
# tol = 0
# self.events.append(
# pybamm.Event(
# "Minimum voltage",
# V - (self.param.voltage_low_cut - tol),
# pybamm.EventType.SWITCH,
# )
# )
# self.events.append(
# pybamm.Event(
# "Maximum voltage",
# V - (self.param.voltage_high_cut + tol),
# pybamm.EventType.SWITCH,
# )
# )
# A tolerance of 1 is sufficiently small since the dimensionless voltage is
# scaled with the thermal voltage (0.025V) and hence has a range of around 60
tol = 1
self.events.append(
pybamm.Event(
"Minimum voltage switch",
V - (self.param.voltage_low_cut - tol),
pybamm.EventType.SWITCH,
)
)
self.events.append(
pybamm.Event(
"Maximum voltage switch",
V - (self.param.voltage_high_cut + tol),
pybamm.EventType.SWITCH,
)
)

# Power
I_dim = self.variables["Current [A]"]
Expand Down
8 changes: 2 additions & 6 deletions pybamm/solvers/base_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,7 @@ def report(string):
discontinuity_events_eval.append(event)
else:
event_eval = process(event.expression, "event", use_jacobian=False)[1]
if event.event_type == pybamm.EventType.SWITCH or (
event.event_type == pybamm.EventType.TERMINATION
and "[experiment]" in event.name
and "voltage" in event.name.lower()
):
if event.event_type == pybamm.EventType.SWITCH:
# Save some events to casadi_terminate_events for the 'fast with
# events' mode of the casadi solver
# see #1082
Expand All @@ -393,7 +389,7 @@ def report(string):
0
]
casadi_terminate_events.append(event_casadi)
if event.event_type == pybamm.EventType.TERMINATION:
elif event.event_type == pybamm.EventType.TERMINATION:
terminate_events_eval.append(event_eval)
elif event.event_type == pybamm.EventType.INTERPOLANT_EXTRAPOLATION:
interpolant_extrapolation_events_eval.append(event_eval)
Expand Down
2 changes: 1 addition & 1 deletion pybamm/solvers/casadi_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def find_t_event(sol, typ):
return None, None

# get the index of the events that have been crossed
event_ind = np.where(crossed_events == -1)[0]
event_ind = np.where(crossed_events != 1)[0]
active_events = [model.terminate_events_eval[i] for i in event_ind]

# loop over events to compute the time at which they were triggered
Expand Down

0 comments on commit 68bb06f

Please sign in to comment.