Skip to content

Commit d844228

Browse files
committed
#759 fix tests, add some doctrings and fix flake8
1 parent 50ca0fc commit d844228

File tree

7 files changed

+62
-51
lines changed

7 files changed

+62
-51
lines changed

pybamm/discretisations/discretisation.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ def process_model(self, model, inplace=True, check_model=True):
178178
for event in model.events:
179179
pybamm.logger.debug("Discretise event '{}'".format(event.name))
180180
processed_event = pybamm.Event(
181-
event.name,
182-
self.process_symbol(event.expression),
183-
event.event_type
181+
event.name,
182+
self.process_symbol(event.expression),
183+
event.event_type
184184
)
185185
processed_events.append(processed_event)
186186
model_disc.events = processed_events

pybamm/models/event.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
import pybamm
2-
31
from enum import Enum
42

53

64
class EventType(Enum):
7-
"""Defines the type of event, see Event"""
5+
"""
6+
Defines the type of event, see Event
7+
8+
TERMINATION indicates an event that will terminate the solver, the expression should
9+
return 0 when the event is triggered
10+
11+
DISCONTINUITY indicates an expected discontinuity in the solution, the expression
12+
should return the time that the discontinuity occurs. The solver will integrate up
13+
to the discontinuity and then restart just after the discontinuity.
14+
15+
"""
816
TERMINATION = 0
917
DISCONTINUITY = 1
1018

@@ -20,7 +28,7 @@ class Event:
2028
name: str
2129
A string giving the name of the event
2230
event_type: EventType
23-
An enum defining the type of event, see EventType
31+
An enum defining the type of event
2432
expression: pybamm.Symbol
2533
An expression that defines when the event occurs
2634

pybamm/models/full_battery_models/lithium_ion/basic_dfn.py

+16-17
Original file line numberDiff line numberDiff line change
@@ -180,20 +180,16 @@ def __init__(self, name="Doyle-Fuller-Newman model"):
180180
self.initial_conditions[c_s_n] = param.c_n_init
181181
self.initial_conditions[c_s_p] = param.c_p_init
182182
# Events specify points at which a solution should terminate
183-
self.events.update(
184-
{
185-
"Minimum negative particle surface concentration": (
186-
pybamm.min(c_s_surf_n) - 0.01
187-
),
188-
"Maximum negative particle surface concentration": (1 - 0.01)
189-
- pybamm.max(c_s_surf_n),
190-
"Minimum positive particle surface concentration": (
191-
pybamm.min(c_s_surf_p) - 0.01
192-
),
193-
"Maximum positive particle surface concentration": (1 - 0.01)
194-
- pybamm.max(c_s_surf_p),
195-
}
196-
)
183+
self.events += [
184+
pybamm.Event("Minimum negative particle surface concentration",
185+
pybamm.min(c_s_surf_n) - 0.01),
186+
pybamm.Event("Maximum negative particle surface concentration",
187+
(1 - 0.01) - pybamm.max(c_s_surf_n)),
188+
pybamm.Event("Minimum positive particle surface concentration",
189+
pybamm.min(c_s_surf_p) - 0.01),
190+
pybamm.Event("Maximum positive particle surface concentration",
191+
(1 - 0.01) - pybamm.max(c_s_surf_p)),
192+
]
197193
######################
198194
# Current in the solid
199195
######################
@@ -245,7 +241,8 @@ def __init__(self, name="Doyle-Fuller-Newman model"):
245241
"right": (pybamm.Scalar(0), "Neumann"),
246242
}
247243
self.initial_conditions[c_e] = param.c_e_init
248-
self.events["Zero electrolyte concentration cut-off"] = pybamm.min(c_e) - 0.002
244+
self.events.append(pybamm.Event("Zero electrolyte concentration cut-off",
245+
pybamm.min(c_e) - 0.002))
249246

250247
######################
251248
# (Some) variables
@@ -263,8 +260,10 @@ def __init__(self, name="Doyle-Fuller-Newman model"):
263260
"Positive electrode potential": phi_s_p,
264261
"Terminal voltage": voltage,
265262
}
266-
self.events["Minimum voltage"] = voltage - param.voltage_low_cut
267-
self.events["Maximum voltage"] = voltage - param.voltage_high_cut
263+
self.events += [
264+
pybamm.Event("Minimum voltage", voltage - param.voltage_low_cut),
265+
pybamm.Event("Maximum voltage", voltage - param.voltage_high_cut),
266+
]
268267

269268
@property
270269
def default_geometry(self):

pybamm/models/full_battery_models/lithium_ion/basic_spm.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,16 @@ def __init__(self, name="Single Particle Model"):
9797
c_s_surf_n = pybamm.surf(c_s_n)
9898
c_s_surf_p = pybamm.surf(c_s_p)
9999
# Events specify points at which a solution should terminate
100-
self.events.update(
101-
{
102-
"Minimum negative particle surface concentration": (
103-
pybamm.min(c_s_surf_n) - 0.01
104-
),
105-
"Maximum negative particle surface concentration": (1 - 0.01)
106-
- pybamm.max(c_s_surf_n),
107-
"Minimum positive particle surface concentration": (
108-
pybamm.min(c_s_surf_p) - 0.01
109-
),
110-
"Maximum positive particle surface concentration": (1 - 0.01)
111-
- pybamm.max(c_s_surf_p),
112-
}
113-
)
100+
self.events += [
101+
pybamm.Event("Minimum negative particle surface concentration",
102+
pybamm.min(c_s_surf_n) - 0.01),
103+
pybamm.Event("Maximum negative particle surface concentration",
104+
(1 - 0.01) - pybamm.max(c_s_surf_n)),
105+
pybamm.Event("Minimum positive particle surface concentration",
106+
pybamm.min(c_s_surf_p) - 0.01),
107+
pybamm.Event("Maximum positive particle surface concentration",
108+
(1 - 0.01) - pybamm.max(c_s_surf_p)),
109+
]
114110

115111
# Note that the SPM does not have any algebraic equations, so the `algebraic`
116112
# dictionary remains empty
@@ -164,8 +160,10 @@ def __init__(self, name="Single Particle Model"):
164160
),
165161
"Terminal voltage": V,
166162
}
167-
self.events["Minimum voltage"] = V - param.voltage_low_cut
168-
self.events["Maximum voltage"] = V - param.voltage_high_cut
163+
self.events += [
164+
pybamm.Event("Minimum voltage", V - param.voltage_low_cut),
165+
pybamm.Event("Maximum voltage", V - param.voltage_high_cut),
166+
]
169167

170168
@property
171169
def default_geometry(self):

pybamm/solvers/base_solver.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,8 @@ def solve(self, model, t_eval, external_variables=None, inputs=None):
426426

427427
# Calculate discontinuities
428428
discontinuities = [
429-
event.expression.evaluate(u=inputs) for event in model.discontinuity_events_eval
429+
event.expression.evaluate(u=inputs)
430+
for event in model.discontinuity_events_eval
430431
]
431432

432433
# make sure they are increasing in time
@@ -436,31 +437,33 @@ def solve(self, model, t_eval, external_variables=None, inputs=None):
436437
)
437438
# remove any identical discontinuities
438439
discontinuities = [
439-
v for i, v in enumerate(discontinuities)
440-
if i==len(discontinuities)-1 or discontinuities[i] < discontinuities[i+1]
441-
]
440+
v for i, v in enumerate(discontinuities)
441+
if i == len(discontinuities) - 1
442+
or discontinuities[i] < discontinuities[i + 1]
443+
]
442444

443445
# insert time points around discontinuities in t_eval
444446
# keep track of sub sections to integrate by storing start and end indices
445447
start_indices = [0]
446448
end_indices = []
447449
for dtime in discontinuities:
448450
dindex = np.searchsorted(t_eval, dtime, side='left')
449-
end_indices.append(dindex+1)
450-
start_indices.append(dindex+1)
451+
end_indices.append(dindex + 1)
452+
start_indices.append(dindex + 1)
451453
if t_eval[dindex] == dtime:
452454
t_eval[dindex] += sys.float_info.epsilon
453455
t_eval = np.insert(t_eval, dindex, dtime - sys.float_info.epsilon)
454456
else:
455457
t_eval = np.insert(t_eval, dindex,
456-
[dtime - sys.float_info.epsilon, dtime + sys.float_info.epsilon])
458+
[dtime - sys.float_info.epsilon,
459+
dtime + sys.float_info.epsilon])
457460
end_indices.append(len(t_eval))
458461

459462
old_y0 = model.y0
460463
solution = None
461464
for start_index, end_index in zip(start_indices, end_indices):
462465
pybamm.logger.info("Calling solver for {} < t < {}"
463-
.format(t_eval[start_index], t_eval[end_index-1]))
466+
.format(t_eval[start_index], t_eval[end_index - 1]))
464467
timer.reset()
465468
if solution is None:
466469
solution = self._integrate(
@@ -479,14 +482,15 @@ def solve(self, model, t_eval, external_variables=None, inputs=None):
479482
# setup for next integration subsection
480483
y0_guess = solution.y[:, -1]
481484
if model.algebraic:
482-
model.y0 = self.calculate_consistent_state(model, t_eval[end_index], y0_guess)
485+
model.y0 = self.calculate_consistent_state(
486+
model, t_eval[end_index], y0_guess)
483487
else:
484488
model.y0 = y0_guess
485489

486490
last_state = solution.y[:, -1]
487491
if len(model.algebraic) > 0:
488492
model.y0 = self.calculate_consistent_state(
489-
model, t_eval[end_index], last_state)
493+
model, t_eval[end_index], last_state)
490494
else:
491495
model.y0 = last_state
492496

pybamm/solvers/casadi_solver.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ def _integrate(self, model, t_eval, inputs=None):
104104
elif self.mode == "safe":
105105
# Step-and-check
106106
init_event_signs = np.sign(
107-
np.concatenate([event(0, model.y0) for event in model.terminate_events_eval])
107+
np.concatenate([event(0, model.y0)
108+
for event in model.terminate_events_eval])
108109
)
109110
pybamm.logger.info(
110111
"Start solving {} with {} in 'safe' mode".format(model.name, self.name)

tests/unit/test_solvers/test_scikits_solvers.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import unittest
77
import warnings
88
from tests import get_mesh_for_testing, get_discretisation_for_testing
9+
import sys
910

1011

1112
@unittest.skipIf(not pybamm.have_scikits_odes(), "scikits.odes not installed")

0 commit comments

Comments
 (0)