www.mooseframework.org
MultiApp.h
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 #pragma once
11 
12 #include "MooseObject.h"
13 #include "SetupInterface.h"
14 #include "Restartable.h"
15 
16 #include "libmesh/communicator.h"
17 #include "libmesh/point.h"
18 
19 class MultiApp;
20 class UserObject;
21 class FEProblemBase;
22 class FEProblem;
23 class Executioner;
24 class MooseApp;
25 class Backup;
26 
27 // libMesh forward declarations
28 namespace libMesh
29 {
30 class BoundingBox;
31 namespace MeshTools
32 {
33 class BoundingBox;
34 }
35 template <typename T>
36 class NumericVector;
37 } // namespace libMesh
38 
39 template <>
41 
45 class SubAppBackups : public std::vector<std::shared_ptr<Backup>>
46 {
47 };
48 
57 class MultiApp : public MooseObject, public SetupInterface, public Restartable
58 {
59 public:
61 
62  virtual void preExecute() {}
63 
67  virtual void finalize();
68 
72  virtual void postExecute();
73 
77  void setupPositions();
78 
79  virtual void initialSetup() override;
80 
85  virtual void preTransfer(Real dt, Real target_time);
86 
100  virtual bool solveStep(Real dt, Real target_time, bool auto_advance = true) = 0;
101 
108  virtual void incrementTStep(Real /*target_time*/) {}
109 
116  virtual void finishStep() {}
117 
123  virtual void backup();
124 
130  virtual void restore();
131 
136  virtual bool needsRestoration() { return true; }
137 
142  virtual Executioner * getExecutioner(unsigned int app);
143 
154  virtual BoundingBox getBoundingBox(unsigned int app, bool displaced_mesh);
155 
160 
165  FEProblemBase & appProblemBase(unsigned int app);
166 
171  FEProblem & appProblem(unsigned int app);
172 
178  const UserObject & appUserObjectBase(unsigned int app, const std::string & name);
179 
185  Real appPostprocessorValue(unsigned int app, const std::string & name);
186 
195  virtual NumericVector<Number> & appTransferVector(unsigned int app, std::string var_name);
196 
200  unsigned int numGlobalApps() { return _total_num_apps; }
201 
205  unsigned int numLocalApps() { return _apps.size(); }
206 
210  unsigned int firstLocalApp() { return _first_local_app; }
211 
215  bool hasApp() { return _has_an_app; }
216 
222  bool hasLocalApp(unsigned int global_app);
223 
228  MooseApp * localApp(unsigned int local_app);
229 
235  Point position(unsigned int app) { return _positions[app]; }
236 
249  virtual void resetApp(unsigned int global_app, Real time = 0.0);
250 
257  virtual void moveApp(unsigned int global_app, Point p);
258 
263  virtual void parentOutputPositionChanged();
264 
269  MPI_Comm & comm() { return _my_comm; }
270 
275  bool isRootProcessor() { return _my_rank == 0; }
276 
277 protected:
281  virtual void fillPositions();
282 
289  void createApp(unsigned int i, Real start_time);
290 
296  void buildComm();
297 
305  unsigned int globalAppToLocal(unsigned int global_app);
306 
308  virtual void preRunInputFile();
309 
315  virtual std::string getCommandLineArgsParamHelper(unsigned int local_app);
316 
322  void init(unsigned int num);
323 
326 
328  std::string _app_type;
329 
331  std::vector<Point> _positions;
332 
334  const bool _use_positions;
335 
337  std::vector<FileName> _input_files;
338 
340  std::string _output_base;
341 
343  unsigned int _total_num_apps;
344 
346  unsigned int _my_num_apps;
347 
349  unsigned int _first_local_app;
350 
352  const MPI_Comm & _orig_comm;
353 
355  libMesh::Parallel::Communicator _my_communicator;
356 
358  MPI_Comm & _my_comm;
359 
362 
365 
367  std::string _node_name;
368 
370  int _my_rank;
371 
373  std::vector<std::shared_ptr<MooseApp>> _apps;
374 
376  std::vector<bool> _has_bounding_box;
377 
379  std::vector<BoundingBox> _bounding_box;
380 
383 
386 
388  unsigned int _max_procs_per_app;
389 
392 
395 
398 
400  std::vector<unsigned int> _reset_apps;
401 
404 
407 
409  std::vector<unsigned int> _move_apps;
410 
412  std::vector<Point> _move_positions;
413 
416 
419 
422 
424  const std::vector<std::string> & _cli_args;
425 };
426 
427 template <>
428 inline void
429 dataStore(std::ostream & stream, SubAppBackups & backups, void * context)
430 {
431  MultiApp * multi_app = static_cast<MultiApp *>(context);
432 
433  multi_app->backup();
434 
435  if (!multi_app)
436  mooseError("Error storing std::vector<Backup*>");
437 
438  for (unsigned int i = 0; i < backups.size(); i++)
439  dataStore(stream, backups[i], context);
440 }
441 
442 template <>
443 inline void
444 dataLoad(std::istream & stream, SubAppBackups & backups, void * context)
445 {
446  MultiApp * multi_app = static_cast<MultiApp *>(context);
447 
448  if (!multi_app)
449  mooseError("Error loading std::vector<Backup*>");
450 
451  for (unsigned int i = 0; i < backups.size(); i++)
452  dataLoad(stream, backups[i], context);
453 
454  multi_app->restore();
455 }
bool hasLocalApp(unsigned int global_app)
Whether or not the given global app number is on this processor.
Definition: MultiApp.C:535
Point position(unsigned int app)
The physical position of a global App number.
Definition: MultiApp.h:235
std::vector< unsigned int > _reset_apps
The apps to be reset.
Definition: MultiApp.h:400
virtual void restore()
Restore the state of every Sub App.
Definition: MultiApp.C:409
virtual BoundingBox getBoundingBox(unsigned int app, bool displaced_mesh)
Get the BoundingBox for the mesh associated with app The bounding box will be shifted to be in the co...
Definition: MultiApp.C:424
A class for creating restricted objects.
Definition: Restartable.h:29
MultiApp(const InputParameters &parameters)
Definition: MultiApp.C:171
virtual void finishStep()
Calls multi-apps executioners&#39; endStep and postStep methods which creates output and advances time (n...
Definition: MultiApp.h:116
unsigned int _max_procs_per_app
Maximum number of processors to give to each app.
Definition: MultiApp.h:388
virtual void backup()
Save off the state of every Sub App.
Definition: MultiApp.C:400
const bool _use_positions
Toggle use of "positions".
Definition: MultiApp.h:334
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition: FEProblem.h:24
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
void setupPositions()
Called just after construction to allow derived classes to set _positions;.
Definition: MultiApp.C:223
Real _move_time
The time at which to move apps.
Definition: MultiApp.h:406
unsigned int numLocalApps()
Definition: MultiApp.h:205
Real appPostprocessorValue(unsigned int app, const std::string &name)
Get a Postprocessor value for a specified global app.
Definition: MultiApp.C:520
int _orig_rank
The mpi "rank" of this processor in the original communicator.
Definition: MultiApp.h:364
virtual std::string getCommandLineArgsParamHelper(unsigned int local_app)
Method to aid in getting the "cli_args" parameters.
Definition: MultiApp.C:688
std::vector< std::shared_ptr< MooseApp > > _apps
Pointers to each of the Apps.
Definition: MultiApp.h:373
virtual void parentOutputPositionChanged()
For apps outputting in position we need to change their output positions if their parent app moves...
Definition: MultiApp.C:588
std::vector< Point > _move_positions
The new positions for the apps to be moved.
Definition: MultiApp.h:412
Base class for MOOSE-based applications.
Definition: MooseApp.h:59
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void dataLoad(std::istream &stream, SubAppBackups &backups, void *context)
Definition: MultiApp.h:444
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
Real _inflation
Relative bounding box inflation.
Definition: MultiApp.h:382
FEProblemBase & _fe_problem
The FEProblemBase this MultiApp is part of.
Definition: MultiApp.h:325
bool _reset_happened
Whether or not apps have been reset.
Definition: MultiApp.h:403
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
int _orig_num_procs
The number of processors in the original comm.
Definition: MultiApp.h:361
virtual void preTransfer(Real dt, Real target_time)
Gets called just before transfers are done to the MultiApp (Which is just before the MultiApp is solv...
Definition: MultiApp.C:346
virtual NumericVector< Number > & appTransferVector(unsigned int app, std::string var_name)
Get the vector to transfer to for this MultiApp.
Definition: MultiApp.C:529
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:42
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseObject.h:65
virtual void fillPositions()
must fill in _positions with the positions of the sub-aps
Definition: MultiApp.C:252
bool _move_happened
Whether or not the move has happened.
Definition: MultiApp.h:415
Helper class for holding Sub-app backups.
Definition: MultiApp.h:45
InputParameters validParams< MultiApp >()
Definition: MultiApp.C:43
bool hasApp()
Whether or not this MultiApp has an app on this processor.
Definition: MultiApp.h:215
Real _reset_time
The time at which to reset apps.
Definition: MultiApp.h:397
std::string _output_base
The output file basename for each multiapp.
Definition: MultiApp.h:340
std::string _app_type
The type of application to build.
Definition: MultiApp.h:328
bool isRootProcessor()
Whether or not this processor is the "root" processor for the sub communicator.
Definition: MultiApp.h:275
FEProblemBase & appProblemBase(unsigned int app)
Get the FEProblemBase for the global app is part of.
Definition: MultiApp.C:487
std::vector< unsigned int > _move_apps
The apps to be moved.
Definition: MultiApp.h:409
Executioners are objects that do the actual work of solving your problem.
Definition: Executioner.h:32
void buildComm()
Create an MPI communicator suitable for each app.
Definition: MultiApp.C:701
virtual void resetApp(unsigned int global_app, Real time=0.0)
"Reset" the App corresponding to the global App number passed in.
Definition: MultiApp.C:552
unsigned int _total_num_apps
The total number of apps to simulate.
Definition: MultiApp.h:343
std::vector< BoundingBox > _bounding_box
This multi-app&#39;s bounding box.
Definition: MultiApp.h:379
virtual bool solveStep(Real dt, Real target_time, bool auto_advance=true)=0
Re-solve all of the Apps.
unsigned int numGlobalApps()
Definition: MultiApp.h:200
unsigned int _my_num_apps
The number of apps this object is involved in simulating.
Definition: MultiApp.h:346
std::string _node_name
Node Name.
Definition: MultiApp.h:367
bool _has_an_app
Whether or not this processor as an App at all
Definition: MultiApp.h:418
SubAppBackups & _backups
Backups for each local App.
Definition: MultiApp.h:421
libMesh::Parallel::Communicator _my_communicator
The communicator object that holds the MPI_Comm that we&#39;re going to use.
Definition: MultiApp.h:355
void createApp(unsigned int i, Real start_time)
Helper function for creating an App instance.
Definition: MultiApp.C:596
unsigned int _first_local_app
The number of the first app on this processor.
Definition: MultiApp.h:349
Helper class to hold streams for Backup and Restore operations.
Definition: Backup.h:20
virtual void preExecute()
Definition: MultiApp.h:62
unsigned int firstLocalApp()
Definition: MultiApp.h:210
MooseApp * localApp(unsigned int local_app)
Get the local MooseApp object.
Definition: MultiApp.C:545
virtual void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job...
Definition: MultiApp.C:233
void init(unsigned int num)
Initialize the MultiApp by creating the provided number of apps.
Definition: MultiApp.C:206
virtual bool needsRestoration()
Whether or not this MultiApp should be restored at the beginning of each Picard iteration.
Definition: MultiApp.h:136
MPI_Comm & comm()
Get the MPI communicator this MultiApp is operating on.
Definition: MultiApp.h:269
FEProblem & appProblem(unsigned int app)
Get the FEProblem for the global app is part of.
Definition: MultiApp.C:498
virtual void postExecute()
Method called at the end of the simulation (after finalize)
Definition: MultiApp.C:388
bool _output_in_position
Whether or not to move the output of the MultiApp into position.
Definition: MultiApp.h:391
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
std::vector< Point > _positions
The positions of all of the apps.
Definition: MultiApp.h:331
virtual void finalize()
Method called towards the end of the simulation to execute on final.
Definition: MultiApp.C:375
int _my_rank
The mpi "rank" of this processor in the sub communicator.
Definition: MultiApp.h:370
std::vector< FileName > _input_files
The input file for each app&#39;s simulation.
Definition: MultiApp.h:337
virtual void preRunInputFile()
call back executed right before app->runInputFile()
Definition: MultiApp.C:807
Point _bounding_box_padding
Additional padding added to the bounding box, useful for 1D meshes.
Definition: MultiApp.h:385
const Real _global_time_offset
The offset time so the MultiApp local time relative to the global time.
Definition: MultiApp.h:394
const MPI_Comm & _orig_comm
The original comm handle.
Definition: MultiApp.h:352
virtual void incrementTStep(Real)
Advances the multi-apps time step which is important for dt selection.
Definition: MultiApp.h:108
A MultiApp represents one or more MOOSE applications that are running simultaneously.
Definition: MultiApp.h:57
FEProblemBase & problemBase()
Get the FEProblemBase this MultiApp is part of.
Definition: MultiApp.h:159
MPI_Comm & _my_comm
The MPI communicator this object is going to use.
Definition: MultiApp.h:358
virtual void moveApp(unsigned int global_app, Point p)
Move the global_app to Point p.
Definition: MultiApp.C:571
virtual Executioner * getExecutioner(unsigned int app)
Definition: MultiApp.C:366
Base class for user-specific data.
Definition: UserObject.h:37
const std::vector< std::string > & _cli_args
Storage for command line arguments.
Definition: MultiApp.h:424
const UserObject & appUserObjectBase(unsigned int app, const std::string &name)
Get a UserObject base for a specific global app.
Definition: MultiApp.C:511
unsigned int globalAppToLocal(unsigned int global_app)
Map a global App number to the local number.
Definition: MultiApp.C:797
void dataStore(std::ostream &stream, SubAppBackups &backups, void *context)
Definition: MultiApp.h:429
std::vector< bool > _has_bounding_box
Flag if this multi-app computed its bounding box (valid only for non-displaced meshes) ...
Definition: MultiApp.h:376