10#include "CoordinateElement.h"
12#include "FiniteElement.h"
13#include "FunctionSpace.h"
15#include <basix/mdspan.hpp>
17#include <dolfinx/common/IndexMap.h>
18#include <dolfinx/common/types.h>
19#include <dolfinx/geometry/utils.h>
20#include <dolfinx/mesh/Mesh.h>
29template <dolfinx::scalar T, std::
floating_po
int U>
33concept MDSpan =
requires(T x, std::size_t idx) {
35 { x.extent(0) } -> std::integral;
36 { x.extent(1) } -> std::integral;
50template <std::
floating_po
int T>
53 std::span<const std::int32_t> cells)
56 const std::vector<CoordinateElement<T>>& cmaps =
geometry.cmaps();
59 = std::find_if(cmaps.begin(), cmaps.end(), [&cell_type](
const auto& cm)
60 { return cell_type == cm.cell_shape(); });
61 if (it == cmaps.end())
62 throw std::runtime_error(
"Cannot find CoordinateElement for FiniteElement");
63 int index = std::distance(cmaps.begin(), it);
66 const std::size_t gdim =
geometry.dim();
67 auto x_dofmap =
geometry.dofmap(index);
68 std::span<const T> x_g =
geometry.x();
71 const std::size_t num_dofs_g = cmap.
dim();
77 std::array<std::size_t, 4> phi_shape = cmap.
tabulate_shape(0, Xshape[0]);
79 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
80 md::mdspan<
const T, md::extents<std::size_t, 1, md::dynamic_extent,
81 md::dynamic_extent, 1>>
82 phi_full(phi_b.data(), phi_shape);
84 auto phi = md::submdspan(phi_full, 0, md::full_extent, md::full_extent, 0);
88 std::vector<T> coordinate_dofs(num_dofs_g * gdim, 0);
89 std::vector<T> x(3 * (cells.size() * Xshape[0]), 0);
90 for (std::size_t c = 0; c < cells.size(); ++c)
93 auto x_dofs = md::submdspan(x_dofmap, cells[c], md::full_extent);
94 for (std::size_t i = 0; i < x_dofs.size(); ++i)
96 std::copy_n(std::next(x_g.begin(), 3 * x_dofs[i]), gdim,
97 std::next(coordinate_dofs.begin(), i * gdim));
101 for (std::size_t p = 0; p < Xshape[0]; ++p)
103 for (std::size_t j = 0; j < gdim; ++j)
106 for (std::size_t k = 0; k < num_dofs_g; ++k)
107 acc += phi(p, k) * coordinate_dofs[k * gdim + j];
108 x[j * (cells.size() * Xshape[0]) + c * Xshape[0] + p] = acc;
132template <dolfinx::scalar T, std::
floating_po
int U>
133void interpolate(Function<T, U>& u, std::span<const T> f,
134 std::array<std::size_t, 2> fshape,
135 std::span<const std::int32_t> cells);
140template <
typename T, std::
size_t D>
141using mdspan_t = md::mdspan<T, md::dextents<std::size_t, D>>;
162template <dolfinx::scalar T>
163void scatter_values(MPI_Comm comm, std::span<const std::int32_t> src_ranks,
164 std::span<const std::int32_t> dest_ranks,
165 mdspan_t<const T, 2> send_values, std::span<T> recv_values)
167 const std::size_t block_size = send_values.extent(1);
168 assert(src_ranks.size() * block_size == send_values.size());
169 assert(recv_values.size() == dest_ranks.size() * block_size);
172 std::vector<std::int32_t> out_ranks(src_ranks.size());
173 out_ranks.assign(src_ranks.begin(), src_ranks.end());
174 auto [unique_end, range_end] = std::ranges::unique(out_ranks);
175 out_ranks.erase(unique_end, range_end);
176 out_ranks.reserve(out_ranks.size() + 1);
179 std::vector<std::int32_t> in_ranks;
180 in_ranks.reserve(dest_ranks.size());
181 std::copy_if(dest_ranks.begin(), dest_ranks.end(),
182 std::back_inserter(in_ranks),
183 [](
auto rank) { return rank >= 0; });
187 std::ranges::sort(in_ranks);
188 auto [unique_end, range_end] = std::ranges::unique(in_ranks);
189 in_ranks.erase(unique_end, range_end);
191 in_ranks.reserve(in_ranks.size() + 1);
194 MPI_Comm reverse_comm;
195 MPI_Dist_graph_create_adjacent(
196 comm, in_ranks.size(), in_ranks.data(), MPI_UNWEIGHTED, out_ranks.size(),
197 out_ranks.data(), MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &reverse_comm);
199 std::vector<std::int32_t> comm_to_output;
200 std::vector<std::int32_t> recv_sizes(in_ranks.size());
201 recv_sizes.reserve(1);
202 std::vector<std::int32_t> recv_offsets(in_ranks.size() + 1, 0);
205 std::vector<std::pair<std::int32_t, std::int32_t>> rank_to_neighbor;
206 rank_to_neighbor.reserve(in_ranks.size());
207 for (std::size_t i = 0; i < in_ranks.size(); i++)
208 rank_to_neighbor.push_back({in_ranks[i], i});
209 std::ranges::sort(rank_to_neighbor);
212 std::ranges::for_each(
214 [&rank_to_neighbor, &recv_sizes, block_size](
auto rank)
218 auto it = std::ranges::lower_bound(rank_to_neighbor, rank,
220 [](
auto e) {
return e.first; });
221 assert(it != rank_to_neighbor.end() and it->first == rank);
222 recv_sizes[it->second] += block_size;
227 std::partial_sum(recv_sizes.begin(), recv_sizes.end(),
228 std::next(recv_offsets.begin(), 1));
231 comm_to_output.resize(recv_offsets.back() / block_size);
232 std::vector<std::int32_t> recv_counter(recv_sizes.size(), 0);
233 for (std::size_t i = 0; i < dest_ranks.size(); ++i)
235 if (
const std::int32_t rank = dest_ranks[i];
rank >= 0)
237 auto it = std::ranges::lower_bound(rank_to_neighbor, rank,
239 [](
auto e) {
return e.first; });
240 assert(it != rank_to_neighbor.end() and it->first == rank);
241 int insert_pos = recv_offsets[it->second] + recv_counter[it->second];
242 comm_to_output[insert_pos / block_size] = i * block_size;
243 recv_counter[it->second] += block_size;
248 std::vector<std::int32_t> send_sizes(out_ranks.size());
249 send_sizes.reserve(1);
254 std::vector<std::pair<std::int32_t, std::int32_t>> rank_to_neighbor;
255 rank_to_neighbor.reserve(out_ranks.size());
256 for (std::size_t i = 0; i < out_ranks.size(); i++)
257 rank_to_neighbor.push_back({out_ranks[i], i});
261 auto start = rank_to_neighbor.begin();
262 std::ranges::for_each(
264 [&rank_to_neighbor, &send_sizes, block_size, &start](
auto rank)
266 auto it = std::ranges::lower_bound(start, rank_to_neighbor.end(),
267 rank, std::ranges::less(),
268 [](
auto e) { return e.first; });
269 assert(it != rank_to_neighbor.end() and it->first == rank);
270 send_sizes[it->second] += block_size;
276 std::vector<std::int32_t> send_offsets(send_sizes.size() + 1, 0);
277 std::partial_sum(send_sizes.begin(), send_sizes.end(),
278 std::next(send_offsets.begin(), 1));
281 std::vector<T> values(recv_offsets.back());
283 MPI_Neighbor_alltoallv(send_values.data_handle(), send_sizes.data(),
285 values.data(), recv_sizes.data(), recv_offsets.data(),
287 MPI_Comm_free(&reverse_comm);
291 std::ranges::fill(recv_values, T(0));
292 for (std::size_t i = 0; i < comm_to_output.size(); i++)
294 auto vals = std::next(recv_values.begin(), comm_to_output[i]);
295 auto vals_from = std::next(values.begin(), i * block_size);
296 std::copy_n(vals_from, block_size, vals);
308template <MDSpan U, MDSpan V, dolfinx::scalar T>
309void interpolation_apply(U&& Pi, V&& data, std::span<T> coeffs,
int bs)
311 using X =
typename dolfinx::scalar_value_t<T>;
316 assert(data.extent(0) * data.extent(1) == Pi.extent(1));
317 for (std::size_t i = 0; i < Pi.extent(0); ++i)
320 for (std::size_t k = 0; k < data.extent(1); ++k)
321 for (std::size_t j = 0; j < data.extent(0); ++j)
323 +=
static_cast<X
>(Pi(i, k * data.extent(0) + j)) * data(j, k);
328 const std::size_t cols = Pi.extent(1);
329 assert(data.extent(0) == Pi.extent(1));
330 assert(data.extent(1) == bs);
331 for (
int k = 0; k < bs; ++k)
333 for (std::size_t i = 0; i < Pi.extent(0); ++i)
336 for (std::size_t j = 0; j < cols; ++j)
337 acc +=
static_cast<X
>(Pi(i, j)) * data(j, k);
338 coeffs[bs * i + k] = acc;
363template <dolfinx::scalar T, std::
floating_po
int U>
364void interpolate_same_map(Function<T, U>& u1,
const Function<T, U>& u0,
365 std::span<const std::int32_t> cells1,
366 std::span<const std::int32_t> cells0)
368 auto V0 = u0.function_space();
370 auto V1 = u1.function_space();
372 auto mesh0 = V0->mesh();
375 auto mesh1 = V1->mesh();
378 auto element0 = V0->element();
380 auto element1 = V1->element();
383 assert(mesh0->topology()->dim());
384 const int tdim = mesh0->topology()->dim();
385 auto map = mesh0->topology()->index_map(tdim);
387 std::span<T> u1_array = u1.x()->mutable_array();
388 std::span<const T> u0_array = u0.x()->array();
390 std::span<const std::uint32_t> cell_info0;
391 std::span<const std::uint32_t> cell_info1;
392 if (element1->needs_dof_transformations()
393 or element0->needs_dof_transformations())
395 mesh0->topology_mutable()->create_entity_permutations();
396 cell_info0 = std::span(mesh0->topology()->get_cell_permutation_info());
397 mesh1->topology_mutable()->create_entity_permutations();
398 cell_info1 = std::span(mesh1->topology()->get_cell_permutation_info());
402 auto dofmap1 = V1->dofmap();
403 auto dofmap0 = V0->dofmap();
406 const int bs1 = dofmap1->bs();
407 const int bs0 = dofmap0->bs();
408 auto apply_dof_transformation = element0->template dof_transformation_fn<T>(
410 auto apply_inverse_dof_transform
411 = element1->template dof_transformation_fn<T>(
415 std::vector<T> local0(element0->space_dimension());
416 std::vector<T> local1(element1->space_dimension());
419 const auto [i_m, im_shape]
420 = element1->create_interpolation_operator(*element0);
423 using X =
typename dolfinx::scalar_value_t<T>;
424 for (std::size_t c = 0; c < cells0.size(); c++)
427 std::span<const std::int32_t> dofs0 = dofmap0->cell_dofs(cells0[c]);
428 for (std::size_t i = 0; i < dofs0.size(); ++i)
429 for (
int k = 0; k < bs0; ++k)
430 local0[bs0 * i + k] = u0_array[bs0 * dofs0[i] + k];
432 apply_dof_transformation(local0, cell_info0, cells0[c], 1);
436 std::ranges::fill(local1, 0);
437 for (std::size_t i = 0; i < im_shape[0]; ++i)
438 for (std::size_t j = 0; j < im_shape[1]; ++j)
439 local1[i] +=
static_cast<X
>(i_m[im_shape[1] * i + j]) * local0[j];
441 apply_inverse_dof_transform(local1, cell_info1, cells1[c], 1);
442 std::span<const std::int32_t> dofs1 = dofmap1->cell_dofs(cells1[c]);
443 for (std::size_t i = 0; i < dofs1.size(); ++i)
444 for (
int k = 0; k < bs1; ++k)
445 u1_array[bs1 * dofs1[i] + k] = local1[bs1 * i + k];
463template <dolfinx::scalar T, std::
floating_po
int U>
464void interpolate_nonmatching_maps(Function<T, U>& u1,
465 std::span<const std::int32_t> cells1,
466 const Function<T, U>& u0,
467 std::span<const std::int32_t> cells0)
470 auto V0 = u0.function_space();
472 auto mesh0 = V0->mesh();
476 const int tdim = mesh0->topology()->dim();
477 const int gdim = mesh0->geometry().dim();
480 auto V1 = u1.function_space();
482 auto mesh1 = V1->mesh();
484 auto element0 = V0->element();
486 auto element1 = V1->element();
489 std::span<const std::uint32_t> cell_info0;
490 std::span<const std::uint32_t> cell_info1;
491 if (element1->needs_dof_transformations()
492 or element0->needs_dof_transformations())
494 mesh0->topology_mutable()->create_entity_permutations();
495 cell_info0 = std::span(mesh0->topology()->get_cell_permutation_info());
496 mesh1->topology_mutable()->create_entity_permutations();
497 cell_info1 = std::span(mesh1->topology()->get_cell_permutation_info());
501 auto dofmap0 = V0->dofmap();
502 auto dofmap1 = V1->dofmap();
504 const auto [X, Xshape] = element1->interpolation_points();
507 const int bs0 = element0->block_size();
508 const int bs1 = element1->block_size();
509 auto apply_dof_transformation0 = element0->template dof_transformation_fn<U>(
511 auto apply_inverse_dof_transform1
512 = element1->template dof_transformation_fn<T>(
516 const std::size_t dim0 = element0->space_dimension() / bs0;
517 const std::size_t value_size_ref0 = element0->reference_value_size();
518 const std::size_t value_size0 = V0->element()->reference_value_size();
520 const CoordinateElement<U>& cmap = mesh0->geometry().cmap();
521 auto x_dofmap = mesh0->geometry().dofmap();
522 const std::size_t num_dofs_g = cmap.dim();
523 std::span<const U> x_g = mesh0->geometry().x();
529 const std::array<std::size_t, 4> phi_shape
530 = cmap.tabulate_shape(1, Xshape[0]);
531 std::vector<U> phi_b(
532 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
533 md::mdspan<
const U, md::extents<std::size_t, md::dynamic_extent,
534 md::dynamic_extent, md::dynamic_extent, 1>>
535 phi(phi_b.data(), phi_shape);
536 cmap.tabulate(1, X, Xshape, phi_b);
539 const auto [_basis_derivatives_reference0, b0shape]
540 = element0->tabulate(X, Xshape, 0);
541 md::mdspan<
const U, std::extents<std::size_t, 1, md::dynamic_extent,
542 md::dynamic_extent, md::dynamic_extent>>
543 basis_derivatives_reference0(_basis_derivatives_reference0.data(),
547 std::vector<T> local1(element1->space_dimension());
548 std::vector<T> coeffs0(element0->space_dimension());
550 std::vector<U> basis0_b(Xshape[0] * dim0 * value_size0);
551 md::mdspan<U, std::dextents<std::size_t, 3>> basis0(
552 basis0_b.data(), Xshape[0], dim0, value_size0);
554 std::vector<U> basis_reference0_b(Xshape[0] * dim0 * value_size_ref0);
555 md::mdspan<U, std::dextents<std::size_t, 3>> basis_reference0(
556 basis_reference0_b.data(), Xshape[0], dim0, value_size_ref0);
558 std::vector<T> values0_b(Xshape[0] * 1 * V1->element()->value_size());
560 T, md::extents<std::size_t, md::dynamic_extent, 1, md::dynamic_extent>>
561 values0(values0_b.data(), Xshape[0], 1, V1->element()->value_size());
563 std::vector<T> mapped_values_b(Xshape[0] * 1 * V1->element()->value_size());
565 T, md::extents<std::size_t, md::dynamic_extent, 1, md::dynamic_extent>>
566 mapped_values0(mapped_values_b.data(), Xshape[0], 1,
567 V1->element()->value_size());
569 std::vector<U> coord_dofs_b(num_dofs_g * gdim);
570 md::mdspan<U, std::dextents<std::size_t, 2>> coord_dofs(coord_dofs_b.data(),
573 std::vector<U> J_b(Xshape[0] * gdim * tdim);
574 md::mdspan<U, std::dextents<std::size_t, 3>> J(J_b.data(), Xshape[0], gdim,
576 std::vector<U> K_b(Xshape[0] * tdim * gdim);
577 md::mdspan<U, std::dextents<std::size_t, 3>> K(K_b.data(), Xshape[0], tdim,
579 std::vector<U> detJ(Xshape[0]);
580 std::vector<U> det_scratch(2 * gdim * tdim);
583 const auto [_Pi_1, pi_shape] = element1->interpolation_operator();
584 impl::mdspan_t<const U, 2> Pi_1(_Pi_1.data(), pi_shape);
586 using u_t = md::mdspan<U, std::dextents<std::size_t, 2>>;
587 using U_t = md::mdspan<const U, std::dextents<std::size_t, 2>>;
588 using J_t = md::mdspan<const U, std::dextents<std::size_t, 2>>;
589 using K_t = md::mdspan<const U, std::dextents<std::size_t, 2>>;
590 auto push_forward_fn0
591 = element0->basix_element().template map_fn<u_t, U_t, J_t, K_t>();
593 using v_t = md::mdspan<const T, std::dextents<std::size_t, 2>>;
594 using V_t =
decltype(md::submdspan(mapped_values0, 0, md::full_extent,
597 = element1->basix_element().template map_fn<V_t, v_t, K_t, J_t>();
600 std::span<const T> array0 = u0.x()->array();
601 std::span<T> array1 = u1.x()->mutable_array();
602 for (std::size_t c = 0; c < cells0.size(); c++)
605 auto x_dofs = md::submdspan(x_dofmap, cells0[c], md::full_extent);
606 for (std::size_t i = 0; i < num_dofs_g; ++i)
608 const int pos = 3 * x_dofs[i];
609 for (
int j = 0; j < gdim; ++j)
610 coord_dofs(i, j) = x_g[pos + j];
614 std::ranges::fill(J_b, 0);
615 for (std::size_t p = 0; p < Xshape[0]; ++p)
618 = md::submdspan(phi, std::pair(1, tdim + 1), p, md::full_extent, 0);
619 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
620 cmap.compute_jacobian(dphi, coord_dofs, _J);
621 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
622 cmap.compute_jacobian_inverse(_J, _K);
623 detJ[p] = cmap.compute_jacobian_determinant(_J, det_scratch);
628 for (std::size_t k0 = 0; k0 < basis_reference0.extent(0); ++k0)
629 for (std::size_t k1 = 0; k1 < basis_reference0.extent(1); ++k1)
630 for (std::size_t k2 = 0; k2 < basis_reference0.extent(2); ++k2)
631 basis_reference0(k0, k1, k2)
632 = basis_derivatives_reference0(0, k0, k1, k2);
634 for (std::size_t p = 0; p < Xshape[0]; ++p)
636 apply_dof_transformation0(
637 std::span(basis_reference0_b.data() + p * dim0 * value_size_ref0,
638 dim0 * value_size_ref0),
639 cell_info0, cells0[c], value_size_ref0);
642 for (std::size_t i = 0; i < basis0.extent(0); ++i)
644 auto _u = md::submdspan(basis0, i, md::full_extent, md::full_extent);
645 auto _U = md::submdspan(basis_reference0, i, md::full_extent,
647 auto _K = md::submdspan(K, i, md::full_extent, md::full_extent);
648 auto _J = md::submdspan(J, i, md::full_extent, md::full_extent);
649 push_forward_fn0(_u, _U, _J, detJ[i], _K);
653 const int dof_bs0 = dofmap0->bs();
654 std::span<const std::int32_t> dofs0 = dofmap0->cell_dofs(cells0[c]);
655 for (std::size_t i = 0; i < dofs0.size(); ++i)
656 for (
int k = 0; k < dof_bs0; ++k)
657 coeffs0[dof_bs0 * i + k] = array0[dof_bs0 * dofs0[i] + k];
660 using X =
typename dolfinx::scalar_value_t<T>;
661 for (std::size_t p = 0; p < Xshape[0]; ++p)
663 for (
int k = 0; k < bs0; ++k)
665 for (std::size_t j = 0; j < value_size0; ++j)
668 for (std::size_t i = 0; i < dim0; ++i)
669 acc += coeffs0[bs0 * i + k] *
static_cast<X
>(basis0(p, i, j));
670 values0(p, 0, j * bs0 + k) = acc;
676 for (std::size_t i = 0; i < values0.extent(0); ++i)
678 auto _u = md::submdspan(values0, i, md::full_extent, md::full_extent);
680 = md::submdspan(mapped_values0, i, md::full_extent, md::full_extent);
681 auto _K = md::submdspan(K, i, md::full_extent, md::full_extent);
682 auto _J = md::submdspan(J, i, md::full_extent, md::full_extent);
683 pull_back_fn1(_U, _u, _K, 1.0 / detJ[i], _J);
687 = md::submdspan(mapped_values0, md::full_extent, 0, md::full_extent);
688 interpolation_apply(Pi_1, values, std::span(local1), bs1);
689 apply_inverse_dof_transform1(local1, cell_info1, cells1[c], 1);
692 const int dof_bs1 = dofmap1->bs();
693 std::span<const std::int32_t> dofs1 = dofmap1->cell_dofs(c);
694 for (std::size_t i = 0; i < dofs1.size(); ++i)
695 for (
int k = 0; k < dof_bs1; ++k)
696 array1[dof_bs1 * dofs1[i] + k] = local1[dof_bs1 * i + k];
710template <dolfinx::scalar T, std::
floating_po
int U>
711void point_evaluation(
const FiniteElement<U>& element,
bool symmetric,
712 const DofMap& dofmap, std::span<const std::int32_t> cells,
713 std::span<const std::uint32_t> cell_info,
714 std::span<const T> f, std::array<std::size_t, 2> fshape,
720 const int element_bs = element.block_size();
721 const int num_scalar_dofs = element.space_dimension() / element_bs;
722 const int dofmap_bs = dofmap.bs();
724 std::vector<T> _coeffs(num_scalar_dofs);
726 auto apply_inv_transpose_dof_transformation
727 = element.template dof_transformation_fn<T>(
732 std::size_t matrix_size = 0;
733 while (matrix_size * matrix_size < fshape[0])
736 spdlog::info(
"Loop over cells");
738 for (std::size_t c = 0; c <
cells.size(); ++c)
749 std::size_t rowstart = 0;
751 std::span<const std::int32_t> dofs = dofmap.cell_dofs(
cell);
752 for (
int k = 0; k < element_bs; ++k)
754 if (k - rowstart > row)
763 std::next(f.begin(), (row * matrix_size + k - rowstart) * fshape[1]
764 + c * num_scalar_dofs),
765 num_scalar_dofs, _coeffs.begin());
766 apply_inv_transpose_dof_transformation(_coeffs, cell_info,
cell, 1);
767 for (
int i = 0; i < num_scalar_dofs; ++i)
769 const int dof = i * element_bs + k;
770 std::div_t pos = std::div(dof, dofmap_bs);
771 coeffs[dofmap_bs * dofs[pos.quot] + pos.rem] = _coeffs[i];
779 for (std::size_t c = 0; c <
cells.size(); ++c)
782 std::span<const std::int32_t> dofs = dofmap.cell_dofs(
cell);
783 for (
int k = 0; k < element_bs; ++k)
787 std::copy_n(std::next(f.begin(), k * fshape[1] + c * num_scalar_dofs),
788 num_scalar_dofs, _coeffs.begin());
789 apply_inv_transpose_dof_transformation(_coeffs, cell_info,
cell, 1);
790 for (
int i = 0; i < num_scalar_dofs; ++i)
792 const int dof = i * element_bs + k;
793 std::div_t pos = std::div(dof, dofmap_bs);
794 coeffs[dofmap_bs * dofs[pos.quot] + pos.rem] = _coeffs[i];
811template <dolfinx::scalar T, std::
floating_po
int U>
812void identity_mapped_evaluation(
const FiniteElement<U>& element,
bool symmetric,
813 const DofMap& dofmap,
814 std::span<const std::int32_t> cells,
815 std::span<const std::uint32_t> cell_info,
816 std::span<const T> f,
817 std::array<std::size_t, 2> fshape,
823 const int element_bs = element.block_size();
824 const int num_scalar_dofs = element.space_dimension() / element_bs;
825 const int dofmap_bs = dofmap.bs();
827 std::vector<T> _coeffs(num_scalar_dofs);
831 throw std::runtime_error(
"Interpolation into this element not supported.");
834 const int element_vs = element.reference_value_size();
836 if (element_vs > 1 and element_bs > 1)
838 throw std::runtime_error(
"Interpolation into this element not supported.");
842 const auto [_Pi, pi_shape] = element.interpolation_operator();
843 md::mdspan<const U, std::dextents<std::size_t, 2>> Pi(_Pi.data(), pi_shape);
844 const std::size_t num_interp_points = Pi.extent(1);
845 assert(Pi.extent(0) == num_scalar_dofs);
847 auto apply_inv_transpose_dof_transformation
848 = element.template dof_transformation_fn<T>(
852 std::vector<T> ref_data_b(num_interp_points);
853 md::mdspan<T, md::extents<std::size_t, md::dynamic_extent, 1>> ref_data(
854 ref_data_b.data(), num_interp_points, 1);
855 for (std::size_t c = 0; c <
cells.size(); ++c)
858 std::span<const std::int32_t> dofs = dofmap.cell_dofs(
cell);
859 for (
int k = 0; k < element_bs; ++k)
861 for (
int i = 0; i < element_vs; ++i)
865 (i + k) * fshape[1] + c * num_interp_points / element_vs),
866 num_interp_points / element_vs,
867 std::next(ref_data_b.begin(), i * num_interp_points / element_vs));
869 impl::interpolation_apply(Pi, ref_data, std::span(_coeffs), 1);
870 apply_inv_transpose_dof_transformation(_coeffs, cell_info,
cell, 1);
871 for (
int i = 0; i < num_scalar_dofs; ++i)
873 const int dof = i * element_bs + k;
874 std::div_t pos = std::div(dof, dofmap_bs);
875 coeffs[dofmap_bs * dofs[pos.quot] + pos.rem] = _coeffs[i];
892template <dolfinx::scalar T, std::
floating_po
int U>
893void piola_mapped_evaluation(
const FiniteElement<U>& element,
bool symmetric,
894 const DofMap& dofmap,
895 std::span<const std::int32_t> cells,
896 std::span<const std::uint32_t> cell_info,
897 std::span<const T> f,
898 std::array<std::size_t, 2> fshape,
899 const mesh::Mesh<U>& mesh, std::span<T> coeffs)
902 const int gdim = mesh.geometry().dim();
903 const int tdim = mesh.topology()->dim();
905 const int element_bs = element.block_size();
906 const int num_scalar_dofs = element.space_dimension() / element_bs;
907 const int value_size = element.reference_value_size();
908 const int dofmap_bs = dofmap.bs();
910 std::vector<T> _coeffs(num_scalar_dofs);
911 md::mdspan<const T, md::dextents<std::size_t, 2>> _f(f.data(), fshape);
915 throw std::runtime_error(
"Interpolation into this element not supported.");
918 const auto [X, Xshape] = element.interpolation_points();
921 throw std::runtime_error(
922 "Interpolation into this space is not yet supported.");
925 if (_f.extent(1) !=
cells.size() * Xshape[0])
926 throw std::runtime_error(
"Interpolation data has the wrong shape.");
929 const CoordinateElement<U>& cmap = mesh.geometry().cmap();
932 auto x_dofmap = mesh.geometry().dofmap();
933 const int num_dofs_g = cmap.dim();
934 std::span<const U> x_g = mesh.geometry().x();
937 std::vector<U> J_b(Xshape[0] * gdim * tdim);
938 md::mdspan<U, std::dextents<std::size_t, 3>> J(J_b.data(), Xshape[0], gdim,
940 std::vector<U> K_b(Xshape[0] * tdim * gdim);
941 md::mdspan<U, std::dextents<std::size_t, 3>> K(K_b.data(), Xshape[0], tdim,
943 std::vector<U> detJ(Xshape[0]);
944 std::vector<U> det_scratch(2 * gdim * tdim);
946 std::vector<U> coord_dofs_b(num_dofs_g * gdim);
947 md::mdspan<U, std::dextents<std::size_t, 2>> coord_dofs(coord_dofs_b.data(),
949 const std::size_t value_size_ref = element.reference_value_size();
950 std::vector<T> ref_data_b(Xshape[0] * 1 * value_size_ref);
952 T, md::extents<std::size_t, md::dynamic_extent, 1, md::dynamic_extent>>
953 ref_data(ref_data_b.data(), Xshape[0], 1, value_size_ref);
955 std::vector<T> _vals_b(Xshape[0] * 1 * value_size);
957 T, md::extents<std::size_t, md::dynamic_extent, 1, md::dynamic_extent>>
958 _vals(_vals_b.data(), Xshape[0], 1, value_size);
962 std::array<std::size_t, 4> phi_shape = cmap.tabulate_shape(1, Xshape[0]);
963 std::vector<U> phi_b(
964 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
965 md::mdspan<
const U, md::extents<std::size_t, md::dynamic_extent,
966 md::dynamic_extent, md::dynamic_extent, 1>>
967 phi(phi_b.data(), phi_shape);
968 cmap.tabulate(1, X, Xshape, phi_b);
969 auto dphi = md::submdspan(phi, std::pair(1, tdim + 1), md::full_extent,
972 const std::function<void(std::span<T>, std::span<const std::uint32_t>,
974 apply_inverse_transpose_dof_transformation
975 = element.template dof_transformation_fn<T>(
979 const auto [_Pi, pi_shape] = element.interpolation_operator();
980 md::mdspan<const U, std::dextents<std::size_t, 2>> Pi(_Pi.data(), pi_shape);
982 using u_t = md::mdspan<const T, md::dextents<std::size_t, 2>>;
984 =
decltype(md::submdspan(ref_data, 0, md::full_extent, md::full_extent));
985 using J_t = md::mdspan<const U, md::dextents<std::size_t, 2>>;
986 using K_t = md::mdspan<const U, md::dextents<std::size_t, 2>>;
988 = element.basix_element().template map_fn<U_t, u_t, J_t, K_t>();
990 for (std::size_t c = 0; c <
cells.size(); ++c)
993 auto x_dofs = md::submdspan(x_dofmap,
cell, md::full_extent);
994 for (
int i = 0; i < num_dofs_g; ++i)
996 const int pos = 3 * x_dofs[i];
997 for (
int j = 0; j < gdim; ++j)
998 coord_dofs(i, j) = x_g[pos + j];
1002 std::ranges::fill(J_b, 0);
1003 for (std::size_t p = 0; p < Xshape[0]; ++p)
1005 auto _dphi = md::submdspan(dphi, md::full_extent, p, md::full_extent);
1006 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
1007 cmap.compute_jacobian(_dphi, coord_dofs, _J);
1008 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
1009 cmap.compute_jacobian_inverse(_J, _K);
1010 detJ[p] = cmap.compute_jacobian_determinant(_J, det_scratch);
1013 std::span<const std::int32_t> dofs = dofmap.cell_dofs(
cell);
1014 for (
int k = 0; k < element_bs; ++k)
1017 for (
int m = 0; m < value_size; ++m)
1019 for (std::size_t k0 = 0; k0 < Xshape[0]; ++k0)
1022 = f[fshape[1] * (k * value_size + m) + c * Xshape[0] + k0];
1027 for (std::size_t i = 0; i < Xshape[0]; ++i)
1029 auto _u = md::submdspan(_vals, i, md::full_extent, md::full_extent);
1030 auto _U = md::submdspan(ref_data, i, md::full_extent, md::full_extent);
1031 auto _K = md::submdspan(K, i, md::full_extent, md::full_extent);
1032 auto _J = md::submdspan(J, i, md::full_extent, md::full_extent);
1033 pull_back_fn(_U, _u, _K, 1.0 / detJ[i], _J);
1036 auto ref = md::submdspan(ref_data, md::full_extent, 0, md::full_extent);
1037 impl::interpolation_apply(Pi, ref, std::span(_coeffs), element_bs);
1038 apply_inverse_transpose_dof_transformation(_coeffs, cell_info,
cell, 1);
1041 assert(_coeffs.size() == num_scalar_dofs);
1042 for (
int i = 0; i < num_scalar_dofs; ++i)
1044 const int dof = i * element_bs + k;
1045 std::div_t pos = std::div(dof, dofmap_bs);
1046 coeffs[dofmap_bs * dofs[pos.quot] + pos.rem] = _coeffs[i];
1055template <dolfinx::scalar T, std::
floating_po
int U>
1057 std::array<std::size_t, 2> fshape,
1058 std::span<const std::int32_t> cells)
1061 const int index = 0;
1062 auto element = u.function_space()->elements(index);
1064 const int element_bs = element->block_size();
1065 if (
int num_sub = element->num_sub_elements();
1066 num_sub > 0 and num_sub != element_bs)
1068 throw std::runtime_error(
"Cannot directly interpolate a mixed space. "
1069 "Interpolate into subspaces.");
1073 assert(u.function_space());
1074 auto mesh = u.function_space()->mesh();
1078 != (std::size_t)u.function_space()->elements(index)->value_size()
1079 or f.size() != fshape[0] * fshape[1])
1080 throw std::runtime_error(
"Interpolation data has the wrong shape/size.");
1082 spdlog::debug(
"Check for dof transformation");
1083 std::span<const std::uint32_t> cell_info;
1084 if (element->needs_dof_transformations())
1086 mesh->topology_mutable()->create_entity_permutations();
1087 cell_info = std::span(
mesh->topology()->get_cell_permutation_info());
1090 spdlog::debug(
"Interpolate: get dofmap");
1092 const auto dofmap = u.function_space()->dofmaps(index);
1096 std::span<T> coeffs = u.x()->mutable_array();
1098 const bool symmetric = u.function_space()->symmetric();
1099 if (element->map_ident() && element->interpolation_ident())
1101 spdlog::debug(
"Interpolate: point evaluation");
1104 impl::point_evaluation(*element, symmetric, *dofmap, cells, cell_info, f,
1107 else if (element->map_ident())
1109 spdlog::debug(
"Interpolate: identity-mapped evaluation");
1110 impl::identity_mapped_evaluation(*element, symmetric, *dofmap, cells,
1111 cell_info, f, fshape, coeffs);
1115 spdlog::debug(
"Interpolate: Piola-mapped evaluation");
1116 impl::piola_mapped_evaluation(*element, symmetric, *dofmap, cells,
1117 cell_info, f, fshape, *
mesh, coeffs);
1139template <std::
floating_po
int T>
1142 const mesh::Mesh<T>& mesh1, std::span<const std::int32_t> cells, T padding)
1149 std::vector<T> x(coords.size());
1150 std::size_t num_points = coords.size() / 3;
1151 for (std::size_t i = 0; i < num_points; ++i)
1152 for (std::size_t j = 0; j < 3; ++j)
1153 x[3 * i + j] = coords[i + j * num_points];
1171template <dolfinx::scalar T, std::
floating_po
int U>
1173 std::span<const std::int32_t> cells,
1176 auto mesh = u.function_space()->mesh();
1178 MPI_Comm comm =
mesh->comm();
1180 auto mesh_v = v.function_space()->mesh();
1183 MPI_Comm_compare(comm, mesh_v->comm(), &result);
1184 if (result == MPI_UNEQUAL)
1186 throw std::runtime_error(
"Interpolation on different meshes is only "
1187 "supported on the same communicator.");
1191 assert(
mesh->topology());
1192 auto cell_map =
mesh->topology()->index_map(
mesh->topology()->dim());
1194 auto element_u = u.function_space()->element();
1196 const std::size_t value_size = u.function_space()->element()->value_size();
1198 const std::vector<int>& dest_ranks = interpolation_data.
src_owner;
1199 const std::vector<int>& src_ranks = interpolation_data.
dest_owners;
1200 const std::vector<U>& recv_points = interpolation_data.
dest_points;
1201 const std::vector<std::int32_t>& evaluation_cells
1205 std::vector<T> send_values(recv_points.size() / 3 * value_size);
1206 v.eval(recv_points, {recv_points.size() / 3, (std::size_t)3},
1207 evaluation_cells, send_values, {recv_points.size() / 3, value_size});
1210 std::vector<T> values_b(dest_ranks.size() * value_size);
1211 md::mdspan<const T, md::dextents<std::size_t, 2>> _send_values(
1212 send_values.data(), src_ranks.size(), value_size);
1213 impl::scatter_values(comm, src_ranks, dest_ranks, _send_values,
1214 std::span(values_b));
1217 md::mdspan<const T, md::dextents<std::size_t, 2>> values(
1218 values_b.data(), dest_ranks.size(), value_size);
1219 std::vector<T> valuesT_b(value_size * dest_ranks.size());
1220 md::mdspan<T, md::dextents<std::size_t, 2>> valuesT(
1221 valuesT_b.data(), value_size, dest_ranks.size());
1222 for (std::size_t i = 0; i < values.extent(0); ++i)
1223 for (std::size_t j = 0; j < values.extent(1); ++j)
1224 valuesT(j, i) = values(i, j);
1246template <dolfinx::scalar T, std::
floating_po
int U>
1250 if (cells0.size() != cells1.size())
1251 throw std::runtime_error(
"Length of cell lists do not match.");
1254 assert(u0.function_space());
1257 assert(cells0.size() == cells1.size());
1259 auto cell_map0 =
mesh->topology()->index_map(
mesh->topology()->dim());
1261 std::size_t num_cells0 = cell_map0->size_local() + cell_map0->num_ghosts();
1263 and cells1.size() == num_cells0)
1266 std::span<T> u1_array = u1.
x()->mutable_array();
1267 std::span<const T> u0_array = u0.x()->array();
1268 std::ranges::copy(u0_array, u1_array.begin());
1273 auto fs0 = u0.function_space();
1274 auto element0 = fs0->element();
1277 auto element1 = fs1->element();
1279 if (!std::ranges::equal(fs0->element()->value_shape(),
1280 fs1->element()->value_shape()))
1282 throw std::runtime_error(
1283 "Interpolation: elements have different value dimensions");
1286 if (element1 == element0 or *element1 == *element0)
1289 const int tdim =
mesh->topology()->dim();
1290 auto cell_map1 =
mesh->topology()->index_map(tdim);
1292 assert(element1->block_size() == element0->block_size());
1295 std::shared_ptr<const DofMap> dofmap0 = u0.function_space()->dofmap();
1297 std::shared_ptr<const DofMap> dofmap1 = u1.
function_space()->dofmap();
1300 std::span<T> u1_array = u1.
x()->mutable_array();
1301 std::span<const T> u0_array = u0.x()->array();
1304 const int bs0 = dofmap0->bs();
1305 const int bs1 = dofmap1->bs();
1306 for (std::size_t c = 0; c < cells1.size(); ++c)
1308 std::span<const std::int32_t> dofs0 = dofmap0->cell_dofs(cells0[c]);
1309 std::span<const std::int32_t> dofs1 = dofmap1->cell_dofs(cells1[c]);
1310 assert(bs0 * dofs0.size() == bs1 * dofs1.size());
1311 for (std::size_t i = 0; i < dofs0.size(); ++i)
1313 for (
int k = 0; k < bs0; ++k)
1315 int index = bs0 * i + k;
1316 std::div_t dv1 = std::div(index, bs1);
1317 u1_array[bs1 * dofs1[dv1.quot] + dv1.rem]
1318 = u0_array[bs0 * dofs0[i] + k];
1323 else if (element1->map_type() == element0->map_type())
1326 impl::interpolate_same_map(u1, u0, cells1, cells0);
1331 impl::interpolate_nonmatching_maps(u1, cells1, u0, cells0);
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
Model of a finite element.
Definition FiniteElement.h:57
std::pair< std::vector< geometry_type >, std::array< std::size_t, 2 > > interpolation_points() const
Points on the reference cell at which an expression needs to be evaluated in order to interpolate the...
Definition FiniteElement.cpp:464
mesh::CellType cell_type() const noexcept
Cell shape that the element is defined on.
Definition FiniteElement.cpp:279
std::shared_ptr< const FunctionSpace< geometry_type > > function_space() const
Access the function space.
Definition Function.h:144
std::shared_ptr< const la::Vector< value_type > > x() const
Underlying vector (const version).
Definition Function.h:150
Geometry stores the geometry imposed on a mesh.
Definition Geometry.h:34
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:23
Definition interpolate.h:33
MPI_Datatype mpi_t
Retrieves the MPI data type associated to the provided type.
Definition MPI.h:280
int rank(MPI_Comm comm)
Return process rank for the communicator.
Definition MPI.cpp:64
void cells(la::SparsityPattern &pattern, std::array< std::span< const std::int32_t >, 2 > cells, std::array< std::reference_wrapper< const DofMap >, 2 > dofmaps)
Iterate over cells and insert entries into sparsity pattern.
Definition sparsitybuild.cpp:16
Finite element method functionality.
Definition assemble_expression_impl.h:23
geometry::PointOwnershipData< T > create_interpolation_data(const mesh::Geometry< T > &geometry0, const FiniteElement< T > &element0, const mesh::Mesh< T > &mesh1, std::span< const std::int32_t > cells, T padding)
Generate data needed to interpolate finite element fem::Function's across different meshes.
Definition interpolate.h:1140
void interpolate(Function< T, U > &u, std::span< const T > f, std::array< std::size_t, 2 > fshape, std::span< const std::int32_t > cells)
Interpolate an evaluated expression f(x) in a finite element space.
Definition interpolate.h:1056
@ transpose
Transpose.
Definition FiniteElement.h:28
@ inverse_transpose
Transpose inverse.
Definition FiniteElement.h:30
@ standard
Standard.
Definition FiniteElement.h:27
@ cell
Cell.
Definition Form.h:40
std::vector< T > interpolation_coords(const fem::FiniteElement< T > &element, const mesh::Geometry< T > &geometry, std::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.h:51
Geometry data structures and algorithms.
Definition BoundingBoxTree.h:22
PointOwnershipData< T > determine_point_ownership(const mesh::Mesh< T > &mesh, std::span< const T > points, T padding)
Given a set of points, determine which process is colliding, using the GJK algorithm on cells to dete...
Definition utils.h:686
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
CellType
Cell type identifier.
Definition cell_types.h:22
Information on the ownership of points distributed across processes.
Definition utils.h:30
std::vector< T > dest_points
Points that are owned by current process.
Definition utils.h:35
std::vector< std::int32_t > dest_cells
Definition utils.h:37
std::vector< int > dest_owners
Ranks that sent dest_points to current process.
Definition utils.h:34
std::vector< int > src_owner
Definition utils.h:31