Repository: pybamm-team/PyBaMM

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`

Discussion:

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"])