https://mooseframework.inl.gov
Loading...
Searching...
No Matches
SamplerTransientMultiApp.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// StochasticTools includes
12#include "Sampler.h"
15
17
20{
23 params.addClassDescription("Creates a sub-application for each row of each Sampler matrix.");
24 params.addRequiredParam<SamplerName>("sampler",
25 "The Sampler object to utilize for creating MultiApps.");
26 params.suppressParameter<std::vector<Point>>("positions");
27 params.suppressParameter<bool>("output_in_position");
28 params.suppressParameter<std::vector<FileName>>("positions_file");
29 params.suppressParameter<Real>("move_time");
30 params.suppressParameter<std::vector<Point>>("move_positions");
31 params.suppressParameter<std::vector<unsigned int>>("move_apps");
32 params.set<bool>("use_positions") = false;
33
34 // use "batch-restore=2" to be consistent with SamplerFullSolveMultiApp and use the
35 // allow_out_of_range flag to allow the StochasticToolsTransfer object to inspect the MultiApp
36 // object parameters without triggering an assert.
37 MooseEnum modes("normal=0 batch-reset=1 batch-restore=2", "normal");
38 params.addParam<MooseEnum>(
39 "mode",
40 modes,
41 "The operation mode, 'normal' creates one sub-application for each row in the Sampler and "
42 "'batch-reset' and 'batch-restore' creates N sub-applications, where N is the minimum of "
43 "'num_rows' in the Sampler and floor(number of processes / min_procs_per_app). To run "
44 "the rows in the Sampler, 'batch-reset' will destroy and re-create sub-apps as needed, "
45 "whereas the 'batch-restore' will backup and restore sub-apps to the initial state prior "
46 "to execution, without destruction.");
47
48 return params;
49}
50
52 : TransientMultiApp(parameters),
53 SamplerInterface(this),
54 _sampler(getSampler("sampler")),
55 _mode(getParam<MooseEnum>("mode").getEnum<StochasticTools::MultiAppMode>()),
56 _local_batch_app_index(0),
57 _number_of_sampler_rows(_sampler.getNumberOfRows())
58{
60 paramError("mode",
61 "The supplied mode, '",
62 getParam<MooseEnum>("mode"),
63 "', currently is not implemented for the SamplerTransientMultiApp, the available "
64 "options are 'normal' or 'batch-restore'.");
65
66 if (getParam<unsigned int>("min_procs_per_app") !=
67 _sampler.getParam<unsigned int>("min_procs_per_row") ||
68 getParam<unsigned int>("max_procs_per_app") !=
69 _sampler.getParam<unsigned int>("max_procs_per_row"))
70 paramError("sampler",
71 "Sampler and multiapp communicator configuration inconsistent. Please ensure that "
72 "'MultiApps/",
73 name(),
74 "/min(max)_procs_per_app' and 'Samplers/",
75 _sampler.name(),
76 "/min(max)_procs_per_row' are the same.");
77
81}
82
83void
85{
86 TIME_SECTION("initialSetup", 2, "Setting Up SamplerTransientMultiApp");
87
89
90 // Perform initial backup for the batch sub-applications
92 {
93 dof_id_type n = _rank_config.num_local_sims;
94 _batch_backup.resize(n);
95 for (MooseIndex(n) i = 0; i < n; ++i)
96 for (MooseIndex(_my_num_apps) j = 0; j < _my_num_apps; j++)
97 _batch_backup[i].emplace_back(_apps[j]->backup());
98 }
99}
100
101bool
102SamplerTransientMultiApp::solveStep(Real dt, Real target_time, bool auto_advance)
103{
104 TIME_SECTION("solveStep", 3, "Solving SamplerTransientMultiApp");
105
107 mooseError("The size of the sampler has changed; SamplerTransientMultiApp object do not "
108 "support dynamic Sampler output.");
109
110 bool last_solve_converged = true;
112 last_solve_converged = solveStepBatch(dt, target_time, auto_advance);
113 else
114 last_solve_converged = TransientMultiApp::solveStep(dt, target_time, auto_advance);
115 return last_solve_converged;
116}
117
118bool
119SamplerTransientMultiApp::solveStepBatch(Real dt, Real target_time, bool auto_advance)
120{
121 // Value to return
122 bool last_solve_converged = true;
123
124 // List of active relevant Transfer objects
125 std::vector<std::shared_ptr<StochasticToolsTransfer>> to_transfers =
127 std::vector<std::shared_ptr<StochasticToolsTransfer>> from_transfers =
129
130 // Initialize to/from transfers
131 for (auto transfer : to_transfers)
132 {
133 transfer->setGlobalMultiAppIndex(_rank_config.first_local_app_index);
134 transfer->initializeToMultiapp();
135 }
136 for (auto transfer : from_transfers)
137 {
138 transfer->setGlobalMultiAppIndex(_rank_config.first_local_app_index);
139 transfer->initializeFromMultiapp();
140 }
141
142 // Perform batch MultiApp solves
144 for (dof_id_type i = _rank_config.first_local_sim_index;
146 ++i)
147 {
149
151 for (MooseIndex(_my_num_apps) j = 0; j < _my_num_apps; j++)
152 {
153 _apps[j]->restore(std::move(_batch_backup[_local_batch_app_index][j]), false);
154 _apps[j]->finalizeRestore();
155 }
156
158 i,
159 _row_data,
162 _console);
163
164 // Set the file base based on the current row
165 for (unsigned int ai = 0; ai < _my_num_apps; ++ai)
166 {
167 const std::string mname = getMultiAppName(name(), i, _number_of_sampler_rows);
168 _apps[ai]->setOutputFileBase(_app.getOutputFileBase() + "_" + mname);
169 }
170
171 const bool curr_last_solve_converged =
172 TransientMultiApp::solveStep(dt, target_time, auto_advance);
173 last_solve_converged = last_solve_converged && curr_last_solve_converged;
174
176 i,
177 _row_data,
180 _console);
181
182 incrementTStep(target_time);
183
185 for (MooseIndex(_my_num_apps) j = 0; j < _my_num_apps; j++)
187
189 }
191
192 // Finalize to/from transfers
193 for (auto transfer : to_transfers)
194 transfer->finalizeToMultiapp();
195 for (auto transfer : from_transfers)
196 transfer->finalizeFromMultiapp();
197
198 return last_solve_converged;
199}
200
201std::vector<std::shared_ptr<StochasticToolsTransfer>>
203{
204 std::vector<std::shared_ptr<StochasticToolsTransfer>> output;
205 const ExecuteMooseObjectWarehouse<Transfer> & warehouse =
207 for (std::shared_ptr<Transfer> transfer : warehouse.getActiveObjects())
208 {
209 std::shared_ptr<StochasticToolsTransfer> ptr =
210 std::dynamic_pointer_cast<StochasticToolsTransfer>(transfer);
211 if (ptr && ptr->getMultiApp().get() == this)
212 output.push_back(ptr);
213 }
214 return output;
215}
216
217std::vector<std::string>
219{
220 std::vector<std::string> args;
221
222 // With multiple processors per app, there are no local rows for non-root processors
223 if (isRootProcessor())
224 {
225 // Since we only store param_names in cli_args, we need to find the values for each param from
226 // sampler data and combine them to get full command line option strings.
231 }
232
234 return args;
235}
236
237void
239{
240 if (!isRootProcessor())
241 return;
242
243 mooseAssert(local_index < _sampler.getNumberOfLocalRows(),
244 "Local index must be less than number of local rows.");
245
246 if (_row_data.empty() ||
247 (_local_row_index == _sampler.getNumberOfLocalRows() - 1 && local_index == 0))
248 {
249 mooseAssert(local_index == 0,
250 "The first time calling updateRowData must have a local index of 0.");
253 }
254 else if (local_index - _local_row_index == 1)
255 {
258 }
259
260 mooseAssert(local_index == _local_row_index,
261 "Local index must be equal or one greater than the index previously called.");
262}
registerMooseObject("StochasticToolsApp", SamplerTransientMultiApp)
const ConsoleStream _console
bool verboseMultiApps() const
const ExecuteMooseObjectWarehouse< Transfer > & getMultiAppTransferWarehouse(Transfer::DIRECTION direction) const
void suppressParameter(const std::string &name)
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
T & set(const std::string &name, bool quiet_mode=false)
std::string getOutputFileBase(bool for_non_moose_build_output=false) const
const std::string & name() const
void paramError(const std::string &param, Args... args) const
void mooseError(Args &&... args) const
const T & getParam(const std::string &name) const
const std::vector< std::shared_ptr< T > > & getActiveObjects(THREAD_ID tid=0) const
libMesh::Parallel::Communicator _my_communicator
static std::string getMultiAppName(const std::string &base_name, dof_id_type index, dof_id_type total)
virtual void backup()
void init(unsigned int num_apps, bool batch_mode=false)
FEProblemBase & _fe_problem
std::vector< std::shared_ptr< MooseApp > > _apps
virtual std::vector< std::string > getCommandLineArgs(const unsigned int local_app)
LocalRankConfig _rank_config
unsigned int _my_num_apps
static std::vector< std::string > sampledCommandLineArgs(const std::vector< Real > &row, const std::vector< std::string > &full_args_name)
Helper for inserting row data into commandline arguments Used here and in SamplerTransientMultiApp.
static void execBatchTransfers(const std::vector< std::shared_ptr< StochasticToolsTransfer > > &transfers, dof_id_type global_row_index, const std::vector< Real > &row_data, Transfer::DIRECTION direction, bool verbose, const ConsoleStream &console)
Helper for executing transfers when doing batch stochastic simulations.
static InputParameters validParams()
virtual bool solveStep(Real dt, Real target_time, bool auto_advance=true) override
Override solveStep to allow for batch execution.
std::vector< Real > _row_data
Current row of data updated by updateRowData. Used by transfers and setting command line args.
const dof_id_type _number_of_sampler_rows
Store the number of rows initialized, if this changes error because it doesn't make sense.
virtual void initialSetup() override
Override to initialize batch backups.
void updateRowData(dof_id_type local_index)
Helper function for updating _row_data and _local_row_index.
dof_id_type _local_row_index
Current local index representing _row_data.
bool solveStepBatch(Real dt, Real target_time, bool auto_advance=true)
Helper method for running in mode='batch'.
Sampler & _sampler
Sampler to utilize for creating MultiApps.
const StochasticTools::MultiAppMode _mode
The Sup-application solve mode.
virtual std::vector< std::string > getCommandLineArgs(const unsigned int local_app) override
Override to allow to get correct cli_args.
std::vector< std::shared_ptr< StochasticToolsTransfer > > getActiveStochasticToolsTransfers(Transfer::DIRECTION direction)
Helper for getting StochasticToolsTransfer objects.
static InputParameters validParams()
dof_id_type _local_batch_app_index
Counter for extracting command line arguments in batch mode.
std::vector< std::vector< std::unique_ptr< Backup > > > _batch_backup
Storage for batch-restore mode; the outer vector if for the local stochastic data and the inner vecto...
SamplerTransientMultiApp(const InputParameters &parameters)
std::vector< Real > getNextLocalRow()
dof_id_type getNumberOfLocalRows() const
dof_id_type getNumberOfRows() const
const LocalRankConfig & getRankConfig(bool batch_mode) const
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
virtual void initialSetup() override
virtual void incrementTStep(Real target_time) override
virtual bool solveStep(Real dt, Real target_time, bool auto_advance=true) override
static InputParameters validParams()
Enum for batch type in stochastic tools MultiApp.
dof_id_type num_local_sims
dof_id_type first_local_sim_index
dof_id_type first_local_app_index