def test_convert_array_symbols(self): # Arrays a = np.array([1, 2, 3, 4, 5]) pybamm_a = pybamm.Array(a) self.assert_casadi_equal(pybamm_a.to_casadi(), casadi.MX(a)) casadi_t = casadi.MX.sym("t") casadi_y = casadi.MX.sym("y", 10) casadi_y_dot = casadi.MX.sym("y_dot", 10) pybamm_t = pybamm.Time() pybamm_y = pybamm.StateVector(slice(0, 10)) pybamm_y_dot = pybamm.StateVectorDot(slice(0, 10)) # Time self.assertEqual(pybamm_t.to_casadi(casadi_t, casadi_y), casadi_t) # State Vector self.assert_casadi_equal(pybamm_y.to_casadi(casadi_t, casadi_y), casadi_y) # State Vector Dot self.assert_casadi_equal( pybamm_y_dot.to_casadi(casadi_t, casadi_y, casadi_y_dot), casadi_y_dot)
def test_symbol_evaluates_to_number(self): a = pybamm.Scalar(3) self.assertTrue(a.evaluates_to_number()) a = pybamm.Parameter("a") self.assertFalse(a.evaluates_to_number()) a = pybamm.Scalar(3) * pybamm.Time() self.assertTrue(a.evaluates_to_number()) # highlight difference between this function and isinstance(a, Scalar) self.assertNotIsInstance(a, pybamm.Scalar) a = pybamm.Variable("a") self.assertFalse(a.evaluates_to_number()) a = pybamm.Scalar(3) - 2 self.assertTrue(a.evaluates_to_number()) a = pybamm.Vector(np.ones(5)) self.assertFalse(a.evaluates_to_number()) a = pybamm.Matrix(np.ones((4, 6))) self.assertFalse(a.evaluates_to_number()) a = pybamm.StateVector(slice(0, 10)) self.assertFalse(a.evaluates_to_number()) # Time variable returns true a = 3 * pybamm.t + 2 self.assertTrue(a.evaluates_to_number())
def test_time(self): t = pybamm.Time() self.assertEqual(t.name, "time") self.assertEqual(t.evaluate(4), 4) with self.assertRaises(ValueError): t.evaluate(None) t = pybamm.t self.assertEqual(t.name, "time") self.assertEqual(t.evaluate(4), 4) with self.assertRaises(ValueError): t.evaluate(None)
def test_convert_array_symbols(self): # Arrays a = np.array([1, 2, 3, 4, 5]) pybamm_a = pybamm.Array(a) self.assertTrue(casadi.is_equal(pybamm_a.to_casadi(), casadi.SX(a))) casadi_t = casadi.SX.sym("t") casadi_y = casadi.SX.sym("y", 10) pybamm_t = pybamm.Time() pybamm_y = pybamm.StateVector(slice(0, 10)) # Time self.assertEqual(pybamm_t.to_casadi(casadi_t, casadi_y), casadi_t) # State Vector self.assertTrue( casadi.is_equal(pybamm_y.to_casadi(casadi_t, casadi_y), casadi_y)) # outer product outer = pybamm.Outer(pybamm_a, pybamm_a) self.assertTrue( casadi.is_equal(outer.to_casadi(), casadi.SX(outer.evaluate())))