Skip to content

Commit

Permalink
Merge pull request #301 from pybop-team/300-update-default-casadi-sol…
Browse files Browse the repository at this point in the history
…ver-mode-to-fast-with-events

Adds `"fast with events"` as default echem solver
  • Loading branch information
BradyPlanden authored Apr 26, 2024
2 parents 98d4003 + 02bd80a commit 1431f67
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Features


- [#301](https://github.com/pybop-team/PyBOP/pull/301) - Updates default echem solver to "fast with events" mode.
- [#251](https://github.com/pybop-team/PyBOP/pull/251) - Increment PyBaMM > v23.5, remove redundant tests within integration tests, increment citation version, fix examples with incorrect model definitions.
- [#285](https://github.com/pybop-team/PyBOP/pull/285) - Drop support for Python 3.8.
- [#275](https://github.com/pybop-team/PyBOP/pull/275) - Adds Maximum a Posteriori (MAP) cost function with corresponding tests.
Expand Down
2 changes: 1 addition & 1 deletion pybop/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def simulate(self, inputs, t_eval) -> np.ndarray[np.float64]:
)
except Exception as e:
print(f"Error: {e}")
return [np.inf]
return {signal: [np.inf] for signal in self.signal}
else:
return {signal: [np.inf] for signal in self.signal}

Expand Down
8 changes: 6 additions & 2 deletions pybop/models/lithium_ion/base_echem.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ def __init__(
self.spatial_methods = (
spatial_methods or self.pybamm_model.default_spatial_methods
)
self.solver = solver or self.pybamm_model.default_solver
self.solver.max_step_decrease_count = 1
if solver is None:
self.solver = self.pybamm_model.default_solver
self.solver.mode = "fast with events"
self.solver.max_step_decrease_count = 1
else:
self.solver = solver

# Internal attributes for the built model are initialized but not set
self._model_with_set_params = None
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/test_model_experiment_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def test_changing_experiment(self, parameter):
)

# The datasets are not corrupted so the costs should be zero
np.testing.assert_almost_equal(cost_1, 0)
np.testing.assert_almost_equal(cost_2, 0)
np.testing.assert_allclose(cost_1, 0, atol=1e-5)
np.testing.assert_allclose(cost_2, 0, atol=1e-5)

@pytest.mark.integration
def test_changing_model(self, parameter):
Expand All @@ -81,8 +81,8 @@ def test_changing_model(self, parameter):
)

# The datasets are not corrupted so the costs should be zero
np.testing.assert_almost_equal(cost_1, 0)
np.testing.assert_almost_equal(cost_2, 0)
np.testing.assert_allclose(cost_1, 0, atol=1e-5)
np.testing.assert_allclose(cost_2, 0, atol=1e-5)

def final_cost(self, solution, model, parameters, init_soc):
# Compute the cost corresponding to a particular solution
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ def test_simulate_without_build_model(self, model):
):
model.simulateS1(None, None)

@pytest.mark.unit
def test_non_default_solver(self):
solver = pybamm.CasadiSolver(
mode="fast",
atol=1e-6,
rtol=1e-6,
)
model = pybop.lithium_ion.SPM(solver=solver)
assert model.solver.mode == "fast"
assert model.solver.atol == 1e-6
assert model.solver.rtol == 1e-6

@pytest.mark.unit
def test_predict_without_pybamm(self, model):
model._unprocessed_model = None
Expand Down Expand Up @@ -269,5 +281,6 @@ def test_non_converged_solution(self):
res = problem.evaluate([-0.2, -0.2])
_, res_grad = problem.evaluateS1([-0.2, -0.2])

assert np.isinf(res).any()
for key in problem.signal:
assert np.isinf(res.get(key, [])).any()
assert np.isinf(res_grad).any()

0 comments on commit 1431f67

Please sign in to comment.