void
FunctionParserUtils::addFParserConstants(ADFunctionPtr & parser,
                                         const std::vector<std::string> & constant_names,
                                         const std::vector<std::string> & constant_expressions)
{
  // check constant vectors
  unsigned int nconst = constant_expressions.size();
  if (nconst != constant_expressions.size())
    mooseError("The parameter vectors constant_names and constant_values must have equal length.");

  // previously evaluated constant_expressions may be used in following constant_expressions
  std::vector<Real> constant_values(nconst);

  for (unsigned int i = 0; i < nconst; ++i)
  {
    ADFunctionPtr expression = ADFunctionPtr(new ADFunction());

    // set FParser internal feature flags
    setParserFeatureFlags(expression);

    // add previously evaluated constants
    for (unsigned int j = 0; j < i; ++j)
      if (!expression->AddConstant(constant_names[j], constant_values[j]))
        mooseError("Invalid constant name in ParsedMaterialHelper");

    // build the temporary comnstant expression function
    if (expression->Parse(constant_expressions[i], "") >= 0)
      mooseError("Invalid constant expression\n",
                 constant_expressions[i],
                 "\n in parsed function object.\n",
                 expression->ErrorMsg());

    constant_values[i] = expression->Eval(NULL);

    if (!parser->AddConstant(constant_names[i], constant_values[i]))
      mooseError("Invalid constant name in parsed function object");
  }
}
DerivativeParsedMaterial::DerivativeParsedMaterial(const std::string & name,
                                                   InputParameters parameters) :
    DerivativeBaseMaterial(name, parameters)
{
  // check number of coupled variables
  if (_arg_names.size() == 0)
    mooseError("Need at least one couple variable for DerivativeParsedMaterial.");

  // get and check constant vectors
  std::vector<std::string> constant_names = getParam<std::vector<std::string> >("constant_names");
  std::vector<std::string> constant_expressions = getParam<std::vector<std::string> >("constant_expressions");
  unsigned int nconst = constant_expressions.size();
  if (nconst != constant_expressions.size())
    mooseError("The parameter vectors constant_names and constant_values must have equal length.");

  // initialize constants
  ADFunction *expression;
  std::vector<Real> constant_values(nconst);
  for (unsigned int i = 0; i < nconst; ++i)
  {
    expression = new ADFunction();

    // add previously evaluated constants
    for (unsigned int j = 0; j < i; ++j)
      if (!expression->AddConstant(constant_names[j], constant_values[j]))
        mooseError("Invalid constant name in DerivativeParsedMaterial");

    // build the temporary comnstant expression function
    if (expression->Parse(constant_expressions[i], "") >= 0)
       mooseError(std::string("Invalid constant expression\n" + constant_expressions[i] + "\n in DerivativeParsedMaterial. ") + expression->ErrorMsg());

    constant_values[i] = expression->Eval(NULL);

#ifdef DEBUG
    _console << "Constant value " << i << ' ' << constant_expressions[i] << " = " << constant_values[i] << std::endl;
#endif

    if (!_func_F.AddConstant(constant_names[i], constant_values[i]))
      mooseError("Invalid constant name in DerivativeParsedMaterial");

    delete expression;
  }

  // get and check tolerance vectors
  std::vector<std::string> tol_names = getParam<std::vector<std::string> >("tol_names");
  std::vector<Real> tol_values = getParam<std::vector<Real> >("tol_values");

  if (tol_names.size() != tol_values.size())
    mooseError("The parameter vectors tol_names and tol_values must have equal length.");

  // set tolerances
  _tol.resize(_nargs);
  for (unsigned int i = 0; i < _nargs; ++i)
  {
    _tol[i] = -1.0;

    // for every argument look throug the entire tolerance vector to find a match
    for (unsigned int j = 0; j < tol_names.size(); ++j)
      if (_arg_names[i] == tol_names[j])
      {
        _tol[i] = tol_values[j];
        break;
      }
  }

  // build 'variables' argument for fparser
  std::string variables = _arg_names[0];
  for (unsigned i = 1; i < _arg_names.size(); ++i)
    variables += "," + _arg_names[i];

  // get material property names
  std::vector<std::string> mat_prop_names = getParam<std::vector<std::string> >("material_property_names");
  _nmat_props = mat_prop_names.size();

  // get all material properties
  _mat_props.resize(_nmat_props);
  for (unsigned int i = 0; i < _nmat_props; ++i)
  {
    _mat_props[i] = &getMaterialProperty<Real>(mat_prop_names[i]);
    variables += "," + mat_prop_names[i];
  }

  // build the base function
  if (_func_F.Parse(getParam<std::string>("function"), variables) >= 0)
     mooseError(std::string("Invalid function\n" + getParam<std::string>("function") + "\nin DerivativeParsedMaterial.\n") + _func_F.ErrorMsg());

  // Auto-Derivatives
  functionsDerivative();

  // Optimization
  functionsOptimize();

  // create parameter passing buffer
  _func_params = new Real[_nargs + _nmat_props];
}