www.mooseframework.org
AppFactory.C
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 #include "AppFactory.h"
11 #include "CommandLine.h"
12 #include "InputParameters.h"
13 #include "MooseApp.h"
14 #include "Parser.h"
15 
16 AppFactory &
18 {
19  // We need a naked new here (_not_ a smart pointer or object instance) due to what seems like a
20  // bug in clang's static object destruction when using dynamic library loading.
21  static AppFactory * instance = nullptr;
22  if (!instance)
23  instance = new AppFactory;
24  return *instance;
25 }
26 
28 
30 AppFactory::getValidParams(const std::string & name)
31 {
32  if (const auto it = _name_to_build_info.find(name); it != _name_to_build_info.end())
33  return it->second->buildParameters();
34 
35  mooseError(std::string("A '") + name + "' is not a registered object\n\n");
36 }
37 
39 AppFactory::createAppShared(const std::string & default_app_type,
40  int argc,
41  char ** argv,
42  std::unique_ptr<Parser> parser,
43  MPI_Comm comm_world_in)
44 {
45  auto command_line = std::make_shared<CommandLine>(argc, argv);
46  auto which_app_param = emptyInputParameters();
47 
48  MooseApp::addAppParam(which_app_param);
49  command_line->addCommandLineOptionsFromParams(which_app_param);
50 
51  std::string app_type;
52  if (!command_line->search("app_to_run", app_type))
53  app_type = default_app_type;
54  else
55  mooseDeprecated("Please use [Application] block to specify application type, '--app <AppName>' "
56  "is deprecated and will be removed in a future release.");
57 
58  auto app_params = AppFactory::instance().getValidParams(app_type);
59  parser->setAppType(app_type);
60 
61  app_params.set<int>("_argc") = argc;
62  app_params.set<char **>("_argv") = argv;
63  app_params.set<std::shared_ptr<CommandLine>>("_command_line") = command_line;
64 
65  // Take the front parser and add it to the parameters so that it can be retrieved in the
66  // Application
67  app_params.set<std::shared_ptr<Parser>>("_parser") = std::move(parser);
68 
69  return AppFactory::instance().createShared(app_type, "main", app_params, comm_world_in);
70 }
71 
73 AppFactory::createAppShared(const std::string & default_app_type,
74  int argc,
75  char ** argv,
76  MPI_Comm comm_world_in)
77 {
78  mooseDeprecated("Please update your main.C to adapt new main function in MOOSE framework, "
79  "see'test/src/main.C in MOOSE as an example of moose::main()'. ");
80 
81  auto command_line = std::make_shared<CommandLine>(argc, argv);
82  auto which_app_param = emptyInputParameters();
83 
84  which_app_param.addCommandLineParam<std::vector<std::string>>(
85  "input_file",
86  "-i <input_files>",
87  "Specify one or multiple input files. Multiple files get merged into a single simulation "
88  "input.");
89 
90  command_line->addCommandLineOptionsFromParams(which_app_param);
91 
92  std::vector<std::string> input_filenames;
93  command_line->search("input_file", input_filenames);
94 
95  auto parser = std::make_unique<Parser>(input_filenames);
96  if (input_filenames.size())
97  parser->parse();
98 
99  MooseApp::addAppParam(which_app_param);
100  command_line->addCommandLineOptionsFromParams(which_app_param);
101 
102  std::string app_type;
103  if (!command_line->search("app_to_run", app_type))
104  app_type = default_app_type;
105  else
106  mooseDeprecated("Please use [Application] block to specify application type, '--app <AppName>' "
107  "is deprecated and will be removed in a future release.");
108 
109  parser->setAppType(app_type);
110  auto app_params = AppFactory::instance().getValidParams(app_type);
111 
112  app_params.set<int>("_argc") = argc;
113  app_params.set<char **>("_argv") = argv;
114  app_params.set<std::shared_ptr<CommandLine>>("_command_line") = command_line;
115 
116  // Take the front parser and add it to the parameters so that it can be retrieved in the
117  // Application
118  app_params.set<std::shared_ptr<Parser>>("_parser") = std::move(parser);
119 
120  return AppFactory::instance().createShared(app_type, "main", app_params, comm_world_in);
121 }
122 
124 AppFactory::createShared(const std::string & app_type,
125  const std::string & name,
126  InputParameters parameters,
127  MPI_Comm comm_world_in)
128 {
129  // Error if the application type is not located
130  const auto it = _name_to_build_info.find(app_type);
131  if (it == _name_to_build_info.end())
132  mooseError("Object '" + app_type + "' was not registered.");
133  auto & build_info = it->second;
134 
135  // Take the app_type and add it to the parameters so that it can be retrieved in the Application
136  parameters.set<std::string>("_type") = app_type;
137 
138  // Check to make sure that all required parameters are supplied
139  parameters.finalize("");
140 
141  auto comm = std::make_shared<Parallel::Communicator>(comm_world_in);
142 
143  parameters.set<std::shared_ptr<Parallel::Communicator>>("_comm") = comm;
144  parameters.set<std::string>("_app_name") = name;
145 
146  if (!parameters.isParamValid("_command_line"))
147  mooseError("Valid CommandLine object required");
148 
149  std::shared_ptr<CommandLine> command_line =
150  parameters.get<std::shared_ptr<CommandLine>>("_command_line");
151  command_line->addCommandLineOptionsFromParams(parameters);
152  command_line->populateInputParams(parameters);
153 
154  build_info->_app_creation_count++;
155 
156  return build_info->build(parameters);
157 }
158 
159 std::size_t
160 AppFactory::createdAppCount(const std::string & app_type) const
161 {
162  // Error if the application type is not located
163  const auto it = _name_to_build_info.find(app_type);
164  if (it == _name_to_build_info.end())
165  mooseError("AppFactory::createdAppCount(): '", app_type, "' is not a registered app");
166 
167  return it->second->_app_creation_count;
168 }
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:299
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:124
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()
virtual ~AppFactory()
Definition: AppFactory.C:27
std::shared_ptr< MooseApp > MooseAppPtr
alias to wrap shared pointer type
Definition: AppFactory.h:27
Generic AppFactory class for building Application objects.
Definition: AppFactory.h:55
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:144
std::size_t createdAppCount(const std::string &app_type) const
Definition: AppFactory.C:160
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:350
static AppFactory & instance()
Get the instance of the AppFactory.
Definition: AppFactory.C:17
static MooseAppPtr createAppShared(const std::string &default_app_type, int argc, char **argv, std::unique_ptr< Parser > parser, MPI_Comm comm_word=MPI_COMM_WORLD)
Helper function for creating a MooseApp from command-line arguments and a Parser. ...
Definition: AppFactory.C:39
static void addAppParam(InputParameters &params)
Definition: MooseApp.C:85
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: AppFactory.C:30
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.