https://mooseframework.inl.gov
MultiAppMFEMCopyTransfer.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 #ifdef MOOSE_MFEM_ENABLED
11 
13 #include "FEProblemBase.h"
14 #include "MultiApp.h"
15 #include "SystemBase.h"
16 #include <memory>
17 #include "MFEMProblem.h"
18 #include "MFEMMesh.h"
19 
21 
24 {
26  params.addRequiredParam<std::vector<AuxVariableName>>(
27  "variable", "AuxVariable to store transferred value in.");
28  params.addRequiredParam<std::vector<VariableName>>("source_variable",
29  "Variable to transfer from");
30  params.addClassDescription("Copies variable values from one MFEM application to another");
31  return params;
32 }
33 
35  : MultiAppTransfer(params),
36  _from_var_names(getParam<std::vector<VariableName>>("source_variable")),
37  _to_var_names(getParam<std::vector<AuxVariableName>>("variable"))
38 {
39  auto bad_problem = [this]()
40  {
41  mooseError(type(),
42  " only works with MFEMProblem based applications. Check that all your inputs "
43  "involved in this transfer are MFEMProblem based");
44  };
45  if (hasToMultiApp())
46  {
47  if (!dynamic_cast<MFEMProblem *>(&getToMultiApp()->problemBase()))
48  bad_problem();
49  for (const auto i : make_range(getToMultiApp()->numGlobalApps()))
50  if (getToMultiApp()->hasLocalApp(i) &&
51  !dynamic_cast<MFEMProblem *>(&getToMultiApp()->appProblemBase(i)))
52  bad_problem();
53  }
54  if (hasFromMultiApp())
55  {
56  if (!dynamic_cast<MFEMProblem *>(&getFromMultiApp()->problemBase()))
57  bad_problem();
58  for (const auto i : make_range(getFromMultiApp()->numGlobalApps()))
59  if (getFromMultiApp()->hasLocalApp(i) &&
60  !dynamic_cast<MFEMProblem *>(&getFromMultiApp()->appProblemBase(i)))
61  bad_problem();
62  }
63 }
64 
65 //
66 void
68 {
69  // Redundant as source name is required?
70  if (!numToVar())
71  mooseError("No transferred variables were specified, neither programmatically or through the "
72  "'source_variable' parameter");
73  if (numToVar() != numFromVar())
74  mooseError("Number of variables transferred must be same in both systems.");
75  for (unsigned v = 0; v < numToVar(); ++v)
76  {
77  auto & to_var = to_problem.getProblemData().gridfunctions.GetRef(getToVarName(v));
78  auto & from_var = from_problem.getProblemData().gridfunctions.GetRef(getFromVarName(v));
79  // TODO: Probably need more checking here to make sure the variables are
80  // copyable - as per the MultiAppDofCopyTransfer
81  to_var = from_var;
82  }
83 }
84 
85 void
87 {
88  TIME_SECTION("MultiAppMFEMCopyTransfer::execute", 5, "Copies variables");
90  {
91  for (unsigned int i = 0; i < getToMultiApp()->numGlobalApps(); i++)
92  {
93  if (getToMultiApp()->hasLocalApp(i))
94  {
95  transfer(static_cast<MFEMProblem &>(getToMultiApp()->appProblemBase(i)),
96  static_cast<MFEMProblem &>(getToMultiApp()->problemBase()));
97  }
98  }
99  }
100  else if (_current_direction == FROM_MULTIAPP)
101  {
102  for (unsigned int i = 0; i < getFromMultiApp()->numGlobalApps(); i++)
103  {
104  if (getFromMultiApp()->hasLocalApp(i))
105  {
106  transfer(static_cast<MFEMProblem &>(getFromMultiApp()->problemBase()),
107  static_cast<MFEMProblem &>(getFromMultiApp()->appProblemBase(i)));
108  }
109  }
110  }
112  {
113  int transfers_done = 0;
114  for (unsigned int i = 0; i < getFromMultiApp()->numGlobalApps(); i++)
115  {
116  if (getFromMultiApp()->hasLocalApp(i))
117  {
118  if (getToMultiApp()->hasLocalApp(i))
119  {
120  transfer(static_cast<MFEMProblem &>(getToMultiApp()->appProblemBase(i)),
121  static_cast<MFEMProblem &>(getFromMultiApp()->appProblemBase(i)));
122  transfers_done++;
123  }
124  }
125  }
126  if (!transfers_done)
127  mooseError("BETWEEN_MULTIAPP transfer not supported if there is not at least one subapp "
128  "per multiapp involved on each rank");
129  }
130 }
131 
132 void
134 {
135  // Check that we are in the supported configuration: same number of source and target apps
136  // The allocation of the child apps on the processors must be the same
137  if (getFromMultiApp()->numGlobalApps() == getToMultiApp()->numGlobalApps())
138  {
139  for (const auto i : make_range(getToMultiApp()->numGlobalApps()))
140  if (getFromMultiApp()->hasLocalApp(i) + getToMultiApp()->hasLocalApp(i) == 1)
141  mooseError("Child application allocation on parallel processes must be the same to support "
142  "siblings variable field copy transfer");
143  }
144  else
145  mooseError("Number of source and target child apps must match for siblings transfer");
146 }
147 
148 #endif
MFEMProblemData & getProblemData()
Method to get the current MFEMProblemData object storing the current data specifying the FE problem...
Definition: MFEMProblem.h:190
const std::shared_ptr< MultiApp > getFromMultiApp() const
Get the MultiApp to transfer data from.
MooseEnum _current_direction
Definition: Transfer.h:106
void execute() override
Execute the transfer.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const std::shared_ptr< MultiApp > getToMultiApp() const
Get the MultiApp to transfer data to.
bool hasFromMultiApp() const
Whether the transfer owns a non-null from_multi_app.
void transfer(MFEMProblem &to_problem, MFEMProblem &from_problem)
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...
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:51
MultiAppMFEMCopyTransfer(InputParameters const &params)
void checkSiblingsTransferSupported() const override
Whether the transfer supports siblings transfer.
static InputParameters validParams()
auto const & getFromVarName(int i)
Base class for all MultiAppTransfer objects.
IntRange< T > make_range(T beg, T end)
bool hasToMultiApp() const
Whether the transfer owns a non-null to_multi_app.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
T & GetRef(const std::string &field_name) const
Returns a reference to a field.
registerMooseObject("MooseApp", MultiAppMFEMCopyTransfer)
Moose::MFEM::GridFunctions gridfunctions
static InputParameters validParams()