Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1507 remove algebraic equation check #1510

Merged
merged 2 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 8 additions & 28 deletions pybamm/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,40 +639,20 @@ def check_well_determined(self, post_discretisation):

def check_algebraic_equations(self, post_discretisation):
"""
Check that the algebraic equations are well-posed.
Before discretisation, each algebraic equation key must appear in the equation
After discretisation, there must be at least one StateVector in each algebraic
equation
Check that the algebraic equations are well-posed. After discretisation,
there must be at least one StateVector in each algebraic equation.
"""
vars_in_bcs = set()
unpacker = pybamm.SymbolUnpacker(pybamm.Variable)
for side_eqn in self.boundary_conditions.values():
all_vars = unpacker.unpack_list_of_symbols(
[eqn for eqn, _ in side_eqn.values()]
)
vars_in_bcs.update(all_vars.keys())
if not post_discretisation:
# After the model has been defined, each algebraic equation key should
# appear in that algebraic equation, or in the boundary conditions
# this has been relaxed for concatenations for now
for var, eqn in self.algebraic.items():
if not (
any(x.id == var.id for x in eqn.pre_order())
or var.id in vars_in_bcs
or isinstance(var, pybamm.Concatenation)
):
raise pybamm.ModelError(
"each variable in the algebraic eqn keys must appear in the eqn"
)
else:
# variables in keys don't get discretised so they will no longer match
# with the state vectors in the algebraic equations. Instead, we check
# that each algebraic equation contains some StateVector
if post_discretisation:
# Check that each algebraic equation contains some StateVector
for eqn in self.algebraic.values():
if not eqn.has_symbol_of_classes(pybamm.StateVector):
raise pybamm.ModelError(
"each algebraic equation must contain at least one StateVector"
)
else:
# We do not perfom any checks before discretisation (most problematic
# cases should be caught by `check_well_determined`)
pass

def check_ics_bcs(self):
"""Check that the initial and boundary conditions are well-posed."""
Expand Down
12 changes: 0 additions & 12 deletions tests/unit/test_models/test_base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,18 +342,6 @@ def test_check_well_posedness_variables(self):
with self.assertRaisesRegex(pybamm.ModelError, "extra algebraic keys"):
model.check_well_posedness()

# before discretisation, fail if the algebraic eqn keys don't appear in the eqns
model = pybamm.BaseModel()
model.algebraic = {c: d - 2, d: d - c}
with self.assertRaisesRegex(
pybamm.ModelError,
"each variable in the algebraic eqn keys must appear in the eqn",
):
model.check_well_posedness()
# passes when we switch the equations around
model.algebraic = {c: d - c, d: d - 2}
model.check_well_posedness()

# after discretisation, algebraic equation without a StateVector fails
model = pybamm.BaseModel()
model.algebraic = {
Expand Down