wrapped_function.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 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"
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 
37 
41 // ------------------------------------------------------------
42 // WrappedFunction class definition
43 template <typename Output=Number>
44 class WrappedFunction : public FunctionBase<Output>
45 {
46 public:
47 
51  WrappedFunction (const System &sys,
52  Output fptr(const Point& p,
53  const Parameters& parameters,
54  const std::string& sys_name,
55  const std::string& unknown_name) = NULL,
56  const Parameters* parameters = NULL,
57  unsigned int varnum=0)
58  : _sys(sys),
59  _fptr(fptr),
60  _parameters(parameters),
61  _varnum(varnum)
62  {
63  this->_initialized = true;
64  if (!parameters)
66  }
67 
68  virtual AutoPtr<FunctionBase<Output> > clone () const;
69 
74  virtual Output operator() (const Point& p,
75  const Real time = 0.);
76 
82  virtual void operator() (const Point& p,
83  const Real time,
84  DenseVector<Output>& output);
85 
90  virtual Output component (unsigned int i,
91  const Point& p,
92  Real time=0.);
93 
94 protected:
95 
96  const System& _sys;
97 
98  Output (*_fptr)(const Point& p,
99  const Parameters& parameters,
100  const std::string& sys_name,
101  const std::string& unknown_name);
102 
104 
105  unsigned int _varnum;
106 };
107 
108 
109 // ------------------------------------------------------------
110 // WrappedFunction inline methods
111 
112 
113 template <typename Output>
114 inline
116  const Real /*time*/)
117 {
118  libmesh_assert(_fptr);
119  libmesh_assert(_parameters);
120  return _fptr(p,
121  *_parameters,
122  _sys.name(),
123  _sys.variable_name(_varnum));
124 }
125 
126 
127 template <typename Output>
128 inline
131 {
134  (_sys, _fptr, _parameters, _varnum));
135 }
136 
137 
143 template <typename Output>
144 inline
146  const Real /*time*/,
147  DenseVector<Output>& output)
148 {
149  libmesh_assert(_fptr);
150  libmesh_assert(_parameters);
151 
152  // We fill each entry of output with a single scalar component of
153  // the data in our System
154  const unsigned int size = output.size();
155  libmesh_assert_equal_to (size, _sys.n_components());
156 
157  // Loop over variables, then over each component in
158  // vector-valued variables, evaluating each.
159  const unsigned int n_vars = _sys.n_vars();
160  for (unsigned int v = 0; v != n_vars; ++v)
161  {
162  const unsigned int n_components =
163  _sys.variable(v).n_components();
164  if (n_components == 1)
165  output(_sys.variable_scalar_number(v,0)) =
166  _fptr(p, *_parameters, _sys.name(), _sys.variable_name(v));
167  else
168  {
169  // Right now our only non-scalar variable type is the
170  // SCALAR variables. The irony is priceless.
171  libmesh_assert_equal_to (_sys.variable(v).type().family, SCALAR);
172 
173  // We pass the point (j,0,0) to an old-style fptr function
174  // pointer to distinguish the different scalars within the
175  // SCALAR variable.
176  for (unsigned int j=0; j != n_components; ++j)
177  output(_sys.variable_scalar_number(v,j)) =
178  _fptr(Point(j,0,0), *_parameters,
179  _sys.name(), _sys.variable_name(v));
180  }
181  }
182 }
183 
184 
189 template <typename Output>
190 inline
191 Output WrappedFunction<Output>::component (unsigned int i,
192  const Point& p,
193  Real /*time*/)
194 {
195  libmesh_assert(_fptr);
196  libmesh_assert(_parameters);
197 
198  // Loop over variables, then over each component in
199  // vector-valued variables.
200  const unsigned int n_vars = _sys.n_vars();
201  for (unsigned int v = 0; v != n_vars; ++v)
202  {
203  const unsigned int n_components =
204  _sys.variable(v).n_components();
205  if (n_components == 1 &&
206  i == _sys.variable_scalar_number(v,0))
207  return _fptr(p, *_parameters, _sys.name(), _sys.variable_name(v));
208  else if (i >= _sys.variable_scalar_number(v,0) &&
209  i <= _sys.variable_scalar_number(v,n_components-1))
210  {
211  // Right now our only non-scalar variable type is the
212  // SCALAR variables. The irony is priceless.
213  libmesh_assert_equal_to (_sys.variable(i).type().family, SCALAR);
214 
215  // We pass the point (j,0,0) to an old-style fptr function
216  // pointer to distinguish the different scalars within the
217  // SCALAR variable.
218  for (unsigned int j=0; j != n_components; ++j)
219  if (i == _sys.variable_scalar_number(v,j))
220  return _fptr(Point(j,0,0), *_parameters,
221  _sys.name(), _sys.variable_name(v));
222  }
223  }
224  libMesh::err << "Component index " << i <<
225  " not found in system " << _sys.name() << std::endl;
226  libmesh_error();
227  return Output();
228 }
229 
230 
231 
232 } // namespace libMesh
233 
234 #endif // LIBMESH_WRAPPED_FUNCTION_H

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:07 UTC

Hosted By:
SourceForge.net Logo