DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
functionspace_factory.h
Go to the documentation of this file.
1// Copyright (C) 2013-2026 Johan Hake, Jan Blechta, Garth N. Wells and Paul T.
2// Kühner
3//
4// This file is part of DOLFINx (https://www.fenicsproject.org)
5//
6// SPDX-License-Identifier: LGPL-3.0-or-later
7
8#pragma once
9
10#include "DofMap.h"
11#include "ElementDofLayout.h"
12#include "FiniteElement.h"
13#include "FunctionSpace.h"
14#include <algorithm>
15#include <concepts>
16#include <cstdint>
17#include <dolfinx/common/IndexMap.h>
18#include <dolfinx/graph/AdjacencyList.h>
19#include <dolfinx/mesh/Mesh.h>
20#include <dolfinx/mesh/Topology.h>
21#include <functional>
22#include <memory>
23#include <mpi.h>
24#include <span>
25#include <stdexcept>
26#include <utility>
27#include <vector>
28
31
32namespace dolfinx::fem
33{
35template <std::floating_point T>
37 const std::vector<int>& parent_map
38 = {})
39{
40 // Create subdofmaps and compute offset
41 std::vector<int> offsets(1, 0);
42 std::vector<dolfinx::fem::ElementDofLayout> sub_doflayout;
43 int bs = element.block_size();
44 for (int i = 0; i < element.num_sub_elements(); ++i)
45 {
46 // The ith sub-element. For mixed elements this is subelements()[i]. For
47 // blocked elements, the sub-element will always be the same, so we'll use
48 // sub_elements()[0]
49 std::shared_ptr<const fem::FiniteElement<T>> sub_e
50 = element.sub_elements()[bs > 1 ? 0 : i];
51
52 // In a mixed element DOFs are ordered element by element, so the offset to
53 // the next sub-element is sub_e->space_dimension(). Blocked elements use
54 // xxyyzz ordering, so the offset to the next sub-element is 1
55
56 std::vector<int> parent_map_sub(sub_e->space_dimension(), offsets.back());
57 for (std::size_t j = 0; j < parent_map_sub.size(); ++j)
58 parent_map_sub[j] += bs * j;
59 offsets.push_back(offsets.back() + (bs > 1 ? 1 : sub_e->space_dimension()));
60 sub_doflayout.push_back(
61 dolfinx::fem::create_element_dof_layout(*sub_e, parent_map_sub));
62 }
63
64 return ElementDofLayout(bs, element.entity_dofs(),
65 element.entity_closure_dofs(), parent_map,
66 sub_doflayout);
67}
68
78create_dofmap(MPI_Comm comm, const ElementDofLayout& layout,
79 mesh::Topology& topology,
80 const std::function<void(std::span<std::int32_t>, std::uint32_t)>&
81 permute_inv,
82 const std::function<std::vector<int>(
83 const graph::AdjacencyList<std::int32_t>&)>& reorder_fn);
84
95std::vector<DofMap> create_dofmaps(
96 MPI_Comm comm, const std::vector<ElementDofLayout>& layouts,
97 mesh::Topology& topology,
98 const std::function<void(std::span<std::int32_t>, std::uint32_t)>&
99 permute_inv,
100 const std::function<std::vector<int>(
101 const graph::AdjacencyList<std::int32_t>&)>& reorder_fn);
102
104template <std::floating_point T>
106 std::shared_ptr<mesh::Mesh<T>> mesh,
107 std::shared_ptr<const fem::FiniteElement<T>> e,
108 std::function<std::vector<int>(const graph::AdjacencyList<std::int32_t>&)>
109 reorder_fn = nullptr)
110{
111 // TODO: check cell type of e (need to add method to fem::FiniteElement)
112 assert(e);
113 assert(mesh);
114 assert(mesh->topology());
115 if (e->cell_type() != mesh->topology()->cell_type())
116 throw std::invalid_argument("Cell type of element and mesh must match.");
117
118 // Create element dof layout
120
121 // Create a dofmap
122 std::function<void(std::span<std::int32_t>, std::uint32_t)> permute_inv
123 = e->needs_dof_permutations() ? e->dof_permutation_fn(true, true)
124 : nullptr;
125 auto dofmap = std::make_shared<const DofMap>(create_dofmap(
126 mesh->comm(), layout, *mesh->topology(), permute_inv, reorder_fn));
127
128 return FunctionSpace(mesh, e, dofmap);
129}
130} // namespace dolfinx::fem
Degree-of-freedom map representations and tools.
Degree-of-freedom map.
Definition DofMap.h:73
Definition ElementDofLayout.h:31
Model of a finite element.
Definition FiniteElement.h:199
const std::vector< std::shared_ptr< const FiniteElement< geometry_type > > > & sub_elements() const noexcept
Get subelements (if any).
Definition FiniteElement.cpp:468
const std::vector< std::vector< std::vector< int > > > & entity_dofs() const noexcept
Local DOFs associated with each sub-entity of the cell.
Definition FiniteElement.cpp:412
int num_sub_elements() const noexcept
Number of sub elements (for a mixed or blocked element).
Definition FiniteElement.cpp:455
const std::vector< std::vector< std::vector< int > > > & entity_closure_dofs() const noexcept
Local DOFs associated with the closure of each sub-entity of the cell.
Definition FiniteElement.cpp:419
int block_size() const noexcept
Block size of the finite element function space.
Definition FiniteElement.cpp:431
This class represents a finite element function space defined by a mesh, a finite element,...
Definition FunctionSpace.h:35
This class provides a static adjacency list data structure.
Definition AdjacencyList.h:41
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:25
Finite element method functionality.
Definition assemble_expression_impl.h:22
FunctionSpace< T > create_functionspace(std::shared_ptr< mesh::Mesh< T > > mesh, std::shared_ptr< const fem::FiniteElement< T > > e, std::function< std::vector< int >(const graph::AdjacencyList< std::int32_t > &)> reorder_fn=nullptr)
NEW Create a function space from a fem::FiniteElement.
Definition functionspace_factory.h:105
ElementDofLayout create_element_dof_layout(const fem::FiniteElement< T > &element, const std::vector< int > &parent_map={})
Create an ElementDofLayout from a FiniteElement.
Definition functionspace_factory.h:36
DofMap create_dofmap(MPI_Comm comm, const ElementDofLayout &layout, mesh::Topology &topology, const std::function< void(std::span< std::int32_t >, std::uint32_t)> &permute_inv, const std::function< std::vector< int >(const graph::AdjacencyList< std::int32_t > &)> &reorder_fn)
Create a dof map on mesh.
Definition utils.cpp:34
std::vector< DofMap > create_dofmaps(MPI_Comm comm, const std::vector< ElementDofLayout > &layouts, mesh::Topology &topology, const std::function< void(std::span< std::int32_t >, std::uint32_t)> &permute_inv, const std::function< std::vector< int >(const graph::AdjacencyList< std::int32_t > &)> &reorder_fn)
Create a set of dofmaps on a given topology.
Definition utils.cpp:80
FunctionSpace(U mesh, V element, W dofmap) -> FunctionSpace< typename std::remove_cvref< typename U::element_type >::type::geometry_type::value_type >
Type deduction.
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32