|
8 | 8 | import warnings
|
9 | 9 | from tests import get_mesh_for_testing, get_discretisation_for_testing
|
10 | 10 |
|
| 11 | +# TODO: remove this |
| 12 | +import matplotlib.pylab as plt |
| 13 | + |
11 | 14 |
|
12 | 15 | @unittest.skipIf(not pybamm.have_scikits_odes(), "scikits.odes not installed")
|
13 | 16 | class TestScikitsSolvers(unittest.TestCase):
|
@@ -548,6 +551,42 @@ def test_model_solver_dae_events_python(self):
|
548 | 551 | np.testing.assert_allclose(solution.y[0], np.exp(0.1 * solution.t))
|
549 | 552 | np.testing.assert_allclose(solution.y[-1], 2 * np.exp(0.1 * solution.t))
|
550 | 553 |
|
| 554 | + def test_model_solver_dae_nonsmooth_python(self): |
| 555 | + model = pybamm.BaseModel() |
| 556 | + model.convert_to_format = "python" |
| 557 | + whole_cell = ["negative electrode", "separator", "positive electrode"] |
| 558 | + var1 = pybamm.Variable("var1", domain=whole_cell) |
| 559 | + var2 = pybamm.Variable("var2", domain=whole_cell) |
| 560 | + |
| 561 | + def nonsmooth_rate(t): |
| 562 | + return 0.1 * int(t < 2.5) + 0.1 |
| 563 | + rate = pybamm.Function(nonsmooth_rate, pybamm.t) |
| 564 | + model.rhs = {var1: rate * var1} |
| 565 | + model.algebraic = {var2: 2 * var1 - var2} |
| 566 | + model.initial_conditions = {var1: 1, var2: 2} |
| 567 | + model.events = [ |
| 568 | + pybamm.Event("var1 = 1.5", pybamm.min(var1 - 1.5)), |
| 569 | + pybamm.Event("var2 = 2.5", pybamm.min(var2 - 2.5)), |
| 570 | + pybamm.Event("nonsmooth rate", |
| 571 | + pybamm.Scalar(2.5), |
| 572 | + pybamm.EventType.DISCONTINUITY |
| 573 | + ) |
| 574 | + ] |
| 575 | + disc = get_discretisation_for_testing() |
| 576 | + disc.process_model(model) |
| 577 | + |
| 578 | + # Solve |
| 579 | + solver = pybamm.ScikitsDaeSolver(rtol=1e-8, atol=1e-8) |
| 580 | + t_eval = np.linspace(0, 5, 100) |
| 581 | + solution = solver.solve(model, t_eval) |
| 582 | + plt.plot(solution.y[0]) |
| 583 | + plt.plot(solution.y[1]) |
| 584 | + plt.show() |
| 585 | + #np.testing.assert_array_less(solution.y[0], 1.5) |
| 586 | + #np.testing.assert_array_less(solution.y[-1], 2.5) |
| 587 | + #np.testing.assert_allclose(solution.y[0], np.exp(0.1 * solution.t)) |
| 588 | + #np.testing.assert_allclose(solution.y[-1], 2 * np.exp(0.1 * solution.t)) |
| 589 | + |
551 | 590 | def test_model_solver_dae_with_jacobian_python(self):
|
552 | 591 | model = pybamm.BaseModel()
|
553 | 592 | model.convert_to_format = "python"
|
|
0 commit comments