libMesh
Loading...
Searching...
No Matches
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.
 
 WrappedFunction (WrappedFunction &&)=default
 The move/copy ctor and destructor are defaulted for this class.
 
 WrappedFunction (const WrappedFunction &)=default
 
virtual ~WrappedFunction ()=default
 
WrappedFunctionoperator= (const WrappedFunction &)=delete
 This class contains a const reference so it can't be assigned.
 
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.
 
virtual Output component (unsigned int i, const Point &p, Real time=0.) override
 
virtual void init ()
 The actual initialization process.
 
virtual void clear ()
 Clears the function.
 
void operator() (const Point &p, DenseVector< Output > &output)
 Evaluation function for time-independent vector-valued functions.
 
bool initialized () const
 
void set_is_time_dependent (bool is_time_dependent)
 Function to set whether this is a time-dependent function or not.
 
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.
 
bool _initialized
 When init() was called so that everything is ready for calls to operator() (...), then this bool is true.
 
bool _is_time_dependent
 Cache whether or not this function is actually time-dependent.
 

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 53 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 60 of file wrapped_function.h.

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

References libMesh::FunctionBase< Output >::_initialized, libMesh::WrappedFunction< Output >::_parameters, libMesh::System::get_equation_systems(), and libMesh::EquationSystems::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 139 of file wrapped_function.h.

140{
141 return std::make_unique<WrappedFunction<Output>>
143}

◆ 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 189 of file wrapped_function.h.

192{
195
196 // Loop over variables, then over each component in
197 // FEFamily SCALAR variables.
198 unsigned int vc = 0;
199 const unsigned int n_vars = _sys.n_vars();
200 for (unsigned int v = 0; v != n_vars; ++v)
201 {
202 const auto & var_fe_type = _sys.variable_type(v);
203 const auto n_components = _sys.variable(v).n_components(_sys.get_mesh());
204 if (i >= vc + n_components)
205 {
206 vc += n_components;
207 continue;
208 }
209
210 if (n_components > 1 && var_fe_type.family != SCALAR)
211 libmesh_error_msg(
212 "WrappedFunction::component cannot currently evaluate vector finite element families");
213
214 if (n_components == 1)
215 return _fptr(p, *_parameters, _sys.name(), _sys.variable_name(v));
216 else
217 {
218 libmesh_assert_equal_to (_sys.variable(i).type().family, SCALAR);
219
220 // We pass the point (j,0,0) to an old-style fptr function
221 // pointer to distinguish the different scalars within the
222 // SCALAR variable.
223 for (unsigned int j=0; j != n_components; ++j)
224 if (i == vc + j)
225 return _fptr(Point(j,0,0), *_parameters,
227 }
228 }
229
230 libmesh_error_msg("Component index " << i << " not found in system " << _sys.name());
231 return Output();
232}
unsigned int n_vars
FEFamily family
The type of finite element.
Definition fe_type.h:228
const std::string & name() const
Definition system.h:2385
const Variable & variable(unsigned int var) const
Return a constant reference to Variable var.
Definition system.C:2704
const FEType & variable_type(const unsigned int i) const
Definition system.C:2721
const std::string & variable_name(const unsigned int i) const
Definition system.C:2679
unsigned int n_vars() const
Definition system.C:2674
const MeshBase & get_mesh() const
Definition system.h:2401
unsigned int n_components() const
Definition variable.C:23
const FEType & type() const
Definition variable.h:144
libmesh_assert(ctx)

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

◆ 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.

211{
212 return (this->_initialized);
213}

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

◆ 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.

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

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

◆ operator()() [1/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 148 of file wrapped_function.h.

151{
154
155 // We fill each entry of output with a single scalar component of
156 // the data in our System
157 libmesh_assert_equal_to (output.size(), _sys.n_components());
158
159 // Loop over variables, then over each component in
160 // vector-valued variables, evaluating each.
161 const unsigned int n_vars = _sys.n_vars();
162 for (unsigned int v = 0; v != n_vars; ++v)
163 {
164 const auto n_components =
166 if (n_components == 1)
167 output(_sys.variable_scalar_number(v,0)) =
169 else
170 {
171 // Right now our only non-scalar variable type is the
172 // SCALAR variables. The irony is priceless.
173 libmesh_assert_equal_to (_sys.variable(v).type().family, SCALAR);
174
175 // We pass the point (j,0,0) to an old-style fptr function
176 // pointer to distinguish the different scalars within the
177 // SCALAR variable.
178 for (unsigned int j=0; j != n_components; ++j)
179 output(_sys.variable_scalar_number(v,j)) =
180 _fptr(Point(j,0,0), *_parameters,
182 }
183 }
184}
unsigned int n_components() const
Definition system.C:2694
unsigned int variable_scalar_number(std::string_view var, unsigned int component) const
Definition system.h:2474

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

◆ operator()() [2/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 124 of file wrapped_function.h.

126{
129 return _fptr(p,
131 _sys.name(),
133}

References libMesh::libmesh_assert().

◆ 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

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 107 of file wrapped_function.h.

◆ _initialized

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

◆ _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 105 of file wrapped_function.h.

◆ _varnum

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

Definition at line 114 of file wrapped_function.h.


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