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 
15 
16 AppFactory &
18 {
19  return _instance;
20 }
21 
23 
25 AppFactory::getValidParams(const std::string & name)
26 {
27  if (_name_to_params_pointer.find(name) == _name_to_params_pointer.end())
28  mooseError(std::string("A '") + name + "' is not a registered object\n\n");
29 
31  return params;
32 }
33 
35 AppFactory::createAppShared(const std::string & app_type,
36  int argc,
37  char ** argv,
38  MPI_Comm comm_world_in)
39 {
40  auto command_line = std::make_shared<CommandLine>(argc, argv);
41  auto app_params = AppFactory::instance().getValidParams(app_type);
42 
43  app_params.set<int>("_argc") = argc;
44  app_params.set<char **>("_argv") = argv;
45  app_params.set<std::shared_ptr<CommandLine>>("_command_line") = command_line;
46 
47  return AppFactory::instance().createShared(app_type, "main", app_params, comm_world_in);
48 }
49 
51 AppFactory::createShared(const std::string & app_type,
52  const std::string & name,
53  InputParameters parameters,
54  MPI_Comm comm_world_in)
55 {
56  // Error if the application type is not located
57  if (_name_to_build_pointer.find(app_type) == _name_to_build_pointer.end())
58  mooseError("Object '" + app_type + "' was not registered.");
59 
60  // Take the app_type and add it to the parameters so that it can be retrieved in the Application
61  parameters.set<std::string>("_type") = app_type;
62 
63  // Check to make sure that all required parameters are supplied
64  parameters.checkParams("");
65 
66  auto comm = std::make_shared<Parallel::Communicator>(comm_world_in);
67 
68  parameters.set<std::shared_ptr<Parallel::Communicator>>("_comm") = comm;
69  parameters.set<std::string>("_app_name") = name;
70 
71  if (!parameters.isParamValid("_command_line"))
72  mooseError("Valid CommandLine object required");
73 
74  std::shared_ptr<CommandLine> command_line =
75  parameters.get<std::shared_ptr<CommandLine>>("_command_line");
76  command_line->addCommandLineOptionsFromParams(parameters);
77  command_line->populateInputParams(parameters);
78 
79  return (*_name_to_build_pointer[app_type])(parameters);
80 }
81 
82 bool
83 AppFactory::isRegistered(const std::string & app_name) const
84 {
85  return _name_to_params_pointer.find(app_name) != _name_to_params_pointer.end();
86 }
bool isRegistered(const std::string &app_name) const
Returns a Boolean indicating whether an application type has been registered.
Definition: AppFactory.C:83
static AppFactory _instance
Definition: AppFactory.h:128
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
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:51
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
static MooseAppPtr createAppShared(const std::string &app_type, int argc, char **argv, MPI_Comm comm_word=MPI_COMM_WORLD)
Helper function for creating a MooseApp from command-line arguments.
Definition: AppFactory.C:35
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::map< std::string, appBuildPtr > _name_to_build_pointer
Definition: AppFactory.h:124
std::map< std::string, paramsPtr > _name_to_params_pointer
Definition: AppFactory.h:126
virtual ~AppFactory()
Definition: AppFactory.C:22
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:57
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:17
MPI_Comm comm
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: AppFactory.C:25
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.