Skip to content

Commit

Permalink
Merge pull request #1449 from pybamm-team/issue-1429-simplifications
Browse files Browse the repository at this point in the history
Issue 1429 simplifications
  • Loading branch information
valentinsulzer authored Mar 29, 2021
2 parents 79236f1 + d6d36c0 commit c15b894
Show file tree
Hide file tree
Showing 73 changed files with 928 additions and 363 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
## Optimizations

- Improved how the CasADi solver's "safe" mode finds events ([#1450](https://github.com/pybamm-team/PyBaMM/pull/1450))
- Perform more automatic simplifications of the expression tree ([#1449](https://github.com/pybamm-team/PyBaMM/pull/1449))
- Reduce time taken to hash a sparse `Matrix` object ([#1449](https://github.com/pybamm-team/PyBaMM/pull/1449))

# [v0.4.0](https://github.com/pybamm-team/PyBaMM/tree/v0.4.0) - 2021-03-28

Expand Down
2 changes: 1 addition & 1 deletion docs/source/expression_tree/unary_operator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Unary Operators
.. autoclass:: pybamm.Laplacian
:members:

.. autoclass:: pybamm.Gradient_Squared
.. autoclass:: pybamm.GradientSquared
:members:

.. autoclass:: pybamm.Mass
Expand Down
4 changes: 2 additions & 2 deletions pybamm/discretisations/discretisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ def process_symbol(self, symbol):
discretised_symbol = self._process_symbol(symbol)
self._discretised_symbols[symbol.id] = discretised_symbol
discretised_symbol.test_shape()

# Assign mesh as an attribute to the processed variable
if symbol.domain != []:
discretised_symbol.mesh = self.mesh.combine_submeshes(*symbol.domain)
Expand Down Expand Up @@ -883,7 +884,7 @@ def _process_symbol(self, symbol):
elif isinstance(symbol, pybamm.Laplacian):
return child_spatial_method.laplacian(child, disc_child, self.bcs)

elif isinstance(symbol, pybamm.Gradient_Squared):
elif isinstance(symbol, pybamm.GradientSquared):
return child_spatial_method.gradient_squared(
child, disc_child, self.bcs
)
Expand Down Expand Up @@ -1030,7 +1031,6 @@ def _process_symbol(self, symbol):
elif isinstance(symbol, pybamm.Concatenation):
new_children = [self.process_symbol(child) for child in symbol.children]
new_symbol = spatial_method.concatenation(new_children)

return new_symbol

elif isinstance(symbol, pybamm.InputParameter):
Expand Down
14 changes: 10 additions & 4 deletions pybamm/expression_tree/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ def __init__(
auxiliary_domains=None,
entries_string=None,
):
# if
if isinstance(entries, list):
entries = np.array(entries)
if entries.ndim == 1:
entries = entries[:, np.newaxis]
if name is None:
name = "Array of shape {!s}".format(entries.shape)
self._entries = entries
self._entries = entries.astype(float)
# Use known entries string to avoid re-hashing, where possible
self.entries_string = entries_string
super().__init__(name, domain=domain, auxiliary_domains=auxiliary_domains)
Expand Down Expand Up @@ -75,14 +76,19 @@ def entries_string(self, value):
else:
entries = self._entries
if issparse(entries):
self._entries_string = str(entries.__dict__)
dct = entries.__dict__
self._entries_string = ["shape", str(dct["_shape"])]
for key in ["data", "indices", "indptr"]:
self._entries_string += [key, dct[key].tobytes()]
self._entries_string = tuple(self._entries_string)
# self._entries_string = str(entries.__dict__)
else:
self._entries_string = entries.tobytes()
self._entries_string = (entries.tobytes(),)

def set_id(self):
""" See :meth:`pybamm.Symbol.set_id()`. """
self._id = hash(
(self.__class__, self.name, self.entries_string) + tuple(self.domain)
(self.__class__, self.name) + self.entries_string + tuple(self.domain)
)

def _jac(self, variable):
Expand Down
Loading

0 comments on commit c15b894

Please sign in to comment.