DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
assemble_expression_impl.h
1// Copyright (C) 2025-2026 Garth N. Wells
2//
3// This file is part of DOLFINx (https://www.fenicsproject.org)
4//
5// SPDX-License-Identifier: LGPL-3.0-or-later
6
7#pragma once
8
9#include "FiniteElement.h"
10#include "traits.h"
11#include <algorithm>
12#include <basix/mdspan.hpp>
13#include <dolfinx/common/IndexMap.h>
14#include <dolfinx/mesh/Geometry.h>
15#include <dolfinx/mesh/Mesh.h>
16#include <dolfinx/mesh/Topology.h>
17#include <memory>
18#include <type_traits>
19#include <vector>
20
21namespace dolfinx::fem::impl
22{
61template <dolfinx::scalar T, std::floating_point U>
63 std::span<T> values, const fem::FEkernel<T, U> auto& fn,
64 std::array<std::size_t, 2> Xshape, std::size_t value_size,
65 std::size_t num_argument_dofs,
66 md::mdspan<const std::int32_t, md::dextents<std::size_t, 2>> x_dofmap,
67 std::span<const U> x,
68 md::mdspan<const T, md::dextents<std::size_t, 2>> coeffs,
69 std::span<const T> constants, fem::MDSpan2 auto entities,
70 std::span<const std::uint32_t> cell_info,
71 const fem::DofTransformKernel<T> auto& P0,
72 md::mdspan<const std::uint8_t, md::dextents<std::size_t, 2>> perms)
73{
74 static_assert(entities.rank() == 1 or entities.rank() == 2);
75
76 // Create data structures used in evaluation
77 std::vector<U> coord_dofs(3 * x_dofmap.extent(1));
79 x_dofmap,
80 md::mdspan<const U, md::extents<std::size_t, md::dynamic_extent, 3>>(
81 x.data(), x.size() / 3, 3)};
82
83 // Iterate over cells and 'assemble' into values
84 int size0 = Xshape[0] * value_size;
85 std::vector<T> values_local(size0 * num_argument_dofs, 0);
86 std::size_t offset = values_local.size();
87
88 const T* coeffs_data = coeffs.data_handle();
89 const std::size_t cstride = coeffs.extent(1);
90
91 for (std::size_t e = 0; e < entities.extent(0); ++e)
92 {
93 std::ranges::fill(values_local, T(0));
94 if constexpr (entities.rank() == 1)
95 {
96 std::int32_t entity = entities(e);
97 gather_cell_coordinates(geometry, entity, coord_dofs.data());
98 fn(values_local.data(), coeffs_data + e * cstride, constants.data(),
99 coord_dofs.data(), nullptr, nullptr, nullptr);
100
101 P0(values_local, cell_info, entity, size0);
102 }
103 else
104 {
105 std::int32_t entity = entities(e, 0);
106 std::int32_t local_entity = entities(e, 1);
107 std::uint8_t perm = perms.empty() ? 0 : perms(entity, local_entity);
108 gather_cell_coordinates(geometry, entity, coord_dofs.data());
109 fn(values_local.data(), coeffs_data + e * cstride, constants.data(),
110 coord_dofs.data(), &local_entity, &perm, nullptr);
111 P0(values_local, cell_info, entity, size0);
112 }
113
114 for (std::size_t j = 0; j < values_local.size(); ++j)
115 values[e * offset + j] = values_local[j];
116 }
117}
118
151template <dolfinx::scalar T, std::floating_point U>
153 std::span<T> values, const fem::FEkernel<T, U> auto& fn,
154 std::array<std::size_t, 2> Xshape, std::size_t value_size,
155 md::mdspan<const T, md::dextents<std::size_t, 2>> coeffs,
156 std::span<const T> constants, const mesh::Mesh<U>& mesh,
157 fem::MDSpan2 auto entities,
158 std::optional<
159 std::pair<std::reference_wrapper<const FiniteElement<U>>, std::size_t>>
160 element)
161{
162 std::function<void(std::span<T>, std::span<const std::uint32_t>, std::int32_t,
163 int)>
164 post_dof_transform
165 = [](std::span<T>, std::span<const std::uint32_t>, std::int32_t, int)
166 {
167 // Do nothing
168 };
169
170 std::shared_ptr<const mesh::Topology> topology = mesh.topology();
171 assert(topology);
172 std::size_t num_argument_dofs = 1;
173 std::span<const std::uint32_t> cell_info;
174 if (element)
175 {
176 num_argument_dofs = element->second;
177 if (element->first.get().needs_dof_transformations())
178 {
179 mesh.topology_mutable()->create_cell_permutations();
180 cell_info = std::span(topology->get_cell_permutation_info());
181 post_dof_transform
182 = element->first.get().template dof_transformation_right_fn<T>(
184 }
185 }
186
187 // An expression has no notion of requiring a facet permutation.
188 md::mdspan<const std::uint8_t, md::dextents<std::size_t, 2>> facet_perms;
189 if constexpr (std::remove_cvref_t<decltype(entities)>::rank() == 2)
190 {
191 const int facet_dim = mesh.topology()->dim() - 1;
192 mesh::CellType cell_type = mesh.topology()->cell_types()[0];
193 int num_facets_per_cell = mesh::cell_num_entities(cell_type, facet_dim);
194 mesh.topology_mutable()->create_entity_permutations(facet_dim);
195 const std::vector<std::uint8_t>& p
196 = mesh.topology()->get_entity_permutations(facet_dim);
197 facet_perms = md::mdspan(p.data(), p.size() / num_facets_per_cell,
198 num_facets_per_cell);
199 }
200 tabulate_expression(values, fn, Xshape, value_size, num_argument_dofs,
201 mesh.geometry().dofmaps().front(), mesh.geometry().x(),
202 coeffs, constants, entities, cell_info,
203 post_dof_transform, facet_perms);
204}
205} // namespace dolfinx::fem::impl
Model of a finite element.
Definition FiniteElement.h:199
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:25
DOF transform kernel concept.
Definition traits.h:22
Finite element cell kernel concept.
Definition traits.h:51
Concept for mdspan of rank 1 or 2.
Definition traits.h:57
void tabulate_expression(std::span< T > values, const fem::Expression< T, U > &e, md::mdspan< const T, md::dextents< std::size_t, 2 > > coeffs, std::span< const T > constants, const mesh::Mesh< U > &mesh, fem::MDSpan2 auto entities, std::optional< std::pair< std::reference_wrapper< const FiniteElement< U > >, std::size_t > > element)
Evaluate an Expression on cells or facets.
Definition expression_evaluate.h:69
@ transpose
Transpose.
Definition FiniteElement.h:32
Geometry data structures and algorithms.
Definition BoundingBoxTree.h:24
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
CellType
Cell type identifier.
Definition cell_types.h:24
int cell_num_entities(CellType type, int dim)
Number of entities of dimension.
Definition cell_types.cpp:97
Mesh geometry data passed to the assembly kernels.
Definition traits.h:102