www.mooseframework.org
MultiAppScalarToAuxScalarTransfer.C
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 
11 
12 // MOOSE includes
13 #include "FEProblem.h"
14 #include "MooseTypes.h"
15 #include "MooseVariableScalar.h"
16 #include "MultiApp.h"
17 #include "SystemBase.h"
18 
19 #include "libmesh/meshfree_interpolation.h"
20 #include "libmesh/system.h"
21 
22 // Define the input parameters
24 
27 {
29  params.addClassDescription("Transfers data from a scalar variable to an auxiliary scalar "
30  "variable from different applications.");
31  params.addRequiredParam<VariableName>("source_variable",
32  "The name of the scalar variable to "
33  "transfer the value from.");
34  params.addRequiredParam<VariableName>("to_aux_scalar",
35  "The name of the scalar auxiliary variable to "
36  "transfer the value to.");
37  return params;
38 }
39 
41  const InputParameters & parameters)
42  : MultiAppTransfer(parameters),
43  _from_variable_name(getParam<VariableName>("source_variable")),
44  _to_aux_name(getParam<VariableName>("to_aux_scalar"))
45 {
46  if (_directions.size() != 1)
47  paramError("direction", "This transfer is only unidirectional");
48 }
49 
50 void
52 {
53  TIME_SECTION(
54  "MultiAppScalarToAuxScalarTransfer::execute()", 5, "Performing a scalar variable transfer");
55 
56  // Perform action based on the transfer direction
57  switch (_current_direction)
58  {
59  // main app to multi_app
60  case TO_MULTIAPP:
61  {
62  // Extract the scalar variable that is being transferred
63  FEProblemBase & from_problem = getToMultiApp()->problemBase();
64  MooseVariableScalar * from_variable =
66 
67  // Loop through each of the sub apps
68  for (unsigned int i = 0; i < getToMultiApp()->numGlobalApps(); i++)
69  if (getToMultiApp()->hasLocalApp(i))
70  {
71  // Get reference to the scalar variable that will be written
72  MooseVariableScalar * to_variable =
73  &getToMultiApp()->appProblemBase(i).getScalarVariable(_tid, _to_aux_name);
74 
75  to_variable->reinit();
76 
77  // Determine number of DOFs that we're going to read and write
78  auto && to_dof = to_variable->dofIndices();
79  auto & from_values = from_variable->sln();
80 
81  // Check that the DOF matches
82  if (from_variable->sln().size() != to_variable->sln().size())
83  mooseError("Order of SCALAR variables do not match for sending and "
84  "receiving data for the "
85  "MultiAppScalarToAuxScalarTransfer!");
86 
87  for (MooseIndex(from_values) j = 0; j < from_values.size(); ++j)
88  to_variable->sys().solution().set(to_dof[j], from_values[j]);
89 
90  to_variable->sys().solution().close();
91  }
92  break;
93  }
94 
95  // multi_app to main app
96  case FROM_MULTIAPP:
97  {
98  // The AuxVariable that will be modified with data from the subapp
99  MooseVariableScalar * to_variable =
100  &getFromMultiApp()->problemBase().getScalarVariable(_tid, _to_aux_name);
101 
102  // Ensure that the variable is up to date
103  to_variable->reinit();
104 
105  // The dof indices for the scalar variable of interest
106  auto && to_dof = to_variable->dofIndices();
107 
108  // Loop over each sub-app and populate the AuxVariable values
109  for (unsigned int i = 0; i < getFromMultiApp()->numGlobalApps(); i++)
110  {
111  if (getFromMultiApp()->hasLocalApp(i) && getFromMultiApp()->isRootProcessor())
112  {
113  // Extract the scalar variable that is being transferred
114  FEProblemBase & from_problem = getFromMultiApp()->appProblemBase(i);
115  MooseVariableScalar * from_variable =
117 
118  // Loop over the scalar aux variable that we're going to write
119  auto & from_values = from_variable->sln();
120 
121  // Check that DOFs match
122  if (from_variable->sln().size() != to_variable->sln().size())
123  mooseError("Order of SCALAR variables do not match for sending and "
124  "receiving data for the "
125  "MultiAppScalarToAuxScalarTransfer!");
126 
127  for (MooseIndex(from_values) j = 0; j < from_values.size(); ++j)
128  to_variable->sys().solution().set(to_dof[j], from_values[j]);
129  }
130  }
131  to_variable->sys().solution().close();
132 
133  break;
134  }
135 
136  // multi_app to multi_app
137  case BETWEEN_MULTIAPP:
138  {
139  for (unsigned int i = 0; i < getFromMultiApp()->numGlobalApps(); i++)
140  {
141  if (getFromMultiApp()->hasLocalApp(i))
142  {
143  // The AuxVariable that will be modified with data from the subapp
144  MooseVariableScalar * from_variable =
145  &getFromMultiApp()->appProblemBase(i).getScalarVariable(_tid, _from_variable_name);
146 
147  // Ensure that the variable is up to date
148  from_variable->reinit();
149 
150  if (getToMultiApp()->hasLocalApp(i))
151  {
152  // Get reference to the scalar variable that will be written
153  MooseVariableScalar * to_variable =
154  &getToMultiApp()->appProblemBase(i).getScalarVariable(_tid, _to_aux_name);
155 
156  to_variable->reinit();
157 
158  // Determine number of DOFs that we're going to read and write
159  auto && to_dof = to_variable->dofIndices();
160  auto & from_values = from_variable->sln();
161 
162  // Check that the DOF matches
163  if (from_variable->sln().size() != to_variable->sln().size())
164  mooseError("Order of SCALAR variables do not match for sending and "
165  "receiving data for the "
166  "MultiAppScalarToAuxScalarTransfer!");
167 
168  for (MooseIndex(from_values) k = 0; k < from_values.size(); ++k)
169  to_variable->sys().solution().set(to_dof[k], from_values[k]);
170 
171  to_variable->sys().solution().close();
172  }
173  }
174  }
175  break;
176  }
177  }
178 }
179 
180 void
182 {
183  // Check that we are in the supported configuration: same number of source and target apps
184  // The allocation of the child apps on the processors must be the same
185  if (getFromMultiApp()->numGlobalApps() == getToMultiApp()->numGlobalApps())
186  {
187  for (const auto i : make_range(getToMultiApp()->numGlobalApps()))
188  if (getFromMultiApp()->hasLocalApp(i) + getToMultiApp()->hasLocalApp(i) == 1)
189  mooseError("Child application allocation on parallel processes must be the same to support "
190  "siblings scalar variable transfer");
191  }
192  else
193  mooseError("Number of source and target child apps must match for siblings transfer");
194 }
VariableName _to_aux_name
The name of the auxiliary scalar variable to which the scalar values are being transfered.
const std::shared_ptr< MultiApp > getFromMultiApp() const
Get the MultiApp to transfer data from.
NumericVector< Number > & solution()
Definition: SystemBase.h:176
MooseEnum _current_direction
Definition: Transfer.h:106
void reinit(bool reinit_for_derivative_reordering=false)
Fill out the VariableValue arrays from the system solution vector.
MultiAppScalarToAuxScalarTransfer(const InputParameters &parameters)
virtual void execute() override
Execute the transfer.
unsigned int size() const
Return the number of active items in the MultiMooseEnum.
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.
Copies the value of a SCALAR variable from one App to another.
VariableName _from_variable_name
The name of the scalar variable from which the values are being transfered.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
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...
virtual void checkSiblingsTransferSupported() const override
Whether the transfer supports siblings transfer.
virtual MooseVariableScalar & getScalarVariable(const THREAD_ID tid, const std::string &var_name) override
Returns the scalar variable reference from whichever system contains it.
virtual const std::vector< dof_id_type > & dofIndices() const
Get local DoF indices.
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
static InputParameters validParams()
virtual void close()=0
THREAD_ID _tid
Definition: Transfer.h:100
MultiMooseEnum _directions
The directions this Transfer is to be executed on.
Definition: Transfer.h:110
Base class for all MultiAppTransfer objects.
Class for scalar variables (they are different).
IntRange< T > make_range(T beg, T end)
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...
virtual void set(const numeric_index_type i, const Number value)=0
SystemBase & sys()
Get the system this variable is part of.
registerMooseObject("MooseApp", MultiAppScalarToAuxScalarTransfer)
const VariableValue & sln() const