9 #include "FunctionSpace.h"
11 #include <dolfinx/common/span.hpp>
12 #include <dolfinx/fem/DofMap.h>
13 #include <dolfinx/fem/FiniteElement.h>
14 #include <dolfinx/mesh/Mesh.h>
36 const tcb::span<const std::int32_t>& cells);
43 void interpolate(Function<T>& u,
const Function<T>& v);
58 const std::function<std::variant<std::vector<T>, array2d<T>>(
59 const array2d<double>&)>& f,
60 const array2d<double>& x,
61 const tcb::span<const std::int32_t>& cells);
82 const std::function<
void(array2d<T>&,
const array2d<double>&)>& f,
83 const array2d<double>& x,
const tcb::span<const std::int32_t>& cells);
89 void interpolate_from_any(Function<T>& u,
const Function<T>& v)
91 assert(v.function_space());
92 const auto element = u.function_space()->element();
94 if (v.function_space()->element()->hash() != element->hash())
96 throw std::runtime_error(
"Restricting finite elements function in "
97 "different elements not supported.");
100 const auto mesh = u.function_space()->mesh();
102 assert(v.function_space()->mesh());
103 if (mesh->id() != v.function_space()->mesh()->id())
105 throw std::runtime_error(
106 "Interpolation on different meshes not supported (yet).");
108 const int tdim = mesh->topology().dim();
111 assert(v.function_space());
112 std::shared_ptr<const fem::DofMap> dofmap_v = v.function_space()->dofmap();
114 auto map = mesh->topology().index_map(tdim);
117 std::vector<T>& coeffs = u.x()->mutable_array();
120 const auto dofmap_u = u.function_space()->dofmap();
121 const std::vector<T>& v_array = v.x()->array();
122 const int num_cells = map->size_local() + map->num_ghosts();
123 const int bs = dofmap_v->bs();
124 assert(bs == dofmap_u->bs());
125 for (
int c = 0; c < num_cells; ++c)
127 tcb::span<const std::int32_t> dofs_v = dofmap_v->cell_dofs(c);
128 tcb::span<const std::int32_t> cell_dofs = dofmap_u->cell_dofs(c);
129 assert(dofs_v.size() == cell_dofs.size());
130 for (std::size_t i = 0; i < dofs_v.size(); ++i)
132 for (
int k = 0; k < bs; ++k)
133 coeffs[bs * cell_dofs[i] + k] = v_array[bs * dofs_v[i] + k];
141 template <
typename T>
150 element->value_rank() != rank_v)
152 throw std::runtime_error(
"Cannot interpolate function into function space. "
154 + std::to_string(rank_v)
155 +
") does not match rank of function space ("
156 + std::to_string(element->value_rank()) +
")");
160 for (
int i = 0; i < element->value_rank(); ++i)
162 if (
int v_dim = v.
function_space()->element()->value_dimension(i);
163 element->value_dimension(i) != v_dim)
165 throw std::runtime_error(
166 "Cannot interpolate function into function space. "
168 + std::to_string(i) +
" of function (" + std::to_string(v_dim)
169 +
") does not match dimension " + std::to_string(i)
170 +
" of function space(" + std::to_string(element->value_dimension(i))
175 detail::interpolate_from_any(u, v);
178 template <
typename T>
180 const std::function<std::variant<std::vector<T>,
array2d<T>>(
183 const tcb::span<const std::int32_t>& cells)
187 const int element_bs = element->block_size();
188 if (
int num_sub = element->num_sub_elements();
189 num_sub > 0 and num_sub != element_bs)
191 throw std::runtime_error(
"Cannot directly interpolate a mixed space. "
192 "Interpolate into subspaces.");
200 const int gdim = mesh->geometry().dim();
201 const int tdim = mesh->topology().dim();
207 throw std::runtime_error(
208 "Interpolation into this space is not yet supported.");
210 mesh->topology_mutable().create_entity_permutations();
211 const std::vector<std::uint32_t>& cell_info
212 = mesh->topology().get_cell_permutation_info();
222 std::variant<std::vector<T>,
array2d<T>> values_v = f(x);
223 if (std::holds_alternative<
array2d<T>>(values_v))
225 values = std::get<1>(values_v);
226 if (values.
shape[0] != element->value_size())
227 throw std::runtime_error(
"Interpolation data has the wrong shape.");
231 if (element->value_size() != 1)
232 throw std::runtime_error(
"Interpolation data has the wrong shape.");
233 std::copy(std::get<0>(values_v).begin(), std::get<0>(values_v).end(),
237 if (values.
shape[1] != cells.size() * X.
shape[0])
238 throw std::runtime_error(
"Interpolation data has the wrong shape.");
243 const int dofmap_bs = dofmap->bs();
250 = mesh->geometry().dofmap();
252 const int num_dofs_g = x_dofmap.
num_links(0);
259 const int num_scalar_dofs = element->space_dimension() / element_bs;
260 const int value_size = element->value_size() / element_bs;
263 std::vector<double> J(X.
shape[0] * gdim * tdim);
264 std::vector<double> detJ(X.
shape[0]);
265 std::vector<double> K(X.
shape[0] * tdim * gdim);
272 std::vector<T>& coeffs = u.
x()->mutable_array();
273 std::vector<T> _coeffs(num_scalar_dofs);
275 for (std::int32_t c : cells)
277 auto x_dofs = x_dofmap.
links(c);
278 for (
int i = 0; i < num_dofs_g; ++i)
279 for (
int j = 0; j < gdim; ++j)
280 coordinate_dofs(i, j) = x_g(x_dofs[i], j);
285 auto dofs = dofmap->cell_dofs(c);
287 for (
int k = 0; k < element_bs; ++k)
290 for (
int m = 0; m < value_size; ++m)
292 std::copy_n(&values(k * value_size + m, c * X.
shape[0]), X.
shape[0],
293 _vals.
row(m).begin());
297 element->map_pull_back(reference_data.
data(), _vals.
data(), J.data(),
298 detJ.data(), K.data(), gdim, value_size, 1,
301 element->interpolate(reference_data, tcb::make_span(_coeffs));
302 element->apply_inverse_transpose_dof_transformation(_coeffs.data(),
305 assert(_coeffs.size() == num_scalar_dofs);
308 for (
int i = 0; i < num_scalar_dofs; ++i)
310 const int dof = i * element_bs + k;
311 std::div_t pos = std::div(dof, dofmap_bs);
312 coeffs[dofmap_bs * dofs[pos.quot] + pos.rem] = _coeffs[i];
318 template <
typename T>
326 std::vector<int> vshape(element->value_rank(), 1);
327 for (std::size_t i = 0; i < vshape.size(); ++i)
328 vshape[i] = element->value_dimension(i);
329 const int value_size = std::accumulate(std::begin(vshape), std::end(vshape),
330 1, std::multiplies<>());
338 interpolate<T>(u, fn, x, cells);
This class provides a dynamic 2-dimensional row-wise array data structure.
Definition: array2d.h:21
std::array< size_type, 2 > shape
The shape of the array.
Definition: array2d.h:157
constexpr tcb::span< value_type > row(size_type i)
Access a row in the array.
Definition: array2d.h:116
constexpr value_type * data() noexcept
Get pointer to the first element of the underlying storage.
Definition: array2d.h:133
This class manages coordinate mappings for isoparametric cells.
Definition: CoordinateElement.h:25
void push_forward(array2d< double > &x, const array2d< double > &X, const array2d< double > &cell_geometry) const
Compute physical coordinates x for points X in the reference configuration.
Definition: CoordinateElement.cpp:67
void compute_reference_geometry(array2d< double > &X, std::vector< double > &J, tcb::span< double > detJ, std::vector< double > &K, const array2d< double > &x, const array2d< double > &cell_geometry) const
Compute reference coordinates X, and J, detJ and K for physical coordinates x.
Definition: CoordinateElement.cpp:89
This class represents a function in a finite element function space , given by.
Definition: Function.h:44
std::shared_ptr< const FunctionSpace > function_space() const
Return shared pointer to function space.
Definition: Function.h:152
std::shared_ptr< const la::Vector< T > > x() const
Underlying vector.
Definition: Function.h:193
This class provides a static adjacency list data structure. It is commonly used to store directed gra...
Definition: AdjacencyList.h:68
tcb::span< T > links(int node)
Get the links (edges) for given node.
Definition: AdjacencyList.h:151
int num_links(int node) const
Number of connections for given node.
Definition: AdjacencyList.h:141
Finite element method functionality.
Definition: assemble_matrix_impl.h:23
array2d< double > interpolation_coords(const fem::FiniteElement &element, const mesh::Mesh &mesh, const tcb::span< const std::int32_t > &cells)
Compute the evaluation points in the physical space at which an expression should be computed to inte...
Definition: interpolate.cpp:15
void interpolate_c(Function< T > &u, const std::function< void(array2d< T > &, const array2d< double > &)> &f, const array2d< double > &x, const tcb::span< const std::int32_t > &cells)
Interpolate an expression f(x)
Definition: interpolate.h:319
void interpolate(Function< T > &u, const Function< T > &v)
Interpolate a finite element Function (on possibly non-matching meshes) in another finite element spa...
Definition: interpolate.h:142