315 std::span<const std::int32_t> cells0,
316 std::span<const std::int32_t> cells1 = {})
323 assert(c->function_space());
324 assert(c->function_space()->mesh());
326 = c->function_space()->mesh().get();
331 else if (mesh != mesh0)
333 throw std::runtime_error(
334 "Expression coefficient Functions have different meshes.");
339 assert(_function_space);
340 assert(_function_space->mesh());
342 mesh0 = _function_space->mesh().get();
347 if (cells1.empty() and mesh0 == _function_space->mesh().get())
349 else if (cells0.size() != cells1.size())
350 throw std::runtime_error(
"Cells lists have different lengths.");
353 assert(_function_space->element());
356 throw std::runtime_error(
"Cannot interpolate Expression with Argument.");
357 if (value_size != _function_space->element()->value_size())
359 throw std::runtime_error(
360 "Function value size not equal to Expression value size.");
365 auto [X0, shape0] = e0.
X();
366 auto [X1, shape1] = _function_space->element()->interpolation_points();
367 if (shape0 != shape1)
369 throw std::runtime_error(
370 "Function element interpolation points has different shape to "
371 "Expression interpolation points");
374 for (std::size_t i = 0; i < X0.size(); ++i)
376 if (std::abs(X0[i] - X1[i]) > 1.0e-10)
378 throw std::runtime_error(
"Function element interpolation points not "
379 "equal to Expression interpolation points");
385 std::size_t num_cells = cells0.size();
386 std::size_t num_points = e0.
X().second[0];
387 std::vector<value_type> fdata(num_cells * num_points * value_size);
388 MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
390 MDSPAN_IMPL_STANDARD_NAMESPACE::dextents<std::size_t, 3>>
391 f(fdata.data(), num_cells, num_points, value_size);
394 e0.
eval(*mesh0, cells0, fdata, {num_cells, num_points * value_size});
401 std::vector<value_type> fdata1(num_cells * num_points * value_size);
402 MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
403 value_type, MDSPAN_IMPL_STANDARD_NAMESPACE::dextents<std::size_t, 3>>
404 f1(fdata1.data(), value_size, num_cells, num_points);
405 for (std::size_t i = 0; i < f.extent(0); ++i)
406 for (std::size_t j = 0; j < f.extent(1); ++j)
407 for (std::size_t k = 0; k < f.extent(2); ++k)
408 f1(k, i, j) = f(i, j, k);
412 std::span<const value_type>(fdata1.data(), fdata1.size()),
413 {value_size, num_cells * num_points}, cells1);
443 void eval(std::span<const geometry_type>
x, std::array<std::size_t, 2> xshape,
444 std::span<const std::int32_t> cells, std::span<value_type> u,
445 std::array<std::size_t, 2> ushape)
const
450 assert(
x.size() == xshape[0] * xshape[1]);
451 assert(u.size() == ushape[0] * ushape[1]);
456 if (xshape[0] != cells.size())
458 throw std::runtime_error(
459 "Number of points and number of cells must be equal.");
462 if (xshape[0] != ushape[0])
464 throw std::runtime_error(
465 "Length of array for Function values must be the "
466 "same as the number of points.");
470 assert(_function_space);
471 auto mesh = _function_space->mesh();
473 const std::size_t gdim = mesh->geometry().dim();
474 const std::size_t tdim = mesh->topology()->dim();
475 auto map = mesh->topology()->index_map(tdim);
481 auto x_dofmap = mesh->geometry().dofmap();
482 const std::size_t num_dofs_g = cmap.
dim();
483 auto x_g = mesh->geometry().x();
486 auto element = _function_space->element();
488 const int bs_element = element->block_size();
489 const std::size_t reference_value_size = element->reference_value_size();
490 const std::size_t value_size
491 = _function_space->element()->reference_value_size();
492 const std::size_t space_dimension = element->space_dimension() / bs_element;
496 const int num_sub_elements = element->num_sub_elements();
497 if (num_sub_elements > 1 and num_sub_elements != bs_element)
499 throw std::runtime_error(
"Function::eval is not supported for mixed "
500 "elements. Extract subspaces.");
504 std::vector<value_type> coefficients(space_dimension * bs_element);
507 std::shared_ptr<const DofMap> dofmap = _function_space->dofmap();
509 const int bs_dof = dofmap->bs();
511 std::span<const std::uint32_t> cell_info;
512 if (element->needs_dof_transformations())
514 mesh->topology_mutable()->create_entity_permutations();
515 cell_info = std::span(mesh->topology()->get_cell_permutation_info());
518 std::vector<geometry_type> coord_dofs_b(num_dofs_g * gdim);
519 impl::mdspan_t<geometry_type, 2> coord_dofs(coord_dofs_b.data(), num_dofs_g,
521 std::vector<geometry_type> xp_b(1 * gdim);
522 impl::mdspan_t<geometry_type, 2> xp(xp_b.data(), 1, gdim);
525 std::ranges::fill(u, 0.0);
526 std::span<const value_type> _v = _x->array();
530 std::array<std::size_t, 4> phi0_shape = cmap.
tabulate_shape(1, 1);
531 std::vector<geometry_type> phi0_b(std::reduce(
532 phi0_shape.begin(), phi0_shape.end(), 1, std::multiplies{}));
533 impl::mdspan_t<const geometry_type, 4> phi0(phi0_b.data(), phi0_shape);
534 cmap.
tabulate(1, std::vector<geometry_type>(tdim), {1, tdim}, phi0_b);
535 auto dphi0 = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
536 phi0, std::pair(1, tdim + 1), 0,
537 MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent, 0);
542 std::vector<geometry_type> phi_b(
543 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
544 impl::mdspan_t<const geometry_type, 4> phi(phi_b.data(), phi_shape);
545 auto dphi = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
546 phi, std::pair(1, tdim + 1), 0,
547 MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent, 0);
550 std::vector<geometry_type> Xb(xshape[0] * tdim);
551 impl::mdspan_t<geometry_type, 2> X(Xb.data(), xshape[0], tdim);
554 std::vector<geometry_type> J_b(xshape[0] * gdim * tdim);
555 impl::mdspan_t<geometry_type, 3> J(J_b.data(), xshape[0], gdim, tdim);
556 std::vector<geometry_type> K_b(xshape[0] * tdim * gdim);
557 impl::mdspan_t<geometry_type, 3> K(K_b.data(), xshape[0], tdim, gdim);
558 std::vector<geometry_type> detJ(xshape[0]);
559 std::vector<geometry_type> det_scratch(2 * gdim * tdim);
562 for (std::size_t p = 0; p < cells.size(); ++p)
564 const int cell_index = cells[p];
571 auto x_dofs = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
572 x_dofmap, cell_index, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
573 assert(x_dofs.size() == num_dofs_g);
574 for (std::size_t i = 0; i < num_dofs_g; ++i)
576 const int pos = 3 * x_dofs[i];
577 for (std::size_t j = 0; j < gdim; ++j)
578 coord_dofs(i, j) = x_g[pos + j];
581 for (std::size_t j = 0; j < gdim; ++j)
582 xp(0, j) =
x[p * xshape[1] + j];
584 auto _J = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
585 J, p, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent,
586 MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
587 auto _K = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
588 K, p, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent,
589 MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
591 std::array<geometry_type, 3> Xpb = {0, 0, 0};
592 MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
594 MDSPAN_IMPL_STANDARD_NAMESPACE::extents<
595 std::size_t, 1, MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent>>
596 Xp(Xpb.data(), 1, tdim);
604 std::array<geometry_type, 3> x0 = {0, 0, 0};
605 for (std::size_t i = 0; i < coord_dofs.extent(1); ++i)
606 x0[i] += coord_dofs(0, i);
616 cmap.
tabulate(1, std::span(Xpb.data(), tdim), {1, tdim}, phi_b);
625 for (std::size_t j = 0; j < X.extent(1); ++j)
630 std::vector<geometry_type> basis_derivatives_reference_values_b(
631 1 * xshape[0] * space_dimension * reference_value_size);
632 impl::mdspan_t<const geometry_type, 4> basis_derivatives_reference_values(
633 basis_derivatives_reference_values_b.data(), 1, xshape[0],
634 space_dimension, reference_value_size);
635 std::vector<geometry_type> basis_values_b(space_dimension * value_size);
636 impl::mdspan_t<geometry_type, 2> basis_values(basis_values_b.data(),
637 space_dimension, value_size);
640 element->tabulate(basis_derivatives_reference_values_b, Xb,
641 {X.extent(0), X.extent(1)}, 0);
643 using xu_t = impl::mdspan_t<geometry_type, 2>;
644 using xU_t = impl::mdspan_t<const geometry_type, 2>;
645 using xJ_t = impl::mdspan_t<const geometry_type, 2>;
646 using xK_t = impl::mdspan_t<const geometry_type, 2>;
648 = element->basix_element().template map_fn<xu_t, xU_t, xJ_t, xK_t>();
651 auto apply_dof_transformation
652 = element->template dof_transformation_fn<geometry_type>(
658 if (element->symmetric())
661 while (matrix_size * matrix_size < ushape[1])
665 const std::size_t num_basis_values = space_dimension * reference_value_size;
666 for (std::size_t p = 0; p < cells.size(); ++p)
668 const int cell_index = cells[p];
674 apply_dof_transformation(
675 std::span(basis_derivatives_reference_values_b.data()
676 + p * num_basis_values,
678 cell_info, cell_index, reference_value_size);
681 auto _U = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
682 basis_derivatives_reference_values, 0, p,
683 MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent,
684 MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
685 auto _J = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
686 J, p, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent,
687 MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
688 auto _K = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
689 K, p, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent,
690 MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
691 push_forward_fn(basis_values, _U, _J, detJ[p], _K);
695 std::span<const std::int32_t> dofs = dofmap->cell_dofs(cell_index);
696 for (std::size_t i = 0; i < dofs.size(); ++i)
697 for (
int k = 0; k < bs_dof; ++k)
698 coefficients[bs_dof * i + k] = _v[bs_dof * dofs[i] + k];
700 if (element->symmetric())
705 for (
int k = 0; k < bs_element; ++k)
707 if (k - rowstart > row)
712 for (std::size_t i = 0; i < space_dimension; ++i)
714 for (std::size_t j = 0; j < value_size; ++j)
717 + (j * bs_element + row * matrix_size + k - rowstart)]
718 += coefficients[bs_element * i + k] * basis_values(i, j);
719 if (k - rowstart != row)
722 + (j * bs_element + row + matrix_size * (k - rowstart))]
723 += coefficients[bs_element * i + k] * basis_values(i, j);
732 for (
int k = 0; k < bs_element; ++k)
734 for (std::size_t i = 0; i < space_dimension; ++i)
736 for (std::size_t j = 0; j < value_size; ++j)
738 u[p * ushape[1] + (j * bs_element + k)]
739 += coefficients[bs_element * i + k] * basis_values(i, j);