libMesh
composite_fem_function.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 #ifndef LIBMESH_COMPOSITE_FEM_FUNCTION_H
19 #define LIBMESH_COMPOSITE_FEM_FUNCTION_H
20 
21 // libMesh includes
22 #include "libmesh/dense_vector.h"
23 #include "libmesh/fem_function_base.h"
24 #include "libmesh/int_range.h"
25 #include "libmesh/libmesh.h"
26 #include "libmesh/point.h"
27 
28 // C++ includes
29 #include <algorithm>
30 #include <utility>
31 #include <vector>
32 
33 namespace libMesh
34 {
35 
43 template <typename Output=Number>
44 class CompositeFEMFunction : public FEMFunctionBase<Output>
45 {
46 public:
47  explicit
48  CompositeFEMFunction () = default;
49 
55 
60  CompositeFEMFunction (const CompositeFEMFunction &) = delete;
62 
66  virtual ~CompositeFEMFunction () = default;
67 
74  const std::vector<unsigned int> & index_map)
75  {
76  const unsigned int subfunction_index =
77  cast_int<unsigned int>(subfunctions.size());
78  libmesh_assert_equal_to(subfunctions.size(), index_maps.size());
79 
80  subfunctions.push_back(f.clone());
81  index_maps.push_back(index_map);
82 
83  unsigned int max_index =
84  *std::max_element(index_map.begin(), index_map.end());
85 
86  if (max_index >= reverse_index_map.size())
87  reverse_index_map.resize
88  (max_index+1, std::make_pair(libMesh::invalid_uint,
90 
91  for (auto j : index_range(index_map))
92  {
93  libmesh_assert_less(index_map[j], reverse_index_map.size());
94  libmesh_assert_equal_to(reverse_index_map[index_map[j]].first,
96  libmesh_assert_equal_to(reverse_index_map[index_map[j]].second,
98  reverse_index_map[index_map[j]] =
99  std::make_pair(subfunction_index, j);
100  }
101  }
102 
103  virtual Output operator() (const FEMContext & c,
104  const Point & p,
105  const Real time = 0) override
106  {
107  return this->component(c,0,p,time);
108  }
109 
110  virtual void operator() (const FEMContext & c,
111  const Point & p,
112  const Real time,
113  DenseVector<Output> & output) override
114  {
115  libmesh_assert_greater_equal (output.size(),
116  reverse_index_map.size());
117 
118  // Necessary in case we have output components not covered by
119  // any subfunctions
120  output.zero();
121 
122  DenseVector<Output> temp;
123  for (auto i : index_range(subfunctions))
124  {
125  temp.resize(cast_int<unsigned int>(index_maps[i].size()));
126  (*subfunctions[i])(c, p, time, temp);
127  for (auto j : index_range(temp))
128  output(index_maps[i][j]) = temp(j);
129  }
130  }
131 
132  virtual Output component (const FEMContext & c,
133  unsigned int i,
134  const Point & p,
135  Real time) override
136  {
137  if (i >= reverse_index_map.size() ||
139  return 0;
140 
141  libmesh_assert_less(reverse_index_map[i].first,
142  subfunctions.size());
143  libmesh_assert_not_equal_to(reverse_index_map[i].second,
145  return subfunctions[reverse_index_map[i].first]->
146  component(c, reverse_index_map[i].second, p, time);
147  }
148 
149  virtual std::unique_ptr<FEMFunctionBase<Output>> clone() const override
150  {
151  CompositeFEMFunction * returnval = new CompositeFEMFunction();
152  for (auto i : index_range(subfunctions))
153  returnval->attach_subfunction(*subfunctions[i], index_maps[i]);
154  return std::unique_ptr<FEMFunctionBase<Output>> (returnval);
155  }
156 
157  unsigned int n_subfunctions () const
158  {
159  return subfunctions.size();
160  }
161 
162  unsigned int n_components () const
163  {
164  return reverse_index_map.size();
165  }
166 
167 private:
168  // list of functions which fill in our values
169  std::vector<std::unique_ptr<FEMFunctionBase<Output>>> subfunctions;
170 
171  // for each function, list of which global indices it fills in
172  std::vector<std::vector<unsigned int>> index_maps;
173 
174  // for each global index, which local index of which function is it?
175  std::vector<std::pair<unsigned int, unsigned int>> reverse_index_map;
176 };
177 
178 
179 } // namespace libMesh
180 
181 #endif // LIBMESH_COMPOSITE_FEM_FUNCTION_H
libMesh::CompositeFEMFunction::~CompositeFEMFunction
virtual ~CompositeFEMFunction()=default
The subfunctions vector is automatically cleaned up.
libMesh::invalid_uint
const unsigned int invalid_uint
A number which is used quite often to represent an invalid or uninitialized value.
Definition: libmesh.h:249
libMesh::CompositeFEMFunction::n_subfunctions
unsigned int n_subfunctions() const
Definition: composite_fem_function.h:157
libMesh::FEMFunctionBase
FEMFunctionBase is a base class from which users can derive in order to define "function-like" object...
Definition: dirichlet_boundaries.h:43
libMesh::FEMFunctionBase::clone
virtual std::unique_ptr< FEMFunctionBase< Output > > clone() const =0
libMesh::CompositeFEMFunction::subfunctions
std::vector< std::unique_ptr< FEMFunctionBase< Output > > > subfunctions
Definition: composite_fem_function.h:169
libMesh::index_range
IntRange< std::size_t > index_range(const std::vector< T > &vec)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition: int_range.h:106
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::DenseVector::zero
virtual void zero() override
Set every element in the vector to 0.
Definition: dense_vector.h:379
libMesh::CompositeFEMFunction::reverse_index_map
std::vector< std::pair< unsigned int, unsigned int > > reverse_index_map
Definition: composite_fem_function.h:175
libMesh::CompositeFEMFunction::operator=
CompositeFEMFunction & operator=(CompositeFEMFunction &&)=default
libMesh::CompositeFEMFunction::clone
virtual std::unique_ptr< FEMFunctionBase< Output > > clone() const override
Definition: composite_fem_function.h:149
libMesh::CompositeFEMFunction::index_maps
std::vector< std::vector< unsigned int > > index_maps
Definition: composite_fem_function.h:172
libMesh::CompositeFEMFunction
FEMFunction which is a function of another function.
Definition: composite_fem_function.h:44
libMesh::CompositeFEMFunction::CompositeFEMFunction
CompositeFEMFunction()=default
libMesh::CompositeFEMFunction::component
virtual Output component(const FEMContext &c, unsigned int i, const Point &p, Real time) override
Definition: composite_fem_function.h:132
libMesh::Point
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38
libMesh::DenseVector::size
virtual unsigned int size() const override
Definition: dense_vector.h:92
libMesh::DenseVector::resize
void resize(const unsigned int n)
Resize the vector.
Definition: dense_vector.h:355
libMesh::CompositeFEMFunction::operator()
virtual Output operator()(const FEMContext &c, const Point &p, const Real time=0) override
Definition: composite_fem_function.h:103
libMesh::CompositeFEMFunction::attach_subfunction
void attach_subfunction(const FEMFunctionBase< Output > &f, const std::vector< unsigned int > &index_map)
Attach a new subfunction, along with a map from the indices of that subfunction to the indices of the...
Definition: composite_fem_function.h:73
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::DenseVector< Output >
libMesh::FEMContext
This class provides all data required for a physics package (e.g.
Definition: fem_context.h:62
libMesh::CompositeFEMFunction::n_components
unsigned int n_components() const
Definition: composite_fem_function.h:162