324 void eval(std::span<const geometry_type>
x, std::array<std::size_t, 2> xshape,
326 std::array<std::size_t, 2> ushape,
double tol,
int maxit)
const
331 assert(
x.size() == xshape[0] * xshape[1]);
332 assert(u.size() == ushape[0] * ushape[1]);
337 if (xshape[0] != cells.size())
339 throw std::invalid_argument(
340 "Number of points and number of cells must be equal.");
343 if (xshape[0] != ushape[0])
345 throw std::invalid_argument(
346 "Length of array for Function values must be the "
347 "same as the number of points.");
351 assert(_function_space);
352 auto mesh = _function_space->mesh();
354 const std::size_t gdim =
mesh->geometry().dim();
355 const std::size_t tdim =
mesh->topology()->dim();
356 auto map =
mesh->topology()->index_map(tdim);
360 =
mesh->geometry().cmaps().front();
363 auto x_dofmap =
mesh->geometry().dofmaps().front();
364 const std::size_t num_dofs_g = cmap.
dim();
365 auto x_g =
mesh->geometry().x();
368 auto element = _function_space->element();
370 const int bs_element = element->block_size();
371 const std::size_t reference_value_size = element->reference_value_size();
375 const std::size_t value_size = element->physical_base_value_size();
376 const std::size_t space_dimension = element->space_dimension() / bs_element;
380 const int num_sub_elements = element->num_sub_elements();
381 if (num_sub_elements > 1 and num_sub_elements != bs_element)
383 throw std::invalid_argument(
"Function::eval is not supported for mixed "
384 "elements. Extract subspaces.");
388 std::vector<value_type> coefficients(space_dimension * bs_element);
391 std::shared_ptr<const DofMap> dofmap = _function_space->dofmap();
393 const int bs_dof = dofmap->bs();
395 std::span<const std::uint32_t> cell_info;
396 if (element->needs_dof_transformations())
398 mesh->topology_mutable()->create_cell_permutations();
399 cell_info = std::span(
mesh->topology()->get_cell_permutation_info());
402 std::vector<geometry_type> coord_dofs_b(num_dofs_g * gdim);
403 impl::mdspan_t<geometry_type, 2> coord_dofs(coord_dofs_b.data(), num_dofs_g,
405 std::vector<geometry_type> xp_b(1 * gdim);
406 impl::mdspan_t<geometry_type, 2> xp(xp_b.data(), 1, gdim);
409 std::ranges::fill(u, 0);
410 std::span<const value_type> _v = _x->array();
414 std::array<std::size_t, 4> phi0_shape = cmap.
tabulate_shape(1, 1);
415 std::vector<geometry_type> phi0_b(std::reduce(
416 phi0_shape.begin(), phi0_shape.end(), 1, std::multiplies{}));
417 impl::mdspan_t<const geometry_type, 4> phi0(phi0_b.data(), phi0_shape);
418 cmap.
tabulate(1, std::vector<geometry_type>(tdim), {1, tdim}, phi0_b);
420 = md::submdspan(phi0, std::pair(1, tdim + 1), 0, md::full_extent, 0);
425 std::vector<geometry_type> phi_b(
426 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
427 impl::mdspan_t<const geometry_type, 4> phi(phi_b.data(), phi_shape);
429 = md::submdspan(phi, std::pair(1, tdim + 1), 0, md::full_extent, 0);
432 std::vector<geometry_type> Xb(xshape[0] * tdim);
433 impl::mdspan_t<geometry_type, 2> X(Xb.data(), xshape[0], tdim);
436 std::vector<geometry_type> J_b(xshape[0] * gdim * tdim);
437 impl::mdspan_t<geometry_type, 3> J(J_b.data(), xshape[0], gdim, tdim);
438 std::vector<geometry_type> K_b(xshape[0] * tdim * gdim);
439 impl::mdspan_t<geometry_type, 3> K(K_b.data(), xshape[0], tdim, gdim);
440 std::vector<geometry_type> detJ(xshape[0]);
441 std::vector<geometry_type> det_scratch(2 * gdim * tdim);
444 std::vector<geometry_type> pull_back_scratch(
448 for (
auto cell_it = cells.begin(); cell_it != cells.end(); ++cell_it)
455 auto x_dofs = md::submdspan(x_dofmap, *cell_it, md::full_extent);
456 assert(x_dofs.size() == num_dofs_g);
457 for (std::size_t i = 0; i < num_dofs_g; ++i)
459 const int pos = 3 * x_dofs[i];
460 for (std::size_t j = 0; j < gdim; ++j)
461 coord_dofs(i, j) = x_g[pos + j];
464 std::size_t p = std::ranges::distance(cells.begin(), cell_it);
465 for (std::size_t j = 0; j < gdim; ++j)
466 xp(0, j) =
x[p * xshape[1] + j];
468 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
469 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
471 std::array<geometry_type, 3> Xpb{0, 0, 0};
472 md::mdspan<geometry_type, md::extents<std::size_t, 1, md::dynamic_extent>>
473 Xp(Xpb.data(), 1, tdim);
481 std::array<geometry_type, 3> x0{0, 0, 0};
482 for (std::size_t i = 0; i < coord_dofs.extent(1); ++i)
483 x0[i] += coord_dofs(0, i);
494 cmap.
tabulate(1, std::span(Xpb.data(), tdim), {1, tdim}, phi_b);
503 for (std::size_t j = 0; j < X.extent(1); ++j)
508 std::vector<geometry_type> basis_derivatives_reference_values_b(
509 1 * xshape[0] * space_dimension * reference_value_size);
510 impl::mdspan_t<const geometry_type, 4> basis_derivatives_reference_values(
511 basis_derivatives_reference_values_b.data(), 1, xshape[0],
512 space_dimension, reference_value_size);
513 std::vector<geometry_type> basis_values_b(space_dimension * value_size);
514 impl::mdspan_t<geometry_type, 2> basis_values(basis_values_b.data(),
515 space_dimension, value_size);
518 element->tabulate(basis_derivatives_reference_values_b, Xb,
519 {X.extent(0), X.extent(1)}, 0);
521 using xu_t = impl::mdspan_t<geometry_type, 2>;
522 using xU_t = impl::mdspan_t<const geometry_type, 2>;
523 using xJ_t = impl::mdspan_t<const geometry_type, 2>;
524 using xK_t = impl::mdspan_t<const geometry_type, 2>;
526 = element->basix_element().template map_fn<xu_t, xU_t, xJ_t, xK_t>();
529 auto apply_dof_transformation
530 = element->template dof_transformation_fn<geometry_type>(
536 if (element->symmetric())
538 while (matrix_size * matrix_size < (
int)ushape[1])
542 const std::size_t num_basis_values = space_dimension * reference_value_size;
543 for (
auto cell_it = cells.begin(); cell_it != cells.end(); ++cell_it)
550 std::size_t p = std::ranges::distance(cells.begin(), cell_it);
551 if (apply_dof_transformation)
553 apply_dof_transformation(
554 std::span(basis_derivatives_reference_values_b.data()
555 + p * num_basis_values,
557 cell_info, *cell_it, reference_value_size);
561 auto _U = md::submdspan(basis_derivatives_reference_values, 0, p,
562 md::full_extent, md::full_extent);
563 auto _J = md::submdspan(J, p, md::full_extent, md::full_extent);
564 auto _K = md::submdspan(K, p, md::full_extent, md::full_extent);
565 push_forward_fn(basis_values, _U, _J, detJ[p], _K);
569 std::span<const std::int32_t> dofs = dofmap->cell_dofs(*cell_it);
570 for (std::size_t i = 0; i < dofs.size(); ++i)
571 for (
int k = 0; k < bs_dof; ++k)
572 coefficients[bs_dof * i + k] = _v[bs_dof * dofs[i] + k];
574 if (element->symmetric())
579 for (
int k = 0; k < bs_element; ++k)
581 if (k - rowstart > row)
586 for (std::size_t i = 0; i < space_dimension; ++i)
588 for (std::size_t j = 0; j < value_size; ++j)
591 + (j * bs_element + row * matrix_size + k - rowstart)]
592 += coefficients[bs_element * i + k] * basis_values(i, j);
593 if (k - rowstart != row)
596 + (j * bs_element + row + matrix_size * (k - rowstart))]
597 += coefficients[bs_element * i + k] * basis_values(i, j);
606 for (
int k = 0; k < bs_element; ++k)
608 for (std::size_t i = 0; i < space_dimension; ++i)
610 for (std::size_t j = 0; j < value_size; ++j)
612 u[p * ushape[1] + (j * bs_element + k)]
613 += coefficients[bs_element * i + k] * basis_values(i, j);