www.mooseframework.org
MultiAppCommandLineControl.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 // MOOSE includes
12 #include "Function.h"
13 #include "Sampler.h"
14 #include "MultiApp.h"
16 
17 registerMooseObject("StochasticToolsApp", MultiAppCommandLineControl);
18 
19 template <>
20 InputParameters
22 {
23  InputParameters params = validParams<Control>();
24  params += validParams<SamplerInterface>();
25  params.addClassDescription("Control for modifying the command line arguments of MultiApps.");
26 
27  // Set and suppress the 'execute_on' flag, it doesn't work with any other flag
28  params.set<ExecFlagEnum>("execute_on") = {EXEC_PRE_MULTIAPP_SETUP};
29  params.suppressParameter<ExecFlagEnum>("execute_on");
30 
31  params.addRequiredParam<MultiAppName>("multi_app", "The name of the MultiApp to control.");
32  params.addParam<SamplerName>(
33  "sampler",
34  "The Sampler object to utilize for altering the command line options of the MultiApp.");
35  params.addParam<std::vector<std::string>>(
36  "param_names", "The names of the command line parameters to set via the sampled data.");
37 
38  return params;
39 }
40 
41 MultiAppCommandLineControl::MultiAppCommandLineControl(const InputParameters & parameters)
42  : Control(parameters),
43  SamplerInterface(this),
44  _multi_app(_fe_problem.getMultiApp(getParam<MultiAppName>("multi_app"))),
45  _sampler(SamplerInterface::getSampler("sampler")),
46  _param_names(getParam<std::vector<std::string>>("param_names"))
47 {
48  if (!_sampler.getParam<ExecFlagEnum>("execute_on").contains(EXEC_PRE_MULTIAPP_SETUP))
49  _sampler.paramError(
50  "execute_on",
51  "The sampler object, '",
52  _sampler.name(),
53  "', is being used by the '",
54  name(),
55  "' object, thus the 'execute_on' of the sampler must include 'PRE_MULTIAPP_SETUP'.");
56 
57  bool batch_reset = _multi_app->isParamValid("mode") &&
58  (_multi_app->getParam<MooseEnum>("mode") == "batch-reset");
59  bool batch_restore = _multi_app->isParamValid("mode") &&
60  (_multi_app->getParam<MooseEnum>("mode") == "batch-restore");
61  if (batch_reset)
62  ; // Do not perform the App count test, because in batch mode there is only one
63 
64  else if (batch_restore)
65  _multi_app->paramError(
66  "mode",
67  "The MultiApp object, '",
68  _multi_app->name(),
69  "', supplied to the '",
70  name(),
71  "' object is setup to run in 'batch-restore' mode, when using this mode command line "
72  "arguments cannot be modified; batch-reset mode should be used instead.");
73 
74  else if (_multi_app->numGlobalApps() != _sampler.getTotalNumberOfRows())
75  mooseError("The number of sub apps (",
76  _multi_app->numGlobalApps(),
77  ") created by MultiApp object '",
78  _multi_app->name(),
79  "' must be equal to the number for rows (",
80  _sampler.getTotalNumberOfRows(),
81  ") for the '",
82  _sampler.name(),
83  "' Sampler object.");
84 }
85 
86 void
88 {
89  // Do not put anything here, this method is being called after execute because the execute_on
90  // is set to PRE_MULTIAPP_SETUP for this class. It won't work any other way.
91 }
92 
93 void
95 {
96  std::vector<std::string> cli_args;
97  std::vector<DenseMatrix<Real>> samples = _sampler.getSamples();
98 
99  for (const DenseMatrix<Real> & matrix : samples)
100  {
101  if (matrix.n() != _param_names.size())
102  paramError("param_names",
103  "The number of columns (",
104  matrix.n(),
105  ") must match the number of parameters (",
106  _param_names.size(),
107  ").");
108 
109  for (unsigned int row = 0; row < matrix.m(); ++row)
110  {
111  std::ostringstream oss;
112  for (unsigned int col = 0; col < matrix.n(); ++col)
113  {
114  if (col > 0)
115  oss << ";";
116  oss << _param_names[col] << "=" << Moose::stringify(matrix(row, col));
117  }
118 
119  cli_args.push_back(oss.str());
120  }
121  }
122 
123  setControllableValueByName<std::vector<std::string>>(
124  "MultiApp", _multi_app->name(), "cli_args", cli_args);
125 }
virtual void initialSetup() override final
Do not allow the use of initialSetup, because this class is designed to operate on PRE_MULTIAPP_SETUP...
InputParameters validParams< MultiAppCommandLineControl >()
std::shared_ptr< MultiApp > _multi_app
The MultiApp this Transfer is transferring data to or from.
A Control object for receiving data from a master application Sampler object.
Sampler & _sampler
Sampler to utilize for creating MultiApps.
const std::string name
Definition: Setup.h:21
MultiAppCommandLineControl(const InputParameters &parameters)
const std::vector< std::string > & _param_names
Storage for the parameter names to be applied.
registerMooseObject("StochasticToolsApp", MultiAppCommandLineControl)