DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
expression_evaluate.h
Go to the documentation of this file.
1// Copyright (C) 2018-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 "Expression.h"
10#include "FiniteElement.h"
11#include "Function.h"
12#include "FunctionSpace.h"
13#include "assemble_expression_impl.h"
14#include "interpolate.h"
15#include "pack.h"
16#include "traits.h"
17#include <algorithm>
18#include <basix/mdspan.hpp>
19#include <cassert>
20#include <cmath>
21#include <concepts>
22#include <cstddef>
23#include <cstdint>
24#include <dolfinx/common/types.h>
25#include <dolfinx/mesh/EntityMap.h>
26#include <dolfinx/mesh/Mesh.h>
27#include <dolfinx/mesh/Topology.h>
28#include <functional>
29#include <iterator>
30#include <memory>
31#include <optional>
32#include <ranges>
33#include <span>
34#include <stdexcept>
35#include <utility>
36#include <vector>
37
45
46namespace dolfinx::fem
47{
74template <dolfinx::scalar T, std::floating_point U>
76 std::span<T> values, const fem::Expression<T, U>& e,
77 md::mdspan<const T, md::dextents<std::size_t, 2>> coeffs,
78 std::span<const T> constants, const mesh::Mesh<U>& mesh,
79 fem::MDSpan2 auto entities,
80 std::optional<
81 std::pair<std::reference_wrapper<const FiniteElement<U>>, std::size_t>>
82 element)
83{
84 // Check that domain is the same as mesh of the expression
85 if (e.coordinate_element_hash() != mesh.geometry().cmaps().front().hash())
86 {
87 throw std::invalid_argument(
88 "Expression was created on a different mesh. Cannot tabulate.");
89 }
90
91 // Topology providing the argument's cell orientation data and, when
92 // the argument is on another mesh, its cell for each of `entities`.
93 // `topology_v` and `argument_cells` back the spans passed to
94 // impl::tabulate_expression.
95 std::shared_ptr<mesh::Topology> topology_v = mesh.topology_mutable();
96 std::vector<std::int32_t> argument_cells;
97 std::span<const std::uint32_t> cell_info;
98 if (element and element->first.get().needs_dof_transformations())
99 {
100 if (std::shared_ptr<const FunctionSpace<U>> V = e.argument_space();
101 V and V->mesh()->topology() != mesh.topology())
102 {
103 const mesh::Topology& topology = *mesh.topology();
104 topology_v = V->mesh()->topology_mutable();
105 const mesh::EntityMap& emap
106 = mesh::find_entity_map(e.entity_maps(), topology, *topology_v);
107 argument_cells = mesh::extract_cells_from_entities(
108 *topology_v, topology, entities, std::cref(emap));
109#ifndef NDEBUG
110 if (std::ranges::find(argument_cells, -1) != argument_cells.end())
111 {
112 throw std::invalid_argument(
113 "An entity has no cell in the mesh of the argument.");
114 }
115#endif
116 }
117 topology_v->create_cell_permutations();
118 cell_info = topology_v->get_cell_permutation_info();
119 }
120
121 auto [X, Xshape] = e.X();
122 impl::tabulate_expression(values, e.kernel(), Xshape, e.value_size(), coeffs,
123 constants, mesh, entities, element, cell_info,
124 argument_cells);
125}
126
143template <dolfinx::scalar T, std::floating_point U>
144void tabulate_expression(std::span<T> values, const fem::Expression<T, U>& e,
145 const mesh::Mesh<U>& mesh, fem::MDSpan2 auto entities)
146{
147 // Check that domain is the same as mesh of the expression
148 if (e.coordinate_element_hash() != mesh.geometry().cmaps().front().hash())
149 {
150 throw std::invalid_argument(
151 "Expression was created on a different mesh. Cannot tabulate.");
152 }
153
154 std::optional<
155 std::pair<std::reference_wrapper<const FiniteElement<U>>, std::size_t>>
156 element = std::nullopt;
157 if (auto V = e.argument_space(); V)
158 {
159 std::size_t num_argument_dofs
160 = V->dofmap()->element_dof_layout().num_dofs() * V->dofmap()->bs();
161 assert(V->element());
162 element = {std::cref(*V->element()), num_argument_dofs};
163 }
164
165 std::vector<int> coffsets = e.coefficient_offsets();
166 const std::vector<std::shared_ptr<const Function<T, U>>>& coefficients
167 = e.coefficients();
168 std::vector<T> coeffs(entities.extent(0) * coffsets.back());
169 int cstride = coffsets.back();
170 {
171 std::vector<std::reference_wrapper<const Function<T, U>>> c;
172 std::ranges::transform(coefficients, std::back_inserter(c),
173 [](auto c) -> const Function<T, U>& { return *c; });
174 fem::pack_coefficients(c, mesh, entities, e.entity_maps(), coffsets,
175 std::span(coeffs));
176 }
177 std::vector<T> constants = fem::pack_constants(e);
178
180 values, e,
181 md::mdspan(std::as_const(coeffs).data(), entities.extent(0), cstride),
182 std::span<const T>(constants), mesh, entities, element);
183}
184
208template <dolfinx::scalar T, std::floating_point U>
210 const Expression<T, U>& e0, mesh::CellRange auto&& cells0)
211{
212 // Extract mesh
213 const mesh::Mesh<U>* mesh0 = nullptr;
214 for (auto& c : e0.coefficients())
215 {
216 assert(c);
217 assert(c->function_space());
218 assert(c->function_space()->mesh());
219 if (auto mesh = c->function_space()->mesh().get(); !mesh0)
220 mesh0 = mesh;
221 else if (mesh != mesh0)
222 {
223 throw std::invalid_argument(
224 "Expression coefficient Functions have different meshes.");
225 }
226 }
227
228 // If Expression has no Function coefficients take mesh from `u1`.
229 auto V1 = u1.function_space();
230 assert(V1);
231 assert(V1->mesh());
232 if (!mesh0)
233 mesh0 = V1->mesh().get();
234
235 if (cells0.size() != cells1.size())
236 throw std::invalid_argument("Cell lists have different lengths.");
237
238 // Check that Function and Expression spaces are compatible
239 assert(V1->element());
240 std::size_t value_size = e0.value_size();
241 if (e0.argument_space())
242 throw std::invalid_argument("Cannot interpolate Expression with Argument.");
243
244 if (value_size != (std::size_t)V1->element()->value_size())
245 {
246 throw std::invalid_argument(
247 "Function value size not equal to Expression value size.");
248 }
249
250 // Compatibility check
251 {
252 auto [X0, shape0] = e0.X();
253 auto [X1, shape1] = V1->element()->interpolation_points();
254 if (shape0 != shape1)
255 {
256 throw std::invalid_argument(
257 "Function element interpolation points has different shape to "
258 "Expression interpolation points");
259 }
260
261 for (std::size_t i = 0; i < X0.size(); ++i)
262 {
263 if (std::abs(X0[i] - X1[i]) > 1.0e-10)
264 {
265 throw std::invalid_argument("Function element interpolation points not "
266 "equal to Expression interpolation points");
267 }
268 }
269 }
270
271 // Array to hold evaluated Expression
272 std::size_t num_cells = cells0.size();
273 std::size_t num_points = e0.X().second[0];
274 std::vector<T> fdata(num_cells * num_points * value_size);
275 md::mdspan<const T, md::dextents<std::size_t, 3>> f(fdata.data(), num_cells,
276 num_points, value_size);
277
278 // Evaluate Expression at points
279 std::vector<std::int32_t> _cells0(cells0.begin(), cells0.end());
280 tabulate_expression(std::span(fdata), e0, *mesh0,
281 md::mdspan(_cells0.data(), _cells0.size()));
282
283 // Reshape evaluated data to fit interpolate.
284 // Expression returns matrix of shape (num_cells, num_points *
285 // value_size), i.e. xyzxyz ordering of dof values per cell per
286 // point. The interpolation uses xxyyzz input, ordered for all
287 // points of each cell, i.e. (value_size, num_cells*num_points).
288 std::vector<T> fdata1(num_cells * num_points * value_size);
289 md::mdspan<T, md::dextents<std::size_t, 3>> f1(fdata1.data(), value_size,
290 num_cells, num_points);
291 for (std::size_t i = 0; i < f.extent(0); ++i)
292 for (std::size_t j = 0; j < f.extent(1); ++j)
293 for (std::size_t k = 0; k < f.extent(2); ++k)
294 f1(k, i, j) = f(i, j, k);
295
296 // Interpolate values into appropriate space
297 fem::interpolate<T>(u1, std::span<const T>(fdata1.data(), fdata1.size()),
298 {value_size, num_cells * num_points}, cells1);
299}
300
311template <dolfinx::scalar T, std::floating_point U>
313 mesh::CellRange auto&& cells)
314{
315 interpolate<T, U>(u1, cells, e0, cells);
316}
317
327template <dolfinx::scalar T, std::floating_point U>
329{
330 assert(u1.function_space());
331 assert(u1.function_space()->mesh());
332 int tdim = u1.function_space()->mesh()->topology()->dim();
333 auto map = u1.function_space()->mesh()->topology()->index_map(tdim);
334 assert(map);
336 u1, e0, std::ranges::iota_view(0, map->size_local() + map->num_ghosts()));
337}
338} // namespace dolfinx::fem
An Expression represents a mathematical expression evaluated at a pre-defined points on a reference c...
Definition Expression.h:43
std::pair< std::vector< geometry_type >, std::array< std::size_t, 2 > > X() const
Evaluation point coordinates on the reference cell.
Definition Expression.h:174
const std::vector< std::shared_ptr< const Function< scalar_type, geometry_type > > > & coefficients() const
Expression coefficients.
Definition Expression.h:123
std::shared_ptr< const FunctionSpace< geometry_type > > argument_space() const
Argument function space.
Definition Expression.h:114
std::uint64_t coordinate_element_hash() const
Hash for coordinate element used to create the expression.
Definition Expression.h:187
const std::function< void(scalar_type *, const scalar_type *, const scalar_type *, const geometry_type *, const int *, const uint8_t *, void *)> & kernel() const
Function for tabulating the Expression.
Definition Expression.h:158
std::vector< int > coefficient_offsets() const
Offset for each coefficient expansion array on a cell.
Definition Expression.h:142
int value_size() const
Value size of the Expression result.
Definition Expression.h:164
const std::vector< std::reference_wrapper< const dolfinx::mesh::EntityMap > > & entity_maps() const
Maps between entities of different meshes.
Definition Expression.h:181
Model of a finite element.
Definition FiniteElement.h:201
This class represents a finite element function space defined by a mesh, a finite element,...
Definition FunctionSpace.h:36
Definition Function.h:44
std::shared_ptr< const FunctionSpace< geometry_type > > function_space() const
Access the function space.
Definition Function.h:145
A bidirectional map relating entities in one topology to another.
Definition EntityMap.h:36
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:25
Topology stores the topology of a mesh, consisting of mesh entities and connectivity (incidence relat...
Definition Topology.h:49
Concept for mdspan of rank 1 or 2.
Definition traits.h:57
Requirement on range of cell indices.
Definition Topology.h:32
Finite element method functionality.
Definition assemble_expression_impl.h:28
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:75
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:267
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:459
void interpolate(Function< T, U > &u1, mesh::CellRange auto &&cells1, const Expression< T, U > &e0, mesh::CellRange auto &&cells0)
Interpolate an Expression into a Function over a subset of cells.
Definition expression_evaluate.h:209
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
const EntityMap & find_entity_map(std::span< const std::reference_wrapper< const EntityMap > > entity_maps, const Topology &topology0, const Topology &topology1)
Find the entity map relating two topologies.
Definition EntityMap.cpp:31
std::vector< std::int32_t > extract_cells_from_entities(const Topology &topology_c, const Topology &topology, E entities, std::optional< std::reference_wrapper< const EntityMap > > entity_map)
Map integration entities of a topology to the cells of a related topology.
Definition EntityMap.h:231
Functions supporting the packing of coefficient data.