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

Fix Constant numbering #3011

Merged
merged 8 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ jobs:
--install icepack \
--install irksome \
--install femlium \
--package-branch ufl connorjward/counted-mixin2 \
connorjward marked this conversation as resolved.
Show resolved Hide resolved
|| (cat firedrake-install.log && /bin/false)
- name: Install test dependencies
run: |
Expand Down
21 changes: 14 additions & 7 deletions firedrake/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pyop2.exceptions import DataTypeError, DataValueError
from firedrake.petsc import PETSc
from firedrake.utils import ScalarType
from ufl.utils.counted import counted_init
from ufl.utils.counted import Counted


import firedrake.utils as utils
Expand All @@ -30,7 +30,7 @@ def _create_dat(op2type, value, comm):
return dat, rank, shape


class Constant(ufl.constantvalue.ConstantValue, ConstantMixin, TSFCConstantMixin):
class Constant(ufl.constantvalue.ConstantValue, ConstantMixin, TSFCConstantMixin, Counted):
"""A "constant" coefficient

A :class:`Constant` takes one value over the whole
Expand All @@ -53,9 +53,8 @@ class Constant(ufl.constantvalue.ConstantValue, ConstantMixin, TSFCConstantMixin
:class:`~ufl.form.Form` on its own you need to pass a
:func:`~.Mesh` as the domain argument.
"""
_globalcount = 0

def __new__(cls, value, domain=None):
def __new__(cls, value, domain=None, name=None, count=None):
if domain:
# Avoid circular import
from firedrake.function import Function
Expand Down Expand Up @@ -83,7 +82,7 @@ def __new__(cls, value, domain=None):
return object.__new__(cls)

@ConstantMixin._ad_annotate_init
def __init__(self, value, domain=None, name=None):
def __init__(self, value, domain=None, name=None, count=None):
# Init also called in mesh constructor, but constant can be built without mesh
utils._init()

Expand All @@ -93,12 +92,20 @@ def __init__(self, value, domain=None, name=None):
self.name = name or 'constant_%d' % self.uid

super().__init__()
counted_init(self, None, self.__class__)
self._hash = None
Counted.__init__(self, count)

def __repr__(self):
return f"Constant({self.dat.data_ro}, {self.count()})"

def _ufl_signature_data_(self, renumbering):
return (type(self).__name__, renumbering[self])

def __hash__(self):
return hash((type(self), self.count()))

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

@property
def ufl_shape(self):
return self._ufl_shape
Expand Down
5 changes: 4 additions & 1 deletion firedrake/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,7 @@ def hash_expr(expr):
"""
domain_numbering = {d: i for i, d in enumerate(ufl.domain.extract_domains(expr))}
coefficient_numbering = {c: i for i, c in enumerate(extract_coefficients(expr))}
return compute_expression_signature(expr, {**domain_numbering, **coefficient_numbering})
constant_numbering = {c: i for i, c in enumerate(extract_firedrake_constants(expr))}
return compute_expression_signature(
expr, {**domain_numbering, **coefficient_numbering, **constant_numbering}
)
9 changes: 9 additions & 0 deletions tests/regression/test_constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,12 @@ def test_fresh_constant_hashes_different():
d = Constant(1)

assert hash(c) != hash(d)


def test_constants_are_renumbered_in_form_signature():
mesh = UnitSquareMesh(1, 1)
c = Constant(1)
d = Constant(1)

assert c.count() != d.count()
assert (c*dx(domain=mesh)).signature() == (d*dx(domain=mesh)).signature()