DOLFIN-X
DOLFIN-X C++ interface
Expression.h
1 // Copyright (C) 2020 Jack S. Hale
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 <Eigen/Dense>
10 #include <dolfinx/fem/evaluate.h>
11 #include <functional>
12 #include <utility>
13 #include <vector>
14 
15 namespace dolfinx
16 {
17 
18 namespace mesh
19 {
20 class Mesh;
21 }
22 
23 namespace fem
24 {
25 template <typename T>
26 class Constant;
27 
36 
37 template <typename T>
39 {
40 public:
51  const std::vector<std::shared_ptr<const fem::Function<T>>>& coefficients,
52  const std::vector<std::shared_ptr<const fem::Constant<T>>>& constants,
53  const std::shared_ptr<const mesh::Mesh>& mesh,
54  const Eigen::Ref<const Eigen::Array<double, Eigen::Dynamic,
55  Eigen::Dynamic, Eigen::RowMajor>>& x,
56  const std::function<void(T*, const T*, const T*, const double*)> fn,
57  const std::size_t value_size)
58  : _coefficients(coefficients), _constants(constants), _mesh(mesh), _x(x),
59  _fn(fn), _value_size(value_size)
60  {
61  // Do nothing
62  }
63 
65  Expression(Expression&& form) = default;
66 
68  virtual ~Expression() = default;
69 
71  const std::vector<std::shared_ptr<const fem::Function<T>>>&
72  coefficients() const
73  {
74  return _coefficients;
75  }
76 
80  std::vector<int> coefficient_offsets() const
81  {
82  std::vector<int> n{0};
83  for (const auto& c : _coefficients)
84  {
85  if (!c)
86  throw std::runtime_error("Not all form coefficients have been set.");
87  n.push_back(n.back() + c->function_space()->element()->space_dimension());
88  }
89  return n;
90  }
91 
96  void
97  eval(const std::vector<std::int32_t>& active_cells,
98  Eigen::Ref<
99  Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>
100  values) const
101  {
102  fem::eval(values, *this, active_cells);
103  }
104 
107  const std::function<void(T*, const T*, const T*, const double*)>&
109  {
110  return _fn;
111  }
112 
117  const std::vector<std::shared_ptr<const fem::Constant<T>>>& constants() const
118  {
119  return _constants;
120  }
121 
124  std::shared_ptr<const mesh::Mesh> mesh() const { return _mesh; }
125 
128  const Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>&
129  x() const
130  {
131  return _x;
132  }
133 
136  const std::size_t value_size() const { return _value_size; }
137 
140  const Eigen::Index num_points() const { return _x.rows(); }
141 
143  using scalar_type = T;
144 
145 private:
146  // Coefficients associated with the Expression
147  std::vector<std::shared_ptr<const fem::Function<T>>> _coefficients;
148 
149  // Constants associated with the Expression
150  std::vector<std::shared_ptr<const fem::Constant<T>>> _constants;
151 
152  // Function to evaluate the Expression
153  std::function<void(T*, const T*, const T*, const double*)> _fn;
154 
155  // Evaluation points on reference cell
156  Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> _x;
157 
158  // The mesh.
159  std::shared_ptr<const mesh::Mesh> _mesh;
160 
161  // Evaluation size
162  std::size_t _value_size;
163 };
164 } // namespace fem
165 } // namespace dolfinx
A constant value which can be attached to a Form. Constants may be scalar (rank 0),...
Definition: Constant.h:19
Represents a mathematical expression evaluated at a pre-defined set of points on the reference cell....
Definition: Expression.h:39
const std::size_t value_size() const
Get value size.
Definition: Expression.h:136
const std::vector< std::shared_ptr< const fem::Constant< T > > > & constants() const
Access constants.
Definition: Expression.h:117
Expression(Expression &&form)=default
Move constructor.
const std::vector< std::shared_ptr< const fem::Function< T > > > & coefficients() const
Access coefficients.
Definition: Expression.h:72
std::shared_ptr< const mesh::Mesh > mesh() const
Get mesh.
Definition: Expression.h:124
Expression(const std::vector< std::shared_ptr< const fem::Function< T >>> &coefficients, const std::vector< std::shared_ptr< const fem::Constant< T >>> &constants, const std::shared_ptr< const mesh::Mesh > &mesh, const Eigen::Ref< const Eigen::Array< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor >> &x, const std::function< void(T *, const T *, const T *, const double *)> fn, const std::size_t value_size)
Create Expression.
Definition: Expression.h:50
const Eigen::Array< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > & x() const
Get evaluation points on reference cell.
Definition: Expression.h:129
void eval(const std::vector< std::int32_t > &active_cells, Eigen::Ref< Eigen::Array< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor >> values) const
Evaluate the expression on cells.
Definition: Expression.h:97
std::vector< int > coefficient_offsets() const
Offset for each coefficient expansion array on a cell. Used to pack data for multiple coefficients in...
Definition: Expression.h:80
virtual ~Expression()=default
Destructor.
const std::function< void(T *, const T *, const T *, const double *)> & get_tabulate_expression() const
Get function for tabulate_expression.
Definition: Expression.h:108
T scalar_type
Scalar type (T).
Definition: Expression.h:143
const Eigen::Index num_points() const
Get number of points.
Definition: Expression.h:140
This class represents a function in a finite element function space , given by.
Definition: Function.h:43
void eval(Eigen::Ref< Eigen::Array< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor >> values, const fem::Expression< T > &e, const std::vector< std::int32_t > &active_cells)
Evaluate a UFC expression.
Definition: evaluate.h:25