314 std::span<const std::int32_t> cells0,
315 std::span<const std::int32_t> cells1 = {})
322 assert(c->function_space());
323 assert(c->function_space()->mesh());
325 = c->function_space()->mesh().get();
330 else if (mesh != mesh0)
332 throw std::runtime_error(
333 "Expression coefficient Functions have different meshes.");
338 assert(_function_space);
339 assert(_function_space->mesh());
341 mesh0 = _function_space->mesh().get();
346 if (cells1.empty() and mesh0 == _function_space->mesh().get())
348 else if (cells0.size() != cells1.size())
349 throw std::runtime_error(
"Cells lists have different lengths.");
352 assert(_function_space->element());
355 throw std::runtime_error(
"Cannot interpolate Expression with Argument.");
356 if (value_size != _function_space->value_size())
358 throw std::runtime_error(
359 "Function value size not equal to Expression value size.");
364 auto [X0, shape0] = e0.
X();
365 auto [X1, shape1] = _function_space->element()->interpolation_points();
366 if (shape0 != shape1)
368 throw std::runtime_error(
369 "Function element interpolation points has different shape to "
370 "Expression interpolation points");
373 for (std::size_t i = 0; i < X0.size(); ++i)
375 if (std::abs(X0[i] - X1[i]) > 1.0e-10)
377 throw std::runtime_error(
"Function element interpolation points not "
378 "equal to Expression interpolation points");
384 std::size_t num_cells = cells0.size();
385 std::size_t num_points = e0.
X().second[0];
386 std::vector<value_type> fdata(num_cells * num_points * value_size);
387 MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
389 MDSPAN_IMPL_STANDARD_NAMESPACE::dextents<std::size_t, 3>>
390 f(fdata.data(), num_cells, num_points, value_size);
393 e0.
eval(*mesh0, cells0, fdata, {num_cells, num_points * value_size});
400 std::vector<value_type> fdata1(num_cells * num_points * value_size);
401 MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
402 value_type, MDSPAN_IMPL_STANDARD_NAMESPACE::dextents<std::size_t, 3>>
403 f1(fdata1.data(), value_size, num_cells, num_points);
404 for (std::size_t i = 0; i < f.extent(0); ++i)
405 for (std::size_t j = 0; j < f.extent(1); ++j)
406 for (std::size_t k = 0; k < f.extent(2); ++k)
407 f1(k, i, j) = f(i, j, k);
411 std::span<const value_type>(fdata1.data(), fdata1.size()),
412 {value_size, num_cells * num_points}, cells1);
442 void eval(std::span<const geometry_type>
x, std::array<std::size_t, 2> xshape,
443 std::span<const std::int32_t> cells, std::span<value_type> u,
444 std::array<std::size_t, 2> ushape)
const
449 assert(
x.size() == xshape[0] * xshape[1]);
450 assert(u.size() == ushape[0] * ushape[1]);
455 if (xshape[0] != cells.size())
457 throw std::runtime_error(
458 "Number of points and number of cells must be equal.");
461 if (xshape[0] != ushape[0])
463 throw std::runtime_error(
464 "Length of array for Function values must be the "
465 "same as the number of points.");
469 assert(_function_space);
470 auto mesh = _function_space->mesh();
472 const std::size_t gdim = mesh->geometry().dim();
473 const std::size_t tdim = mesh->topology()->dim();
474 auto map = mesh->topology()->index_map(tdim);
480 auto x_dofmap = mesh->geometry().dofmap();
481 const std::size_t num_dofs_g = cmap.
dim();
482 auto x_g = mesh->geometry().x();
485 auto element = _function_space->element();
487 const int bs_element = element->block_size();
488 const std::size_t reference_value_size
489 = element->reference_value_size() / bs_element;
490 const std::size_t value_size = _function_space->value_size() / bs_element;
491 const std::size_t space_dimension = element->space_dimension() / bs_element;
495 const int num_sub_elements = element->num_sub_elements();
496 if (num_sub_elements > 1 and num_sub_elements != bs_element)
498 throw std::runtime_error(
"Function::eval is not supported for mixed "
499 "elements. Extract subspaces.");
503 std::vector<value_type> coefficients(space_dimension * bs_element);
506 std::shared_ptr<const DofMap> dofmap = _function_space->dofmap();
508 const int bs_dof = dofmap->bs();
510 std::span<const std::uint32_t> cell_info;
511 if (element->needs_dof_transformations())
513 mesh->topology_mutable()->create_entity_permutations();
514 cell_info = std::span(mesh->topology()->get_cell_permutation_info());
517 std::vector<geometry_type> coord_dofs_b(num_dofs_g * gdim);
518 impl::mdspan_t<geometry_type, 2> coord_dofs(coord_dofs_b.data(), num_dofs_g,
520 std::vector<geometry_type> xp_b(1 * gdim);
521 impl::mdspan_t<geometry_type, 2> xp(xp_b.data(), 1, gdim);
524 std::ranges::fill(u, 0.0);
525 std::span<const value_type> _v = _x->array();
529 std::array<std::size_t, 4> phi0_shape = cmap.
tabulate_shape(1, 1);
530 std::vector<geometry_type> phi0_b(std::reduce(
531 phi0_shape.begin(), phi0_shape.end(), 1, std::multiplies{}));
532 impl::mdspan_t<const geometry_type, 4> phi0(phi0_b.data(), phi0_shape);
533 cmap.
tabulate(1, std::vector<geometry_type>(tdim), {1, tdim}, phi0_b);
534 auto dphi0 = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
535 phi0, std::pair(1, tdim + 1), 0,
536 MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent, 0);
541 std::vector<geometry_type> phi_b(
542 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
543 impl::mdspan_t<const geometry_type, 4> phi(phi_b.data(), phi_shape);
544 auto dphi = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
545 phi, std::pair(1, tdim + 1), 0,
546 MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent, 0);
549 std::vector<geometry_type> Xb(xshape[0] * tdim);
550 impl::mdspan_t<geometry_type, 2> X(Xb.data(), xshape[0], tdim);
553 std::vector<geometry_type> J_b(xshape[0] * gdim * tdim);
554 impl::mdspan_t<geometry_type, 3> J(J_b.data(), xshape[0], gdim, tdim);
555 std::vector<geometry_type> K_b(xshape[0] * tdim * gdim);
556 impl::mdspan_t<geometry_type, 3> K(K_b.data(), xshape[0], tdim, gdim);
557 std::vector<geometry_type> detJ(xshape[0]);
558 std::vector<geometry_type> det_scratch(2 * gdim * tdim);
561 for (std::size_t p = 0; p < cells.size(); ++p)
563 const int cell_index = cells[p];
570 auto x_dofs = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
571 x_dofmap, cell_index, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
572 assert(x_dofs.size() == num_dofs_g);
573 for (std::size_t i = 0; i < num_dofs_g; ++i)
575 const int pos = 3 * x_dofs[i];
576 for (std::size_t j = 0; j < gdim; ++j)
577 coord_dofs(i, j) = x_g[pos + j];
580 for (std::size_t j = 0; j < gdim; ++j)
581 xp(0, j) =
x[p * xshape[1] + j];
583 auto _J = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
584 J, p, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent,
585 MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
586 auto _K = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
587 K, p, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent,
588 MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
590 std::array<geometry_type, 3> Xpb = {0, 0, 0};
591 MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
593 MDSPAN_IMPL_STANDARD_NAMESPACE::extents<
594 std::size_t, 1, MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent>>
595 Xp(Xpb.data(), 1, tdim);
603 std::array<geometry_type, 3> x0 = {0, 0, 0};
604 for (std::size_t i = 0; i < coord_dofs.extent(1); ++i)
605 x0[i] += coord_dofs(0, i);
615 cmap.
tabulate(1, std::span(Xpb.data(), tdim), {1, tdim}, phi_b);
624 for (std::size_t j = 0; j < X.extent(1); ++j)
629 std::vector<geometry_type> basis_derivatives_reference_values_b(
630 1 * xshape[0] * space_dimension * reference_value_size);
631 impl::mdspan_t<const geometry_type, 4> basis_derivatives_reference_values(
632 basis_derivatives_reference_values_b.data(), 1, xshape[0],
633 space_dimension, reference_value_size);
634 std::vector<geometry_type> basis_values_b(space_dimension * value_size);
635 impl::mdspan_t<geometry_type, 2> basis_values(basis_values_b.data(),
636 space_dimension, value_size);
639 element->tabulate(basis_derivatives_reference_values_b, Xb,
640 {X.extent(0), X.extent(1)}, 0);
642 using xu_t = impl::mdspan_t<geometry_type, 2>;
643 using xU_t = impl::mdspan_t<const geometry_type, 2>;
644 using xJ_t = impl::mdspan_t<const geometry_type, 2>;
645 using xK_t = impl::mdspan_t<const geometry_type, 2>;
647 = element->basix_element().template map_fn<xu_t, xU_t, xJ_t, xK_t>();
650 auto apply_dof_transformation
651 = element->template dof_transformation_fn<geometry_type>(
657 if (element->symmetric())
660 while (matrix_size * matrix_size < ushape[1])
664 const std::size_t num_basis_values = space_dimension * reference_value_size;
665 for (std::size_t p = 0; p < cells.size(); ++p)
667 const int cell_index = cells[p];
673 apply_dof_transformation(
674 std::span(basis_derivatives_reference_values_b.data()
675 + p * num_basis_values,
677 cell_info, cell_index, reference_value_size);
680 auto _U = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
681 basis_derivatives_reference_values, 0, p,
682 MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent,
683 MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
684 auto _J = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
685 J, p, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent,
686 MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
687 auto _K = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
688 K, p, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent,
689 MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
690 push_forward_fn(basis_values, _U, _J, detJ[p], _K);
694 std::span<const std::int32_t> dofs = dofmap->cell_dofs(cell_index);
695 for (std::size_t i = 0; i < dofs.size(); ++i)
696 for (
int k = 0; k < bs_dof; ++k)
697 coefficients[bs_dof * i + k] = _v[bs_dof * dofs[i] + k];
699 if (element->symmetric())
704 for (
int k = 0; k < bs_element; ++k)
706 if (k - rowstart > row)
711 for (std::size_t i = 0; i < space_dimension; ++i)
713 for (std::size_t j = 0; j < value_size; ++j)
716 + (j * bs_element + row * matrix_size + k - rowstart)]
717 += coefficients[bs_element * i + k] * basis_values(i, j);
718 if (k - rowstart != row)
721 + (j * bs_element + row + matrix_size * (k - rowstart))]
722 += coefficients[bs_element * i + k] * basis_values(i, j);
731 for (
int k = 0; k < bs_element; ++k)
733 for (std::size_t i = 0; i < space_dimension; ++i)
735 for (std::size_t j = 0; j < value_size; ++j)
737 u[p * ushape[1] + (j * bs_element + k)]
738 += coefficients[bs_element * i + k] * basis_values(i, j);