11#include "FiniteElement.h"
14#include "FunctionSpace.h"
18#include <basix/mdspan.hpp>
20#include <dolfinx/mesh/Topology.h>
33template <dolfinx::scalar T, std::
floating_po
int U>
45template <dolfinx::scalar T, std::
floating_po
int U>
46std::span<const std::uint32_t>
47get_cell_orientation_info(
const Function<T, U>& coefficient)
49 std::span<const std::uint32_t> cell_info;
50 auto element = coefficient.function_space()->element();
52 if (element->needs_dof_transformations())
54 auto mesh = coefficient.function_space()->mesh();
55 mesh->topology_mutable()->create_entity_permutations();
56 cell_info = std::span(mesh->topology()->get_cell_permutation_info());
81template <dolfinx::scalar T>
83 std::span<const T> v, std::span<const std::uint32_t> cell_info,
84 const DofMap& dofmap,
auto transform,
bool transform_set)
87 for (std::size_t i = 0; i < dofs.size(); ++i)
88 std::copy_n(v.data() + bs * dofs[i], bs, coeffs.data() + bs * i);
91 transform(coeffs, cell_info,
cell, 1);
110template <dolfinx::scalar T, std::
floating_po
int U>
113 std::span<const std::uint32_t> cell_info,
114 auto cells, std::int32_t offset)
116 static_assert(cells.rank() == 1);
119 std::span<const T> v = u.
x()->array();
123 int space_dim = element->space_dimension();
129 const int bs = dofmap.
bs();
140 auto pack_for_bs = [&cells, &c, &cstride, &offset, &space_dim, &v, &cell_info,
141 &dofmap, &transformation, transform_set](
auto bs)
143 for (std::size_t e = 0; e < cells.extent(0); ++e)
145 if (std::int32_t
cell = cells(e);
cell >= 0)
147 auto cell_coeff = c.subspan(e * cstride + offset, space_dim);
148 pack_impl(cell_coeff,
cell, bs, v, cell_info, dofmap, transformation,
157 pack_for_bs(std::integral_constant<int, 1>());
160 pack_for_bs(std::integral_constant<int, 2>());
163 pack_for_bs(std::integral_constant<int, 3>());
179template <dolfinx::scalar T, std::
floating_po
int U>
180std::pair<std::vector<T>,
int>
184 std::size_t num_entities = 0;
186 if (
const std::vector<std::shared_ptr<
const Function<T, U>>>& coefficients
188 !coefficients.empty())
191 cstride = offsets.back();
203 num_entities = form.
domain(integral_type, idx, 0).size();
208 return {std::vector<T>(num_entities * cstride), cstride};
216template <dolfinx::scalar T, std::
floating_po
int U>
217std::map<std::pair<IntegralType, int>, std::pair<std::vector<T>,
int>>
220 std::map<std::pair<IntegralType, int>, std::pair<std::vector<T>,
int>> coeffs;
227 for (
int idx = 0; idx < n; ++idx)
229 coeffs.emplace_hint(coeffs.end(), std::pair{type, idx},
258template <dolfinx::scalar T, std::
floating_po
int U>
260 std::map<std::pair<IntegralType, int>,
261 std::pair<std::vector<T>,
int>>& coeffs)
263 const std::vector<std::shared_ptr<const Function<T, U>>>& coefficients
267 for (
auto& [integral_key, coeff_data] : coeffs)
269 auto [integral_type, idx] = integral_key;
270 std::vector<T>& c = coeff_data.first;
271 int cstride = coeff_data.second;
272 if (!coefficients.empty())
274 switch (integral_type)
280 const int form_tdim = form.
mesh()->topology()->dim();
286 auto mesh = coefficients[coeff]->function_space()->mesh();
293 if (
int codim = form_tdim -
mesh->topology()->dim(); codim > 0)
295 throw std::invalid_argument(
296 "Should not be packing coefficients with "
297 "codim>0 in a cell integral");
300 std::span<const std::int32_t> cells_b
302 md::mdspan cells(cells_b.data(), cells_b.size());
303 std::span<const std::uint32_t> cell_info
304 = impl::get_cell_orientation_info(*coefficients[coeff]);
305 impl::pack_coefficient_entity(std::span(c), cstride,
306 *coefficients[coeff], cell_info, cells,
317 auto mesh = coefficients[coeff]->function_space()->mesh();
318 std::span<const std::int32_t> facets_b
320 md::mdspan<
const std::int32_t,
321 md::extents<std::size_t, md::dynamic_extent, 4>>
322 facets(facets_b.data(), facets_b.size() / 4, 4);
324 std::span<const std::uint32_t> cell_info
325 = impl::get_cell_orientation_info(*coefficients[coeff]);
336 auto cells0 = md::submdspan(facets, md::full_extent, 0);
337 impl::pack_coefficient_entity(std::span(c), 2 * cstride,
338 *coefficients[coeff], cell_info, cells0,
342 auto cells1 = md::submdspan(facets, md::full_extent, 2);
343 impl::pack_coefficient_entity(std::span(c), 2 * cstride,
344 *coefficients[coeff], cell_info, cells1,
345 offsets[coeff] + offsets[coeff + 1]);
359 auto mesh = coefficients[coeff]->function_space()->mesh();
362 std::span<const std::int32_t> entities_b
364 md::mdspan<
const std::int32_t,
365 md::extents<std::size_t, md::dynamic_extent, 2>>
366 entities(entities_b.data(), entities_b.size() / 2, 2);
367 std::span<const std::uint32_t> cell_info
368 = impl::get_cell_orientation_info(*coefficients[coeff]);
369 impl::pack_coefficient_entity(
370 std::span(c), cstride, *coefficients[coeff], cell_info,
371 md::submdspan(entities, md::full_extent, 0), offsets[coeff]);
376 throw std::invalid_argument(
377 "Could not pack coefficient. Integral type not supported.");
396template <dolfinx::scalar T, std::
floating_po
int U>
400 std::optional<std::reference_wrapper<const dolfinx::mesh::EntityMap>>
406 auto span_to_vector = [](
auto entities)
408 assert(entities.rank() == 1);
410 std::vector<std::int32_t> vec;
411 vec.reserve(entities.extent(0));
412 for (std::size_t i = 0; i < entities.extent(0); ++i)
413 vec.push_back(entities[i]);
417 if (mesh_c->topology() ==
mesh.topology())
420 if constexpr (entities.rank() == 1)
421 return span_to_vector(entities);
425 return span_to_vector(md::submdspan(entities, md::full_extent, 0));
429 assert(entity_map.has_value());
431 int tdim = topology.
dim();
432 int codim = tdim - mesh_c->topology()->dim();
437 if constexpr (entities.rank() == 1)
443 else if constexpr (entities.rank() == 2)
448 auto cells = md::submdspan(entities, md::full_extent, 0);
457 throw std::invalid_argument(
458 "Unsupported mapping. Can only map from submesh to parent mesh.");
461 auto c_to_e = topology.
connectivity(tdim, tdim - codim);
464 throw std::runtime_error(std::format(
465 "Topology connectivity from codim {} to {} not found.", tdim,
469 std::vector<std::int32_t> contiguous_cells;
470 contiguous_cells.reserve(entities.extent(0));
471 for (std::size_t e = 0; e < entities.extent(0); ++e)
473 contiguous_cells.push_back(
474 c_to_e->links(entities(e, 0))[entities(e, 1)]);
498template <dolfinx::scalar T, std::
floating_po
int U>
500 const std::vector<std::reference_wrapper<
const Function<T, U>>>& coeffs,
502 const std::vector<std::reference_wrapper<const dolfinx::mesh::EntityMap>>&
504 std::span<const int> offsets, std::span<T> c)
507 assert(!offsets.empty());
508 const int cstride = offsets.back();
510 if (c.size() < entities.extent(0) * offsets.back())
511 throw std::runtime_error(
"Coefficient packing span is too small.");
520 auto it = std::ranges::find_if(
524 return (em.
topology() == mesh0->topology()
530 if (it == entity_maps.end())
532 throw std::invalid_argument(
533 "Incompatible mesh. argument entity_maps must be provided.");
539 for (std::size_t coeff = 0; coeff < coeffs.size(); ++coeff)
542 auto mesh_c = coeffs[coeff].get().function_space()->mesh();
543 std::vector<std::int32_t> coefficient_cells;
544 if (mesh_c->topology() ==
mesh.topology())
547 coeffs[coeff].get(),
mesh, entities, std::nullopt);
554 coeffs[coeff].get(),
mesh, entities,
555 std::reference_wrapper<const mesh::EntityMap>(emap));
558 std::span<const std::uint32_t> cell_info
559 = impl::get_cell_orientation_info(coeffs[coeff].get());
560 md::mdspan cells(coefficient_cells.data(), coefficient_cells.size());
561 impl::pack_coefficient_entity(std::span(c), cstride, coeffs[coeff].get(),
562 cell_info, cells, offsets[coeff]);
577 std::int32_t size = std::accumulate(
578 c.cbegin(), c.cend(), 0, [](std::int32_t sum,
auto& constant)
579 { return sum + constant.get().value.size(); });
582 std::vector<T> constant_values(size);
583 std::int32_t offset = 0;
584 for (
auto& constant : c)
586 std::ranges::copy(constant.get().value,
587 std::next(constant_values.begin(), offset));
588 offset += constant.get().value.size();
591 return constant_values;
599 requires std::convertible_to<
601 typename std::decay_t<U>::geometry_type>>
602 or std::convertible_to<
604 typename std::decay_t<U>::geometry_type>>
607 using T =
typename std::decay_t<U>::scalar_type;
608 std::vector<std::reference_wrapper<const Constant<T>>> c;
609 c.reserve(u.constants().size());
610 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:165
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:149
std::shared_ptr< const la::Vector< value_type > > x() const
Underlying vector (const version).
Definition Function.h:155
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:52
Finite element method functionality.
Definition assemble_expression_impl.h:24
@ transpose
Transpose.
Definition FiniteElement.h:30
@ inverse
Inverse.
Definition FiniteElement.h:31
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:259
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:181
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:397
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
constexpr bool is_transform_set(const F &fn)
Whether a DofTransformKernel fn should be invoked.
Definition traits.h:33
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:573
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
void pack_impl(std::span< T > coeffs, std::int32_t cell, auto bs, std::span< const T > v, std::span< const std::uint32_t > cell_info, const DofMap &dofmap, auto transform, bool transform_set)
Gather a single coefficient's degrees-of-freedom for a single cell and apply its DOF transformation.
Definition pack.h:82
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:111