www.mooseframework.org
Parser.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 // MOOSE includes
13 #include "ConsoleStreamInterface.h"
14 #include "MooseTypes.h"
15 #include "InputParameters.h"
16 #include "Syntax.h"
17 
18 #include "hit.h"
19 
20 #include <vector>
21 #include <string>
22 #include <iomanip>
23 
24 // Forward declarations
25 class ActionWarehouse;
26 class SyntaxTree;
27 class MooseApp;
28 class Factory;
29 class ActionFactory;
30 class GlobalParamsAction;
31 class JsonSyntaxTree;
32 
33 class FuncParseEvaler : public hit::Evaler
34 {
35 public:
36  virtual std::string
37  eval(hit::Field * n, const std::list<std::string> & args, hit::BraceExpander & exp)
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  }
90 };
91 
97 class Parser : public ConsoleStreamInterface, public hit::Walker
98 {
99 public:
101  {
104  };
105 
106  Parser(MooseApp & app, ActionWarehouse & action_wh);
107 
108  virtual ~Parser();
109 
113  std::string getFileName(bool stripLeadingPath = true) const;
114 
119  void parse(const std::string & input_filename);
120 
126  void extractParams(const std::string & prefix, InputParameters & p);
127 
131  void initSyntaxFormatter(SyntaxFormatterType type, bool dump_mode);
132 
136  void buildFullTree(const std::string & search_string);
137 
141  void buildJsonSyntaxTree(JsonSyntaxTree & tree) const;
142 
143  void walk(const std::string & fullpath, const std::string & nodepath, hit::Node * n);
144 
145  void errorCheck(const Parallel::Communicator & comm, bool warn_unused, bool err_unused);
146 
147  std::vector<std::string> listValidParams(std::string & section_name);
148 
149 protected:
154  template <typename T, typename Base>
156  void setScalarParameter(const std::string & full_name,
157  const std::string & short_name,
158  InputParameters::Parameter<T> * param,
159  bool in_global,
160  GlobalParamsAction * global_block);
161 
162  template <typename T, typename UP_T, typename Base>
163  void setScalarValueTypeParameter(const std::string & full_name,
164  const std::string & short_name,
165  InputParameters::Parameter<T> * param,
166  bool in_global,
167  GlobalParamsAction * global_block);
168 
170  template <typename T, typename Base>
171  void setVectorParameter(const std::string & full_name,
172  const std::string & short_name,
173  InputParameters::Parameter<std::vector<T>> * param,
174  bool in_global,
175  GlobalParamsAction * global_block);
176 
181  template <typename T>
182  void setFilePathParam(const std::string & full_name,
183  const std::string & short_name,
184  InputParameters::Parameter<T> * param,
185  InputParameters & params,
186  bool in_global,
187  GlobalParamsAction * global_block);
188 
193  template <typename T>
194  void setVectorFilePathParam(const std::string & full_name,
195  const std::string & short_name,
196  InputParameters::Parameter<std::vector<T>> * param,
197  InputParameters & params,
198  bool in_global,
199  GlobalParamsAction * global_block);
204  template <typename T>
205  void setDoubleIndexParameter(const std::string & full_name,
206  const std::string & short_name,
207  InputParameters::Parameter<std::vector<std::vector<T>>> * param,
208  bool in_global,
209  GlobalParamsAction * global_block);
210 
215  template <typename T>
216  void setScalarComponentParameter(const std::string & full_name,
217  const std::string & short_name,
218  InputParameters::Parameter<T> * param,
219  bool in_global,
220  GlobalParamsAction * global_block);
221 
226  template <typename T>
227  void setVectorComponentParameter(const std::string & full_name,
228  const std::string & short_name,
229  InputParameters::Parameter<std::vector<T>> * param,
230  bool in_global,
231  GlobalParamsAction * global_block);
232 
233  std::unique_ptr<hit::Node> _cli_root = nullptr;
234  std::unique_ptr<hit::Node> _root = nullptr;
235  std::vector<std::string> _secs_need_first;
236 
247 
249  std::unique_ptr<SyntaxTree> _syntax_formatter;
250 
252  std::string _input_filename;
253 
255  std::set<std::string> _extracted_vars;
256 
259 
262 
264  std::ostringstream * _current_error_stream;
265 
266 private:
267  std::string _errmsg;
268  std::string _warnmsg;
269  std::string hitCLIFilter(std::string appname, const std::vector<std::string> & argv);
270  void walkRaw(std::string fullpath, std::string nodepath, hit::Node * n);
271 };
void setScalarParameter(const std::string &full_name, const std::string &short_name, InputParameters::Parameter< T > *param, bool in_global, GlobalParamsAction *global_block)
Helper functions for setting parameters of arbitrary types - bodies are in the .C file since they are...
Definition: Parser.C:1294
void setScalarValueTypeParameter(const std::string &full_name, const std::string &short_name, InputParameters::Parameter< T > *param, bool in_global, GlobalParamsAction *global_block)
Definition: Parser.C:1382
MooseApp & _app
The MooseApp this Parser is part of.
Definition: Parser.h:238
void walkRaw(std::string fullpath, std::string nodepath, hit::Node *n)
Definition: Parser.C:306
std::string _warnmsg
Definition: Parser.h:268
Generic factory class for build all sorts of objects.
Definition: Factory.h:153
std::vector< std::string > _secs_need_first
Definition: Parser.h:235
bool _sections_read
Boolean to indicate whether parsing has started (sections have been extracted)
Definition: Parser.h:258
SyntaxFormatterType
Definition: Parser.h:100
virtual std::string eval(hit::Field *n, const std::list< std::string > &args, hit::BraceExpander &exp)
Definition: Parser.h:37
Base class for MOOSE-based applications.
Definition: MooseApp.h:59
Storage for action instances.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void walk(const std::string &fullpath, const std::string &nodepath, hit::Node *n)
Definition: Parser.C:383
virtual ~Parser()
Definition: Parser.C:65
void extractParams(const std::string &prefix, InputParameters &p)
This function attempts to extract values from the input file based on the contents of the passed para...
Definition: Parser.C:957
Syntax & _syntax
Reference to an object that defines input file syntax.
Definition: Parser.h:246
std::string hitCLIFilter(std::string appname, const std::vector< std::string > &argv)
Definition: Parser.C:393
Holds the syntax in a Json::Value tree.
std::string _input_filename
The input file name that is used for parameter extraction.
Definition: Parser.h:252
void initSyntaxFormatter(SyntaxFormatterType type, bool dump_mode)
Creates a syntax formatter for printing.
Definition: Parser.C:632
std::unique_ptr< hit::Node > _cli_root
Definition: Parser.h:233
std::vector< std::string > listValidParams(std::string &section_name)
Definition: Parser.C:162
void setDoubleIndexParameter(const std::string &full_name, const std::string &short_name, InputParameters::Parameter< std::vector< std::vector< T >>> *param, bool in_global, GlobalParamsAction *global_block)
Template method for setting any double indexed type parameter read from the input file or command lin...
Definition: Parser.C:1472
Specialized factory for generic Action System objects.
Definition: ActionFactory.h:64
An inteface for the _console for outputting to the Console object.
void setVectorParameter(const std::string &full_name, const std::string &short_name, InputParameters::Parameter< std::vector< T >> *param, bool in_global, GlobalParamsAction *global_block)
Template method for setting any vector type parameter read from the input file or command line...
Definition: Parser.C:1399
std::unique_ptr< SyntaxTree > _syntax_formatter
Object for holding the syntax parse tree.
Definition: Parser.h:249
Parser(MooseApp &app, ActionWarehouse &action_wh)
Definition: Parser.C:51
std::string _errmsg
Definition: Parser.h:267
std::set< std::string > _extracted_vars
The set of all variables extracted from the input file.
Definition: Parser.h:255
void setVectorComponentParameter(const std::string &full_name, const std::string &short_name, InputParameters::Parameter< std::vector< T >> *param, bool in_global, GlobalParamsAction *global_block)
Template method for setting several multivalue "scalar" type parameter read from the input file or co...
Definition: Parser.C:1557
ActionWarehouse & _action_wh
Action warehouse that will be filled by actions.
Definition: Parser.h:242
std::ostringstream * _current_error_stream
The current stream object used for capturing errors during extraction.
Definition: Parser.h:264
void setVectorFilePathParam(const std::string &full_name, const std::string &short_name, InputParameters::Parameter< std::vector< T >> *param, InputParameters &params, bool in_global, GlobalParamsAction *global_block)
Sets an input parameter representing a vector of file paths using input file data.
Definition: Parser.C:1434
MatType type
PetscInt n
std::unique_ptr< hit::Node > _root
Definition: Parser.h:234
void setFilePathParam(const std::string &full_name, const std::string &short_name, InputParameters::Parameter< T > *param, InputParameters &params, bool in_global, GlobalParamsAction *global_block)
Sets an input parameter representing a file path using input file data.
Definition: Parser.C:1357
void errorCheck(const Parallel::Communicator &comm, bool warn_unused, bool err_unused)
Definition: Parser.C:582
std::string getFileName(bool stripLeadingPath=true) const
Return the filename that was parsed.
Definition: Parser.C:290
MPI_Comm comm
Holding syntax for parsing input files.
Definition: Syntax.h:20
InputParameters * _current_params
The current parameter object for which parameters are being extracted.
Definition: Parser.h:261
void buildJsonSyntaxTree(JsonSyntaxTree &tree) const
Use MOOSE Factories to construct a parameter tree for documentation or echoing input.
Definition: Parser.C:649
void setScalarComponentParameter(const std::string &full_name, const std::string &short_name, InputParameters::Parameter< T > *param, bool in_global, GlobalParamsAction *global_block)
Template method for setting any multivalue "scalar" type parameter read from the input file or comman...
Definition: Parser.C:1512
Factory & _factory
The Factory associated with that MooseApp.
Definition: Parser.h:240
ActionFactory & _action_factory
The Factory that builds actions.
Definition: Parser.h:244
void parse(const std::string &input_filename)
Parse an input file consisting of hit syntax and setup objects in the MOOSE derived application...
Definition: Parser.C:482
Class for parsing input files.
Definition: Parser.h:97
void buildFullTree(const std::string &search_string)
Use MOOSE Factories to construct a full parse tree for documentation or echoing input.
Definition: Parser.C:767