Skip to content

Commit

Permalink
Merge branch 'main' into jhale/external-operator-2023
Browse files Browse the repository at this point in the history
  • Loading branch information
mscroggs authored Aug 9, 2023
2 parents 6e1d9b8 + a49736c commit bee3cba
Show file tree
Hide file tree
Showing 22 changed files with 138 additions and 114 deletions.
6 changes: 6 additions & 0 deletions test/test_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ def test_cells_2d(cell):
assert cell.num_peaks() == cell.num_vertices()


def test_tensorproductcell():
orig = ufl.TensorProductCell(ufl.interval, ufl.interval)
cell = orig.reconstruct()
assert cell.sub_cells() == orig.sub_cells()
assert cell.topological_dimension() == orig.topological_dimension()
assert cell.geometric_dimension() == orig.geometric_dimension()
8 changes: 4 additions & 4 deletions test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
from ufl.algorithms import estimate_total_polynomial_degree
from ufl.algorithms.comparison_checker import do_comparison_check, ComplexComparisonError
from ufl.algorithms.formtransformations import compute_form_adjoint
from ufl import TestFunction, TrialFunction, triangle, FiniteElement, \
as_ufl, inner, grad, dx, dot, outer, conj, sqrt, sin, cosh, \
atan, ln, exp, as_tensor, real, imag, conditional, \
min_value, max_value, gt, lt, cos, ge, le, Coefficient
from ufl import (TestFunction, TrialFunction, triangle, FiniteElement,
as_ufl, inner, grad, dx, dot, outer, conj, sqrt, sin, cosh,
atan, ln, exp, as_tensor, real, imag, conditional,
min_value, max_value, gt, lt, cos, ge, le, Coefficient)


def test_conj(self):
Expand Down
8 changes: 4 additions & 4 deletions test/test_duals.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env py.test
# -*- coding: utf-8 -*-

from ufl import FiniteElement, FunctionSpace, MixedFunctionSpace, \
Coefficient, Matrix, Cofunction, FormSum, Argument, Coargument,\
TestFunction, TrialFunction, Adjoint, Action, \
action, adjoint, derivative, tetrahedron, triangle, interval, dx
from ufl import (FiniteElement, FunctionSpace, MixedFunctionSpace,
Coefficient, Matrix, Cofunction, FormSum, Argument, Coargument,
TestFunction, TrialFunction, Adjoint, Action,
action, adjoint, derivative, tetrahedron, triangle, interval, dx)
from ufl.constantvalue import Zero
from ufl.form import ZeroBaseForm

Expand Down
6 changes: 6 additions & 0 deletions test/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,9 @@ def test_mse():

element = FiniteElement('GLL-Edge L2', interval, degree - 1)
assert element == eval(repr(element))


def test_withmapping():
base = FiniteElement("CG", interval, 1)
element = WithMapping(base, "identity")
assert element == eval(repr(element))
5 changes: 3 additions & 2 deletions test/test_new_ad.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from ufl.classes import Grad
from ufl.algorithms import tree_format
from ufl.algorithms.renumbering import renumber_indices
from ufl.algorithms.apply_derivatives import apply_derivatives, GenericDerivativeRuleset, \
GradRuleset, VariableRuleset, GateauxDerivativeRuleset
from ufl.algorithms.apply_derivatives import (
apply_derivatives, GenericDerivativeRuleset,
GradRuleset, VariableRuleset, GateauxDerivativeRuleset)


# Note: the old tests in test_automatic_differentiation.py are a bit messy
Expand Down
3 changes: 1 addition & 2 deletions test/test_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
from ufl import *

from ufl.classes import MultiIndex, FixedIndex
from ufl.algorithms.signature import compute_multiindex_hashdata, \
compute_terminal_hashdata
from ufl.algorithms.signature import compute_multiindex_hashdata, compute_terminal_hashdata

from itertools import chain

Expand Down
8 changes: 1 addition & 7 deletions ufl/algorithms/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def compute_terminal_hashdata(expressions, renumbering):
# arguments, and just take repr of the rest of the terminals while
# we're iterating over them
terminal_hashdata = {}
labels = {}
index_numbering = {}
for expression in expressions:
for expr in traverse_unique_terminals(expression):
Expand All @@ -69,12 +68,7 @@ def compute_terminal_hashdata(expressions, renumbering):
data = expr._ufl_signature_data_(renumbering)

elif isinstance(expr, Label):
# Numbering labels as we visit them # TODO: Include in
# renumbering
data = labels.get(expr)
if data is None:
data = "L%d" % len(labels)
labels[expr] = data
data = expr._ufl_signature_data_(renumbering)

elif isinstance(expr, ExprList):
# Not really a terminal but can have 0 operands...
Expand Down
2 changes: 1 addition & 1 deletion ufl/argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def __eq__(self, other):
are the same ufl element but different dolfin function spaces.
"""
return (
type(self) == type(other) and self._number == other._number and # noqa: W504
type(self) is type(other) and self._number == other._number and # noqa: W504
self._part == other._part and self._ufl_function_space == other._ufl_function_space
)

Expand Down
4 changes: 2 additions & 2 deletions ufl/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def reconstruct(self, **kwargs: typing.Any) -> Cell:

def __lt__(self, other: AbstractCell) -> bool:
"""Define an arbitrarily chosen but fixed sort order for all cells."""
if type(self) == type(other):
if type(self) is type(other):
s = (self.geometric_dimension(), self.topological_dimension())
o = (other.geometric_dimension(), other.topological_dimension())
if s != o:
Expand Down Expand Up @@ -409,7 +409,7 @@ def reconstruct(self, **kwargs: typing.Any) -> Cell:
gdim = value
else:
raise TypeError(f"reconstruct() got unexpected keyword argument '{key}'")
return TensorProductCell(self._cellname, geometric_dimension=gdim)
return TensorProductCell(*self._cells, geometric_dimension=gdim)


def simplex(topological_dimension: int, geometric_dimension: typing.Optional[int] = None):
Expand Down
13 changes: 4 additions & 9 deletions ufl/coefficient.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,27 @@
from ufl.functionspace import AbstractFunctionSpace, FunctionSpace, MixedFunctionSpace
from ufl.form import BaseForm
from ufl.split_functions import split
from ufl.utils.counted import counted_init
from ufl.utils.counted import Counted
from ufl.duals import is_primal, is_dual

# --- The Coefficient class represents a coefficient in a form ---


class BaseCoefficient(object):
class BaseCoefficient(Counted):
"""UFL form argument type: Parent Representation of a form coefficient."""

# Slots are disabled here because they cause trouble in PyDOLFIN
# multiple inheritance pattern:
# __slots__ = ("_count", "_ufl_function_space", "_repr", "_ufl_shape")
_ufl_noslots_ = True
__slots__ = ()
_globalcount = 0
_ufl_is_abstract_ = True

def __getnewargs__(self):
return (self._ufl_function_space, self._count)

def __init__(self, function_space, count=None):
counted_init(self, count, Coefficient)
Counted.__init__(self, count, Coefficient)

if isinstance(function_space, FiniteElementBase):
# For legacy support for .ufl files using cells, we map
Expand All @@ -57,9 +56,6 @@ def __init__(self, function_space, count=None):
self._repr = "BaseCoefficient(%s, %s)" % (
repr(self._ufl_function_space), repr(self._count))

def count(self):
return self._count

@property
def ufl_shape(self):
"Return the associated UFL shape."
Expand Down Expand Up @@ -111,14 +107,14 @@ class Cofunction(BaseCoefficient, BaseForm):

__slots__ = (
"_count",
"_counted_class",
"_arguments",
"_ufl_function_space",
"ufl_operands",
"_repr",
"_ufl_shape",
"_hash"
)
# _globalcount = 0
_primal = False
_dual = True

Expand Down Expand Up @@ -161,7 +157,6 @@ class Coefficient(FormArgument, BaseCoefficient):
"""UFL form argument type: Representation of a form coefficient."""

_ufl_noslots_ = True
_globalcount = 0
_primal = True
_dual = False

Expand Down
10 changes: 3 additions & 7 deletions ufl/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@
from ufl.core.ufl_type import ufl_type
from ufl.core.terminal import Terminal
from ufl.domain import as_domain
from ufl.utils.counted import counted_init
from ufl.utils.counted import Counted


@ufl_type()
class Constant(Terminal):
class Constant(Terminal, Counted):
_ufl_noslots_ = True
_globalcount = 0

def __init__(self, domain, shape=(), count=None):
Terminal.__init__(self)
counted_init(self, count=count, countedclass=Constant)
Counted.__init__(self, count, Constant)

self._ufl_domain = as_domain(domain)
self._ufl_shape = shape
Expand All @@ -31,9 +30,6 @@ def __init__(self, domain, shape=(), count=None):
self._repr = "Constant({}, {}, {})".format(
repr(self._ufl_domain), repr(self._ufl_shape), repr(self._count))

def count(self):
return self._count

@property
def ufl_shape(self):
return self._ufl_shape
Expand Down
13 changes: 4 additions & 9 deletions ufl/core/multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Modified by Massimiliano Leoni, 2016.


from ufl.utils.counted import counted_init
from ufl.utils.counted import Counted
from ufl.core.ufl_type import ufl_type
from ufl.core.terminal import Terminal

Expand Down Expand Up @@ -70,20 +70,15 @@ def __repr__(self):
return r


class Index(IndexBase):
class Index(IndexBase, Counted):
"""UFL value: An index with no value assigned.
Used to represent free indices in Einstein indexing notation."""
__slots__ = ("_count",)

_globalcount = 0
__slots__ = ("_count", "_counted_class")

def __init__(self, count=None):
IndexBase.__init__(self)
counted_init(self, count, Index)

def count(self):
return self._count
Counted.__init__(self, count, Index)

def __hash__(self):
return hash(("Index", self._count))
Expand Down
4 changes: 2 additions & 2 deletions ufl/core/ufl_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __hash__(self) -> int:
return hash(self._ufl_hash_data_())

def __eq__(self, other):
return type(self) == type(other) and self._ufl_hash_data_() == other._ufl_hash_data_()
return type(self) is type(other) and self._ufl_hash_data_() == other._ufl_hash_data_()

def __ne__(self, other):
return not self.__eq__(other)
Expand All @@ -61,7 +61,7 @@ def __hash__(self):

def __eq__(self, other):
"__eq__ implementation attached in attach_operators_from_hash_data"
return type(self) == type(other) and self._ufl_hash_data_() == other._ufl_hash_data_()
return type(self) is type(other) and self._ufl_hash_data_() == other._ufl_hash_data_()
cls.__eq__ = __eq__

def __ne__(self, other):
Expand Down
2 changes: 1 addition & 1 deletion ufl/exprequals.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def expr_equals(self, other):

# Fast cutoffs for common cases, type difference or hash
# difference will cutoff more or less all nonequal types
if type(self) != type(other) or hash(self) != hash(other):
if type(self) is not type(other) or hash(self) != hash(other):
return False

# Large objects are costly to compare with themselves
Expand Down
2 changes: 1 addition & 1 deletion ufl/finiteelement/finiteelementbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __hash__(self):

def __eq__(self, other):
"Compute element equality for insertion in hashmaps."
return type(self) == type(other) and self._ufl_hash_data_() == other._ufl_hash_data_()
return type(self) is type(other) and self._ufl_hash_data_() == other._ufl_hash_data_()

def __ne__(self, other):
"Compute element inequality for insertion in hashmaps."
Expand Down
2 changes: 1 addition & 1 deletion ufl/finiteelement/hdivcurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def __getattr__(self, attr):
(type(self).__name__, attr))

def __repr__(self):
return f"WithMapping({repr(self.wrapee)}, {self._mapping})"
return f"WithMapping({repr(self.wrapee)}, '{self._mapping}')"

def value_shape(self):
gdim = self.cell().geometric_dimension()
Expand Down
Loading

0 comments on commit bee3cba

Please sign in to comment.