www.mooseframework.org
Public Member Functions | List of all members
FuncParseEvaler Class Reference

#include <Parser.h>

Inheritance diagram for FuncParseEvaler:
[legend]

Public Member Functions

virtual std::string eval (hit::Field *n, const std::list< std::string > &args, hit::BraceExpander &exp)
 

Detailed Description

Definition at line 34 of file Parser.h.

Member Function Documentation

◆ eval()

std::string FuncParseEvaler::eval ( hit::Field *  n,
const std::list< std::string > &  args,
hit::BraceExpander &  exp 
)
virtual

Definition at line 36 of file Parser.C.

37 {
38  std::string func_text;
39  for (auto & s : args)
40  func_text += s;
41  auto n_errs = exp.errors.size();
42 
43  FunctionParser fp;
44  fp.AddConstant("pi", libMesh::pi);
45  fp.AddConstant("e", std::exp(Real(1)));
46  std::vector<std::string> var_names;
47  auto ret = fp.ParseAndDeduceVariables(func_text, var_names);
48  if (ret != -1)
49  {
50  exp.errors.push_back(hit::errormsg(n, "fparse error: ", fp.ErrorMsg()));
51  return n->val();
52  }
53 
54  std::string errors;
55  std::vector<double> var_vals;
56  for (auto & var : var_names)
57  {
58  // recursively check all parent scopes for the needed variables
59  hit::Node * curr = n;
60  while ((curr = curr->parent()))
61  {
62  auto src = curr->find(var);
63  if (src && src != n && src->type() == hit::NodeType::Field)
64  {
65  exp.used.push_back(hit::pathJoin({curr->fullpath(), var}));
66  var_vals.push_back(curr->param<double>(var));
67  break;
68  }
69  }
70 
71  if (curr == nullptr)
72  exp.errors.push_back(hit::errormsg(
73  n, "\n no variable '", var, "' found for use in function parser expression"));
74  }
75 
76  if (exp.errors.size() != n_errs)
77  return n->val();
78 
79  std::stringstream ss;
80  ss << std::setprecision(17) << fp.Eval(var_vals.data());
81 
82  // change kind only (not val)
83  n->setVal(n->val(), hit::Field::Kind::Float);
84  return ss.str();
85 }
auto exp(const T &)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const Real pi

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