Repository: tinosulzer/PyBaMM

Issue:
IndependentVariable: a node representing an independent variable (space or time)
Add to expression tree hierarchy: `class IndependentVariable(Symbol)`: a node representing an independent variable (space or time)

Discussion:

Patch:
diff --git a/docs/source/expression_tree/independent_variable.rst b/docs/source/expression_tree/independent_variable.rst
new file mode 100644
index 00000000..731abc9d
--- /dev/null
+++ b/docs/source/expression_tree/independent_variable.rst
@@ -0,0 +1,9 @@
+Independent Variable
+====================
+
+.. autoclass:: pybamm.IndependentVariable
+  :members:
+
+.. autodata:: pybamm.t
+  :annotation: = the independent variable time
+
diff --git a/docs/source/expression_tree/index.rst b/docs/source/expression_tree/index.rst
index f0397ea4..15f14135 100644
--- a/docs/source/expression_tree/index.rst
+++ b/docs/source/expression_tree/index.rst
@@ -7,6 +7,7 @@ Expression Tree
   domain
   parameter
   variable
+  independent_variable
   binary_operator
   unary_operator
   scalar
diff --git a/pybamm/__init__.py b/pybamm/__init__.py
index df59e494..1238e895 100644
--- a/pybamm/__init__.py
+++ b/pybamm/__init__.py
@@ -71,6 +71,8 @@ from .expression_tree.unary_operators import (
 )
 from .expression_tree.scalar import Scalar
 from .expression_tree.variable import Variable
+from .expression_tree.independent_variable import IndependentVariable
+from .expression_tree.independent_variable import t
 from .expression_tree.vector import Vector
 
 #
diff --git a/pybamm/expression_tree/independent_variable.py b/pybamm/expression_tree/independent_variable.py
new file mode 100644
index 00000000..404ded42
--- /dev/null
+++ b/pybamm/expression_tree/independent_variable.py
@@ -0,0 +1,33 @@
+#
+# IndependentVariable class
+#
+from __future__ import absolute_import, division
+from __future__ import print_function, unicode_literals
+import pybamm
+
+
+class IndependentVariable(pybamm.Domain, pybamm.Symbol):
+    """A node in the expression tree represending an independent variable
+
+    Used for expressing functions depending on a spatial variable or time
+
+    A variable has a list of domains (text) that it is valid over
+    (inherits from :class:`.Domain`)
+
+    Arguments:
+
+    ``name`` (str)
+        name of the node
+    ``domain`` (iterable of str)
+        list of domains that this variable is valid over
+
+
+    *Extends:* :class:`Domain` and :class:`Symbol`
+    """
+
+    def __init__(self, name, domain=[]):
+        super().__init__(name, domain=domain)
+
+
+#: the independent variable time
+t = IndependentVariable("t")


Test patch:
diff --git a/tests/test_expression_tree/test_independent_variable.py b/tests/test_expression_tree/test_independent_variable.py
new file mode 100644
index 00000000..7ce7b560
--- /dev/null
+++ b/tests/test_expression_tree/test_independent_variable.py
@@ -0,0 +1,27 @@
+#
+# Tests for the Parameter class
+#
+from __future__ import absolute_import, division
+from __future__ import print_function, unicode_literals
+import pybamm
+
+import unittest
+
+
+class TestIndependentVariable(unittest.TestCase):
+    def test_variable_init(self):
+        a = pybamm.IndependentVariable("a")
+        self.assertEqual(a.name, "a")
+        self.assertEqual(a.domain, [])
+        a = pybamm.IndependentVariable("a", domain=['test'])
+        self.assertEqual(a.domain[0], 'test')
+        self.assertRaises(TypeError, pybamm.IndependentVariable("a", domain='test'))
+
+
+if __name__ == "__main__":
+    print("Add -v for more debug output")
+    import sys
+
+    if "-v" in sys.argv:
+        debug = True
+    unittest.main()