www.mooseframework.org
AppFactory.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "AppFactory.h"
16 #include "CommandLine.h"
17 #include "InputParameters.h"
18 
20 
21 AppFactory &
23 {
24  return _instance;
25 }
26 
28 
29 MooseApp *
30 AppFactory::createApp(std::string app_type, int argc, char ** argv, MPI_Comm COMM_WORLD_IN)
31 {
32  auto command_line = std::make_shared<CommandLine>(argc, argv);
33  InputParameters app_params = AppFactory::instance().getValidParams(app_type);
34 
35  app_params.set<int>("_argc") = argc;
36  app_params.set<char **>("_argv") = argv;
37  app_params.set<std::shared_ptr<CommandLine>>("_command_line") = command_line;
38 
39  MooseApp * app = AppFactory::instance().create(app_type, "main", app_params, COMM_WORLD_IN);
40  return app;
41 }
42 
43 std::shared_ptr<MooseApp>
44 AppFactory::createAppShared(const std::string & app_type, int argc, char ** argv)
45 {
46  return std::shared_ptr<MooseApp>(createApp(app_type, argc, argv));
47 }
48 
50 AppFactory::getValidParams(const std::string & name)
51 {
52  if (_name_to_params_pointer.find(name) == _name_to_params_pointer.end())
53  mooseError(std::string("A '") + name + "' is not a registered object\n\n");
54 
56  return params;
57 }
58 
59 MooseApp *
60 AppFactory::create(const std::string & app_type,
61  const std::string & name,
62  InputParameters parameters,
63  MPI_Comm COMM_WORLD_IN)
64 {
65  // Error if the application type is not located
66  if (_name_to_build_pointer.find(app_type) == _name_to_build_pointer.end())
67  mooseError("Object '" + app_type + "' was not registered.");
68 
69  // Take the app_type and add it to the parameters so that it can be retrieved in the Application
70  parameters.set<std::string>("_type") = app_type;
71 
72  // Check to make sure that all required parameters are supplied
73  parameters.checkParams("");
74 
75  auto comm = std::make_shared<Parallel::Communicator>(COMM_WORLD_IN);
76 
77  parameters.set<std::shared_ptr<Parallel::Communicator>>("_comm") = comm;
78  parameters.set<std::string>("_app_name") = name;
79 
80  if (!parameters.isParamValid("_command_line"))
81  mooseError("Valid CommandLine object required");
82 
83  std::shared_ptr<CommandLine> command_line =
84  parameters.get<std::shared_ptr<CommandLine>>("_command_line");
85  command_line->addCommandLineOptionsFromParams(parameters);
86  command_line->populateInputParams(parameters);
87 
88  return (*_name_to_build_pointer[app_type])(parameters);
89 }
90 
91 bool
92 AppFactory::isRegistered(const std::string & app_name) const
93 {
94  return _name_to_params_pointer.find(app_name) != _name_to_params_pointer.end();
95 }
static AppFactory _instance
Definition: AppFactory.h:143
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
Base class for MOOSE-based applications.
Definition: MooseApp.h:58
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
MooseApp * create(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:60
std::map< std::string, appBuildPtr > _name_to_build_pointer
Definition: AppFactory.h:139
std::map< std::string, paramsPtr > _name_to_params_pointer
Definition: AppFactory.h:141
static MooseApp * createApp(std::string app_type, int argc, char **argv, MPI_Comm COMM_WORLD_IN=MPI_COMM_WORLD)
Helper function for creating a MooseApp from command-line arguments.
Definition: AppFactory.C:30
virtual ~AppFactory()
Definition: AppFactory.C:27
bool isRegistered(const std::string &app_name) const
Returns a Boolean indicating whether an application type has been registered.
Definition: AppFactory.C:92
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.
Generic AppFactory class for building Application objects.
Definition: AppFactory.h:71
void checkParams(const std::string &parsing_syntax)
This function checks parameters stored in the object to make sure they are in the correct state as th...
static AppFactory & instance()
Get the instance of the AppFactory.
Definition: AppFactory.C:22
MPI_Comm comm
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: AppFactory.C:50
static std::shared_ptr< MooseApp > createAppShared(const std::string &app_type, int argc, char **argv)
Definition: AppFactory.C:44