Пример #1
0
 def __abs__(self):
     """return an :class:`AbsoluteValue` object, or a smooth approximation"""
     k = pybamm.settings.abs_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):
         out = pybamm.AbsoluteValue(self)
     else:
         out = pybamm.smooth_absolute_value(self, k)
     return pybamm.simplify_if_constant(out, keep_domains=True)
Пример #2
0
 def test_smooth_absolute_value(self):
     a = pybamm.StateVector(slice(0, 1))
     expr = pybamm.smooth_absolute_value(a, 10)
     self.assertAlmostEqual(expr.evaluate(y=np.array([1]))[0, 0], 1)
     self.assertEqual(expr.evaluate(y=np.array([0])), 0)
     self.assertAlmostEqual(expr.evaluate(y=np.array([-1]))[0, 0], 1)
     self.assertEqual(
         str(expr),
         "y[0:1] * (exp(10.0 * y[0:1]) - exp(-10.0 * y[0:1])) "
         "/ (exp(10.0 * y[0:1]) + exp(-10.0 * y[0:1]))",
     )
Пример #3
0
    def test_smooth_absolute_value(self):
        # Test that smooth absolute value is used when the setting is changed
        a = pybamm.Symbol("a")
        pybamm.settings.abs_smoothing = 10
        self.assertEqual(str(abs(a)), str(pybamm.smooth_absolute_value(a, 10)))

        # But exact absolute value should still be used for constants
        a = pybamm.Scalar(-5)
        self.assertEqual(str(abs(a)), str(pybamm.Scalar(5)))

        # Change setting back for other tests
        pybamm.settings.abs_smoothing = "exact"
Пример #4
0
 def __abs__(self):
     """return an :class:`AbsoluteValue` object, or a smooth approximation."""
     if isinstance(self, pybamm.AbsoluteValue):
         # No need to apply abs a second time
         return self
     elif isinstance(self, pybamm.Broadcast):
         # Move absolute value inside the broadcast
         # Apply recursively
         abs_self_not_broad = pybamm.simplify_if_constant(
             abs(self.orphans[0]))
         return self._unary_new_copy(abs_self_not_broad)
     else:
         k = pybamm.settings.abs_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):
             out = pybamm.AbsoluteValue(self)
         else:
             out = pybamm.smooth_absolute_value(self, k)
         return pybamm.simplify_if_constant(out)