Skip to content

Commit

Permalink
Move FiniteElement.components to FunctionSpace.components (#307)
Browse files Browse the repository at this point in the history
* missing reference_

* remove IPython embed

* remove components property

* ruff

* move components to function space

* import numpy as np

---------

Co-authored-by: Jørgen Schartum Dokken <jsd55@cam.ac.uk>
  • Loading branch information
mscroggs and jorgensd authored Oct 2, 2024
1 parent bde4f2f commit cfd864e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
6 changes: 3 additions & 3 deletions ufl/algorithms/expand_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def form_argument(self, x):
if sh == ():
return x
else:
e = x.ufl_element()
space = x.ufl_function_space()
r = len(sh)

# Get component
Expand All @@ -58,8 +58,8 @@ def form_argument(self, x):
raise ValueError("Component size mismatch.")

# Map it through an eventual symmetry mapping
if len(e.components) > 1:
c = min(i for i, j in e.components.items() if j == e.components[c])
if len(space.components) > 1:
c = min(i for i, j in space.components.items() if j == space.components[c])
if r != len(c):
raise ValueError("Component size mismatch after symmetry mapping.")

Expand Down
26 changes: 0 additions & 26 deletions ufl/finiteelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import abc as _abc
import typing as _typing

import numpy as np

from ufl.cell import Cell as _Cell
from ufl.pullback import AbstractPullback as _AbstractPullback
from ufl.pullback import IdentityPullback as _IdentityPullback
Expand Down Expand Up @@ -130,30 +128,6 @@ def _ufl_signature_data_(self) -> str:
"""Return UFL signature data."""
return repr(self)

@property
def components(self) -> _typing.Dict[_typing.Tuple[int, ...], int]:
"""Get the numbering of the components of the element.
Returns:
A map from the components of the values on a physical cell (eg (0, 1))
to flat component numbers on the reference cell (eg 1)
"""
if isinstance(self.pullback, _SymmetricPullback):
return self.pullback._symmetry

if len(self.sub_elements) == 0:
return {(): 0}

components = {}
offset = 0
c_offset = 0
for e in self.sub_elements:
for i, j in enumerate(np.ndindex(e.reference_value_shape)):
components[(offset + i,)] = c_offset + e.components[j]
c_offset += max(e.components.values()) + 1
offset += e.value_size
return components

@property
def reference_value_size(self) -> int:
"""Return the integer product of the reference value shape."""
Expand Down
28 changes: 28 additions & 0 deletions ufl/functionspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import typing

import numpy as np

from ufl.core.ufl_type import UFLObject
from ufl.domain import join_domains
from ufl.duals import is_dual, is_primal
Expand Down Expand Up @@ -60,6 +62,32 @@ def __init__(self, domain, element, label=""):
self._ufl_domain = domain
self._ufl_element = element

@property
def components(self) -> typing.Dict[typing.Tuple[int, ...], int]:
"""Get the numbering of the components of the element of this space.
Returns:
A map from the components of the values on a physical cell (eg (0, 1))
to flat component numbers on the reference cell (eg 1)
"""
from ufl.pullback import SymmetricPullback

if isinstance(self._ufl_element.pullback, SymmetricPullback):
return self._ufl_element.pullback._symmetry

if len(self._ufl_element.sub_elements) == 0:
return {(): 0}

components = {}
offset = 0
c_offset = 0
for s in self.ufl_sub_spaces():
for i, j in enumerate(np.ndindex(s.value_shape)):
components[(offset + i,)] = c_offset + s.components[j]
c_offset += max(s.components.values()) + 1
offset += s.value_size
return components

def label(self):
"""Return label of boundary domains to differentiate restricted and unrestricted."""
return self._label
Expand Down

0 comments on commit cfd864e

Please sign in to comment.