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