320 void eval(std::span<const geometry_type>
x, std::array<std::size_t, 2> xshape,
322 std::array<std::size_t, 2> ushape,
double tol,
int maxit)
const
327 assert(
x.size() == xshape[0] * xshape[1]);
328 assert(u.size() == ushape[0] * ushape[1]);
333 if (xshape[0] != cells.size())
335 throw std::invalid_argument(
336 "Number of points and number of cells must be equal.");
339 if (xshape[0] != ushape[0])
341 throw std::invalid_argument(
342 "Length of array for Function values must be the "
343 "same as the number of points.");
347 assert(_function_space);
348 auto mesh = _function_space->mesh();
350 const std::size_t gdim =
mesh->geometry().dim();
351 const std::size_t tdim =
mesh->topology()->dim();
352 auto map =
mesh->topology()->index_map(tdim);
356 =
mesh->geometry().cmaps().front();
359 auto x_dofmap =
mesh->geometry().dofmaps().front();
360 const std::size_t num_dofs_g = cmap.
dim();
361 auto x_g =
mesh->geometry().x();
364 auto element = _function_space->element();
366 const int bs_element = element->block_size();
367 const std::size_t reference_value_size = element->reference_value_size();
371 const std::size_t value_size = element->physical_base_value_size();
372 const std::size_t space_dimension = element->space_dimension() / bs_element;
376 const int num_sub_elements = element->num_sub_elements();
377 if (num_sub_elements > 1 and num_sub_elements != bs_element)
379 throw std::invalid_argument(
"Function::eval is not supported for mixed "
380 "elements. Extract subspaces.");
384 std::vector<value_type> coefficients(space_dimension * bs_element);
387 std::shared_ptr<const DofMap> dofmap = _function_space->dofmap();
389 const int bs_dof = dofmap->bs();
391 std::span<const std::uint32_t> cell_info;
392 if (element->needs_dof_transformations())
394 mesh->topology_mutable()->create_cell_permutations();
395 cell_info = std::span(
mesh->topology()->get_cell_permutation_info());
398 std::vector<geometry_type> coord_dofs_b(num_dofs_g * gdim);
399 impl::mdspan_t<geometry_type, 2> coord_dofs(coord_dofs_b.data(), num_dofs_g,
401 std::vector<geometry_type> xp_b(1 * gdim);
402 impl::mdspan_t<geometry_type, 2> xp(xp_b.data(), 1, gdim);
405 std::ranges::fill(u, 0);
406 std::span<const value_type> _v = _x->array();
410 std::array<std::size_t, 4> phi0_shape = cmap.
tabulate_shape(1, 1);
411 std::vector<geometry_type> phi0_b(std::reduce(
412 phi0_shape.begin(), phi0_shape.end(), 1, std::multiplies{}));
413 impl::mdspan_t<const geometry_type, 4> phi0(phi0_b.data(), phi0_shape);
414 cmap.
tabulate(1, std::vector<geometry_type>(tdim), {1, tdim}, phi0_b);
416 = md::submdspan(phi0, std::pair(1, tdim + 1), 0, md::full_extent, 0);
421 std::vector<geometry_type> phi_b(
422 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
423 impl::mdspan_t<const geometry_type, 4> phi(phi_b.data(), phi_shape);
425 = md::submdspan(phi, std::pair(1, tdim + 1), 0, md::full_extent, 0);
428 std::vector<geometry_type> Xb(xshape[0] * tdim);
429 impl::mdspan_t<geometry_type, 2> X(Xb.data(), xshape[0], tdim);
432 std::vector<geometry_type> J_b(xshape[0] * gdim * tdim);
433 impl::mdspan_t<geometry_type, 3> J(J_b.data(), xshape[0], gdim, tdim);
434 std::vector<geometry_type> K_b(xshape[0] * tdim * gdim);
435 impl::mdspan_t<geometry_type, 3> K(K_b.data(), xshape[0], tdim, gdim);
436 std::vector<geometry_type> detJ(xshape[0]);
437 std::vector<geometry_type> det_scratch(2 * gdim * tdim);
440 std::vector<geometry_type> pull_back_scratch(
444 for (
auto cell_it = cells.begin(); cell_it != cells.end(); ++cell_it)
451 auto x_dofs = md::submdspan(x_dofmap, *cell_it, md::full_extent);
452 assert(x_dofs.size() == num_dofs_g);
453 for (std::size_t i = 0; i < num_dofs_g; ++i)
455 const int pos = 3 * x_dofs[i];
456 for (std::size_t j = 0; j < gdim; ++j)
457 coord_dofs(i, j) = x_g[pos + j];
460 std::size_t p = std::ranges::distance(cells.begin(), cell_it);
461 for (std::size_t j = 0; j < gdim; ++j)
462 xp(0, j) =
x[p * xshape[1] + j];
464 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
465 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
467 std::array<geometry_type, 3> Xpb{0, 0, 0};
468 md::mdspan<geometry_type, md::extents<std::size_t, 1, md::dynamic_extent>>
469 Xp(Xpb.data(), 1, tdim);
477 std::array<geometry_type, 3> x0{0, 0, 0};
478 for (std::size_t i = 0; i < coord_dofs.extent(1); ++i)
479 x0[i] += coord_dofs(0, i);
490 cmap.
tabulate(1, std::span(Xpb.data(), tdim), {1, tdim}, phi_b);
499 for (std::size_t j = 0; j < X.extent(1); ++j)
504 std::vector<geometry_type> basis_derivatives_reference_values_b(
505 1 * xshape[0] * space_dimension * reference_value_size);
506 impl::mdspan_t<const geometry_type, 4> basis_derivatives_reference_values(
507 basis_derivatives_reference_values_b.data(), 1, xshape[0],
508 space_dimension, reference_value_size);
509 std::vector<geometry_type> basis_values_b(space_dimension * value_size);
510 impl::mdspan_t<geometry_type, 2> basis_values(basis_values_b.data(),
511 space_dimension, value_size);
514 element->tabulate(basis_derivatives_reference_values_b, Xb,
515 {X.extent(0), X.extent(1)}, 0);
517 using xu_t = impl::mdspan_t<geometry_type, 2>;
518 using xU_t = impl::mdspan_t<const geometry_type, 2>;
519 using xJ_t = impl::mdspan_t<const geometry_type, 2>;
520 using xK_t = impl::mdspan_t<const geometry_type, 2>;
522 = element->basix_element().template map_fn<xu_t, xU_t, xJ_t, xK_t>();
525 auto apply_dof_transformation
526 = element->template dof_transformation_fn<geometry_type>(
532 if (element->symmetric())
534 while (matrix_size * matrix_size < (
int)ushape[1])
538 const std::size_t num_basis_values = space_dimension * reference_value_size;
539 for (
auto cell_it = cells.begin(); cell_it != cells.end(); ++cell_it)
546 std::size_t p = std::ranges::distance(cells.begin(), cell_it);
547 if (apply_dof_transformation)
549 apply_dof_transformation(
550 std::span(basis_derivatives_reference_values_b.data()
551 + p * num_basis_values,
553 cell_info, *cell_it, reference_value_size);
557 auto _U = md::submdspan(basis_derivatives_reference_values, 0, p,
558 md::full_extent, md::full_extent);
559 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
560 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
561 push_forward_fn(basis_values, _U, _J, detJ[p], _K);
565 std::span<const std::int32_t> dofs = dofmap->cell_dofs(*cell_it);
566 for (std::size_t i = 0; i < dofs.size(); ++i)
567 for (
int k = 0; k < bs_dof; ++k)
568 coefficients[bs_dof * i + k] = _v[bs_dof * dofs[i] + k];
570 if (element->symmetric())
575 for (
int k = 0; k < bs_element; ++k)
577 if (k - rowstart > row)
582 for (std::size_t i = 0; i < space_dimension; ++i)
584 for (std::size_t j = 0; j < value_size; ++j)
587 + (j * bs_element + row * matrix_size + k - rowstart)]
588 += coefficients[bs_element * i + k] * basis_values(i, j);
589 if (k - rowstart != row)
592 + (j * bs_element + row + matrix_size * (k - rowstart))]
593 += coefficients[bs_element * i + k] * basis_values(i, j);
602 for (
int k = 0; k < bs_element; ++k)
604 for (std::size_t i = 0; i < space_dimension; ++i)
606 for (std::size_t j = 0; j < value_size; ++j)
608 u[p * ushape[1] + (j * bs_element + k)]
609 += coefficients[bs_element * i + k] * basis_values(i, j);