9#include "CoordinateElement.h"
11#include "FiniteElement.h"
12#include <boost/uuid/uuid.hpp>
13#include <boost/uuid/uuid_generators.hpp>
17#include <dolfinx/common/IndexMap.h>
18#include <dolfinx/mesh/Geometry.h>
19#include <dolfinx/mesh/Mesh.h>
20#include <dolfinx/mesh/Topology.h>
32template <std::
floating_po
int T>
46 std::shared_ptr<const DofMap>
dofmap)
48 _id(boost::uuids::random_generator()()), _root_space_id(_id)
64 std::vector<std::shared_ptr<const DofMap>>
dofmaps)
66 _id(boost::uuids::random_generator()()), _root_space_id(_id)
68 std::vector<mesh::CellType> cell_types =
mesh->topology()->cell_types();
69 std::size_t num_cell_types = cell_types.size();
70 if (
elements.size() != num_cell_types)
72 throw std::runtime_error(
73 "Number of elements must match number of cell types");
75 if (
dofmaps.size() != num_cell_types)
77 throw std::runtime_error(
78 "Number of dofmaps must match number of cell types");
80 for (std::size_t i = 0; i < num_cell_types; ++i)
82 if (
elements.at(i)->cell_type() != cell_types.at(i))
83 throw std::runtime_error(
84 "Element cell types must match mesh cell types");
114 assert(_elements.front());
115 assert(_dofmaps.front());
119 throw std::runtime_error(
"Component must be non-empty");
125 auto dofmap = std::make_shared<DofMap>(
126 _dofmaps.front()->extract_sub_dofmap(
component));
132 sub_space._root_space_id = _root_space_id;
133 sub_space._component = _component;
134 sub_space._component.insert(sub_space._component.end(),
component.begin(),
145 if (
this == std::addressof(V))
147 else if (_root_space_id != V._root_space_id)
149 else if (_component.size()
150 > V._component.size())
154 else if (!std::equal(_component.begin(), _component.end(),
155 V._component.begin()))
168 std::pair<FunctionSpace, std::vector<std::int32_t>>
collapse()
const
170 if (_component.empty())
171 throw std::runtime_error(
"Function space is not a subspace");
174 auto [_collapsed_dofmap, collapsed_dofs]
175 = _dofmaps.front()->collapse(_mesh->comm(), *_mesh->topology());
176 auto collapsed_dofmap
177 = std::make_shared<DofMap>(std::move(_collapsed_dofmap));
179 return {
FunctionSpace(_mesh, _elements.front(), collapsed_dofmap),
180 std::move(collapsed_dofs)};
186 std::vector<int>
component()
const {
return _component; }
192 if (_elements.front())
193 return _elements.front()->symmetric();
210 if (!_component.empty())
212 throw std::runtime_error(
"Cannot tabulate coordinates for a "
213 "FunctionSpace that is a subspace.");
216 assert(_elements.front());
217 if (_elements.front()->is_mixed())
219 throw std::runtime_error(
220 "Cannot tabulate coordinates for a mixed FunctionSpace.");
225 assert(_elements.front());
226 const std::size_t gdim = _mesh->geometry().dim();
227 const int tdim = _mesh->topology()->dim();
230 assert(_dofmaps.front());
231 std::shared_ptr<const common::IndexMap> index_map
232 = _dofmaps.front()->index_map;
234 const int index_map_bs = _dofmaps.front()->index_map_bs();
235 const int dofmap_bs = _dofmaps.front()->bs();
237 const int element_block_size = _elements.front()->block_size();
238 const std::size_t scalar_dofs
239 = _elements.front()->space_dimension() / element_block_size;
240 const std::int32_t num_dofs
241 = index_map_bs * (index_map->size_local() + index_map->num_ghosts())
245 if (!_elements.front()->interpolation_ident())
247 throw std::runtime_error(
"Cannot evaluate dof coordinates - this element "
248 "does not have pointwise evaluation.");
250 const auto [X, Xshape] = _elements.front()->interpolation_points();
256 auto x_dofmap = _mesh->geometry().dofmap();
257 const std::size_t num_dofs_g = cmap.
dim();
258 std::span<const geometry_type> x_g = _mesh->geometry().x();
261 const std::size_t shape_c0 =
transpose ? 3 : num_dofs;
262 const std::size_t shape_c1 =
transpose ? num_dofs : 3;
263 std::vector<geometry_type> coords(shape_c0 * shape_c1, 0);
265 using mdspan2_t = md::mdspan<geometry_type, md::dextents<std::size_t, 2>>;
267 = md::mdspan<const geometry_type, md::dextents<std::size_t, 4>>;
270 std::vector<geometry_type> x_b(scalar_dofs * gdim);
271 mdspan2_t x(x_b.data(), scalar_dofs, gdim);
273 std::vector<geometry_type> coordinate_dofs_b(num_dofs_g * gdim);
274 mdspan2_t coordinate_dofs(coordinate_dofs_b.data(), num_dofs_g, gdim);
276 auto map = _mesh->topology()->index_map(tdim);
278 const int num_cells = map->size_local() + map->num_ghosts();
280 std::span<const std::uint32_t> cell_info;
281 if (_elements.front()->needs_dof_transformations())
283 _mesh->topology_mutable()->create_entity_permutations();
284 cell_info = std::span(_mesh->topology()->get_cell_permutation_info());
287 const std::array<std::size_t, 4> phi_shape
289 std::vector<geometry_type> phi_b(
290 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
291 cmdspan4_t phi_full(phi_b.data(), phi_shape);
293 auto phi = md::submdspan(phi_full, 0, md::full_extent, md::full_extent, 0);
297 auto apply_dof_transformation
298 = _elements.front()->template dof_transformation_fn<geometry_type>(
301 for (
int c = 0; c < num_cells; ++c)
304 auto x_dofs = md::submdspan(x_dofmap, c, md::full_extent);
305 for (std::size_t i = 0; i < x_dofs.size(); ++i)
306 for (std::size_t j = 0; j < gdim; ++j)
307 coordinate_dofs(i, j) = x_g[3 * x_dofs[i] + j];
311 apply_dof_transformation(
312 x_b, std::span(cell_info.data(), cell_info.size()), c, x.extent(1));
315 auto dofs = _dofmaps.front()->cell_dofs(c);
320 for (std::size_t i = 0; i < dofs.size(); ++i)
321 for (std::size_t j = 0; j < gdim; ++j)
322 coords[dofs[i] * 3 + j] = x(i, j);
326 for (std::size_t i = 0; i < dofs.size(); ++i)
327 for (std::size_t j = 0; j < gdim; ++j)
328 coords[j * num_dofs + dofs[i]] = x(i, j);
336 std::shared_ptr<const mesh::Mesh<geometry_type>>
mesh()
const
342 std::shared_ptr<const FiniteElement<geometry_type>>
element()
const
344 if (_elements.size() > 1)
346 throw std::runtime_error(
347 "FunctionSpace has multiple elements, call `elements` instead.");
354 std::shared_ptr<const FiniteElement<geometry_type>>
357 return _elements.at(cell_type_idx);
361 std::shared_ptr<const DofMap>
dofmap()
const
363 if (_dofmaps.size() > 1)
365 throw std::runtime_error(
366 "FunctionSpace has multiple dofmaps, call `dofmaps` instead.");
373 std::shared_ptr<const DofMap>
dofmaps(
int cell_type_idx)
const
375 return _dofmaps.at(cell_type_idx);
380 std::shared_ptr<const mesh::Mesh<geometry_type>> _mesh;
383 std::vector<std::shared_ptr<const FiniteElement<geometry_type>>> _elements;
386 std::vector<std::shared_ptr<const DofMap>> _dofmaps;
389 std::vector<int> _component;
392 boost::uuids::uuid _id;
393 boost::uuids::uuid _root_space_id;
406template <dolfinx::scalar T>
407std::array<std::vector<std::shared_ptr<const FunctionSpace<T>>>, 2>
413 std::vector<std::shared_ptr<const FunctionSpace<T>>> spaces0(V.size(),
415 std::vector<std::shared_ptr<const FunctionSpace<T>>> spaces1(V.front().size(),
419 for (std::size_t i = 0; i < V.size(); ++i)
422 for (std::size_t j = 0; j < V[i].size(); ++j)
424 auto& V0 = V[i][j][0];
425 auto& V1 = V[i][j][1];
432 if (spaces0[i] != V0)
433 throw std::runtime_error(
"Mismatched test space for row.");
440 if (spaces1[j] != V1)
441 throw std::runtime_error(
"Mismatched trial space for column.");
448 if (std::find(spaces0.begin(), spaces0.end(),
nullptr) != spaces0.end())
449 throw std::runtime_error(
"Could not deduce all block test spaces.");
450 if (std::find(spaces1.begin(), spaces1.end(),
nullptr) != spaces1.end())
451 throw std::runtime_error(
"Could not deduce all block trial spaces.");
453 return {spaces0, spaces1};
457template <
typename U,
typename V,
typename W>
460 typename U::element_type>::type::geometry_type::value_type>;
Degree-of-freedom map representations and tools.
Definition CoordinateElement.h:38
void tabulate(int nd, std::span< const T > X, std::array< std::size_t, 2 > shape, std::span< T > basis) const
Evaluate basis values and derivatives at set of points.
Definition CoordinateElement.cpp:55
std::array< std::size_t, 4 > tabulate_shape(std::size_t nd, std::size_t num_points) const
Shape of array to fill when calling tabulate.
Definition CoordinateElement.cpp:48
int dim() const
The dimension of the coordinate element space.
Definition CoordinateElement.cpp:205
static void push_forward(U &&x, const V &cell_geometry, const W &phi)
Compute physical coordinates x for points X in the reference configuration.
Definition CoordinateElement.h:194
Model of a finite element.
Definition FiniteElement.h:57
This class represents a finite element function space defined by a mesh, a finite element,...
Definition FunctionSpace.h:34
std::vector< int > component() const
Get the component with respect to the root superspace.
Definition FunctionSpace.h:186
bool symmetric() const
Indicate whether this function space represents a symmetric 2-tensor.
Definition FunctionSpace.h:190
FunctionSpace & operator=(FunctionSpace &&V)=default
Move assignment operator.
std::shared_ptr< const FiniteElement< geometry_type > > elements(int cell_type_idx) const
The finite elements.
Definition FunctionSpace.h:355
std::pair< FunctionSpace, std::vector< std::int32_t > > collapse() const
Definition FunctionSpace.h:168
std::shared_ptr< const DofMap > dofmap() const
The dofmap.
Definition FunctionSpace.h:361
FunctionSpace(std::shared_ptr< const mesh::Mesh< geometry_type > > mesh, std::vector< std::shared_ptr< const FiniteElement< geometry_type > > > elements, std::vector< std::shared_ptr< const DofMap > > dofmaps)
Create function space for given mesh, elements and degree-of-freedom maps.
Definition FunctionSpace.h:61
FunctionSpace sub(const std::vector< int > &component) const
Create a subspace (view) for a specific component.
Definition FunctionSpace.h:111
bool contains(const FunctionSpace &V) const
Check whether V is subspace of this, or this itself.
Definition FunctionSpace.h:143
std::shared_ptr< const mesh::Mesh< geometry_type > > mesh() const
The mesh.
Definition FunctionSpace.h:336
virtual ~FunctionSpace()=default
Destructor.
std::shared_ptr< const FiniteElement< geometry_type > > element() const
The finite element.
Definition FunctionSpace.h:342
FunctionSpace(std::shared_ptr< const mesh::Mesh< geometry_type > > mesh, std::shared_ptr< const FiniteElement< geometry_type > > element, std::shared_ptr< const DofMap > dofmap)
Create function space for given mesh, element and degree-of-freedom map.
Definition FunctionSpace.h:44
T geometry_type
Geometry type of the Mesh that the FunctionSpace is defined on.
Definition FunctionSpace.h:37
std::vector< geometry_type > tabulate_dof_coordinates(bool transpose) const
Tabulate the physical coordinates of all dofs on this process.
Definition FunctionSpace.h:208
std::shared_ptr< const DofMap > dofmaps(int cell_type_idx) const
The dofmaps.
Definition FunctionSpace.h:373
FunctionSpace(FunctionSpace &&V)=default
Move constructor.
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:23
Finite element method functionality.
Definition assemble_expression_impl.h:23
@ transpose
Transpose.
Definition FiniteElement.h:28
@ standard
Standard.
Definition FiniteElement.h:27
std::array< std::vector< std::shared_ptr< const FunctionSpace< T > > >, 2 > common_function_spaces(const std::vector< std::vector< std::array< std::shared_ptr< const FunctionSpace< T > >, 2 > > > &V)
Extract FunctionSpaces for (0) rows blocks and (1) columns blocks from a rectangular array of (test,...
Definition FunctionSpace.h:408
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