Skip to content

Commit

Permalink
Merge pull request #971 from pybamm-team/issue-963-misc-bugs
Browse files Browse the repository at this point in the history
Issue 963 misc bugs
  • Loading branch information
valentinsulzer authored Apr 29, 2020
2 parents 6b62171 + 1ace4ed commit 0805104
Show file tree
Hide file tree
Showing 28 changed files with 392 additions and 203 deletions.
15 changes: 8 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@

## Bug fixes

- Fixed `Interpolant` ids to allow processing ([#962](https://github.com/pybamm-team/PyBaMM/pull/962)
- Added extra checks when creating a model, for clearer errors ([#971](https://github.com/pybamm-team/PyBaMM/pull/971))
- 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)
- Reformatted thermal submodels ([#938](https://github.com/pybamm-team/PyBaMM/pull/938)
- 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))
- Reformatted thermal submodels ([#938](https://github.com/pybamm-team/PyBaMM/pull/938))
- Reformatted electrolyte submodels ([#927](https://github.com/pybamm-team/PyBaMM/pull/927))
- Reformatted convection submodels ([#635](https://github.com/pybamm-team/PyBaMM/pull/635))

## Breaking changes

- Removed some inputs like `T_inf`, `R_g` and activation energies to some of the standard function parameters. This is because each of those inputs is specific to a particular function (e.g. the reference temperature at which the function was measured). To change a property such as the activation energy, users should create a new function, specifying the relevant property as a `Parameter` or `InputParameter` ([#942](https://github.com/pybamm-team/PyBaMM/pull/942))
- The thermal option 'xyz-lumped' has been removed. The option 'thermal current collector' has also been removed ([#938](https://github.com/pybamm-team/PyBaMM/pull/938)
- The 'C-rate' parameter has been deprecated. Use 'Current function [A]' instead. The cell capacity can be accessed as 'Cell capacity [A.h]', and used to calculate current from C-rate ([#952](https://github.com/pybamm-team/PyBaMM/pull/952)
- The thermal option 'xyz-lumped' has been removed. The option 'thermal current collector' has also been removed ([#938](https://github.com/pybamm-team/PyBaMM/pull/938))
- The 'C-rate' parameter has been deprecated. Use 'Current function [A]' instead. The cell capacity can be accessed as 'Cell capacity [A.h]', and used to calculate current from C-rate ([#952](https://github.com/pybamm-team/PyBaMM/pull/952))

# [v0.2.1](https://github.com/pybamm-team/PyBaMM/tree/v0.2.1) - 2020-03-31

Expand Down
6 changes: 6 additions & 0 deletions pybamm/discretisations/discretisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ def process_model(self, model, inplace=True, check_model=True):
`model.variables = {}`)
"""
if model.is_discretised is True:
raise pybamm.ModelError(
"Cannot re-discretise a model. "
"Set 'inplace=False' when first discretising a model to then be able "
"to discretise it more times (e.g. for convergence studies)."
)

pybamm.logger.info("Start discretising {}".format(model.name))

Expand Down
4 changes: 4 additions & 0 deletions pybamm/expression_tree/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ def evaluate(self, t=None, y=None, y_dot=None, inputs=None, known_evals=None):
]
return self._function_evaluate(evaluated_children)

def evaluates_on_edges(self):
""" See :meth:`pybamm.Symbol.evaluates_on_edges()`. """
return any(child.evaluates_on_edges() for child in self.children)

def _evaluate_for_shape(self):
"""
Default behaviour: has same shape as all child
Expand Down
19 changes: 13 additions & 6 deletions pybamm/expression_tree/operations/simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ class Simplification(object):
def __init__(self, simplified_symbols=None):
self._simplified_symbols = simplified_symbols or {}

def simplify(self, symbol):
def simplify(self, symbol, clear_domains=True):
"""
This function recurses down the tree, applying any simplifications defined in
classes derived from pybamm.Symbol. E.g. any expression multiplied by a
Expand All @@ -577,7 +577,9 @@ def simplify(self, symbol):
Parameters
----------
symbol : :class:`pybamm.Symbol`
The symbol to simplify
The symbol to simplify
clear_domains : bool
Whether to remove a symbol's domain when simplifying. Default is True.
Returns
-------
Expand All @@ -588,15 +590,16 @@ def simplify(self, symbol):
try:
return self._simplified_symbols[symbol.id]
except KeyError:
simplified_symbol = self._simplify(symbol)
simplified_symbol = self._simplify(symbol, clear_domains)

self._simplified_symbols[symbol.id] = simplified_symbol

return simplified_symbol

def _simplify(self, symbol):
def _simplify(self, symbol, clear_domains=True):
""" See :meth:`Simplification.simplify()`. """
symbol.clear_domains()
if clear_domains:
symbol.clear_domains()

if isinstance(symbol, pybamm.BinaryOperator):
left, right = symbol.children
Expand All @@ -607,7 +610,11 @@ def _simplify(self, symbol):
new_symbol = symbol._binary_simplify(new_left, new_right)

elif isinstance(symbol, pybamm.UnaryOperator):
new_child = self.simplify(symbol.child)
# Reassign domain for gradient and divergence
if isinstance(symbol, (pybamm.Gradient, pybamm.Divergence)):
new_child = self.simplify(symbol.child, clear_domains=False)
else:
new_child = self.simplify(symbol.child)
# _unary_simplify defined in derived classes for specific rules
new_symbol = symbol._unary_simplify(new_child)

Expand Down
8 changes: 7 additions & 1 deletion pybamm/expression_tree/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,16 @@ def _diff(self, variable):

def jac(self, variable, known_jacs=None, clear_domain=True):
"""
Differentiate a symbol with respect to a (slice of) a State Vector.
Differentiate a symbol with respect to a (slice of) a StateVector
or StateVectorDot.
See :class:`pybamm.Jacobian`.
"""
jac = pybamm.Jacobian(known_jacs, clear_domain=clear_domain)
if not isinstance(variable, (pybamm.StateVector, pybamm.StateVectorDot)):
raise TypeError(
"Jacobian can only be taken with respect to a 'StateVector' "
"or 'StateVectorDot', but {} is a {}".format(variable, type(variable))
)
return jac.jac(self, variable)

def _jac(self, variable):
Expand Down
40 changes: 36 additions & 4 deletions pybamm/expression_tree/unary_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,16 @@ class Gradient(SpatialOperator):
"""

def __init__(self, child):
if child.domain == []:
raise pybamm.DomainError(
"Cannot take gradient of '{}' since its domain is empty. ".format(child)
+ "Try broadcasting the object first, e.g.\n\n"
"\tpybamm.grad(pybamm.PrimaryBroadcast(symbol, 'domain'))"
)
if child.evaluates_on_edges() is True:
raise TypeError(
"Cannot take gradient of '{}' since it evaluates on edges".format(child)
)
super().__init__("grad", child)

def evaluates_on_edges(self):
Expand All @@ -324,6 +334,20 @@ class Divergence(SpatialOperator):
"""

def __init__(self, child):
if child.domain == []:
raise pybamm.DomainError(
"Cannot take divergence of '{}' since its domain is empty. ".format(
child
)
+ "Try broadcasting the object first, e.g.\n\n"
"\tpybamm.div(pybamm.PrimaryBroadcast(symbol, 'domain'))"
)
if child.evaluates_on_edges() is False:
raise TypeError(
"Cannot take divergence of '{}' since it does not ".format(child)
+ "evaluates on nodes. Usually, a gradient should be taken before the "
"divergence."
)
super().__init__("div", child)

def evaluates_on_edges(self):
Expand Down Expand Up @@ -814,8 +838,12 @@ def grad(expression):
:class:`Gradient`
the gradient of ``expression``
"""

return Gradient(expression)
# Gradient of a broadcast is zero
if isinstance(expression, pybamm.PrimaryBroadcast):
new_child = pybamm.PrimaryBroadcast(0, expression.child.domain)
return pybamm.PrimaryBroadcastToEdges(new_child, expression.domain)
else:
return Gradient(expression)


def div(expression):
Expand All @@ -833,8 +861,12 @@ def div(expression):
:class:`Divergence`
the divergence of ``expression``
"""

return Divergence(expression)
# Divergence of a broadcast is zero
if isinstance(expression, pybamm.PrimaryBroadcastToEdges):
new_child = pybamm.PrimaryBroadcast(0, expression.child.domain)
return pybamm.PrimaryBroadcast(new_child, expression.domain)
else:
return Divergence(expression)


def laplacian(expression):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Ambient temperature [K], 298.15,,
Number of electrodes connected in parallel to make a cell,1,,
Number of cells connected in series to make a battery,1,,
Lower voltage cut-off [V],2.5,,
Upper voltage cut-off [V],4.2,,
Upper voltage cut-off [V],4.25,,just above 4.2
,,,
# Initial conditions
Initial concentration in negative electrode [mol.m-3],48.8682,Peyman MPM, x0 (0.0017) * Csmax_n
Expand Down
2 changes: 2 additions & 0 deletions pybamm/meshes/meshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def combine_submeshes(self, *submeshnames):
submesh: :class:`self.submeshclass`
A new submesh with the class defined by self.submeshclass
"""
if submeshnames == ():
raise ValueError("Submesh domains being combined cannot be empty")
# Check that the final edge of each submesh is the same as the first edge of the
# next submesh
for i in range(len(submeshnames) - 1):
Expand Down
Loading

0 comments on commit 0805104

Please sign in to comment.