MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Function.h File Reference
#include "muParser.h"
+ Include dependency graph for Function.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Function
 

Functions

double * _functionAddVar (const char *name, void *data)
 

Function Documentation

double* _functionAddVar ( const char *  name,
void *  data 
)

Simple function parser and evaluator for MOOSE. This can take a mathematical expression in standard C form and a list of variables values and evaluate the results.

Call-back to add variables to parser automatically.

We use different storage for constants and variables. Variables are stored in a vector of Variable object pointers. They must have the name x{index} where index is any non-negative integer. Note that the vector is resized to whatever the maximum index is. It is up to the user to make sure that indices are sequential without any gap. In case there is a gap in indices, those entries will remain unused.

If the name starts with anything other than x or y, then it is taken to be a named constant, which must be set before any expression or variables and error is thrown.

NOTE: this is called not on setting expression but on first attempt at evaluation of the same, i.e. when you access value of the Function object.

Definition at line 482 of file Function.cpp.

483 {
484  Function* function = reinterpret_cast< Function * >(data);
485  double * ret = NULL;
486  string strname(name);
487  // Names starting with x are variables, everything else is constant.
488  if (strname[0] == 'x'){
489  int index = atoi(strname.substr(1).c_str());
490  if ((unsigned)index >= function->_varbuf.size()){
491  function->_varbuf.resize(index+1, 0);
492  for (int ii = 0; ii <= index; ++ii){
493  if (function->_varbuf[ii] == 0){
494  function->_varbuf[ii] = new Variable();
495  }
496  }
497  function->_numVar = function->_varbuf.size();
498  }
499  ret = &(function->_varbuf[index]->value);
500  } else if (strname[0] == 'y'){
501  int index = atoi(strname.substr(1).c_str());
502  if ((unsigned)index >= function->_pullbuf.size()){
503  function->_pullbuf.resize(index+1, 0 );
504  for (int ii = 0; ii <= index; ++ii){
505  if (function->_pullbuf[ii] == 0){
506  function->_pullbuf[ii] = new double();
507  }
508  }
509  }
510  ret = function->_pullbuf[index];
511  } else if (strname == "t"){
512  ret = &function->_t;
513  } else {
514  cerr << "Got an undefined symbol: " << name << endl
515  << "Variables must be named xi, yi, where i is integer index."
516  << " You must define the constants beforehand using LookupField c: c[name]"
517  " = value"
518  << endl;
519  throw mu::ParserError("Undefined constant.");
520  }
521 
522  return ret;
523 }
static char name[]
Definition: mfield.cpp:401