DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
assemble_expression_impl.h
1// Copyright (C) 2025-2026 Garth N. Wells and Jørgen S. Dokken
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 <cassert>
14#include <cstdint>
15#include <dolfinx/common/IndexMap.h>
16#include <dolfinx/mesh/Geometry.h>
17#include <dolfinx/mesh/Mesh.h>
18#include <dolfinx/mesh/Topology.h>
19#include <functional>
20#include <memory>
21#include <optional>
22#include <span>
23#include <type_traits>
24#include <utility>
25#include <vector>
26
27namespace dolfinx::fem::impl
28{
71template <dolfinx::scalar T, std::floating_point U>
73 std::span<T> values, const fem::FEkernel<T, U> auto& fn,
74 std::array<std::size_t, 2> Xshape, std::size_t value_size,
75 std::size_t num_argument_dofs,
76 md::mdspan<const std::int32_t, md::dextents<std::size_t, 2>> x_dofmap,
77 std::span<const U> x,
78 md::mdspan<const T, md::dextents<std::size_t, 2>> coeffs,
79 std::span<const T> constants, fem::MDSpan2 auto entities,
80 std::span<const std::uint32_t> cell_info,
81 std::span<const std::int32_t> argument_cells,
82 const fem::DofTransformKernel<T> auto& P0,
83 md::mdspan<const std::uint8_t, md::dextents<std::size_t, 2>> perms)
84{
85 static_assert(entities.rank() == 1 or entities.rank() == 2);
86
87 // Create data structures used in evaluation
88 std::vector<U> coord_dofs(3 * x_dofmap.extent(1));
90 x_dofmap,
91 md::mdspan<const U, md::extents<std::size_t, md::dynamic_extent, 3>>(
92 x.data(), x.size() / 3, 3)};
93
94 // Iterate over cells and 'assemble' into values
95 int size0 = Xshape[0] * value_size;
96 std::vector<T> values_local(size0 * num_argument_dofs, 0);
97 std::size_t offset = values_local.size();
98
99 const T* coeffs_data = coeffs.data_handle();
100 const std::size_t cstride = coeffs.extent(1);
101
102 for (std::size_t e = 0; e < entities.extent(0); ++e)
103 {
104 std::ranges::fill(values_local, T(0));
105 if constexpr (entities.rank() == 1)
106 {
107 std::int32_t entity = entities(e);
108 gather_cell_coordinates(geometry, entity, coord_dofs.data());
109 fn(values_local.data(), coeffs_data + e * cstride, constants.data(),
110 coord_dofs.data(), nullptr, nullptr, nullptr);
111
112 P0(values_local, cell_info,
113 argument_cells.empty() ? entity : argument_cells[e], size0);
114 }
115 else
116 {
117 std::int32_t entity = entities(e, 0);
118 std::int32_t local_entity = entities(e, 1);
119 std::uint8_t perm = perms.empty() ? 0 : perms(entity, local_entity);
120 gather_cell_coordinates(geometry, entity, coord_dofs.data());
121 fn(values_local.data(), coeffs_data + e * cstride, constants.data(),
122 coord_dofs.data(), &local_entity, &perm, nullptr);
123 P0(values_local, cell_info,
124 argument_cells.empty() ? entity : argument_cells[e], size0);
125 }
126
127 for (std::size_t j = 0; j < values_local.size(); ++j)
128 values[e * offset + j] = values_local[j];
129 }
130}
131
170template <dolfinx::scalar T, std::floating_point U>
172 std::span<T> values, const fem::FEkernel<T, U> auto& fn,
173 std::array<std::size_t, 2> Xshape, std::size_t value_size,
174 md::mdspan<const T, md::dextents<std::size_t, 2>> coeffs,
175 std::span<const T> constants, const mesh::Mesh<U>& mesh,
176 fem::MDSpan2 auto entities,
177 std::optional<
178 std::pair<std::reference_wrapper<const FiniteElement<U>>, std::size_t>>
179 element,
180 std::span<const std::uint32_t> cell_info,
181 std::span<const std::int32_t> argument_cells)
182{
183 assert(argument_cells.empty() or argument_cells.size() == entities.extent(0));
184
185 std::function<void(std::span<T>, std::span<const std::uint32_t>, std::int32_t,
186 int)>
187 post_dof_transform
188 = [](std::span<T>, std::span<const std::uint32_t>, std::int32_t, int)
189 {
190 // Do nothing
191 };
192
193 std::size_t num_argument_dofs = 1;
194 if (element)
195 {
196 num_argument_dofs = element->second;
197 if (element->first.get().needs_dof_transformations())
198 {
199 post_dof_transform
200 = element->first.get().template dof_transformation_right_fn<T>(
202 }
203 }
204
205 // The kernel is passed the permutation of the entity it is evaluated
206 // on, so that the evaluation points are ordered consistently with a
207 // neighbouring cell's. The points are given on the reference cell of
208 // the entity, so `Xshape[1]` is the entity dimension. The mesh has a
209 // single cell type, see fem::impl::check_expression_entities.
210 md::mdspan<const std::uint8_t, md::dextents<std::size_t, 2>> perms;
211 if constexpr (std::remove_cvref_t<decltype(entities)>::rank() == 2)
212 {
213 perms = mesh::entity_permutations(*mesh.topology_mutable(), Xshape[1], 0);
214 }
215
216 tabulate_expression(values, fn, Xshape, value_size, num_argument_dofs,
217 mesh.geometry().dofmaps().front(), mesh.geometry().x(),
218 coeffs, constants, entities, cell_info, argument_cells,
219 post_dof_transform, perms);
220}
221} // namespace dolfinx::fem::impl
Model of a finite element.
Definition FiniteElement.h:202
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, facets, ridges or peaks.
Definition expression_evaluate.h:169
@ transpose
Transpose.
Definition FiniteElement.h:35
Geometry data structures and algorithms.
Definition BoundingBoxTree.h:26
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
md::mdspan< const std::uint8_t, md::dextents< std::size_t, 2 > > entity_permutations(Topology &topology, int dim, int cell_type_idx)
Permutations of the cell-local entities of a given dimension.
Definition Topology.cpp:1902
Mesh geometry data passed to the assembly kernels.
Definition traits.h:102