307 assert(c->function_space());
308 assert(c->function_space()->mesh());
309 if (
auto mesh = c->function_space()->mesh().get(); !mesh0)
311 else if (
mesh != mesh0)
313 throw std::runtime_error(
314 "Expression coefficient Functions have different meshes.");
319 assert(_function_space);
320 assert(_function_space->mesh());
322 mesh0 = _function_space->mesh().get();
324 if (cells0.size() != cells1.size())
325 throw std::runtime_error(
"Cell lists have different lengths.");
328 assert(_function_space->element());
331 throw std::runtime_error(
"Cannot interpolate Expression with Argument.");
333 if (value_size != (std::size_t)_function_space->element()->value_size())
335 throw std::runtime_error(
336 "Function value size not equal to Expression value size.");
341 auto [X0, shape0] = e0.
X();
342 auto [X1, shape1] = _function_space->element()->interpolation_points();
343 if (shape0 != shape1)
345 throw std::runtime_error(
346 "Function element interpolation points has different shape to "
347 "Expression interpolation points");
350 for (std::size_t i = 0; i < X0.size(); ++i)
352 if (std::abs(X0[i] - X1[i]) > 1.0e-10)
354 throw std::runtime_error(
"Function element interpolation points not "
355 "equal to Expression interpolation points");
361 std::size_t num_cells = cells0.size();
362 std::size_t num_points = e0.
X().second[0];
363 std::vector<value_type> fdata(num_cells * num_points * value_size);
364 md::mdspan<const value_type, md::dextents<std::size_t, 3>> f(
365 fdata.data(), num_cells, num_points, value_size);
368 std::vector<std::int32_t> _cells0(cells0.begin(), cells0.end());
370 md::mdspan(_cells0.data(), _cells0.size()));
377 std::vector<value_type> fdata1(num_cells * num_points * value_size);
378 md::mdspan<value_type, md::dextents<std::size_t, 3>> f1(
379 fdata1.data(), value_size, num_cells, num_points);
380 for (std::size_t i = 0; i < f.extent(0); ++i)
381 for (std::size_t j = 0; j < f.extent(1); ++j)
382 for (std::size_t k = 0; k < f.extent(2); ++k)
383 f1(k, i, j) = f(i, j, k);
387 std::span<const value_type>(fdata1.data(), fdata1.size()),
388 {value_size, num_cells * num_points}, cells1);
457 void eval(std::span<const geometry_type>
x, std::array<std::size_t, 2> xshape,
458 CellRange auto&& cells, std::span<value_type> u,
459 std::array<std::size_t, 2> ushape,
double tol,
int maxit)
const
464 assert(
x.size() == xshape[0] * xshape[1]);
465 assert(u.size() == ushape[0] * ushape[1]);
470 if (xshape[0] != cells.size())
472 throw std::runtime_error(
473 "Number of points and number of cells must be equal.");
476 if (xshape[0] != ushape[0])
478 throw std::runtime_error(
479 "Length of array for Function values must be the "
480 "same as the number of points.");
484 assert(_function_space);
485 auto mesh = _function_space->mesh();
487 const std::size_t gdim =
mesh->geometry().dim();
488 const std::size_t tdim =
mesh->topology()->dim();
489 auto map =
mesh->topology()->index_map(tdim);
495 auto x_dofmap =
mesh->geometry().dofmap();
496 const std::size_t num_dofs_g = cmap.
dim();
497 auto x_g =
mesh->geometry().x();
500 auto element = _function_space->element();
502 const int bs_element = element->block_size();
503 const std::size_t reference_value_size = element->reference_value_size();
504 const std::size_t value_size
505 = _function_space->element()->reference_value_size();
506 const std::size_t space_dimension = element->space_dimension() / bs_element;
510 const int num_sub_elements = element->num_sub_elements();
511 if (num_sub_elements > 1 and num_sub_elements != bs_element)
513 throw std::runtime_error(
"Function::eval is not supported for mixed "
514 "elements. Extract subspaces.");
518 std::vector<value_type> coefficients(space_dimension * bs_element);
521 std::shared_ptr<const DofMap> dofmap = _function_space->dofmap();
523 const int bs_dof = dofmap->bs();
525 std::span<const std::uint32_t> cell_info;
526 if (element->needs_dof_transformations())
528 mesh->topology_mutable()->create_entity_permutations();
529 cell_info = std::span(
mesh->topology()->get_cell_permutation_info());
532 std::vector<geometry_type> coord_dofs_b(num_dofs_g * gdim);
533 impl::mdspan_t<geometry_type, 2> coord_dofs(coord_dofs_b.data(), num_dofs_g,
535 std::vector<geometry_type> xp_b(1 * gdim);
536 impl::mdspan_t<geometry_type, 2> xp(xp_b.data(), 1, gdim);
539 std::ranges::fill(u, 0);
540 std::span<const value_type> _v = _x->array();
544 std::array<std::size_t, 4> phi0_shape = cmap.
tabulate_shape(1, 1);
545 std::vector<geometry_type> phi0_b(std::reduce(
546 phi0_shape.begin(), phi0_shape.end(), 1, std::multiplies{}));
547 impl::mdspan_t<const geometry_type, 4> phi0(phi0_b.data(), phi0_shape);
548 cmap.
tabulate(1, std::vector<geometry_type>(tdim), {1, tdim}, phi0_b);
550 = md::submdspan(phi0, std::pair(1, tdim + 1), 0, md::full_extent, 0);
555 std::vector<geometry_type> phi_b(
556 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
557 impl::mdspan_t<const geometry_type, 4> phi(phi_b.data(), phi_shape);
559 = md::submdspan(phi, std::pair(1, tdim + 1), 0, md::full_extent, 0);
562 std::vector<geometry_type> Xb(xshape[0] * tdim);
563 impl::mdspan_t<geometry_type, 2> X(Xb.data(), xshape[0], tdim);
566 std::vector<geometry_type> J_b(xshape[0] * gdim * tdim);
567 impl::mdspan_t<geometry_type, 3> J(J_b.data(), xshape[0], gdim, tdim);
568 std::vector<geometry_type> K_b(xshape[0] * tdim * gdim);
569 impl::mdspan_t<geometry_type, 3> K(K_b.data(), xshape[0], tdim, gdim);
570 std::vector<geometry_type> detJ(xshape[0]);
571 std::vector<geometry_type> det_scratch(2 * gdim * tdim);
574 for (
auto cell_it = cells.begin(); cell_it != cells.end(); ++cell_it)
581 auto x_dofs = md::submdspan(x_dofmap, *cell_it, md::full_extent);
582 assert(x_dofs.size() == num_dofs_g);
583 for (std::size_t i = 0; i < num_dofs_g; ++i)
585 const int pos = 3 * x_dofs[i];
586 for (std::size_t j = 0; j < gdim; ++j)
587 coord_dofs(i, j) = x_g[pos + j];
590 std::size_t p = std::distance(cells.begin(), cell_it);
591 for (std::size_t j = 0; j < gdim; ++j)
592 xp(0, j) =
x[p * xshape[1] + j];
594 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
595 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
597 std::array<geometry_type, 3> Xpb{0, 0, 0};
598 md::mdspan<geometry_type, md::extents<std::size_t, 1, md::dynamic_extent>>
599 Xp(Xpb.data(), 1, tdim);
607 std::array<geometry_type, 3> x0{0, 0, 0};
608 for (std::size_t i = 0; i < coord_dofs.extent(1); ++i)
609 x0[i] += coord_dofs(0, i);
619 cmap.
tabulate(1, std::span(Xpb.data(), tdim), {1, tdim}, phi_b);
628 for (std::size_t j = 0; j < X.extent(1); ++j)
633 std::vector<geometry_type> basis_derivatives_reference_values_b(
634 1 * xshape[0] * space_dimension * reference_value_size);
635 impl::mdspan_t<const geometry_type, 4> basis_derivatives_reference_values(
636 basis_derivatives_reference_values_b.data(), 1, xshape[0],
637 space_dimension, reference_value_size);
638 std::vector<geometry_type> basis_values_b(space_dimension * value_size);
639 impl::mdspan_t<geometry_type, 2> basis_values(basis_values_b.data(),
640 space_dimension, value_size);
643 element->tabulate(basis_derivatives_reference_values_b, Xb,
644 {X.extent(0), X.extent(1)}, 0);
646 using xu_t = impl::mdspan_t<geometry_type, 2>;
647 using xU_t = impl::mdspan_t<const geometry_type, 2>;
648 using xJ_t = impl::mdspan_t<const geometry_type, 2>;
649 using xK_t = impl::mdspan_t<const geometry_type, 2>;
651 = element->basix_element().template map_fn<xu_t, xU_t, xJ_t, xK_t>();
654 auto apply_dof_transformation
655 = element->template dof_transformation_fn<geometry_type>(
661 if (element->symmetric())
664 while (matrix_size * matrix_size < (
int)ushape[1])
668 const std::size_t num_basis_values = space_dimension * reference_value_size;
669 for (
auto cell_it = cells.begin(); cell_it != cells.end(); ++cell_it)
676 std::size_t p = std::distance(cells.begin(), cell_it);
677 apply_dof_transformation(
678 std::span(basis_derivatives_reference_values_b.data()
679 + p * num_basis_values,
681 cell_info, *cell_it, reference_value_size);
684 auto _U = md::submdspan(basis_derivatives_reference_values, 0, p,
685 md::full_extent, md::full_extent);
686 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
687 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
688 push_forward_fn(basis_values, _U, _J, detJ[p], _K);
692 std::span<const std::int32_t> dofs = dofmap->cell_dofs(*cell_it);
693 for (std::size_t i = 0; i < dofs.size(); ++i)
694 for (
int k = 0; k < bs_dof; ++k)
695 coefficients[bs_dof * i + k] = _v[bs_dof * dofs[i] + k];
697 if (element->symmetric())
702 for (
int k = 0; k < bs_element; ++k)
704 if (k - rowstart > row)
709 for (std::size_t i = 0; i < space_dimension; ++i)
711 for (std::size_t j = 0; j < value_size; ++j)
714 + (j * bs_element + row * matrix_size + k - rowstart)]
715 += coefficients[bs_element * i + k] * basis_values(i, j);
716 if (k - rowstart != row)
719 + (j * bs_element + row + matrix_size * (k - rowstart))]
720 += coefficients[bs_element * i + k] * basis_values(i, j);
729 for (
int k = 0; k < bs_element; ++k)
731 for (std::size_t i = 0; i < space_dimension; ++i)
733 for (std::size_t j = 0; j < value_size; ++j)
735 u[p * ushape[1] + (j * bs_element + k)]
736 += coefficients[bs_element * i + k] * basis_values(i, j);