Repository: pybamm-team/PyBaMM

Issue:
debugging within model
add functionality to model to set parameters and discretise a symbol, to see what processed symbol looks like

Discussion:

Patch:
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index a96a29c6c..72c1acf50 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -237,6 +237,7 @@ Figuring out where to start the debugger is the real challenge. Some good ways t
 ```python3
 pybamm.set_logging_level("DEBUG")
 ```
+6. In models that inherit from `pybamm.BaseBatteryModel` (i.e. any battery model), you can use `self.process_parameters_and_discretise` to process a symbol and see what it will look like.
 
 ### Profiling
 
diff --git a/pybamm/models/full_battery_models/base_battery_model.py b/pybamm/models/full_battery_models/base_battery_model.py
index 77c1f60a0..6080d11c8 100644
--- a/pybamm/models/full_battery_models/base_battery_model.py
+++ b/pybamm/models/full_battery_models/base_battery_model.py
@@ -19,6 +19,7 @@ class BaseBatteryModel(pybamm.BaseModel):
         self._extra_options = options
         self.set_standard_output_variables()
         self.submodels = OrderedDict()  # ordered dict not default in 3.5
+        self._built = False
 
     @property
     def default_parameter_values(self):
@@ -334,6 +335,8 @@ class BaseBatteryModel(pybamm.BaseModel):
         pybamm.logger.debug("Setting voltage variables")
         self.set_voltage_variables()
 
+        self._built = True
+
     def set_thermal_submodel(self):
 
         if self.options["thermal"] is None:
@@ -449,3 +452,39 @@ class BaseBatteryModel(pybamm.BaseModel):
         # Cut-off voltage
         voltage = self.variables["Terminal voltage"]
         self.events["Minimum voltage"] = voltage - self.param.voltage_low_cut
+
+    def process_parameters_and_discretise(self, symbol):
+        """
+        Process parameters and discretise a symbol using default parameter values,
+        geometry, etc. Note that the model needs to be built first for this to be
+        possible.
+
+        Parameters
+        ----------
+        symbol : :class:`pybamm.Symbol`
+            Symbol to be processed
+
+        Returns
+        -------
+        :class:`pybamm.Symbol`
+            Processed symbol
+        """
+        if not self._built:
+            self.build_model()
+
+        # Set up parameters
+        geometry = self.default_geometry
+        parameter_values = self.default_parameter_values
+        parameter_values.process_geometry(geometry)
+
+        # Set up discretisation
+        mesh = pybamm.Mesh(geometry, self.default_submesh_types, self.default_var_pts)
+        disc = pybamm.Discretisation(mesh, self.default_spatial_methods)
+        variables = list(self.rhs.keys()) + list(self.algebraic.keys())
+        disc.set_variable_slices(variables)
+
+        # Process
+        param_symbol = parameter_values.process_symbol(symbol)
+        disc_symbol = disc.process_symbol(param_symbol)
+
+        return disc_symbol


Test patch:
diff --git a/tests/unit/test_models/test_full_battery_models/test_base_battery_model.py b/tests/unit/test_models/test_full_battery_models/test_base_battery_model.py
new file mode 100644
index 000000000..b32e75836
--- /dev/null
+++ b/tests/unit/test_models/test_full_battery_models/test_base_battery_model.py
@@ -0,0 +1,27 @@
+#
+# Tests for the base battery model class
+#
+import pybamm
+import unittest
+
+
+class TestBaseBatteryModel(unittest.TestCase):
+    def test_process_parameters_and_discretise(self):
+        model = pybamm.ReactionDiffusionModel()
+        c = pybamm.Parameter("Negative electrode width [m]") * pybamm.Variable(
+            "Negative electrolyte concentration", domain="negative electrode"
+        )
+        processed_c = model.process_parameters_and_discretise(c)
+        self.assertIsInstance(processed_c, pybamm.Multiplication)
+        self.assertIsInstance(processed_c.left, pybamm.Scalar)
+        self.assertIsInstance(processed_c.right, pybamm.StateVector)
+
+
+if __name__ == "__main__":
+    print("Add -v for more debug output")
+    import sys
+
+    if "-v" in sys.argv:
+        debug = True
+    pybamm.settings.debug_mode = True
+    unittest.main()