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,
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);
493 =
mesh->geometry().cmaps().front();
496 auto x_dofmap =
mesh->geometry().dofmaps().front();
497 const std::size_t num_dofs_g = cmap.
dim();
498 auto x_g =
mesh->geometry().x();
501 auto element = _function_space->element();
503 const int bs_element = element->block_size();
504 const std::size_t reference_value_size = element->reference_value_size();
505 const std::size_t value_size
506 = _function_space->element()->reference_value_size();
507 const std::size_t space_dimension = element->space_dimension() / bs_element;
511 const int num_sub_elements = element->num_sub_elements();
512 if (num_sub_elements > 1 and num_sub_elements != bs_element)
514 throw std::runtime_error(
"Function::eval is not supported for mixed "
515 "elements. Extract subspaces.");
519 std::vector<value_type> coefficients(space_dimension * bs_element);
522 std::shared_ptr<const DofMap> dofmap = _function_space->dofmap();
524 const int bs_dof = dofmap->bs();
526 std::span<const std::uint32_t> cell_info;
527 if (element->needs_dof_transformations())
529 mesh->topology_mutable()->create_entity_permutations();
530 cell_info = std::span(
mesh->topology()->get_cell_permutation_info());
533 std::vector<geometry_type> coord_dofs_b(num_dofs_g * gdim);
534 impl::mdspan_t<geometry_type, 2> coord_dofs(coord_dofs_b.data(), num_dofs_g,
536 std::vector<geometry_type> xp_b(1 * gdim);
537 impl::mdspan_t<geometry_type, 2> xp(xp_b.data(), 1, gdim);
540 std::ranges::fill(u, 0);
541 std::span<const value_type> _v = _x->array();
545 std::array<std::size_t, 4> phi0_shape = cmap.
tabulate_shape(1, 1);
546 std::vector<geometry_type> phi0_b(std::reduce(
547 phi0_shape.begin(), phi0_shape.end(), 1, std::multiplies{}));
548 impl::mdspan_t<const geometry_type, 4> phi0(phi0_b.data(), phi0_shape);
549 cmap.
tabulate(1, std::vector<geometry_type>(tdim), {1, tdim}, phi0_b);
551 = md::submdspan(phi0, std::pair(1, tdim + 1), 0, md::full_extent, 0);
556 std::vector<geometry_type> phi_b(
557 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
558 impl::mdspan_t<const geometry_type, 4> phi(phi_b.data(), phi_shape);
560 = md::submdspan(phi, std::pair(1, tdim + 1), 0, md::full_extent, 0);
563 std::vector<geometry_type> Xb(xshape[0] * tdim);
564 impl::mdspan_t<geometry_type, 2> X(Xb.data(), xshape[0], tdim);
567 std::vector<geometry_type> J_b(xshape[0] * gdim * tdim);
568 impl::mdspan_t<geometry_type, 3> J(J_b.data(), xshape[0], gdim, tdim);
569 std::vector<geometry_type> K_b(xshape[0] * tdim * gdim);
570 impl::mdspan_t<geometry_type, 3> K(K_b.data(), xshape[0], tdim, gdim);
571 std::vector<geometry_type> detJ(xshape[0]);
572 std::vector<geometry_type> det_scratch(2 * gdim * tdim);
575 for (
auto cell_it = cells.begin(); cell_it != cells.end(); ++cell_it)
582 auto x_dofs = md::submdspan(x_dofmap, *cell_it, md::full_extent);
583 assert(x_dofs.size() == num_dofs_g);
584 for (std::size_t i = 0; i < num_dofs_g; ++i)
586 const int pos = 3 * x_dofs[i];
587 for (std::size_t j = 0; j < gdim; ++j)
588 coord_dofs(i, j) = x_g[pos + j];
591 std::size_t p = std::distance(cells.begin(), cell_it);
592 for (std::size_t j = 0; j < gdim; ++j)
593 xp(0, j) =
x[p * xshape[1] + j];
595 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
596 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
598 std::array<geometry_type, 3> Xpb{0, 0, 0};
599 md::mdspan<geometry_type, md::extents<std::size_t, 1, md::dynamic_extent>>
600 Xp(Xpb.data(), 1, tdim);
608 std::array<geometry_type, 3> x0{0, 0, 0};
609 for (std::size_t i = 0; i < coord_dofs.extent(1); ++i)
610 x0[i] += coord_dofs(0, i);
620 cmap.
tabulate(1, std::span(Xpb.data(), tdim), {1, tdim}, phi_b);
629 for (std::size_t j = 0; j < X.extent(1); ++j)
634 std::vector<geometry_type> basis_derivatives_reference_values_b(
635 1 * xshape[0] * space_dimension * reference_value_size);
636 impl::mdspan_t<const geometry_type, 4> basis_derivatives_reference_values(
637 basis_derivatives_reference_values_b.data(), 1, xshape[0],
638 space_dimension, reference_value_size);
639 std::vector<geometry_type> basis_values_b(space_dimension * value_size);
640 impl::mdspan_t<geometry_type, 2> basis_values(basis_values_b.data(),
641 space_dimension, value_size);
644 element->tabulate(basis_derivatives_reference_values_b, Xb,
645 {X.extent(0), X.extent(1)}, 0);
647 using xu_t = impl::mdspan_t<geometry_type, 2>;
648 using xU_t = impl::mdspan_t<const geometry_type, 2>;
649 using xJ_t = impl::mdspan_t<const geometry_type, 2>;
650 using xK_t = impl::mdspan_t<const geometry_type, 2>;
652 = element->basix_element().template map_fn<xu_t, xU_t, xJ_t, xK_t>();
655 auto apply_dof_transformation
656 = element->template dof_transformation_fn<geometry_type>(
662 if (element->symmetric())
665 while (matrix_size * matrix_size < (
int)ushape[1])
669 const std::size_t num_basis_values = space_dimension * reference_value_size;
670 for (
auto cell_it = cells.begin(); cell_it != cells.end(); ++cell_it)
677 std::size_t p = std::distance(cells.begin(), cell_it);
678 apply_dof_transformation(
679 std::span(basis_derivatives_reference_values_b.data()
680 + p * num_basis_values,
682 cell_info, *cell_it, reference_value_size);
685 auto _U = md::submdspan(basis_derivatives_reference_values, 0, p,
686 md::full_extent, md::full_extent);
687 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
688 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
689 push_forward_fn(basis_values, _U, _J, detJ[p], _K);
693 std::span<const std::int32_t> dofs = dofmap->cell_dofs(*cell_it);
694 for (std::size_t i = 0; i < dofs.size(); ++i)
695 for (
int k = 0; k < bs_dof; ++k)
696 coefficients[bs_dof * i + k] = _v[bs_dof * dofs[i] + k];
698 if (element->symmetric())
703 for (
int k = 0; k < bs_element; ++k)
705 if (k - rowstart > row)
710 for (std::size_t i = 0; i < space_dimension; ++i)
712 for (std::size_t j = 0; j < value_size; ++j)
715 + (j * bs_element + row * matrix_size + k - rowstart)]
716 += coefficients[bs_element * i + k] * basis_values(i, j);
717 if (k - rowstart != row)
720 + (j * bs_element + row + matrix_size * (k - rowstart))]
721 += coefficients[bs_element * i + k] * basis_values(i, j);
730 for (
int k = 0; k < bs_element; ++k)
732 for (std::size_t i = 0; i < space_dimension; ++i)
734 for (std::size_t j = 0; j < value_size; ++j)
736 u[p * ushape[1] + (j * bs_element + k)]
737 += coefficients[bs_element * i + k] * basis_values(i, j);