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;
49template <std::
floating_po
int T>
57 for (std::size_t i = 0; i <
geometry.num_maps(); ++i)
59 if (
geometry.cmap(i).cell_shape() == cell_type)
62 throw std::runtime_error(
"Cannot find CoordinateElement for FiniteElement");
64 int index = cmap_index(element.
cell_type());
67 const std::size_t gdim =
geometry.dim();
68 auto x_dofmap =
geometry.dofmap(index);
69 std::span<const T> x_g =
geometry.x();
72 const std::size_t num_dofs_g = cmap.
dim();
78 std::array<std::size_t, 4> phi_shape = cmap.
tabulate_shape(0, Xshape[0]);
80 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
81 md::mdspan<
const T, md::extents<std::size_t, 1, md::dynamic_extent,
82 md::dynamic_extent, 1>>
83 phi_full(phi_b.data(), phi_shape);
85 auto phi = md::submdspan(phi_full, 0, md::full_extent, md::full_extent, 0);
89 std::vector<T> coordinate_dofs(num_dofs_g * gdim, 0);
90 std::vector<T> x(3 * (cells.size() * Xshape[0]), 0);
91 for (
auto cell_it = cells.begin(); cell_it != cells.end(); ++cell_it)
94 auto x_dofs = md::submdspan(x_dofmap, *cell_it, md::full_extent);
95 for (std::size_t i = 0; i < x_dofs.size(); ++i)
97 std::copy_n(std::next(x_g.begin(), 3 * x_dofs[i]), gdim,
98 std::next(coordinate_dofs.begin(), i * gdim));
102 std::size_t offset = std::distance(cells.begin(), cell_it);
103 for (std::size_t p = 0; p < Xshape[0]; ++p)
105 for (std::size_t j = 0; j < gdim; ++j)
108 for (std::size_t k = 0; k < num_dofs_g; ++k)
109 acc += phi(p, k) * coordinate_dofs[k * gdim + j];
110 x[j * (cells.size() * Xshape[0]) + offset * Xshape[0] + p] = acc;
134template <dolfinx::scalar T, std::
floating_po
int U>
135void interpolate(Function<T, U>& u, std::span<const T> f,
136 std::array<std::size_t, 2> fshape,
142template <
typename T, std::
size_t D>
143using mdspan_t = md::mdspan<T, md::dextents<std::size_t, D>>;
164template <dolfinx::scalar T>
165void scatter_values(MPI_Comm comm, std::span<const std::int32_t> src_ranks,
166 std::span<const std::int32_t> dest_ranks,
167 mdspan_t<const T, 2> send_values, std::span<T> recv_values)
169 const std::size_t block_size = send_values.extent(1);
170 assert(src_ranks.size() * block_size == send_values.size());
171 assert(recv_values.size() == dest_ranks.size() * block_size);
174 std::vector<std::int32_t> out_ranks(src_ranks.size());
175 out_ranks.assign(src_ranks.begin(), src_ranks.end());
176 auto [unique_end, range_end] = std::ranges::unique(out_ranks);
177 out_ranks.erase(unique_end, range_end);
178 out_ranks.reserve(out_ranks.size() + 1);
181 std::vector<std::int32_t> in_ranks;
182 in_ranks.reserve(dest_ranks.size());
183 std::copy_if(dest_ranks.begin(), dest_ranks.end(),
184 std::back_inserter(in_ranks),
185 [](
auto rank) { return rank >= 0; });
189 std::ranges::sort(in_ranks);
190 auto [unique_end, range_end] = std::ranges::unique(in_ranks);
191 in_ranks.erase(unique_end, range_end);
193 in_ranks.reserve(in_ranks.size() + 1);
196 MPI_Comm reverse_comm;
197 MPI_Dist_graph_create_adjacent(
198 comm, in_ranks.size(), in_ranks.data(), MPI_UNWEIGHTED, out_ranks.size(),
199 out_ranks.data(), MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &reverse_comm);
201 std::vector<std::int32_t> comm_to_output;
202 std::vector<std::int32_t> recv_sizes(in_ranks.size());
203 recv_sizes.reserve(1);
204 std::vector<std::int32_t> recv_offsets(in_ranks.size() + 1, 0);
207 std::vector<std::pair<std::int32_t, std::int32_t>> rank_to_neighbor;
208 rank_to_neighbor.reserve(in_ranks.size());
209 for (std::size_t i = 0; i < in_ranks.size(); i++)
210 rank_to_neighbor.push_back({in_ranks[i], i});
211 std::ranges::sort(rank_to_neighbor);
214 std::ranges::for_each(
216 [&rank_to_neighbor, &recv_sizes, block_size](
auto rank)
220 auto it = std::ranges::lower_bound(rank_to_neighbor, rank,
222 [](
auto e) {
return e.first; });
223 assert(it != rank_to_neighbor.end() and it->first == rank);
224 recv_sizes[it->second] += block_size;
229 std::partial_sum(recv_sizes.begin(), recv_sizes.end(),
230 std::next(recv_offsets.begin(), 1));
233 comm_to_output.resize(recv_offsets.back() / block_size);
234 std::vector<std::int32_t> recv_counter(recv_sizes.size(), 0);
235 for (std::size_t i = 0; i < dest_ranks.size(); ++i)
237 if (
const std::int32_t rank = dest_ranks[i];
rank >= 0)
239 auto it = std::ranges::lower_bound(rank_to_neighbor, rank,
241 [](
auto e) {
return e.first; });
242 assert(it != rank_to_neighbor.end() and it->first == rank);
243 int insert_pos = recv_offsets[it->second] + recv_counter[it->second];
244 comm_to_output[insert_pos / block_size] = i * block_size;
245 recv_counter[it->second] += block_size;
250 std::vector<std::int32_t> send_sizes(out_ranks.size());
251 send_sizes.reserve(1);
256 std::vector<std::pair<std::int32_t, std::int32_t>> rank_to_neighbor;
257 rank_to_neighbor.reserve(out_ranks.size());
258 for (std::size_t i = 0; i < out_ranks.size(); i++)
259 rank_to_neighbor.push_back({out_ranks[i], i});
263 auto start = rank_to_neighbor.begin();
264 std::ranges::for_each(
266 [&rank_to_neighbor, &send_sizes, block_size, &start](
auto rank)
268 auto it = std::ranges::lower_bound(start, rank_to_neighbor.end(),
269 rank, std::ranges::less(),
270 [](
auto e) { return e.first; });
271 assert(it != rank_to_neighbor.end() and it->first == rank);
272 send_sizes[it->second] += block_size;
278 std::vector<std::int32_t> send_offsets(send_sizes.size() + 1, 0);
279 std::partial_sum(send_sizes.begin(), send_sizes.end(),
280 std::next(send_offsets.begin(), 1));
283 std::vector<T> values(recv_offsets.back());
285 MPI_Neighbor_alltoallv(send_values.data_handle(), send_sizes.data(),
287 values.data(), recv_sizes.data(), recv_offsets.data(),
289 MPI_Comm_free(&reverse_comm);
293 std::ranges::fill(recv_values, T(0));
294 for (std::size_t i = 0; i < comm_to_output.size(); i++)
296 auto vals = std::next(recv_values.begin(), comm_to_output[i]);
297 auto vals_from = std::next(values.begin(), i * block_size);
298 std::copy_n(vals_from, block_size, vals);
310template <MDSpan U, MDSpan V, dolfinx::scalar T>
311void interpolation_apply(U&& Pi, V&& data, std::span<T> coeffs,
int bs)
313 using X =
typename dolfinx::scalar_value_t<T>;
318 assert(data.extent(0) * data.extent(1) == Pi.extent(1));
319 for (std::size_t i = 0; i < Pi.extent(0); ++i)
322 for (std::size_t k = 0; k < data.extent(1); ++k)
323 for (std::size_t j = 0; j < data.extent(0); ++j)
325 +=
static_cast<X
>(Pi(i, k * data.extent(0) + j)) * data(j, k);
330 assert(data.extent(0) == Pi.extent(1));
331 assert(
static_cast<int>(data.extent(1)) == bs);
332 std::size_t cols = Pi.extent(1);
333 for (
int k = 0; k < bs; ++k)
335 for (std::size_t i = 0; i < Pi.extent(0); ++i)
338 for (std::size_t j = 0; j < cols; ++j)
339 acc +=
static_cast<X
>(Pi(i, j)) * data(j, k);
340 coeffs[bs * i + k] = acc;
365template <dolfinx::scalar T, std::
floating_po
int U>
366void interpolate_same_map(Function<T, U>& u1, mesh::CellRange
auto&& cells1,
367 const Function<T, U>& u0,
368 mesh::CellRange
auto&& cells0)
370 auto V0 = u0.function_space();
372 auto V1 = u1.function_space();
374 auto mesh0 = V0->mesh();
377 auto mesh1 = V1->mesh();
380 auto element0 = V0->element();
382 auto element1 = V1->element();
385 assert(mesh0->topology()->dim());
386 const int tdim = mesh0->topology()->dim();
387 auto map = mesh0->topology()->index_map(tdim);
389 std::span<T> u1_array = u1.x()->array();
390 std::span<const T> u0_array = u0.x()->array();
392 std::span<const std::uint32_t> cell_info0;
393 std::span<const std::uint32_t> cell_info1;
394 if (element1->needs_dof_transformations()
395 or element0->needs_dof_transformations())
397 mesh0->topology_mutable()->create_entity_permutations();
398 cell_info0 = std::span(mesh0->topology()->get_cell_permutation_info());
399 mesh1->topology_mutable()->create_entity_permutations();
400 cell_info1 = std::span(mesh1->topology()->get_cell_permutation_info());
404 auto dofmap1 = V1->dofmap();
405 auto dofmap0 = V0->dofmap();
408 const int bs1 = dofmap1->bs();
409 const int bs0 = dofmap0->bs();
410 auto apply_dof_transformation = element0->template dof_transformation_fn<T>(
412 auto apply_inverse_dof_transform
413 = element1->template dof_transformation_fn<T>(
417 std::vector<T> local0(element0->space_dimension());
418 std::vector<T> local1(element1->space_dimension());
421 auto [i_m, im_shape] = element1->create_interpolation_operator(*element0);
424 using X =
typename dolfinx::scalar_value_t<T>;
425 assert(cells0.size() == cells1.size());
426 for (
auto cell0_it = cells0.begin(), cell1_it = cells1.begin();
427 cell0_it != cells0.end() and cell1_it != cells1.end();
428 ++cell0_it, ++cell1_it)
431 std::span<const std::int32_t> dofs0 = dofmap0->cell_dofs(*cell0_it);
432 for (std::size_t i = 0; i < dofs0.size(); ++i)
433 for (
int k = 0; k < bs0; ++k)
434 local0[bs0 * i + k] = u0_array[bs0 * dofs0[i] + k];
436 apply_dof_transformation(local0, cell_info0, *cell0_it, 1);
440 std::ranges::fill(local1, 0);
441 for (std::size_t i = 0; i < im_shape[0]; ++i)
442 for (std::size_t j = 0; j < im_shape[1]; ++j)
443 local1[i] +=
static_cast<X
>(i_m[im_shape[1] * i + j]) * local0[j];
445 apply_inverse_dof_transform(local1, cell_info1, *cell1_it, 1);
446 std::span<const std::int32_t> dofs1 = dofmap1->cell_dofs(*cell1_it);
447 for (std::size_t i = 0; i < dofs1.size(); ++i)
448 for (
int k = 0; k < bs1; ++k)
449 u1_array[bs1 * dofs1[i] + k] = local1[bs1 * i + k];
467template <dolfinx::scalar T, std::
floating_po
int U>
468void interpolate_nonmatching_maps(Function<T, U>& u1,
469 mesh::CellRange
auto&& cells1,
470 const Function<T, U>& u0,
471 mesh::CellRange
auto&& cells0)
474 auto V0 = u0.function_space();
476 auto mesh0 = V0->mesh();
480 const int tdim = mesh0->topology()->dim();
481 const int gdim = mesh0->geometry().dim();
484 auto V1 = u1.function_space();
486 auto mesh1 = V1->mesh();
488 auto element0 = V0->element();
490 auto element1 = V1->element();
493 std::span<const std::uint32_t> cell_info0;
494 std::span<const std::uint32_t> cell_info1;
495 if (element1->needs_dof_transformations()
496 or element0->needs_dof_transformations())
498 mesh0->topology_mutable()->create_entity_permutations();
499 cell_info0 = std::span(mesh0->topology()->get_cell_permutation_info());
500 mesh1->topology_mutable()->create_entity_permutations();
501 cell_info1 = std::span(mesh1->topology()->get_cell_permutation_info());
505 auto dofmap0 = V0->dofmap();
506 auto dofmap1 = V1->dofmap();
508 const auto [X, Xshape] = element1->interpolation_points();
511 const int bs0 = element0->block_size();
512 const int bs1 = element1->block_size();
513 auto apply_dof_transformation0 = element0->template dof_transformation_fn<U>(
515 auto apply_inv_dof_transform1 = element1->template dof_transformation_fn<T>(
519 const std::size_t dim0 = element0->space_dimension() / bs0;
520 const std::size_t value_size_ref0 = element0->reference_value_size();
521 const std::size_t value_size0 = V0->element()->reference_value_size();
523 const CoordinateElement<U>& cmap = mesh0->geometry().cmap();
524 auto x_dofmap = mesh0->geometry().dofmap();
525 std::span<const U> x_g = mesh0->geometry().x();
531 const std::array<std::size_t, 4> phi_shape
532 = cmap.tabulate_shape(1, Xshape[0]);
533 std::vector<U> phi_b(
534 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
535 md::mdspan<
const U, md::extents<std::size_t, md::dynamic_extent,
536 md::dynamic_extent, md::dynamic_extent, 1>>
537 phi(phi_b.data(), phi_shape);
538 cmap.tabulate(1, X, Xshape, phi_b);
541 const auto [_basis_derivatives_reference0, b0shape]
542 = element0->tabulate(X, Xshape, 0);
543 md::mdspan<
const U, std::extents<std::size_t, 1, md::dynamic_extent,
544 md::dynamic_extent, md::dynamic_extent>>
545 basis_derivatives_reference0(_basis_derivatives_reference0.data(),
549 std::vector<T> local1(element1->space_dimension());
550 std::vector<T> coeffs0(element0->space_dimension());
552 std::vector<U> basis0_b(Xshape[0] * dim0 * value_size0);
553 md::mdspan<U, std::dextents<std::size_t, 3>> basis0(
554 basis0_b.data(), Xshape[0], dim0, value_size0);
556 std::vector<U> basis_reference0_b(Xshape[0] * dim0 * value_size_ref0);
557 md::mdspan<U, std::dextents<std::size_t, 3>> basis_reference0(
558 basis_reference0_b.data(), Xshape[0], dim0, value_size_ref0);
560 std::vector<T> values0_b(Xshape[0] * 1 * V1->element()->value_size());
562 T, md::extents<std::size_t, md::dynamic_extent, 1, md::dynamic_extent>>
563 values0(values0_b.data(), Xshape[0], 1, V1->element()->value_size());
565 std::vector<T> mapped_values_b(Xshape[0] * 1 * V1->element()->value_size());
567 T, md::extents<std::size_t, md::dynamic_extent, 1, md::dynamic_extent>>
568 mapped_values0(mapped_values_b.data(), Xshape[0], 1,
569 V1->element()->value_size());
571 const std::size_t num_dofs_g = cmap.dim();
572 std::vector<U> coord_dofs_b(num_dofs_g * gdim);
573 md::mdspan<U, std::dextents<std::size_t, 2>> coord_dofs(coord_dofs_b.data(),
576 std::vector<U> J_b(Xshape[0] * gdim * tdim);
577 md::mdspan<U, std::dextents<std::size_t, 3>> J(J_b.data(), Xshape[0], gdim,
579 std::vector<U> K_b(Xshape[0] * tdim * gdim);
580 md::mdspan<U, std::dextents<std::size_t, 3>> K(K_b.data(), Xshape[0], tdim,
582 std::vector<U> detJ(Xshape[0]);
583 std::vector<U> det_scratch(2 * gdim * tdim);
586 const auto [_Pi_1, pi_shape] = element1->interpolation_operator();
587 impl::mdspan_t<const U, 2> Pi_1(_Pi_1.data(), pi_shape);
589 using u_t = md::mdspan<U, std::dextents<std::size_t, 2>>;
590 using U_t = md::mdspan<const U, std::dextents<std::size_t, 2>>;
591 using J_t = md::mdspan<const U, std::dextents<std::size_t, 2>>;
592 using K_t = md::mdspan<const U, std::dextents<std::size_t, 2>>;
593 auto push_forward_fn0
594 = element0->basix_element().template map_fn<u_t, U_t, J_t, K_t>();
596 using v_t = md::mdspan<const T, std::dextents<std::size_t, 2>>;
597 using V_t =
decltype(md::submdspan(mapped_values0, 0, md::full_extent,
600 = element1->basix_element().template map_fn<V_t, v_t, K_t, J_t>();
603 std::span<const T> array0 = u0.x()->array();
604 std::span<T> array1 = u1.x()->array();
605 assert(cells0.size() == cells1.size());
606 for (
auto cell0_it = cells0.begin(), cell1_it = cells1.begin();
607 cell0_it != cells0.end() and cell1_it != cells0.end();
608 ++cell0_it, ++cell1_it)
611 auto x_dofs = md::submdspan(x_dofmap, *cell0_it, md::full_extent);
612 for (std::size_t i = 0; i < num_dofs_g; ++i)
614 const int pos = 3 * x_dofs[i];
615 for (
int j = 0; j < gdim; ++j)
616 coord_dofs(i, j) = x_g[pos + j];
620 std::ranges::fill(J_b, 0);
621 for (std::size_t p = 0; p < Xshape[0]; ++p)
624 = md::submdspan(phi, std::pair(1, tdim + 1), p, md::full_extent, 0);
625 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
626 cmap.compute_jacobian(dphi, coord_dofs, _J);
627 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
628 cmap.compute_jacobian_inverse(_J, _K);
629 detJ[p] = cmap.compute_jacobian_determinant(_J, det_scratch);
634 for (std::size_t k0 = 0; k0 < basis_reference0.extent(0); ++k0)
635 for (std::size_t k1 = 0; k1 < basis_reference0.extent(1); ++k1)
636 for (std::size_t k2 = 0; k2 < basis_reference0.extent(2); ++k2)
637 basis_reference0(k0, k1, k2)
638 = basis_derivatives_reference0(0, k0, k1, k2);
640 for (std::size_t p = 0; p < Xshape[0]; ++p)
642 apply_dof_transformation0(
643 std::span(basis_reference0_b.data() + p * dim0 * value_size_ref0,
644 dim0 * value_size_ref0),
645 cell_info0, *cell0_it, value_size_ref0);
648 for (std::size_t i = 0; i < basis0.extent(0); ++i)
650 auto _u = md::submdspan(basis0, i, md::full_extent, md::full_extent);
651 auto _U = md::submdspan(basis_reference0, i, md::full_extent,
653 auto _K = md::submdspan(K, i, md::full_extent, md::full_extent);
654 auto _J = md::submdspan(J, i, md::full_extent, md::full_extent);
655 push_forward_fn0(_u, _U, _J, detJ[i], _K);
659 const int dof_bs0 = dofmap0->bs();
660 std::span<const std::int32_t> dofs0 = dofmap0->cell_dofs(*cell0_it);
661 for (std::size_t i = 0; i < dofs0.size(); ++i)
662 for (
int k = 0; k < dof_bs0; ++k)
663 coeffs0[dof_bs0 * i + k] = array0[dof_bs0 * dofs0[i] + k];
666 using X =
typename dolfinx::scalar_value_t<T>;
667 for (std::size_t p = 0; p < Xshape[0]; ++p)
669 for (
int k = 0; k < bs0; ++k)
671 for (std::size_t j = 0; j < value_size0; ++j)
674 for (std::size_t i = 0; i < dim0; ++i)
675 acc += coeffs0[bs0 * i + k] *
static_cast<X
>(basis0(p, i, j));
676 values0(p, 0, j * bs0 + k) = acc;
682 for (std::size_t i = 0; i < values0.extent(0); ++i)
684 auto _u = md::submdspan(values0, i, md::full_extent, md::full_extent);
686 = md::submdspan(mapped_values0, i, md::full_extent, md::full_extent);
687 auto _K = md::submdspan(K, i, md::full_extent, md::full_extent);
688 auto _J = md::submdspan(J, i, md::full_extent, md::full_extent);
689 pull_back_fn1(_U, _u, _K, 1.0 / detJ[i], _J);
693 = md::submdspan(mapped_values0, md::full_extent, 0, md::full_extent);
694 interpolation_apply(Pi_1, values, std::span(local1), bs1);
695 apply_inv_dof_transform1(local1, cell_info1, *cell1_it, 1);
698 const int dof_bs1 = dofmap1->bs();
699 std::span<const std::int32_t> dofs1 = dofmap1->cell_dofs(*cell1_it);
700 for (std::size_t i = 0; i < dofs1.size(); ++i)
701 for (
int k = 0; k < dof_bs1; ++k)
702 array1[dof_bs1 * dofs1[i] + k] = local1[dof_bs1 * i + k];
717template <dolfinx::scalar T, std::
floating_po
int U>
718void point_evaluation(
const FiniteElement<U>& element,
bool symmetric,
719 const DofMap& dofmap, mesh::CellRange
auto&& cells,
720 std::span<const std::uint32_t> cell_info,
721 std::span<const T> f, std::array<std::size_t, 2> fshape,
727 const int element_bs = element.block_size();
728 const int num_scalar_dofs = element.space_dimension() / element_bs;
729 const int dofmap_bs = dofmap.bs();
731 auto apply_inv_transpose_dof_transformation
732 = element.template dof_transformation_fn<T>(
734 std::vector<T> coeffs_b(num_scalar_dofs);
737 std::size_t matrix_size = 0;
738 while (matrix_size * matrix_size < fshape[0])
742 for (
auto cell_it =
cells.begin(); cell_it !=
cells.end(); ++cell_it)
753 std::size_t rowstart = 0;
754 std::span<const std::int32_t> dofs = dofmap.cell_dofs(*cell_it);
755 std::size_t offset = std::distance(
cells.begin(), cell_it);
756 for (
int k = 0; k < element_bs; ++k)
758 if (k - rowstart > row)
767 std::next(f.begin(), (row * matrix_size + k - rowstart) * fshape[1]
768 + offset * num_scalar_dofs),
769 num_scalar_dofs, coeffs_b.data());
770 apply_inv_transpose_dof_transformation(coeffs_b, cell_info, *cell_it,
772 for (
int i = 0; i < num_scalar_dofs; ++i)
774 const int dof = i * element_bs + k;
775 std::div_t pos = std::div(dof, dofmap_bs);
776 coeffs[dofmap_bs * dofs[pos.quot] + pos.rem] = coeffs_b[i];
784 for (
auto cell_it =
cells.begin(); cell_it !=
cells.end(); ++cell_it)
786 std::size_t offset = std::distance(
cells.begin(), cell_it);
787 std::span<const std::int32_t> dofs = dofmap.cell_dofs(*cell_it);
788 for (
int k = 0; k < element_bs; ++k)
793 std::next(f.begin(), k * fshape[1] + offset * num_scalar_dofs),
794 num_scalar_dofs, coeffs_b.data());
795 apply_inv_transpose_dof_transformation(coeffs_b, cell_info, *cell_it,
797 for (
int i = 0; i < num_scalar_dofs; ++i)
799 const int dof = i * element_bs + k;
800 std::div_t pos = std::div(dof, dofmap_bs);
801 coeffs[dofmap_bs * dofs[pos.quot] + pos.rem] = coeffs_b[i];
819template <dolfinx::scalar T, std::
floating_po
int U>
820void identity_mapped_evaluation(
const FiniteElement<U>& element,
bool symmetric,
821 const DofMap& dofmap,
822 mesh::CellRange
auto&& cells,
823 std::span<const std::uint32_t> cell_info,
824 std::span<const T> f,
825 std::array<std::size_t, 2> fshape,
832 throw std::runtime_error(
"Interpolation into this element not supported.");
834 const int element_bs = element.block_size();
835 const int num_scalar_dofs = element.space_dimension() / element_bs;
836 const int dofmap_bs = dofmap.bs();
838 const int element_vs = element.reference_value_size();
839 if (element_vs > 1 and element_bs > 1)
840 throw std::runtime_error(
"Interpolation into this element not supported.");
843 const auto [_Pi, pi_shape] = element.interpolation_operator();
844 md::mdspan<const U, std::dextents<std::size_t, 2>> Pi(_Pi.data(), pi_shape);
845 const std::size_t num_interp_points = Pi.extent(1);
846 assert(
static_cast<int>(Pi.extent(0)) == num_scalar_dofs);
848 auto apply_inv_transpose_dof_transformation
849 = element.template dof_transformation_fn<T>(
853 std::vector<T> ref_data_b(num_interp_points);
854 md::mdspan<T, md::extents<std::size_t, md::dynamic_extent, 1>> ref_data(
855 ref_data_b.data(), num_interp_points, 1);
856 std::vector<T> coeffs_b(num_scalar_dofs);
857 for (
auto cell_it =
cells.begin(); cell_it !=
cells.end(); ++cell_it)
859 std::size_t offset = std::distance(
cells.begin(), cell_it);
860 std::span<const std::int32_t> dofs = dofmap.cell_dofs(*cell_it);
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) * fshape[1]
867 + offset * num_interp_points / element_vs),
868 num_interp_points / element_vs,
869 std::next(ref_data_b.begin(), i * num_interp_points / element_vs));
872 impl::interpolation_apply(Pi, ref_data, std::span(coeffs_b), 1);
873 apply_inv_transpose_dof_transformation(coeffs_b, cell_info, *cell_it, 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_b[i];
896template <dolfinx::scalar T, std::
floating_po
int U>
897void piola_mapped_evaluation(
const FiniteElement<U>& element,
bool symmetric,
898 const DofMap& dofmap, mesh::CellRange
auto&& cells,
899 std::span<const std::uint32_t> cell_info,
900 std::span<const T> f,
901 std::array<std::size_t, 2> fshape,
902 const mesh::Mesh<U>& mesh, std::span<T> coeffs)
905 throw std::runtime_error(
"Interpolation into this element not supported.");
907 const int gdim = mesh.geometry().dim();
908 assert(mesh.topology());
909 const int tdim = mesh.topology()->dim();
911 const int element_bs = element.block_size();
912 const int num_scalar_dofs = element.space_dimension() / element_bs;
913 const int value_size = element.reference_value_size();
914 const int dofmap_bs = dofmap.bs();
916 md::mdspan<const T, md::dextents<std::size_t, 2>> _f(f.data(), fshape);
919 const auto [X, Xshape] = element.interpolation_points();
922 throw std::runtime_error(
923 "Interpolation into this space is not yet supported.");
926 if (_f.extent(1) !=
cells.size() * Xshape[0])
927 throw std::runtime_error(
"Interpolation data has the wrong shape.");
930 const CoordinateElement<U>& cmap = mesh.geometry().cmap();
933 auto x_dofmap = mesh.geometry().dofmap();
934 const int num_dofs_g = cmap.dim();
935 std::span<const U> x_g = mesh.geometry().x();
938 std::vector<U> J_b(Xshape[0] * gdim * tdim);
939 md::mdspan<U, std::dextents<std::size_t, 3>> J(J_b.data(), Xshape[0], gdim,
941 std::vector<U> K_b(Xshape[0] * tdim * gdim);
942 md::mdspan<U, std::dextents<std::size_t, 3>> K(K_b.data(), Xshape[0], tdim,
944 std::vector<U> detJ(Xshape[0]);
945 std::vector<U> det_scratch(2 * gdim * tdim);
947 std::vector<U> coord_dofs_b(num_dofs_g * gdim);
948 md::mdspan<U, std::dextents<std::size_t, 2>> coord_dofs(coord_dofs_b.data(),
950 const std::size_t value_size_ref = element.reference_value_size();
951 std::vector<T> ref_data_b(Xshape[0] * 1 * value_size_ref);
953 T, md::extents<std::size_t, md::dynamic_extent, 1, md::dynamic_extent>>
954 ref_data(ref_data_b.data(), Xshape[0], 1, value_size_ref);
956 std::vector<T> _vals_b(Xshape[0] * 1 * value_size);
958 T, md::extents<std::size_t, md::dynamic_extent, 1, md::dynamic_extent>>
959 _vals(_vals_b.data(), Xshape[0], 1, value_size);
963 std::array<std::size_t, 4> phi_shape = cmap.tabulate_shape(1, Xshape[0]);
964 std::vector<U> phi_b(
965 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
966 md::mdspan<
const U, md::extents<std::size_t, md::dynamic_extent,
967 md::dynamic_extent, md::dynamic_extent, 1>>
968 phi(phi_b.data(), phi_shape);
969 cmap.tabulate(1, X, Xshape, phi_b);
970 auto dphi = md::submdspan(phi, std::pair(1, tdim + 1), md::full_extent,
973 std::function<void(std::span<T>, std::span<const std::uint32_t>, std::int32_t,
975 apply_inv_trans_dof_transformation
976 = element.template dof_transformation_fn<T>(
980 const auto [_Pi, pi_shape] = element.interpolation_operator();
981 md::mdspan<const U, std::dextents<std::size_t, 2>> Pi(_Pi.data(), pi_shape);
983 using u_t = md::mdspan<const T, md::dextents<std::size_t, 2>>;
985 =
decltype(md::submdspan(ref_data, 0, md::full_extent, md::full_extent));
986 using J_t = md::mdspan<const U, md::dextents<std::size_t, 2>>;
987 using K_t = md::mdspan<const U, md::dextents<std::size_t, 2>>;
989 = element.basix_element().template map_fn<U_t, u_t, J_t, K_t>();
991 std::vector<T> coeffs_b(num_scalar_dofs);
992 for (
auto cell_it =
cells.begin(); cell_it !=
cells.end(); ++cell_it)
994 auto x_dofs = md::submdspan(x_dofmap, *cell_it, md::full_extent);
995 for (
int i = 0; i < num_dofs_g; ++i)
997 const int pos = 3 * x_dofs[i];
998 for (
int j = 0; j < gdim; ++j)
999 coord_dofs(i, j) = x_g[pos + j];
1003 std::ranges::fill(J_b, 0);
1004 for (std::size_t p = 0; p < Xshape[0]; ++p)
1006 auto _dphi = md::submdspan(dphi, md::full_extent, p, md::full_extent);
1007 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
1008 cmap.compute_jacobian(_dphi, coord_dofs, _J);
1009 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
1010 cmap.compute_jacobian_inverse(_J, _K);
1011 detJ[p] = cmap.compute_jacobian_determinant(_J, det_scratch);
1014 const std::size_t offset = std::distance(
cells.begin(), cell_it);
1015 std::span<const std::int32_t> dofs = dofmap.cell_dofs(*cell_it);
1016 for (
int k = 0; k < element_bs; ++k)
1019 for (
int m = 0; m < value_size; ++m)
1021 for (std::size_t k0 = 0; k0 < Xshape[0]; ++k0)
1024 = f[fshape[1] * (k * value_size + m) + offset * Xshape[0] + k0];
1029 for (std::size_t i = 0; i < Xshape[0]; ++i)
1031 auto _u = md::submdspan(_vals, i, md::full_extent, md::full_extent);
1032 auto _U = md::submdspan(ref_data, i, md::full_extent, md::full_extent);
1033 auto _K = md::submdspan(K, i, md::full_extent, md::full_extent);
1034 auto _J = md::submdspan(J, i, md::full_extent, md::full_extent);
1035 pull_back_fn(_U, _u, _K, 1.0 / detJ[i], _J);
1038 auto ref = md::submdspan(ref_data, md::full_extent, 0, md::full_extent);
1039 impl::interpolation_apply(Pi, ref, std::span(coeffs_b), element_bs);
1040 apply_inv_trans_dof_transformation(coeffs_b, cell_info, *cell_it, 1);
1043 assert(coeffs_b.size() ==
static_cast<std::size_t
>(num_scalar_dofs));
1044 for (
int i = 0; i < num_scalar_dofs; ++i)
1046 const int dof = i * element_bs + k;
1047 std::div_t pos = std::div(dof, dofmap_bs);
1048 coeffs[dofmap_bs * dofs[pos.quot] + pos.rem] = coeffs_b[i];
1075template <std::
floating_po
int T>
1085 std::vector<T> x(coords.size());
1086 std::size_t num_points = coords.size() / 3;
1087 for (std::size_t i = 0; i < num_points; ++i)
1088 for (std::size_t j = 0; j < 3; ++j)
1089 x[3 * i + j] = coords[i + j * num_points];
1096template <dolfinx::scalar T, std::
floating_po
int U>
1098 std::array<std::size_t, 2> fshape,
1102 const int index = 0;
1105 const int element_bs = element->block_size();
1106 if (
int num_sub = element->num_sub_elements();
1107 num_sub > 0 and num_sub != element_bs)
1109 throw std::runtime_error(
"Cannot directly interpolate a mixed space. "
1110 "Interpolate into subspaces.");
1119 != (std::size_t)u.
function_space()->elements(index)->value_size()
1120 or f.size() != fshape[0] * fshape[1])
1122 throw std::runtime_error(
"Interpolation data has the wrong shape/size.");
1125 spdlog::debug(
"Check for dof transformation");
1126 std::span<const std::uint32_t> cell_info;
1127 if (element->needs_dof_transformations())
1129 mesh->topology_mutable()->create_entity_permutations();
1130 cell_info = std::span(
mesh->topology()->get_cell_permutation_info());
1134 spdlog::debug(
"Interpolate: get dofmap");
1139 std::span<T> coeffs = u.
x()->array();
1142 element->map_ident() and element->interpolation_ident())
1146 spdlog::debug(
"Interpolate: point evaluation");
1147 impl::point_evaluation(*element, symmetric, *dofmap, cells, cell_info, f,
1150 else if (element->map_ident())
1152 spdlog::debug(
"Interpolate: identity-mapped evaluation");
1153 impl::identity_mapped_evaluation(*element, symmetric, *dofmap, cells,
1154 cell_info, f, fshape, coeffs);
1158 spdlog::debug(
"Interpolate: Piola-mapped evaluation");
1159 impl::piola_mapped_evaluation(*element, symmetric, *dofmap, cells,
1160 cell_info, f, fshape, *
mesh, coeffs);
1180template <dolfinx::scalar T, std::
floating_po
int U>
1187 MPI_Comm comm = mesh1->comm();
1193 MPI_Comm_compare(comm, mesh0->comm(), &result);
1194 if (result == MPI_UNEQUAL)
1196 throw std::runtime_error(
"Interpolation on different meshes is only "
1197 "supported on the same communicator.");
1201 assert(mesh1->topology());
1202 auto cell_map = mesh1->topology()->index_map(mesh1->topology()->dim());
1206 const std::size_t value_size = element1->value_size();
1208 const std::vector<int>& dest_ranks = interpolation_data.
src_owner;
1209 const std::vector<int>& src_ranks = interpolation_data.
dest_owners;
1210 const std::vector<U>& recv_points = interpolation_data.
dest_points;
1211 const std::vector<std::int32_t>& evaluation_cells
1215 std::vector<T> send_values(recv_points.size() / 3 * value_size);
1216 u0.
eval(recv_points, {recv_points.size() / 3, (std::size_t)3},
1217 evaluation_cells, send_values, {recv_points.size() / 3, value_size},
1221 std::vector<T> values_b(dest_ranks.size() * value_size);
1222 md::mdspan<const T, md::dextents<std::size_t, 2>> _send_values(
1223 send_values.data(), src_ranks.size(), value_size);
1224 impl::scatter_values(comm, src_ranks, dest_ranks, _send_values,
1225 std::span(values_b));
1228 md::mdspan<const T, md::dextents<std::size_t, 2>> values(
1229 values_b.data(), dest_ranks.size(), value_size);
1230 std::vector<T> valuesT_b(value_size * dest_ranks.size());
1231 md::mdspan<T, md::dextents<std::size_t, 2>> valuesT(
1232 valuesT_b.data(), value_size, dest_ranks.size());
1233 for (std::size_t i = 0; i < values.extent(0); ++i)
1234 for (std::size_t j = 0; j < values.extent(1); ++j)
1235 valuesT(j, i) = values(i, j);
1258template <dolfinx::scalar T, std::
floating_po
int U>
1262 if (cells0.size() != cells1.size())
1263 throw std::runtime_error(
"Length of cell lists do not match.");
1271 auto e0 = V0->element();
1273 auto e1 = V1->element();
1275 if (!std::ranges::equal(e0->value_shape(), e1->value_shape()))
1277 throw std::runtime_error(
1278 "Interpolation: elements have different value dimensions");
1281 if (V1->mesh() == V0->mesh() and (e1 == e0 or *e1 == *e0))
1284 if (e1->block_size() != e0->block_size())
1285 throw std::runtime_error(
"Mismatch in element block size.");
1288 std::shared_ptr<const DofMap> dofmap0 = V0->dofmap();
1290 std::shared_ptr<const DofMap> dofmap1 = V1->dofmap();
1294 const int bs0 = dofmap0->bs();
1295 const int bs1 = dofmap1->bs();
1296 std::span<T> u1_array = u1.
x()->array();
1297 std::span<const T> u0_array = u0.
x()->array();
1298 assert(cells0.size() == cells1.size());
1299 for (
auto cell0_it = cells0.begin(), cell1_it = cells1.begin();
1300 cell0_it != cells0.end() and cell1_it != cells1.end();
1301 ++cell0_it, ++cell1_it)
1304 std::span<const std::int32_t> dofs0 = dofmap0->cell_dofs(*cell0_it);
1305 std::span<const std::int32_t> dofs1 = dofmap1->cell_dofs(*cell1_it);
1306 assert(bs0 * dofs0.size() == bs1 * dofs1.size());
1307 for (std::size_t i = 0; i < dofs0.size(); ++i)
1309 for (
int k = 0; k < bs0; ++k)
1311 int index = bs0 * i + k;
1312 std::div_t dv1 = std::div(index, bs1);
1313 u1_array[bs1 * dofs1[dv1.quot] + dv1.rem]
1314 = u0_array[bs0 * dofs0[i] + k];
1319 else if (e1->map_type() == e0->map_type())
1322 impl::interpolate_same_map(u1, cells1, u0, cells0);
1327 impl::interpolate_nonmatching_maps(u1, cells1, u0, cells0);
1340template <dolfinx::scalar T, std::
floating_po
int U>
1342 std::ranges::input_range
auto&& cells)
1349 throw std::runtime_error(
"Meshes do no match.");
1361template <dolfinx::scalar T, std::
floating_po
int U>
1367 std::ranges::copy(u0.
x()->array(), u1.
x()->array().begin());
1370 auto mesh = V1->mesh();
1372 assert(
mesh->topology());
1373 auto map =
mesh->topology()->index_map(
mesh->topology()->dim());
1375 std::int32_t num_cells = map->size_local() + map->num_ghosts();
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:147
void eval(std::span< const geometry_type > x, std::array< std::size_t, 2 > xshape, mesh::CellRange auto &&cells, std::span< value_type > u, std::array< std::size_t, 2 > ushape, double tol, int maxit) const
Evaluate the Function at points.
Definition Function.h:457
std::shared_ptr< const la::Vector< value_type > > x() const
Underlying vector (const version).
Definition Function.h:153
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
Requirement on range of cell indices.
Definition Topology.h:32
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, const std::pair< R0, R1 > &cells, std::array< std::reference_wrapper< const DofMap >, 2 > dofmaps)
Iterate over cells and insert entries into sparsity pattern.
Definition sparsitybuild.h:37
Finite element method functionality.
Definition assemble_expression_impl.h:23
void interpolate(Function< T, U > &u, std::span< const T > f, std::array< std::size_t, 2 > fshape, mesh::CellRange auto &&cells)
Interpolate an evaluated expression f(x) in a finite element space.
Definition interpolate.h:1097
@ transpose
Transpose.
Definition FiniteElement.h:28
@ inverse_transpose
Transpose inverse.
Definition FiniteElement.h:30
@ standard
Standard.
Definition FiniteElement.h:27
std::vector< T > interpolation_coords(const fem::FiniteElement< T > &element, const mesh::Geometry< T > &geometry, mesh::CellRange auto &&cells)
Compute the evaluation points in the physical space at which an expression should be computed to inte...
Definition interpolate.h:50
geometry::PointOwnershipData< T > create_interpolation_data(const mesh::Geometry< T > &geometry0, const FiniteElement< T > &element0, const mesh::Mesh< T > &mesh1, mesh::CellRange auto &&cells, T padding)
Generate data needed to interpolate finite element fem::Function's across different meshes.
Definition interpolate.h:1076
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, std::optional< std::span< const std::int32_t > > cells)
Given a set of points, determine which process is colliding, using the GJK algorithm on cells to dete...
Definition utils.h:683
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
CellType
Cell type identifier.
Definition cell_types.h:21
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