Skip to content

Commit

Permalink
Remove deprecated fem.FunctionSpace function (#2818)
Browse files Browse the repository at this point in the history
* FunctionSpace -> functionspace

* Run isort

* FunctionSpaceBase -> FunctionSpace
  • Loading branch information
francesco-ballarin authored Oct 16, 2023
1 parent adde58a commit a34b0c9
Show file tree
Hide file tree
Showing 50 changed files with 404 additions and 461 deletions.
2 changes: 1 addition & 1 deletion python/demo/demo_biharmonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
# We begin by using {py:func}`create_rectangle
# <dolfinx.mesh.create_rectangle>` to create a rectangular
# {py:class}`Mesh <dolfinx.mesh.Mesh>` of the domain, and creating a
# finite element {py:class}`FunctionSpaceBase <dolfinx.fem.FunctionSpaceBase>`
# finite element {py:class}`FunctionSpace <dolfinx.fem.FunctionSpace>`
# $V$ on the mesh.

msh = mesh.create_rectangle(comm=MPI.COMM_WORLD,
Expand Down
2 changes: 1 addition & 1 deletion python/demo/demo_cahn-hilliard.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@

# A unit square mesh with 96 cells edges in each direction is created,
# and on this mesh a
# {py:class}`FunctionSpaceBase <dolfinx.fem.FunctionSpaceBase>` `ME` is built
# {py:class}`FunctionSpace <dolfinx.fem.FunctionSpace>` `ME` is built
# using a pair of linear Lagrange elements.

msh = create_unit_square(MPI.COMM_WORLD, 96, 96, CellType.triangle)
Expand Down
4 changes: 2 additions & 2 deletions python/demo/demo_elasticity.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import dolfinx
import ufl
from dolfinx import la
from dolfinx.fem import (Expression, Function, FunctionSpaceBase, dirichletbc,
from dolfinx.fem import (Expression, Function, FunctionSpace, dirichletbc,
form, functionspace, locate_dofs_topological)
from dolfinx.fem.petsc import (apply_lifting, assemble_matrix, assemble_vector,
set_bc)
Expand All @@ -53,7 +53,7 @@
# modes.


def build_nullspace(V: FunctionSpaceBase):
def build_nullspace(V: FunctionSpace):
"""Build PETSc nullspace for 3D elasticity"""

# Create vectors that will span the nullspace
Expand Down
4 changes: 2 additions & 2 deletions python/demo/demo_poisson.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# This demo is implemented in {download}`demo_poisson.py`. It
# illustrates how to:
#
# - Create a {py:class}`function space <dolfinx.fem.FunctionSpaceBase>`
# - Create a {py:class}`function space <dolfinx.fem.FunctionSpace>`
# - Solve a linear partial differential equation
#
# ## Equation and problem definition
Expand Down Expand Up @@ -76,7 +76,7 @@
# We create a rectangular {py:class}`Mesh <dolfinx.mesh.Mesh>` using
# {py:func}`create_rectangle <dolfinx.mesh.create_rectangle>`, and
# create a finite element {py:class}`function space
# <dolfinx.fem.FunctionSpaceBase>` $V$ on the mesh.
# <dolfinx.fem.FunctionSpace>` $V$ on the mesh.

# +
msh = mesh.create_rectangle(comm=MPI.COMM_WORLD,
Expand Down
2 changes: 1 addition & 1 deletion python/demo/demo_stokes.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def lid_velocity_expression(x):
return np.stack((np.ones(x.shape[1]), np.zeros(x.shape[1])))
# -

# Two {py:class}`function spaces <dolfinx.fem.FunctionSpaceBase>` are
# Two {py:class}`function spaces <dolfinx.fem.FunctionSpace>` are
# defined using different finite elements. `P2` corresponds to a
# continuous piecewise quadratic basis (vector) and `P1` to a continuous
# piecewise linear basis (scalar).
Expand Down
2 changes: 1 addition & 1 deletion python/demo/demo_tnt-elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def create_tnt_quad(degree):
# the solution.


def poisson_error(V: fem.FunctionSpaceBase):
def poisson_error(V: fem.FunctionSpace):
msh = V.mesh
u, v = TrialFunction(V), TestFunction(V)

Expand Down
6 changes: 2 additions & 4 deletions python/dolfinx/fem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
from dolfinx.fem.dofmap import DofMap
from dolfinx.fem.forms import Form, extract_function_spaces, form
from dolfinx.fem.function import (Constant, ElementMetaData, Expression,
Function, FunctionSpace, FunctionSpaceBase,
VectorFunctionSpace, functionspace)
Function, FunctionSpace, functionspace)


def create_sparsity_pattern(a: Form):
Expand All @@ -40,8 +39,7 @@ def create_sparsity_pattern(a: Form):

__all__ = [
"Constant", "Expression", "Function", "ElementMetaData", "create_matrix",
"functionspace", "FunctionSpace", "FunctionSpaceBase", "VectorFunctionSpace",
"create_sparsity_pattern",
"functionspace", "FunctionSpace", "create_sparsity_pattern",
"assemble_scalar", "assemble_matrix", "assemble_vector", "apply_lifting", "set_bc",
"DirichletBC", "dirichletbc", "bcs_by_block", "DofMap", "Form",
"form", "IntegralType", "create_vector",
Expand Down
14 changes: 7 additions & 7 deletions python/dolfinx/fem/bcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from dolfinx import cpp as _cpp


def locate_dofs_geometrical(V: typing.Union[dolfinx.fem.FunctionSpaceBase,
typing.Iterable[dolfinx.fem.FunctionSpaceBase]],
def locate_dofs_geometrical(V: typing.Union[dolfinx.fem.FunctionSpace,
typing.Iterable[dolfinx.fem.FunctionSpace]],
marker: typing.Callable) -> np.ndarray:
"""Locate degrees-of-freedom geometrically using a marker function.
Expand Down Expand Up @@ -54,8 +54,8 @@ def locate_dofs_geometrical(V: typing.Union[dolfinx.fem.FunctionSpaceBase,
return _cpp.fem.locate_dofs_geometrical(_V, marker)


def locate_dofs_topological(V: typing.Union[dolfinx.fem.FunctionSpaceBase,
typing.Iterable[dolfinx.fem.FunctionSpaceBase]],
def locate_dofs_topological(V: typing.Union[dolfinx.fem.FunctionSpace,
typing.Iterable[dolfinx.fem.FunctionSpace]],
entity_dim: int, entities: numpy.typing.NDArray[np.int32],
remote: bool = True) -> np.ndarray:
"""Locate degrees-of-freedom belonging to mesh entities topologically.
Expand Down Expand Up @@ -116,14 +116,14 @@ def g(self) -> typing.Union[Function, Constant, np.ndarray]:
return self._cpp_object.value

@property
def function_space(self) -> dolfinx.fem.FunctionSpaceBase:
def function_space(self) -> dolfinx.fem.FunctionSpace:
"""The function space on which the boundary condition is defined"""
return self._cpp_object.function_space


def dirichletbc(value: typing.Union[Function, Constant, np.ndarray],
dofs: numpy.typing.NDArray[np.int32],
V: typing.Optional[dolfinx.fem.FunctionSpaceBase] = None) -> DirichletBC:
V: typing.Optional[dolfinx.fem.FunctionSpace] = None) -> DirichletBC:
"""Create a representation of Dirichlet boundary condition which
is imposed on a linear system.
Expand Down Expand Up @@ -181,7 +181,7 @@ def dirichletbc(value: typing.Union[Function, Constant, np.ndarray],
return DirichletBC(bc)


def bcs_by_block(spaces: typing.Iterable[typing.Union[dolfinx.fem.FunctionSpaceBase, None]],
def bcs_by_block(spaces: typing.Iterable[typing.Union[dolfinx.fem.FunctionSpace, None]],
bcs: typing.Iterable[DirichletBC]) -> typing.List[typing.List[DirichletBC]]:
"""Arrange Dirichlet boundary conditions by the function space that
they constrain.
Expand Down
6 changes: 3 additions & 3 deletions python/dolfinx/fem/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from dolfinx import cpp as _cpp
from dolfinx import default_scalar_type, jit
from dolfinx.fem import IntegralType
from dolfinx.fem.function import FunctionSpaceBase
from dolfinx.fem.function import FunctionSpace

if typing.TYPE_CHECKING:
from dolfinx.fem import function
Expand Down Expand Up @@ -59,7 +59,7 @@ def rank(self) -> int:
return self._cpp_object.rank

@property
def function_spaces(self) -> typing.List[FunctionSpaceBase]:
def function_spaces(self) -> typing.List[FunctionSpace]:
"""Function spaces on which this form is defined"""
return self._cpp_object.function_spaces

Expand Down Expand Up @@ -190,7 +190,7 @@ def _create_form(form):

def extract_function_spaces(forms: typing.Union[typing.Iterable[Form], # type: ignore [return]
typing.Iterable[typing.Iterable[Form]]],
index: int = 0) -> typing.Iterable[typing.Union[None, function.FunctionSpaceBase]]:
index: int = 0) -> typing.Iterable[typing.Union[None, function.FunctionSpace]]:
"""Extract common function spaces from an array of forms. If `forms`
is a list of linear form, this function returns of list of the
corresponding test functions. If `forms` is a 2D array of bilinear
Expand Down
81 changes: 13 additions & 68 deletions python/dolfinx/fem/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from __future__ import annotations

import typing
import warnings
from functools import singledispatch

import numpy as np
Expand Down Expand Up @@ -231,7 +230,7 @@ def value_size(self) -> int:
return self._cpp_object.value_size

@property
def argument_function_space(self) -> typing.Optional[FunctionSpaceBase]:
def argument_function_space(self) -> typing.Optional[FunctionSpace]:
"""The argument function space if expression has argument"""
return self._argument_function_space

Expand All @@ -257,7 +256,7 @@ class Function(ufl.Coefficient):
"""

def __init__(self, V: FunctionSpaceBase, x: typing.Optional[la.Vector] = None,
def __init__(self, V: FunctionSpace, x: typing.Optional[la.Vector] = None,
name: typing.Optional[str] = None, dtype: typing.Optional[npt.DTypeLike] = None):
"""Initialize a finite element Function.
Expand Down Expand Up @@ -318,7 +317,7 @@ def __del__(self):
self._petsc_x.destroy()

@property
def function_space(self) -> FunctionSpaceBase:
def function_space(self) -> FunctionSpace:
"""The FunctionSpace that the Function is defined on"""
return self._V

Expand Down Expand Up @@ -478,7 +477,7 @@ def split(self) -> tuple[Function, ...]:

def collapse(self) -> Function:
u_collapsed = self._cpp_object.collapse()
V_collapsed = FunctionSpaceBase(self.function_space._mesh, self.ufl_element(), u_collapsed.function_space)
V_collapsed = FunctionSpace(self.function_space._mesh, self.ufl_element(), u_collapsed.function_space)
return Function(V_collapsed, la.Vector(u_collapsed.x))


Expand All @@ -502,7 +501,7 @@ def functionspace(mesh: Mesh,
element: typing.Union[ufl.FiniteElementBase, ElementMetaData,
typing.Tuple[str, int, typing.Tuple, bool]],
form_compiler_options: typing.Optional[dict[str, typing.Any]] = None,
jit_options: typing.Optional[dict[str, typing.Any]] = None) -> FunctionSpaceBase:
jit_options: typing.Optional[dict[str, typing.Any]] = None) -> FunctionSpace:
"""Create a finite element function space.
Args:
Expand Down Expand Up @@ -555,33 +554,10 @@ def functionspace(mesh: Mesh,
except TypeError:
cppV = _cpp.fem.FunctionSpace_float32(mesh._cpp_object, cpp_element, cpp_dofmap)

return FunctionSpaceBase(mesh, ufl_e, cppV)
return FunctionSpace(mesh, ufl_e, cppV)


def FunctionSpace(mesh: Mesh,
element: typing.Union[ufl.FiniteElementBase, ElementMetaData,
typing.Tuple[str, int, typing.Tuple, bool]],
form_compiler_options: typing.Optional[dict[str, typing.Any]] = None,
jit_options: typing.Optional[dict[str, typing.Any]] = None) -> FunctionSpaceBase:
"""Create a finite element function space.
.. deprecated:: 0.7
Use :func:`functionspace` (no caps) instead.
Args:
mesh: Mesh that space is defined on
element: Finite element description
form_compiler_options: Options passed to the form compiler
jit_options: Options controlling just-in-time compilation
Returns:
A function space.
"""
return functionspace(mesh, element, form_compiler_options, jit_options)


class FunctionSpaceBase(ufl.FunctionSpace):
class FunctionSpace(ufl.FunctionSpace):
"""A space on which Functions (fields) can be defined."""

def __init__(self, mesh: Mesh, element: ufl.FiniteElementBase,
Expand All @@ -605,7 +581,7 @@ def __init__(self, mesh: Mesh, element: ufl.FiniteElementBase,
self._mesh = mesh
super().__init__(ufl_domain, element)

def clone(self) -> FunctionSpaceBase:
def clone(self) -> FunctionSpace:
"""Create a new FunctionSpace :math:`W` which shares data with this
FunctionSpace :math:`V`, but with a different unique integer ID.
Expand All @@ -628,14 +604,14 @@ def clone(self) -> FunctionSpaceBase:
except TypeError:
Vcpp = _cpp.fem.FunctionSpace_float32(
self._cpp_object.mesh, self._cpp_object.element, self._cpp_object.dofmap)
return FunctionSpaceBase(self._mesh, self.ufl_element(), Vcpp)
return FunctionSpace(self._mesh, self.ufl_element(), Vcpp)

@property
def num_sub_spaces(self) -> int:
"""Number of sub spaces."""
return self.element.num_sub_elements

def sub(self, i: int) -> FunctionSpaceBase:
def sub(self, i: int) -> FunctionSpace:
"""Return the i-th sub space.
Args:
Expand All @@ -648,7 +624,7 @@ def sub(self, i: int) -> FunctionSpaceBase:
assert self.ufl_element().num_sub_elements > i
sub_element = self.ufl_element().sub_elements[i]
cppV_sub = self._cpp_object.sub([i])
return FunctionSpaceBase(self._mesh, sub_element, cppV_sub)
return FunctionSpace(self._mesh, sub_element, cppV_sub)

def component(self):
"""Return the component relative to the parent space."""
Expand Down Expand Up @@ -694,7 +670,7 @@ def mesh(self) -> Mesh:
"""Mesh on which the function space is defined."""
return self._mesh

def collapse(self) -> tuple[FunctionSpaceBase, np.ndarray]:
def collapse(self) -> tuple[FunctionSpace, np.ndarray]:
"""Collapse a subspace and return a new function space and a map
from new to old dofs.
Expand All @@ -704,7 +680,7 @@ def collapse(self) -> tuple[FunctionSpaceBase, np.ndarray]:
"""
cpp_space, dofs = self._cpp_object.collapse()
V = FunctionSpaceBase(self._mesh, self.ufl_element(), cpp_space)
V = FunctionSpace(self._mesh, self.ufl_element(), cpp_space)
return V, dofs

def tabulate_dof_coordinates(self) -> npt.NDArray[np.float64]:
Expand All @@ -719,34 +695,3 @@ def tabulate_dof_coordinates(self) -> npt.NDArray[np.float64]:
"""
return self._cpp_object.tabulate_dof_coordinates()


def VectorFunctionSpace(mesh: Mesh,
element: typing.Union[ElementMetaData, typing.Tuple[str, int]],
dim: typing.Optional[int] = None) -> FunctionSpaceBase:
"""Create a vector finite element (composition of scalar elements) function space.
.. deprecated:: 0.7
Use :func:`FunctionSpace` with a shape argument instead.
Args:
mesh: Mesh that space is defined on
element: Finite element description. Must be a scalar element,
e.g. Lagrange.
dim: Dimension of the vector, e.g. number of vector components.
It defaults to the geometric dimension of the mesh.
Returns:
A blocked vector function space.
"""
warnings.warn('This method is deprecated. Use FunctionSpace with an element shape argument instead',
DeprecationWarning, stacklevel=2)
ed = ElementMetaData(*element)
e = basix.ufl.element(ed.family, mesh.basix_cell(), ed.degree, gdim=mesh.geometry.dim)
if len(e.value_shape) != 0:
raise ValueError("Cannot create vector element containing a non-scalar.")
ufl_e = basix.ufl.element(ed.family, mesh.basix_cell(), ed.degree,
shape=(mesh.geometry.dim,) if dim is None else (dim,),
gdim=mesh.geometry.dim)
return FunctionSpace(mesh, ufl_e)
1 change: 0 additions & 1 deletion python/dolfinx/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from __future__ import annotations


import typing

import numpy as np
Expand Down
4 changes: 2 additions & 2 deletions python/dolfinx/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def vtk_mesh(msh: mesh.Mesh, dim: typing.Optional[int] = None, entities=None):
return topology.reshape(-1), cell_types, msh.geometry.x


@vtk_mesh.register(fem.FunctionSpaceBase)
def _(V: fem.FunctionSpaceBase, entities=None):
@vtk_mesh.register(fem.FunctionSpace)
def _(V: fem.FunctionSpace, entities=None):
"""Creates a VTK mesh topology (topology array and array of cell
types) that is based on the degree-of-freedom coordinates.
Expand Down
8 changes: 4 additions & 4 deletions python/test/unit/fem/test_assemble_cppimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import dolfinx.pkgconfig
import ufl
from dolfinx.fem import (FunctionSpace, assemble_matrix, dirichletbc, form,
from dolfinx.fem import (assemble_matrix, dirichletbc, form, functionspace,
locate_dofs_geometrical)
from dolfinx.mesh import create_unit_square
from dolfinx.wrappers import get_include_path as pybind_inc
Expand Down Expand Up @@ -120,7 +120,7 @@ def assemble_csr_matrix(a, bcs):

realtype = np.real(dtype(1.0)).dtype
mesh = create_unit_square(MPI.COMM_SELF, 12, 12, dtype=realtype)
Q = FunctionSpace(mesh, ("Lagrange", 1))
Q = functionspace(mesh, ("Lagrange", 1))
u = ufl.TrialFunction(Q)
v = ufl.TestFunction(Q)

Expand Down Expand Up @@ -201,8 +201,8 @@ def compile_assemble_csr_assembler_module(a_form):

mesh = create_unit_square(MPI.COMM_SELF, 11, 7)
gdim = mesh.geometry.dim
Q = FunctionSpace(mesh, ("Lagrange", 1, (gdim,)))
Q2 = FunctionSpace(mesh, ("Lagrange", 1, (3,)))
Q = functionspace(mesh, ("Lagrange", 1, (gdim,)))
Q2 = functionspace(mesh, ("Lagrange", 1, (3,)))
u = ufl.TrialFunction(Q)
v = ufl.TestFunction(Q2)

Expand Down
Loading

0 comments on commit a34b0c9

Please sign in to comment.