libMesh
Public Member Functions | Protected Attributes | Private Attributes | List of all members
libMesh::CompositeFunction< Output > Class Template Reference

A function that returns a vector whose components are defined by multiple functions. More...

#include <composite_function.h>

Inheritance diagram for libMesh::CompositeFunction< Output >:
[legend]

Public Member Functions

 CompositeFunction ()=default
 
 CompositeFunction (CompositeFunction &&)=default
 This class can be default move constructed and assigned. More...
 
CompositeFunctionoperator= (CompositeFunction &&)=default
 
 CompositeFunction (const CompositeFunction &)=delete
 This class contains unique_ptr members so it can't be default copied or assigned. More...
 
CompositeFunctionoperator= (const CompositeFunction &)=delete
 
virtual ~CompositeFunction ()=default
 The subfunctions vector is automatically cleaned up. More...
 
void attach_subfunction (const FunctionBase< Output > &f, const std::vector< unsigned int > &index_map)
 Attach a new subfunction, along with a map from the indices of the attached subfunction to the indices of the composed function. More...
 
virtual Output operator() (const Point &p, const Real time=0) override
 
virtual void operator() (const Point &p, const Real time, DenseVector< Output > &output) override
 Evaluation function for time-dependent vector-valued functions. More...
 
virtual Output component (unsigned int i, const Point &p, Real time) override
 
virtual std::unique_ptr< FunctionBase< Output > > clone () const override
 
unsigned int n_subfunctions () const
 
unsigned int n_components () const
 
virtual void init ()
 The actual initialization process. More...
 
virtual void clear ()
 Clears the function. More...
 
void operator() (const Point &p, DenseVector< Output > &output)
 Evaluation function for time-independent vector-valued functions. More...
 
bool initialized () const
 
void set_is_time_dependent (bool is_time_dependent)
 Function to set whether this is a time-dependent function or not. More...
 
bool is_time_dependent () const
 

Protected Attributes

const FunctionBase_master
 Const pointer to our master, initialized to nullptr. More...
 
bool _initialized
 When init() was called so that everything is ready for calls to operator() (...), then this bool is true. More...
 
bool _is_time_dependent
 Cache whether or not this function is actually time-dependent. More...
 

Private Attributes

std::vector< std::unique_ptr< FunctionBase< Output > > > subfunctions
 
std::vector< std::vector< unsigned int > > index_maps
 
std::vector< std::pair< unsigned int, unsigned int > > reverse_index_map
 

Detailed Description

template<typename Output = Number>
class libMesh::CompositeFunction< Output >

A function that returns a vector whose components are defined by multiple functions.

A function which is defined by composing the result of different functions into a single vector. All overridden virtual functions are documented in function_base.h.

Author
Roy Stogner
Date
2012

Function which is a function of another function.

Definition at line 49 of file composite_function.h.

Constructor & Destructor Documentation

◆ CompositeFunction() [1/3]

template<typename Output = Number>
libMesh::CompositeFunction< Output >::CompositeFunction ( )
explicitdefault

◆ CompositeFunction() [2/3]

template<typename Output = Number>
libMesh::CompositeFunction< Output >::CompositeFunction ( CompositeFunction< Output > &&  )
default

This class can be default move constructed and assigned.

◆ CompositeFunction() [3/3]

template<typename Output = Number>
libMesh::CompositeFunction< Output >::CompositeFunction ( const CompositeFunction< Output > &  )
delete

This class contains unique_ptr members so it can't be default copied or assigned.

◆ ~CompositeFunction()

template<typename Output = Number>
virtual libMesh::CompositeFunction< Output >::~CompositeFunction ( )
virtualdefault

The subfunctions vector is automatically cleaned up.

Member Function Documentation

◆ attach_subfunction()

template<typename Output = Number>
void libMesh::CompositeFunction< Output >::attach_subfunction ( const FunctionBase< Output > &  f,
const std::vector< unsigned int > &  index_map 
)
inline

Attach a new subfunction, along with a map from the indices of the attached subfunction to the indices of the composed function.

The composed function will return a vector whose value at index index_map[i] is the value of the attached function at index i, i.e., (*this)(x, t)(index_map[i]) will return f(x, t)(i).

Definition at line 82 of file composite_function.h.

84  {
85  const unsigned int subfunction_index =
86  cast_int<unsigned int>(subfunctions.size());
87  libmesh_assert_equal_to(subfunctions.size(), index_maps.size());
88 
89  subfunctions.push_back(f.clone());
90  index_maps.push_back(index_map);
91 
92  unsigned int max_index =
93  *std::max_element(index_map.begin(), index_map.end());
94 
95  if (max_index >= reverse_index_map.size())
96  reverse_index_map.resize
97  (max_index+1, std::make_pair(libMesh::invalid_uint,
99 
100  for (auto j : index_range(index_map))
101  {
102  libmesh_assert_less(index_map[j], reverse_index_map.size());
103  libmesh_assert_equal_to(reverse_index_map[index_map[j]].first,
105  libmesh_assert_equal_to(reverse_index_map[index_map[j]].second,
107  reverse_index_map[index_map[j]] =
108  std::make_pair(subfunction_index, j);
109  }
110 
111  // Now check for time dependence
112  // We only check the function we just added instead of researching all subfunctions
113  // If this is the first subfunction, then that determines the time-dependence.
114  if (subfunctions.size() == 1)
116 
117  // Otherwise, we have more than 1 function already.
118  // If _is_time_dependent is true, then one of the previous
119  // subfunctions is time-dependent and thus this CompositeFunction
120  // time-dependent. If _is_time_dependent is false, then the subfunction
121  // just added determines the time-dependence.
122  else if (!this->_is_time_dependent)
124  }

References libMesh::FunctionBase< Output >::_is_time_dependent, libMesh::FunctionBase< Output >::clone(), libMesh::CompositeFunction< Output >::index_maps, libMesh::index_range(), libMesh::invalid_uint, libMesh::FunctionBase< Output >::is_time_dependent(), libMesh::CompositeFunction< Output >::reverse_index_map, and libMesh::CompositeFunction< Output >::subfunctions.

Referenced by libMesh::CompositeFunction< Output >::clone(), CompositeFunctionTest::testRemap(), and CompositeFunctionTest::testTimeDependence().

◆ clear()

template<typename Output = Number>
virtual void libMesh::FunctionBase< Output >::clear ( )
inlinevirtualinherited

◆ clone()

template<typename Output = Number>
virtual std::unique_ptr<FunctionBase<Output> > libMesh::CompositeFunction< Output >::clone ( ) const
inlineoverridevirtual
Returns
A new copy of the function.

The new copy should be as "deep" as necessary to allow independent destruction and simultaneous evaluations of the copies in different threads.

Implements libMesh::FunctionBase< Output >.

Definition at line 169 of file composite_function.h.

170  {
171  CompositeFunction * returnval = new CompositeFunction();
172  for (auto i : index_range(subfunctions))
173  returnval->attach_subfunction(*subfunctions[i], index_maps[i]);
174  return std::unique_ptr<FunctionBase<Output>> (returnval);
175  }

References libMesh::CompositeFunction< Output >::attach_subfunction(), libMesh::CompositeFunction< Output >::CompositeFunction(), libMesh::CompositeFunction< Output >::index_maps, libMesh::index_range(), and libMesh::CompositeFunction< Output >::subfunctions.

◆ component()

template<typename Output = Number>
virtual Output libMesh::CompositeFunction< Output >::component ( unsigned int  i,
const Point p,
Real  time 
)
inlineoverridevirtual
Returns
The vector component i at coordinate p and time time.
Note
Subclasses aren't required to override this, since the default implementation is based on the full vector evaluation, which is often correct.
Subclasses are recommended to override this, since the default implementation is based on a vector evaluation, which is usually unnecessarily inefficient.

Reimplemented from libMesh::FunctionBase< Output >.

Definition at line 153 of file composite_function.h.

156  {
157  if (i >= reverse_index_map.size() ||
159  return 0;
160 
161  libmesh_assert_less(reverse_index_map[i].first,
162  subfunctions.size());
163  libmesh_assert_not_equal_to(reverse_index_map[i].second,
165  return subfunctions[reverse_index_map[i].first]->
166  component(reverse_index_map[i].second,p,time);
167  }

References libMesh::invalid_uint, libMesh::CompositeFunction< Output >::reverse_index_map, and libMesh::CompositeFunction< Output >::subfunctions.

Referenced by libMesh::CompositeFunction< Output >::operator()().

◆ init()

template<typename Output = Number>
virtual void libMesh::FunctionBase< Output >::init ( )
inlinevirtualinherited

◆ initialized()

template<typename Output >
bool libMesh::FunctionBase< Output >::initialized ( ) const
inlineinherited
Returns
true when this object is properly initialized and ready for use, false otherwise.

Definition at line 205 of file function_base.h.

206 {
207  return (this->_initialized);
208 }

◆ is_time_dependent()

template<typename Output >
bool libMesh::FunctionBase< Output >::is_time_dependent ( ) const
inlineinherited
Returns
true when the function this object represents is actually time-dependent, false otherwise.

Definition at line 219 of file function_base.h.

220 {
221  return (this->_is_time_dependent);
222 }

Referenced by libMesh::CompositeFunction< Output >::attach_subfunction(), CompositeFunctionTest::testTimeDependence(), and ParsedFunctionTest::testTimeDependence().

◆ n_components()

template<typename Output = Number>
unsigned int libMesh::CompositeFunction< Output >::n_components ( ) const
inline

Definition at line 182 of file composite_function.h.

183  {
184  return reverse_index_map.size();
185  }

References libMesh::CompositeFunction< Output >::reverse_index_map.

◆ n_subfunctions()

template<typename Output = Number>
unsigned int libMesh::CompositeFunction< Output >::n_subfunctions ( ) const
inline

Definition at line 177 of file composite_function.h.

178  {
179  return subfunctions.size();
180  }

References libMesh::CompositeFunction< Output >::subfunctions.

◆ operator()() [1/3]

template<typename Output = Number>
virtual void libMesh::CompositeFunction< Output >::operator() ( const Point p,
const Real  time,
DenseVector< Output > &  output 
)
inlineoverridevirtual

Evaluation function for time-dependent vector-valued functions.

Sets output values in the passed-in output DenseVector.

Pure virtual, so you have to override it.

Implements libMesh::FunctionBase< Output >.

Definition at line 132 of file composite_function.h.

135  {
136  libmesh_assert_greater_equal (output.size(),
137  reverse_index_map.size());
138 
139  // Necessary in case we have output components not covered by
140  // any subfunctions
141  output.zero();
142 
143  DenseVector<Output> temp;
144  for (auto i : index_range(subfunctions))
145  {
146  temp.resize(cast_int<unsigned int>(index_maps[i].size()));
147  (*subfunctions[i])(p, time, temp);
148  for (auto j : index_range(temp))
149  output(index_maps[i][j]) = temp(j);
150  }
151  }

References libMesh::CompositeFunction< Output >::index_maps, libMesh::index_range(), libMesh::DenseVector< T >::resize(), libMesh::CompositeFunction< Output >::reverse_index_map, libMesh::DenseVector< T >::size(), libMesh::CompositeFunction< Output >::subfunctions, and libMesh::DenseVector< T >::zero().

◆ operator()() [2/3]

template<typename Output = Number>
virtual Output libMesh::CompositeFunction< Output >::operator() ( const Point p,
const Real  time = 0 
)
inlineoverridevirtual
Returns
The scalar function value at coordinate p and time time, which defaults to zero.

Pure virtual, so you have to override it.

Implements libMesh::FunctionBase< Output >.

Definition at line 126 of file composite_function.h.

128  {
129  return this->component(0,p,time);
130  }

References libMesh::CompositeFunction< Output >::component().

◆ operator()() [3/3]

template<typename Output>
void libMesh::FunctionBase< Output >::operator() ( const Point p,
DenseVector< Output > &  output 
)
inlineinherited

Evaluation function for time-independent vector-valued functions.

Sets output values in the passed-in output DenseVector.

Definition at line 240 of file function_base.h.

242 {
243  // Call the time-dependent function with t=0.
244  this->operator()(p, 0., output);
245 }

◆ operator=() [1/2]

template<typename Output = Number>
CompositeFunction& libMesh::CompositeFunction< Output >::operator= ( CompositeFunction< Output > &&  )
default

◆ operator=() [2/2]

template<typename Output = Number>
CompositeFunction& libMesh::CompositeFunction< Output >::operator= ( const CompositeFunction< Output > &  )
delete

◆ set_is_time_dependent()

template<typename Output >
void libMesh::FunctionBase< Output >::set_is_time_dependent ( bool  is_time_dependent)
inlineinherited

Function to set whether this is a time-dependent function or not.

This is intended to be only used by subclasses who cannot natively determine time-dependence. In such a case, this function should be used immediately following construction.

Definition at line 212 of file function_base.h.

213 {
215 }

Member Data Documentation

◆ _initialized

template<typename Output = Number>
bool libMesh::FunctionBase< Output >::_initialized
protectedinherited

When init() was called so that everything is ready for calls to operator() (...), then this bool is true.

Definition at line 179 of file function_base.h.

Referenced by libMesh::AnalyticFunction< Output >::AnalyticFunction(), libMesh::ConstFunction< Output >::ConstFunction(), and libMesh::WrappedFunction< Output >::WrappedFunction().

◆ _is_time_dependent

template<typename Output = Number>
bool libMesh::FunctionBase< Output >::_is_time_dependent
protectedinherited

Cache whether or not this function is actually time-dependent.

Definition at line 184 of file function_base.h.

Referenced by libMesh::CompositeFunction< Output >::attach_subfunction(), and libMesh::ConstFunction< Output >::ConstFunction().

◆ _master

template<typename Output = Number>
const FunctionBase* libMesh::FunctionBase< Output >::_master
protectedinherited

Const pointer to our master, initialized to nullptr.

There may be cases where multiple functions are required, but to save memory, one master handles some centralized data.

Definition at line 173 of file function_base.h.

◆ index_maps

template<typename Output = Number>
std::vector<std::vector<unsigned int> > libMesh::CompositeFunction< Output >::index_maps
private

◆ reverse_index_map

template<typename Output = Number>
std::vector<std::pair<unsigned int, unsigned int> > libMesh::CompositeFunction< Output >::reverse_index_map
private

◆ subfunctions

template<typename Output = Number>
std::vector<std::unique_ptr<FunctionBase<Output> > > libMesh::CompositeFunction< Output >::subfunctions
private

The documentation for this class was generated from the following file:
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::FunctionBase::is_time_dependent
bool is_time_dependent() const
Definition: function_base.h:219
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::DenseVector::zero
virtual void zero() override
Set every element in the vector to 0.
Definition: dense_vector.h:379
libMesh::FunctionBase::_is_time_dependent
bool _is_time_dependent
Cache whether or not this function is actually time-dependent.
Definition: function_base.h:184
libMesh::CompositeFunction::CompositeFunction
CompositeFunction()=default
libMesh::CompositeFunction::reverse_index_map
std::vector< std::pair< unsigned int, unsigned int > > reverse_index_map
Definition: composite_function.h:195
libMesh::CompositeFunction::subfunctions
std::vector< std::unique_ptr< FunctionBase< Output > > > subfunctions
Definition: composite_function.h:189
libMesh::FunctionBase::_initialized
bool _initialized
When init() was called so that everything is ready for calls to operator() (...), then this bool is t...
Definition: function_base.h:179
libMesh::CompositeFunction::component
virtual Output component(unsigned int i, const Point &p, Real time) override
Definition: composite_function.h:153
libMesh::CompositeFunction::index_maps
std::vector< std::vector< unsigned int > > index_maps
Definition: composite_function.h:192
libMesh::DenseVector::size
virtual unsigned int size() const override
Definition: dense_vector.h:92
libMesh::FunctionBase::operator()
virtual Output operator()(const Point &p, const Real time=0.)=0