Skip to content

Commit

Permalink
u.sub(0) should still be in a ComponentFunctionSpace if the vecto…
Browse files Browse the repository at this point in the history
…r is 1 dimensional (#3902)

Co-authored-by: Connor Ward <c.ward20@imperial.ac.uk>
  • Loading branch information
JHopeCollins and connorjward authored Dec 3, 2024
1 parent 4c93354 commit 530243e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
36 changes: 23 additions & 13 deletions firedrake/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,16 @@ def split(self):

@utils.cached_property
def _components(self):
if self.dof_dset.cdim == 1:
return (self, )
if self.function_space().rank == 0:
return tuple((self, ))
else:
return tuple(CoordinatelessFunction(self.function_space().sub(i), val=op2.DatView(self.dat, j),
name="view[%d](%s)" % (i, self.name()))
for i, j in enumerate(np.ndindex(self.dof_dset.dim)))
if self.dof_dset.cdim == 1:
return (CoordinatelessFunction(self.function_space().sub(0), val=self.dat,
name=f"view[0]({self.name()})"),)
else:
return tuple(CoordinatelessFunction(self.function_space().sub(i), val=op2.DatView(self.dat, j),
name=f"view[{i}]({self.name()})")
for i, j in enumerate(np.ndindex(self.dof_dset.dim)))

@PETSc.Log.EventDecorator()
def sub(self, i):
Expand All @@ -143,9 +147,12 @@ def sub(self, i):
rank-n :class:`~.FunctionSpace`, this returns a proxy object
indexing the ith component of the space, suitable for use in
boundary condition application."""
if len(self.function_space()) == 1:
return self._components[i]
return self.subfunctions[i]
mixed = len(self.function_space()) != 1
data = self.subfunctions if mixed else self._components
bound = len(data)
if i < 0 or i >= bound:
raise IndexError(f"Invalid component {i}, not in [0, {bound})")
return data[i]

@property
def cell_set(self):
Expand Down Expand Up @@ -327,8 +334,8 @@ def split(self):

@utils.cached_property
def _components(self):
if self.function_space().block_size == 1:
return (self, )
if self.function_space().rank == 0:
return tuple((self, ))
else:
return tuple(type(self)(self.function_space().sub(i), self.topological.sub(i))
for i in range(self.function_space().block_size))
Expand All @@ -345,9 +352,12 @@ def sub(self, i):
:func:`~.VectorFunctionSpace` or :func:`~.TensorFunctionSpace` this returns a proxy object
indexing the ith component of the space, suitable for use in
boundary condition application."""
if len(self.function_space()) == 1:
return self._components[i]
return self.subfunctions[i]
mixed = len(self.function_space()) != 1
data = self.subfunctions if mixed else self._components
bound = len(data)
if i < 0 or i >= bound:
raise IndexError(f"Invalid component {i}, not in [0, {bound})")
return data[i]

@PETSc.Log.EventDecorator()
@FunctionMixin._ad_annotate_project
Expand Down
19 changes: 12 additions & 7 deletions firedrake/functionspaceimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,12 @@ def _components(self):

@PETSc.Log.EventDecorator()
def sub(self, i):
bound = len(self._components)
mixed = len(self) != 1
data = self.subfunctions if mixed else self._components
bound = len(data)
if i < 0 or i >= bound:
raise IndexError("Invalid component %d, not in [0, %d)" % (i, bound))
return self._components[i]
raise IndexError(f"Invalid component {i}, not in [0, {bound})")
return data[i]

@utils.cached_property
def dm(self):
Expand Down Expand Up @@ -654,13 +656,16 @@ def __getitem__(self, i):

@utils.cached_property
def _components(self):
return tuple(ComponentFunctionSpace(self, i) for i in range(self.block_size))
if self.rank == 0:
return self.subfunctions
else:
return tuple(ComponentFunctionSpace(self, i) for i in range(self.block_size))

def sub(self, i):
r"""Return a view into the ith component."""
if self.rank == 0:
assert i == 0
return self
bound = len(self._components)
if i < 0 or i >= bound:
raise IndexError(f"Invalid component {i}, not in [0, {bound})")
return self._components[i]

def __mul__(self, other):
Expand Down

0 comments on commit 530243e

Please sign in to comment.