-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dokken/form-split-no-arguments
- Loading branch information
Showing
3 changed files
with
103 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,57 @@ | ||
"""Algorithms for renumbering of counted objects, currently variables and indices.""" | ||
# Copyright (C) 2008-2016 Martin Sandve Alnæs and Anders Logg | ||
# Copyright (C) 2008-2024 Martin Sandve Alnæs, Anders Logg, Jørgen S. Dokken and Lawrence Mitchell | ||
# | ||
# This file is part of UFL (https://www.fenicsproject.org) | ||
# | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
||
from ufl.algorithms.transformer import ReuseTransformer, apply_transformer | ||
from ufl.classes import Zero | ||
from ufl.core.expr import Expr | ||
from ufl.core.multiindex import FixedIndex, Index, MultiIndex | ||
from ufl.variable import Label, Variable | ||
from collections import defaultdict | ||
from itertools import count as _count | ||
|
||
from ufl.algorithms.map_integrands import map_integrand_dags | ||
from ufl.core.multiindex import Index | ||
from ufl.corealg.multifunction import MultiFunction | ||
|
||
class VariableRenumberingTransformer(ReuseTransformer): | ||
"""Variable renumbering transformer.""" | ||
|
||
def __init__(self): | ||
"""Initialise.""" | ||
ReuseTransformer.__init__(self) | ||
self.variable_map = {} | ||
|
||
def variable(self, o): | ||
"""Apply to variable.""" | ||
e, l = o.ufl_operands # noqa: E741 | ||
v = self.variable_map.get(l) | ||
if v is None: | ||
e = self.visit(e) | ||
l2 = Label(len(self.variable_map)) | ||
v = Variable(e, l2) | ||
self.variable_map[l] = v | ||
return v | ||
|
||
class IndexRelabeller(MultiFunction): | ||
"""Renumber indices to have a consistent index numbering starting from 0.""" | ||
|
||
class IndexRenumberingTransformer(VariableRenumberingTransformer): | ||
"""Index renumbering transformer. | ||
def __init__(self): | ||
"""Initialize index relabeller with a zero count.""" | ||
super().__init__() | ||
count = _count() | ||
self.index_cache = defaultdict(lambda: Index(next(count))) | ||
|
||
This is a poorly designed algorithm. It is used in some tests, | ||
please do not use for anything else. | ||
""" | ||
expr = MultiFunction.reuse_if_untouched | ||
|
||
def __init__(self): | ||
"""Initialise.""" | ||
VariableRenumberingTransformer.__init__(self) | ||
self.index_map = {} | ||
def multi_index(self, o): | ||
"""Apply to multi-indices.""" | ||
return type(o)( | ||
tuple(self.index_cache[i] if isinstance(i, Index) else i for i in o.indices()) | ||
) | ||
|
||
def zero(self, o): | ||
"""Apply to zero.""" | ||
fi = o.ufl_free_indices | ||
fid = o.ufl_index_dimensions | ||
mapped_fi = tuple(self.index(Index(count=i)) for i in fi) | ||
paired_fid = [(mapped_fi[pos], fid[pos]) for pos, a in enumerate(fi)] | ||
new_fi, new_fid = zip(*tuple(sorted(paired_fid))) | ||
return Zero(o.ufl_shape, new_fi, new_fid) | ||
|
||
def index(self, o): | ||
"""Apply to index.""" | ||
if isinstance(o, FixedIndex): | ||
new_indices = [self.index_cache[Index(i)].count() for i in fi] | ||
if fi == () and fid == (): | ||
return o | ||
else: | ||
c = o._count | ||
i = self.index_map.get(c) | ||
if i is None: | ||
i = Index(count=len(self.index_map)) | ||
self.index_map[c] = i | ||
return i | ||
new_fi, new_fid = zip(*sorted(zip(new_indices, fid), key=lambda x: x[0])) | ||
return type(o)(o.ufl_shape, tuple(new_fi), tuple(new_fid)) | ||
|
||
def multi_index(self, o): | ||
"""Apply to multi_index.""" | ||
new_indices = tuple(self.index(i) for i in o.indices()) | ||
return MultiIndex(new_indices) | ||
|
||
def renumber_indices(form): | ||
"""Renumber indices to have a consistent index numbering starting from 0. | ||
def renumber_indices(expr): | ||
"""Renumber indices.""" | ||
if isinstance(expr, Expr): | ||
num_free_indices = len(expr.ufl_free_indices) | ||
This is useful to avoid multiple kernels for the same integrand, | ||
but with different subdomain ids. | ||
result = apply_transformer(expr, IndexRenumberingTransformer()) | ||
Args: | ||
form: A UFL form, integral or expression. | ||
if isinstance(expr, Expr): | ||
if num_free_indices != len(result.ufl_free_indices): | ||
raise ValueError( | ||
"The number of free indices left in expression " | ||
"should be invariant w.r.t. renumbering." | ||
) | ||
return result | ||
Returns: | ||
A new form, integral or expression with renumbered indices. | ||
""" | ||
reindexer = IndexRelabeller() | ||
return map_integrand_dags(reindexer, form) |