Repository: tinosulzer/PyBaMM

Issue:
Test in SPM failing
One of the [tests](https://github.com/tinosulzer/PyBaMM/blob/61fa60306919edab1bcb6e7c162329217da3311b/tests/test_models/test_li_ion.py#L21) for the SPM isn't inside the testing class so isn't being called, but once put inside the class it fails (`cp_surf` not increasing with time). Both `cn_surf` and `cp_surf` quickly go negative.
My guess is this is due to parameter values being wrong, but could be something else. Could you have a look @rtimms ?

Discussion:

Patch:
diff --git a/input/parameters/lithium-ion/parameters/LCO.csv b/input/parameters/lithium-ion/parameters/LCO.csv
index 141cb2ed..9d8b59c2 100644
--- a/input/parameters/lithium-ion/parameters/LCO.csv
+++ b/input/parameters/lithium-ion/parameters/LCO.csv
@@ -51,5 +51,5 @@ alpha,0.5,,Charge transfer coefficient,Scott Moura,
 ,,,,,
 # Initial Conditions,,,,,
 ce0,1000,mol.m-3, Initial lithium ion concentration in electrolyte,,
-cn0,1000,mol.m-3, Initial lithium concentration in positive electrode,,
-cp0,1000,mol.m-3, Initial lithium concentration in negative electrode,,
+cn0,1998.6,mol.m-3, Initial lithium concentration in positive electrode,,
+cp0,3073.1,mol.m-3, Initial lithium concentration in negative electrode,,
diff --git a/pybamm/models/li_ion/single_particle_model.py b/pybamm/models/li_ion/single_particle_model.py
index 445b2cc9..fcf2e535 100644
--- a/pybamm/models/li_ion/single_particle_model.py
+++ b/pybamm/models/li_ion/single_particle_model.py
@@ -79,7 +79,7 @@ class SPM(pybamm.BaseModel):
         Nn = - gamma_n * D_n(cn) * pybamm.grad(cn)
         dcndt = - pybamm.div(Nn)
         Np = - gamma_p * D_p(cp) * pybamm.grad(cp)
-        dcpdt = -pybamm.div(Np)
+        dcpdt = - pybamm.div(Np)
         self.rhs = {cn: dcndt, cp: dcpdt}
 
         # Boundary conditions
@@ -88,7 +88,7 @@ class SPM(pybamm.BaseModel):
             Nn: {"left": pybamm.Scalar(0),
                  "right": pybamm.Scalar(1) / ln / beta_n},
             Np: {"left": pybamm.Scalar(0),
-                 "right": pybamm.Scalar(1) / lp / beta_p / C_hat_p},
+                 "right": - pybamm.Scalar(1) / lp / beta_p / C_hat_p},
         }
 
         # Initial conditions


Test patch:
diff --git a/tests/test_models/test_li_ion.py b/tests/test_models/test_li_ion.py
index d14c23c8..e0a1e59c 100644
--- a/tests/test_models/test_li_ion.py
+++ b/tests/test_models/test_li_ion.py
@@ -17,23 +17,24 @@ class TestLiIonSPM(unittest.TestCase):
 
         modeltest.test_all()
 
+    def test_surface_concentrartion(self):
+        model = pybamm.li_ion.SPM()
+        params = model.default_parameter_values
+        params.process_model(model)
+        disc = model.default_discretisation
+        disc.process_model(model)
+        t_eval = np.linspace(0, 1, 100)
+        solver = model.default_solver
+        solver.solve(model, t_eval)
+        T, Y = solver.t, solver.y
 
-def test_surface_concentrartion(self):
-    model = pybamm.li_ion.SPM()
-    disc = model.default_discretisation
-    disc.process_model(model)
-    t_eval = np.linspace(0, 1, 100)
-    solver = model.default_solver
-    solver.solve(model, t_eval)
-    T, Y = solver.t, solver.y
-
-    # check surface concentration decreases in negative particle and
-    # increases in positive particle for discharge
-    np.testing.assert_array_less(
-        model.variables["cn_surf"].evaluate(T, Y)[:, 1:],
-        model.variables["cn_surf"].evaluate(T, Y)[:, :-1],
-    )
-    np.testing.assert_array_less(
-        model.variables["cp_surf"].evaluate(T, Y)[:, :-1],
-        model.variables["cp_surf"].evaluate(T, Y)[:, 1:],
-    )
+        # check surface concentration decreases in negative particle and
+        # increases in positive particle for discharge
+        np.testing.assert_array_less(
+            model.variables["cn_surf"].evaluate(T, Y)[:, 1:],
+            model.variables["cn_surf"].evaluate(T, Y)[:, :-1],
+        )
+        np.testing.assert_array_less(
+            model.variables["cp_surf"].evaluate(T, Y)[:, :-1],
+            model.variables["cp_surf"].evaluate(T, Y)[:, 1:],
+        )