DOLFINx 0.10.0.0
DOLFINx C++ interface
Loading...
Searching...
No Matches
FunctionSpace.h
1// Copyright (C) 2008-2023 Anders Logg and Garth N. Wells
2//
3// This file is part of DOLFINx (https://www.fenicsproject.org)
4//
5// SPDX-License-Identifier: LGPL-3.0-or-later
6
7#pragma once
8
9#include "CoordinateElement.h"
10#include "DofMap.h"
11#include "FiniteElement.h"
12#include <boost/uuid/uuid.hpp>
13#include <boost/uuid/uuid_generators.hpp>
14#include <concepts>
15#include <cstddef>
16#include <cstdint>
17#include <dolfinx/common/IndexMap.h>
18#include <dolfinx/mesh/Geometry.h>
19#include <dolfinx/mesh/Mesh.h>
20#include <dolfinx/mesh/Topology.h>
21#include <map>
22#include <memory>
23#include <vector>
24
25namespace dolfinx::fem
26{
32template <std::floating_point T>
34{
35public:
37 using geometry_type = T;
38
45 std::shared_ptr<const FiniteElement<geometry_type>> element,
46 std::shared_ptr<const DofMap> dofmap)
47 : _mesh(mesh), _elements{element}, _dofmaps{dofmap},
48 _id(boost::uuids::random_generator()()), _root_space_id(_id)
49 {
50 // Do nothing
51 }
52
62 std::shared_ptr<const mesh::Mesh<geometry_type>> mesh,
63 std::vector<std::shared_ptr<const FiniteElement<geometry_type>>> elements,
64 std::vector<std::shared_ptr<const DofMap>> dofmaps)
65 : _mesh(mesh), _elements(elements), _dofmaps(dofmaps),
66 _id(boost::uuids::random_generator()()), _root_space_id(_id)
67 {
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)
71 {
72 throw std::runtime_error(
73 "Number of elements must match number of cell types");
74 }
75 if (dofmaps.size() != num_cell_types)
76 {
77 throw std::runtime_error(
78 "Number of dofmaps must match number of cell types");
79 }
80 for (std::size_t i = 0; i < num_cell_types; ++i)
81 {
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");
85 }
86 }
87
88 // Copy constructor (deleted)
89 FunctionSpace(const FunctionSpace& V) = delete;
90
93
95 virtual ~FunctionSpace() = default;
96
97 // Assignment operator (delete)
98 FunctionSpace& operator=(const FunctionSpace& V) = delete;
99
102
111 FunctionSpace sub(const std::vector<int>& component) const
112 {
113 assert(_mesh);
114 assert(_elements.front());
115 assert(_dofmaps.front());
116
117 // Check that component is valid
118 if (component.empty())
119 throw std::runtime_error("Component must be non-empty");
120
121 // Extract sub-element
122 auto element = this->_elements.front()->extract_sub_element(component);
123
124 // Extract sub dofmap
125 auto dofmap = std::make_shared<DofMap>(
126 _dofmaps.front()->extract_sub_dofmap(component));
127
128 // Create new sub space
129 FunctionSpace sub_space(_mesh, element, dofmap);
130
131 // Set root space id and component w.r.t. root
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(),
135 component.end());
136 return sub_space;
137 }
138
143 bool contains(const FunctionSpace& V) const
144 {
145 if (this == std::addressof(V)) // Spaces are the same (same memory address)
146 return true;
147 else if (_root_space_id != V._root_space_id) // Different root spaces
148 return false;
149 else if (_component.size()
150 > V._component.size()) // V is a superspace of *this
151 {
152 return false;
153 }
154 else if (!std::equal(_component.begin(), _component.end(),
155 V._component.begin())) // Components of 'this' are not
156 // the same as the leading
157 // components of V
158 {
159 return false;
160 }
161 else // Ok, V is really our subspace
162 return true;
163 }
164
168 std::pair<FunctionSpace, std::vector<std::int32_t>> collapse() const
169 {
170 if (_component.empty())
171 throw std::runtime_error("Function space is not a subspace");
172
173 // Create collapsed DofMap
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));
178
179 return {FunctionSpace(_mesh, _elements.front(), collapsed_dofmap),
180 std::move(collapsed_dofs)};
181 }
182
186 std::vector<int> component() const { return _component; }
187
190 bool symmetric() const
191 {
192 if (_elements.front())
193 return _elements.front()->symmetric();
194 return false;
195 }
196
208 std::vector<geometry_type> tabulate_dof_coordinates(bool transpose) const
209 {
210 if (!_component.empty())
211 {
212 throw std::runtime_error("Cannot tabulate coordinates for a "
213 "FunctionSpace that is a subspace.");
214 }
215
216 assert(_elements.front());
217 if (_elements.front()->is_mixed())
218 {
219 throw std::runtime_error(
220 "Cannot tabulate coordinates for a mixed FunctionSpace.");
221 }
222
223 // Geometric dimension
224 assert(_mesh);
225 assert(_elements.front());
226 const std::size_t gdim = _mesh->geometry().dim();
227 const int tdim = _mesh->topology()->dim();
228
229 // Get dofmap local size
230 assert(_dofmaps.front());
231 std::shared_ptr<const common::IndexMap> index_map
232 = _dofmaps.front()->index_map;
233 assert(index_map);
234 const int index_map_bs = _dofmaps.front()->index_map_bs();
235 const int dofmap_bs = _dofmaps.front()->bs();
236
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())
242 / dofmap_bs;
243
244 // Get the dof coordinates on the reference element
245 if (!_elements.front()->interpolation_ident())
246 {
247 throw std::runtime_error("Cannot evaluate dof coordinates - this element "
248 "does not have pointwise evaluation.");
249 }
250 const auto [X, Xshape] = _elements.front()->interpolation_points();
251
252 // Get coordinate map
253 const CoordinateElement<geometry_type>& cmap = _mesh->geometry().cmap();
254
255 // Prepare cell geometry
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();
259
260 // Array to hold coordinates to return
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);
264
265 using mdspan2_t = md::mdspan<geometry_type, md::dextents<std::size_t, 2>>;
266 using cmdspan4_t
267 = md::mdspan<const geometry_type, md::dextents<std::size_t, 4>>;
268
269 // Loop over cells and tabulate dofs
270 std::vector<geometry_type> x_b(scalar_dofs * gdim);
271 mdspan2_t x(x_b.data(), scalar_dofs, gdim);
272
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);
275
276 auto map = _mesh->topology()->index_map(tdim);
277 assert(map);
278 const int num_cells = map->size_local() + map->num_ghosts();
279
280 std::span<const std::uint32_t> cell_info;
281 if (_elements.front()->needs_dof_transformations())
282 {
283 _mesh->topology_mutable()->create_entity_permutations();
284 cell_info = std::span(_mesh->topology()->get_cell_permutation_info());
285 }
286
287 const std::array<std::size_t, 4> phi_shape
288 = cmap.tabulate_shape(0, Xshape[0]);
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);
292 cmap.tabulate(0, X, Xshape, phi_b);
293 auto phi = md::submdspan(phi_full, 0, md::full_extent, md::full_extent, 0);
294
295 // TODO: Check transform
296 // Basis function reference-to-conforming transformation function
297 auto apply_dof_transformation
298 = _elements.front()->template dof_transformation_fn<geometry_type>(
300
301 for (int c = 0; c < num_cells; ++c)
302 {
303 // Extract cell geometry 'dofs'
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];
308
309 // Tabulate dof coordinates on cell
310 cmap.push_forward(x, coordinate_dofs, phi);
311 apply_dof_transformation(
312 x_b, std::span(cell_info.data(), cell_info.size()), c, x.extent(1));
313
314 // Get cell dofmap
315 auto dofs = _dofmaps.front()->cell_dofs(c);
316
317 // Copy dof coordinates into vector
318 if (!transpose)
319 {
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);
323 }
324 else
325 {
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);
329 }
330 }
331
332 return coords;
333 }
334
336 std::shared_ptr<const mesh::Mesh<geometry_type>> mesh() const
337 {
338 return _mesh;
339 }
340
342 std::shared_ptr<const FiniteElement<geometry_type>> element() const
343 {
344 if (_elements.size() > 1)
345 {
346 throw std::runtime_error(
347 "FunctionSpace has multiple elements, call `elements` instead.");
348 }
349
350 return elements(0);
351 }
352
354 std::shared_ptr<const FiniteElement<geometry_type>>
355 elements(int cell_type_idx) const
356 {
357 return _elements.at(cell_type_idx);
358 }
359
361 std::shared_ptr<const DofMap> dofmap() const
362 {
363 if (_dofmaps.size() > 1)
364 {
365 throw std::runtime_error(
366 "FunctionSpace has multiple dofmaps, call `dofmaps` instead.");
367 }
368
369 return dofmaps(0);
370 }
371
373 std::shared_ptr<const DofMap> dofmaps(int cell_type_idx) const
374 {
375 return _dofmaps.at(cell_type_idx);
376 }
377
378private:
379 // The mesh
380 std::shared_ptr<const mesh::Mesh<geometry_type>> _mesh;
381
382 // The finite element
383 std::vector<std::shared_ptr<const FiniteElement<geometry_type>>> _elements;
384
385 // The dofmap
386 std::vector<std::shared_ptr<const DofMap>> _dofmaps;
387
388 // The component w.r.t. to root space
389 std::vector<int> _component;
390
391 // Unique identifier for the space and for its root space
392 boost::uuids::uuid _id;
393 boost::uuids::uuid _root_space_id;
394};
395
406template <dolfinx::scalar T>
407std::array<std::vector<std::shared_ptr<const FunctionSpace<T>>>, 2>
409 const std::vector<
410 std::vector<std::array<std::shared_ptr<const FunctionSpace<T>>, 2>>>& V)
411{
412 assert(!V.empty());
413 std::vector<std::shared_ptr<const FunctionSpace<T>>> spaces0(V.size(),
414 nullptr);
415 std::vector<std::shared_ptr<const FunctionSpace<T>>> spaces1(V.front().size(),
416 nullptr);
417
418 // Loop over rows
419 for (std::size_t i = 0; i < V.size(); ++i)
420 {
421 // Loop over columns
422 for (std::size_t j = 0; j < V[i].size(); ++j)
423 {
424 auto& V0 = V[i][j][0];
425 auto& V1 = V[i][j][1];
426 if (V0 and V1)
427 {
428 if (!spaces0[i])
429 spaces0[i] = V0;
430 else
431 {
432 if (spaces0[i] != V0)
433 throw std::runtime_error("Mismatched test space for row.");
434 }
435
436 if (!spaces1[j])
437 spaces1[j] = V1;
438 else
439 {
440 if (spaces1[j] != V1)
441 throw std::runtime_error("Mismatched trial space for column.");
442 }
443 }
444 }
445 }
446
447 // Check that there are no null entries
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.");
452
453 return {spaces0, spaces1};
454}
455
457template <typename U, typename V, typename W>
458FunctionSpace(U mesh, V element, W dofmap)
459 -> FunctionSpace<typename std::remove_cvref<
460 typename U::element_type>::type::geometry_type::value_type>;
461
462} // namespace dolfinx::fem
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