Skip to content

Commit

Permalink
Merge pull request #956 from pybamm-team/issue-853-casadi-safe
Browse files Browse the repository at this point in the history
Issue 853 casadi safe
  • Loading branch information
rtimms authored Apr 29, 2020
2 parents cde479d + 33aff3a commit 6852e0e
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

## Optimizations

- Changed the behaviour of "safe" mode in `CasadiSolver` ([#956](https://github.com/pybamm-team/PyBaMM/pull/956))
- Sped up model building ([#927](https://github.com/pybamm-team/PyBaMM/pull/927))
- Changed default solver for lead-acid to `CasadiSolver` ([#927](https://github.com/pybamm-team/PyBaMM/pull/927))

## Bug fixes

- Fixed `Interpolant` ids to allow processing ([#962](https://github.com/pybamm-team/PyBaMM/pull/962)
- Fixed a bug in the initial conditions of the potential pair model ([#954](https://github.com/pybamm-team/PyBaMM/pull/954))
- Changed simulation attributes to assign copies rather than the objects themselves ([#952](https://github.com/pybamm-team/PyBaMM/pull/952)
- Added default values to base model so that it works with the `Simulation` class ([#952](https://github.com/pybamm-team/PyBaMM/pull/952)
- Fixed solver to recompute initial conditions when inputs are changed ([#951](https://github.com/pybamm-team/PyBaMM/pull/951)
Expand Down
23 changes: 11 additions & 12 deletions examples/notebooks/change-input-current.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now solve the model in the ususal way, with a 16A current"
"We can now solve the model in the usual way, with a 1.6A current"
]
},
{
Expand All @@ -66,12 +66,12 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8247adc1a3fd42dba31101bc6b501ed9",
"model_id": "807f6dec221b4226aea64326d51c751b",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=0.0, description='t', max=15.050167224080266, step=0.15050167224080266"
"interactive(children=(FloatSlider(value=0.0, description='t', max=600.0, step=6.0), Output()), _dom_classes=('"
]
},
"metadata": {},
Expand All @@ -92,9 +92,8 @@
"\n",
"# Solve the model at the given time points\n",
"solver = pybamm.CasadiSolver()\n",
"n = 300\n",
"t_eval = np.linspace(0, 500, n)\n",
"solution = solver.solve(model, t_eval, inputs={\"Current function [A]\": 16})\n",
"t_eval = np.linspace(0, 600, 300)\n",
"solution = solver.solve(model, t_eval, inputs={\"Current function [A]\": 1.6})\n",
"\n",
"# plot\n",
"quick_plot = pybamm.QuickPlot(solution)\n",
Expand All @@ -116,12 +115,12 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "511a6922f3f34350a543367723f09572",
"model_id": "eaab57697cef4852b4fbab5ee1f7efd7",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=0.0, description='t', max=500.0, step=5.0), Output()), _dom_classes=('…"
"interactive(children=(FloatSlider(value=0.0, description='t', max=600.0, step=6.0), Output()), _dom_classes=('…"
]
},
"metadata": {},
Expand Down Expand Up @@ -154,7 +153,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "def542dad39b4ddebcd9a555cf684580",
"model_id": "a72be467b6f84733927cb2e7a5c4b3f9",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -273,7 +272,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7f4df64df94d410fb9aff92003538b14",
"model_id": "7dff98a2d9a44f87b71abeb0a94e16c9",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -330,9 +329,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
}
Binary file modified examples/notebooks/models/spm1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/notebooks/models/spm2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions pybamm/models/submodels/current_collector/potential_pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,12 @@ def set_algebraic(self, variables):
def set_initial_conditions(self, variables):

applied_current = self.param.current_with_time
cc_area = self._get_effective_current_collector_area()
phi_s_cn = variables["Negative current collector potential"]
i_boundary_cc = variables["Current collector current density"]

self.initial_conditions = {
phi_s_cn: pybamm.Scalar(0),
i_boundary_cc: applied_current / cc_area,
i_boundary_cc: applied_current,
}


Expand Down
12 changes: 3 additions & 9 deletions pybamm/solvers/base_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ def solve(self, model, t_eval=None, external_variables=None, inputs=None):
The model whose solution to calculate. Must have attributes rhs and
initial_conditions
t_eval : numeric type
The times at which to compute the solution
The times (in seconds) at which to compute the solution
external_variables : dict
A dictionary of external variables and their corresponding
values at the current time
Expand Down Expand Up @@ -491,10 +491,6 @@ def solve(self, model, t_eval=None, external_variables=None, inputs=None):
# Set up external variables and inputs
ext_and_inputs = self._set_up_ext_and_inputs(model, external_variables, inputs)

# Make sure t_eval is monotonic
if (np.diff(t_eval) < 0).any():
raise pybamm.SolverError("t_eval must increase monotonically")

# Set up
timer = pybamm.Timer()

Expand All @@ -511,7 +507,6 @@ def solve(self, model, t_eval=None, external_variables=None, inputs=None):

# Non-dimensionalise time
t_eval_dimensionless = t_eval / model.timescale_eval
# Solve

# Calculate discontinuities
discontinuities = [
Expand Down Expand Up @@ -646,7 +641,7 @@ def step(
The model whose solution to calculate. Must have attributes rhs and
initial_conditions
dt : numeric type
The timestep over which to step the solution
The timestep (in seconds) over which to step the solution
npts : int, optional
The number of points at which the solution will be returned during
the step dt. default is 2 (returns the solution at t0 and t0 + dt).
Expand Down Expand Up @@ -708,10 +703,9 @@ def step(

# Non-dimensionalise dt
dt_dimensionless = dt / model.timescale_eval

# Step
t_eval = np.linspace(t, t + dt_dimensionless, npts)
# Set inputs and external

pybamm.logger.info("Calling solver")
timer.reset()
solution = self._integrate(model, t_eval, ext_and_inputs)
Expand Down
Loading

0 comments on commit 6852e0e

Please sign in to comment.