DOLFIN-X
DOLFIN-X C++ interface
Constant.h
1 // Copyright (C) 2019 Chris Richardson and Michal Habera
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 <vector>
11 
12 namespace dolfinx::function
13 {
14 
17 template <typename T>
18 class Constant
19 {
20 
21 public:
23  explicit Constant(T c) : value({c}) {}
24 
26  explicit Constant(const std::vector<T>& c) : shape(1, c.size()), value({c}) {}
27 
29  explicit Constant(
30  const Eigen::Ref<
31  Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>& c)
32  : shape({(int)c.rows(), (int)c.cols()}), value(c.rows() * c.cols())
33  {
34  for (int i = 0; i < c.rows(); ++i)
35  for (int j = 0; j < c.cols(); ++j)
36  value[i * c.cols() + j] = c(i, j);
37  }
38 
40  Constant(std::vector<int> shape, std::vector<T> value)
41  : shape(shape), value(value)
42  {
43  // Do nothing
44  }
45 
47  std::vector<int> shape;
48 
50  std::vector<T> value;
51 };
52 } // namespace dolfinx::function
dolfinx::function::Constant::shape
std::vector< int > shape
Shape.
Definition: Constant.h:47
dolfinx::function::Constant::Constant
Constant(T c)
Create a rank-0 (scalar-valued) constant.
Definition: Constant.h:23
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::Constant::Constant
Constant(std::vector< int > shape, std::vector< T > value)
Create an arbitrary rank constant. Data layout is row-major (C style).
Definition: Constant.h:40
dolfinx::function
Functions tools, including FEM functions and pointwise defined functions.
Definition: assembler.h:19
dolfinx::function::Constant::value
std::vector< T > value
Values, stored as a flattened array.
Definition: Constant.h:50
dolfinx::function::Constant::Constant
Constant(const std::vector< T > &c)
Create a rank-1 (vector-valued) constant.
Definition: Constant.h:26
dolfinx::function::Constant::Constant
Constant(const Eigen::Ref< Eigen::Array< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor >> &c)
Create a rank-2 constant.
Definition: Constant.h:29