https://mooseframework.inl.gov
MooseMain.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 "MooseMain.h"
11 #include "ParallelUniqueId.h"
12 #include "Parser.h"
13 #include "AppFactory.h"
14 #include "CommandLine.h"
15 #include "InputParameters.h"
16 #include "MooseApp.h"
17 
18 #ifdef LIBMESH_HAVE_OPENMP
19 #include <omp.h>
20 #endif
21 #include <regex>
22 
23 namespace Moose
24 {
25 
26 std::shared_ptr<MooseApp>
27 createMooseApp(const std::string & default_app_type, int argc, char * argv[])
28 {
29  // Parse the command line early in order to determine the application type, from:
30  // - the input file, to load and search for Application/type
31  // - the --app command line argument
32  // - The Application/type= hit command line argument
33  CommandLine cl(argc, argv);
34  cl.parse();
35  auto command_line_params = emptyInputParameters();
36  MooseApp::addInputParam(command_line_params);
37  MooseApp::addAppParam(command_line_params);
38  cl.populateCommandLineParams(command_line_params);
39 
40  // Do not allow overriding Application/type= for subapps
41  for (const auto & arg : cl.getArguments())
42  if (std::regex_match(arg, std::regex("[A-Za-z0-9]*:Application/.*")))
43  mooseError(
44  "For command line argument '",
45  arg,
46  "': overriding the application type for MultiApps via command line is not allowed.");
47 
48  // Parse the input file; this will set Parser::getAppType() if Application/type= is found
49  const auto & input_filenames = command_line_params.get<std::vector<std::string>>("input_file");
50  auto parser = std::make_unique<Parser>(input_filenames);
51  parser->setAppType(default_app_type);
52  if (input_filenames.size())
53  parser->parse();
54 
55  // Search the command line for either --app or Application/type and let the last one win
56  for (const auto & entry : std::as_const(cl).getEntries())
57  if (!entry.subapp_name && entry.value &&
58  (entry.name == "--app" || entry.name == "Application/type"))
59  parser->setAppType(*entry.value);
60 
61  const auto & app_type = parser->getAppType();
62  if (!AppFactory::instance().isRegistered(app_type))
63  mooseError("'", app_type, "' is not a registered application type.");
64 
65  // Create an instance of the application and store it in a smart pointer for easy cleanup
66  return AppFactory::createAppShared(argc, argv, std::move(parser));
67 }
68 }
std::shared_ptr< MooseApp > createMooseApp(const std::string &default_app_type, int argc, char *argv[])
Create a MooseApp from command-line arguments.
Definition: MooseMain.C:27
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
static void addInputParam(InputParameters &params)
Definition: MooseApp.C:101
InputParameters emptyInputParameters()
This class wraps provides and tracks access to command line parameters.
Definition: CommandLine.h:29
void populateCommandLineParams(InputParameters &params)
Populates the command line input parameters from params.
Definition: CommandLine.C:294
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:60
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:69
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
const std::vector< std::string > & getArguments()
Definition: CommandLine.h:132