Loading [MathJax]/extensions/tex2jax.js
https://mooseframework.inl.gov
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
AppFactory.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
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 #include "AppFactory.h"
11 #include "CommandLine.h"
12 #include "InputParameters.h"
13 #include "MooseApp.h"
14 #include "Parser.h"
15 #include "MooseMain.h"
16 
17 AppFactory &
19 {
20  // We need a naked new here (_not_ a smart pointer or object instance) due to what seems like a
21  // bug in clang's static object destruction when using dynamic library loading.
22  static AppFactory * instance = nullptr;
23  if (!instance)
24  instance = new AppFactory;
25  return *instance;
26 }
27 
29 
31 AppFactory::getValidParams(const std::string & name)
32 {
33  if (const auto it = _name_to_build_info.find(name); it != _name_to_build_info.end())
34  return it->second->buildParameters();
35 
36  mooseError(std::string("A '") + name + "' is not a registered object\n\n");
37 }
38 
40 AppFactory::createAppShared(int argc, char ** argv, std::unique_ptr<Parser> parser)
41 {
42  mooseAssert(parser, "Not set");
43  mooseAssert(parser->getAppType().size(), "App type not set");
44  const std::string app_type = parser->getAppType();
45 
46  auto command_line = std::make_unique<CommandLine>(argc, argv);
47  command_line->parse();
48 
49  auto app_params = AppFactory::instance().getValidParams(parser->getAppType());
50  app_params.set<int>("_argc") = argc;
51  app_params.set<char **>("_argv") = argv;
52  app_params.set<std::shared_ptr<CommandLine>>("_command_line") = std::move(command_line);
53  app_params.set<std::shared_ptr<Parser>>("_parser") = std::move(parser);
54 
55  return AppFactory::instance().createShared(app_type, "main", app_params, MPI_COMM_WORLD);
56 }
57 
59 AppFactory::createAppShared(const std::string & default_app_type,
60  int argc,
61  char ** argv,
62  MPI_Comm comm_world_in)
63 {
64  mooseDeprecated("Please update your main.C to adapt new main function in MOOSE framework, "
65  "see'test/src/main.C in MOOSE as an example of moose::main()'. ");
66 
67  auto command_line_params = emptyInputParameters();
68  MooseApp::addInputParam(command_line_params);
69  MooseApp::addAppParam(command_line_params);
70 
71  {
72  CommandLine pre_command_line(argc, argv);
73  pre_command_line.parse();
74  pre_command_line.populateCommandLineParams(command_line_params);
75  }
76 
77  const auto & input_filenames = command_line_params.get<std::vector<std::string>>("input_file");
78  auto parser = std::make_unique<Parser>(input_filenames);
79  if (input_filenames.size())
80  parser->parse();
81 
82  std::string app_type = command_line_params.get<std::string>("app_to_run");
83  if (app_type.empty())
84  app_type = default_app_type;
85  else
86  mooseDeprecated("Please use [Application] block to specify application type, '--app <AppName>' "
87  "is deprecated and will be removed in a future release.");
88 
89  parser->setAppType(app_type);
90  auto app_params = AppFactory::instance().getValidParams(app_type);
91 
92  app_params.set<int>("_argc") = argc;
93  app_params.set<char **>("_argv") = argv;
94 
95  auto command_line = std::make_unique<CommandLine>(argc, argv);
96  command_line->parse();
97  app_params.set<std::shared_ptr<CommandLine>>("_command_line") = std::move(command_line);
98 
99  // Take the front parser and add it to the parameters so that it can be retrieved in the
100  // Application
101  app_params.set<std::shared_ptr<Parser>>("_parser") = std::move(parser);
102 
103  return AppFactory::instance().createShared(app_type, "main", app_params, comm_world_in);
104 }
105 
107 AppFactory::createShared(const std::string & app_type,
108  const std::string & name,
109  InputParameters parameters,
110  MPI_Comm comm_world_in)
111 {
112  // Error if the application type is not located
113  const auto it = _name_to_build_info.find(app_type);
114  if (it == _name_to_build_info.end())
115  mooseError("Object '" + app_type + "' was not registered.");
116  auto & build_info = it->second;
117 
118  // Take the app_type and add it to the parameters so that it can be retrieved in the Application
119  parameters.set<std::string>("_type") = app_type;
120 
121  // Check to make sure that all required parameters are supplied
122  parameters.finalize("");
123 
124  auto comm = std::make_shared<Parallel::Communicator>(comm_world_in);
125 
126  parameters.set<std::shared_ptr<Parallel::Communicator>>("_comm") = comm;
127  parameters.set<std::string>("_app_name") = name;
128 
129  if (!parameters.isParamValid("_command_line"))
130  mooseError("Valid CommandLine object required");
131 
132  std::shared_ptr<CommandLine> command_line =
133  parameters.get<std::shared_ptr<CommandLine>>("_command_line");
134  mooseAssert(command_line->hasParsed(), "Should have been parsed");
135 
136  command_line->populateCommandLineParams(parameters);
137 
138  build_info->_app_creation_count++;
139 
140  return build_info->build(parameters);
141 }
142 
143 std::size_t
144 AppFactory::createdAppCount(const std::string & app_type) const
145 {
146  // Error if the application type is not located
147  const auto it = _name_to_build_info.find(app_type);
148  if (it == _name_to_build_info.end())
149  mooseError("AppFactory::createdAppCount(): '", app_type, "' is not a registered app");
150 
151  return it->second->_app_creation_count;
152 }
std::string name(const ElemQuality q)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
MooseAppPtr createShared(const std::string &app_type, const std::string &name, InputParameters parameters, MPI_Comm COMM_WORLD_IN)
Build an application object (must be registered)
Definition: AppFactory.C:107
static void addInputParam(InputParameters &params)
Definition: MooseApp.C:101
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void finalize(const std::string &parsing_syntax)
Finalizes the parameters, which must be done before constructing any objects with these parameters (t...
InputParameters emptyInputParameters()
This class wraps provides and tracks access to command line parameters.
Definition: CommandLine.h:29
virtual ~AppFactory()
Definition: AppFactory.C:28
std::shared_ptr< MooseApp > MooseAppPtr
alias to wrap shared pointer type
Definition: AppFactory.h:28
void populateCommandLineParams(InputParameters &params)
Populates the command line input parameters from params.
Definition: CommandLine.C:293
Generic AppFactory class for building Application objects.
Definition: AppFactory.h:56
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:143
std::size_t createdAppCount(const std::string &app_type) const
Definition: AppFactory.C:144
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:353
static MooseAppPtr createAppShared(int argc, char **argv, std::unique_ptr< Parser > parser)
Helper function for creating a MooseApp from command-line arguments and a Parser. ...
Definition: AppFactory.C:40
static AppFactory & instance()
Get the instance of the AppFactory.
Definition: AppFactory.C:18
static void addAppParam(InputParameters &params)
Definition: MooseApp.C:94
void parse()
Performs the parsing, which is the combining of arguments into [name, value] pairs.
Definition: CommandLine.C:68
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: AppFactory.C:31
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.