Exemple #1
0
    def test_ceiling(self):
        a = pybamm.Symbol("a")
        ceila = pybamm.Ceiling(a)
        self.assertEqual(ceila.name, "ceil")
        self.assertEqual(ceila.children[0].name, a.name)

        b = pybamm.Scalar(3.5)
        ceilb = pybamm.Ceiling(b)
        self.assertEqual(ceilb.evaluate(), 4)

        c = pybamm.Scalar(-3.2)
        ceilc = pybamm.Ceiling(c)
        self.assertEqual(ceilc.evaluate(), -3)
Exemple #2
0
    def test_diff(self):
        a = pybamm.StateVector(slice(0, 1))
        y = np.array([5])

        # negation
        self.assertEqual((-a).diff(a).evaluate(y=y), -1)
        self.assertEqual((-a).diff(-a).evaluate(), 1)

        # absolute value
        self.assertEqual((a**3).diff(a).evaluate(y=y), 3 * 5**2)
        self.assertEqual((abs(a**3)).diff(a).evaluate(y=y), 3 * 5**2)
        self.assertEqual((a**3).diff(a).evaluate(y=-y), 3 * 5**2)
        self.assertEqual((abs(a**3)).diff(a).evaluate(y=-y), -3 * 5**2)

        # sign
        self.assertEqual((pybamm.sign(a)).diff(a).evaluate(y=y), 0)

        # floor
        self.assertEqual((pybamm.Floor(a)).diff(a).evaluate(y=y), 0)

        # ceil
        self.assertEqual((pybamm.Ceiling(a)).diff(a).evaluate(y=y), 0)

        # spatial operator (not implemented)
        spatial_a = pybamm.SpatialOperator("name", a)
        with self.assertRaises(NotImplementedError):
            spatial_a.diff(a)
Exemple #3
0
 def test_jac_of_ceiling(self):
     y = pybamm.StateVector(slice(0, 10))
     func = pybamm.Ceiling(y) * y
     jac = func.jac(y)
     y_test = np.linspace(-2, 2, 10)
     np.testing.assert_array_equal(np.diag(jac.evaluate(y=y_test)),
                                   np.ceil(y_test))
    def test_convert_scalar_symbols(self):
        a = pybamm.Scalar(0)
        b = pybamm.Scalar(1)
        c = pybamm.Scalar(-1)
        d = pybamm.Scalar(2)
        e = pybamm.Scalar(3)
        g = pybamm.Scalar(3.3)

        self.assertEqual(a.to_casadi(), casadi.MX(0))
        self.assertEqual(d.to_casadi(), casadi.MX(2))

        # negate
        self.assertEqual((-b).to_casadi(), casadi.MX(-1))
        # absolute value
        self.assertEqual(abs(c).to_casadi(), casadi.MX(1))
        # floor
        self.assertEqual(pybamm.Floor(g).to_casadi(), casadi.MX(3))
        # ceiling
        self.assertEqual(pybamm.Ceiling(g).to_casadi(), casadi.MX(4))

        # function
        def square_plus_one(x):
            return x**2 + 1

        f = pybamm.Function(square_plus_one, b)
        self.assertEqual(f.to_casadi(), 2)

        def myfunction(x, y):
            return x + y

        f = pybamm.Function(myfunction, b, d)
        self.assertEqual(f.to_casadi(), casadi.MX(3))

        # use classes to avoid simplification
        # addition
        self.assertEqual((pybamm.Addition(a, b)).to_casadi(), casadi.MX(1))
        # subtraction
        self.assertEqual(pybamm.Subtraction(c, d).to_casadi(), casadi.MX(-3))
        # multiplication
        self.assertEqual(
            pybamm.Multiplication(c, d).to_casadi(), casadi.MX(-2))
        # power
        self.assertEqual(pybamm.Power(c, d).to_casadi(), casadi.MX(1))
        # division
        self.assertEqual(pybamm.Division(b, d).to_casadi(), casadi.MX(1 / 2))

        # modulo
        self.assertEqual(pybamm.Modulo(e, d).to_casadi(), casadi.MX(1))

        # minimum and maximum
        self.assertEqual(pybamm.Minimum(a, b).to_casadi(), casadi.MX(0))
        self.assertEqual(pybamm.Maximum(a, b).to_casadi(), casadi.MX(1))