Skip to content

Commit

Permalink
#632 add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed May 27, 2020
1 parent 1007c87 commit d1f53ea
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
44 changes: 28 additions & 16 deletions pybamm/expression_tree/unary_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,34 @@ def __init__(self, child, integration_variable):
if not isinstance(integration_variable, list):
integration_variable = [integration_variable]

name = "integral"
for var in integration_variable:
if isinstance(var, pybamm.SpatialVariable):
# Check that child and integration_variable domains agree
if var.domain == child.domain:
self._integration_domain = "primary"
elif (
"secondary" in child.auxiliary_domains
and var.domain == child.auxiliary_domains["secondary"]
):
self._integration_domain = "secondary"
elif (
"tertiary" in child.auxiliary_domains
and var.domain == child.auxiliary_domains["tertiary"]
):
self._integration_domain = "tertiary"
else:
raise pybamm.DomainError(
"integration_variable must be the same as child domain or "
"auxiliary domain"
)
elif not isinstance(var, pybamm.IndependentVariable):
raise ValueError(
"integration_variable must be of type pybamm.IndependentVariable, "
"not {}".format(type(var))
)
name += " d{}".format(var.name)

# integral of a child takes the domain from auxiliary domain of the child
if child.auxiliary_domains != {}:
domain = child.auxiliary_domains["secondary"]
Expand All @@ -448,22 +476,6 @@ def __init__(self, child, integration_variable):
else:
domain = []
auxiliary_domains = {}
name = "integral"
for var in integration_variable:
if isinstance(var, pybamm.SpatialVariable):
# Check that child and integration_variable domains agree
if child.domain != var.domain:
raise pybamm.DomainError(
"child and integration_variable must have the same domain"
)
elif not isinstance(var, pybamm.IndependentVariable):
raise ValueError(
"""integration_variable must be of type pybamm.IndependentVariable,
not {}""".format(
type(var)
)
)
name += " d{}".format(var.name)

if any(isinstance(var, pybamm.SpatialVariable) for var in integration_variable):
name += " {}".format(child.domain)
Expand Down
5 changes: 2 additions & 3 deletions pybamm/models/submodels/particle/fickian_many_particles.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ def get_fundamental_variables(self):
elif self.domain == "Positive":
c_s = pybamm.standard_variables.c_s_p

# TODO: implement c_s_xav for Fickian many particles (tricky because this
# requires averaging a secondary domain)
variables = self._get_standard_concentration_variables(c_s, c_s)
c_s_xav = pybamm.x_average(c_s)
variables = self._get_standard_concentration_variables(c_s, c_s_xav)

return variables

Expand Down
18 changes: 17 additions & 1 deletion tests/unit/test_expression_tree/test_unary_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_integral(self):
inta_sec = pybamm.Integral(a_sec, x)
self.assertEqual(inta_sec.domain, ["current collector"])
self.assertEqual(inta_sec.auxiliary_domains, {})
# space integral with secondary domain
# space integral with tertiary domain
a_tert = pybamm.Symbol(
"a",
domain=["negative electrode"],
Expand All @@ -151,6 +151,22 @@ def test_integral(self):
inta_tert.auxiliary_domains, {"secondary": ["some extra domain"]}
)

# space integral *in* secondary domain
y = pybamm.SpatialVariable("y", ["current collector"])
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"])
Expand Down

0 comments on commit d1f53ea

Please sign in to comment.