DOLFINx 0.10.0.0
DOLFINx C++ interface
Loading...
Searching...
No Matches
Expression.h
1// Copyright (C) 2020-2025 Jack S. Hale, Michal Habera and 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 "Constant.h"
10#include "Function.h"
11#include <algorithm>
12#include <array>
13#include <concepts>
14#include <dolfinx/common/types.h>
15#include <dolfinx/mesh/Mesh.h>
16#include <functional>
17#include <memory>
18#include <span>
19#include <utility>
20#include <vector>
21
22namespace dolfinx::fem
23{
24template <dolfinx::scalar T, std::floating_point U>
25class Function;
26
39template <dolfinx::scalar T, std::floating_point U = dolfinx::scalar_value_t<T>>
41{
42public:
47 using scalar_type = T;
48
50 using geometry_type = U;
51
69 Expression(const std::vector<std::shared_ptr<
71 const std::vector<std::shared_ptr<const Constant<scalar_type>>>&
73 std::span<const geometry_type> X,
74 std::array<std::size_t, 2> Xshape,
75 std::function<void(scalar_type*, const scalar_type*,
76 const scalar_type*, const geometry_type*,
77 const int*, const uint8_t*, void*)>
78 fn,
79 const std::vector<std::size_t>& value_shape,
80 std::shared_ptr<const FunctionSpace<geometry_type>> argument_space
81 = nullptr)
82 : _argument_space(argument_space), _coefficients(coefficients),
83 _constants(constants), _fn(fn), _value_shape(value_shape),
84 _x_ref(std::vector<geometry_type>(X.begin(), X.end()), Xshape)
85
86 {
87 for (auto& c : _coefficients)
88 {
89 assert(c);
90 if (c->function_space()->mesh()
91 != _coefficients.front()->function_space()->mesh())
92 {
93 throw std::runtime_error("Coefficients not all defined on same mesh.");
94 }
95 }
96 }
97
99 Expression(Expression&& e) = default;
100
102 virtual ~Expression() = default;
103
106 std::shared_ptr<const FunctionSpace<geometry_type>> argument_space() const
107 {
108 return _argument_space;
109 };
110
113 const std::vector<
114 std::shared_ptr<const Function<scalar_type, geometry_type>>>&
116 {
117 return _coefficients;
118 }
119
123 const std::vector<std::shared_ptr<const Constant<scalar_type>>>&
124 constants() const
125 {
126 return _constants;
127 }
128
134 std::vector<int> coefficient_offsets() const
135 {
136 std::vector<int> n{0};
137 for (auto& c : _coefficients)
138 {
139 if (!c)
140 throw std::runtime_error("Not all form coefficients have been set.");
141 n.push_back(n.back() + c->function_space()->element()->space_dimension());
142 }
143 return n;
144 }
145
147 const std::function<void(scalar_type*, const scalar_type*, const scalar_type*,
148 const geometry_type*, const int*, const uint8_t*,
149 void*)>&
150 kernel() const
151 {
152 return _fn;
153 }
154
156 int value_size() const
157 {
158 return std::reduce(_value_shape.begin(), _value_shape.end(), 1,
159 std::multiplies{});
160 }
161
163 const std::vector<std::size_t>& value_shape() const { return _value_shape; }
164
166 std::pair<std::vector<geometry_type>, std::array<std::size_t, 2>> X() const
167 {
168 return _x_ref;
169 }
170
171private:
172 // Function space for Argument
173 std::shared_ptr<const FunctionSpace<geometry_type>> _argument_space;
174
175 // Coefficients associated with the Expression
176 std::vector<std::shared_ptr<const Function<scalar_type, geometry_type>>>
177 _coefficients;
178
179 // Constants associated with the Expression
180 std::vector<std::shared_ptr<const Constant<scalar_type>>> _constants;
181
182 // Function to evaluate the Expression
183 std::function<void(scalar_type*, const scalar_type*, const scalar_type*,
184 const geometry_type*, const int*, const uint8_t*, void*)>
185 _fn;
186
187 // Shape of the evaluated Expression
188 std::vector<std::size_t> _value_shape;
189
190 // Evaluation points on reference cell
191 std::pair<std::vector<geometry_type>, std::array<std::size_t, 2>> _x_ref;
192};
193} // namespace dolfinx::fem
Constant value which can be attached to a Form.
Definition Constant.h:23
U geometry_type
Geometry type of the points.
Definition Expression.h:50
std::pair< std::vector< geometry_type >, std::array< std::size_t, 2 > > X() const
Evaluation point coordinates on the reference cell.
Definition Expression.h:166
const std::vector< std::shared_ptr< const Function< scalar_type, geometry_type > > > & coefficients() const
Expression coefficients.
Definition Expression.h:115
std::shared_ptr< const FunctionSpace< geometry_type > > argument_space() const
Argument function space.
Definition Expression.h:106
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:150
const std::vector< std::shared_ptr< const Constant< scalar_type > > > & constants() const
Expression constants.
Definition Expression.h:124
std::vector< int > coefficient_offsets() const
Offset for each coefficient expansion array on a cell.
Definition Expression.h:134
int value_size() const
Value size of the Expression result.
Definition Expression.h:156
Expression(Expression &&e)=default
Move constructor.
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, std::shared_ptr< const FunctionSpace< geometry_type > > argument_space=nullptr)
Create an Expression.
Definition Expression.h:69
virtual ~Expression()=default
Destructor.
T scalar_type
Scalar type.
Definition Expression.h:47
const std::vector< std::size_t > & value_shape() const
Value shape of of Expression result (at a point),.
Definition Expression.h:163
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