11#include "FiniteElement.h"
14#include "FunctionSpace.h"
17#include <basix/mdspan.hpp>
19#include <dolfinx/mesh/Topology.h>
32template <dolfinx::scalar T, std::
floating_po
int U>
44template <dolfinx::scalar T, std::
floating_po
int U>
45std::span<const std::uint32_t>
46get_cell_orientation_info(
const Function<T, U>& coefficient)
48 std::span<const std::uint32_t> cell_info;
49 auto element = coefficient.function_space()->element();
51 if (element->needs_dof_transformations())
53 auto mesh = coefficient.function_space()->mesh();
54 mesh->topology_mutable()->create_entity_permutations();
55 cell_info = std::span(mesh->topology()->get_cell_permutation_info());
77template <
int _bs, dolfinx::scalar T>
79 std::span<const T> v, std::span<const std::uint32_t> cell_info,
80 const DofMap& dofmap,
auto transform)
83 for (std::size_t i = 0; i < dofs.size(); ++i)
85 if constexpr (_bs < 0)
87 const int pos_c = bs * i;
88 const int pos_v = bs * dofs[i];
89 for (
int k = 0; k < bs; ++k)
90 coeffs[pos_c + k] = v[pos_v + k];
95 const int pos_c = _bs * i;
96 const int pos_v = _bs * dofs[i];
97 for (
int k = 0; k < _bs; ++k)
98 coeffs[pos_c + k] = v[pos_v + k];
102 transform(coeffs, cell_info,
cell, 1);
121template <dolfinx::scalar T, std::
floating_po
int U>
124 std::span<const std::uint32_t> cell_info,
125 auto cells, std::int32_t offset)
127 static_assert(cells.rank() == 1);
130 std::span<const T> v = u.
x()->array();
134 int space_dim = element->space_dimension();
140 const int bs = dofmap.
bs();
147 auto pack_for_bs = [&cells, &c, &cstride, &offset, &space_dim, &bs, &v,
148 &cell_info, &dofmap, &transformation](
auto bs_c)
150 for (std::size_t e = 0; e < cells.extent(0); ++e)
152 if (std::int32_t
cell = cells(e);
cell >= 0)
154 auto cell_coeff = c.subspan(e * cstride + offset, space_dim);
164 pack_for_bs(std::integral_constant<int, 1>());
167 pack_for_bs(std::integral_constant<int, 2>());
170 pack_for_bs(std::integral_constant<int, 3>());
173 pack_for_bs(std::integral_constant<int, -1>());
186template <dolfinx::scalar T, std::
floating_po
int U>
187std::pair<std::vector<T>,
int>
191 std::size_t num_entities = 0;
193 if (
const std::vector<std::shared_ptr<
const Function<T, U>>>& coefficients
195 !coefficients.empty())
198 cstride = offsets.back();
210 num_entities = form.
domain(integral_type, idx, 0).size();
215 return {std::vector<T>(num_entities * cstride), cstride};
223template <dolfinx::scalar T, std::
floating_po
int U>
224std::map<std::pair<IntegralType, int>, std::pair<std::vector<T>,
int>>
227 std::map<std::pair<IntegralType, int>, std::pair<std::vector<T>,
int>> coeffs;
234 for (
int idx = 0; idx < n; ++idx)
236 coeffs.emplace_hint(coeffs.end(), std::pair{type, idx},
265template <dolfinx::scalar T, std::
floating_po
int U>
267 std::map<std::pair<IntegralType, int>,
268 std::pair<std::vector<T>,
int>>& coeffs)
270 const std::vector<std::shared_ptr<const Function<T, U>>>& coefficients
274 for (
auto& [integral_key, coeff_data] : coeffs)
276 auto [integral_type, idx] = integral_key;
277 std::vector<T>& c = coeff_data.first;
278 int cstride = coeff_data.second;
279 if (!coefficients.empty())
281 switch (integral_type)
287 const int form_tdim = form.
mesh()->topology()->dim();
293 auto mesh = coefficients[coeff]->function_space()->mesh();
300 if (
int codim = form_tdim -
mesh->topology()->dim(); codim > 0)
302 throw std::runtime_error(
"Should not be packing coefficients with "
303 "codim>0 in a cell integral");
306 std::span<const std::int32_t> cells_b
308 md::mdspan cells(cells_b.data(), cells_b.size());
309 std::span<const std::uint32_t> cell_info
310 = impl::get_cell_orientation_info(*coefficients[coeff]);
311 impl::pack_coefficient_entity(std::span(c), cstride,
312 *coefficients[coeff], cell_info, cells,
323 auto mesh = coefficients[coeff]->function_space()->mesh();
324 std::span<const std::int32_t> facets_b
326 md::mdspan<
const std::int32_t,
327 md::extents<std::size_t, md::dynamic_extent, 4>>
328 facets(facets_b.data(), facets_b.size() / 4, 4);
330 std::span<const std::uint32_t> cell_info
331 = impl::get_cell_orientation_info(*coefficients[coeff]);
342 auto cells0 = md::submdspan(facets, md::full_extent, 0);
343 impl::pack_coefficient_entity(std::span(c), 2 * cstride,
344 *coefficients[coeff], cell_info, cells0,
348 auto cells1 = md::submdspan(facets, md::full_extent, 2);
349 impl::pack_coefficient_entity(std::span(c), 2 * cstride,
350 *coefficients[coeff], cell_info, cells1,
351 offsets[coeff] + offsets[coeff + 1]);
365 auto mesh = coefficients[coeff]->function_space()->mesh();
368 std::span<const std::int32_t> entities_b
370 md::mdspan<
const std::int32_t,
371 md::extents<std::size_t, md::dynamic_extent, 2>>
372 entities(entities_b.data(), entities_b.size() / 2, 2);
373 std::span<const std::uint32_t> cell_info
374 = impl::get_cell_orientation_info(*coefficients[coeff]);
375 impl::pack_coefficient_entity(
376 std::span(c), cstride, *coefficients[coeff], cell_info,
377 md::submdspan(entities, md::full_extent, 0), offsets[coeff]);
382 throw std::runtime_error(
383 "Could not pack coefficient. Integral type not supported.");
402template <dolfinx::scalar T, std::
floating_po
int U>
406 std::optional<std::reference_wrapper<const dolfinx::mesh::EntityMap>>
412 auto span_to_vector = [](
auto entities)
414 assert(entities.rank() == 1);
416 std::vector<std::int32_t> vec;
417 vec.reserve(entities.extent(0));
418 for (std::size_t i = 0; i < entities.extent(0); ++i)
419 vec.push_back(entities[i]);
423 if (mesh_c->topology() ==
mesh.topology())
426 if constexpr (entities.rank() == 1)
427 return span_to_vector(entities);
431 return span_to_vector(md::submdspan(entities, md::full_extent, 0));
435 assert(entity_map.has_value());
437 int tdim = topology.
dim();
438 int codim = tdim - mesh_c->topology()->dim();
443 if constexpr (entities.rank() == 1)
449 else if constexpr (entities.rank() == 2)
454 auto cells = md::submdspan(entities, md::full_extent, 0);
463 throw std::runtime_error(
464 "Unsupported mapping. Can only map from submesh to parent mesh.");
467 auto c_to_e = topology.
connectivity(tdim, tdim - codim);
470 throw std::runtime_error(std::format(
471 "Topology connectivity from codim {} to {} not found.", tdim,
475 std::vector<std::int32_t> contiguous_cells;
476 contiguous_cells.reserve(entities.extent(0));
477 for (std::size_t e = 0; e < entities.extent(0); ++e)
479 contiguous_cells.push_back(
480 c_to_e->links(entities(e, 0))[entities(e, 1)]);
504template <dolfinx::scalar T, std::
floating_po
int U>
506 const std::vector<std::reference_wrapper<
const Function<T, U>>>& coeffs,
508 const std::vector<std::reference_wrapper<const dolfinx::mesh::EntityMap>>&
510 std::span<const int> offsets, std::span<T> c)
513 assert(!offsets.empty());
514 const int cstride = offsets.back();
516 if (c.size() < entities.extent(0) * offsets.back())
517 throw std::runtime_error(
"Coefficient packing span is too small.");
526 auto it = std::ranges::find_if(
530 return (em.
topology() == mesh0->topology()
536 if (it == entity_maps.end())
538 throw std::runtime_error(
539 "Incompatible mesh. argument entity_maps must be provided.");
545 for (std::size_t coeff = 0; coeff < coeffs.size(); ++coeff)
548 auto mesh_c = coeffs[coeff].get().function_space()->mesh();
549 std::vector<std::int32_t> coefficient_cells;
550 if (mesh_c->topology() ==
mesh.topology())
553 coeffs[coeff].get(),
mesh, entities, std::nullopt);
560 coeffs[coeff].get(),
mesh, entities,
561 std::reference_wrapper<const mesh::EntityMap>(emap));
564 std::span<const std::uint32_t> cell_info
565 = impl::get_cell_orientation_info(coeffs[coeff].get());
566 md::mdspan cells(coefficient_cells.data(), coefficient_cells.size());
567 impl::pack_coefficient_entity(std::span(c), cstride, coeffs[coeff].get(),
568 cell_info, cells, offsets[coeff]);
583 std::int32_t size = std::accumulate(
584 c.cbegin(), c.cend(), 0, [](std::int32_t sum,
auto& constant)
585 { return sum + constant.get().value.size(); });
588 std::vector<T> constant_values(size);
589 std::int32_t offset = 0;
590 for (
auto& constant : c)
592 std::ranges::copy(constant.get().value,
593 std::next(constant_values.begin(), offset));
594 offset += constant.get().value.size();
597 return constant_values;
605 requires std::convertible_to<
607 typename std::decay_t<U>::geometry_type>>
608 or std::convertible_to<
610 typename std::decay_t<U>::geometry_type>>
613 using T =
typename std::decay_t<U>::scalar_type;
614 std::vector<std::reference_wrapper<const Constant<T>>> c;
615 c.reserve(u.constants().size());
616 std::ranges::transform(u.constants(), std::back_inserter(c),
Degree-of-freedom map representations and tools.
Constant (in space) value which can be attached to a Form.
Definition Constant.h:22
Degree-of-freedom map.
Definition DofMap.h:73
std::span< const std::int32_t > cell_dofs(std::int32_t c) const
Local-to-global mapping of dofs on a cell.
Definition DofMap.h:127
int bs() const noexcept
Return the block size for the dofmap.
Definition DofMap.cpp:164
An Expression represents a mathematical expression evaluated at a pre-defined points on a reference c...
Definition Expression.h:43
std::shared_ptr< const FunctionSpace< geometry_type > > function_space() const
Access the function space.
Definition Function.h:147
std::shared_ptr< const la::Vector< value_type > > x() const
Underlying vector (const version).
Definition Function.h:153
A bidirectional map relating entities in one topology to another.
Definition EntityMap.h:22
std::vector< std::int32_t > sub_topology_to_topology(CellRange auto &&entities, bool inverse) const
Map entities between the sub-topology and the parent topology.
Definition EntityMap.h:104
std::shared_ptr< const Topology > sub_topology() const
Get the sub-topology.
Definition EntityMap.cpp:23
std::shared_ptr< const Topology > topology() const
Get the (parent) topology.
Definition EntityMap.cpp:18
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:23
Topology stores the topology of a mesh, consisting of mesh entities and connectivity (incidence relat...
Definition Topology.h:49
std::shared_ptr< const graph::AdjacencyList< std::int32_t > > connectivity(std::array< int, 2 > d0, std::array< int, 2 > d1) const
Get the connectivity from entities of topological dimension d0 to dimension d1.
Definition Topology.cpp:950
int dim() const noexcept
Topological dimension of the mesh.
Definition Topology.cpp:888
Concept for mdspan of rank 1 or 2.
Definition traits.h:35
Finite element method functionality.
Definition assemble_expression_impl.h:23
@ transpose
Transpose.
Definition FiniteElement.h:28
@ inverse
Inverse.
Definition FiniteElement.h:29
void pack_coefficients(const Form< T, U > &form, std::map< std::pair< IntegralType, int >, std::pair< std::vector< T >, int > > &coeffs)
Pack coefficients of a Form.
Definition pack.h:266
std::pair< std::vector< T >, int > allocate_coefficient_storage(const Form< T, U > &form, IntegralType integral_type, int idx)
Allocate storage for coefficients of a pair (integral_type, idx) from a Form.
Definition pack.h:188
std::vector< std::int32_t > extract_coefficient_cells_from_entities(const fem::Function< T, U > &coeff, const mesh::Mesh< U > &mesh, fem::MDSpan2 auto entities, std::optional< std::reference_wrapper< const dolfinx::mesh::EntityMap > > entity_map)
Given a Function and a related mesh and its integration entities, extract the cell indices of the coe...
Definition pack.h:403
IntegralType
Type of integral.
Definition Form.h:41
@ vertex
Vertex.
Definition Form.h:45
@ interior_facet
Interior facet.
Definition Form.h:44
@ ridge
Ridge.
Definition Form.h:46
@ cell
Cell.
Definition Form.h:42
@ exterior_facet
Exterior facet.
Definition Form.h:43
std::vector< T > pack_constants(const std::vector< std::reference_wrapper< const fem::Constant< T > > > &c)
Pack constants of an Expression or Form into a single array ready for assembly.
Definition pack.h:579
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
void pack_impl(std::span< T > coeffs, std::int32_t cell, int bs, std::span< const T > v, std::span< const std::uint32_t > cell_info, const DofMap &dofmap, auto transform)
Gather a single coefficient's degrees-of-freedom for a single cell and apply its DOF transformation.
Definition pack.h:78
void pack_coefficient_entity(std::span< T > c, int cstride, const Function< T, U > &u, std::span< const std::uint32_t > cell_info, auto cells, std::int32_t offset)
Pack a single coefficient for a set of active entities.
Definition pack.h:122