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::size_t gdim =
geometry.dim();
58 std::span<const T> x_g =
geometry.x();
61 const std::size_t num_dofs_g = cmap.
dim();
67 std::array<std::size_t, 4> phi_shape = cmap.
tabulate_shape(0, Xshape[0]);
69 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
70 md::mdspan<
const T, md::extents<std::size_t, 1, md::dynamic_extent,
71 md::dynamic_extent, 1>>
72 phi_full(phi_b.data(), phi_shape);
74 auto phi = md::submdspan(phi_full, 0, md::full_extent, md::full_extent, 0);
78 std::vector<T> coordinate_dofs(num_dofs_g * gdim, 0);
79 std::vector<T> x(3 * (cells.size() * Xshape[0]), 0);
80 for (std::size_t c = 0; c < cells.size(); ++c)
83 auto x_dofs = md::submdspan(x_dofmap, cells[c], md::full_extent);
84 for (std::size_t i = 0; i < x_dofs.size(); ++i)
86 std::copy_n(std::next(x_g.begin(), 3 * x_dofs[i]), gdim,
87 std::next(coordinate_dofs.begin(), i * gdim));
91 for (std::size_t p = 0; p < Xshape[0]; ++p)
93 for (std::size_t j = 0; j < gdim; ++j)
96 for (std::size_t k = 0; k < num_dofs_g; ++k)
97 acc += phi(p, k) * coordinate_dofs[k * gdim + j];
98 x[j * (cells.size() * Xshape[0]) + c * Xshape[0] + p] = acc;
122template <dolfinx::scalar T, std::
floating_po
int U>
123void interpolate(Function<T, U>& u, std::span<const T> f,
124 std::array<std::size_t, 2> fshape,
125 std::span<const std::int32_t> cells);
130template <
typename T, std::
size_t D>
131using mdspan_t = md::mdspan<T, md::dextents<std::size_t, D>>;
152template <dolfinx::scalar T>
153void scatter_values(MPI_Comm comm, std::span<const std::int32_t> src_ranks,
154 std::span<const std::int32_t> dest_ranks,
155 mdspan_t<const T, 2> send_values, std::span<T> recv_values)
157 const std::size_t block_size = send_values.extent(1);
158 assert(src_ranks.size() * block_size == send_values.size());
159 assert(recv_values.size() == dest_ranks.size() * block_size);
162 std::vector<std::int32_t> out_ranks(src_ranks.size());
163 out_ranks.assign(src_ranks.begin(), src_ranks.end());
164 auto [unique_end, range_end] = std::ranges::unique(out_ranks);
165 out_ranks.erase(unique_end, range_end);
166 out_ranks.reserve(out_ranks.size() + 1);
169 std::vector<std::int32_t> in_ranks;
170 in_ranks.reserve(dest_ranks.size());
171 std::copy_if(dest_ranks.begin(), dest_ranks.end(),
172 std::back_inserter(in_ranks),
173 [](
auto rank) { return rank >= 0; });
177 std::ranges::sort(in_ranks);
178 auto [unique_end, range_end] = std::ranges::unique(in_ranks);
179 in_ranks.erase(unique_end, range_end);
181 in_ranks.reserve(in_ranks.size() + 1);
184 MPI_Comm reverse_comm;
185 MPI_Dist_graph_create_adjacent(
186 comm, in_ranks.size(), in_ranks.data(), MPI_UNWEIGHTED, out_ranks.size(),
187 out_ranks.data(), MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &reverse_comm);
189 std::vector<std::int32_t> comm_to_output;
190 std::vector<std::int32_t> recv_sizes(in_ranks.size());
191 recv_sizes.reserve(1);
192 std::vector<std::int32_t> recv_offsets(in_ranks.size() + 1, 0);
195 std::vector<std::pair<std::int32_t, std::int32_t>> rank_to_neighbor;
196 rank_to_neighbor.reserve(in_ranks.size());
197 for (std::size_t i = 0; i < in_ranks.size(); i++)
198 rank_to_neighbor.push_back({in_ranks[i], i});
199 std::ranges::sort(rank_to_neighbor);
202 std::ranges::for_each(
204 [&rank_to_neighbor, &recv_sizes, block_size](
auto rank)
208 auto it = std::ranges::lower_bound(rank_to_neighbor, rank,
210 [](
auto e) {
return e.first; });
211 assert(it != rank_to_neighbor.end() and it->first == rank);
212 recv_sizes[it->second] += block_size;
217 std::partial_sum(recv_sizes.begin(), recv_sizes.end(),
218 std::next(recv_offsets.begin(), 1));
221 comm_to_output.resize(recv_offsets.back() / block_size);
222 std::vector<std::int32_t> recv_counter(recv_sizes.size(), 0);
223 for (std::size_t i = 0; i < dest_ranks.size(); ++i)
225 if (
const std::int32_t rank = dest_ranks[i];
rank >= 0)
227 auto it = std::ranges::lower_bound(rank_to_neighbor, rank,
229 [](
auto e) {
return e.first; });
230 assert(it != rank_to_neighbor.end() and it->first == rank);
231 int insert_pos = recv_offsets[it->second] + recv_counter[it->second];
232 comm_to_output[insert_pos / block_size] = i * block_size;
233 recv_counter[it->second] += block_size;
238 std::vector<std::int32_t> send_sizes(out_ranks.size());
239 send_sizes.reserve(1);
244 std::vector<std::pair<std::int32_t, std::int32_t>> rank_to_neighbor;
245 rank_to_neighbor.reserve(out_ranks.size());
246 for (std::size_t i = 0; i < out_ranks.size(); i++)
247 rank_to_neighbor.push_back({out_ranks[i], i});
251 auto start = rank_to_neighbor.begin();
252 std::ranges::for_each(
254 [&rank_to_neighbor, &send_sizes, block_size, &start](
auto rank)
256 auto it = std::ranges::lower_bound(start, rank_to_neighbor.end(),
257 rank, std::ranges::less(),
258 [](
auto e) { return e.first; });
259 assert(it != rank_to_neighbor.end() and it->first == rank);
260 send_sizes[it->second] += block_size;
266 std::vector<std::int32_t> send_offsets(send_sizes.size() + 1, 0);
267 std::partial_sum(send_sizes.begin(), send_sizes.end(),
268 std::next(send_offsets.begin(), 1));
271 std::vector<T> values(recv_offsets.back());
273 MPI_Neighbor_alltoallv(send_values.data_handle(), send_sizes.data(),
275 values.data(), recv_sizes.data(), recv_offsets.data(),
277 MPI_Comm_free(&reverse_comm);
281 std::ranges::fill(recv_values, T(0));
282 for (std::size_t i = 0; i < comm_to_output.size(); i++)
284 auto vals = std::next(recv_values.begin(), comm_to_output[i]);
285 auto vals_from = std::next(values.begin(), i * block_size);
286 std::copy_n(vals_from, block_size, vals);
298template <MDSpan U, MDSpan V, dolfinx::scalar T>
299void interpolation_apply(U&& Pi, V&& data, std::span<T> coeffs,
int bs)
301 using X =
typename dolfinx::scalar_value_t<T>;
306 assert(data.extent(0) * data.extent(1) == Pi.extent(1));
307 for (std::size_t i = 0; i < Pi.extent(0); ++i)
310 for (std::size_t k = 0; k < data.extent(1); ++k)
311 for (std::size_t j = 0; j < data.extent(0); ++j)
313 +=
static_cast<X
>(Pi(i, k * data.extent(0) + j)) * data(j, k);
318 const std::size_t cols = Pi.extent(1);
319 assert(data.extent(0) == Pi.extent(1));
320 assert(data.extent(1) == bs);
321 for (
int k = 0; k < bs; ++k)
323 for (std::size_t i = 0; i < Pi.extent(0); ++i)
326 for (std::size_t j = 0; j < cols; ++j)
327 acc +=
static_cast<X
>(Pi(i, j)) * data(j, k);
328 coeffs[bs * i + k] = acc;
353template <dolfinx::scalar T, std::
floating_po
int U>
354void interpolate_same_map(Function<T, U>& u1,
const Function<T, U>& u0,
355 std::span<const std::int32_t> cells1,
356 std::span<const std::int32_t> cells0)
358 auto V0 = u0.function_space();
360 auto V1 = u1.function_space();
362 auto mesh0 = V0->mesh();
365 auto mesh1 = V1->mesh();
368 auto element0 = V0->element();
370 auto element1 = V1->element();
373 assert(mesh0->topology()->dim());
374 const int tdim = mesh0->topology()->dim();
375 auto map = mesh0->topology()->index_map(tdim);
377 std::span<T> u1_array = u1.x()->mutable_array();
378 std::span<const T> u0_array = u0.x()->array();
380 std::span<const std::uint32_t> cell_info0;
381 std::span<const std::uint32_t> cell_info1;
382 if (element1->needs_dof_transformations()
383 or element0->needs_dof_transformations())
385 mesh0->topology_mutable()->create_entity_permutations();
386 cell_info0 = std::span(mesh0->topology()->get_cell_permutation_info());
387 mesh1->topology_mutable()->create_entity_permutations();
388 cell_info1 = std::span(mesh1->topology()->get_cell_permutation_info());
392 auto dofmap1 = V1->dofmap();
393 auto dofmap0 = V0->dofmap();
396 const int bs1 = dofmap1->bs();
397 const int bs0 = dofmap0->bs();
398 auto apply_dof_transformation = element0->template dof_transformation_fn<T>(
400 auto apply_inverse_dof_transform
401 = element1->template dof_transformation_fn<T>(
405 std::vector<T> local0(element0->space_dimension());
406 std::vector<T> local1(element1->space_dimension());
409 const auto [i_m, im_shape]
410 = element1->create_interpolation_operator(*element0);
413 using X =
typename dolfinx::scalar_value_t<T>;
414 for (std::size_t c = 0; c < cells0.size(); c++)
417 std::span<const std::int32_t> dofs0 = dofmap0->cell_dofs(cells0[c]);
418 for (std::size_t i = 0; i < dofs0.size(); ++i)
419 for (
int k = 0; k < bs0; ++k)
420 local0[bs0 * i + k] = u0_array[bs0 * dofs0[i] + k];
422 apply_dof_transformation(local0, cell_info0, cells0[c], 1);
426 std::ranges::fill(local1, 0);
427 for (std::size_t i = 0; i < im_shape[0]; ++i)
428 for (std::size_t j = 0; j < im_shape[1]; ++j)
429 local1[i] +=
static_cast<X
>(i_m[im_shape[1] * i + j]) * local0[j];
431 apply_inverse_dof_transform(local1, cell_info1, cells1[c], 1);
432 std::span<const std::int32_t> dofs1 = dofmap1->cell_dofs(cells1[c]);
433 for (std::size_t i = 0; i < dofs1.size(); ++i)
434 for (
int k = 0; k < bs1; ++k)
435 u1_array[bs1 * dofs1[i] + k] = local1[bs1 * i + k];
453template <dolfinx::scalar T, std::
floating_po
int U>
454void interpolate_nonmatching_maps(Function<T, U>& u1,
455 std::span<const std::int32_t> cells1,
456 const Function<T, U>& u0,
457 std::span<const std::int32_t> cells0)
460 auto V0 = u0.function_space();
462 auto mesh0 = V0->mesh();
466 const int tdim = mesh0->topology()->dim();
467 const int gdim = mesh0->geometry().dim();
470 auto V1 = u1.function_space();
472 auto mesh1 = V1->mesh();
474 auto element0 = V0->element();
476 auto element1 = V1->element();
479 std::span<const std::uint32_t> cell_info0;
480 std::span<const std::uint32_t> cell_info1;
481 if (element1->needs_dof_transformations()
482 or element0->needs_dof_transformations())
484 mesh0->topology_mutable()->create_entity_permutations();
485 cell_info0 = std::span(mesh0->topology()->get_cell_permutation_info());
486 mesh1->topology_mutable()->create_entity_permutations();
487 cell_info1 = std::span(mesh1->topology()->get_cell_permutation_info());
491 auto dofmap0 = V0->dofmap();
492 auto dofmap1 = V1->dofmap();
494 const auto [X, Xshape] = element1->interpolation_points();
497 const int bs0 = element0->block_size();
498 const int bs1 = element1->block_size();
499 auto apply_dof_transformation0 = element0->template dof_transformation_fn<U>(
501 auto apply_inverse_dof_transform1
502 = element1->template dof_transformation_fn<T>(
506 const std::size_t dim0 = element0->space_dimension() / bs0;
507 const std::size_t value_size_ref0 = element0->reference_value_size();
508 const std::size_t value_size0 = V0->element()->reference_value_size();
510 const CoordinateElement<U>& cmap = mesh0->geometry().cmap();
511 auto x_dofmap = mesh0->geometry().dofmap();
512 const std::size_t num_dofs_g = cmap.dim();
513 std::span<const U> x_g = mesh0->geometry().x();
519 const std::array<std::size_t, 4> phi_shape
520 = cmap.tabulate_shape(1, Xshape[0]);
521 std::vector<U> phi_b(
522 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
523 md::mdspan<
const U, md::extents<std::size_t, md::dynamic_extent,
524 md::dynamic_extent, md::dynamic_extent, 1>>
525 phi(phi_b.data(), phi_shape);
526 cmap.tabulate(1, X, Xshape, phi_b);
529 const auto [_basis_derivatives_reference0, b0shape]
530 = element0->tabulate(X, Xshape, 0);
531 md::mdspan<
const U, std::extents<std::size_t, 1, md::dynamic_extent,
532 md::dynamic_extent, md::dynamic_extent>>
533 basis_derivatives_reference0(_basis_derivatives_reference0.data(),
537 std::vector<T> local1(element1->space_dimension());
538 std::vector<T> coeffs0(element0->space_dimension());
540 std::vector<U> basis0_b(Xshape[0] * dim0 * value_size0);
541 md::mdspan<U, std::dextents<std::size_t, 3>> basis0(
542 basis0_b.data(), Xshape[0], dim0, value_size0);
544 std::vector<U> basis_reference0_b(Xshape[0] * dim0 * value_size_ref0);
545 md::mdspan<U, std::dextents<std::size_t, 3>> basis_reference0(
546 basis_reference0_b.data(), Xshape[0], dim0, value_size_ref0);
548 std::vector<T> values0_b(Xshape[0] * 1 * V1->element()->value_size());
550 T, md::extents<std::size_t, md::dynamic_extent, 1, md::dynamic_extent>>
551 values0(values0_b.data(), Xshape[0], 1, V1->element()->value_size());
553 std::vector<T> mapped_values_b(Xshape[0] * 1 * V1->element()->value_size());
555 T, md::extents<std::size_t, md::dynamic_extent, 1, md::dynamic_extent>>
556 mapped_values0(mapped_values_b.data(), Xshape[0], 1,
557 V1->element()->value_size());
559 std::vector<U> coord_dofs_b(num_dofs_g * gdim);
560 md::mdspan<U, std::dextents<std::size_t, 2>> coord_dofs(coord_dofs_b.data(),
563 std::vector<U> J_b(Xshape[0] * gdim * tdim);
564 md::mdspan<U, std::dextents<std::size_t, 3>> J(J_b.data(), Xshape[0], gdim,
566 std::vector<U> K_b(Xshape[0] * tdim * gdim);
567 md::mdspan<U, std::dextents<std::size_t, 3>> K(K_b.data(), Xshape[0], tdim,
569 std::vector<U> detJ(Xshape[0]);
570 std::vector<U> det_scratch(2 * gdim * tdim);
573 const auto [_Pi_1, pi_shape] = element1->interpolation_operator();
574 impl::mdspan_t<const U, 2> Pi_1(_Pi_1.data(), pi_shape);
576 using u_t = md::mdspan<U, std::dextents<std::size_t, 2>>;
577 using U_t = md::mdspan<const U, std::dextents<std::size_t, 2>>;
578 using J_t = md::mdspan<const U, std::dextents<std::size_t, 2>>;
579 using K_t = md::mdspan<const U, std::dextents<std::size_t, 2>>;
580 auto push_forward_fn0
581 = element0->basix_element().template map_fn<u_t, U_t, J_t, K_t>();
583 using v_t = md::mdspan<const T, std::dextents<std::size_t, 2>>;
584 using V_t =
decltype(md::submdspan(mapped_values0, 0, md::full_extent,
587 = element1->basix_element().template map_fn<V_t, v_t, K_t, J_t>();
590 std::span<const T> array0 = u0.x()->array();
591 std::span<T> array1 = u1.x()->mutable_array();
592 for (std::size_t c = 0; c < cells0.size(); c++)
595 auto x_dofs = md::submdspan(x_dofmap, cells0[c], md::full_extent);
596 for (std::size_t i = 0; i < num_dofs_g; ++i)
598 const int pos = 3 * x_dofs[i];
599 for (
int j = 0; j < gdim; ++j)
600 coord_dofs(i, j) = x_g[pos + j];
604 std::ranges::fill(J_b, 0);
605 for (std::size_t p = 0; p < Xshape[0]; ++p)
608 = md::submdspan(phi, std::pair(1, tdim + 1), p, md::full_extent, 0);
609 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
610 cmap.compute_jacobian(dphi, coord_dofs, _J);
611 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
612 cmap.compute_jacobian_inverse(_J, _K);
613 detJ[p] = cmap.compute_jacobian_determinant(_J, det_scratch);
618 for (std::size_t k0 = 0; k0 < basis_reference0.extent(0); ++k0)
619 for (std::size_t k1 = 0; k1 < basis_reference0.extent(1); ++k1)
620 for (std::size_t k2 = 0; k2 < basis_reference0.extent(2); ++k2)
621 basis_reference0(k0, k1, k2)
622 = basis_derivatives_reference0(0, k0, k1, k2);
624 for (std::size_t p = 0; p < Xshape[0]; ++p)
626 apply_dof_transformation0(
627 std::span(basis_reference0_b.data() + p * dim0 * value_size_ref0,
628 dim0 * value_size_ref0),
629 cell_info0, cells0[c], value_size_ref0);
632 for (std::size_t i = 0; i < basis0.extent(0); ++i)
634 auto _u = md::submdspan(basis0, i, md::full_extent, md::full_extent);
635 auto _U = md::submdspan(basis_reference0, i, md::full_extent,
637 auto _K = md::submdspan(K, i, md::full_extent, md::full_extent);
638 auto _J = md::submdspan(J, i, md::full_extent, md::full_extent);
639 push_forward_fn0(_u, _U, _J, detJ[i], _K);
643 const int dof_bs0 = dofmap0->bs();
644 std::span<const std::int32_t> dofs0 = dofmap0->cell_dofs(cells0[c]);
645 for (std::size_t i = 0; i < dofs0.size(); ++i)
646 for (
int k = 0; k < dof_bs0; ++k)
647 coeffs0[dof_bs0 * i + k] = array0[dof_bs0 * dofs0[i] + k];
650 using X =
typename dolfinx::scalar_value_t<T>;
651 for (std::size_t p = 0; p < Xshape[0]; ++p)
653 for (
int k = 0; k < bs0; ++k)
655 for (std::size_t j = 0; j < value_size0; ++j)
658 for (std::size_t i = 0; i < dim0; ++i)
659 acc += coeffs0[bs0 * i + k] *
static_cast<X
>(basis0(p, i, j));
660 values0(p, 0, j * bs0 + k) = acc;
666 for (std::size_t i = 0; i < values0.extent(0); ++i)
668 auto _u = md::submdspan(values0, i, md::full_extent, md::full_extent);
670 = md::submdspan(mapped_values0, i, md::full_extent, md::full_extent);
671 auto _K = md::submdspan(K, i, md::full_extent, md::full_extent);
672 auto _J = md::submdspan(J, i, md::full_extent, md::full_extent);
673 pull_back_fn1(_U, _u, _K, 1.0 / detJ[i], _J);
677 = md::submdspan(mapped_values0, md::full_extent, 0, md::full_extent);
678 interpolation_apply(Pi_1, values, std::span(local1), bs1);
679 apply_inverse_dof_transform1(local1, cell_info1, cells1[c], 1);
682 const int dof_bs1 = dofmap1->bs();
683 std::span<const std::int32_t> dofs1 = dofmap1->cell_dofs(c);
684 for (std::size_t i = 0; i < dofs1.size(); ++i)
685 for (
int k = 0; k < dof_bs1; ++k)
686 array1[dof_bs1 * dofs1[i] + k] = local1[dof_bs1 * i + k];
693template <dolfinx::scalar T, std::
floating_po
int U>
695 std::array<std::size_t, 2> fshape,
696 std::span<const std::int32_t> cells)
698 auto element = u.function_space()->element();
700 const int element_bs = element->block_size();
701 if (
int num_sub = element->num_sub_elements();
702 num_sub > 0 and num_sub != element_bs)
704 throw std::runtime_error(
"Cannot directly interpolate a mixed space. "
705 "Interpolate into subspaces.");
709 assert(u.function_space());
710 auto mesh = u.function_space()->mesh();
713 const int gdim =
mesh->geometry().dim();
714 const int tdim =
mesh->topology()->dim();
716 if (fshape[0] != (std::size_t)u.function_space()->element()->value_size())
717 throw std::runtime_error(
"Interpolation data has the wrong shape/size.");
719 std::span<const std::uint32_t> cell_info;
720 if (element->needs_dof_transformations())
722 mesh->topology_mutable()->create_entity_permutations();
723 cell_info = std::span(
mesh->topology()->get_cell_permutation_info());
726 const std::size_t f_shape1
727 = f.size() / u.function_space()->element()->value_size();
728 md::mdspan<const T, md::dextents<std::size_t, 2>> _f(f.data(), fshape);
731 const auto dofmap = u.function_space()->dofmap();
733 const int dofmap_bs = dofmap->bs();
736 const int num_scalar_dofs = element->space_dimension() / element_bs;
737 const int value_size = u.function_space()->element()->reference_value_size();
739 std::span<T> coeffs = u.x()->mutable_array();
740 std::vector<T> _coeffs(num_scalar_dofs);
744 const bool symmetric = u.function_space()->symmetric();
745 if (element->map_ident() && element->interpolation_ident())
750 auto apply_inv_transpose_dof_transformation
751 = element->template dof_transformation_fn<T>(
756 std::size_t matrix_size = 0;
757 while (matrix_size * matrix_size < fshape[0])
761 for (std::size_t c = 0; c < cells.size(); ++c)
772 std::size_t rowstart = 0;
773 const std::int32_t
cell = cells[c];
774 std::span<const std::int32_t> dofs = dofmap->cell_dofs(
cell);
775 for (
int k = 0; k < element_bs; ++k)
777 if (k - rowstart > row)
786 std::next(f.begin(), (row * matrix_size + k - rowstart) * f_shape1
787 + 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];
802 for (std::size_t c = 0; c < cells.size(); ++c)
804 const std::int32_t
cell = cells[c];
805 std::span<const std::int32_t> dofs = dofmap->cell_dofs(
cell);
806 for (
int k = 0; k < element_bs; ++k)
810 std::copy_n(std::next(f.begin(), k * f_shape1 + c * num_scalar_dofs),
811 num_scalar_dofs, _coeffs.begin());
812 apply_inv_transpose_dof_transformation(_coeffs, cell_info,
cell, 1);
813 for (
int i = 0; i < num_scalar_dofs; ++i)
815 const int dof = i * element_bs + k;
816 std::div_t pos = std::div(dof, dofmap_bs);
817 coeffs[dofmap_bs * dofs[pos.quot] + pos.rem] = _coeffs[i];
823 else if (element->map_ident())
830 throw std::runtime_error(
831 "Interpolation into this element not supported.");
835 = u.function_space()->element()->reference_value_size();
837 if (element_vs > 1 and element_bs > 1)
839 throw std::runtime_error(
840 "Interpolation into this element not supported.");
844 const auto [_Pi, pi_shape] = element->interpolation_operator();
845 md::mdspan<const U, std::dextents<std::size_t, 2>> Pi(_Pi.data(), pi_shape);
846 const std::size_t num_interp_points = Pi.extent(1);
847 assert(Pi.extent(0) == num_scalar_dofs);
849 auto apply_inv_transpose_dof_transformation
850 = element->template dof_transformation_fn<T>(
854 std::vector<T> ref_data_b(num_interp_points);
855 md::mdspan<T, md::extents<std::size_t, md::dynamic_extent, 1>> ref_data(
856 ref_data_b.data(), num_interp_points, 1);
857 for (std::size_t c = 0; c < cells.size(); ++c)
859 const std::int32_t
cell = cells[c];
860 std::span<const std::int32_t> dofs = dofmap->cell_dofs(
cell);
861 for (
int k = 0; k < element_bs; ++k)
863 for (
int i = 0; i < element_vs; ++i)
866 std::next(f.begin(), (i + k) * f_shape1
867 + c * num_interp_points / element_vs),
868 num_interp_points / element_vs,
869 std::next(ref_data_b.begin(),
870 i * num_interp_points / element_vs));
872 impl::interpolation_apply(Pi, ref_data, std::span(_coeffs), 1);
873 apply_inv_transpose_dof_transformation(_coeffs, cell_info,
cell, 1);
874 for (
int i = 0; i < num_scalar_dofs; ++i)
876 const int dof = i * element_bs + k;
877 std::div_t pos = std::div(dof, dofmap_bs);
878 coeffs[dofmap_bs * dofs[pos.quot] + pos.rem] = _coeffs[i];
887 throw std::runtime_error(
888 "Interpolation into this element not supported.");
891 const auto [X, Xshape] = element->interpolation_points();
894 throw std::runtime_error(
895 "Interpolation into this space is not yet supported.");
898 if (_f.extent(1) != cells.size() * Xshape[0])
899 throw std::runtime_error(
"Interpolation data has the wrong shape.");
905 auto x_dofmap =
mesh->geometry().dofmap();
906 const int num_dofs_g = cmap.
dim();
907 std::span<const U> x_g =
mesh->geometry().x();
910 std::vector<U> J_b(Xshape[0] * gdim * tdim);
911 md::mdspan<U, std::dextents<std::size_t, 3>> J(J_b.data(), Xshape[0], gdim,
913 std::vector<U> K_b(Xshape[0] * tdim * gdim);
914 md::mdspan<U, std::dextents<std::size_t, 3>> K(K_b.data(), Xshape[0], tdim,
916 std::vector<U> detJ(Xshape[0]);
917 std::vector<U> det_scratch(2 * gdim * tdim);
919 std::vector<U> coord_dofs_b(num_dofs_g * gdim);
920 md::mdspan<U, std::dextents<std::size_t, 2>> coord_dofs(coord_dofs_b.data(),
922 const std::size_t value_size_ref = element->reference_value_size();
923 std::vector<T> ref_data_b(Xshape[0] * 1 * value_size_ref);
925 T, md::extents<std::size_t, md::dynamic_extent, 1, md::dynamic_extent>>
926 ref_data(ref_data_b.data(), Xshape[0], 1, value_size_ref);
928 std::vector<T> _vals_b(Xshape[0] * 1 * value_size);
930 T, md::extents<std::size_t, md::dynamic_extent, 1, md::dynamic_extent>>
931 _vals(_vals_b.data(), Xshape[0], 1, value_size);
935 std::array<std::size_t, 4> phi_shape = cmap.
tabulate_shape(1, Xshape[0]);
936 std::vector<U> phi_b(
937 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
938 md::mdspan<
const U, md::extents<std::size_t, md::dynamic_extent,
939 md::dynamic_extent, md::dynamic_extent, 1>>
940 phi(phi_b.data(), phi_shape);
942 auto dphi = md::submdspan(phi, std::pair(1, tdim + 1), md::full_extent,
945 const std::function<void(std::span<T>, std::span<const std::uint32_t>,
947 apply_inverse_transpose_dof_transformation
948 = element->template dof_transformation_fn<T>(
952 const auto [_Pi, pi_shape] = element->interpolation_operator();
953 md::mdspan<const U, std::dextents<std::size_t, 2>> Pi(_Pi.data(), pi_shape);
955 using u_t = md::mdspan<const T, md::dextents<std::size_t, 2>>;
956 using U_t =
decltype(md::submdspan(ref_data, 0, md::full_extent,
958 using J_t = md::mdspan<const U, md::dextents<std::size_t, 2>>;
959 using K_t = md::mdspan<const U, md::dextents<std::size_t, 2>>;
961 = element->basix_element().template map_fn<U_t, u_t, J_t, K_t>();
963 for (std::size_t c = 0; c < cells.size(); ++c)
965 const std::int32_t
cell = cells[c];
966 auto x_dofs = md::submdspan(x_dofmap,
cell, md::full_extent);
967 for (
int i = 0; i < num_dofs_g; ++i)
969 const int pos = 3 * x_dofs[i];
970 for (
int j = 0; j < gdim; ++j)
971 coord_dofs(i, j) = x_g[pos + j];
975 std::ranges::fill(J_b, 0);
976 for (std::size_t p = 0; p < Xshape[0]; ++p)
978 auto _dphi = md::submdspan(dphi, md::full_extent, p, md::full_extent);
979 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
981 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
986 std::span<const std::int32_t> dofs = dofmap->cell_dofs(
cell);
987 for (
int k = 0; k < element_bs; ++k)
990 for (
int m = 0; m < value_size; ++m)
992 for (std::size_t k0 = 0; k0 < Xshape[0]; ++k0)
995 = f[f_shape1 * (k * value_size + m) + c * Xshape[0] + k0];
1000 for (std::size_t i = 0; i < Xshape[0]; ++i)
1002 auto _u = md::submdspan(_vals, i, md::full_extent, md::full_extent);
1004 = md::submdspan(ref_data, i, md::full_extent, md::full_extent);
1005 auto _K = md::submdspan(K, i, md::full_extent, md::full_extent);
1006 auto _J = md::submdspan(J, i, md::full_extent, md::full_extent);
1007 pull_back_fn(_U, _u, _K, 1.0 / detJ[i], _J);
1010 auto ref = md::submdspan(ref_data, md::full_extent, 0, md::full_extent);
1011 impl::interpolation_apply(Pi, ref, std::span(_coeffs), element_bs);
1012 apply_inverse_transpose_dof_transformation(_coeffs, cell_info,
cell, 1);
1015 assert(_coeffs.size() == num_scalar_dofs);
1016 for (
int i = 0; i < num_scalar_dofs; ++i)
1018 const int dof = i * element_bs + k;
1019 std::div_t pos = std::div(dof, dofmap_bs);
1020 coeffs[dofmap_bs * dofs[pos.quot] + pos.rem] = _coeffs[i];
1045template <std::
floating_po
int T>
1048 const mesh::Mesh<T>& mesh1, std::span<const std::int32_t> cells, T padding)
1055 std::vector<T> x(coords.size());
1056 std::size_t num_points = coords.size() / 3;
1057 for (std::size_t i = 0; i < num_points; ++i)
1058 for (std::size_t j = 0; j < 3; ++j)
1059 x[3 * i + j] = coords[i + j * num_points];
1077template <dolfinx::scalar T, std::
floating_po
int U>
1079 std::span<const std::int32_t> cells,
1082 auto mesh = u.function_space()->mesh();
1084 MPI_Comm comm =
mesh->comm();
1086 auto mesh_v = v.function_space()->mesh();
1089 MPI_Comm_compare(comm, mesh_v->comm(), &result);
1090 if (result == MPI_UNEQUAL)
1092 throw std::runtime_error(
"Interpolation on different meshes is only "
1093 "supported on the same communicator.");
1097 assert(
mesh->topology());
1098 auto cell_map =
mesh->topology()->index_map(
mesh->topology()->dim());
1100 auto element_u = u.function_space()->element();
1102 const std::size_t value_size = u.function_space()->element()->value_size();
1104 const std::vector<int>& dest_ranks = interpolation_data.
src_owner;
1105 const std::vector<int>& src_ranks = interpolation_data.
dest_owners;
1106 const std::vector<U>& recv_points = interpolation_data.
dest_points;
1107 const std::vector<std::int32_t>& evaluation_cells
1111 std::vector<T> send_values(recv_points.size() / 3 * value_size);
1112 v.eval(recv_points, {recv_points.size() / 3, (std::size_t)3},
1113 evaluation_cells, send_values, {recv_points.size() / 3, value_size});
1116 std::vector<T> values_b(dest_ranks.size() * value_size);
1117 md::mdspan<const T, md::dextents<std::size_t, 2>> _send_values(
1118 send_values.data(), src_ranks.size(), value_size);
1119 impl::scatter_values(comm, src_ranks, dest_ranks, _send_values,
1120 std::span(values_b));
1123 md::mdspan<const T, md::dextents<std::size_t, 2>> values(
1124 values_b.data(), dest_ranks.size(), value_size);
1125 std::vector<T> valuesT_b(value_size * dest_ranks.size());
1126 md::mdspan<T, md::dextents<std::size_t, 2>> valuesT(
1127 valuesT_b.data(), value_size, dest_ranks.size());
1128 for (std::size_t i = 0; i < values.extent(0); ++i)
1129 for (std::size_t j = 0; j < values.extent(1); ++j)
1130 valuesT(j, i) = values(i, j);
1152template <dolfinx::scalar T, std::
floating_po
int U>
1156 if (cells0.size() != cells1.size())
1157 throw std::runtime_error(
"Length of cell lists do not match.");
1160 assert(u0.function_space());
1163 assert(cells0.size() == cells1.size());
1165 auto cell_map0 =
mesh->topology()->index_map(
mesh->topology()->dim());
1167 std::size_t num_cells0 = cell_map0->size_local() + cell_map0->num_ghosts();
1169 and cells1.size() == num_cells0)
1172 std::span<T> u1_array = u1.
x()->mutable_array();
1173 std::span<const T> u0_array = u0.x()->array();
1174 std::ranges::copy(u0_array, u1_array.begin());
1179 auto fs0 = u0.function_space();
1180 auto element0 = fs0->element();
1183 auto element1 = fs1->element();
1185 if (!std::ranges::equal(fs0->element()->value_shape(),
1186 fs1->element()->value_shape()))
1188 throw std::runtime_error(
1189 "Interpolation: elements have different value dimensions");
1192 if (element1 == element0 or *element1 == *element0)
1195 const int tdim =
mesh->topology()->dim();
1196 auto cell_map1 =
mesh->topology()->index_map(tdim);
1198 assert(element1->block_size() == element0->block_size());
1201 std::shared_ptr<const DofMap> dofmap0 = u0.function_space()->dofmap();
1203 std::shared_ptr<const DofMap> dofmap1 = u1.
function_space()->dofmap();
1206 std::span<T> u1_array = u1.
x()->mutable_array();
1207 std::span<const T> u0_array = u0.x()->array();
1210 const int bs0 = dofmap0->bs();
1211 const int bs1 = dofmap1->bs();
1212 for (std::size_t c = 0; c < cells1.size(); ++c)
1214 std::span<const std::int32_t> dofs0 = dofmap0->cell_dofs(cells0[c]);
1215 std::span<const std::int32_t> dofs1 = dofmap1->cell_dofs(cells1[c]);
1216 assert(bs0 * dofs0.size() == bs1 * dofs1.size());
1217 for (std::size_t i = 0; i < dofs0.size(); ++i)
1219 for (
int k = 0; k < bs0; ++k)
1221 int index = bs0 * i + k;
1222 std::div_t dv1 = std::div(index, bs1);
1223 u1_array[bs1 * dofs1[dv1.quot] + dv1.rem]
1224 = u0_array[bs0 * dofs0[i] + k];
1229 else if (element1->map_type() == element0->map_type())
1232 impl::interpolate_same_map(u1, u0, cells1, cells0);
1237 impl::interpolate_nonmatching_maps(u1, cells1, u0, cells0);
Degree-of-freedom map representations and tools.
Definition CoordinateElement.h:38
static void compute_jacobian(const U &dphi, const V &cell_geometry, W &&J)
Definition CoordinateElement.h:133
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
static void compute_jacobian_inverse(const U &J, V &&K)
Compute the inverse of the Jacobian.
Definition CoordinateElement.h:142
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 double compute_jacobian_determinant(const U &J, std::span< typename U::value_type > w)
Compute the determinant of the Jacobian.
Definition CoordinateElement.h:159
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
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:281
int rank(MPI_Comm comm)
Return process rank for the communicator.
Definition MPI.cpp:64
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:1046
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:694
@ 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:37
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
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