https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MultiApp.h
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#pragma once
11
12#include "MooseObject.h"
13#include "SetupInterface.h"
14#include "Restartable.h"
15#include "PerfGraphInterface.h"
16#include "Backup.h"
17
18#include "libmesh/communicator.h"
19#include "libmesh/point.h"
20
21#include "nlohmann/json.h"
22
23class UserObject;
24class FEProblemBase;
25class FEProblem;
26class Executioner;
27class MooseApp;
30class Positions;
31
32// libMesh forward declarations
33namespace libMesh
34{
35class BoundingBox;
36namespace MeshTools
37{
38class BoundingBox;
39}
40template <typename T>
41class NumericVector;
42} // namespace libMesh
43
47{
49 dof_id_type num_local_sims;
58 dof_id_type num_local_apps;
74 processor_id_type my_first_rank;
75};
76
77template <>
78inline void
79dataStore(std::ostream & stream, LocalRankConfig & v, void * context)
80{
81 dataStore(stream, v.num_local_sims, context);
82 dataStore(stream, v.first_local_sim_index, context);
83 dataStore(stream, v.num_local_apps, context);
84 dataStore(stream, v.first_local_app_index, context);
85 dataStore(stream, v.is_first_local_rank, context);
86 dataStore(stream, v.my_first_rank, context);
87}
88
89template <>
90inline void
91dataLoad(std::istream & stream, LocalRankConfig & v, void * context)
92{
93 dataLoad(stream, v.num_local_sims, context);
94 dataLoad(stream, v.first_local_sim_index, context);
95 dataLoad(stream, v.num_local_apps, context);
96 dataLoad(stream, v.first_local_app_index, context);
97 dataLoad(stream, v.is_first_local_rank, context);
98 dataLoad(stream, v.my_first_rank, context);
99}
100
112LocalRankConfig rankConfig(processor_id_type rank,
113 processor_id_type nprocs,
114 dof_id_type napps,
115 processor_id_type min_app_procs,
116 processor_id_type max_app_procs,
117 bool batch_mode = false);
118
124class SubAppBackups : public std::vector<std::unique_ptr<Backup>>
125{
126};
127
136class MultiApp : public MooseObject,
137 public SetupInterface,
138 public Restartable,
139 public PerfGraphInterface
140{
141public:
143
145
146 virtual void preExecute() {}
147
151 virtual void finalize();
152
156 virtual void postExecute();
157
163
170 void init(unsigned int num_apps, const LocalRankConfig & config);
171
176 virtual void createLocalApp(const unsigned int i);
177
181 virtual void initialSetup() override;
182
187 virtual void preTransfer(Real dt, Real target_time);
188
202 virtual bool solveStep(Real dt, Real target_time, bool auto_advance = true) = 0;
203
210 virtual void incrementTStep(Real /*target_time*/) {}
211
220 virtual void finishStep(bool /*recurse_through_multiapp_levels*/ = false) {}
221
227 virtual void backup();
228
236 virtual void restore(bool force = true);
237
242 bool needsRestoration() { return !_no_restore; }
243
248 virtual Executioner * getExecutioner(unsigned int app);
249
262 getBoundingBox(unsigned int app,
263 bool displaced_mesh,
264 const MultiAppCoordTransform * coord_transform = nullptr);
265
270
275 FEProblemBase & appProblemBase(unsigned int app);
276
281 FEProblem & appProblem(unsigned int app);
282
288 const UserObject & appUserObjectBase(unsigned int app, const std::string & name);
289
295 Real appPostprocessorValue(unsigned int app, const std::string & name);
296
305 std::string var_name);
306
310 unsigned int numGlobalApps() const { return _total_num_apps; }
311
315 unsigned int numLocalApps() { return _apps.size(); }
316
320 unsigned int firstLocalApp() { return _first_local_app; }
321
325 bool isFirstLocalRank() const;
326
330 bool hasApp() { return _has_an_app; }
331
337 bool hasLocalApp(unsigned int global_app) const;
338
343 MooseApp * localApp(unsigned int local_app);
344
350 const Point & position(unsigned int app) const;
351
364 virtual void resetApp(unsigned int global_app, Real time = 0.0);
365
372 virtual void moveApp(unsigned int global_app, Point p);
373
378 virtual void parentOutputPositionChanged();
379
384 MPI_Comm & comm() { return _my_comm; }
385
390 bool isRootProcessor() { return _my_rank == 0; }
391
396 bool usingPositions() const { return _use_positions; }
397
402 bool runningInPosition() const { return _run_in_position; }
403
408
414 const MultiAppCoordTransform & transform);
415
420
421protected:
423 virtual std::vector<std::string> cliArgs() const;
424
428 virtual void fillPositions();
429
434
441 void createApp(unsigned int i, Real start_time);
442
454 virtual bool propagateRecoverToSubApps() const { return true; }
455
461 void buildComm();
462
470 unsigned int globalAppToLocal(unsigned int global_app);
471
473 virtual void preRunInputFile();
474
483 virtual std::vector<std::string> getCommandLineArgs(const unsigned int local_app);
484
488 void init(unsigned int num_apps, bool batch_mode = false);
489
495 void createApps();
496
501 void keepSolutionDuringRestore(bool keep_solution_during_restore);
502
509 void setAppOutputFileBase(unsigned int index);
510
519 static std::string
520 getMultiAppName(const std::string & base_name, dof_id_type index, dof_id_type total);
521
524
526 std::string _app_type;
527
529 std::vector<Point> _positions;
531 std::vector<const Positions *> _positions_objs;
533 std::vector<unsigned int> _positions_index_offsets;
534
537 const bool _use_positions;
538
542
544 std::vector<FileName> _input_files;
545
548
550 std::vector<unsigned int> _npositions_inputfile;
551
553 std::string _output_base;
554
556 unsigned int _total_num_apps;
557
559 unsigned int _my_num_apps;
560
562 unsigned int _first_local_app;
563
565 const MPI_Comm & _orig_comm;
566
569
571 MPI_Comm & _my_comm;
572
575
578
580 std::string _node_name;
581
584
586 std::vector<std::shared_ptr<MooseApp>> _apps;
587
589 std::vector<bool> _has_bounding_box;
590
592 std::vector<libMesh::BoundingBox> _bounding_box;
593
596
599
601 processor_id_type _max_procs_per_app;
602
604 processor_id_type _min_procs_per_app;
605
608
611
613 std::vector<Real> _reset_times;
614
616 std::vector<unsigned int> _reset_apps;
617
619 std::vector<bool> _reset_happened;
620
623
625 std::vector<unsigned int> _move_apps;
626
628 std::vector<Point> _move_positions;
629
632
635
637 const std::vector<CLIArgString> & _cli_args;
638
640 std::vector<std::string> _cli_args_from_file;
641
644
647
649 const bool _no_restore;
650
653 std::vector<std::vector<std::unique_ptr<libMesh::NumericVector<Real>>>> _end_solutions;
654
656 std::vector<std::unique_ptr<NumericVector<Real>>> _end_aux_solutions;
657
660
662 std::vector<MultiAppTransfer *> _associated_transfers;
663
666
669
676
677private:
679 mutable std::optional<std::string> _cli_args_param;
680};
681
682void dataStore(std::ostream & stream, SubAppBackups & backups, void * context);
683void dataLoad(std::istream & stream, SubAppBackups & backups, void * context);
unsigned int PerfID
Definition MooseTypes.h:240
LocalRankConfig rankConfig(processor_id_type rank, processor_id_type nprocs, dof_id_type napps, processor_id_type min_app_procs, processor_id_type max_app_procs, bool batch_mode=false)
Returns app partitioning information relevant to the given rank for a multiapp scenario with the give...
Definition MultiApp.C:1388
void dataLoad(std::istream &stream, LocalRankConfig &v, void *context)
Definition MultiApp.h:91
void dataStore(std::ostream &stream, LocalRankConfig &v, void *context)
Definition MultiApp.h:79
Executioners are objects that do the actual work of solving your problem.
Definition Executioner.h:37
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition FEProblem.h:21
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
Base class for MOOSE-based applications.
Definition MooseApp.h:110
const InputParameters & parameters() const
Get the parameters of the object.
Definition MooseBase.h:131
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
This class contains transformation information that only exists in a context in which there are multi...
Base class for all MultiAppTransfer objects.
A MultiApp represents one or more MOOSE applications that are running simultaneously.
Definition MultiApp.h:140
std::vector< Real > _reset_times
The times at which to reset apps.
Definition MultiApp.h:613
bool hasApp()
Whether or not this MultiApp has an app on this processor.
Definition MultiApp.h:330
virtual void postExecute()
Method called at the end of the simulation (after finalize).
Definition MultiApp.C:779
libMesh::Parallel::Communicator _my_communicator
The communicator object that holds the MPI_Comm that we're going to use.
Definition MultiApp.h:568
virtual void finalize()
Method called towards the end of the simulation to execute on final.
Definition MultiApp.C:766
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:684
std::vector< std::string > _cli_args_from_file
CommandLine arguments from files.
Definition MultiApp.h:640
const bool & _wait_for_first_app_init
Whether to create the first app on rank 0 while all other MPI ranks are idle.
Definition MultiApp.h:547
bool usingPositions() const
Whether or not this MultiApp is using positions to track the number and location of the child applica...
Definition MultiApp.h:396
bool runningInPosition() const
Whether or not this MultiApp is being run in position, eg with the coordinate transform already appli...
Definition MultiApp.h:402
virtual std::vector< std::string > cliArgs() const
function that provides cli_args to subapps
Definition MultiApp.C:1545
void buildComm()
Create an MPI communicator suitable for each app.
Definition MultiApp.C:1466
Real _inflation
Relative bounding box inflation.
Definition MultiApp.h:595
unsigned int numLocalApps()
Definition MultiApp.h:315
std::vector< unsigned int > _positions_index_offsets
The offsets, in case multiple Positions objects are specified.
Definition MultiApp.h:533
void keepSolutionDuringRestore(bool keep_solution_during_restore)
Preserve the solution from the previous simulation, and it is used as an initial guess for the next r...
Definition MultiApp.C:924
bool _output_in_position
Whether or not to move the output of the MultiApp into position.
Definition MultiApp.h:607
std::vector< bool > _has_bounding_box
Flag if this multi-app computed its bounding box (valid only for non-displaced meshes)
Definition MultiApp.h:589
processor_id_type _min_procs_per_app
Minimum number of processors to give to each app.
Definition MultiApp.h:604
bool isRootProcessor()
Whether or not this processor is the "root" processor for the sub communicator.
Definition MultiApp.h:390
const PerfID _init_timer
Definition MultiApp.h:672
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:1119
FEProblemBase & appProblemBase(unsigned int app)
Get the FEProblemBase for the global app desired.
Definition MultiApp.C:1048
bool isFirstLocalRank() const
Definition MultiApp.C:1096
std::string _app_type
The type of application to build.
Definition MultiApp.h:526
int _my_rank
The mpi "rank" of this processor in the sub communicator.
Definition MultiApp.h:583
virtual void restore(bool force=true)
Restore the state of every Sub App.
Definition MultiApp.C:806
virtual void moveApp(unsigned int global_app, Point p)
Move the global_app to Point p.
Definition MultiApp.C:1140
const Real _global_time_offset
The offset time so the MultiApp local time relative to the global time.
Definition MultiApp.h:610
Real _move_time
The time at which to move apps.
Definition MultiApp.h:622
bool needsRestoration()
Whether or not this MultiApp should be restored at the beginning of each Picard iteration.
Definition MultiApp.h:242
void init(unsigned int num_apps, const LocalRankConfig &config)
Build communicators and reserve backups using a custom rank configuration.
Definition MultiApp.C:361
const PerfID _backup_timer
Definition MultiApp.h:673
void createApp(unsigned int i, Real start_time)
Helper function for creating an App instance.
Definition MultiApp.C:1167
static std::string getMultiAppName(const std::string &base_name, dof_id_type index, dof_id_type total)
Helper for constructing the name of the multiapp.
Definition MultiApp.C:1561
int _orig_num_procs
The number of processors in the original comm.
Definition MultiApp.h:574
std::vector< libMesh::BoundingBox > _bounding_box
This multi-app's bounding box.
Definition MultiApp.h:592
std::vector< unsigned int > _npositions_inputfile
Number of positions for each input file.
Definition MultiApp.h:550
std::optional< std::string > _cli_args_param
The parameter that was used to set the command line args, if any.
Definition MultiApp.h:679
virtual void initialSetup() override
Method to be called in main-app initial setup for create sub-apps if using positions is false.
Definition MultiApp.C:462
std::vector< MultiAppTransfer * > _associated_transfers
Transfers associated with this multiapp.
Definition MultiApp.h:662
virtual void incrementTStep(Real)
Advances the multi-apps time step which is important for dt selection.
Definition MultiApp.h:210
MPI_Comm & _my_comm
The MPI communicator this object is going to use.
Definition MultiApp.h:571
bool hasLocalApp(unsigned int global_app) const
Whether or not the given global app number is on this processor.
Definition MultiApp.C:1102
virtual void preRunInputFile()
call back executed right before app->runInputFile()
Definition MultiApp.C:1527
const PerfID _restore_timer
Definition MultiApp.h:674
void possiblyCreateChildApplications()
Called just after construction to allow derived classes to set _positions and create sub-apps accordi...
Definition MultiApp.C:390
unsigned int _first_local_app
The number of the first app on this processor.
Definition MultiApp.h:562
Point _bounding_box_padding
Additional padding added to the bounding box, useful for 1D meshes.
Definition MultiApp.h:598
std::vector< const Positions * > _positions_objs
The positions of all of the apps, using the Positions system.
Definition MultiApp.h:531
const Point & position(unsigned int app) const
The physical position of a global App number.
Definition MultiApp.C:1570
bool _has_an_app
Whether or not this processor as an App at all
Definition MultiApp.h:634
processor_id_type _max_procs_per_app
Maximum number of processors to give to each app.
Definition MultiApp.h:601
bool _move_happened
Whether or not the move has happened.
Definition MultiApp.h:631
void createApps()
Create the provided number of apps.
Definition MultiApp.C:412
bool _keep_aux_solution_during_restore
Flag indicates if or not restart the auxiliary system from the latest auxiliary solution.
Definition MultiApp.h:646
std::vector< std::vector< std::unique_ptr< libMesh::NumericVector< Real > > > > _end_solutions
The solution from the end of the previous solve, this is cloned from the Nonlinear solution during re...
Definition MultiApp.h:653
std::vector< unsigned int > _move_apps
The apps to be moved.
Definition MultiApp.h:625
virtual void finishStep(bool=false)
Calls multi-apps executioners' endStep and postStep methods which creates output and advances time (n...
Definition MultiApp.h:220
const PerfID _solve_step_timer
Timers.
Definition MultiApp.h:671
int _orig_rank
The mpi "rank" of this processor in the original communicator.
Definition MultiApp.h:577
MPI_Comm & comm()
Get the MPI communicator this MultiApp is operating on.
Definition MultiApp.h:384
FEProblem & appProblem(unsigned int app)
Get the FEProblem for the global app is part of.
Definition MultiApp.C:1059
virtual void backup()
Save off the state of every Sub App.
Definition MultiApp.C:791
const bool _no_restore
Whether or not to skip restoring completely.
Definition MultiApp.h:649
const bool _use_positions
Toggle use of "positions".
Definition MultiApp.h:537
void readCommandLineArguments()
Fill command line arguments for sub apps.
Definition MultiApp.C:473
bool _keep_solution_during_restore
Flag indicates if or not restart from the latest solution.
Definition MultiApp.h:643
static InputParameters validParams()
Definition MultiApp.C:51
virtual void preExecute()
Definition MultiApp.h:146
std::string _output_base
The output file basename for each multiapp.
Definition MultiApp.h:553
Real appPostprocessorValue(unsigned int app, const std::string &name)
Get a Postprocessor value for a specified global app.
Definition MultiApp.C:1081
void addAssociatedTransfer(MultiAppTransfer &transfer)
Add a transfer that is associated with this multiapp.
Definition MultiApp.C:1532
unsigned int firstLocalApp()
Definition MultiApp.h:320
const MPI_Comm & _orig_comm
The original comm handle.
Definition MultiApp.h:565
FEProblemBase & _fe_problem
The FEProblemBase this MultiApp is part of.
Definition MultiApp.h:523
unsigned int _total_num_apps
The total number of apps to simulate.
Definition MultiApp.h:556
unsigned int globalAppToLocal(unsigned int global_app)
Map a global App number to the local number.
Definition MultiApp.C:1508
SubAppBackups & _sub_app_backups
The cached subapp backups (passed from the parent app)
Definition MultiApp.h:668
const bool _create_child_apps_on_initial_setup
Whether we create child apps during the initialSetup() call or immediately after the multiapps constr...
Definition MultiApp.h:541
std::vector< std::shared_ptr< MooseApp > > _apps
Pointers to each of the Apps.
Definition MultiApp.h:586
std::vector< bool > _reset_happened
Whether or not apps have been reset at each time.
Definition MultiApp.h:619
MooseApp * localApp(unsigned int local_app)
Get the local MooseApp object.
Definition MultiApp.C:1112
void setAppOutputFileBase()
Sets all the app's output file bases.
Definition MultiApp.C:1538
virtual std::vector< std::string > getCommandLineArgs(const unsigned int local_app)
Definition MultiApp.C:1335
const UserObject & appUserObjectBase(unsigned int app, const std::string &name)
Get a UserObject base for a specific global app.
Definition MultiApp.C:1072
LocalRankConfig _rank_config
The app configuration resulting from calling init.
Definition MultiApp.h:659
std::vector< std::unique_ptr< NumericVector< Real > > > _end_aux_solutions
The auxiliary solution from the end of the previous solve, this is cloned from the auxiliary solution...
Definition MultiApp.h:656
virtual bool propagateRecoverToSubApps() const
Whether or not to propagate the parent's recover state (the –recover and –test-checkpoint-half-transi...
Definition MultiApp.h:454
std::string _node_name
Node Name.
Definition MultiApp.h:580
FEProblemBase & problemBase()
Get the FEProblemBase this MultiApp is part of.
Definition MultiApp.h:269
virtual void fillPositions()
must fill in _positions with the positions of the sub-aps
Definition MultiApp.C:571
unsigned int _my_num_apps
The number of apps this object is involved in simulating.
Definition MultiApp.h:559
virtual Executioner * getExecutioner(unsigned int app)
Definition MultiApp.C:757
const PerfID _reset_timer
Definition MultiApp.h:675
const std::vector< CLIArgString > & _cli_args
CommandLine arguments (controllable!)
Definition MultiApp.h:637
std::vector< FileName > _input_files
The input file for each app's simulation.
Definition MultiApp.h:544
unsigned int numGlobalApps() const
Definition MultiApp.h:310
virtual void createLocalApp(const unsigned int i)
Create the i-th local app.
Definition MultiApp.C:456
std::vector< Point > _move_positions
The new positions for the apps to be moved.
Definition MultiApp.h:628
std::vector< Point > _positions
The positions of all of the apps, using input constant vectors (to be deprecated)
Definition MultiApp.h:529
virtual libMesh::NumericVector< libMesh::Number > & appTransferVector(unsigned int app, std::string var_name)
Get the vector to transfer to for this MultiApp.
Definition MultiApp.C:1090
virtual void parentOutputPositionChanged()
For apps outputting in position we need to change their output positions if their parent app moves.
Definition MultiApp.C:1159
virtual bool solveStep(Real dt, Real target_time, bool auto_advance=true)=0
Re-solve all of the Apps.
const bool _run_in_position
Whether to run the child apps with their meshes transformed with the coordinate transforms.
Definition MultiApp.h:665
static void transformBoundingBox(libMesh::BoundingBox &box, const MultiAppCoordTransform &transform)
Transform a bounding box according to the transformations in the provided coordinate transformation o...
Definition MultiApp.C:934
virtual libMesh::BoundingBox getBoundingBox(unsigned int app, bool displaced_mesh, const MultiAppCoordTransform *coord_transform=nullptr)
Get the BoundingBox for the mesh associated with app The bounding box will be shifted to be in the co...
Definition MultiApp.C:974
std::vector< unsigned int > _reset_apps
The apps to be reset.
Definition MultiApp.h:616
Interface for objects interacting with the PerfGraph.
Positions objects are under the hood Reporters.
Definition Positions.h:21
A class for creating restricted objects.
Definition Restartable.h:29
Helper class for holding Sub-app backups.
Definition MultiApp.h:125
Base class for user-specific data.
Definition UserObject.h:20
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
Holds app partitioning information relevant to the a particular rank for a multiapp scenario.
Definition MultiApp.h:47
dof_id_type num_local_sims
The number of simulations that should/will be run locally on this rank.
Definition MultiApp.h:49
dof_id_type first_local_sim_index
The (global) index of the first local simulation for this rank.
Definition MultiApp.h:53
processor_id_type my_first_rank
For every rank working on a subapp, we store the first rank on each process to make the communication...
Definition MultiApp.h:74
bool is_first_local_rank
This is true if this rank is the primary/zero rank for a (sub)app slot.
Definition MultiApp.h:71
dof_id_type num_local_apps
The number of (sub)apps that should/will be run locally on this rank.
Definition MultiApp.h:58
dof_id_type first_local_app_index
The (global) index of the first local app for this rank.
Definition MultiApp.h:64