libMesh::ParsedFunction< Output > Class Template Reference

#include <parsed_function.h>

Inheritance diagram for libMesh::ParsedFunction< Output >:

Public Member Functions

 ParsedFunction (const std::string &expression, const std::vector< std::string > *additional_vars=NULL, const std::vector< Output > *initial_vals=NULL)
 
virtual Output operator() (const Point &p, const Real time=0)
 
virtual void operator() (const Point &p, const Real time, DenseVector< Output > &output)
 
virtual Output component (unsigned int i, const Point &p, Real time)
 
virtual Output & getVarAddress (const std::string &variable_name)
 
virtual AutoPtr< FunctionBase
< Output > > 
clone () const
 
 ParsedFunction (std::string)
 
virtual Output operator() (const Point &, const Real=0)
 
virtual void operator() (const Point &, const Real, DenseVector< Output > &)
 
virtual void init ()
 
virtual void clear ()
 
virtual Output & getVarAddress (const std::string &)
 
virtual AutoPtr< FunctionBase
< Output > > 
clone () const
 
void operator() (const Point &p, DenseVector< Output > &output)
 
bool initialized () const
 

Protected Attributes

const FunctionBase_master
 
bool _initialized
 

Private Attributes

std::string _expression
 
std::vector
< FunctionParserBase< Output > > 
parsers
 
std::vector< Output > _spacetime
 
std::vector< std::string > _additional_vars
 
std::vector< Output > _initial_vals
 
Output _dummy
 

Detailed Description

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

Definition at line 43 of file parsed_function.h.

Constructor & Destructor Documentation

template<typename Output = Number>
libMesh::ParsedFunction< Output >::ParsedFunction ( const std::string &  expression,
const std::vector< std::string > *  additional_vars = NULL,
const std::vector< Output > *  initial_vals = NULL 
)
inlineexplicit

Definition at line 47 of file parsed_function.h.

References libMesh::ParsedFunction< Output >::_additional_vars, libMesh::ParsedFunction< Output >::_initial_vals, libMesh::FunctionBase< Output >::_initialized, libMesh::ParsedFunction< Output >::_spacetime, end, libMesh::libmesh_assert(), libMesh::ParsedFunction< Output >::parsers, and libMesh::Real.

Referenced by libMesh::ParsedFunction< Output >::clone().

49  : _expression(expression)
50  // Size the spacetime vector to account for space, time, and any additional
51  // variables passed
52  //_spacetime(LIBMESH_DIM+1 + (additional_vars ? additional_vars->size() : 0)),
53  {
54  std::string variables = "x";
55 #if LIBMESH_DIM > 1
56  variables += ",y";
57 #endif
58 #if LIBMESH_DIM > 2
59  variables += ",z";
60 #endif
61  variables += ",t";
62 
63  _spacetime.resize(LIBMESH_DIM+1 + (additional_vars ? additional_vars->size() : 0));
64 
65  // If additional vars were passed, append them to the string
66  // that we send to the function parser. Also add them to the
67  // end of our spacetime vector
68  if (additional_vars)
69  {
70  if (initial_vals)
71  std::copy(initial_vals->begin(), initial_vals->end(), std::back_inserter(_initial_vals));
72 
73  std::copy(additional_vars->begin(), additional_vars->end(), std::back_inserter(_additional_vars));
74 
75  for (unsigned int i=0; i < additional_vars->size(); ++i)
76  {
77  variables += "," + (*additional_vars)[i];
78  // Initialize extra variables to the vector passed in or zero
79  // Note: The initial_vals vector can be shorter than the additional_vars vector
80  _spacetime[LIBMESH_DIM+1 + i] = (initial_vals && i < initial_vals->size()) ? (*initial_vals)[i] : 0;
81  }
82  }
83 
84  size_t nextstart = 0, end = 0;
85 
86  while (end != std::string::npos)
87  {
88  // If we're past the end of the string, we can't make any more
89  // subparsers
90  if (nextstart >= expression.size())
91  break;
92 
93  // If we're at the start of a brace delimited section, then we
94  // parse just that section:
95  if (expression[nextstart] == '{')
96  {
97  nextstart++;
98  end = expression.find('}', nextstart);
99  }
100  // otherwise we parse the whole thing
101  else
102  end = std::string::npos;
103 
104  // We either want the whole end of the string (end == npos) or
105  // a substring in the middle.
106  std::string subexpression =
107  expression.substr(nextstart, (end == std::string::npos) ?
108  std::string::npos : end - nextstart);
109 
110  // fparser can crash on empty expressions
111  libmesh_assert(!subexpression.empty());
112 
113  // Parse (and optimize if possible) the subexpression.
114  // Add some basic constants, to Real precision.
115  FunctionParserBase<Output> fp;
116  fp.AddConstant("pi", std::acos(Real(-1)));
117  fp.AddConstant("e", std::exp(Real(1)));
118  fp.Parse(subexpression, variables);
119  fp.Optimize();
120  parsers.push_back(fp);
121 
122  // If at end, use nextstart=maxSize. Else start at next
123  // character.
124  nextstart = (end == std::string::npos) ?
125  std::string::npos : end + 1;
126  }
127 
128  this->_initialized = true;
129  }
template<typename Output = Number>
libMesh::ParsedFunction< Output >::ParsedFunction ( std::string  )
inline

Definition at line 243 of file parsed_function.h.

243  : _dummy(0)
244  {
245  libmesh_not_implemented();
246  }

Member Function Documentation

template<typename Output = Number>
virtual void libMesh::ParsedFunction< Output >::clear ( )
inlinevirtual

Clears the function.

Reimplemented from libMesh::FunctionBase< Output >.

Definition at line 257 of file parsed_function.h.

257 {}
template<typename Output = Number>
virtual AutoPtr<FunctionBase<Output> > libMesh::ParsedFunction< Output >::clone ( ) const
inlinevirtual

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 214 of file parsed_function.h.

References libMesh::ParsedFunction< Output >::_additional_vars, libMesh::ParsedFunction< Output >::_expression, libMesh::ParsedFunction< Output >::_initial_vals, and libMesh::ParsedFunction< Output >::ParsedFunction().

214  {
215  return AutoPtr<FunctionBase<Output> >
217  }
template<typename Output = Number>
virtual AutoPtr<FunctionBase<Output> > libMesh::ParsedFunction< Output >::clone ( ) const
inlinevirtual

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 259 of file parsed_function.h.

259  {
260  return AutoPtr<FunctionBase<Output> >
261  (new ParsedFunction<Output>(""));
262  }
template<typename Output = Number>
virtual Output libMesh::ParsedFunction< Output >::component ( unsigned int  i,
const Point p,
Real  time 
)
inlinevirtual
Returns
the vector component i at coordinate p and time time.

Reimplemented from libMesh::FunctionBase< Output >.

Definition at line 175 of file parsed_function.h.

References libMesh::ParsedFunction< Output >::_spacetime, and libMesh::ParsedFunction< Output >::parsers.

178  {
179  _spacetime[0] = p(0);
180 #if LIBMESH_DIM > 1
181  _spacetime[1] = p(1);
182 #endif
183 #if LIBMESH_DIM > 2
184  _spacetime[2] = p(2);
185 #endif
186  _spacetime[LIBMESH_DIM] = time;
187 
188  libmesh_assert_less (i, parsers.size());
189 
190  // The remaining locations in _spacetime are currently fixed at construction
191  // but could potentially be made dynamic
192  return parsers[i].Eval(&_spacetime[0]);
193  }
template<typename Output = Number>
virtual Output& libMesh::ParsedFunction< Output >::getVarAddress ( const std::string &  variable_name)
inlinevirtual
Returns
the address of a parsed variable so you can supply a parameterized value

Definition at line 198 of file parsed_function.h.

References libMesh::ParsedFunction< Output >::_additional_vars, libMesh::ParsedFunction< Output >::_spacetime, and libMesh::err.

199  {
200  const std::vector<std::string>::iterator it =
201  std::find(_additional_vars.begin(), _additional_vars.end(), variable_name);
202 
203  if (it == _additional_vars.end())
204  {
205  libMesh::err << "ERROR: Requested variable not found in parsed function\n" << std::endl;
206  libmesh_error();
207  }
208 
209  // Iterator Arithmetic (How far from the end of the array is our target address?)
210  return _spacetime[_spacetime.size() - (_additional_vars.end() - it)];
211  }
template<typename Output = Number>
virtual Output& libMesh::ParsedFunction< Output >::getVarAddress ( const std::string &  )
inlinevirtual

Definition at line 258 of file parsed_function.h.

References libMesh::ParsedFunction< Output >::_dummy.

258 { return _dummy; }
template<typename Output = Number>
virtual void libMesh::ParsedFunction< Output >::init ( )
inlinevirtual

The actual initialization process.

Reimplemented from libMesh::FunctionBase< Output >.

Definition at line 256 of file parsed_function.h.

256 {}
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 194 of file function_base.h.

195 {
196  return (this->_initialized);
197 }
template<typename Output>
void libMesh::FunctionBase< Output >::operator() ( const Point p,
DenseVector< Output > &  output 
)
inlineinherited

Return function for vectors. Returns in output the values of the data at the coordinate p.

Definition at line 216 of file function_base.h.

218 {
219  // Call the time-dependent function with t=0.
220  this->operator()(p, 0., output);
221 }
template<typename Output = Number>
virtual Output libMesh::ParsedFunction< Output >::operator() ( const Point p,
const Real  time = 0 
)
inlinevirtual
Returns
the scalar value at coordinate p and time time, which defaults to zero. Purely virtual, so you have to overload it. Note that this cannot be a const method, check MeshFunction.

Implements libMesh::FunctionBase< Output >.

Definition at line 131 of file parsed_function.h.

References libMesh::ParsedFunction< Output >::_spacetime, and libMesh::ParsedFunction< Output >::parsers.

133  {
134  _spacetime[0] = p(0);
135 #if LIBMESH_DIM > 1
136  _spacetime[1] = p(1);
137 #endif
138 #if LIBMESH_DIM > 2
139  _spacetime[2] = p(2);
140 #endif
141  _spacetime[LIBMESH_DIM] = time;
142 
143  // The remaining locations in _spacetime are currently fixed at construction
144  // but could potentially be made dynamic
145  return parsers[0].Eval(&_spacetime[0]);
146  }
template<typename Output = Number>
virtual void libMesh::ParsedFunction< Output >::operator() ( const Point p,
const Real  time,
DenseVector< Output > &  output 
)
inlinevirtual

Return function for vectors. Returns in output the values of the data at the coordinate p and for time time. Purely virtual, so you have to overload it. Note that this cannot be a const method, check MeshFunction.

Implements libMesh::FunctionBase< Output >.

Definition at line 148 of file parsed_function.h.

References libMesh::ParsedFunction< Output >::_spacetime, libMesh::ParsedFunction< Output >::parsers, and libMesh::DenseVector< T >::size().

151  {
152  _spacetime[0] = p(0);
153 #if LIBMESH_DIM > 1
154  _spacetime[1] = p(1);
155 #endif
156 #if LIBMESH_DIM > 2
157  _spacetime[2] = p(2);
158 #endif
159  _spacetime[LIBMESH_DIM] = time;
160 
161  unsigned int size = output.size();
162 
163  libmesh_assert_equal_to (size, parsers.size());
164 
165  // The remaining locations in _spacetime are currently fixed at construction
166  // but could potentially be made dynamic
167  for (unsigned int i=0; i != size; ++i)
168  output(i) = parsers[i].Eval(&_spacetime[0]);
169  }
template<typename Output = Number>
virtual Output libMesh::ParsedFunction< Output >::operator() ( const Point p,
const Real  time = 0 
)
inlinevirtual
Returns
the scalar value at coordinate p and time time, which defaults to zero. Purely virtual, so you have to overload it. Note that this cannot be a const method, check MeshFunction.

Implements libMesh::FunctionBase< Output >.

Definition at line 248 of file parsed_function.h.

250  { return 0.; }
template<typename Output = Number>
virtual void libMesh::ParsedFunction< Output >::operator() ( const Point p,
const Real  time,
DenseVector< Output > &  output 
)
inlinevirtual

Return function for vectors. Returns in output the values of the data at the coordinate p and for time time. Purely virtual, so you have to overload it. Note that this cannot be a const method, check MeshFunction.

Implements libMesh::FunctionBase< Output >.

Definition at line 252 of file parsed_function.h.

254  {}

Member Data Documentation

template<typename Output = Number>
std::vector<std::string> libMesh::ParsedFunction< Output >::_additional_vars
private
template<typename Output = Number>
Output libMesh::ParsedFunction< Output >::_dummy
private

Definition at line 264 of file parsed_function.h.

Referenced by libMesh::ParsedFunction< Output >::getVarAddress().

template<typename Output = Number>
std::string libMesh::ParsedFunction< Output >::_expression
private

Definition at line 220 of file parsed_function.h.

Referenced by libMesh::ParsedFunction< Output >::clone().

template<typename Output = Number>
std::vector<Output> libMesh::ParsedFunction< Output >::_initial_vals
private
template<typename Output>
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 166 of file function_base.h.

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

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

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

Definition at line 160 of file function_base.h.

template<typename Output = Number>
std::vector<FunctionParserBase<Output> > libMesh::ParsedFunction< Output >::parsers
private

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

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

Hosted By:
SourceForge.net Logo