DOLFIN-X
DOLFIN-X C++ interface
FunctionSpace.h
1 // Copyright (C) 2008-2019 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 <Eigen/Dense>
10 #include <cstddef>
11 #include <dolfinx/fem/FiniteElement.h>
12 #include <functional>
13 #include <map>
14 #include <memory>
15 #include <petscsys.h>
16 #include <vector>
17 
18 namespace dolfinx
19 {
20 
21 namespace fem
22 {
23 class DofMap;
24 }
25 
26 namespace mesh
27 {
28 class Mesh;
29 }
30 
31 namespace function
32 {
33 class Function;
34 
38 
40 {
41 public:
46  FunctionSpace(std::shared_ptr<const mesh::Mesh> mesh,
47  std::shared_ptr<const fem::FiniteElement> element,
48  std::shared_ptr<const fem::DofMap> dofmap);
49 
50  // Copy constructor (deleted)
51  FunctionSpace(const FunctionSpace& V) = delete;
52 
54  FunctionSpace(FunctionSpace&& V) = default;
55 
57  virtual ~FunctionSpace() = default;
58 
59  // Assignment operator (delete)
60  FunctionSpace& operator=(const FunctionSpace& V) = delete;
61 
63  FunctionSpace& operator=(FunctionSpace&& V) = default;
64 
67  bool operator==(const FunctionSpace& V) const;
68 
71  bool operator!=(const FunctionSpace& V) const;
72 
75  std::int64_t dim() const;
76 
84  void interpolate(
85  Eigen::Ref<Eigen::Array<PetscScalar, Eigen::Dynamic, 1>> coefficients,
86  const Function& v) const;
87 
89  using interpolation_function = std::function<void(
90  Eigen::Ref<Eigen::Array<PetscScalar, Eigen::Dynamic, Eigen::Dynamic,
91  Eigen::RowMajor>>,
92  const Eigen::Ref<
93  const Eigen::Array<double, Eigen::Dynamic, 3, Eigen::RowMajor>>&)>;
94 
104  void interpolate(
105  Eigen::Ref<Eigen::Array<PetscScalar, Eigen::Dynamic, 1>> coefficients,
106  const std::function<Eigen::Array<PetscScalar, Eigen::Dynamic,
107  Eigen::Dynamic, Eigen::RowMajor>(
108  const Eigen::Ref<const Eigen::Array<double, 3, Eigen::Dynamic,
109  Eigen::RowMajor>>&)>& f) const;
110 
121  void interpolate_c(
122  Eigen::Ref<Eigen::Array<PetscScalar, Eigen::Dynamic, 1>> coefficients,
123  const interpolation_function& f) const;
124 
128  std::shared_ptr<FunctionSpace> sub(const std::vector<int>& component) const;
129 
133  bool contains(const FunctionSpace& V) const;
134 
138  std::pair<std::shared_ptr<FunctionSpace>, std::vector<std::int32_t>>
139  collapse() const;
140 
145  {
146  return element.hash() == this->_element->hash();
147  }
148 
152  std::vector<int> component() const;
153 
156  Eigen::Array<double, Eigen::Dynamic, 3, Eigen::RowMajor>
157  tabulate_dof_coordinates() const;
158 
168  void set_x(Eigen::Ref<Eigen::Array<PetscScalar, Eigen::Dynamic, 1>> x,
169  PetscScalar value, int component) const;
170 
172  std::size_t id() const;
173 
175  std::shared_ptr<const mesh::Mesh> mesh() const;
176 
178  std::shared_ptr<const fem::FiniteElement> element() const;
179 
181  std::shared_ptr<const fem::DofMap> dofmap() const;
182 
183 private:
184  // Interpolate data. Fills coefficients using 'values', which are the
185  // values of an expression at each dof.
186  void interpolate(
187  Eigen::Ref<Eigen::Array<PetscScalar, Eigen::Dynamic, 1>> coefficients,
188  const Eigen::Ref<const Eigen::Array<PetscScalar, Eigen::Dynamic,
189  Eigen::Dynamic, Eigen::RowMajor>>&
190  values) const;
191 
192  // The mesh
193  std::shared_ptr<const mesh::Mesh> _mesh;
194 
195  // The finite element
196  std::shared_ptr<const fem::FiniteElement> _element;
197 
198  // The dofmap
199  std::shared_ptr<const fem::DofMap> _dofmap;
200 
201  // General interpolation from any Function on any mesh
202  void
203  interpolate_from_any(Eigen::Ref<Eigen::Array<PetscScalar, Eigen::Dynamic, 1>>
204  expansion_coefficients,
205  const Function& v) const;
206 
207  // The component w.r.t. to root space
208  std::vector<int> _component;
209 
210  // Unique identifier
211  std::size_t _id;
212 
213  // The identifier of root space
214  std::size_t _root_space_id;
215 
216  // Cache of subspaces
217  mutable std::map<std::vector<int>, std::weak_ptr<FunctionSpace>> _subspaces;
218 };
219 } // namespace function
220 } // namespace dolfinx
dolfinx::function::FunctionSpace::set_x
void set_x(Eigen::Ref< Eigen::Array< PetscScalar, Eigen::Dynamic, 1 >> x, PetscScalar value, int component) const
Set dof entries in vector to value*x[i], where [x][i] is the coordinate of the dof spatial coordinate...
Definition: FunctionSpace.cpp:353
dolfinx::function::FunctionSpace::~FunctionSpace
virtual ~FunctionSpace()=default
Destructor.
dolfinx::function::FunctionSpace::sub
std::shared_ptr< FunctionSpace > sub(const std::vector< int > &component) const
Extract subspace for component.
Definition: FunctionSpace.cpp:216
dolfinx::function::FunctionSpace::collapse
std::pair< std::shared_ptr< FunctionSpace >, std::vector< std::int32_t > > collapse() const
Collapse a subspace and return a new function space and a map from new to old dofs.
Definition: FunctionSpace.cpp:255
dolfinx::function::FunctionSpace::mesh
std::shared_ptr< const mesh::Mesh > mesh() const
The mesh.
Definition: FunctionSpace.cpp:414
dolfinx::function::FunctionSpace::has_element
bool has_element(const fem::FiniteElement &element) const
Check if function space has given element.
Definition: FunctionSpace.h:144
dolfinx::function::FunctionSpace::dofmap
std::shared_ptr< const fem::DofMap > dofmap() const
The dofmap.
Definition: FunctionSpace.cpp:421
dolfinx::function::FunctionSpace::interpolate
void interpolate(Eigen::Ref< Eigen::Array< PetscScalar, Eigen::Dynamic, 1 >> coefficients, const Function &v) const
Interpolate a finite element Function into this function space, filling the array of expansion coeffi...
Definition: FunctionSpace.cpp:109
dolfinx::function::FunctionSpace::element
std::shared_ptr< const fem::FiniteElement > element() const
The finite element.
Definition: FunctionSpace.cpp:416
dolfinx::fem::FiniteElement
Finite Element, containing the dof layout on a reference element, and various methods for evaluating ...
Definition: FiniteElement.h:25
dolfinx::function::FunctionSpace::dim
std::int64_t dim() const
Return global dimension of the function space.
Definition: FunctionSpace.cpp:48
dolfinx::function::FunctionSpace::component
std::vector< int > component() const
Get the component with respect to the root superspace.
Definition: FunctionSpace.cpp:273
dolfinx::function::FunctionSpace::FunctionSpace
FunctionSpace(std::shared_ptr< const mesh::Mesh > mesh, std::shared_ptr< const fem::FiniteElement > element, std::shared_ptr< const fem::DofMap > dofmap)
Create function space for given mesh, element and dofmap.
Definition: FunctionSpace.cpp:26
dolfinx::function::FunctionSpace::interpolate_c
void interpolate_c(Eigen::Ref< Eigen::Array< PetscScalar, Eigen::Dynamic, 1 >> coefficients, const interpolation_function &f) const
Interpolate an expression into this function space, filling the array of expansion coefficients assoc...
Definition: FunctionSpace.cpp:193
dolfinx::function::FunctionSpace::tabulate_dof_coordinates
Eigen::Array< double, Eigen::Dynamic, 3, Eigen::RowMajor > tabulate_dof_coordinates() const
Tabulate the physical coordinates of all dofs on this process.
Definition: FunctionSpace.cpp:276
dolfinx::function::FunctionSpace::operator==
bool operator==(const FunctionSpace &V) const
Equality operator.
Definition: FunctionSpace.cpp:35
dolfinx::function::Function
This class represents a function in a finite element function space , given by.
Definition: Function.h:41
dolfinx::function::FunctionSpace::id
std::size_t id() const
Unique identifier.
Definition: FunctionSpace.cpp:412
dolfinx::function::FunctionSpace::interpolation_function
std::function< void(Eigen::Ref< Eigen::Array< PetscScalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > >, const Eigen::Ref< const Eigen::Array< double, Eigen::Dynamic, 3, Eigen::RowMajor > > &)> interpolation_function
Interpolation function.
Definition: FunctionSpace.h:93
dolfinx::function::FunctionSpace::contains
bool contains(const FunctionSpace &V) const
Check whether V is subspace of this, or this itself.
Definition: FunctionSpace.cpp:426
dolfinx::function::FunctionSpace
This class represents a finite element function space defined by a mesh, a finite element,...
Definition: FunctionSpace.h:39
dolfinx::function::FunctionSpace::operator!=
bool operator!=(const FunctionSpace &V) const
Inequality operator.
Definition: FunctionSpace.cpp:42