libMesh
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
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 >:
[legend]

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)
 Re-parse with new expression. More...
 
virtual Output operator() (const Point &p, const Real time=0) libmesh_override
 
virtual bool has_derivatives ()
 Query if the automatic derivative generation was successful. More...
 
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) libmesh_override
 Evaluation function for time-dependent vector-valued functions. More...
 
virtual Output component (unsigned int i, const Point &p, Real time) libmesh_override
 
const std::string & expression ()
 
virtual Output & getVarAddress (const std::string &variable_name)
 
virtual UniquePtr< FunctionBase< Output > > clone () const libmesh_override
 
Output get_inline_value (const std::string &inline_var_name) const
 
void set_inline_value (const std::string &inline_var_name, Output newval)
 Changes the value of an inline variable. More...
 
 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 > &)
 Evaluation function for time-dependent vector-valued functions. More...
 
virtual void init ()
 The actual initialization process. More...
 
virtual void clear ()
 Clears the function. More...
 
virtual Output & getVarAddress (const std::string &)
 
virtual UniquePtr< FunctionBase< Output > > clone () const
 
void operator() (const Point &p, DenseVector< Output > &output)
 Evaluation function for time-independent vector-valued functions. More...
 
bool initialized () const
 
void set_is_time_dependent (bool is_time_dependent)
 Function to set whether this is a time-dependent function or not. More...
 
bool is_time_dependent () const
 

Protected Member Functions

void partial_reparse (const std::string &expression)
 Re-parse with minor changes to expression. More...
 
std::size_t find_name (const std::string &varname, const std::string &expr) const
 Helper function for parsing out variable names. More...
 
bool expression_is_time_dependent (const std::string &expression) const
 

Protected Attributes

const FunctionBase_master
 Const pointer to our master, initialized to NULL. More...
 
bool _initialized
 When init() was called so that everything is ready for calls to operator() (...), then this bool is true. More...
 
bool _is_time_dependent
 Cache whether or not this function is actually time-dependent. More...
 

Private Member Functions

void set_spacetime (const Point &p, const Real time=0)
 Set the _spacetime argument vector. More...
 
Output eval (FunctionParserADBase< Output > &parser, const std::string &libmesh_dbg_var(function_name), unsigned int libmesh_dbg_var(component_idx)) const
 Evaluate the ith FunctionParser and check the result. More...
 

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.

All overridden virtual functions are documented in function_base.h.

Author
Roy Stogner
Date
2012 A Function defined by a std::string.

Definition at line 58 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 
)
explicit

Definition at line 183 of file parsed_function.h.

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

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

185  :
186  _expression (), // overridden by parse()
187  // Size the spacetime vector to account for space, time, and any additional
188  // variables passed
189  _spacetime (LIBMESH_DIM+1 + (additional_vars ? additional_vars->size() : 0)),
190  _valid_derivatives (true),
191  _additional_vars (additional_vars ? *additional_vars : std::vector<std::string>()),
192  _initial_vals (initial_vals ? *initial_vals : std::vector<Output>())
193 {
194  // time-dependence established in reparse function
195  this->reparse(expression);
196  this->_initialized = true;
197 }
std::vector< Output > _spacetime
std::vector< std::string > _additional_vars
bool _initialized
When init() was called so that everything is ready for calls to operator() (...), then this bool is t...
void reparse(const std::string &expression)
Re-parse with new 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 
)

Definition at line 691 of file parsed_function.h.

693  : _dummy(0)
694  {
695  libmesh_not_implemented();
696  }

Member Function Documentation

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

Clears the function.

Reimplemented from libMesh::FunctionBase< Output >.

Definition at line 707 of file parsed_function.h.

707 {}
template<typename Output , typename OutputGradient >
UniquePtr< FunctionBase< Output > > libMesh::ParsedFunction< Output, OutputGradient >::clone ( ) const
virtual
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 330 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().

331 {
332  return UniquePtr<FunctionBase<Output>>
334 }
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
virtual
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 709 of file parsed_function.h.

710  {
711  return UniquePtr<FunctionBase<Output>> (new ParsedFunction<Output>(""));
712  }
template<typename Output , typename OutputGradient >
Output libMesh::ParsedFunction< Output, OutputGradient >::component ( unsigned int  i,
const Point p,
Real  time 
)
virtual
Returns
The vector component i at coordinate p and time time.

Reimplemented from libMesh::FunctionBase< Output >.

Definition at line 295 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().

298 {
299  set_spacetime(p, time);
300  libmesh_assert_less (i, parsers.size());
301 
302  // The remaining locations in _spacetime are currently fixed at construction
303  // but could potentially be made dynamic
304  libmesh_assert_less(i, parsers.size());
305  return eval(parsers[i], "f", i);
306 }
Output eval(FunctionParserADBase< Output > &parser, const std::string &libmesh_dbg_var(function_name), unsigned int libmesh_dbg_var(component_idx)) const
Evaluate the ith FunctionParser and check the result.
std::vector< FunctionParserADBase< Output > > parsers
void set_spacetime(const Point &p, const Real time=0)
Set the _spacetime argument vector.
template<typename Output , typename OutputGradient >
Output libMesh::ParsedFunction< Output, OutputGradient >::dot ( const Point p,
const Real  time = 0 
)
virtual

Definition at line 243 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().

244 {
245  set_spacetime(p, time);
246  return eval(dt_parsers[0], "df/dt", 0);
247 }
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
Evaluate the ith FunctionParser and check the result.
void set_spacetime(const Point &p, const Real time=0)
Set the _spacetime argument vector.
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
private

Evaluate the ith FunctionParser and check the result.

Definition at line 627 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()().

630 {
631 #ifndef NDEBUG
632  Output result = parser.Eval(&_spacetime[0]);
633  int error_code = parser.EvalError();
634  if (error_code)
635  {
636  libMesh::err << "ERROR: FunctionParser is unable to evaluate component "
637  << component_idx
638  << " of expression '"
639  << function_name
640  << "' with arguments:\n";
641  for (std::size_t j=0; j<_spacetime.size(); ++j)
642  libMesh::err << '\t' << _spacetime[j] << '\n';
643  libMesh::err << '\n';
644 
645  // Currently no API to report error messages, we'll do it manually
646  std::string error_message = "Reason: ";
647 
648  switch (error_code)
649  {
650  case 1:
651  error_message += "Division by zero";
652  break;
653  case 2:
654  error_message += "Square Root error (negative value)";
655  break;
656  case 3:
657  error_message += "Log error (negative value)";
658  break;
659  case 4:
660  error_message += "Trigonometric error (asin or acos of illegal value)";
661  break;
662  case 5:
663  error_message += "Maximum recursion level reached";
664  break;
665  default:
666  error_message += "Unknown";
667  break;
668  }
669  libmesh_error_msg(error_message);
670  }
671 
672  return result;
673 #else
674  return parser.Eval(&_spacetime[0]);
675 #endif
676 }
OStreamProxy err
std::vector< Output > _spacetime
template<typename Output = Number, typename OutputGradient = Gradient>
const std::string& libMesh::ParsedFunction< Output, OutputGradient >::expression ( )
template<typename Output , typename OutputGradient >
bool libMesh::ParsedFunction< Output, OutputGradient >::expression_is_time_dependent ( const std::string &  expression) const
protected
Returns
true if the expression is time-dependent, false otherwise.

Definition at line 591 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().

592 {
593  bool is_time_dependent = false;
594 
595  // By definition, time is "t" for FunctionBase-based objects, so we just need to
596  // see if this expression has the variable "t" in it.
597  if (this->find_name(std::string("t"), expression) != std::string::npos)
598  is_time_dependent = true;
599 
600  return is_time_dependent;
601 }
bool is_time_dependent() const
std::size_t find_name(const std::string &varname, const std::string &expr) const
Helper function for parsing out variable names.
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
protected

Helper function for parsing out variable names.

Definition at line 569 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().

571 {
572  const std::size_t namesize = varname.size();
573  std::size_t varname_i = expr.find(varname);
574 
575  while ((varname_i != std::string::npos) &&
576  (((varname_i > 0) &&
577  (std::isalnum(expr[varname_i-1]) ||
578  (expr[varname_i-1] == '_'))) ||
579  ((varname_i+namesize < expr.size()) &&
580  (std::isalnum(expr[varname_i+namesize]) ||
581  (expr[varname_i+namesize] == '_')))))
582  {
583  varname_i = expr.find(varname, varname_i+1);
584  }
585 
586  return varname_i;
587 }
template<typename Output , typename OutputGradient >
Output libMesh::ParsedFunction< Output, OutputGradient >::get_inline_value ( const std::string &  inline_var_name) const
Returns
The value of an inline variable.
Note
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 339 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(), libMesh::ParsedFunctionParameter< T >::get(), ParsedFunctionTest::testInlineGetter(), and ParsedFunctionTest::testInlineSetter().

340 {
341  libmesh_assert_greater (_subexpressions.size(), 0);
342 
343 #ifndef NDEBUG
344  bool found_var_name = false;
345 #endif
346  Output old_var_value(0.);
347 
348  for (std::size_t s=0; s != _subexpressions.size(); ++s)
349  {
350  const std::string & subexpression = _subexpressions[s];
351  const std::size_t varname_i =
352  find_name(inline_var_name, subexpression);
353  if (varname_i == std::string::npos)
354  continue;
355 
356  const std::size_t assignment_i =
357  subexpression.find(":", varname_i+1);
358 
359  libmesh_assert_not_equal_to(assignment_i, std::string::npos);
360 
361  libmesh_assert_equal_to(subexpression[assignment_i+1], '=');
362  for (unsigned int i = varname_i+1; i != assignment_i; ++i)
363  libmesh_assert_equal_to(subexpression[i], ' ');
364 
365  std::size_t end_assignment_i =
366  subexpression.find(";", assignment_i+1);
367 
368  libmesh_assert_not_equal_to(end_assignment_i, std::string::npos);
369 
370  std::string new_subexpression =
371  subexpression.substr(0, end_assignment_i+1) +
372  inline_var_name;
373 
374 #ifdef LIBMESH_HAVE_FPARSER
375  // Parse and evaluate the new subexpression.
376  // Add the same constants as we used originally.
377  FunctionParserADBase<Output> fp;
378  fp.AddConstant("NaN", std::numeric_limits<Real>::quiet_NaN());
379  fp.AddConstant("pi", std::acos(Real(-1)));
380  fp.AddConstant("e", std::exp(Real(1)));
381  if (fp.Parse(new_subexpression, variables) != -1) // -1 for success
382  libmesh_error_msg
383  ("ERROR: FunctionParser is unable to parse modified expression: "
384  << new_subexpression << '\n' << fp.ErrorMsg());
385 
386  Output new_var_value = this->eval(fp, new_subexpression, 0);
387 #ifdef NDEBUG
388  return new_var_value;
389 #else
390  if (found_var_name)
391  {
392  libmesh_assert_equal_to(old_var_value, new_var_value);
393  }
394  else
395  {
396  old_var_value = new_var_value;
397  found_var_name = true;
398  }
399 #endif
400 
401 #else
402  libmesh_error_msg("ERROR: This functionality requires fparser!");
403 #endif
404  }
405 
406  libmesh_assert(found_var_name);
407  return old_var_value;
408 }
Output eval(FunctionParserADBase< Output > &parser, const std::string &libmesh_dbg_var(function_name), unsigned int libmesh_dbg_var(component_idx)) const
Evaluate the ith FunctionParser and check the result.
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
Helper function for parsing out variable names.
template<typename Output , typename OutputGradient >
Output & libMesh::ParsedFunction< Output, OutputGradient >::getVarAddress ( const std::string &  variable_name)
virtual
Returns
The address of a parsed variable so you can supply a parameterized value.
The address of a parsed variable so you can supply a parameterized value

Definition at line 314 of file parsed_function.h.

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

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

315 {
316  const std::vector<std::string>::iterator it =
317  std::find(_additional_vars.begin(), _additional_vars.end(), variable_name);
318 
319  if (it == _additional_vars.end())
320  libmesh_error_msg("ERROR: Requested variable not found in parsed function");
321 
322  // Iterator Arithmetic (How far from the end of the array is our target address?)
323  return _spacetime[_spacetime.size() - (_additional_vars.end() - it)];
324 }
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 &  )
virtual

Definition at line 708 of file parsed_function.h.

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

Definition at line 252 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().

253 {
254  OutputGradient grad;
255  set_spacetime(p, time);
256 
257  grad(0) = eval(dx_parsers[0], "df/dx", 0);
258 #if LIBMESH_DIM > 1
259  grad(1) = eval(dy_parsers[0], "df/dy", 0);
260 #endif
261 #if LIBMESH_DIM > 2
262  grad(2) = eval(dz_parsers[0], "df/dz", 0);
263 #endif
264 
265  return grad;
266 }
Output eval(FunctionParserADBase< Output > &parser, const std::string &libmesh_dbg_var(function_name), unsigned int libmesh_dbg_var(component_idx)) const
Evaluate the ith FunctionParser and check the result.
void set_spacetime(const Point &p, const Real time=0)
Set the _spacetime argument vector.
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 ( )
virtual

Query if the automatic derivative generation was successful.

Definition at line 77 of file parsed_function.h.

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

The actual initialization process.

Reimplemented from libMesh::FunctionBase< Output >.

Definition at line 706 of file parsed_function.h.

706 {}
template<typename Output >
bool libMesh::FunctionBase< Output >::initialized ( ) const
inherited
Returns
true when this object is properly initialized and ready for use, false otherwise.

Definition at line 208 of file function_base.h.

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

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

209 {
210  return (this->_initialized);
211 }
bool _initialized
When init() was called so that everything is ready for calls to operator() (...), then this bool is t...
template<typename Output >
bool libMesh::FunctionBase< Output >::is_time_dependent ( ) const
inherited
template<typename Output , typename OutputGradient >
Output libMesh::ParsedFunction< Output, OutputGradient >::operator() ( const Point p,
const Real  time = 0 
)
virtual
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 234 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().

235 {
236  set_spacetime(p, time);
237  return eval(parsers[0], "f", 0);
238 }
Output eval(FunctionParserADBase< Output > &parser, const std::string &libmesh_dbg_var(function_name), unsigned int libmesh_dbg_var(component_idx)) const
Evaluate the ith FunctionParser and check the result.
std::vector< FunctionParserADBase< Output > > parsers
void set_spacetime(const Point &p, const Real time=0)
Set the _spacetime argument vector.
template<typename Output, typename OutputGradient >
void libMesh::ParsedFunction< Output, OutputGradient >::operator() ( const Point p,
const Real  time,
DenseVector< Output > &  output 
)
virtual

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

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

275 {
276  set_spacetime(p, time);
277 
278  unsigned int size = output.size();
279 
280  libmesh_assert_equal_to (size, parsers.size());
281 
282  // The remaining locations in _spacetime are currently fixed at construction
283  // but could potentially be made dynamic
284  for (unsigned int i=0; i != size; ++i)
285  output(i) = eval(parsers[i], "f", i);
286 }
Output eval(FunctionParserADBase< Output > &parser, const std::string &libmesh_dbg_var(function_name), unsigned int libmesh_dbg_var(component_idx)) const
Evaluate the ith FunctionParser and check the result.
std::vector< FunctionParserADBase< Output > > parsers
void set_spacetime(const Point &p, const Real time=0)
Set the _spacetime argument vector.
template<typename Output>
void libMesh::FunctionBase< Output >::operator() ( const Point p,
DenseVector< Output > &  output 
)
inherited

Evaluation function for time-independent vector-valued functions.

Sets output values in the passed-in output DenseVector.

Definition at line 243 of file function_base.h.

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

245 {
246  // Call the time-dependent function with t=0.
247  this->operator()(p, 0., output);
248 }
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 
)
virtual
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 698 of file parsed_function.h.

700  { 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 
)
virtual

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

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

Re-parse with minor changes to expression.

Definition at line 479 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().

480 {
482  _subexpressions.clear();
483  parsers.clear();
484 
485  size_t nextstart = 0, end = 0;
486 
487  while (end != std::string::npos)
488  {
489  // If we're past the end of the string, we can't make any more
490  // subparsers
491  if (nextstart >= expression.size())
492  break;
493 
494  // If we're at the start of a brace delimited section, then we
495  // parse just that section:
496  if (expression[nextstart] == '{')
497  {
498  nextstart++;
499  end = expression.find('}', nextstart);
500  }
501  // otherwise we parse the whole thing
502  else
503  end = std::string::npos;
504 
505  // We either want the whole end of the string (end == npos) or
506  // a substring in the middle.
507  _subexpressions.push_back
508  (expression.substr(nextstart, (end == std::string::npos) ?
509  std::string::npos : end - nextstart));
510 
511  // fparser can crash on empty expressions
512  if (_subexpressions.back().empty())
513  libmesh_error_msg("ERROR: FunctionParser is unable to parse empty expression.\n");
514 
515  // Parse (and optimize if possible) the subexpression.
516  // Add some basic constants, to Real precision.
517  FunctionParserADBase<Output> fp;
518  fp.AddConstant("NaN", std::numeric_limits<Real>::quiet_NaN());
519  fp.AddConstant("pi", std::acos(Real(-1)));
520  fp.AddConstant("e", std::exp(Real(1)));
521  if (fp.Parse(_subexpressions.back(), variables) != -1) // -1 for success
522  libmesh_error_msg
523  ("ERROR: FunctionParser is unable to parse expression: "
524  << _subexpressions.back() << '\n' << fp.ErrorMsg());
525 
526  // use of derivatives is optional. suppress error output on the console
527  // use the has_derivatives() method to check if AutoDiff was successful.
528  // also enable immediate optimization
529  fp.SetADFlags(FunctionParserADBase<Output>::ADSilenceErrors |
530  FunctionParserADBase<Output>::ADAutoOptimize);
531 
532  // optimize original function
533  fp.Optimize();
534  parsers.push_back(fp);
535 
536  // generate derivatives through automatic differentiation
537  FunctionParserADBase<Output> dx_fp(fp);
538  if (dx_fp.AutoDiff("x") != -1) // -1 for success
539  _valid_derivatives = false;
540  dx_parsers.push_back(dx_fp);
541 #if LIBMESH_DIM > 1
542  FunctionParserADBase<Output> dy_fp(fp);
543  if (dy_fp.AutoDiff("y") != -1) // -1 for success
544  _valid_derivatives = false;
545  dy_parsers.push_back(dy_fp);
546 #endif
547 #if LIBMESH_DIM > 2
548  FunctionParserADBase<Output> dz_fp(fp);
549  if (dz_fp.AutoDiff("z") != -1) // -1 for success
550  _valid_derivatives = false;
551  dz_parsers.push_back(dz_fp);
552 #endif
553  FunctionParserADBase<Output> dt_fp(fp);
554  if (dt_fp.AutoDiff("t") != -1) // -1 for success
555  _valid_derivatives = false;
556  dt_parsers.push_back(dt_fp);
557 
558  // If at end, use nextstart=maxSize. Else start at next
559  // character.
560  nextstart = (end == std::string::npos) ?
561  std::string::npos : end + 1;
562  }
563 }
std::vector< FunctionParserADBase< Output > > dt_parsers
std::vector< FunctionParserADBase< Output > > parsers
std::vector< std::string > _subexpressions
IterBase * end
Also have a polymorphic pointer to the end object, this prevents iterating past the 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)

Re-parse with new expression.

Definition at line 203 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().

204 {
205  variables = "x";
206 #if LIBMESH_DIM > 1
207  variables += ",y";
208 #endif
209 #if LIBMESH_DIM > 2
210  variables += ",z";
211 #endif
212  variables += ",t";
213 
214  // If additional vars were passed, append them to the string
215  // that we send to the function parser. Also add them to the
216  // end of our spacetime vector
217  for (std::size_t i=0; i < _additional_vars.size(); ++i)
218  {
219  variables += "," + _additional_vars[i];
220  // Initialize extra variables to the vector passed in or zero
221  // Note: The initial_vals vector can be shorter than the additional_vars vector
222  _spacetime[LIBMESH_DIM+1 + i] =
223  (i < _initial_vals.size()) ? _initial_vals[i] : 0;
224  }
225 
227 
229 }
std::vector< Output > _spacetime
std::vector< std::string > _additional_vars
bool _is_time_dependent
Cache whether or not this function is actually time-dependent.
bool expression_is_time_dependent(const std::string &expression) const
void partial_reparse(const std::string &expression)
Re-parse with minor changes to 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 
)

Changes the value of an inline variable.

Note
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 414 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(), libMesh::ParsedFunctionParameter< T >::set(), and ParsedFunctionTest::testInlineSetter().

416 {
417  libmesh_assert_greater (_subexpressions.size(), 0);
418 
419 #ifndef NDEBUG
420  bool found_var_name = false;
421 #endif
422  for (std::size_t s=0; s != _subexpressions.size(); ++s)
423  {
424  const std::string & subexpression = _subexpressions[s];
425  const std::size_t varname_i =
426  find_name(inline_var_name, subexpression);
427  if (varname_i == std::string::npos)
428  continue;
429 
430 #ifndef NDEBUG
431  found_var_name = true;
432 #endif
433  const std::size_t assignment_i =
434  subexpression.find(":", varname_i+1);
435 
436  libmesh_assert_not_equal_to(assignment_i, std::string::npos);
437 
438  libmesh_assert_equal_to(subexpression[assignment_i+1], '=');
439  for (unsigned int i = varname_i+1; i != assignment_i; ++i)
440  libmesh_assert_equal_to(subexpression[i], ' ');
441 
442  std::size_t end_assignment_i =
443  subexpression.find(";", assignment_i+1);
444 
445  libmesh_assert_not_equal_to(end_assignment_i, std::string::npos);
446 
447  std::ostringstream new_subexpression;
448  new_subexpression << subexpression.substr(0, assignment_i+2)
449  << std::setprecision(std::numeric_limits<Output>::digits10+2)
450 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
451  << '(' << newval.real() << '+'
452  << newval.imag() << 'i' << ')'
453 #else
454  << newval
455 #endif
456  << subexpression.substr(end_assignment_i,
457  std::string::npos);
458  _subexpressions[s] = new_subexpression.str();
459  }
460 
461  libmesh_assert(found_var_name);
462 
463  std::string new_expression;
464 
465  for (std::size_t s=0; s != _subexpressions.size(); ++s)
466  {
467  new_expression += '{';
468  new_expression += _subexpressions[s];
469  new_expression += '}';
470  }
471 
472  this->partial_reparse(new_expression);
473 }
std::vector< std::string > _subexpressions
libmesh_assert(j)
void partial_reparse(const std::string &expression)
Re-parse with minor changes to expression.
std::size_t find_name(const std::string &varname, const std::string &expr) const
Helper function for parsing out variable names.
template<typename Output >
void libMesh::FunctionBase< Output >::set_is_time_dependent ( bool  is_time_dependent)
inherited

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 215 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().

216 {
218 }
bool is_time_dependent() const
bool _is_time_dependent
Cache whether or not this function is actually time-dependent.
template<typename Output , typename OutputGradient >
void libMesh::ParsedFunction< Output, OutputGradient >::set_spacetime ( const Point p,
const Real  time = 0 
)
private

Set the _spacetime argument vector.

Definition at line 607 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()().

609 {
610  _spacetime[0] = p(0);
611 #if LIBMESH_DIM > 1
612  _spacetime[1] = p(1);
613 #endif
614 #if LIBMESH_DIM > 2
615  _spacetime[2] = p(2);
616 #endif
617  _spacetime[LIBMESH_DIM] = time;
618 
619  // The remaining locations in _spacetime are currently fixed at construction
620  // but could potentially be made dynamic
621 }
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 714 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 >::_initialized
protectedinherited
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 168 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: