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::unique_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  auto command_line_params = emptyInputParameters();
34  {
35  CommandLine cl(argc, argv);
36  cl.parse();
37  MooseApp::addInputParam(command_line_params);
38  MooseApp::addAppParam(command_line_params);
39  cl.populateCommandLineParams(command_line_params);
40 
41  // Do not allow overriding Application/type= for subapps
42  for (const auto & arg : cl.getArguments())
43  if (std::regex_match(arg, std::regex("[A-Za-z0-9]*:Application/.*")))
44  mooseError(
45  "For command line argument '",
46  arg,
47  "': overriding the application type for MultiApps via command line is not allowed.");
48  }
49  const auto & input_filenames = command_line_params.get<std::vector<std::string>>("input_file");
50 
51  // Parse command line arguments so that we can get the "--app" entry (if any) and the HIT
52  // command line arguments for the Parser
53  auto command_line = std::make_unique<CommandLine>(argc, argv);
54  command_line->parse();
55 
56  // Setup the parser with the input and the HIT parameters from the command line. The parse
57  // will also look for "Application/type=" in input to specify the application type
58  auto parser = std::make_unique<Parser>(input_filenames);
59  parser->setAppType(default_app_type);
60  parser->setCommandLineParams(command_line->buildHitParams());
61  parser->parse();
62 
63  // Search the command line for either --app or Application/type and let the last one win
64  for (const auto & entry : std::as_const(*command_line).getEntries())
65  if (!entry.subapp_name && entry.value &&
66  (entry.name == "--app" || entry.name == "Application/type"))
67  parser->setAppType(*entry.value);
68 
69  const auto & app_type = parser->getAppType();
70  if (!AppFactory::instance().isRegistered(app_type))
71  mooseError("'", app_type, "' is not a registered application type.");
72 
73  // Create an instance of the application and store it in a smart pointer for easy cleanup
74  return AppFactory::create(std::move(parser), std::move(command_line));
75 }
76 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:333
static void addInputParam(InputParameters &params)
Definition: MooseApp.C:102
InputParameters emptyInputParameters()
This class wraps provides and tracks access to command line parameters.
Definition: CommandLine.h:29
static std::unique_ptr< MooseApp > create(const std::string &app_type, const std::vector< std::string > &cli_args={})
Create an app with no input and command line arguments.
Definition: AppFactory.C:62
void populateCommandLineParams(InputParameters &params)
Populates the command line input parameters from params.
Definition: CommandLine.C:295
static AppFactory & instance()
Get the instance of the AppFactory.
Definition: AppFactory.C:18
static void addAppParam(InputParameters &params)
Definition: MooseApp.C:95
std::unique_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 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