DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
Expression.h
1// Copyright (C) 2020-2026 Jack S. Hale, Michal Habera, Garth N. Wells and
2// Jørgen S. Dokken
3//
4// This file is part of DOLFINx (https://www.fenicsproject.org)
5//
6// SPDX-License-Identifier: LGPL-3.0-or-later
7
8#pragma once
9
10#include "Constant.h"
11#include "Function.h"
12#include <algorithm>
13#include <array>
14#include <concepts>
15#include <cstddef>
16#include <cstdint>
17#include <dolfinx/common/types.h>
18#include <dolfinx/mesh/EntityMap.h>
19#include <dolfinx/mesh/Mesh.h>
20#include <functional>
21#include <memory>
22#include <span>
23#include <utility>
24#include <vector>
25
26namespace dolfinx::fem
27{
28template <dolfinx::scalar T, std::floating_point U>
29class Function;
30
43template <dolfinx::scalar T, std::floating_point U = dolfinx::scalar_value_t<T>>
45{
46public:
51 using scalar_type = T;
52
54 using geometry_type = U;
55
79 const std::vector<std::shared_ptr<
81 const std::vector<std::shared_ptr<const Constant<scalar_type>>>&
83 std::span<const geometry_type> X, std::array<std::size_t, 2> Xshape,
84 std::function<void(scalar_type*, const scalar_type*, const scalar_type*,
85 const geometry_type*, const int*, const std::uint8_t*,
86 void*)>
87 fn,
88 const std::vector<std::size_t>& value_shape,
89 const std::vector<std::reference_wrapper<const dolfinx::mesh::EntityMap>>&
91
92 std::uint64_t coordinate_element_hash,
93 std::shared_ptr<const FunctionSpace<geometry_type>> argument_space
94 = nullptr)
95 : _argument_space(argument_space), _coefficients(coefficients),
96 _constants(constants), _fn(fn), _value_shape(value_shape),
97 _x_ref(std::vector<geometry_type>(X.begin(), X.end()), Xshape),
98 _entity_maps(entity_maps),
99 _coordinate_element_hash(coordinate_element_hash) {};
100
101 // Copy constructor (deleted)
102 Expression(const Expression& e) = delete;
103
105 Expression(Expression&& e) = default;
106
108 ~Expression() = default;
109
110 // Copy assignment (deleted)
111 Expression& operator=(const Expression& e) = delete;
112
115
118 std::shared_ptr<const FunctionSpace<geometry_type>> argument_space() const
119 {
120 return _argument_space;
121 };
122
125 const std::vector<
126 std::shared_ptr<const Function<scalar_type, geometry_type>>>&
128 {
129 return _coefficients;
130 }
131
135 const std::vector<std::shared_ptr<const Constant<scalar_type>>>&
136 constants() const
137 {
138 return _constants;
139 }
140
146 std::vector<int> coefficient_offsets() const
147 {
148 std::vector<int> n{0};
149 for (auto& c : _coefficients)
150 {
151 if (!c)
152 throw std::runtime_error("Not all form coefficients have been set.");
153 n.push_back(n.back() + c->function_space()->element()->space_dimension());
154 }
155 return n;
156 }
157
159 const std::function<void(scalar_type*, const scalar_type*, const scalar_type*,
160 const geometry_type*, const int*,
161 const std::uint8_t*, void*)>&
162 kernel() const
163 {
164 return _fn;
165 }
166
168 int value_size() const
169 {
170 return std::reduce(_value_shape.begin(), _value_shape.end(), 1,
171 std::multiplies{});
172 }
173
175 const std::vector<std::size_t>& value_shape() const { return _value_shape; }
176
180 const std::pair<std::vector<geometry_type>, std::array<std::size_t, 2>>&
181 X() const
182 {
183 return _x_ref;
184 }
185
187 std::size_t num_points() const { return _x_ref.second[0]; }
188
195 int entity_dim() const { return static_cast<int>(_x_ref.second[1]); }
196
198 const std::vector<std::reference_wrapper<const dolfinx::mesh::EntityMap>>&
200 {
201 return _entity_maps;
202 }
203
205 std::uint64_t coordinate_element_hash() const
206 {
207 return _coordinate_element_hash;
208 }
209
210private:
211 // Function space for Argument
212 std::shared_ptr<const FunctionSpace<geometry_type>> _argument_space;
213
214 // Coefficients associated with the Expression
215 std::vector<std::shared_ptr<const Function<scalar_type, geometry_type>>>
216 _coefficients;
217
218 // Constants associated with the Expression
219 std::vector<std::shared_ptr<const Constant<scalar_type>>> _constants;
220
221 // Function to evaluate the Expression
222 std::function<void(scalar_type*, const scalar_type*, const scalar_type*,
223 const geometry_type*, const int*, const std::uint8_t*,
224 void*)>
225 _fn;
226
227 // Shape of the evaluated Expression
228 std::vector<std::size_t> _value_shape;
229
230 // Evaluation points on reference entity
231 std::pair<std::vector<geometry_type>, std::array<std::size_t, 2>> _x_ref;
232
233 // Set of bidirectional maps of mesh entities (cells, facets, ridges or peaks)
234 // between meshes and submeshes of any codimension
235 std::vector<std::reference_wrapper<const dolfinx::mesh::EntityMap>>
236 _entity_maps;
237
238 // Hash for coordinate element used to create the expression
239 std::uint64_t _coordinate_element_hash;
240};
241} // namespace dolfinx::fem
Constant (in space) value which can be attached to a Form.
Definition Constant.h:22
An Expression represents a mathematical expression evaluated at a pre-defined points on a reference c...
Definition Expression.h:45
U geometry_type
Geometry type of the points.
Definition Expression.h:54
const std::vector< std::shared_ptr< const Function< scalar_type, geometry_type > > > & coefficients() const
Expression coefficients.
Definition Expression.h:127
std::shared_ptr< const FunctionSpace< geometry_type > > argument_space() const
Argument function space.
Definition Expression.h:118
Expression(const std::vector< std::shared_ptr< const Function< scalar_type, geometry_type > > > &coefficients, const std::vector< std::shared_ptr< const Constant< scalar_type > > > &constants, std::span< const geometry_type > X, std::array< std::size_t, 2 > Xshape, std::function< void(scalar_type *, const scalar_type *, const scalar_type *, const geometry_type *, const int *, const std::uint8_t *, void *)> fn, const std::vector< std::size_t > &value_shape, const std::vector< std::reference_wrapper< const dolfinx::mesh::EntityMap > > &entity_maps, std::uint64_t coordinate_element_hash, std::shared_ptr< const FunctionSpace< geometry_type > > argument_space=nullptr)
Create an Expression.
Definition Expression.h:78
std::uint64_t coordinate_element_hash() const
Hash for coordinate element used to create the expression.
Definition Expression.h:205
const std::vector< std::shared_ptr< const Constant< scalar_type > > > & constants() const
Expression constants.
Definition Expression.h:136
std::vector< int > coefficient_offsets() const
Offset for each coefficient expansion array on a cell.
Definition Expression.h:146
std::size_t num_points() const
Number of evaluation points (X).
Definition Expression.h:187
const std::pair< std::vector< geometry_type >, std::array< std::size_t, 2 > > & X() const
Evaluation point coordinates on the reference entity.
Definition Expression.h:181
int value_size() const
Value size of the Expression result.
Definition Expression.h:168
Expression & operator=(Expression &&e)=default
Move assignment.
Expression(Expression &&e)=default
Move constructor.
int entity_dim() const
Topological dimension of the entities the Expression is evaluated on.
Definition Expression.h:195
~Expression()=default
Destructor.
const std::function< void(scalar_type *, const scalar_type *, const scalar_type *, const geometry_type *, const int *, const std::uint8_t *, void *)> & kernel() const
Function for tabulating the Expression.
Definition Expression.h:162
T scalar_type
Scalar type.
Definition Expression.h:51
const std::vector< std::reference_wrapper< const dolfinx::mesh::EntityMap > > & entity_maps() const
Maps between entities of different meshes.
Definition Expression.h:199
const std::vector< std::size_t > & value_shape() const
Value shape of Expression result (at a point),.
Definition Expression.h:175
This class represents a finite element function space defined by a mesh, a finite element,...
Definition FunctionSpace.h:36
Definition Function.h:44
Finite element method functionality.
Definition assemble_expression_impl.h:28