libMesh
wrapped_function.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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 
19 
20 #ifndef LIBMESH_WRAPPED_FUNCTION_H
21 #define LIBMESH_WRAPPED_FUNCTION_H
22 
23 // Local Includes
24 #include "libmesh/dense_vector.h"
25 #include "libmesh/equation_systems.h"
26 #include "libmesh/function_base.h"
27 #include "libmesh/libmesh_common.h"
28 #include "libmesh/point.h"
29 #include "libmesh/system.h"
30 
31 // C++ includes
32 #include <cstddef>
33 
34 namespace libMesh
35 {
36 
46 template <typename Output=Number>
47 class WrappedFunction : public FunctionBase<Output>
48 {
49 public:
50 
55  Output fptr(const Point & p,
56  const Parameters & parameters,
57  const std::string & sys_name,
58  const std::string & unknown_name) = libmesh_nullptr,
59  const Parameters * parameters = libmesh_nullptr,
60  unsigned int varnum=0)
61  : _sys(sys),
62  _fptr(fptr),
63  _parameters(parameters),
64  _varnum(varnum)
65  {
66  this->_initialized = true;
67  if (!parameters)
69  }
70 
71  virtual UniquePtr<FunctionBase<Output>> clone () const libmesh_override;
72 
73  virtual Output operator() (const Point & p,
74  const Real time = 0.) libmesh_override;
75 
76  virtual void operator() (const Point & p,
77  const Real time,
78  DenseVector<Output> & output) libmesh_override;
79 
80  virtual Output component (unsigned int i,
81  const Point & p,
82  Real time=0.) libmesh_override;
83 
84 protected:
85 
86  const System & _sys;
87 
88  Output (*_fptr)(const Point & p,
89  const Parameters & parameters,
90  const std::string & sys_name,
91  const std::string & unknown_name);
92 
94 
95  unsigned int _varnum;
96 };
97 
98 
99 // ------------------------------------------------------------
100 // WrappedFunction inline methods
101 
102 
103 template <typename Output>
104 inline
106  const Real /*time*/)
107 {
110  return _fptr(p,
111  *_parameters,
112  _sys.name(),
114 }
115 
116 
117 template <typename Output>
118 inline
121 {
125 }
126 
127 
128 template <typename Output>
129 inline
131  const Real /*time*/,
132  DenseVector<Output> & output)
133 {
136 
137  // We fill each entry of output with a single scalar component of
138  // the data in our System
139  libmesh_assert_equal_to (output.size(), _sys.n_components());
140 
141  // Loop over variables, then over each component in
142  // vector-valued variables, evaluating each.
143  const unsigned int n_vars = _sys.n_vars();
144  for (unsigned int v = 0; v != n_vars; ++v)
145  {
146  const unsigned int n_components =
148  if (n_components == 1)
149  output(_sys.variable_scalar_number(v,0)) =
151  else
152  {
153  // Right now our only non-scalar variable type is the
154  // SCALAR variables. The irony is priceless.
155  libmesh_assert_equal_to (_sys.variable(v).type().family, SCALAR);
156 
157  // We pass the point (j,0,0) to an old-style fptr function
158  // pointer to distinguish the different scalars within the
159  // SCALAR variable.
160  for (unsigned int j=0; j != n_components; ++j)
161  output(_sys.variable_scalar_number(v,j)) =
162  _fptr(Point(j,0,0), *_parameters,
163  _sys.name(), _sys.variable_name(v));
164  }
165  }
166 }
167 
168 
169 template <typename Output>
170 inline
171 Output WrappedFunction<Output>::component (unsigned int i,
172  const Point & p,
173  Real /*time*/)
174 {
177 
178  // Loop over variables, then over each component in
179  // vector-valued variables.
180  const unsigned int n_vars = _sys.n_vars();
181  for (unsigned int v = 0; v != n_vars; ++v)
182  {
183  const unsigned int n_components =
185  if (n_components == 1 &&
186  i == _sys.variable_scalar_number(v,0))
187  return _fptr(p, *_parameters, _sys.name(), _sys.variable_name(v));
188  else if (i >= _sys.variable_scalar_number(v,0) &&
189  i <= _sys.variable_scalar_number(v,n_components-1))
190  {
191  // Right now our only non-scalar variable type is the
192  // SCALAR variables. The irony is priceless.
193  libmesh_assert_equal_to (_sys.variable(i).type().family, SCALAR);
194 
195  // We pass the point (j,0,0) to an old-style fptr function
196  // pointer to distinguish the different scalars within the
197  // SCALAR variable.
198  for (unsigned int j=0; j != n_components; ++j)
199  if (i == _sys.variable_scalar_number(v,j))
200  return _fptr(Point(j,0,0), *_parameters,
201  _sys.name(), _sys.variable_name(v));
202  }
203  }
204 
205  libmesh_error_msg("Component index " << i << " not found in system " << _sys.name());
206  return Output();
207 }
208 
209 
210 
211 } // namespace libMesh
212 
213 #endif // LIBMESH_WRAPPED_FUNCTION_H
virtual Output component(unsigned int i, const Point &p, Real time=0.) libmesh_override
FEFamily family
The type of finite element.
Definition: fe_type.h:203
This class provides a wrapper with which to evaluate a (libMesh-style) function pointer in a Function...
const FEType & type() const
Definition: variable.h:119
const std::string & variable_name(const unsigned int i) const
Definition: system.h:2134
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition: parameters.h:63
Output(* _fptr)(const Point &p, const Parameters &parameters, const std::string &sys_name, const std::string &unknown_name)
ImplicitSystem & sys
bool _initialized
When init() was called so that everything is ready for calls to operator() (...), then this bool is t...
const class libmesh_nullptr_t libmesh_nullptr
WrappedFunction(const System &sys, Output fptr(const Point &p, const Parameters &parameters, const std::string &sys_name, const std::string &unknown_name)=libmesh_nullptr, const Parameters *parameters=libmesh_nullptr, unsigned int varnum=0)
Constructor to wrap scalar-valued function pointers.
const unsigned int n_vars
Definition: tecplot_io.C:68
The libMesh namespace provides an interface to certain functionality in the library.
virtual unsigned int size() const libmesh_override
Definition: dense_vector.h:87
const std::string & name() const
Definition: system.h:1998
Number fptr(const Point &p, const Parameters &, const std::string &libmesh_dbg_var(sys_name), const std::string &unknown_name)
Definition: projection.C:78
libmesh_assert(j)
std::unique_ptr< T > UniquePtr
Definition: auto_ptr.h:46
virtual Output operator()(const Point &p, const Real time=0.) libmesh_override
unsigned int n_components() const
Definition: system.h:2102
const Variable & variable(unsigned int var) const
Return a constant reference to Variable var.
Definition: system.h:2114
This is the base class for classes which contain information related to any physical process that mig...
Definition: system.h:76
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
unsigned int n_components() const
Definition: variable.h:125
const Parameters * _parameters
const EquationSystems & get_equation_systems() const
Definition: system.h:712
Parameters parameters
Data structure holding arbitrary parameters.
unsigned int variable_scalar_number(const std::string &var, unsigned int component) const
Definition: system.h:2145
unsigned int n_vars() const
Definition: system.h:2086
Defines a dense vector for use in Finite Element-type computations.
This is the base class for functor-like classes.
virtual UniquePtr< FunctionBase< Output > > clone() const libmesh_override
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38