Loading [MathJax]/extensions/tex2jax.js
https://mooseframework.inl.gov
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
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 37 of file Parser.C.

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(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(
74  n, "\n no variable '", var, "' found for use in function parser expression"));
75  }
76 
77  if (exp.errors.size() != n_errs)
78  return n->val();
79 
80  std::stringstream ss;
81  ss << std::setprecision(17) << fp.Eval(var_vals.data());
82 
83  // change kind only (not val)
84  n->setVal(n->val(), hit::Field::Kind::Float);
85  return ss.str();
86 }
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: