Note: this is documentation for an old release. View the latest documentation at docs.fenicsproject.org/v0.1.0/v0.9.0/cpp
DOLFINx  0.1.0
DOLFINx 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 <dolfinx/common/array2d.h>
10 #include <vector>
11 
12 namespace dolfinx::fem
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(const array2d<T>& c)
30  : shape({(int)c.rows(), (int)c.cols()}), value(c.rows() * c.cols())
31  {
32  for (int i = 0; i < c.rows(); ++i)
33  for (int j = 0; j < c.cols(); ++j)
34  value[i * c.cols() + j] = c(i, j);
35  }
36 
38  Constant(std::vector<int> shape, std::vector<T> value)
39  : shape(shape), value(value)
40  {
41  // Do nothing
42  }
43 
45  std::vector<int> shape;
46 
48  std::vector<T> value;
49 };
50 } // namespace dolfinx::fem
dolfinx::fem::Constant::Constant
Constant(T c)
Create a rank-0 (scalar-valued) constant.
Definition: Constant.h:23
dolfinx::fem::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:38
dolfinx::fem::Constant::Constant
Constant(const array2d< T > &c)
Create a rank-2 constant.
Definition: Constant.h:29
dolfinx::fem::Constant::Constant
Constant(const std::vector< T > &c)
Create a rank-1 (vector-valued) constant.
Definition: Constant.h:26
dolfinx::fem::Constant::value
std::vector< T > value
Values, stored as a flattened array.
Definition: Constant.h:48
dolfinx::fem
Finite element method functionality.
Definition: assemble_matrix_impl.h:22
dolfinx::array2d
This class provides a dynamic 2-dimensional row-wise array data structure.
Definition: array2d.h:20
dolfinx::fem::Constant
A constant value which can be attached to a Form. Constants may be scalar (rank 0),...
Definition: Constant.h:18
dolfinx::fem::Constant::shape
std::vector< int > shape
Shape.
Definition: Constant.h:45