www.mooseframework.org
MultiAppTransfer.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 // MOOSE includes
16 #include "MultiAppTransfer.h"
17 #include "Transfer.h"
18 #include "MooseTypes.h"
19 #include "FEProblem.h"
20 #include "DisplacedProblem.h"
21 #include "MultiApp.h"
22 #include "MooseMesh.h"
23 
24 #include "libmesh/parallel_algebra.h"
25 #include "libmesh/mesh_tools.h"
26 
27 template <>
30 {
32  params.addRequiredParam<MultiAppName>("multi_app", "The name of the MultiApp to use.");
33 
34  params.addRequiredParam<MooseEnum>("direction",
36  "Whether this Transfer will be 'to' or 'from' a MultiApp.");
37 
38  // MultiAppTransfers by default will execute with their associated MultiApp. These flags will be
39  // added by FEProblemBase when the transfer is added.
40  MultiMooseEnum multi_transfer_execute_on(params.get<MultiMooseEnum>("execute_on").getRawNames() +
41  " same_as_multiapp",
42  "same_as_multiapp");
43  params.set<MultiMooseEnum>("execute_on") = multi_transfer_execute_on;
44 
45  params.addParam<bool>(
46  "check_multiapp_execute_on",
47  true,
48  "When false the check between the multiapp and transfer execute on flags is not preformed.");
49  params.addParam<bool>("displaced_source_mesh",
50  false,
51  "Whether or not to use the displaced mesh for the source mesh.");
52  params.addParam<bool>("displaced_target_mesh",
53  false,
54  "Whether or not to use the displaced mesh for the target mesh.");
55 
56  return params;
57 }
58 
59 // Free function to clear special execute_on option before initializing SetupInterface
60 const InputParameters &
62 {
63  InputParameters * params = const_cast<InputParameters *>(&parameters);
64  params->set<MultiMooseEnum>("execute_on").erase("SAME_AS_MULTIAPP");
65  return *params;
66 }
67 
69  :
77  Transfer(removeSpecialOption(parameters)),
78  _multi_app(_fe_problem.getMultiApp(getParam<MultiAppName>("multi_app"))),
79  _direction(getParam<MooseEnum>("direction")),
80  _displaced_source_mesh(false),
81  _displaced_target_mesh(false)
82 {
83  if (getParam<bool>("check_multiapp_execute_on") && (execFlags() != _multi_app->execFlags()))
84  mooseDoOnce(mooseWarning(
85  "MultiAppTransfer execute_on flags do not match associated Multiapp execute_on flags"));
86 }
87 
88 void
89 MultiAppTransfer::variableIntegrityCheck(const AuxVariableName & var_name) const
90 {
91  for (unsigned int i = 0; i < _multi_app->numGlobalApps(); i++)
92  if (_multi_app->hasLocalApp(i) && !find_sys(_multi_app->appProblemBase(i).es(), var_name))
93  mooseError("Cannot find variable ", var_name, " for ", name(), " Transfer");
94 }
95 
96 const std::vector<ExecFlagType> &
98 {
99  if (Transfer::execFlags().empty())
100  return _multi_app->execFlags();
101  else
102  return Transfer::execFlags();
103 }
104 
105 void
107 {
108  // I would like to do all of this in initialSetup, but it will fail with
109  // multiapps that reset. A reset deletes and rebuilds the FEProblems so all
110  // of the pointers will be broken.
111 
112  // Clear the vectors since we've probably built them up from a previous call
113  _from_problems.clear();
114  _to_problems.clear();
115  _from_es.clear();
116  _to_es.clear();
117  _from_meshes.clear();
118  _to_meshes.clear();
119  _to_positions.clear();
120  _from_positions.clear();
121 
122  // Build the vectors for to problems, from problems, and subapps positions.
123  switch (_direction)
124  {
125  case TO_MULTIAPP:
126  _from_problems.push_back(&_multi_app->problemBase());
127  _from_positions.push_back(Point(0., 0., 0.));
128  for (unsigned int i_app = 0; i_app < _multi_app->numGlobalApps(); i_app++)
129  {
130  if (!_multi_app->hasLocalApp(i_app))
131  continue;
132  _local2global_map.push_back(i_app);
133  _to_problems.push_back(&_multi_app->appProblemBase(i_app));
134  _to_positions.push_back(_multi_app->position(i_app));
135  }
136  break;
137 
138  case FROM_MULTIAPP:
139  _to_problems.push_back(&_multi_app->problemBase());
140  _to_positions.push_back(Point(0., 0., 0.));
141  for (unsigned int i_app = 0; i_app < _multi_app->numGlobalApps(); i_app++)
142  {
143  if (!_multi_app->hasLocalApp(i_app))
144  continue;
145  _local2global_map.push_back(i_app);
146  _from_problems.push_back(&_multi_app->appProblemBase(i_app));
147  _from_positions.push_back(_multi_app->position(i_app));
148  }
149  break;
150  }
151 
152  // Build the from and to equation systems and mesh vectors.
153  for (unsigned int i = 0; i < _to_problems.size(); i++)
154  {
155  // TODO: Do I actually want es or displaced es?
156  _to_es.push_back(&_to_problems[i]->es());
157  if (_displaced_target_mesh && _to_problems[i]->getDisplacedProblem())
158  _to_meshes.push_back(&_to_problems[i]->getDisplacedProblem()->mesh());
159  else
160  _to_meshes.push_back(&_to_problems[i]->mesh());
161  }
162  for (unsigned int i = 0; i < _from_problems.size(); i++)
163  {
164  _from_es.push_back(&_from_problems[i]->es());
165  if (_displaced_source_mesh && _from_problems[i]->getDisplacedProblem())
166  _from_meshes.push_back(&_from_problems[i]->getDisplacedProblem()->mesh());
167  else
168  _from_meshes.push_back(&_from_problems[i]->mesh());
169  }
170 }
171 
172 std::vector<BoundingBox>
174 {
175  std::vector<std::pair<Point, Point>> bb_points(_from_meshes.size());
176  for (unsigned int i = 0; i < _from_meshes.size(); i++)
177  {
178  // Get a bounding box around the mesh elements that are local to the current
179  // processor.
180  BoundingBox bbox = MeshTools::create_local_bounding_box(*_from_meshes[i]);
181 
182  // Translate the bounding box to the from domain's position.
183  bbox.first += _from_positions[i];
184  bbox.second += _from_positions[i];
185 
186  // Cast the bounding box into a pair of points (so it can be put through
187  // MPI communication).
188  bb_points[i] = static_cast<std::pair<Point, Point>>(bbox);
189  }
190 
191  // Serialize the bounding box points.
192  _communicator.allgather(bb_points);
193 
194  // Recast the points back into bounding boxes and return.
195  std::vector<BoundingBox> bboxes(bb_points.size());
196  for (unsigned int i = 0; i < bb_points.size(); i++)
197  bboxes[i] = static_cast<BoundingBox>(bb_points[i]);
198 
199  return bboxes;
200 }
201 
202 std::vector<unsigned int>
204 {
205  std::vector<unsigned int> froms_per_proc;
206  switch (_direction)
207  {
208  case TO_MULTIAPP:
209  froms_per_proc.resize(n_processors(), 1);
210  break;
211  case FROM_MULTIAPP:
212  froms_per_proc.resize(n_processors());
213  _communicator.allgather(_multi_app->numLocalApps(), froms_per_proc);
214  break;
215  }
216  return froms_per_proc;
217 }
218 
219 NumericVector<Real> &
220 MultiAppTransfer::getTransferVector(unsigned int i_local, std::string var_name)
221 {
222  mooseAssert(_direction == TO_MULTIAPP, "getTransferVector only works for transfers to multiapps");
223 
224  return _multi_app->appTransferVector(_local2global_map[i_local], var_name);
225 }
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:47
NumericVector< Real > & getTransferVector(unsigned int i_local, std::string var_name)
If we are transferring to a multiapp, return the appropriate solution vector.
virtual const std::vector< ExecFlagType > & execFlags() const
Get the execution flag for the object.
void mooseWarning(Args &&...args) const
Definition: MooseObject.h:89
void variableIntegrityCheck(const AuxVariableName &var_name) const
Utility to verify that the vEariable in the destination system exists.
std::vector< EquationSystems * > _to_es
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
MultiAppTransfer(const InputParameters &parameters)
Here we need to remove the special option that indicates to the user that this object will follow it&#39;...
std::vector< BoundingBox > getFromBoundingBoxes()
Return the bounding boxes of all the "from" domains, including all the domains not local to this proc...
std::vector< FEProblemBase * > _to_problems
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::vector< Point > _from_positions
std::shared_ptr< MultiApp > _multi_app
The MultiApp this Transfer is transferring data to or from.
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
InputParameters validParams< Transfer >()
Definition: Transfer.C:31
const InputParameters & removeSpecialOption(const InputParameters &parameters)
std::vector< MooseMesh * > _from_meshes
MooseEnum _direction
Whether we&#39;re transferring to or from the MultiApp.
static MooseEnum directions()
Used to construct InputParameters.
std::vector< unsigned int > getFromsPerProc()
Return the number of "from" domains that each processor owns.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:37
std::vector< unsigned int > _local2global_map
virtual const std::vector< ExecFlagType > & execFlags() const
Return the execution flags, handling "same_as_multiapp".
std::string getRawNames() const
Method for returning the raw name strings for this instance.
std::vector< Point > _to_positions
std::vector< EquationSystems * > _from_es
InputParameters validParams< MultiAppTransfer >()
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
void mooseError(Args &&...args) const
Definition: MooseObject.h:80
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
static System * find_sys(EquationSystems &es, const std::string &var_name)
Small helper function for finding the system containing the variable.
Definition: Transfer.C:70
void getAppInfo()
This method will fill information into the convenience member variables (_to_problems, _from_meshes, etc.)
std::vector< FEProblemBase * > _from_problems
std::vector< MooseMesh * > _to_meshes
Base class for all Transfer objects.
Definition: Transfer.h:46