libMesh::ParsedFunction< Output, OutputGradient > Class Template Reference

A Function generated (via FParser) by parsing a mathematical expression. More...

#include <parsed_function.h>

Inheritance diagram for libMesh::ParsedFunction< Output, OutputGradient >:

Public Member Functions

 ParsedFunction (const std::string &expression, const std::vector< std::string > *additional_vars=libmesh_nullptr, const std::vector< Output > *initial_vals=libmesh_nullptr)
 
void reparse (const std::string &expression)
 
virtual Output operator() (const Point &p, const Real time=0)
 
virtual bool has_derivatives ()
 
virtual Output dot (const Point &p, const Real time=0)
 
virtual OutputGradient gradient (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)
 
const std::string & expression ()
 
virtual Output & getVarAddress (const std::string &variable_name)
 
virtual UniquePtr< FunctionBase< Output > > clone () const
 
Output get_inline_value (const std::string &inline_var_name) const
 
void set_inline_value (const std::string &inline_var_name, Output newval)
 
 ParsedFunction (const std::string &, const std::vector< std::string > *=libmesh_nullptr, const std::vector< Output > *=libmesh_nullptr)
 
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 UniquePtr< FunctionBase< Output > > clone () const
 
void operator() (const Point &p, DenseVector< Output > &output)
 
bool initialized () const
 
void set_is_time_dependent (bool is_time_dependent)
 
bool is_time_dependent () const
 

Protected Member Functions

void partial_reparse (const std::string &expression)
 
std::size_t find_name (const std::string &varname, const std::string &expr) const
 
bool expression_is_time_dependent (const std::string &expression) const
 

Protected Attributes

const FunctionBase_master
 
bool _initialized
 
bool _is_time_dependent
 

Private Member Functions

void set_spacetime (const Point &p, const Real time=0)
 
Output eval (FunctionParserADBase< Output > &parser, const std::string &libmesh_dbg_var(function_name), unsigned int libmesh_dbg_var(component_idx)) const
 

Private Attributes

std::string _expression
 
std::vector< std::string > _subexpressions
 
std::vector< FunctionParserADBase< Output > > parsers
 
std::vector< Output > _spacetime
 
std::vector< FunctionParserADBase< Output > > dx_parsers
 
std::vector< FunctionParserADBase< Output > > dy_parsers
 
std::vector< FunctionParserADBase< Output > > dz_parsers
 
std::vector< FunctionParserADBase< Output > > dt_parsers
 
bool _valid_derivatives
 
std::string variables
 
std::vector< std::string > _additional_vars
 
std::vector< Output > _initial_vals
 
Output _dummy
 

Detailed Description

template<typename Output = Number, typename OutputGradient = Gradient>
class libMesh::ParsedFunction< Output, OutputGradient >

A Function generated (via FParser) by parsing a mathematical expression.

FEMFunction that returns a single.

Author
Roy Stogner
Date
2012

Definition at line 56 of file parsed_function.h.

Constructor & Destructor Documentation

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

Definition at line 167 of file parsed_function.h.

References libMesh::FunctionBase< Output >::_initialized, and libMesh::ParsedFunction< Output, OutputGradient >::reparse().

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

169  :
170  _expression (), // overridden by parse()
171  // Size the spacetime vector to account for space, time, and any additional
172  // variables passed
173  _spacetime (LIBMESH_DIM+1 + (additional_vars ? additional_vars->size() : 0)),
174  _valid_derivatives (true),
175  _additional_vars (additional_vars ? *additional_vars : std::vector<std::string>()),
176  _initial_vals (initial_vals ? *initial_vals : std::vector<Output>())
177 {
178  // time-dependence established in reparse function
179  this->reparse(expression);
180  this->_initialized = true;
181 }
std::vector< Output > _spacetime
std::vector< std::string > _additional_vars
void reparse(const std::string &expression)
std::vector< Output > _initial_vals
const std::string & expression()
template<typename Output = Number, typename OutputGradient = Gradient>
libMesh::ParsedFunction< Output, OutputGradient >::ParsedFunction ( const std::string &  ,
const std::vector< std::string > *  = libmesh_nullptr,
const std::vector< Output > *  = libmesh_nullptr 
)
inline

Definition at line 675 of file parsed_function.h.

677  : _dummy(0)
678  {
679  libmesh_not_implemented();
680  }

Member Function Documentation

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

Clears the function.

Reimplemented from libMesh::FunctionBase< Output >.

Definition at line 691 of file parsed_function.h.

691 {}
template<typename Output , typename OutputGradient >
UniquePtr< FunctionBase< Output > > libMesh::ParsedFunction< Output, OutputGradient >::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 314 of file parsed_function.h.

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

Referenced by libMesh::ParsedFunction< T >::expression().

315 {
316  return UniquePtr<FunctionBase<Output> >
318 }
ParsedFunction(const std::string &expression, const std::vector< std::string > *additional_vars=libmesh_nullptr, const std::vector< Output > *initial_vals=libmesh_nullptr)
std::vector< std::string > _additional_vars
std::vector< Output > _initial_vals
template<typename Output = Number, typename OutputGradient = Gradient>
virtual UniquePtr<FunctionBase<Output> > libMesh::ParsedFunction< Output, OutputGradient >::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 693 of file parsed_function.h.

694  {
695  return UniquePtr<FunctionBase<Output> > (new ParsedFunction<Output>(""));
696  }
template<typename Output , typename OutputGradient >
Output libMesh::ParsedFunction< Output, OutputGradient >::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 279 of file parsed_function.h.

References libMesh::ParsedFunction< Output, OutputGradient >::eval(), libMesh::ParsedFunction< Output, OutputGradient >::parsers, and libMesh::ParsedFunction< Output, OutputGradient >::set_spacetime().

Referenced by libMesh::ParsedFunction< T >::has_derivatives().

282 {
283  set_spacetime(p, time);
284  libmesh_assert_less (i, parsers.size());
285 
286  // The remaining locations in _spacetime are currently fixed at construction
287  // but could potentially be made dynamic
288  libmesh_assert_less(i, parsers.size());
289  return eval(parsers[i], "f", i);
290 }
Output eval(FunctionParserADBase< Output > &parser, const std::string &libmesh_dbg_var(function_name), unsigned int libmesh_dbg_var(component_idx)) const
std::vector< FunctionParserADBase< Output > > parsers
void set_spacetime(const Point &p, const Real time=0)
template<typename Output , typename OutputGradient >
Output libMesh::ParsedFunction< Output, OutputGradient >::dot ( const Point p,
const Real  time = 0 
)
inlinevirtual

Definition at line 227 of file parsed_function.h.

References libMesh::ParsedFunction< Output, OutputGradient >::dt_parsers, libMesh::ParsedFunction< Output, OutputGradient >::eval(), and libMesh::ParsedFunction< Output, OutputGradient >::set_spacetime().

Referenced by libMesh::ParsedFunction< T >::has_derivatives().

228 {
229  set_spacetime(p, time);
230  return eval(dt_parsers[0], "df/dt", 0);
231 }
std::vector< FunctionParserADBase< Output > > dt_parsers
Output eval(FunctionParserADBase< Output > &parser, const std::string &libmesh_dbg_var(function_name), unsigned int libmesh_dbg_var(component_idx)) const
void set_spacetime(const Point &p, const Real time=0)
template<typename Output, typename OutputGradient >
Output libMesh::ParsedFunction< Output, OutputGradient >::eval ( FunctionParserADBase< Output > &  parser,
const std::string &  libmesh_dbg_varfunction_name,
unsigned int   libmesh_dbg_varcomponent_idx 
) const
inlineprivate

Definition at line 611 of file parsed_function.h.

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

Referenced by libMesh::ParsedFunction< Output, OutputGradient >::component(), libMesh::ParsedFunction< Output, OutputGradient >::dot(), libMesh::ParsedFunction< T >::expression(), libMesh::ParsedFunction< Output, OutputGradient >::get_inline_value(), libMesh::ParsedFunction< Output, OutputGradient >::gradient(), and libMesh::ParsedFunction< Output, OutputGradient >::operator()().

614 {
615 #ifndef NDEBUG
616  Output result = parser.Eval(&_spacetime[0]);
617  int error_code = parser.EvalError();
618  if (error_code)
619  {
620  libMesh::err << "ERROR: FunctionParser is unable to evaluate component "
621  << component_idx
622  << " of expression '"
623  << function_name
624  << "' with arguments:\n";
625  for (std::size_t j=0; j<_spacetime.size(); ++j)
626  libMesh::err << '\t' << _spacetime[j] << '\n';
627  libMesh::err << '\n';
628 
629  // Currently no API to report error messages, we'll do it manually
630  std::string error_message = "Reason: ";
631 
632  switch (error_code)
633  {
634  case 1:
635  error_message += "Division by zero";
636  break;
637  case 2:
638  error_message += "Square Root error (negative value)";
639  break;
640  case 3:
641  error_message += "Log error (negative value)";
642  break;
643  case 4:
644  error_message += "Trigonometric error (asin or acos of illegal value)";
645  break;
646  case 5:
647  error_message += "Maximum recursion level reached";
648  break;
649  default:
650  error_message += "Unknown";
651  break;
652  }
653  libmesh_error_msg(error_message);
654  }
655 
656  return result;
657 #else
658  return parser.Eval(&_spacetime[0]);
659 #endif
660 }
std::vector< Output > _spacetime
OStreamProxy err(std::cerr)
template<typename Output = Number, typename OutputGradient = Gradient>
const std::string& libMesh::ParsedFunction< Output, OutputGradient >::expression ( )
inline
template<typename Output , typename OutputGradient >
bool libMesh::ParsedFunction< Output, OutputGradient >::expression_is_time_dependent ( const std::string &  expression) const
inlineprotected

Definition at line 575 of file parsed_function.h.

References libMesh::ParsedFunction< Output, OutputGradient >::find_name(), and libMesh::FunctionBase< Output >::is_time_dependent().

Referenced by libMesh::ParsedFunction< T >::expression(), and libMesh::ParsedFunction< Output, OutputGradient >::reparse().

576 {
577  bool is_time_dependent = false;
578 
579  // By definition, time is "t" for FunctionBase-based objects, so we just need to
580  // see if this expression has the variable "t" in it.
581  if( this->find_name( std::string("t"), expression ) != std::string::npos )
582  is_time_dependent = true;
583 
584  return is_time_dependent;
585 }
bool is_time_dependent() const
std::size_t find_name(const std::string &varname, const std::string &expr) const
const std::string & expression()
template<typename Output , typename OutputGradient >
std::size_t libMesh::ParsedFunction< Output, OutputGradient >::find_name ( const std::string &  varname,
const std::string &  expr 
) const
inlineprotected

Definition at line 553 of file parsed_function.h.

Referenced by libMesh::ParsedFunction< T >::expression(), libMesh::ParsedFunction< Output, OutputGradient >::expression_is_time_dependent(), libMesh::ParsedFunction< Output, OutputGradient >::get_inline_value(), and libMesh::ParsedFunction< Output, OutputGradient >::set_inline_value().

555 {
556  const std::size_t namesize = varname.size();
557  std::size_t varname_i = expr.find(varname);
558 
559  while ((varname_i != std::string::npos) &&
560  (((varname_i > 0) &&
561  (std::isalnum(expr[varname_i-1]) ||
562  (expr[varname_i-1] == '_'))) ||
563  ((varname_i+namesize < expr.size()) &&
564  (std::isalnum(expr[varname_i+namesize]) ||
565  (expr[varname_i+namesize] == '_')))))
566  {
567  varname_i = expr.find(varname, varname_i+1);
568  }
569 
570  return varname_i;
571 }
template<typename Output , typename OutputGradient >
Output libMesh::ParsedFunction< Output, OutputGradient >::get_inline_value ( const std::string &  inline_var_name) const
inline
Returns
the value of an inline variable. Will only be correct if the inline variable value is independent of input variables, if the inline variable is not redefined within any subexpression, and if the inline variable takes the same value within any subexpressions where it appears.

Definition at line 323 of file parsed_function.h.

References libMesh::ParsedFunction< Output, OutputGradient >::_subexpressions, libMesh::ParsedFunction< Output, OutputGradient >::eval(), libMesh::ParsedFunction< Output, OutputGradient >::find_name(), libMesh::libmesh_assert(), libMesh::Real, and libMesh::ParsedFunction< Output, OutputGradient >::variables.

Referenced by libMesh::ParsedFunction< T >::expression(), and libMesh::ParsedFunctionParameter< T >::get().

324 {
325  libmesh_assert_greater (_subexpressions.size(), 0);
326 
327 #ifndef NDEBUG
328  bool found_var_name = false;
329 #endif
330  Output old_var_value(0.);
331 
332  for (std::size_t s=0; s != _subexpressions.size(); ++s)
333  {
334  const std::string & subexpression = _subexpressions[s];
335  const std::size_t varname_i =
336  find_name(inline_var_name, subexpression);
337  if (varname_i == std::string::npos)
338  continue;
339 
340  const std::size_t assignment_i =
341  subexpression.find(":", varname_i+1);
342 
343  libmesh_assert_not_equal_to(assignment_i, std::string::npos);
344 
345  libmesh_assert_equal_to(subexpression[assignment_i+1], '=');
346  for (unsigned int i = varname_i+1; i != assignment_i; ++i)
347  libmesh_assert_equal_to(subexpression[i], ' ');
348 
349  std::size_t end_assignment_i =
350  subexpression.find(";", assignment_i+1);
351 
352  libmesh_assert_not_equal_to(end_assignment_i, std::string::npos);
353 
354  std::string new_subexpression =
355  subexpression.substr(0, end_assignment_i+1) +
356  inline_var_name;
357 
358 #ifdef LIBMESH_HAVE_FPARSER
359  // Parse and evaluate the new subexpression.
360  // Add the same constants as we used originally.
361  FunctionParserADBase<Output> fp;
362  fp.AddConstant("NaN", std::numeric_limits<Real>::quiet_NaN());
363  fp.AddConstant("pi", std::acos(Real(-1)));
364  fp.AddConstant("e", std::exp(Real(1)));
365  if (fp.Parse(new_subexpression, variables) != -1) // -1 for success
366  libmesh_error_msg
367  ("ERROR: FunctionParser is unable to parse modified expression: "
368  << new_subexpression << '\n' << fp.ErrorMsg());
369 
370  Output new_var_value = this->eval(fp, new_subexpression, 0);
371 #ifdef NDEBUG
372  return new_var_value;
373 #else
374  if (found_var_name)
375  {
376  libmesh_assert_equal_to(old_var_value, new_var_value);
377  }
378  else
379  {
380  old_var_value = new_var_value;
381  found_var_name = true;
382  }
383 #endif
384 
385 #else
386  libmesh_error_msg("ERROR: This functionality requires fparser!");
387 #endif
388  }
389 
390  libmesh_assert(found_var_name);
391  return old_var_value;
392 }
Output eval(FunctionParserADBase< Output > &parser, const std::string &libmesh_dbg_var(function_name), unsigned int libmesh_dbg_var(component_idx)) const
std::vector< std::string > _subexpressions
libmesh_assert(j)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::size_t find_name(const std::string &varname, const std::string &expr) const
template<typename Output , typename OutputGradient >
Output & libMesh::ParsedFunction< Output, OutputGradient >::getVarAddress ( const std::string &  variable_name)
inlinevirtual
Returns
the address of a parsed variable so you can supply a parameterized value

Definition at line 298 of file parsed_function.h.

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

Referenced by libMesh::ParsedFunction< T >::expression().

299 {
300  const std::vector<std::string>::iterator it =
301  std::find(_additional_vars.begin(), _additional_vars.end(), variable_name);
302 
303  if (it == _additional_vars.end())
304  libmesh_error_msg("ERROR: Requested variable not found in parsed function");
305 
306  // Iterator Arithmetic (How far from the end of the array is our target address?)
307  return _spacetime[_spacetime.size() - (_additional_vars.end() - it)];
308 }
std::vector< Output > _spacetime
std::vector< std::string > _additional_vars
template<typename Output = Number, typename OutputGradient = Gradient>
virtual Output& libMesh::ParsedFunction< Output, OutputGradient >::getVarAddress ( const std::string &  )
inlinevirtual

Definition at line 692 of file parsed_function.h.

692 { return _dummy; }
template<typename Output , typename OutputGradient >
OutputGradient libMesh::ParsedFunction< Output, OutputGradient >::gradient ( const Point p,
const Real  time = 0 
)
inlinevirtual

Definition at line 236 of file parsed_function.h.

References libMesh::ParsedFunction< Output, OutputGradient >::dx_parsers, libMesh::ParsedFunction< Output, OutputGradient >::dy_parsers, libMesh::ParsedFunction< Output, OutputGradient >::dz_parsers, libMesh::ParsedFunction< Output, OutputGradient >::eval(), and libMesh::ParsedFunction< Output, OutputGradient >::set_spacetime().

Referenced by libMesh::ParsedFunction< T >::has_derivatives().

237 {
238  OutputGradient grad;
239  set_spacetime(p, time);
240 
241  grad(0) = eval(dx_parsers[0], "df/dx", 0);
242 #if LIBMESH_DIM > 1
243  grad(1) = eval(dy_parsers[0], "df/dy", 0);
244 #endif
245 #if LIBMESH_DIM > 2
246  grad(2) = eval(dz_parsers[0], "df/dz", 0);
247 #endif
248 
249  return grad;
250 }
Output eval(FunctionParserADBase< Output > &parser, const std::string &libmesh_dbg_var(function_name), unsigned int libmesh_dbg_var(component_idx)) const
void set_spacetime(const Point &p, const Real time=0)
std::vector< FunctionParserADBase< Output > > dz_parsers
std::vector< FunctionParserADBase< Output > > dx_parsers
std::vector< FunctionParserADBase< Output > > dy_parsers
template<typename Output = Number, typename OutputGradient = Gradient>
virtual bool libMesh::ParsedFunction< Output, OutputGradient >::has_derivatives ( )
inlinevirtual

Definition at line 71 of file parsed_function.h.

template<typename Output = Number, typename OutputGradient = Gradient>
virtual void libMesh::ParsedFunction< Output, OutputGradient >::init ( )
inlinevirtual

The actual initialization process.

Reimplemented from libMesh::FunctionBase< Output >.

Definition at line 690 of file parsed_function.h.

690 {}
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 212 of file function_base.h.

References libMesh::FunctionBase< Output >::_initialized.

Referenced by libMesh::FunctionBase< Real >::clear(), and libMesh::AnalyticFunction< Output >::operator()().

213 {
214  return (this->_initialized);
215 }
template<typename Output >
bool libMesh::FunctionBase< Output >::is_time_dependent ( ) const
inlineinherited
template<typename Output , typename OutputGradient >
Output libMesh::ParsedFunction< Output, OutputGradient >::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 218 of file parsed_function.h.

References libMesh::ParsedFunction< Output, OutputGradient >::eval(), libMesh::ParsedFunction< Output, OutputGradient >::parsers, and libMesh::ParsedFunction< Output, OutputGradient >::set_spacetime().

Referenced by libMesh::ParsedFunction< T >::has_derivatives().

219 {
220  set_spacetime(p, time);
221  return eval(parsers[0], "f", 0);
222 }
Output eval(FunctionParserADBase< Output > &parser, const std::string &libmesh_dbg_var(function_name), unsigned int libmesh_dbg_var(component_idx)) const
std::vector< FunctionParserADBase< Output > > parsers
void set_spacetime(const Point &p, const Real time=0)
template<typename Output, typename OutputGradient >
void libMesh::ParsedFunction< Output, OutputGradient >::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. Can optionally provide subdomain_ids which will restrict the function to operate on elements with subdomain id contained in the set. This is useful in cases where there are multiple dimensioned elements, for example.

Implements libMesh::FunctionBase< Output >.

Definition at line 256 of file parsed_function.h.

References libMesh::ParsedFunction< Output, OutputGradient >::eval(), libMesh::ParsedFunction< Output, OutputGradient >::parsers, and libMesh::ParsedFunction< Output, OutputGradient >::set_spacetime().

259 {
260  set_spacetime(p, time);
261 
262  unsigned int size = output.size();
263 
264  libmesh_assert_equal_to (size, parsers.size());
265 
266  // The remaining locations in _spacetime are currently fixed at construction
267  // but could potentially be made dynamic
268  for (unsigned int i=0; i != size; ++i)
269  output(i) = eval(parsers[i], "f", i);
270 }
Output eval(FunctionParserADBase< Output > &parser, const std::string &libmesh_dbg_var(function_name), unsigned int libmesh_dbg_var(component_idx)) const
std::vector< FunctionParserADBase< Output > > parsers
void set_spacetime(const Point &p, const Real time=0)
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 247 of file function_base.h.

References libMesh::FunctionBase< Output >::operator()().

249 {
250  // Call the time-dependent function with t=0.
251  this->operator()(p, 0., output);
252 }
virtual Output operator()(const Point &p, const Real time=0.)=0
template<typename Output = Number, typename OutputGradient = Gradient>
virtual Output libMesh::ParsedFunction< Output, OutputGradient >::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 682 of file parsed_function.h.

684  { return 0.; }
template<typename Output = Number, typename OutputGradient = Gradient>
virtual void libMesh::ParsedFunction< Output, OutputGradient >::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. Can optionally provide subdomain_ids which will restrict the function to operate on elements with subdomain id contained in the set. This is useful in cases where there are multiple dimensioned elements, for example.

Implements libMesh::FunctionBase< Output >.

Definition at line 686 of file parsed_function.h.

688  {}
template<typename Output , typename OutputGradient >
void libMesh::ParsedFunction< Output, OutputGradient >::partial_reparse ( const std::string &  expression)
inlineprotected

Definition at line 463 of file parsed_function.h.

References libMesh::ParsedFunction< Output, OutputGradient >::_expression, libMesh::ParsedFunction< Output, OutputGradient >::_subexpressions, libMesh::ParsedFunction< Output, OutputGradient >::_valid_derivatives, libMesh::ParsedFunction< Output, OutputGradient >::dt_parsers, libMesh::ParsedFunction< Output, OutputGradient >::dx_parsers, libMesh::ParsedFunction< Output, OutputGradient >::dy_parsers, libMesh::ParsedFunction< Output, OutputGradient >::dz_parsers, end, libMesh::ParsedFunction< Output, OutputGradient >::expression(), libMesh::ParsedFunction< Output, OutputGradient >::parsers, libMesh::Real, and libMesh::ParsedFunction< Output, OutputGradient >::variables.

Referenced by libMesh::ParsedFunction< T >::expression(), libMesh::ParsedFunction< Output, OutputGradient >::reparse(), and libMesh::ParsedFunction< Output, OutputGradient >::set_inline_value().

464 {
466  _subexpressions.clear();
467  parsers.clear();
468 
469  size_t nextstart = 0, end = 0;
470 
471  while (end != std::string::npos)
472  {
473  // If we're past the end of the string, we can't make any more
474  // subparsers
475  if (nextstart >= expression.size())
476  break;
477 
478  // If we're at the start of a brace delimited section, then we
479  // parse just that section:
480  if (expression[nextstart] == '{')
481  {
482  nextstart++;
483  end = expression.find('}', nextstart);
484  }
485  // otherwise we parse the whole thing
486  else
487  end = std::string::npos;
488 
489  // We either want the whole end of the string (end == npos) or
490  // a substring in the middle.
491  _subexpressions.push_back
492  (expression.substr(nextstart, (end == std::string::npos) ?
493  std::string::npos : end - nextstart));
494 
495  // fparser can crash on empty expressions
496  if (_subexpressions.back().empty())
497  libmesh_error_msg("ERROR: FunctionParser is unable to parse empty expression.\n");
498 
499  // Parse (and optimize if possible) the subexpression.
500  // Add some basic constants, to Real precision.
501  FunctionParserADBase<Output> fp;
502  fp.AddConstant("NaN", std::numeric_limits<Real>::quiet_NaN());
503  fp.AddConstant("pi", std::acos(Real(-1)));
504  fp.AddConstant("e", std::exp(Real(1)));
505  if (fp.Parse(_subexpressions.back(), variables) != -1) // -1 for success
506  libmesh_error_msg
507  ("ERROR: FunctionParser is unable to parse expression: "
508  << _subexpressions.back() << '\n' << fp.ErrorMsg());
509 
510  // use of derivatives is optional. suppress error output on the console
511  // use the has_derivatives() method to check if AutoDiff was successful.
512  // also enable immediate optimization
513  fp.SetADFlags(FunctionParserADBase<Output>::ADSilenceErrors |
514  FunctionParserADBase<Output>::ADAutoOptimize);
515 
516  // optimize original function
517  fp.Optimize();
518  parsers.push_back(fp);
519 
520  // generate derivatives through automatic differentiation
521  FunctionParserADBase<Output> dx_fp(fp);
522  if (dx_fp.AutoDiff("x") != -1) // -1 for success
523  _valid_derivatives = false;
524  dx_parsers.push_back(dx_fp);
525 #if LIBMESH_DIM > 1
526  FunctionParserADBase<Output> dy_fp(fp);
527  if (dy_fp.AutoDiff("y") != -1) // -1 for success
528  _valid_derivatives = false;
529  dy_parsers.push_back(dy_fp);
530 #endif
531 #if LIBMESH_DIM > 2
532  FunctionParserADBase<Output> dz_fp(fp);
533  if (dz_fp.AutoDiff("z") != -1) // -1 for success
534  _valid_derivatives = false;
535  dz_parsers.push_back(dz_fp);
536 #endif
537  FunctionParserADBase<Output> dt_fp(fp);
538  if (dt_fp.AutoDiff("t") != -1) // -1 for success
539  _valid_derivatives = false;
540  dt_parsers.push_back(dt_fp);
541 
542  // If at end, use nextstart=maxSize. Else start at next
543  // character.
544  nextstart = (end == std::string::npos) ?
545  std::string::npos : end + 1;
546  }
547 }
std::vector< FunctionParserADBase< Output > > dt_parsers
std::vector< FunctionParserADBase< Output > > parsers
std::vector< std::string > _subexpressions
IterBase * end
std::vector< FunctionParserADBase< Output > > dz_parsers
std::vector< FunctionParserADBase< Output > > dx_parsers
std::vector< FunctionParserADBase< Output > > dy_parsers
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const std::string & expression()
template<typename Output , typename OutputGradient >
void libMesh::ParsedFunction< Output, OutputGradient >::reparse ( const std::string &  expression)
inline

Definition at line 187 of file parsed_function.h.

References libMesh::ParsedFunction< Output, OutputGradient >::_additional_vars, libMesh::ParsedFunction< Output, OutputGradient >::_initial_vals, libMesh::FunctionBase< Output >::_is_time_dependent, libMesh::ParsedFunction< Output, OutputGradient >::_spacetime, libMesh::ParsedFunction< Output, OutputGradient >::expression_is_time_dependent(), libMesh::ParsedFunction< Output, OutputGradient >::partial_reparse(), and libMesh::ParsedFunction< Output, OutputGradient >::variables.

Referenced by libMesh::ParsedFunction< Output, OutputGradient >::ParsedFunction().

188 {
189  variables = "x";
190 #if LIBMESH_DIM > 1
191  variables += ",y";
192 #endif
193 #if LIBMESH_DIM > 2
194  variables += ",z";
195 #endif
196  variables += ",t";
197 
198  // If additional vars were passed, append them to the string
199  // that we send to the function parser. Also add them to the
200  // end of our spacetime vector
201  for (std::size_t i=0; i < _additional_vars.size(); ++i)
202  {
203  variables += "," + _additional_vars[i];
204  // Initialize extra variables to the vector passed in or zero
205  // Note: The initial_vals vector can be shorter than the additional_vars vector
206  _spacetime[LIBMESH_DIM+1 + i] =
207  (i < _initial_vals.size()) ? _initial_vals[i] : 0;
208  }
209 
211 
213 }
std::vector< Output > _spacetime
std::vector< std::string > _additional_vars
bool expression_is_time_dependent(const std::string &expression) const
void partial_reparse(const std::string &expression)
std::vector< Output > _initial_vals
const std::string & expression()
template<typename Output, typename OutputGradient >
void libMesh::ParsedFunction< Output, OutputGradient >::set_inline_value ( const std::string &  inline_var_name,
Output  newval 
)
inline

Changes the value of an inline variable. Forever after the variable value will take the given constant, independent of input variables, in every subexpression where it is already defined. Currently only works if the inline variable is not redefined within any one subexpression.

Definition at line 398 of file parsed_function.h.

References libMesh::ParsedFunction< Output, OutputGradient >::_subexpressions, libMesh::ParsedFunction< Output, OutputGradient >::find_name(), libMesh::libmesh_assert(), and libMesh::ParsedFunction< Output, OutputGradient >::partial_reparse().

Referenced by libMesh::ParsedFunction< T >::expression(), and libMesh::ParsedFunctionParameter< T >::set().

400 {
401  libmesh_assert_greater (_subexpressions.size(), 0);
402 
403 #ifndef NDEBUG
404  bool found_var_name = false;
405 #endif
406  for (std::size_t s=0; s != _subexpressions.size(); ++s)
407  {
408  const std::string & subexpression = _subexpressions[s];
409  const std::size_t varname_i =
410  find_name(inline_var_name, subexpression);
411  if (varname_i == std::string::npos)
412  continue;
413 
414 #ifndef NDEBUG
415  found_var_name = true;
416 #endif
417  const std::size_t assignment_i =
418  subexpression.find(":", varname_i+1);
419 
420  libmesh_assert_not_equal_to(assignment_i, std::string::npos);
421 
422  libmesh_assert_equal_to(subexpression[assignment_i+1], '=');
423  for (unsigned int i = varname_i+1; i != assignment_i; ++i)
424  libmesh_assert_equal_to(subexpression[i], ' ');
425 
426  std::size_t end_assignment_i =
427  subexpression.find(";", assignment_i+1);
428 
429  libmesh_assert_not_equal_to(end_assignment_i, std::string::npos);
430 
431  std::ostringstream new_subexpression;
432  new_subexpression << subexpression.substr(0, assignment_i+2)
433  << std::setprecision(std::numeric_limits<Output>::digits10+2)
434 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
435  << '(' << newval.real() << '+'
436  << newval.imag() << 'i' << ')'
437 #else
438  << newval
439 #endif
440  << subexpression.substr(end_assignment_i,
441  std::string::npos);
442  _subexpressions[s] = new_subexpression.str();
443  }
444 
445  libmesh_assert(found_var_name);
446 
447  std::string new_expression;
448 
449  for (std::size_t s=0; s != _subexpressions.size(); ++s)
450  {
451  new_expression += '{';
452  new_expression += _subexpressions[s];
453  new_expression += '}';
454  }
455 
456  this->partial_reparse(new_expression);
457 }
std::vector< std::string > _subexpressions
libmesh_assert(j)
void partial_reparse(const std::string &expression)
std::size_t find_name(const std::string &varname, const std::string &expr) const
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 219 of file function_base.h.

References libMesh::FunctionBase< Output >::_is_time_dependent, and libMesh::FunctionBase< Output >::is_time_dependent().

Referenced by libMesh::FunctionBase< Real >::clear().

220 {
222 }
bool is_time_dependent() const
template<typename Output , typename OutputGradient >
void libMesh::ParsedFunction< Output, OutputGradient >::set_spacetime ( const Point p,
const Real  time = 0 
)
inlineprivate

Definition at line 591 of file parsed_function.h.

References libMesh::ParsedFunction< Output, OutputGradient >::_spacetime.

Referenced by libMesh::ParsedFunction< Output, OutputGradient >::component(), libMesh::ParsedFunction< Output, OutputGradient >::dot(), libMesh::ParsedFunction< T >::expression(), libMesh::ParsedFunction< Output, OutputGradient >::gradient(), and libMesh::ParsedFunction< Output, OutputGradient >::operator()().

593 {
594  _spacetime[0] = p(0);
595 #if LIBMESH_DIM > 1
596  _spacetime[1] = p(1);
597 #endif
598 #if LIBMESH_DIM > 2
599  _spacetime[2] = p(2);
600 #endif
601  _spacetime[LIBMESH_DIM] = time;
602 
603  // The remaining locations in _spacetime are currently fixed at construction
604  // but could potentially be made dynamic
605 }
std::vector< Output > _spacetime

Member Data Documentation

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

Definition at line 698 of file parsed_function.h.

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

template<typename Output = Number, typename OutputGradient = Gradient>
std::string libMesh::ParsedFunction< Output, OutputGradient >::_expression
private
template<typename Output = Number, typename OutputGradient = Gradient>
std::vector<Output> libMesh::ParsedFunction< Output, OutputGradient >::_initial_vals
private
template<typename Output = Number>
bool libMesh::FunctionBase< Output >::_is_time_dependent
protectedinherited
template<typename Output = Number>
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 172 of file function_base.h.

template<typename Output = Number, typename OutputGradient = Gradient>
std::vector<Output> libMesh::ParsedFunction< Output, OutputGradient >::_spacetime
private
template<typename Output = Number, typename OutputGradient = Gradient>
std::vector<std::string> libMesh::ParsedFunction< Output, OutputGradient >::_subexpressions
private
template<typename Output = Number, typename OutputGradient = Gradient>
bool libMesh::ParsedFunction< Output, OutputGradient >::_valid_derivatives
private
template<typename Output = Number, typename OutputGradient = Gradient>
std::vector<FunctionParserADBase<Output> > libMesh::ParsedFunction< Output, OutputGradient >::dt_parsers
private
template<typename Output = Number, typename OutputGradient = Gradient>
std::vector<FunctionParserADBase<Output> > libMesh::ParsedFunction< Output, OutputGradient >::dx_parsers
private
template<typename Output = Number, typename OutputGradient = Gradient>
std::vector<FunctionParserADBase<Output> > libMesh::ParsedFunction< Output, OutputGradient >::dy_parsers
private
template<typename Output = Number, typename OutputGradient = Gradient>
std::vector<FunctionParserADBase<Output> > libMesh::ParsedFunction< Output, OutputGradient >::dz_parsers
private
template<typename Output = Number, typename OutputGradient = Gradient>
std::vector<FunctionParserADBase<Output> > libMesh::ParsedFunction< Output, OutputGradient >::parsers
private
template<typename Output = Number, typename OutputGradient = Gradient>
std::string libMesh::ParsedFunction< Output, OutputGradient >::variables
private

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