https://mooseframework.inl.gov
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 22 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 29 of file Parser.C.

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