Create a finite element function space. Args: mesh: Mesh that space is defined on. element: Finite element description. Returns: A function space.
(
mesh: Mesh,
element: (
ufl.finiteelement.AbstractFiniteElement
| ElementMetaData
| tuple[str, int]
| tuple[str, int, tuple]
| tuple[str, int, tuple, bool]
),
)
| 643 | |
| 644 | |
| 645 | def functionspace( |
| 646 | mesh: Mesh, |
| 647 | element: ( |
| 648 | ufl.finiteelement.AbstractFiniteElement |
| 649 | | ElementMetaData |
| 650 | | tuple[str, int] |
| 651 | | tuple[str, int, tuple] |
| 652 | | tuple[str, int, tuple, bool] |
| 653 | ), |
| 654 | ) -> FunctionSpace: |
| 655 | """Create a finite element function space. |
| 656 | |
| 657 | Args: |
| 658 | mesh: Mesh that space is defined on. |
| 659 | element: Finite element description. |
| 660 | |
| 661 | Returns: |
| 662 | A function space. |
| 663 | """ |
| 664 | # Create UFL element |
| 665 | dtype = mesh.geometry.x.dtype |
| 666 | try: |
| 667 | e = ElementMetaData(*element) # type: ignore |
| 668 | ufl_e = basix.ufl.element( |
| 669 | e.family, |
| 670 | mesh.basix_cell(), # type: ignore |
| 671 | e.degree, |
| 672 | shape=e.shape, |
| 673 | symmetry=e.symmetry, |
| 674 | dtype=dtype, |
| 675 | ) |
| 676 | except TypeError: |
| 677 | ufl_e = element # type: ignore |
| 678 | |
| 679 | # Check that element and mesh cell types match |
| 680 | if ((domain := mesh.ufl_domain()) is None) or ufl_e.cell != domain.ufl_cell(): |
| 681 | raise ValueError("Non-matching UFL cell and mesh cell shapes.") |
| 682 | # Create DOLFINx objects |
| 683 | element = finiteelement(mesh.topology.cell_type, ufl_e, dtype) # type: ignore |
| 684 | |
| 685 | if ufl_e.is_real: |
| 686 | cpp_dofmap = _cpp.fem.build_real_element_dofmap( |
| 687 | mesh.topology._cpp_object, |
| 688 | element.basix_element.entity_dofs, # type: ignore |
| 689 | element.basix_element.entity_closure_dofs, # type: ignore |
| 690 | int(np.prod(element.value_shape)), # type: ignore |
| 691 | ) |
| 692 | else: |
| 693 | cpp_dofmap = _cpp.fem.create_dofmap( |
| 694 | mesh.comm, |
| 695 | mesh.topology._cpp_object, |
| 696 | element._cpp_object, # type: ignore |
| 697 | ) |
| 698 | assert np.issubdtype(mesh.geometry.x.dtype, element.dtype), ( # type: ignore |
| 699 | "Mesh and element dtype are not compatible." |
| 700 | ) |
| 701 | |
| 702 | # Initialize the cpp.FunctionSpace |