DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
interpolate.h
1// Copyright (C) 2020-2026 Garth N. Wells, Igor A. Baratta, Massimiliano Leoni
2// and Jørgen S.Dokken
3//
4// This file is part of DOLFINx (https://www.fenicsproject.org)
5//
6// SPDX-License-Identifier: LGPL-3.0-or-later
7
8#pragma once
9
10#include "CoordinateElement.h"
11#include "DofMap.h"
12#include "FiniteElement.h"
13#include "FunctionSpace.h"
14#include <algorithm>
15#include <basix/mdspan.hpp>
16#include <concepts>
17#include <dolfinx/common/IndexMap.h>
18#include <dolfinx/common/types.h>
19#include <dolfinx/geometry/utils.h>
20#include <dolfinx/mesh/Mesh.h>
21#include <functional>
22#include <numeric>
23#include <ranges>
24#include <span>
25#include <stdexcept>
26#include <vector>
27
28namespace dolfinx::fem
29{
30template <dolfinx::scalar T, std::floating_point U>
31class Function;
32
43template <std::floating_point T>
44std::vector<T> interpolation_coords(const fem::FiniteElement<T>& element,
46 mesh::CellRange auto&& cells)
47{
48 // Find CoordinateElement appropriate to element
49 auto cmap_index = [&geometry](mesh::CellType cell_type)
50 {
51 for (std::size_t i = 0; i < geometry.cmaps().size(); ++i)
52 {
53 if (geometry.cmaps().at(i).cell_shape() == cell_type)
54 return i;
55 }
56 throw std::runtime_error("Cannot find CoordinateElement for FiniteElement");
57 };
58 int index = cmap_index(element.cell_type());
59
60 // Get geometry data and the element coordinate map
61 const std::size_t gdim = geometry.dim();
62 auto x_dofmap = geometry.dofmaps().at(index);
63 std::span<const T> x_g = geometry.x();
64
65 const CoordinateElement<T>& cmap = geometry.cmaps().at(index);
66 const std::size_t num_dofs_g = cmap.dim();
67
68 // Get the interpolation points on the reference cells
69 const auto [X, Xshape] = element.interpolation_points();
70
71 // Evaluate coordinate element basis at reference points
72 std::array<std::size_t, 4> phi_shape = cmap.tabulate_shape(0, Xshape[0]);
73 std::vector<T> phi_b(
74 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
75 md::mdspan<const T, md::extents<std::size_t, 1, md::dynamic_extent,
76 md::dynamic_extent, 1>>
77 phi_full(phi_b.data(), phi_shape);
78 cmap.tabulate(0, X, Xshape, phi_b);
79 auto phi = md::submdspan(phi_full, 0, md::full_extent, md::full_extent, 0);
80
81 // Push reference coordinates (X) forward to the physical coordinates
82 // (x) for each cell
83 std::vector<T> coordinate_dofs(num_dofs_g * gdim, 0);
84 std::vector<T> x(3 * (cells.size() * Xshape[0]), 0);
85 for (auto cell_it = cells.begin(); cell_it != cells.end(); ++cell_it)
86 {
87 // Get geometry data for current cell
88 auto x_dofs = md::submdspan(x_dofmap, *cell_it, md::full_extent);
89 for (std::size_t i = 0; i < x_dofs.size(); ++i)
90 {
91 std::copy_n(std::next(x_g.begin(), 3 * x_dofs[i]), gdim,
92 std::next(coordinate_dofs.begin(), i * gdim));
93 }
94
95 // Push forward coordinates (X -> x)
96 std::size_t offset = std::ranges::distance(cells.begin(), cell_it);
97 for (std::size_t p = 0; p < Xshape[0]; ++p)
98 {
99 for (std::size_t j = 0; j < gdim; ++j)
100 {
101 T acc = 0;
102 for (std::size_t k = 0; k < num_dofs_g; ++k)
103 acc += phi(p, k) * coordinate_dofs[k * gdim + j];
104 x[j * (cells.size() * Xshape[0]) + offset * Xshape[0] + p] = acc;
105 }
106 }
107 }
108
109 return x;
110}
111
128template <dolfinx::scalar T, std::floating_point U>
129void interpolate(Function<T, U>& u, std::span<const T> f,
130 std::array<std::size_t, 2> fshape,
131 mesh::CellRange auto&& cells);
132
133namespace impl
134{
136template <typename T, std::size_t D>
137using mdspan_t = md::mdspan<T, md::dextents<std::size_t, D>>;
138
158template <dolfinx::scalar T>
159void scatter_values(MPI_Comm comm, std::span<const std::int32_t> src_ranks,
160 std::span<const std::int32_t> dest_ranks,
161 mdspan_t<const T, 2> send_values, std::span<T> recv_values)
162{
163 const std::size_t block_size = send_values.extent(1);
164 assert(src_ranks.size() * block_size == send_values.size());
165 assert(recv_values.size() == dest_ranks.size() * block_size);
166
167 // Build unique set of the sorted src_ranks
168 std::vector<std::int32_t> out_ranks(src_ranks.size());
169 out_ranks.assign(src_ranks.begin(), src_ranks.end());
170 auto [unique_end, range_end] = std::ranges::unique(out_ranks);
171 out_ranks.erase(unique_end, range_end);
172 out_ranks.reserve(out_ranks.size() + 1);
173
174 // Remove negative entries from dest_ranks
175 std::vector<std::int32_t> in_ranks;
176 in_ranks.reserve(dest_ranks.size());
177 std::copy_if(dest_ranks.begin(), dest_ranks.end(),
178 std::back_inserter(in_ranks),
179 [](auto rank) { return rank >= 0; });
180
181 // Create unique set of sorted in-ranks
182 {
183 std::ranges::sort(in_ranks);
184 auto [unique_end, range_end] = std::ranges::unique(in_ranks);
185 in_ranks.erase(unique_end, range_end);
186 }
187 in_ranks.reserve(in_ranks.size() + 1);
188
189 // Create neighborhood communicator
190 MPI_Comm reverse_comm;
191 MPI_Dist_graph_create_adjacent(
192 comm, in_ranks.size(), in_ranks.data(), MPI_UNWEIGHTED, out_ranks.size(),
193 out_ranks.data(), MPI_UNWEIGHTED, MPI_INFO_NULL, false, &reverse_comm);
194
195 std::vector<std::int32_t> comm_to_output;
196 std::vector<std::int32_t> recv_sizes(in_ranks.size());
197 recv_sizes.reserve(1);
198 std::vector<std::int32_t> recv_offsets(in_ranks.size() + 1, 0);
199 {
200 // Build map from parent to neighborhood communicator ranks
201 std::vector<std::pair<std::int32_t, std::int32_t>> rank_to_neighbor;
202 rank_to_neighbor.reserve(in_ranks.size());
203 for (std::size_t i = 0; i < in_ranks.size(); i++)
204 rank_to_neighbor.push_back({in_ranks[i], i});
205 std::ranges::sort(rank_to_neighbor);
206
207 // Compute receive sizes
208 std::ranges::for_each(
209 dest_ranks,
210 [&rank_to_neighbor, &recv_sizes, block_size](auto rank)
211 {
212 if (rank >= 0)
213 {
214 auto it = std::ranges::lower_bound(rank_to_neighbor, rank,
215 std::ranges::less(),
216 [](auto e) { return e.first; });
217 assert(it != rank_to_neighbor.end() and it->first == rank);
218 recv_sizes[it->second] += block_size;
219 }
220 });
221
222 // Compute receiving offsets
223 std::partial_sum(recv_sizes.begin(), recv_sizes.end(),
224 std::next(recv_offsets.begin(), 1));
225
226 // Compute map from receiving values to position in recv_values
227 comm_to_output.resize(recv_offsets.back() / block_size);
228 std::vector<std::int32_t> recv_counter(recv_sizes.size(), 0);
229 for (std::size_t i = 0; i < dest_ranks.size(); ++i)
230 {
231 if (const std::int32_t rank = dest_ranks[i]; rank >= 0)
232 {
233 auto it = std::ranges::lower_bound(rank_to_neighbor, rank,
234 std::ranges::less(),
235 [](auto e) { return e.first; });
236 assert(it != rank_to_neighbor.end() and it->first == rank);
237 int insert_pos = recv_offsets[it->second] + recv_counter[it->second];
238 comm_to_output[insert_pos / block_size] = i * block_size;
239 recv_counter[it->second] += block_size;
240 }
241 }
242 }
243
244 std::vector<std::int32_t> send_sizes(out_ranks.size());
245 send_sizes.reserve(1);
246 {
247 // Compute map from parent MPI rank to neighbor rank for outgoing
248 // data. `out_ranks` is sorted, so rank_to_neighbor will be sorted
249 // too.
250 std::vector<std::pair<std::int32_t, std::int32_t>> rank_to_neighbor;
251 rank_to_neighbor.reserve(out_ranks.size());
252 for (std::size_t i = 0; i < out_ranks.size(); i++)
253 rank_to_neighbor.push_back({out_ranks[i], i});
254
255 // Compute send sizes. As `src_ranks` is sorted, we can move 'start'
256 // in search forward.
257 auto start = rank_to_neighbor.begin();
258 std::ranges::for_each(
259 src_ranks,
260 [&rank_to_neighbor, &send_sizes, block_size, &start](auto rank)
261 {
262 auto it = std::ranges::lower_bound(start, rank_to_neighbor.end(),
263 rank, std::ranges::less(),
264 [](auto e) { return e.first; });
265 assert(it != rank_to_neighbor.end() and it->first == rank);
266 send_sizes[it->second] += block_size;
267 start = it;
268 });
269 }
270
271 // Compute sending offsets
272 std::vector<std::int32_t> send_offsets(send_sizes.size() + 1, 0);
273 std::partial_sum(send_sizes.begin(), send_sizes.end(),
274 std::next(send_offsets.begin(), 1));
275
276 // Send values to dest ranks
277 std::vector<T> values(recv_offsets.back());
278 values.reserve(1);
279 MPI_Neighbor_alltoallv(send_values.data_handle(), send_sizes.data(),
280 send_offsets.data(), dolfinx::MPI::mpi_t<T>,
281 values.data(), recv_sizes.data(), recv_offsets.data(),
282 dolfinx::MPI::mpi_t<T>, reverse_comm);
283 MPI_Comm_free(&reverse_comm);
284
285 // Insert values received from neighborhood communicator in output
286 // span
287 std::ranges::fill(recv_values, T{0});
288 for (std::size_t i = 0; i < comm_to_output.size(); i++)
289 {
290 auto vals = std::next(recv_values.begin(), comm_to_output[i]);
291 auto vals_from = std::next(values.begin(), i * block_size);
292 std::copy_n(vals_from, block_size, vals);
293 }
294};
295
304template <dolfinx::MDSpanRank2 U, dolfinx::MDSpanRank2 V, dolfinx::scalar T>
305void interpolation_apply(U&& Pi, V&& data, std::span<T> coeffs, int bs)
306{
307 // Geometry (real) scalar type, taken from the interpolation operator Pi
308 // rather than scalar_value_t<T> so it is independent of the value scalar T.
309 using X = typename std::remove_cvref_t<U>::value_type;
310
311 // Compute coefficients = Pi * x (matrix-vector multiply)
312 if (bs == 1)
313 {
314 assert(data.extent(0) * data.extent(1) == Pi.extent(1));
315 for (std::size_t i = 0; i < Pi.extent(0); ++i)
316 {
317 coeffs[i] = 0.0;
318 for (std::size_t k = 0; k < data.extent(1); ++k)
319 for (std::size_t j = 0; j < data.extent(0); ++j)
320 coeffs[i]
321 += static_cast<X>(Pi(i, k * data.extent(0) + j)) * data(j, k);
322 }
323 }
324 else
325 {
326 assert(data.extent(0) == Pi.extent(1));
327 assert(static_cast<int>(data.extent(1)) == bs);
328 std::size_t cols = Pi.extent(1);
329 for (int k = 0; k < bs; ++k)
330 {
331 for (std::size_t i = 0; i < Pi.extent(0); ++i)
332 {
333 T acc = 0;
334 for (std::size_t j = 0; j < cols; ++j)
335 acc += static_cast<X>(Pi(i, j)) * data(j, k);
336 coeffs[bs * i + k] = acc;
337 }
338 }
339 }
340}
341
361template <dolfinx::scalar T, std::floating_point U>
362void interpolate_same_map(Function<T, U>& u1, mesh::CellRange auto&& cells1,
363 const Function<T, U>& u0,
364 mesh::CellRange auto&& cells0)
365{
366 auto V0 = u0.function_space();
367 assert(V0);
368 auto V1 = u1.function_space();
369 assert(V1);
370 auto mesh0 = V0->mesh();
371 assert(mesh0);
372
373 auto mesh1 = V1->mesh();
374 assert(mesh1);
375
376 auto element0 = V0->element();
377 assert(element0);
378 auto element1 = V1->element();
379 assert(element1);
380
381 assert(mesh0->topology()->dim());
382 const int tdim = mesh0->topology()->dim();
383 auto map = mesh0->topology()->index_map(tdim);
384 assert(map);
385 std::span<T> u1_array = u1.x()->array();
386 std::span<const T> u0_array = u0.x()->array();
387
388 std::span<const std::uint32_t> cell_info0;
389 std::span<const std::uint32_t> cell_info1;
390 if (element1->needs_dof_transformations()
391 or element0->needs_dof_transformations())
392 {
393 mesh0->topology_mutable()->create_entity_permutations();
394 cell_info0 = std::span(mesh0->topology()->get_cell_permutation_info());
395 mesh1->topology_mutable()->create_entity_permutations();
396 cell_info1 = std::span(mesh1->topology()->get_cell_permutation_info());
397 }
398
399 // Get dofmaps
400 auto dofmap1 = V1->dofmap();
401 auto dofmap0 = V0->dofmap();
402
403 // Get block sizes and dof transformation operators
404 const int bs1 = dofmap1->bs();
405 const int bs0 = dofmap0->bs();
406 auto apply_dof_transformation = element0->template dof_transformation_fn<T>(
408 auto apply_inverse_dof_transform
409 = element1->template dof_transformation_fn<T>(
411
412 // Create working array
413 std::vector<T> local0(element0->space_dimension());
414 std::vector<T> local1(element1->space_dimension());
415
416 // Create interpolation operator
417 auto [i_m, im_shape] = element1->create_interpolation_operator(*element0);
418
419 // Iterate over mesh and interpolate on each cell
420 using X = U; // geometry (real) type, independent of the value scalar T
421 if (cells0.size() != cells1.size())
422 throw std::invalid_argument("Length of cells0 and cells1 must match.");
423 for (auto cell0_it = cells0.begin(), cell1_it = cells1.begin();
424 cell0_it != cells0.end() and cell1_it != cells1.end();
425 ++cell0_it, ++cell1_it)
426 {
427 // Pack and transform cell dofs to reference ordering
428 std::span<const std::int32_t> dofs0 = dofmap0->cell_dofs(*cell0_it);
429 for (std::size_t i = 0; i < dofs0.size(); ++i)
430 for (int k = 0; k < bs0; ++k)
431 local0[bs0 * i + k] = u0_array[bs0 * dofs0[i] + k];
432
433 if (apply_dof_transformation)
434 apply_dof_transformation(local0, cell_info0, *cell0_it, 1);
435
436 // FIXME: Get compile-time ranges from Basix
437 // Apply interpolation operator
438 std::ranges::fill(local1, 0);
439 for (std::size_t i = 0; i < im_shape[0]; ++i)
440 for (std::size_t j = 0; j < im_shape[1]; ++j)
441 local1[i] += static_cast<X>(i_m[im_shape[1] * i + j]) * local0[j];
442
443 if (apply_inverse_dof_transform)
444 apply_inverse_dof_transform(local1, cell_info1, *cell1_it, 1);
445 std::span<const std::int32_t> dofs1 = dofmap1->cell_dofs(*cell1_it);
446 for (std::size_t i = 0; i < dofs1.size(); ++i)
447 for (int k = 0; k < bs1; ++k)
448 u1_array[bs1 * dofs1[i] + k] = local1[bs1 * i + k];
449 }
450}
451
466template <dolfinx::scalar T, std::floating_point U>
467void interpolate_nonmatching_maps(Function<T, U>& u1,
468 mesh::CellRange auto&& cells1,
469 const Function<T, U>& u0,
470 mesh::CellRange auto&& cells0)
471{
472 // Get mesh
473 auto V0 = u0.function_space();
474 assert(V0);
475 auto mesh0 = V0->mesh();
476 assert(mesh0);
477
478 // Mesh dims
479 const int tdim = mesh0->topology()->dim();
480 const int gdim = mesh0->geometry().dim();
481
482 // Get elements
483 auto V1 = u1.function_space();
484 assert(V1);
485 auto mesh1 = V1->mesh();
486 assert(mesh1);
487 auto element0 = V0->element();
488 assert(element0);
489 auto element1 = V1->element();
490 assert(element1);
491
492 std::span<const std::uint32_t> cell_info0;
493 std::span<const std::uint32_t> cell_info1;
494 if (element1->needs_dof_transformations()
495 or element0->needs_dof_transformations())
496 {
497 mesh0->topology_mutable()->create_entity_permutations();
498 cell_info0 = std::span(mesh0->topology()->get_cell_permutation_info());
499 mesh1->topology_mutable()->create_entity_permutations();
500 cell_info1 = std::span(mesh1->topology()->get_cell_permutation_info());
501 }
502
503 // Get dofmaps
504 auto dofmap0 = V0->dofmap();
505 auto dofmap1 = V1->dofmap();
506
507 const auto [X, Xshape] = element1->interpolation_points();
508
509 // Get block sizes and dof transformation operators
510 const int bs0 = element0->block_size();
511 const int bs1 = element1->block_size();
512 auto apply_dof_transformation0 = element0->template dof_transformation_fn<U>(
514 auto apply_inv_dof_transform1 = element1->template dof_transformation_fn<T>(
516
517 // Get sizes of elements
518 const std::size_t dim0 = element0->space_dimension() / bs0;
519 const std::size_t value_size_ref0 = element0->reference_value_size();
520 const std::size_t value_size0 = V0->element()->reference_value_size();
521
522 const CoordinateElement<U>& cmap = mesh0->geometry().cmaps().front();
523 auto x_dofmap = mesh0->geometry().dofmaps().front();
524 std::span<const U> x_g = mesh0->geometry().x();
525
526 // (0) is derivative index, (1) is the point index, (2) is the basis
527 // function index and (3) is the basis function component.
528
529 // Evaluate coordinate map basis at reference interpolation points
530 const std::array<std::size_t, 4> phi_shape
531 = cmap.tabulate_shape(1, Xshape[0]);
532 std::vector<U> phi_b(
533 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
534 md::mdspan<const U, md::extents<std::size_t, md::dynamic_extent,
535 md::dynamic_extent, md::dynamic_extent, 1>>
536 phi(phi_b.data(), phi_shape);
537 cmap.tabulate(1, X, Xshape, phi_b);
538
539 // Evaluate v basis functions at reference interpolation points
540 const auto [_basis_derivatives_reference0, b0shape]
541 = element0->tabulate(X, Xshape, 0);
542 md::mdspan<const U, std::extents<std::size_t, 1, md::dynamic_extent,
543 md::dynamic_extent, md::dynamic_extent>>
544 basis_derivatives_reference0(_basis_derivatives_reference0.data(),
545 b0shape);
546
547 // Create working arrays
548 std::vector<T> local1(element1->space_dimension());
549 std::vector<T> coeffs0(element0->space_dimension());
550
551 std::vector<U> basis0_b(Xshape[0] * dim0 * value_size0);
552 md::mdspan<U, std::dextents<std::size_t, 3>> basis0(
553 basis0_b.data(), Xshape[0], dim0, value_size0);
554
555 std::vector<U> basis_reference0_b(Xshape[0] * dim0 * value_size_ref0);
556 md::mdspan<U, std::dextents<std::size_t, 3>> basis_reference0(
557 basis_reference0_b.data(), Xshape[0], dim0, value_size_ref0);
558
559 std::vector<T> values0_b(Xshape[0] * 1 * V1->element()->value_size());
560 md::mdspan<
561 T, md::extents<std::size_t, md::dynamic_extent, 1, md::dynamic_extent>>
562 values0(values0_b.data(), Xshape[0], 1, V1->element()->value_size());
563
564 std::vector<T> mapped_values_b(Xshape[0] * 1 * V1->element()->value_size());
565 md::mdspan<
566 T, md::extents<std::size_t, md::dynamic_extent, 1, md::dynamic_extent>>
567 mapped_values0(mapped_values_b.data(), Xshape[0], 1,
568 V1->element()->value_size());
569
570 const std::size_t num_dofs_g = cmap.dim();
571 std::vector<U> coord_dofs_b(num_dofs_g * gdim);
572 md::mdspan<U, std::dextents<std::size_t, 2>> coord_dofs(coord_dofs_b.data(),
573 num_dofs_g, gdim);
574
575 std::vector<U> J_b(Xshape[0] * gdim * tdim);
576 md::mdspan<U, std::dextents<std::size_t, 3>> J(J_b.data(), Xshape[0], gdim,
577 tdim);
578 std::vector<U> K_b(Xshape[0] * tdim * gdim);
579 md::mdspan<U, std::dextents<std::size_t, 3>> K(K_b.data(), Xshape[0], tdim,
580 gdim);
581 std::vector<U> detJ(Xshape[0]);
582 std::vector<U> det_scratch(2 * gdim * tdim);
583
584 // Get interpolation operator
585 const auto [_Pi_1, pi_shape] = element1->interpolation_operator();
586 impl::mdspan_t<const U, 2> Pi_1(_Pi_1.data(), pi_shape);
587
588 using u_t = md::mdspan<U, std::dextents<std::size_t, 2>>;
589 using U_t = md::mdspan<const U, std::dextents<std::size_t, 2>>;
590 using J_t = md::mdspan<const U, std::dextents<std::size_t, 2>>;
591 using K_t = md::mdspan<const U, std::dextents<std::size_t, 2>>;
592 auto push_forward_fn0
593 = element0->basix_element().template map_fn<u_t, U_t, J_t, K_t>();
594
595 using v_t = md::mdspan<const T, std::dextents<std::size_t, 2>>;
596 using V_t = decltype(md::submdspan(mapped_values0, 0, md::full_extent,
597 md::full_extent));
598 auto pull_back_fn1
599 = element1->basix_element().template map_fn<V_t, v_t, K_t, J_t>();
600
601 // Iterate over mesh and interpolate on each cell
602 std::span<const T> array0 = u0.x()->array();
603 std::span<T> array1 = u1.x()->array();
604 if (cells0.size() != cells1.size())
605 throw std::invalid_argument("Length of cells0 and cells1 must match.");
606 for (auto cell0_it = cells0.begin(), cell1_it = cells1.begin();
607 cell0_it != cells0.end() and cell1_it != cells1.end();
608 ++cell0_it, ++cell1_it)
609 {
610 // Get cell geometry (coordinate dofs)
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)
613 {
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];
617 }
618
619 // Compute Jacobians and reference points for current cell
620 std::ranges::fill(J_b, 0);
621 for (std::size_t p = 0; p < Xshape[0]; ++p)
622 {
623 auto dphi
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);
630 }
631
632 // Copy evaluated basis on reference, apply DOF transformations, and
633 // push forward to physical element
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);
639
640 if (apply_dof_transformation0)
641 {
642 for (std::size_t p = 0; p < Xshape[0]; ++p)
643 {
644 apply_dof_transformation0(
645 std::span(basis_reference0_b.data() + p * dim0 * value_size_ref0,
646 dim0 * value_size_ref0),
647 cell_info0, *cell0_it, value_size_ref0);
648 }
649 }
650
651 for (std::size_t i = 0; i < basis0.extent(0); ++i)
652 {
653 auto _u = md::submdspan(basis0, i, md::full_extent, md::full_extent);
654 auto _U = md::submdspan(basis_reference0, i, md::full_extent,
655 md::full_extent);
656 auto _K = md::submdspan(K, i, md::full_extent, md::full_extent);
657 auto _J = md::submdspan(J, i, md::full_extent, md::full_extent);
658 push_forward_fn0(_u, _U, _J, detJ[i], _K);
659 }
660
661 // Copy expansion coefficients for v into local array
662 const int dof_bs0 = dofmap0->bs();
663 std::span<const std::int32_t> dofs0 = dofmap0->cell_dofs(*cell0_it);
664 for (std::size_t i = 0; i < dofs0.size(); ++i)
665 for (int k = 0; k < dof_bs0; ++k)
666 coeffs0[dof_bs0 * i + k] = array0[dof_bs0 * dofs0[i] + k];
667
668 // Evaluate v at the interpolation points (physical space values)
669 using X = U; // geometry (real) type, independent of the value scalar T
670 for (std::size_t p = 0; p < Xshape[0]; ++p)
671 {
672 for (int k = 0; k < bs0; ++k)
673 {
674 for (std::size_t j = 0; j < value_size0; ++j)
675 {
676 T acc = 0;
677 for (std::size_t i = 0; i < dim0; ++i)
678 acc += coeffs0[bs0 * i + k] * static_cast<X>(basis0(p, i, j));
679 values0(p, 0, j * bs0 + k) = acc;
680 }
681 }
682 }
683
684 // Pull back the physical values to the u reference
685 for (std::size_t i = 0; i < values0.extent(0); ++i)
686 {
687 auto _u = md::submdspan(values0, i, md::full_extent, md::full_extent);
688 auto _U
689 = md::submdspan(mapped_values0, i, md::full_extent, md::full_extent);
690 auto _K = md::submdspan(K, i, md::full_extent, md::full_extent);
691 auto _J = md::submdspan(J, i, md::full_extent, md::full_extent);
692 pull_back_fn1(_U, _u, _K, 1.0 / detJ[i], _J);
693 }
694
695 auto values
696 = md::submdspan(mapped_values0, md::full_extent, 0, md::full_extent);
697 interpolation_apply(Pi_1, values, std::span(local1), bs1);
698 if (apply_inv_dof_transform1)
699 apply_inv_dof_transform1(local1, cell_info1, *cell1_it, 1);
700
701 // Copy local coefficients to the correct position in u dof array
702 const int dof_bs1 = dofmap1->bs();
703 std::span<const std::int32_t> dofs1 = dofmap1->cell_dofs(*cell1_it);
704 for (std::size_t i = 0; i < dofs1.size(); ++i)
705 for (int k = 0; k < dof_bs1; ++k)
706 array1[dof_bs1 * dofs1[i] + k] = local1[dof_bs1 * i + k];
707 }
708}
709
721template <dolfinx::scalar T, std::floating_point U>
722void point_evaluation(const FiniteElement<U>& element, bool symmetric,
723 const DofMap& dofmap, mesh::CellRange auto&& cells,
724 std::span<const std::uint32_t> cell_info,
725 std::span<const T> f, std::array<std::size_t, 2> fshape,
726 std::span<T> coeffs)
727{
728 // Point evaluation element *and* the geometric map is the identity,
729 // e.g. not Piola mapped
730
731 const int element_bs = element.block_size();
732 const int num_scalar_dofs = element.space_dimension() / element_bs;
733 const int dofmap_bs = dofmap.bs();
734
735 auto apply_inv_transpose_dof_transformation
736 = element.template dof_transformation_fn<T>(
738 std::vector<T> coeffs_b(num_scalar_dofs);
739
740 // Skip the div/mod below when block sizes match (the common case)
741 const bool same_bs = (dofmap_bs == element_bs);
742
743 if (symmetric)
744 {
745 std::size_t matrix_size = 0;
746 while (matrix_size * matrix_size < fshape[0])
747 ++matrix_size;
748
749 // Loop over cells
750 for (auto cell_it = cells.begin(); cell_it != cells.end(); ++cell_it)
751 {
752 // The entries of a symmetric matrix are numbered (for an
753 // example 4x4 element):
754 // 0 * * *
755 // 1 2 * *
756 // 3 4 5 *
757 // 6 7 8 9
758 // The loop extracts these elements. In this loop, row is the
759 // row of this matrix, and (k - rowstart) is the column
760 std::size_t row = 0;
761 std::size_t rowstart = 0;
762 std::span<const std::int32_t> dofs = dofmap.cell_dofs(*cell_it);
763 std::size_t offset = std::ranges::distance(cells.begin(), cell_it);
764 for (int k = 0; k < element_bs; ++k)
765 {
766 if (k - rowstart > row)
767 {
768 ++row;
769 rowstart = k;
770 }
771
772 // num_scalar_dofs is the number of interpolation points per
773 // cell in this case (interpolation matrix is identity)
774 std::copy_n(
775 std::next(f.begin(), (row * matrix_size + k - rowstart) * fshape[1]
776 + offset * num_scalar_dofs),
777 num_scalar_dofs, coeffs_b.data());
778 if (apply_inv_transpose_dof_transformation)
779 {
780 apply_inv_transpose_dof_transformation(coeffs_b, cell_info, *cell_it,
781 1);
782 }
783 if (same_bs)
784 {
785 for (int i = 0; i < num_scalar_dofs; ++i)
786 coeffs[dofmap_bs * dofs[i] + k] = coeffs_b[i];
787 }
788 else
789 {
790 for (int i = 0; i < num_scalar_dofs; ++i)
791 {
792 std::div_t pos = std::div(i * element_bs + k, dofmap_bs);
793 coeffs[dofmap_bs * dofs[pos.quot] + pos.rem] = coeffs_b[i];
794 }
795 }
796 }
797 }
798 }
799 else
800 {
801 // Loop over cells
802 for (auto cell_it = cells.begin(); cell_it != cells.end(); ++cell_it)
803 {
804 std::size_t offset = std::ranges::distance(cells.begin(), cell_it);
805 std::span<const std::int32_t> dofs = dofmap.cell_dofs(*cell_it);
806 for (int k = 0; k < element_bs; ++k)
807 {
808 // num_scalar_dofs is the number of interpolation points per
809 // cell in this case (interpolation matrix is identity)
810 std::copy_n(
811 std::next(f.begin(), k * fshape[1] + offset * num_scalar_dofs),
812 num_scalar_dofs, coeffs_b.data());
813 if (apply_inv_transpose_dof_transformation)
814 {
815 apply_inv_transpose_dof_transformation(coeffs_b, cell_info, *cell_it,
816 1);
817 }
818 if (same_bs)
819 {
820 for (int i = 0; i < num_scalar_dofs; ++i)
821 coeffs[dofmap_bs * dofs[i] + k] = coeffs_b[i];
822 }
823 else
824 {
825 for (int i = 0; i < num_scalar_dofs; ++i)
826 {
827 std::div_t pos = std::div(i * element_bs + k, dofmap_bs);
828 coeffs[dofmap_bs * dofs[pos.quot] + pos.rem] = coeffs_b[i];
829 }
830 }
831 }
832 }
833 }
834}
835
847template <dolfinx::scalar T, std::floating_point U>
848void identity_mapped_evaluation(const FiniteElement<U>& element, bool symmetric,
849 const DofMap& dofmap,
850 mesh::CellRange auto&& cells,
851 std::span<const std::uint32_t> cell_info,
852 std::span<const T> f,
853 std::array<std::size_t, 2> fshape,
854 std::span<T> coeffs)
855{
856 // Not a point evaluation, but the geometric map is the identity,
857 // e.g. not Piola mapped
858
859 if (symmetric)
860 throw std::invalid_argument(
861 "Interpolation into this element not supported.");
862
863 const int element_bs = element.block_size();
864 const int num_scalar_dofs = element.space_dimension() / element_bs;
865 const int dofmap_bs = dofmap.bs();
866
867 const int element_vs = element.reference_value_size();
868 if (element_vs > 1 and element_bs > 1)
869 throw std::runtime_error("Interpolation into this element not supported.");
870
871 // Get interpolation operator
872 const auto [_Pi, pi_shape] = element.interpolation_operator();
873 md::mdspan<const U, std::dextents<std::size_t, 2>> Pi(_Pi.data(), pi_shape);
874 const std::size_t num_interp_points = Pi.extent(1);
875 assert(static_cast<int>(Pi.extent(0)) == num_scalar_dofs);
876
877 auto apply_inv_transpose_dof_transformation
878 = element.template dof_transformation_fn<T>(
880
881 // Skip the div/mod below when block sizes match (the common case)
882 const bool same_bs = (dofmap_bs == element_bs);
883
884 // Loop over cells
885 std::vector<T> ref_data_b(num_interp_points);
886 md::mdspan<T, md::extents<std::size_t, md::dynamic_extent, 1>> ref_data(
887 ref_data_b.data(), num_interp_points, 1);
888 std::vector<T> coeffs_b(num_scalar_dofs);
889 for (auto cell_it = cells.begin(); cell_it != cells.end(); ++cell_it)
890 {
891 std::size_t offset = std::ranges::distance(cells.begin(), cell_it);
892 std::span<const std::int32_t> dofs = dofmap.cell_dofs(*cell_it);
893 for (int k = 0; k < element_bs; ++k)
894 {
895 for (int i = 0; i < element_vs; ++i)
896 {
897 std::copy_n(
898 std::next(f.begin(), (i + k) * fshape[1]
899 + offset * num_interp_points / element_vs),
900 num_interp_points / element_vs,
901 std::next(ref_data_b.begin(), i * num_interp_points / element_vs));
902 }
903
904 impl::interpolation_apply(Pi, ref_data, std::span(coeffs_b), 1);
905 if (apply_inv_transpose_dof_transformation)
906 {
907 apply_inv_transpose_dof_transformation(coeffs_b, cell_info, *cell_it,
908 1);
909 }
910 if (same_bs)
911 {
912 for (int i = 0; i < num_scalar_dofs; ++i)
913 coeffs[dofmap_bs * dofs[i] + k] = coeffs_b[i];
914 }
915 else
916 {
917 for (int i = 0; i < num_scalar_dofs; ++i)
918 {
919 std::div_t pos = std::div(i * element_bs + k, dofmap_bs);
920 coeffs[dofmap_bs * dofs[pos.quot] + pos.rem] = coeffs_b[i];
921 }
922 }
923 }
924 }
925}
926
939template <dolfinx::scalar T, std::floating_point U>
940void piola_mapped_evaluation(const FiniteElement<U>& element, bool symmetric,
941 const DofMap& dofmap, mesh::CellRange auto&& cells,
942 std::span<const std::uint32_t> cell_info,
943 std::span<const T> f,
944 std::array<std::size_t, 2> fshape,
945 const mesh::Mesh<U>& mesh, std::span<T> coeffs)
946{
947 if (symmetric)
948 throw std::invalid_argument(
949 "Interpolation into this element not supported.");
950
951 const int gdim = mesh.geometry().dim();
952 assert(mesh.topology());
953 const int tdim = mesh.topology()->dim();
954
955 const int element_bs = element.block_size();
956 const int num_scalar_dofs = element.space_dimension() / element_bs;
957 const int value_size = element.reference_value_size();
958 const int dofmap_bs = dofmap.bs();
959
960 // Skip the div/mod below when block sizes match (the common case)
961 const bool same_bs = (dofmap_bs == element_bs);
962
963 md::mdspan<const T, md::dextents<std::size_t, 2>> _f(f.data(), fshape);
964
965 // Get the interpolation points on the reference cells
966 const auto [X, Xshape] = element.interpolation_points();
967 if (X.empty())
968 {
969 throw std::invalid_argument(
970 "Interpolation into this space is not yet supported.");
971 }
972
973 if (_f.extent(1) != cells.size() * Xshape[0])
974 throw std::invalid_argument("Interpolation data has the wrong shape.");
975
976 // Get coordinate map
977 const CoordinateElement<U>& cmap = mesh.geometry().cmaps().front();
978
979 // Get geometry data
980 auto x_dofmap = mesh.geometry().dofmaps().front();
981 const int num_dofs_g = cmap.dim();
982 std::span<const U> x_g = mesh.geometry().x();
983
984 // Create data structures for Jacobian info
985 std::vector<U> J_b(Xshape[0] * gdim * tdim);
986 md::mdspan<U, std::dextents<std::size_t, 3>> J(J_b.data(), Xshape[0], gdim,
987 tdim);
988 std::vector<U> K_b(Xshape[0] * tdim * gdim);
989 md::mdspan<U, std::dextents<std::size_t, 3>> K(K_b.data(), Xshape[0], tdim,
990 gdim);
991 std::vector<U> detJ(Xshape[0]);
992 std::vector<U> det_scratch(2 * gdim * tdim);
993
994 std::vector<U> coord_dofs_b(num_dofs_g * gdim);
995 md::mdspan<U, std::dextents<std::size_t, 2>> coord_dofs(coord_dofs_b.data(),
996 num_dofs_g, gdim);
997 const std::size_t value_size_ref = element.reference_value_size();
998 std::vector<T> ref_data_b(Xshape[0] * 1 * value_size_ref);
999 md::mdspan<
1000 T, md::extents<std::size_t, md::dynamic_extent, 1, md::dynamic_extent>>
1001 ref_data(ref_data_b.data(), Xshape[0], 1, value_size_ref);
1002
1003 std::vector<T> _vals_b(Xshape[0] * 1 * value_size);
1004 md::mdspan<
1005 T, md::extents<std::size_t, md::dynamic_extent, 1, md::dynamic_extent>>
1006 _vals(_vals_b.data(), Xshape[0], 1, value_size);
1007
1008 // Tabulate 1st derivative of shape functions at interpolation
1009 // coords
1010 std::array<std::size_t, 4> phi_shape = cmap.tabulate_shape(1, Xshape[0]);
1011 std::vector<U> phi_b(
1012 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
1013 md::mdspan<const U, md::extents<std::size_t, md::dynamic_extent,
1014 md::dynamic_extent, md::dynamic_extent, 1>>
1015 phi(phi_b.data(), phi_shape);
1016 cmap.tabulate(1, X, Xshape, phi_b);
1017 auto dphi = md::submdspan(phi, std::pair(1, tdim + 1), md::full_extent,
1018 md::full_extent, 0);
1019
1020 std::function<void(std::span<T>, std::span<const std::uint32_t>, std::int32_t,
1021 int)>
1022 apply_inv_trans_dof_transformation
1023 = element.template dof_transformation_fn<T>(
1025
1026 // Get interpolation operator
1027 const auto [_Pi, pi_shape] = element.interpolation_operator();
1028 md::mdspan<const U, std::dextents<std::size_t, 2>> Pi(_Pi.data(), pi_shape);
1029
1030 using u_t = md::mdspan<const T, md::dextents<std::size_t, 2>>;
1031 using U_t
1032 = decltype(md::submdspan(ref_data, 0, md::full_extent, md::full_extent));
1033 using J_t = md::mdspan<const U, md::dextents<std::size_t, 2>>;
1034 using K_t = md::mdspan<const U, md::dextents<std::size_t, 2>>;
1035 auto pull_back_fn
1036 = element.basix_element().template map_fn<U_t, u_t, J_t, K_t>();
1037
1038 std::vector<T> coeffs_b(num_scalar_dofs);
1039 for (auto cell_it = cells.begin(); cell_it != cells.end(); ++cell_it)
1040 {
1041 auto x_dofs = md::submdspan(x_dofmap, *cell_it, md::full_extent);
1042 for (int i = 0; i < num_dofs_g; ++i)
1043 {
1044 const int pos = 3 * x_dofs[i];
1045 for (int j = 0; j < gdim; ++j)
1046 coord_dofs(i, j) = x_g[pos + j];
1047 }
1048
1049 // Compute J, detJ and K
1050 std::ranges::fill(J_b, 0);
1051 for (std::size_t p = 0; p < Xshape[0]; ++p)
1052 {
1053 auto _dphi = md::submdspan(dphi, md::full_extent, p, md::full_extent);
1054 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
1055 cmap.compute_jacobian(_dphi, coord_dofs, _J);
1056 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
1057 cmap.compute_jacobian_inverse(_J, _K);
1058 detJ[p] = cmap.compute_jacobian_determinant(_J, det_scratch);
1059 }
1060
1061 const std::size_t offset = std::ranges::distance(cells.begin(), cell_it);
1062 std::span<const std::int32_t> dofs = dofmap.cell_dofs(*cell_it);
1063 for (int k = 0; k < element_bs; ++k)
1064 {
1065 // Extract computed expression values for element block k
1066 for (int m = 0; m < value_size; ++m)
1067 {
1068 for (std::size_t k0 = 0; k0 < Xshape[0]; ++k0)
1069 {
1070 _vals(k0, 0, m)
1071 = f[fshape[1] * (k * value_size + m) + offset * Xshape[0] + k0];
1072 }
1073 }
1074
1075 // Get element degrees of freedom for block
1076 for (std::size_t i = 0; i < Xshape[0]; ++i)
1077 {
1078 auto _u = md::submdspan(_vals, i, md::full_extent, md::full_extent);
1079 auto _U = md::submdspan(ref_data, i, md::full_extent, md::full_extent);
1080 auto _K = md::submdspan(K, i, md::full_extent, md::full_extent);
1081 auto _J = md::submdspan(J, i, md::full_extent, md::full_extent);
1082 pull_back_fn(_U, _u, _K, 1.0 / detJ[i], _J);
1083 }
1084
1085 auto ref = md::submdspan(ref_data, md::full_extent, 0, md::full_extent);
1086 impl::interpolation_apply(Pi, ref, std::span(coeffs_b), element_bs);
1087 if (apply_inv_trans_dof_transformation)
1088 apply_inv_trans_dof_transformation(coeffs_b, cell_info, *cell_it, 1);
1089
1090 // Copy interpolation dofs into coefficient vector
1091 assert(coeffs_b.size() == static_cast<std::size_t>(num_scalar_dofs));
1092 if (same_bs)
1093 {
1094 for (int i = 0; i < num_scalar_dofs; ++i)
1095 coeffs[dofmap_bs * dofs[i] + k] = coeffs_b[i];
1096 }
1097 else
1098 {
1099 for (int i = 0; i < num_scalar_dofs; ++i)
1100 {
1101 std::div_t pos = std::div(i * element_bs + k, dofmap_bs);
1102 coeffs[dofmap_bs * dofs[pos.quot] + pos.rem] = coeffs_b[i];
1103 }
1104 }
1105 }
1106 }
1107}
1108
1109//----------------------------------------------------------------------------
1110} // namespace impl
1111
1137template <std::floating_point T>
1139 const mesh::Geometry<T>& geometry0, const FiniteElement<T>& element0,
1140 const mesh::Mesh<T>& mesh1, mesh::CellRange auto&& cells, T padding,
1141 bool allow_extrapolation = true)
1142{
1143 // Collect all the points at which values are needed to define the
1144 // interpolating function
1145 std::vector<T> coords = interpolation_coords(element0, geometry0, cells);
1146
1147 // Transpose interpolation coords
1148 std::vector<T> x(coords.size());
1149 std::size_t num_points = coords.size() / 3;
1150 for (std::size_t i = 0; i < num_points; ++i)
1151 for (std::size_t j = 0; j < 3; ++j)
1152 x[3 * i + j] = coords[i + j * num_points];
1153
1154 // Determine ownership of each point
1155 return geometry::determine_point_ownership<T>(mesh1, x, padding, std::nullopt,
1156 allow_extrapolation);
1157}
1158
1159template <dolfinx::scalar T, std::floating_point U>
1160void interpolate(Function<T, U>& u, std::span<const T> f,
1161 std::array<std::size_t, 2> fshape,
1162 mesh::CellRange auto&& cells)
1163{
1164 // TODO: Index for mixed-topology, zero for now
1165 const int index = 0;
1166 auto element = u.function_space()->elements(index);
1167 assert(element);
1168 const int element_bs = element->block_size();
1169 if (int num_sub = element->num_sub_elements();
1170 num_sub > 0 and num_sub != element_bs)
1171 {
1172 throw std::invalid_argument("Cannot directly interpolate a mixed space. "
1173 "Interpolate into subspaces.");
1174 }
1175
1176 // Get mesh
1177 assert(u.function_space());
1178 auto mesh = u.function_space()->mesh();
1179 assert(mesh);
1180
1181 if (fshape[0]
1182 != (std::size_t)u.function_space()->elements(index)->value_size()
1183 or f.size() != fshape[0] * fshape[1])
1184 {
1185 throw std::invalid_argument("Interpolation data has the wrong shape/size.");
1186 }
1187
1188 spdlog::debug("Check for dof transformation");
1189 std::span<const std::uint32_t> cell_info;
1190 if (element->needs_dof_transformations())
1191 {
1192 mesh->topology_mutable()->create_entity_permutations();
1193 cell_info = std::span(mesh->topology()->get_cell_permutation_info());
1194 }
1195
1196 // Get dofmap
1197 spdlog::debug("Interpolate: get dofmap");
1198 const auto dofmap = u.function_space()->dofmaps().at(index);
1199 assert(dofmap);
1200
1201 // Result will be stored to coeffs
1202 std::span<T> coeffs = u.x()->array();
1203
1204 if (bool symmetric = u.function_space()->symmetric();
1205 element->map_ident() and element->interpolation_ident())
1206 {
1207 // This assumes that any element with an identity interpolation
1208 // matrix is a point evaluation
1209 spdlog::debug("Interpolate: point evaluation");
1210 impl::point_evaluation(*element, symmetric, *dofmap, cells, cell_info, f,
1211 fshape, coeffs);
1212 }
1213 else if (element->map_ident())
1214 {
1215 spdlog::debug("Interpolate: identity-mapped evaluation");
1216 impl::identity_mapped_evaluation(*element, symmetric, *dofmap, cells,
1217 cell_info, f, fshape, coeffs);
1218 }
1219 else
1220 {
1221 spdlog::debug("Interpolate: Piola-mapped evaluation");
1222 impl::piola_mapped_evaluation(*element, symmetric, *dofmap, cells,
1223 cell_info, f, fshape, *mesh, coeffs);
1224 }
1225}
1226
1243template <dolfinx::scalar T, std::floating_point U>
1245 mesh::CellRange auto&& cells, double tol, int maxit,
1246 const geometry::PointOwnershipData<U>& interpolation_data)
1247{
1248 auto mesh1 = u1.function_space()->mesh();
1249 assert(mesh1);
1250 MPI_Comm comm = mesh1->comm();
1251 {
1252 assert(u0.function_space());
1253 auto mesh0 = u0.function_space()->mesh();
1254 assert(mesh0);
1255 int result;
1256 MPI_Comm_compare(comm, mesh0->comm(), &result);
1257 if (result == MPI_UNEQUAL)
1258 {
1259 throw std::invalid_argument("Interpolation on different meshes is only "
1260 "supported on the same communicator.");
1261 }
1262 }
1263
1264 assert(mesh1->topology());
1265 auto cell_map = mesh1->topology()->index_map(mesh1->topology()->dim());
1266 assert(cell_map);
1267 auto element1 = u1.function_space()->element();
1268 assert(element1);
1269 const std::size_t value_size = element1->value_size();
1270
1271 const std::vector<int>& dest_ranks = interpolation_data.src_owner;
1272 const std::vector<int>& src_ranks = interpolation_data.dest_owners;
1273 const std::vector<U>& recv_points = interpolation_data.dest_points;
1274 const std::vector<std::int32_t>& evaluation_cells
1275 = interpolation_data.dest_cells;
1276
1277 // Evaluate the interpolating function where possible
1278 std::vector<T> send_values(recv_points.size() / 3 * value_size);
1279 u0.eval(recv_points, {recv_points.size() / 3, (std::size_t)3},
1280 evaluation_cells, send_values, {recv_points.size() / 3, value_size},
1281 tol, maxit);
1282
1283 // Send values back to owning process
1284 std::vector<T> values_b(dest_ranks.size() * value_size);
1285 md::mdspan<const T, md::dextents<std::size_t, 2>> _send_values(
1286 send_values.data(), src_ranks.size(), value_size);
1287 impl::scatter_values(comm, src_ranks, dest_ranks, _send_values,
1288 std::span(values_b));
1289
1290 // Transpose received data
1291 md::mdspan<const T, md::dextents<std::size_t, 2>> values(
1292 values_b.data(), dest_ranks.size(), value_size);
1293 std::vector<T> valuesT_b(value_size * dest_ranks.size());
1294 md::mdspan<T, md::dextents<std::size_t, 2>> valuesT(
1295 valuesT_b.data(), value_size, dest_ranks.size());
1296 for (std::size_t i = 0; i < values.extent(0); ++i)
1297 for (std::size_t j = 0; j < values.extent(1); ++j)
1298 valuesT(j, i) = values(i, j);
1299
1300 // Call local interpolation operator
1301 fem::interpolate<T>(u1, valuesT_b, {valuesT.extent(0), valuesT.extent(1)},
1302 cells);
1303}
1304
1321template <dolfinx::scalar T, std::floating_point U>
1323 const Function<T, U>& u0, mesh::CellRange auto&& cells0)
1324{
1325 if (cells0.size() != cells1.size())
1326 throw std::invalid_argument("Length of cell lists do not match.");
1327
1328 auto V1 = u1.function_space();
1329 assert(V1);
1330 auto V0 = u0.function_space();
1331 assert(V0);
1332
1333 // Get elements and check value shape
1334 auto e0 = V0->element();
1335 assert(e0);
1336 auto e1 = V1->element();
1337 assert(e1);
1338 if (!std::ranges::equal(e0->value_shape(), e1->value_shape()))
1339 {
1340 throw std::invalid_argument(
1341 "Interpolation: elements have different value dimensions");
1342 }
1343
1344 if (V1->mesh() == V0->mesh() and (e1 == e0 or *e1 == *e0))
1345 {
1346 // Same element and same mesh
1347 if (e1->block_size() != e0->block_size())
1348 throw std::invalid_argument("Mismatch in element block size.");
1349
1350 // Get dofmaps
1351 std::shared_ptr<const DofMap> dofmap0 = V0->dofmap();
1352 assert(dofmap0);
1353 std::shared_ptr<const DofMap> dofmap1 = V1->dofmap();
1354 assert(dofmap1);
1355
1356 // Iterate over mesh and interpolate on each cell
1357 const int bs0 = dofmap0->bs();
1358 const int bs1 = dofmap1->bs();
1359 std::span<T> u1_array = u1.x()->array();
1360 std::span<const T> u0_array = u0.x()->array();
1361 assert(cells0.size() == cells1.size());
1362 for (auto cell0_it = cells0.begin(), cell1_it = cells1.begin();
1363 cell0_it != cells0.end() and cell1_it != cells1.end();
1364 ++cell0_it, ++cell1_it)
1365
1366 {
1367 std::span<const std::int32_t> dofs0 = dofmap0->cell_dofs(*cell0_it);
1368 std::span<const std::int32_t> dofs1 = dofmap1->cell_dofs(*cell1_it);
1369 assert(bs0 * dofs0.size() == bs1 * dofs1.size());
1370 for (std::size_t i = 0; i < dofs0.size(); ++i)
1371 {
1372 for (int k = 0; k < bs0; ++k)
1373 {
1374 int index = bs0 * i + k;
1375 std::div_t dv1 = std::div(index, bs1);
1376 u1_array[bs1 * dofs1[dv1.quot] + dv1.rem]
1377 = u0_array[bs0 * dofs0[i] + k];
1378 }
1379 }
1380 }
1381 }
1382 else if (e1->map_type() == e0->map_type())
1383 {
1384 // Different elements, same basis function map type
1385 impl::interpolate_same_map(u1, cells1, u0, cells0);
1386 }
1387 else
1388 {
1389 // Different elements with different maps for basis functions
1390 impl::interpolate_nonmatching_maps(u1, cells1, u0, cells0);
1391 }
1392}
1393
1403template <dolfinx::scalar T, std::floating_point U>
1405 std::ranges::input_range auto&& cells)
1406{
1407 assert(u1.function_space());
1408 assert(u0.function_space());
1409 if (u1.function_space()->mesh() == u0.function_space()->mesh())
1410 interpolate<T, U>(u1, cells, u0, cells);
1411 else
1412 throw std::invalid_argument("Meshes do no match.");
1413}
1414
1424template <dolfinx::scalar T, std::floating_point U>
1426{
1427 assert(u1.function_space());
1428 assert(u0.function_space());
1429 if (auto V1 = u1.function_space(); V1 == u0.function_space())
1430 std::ranges::copy(u0.x()->array(), u1.x()->array().begin());
1431 else
1432 {
1433 auto mesh = V1->mesh();
1434 assert(mesh);
1435 assert(mesh->topology());
1436 auto map = mesh->topology()->index_map(mesh->topology()->dim());
1437 assert(map);
1438 std::int32_t num_cells = map->size_local() + map->num_ghosts();
1439 interpolate<T, U>(u1, u0, std::ranges::views::iota(0, num_cells));
1440 }
1441}
1442} // namespace dolfinx::fem
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:59
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:52
int dim() const
The dimension of the coordinate element space.
Definition CoordinateElement.cpp:222
Model of a finite element.
Definition FiniteElement.h:62
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:468
mesh::CellType cell_type() const noexcept
Cell shape that the element is defined on.
Definition FiniteElement.cpp:283
Definition Function.h:48
std::shared_ptr< const FunctionSpace< geometry_type > > function_space() const
Access the function space.
Definition Function.h:149
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:462
std::shared_ptr< const la::Vector< value_type > > x() const
Underlying vector (const version).
Definition Function.h:155
Geometry stores the geometry imposed on a mesh.
Definition Geometry.h:37
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:23
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:320
int rank(MPI_Comm comm)
Return process rank for the communicator.
Definition MPI.cpp:73
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:24
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:1160
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, bool allow_extrapolation=true)
Generate data needed to interpolate finite element fem::Function's across different meshes.
Definition interpolate.h:1138
@ transpose
Transpose.
Definition FiniteElement.h:30
@ inverse_transpose
Transpose inverse.
Definition FiniteElement.h:32
@ standard
Standard.
Definition FiniteElement.h:29
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:44
Geometry data structures and algorithms.
Definition BoundingBoxTree.h:24
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, bool find_closest_cell=true)
Determine, for a set of points, the owning process of the cell (if any) that contains each point.
Definition utils.h:714
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:34
std::vector< T > dest_points
Points that are owned by current process.
Definition utils.h:39
std::vector< std::int32_t > dest_cells
Definition utils.h:41
std::vector< int > dest_owners
Ranks that sent dest_points to current process.
Definition utils.h:38
std::vector< int > src_owner
Definition utils.h:35