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 33 of file Parser.h.

Member Function Documentation

◆ eval()

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

Definition at line 37 of file Parser.h.

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

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