Skip to content

Commit

Permalink
#684 flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Nov 1, 2019
1 parent f4bcce7 commit 551f625
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
16 changes: 14 additions & 2 deletions pybamm/quick_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,26 @@ def reset_axis(self):
# Get min and max y values
y_min = np.min(
[
ax_min(var(self.ts[i], **{spatial_var_name: spatial_var_value}, warn=False))
ax_min(
var(
self.ts[i],
**{spatial_var_name: spatial_var_value},
warn=False
)
)
for i, variable_list in enumerate(variable_lists)
for var in variable_list
]
)
y_max = np.max(
[
ax_max(var(self.ts[i], **{spatial_var_name: spatial_var_value}, warn=False))
ax_max(
var(
self.ts[i],
**{spatial_var_name: spatial_var_value},
warn=False
)
)
for i, variable_list in enumerate(variable_lists)
for var in variable_list
]
Expand Down
10 changes: 4 additions & 6 deletions pybamm/solvers/casadi_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CasadiSolver(pybamm.DaeSolver):
The tolerance for root-finding. Default is 1e-6.
max_step_decrease_counts : float, optional
The maximum number of times step size can be decreased before an error is
raised. Default is 10.
raised. Default is 5.
extra_options : keyword arguments, optional
Any extra keyword-arguments; these are passed directly to the CasADi integrator.
Please consult `CasADi documentation <https://tinyurl.com/y5rk76os>`_ for
Expand All @@ -50,7 +50,7 @@ def __init__(
atol=1e-6,
root_method="lm",
root_tol=1e-6,
max_step_decrease_count=10,
max_step_decrease_count=5,
**extra_options,
):
super().__init__(method, rtol, atol, root_method, root_tol)
Expand Down Expand Up @@ -80,7 +80,7 @@ def solve(self, model, t_eval):
initial_conditions
t_eval : numeric type
The times at which to compute the solution
Raises
------
:class:`pybamm.ValueError`
Expand Down Expand Up @@ -128,7 +128,7 @@ def solve(self, model, t_eval):
if solution is None:
t = 0
else:
t = solution.t
t = solution.t[-1]
raise pybamm.SolverError(
"""
Maximum number of decreased steps occurred at t={}. Try
Expand Down Expand Up @@ -160,8 +160,6 @@ def solve(self, model, t_eval):
else:
# append solution from the current step to solution
solution.append(current_step_sol)
if not hasattr(solution, "termination"):
solution.termination = "final time"

# Calculate more exact termination reason
solution.termination = self.get_termination_reason(solution, self.events)
Expand Down
43 changes: 34 additions & 9 deletions tests/unit/test_solvers/test_casadi_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,41 @@ def test_integrate_failure(self):

y0 = np.array([1])
t_eval = np.linspace(0, 3, 100)
solver = pybamm.CasadiSolver(
rtol=1e-8,
atol=1e-8,
method="idas",
disable_internal_warnings=True,
regularity_check=False,
)
solver = pybamm.CasadiSolver()
problem = {"x": y, "ode": sqrt_decay}
# Expect solver to fail when y goes negative
with self.assertRaises(pybamm.SolverError):
solver.integrate_casadi(problem, y0, t_eval)

# Set up as a model and solve
# Create model
model = pybamm.BaseModel()
domain = ["negative electrode", "separator", "positive electrode"]
var = pybamm.Variable("var", domain=domain)
model.rhs = {var: -pybamm.Function(np.sqrt, var)}
model.initial_conditions = {var: 1}
# add events so that safe mode is used (won't be triggered)
model.events = {"10": var - 10}
# No need to set parameters; can use base discretisation (no spatial operators)

# create discretisation
mesh = get_mesh_for_testing()
spatial_methods = {"macroscale": pybamm.FiniteVolume}
disc = pybamm.Discretisation(mesh, spatial_methods)
disc.process_model(model)
# Solve with failure at t=2
solver = pybamm.CasadiSolver(rtol=1e-8, atol=1e-8, method="idas")
t_eval = np.linspace(0, 20, 100)
with self.assertRaises(pybamm.SolverError):
solver.solve(model, t_eval)
# Solve with failure at t=0
model.initial_conditions = {var: 0}
disc.process_model(model)
solver = pybamm.CasadiSolver(rtol=1e-8, atol=1e-8, method="idas")
t_eval = np.linspace(0, 20, 100)
with self.assertRaises(pybamm.SolverError):
solver.solve(model, t_eval)

# Turn warnings back on
warnings.simplefilter("default")

Expand Down Expand Up @@ -85,8 +108,10 @@ def test_model_solver(self):
np.testing.assert_array_equal(solution.t, t_eval)
np.testing.assert_allclose(solution.y[0], np.exp(0.1 * solution.t))

# Fast mode
solver = pybamm.CasadiSolver(rtol=1e-8, atol=1e-8, method="idas", mode="fast")
# Safe mode (enforce events that won't be triggered)
model.events = {"an event": var + 1}
disc.process_model(model)
solver = pybamm.CasadiSolver(rtol=1e-8, atol=1e-8, method="idas")
t_eval = np.linspace(0, 1, 100)
solution = solver.solve(model, t_eval)
np.testing.assert_array_equal(solution.t, t_eval)
Expand Down

0 comments on commit 551f625

Please sign in to comment.