Skip to content

Commit

Permalink
ruff format .
Browse files Browse the repository at this point in the history
  • Loading branch information
ksagiyam committed Jan 16, 2025
1 parent 20495df commit 988189f
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 87 deletions.
138 changes: 75 additions & 63 deletions test/test_mixed_function_space_with_mixed_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
def test_mixed_function_space_with_mixed_mesh_cell():
cell = triangle
elem0 = FiniteElement("Lagrange", cell, 1, (), identity_pullback, H1)
elem1 = FiniteElement("Brezzi-Douglas-Marini", cell, 1, (2, ), contravariant_piola, HDiv)
elem1 = FiniteElement("Brezzi-Douglas-Marini", cell, 1, (2,), contravariant_piola, HDiv)
elem2 = FiniteElement("Discontinuous Lagrange", cell, 0, (), identity_pullback, L2)
elem = MixedElement([elem0, elem1, elem2])
mesh0 = Mesh(FiniteElement("Lagrange", cell, 1, (2,), identity_pullback, H1), ufl_id=100)
Expand All @@ -48,17 +48,19 @@ def test_mixed_function_space_with_mixed_mesh_cell():
x1 = SpatialCoordinate(mesh1)
# Assemble (0, 1)-block.
form = x1[1] * f0 * div(g1) * inner(u1, grad(v0)) * dx2(999)
fd = compute_form_data(form,
do_apply_function_pullbacks=True,
do_apply_integral_scaling=True,
do_apply_geometry_lowering=True,
preserve_geometry_types=(CellVolume, FacetArea),
do_apply_restrictions=True,
do_estimate_degrees=True,
do_split_coefficients=(f, g),
do_assume_single_integral_type=False,
complex_mode=False)
id0, = fd.integral_data
fd = compute_form_data(
form,
do_apply_function_pullbacks=True,
do_apply_integral_scaling=True,
do_apply_geometry_lowering=True,
preserve_geometry_types=(CellVolume, FacetArea),
do_apply_restrictions=True,
do_estimate_degrees=True,
do_split_coefficients=(f, g),
do_assume_single_integral_type=False,
complex_mode=False,
)
(id0,) = fd.integral_data
assert fd.preprocessed_form.arguments() == (v0, u1)
assert fd.reduced_coefficients == [f, g]
assert form.coefficients()[fd.original_coefficient_positions[0]] is f
Expand All @@ -67,8 +69,8 @@ def test_mixed_function_space_with_mixed_mesh_cell():
assert id0.domain_integral_type_map[mesh1] == "cell"
assert id0.domain_integral_type_map[mesh2] == "cell"
assert id0.domain is mesh2
assert id0.integral_type == 'cell'
assert id0.subdomain_id == (999, )
assert id0.integral_type == "cell"
assert id0.subdomain_id == (999,)
assert fd.original_form.domain_numbering()[id0.domain] == 0
assert id0.integral_coefficients == set([f, g])
assert id0.enabled_coefficients == [True, True]
Expand All @@ -77,12 +79,12 @@ def test_mixed_function_space_with_mixed_mesh_cell():
def test_mixed_function_space_with_mixed_mesh_facet():
cell = triangle
elem0 = FiniteElement("Lagrange", cell, 1, (), identity_pullback, H1)
elem1 = FiniteElement("Brezzi-Douglas-Marini", cell, 1, (2, ), contravariant_piola, HDiv)
elem1 = FiniteElement("Brezzi-Douglas-Marini", cell, 1, (2,), contravariant_piola, HDiv)
elem2 = FiniteElement("Discontinuous Lagrange", cell, 0, (), identity_pullback, L2)
elem = MixedElement([elem0, elem1, elem2])
mesh0 = Mesh(FiniteElement("Lagrange", cell, 1, (2, ), identity_pullback, H1), ufl_id=100)
mesh1 = Mesh(FiniteElement("Lagrange", cell, 1, (2, ), identity_pullback, H1), ufl_id=101)
mesh2 = Mesh(FiniteElement("Lagrange", cell, 1, (2, ), identity_pullback, H1), ufl_id=102)
mesh0 = Mesh(FiniteElement("Lagrange", cell, 1, (2,), identity_pullback, H1), ufl_id=100)
mesh1 = Mesh(FiniteElement("Lagrange", cell, 1, (2,), identity_pullback, H1), ufl_id=101)
mesh2 = Mesh(FiniteElement("Lagrange", cell, 1, (2,), identity_pullback, H1), ufl_id=102)
domain = MeshSequence([mesh0, mesh1, mesh2])
V = FunctionSpace(domain, elem)
V1 = FunctionSpace(mesh1, elem1)
Expand All @@ -97,37 +99,43 @@ def test_mixed_function_space_with_mixed_mesh_facet():
ds2 = Measure("ds", mesh2)
x2 = SpatialCoordinate(mesh2)
# Assemble (2, 1)-block.
form = inner(x2, g1('+')) * g2 * inner(u1('-'), grad(v2)) * dS1(999) + \
f0('-') * div(f1) * inner(div(u1), v2) * ds2(777)
fd = compute_form_data(form,
do_apply_function_pullbacks=True,
do_apply_integral_scaling=True,
do_apply_geometry_lowering=True,
preserve_geometry_types=(CellVolume, FacetArea),
do_apply_restrictions=True,
do_estimate_degrees=True,
do_split_coefficients=(f, g),
do_assume_single_integral_type=False,
complex_mode=False)
id0, id1, = fd.integral_data
form = inner(x2, g1("+")) * g2 * inner(u1("-"), grad(v2)) * dS1(999) + f0("-") * div(
f1
) * inner(div(u1), v2) * ds2(777)
fd = compute_form_data(
form,
do_apply_function_pullbacks=True,
do_apply_integral_scaling=True,
do_apply_geometry_lowering=True,
preserve_geometry_types=(CellVolume, FacetArea),
do_apply_restrictions=True,
do_estimate_degrees=True,
do_split_coefficients=(f, g),
do_assume_single_integral_type=False,
complex_mode=False,
)
(
id0,
id1,
) = fd.integral_data
assert fd.preprocessed_form.arguments() == (v2, u1)
assert fd.reduced_coefficients == [f, g]
assert form.coefficients()[fd.original_coefficient_positions[0]] is f
assert form.coefficients()[fd.original_coefficient_positions[1]] is g
assert id0.domain_integral_type_map[mesh1] == "interior_facet"
assert id0.domain_integral_type_map[mesh2] == "exterior_facet"
assert id0.domain is mesh1
assert id0.integral_type == 'interior_facet'
assert id0.subdomain_id == (999, )
assert id0.integral_type == "interior_facet"
assert id0.subdomain_id == (999,)
assert fd.original_form.domain_numbering()[id0.domain] == 0
assert id0.integral_coefficients == set([g])
assert id0.enabled_coefficients == [False, True]
assert id1.domain_integral_type_map[mesh0] == "interior_facet"
assert id1.domain_integral_type_map[mesh1] == "exterior_facet"
assert id1.domain_integral_type_map[mesh2] == "exterior_facet"
assert id1.domain is mesh2
assert id1.integral_type == 'exterior_facet'
assert id1.subdomain_id == (777, )
assert id1.integral_type == "exterior_facet"
assert id1.subdomain_id == (777,)
assert fd.original_form.domain_numbering()[id1.domain] == 1
assert id1.integral_coefficients == set([f])
assert id1.enabled_coefficients == [True, False]
Expand All @@ -136,12 +144,12 @@ def test_mixed_function_space_with_mixed_mesh_facet():
def test_mixed_function_space_with_mixed_mesh_raise():
cell = triangle
elem0 = FiniteElement("Lagrange", cell, 1, (), identity_pullback, H1)
elem1 = FiniteElement("Brezzi-Douglas-Marini", cell, 1, (2, ), contravariant_piola, HDiv)
elem1 = FiniteElement("Brezzi-Douglas-Marini", cell, 1, (2,), contravariant_piola, HDiv)
elem2 = FiniteElement("Discontinuous Lagrange", cell, 0, (), identity_pullback, L2)
elem = MixedElement([elem0, elem1, elem2])
mesh0 = Mesh(FiniteElement("Lagrange", cell, 1, (2, ), identity_pullback, H1), ufl_id=100)
mesh1 = Mesh(FiniteElement("Lagrange", cell, 1, (2, ), identity_pullback, H1), ufl_id=101)
mesh2 = Mesh(FiniteElement("Lagrange", cell, 1, (2, ), identity_pullback, H1), ufl_id=102)
mesh0 = Mesh(FiniteElement("Lagrange", cell, 1, (2,), identity_pullback, H1), ufl_id=100)
mesh1 = Mesh(FiniteElement("Lagrange", cell, 1, (2,), identity_pullback, H1), ufl_id=101)
mesh2 = Mesh(FiniteElement("Lagrange", cell, 1, (2,), identity_pullback, H1), ufl_id=102)
domain = MeshSequence([mesh0, mesh1, mesh2])
V = FunctionSpace(domain, elem)
f = Coefficient(V, count=1000)
Expand All @@ -150,39 +158,43 @@ def test_mixed_function_space_with_mixed_mesh_raise():
_, g1, _ = split(g)
dS1 = Measure("dS", mesh1)
# Make sure that all mixed functions are split when applying default restrictions.
form = div(g1('+')) * div(f1('-')) * dS1
form = div(g1("+")) * div(f1("-")) * dS1
with pytest.raises(RuntimeError) as e_info:
_ = compute_form_data(form,
do_apply_function_pullbacks=True,
do_apply_integral_scaling=True,
do_apply_geometry_lowering=True,
preserve_geometry_types=(CellVolume, FacetArea),
do_apply_restrictions=True,
do_estimate_degrees=True,
do_split_coefficients=(f,),
do_assume_single_integral_type=False,
complex_mode=False)
_ = compute_form_data(
form,
do_apply_function_pullbacks=True,
do_apply_integral_scaling=True,
do_apply_geometry_lowering=True,
preserve_geometry_types=(CellVolume, FacetArea),
do_apply_restrictions=True,
do_estimate_degrees=True,
do_split_coefficients=(f,),
do_assume_single_integral_type=False,
complex_mode=False,
)
assert e_info.match("Not expecting a terminal object on a mixed mesh at this stage")
# Make sure that g1 is restricted as f1.
form = div(g1) * div(f1('-')) * dS1
form = div(g1) * div(f1("-")) * dS1
with pytest.raises(ValueError) as e_info:
_ = compute_form_data(form,
do_apply_function_pullbacks=True,
do_apply_integral_scaling=True,
do_apply_geometry_lowering=True,
preserve_geometry_types=(CellVolume, FacetArea),
do_apply_restrictions=True,
do_estimate_degrees=True,
do_split_coefficients=(f, g),
do_assume_single_integral_type=False,
complex_mode=False)
_ = compute_form_data(
form,
do_apply_function_pullbacks=True,
do_apply_integral_scaling=True,
do_apply_geometry_lowering=True,
preserve_geometry_types=(CellVolume, FacetArea),
do_apply_restrictions=True,
do_estimate_degrees=True,
do_split_coefficients=(f, g),
do_assume_single_integral_type=False,
complex_mode=False,
)
assert e_info.match("Discontinuous type Coefficient must be restricted.")


def test_mixed_function_space_with_mixed_mesh_signature():
cell = triangle
mesh0 = Mesh(FiniteElement("Lagrange", cell, 1, (2, ), identity_pullback, H1), ufl_id=100)
mesh1 = Mesh(FiniteElement("Lagrange", cell, 1, (2, ), identity_pullback, H1), ufl_id=101)
mesh0 = Mesh(FiniteElement("Lagrange", cell, 1, (2,), identity_pullback, H1), ufl_id=100)
mesh1 = Mesh(FiniteElement("Lagrange", cell, 1, (2,), identity_pullback, H1), ufl_id=101)
dx0 = Measure("dx", mesh0)
dx1 = Measure("dx", mesh1)
n0 = FacetNormal(mesh0)
Expand Down
18 changes: 9 additions & 9 deletions ufl/algorithms/apply_coefficient_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ def modified_terminal(self, o):
if isinstance(t, ReferenceValue):
assert not reference_value, "Got twice pulled back terminal!"
reference_value = True
t, = t.ufl_operands
(t,) = t.ufl_operands
elif isinstance(t, ReferenceGrad):
local_derivatives += 1
t, = t.ufl_operands
(t,) = t.ufl_operands
elif isinstance(t, Restricted):
assert restriction is None, "Got twice restricted terminal!"
restriction = t._side
t, = t.ufl_operands
(t,) = t.ufl_operands
elif t._ufl_terminal_modifiers_:
raise ValueError(
f"Missing handler for terminal modifier type {type(t)}, object is {t!r}."
Expand Down Expand Up @@ -95,9 +95,9 @@ def modified_terminal(self, o):
c = Zero(c.ufl_shape + (dim,), c.ufl_free_indices, c.ufl_index_dimensions)
else:
c = ReferenceGrad(c)
if restriction == '+':
if restriction == "+":
c = PositiveRestricted(c)
elif restriction == '-':
elif restriction == "-":
c = NegativeRestricted(c)
elif restriction is not None:
raise RuntimeError(f"Got unknown restriction: {restriction}")
Expand All @@ -106,7 +106,7 @@ def modified_terminal(self, o):
# New modified terminal: component[alpha + beta]
components.append(c[alpha + beta])
# Repack derivative indices to shape
c, = indices(1)
(c,) = indices(1)
return ComponentTensor(as_tensor(components)[c], MultiIndex((c,) + beta))

positive_restricted = modified_terminal
Expand Down Expand Up @@ -163,7 +163,7 @@ def zero(self, o):
return Zero(
shape=o.ufl_shape,
free_indices=tuple(free_indices),
index_dimensions=tuple(index_dimensions)
index_dimensions=tuple(index_dimensions),
)

@memoized_handler
Expand Down Expand Up @@ -194,13 +194,13 @@ def __init__(self):
@memoized_handler
def _zero_simplify(self, o):
"""Apply simplification for Zero()."""
operand, = o.ufl_operands
(operand,) = o.ufl_operands
operand = map_expr_dag(self, operand)
if isinstance(operand, Zero):
return Zero(
shape=o.ufl_shape,
free_indices=o.ufl_free_indices,
index_dimensions=o.ufl_index_dimensions
index_dimensions=o.ufl_index_dimensions,
)
else:
return o._ufl_expr_reconstruct_(operand)
Expand Down
29 changes: 16 additions & 13 deletions ufl/algorithms/apply_restrictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ def __init__(self, side=None, assume_single_integral_type=True, default_restrict
self.current_restriction = side
self.default_restriction = default_restriction
# Caches for propagating the restriction with map_expr_dag
self.vcaches = {"+": {}, "-": {},}
self.rcaches = {"+": {}, "-": {},}
self.vcaches = {
"+": {},
"-": {},
}
self.rcaches = {
"+": {},
"-": {},
}
if self.current_restriction is None:
self._rp = {
"+": RestrictionPropagator("+", assume_single_integral_type, default_restriction),
Expand Down Expand Up @@ -247,11 +253,7 @@ def facet_normal(self, o):
return self._require_restriction(o)


def apply_restrictions(
expression,
assume_single_integral_type=True,
domain_integral_type_map=None
):
def apply_restrictions(expression, assume_single_integral_type=True, domain_integral_type_map=None):
"""Propagate restriction nodes to wrap differential terminals directly."""
if assume_single_integral_type:
# Hnadle the conventional single-domain case.
Expand Down Expand Up @@ -285,7 +287,7 @@ def apply_restrictions(
integral_types = None
rules = RestrictionPropagator(
assume_single_integral_type=assume_single_integral_type,
default_restriction=default_restriction
default_restriction=default_restriction,
)
if isinstance(expression, FormData):
for integral_data in expression.integral_data:
Expand All @@ -305,6 +307,7 @@ class DomainRestrictionMapMaker(MultiFunction):
This must be done per integral_data.
"""

def __init__(self, domain_restriction_map: dict):
"""Initialise.
Expand All @@ -327,14 +330,14 @@ def _modifier(self, o):
if isinstance(t, ReferenceValue):
assert not reference_value, "Got twice pulled back terminal"
reference_value = True
t, = t.ufl_operands
(t,) = t.ufl_operands
elif isinstance(t, ReferenceGrad):
local_derivatives += 1
t, = t.ufl_operands
(t,) = t.ufl_operands
elif isinstance(t, Restricted):
assert restriction is None, "Got twice restricted terminal"
restriction = t._side
t, = t.ufl_operands
(t,) = t.ufl_operands
elif t._ufl_terminal_modifiers_:
raise ValueError(
f"Missing handler for terminal modifier type {type(t)}, object is {t!r}."
Expand All @@ -349,7 +352,7 @@ def _modifier(self, o):
if domain is not None:
if domain not in self._domain_restriction_map:
self._domain_restriction_map[domain] = set()
if restriction in ['+', '-']:
if restriction in ["+", "-"]:
self._domain_restriction_map[domain].add(restriction)
elif restriction is not None:
raise RuntimeError(f"Got unknown restriction: {restriction}")
Expand Down Expand Up @@ -378,7 +381,7 @@ def make_domain_integral_type_map(integral_data):
integration_type = integral_data.integral_type
domain_integral_type_dict = {}
for d, rs in domain_restriction_map.items():
if rs in [{'+'}, {'-'}, {'+', '-'}]:
if rs in [{"+"}, {"-"}, {"+", "-"}]:
domain_integral_type_dict[d] = "interior_facet"
elif rs == set():
if d.topological_dimension() == integration_domain.topological_dimension():
Expand Down
5 changes: 3 additions & 2 deletions ufl/algorithms/compute_form_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,9 @@ def compute_form_data(
# -- isinstance(elem.pullback, MixedPullback)
if isinstance(mesh, MeshSequence) and o in do_split_coefficients:
assert len(mesh) == len(elem.sub_elements)
coefficient_split[c] = [Coefficient(FunctionSpace(m, e))
for m, e in zip(mesh, elem.sub_elements)]
coefficient_split[c] = [
Coefficient(FunctionSpace(m, e)) for m, e in zip(mesh, elem.sub_elements)
]
self.coefficient_split = coefficient_split
for itg_data in self.integral_data:
new_integrals = []
Expand Down

0 comments on commit 988189f

Please sign in to comment.