Repository: pybamm-team/PyBaMM (python)

Issue:
consistency between entering domains as lists or not
sometimes domains need to be entered as a list and sometimes they don't. make this consistent across classes. 
potentially relax check on the variable `r` so that it's domain can be anyting (but keep check on `r_n` should be neg particle and same for `r_p`

Pull request:
#567 fix domain as list in spatial variables
# Description

Fixed the domain list issue in spatial variable. Don't know where else this was an issue @rtimms ?
Fixes #567 

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

# Key checklist:

- [ ] No style issues: `$ flake8`
- [ ] All tests pass: `$ python run-tests.py --unit`
- [ ] The documentation builds: `$ cd docs` and then `$ make clean; make html`

You can run all three at once, using `$ python run-tests.py --quick`.

## Further checks: 

- [ ] Code is commented, particularly in hard-to-understand areas
- [ ] Tests added that prove fix is effective or that feature works
- [ ] Any dependent changes have been merged and published in downstream modules


Patch:
diff --git a/pybamm/expression_tree/independent_variable.py b/pybamm/expression_tree/independent_variable.py
index cc61bca3d..c9c5a920b 100644
--- a/pybamm/expression_tree/independent_variable.py
+++ b/pybamm/expression_tree/independent_variable.py
@@ -73,9 +73,10 @@ class SpatialVariable(IndependentVariable):
     *Extends:* :class:`Symbol`
     """
 
-    def __init__(self, name, domain=[], coord_sys=None):
+    def __init__(self, name, domain=None, coord_sys=None):
         self.coord_sys = coord_sys
         super().__init__(name, domain=domain)
+        domain = self.domain
 
         if name not in KNOWN_SPATIAL_VARS:
             raise ValueError(
@@ -83,11 +84,18 @@ class SpatialVariable(IndependentVariable):
             )
         if domain == []:
             raise ValueError("domain must be provided")
-        if name in ["r", "r_n", "r_p"] and domain not in [
-            ["negative particle"],
-            ["positive particle"],
-        ]:
+
+        # Check symbol name vs domain name
+        if name == "r" and not (len(domain) == 1 and "particle" in domain[0]):
             raise pybamm.DomainError("domain must be particle if name is 'r'")
+        elif name == "r_n" and domain != ["negative particle"]:
+            raise pybamm.DomainError(
+                "domain must be negative particle if name is 'r_n'"
+            )
+        elif name == "r_p" and domain != ["positive particle"]:
+            raise pybamm.DomainError(
+                "domain must be positive particle if name is 'r_p'"
+            )
         elif name in ["x", "y", "z", "x_n", "x_s", "x_p"] and any(
             ["particle" in dom for dom in domain]
         ):


Test patch:
diff --git a/tests/unit/test_expression_tree/test_independent_variable.py b/tests/unit/test_expression_tree/test_independent_variable.py
index 7ac76d809..8471e5b66 100644
--- a/tests/unit/test_expression_tree/test_independent_variable.py
+++ b/tests/unit/test_expression_tree/test_independent_variable.py
@@ -32,13 +32,13 @@ class TestIndependentVariable(unittest.TestCase):
             t.evaluate(None)
 
     def test_spatial_variable(self):
-        x = pybamm.SpatialVariable("x", ["negative electrode"])
+        x = pybamm.SpatialVariable("x", "negative electrode")
         self.assertEqual(x.name, "x")
-        y = pybamm.SpatialVariable("y", ["separator"])
+        y = pybamm.SpatialVariable("y", "separator")
         self.assertEqual(y.name, "y")
-        z = pybamm.SpatialVariable("z", ["positive electrode"])
+        z = pybamm.SpatialVariable("z", "positive electrode")
         self.assertEqual(z.name, "z")
-        r = pybamm.SpatialVariable("r", ["negative particle"])
+        r = pybamm.SpatialVariable("r", "negative particle")
         self.assertEqual(r.name, "r")
         with self.assertRaises(NotImplementedError):
             x.evaluate()
@@ -49,6 +49,10 @@ class TestIndependentVariable(unittest.TestCase):
             pybamm.SpatialVariable("x", [])
         with self.assertRaises(pybamm.DomainError):
             pybamm.SpatialVariable("r", ["negative electrode"])
+        with self.assertRaises(pybamm.DomainError):
+            pybamm.SpatialVariable("r_n", ["positive particle"])
+        with self.assertRaises(pybamm.DomainError):
+            pybamm.SpatialVariable("r_p", ["negative particle"])
         with self.assertRaises(pybamm.DomainError):
             pybamm.SpatialVariable("x", ["negative particle"])