def test_symbol_new_copy(self): a = pybamm.Parameter("a") b = pybamm.Parameter("b") v_n = pybamm.Variable("v", "negative electrode") x_n = pybamm.standard_spatial_vars.x_n v_s = pybamm.Variable("v", "separator") vec = pybamm.Vector([1, 2, 3, 4, 5]) mat = pybamm.Matrix([[1, 2], [3, 4]]) mesh = get_mesh_for_testing() for symbol in [ a + b, a - b, a * b, a / b, a**b, -a, abs(a), pybamm.Function(np.sin, a), pybamm.FunctionParameter("function", {"a": a}), pybamm.grad(v_n), pybamm.div(pybamm.grad(v_n)), pybamm.upwind(v_n), pybamm.IndefiniteIntegral(v_n, x_n), pybamm.BackwardIndefiniteIntegral(v_n, x_n), pybamm.BoundaryValue(v_n, "right"), pybamm.BoundaryGradient(v_n, "right"), pybamm.PrimaryBroadcast(a, "domain"), pybamm.SecondaryBroadcast(v_n, "current collector"), pybamm.FullBroadcast(a, "domain", {"secondary": "other domain"}), pybamm.concatenation(v_n, v_s), pybamm.NumpyConcatenation(a, b, v_s), pybamm.DomainConcatenation([v_n, v_s], mesh), pybamm.Parameter("param"), pybamm.InputParameter("param"), pybamm.StateVector(slice(0, 56)), pybamm.Matrix(np.ones((50, 40))), pybamm.SpatialVariable("x", ["negative electrode"]), pybamm.t, pybamm.Index(vec, 1), pybamm.NotConstant(a), pybamm.ExternalVariable( "external variable", 20, domain="test", auxiliary_domains={"secondary": "test2"}, ), pybamm.minimum(a, b), pybamm.maximum(a, b), pybamm.SparseStack(mat, mat), ]: self.assertEqual(symbol.id, symbol.new_copy().id)
def test_symbol_new_copy(self): a = pybamm.Scalar(0) b = pybamm.Scalar(1) v_n = pybamm.Variable("v", "negative electrode") x_n = pybamm.standard_spatial_vars.x_n v_s = pybamm.Variable("v", "separator") vec = pybamm.Vector(np.array([1, 2, 3, 4, 5])) mesh = get_mesh_for_testing() for symbol in [ a + b, a - b, a * b, a / b, a**b, -a, abs(a), pybamm.Function(np.sin, a), pybamm.FunctionParameter("function", {"a": a}), pybamm.grad(v_n), pybamm.div(pybamm.grad(v_n)), pybamm.Integral(a, pybamm.t), pybamm.IndefiniteIntegral(v_n, x_n), pybamm.BackwardIndefiniteIntegral(v_n, x_n), pybamm.BoundaryValue(v_n, "right"), pybamm.BoundaryGradient(v_n, "right"), pybamm.PrimaryBroadcast(a, "domain"), pybamm.SecondaryBroadcast(v_n, "current collector"), pybamm.FullBroadcast(a, "domain", {"secondary": "other domain"}), pybamm.Concatenation(v_n, v_s), pybamm.NumpyConcatenation(a, b, v_s), pybamm.DomainConcatenation([v_n, v_s], mesh), pybamm.Parameter("param"), pybamm.InputParameter("param"), pybamm.StateVector(slice(0, 56)), pybamm.Matrix(np.ones((50, 40))), pybamm.SpatialVariable("x", ["negative electrode"]), pybamm.t, pybamm.Index(vec, 1), ]: self.assertEqual(symbol.id, symbol.new_copy().id)
def test_integral(self): # space integral a = pybamm.Symbol("a", domain=["negative electrode"]) x = pybamm.SpatialVariable("x", ["negative electrode"]) inta = pybamm.Integral(a, x) self.assertEqual(inta.name, "integral dx ['negative electrode']") self.assertEqual(inta.children[0].name, a.name) self.assertEqual(inta.integration_variable[0], x) self.assertEqual(inta.domain, []) self.assertEqual(inta.auxiliary_domains, {}) # space integral with secondary domain a_sec = pybamm.Symbol( "a", domain=["negative electrode"], auxiliary_domains={"secondary": "current collector"}, ) x = pybamm.SpatialVariable("x", ["negative electrode"]) inta_sec = pybamm.Integral(a_sec, x) self.assertEqual(inta_sec.domain, ["current collector"]) self.assertEqual(inta_sec.auxiliary_domains, {}) # space integral with tertiary domain a_tert = pybamm.Symbol( "a", domain=["negative electrode"], auxiliary_domains={ "secondary": "current collector", "tertiary": "some extra domain", }, ) x = pybamm.SpatialVariable("x", ["negative electrode"]) inta_tert = pybamm.Integral(a_tert, x) self.assertEqual(inta_tert.domain, ["current collector"]) self.assertEqual(inta_tert.auxiliary_domains, {"secondary": ["some extra domain"]}) # space integral *in* secondary domain y = pybamm.SpatialVariable("y", ["current collector"]) # without a tertiary domain inta_sec_y = pybamm.Integral(a_sec, y) self.assertEqual(inta_sec_y.domain, ["negative electrode"]) self.assertEqual(inta_sec_y.auxiliary_domains, {}) # with a tertiary domain inta_tert_y = pybamm.Integral(a_tert, y) self.assertEqual(inta_tert_y.domain, ["negative electrode"]) self.assertEqual(inta_tert_y.auxiliary_domains, {"secondary": ["some extra domain"]}) # space integral *in* tertiary domain z = pybamm.SpatialVariable("z", ["some extra domain"]) inta_tert_z = pybamm.Integral(a_tert, z) self.assertEqual(inta_tert_z.domain, ["negative electrode"]) self.assertEqual(inta_tert_z.auxiliary_domains, {"secondary": ["current collector"]}) # space integral over two variables b = pybamm.Symbol("b", domain=["current collector"]) y = pybamm.SpatialVariable("y", ["current collector"]) z = pybamm.SpatialVariable("z", ["current collector"]) inta = pybamm.Integral(b, [y, z]) self.assertEqual(inta.name, "integral dy dz ['current collector']") self.assertEqual(inta.children[0].name, b.name) self.assertEqual(inta.integration_variable[0], y) self.assertEqual(inta.integration_variable[1], z) self.assertEqual(inta.domain, []) # Indefinite inta = pybamm.IndefiniteIntegral(a, x) self.assertEqual(inta.name, "a integrated w.r.t x on ['negative electrode']") self.assertEqual(inta.children[0].name, a.name) self.assertEqual(inta.integration_variable[0], x) self.assertEqual(inta.domain, ["negative electrode"]) inta_sec = pybamm.IndefiniteIntegral(a_sec, x) self.assertEqual(inta_sec.domain, ["negative electrode"]) self.assertEqual(inta_sec.auxiliary_domains, {"secondary": ["current collector"]}) # backward indefinite integral inta = pybamm.BackwardIndefiniteIntegral(a, x) self.assertEqual( inta.name, "a integrated backward w.r.t x on ['negative electrode']") # expected errors a = pybamm.Symbol("a", domain=["negative electrode"]) x = pybamm.SpatialVariable("x", ["separator"]) y = pybamm.Variable("y") z = pybamm.SpatialVariable("z", ["negative electrode"]) with self.assertRaises(pybamm.DomainError): pybamm.Integral(a, x) with self.assertRaisesRegex(TypeError, "integration_variable must be"): pybamm.Integral(a, y) with self.assertRaisesRegex( NotImplementedError, "Indefinite integral only implemeted w.r.t. one variable", ): pybamm.IndefiniteIntegral(a, [x, y])