def __ge__(self, other): """return a :class:`EqualHeaviside` object, or a smooth approximation.""" k = pybamm.settings.heaviside_smoothing # Return exact approximation if that is the setting or the outcome is a constant # (i.e. no need for smoothing) if k == "exact" or (is_constant(self) and is_constant(other)): out = pybamm.EqualHeaviside(other, self) else: out = pybamm.sigmoid(other, self, k) return pybamm.simplify_if_constant(out)
def test_sigmoid(self): # Test that smooth heaviside is used when the setting is changed a = pybamm.Symbol("a") b = pybamm.Symbol("b") pybamm.settings.heaviside_smoothing = 10 self.assertEqual(str(a < b), str(pybamm.sigmoid(a, b, 10))) self.assertEqual(str(a <= b), str(pybamm.sigmoid(a, b, 10))) self.assertEqual(str(a > b), str(pybamm.sigmoid(b, a, 10))) self.assertEqual(str(a >= b), str(pybamm.sigmoid(b, a, 10))) # But exact heavisides should still be used if both variables are constant a = 1 b = pybamm.Parameter("b") self.assertEqual(str(a < b), str(pybamm.NotEqualHeaviside(a, b))) self.assertEqual(str(a <= b), str(pybamm.EqualHeaviside(a, b))) self.assertEqual(str(a > b), str(pybamm.NotEqualHeaviside(b, a))) self.assertEqual(str(a >= b), str(pybamm.EqualHeaviside(b, a))) # Change setting back for other tests pybamm.settings.heaviside_smoothing = "exact"
def test_to_equation(self): # Test print_name pybamm.Addition.print_name = "test" self.assertEqual(pybamm.Addition(1, 2).to_equation(), sympy.symbols("test")) # Test Power self.assertEqual(pybamm.Power(7, 2).to_equation(), 49) # Test Division self.assertEqual(pybamm.Division(10, 2).to_equation(), 5) # Test Matrix Multiplication arr1 = pybamm.Array([[1, 0], [0, 1]]) arr2 = pybamm.Array([[4, 1], [2, 2]]) self.assertEqual( pybamm.MatrixMultiplication(arr1, arr2).to_equation(), sympy.Matrix([[4.0, 1.0], [2.0, 2.0]]), ) # Test EqualHeaviside self.assertEqual(pybamm.EqualHeaviside(1, 0).to_equation(), False) # Test NotEqualHeaviside self.assertEqual(pybamm.NotEqualHeaviside(2, 4).to_equation(), True)
def __ge__(self, other): """return a :class:`EqualHeaviside` object""" return pybamm.simplify_if_constant(pybamm.EqualHeaviside(other, self), keep_domains=True)