DOLFIN-X
DOLFIN-X C++ interface
FormCoefficients.h
1 // Copyright (C) 2018 Chris Richardson
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 <algorithm>
10 #include <dolfinx/function/Function.h>
11 #include <memory>
12 #include <string>
13 #include <utility>
14 #include <vector>
15 
16 namespace dolfinx
17 {
18 
19 namespace function
20 {
21 template <typename T>
22 class Function;
23 }
24 
25 namespace fem
26 {
27 class FiniteElement;
28 
31 
32 template <typename T>
34 {
35 public:
40  const std::vector<
41  std::tuple<int, std::string, std::shared_ptr<function::Function<T>>>>&
42  coefficients)
43  {
44  for (const auto& c : coefficients)
45  {
46  _original_pos.push_back(std::get<0>(c));
47  _names.push_back(std::get<1>(c));
48  _coefficients.push_back(std::get<2>(c));
49  }
50  }
51 
53  int size() const { return _coefficients.size(); }
54 
58  std::vector<int> offsets() const
59  {
60  std::vector<int> n{0};
61  for (const auto& c : _coefficients)
62  {
63  if (!c)
64  throw std::runtime_error("Not all form coefficients have been set.");
65  n.push_back(n.back() + c->function_space()->element()->space_dimension());
66  }
67  return n;
68  }
69 
71  void set(int i,
72  const std::shared_ptr<const function::Function<T>>& coefficient)
73  {
74  if (i >= (int)_coefficients.size())
75  _coefficients.resize(i + 1);
76  _coefficients[i] = coefficient;
77  }
78 
80  void set(const std::string& name,
81  const std::shared_ptr<const function::Function<T>>& coefficient)
82  {
83  const int i = get_index(name);
84  if (i >= (int)_coefficients.size())
85  _coefficients.resize(i + 1);
86  _coefficients[i] = coefficient;
87  }
88 
90  std::shared_ptr<const function::Function<T>> get(int i) const
91  {
92  return _coefficients.at(i);
93  }
94 
98  int original_position(int i) const { return _original_pos.at(i); }
99 
103  int get_index(const std::string& name) const
104  {
105  auto it = std::find(_names.begin(), _names.end(), name);
106  if (it == _names.end())
107  throw std::runtime_error("Cannot find coefficient name:" + name);
108  return std::distance(_names.begin(), it);
109  }
110 
114  std::string get_name(int index) const
115  {
116  if (index >= (int)_names.size())
117  throw std::runtime_error("Invalid coefficient index");
118  return _names[index];
119  }
120 
121 private:
122  // Functions for the coefficients
123  std::vector<std::shared_ptr<const function::Function<T>>> _coefficients;
124 
125  // Copy of 'original positions' in UFL form
126  std::vector<int> _original_pos;
127 
128  // Names of coefficients
129  std::vector<std::string> _names;
130 };
131 } // namespace fem
132 } // namespace dolfinx
dolfinx::fem::FormCoefficients::offsets
std::vector< int > offsets() const
Offset for each coefficient expansion array on a cell. Used to pack data for multiple coefficients in...
Definition: FormCoefficients.h:58
dolfinx::fem::FormCoefficients::size
int size() const
Get number of coefficients.
Definition: FormCoefficients.h:53
dolfinx::fem::FormCoefficients::get_index
int get_index(const std::string &name) const
Get index from name of coefficient.
Definition: FormCoefficients.h:103
dolfinx::fem::FormCoefficients::original_position
int original_position(int i) const
Original position of coefficient in UFL form.
Definition: FormCoefficients.h:98
dolfinx::fem::FormCoefficients::set
void set(int i, const std::shared_ptr< const function::Function< T >> &coefficient)
Set coefficient with index i to be a Function.
Definition: FormCoefficients.h:71
dolfinx::fem::FormCoefficients::get
std::shared_ptr< const function::Function< T > > get(int i) const
Get the Function coefficient i.
Definition: FormCoefficients.h:90
dolfinx::function::Function
This class represents a function in a finite element function space , given by.
Definition: DirichletBC.h:22
dolfinx::fem::FormCoefficients::get_name
std::string get_name(int index) const
Get name from index of coefficient.
Definition: FormCoefficients.h:114
dolfinx::fem::FormCoefficients::FormCoefficients
FormCoefficients(const std::vector< std::tuple< int, std::string, std::shared_ptr< function::Function< T >>>> &coefficients)
Initialise the FormCoefficients, using tuples of (original_coeff_position, name, Function)....
Definition: FormCoefficients.h:39
dolfinx::fem::FormCoefficients::set
void set(const std::string &name, const std::shared_ptr< const function::Function< T >> &coefficient)
Set coefficient with name to be a Function.
Definition: FormCoefficients.h:80
dolfinx::fem::FormCoefficients
Storage for the coefficients of a Form consisting of Function and the Element objects they are define...
Definition: FormCoefficients.h:34