user can pass normal BCs to ReducedEquation
As things were before this MR, the user had to pass functions to ReducedEquation.__init__
that apply the BCs, instead of passing the BCs themselves. This is not necessary; instead, we can define
def _default_apply_bc(bc, u):
if hasattr(u, "vector"):
bc.apply(u.vector())
else:
bc.apply(u)
return u
and
def _is_valid_bc(bc):
return hasattr(bc, "apply")
and do
def __init__(self, reduced_function, bcs=None, h_bcs=None):
bcs = [] if bcs is None else Enlist(bcs)
h_bcs = [] if h_bcs is None else Enlist(h_bcs)
bcs, h_bcs = self._validate_arguments(reduced_function, bcs, h_bcs)
self.reduced_function = reduced_function
self.bcs = bcs
self.h_bcs = h_bcs
where _validate_arguments
is defined as
def _validate_arguments(self, reduced_function, bcs, h_bcs):
if not isinstance(reduced_function, ReducedFunction):
raise TypeError("reduced_function should be a ReducedFunction")
for i, bc in enumerate(bcs):
if not callable(bc):
if not _is_valid_bc(bc):
raise TypeError("Boundary conditions must have an 'apply' method!")
else:
bcs[i] = partial(_default_apply_bc, bc)
for i, h_bc in enumerate(h_bcs):
if not callable(bc):
if not _is_valid_bc(h_bc):
raise TypeError("Boundary conditions must have an 'apply' method!")
else:
h_bcs[i] = partial(_default_apply_bc, h_bc)
return bcs, h_bcs