94 throw std::runtime_error(
"Meshes must be the same.");
96 if (
mesh->geometry().dim() != 3)
97 throw std::runtime_error(
"Geometric must be equal to 3..");
98 if (
mesh->geometry().dim() !=
mesh->topology()->dim())
100 throw std::runtime_error(
101 "Geometric and topological dimensions must be equal.");
103 constexpr int gdim = 3;
106 std::shared_ptr<const FiniteElement<T>> e0 = V0.
element();
108 if (e0->map_type() != basix::maps::type::covariantPiola)
110 throw std::runtime_error(
111 "Finite element for parent space must be covariant Piola.");
114 std::shared_ptr<const FiniteElement<T>> e1 = V1.
element();
116 if (e1->map_type() != basix::maps::type::contravariantPiola)
118 throw std::runtime_error(
119 "Finite element for target space must be contracovariant Piola.");
123 std::span<const std::uint32_t> cell_info;
124 if (e1->needs_dof_transformations() or e0->needs_dof_transformations())
126 mesh->topology_mutable()->create_entity_permutations();
127 cell_info = std::span(
mesh->topology()->get_cell_permutation_info());
131 auto dofmap0 = V0.
dofmap();
133 auto dofmap1 = V1.
dofmap();
137 auto apply_dof_transformation0
139 auto apply_inverse_dof_transform1 = e1->template dof_transformation_fn<U>(
143 const std::size_t space_dim0 = e0->space_dimension();
144 const std::size_t space_dim1 = e1->space_dimension();
145 if (e0->reference_value_size() != 3)
146 throw std::runtime_error(
"Value size for parent space should be 3.");
147 if (e1->reference_value_size() != 3)
148 throw std::runtime_error(
"Value size for target space should be 3.");
151 const auto [X, Xshape] = e1->interpolation_points();
155 const auto [Phi0_b, Phi0_shape] = e0->tabulate(X, Xshape, 1);
156 md::mdspan<
const T, md::extents<std::size_t, 4, md::dynamic_extent,
157 md::dynamic_extent, 3>>
158 Phi0(Phi0_b.data(), Phi0_shape);
161 md::extents<std::size_t, md::dynamic_extent, md::dynamic_extent, 3, 3>
162 dPhi0_ext(Phi0.extent(1), Phi0.extent(2), Phi0.extent(0) - 1,
164 std::vector<T> dPhi0_b(dPhi0_ext.extent(0) * dPhi0_ext.extent(1)
165 * dPhi0_ext.extent(2) * dPhi0_ext.extent(3));
166 md::mdspan<T,
decltype(dPhi0_ext)> dPhi0(dPhi0_b.data(), dPhi0_ext);
171 const auto [Pi1_b, pi_shape] = e1->interpolation_operator();
172 md::mdspan<const T, md::dextents<std::size_t, 2>> Pi_1(Pi1_b.data(),
176 std::vector<T> curl_b(dPhi0.extent(0) * dPhi0.extent(1) * dPhi0.extent(3));
178 T, md::extents<std::size_t, md::dynamic_extent, md::dynamic_extent, 3>>
179 curl(curl_b.data(), dPhi0.extent(0), dPhi0.extent(1), dPhi0.extent(3));
181 std::vector<U> Ab(space_dim0 * space_dim1);
184 assert(
mesh->topology()->index_map(gdim));
185 for (std::int32_t c = 0; c <
mesh->topology()->index_map(gdim)->size_local();
194 for (std::size_t p = 0; p < Phi0.extent(1); ++p)
195 for (std::size_t phi = 0; phi < Phi0.extent(2); ++phi)
196 for (std::size_t d = 0; d < Phi0.extent(3); ++d)
197 for (std::size_t dx = 0; dx < dPhi0.extent(2); ++dx)
198 dPhi0(p, phi, dx, d) = Phi0(dx + 1, p, phi, d);
200 for (std::size_t p = 0; p < dPhi0.extent(0); ++p)
203 std::size_t size = dPhi0.extent(1) * dPhi0.extent(2) * dPhi0.extent(3);
204 std::size_t offset = p * size;
207 apply_dof_transformation0(std::span(dPhi0.data_handle() + offset, size),
209 dPhi0.extent(2) * dPhi0.extent(3));
215 for (std::size_t p = 0; p < curl.extent(0); ++p)
217 for (std::size_t i = 0; i < curl.extent(1); ++i)
219 curl(p, i, 0) = dPhi0(p, i, 1, 2) - dPhi0(p, i, 2, 1);
220 curl(p, i, 1) = dPhi0(p, i, 2, 0) - dPhi0(p, i, 0, 2);
221 curl(p, i, 2) = dPhi0(p, i, 0, 1) - dPhi0(p, i, 1, 0);
231 std::ranges::fill(Ab, U(0));
232 for (std::size_t j = 0; j < space_dim1; ++j)
233 for (std::size_t k = 0; k < curl.extent(2); ++k)
234 for (std::size_t p = 0; p < curl.extent(0); ++p)
236 T pi_val = Pi_1(j, k * curl.extent(0) + p);
237 for (std::size_t i = 0; i < space_dim0; ++i)
238 Ab[space_dim0 * j + i] +=
static_cast<U
>(pi_val * curl(p, i, k));
241 apply_inverse_dof_transform1(Ab, cell_info, c, space_dim0);
242 mat_set(dofmap1->cell_dofs(c), dofmap0->cell_dofs(c), Ab);
275 std::reference_wrapper<const DofMap>>
278 std::reference_wrapper<const DofMap>>
282 auto& e0 = V0.first.get();
283 const DofMap& dofmap0 = V0.second.get();
284 auto& e1 = V1.first.get();
285 const DofMap& dofmap1 = V1.second.get();
287 using cmdspan2_t = md::mdspan<const U, md::dextents<std::size_t, 2>>;
288 using cmdspan4_t = md::mdspan<const U, md::dextents<std::size_t, 4>>;
291 if (e0.map_type() != basix::maps::type::identity)
292 throw std::runtime_error(
"Wrong finite element space for V0.");
293 if (e0.block_size() != 1)
294 throw std::runtime_error(
"Block size is greater than 1 for V0.");
295 if (e0.reference_value_size() != 1)
296 throw std::runtime_error(
"Wrong value size for V0.");
298 if (e1.map_type() != basix::maps::type::covariantPiola)
299 throw std::runtime_error(
"Wrong finite element space for V1.");
300 if (e1.block_size() != 1)
301 throw std::runtime_error(
"Block size is greater than 1 for V1.");
304 const auto [X, Xshape] = e1.interpolation_points();
308 const int ndofs0 = e0.space_dimension();
309 const int tdim = topology.
dim();
310 std::vector<U> phi0_b((tdim + 1) * Xshape[0] * ndofs0 * 1);
311 cmdspan4_t phi0(phi0_b.data(), tdim + 1, Xshape[0], ndofs0, 1);
312 e0.tabulate(phi0_b, X, Xshape, 1);
316 cmdspan2_t dphi_reshaped(
317 phi0_b.data() + phi0.extent(3) * phi0.extent(2) * phi0.extent(1),
318 tdim * phi0.extent(1), phi0.extent(2));
321 auto apply_inverse_dof_transform = e1.template dof_transformation_fn<T>(
326 const std::vector<std::uint32_t>& cell_info
332 std::vector<T> Ab(e1.space_dimension() * ndofs0);
334 md::mdspan<T, md::dextents<std::size_t, 2>> A(Ab.data(),
335 e1.space_dimension(), ndofs0);
336 const auto [Pi, shape] = e1.interpolation_operator();
337 cmdspan2_t _Pi(Pi.data(), shape);
338 math::dot(_Pi, dphi_reshaped, A);
342 auto cell_map = topology.
index_map(tdim);
344 std::int32_t num_cells = cell_map->size_local();
345 std::vector<T> Ae(Ab.size());
346 for (std::int32_t c = 0; c < num_cells; ++c)
348 std::ranges::copy(Ab, Ae.begin());
349 apply_inverse_dof_transform(Ae, cell_info, c, ndofs0);
378 const int tdim =
mesh->topology()->dim();
379 const int gdim =
mesh->geometry().dim();
382 std::shared_ptr<const FiniteElement<U>> e0 = V0.
element();
384 std::shared_ptr<const FiniteElement<U>> e1 = V1.
element();
387 std::span<const std::uint32_t> cell_info;
388 if (e1->needs_dof_transformations() or e0->needs_dof_transformations())
390 mesh->topology_mutable()->create_entity_permutations();
391 cell_info = std::span(
mesh->topology()->get_cell_permutation_info());
395 auto dofmap0 = V0.
dofmap();
397 auto dofmap1 = V1.
dofmap();
401 const int bs0 = e0->block_size();
402 const int bs1 = e1->block_size();
403 auto apply_dof_transformation0
405 auto apply_inverse_dof_transform1 = e1->template dof_transformation_fn<T>(
409 const std::size_t space_dim0 = e0->space_dimension();
410 const std::size_t space_dim1 = e1->space_dimension();
411 const std::size_t dim0 = space_dim0 / bs0;
412 const std::size_t value_size_ref0 = e0->reference_value_size();
413 const std::size_t value_size0 = V0.
element()->reference_value_size();
417 auto x_dofmap =
mesh->geometry().dofmaps().front();
418 const std::size_t num_dofs_g = cmap.
dim();
419 std::span<const U> x_g =
mesh->geometry().x();
421 using mdspan2_t = md::mdspan<U, md::dextents<std::size_t, 2>>;
422 using cmdspan2_t = md::mdspan<const U, md::dextents<std::size_t, 2>>;
423 using cmdspan4_t = md::mdspan<const U, md::dextents<std::size_t, 4>>;
424 using mdspan3_t = md::mdspan<U, md::dextents<std::size_t, 3>>;
427 const auto [X, Xshape] = e1->interpolation_points();
428 std::array<std::size_t, 4> phi_shape = cmap.
tabulate_shape(1, Xshape[0]);
429 std::vector<U> phi_b(
430 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
431 cmdspan4_t phi(phi_b.data(), phi_shape);
435 std::vector<U> basis_derivatives_reference0_b(Xshape[0] * dim0
437 cmdspan4_t basis_derivatives_reference0(basis_derivatives_reference0_b.data(),
438 1, Xshape[0], dim0, value_size_ref0);
439 e0->tabulate(basis_derivatives_reference0_b, X, Xshape, 0);
442 std::ranges::transform(
443 basis_derivatives_reference0_b, basis_derivatives_reference0_b.begin(),
444 [atol = 1e-14](
auto x) { return std::abs(x) < atol ? 0.0 : x; });
447 std::vector<U> basis_reference0_b(Xshape[0] * dim0 * value_size_ref0);
448 mdspan3_t basis_reference0(basis_reference0_b.data(), Xshape[0], dim0,
450 std::vector<U> J_b(Xshape[0] * gdim * tdim);
451 mdspan3_t J(J_b.data(), Xshape[0], gdim, tdim);
452 std::vector<U> K_b(Xshape[0] * tdim * gdim);
453 mdspan3_t K(K_b.data(), Xshape[0], tdim, gdim);
454 std::vector<U> detJ(Xshape[0]);
455 std::vector<U> det_scratch(2 * tdim * gdim);
460 const auto [_Pi_1, pi_shape] = e1->interpolation_operator();
461 cmdspan2_t Pi_1(_Pi_1.data(), pi_shape);
463 bool interpolation_ident = e1->interpolation_ident();
465 using u_t = md::mdspan<U, md::dextents<std::size_t, 2>>;
466 using U_t = md::mdspan<const U, md::dextents<std::size_t, 2>>;
467 using J_t = md::mdspan<const U, md::dextents<std::size_t, 2>>;
468 using K_t = md::mdspan<const U, md::dextents<std::size_t, 2>>;
469 auto push_forward_fn0
470 = e0->basix_element().template map_fn<u_t, U_t, J_t, K_t>();
474 std::vector<U> basis_values_b(Xshape[0] * bs0 * dim0
476 mdspan3_t basis_values(basis_values_b.data(), Xshape[0], bs0 * dim0,
478 std::vector<U> mapped_values_b(Xshape[0] * bs0 * dim0
480 mdspan3_t mapped_values(mapped_values_b.data(), Xshape[0], bs0 * dim0,
484 = e1->basix_element().template map_fn<u_t, U_t, K_t, J_t>();
486 std::vector<U> coord_dofs_b(num_dofs_g * gdim);
487 mdspan2_t coord_dofs(coord_dofs_b.data(), num_dofs_g, gdim);
488 std::vector<U> basis0_b(Xshape[0] * dim0 * value_size0);
489 mdspan3_t basis0(basis0_b.data(), Xshape[0], dim0, value_size0);
492 std::vector<T> Ab(space_dim0 * space_dim1);
495 auto cell_map =
mesh->topology()->index_map(tdim);
497 std::int32_t num_cells = cell_map->size_local();
498 int row_bs = dofmap1->bs();
499 std::int32_t num_owned_rows
500 = dofmap1->index_map->size_local() * dofmap1->index_map_bs() / row_bs;
501 std::int32_t num_ghosted_rows
502 = dofmap1->index_map->num_ghosts() * dofmap1->index_map_bs() / row_bs;
503 std::vector<std::int8_t> row_added(num_owned_rows + num_ghosted_rows, 0);
505 for (std::int32_t c = 0; c < num_cells; ++c)
508 auto x_dofs = md::submdspan(x_dofmap, c, md::full_extent);
509 for (std::size_t i = 0; i < x_dofs.size(); ++i)
511 for (
int j = 0; j < gdim; ++j)
512 coord_dofs(i, j) = x_g[3 * x_dofs[i] + j];
520 std::ranges::fill(J_b, 0);
524 = md::submdspan(phi, std::pair(1, tdim + 1), 0, md::full_extent, 0);
525 auto _J = md::submdspan(J, 0, md::full_extent, md::full_extent);
527 auto _K = md::submdspan(K, 0, md::full_extent, md::full_extent);
530 for (std::size_t p = 1; p < Xshape[0]; ++p)
532 std::copy_n(J_b.begin(), gdim * tdim, J_b.begin() + p * gdim * tdim);
533 std::copy_n(K_b.begin(), tdim * gdim, K_b.begin() + p * tdim * gdim);
539 for (std::size_t p = 0; p < Xshape[0]; ++p)
542 = md::submdspan(phi, std::pair(1, tdim + 1), p, md::full_extent, 0);
543 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
545 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
558 std::ranges::copy(basis_derivatives_reference0_b,
559 basis_reference0_b.begin());
560 for (std::size_t p = 0; p < Xshape[0]; ++p)
562 apply_dof_transformation0(
563 std::span(basis_reference0.data_handle() + p * dim0 * value_size_ref0,
564 dim0 * value_size_ref0),
565 cell_info, c, value_size_ref0);
568 for (std::size_t p = 0; p < basis0.extent(0); ++p)
570 auto _u = md::submdspan(basis0, p, md::full_extent, md::full_extent);
571 auto _U = md::submdspan(basis_reference0, p, md::full_extent,
573 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
574 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
575 push_forward_fn0(_u, _U, _J, detJ[p], _K);
579 for (std::size_t p = 0; p < Xshape[0]; ++p)
580 for (std::size_t i = 0; i < dim0; ++i)
581 for (std::size_t j = 0; j < value_size0; ++j)
582 for (
int k = 0; k < bs0; ++k)
583 basis_values(p, i * bs0 + k, j * bs0 + k) = basis0(p, i, j);
586 for (std::size_t p = 0; p < basis_values.extent(0); ++p)
589 = md::submdspan(basis_values, p, md::full_extent, md::full_extent);
591 = md::submdspan(mapped_values, p, md::full_extent, md::full_extent);
592 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
593 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
594 pull_back_fn1(_U, _u, _K, 1.0 / detJ[p], _J);
599 if (interpolation_ident)
601 md::mdspan<T, md::dextents<std::size_t, 3>> A(
602 Ab.data(), Xshape[0], V1.
element()->value_size(), space_dim0);
603 for (std::size_t i = 0; i < mapped_values.extent(0); ++i)
604 for (std::size_t j = 0; j < mapped_values.extent(1); ++j)
605 for (std::size_t k = 0; k < mapped_values.extent(2); ++k)
606 A(i, k, j) = mapped_values(i, j, k);
621 std::ranges::fill(Ab, T(0));
622 std::size_t num_pts = mapped_values.extent(0);
625 for (std::size_t idof = 0; idof < Pi_1.extent(0); ++idof)
626 for (std::size_t k = 0; k < mapped_values.extent(2); ++k)
627 for (std::size_t p = 0; p < num_pts; ++p)
629 U pi_val = Pi_1(idof, k * num_pts + p);
630 for (std::size_t i = 0; i < space_dim0; ++i)
631 Ab[space_dim0 * idof + i]
632 +=
static_cast<T
>(pi_val * mapped_values(p, i, k));
637 for (std::size_t idof = 0; idof < Pi_1.extent(0); ++idof)
638 for (
int k = 0; k < bs1; ++k)
639 for (std::size_t p = 0; p < num_pts; ++p)
641 U pi_val = Pi_1(idof, p);
642 for (std::size_t i = 0; i < space_dim0; ++i)
643 Ab[space_dim0 * (bs1 * idof + k) + i]
644 +=
static_cast<T
>(pi_val * mapped_values(p, i, k));
649 apply_inverse_dof_transform1(Ab, cell_info, c, space_dim0);
654 md::mdspan<T, md::dextents<std::size_t, 2>> A(Ab.data(), space_dim1,
656 auto row_dofs = dofmap1->cell_dofs(c);
657 for (std::size_t i = 0; i < row_dofs.size(); ++i)
659 std::int32_t r = row_dofs[i];
660 if (r >= num_owned_rows || row_added[r])
662 for (std::size_t j = 0; j < space_dim0; ++j)
663 for (
int k = 0; k < row_bs; ++k)
664 A(i * row_bs + k, j) = 0.0;
669 mat_add(dofmap1->cell_dofs(c), dofmap0->cell_dofs(c), Ab);