DOLFINx 0.11.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 <dolfinx/common/types.h>
16#include <dolfinx/mesh/EntityMap.h>
17#include <dolfinx/mesh/Mesh.h>
18#include <functional>
19#include <memory>
20#include <span>
21#include <utility>
22#include <vector>
23
24namespace dolfinx::fem
25{
26template <dolfinx::scalar T, std::floating_point U>
27class Function;
28
41template <dolfinx::scalar T, std::floating_point U = dolfinx::scalar_value_t<T>>
43{
44public:
49 using scalar_type = T;
50
52 using geometry_type = U;
53
75 const std::vector<std::shared_ptr<
77 const std::vector<std::shared_ptr<const Constant<scalar_type>>>&
79 std::span<const geometry_type> X, std::array<std::size_t, 2> Xshape,
80 std::function<void(scalar_type*, const scalar_type*, const scalar_type*,
81 const geometry_type*, const int*, const uint8_t*,
82 void*)>
83 fn,
84 const std::vector<std::size_t>& value_shape,
85 const std::vector<std::reference_wrapper<const dolfinx::mesh::EntityMap>>&
87
88 std::uint64_t coordinate_element_hash,
89 std::shared_ptr<const FunctionSpace<geometry_type>> argument_space
90 = nullptr)
91 : _argument_space(argument_space), _coefficients(coefficients),
92 _constants(constants), _fn(fn), _value_shape(value_shape),
93 _x_ref(std::vector<geometry_type>(X.begin(), X.end()), Xshape),
94 _entity_maps(entity_maps),
95 _coordinate_element_hash(coordinate_element_hash) {};
96
98 Expression(Expression&& e) = default;
99
101 virtual ~Expression() = default;
102
105 std::shared_ptr<const FunctionSpace<geometry_type>> argument_space() const
106 {
107 return _argument_space;
108 };
109
112 const std::vector<
113 std::shared_ptr<const Function<scalar_type, geometry_type>>>&
115 {
116 return _coefficients;
117 }
118
122 const std::vector<std::shared_ptr<const Constant<scalar_type>>>&
123 constants() const
124 {
125 return _constants;
126 }
127
133 std::vector<int> coefficient_offsets() const
134 {
135 std::vector<int> n{0};
136 for (auto& c : _coefficients)
137 {
138 if (!c)
139 throw std::runtime_error("Not all form coefficients have been set.");
140 n.push_back(n.back() + c->function_space()->element()->space_dimension());
141 }
142 return n;
143 }
144
146 const std::function<void(scalar_type*, const scalar_type*, const scalar_type*,
147 const geometry_type*, const int*, const uint8_t*,
148 void*)>&
149 kernel() const
150 {
151 return _fn;
152 }
153
155 int value_size() const
156 {
157 return std::reduce(_value_shape.begin(), _value_shape.end(), 1,
158 std::multiplies{});
159 }
160
162 const std::vector<std::size_t>& value_shape() const { return _value_shape; }
163
165 std::pair<std::vector<geometry_type>, std::array<std::size_t, 2>> X() const
166 {
167 return _x_ref;
168 }
169
171 const std::vector<std::reference_wrapper<const dolfinx::mesh::EntityMap>>&
173 {
174 return _entity_maps;
175 }
176
178 std::uint64_t coordinate_element_hash() const
179 {
180 return _coordinate_element_hash;
181 }
182
183private:
184 // Function space for Argument
185 std::shared_ptr<const FunctionSpace<geometry_type>> _argument_space;
186
187 // Coefficients associated with the Expression
188 std::vector<std::shared_ptr<const Function<scalar_type, geometry_type>>>
189 _coefficients;
190
191 // Constants associated with the Expression
192 std::vector<std::shared_ptr<const Constant<scalar_type>>> _constants;
193
194 // Function to evaluate the Expression
195 std::function<void(scalar_type*, const scalar_type*, const scalar_type*,
196 const geometry_type*, const int*, const uint8_t*, void*)>
197 _fn;
198
199 // Shape of the evaluated Expression
200 std::vector<std::size_t> _value_shape;
201
202 // Evaluation points on reference cell
203 std::pair<std::vector<geometry_type>, std::array<std::size_t, 2>> _x_ref;
204
205 // Set of bidirectional maps of mesh entities (cells, facets, ridges or peaks)
206 // between meshes and submeshes of any codimension
207 std::vector<std::reference_wrapper<const dolfinx::mesh::EntityMap>>
208 _entity_maps;
209
210 // Hash for coordinate element used to create the expression
211 std::uint64_t _coordinate_element_hash;
212};
213} // namespace dolfinx::fem
Constant (in space) value which can be attached to a Form.
Definition Constant.h:22
U geometry_type
Geometry type of the points.
Definition Expression.h:52
std::pair< std::vector< geometry_type >, std::array< std::size_t, 2 > > X() const
Evaluation point coordinates on the reference cell.
Definition Expression.h:165
const std::vector< std::shared_ptr< const Function< scalar_type, geometry_type > > > & coefficients() const
Expression coefficients.
Definition Expression.h:114
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 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:74
std::shared_ptr< const FunctionSpace< geometry_type > > argument_space() const
Argument function space.
Definition Expression.h:105
std::uint64_t coordinate_element_hash() const
Hash for coordinate element used to create the expression.
Definition Expression.h:178
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:149
const std::vector< std::shared_ptr< const Constant< scalar_type > > > & constants() const
Expression constants.
Definition Expression.h:123
std::vector< int > coefficient_offsets() const
Offset for each coefficient expansion array on a cell.
Definition Expression.h:133
int value_size() const
Value size of the Expression result.
Definition Expression.h:155
Expression(Expression &&e)=default
Move constructor.
virtual ~Expression()=default
Destructor.
T scalar_type
Scalar type.
Definition Expression.h:49
const std::vector< std::reference_wrapper< const dolfinx::mesh::EntityMap > > & entity_maps() const
Maps between entities of different meshes.
Definition Expression.h:172
const std::vector< std::size_t > & value_shape() const
Value shape of of Expression result (at a point),.
Definition Expression.h:162
This class represents a finite element function space defined by a mesh, a finite element,...
Definition FunctionSpace.h:34
Definition Function.h:47
Finite element method functionality.
Definition assemble_expression_impl.h:23