Repository: pybamm-team/PyBaMM

Issue:
divide outer product
doing Division(Division(Outer, exp), exp) fails for some expressions

**To Reproduce**
see `test_simplify_divide_outer` in `test_simplify.py` [here](https://github.com/pybamm-team/PyBaMM/blob/c9d94496b3c62f23aec82165b8ff7d422151329b/tests/unit/test_expression_tree/test_simplify.py#L543) which implements the following:

```
    u = pybamm.Scalar(1)
    v = pybamm.StateVector(slice(0, 5), domain="current collector")
    outer = pybamm.Outer(v, u)

    exp1 = pybamm.Division(pybamm.Division(outer, u), u)
    self.assertIsInstance(exp1.simplify(), pybamm.Outer)

    exp2 = pybamm.Division(pybamm.Division(outer, 2 * u), u)
    self.assertIsInstance(exp2.simplify(), pybamm.Multiplication)

    exp3 = pybamm.Division(pybamm.Division(outer, u), 2 * u)
    self.assertIsInstance(exp3.simplify(), pybamm.Multiplication)

    exp4 = pybamm.Division(pybamm.Division(outer, 2 * u), 2 * u)
    exp4.simplify()
```
The first three expressions simplify as expected, but exp4 gives 

`pybamm.expression_tree.exceptions.DomainError: left child domain must be 'current collector', not'[]'`


Discussion:

Patch:
diff --git a/pybamm/expression_tree/binary_operators.py b/pybamm/expression_tree/binary_operators.py
index 8eeeded17..abffe9c4c 100644
--- a/pybamm/expression_tree/binary_operators.py
+++ b/pybamm/expression_tree/binary_operators.py
@@ -112,12 +112,15 @@ class BinaryOperator(pybamm.Symbol):
 
     def new_copy(self):
         """ See :meth:`pybamm.Symbol.new_copy()`. """
+
         # process children
         new_left = self.left.new_copy()
         new_right = self.right.new_copy()
+
         # make new symbol, ensure domain remains the same
         out = self.__class__(new_left, new_right)
         out.domain = self.domain
+
         return out
 
     def evaluate(self, t=None, y=None, known_evals=None):
diff --git a/pybamm/expression_tree/simplify.py b/pybamm/expression_tree/simplify.py
index 1d285bd71..45f5ec5ea 100644
--- a/pybamm/expression_tree/simplify.py
+++ b/pybamm/expression_tree/simplify.py
@@ -70,6 +70,9 @@ def simplify_addition_subtraction(myclass, left, right):
 
         outputs to lists `numerator` and `numerator_types`
 
+        Note that domains are all set to [] as we do not wish to consider domains once
+        simplifications are applied
+
         e.g.
 
         (1 + 2) + 3       -> [1, 2, 3]    and [None, Addition, Addition]
@@ -77,6 +80,9 @@ def simplify_addition_subtraction(myclass, left, right):
         1 - (2 + 3)       -> [1, 2, 3]    and [None, Subtraction, Subtraction]
         (1 + 2) - (2 + 3) -> [1, 2, 2, 3] and [None, Addition, Subtraction, Subtraction]
         """
+
+        left_child.domain = []
+        right_child.domain = []
         for side, child in [("left", left_child), ("right", right_child)]:
             if isinstance(child, (pybamm.Addition, pybamm.Subtraction)):
                 left, right = child.orphans
@@ -264,6 +270,9 @@ def simplify_multiplication_division(myclass, left, right):
         Note that multiplication *within* matrix multiplications, e.g. a@(b*c), are not
         flattened into a@b*c, as this would be incorrect (see #253)
 
+        Note that the domains are all set to [] as we do not wish to consider domains
+        once simplifications are applied
+
         outputs to lists `numerator`, `denominator` and `numerator_types`
 
         e.g.
@@ -272,6 +281,9 @@ def simplify_multiplication_division(myclass, left, right):
         (1 @ 2) / 3 ->  [1, 2]       [3]       [None, MatrixMultiplication]
         1 / (c / 2) ->  [1, 2]       [c]       [None, Multiplication]
         """
+
+        left_child.domain = []
+        right_child.domain = []
         for side, child in [("left", left_child), ("right", right_child)]:
 
             if side == "left":
diff --git a/pybamm/expression_tree/vector.py b/pybamm/expression_tree/vector.py
index be46f4c06..0aee6aad2 100644
--- a/pybamm/expression_tree/vector.py
+++ b/pybamm/expression_tree/vector.py
@@ -141,7 +141,7 @@ class StateVector(pybamm.Symbol):
 
     def new_copy(self):
         """ See :meth:`pybamm.Symbol.new_copy()`. """
-        return StateVector(self.y_slice, self.name, domain=[])
+        return StateVector(self.y_slice, self.name, domain=self.domain)
 
     def evaluate_for_shape(self):
         """


Test patch:
diff --git a/tests/unit/test_expression_tree/test_simplify.py b/tests/unit/test_expression_tree/test_simplify.py
index 61ebe1657..66a5cdcdf 100644
--- a/tests/unit/test_expression_tree/test_simplify.py
+++ b/tests/unit/test_expression_tree/test_simplify.py
@@ -540,6 +540,23 @@ class TestSimplify(unittest.TestCase):
         self.assertIsInstance(outer_simp, pybamm.Vector)
         np.testing.assert_array_equal(outer_simp.evaluate(), 2 * np.ones((15, 1)))
 
+    def test_simplify_divide_outer(self):
+        u = pybamm.Scalar(1)
+        v = pybamm.StateVector(slice(0, 5), domain="current collector")
+        outer = pybamm.Outer(v, u)
+
+        exp1 = pybamm.Division(pybamm.Division(outer, u), u)
+        self.assertIsInstance(exp1.simplify(), pybamm.Outer)
+
+        exp2 = pybamm.Division(pybamm.Division(outer, 2 * u), u)
+        self.assertIsInstance(exp2.simplify(), pybamm.Multiplication)
+
+        exp3 = pybamm.Division(pybamm.Division(outer, u), 2 * u)
+        self.assertIsInstance(exp3.simplify(), pybamm.Multiplication)
+
+        exp4 = pybamm.Division(pybamm.Division(outer, 2 * u), 2 * u)
+        exp4.simplify()
+
 
 if __name__ == "__main__":
     print("Add -v for more debug output")