Skip to content

Commit

Permalink
ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
ksagiyam committed Aug 21, 2024
1 parent 3da43d0 commit 0eeb02f
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 64 deletions.
28 changes: 20 additions & 8 deletions test/test_external_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,22 @@ def test_extractions(domain_2d, V1):

assert extract_coefficients(e) == [u]
assert extract_arguments(e) == [vstar_e, u_hat]
assert extract_arguments_and_coefficients_and_geometric_quantities(e) == \
([vstar_e, u_hat], [u], [])
assert extract_arguments_and_coefficients_and_geometric_quantities(e) == (
[vstar_e, u_hat],
[u],
[],
)
assert extract_base_form_operators(e) == [e]

F = e * dx

assert extract_coefficients(F) == [u]
assert extract_arguments(e) == [vstar_e, u_hat]
assert extract_arguments_and_coefficients_and_geometric_quantities(e) == \
([vstar_e, u_hat], [u], [])
assert extract_arguments_and_coefficients_and_geometric_quantities(e) == (
[vstar_e, u_hat],
[u],
[],
)
assert F.base_form_operators() == (e,)

w = Coefficient(V1)
Expand All @@ -254,16 +260,22 @@ def test_extractions(domain_2d, V1):

assert extract_coefficients(e2) == [u, w]
assert extract_arguments(e2) == [vstar_e2, u_hat]
assert extract_arguments_and_coefficients_and_geometric_quantities(e2) == \
([vstar_e2, u_hat], [u, w], [])
assert extract_arguments_and_coefficients_and_geometric_quantities(e2) == (
[vstar_e2, u_hat],
[u, w],
[],
)
assert extract_base_form_operators(e2) == [e, e2]

F = e2 * dx

assert extract_coefficients(e2) == [u, w]
assert extract_arguments(e2) == [vstar_e2, u_hat]
assert extract_arguments_and_coefficients_and_geometric_quantities(e2) == \
([vstar_e2, u_hat], [u, w], [])
assert extract_arguments_and_coefficients_and_geometric_quantities(e2) == (
[vstar_e2, u_hat],
[u, w],
[],
)
assert F.base_form_operators() == (e, e2)


Expand Down
7 changes: 5 additions & 2 deletions test/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,11 @@ def test_extract_base_form_operators(V1, V2):
# -- Interpolate(u, V2) -- #
Iv = Interpolate(uhat, V2)
assert extract_arguments(Iv) == [vstar, uhat]
assert extract_arguments_and_coefficients_and_geometric_quantities(Iv) == \
([vstar, uhat], [], [])
assert extract_arguments_and_coefficients_and_geometric_quantities(Iv) == (
[vstar, uhat],
[],
[],
)
assert extract_coefficients(Iv) == []
assert extract_base_form_operators(Iv) == [Iv]

Expand Down
36 changes: 19 additions & 17 deletions test/test_mixed_function_space_with_mixed_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
def test_mixed_function_space_with_mixed_mesh_basic():
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 = MixedMesh(mesh0, mesh1, mesh2)
V = FunctionSpace(domain, elem)
u = TrialFunction(V)
Expand All @@ -44,30 +44,32 @@ def test_mixed_function_space_with_mixed_mesh_basic():
dx1 = Measure("dx", mesh1)
x = SpatialCoordinate(mesh1)
form = x[1] * f0 * inner(grad(u0), v1) * dx1(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,
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,
complex_mode=False,
)
(id0,) = fd.integral_data
assert fd.preprocessed_form.arguments() == (v, u)
assert fd.reduced_coefficients == [f]
assert form.coefficients()[fd.original_coefficient_positions[0]] is f
assert id0.domain is mesh1
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])
assert id0.enabled_coefficients == [True]


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
5 changes: 3 additions & 2 deletions ufl/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ def _get_action_form_arguments(left, right):
extract_arguments_and_coefficients_and_geometric_quantities,
)

right_args, right_coeffs, _ = \
extract_arguments_and_coefficients_and_geometric_quantities(right)
right_args, right_coeffs, _ = extract_arguments_and_coefficients_and_geometric_quantities(
right
)
arguments = left_args + tuple(right_args)
coefficients += tuple(right_coeffs)
elif isinstance(right, (BaseCoefficient, Zero)):
Expand Down
6 changes: 3 additions & 3 deletions ufl/algorithms/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ def extract_arguments_and_coefficients_and_geometric_quantities(a):
base_coeff_and_args_and_gq = extract_type(a, (BaseArgument, BaseCoefficient, GeometricQuantity))
arguments = [f for f in base_coeff_and_args_and_gq if isinstance(f, BaseArgument)]
coefficients = [f for f in base_coeff_and_args_and_gq if isinstance(f, BaseCoefficient)]
geometric_quantities = [f
for f in base_coeff_and_args_and_gq
if isinstance(f, GeometricQuantity)]
geometric_quantities = [
f for f in base_coeff_and_args_and_gq if isinstance(f, GeometricQuantity)
]

# Build number,part: instance mappings, should be one to one
bfnp = dict((f, (f.number(), f.part())) for f in arguments)
Expand Down
14 changes: 7 additions & 7 deletions ufl/algorithms/apply_derivatives.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
def flatten_domain_element(domain, element):
"""Return the flattened (domain, element) pairs for mixed domain problems."""
if not isinstance(domain, MixedMesh):
return ((domain, element), )
return ((domain, element),)
flattened = ()
assert len(domain) == len(element.sub_elements)
for d, e in zip(domain, element.sub_elements):
Expand Down Expand Up @@ -683,15 +683,15 @@ def reference_value(self, o):
for d, e in flatten_domain_element(domain, element):
esh = e.reference_value_shape
if esh == ():
esh = (1, )
esh = (1,)
if isinstance(e.pullback, PhysicalPullback):
if ref_dim != self._var_shape[0]:
raise NotImplementedError("""
PhysicalPullback not handled for immersed domain :
reference dim ({ref_dim}) != physical dim (self._var_shape[0])""")
for idx in range(int(np.prod(esh))):
for i in range(ref_dim):
components.append(g[(dofoffset + idx, ) + (i, )])
components.append(g[(dofoffset + idx,) + (i,)])
else:
K = JacobianInverse(d)
rdim, gdim = K.ufl_shape
Expand All @@ -703,7 +703,7 @@ def reference_value(self, o):
for i in range(gdim):
temp = Zero()
for j in range(rdim):
temp += g[(dofoffset + idx, ) + (j, )] * K[j, i]
temp += g[(dofoffset + idx,) + (j,)] * K[j, i]
components.append(temp)
dofoffset += int(np.prod(esh))
return as_tensor(np.asarray(components).reshape(vsh + self._var_shape))
Expand All @@ -730,7 +730,7 @@ def reference_grad(self, o):
if not f._ufl_is_in_reference_frame_:
raise RuntimeError("Expecting a reference frame type")
while not f._ufl_is_terminal_:
f, = f.ufl_operands
(f,) = f.ufl_operands
element = f.ufl_function_space().ufl_element()
if element.num_sub_elements != len(domain):
raise RuntimeError(f"{element.num_sub_elements} != {len(domain)}")
Expand All @@ -742,7 +742,7 @@ def reference_grad(self, o):
for d, e in flatten_domain_element(domain, element):
esh = e.reference_value_shape
if esh == ():
esh = (1, )
esh = (1,)
K = JacobianInverse(d)
rdim, gdim = K.ufl_shape
if rdim != ref_dim:
Expand All @@ -754,7 +754,7 @@ def reference_grad(self, o):
for i in range(gdim):
temp = Zero()
for j in range(rdim):
temp += g[(dofoffset + idx, ) + midx + (j, )] * K[j, i]
temp += g[(dofoffset + idx,) + midx + (j,)] * K[j, i]
components.append(temp)
dofoffset += int(np.prod(esh))
if g.ufl_shape[0] != dofoffset:
Expand Down
7 changes: 2 additions & 5 deletions ufl/algorithms/estimate_degrees.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,8 @@ def _reduce_degree(self, v, f):
TensorProduct elements or quadrilateral elements are involved.
"""
# Can have multiple domains of the same cell type.
cell, = set(d.ufl_cell() for d in extract_domains(v))
if isinstance(f, int) and cell.cellname() not in [
"quadrilateral",
"hexahedron"
]:
(cell,) = set(d.ufl_cell() for d in extract_domains(v))
if isinstance(f, int) and cell.cellname() not in ["quadrilateral", "hexahedron"]:
return max(f - 1, 0)
else:
return f
Expand Down
23 changes: 10 additions & 13 deletions ufl/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def _ufl_sort_key_(self):
@property
def meshes(self):
"""Return the component meshes."""
return (self, )
return (self,)


@attach_ufl_id
Expand All @@ -166,10 +166,10 @@ def __init__(self, *meshes, ufl_id=None, cargo=None):
raise NotImplementedError("""
Currently component meshes can not include MixedMesh instances""")
# currently only support single cell type.
self._ufl_cell, = set(m.ufl_cell() for m in meshes)
gdim, = set(m.geometric_dimension() for m in meshes)
(self._ufl_cell,) = set(m.ufl_cell() for m in meshes)
(gdim,) = set(m.geometric_dimension() for m in meshes)
# TODO: Need to change for more general mixed meshes.
tdim, = set(m.topological_dimension() for m in meshes)
(tdim,) = set(m.topological_dimension() for m in meshes)
AbstractDomain.__init__(self, tdim, gdim)
self._meshes = tuple(meshes)

Expand Down Expand Up @@ -200,9 +200,8 @@ def _ufl_signature_data_(self, renumbering):

def _ufl_sort_key_(self):
"""UFL sort key."""
typespecific = (self._ufl_id, )
return (self.geometric_dimension(), self.topological_dimension(),
"MixedMesh", typespecific)
typespecific = (self._ufl_id,)
return (self.geometric_dimension(), self.topological_dimension(), "MixedMesh", typespecific)

@property
def meshes(self):
Expand Down Expand Up @@ -273,13 +272,13 @@ def as_domain(domain):
"""Convert any valid object to an AbstractDomain type."""
if isinstance(domain, AbstractDomain):
# Modern UFL files and dolfin behaviour
domain, = set(domain.meshes)
(domain,) = set(domain.meshes)
return domain
try:
return extract_unique_domain(domain)
except AttributeError:
domain = domain.ufl_domain()
domain, = set(domain.meshes)
(domain,) = set(domain.meshes)
return domain


Expand Down Expand Up @@ -328,9 +327,7 @@ def extract_domains(expr, expand_mixed_mesh=True):
domainlist = []
for t in traverse_unique_terminals(expr):
domainlist.extend(t.ufl_domains())
return sort_domains(
join_domains(domainlist, expand_mixed_mesh=expand_mixed_mesh)
)
return sort_domains(join_domains(domainlist, expand_mixed_mesh=expand_mixed_mesh))


def extract_unique_domain(expr, expand_mixed_mesh=True):
Expand All @@ -351,7 +348,7 @@ def find_geometric_dimension(expr):
# Can have multiple domains of the same cell type.
domains = extract_domains(t)
if len(domains) > 0:
gdim, = set(domain.geometric_dimension() for domain in domains)
(gdim,) = set(domain.geometric_dimension() for domain in domains)
gdims.add(gdim)

if len(gdims) != 1:
Expand Down
15 changes: 8 additions & 7 deletions ufl/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,14 +607,14 @@ def _analyze_domains(self):
from ufl.domain import join_domains, sort_domains

# Collect integration domains.
self._integration_domains = \
sort_domains(join_domains([itg.ufl_domain() for itg in self._integrals]))
self._integration_domains = sort_domains(
join_domains([itg.ufl_domain() for itg in self._integrals])
)
# Collect domains in integrands.
domains_in_integrands = set()
for o in chain(self.arguments(),
self.coefficients(),
self.constants(),
self.geometric_quantities()):
for o in chain(
self.arguments(), self.coefficients(), self.constants(), self.geometric_quantities()
):
domain = extract_unique_domain(o, expand_mixed_mesh=False)
domains_in_integrands.update(domain.meshes)
domains_in_integrands -= set(self._integration_domains)
Expand Down Expand Up @@ -652,8 +652,9 @@ def _analyze_form_arguments(self):
extract_arguments_and_coefficients_and_geometric_quantities,
)

arguments, coefficients, geometric_quantities = \
arguments, coefficients, geometric_quantities = (
extract_arguments_and_coefficients_and_geometric_quantities(self)
)

# Define canonical numbering of arguments and coefficients
self._arguments = tuple(sorted(set(arguments), key=lambda x: x.number()))
Expand Down

0 comments on commit 0eeb02f

Please sign in to comment.