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/function/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 function
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 function::Function<T>>>&
53  const std::vector<std::shared_ptr<const function::Constant<T>>>&
54  constants,
55  const std::shared_ptr<const mesh::Mesh>& mesh,
56  const Eigen::Ref<const Eigen::Array<double, Eigen::Dynamic,
57  Eigen::Dynamic, Eigen::RowMajor>>& x,
58  const std::function<void(T*, const T*, const T*, const double*)> fn,
59  const std::size_t value_size)
60  : _coefficients(coefficients), _constants(constants), _mesh(mesh), _x(x),
61  _fn(fn), _value_size(value_size)
62  {
63  // Do nothing
64  }
65 
67  Expression(Expression&& form) = default;
68 
70  virtual ~Expression() = default;
71 
73  const std::vector<std::shared_ptr<const function::Function<T>>>&
74  coefficients() const
75  {
76  return _coefficients;
77  }
78 
82  std::vector<int> coefficient_offsets() const
83  {
84  std::vector<int> n{0};
85  for (const auto& c : _coefficients)
86  {
87  if (!c)
88  throw std::runtime_error("Not all form coefficients have been set.");
89  n.push_back(n.back() + c->function_space()->element()->space_dimension());
90  }
91  return n;
92  }
93 
98  void
99  eval(const std::vector<std::int32_t>& active_cells,
100  Eigen::Ref<
101  Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>
102  values) const
103  {
104  function::eval(values, *this, active_cells);
105  }
106 
109  const std::function<void(T*, const T*, const T*, const double*)>&
111  {
112  return _fn;
113  }
114 
119  const std::vector<std::shared_ptr<const function::Constant<T>>>&
120  constants() const
121  {
122  return _constants;
123  }
124 
127  std::shared_ptr<const mesh::Mesh> mesh() const { return _mesh; }
128 
131  const Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>&
132  x() const
133  {
134  return _x;
135  }
136 
139  const std::size_t value_size() const { return _value_size; }
140 
143  const Eigen::Index num_points() const { return _x.rows(); }
144 
146  using scalar_type = T;
147 
148 private:
149  // Coefficients associated with the Expression
150  std::vector<std::shared_ptr<const function::Function<T>>> _coefficients;
151 
152  // Constants associated with the Expression
153  std::vector<std::shared_ptr<const function::Constant<T>>> _constants;
154 
155  // Function to evaluate the Expression
156  std::function<void(T*, const T*, const T*, const double*)> _fn;
157 
158  // Evaluation points on reference cell
159  Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> _x;
160 
161  // The mesh.
162  std::shared_ptr<const mesh::Mesh> _mesh;
163 
164  // Evaluation size
165  std::size_t _value_size;
166 };
167 } // namespace function
168 } // namespace dolfinx
dolfinx::function::Expression::Expression
Expression(Expression &&form)=default
Move constructor.
dolfinx::function::Expression::value_size
const std::size_t value_size() const
Get value size.
Definition: Expression.h:139
dolfinx::function::Expression::x
const Eigen::Array< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > & x() const
Get evaluation points on reference cell.
Definition: Expression.h:132
dolfinx::function::Expression::scalar_type
T scalar_type
Scalar type (T).
Definition: Expression.h:146
dolfinx::function::Expression::coefficient_offsets
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:82
dolfinx::function::eval
void eval(Eigen::Ref< Eigen::Array< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor >> values, const function::Expression< T > &e, const std::vector< std::int32_t > &active_cells)
Evaluate a UFC expression.
Definition: evaluate.h:26
dolfinx::function::Expression::get_tabulate_expression
const std::function< void(T *, const T *, const T *, const double *)> & get_tabulate_expression() const
Get function for tabulate_expression.
Definition: Expression.h:110
dolfinx::function::Expression::Expression
Expression(const std::vector< std::shared_ptr< const function::Function< T >>> &coefficients, const std::vector< std::shared_ptr< const function::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
dolfinx::function::Expression::mesh
std::shared_ptr< const mesh::Mesh > mesh() const
Get mesh.
Definition: Expression.h:127
dolfinx::function::Expression::coefficients
const std::vector< std::shared_ptr< const function::Function< T > > > & coefficients() const
Access coefficients.
Definition: Expression.h:74
dolfinx::function::Constant
A constant value which can be attached to a Form. Constants may be scalar (rank 0),...
Definition: Constant.h:19
dolfinx::function::Expression
Represents a mathematical expression evaluated at a pre-defined set of points on the reference cell....
Definition: Expression.h:39
dolfinx::function::Expression::constants
const std::vector< std::shared_ptr< const function::Constant< T > > > & constants() const
Access constants.
Definition: Expression.h:120
dolfinx::function::Function
This class represents a function in a finite element function space , given by.
Definition: Function.h:42
dolfinx::function::Expression::~Expression
virtual ~Expression()=default
Destructor.
dolfinx::function::Expression::num_points
const Eigen::Index num_points() const
Get number of points.
Definition: Expression.h:143
dolfinx::function::Expression::eval
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:99