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

Wrap a libMesh-style function pointer into a FunctionBase object. More...

#include <wrapped_function.h>

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

Public Member Functions

 WrappedFunction (const System &sys, Output fptr(const Point &p, const Parameters &parameters, const std::string &sys_name, const std::string &unknown_name)=nullptr, const Parameters *parameters=nullptr, unsigned int varnum=0)
 Constructor to wrap scalar-valued function pointers. More...
 
 WrappedFunction (WrappedFunction &&)=default
 The move/copy ctor and destructor are defaulted for this class. More...
 
 WrappedFunction (const WrappedFunction &)=default
 
virtual ~WrappedFunction ()=default
 
WrappedFunctionoperator= (const WrappedFunction &)=delete
 This class contains a const reference so it can't be assigned. More...
 
WrappedFunctionoperator= (WrappedFunction &&)=delete
 
virtual std::unique_ptr< FunctionBase< Output > > clone () const override
 
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=0.) override
 
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 System_sys
 
Output(* _fptr )(const Point &p, const Parameters &parameters, const std::string &sys_name, const std::string &unknown_name)
 
const Parameters_parameters
 
unsigned int _varnum
 
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...
 

Detailed Description

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

Wrap a libMesh-style function pointer into a FunctionBase object.

This class provides a wrapper with which to evaluate a (libMesh-style) function pointer in a FunctionBase-compatible interface. All overridden virtual functions are documented in function_base.h.

Author
Roy Stogner
Date
2012
Note
To wrap an ordinary function pointer use the AnalyticFunction class.

Definition at line 52 of file wrapped_function.h.

Constructor & Destructor Documentation

◆ WrappedFunction() [1/3]

template<typename Output = Number>
libMesh::WrappedFunction< Output >::WrappedFunction ( const System sys,
Output   fptrconst Point &p, const Parameters &parameters, const std::string &sys_name, const std::string &unknown_name = nullptr,
const Parameters parameters = nullptr,
unsigned int  varnum = 0 
)
inline

Constructor to wrap scalar-valued function pointers.

Definition at line 59 of file wrapped_function.h.

References libMesh::FunctionBase< Output >::_initialized, libMesh::WrappedFunction< Output >::_parameters, libMesh::System::get_equation_systems(), and libMesh::EquationSystems::parameters.

66  : _sys(sys),
67  _fptr(fptr),
68  _parameters(parameters),
69  _varnum(varnum)
70  {
71  this->_initialized = true;
72  if (!parameters)
73  _parameters = &sys.get_equation_systems().parameters;
74  }
Output(* _fptr)(const Point &p, const Parameters &parameters, const std::string &sys_name, const std::string &unknown_name)
bool _initialized
When init() was called so that everything is ready for calls to operator() (...), then this bool is t...
Number fptr(const Point &p, const Parameters &, const std::string &libmesh_dbg_var(sys_name), const std::string &unknown_name)
Definition: projection.C:80
const Parameters * _parameters

◆ WrappedFunction() [2/3]

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

The move/copy ctor and destructor are defaulted for this class.

◆ WrappedFunction() [3/3]

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

◆ ~WrappedFunction()

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

Member Function Documentation

◆ clear()

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

◆ clone()

template<typename Output >
std::unique_ptr< FunctionBase< Output > > libMesh::WrappedFunction< 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 138 of file wrapped_function.h.

139 {
140  return std::make_unique<WrappedFunction<Output>>
142 }
Output(* _fptr)(const Point &p, const Parameters &parameters, const std::string &sys_name, const std::string &unknown_name)
const Parameters * _parameters

◆ component()

template<typename Output >
Output libMesh::WrappedFunction< Output >::component ( unsigned int  i,
const Point p,
Real  time = 0. 
)
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.
The default implementation calls operator() with a DenseVector of size i+1 which will result in unexpected behaviour if operator() makes any access beyond that limit.

Reimplemented from libMesh::FunctionBase< Output >.

Definition at line 188 of file wrapped_function.h.

References libMesh::libmesh_assert(), n_vars, libMesh::FEInterface::n_vec_dim(), and libMesh::SCALAR.

191 {
194 
195  // Loop over variables, then over each component in
196  // FEFamily SCALAR variables.
197  unsigned int vc = 0;
198  const unsigned int n_vars = _sys.n_vars();
199  for (unsigned int v = 0; v != n_vars; ++v)
200  {
201  const auto & var_fe_type = _sys.variable_type(v);
202  const unsigned int n_components = [&var_fe_type, v, this](){
203  if (var_fe_type.family == SCALAR)
204  return _sys.variable(v).n_components();
205  else
206  return FEInterface::n_vec_dim(_sys.get_mesh(), var_fe_type);
207  }();
208  if (i >= vc + n_components)
209  {
210  vc += n_components;
211  continue;
212  }
213 
214  if (n_components > 1 && var_fe_type.family != SCALAR)
215  libmesh_error_msg(
216  "WrappedFunction::component cannot currently evaluate vector finite element families");
217 
218  if (n_components == 1)
219  return _fptr(p, *_parameters, _sys.name(), _sys.variable_name(v));
220  else
221  {
222  libmesh_assert_equal_to (_sys.variable(i).type().family, SCALAR);
223 
224  // We pass the point (j,0,0) to an old-style fptr function
225  // pointer to distinguish the different scalars within the
226  // SCALAR variable.
227  for (unsigned int j=0; j != n_components; ++j)
228  if (i == vc + j)
229  return _fptr(Point(j,0,0), *_parameters,
230  _sys.name(), _sys.variable_name(v));
231  }
232  }
233 
234  libmesh_error_msg("Component index " << i << " not found in system " << _sys.name());
235  return Output();
236 }
FEFamily family
The type of finite element.
Definition: fe_type.h:207
const Variable & variable(unsigned int var) const
Return a constant reference to Variable var.
Definition: system.h:2377
Output(* _fptr)(const Point &p, const Parameters &parameters, const std::string &sys_name, const std::string &unknown_name)
const MeshBase & get_mesh() const
Definition: system.h:2277
unsigned int n_vars
unsigned int n_components() const
Definition: variable.h:146
libmesh_assert(ctx)
const std::string & variable_name(const unsigned int i) const
Definition: system.h:2397
static unsigned int n_vec_dim(const MeshBase &mesh, const FEType &fe_type)
const FEType & variable_type(const unsigned int i) const
Definition: system.h:2427
const Parameters * _parameters
const std::string & name() const
Definition: system.h:2261
unsigned int n_vars() const
Definition: system.h:2349
const FEType & type() const
Definition: variable.h:140

◆ 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 210 of file function_base.h.

Referenced by libMesh::MeshFunction::MeshFunction().

211 {
212  return (this->_initialized);
213 }
bool _initialized
When init() was called so that everything is ready for calls to operator() (...), then this bool is t...

◆ 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 224 of file function_base.h.

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

225 {
226  return (this->_is_time_dependent);
227 }
bool _is_time_dependent
Cache whether or not this function is actually time-dependent.

◆ operator()() [1/3]

template<typename Output >
Output libMesh::WrappedFunction< 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 123 of file wrapped_function.h.

References libMesh::libmesh_assert().

125 {
128  return _fptr(p,
129  *_parameters,
130  _sys.name(),
132 }
Output(* _fptr)(const Point &p, const Parameters &parameters, const std::string &sys_name, const std::string &unknown_name)
libmesh_assert(ctx)
const std::string & variable_name(const unsigned int i) const
Definition: system.h:2397
const Parameters * _parameters
const std::string & name() const
Definition: system.h:2261

◆ operator()() [2/3]

template<typename Output >
void libMesh::WrappedFunction< 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 147 of file wrapped_function.h.

References libMesh::libmesh_assert(), n_vars, libMesh::SCALAR, and libMesh::DenseVector< T >::size().

150 {
153 
154  // We fill each entry of output with a single scalar component of
155  // the data in our System
156  libmesh_assert_equal_to (output.size(), _sys.n_components());
157 
158  // Loop over variables, then over each component in
159  // vector-valued variables, evaluating each.
160  const unsigned int n_vars = _sys.n_vars();
161  for (unsigned int v = 0; v != n_vars; ++v)
162  {
163  const unsigned int n_components =
165  if (n_components == 1)
166  output(_sys.variable_scalar_number(v,0)) =
168  else
169  {
170  // Right now our only non-scalar variable type is the
171  // SCALAR variables. The irony is priceless.
172  libmesh_assert_equal_to (_sys.variable(v).type().family, SCALAR);
173 
174  // We pass the point (j,0,0) to an old-style fptr function
175  // pointer to distinguish the different scalars within the
176  // SCALAR variable.
177  for (unsigned int j=0; j != n_components; ++j)
178  output(_sys.variable_scalar_number(v,j)) =
179  _fptr(Point(j,0,0), *_parameters,
180  _sys.name(), _sys.variable_name(v));
181  }
182  }
183 }
FEFamily family
The type of finite element.
Definition: fe_type.h:207
unsigned int variable_scalar_number(std::string_view var, unsigned int component) const
Definition: system.h:2408
const Variable & variable(unsigned int var) const
Return a constant reference to Variable var.
Definition: system.h:2377
Output(* _fptr)(const Point &p, const Parameters &parameters, const std::string &sys_name, const std::string &unknown_name)
unsigned int n_components() const
Definition: system.h:2365
unsigned int n_vars
unsigned int n_components() const
Definition: variable.h:146
libmesh_assert(ctx)
const std::string & variable_name(const unsigned int i) const
Definition: system.h:2397
const Parameters * _parameters
virtual unsigned int size() const override final
Definition: dense_vector.h:104
const std::string & name() const
Definition: system.h:2261
unsigned int n_vars() const
Definition: system.h:2349
const FEType & type() const
Definition: variable.h:140

◆ 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 245 of file function_base.h.

247 {
248  // Call the time-dependent function with t=0.
249  this->operator()(p, 0., output);
250 }
virtual Output operator()(const Point &p, const Real time=0.)=0

◆ operator=() [1/2]

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

This class contains a const reference so it can't be assigned.

◆ operator=() [2/2]

template<typename Output = Number>
WrappedFunction& libMesh::WrappedFunction< Output >::operator= ( WrappedFunction< 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 217 of file function_base.h.

218 {
220 }
bool is_time_dependent() const
bool _is_time_dependent
Cache whether or not this function is actually time-dependent.

Member Data Documentation

◆ _fptr

template<typename Output = Number>
Output(* libMesh::WrappedFunction< Output >::_fptr) (const Point &p, const Parameters &parameters, const std::string &sys_name, const std::string &unknown_name)
protected

Definition at line 106 of file wrapped_function.h.

◆ _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 184 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 189 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 178 of file function_base.h.

◆ _parameters

template<typename Output = Number>
const Parameters* libMesh::WrappedFunction< Output >::_parameters
protected

◆ _sys

template<typename Output = Number>
const System& libMesh::WrappedFunction< Output >::_sys
protected

Definition at line 104 of file wrapped_function.h.

◆ _varnum

template<typename Output = Number>
unsigned int libMesh::WrappedFunction< Output >::_varnum
protected

Definition at line 113 of file wrapped_function.h.


The documentation for this class was generated from the following file: