composite_function.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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_FUNCTION_H
19 #define LIBMESH_COMPOSITE_FUNCTION_H
20 
21 // libMesh includes
22 #include "libmesh/dense_vector.h"
23 #include "libmesh/function_base.h"
24 #include "libmesh/libmesh.h"
25 #include "libmesh/point.h"
26 
27 // C++ includes
28 #include <algorithm>
29 #include <utility>
30 #include <vector>
31 
32 namespace libMesh
33 {
34 
47 template <typename Output=Number>
48 class CompositeFunction : public FunctionBase<Output>
49 {
50 public:
51  explicit
52  CompositeFunction () = default;
53 
57  CompositeFunction (CompositeFunction &&) = default;
59 
64  CompositeFunction (const CompositeFunction &) = delete;
66 
70  virtual ~CompositeFunction () = default;
71 
82  const std::vector<unsigned int> & index_map)
83  {
84  const unsigned int subfunction_index =
85  cast_int<unsigned int>(subfunctions.size());
86  libmesh_assert_equal_to(subfunctions.size(), index_maps.size());
87 
88  subfunctions.push_back(f.clone());
89  index_maps.push_back(index_map);
90 
91  unsigned int max_index =
92  *std::max_element(index_map.begin(), index_map.end());
93 
94  if (max_index >= reverse_index_map.size())
95  reverse_index_map.resize
96  (max_index+1, std::make_pair(libMesh::invalid_uint,
98 
99  for (std::size_t j=0; j != index_map.size(); ++j)
100  {
101  libmesh_assert_less(index_map[j], reverse_index_map.size());
102  libmesh_assert_equal_to(reverse_index_map[index_map[j]].first,
104  libmesh_assert_equal_to(reverse_index_map[index_map[j]].second,
106  reverse_index_map[index_map[j]] =
107  std::make_pair(subfunction_index, j);
108  }
109 
110  // Now check for time dependence
111  // We only check the function we just added instead of researching all subfunctions
112  // If this is the first subfunction, then that determines the time-dependence.
113  if (subfunctions.size() == 1)
115 
116  // Otherwise, we have more than 1 function already.
117  // If _is_time_dependent is true, then one of the previous
118  // subfunctions is time-dependent and thus this CompositeFunction
119  // time-dependent. If _is_time_dependent is false, then the subfunction
120  // just added determines the time-dependence.
121  else if (!this->_is_time_dependent)
123  }
124 
125  virtual Output operator() (const Point & p,
126  const Real time = 0) override
127  {
128  return this->component(0,p,time);
129  }
130 
131  virtual void operator() (const Point & p,
132  const Real time,
133  DenseVector<Output> & output) override
134  {
135  libmesh_assert_greater_equal (output.size(),
136  reverse_index_map.size());
137 
138  // Necessary in case we have output components not covered by
139  // any subfunctions
140  output.zero();
141 
142  DenseVector<Output> temp;
143  for (std::size_t i=0; i != subfunctions.size(); ++i)
144  {
145  temp.resize(cast_int<unsigned int>(index_maps[i].size()));
146  (*subfunctions[i])(p, time, temp);
147  for (unsigned int j=0; j != temp.size(); ++j)
148  output(index_maps[i][j]) = temp(j);
149  }
150  }
151 
152  virtual Output component (unsigned int i,
153  const Point & p,
154  Real time) override
155  {
156  if (i >= reverse_index_map.size() ||
158  return 0;
159 
160  libmesh_assert_less(reverse_index_map[i].first,
161  subfunctions.size());
162  libmesh_assert_not_equal_to(reverse_index_map[i].second,
164  return subfunctions[reverse_index_map[i].first]->
165  component(reverse_index_map[i].second,p,time);
166  }
167 
168  virtual std::unique_ptr<FunctionBase<Output>> clone() const override
169  {
170  CompositeFunction * returnval = new CompositeFunction();
171  for (std::size_t i=0; i != subfunctions.size(); ++i)
172  returnval->attach_subfunction(*subfunctions[i], index_maps[i]);
173  return std::unique_ptr<FunctionBase<Output>> (returnval);
174  }
175 
176  unsigned int n_subfunctions () const
177  {
178  return subfunctions.size();
179  }
180 
181  unsigned int n_components () const
182  {
183  return reverse_index_map.size();
184  }
185 
186 private:
187  // list of functions which fill in our values
188  std::vector<std::unique_ptr<FunctionBase<Output>>> subfunctions;
189 
190  // for each function, list of which global indices it fills in
191  std::vector<std::vector<unsigned int>> index_maps;
192 
193  // for each global index, which local index of which function is it?
194  std::vector<std::pair<unsigned int, unsigned int>> reverse_index_map;
195 };
196 
197 
198 } // namespace libMesh
199 
200 #endif // LIBMESH_COMPOSITE_FUNCTION_H
std::vector< std::unique_ptr< FunctionBase< Output > > > subfunctions
virtual unsigned int size() const override
Definition: dense_vector.h:92
void attach_subfunction(const FunctionBase< Output > &f, const std::vector< unsigned int > &index_map)
CompositeFunction & operator=(CompositeFunction &&)=default
const unsigned int invalid_uint
Definition: libmesh.h:245
unsigned int n_components() const
void resize(const unsigned int n)
Definition: dense_vector.h:355
virtual std::unique_ptr< FunctionBase< Output > > clone() const override
virtual Output operator()(const Point &p, const Real time=0) override
A function that returns a vector whose components are defined by multiple functions.
bool is_time_dependent() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual ~CompositeFunction()=default
std::vector< std::pair< unsigned int, unsigned int > > reverse_index_map
std::vector< std::vector< unsigned int > > index_maps
virtual std::unique_ptr< FunctionBase< Output > > clone() const =0
virtual Output component(unsigned int i, const Point &p, Real time) override
Base class for functors that can be evaluated at a point and (optionally) time.
unsigned int n_subfunctions() const
A geometric point in (x,y,z) space.
Definition: point.h:38
virtual void zero() override
Definition: dense_vector.h:379