https://mooseframework.inl.gov
MultiAppPostprocessorToAuxScalarTransfer.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 
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 from a postprocessor to a scalar auxiliary variable.");
30  params.addRequiredParam<PostprocessorName>(
31  "from_postprocessor", "The name of the Postprocessor to transfer the value from.");
32  params.addRequiredParam<VariableName>(
33  "to_aux_scalar", "The name of the scalar AuxVariable to transfer the value to.");
34  return params;
35 }
36 
38  const InputParameters & parameters)
39  : MultiAppTransfer(parameters),
40  _from_pp_name(getParam<PostprocessorName>("from_postprocessor")),
41  _to_aux_name(getParam<VariableName>("to_aux_scalar"))
42 {
43  if (_directions.size() != 1)
44  paramError("direction", "This transfer is only unidirectional");
45 }
46 
47 void
49 {
50  TIME_SECTION("MultiAppPostprocessorToAuxScalarTransfer::execute()",
51  5,
52  "Performing transfer between a scalar variable and a postprocessor");
53 
54  // Execute the postprocessor if it was specified to execute on TRANSFER
55  switch (_current_direction)
56  {
57  case TO_MULTIAPP:
58  {
61  break;
62  }
63  case FROM_MULTIAPP:
65  }
66 
67  // Perform action based on the transfer direction
68  switch (_current_direction)
69  {
70  // MultiApp -> MultiApp
71  case BETWEEN_MULTIAPP:
72  {
73  for (unsigned int i = 0; i < getFromMultiApp()->numGlobalApps(); i++)
74  {
75  if (getFromMultiApp()->hasLocalApp(i))
76  {
77  // Extract the postprocessor that is being transferred
78  FEProblemBase & from_problem = getFromMultiApp()->appProblemBase(i);
79  Real pp_value = from_problem.getPostprocessorValueByName(_from_pp_name);
80 
81  if (getToMultiApp()->hasLocalApp(i))
82  {
83  // Get reference to the AuxVariable where the postprocessor will be passed
84  MooseVariableScalar & scalar =
85  getToMultiApp()->appProblemBase(i).getScalarVariable(_tid, _to_aux_name);
86 
87  scalar.reinit();
88 
89  // Set all values of the AuxVariable to the value of the postprocessor
90  scalar.setValues(pp_value);
91 
92  // Update the solution
93  scalar.insert(scalar.sys().solution());
94  scalar.sys().solution().close();
95  }
96  }
97  }
98  break;
99  }
100 
101  // main app -> MultiApp
102  case TO_MULTIAPP:
103  {
104  // Extract the postprocessor that is being transferred
105  FEProblemBase & from_problem = getToMultiApp()->problemBase();
106  Real pp_value = from_problem.getPostprocessorValueByName(_from_pp_name);
107 
108  // Loop through each of the sub apps
109  for (unsigned int i = 0; i < getToMultiApp()->numGlobalApps(); i++)
110  if (getToMultiApp()->hasLocalApp(i))
111  {
112  // Get reference to the AuxVariable where the postprocessor will be passed
113  MooseVariableScalar & scalar =
114  getToMultiApp()->appProblemBase(i).getScalarVariable(_tid, _to_aux_name);
115 
116  scalar.reinit();
117 
118  // Set all values of the AuxVariable to the value of the postprocessor
119  scalar.setValues(pp_value);
120 
121  // Update the solution
122  scalar.insert(scalar.sys().solution());
123  scalar.sys().solution().close();
124  }
125  break;
126  }
127 
128  // MultiApp -> main app
129  case FROM_MULTIAPP:
130  {
131  // The number of sub applications
132  unsigned int num_apps = getFromMultiApp()->numGlobalApps();
133 
134  // The AuxVariable for storing the postprocessor values from the sub app
135  MooseVariableScalar & scalar =
136  getFromMultiApp()->problemBase().getScalarVariable(_tid, _to_aux_name);
137 
138  // Ensure that the variable is up to date
139  scalar.reinit();
140 
141  // The dof indices for the scalar variable of interest
142  auto && dof = scalar.dofIndices();
143 
144  // Error if there is a size mismatch between the scalar AuxVariable and the number of sub apps
145  if (num_apps != scalar.sln().size())
146  mooseError("The number of sub apps (",
147  num_apps,
148  ") must be equal to the order of the scalar AuxVariable (",
149  scalar.order(),
150  ")");
151 
152  // Loop over each sub-app and populate the AuxVariable values from the postprocessors
153  for (unsigned int i = 0; i < getFromMultiApp()->numGlobalApps(); i++)
154  if (getFromMultiApp()->hasLocalApp(i) && getFromMultiApp()->isRootProcessor())
155  // Note: This can't be done using MooseScalarVariable::insert() because different
156  // processors will be setting dofs separately.
157  scalar.sys().solution().set(
158  dof[i],
159  getFromMultiApp()->appProblemBase(i).getPostprocessorValueByName(_from_pp_name));
160 
161  scalar.sys().solution().close();
162 
163  break;
164  }
165  }
166 }
167 
168 void
170 {
171  // Check that we are in the supported configuration: same number of source and target apps
172  // The allocation of the child apps on the processors must be the same
173  if (getFromMultiApp()->numGlobalApps() == getToMultiApp()->numGlobalApps())
174  {
175  for (const auto i : make_range(getToMultiApp()->numGlobalApps()))
176  if (getFromMultiApp()->hasLocalApp(i) + getToMultiApp()->hasLocalApp(i) == 1)
177  mooseError("Child application allocation on parallel processes must be the same to support "
178  "siblings postprocessor to scalar variable transfer");
179  }
180  else
181  mooseError("Number of source and target child apps must match for siblings transfer");
182 }
const ExecFlagType EXEC_TRANSFER
Definition: Moose.C:51
virtual void execute() override
Execute the transfer.
const std::shared_ptr< MultiApp > getFromMultiApp() const
Get the MultiApp to transfer data from.
VariableName _to_aux_name
The name of the field variable to which the postprocessor is being transferred.
NumericVector< Number > & solution()
Definition: SystemBase.h:195
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.
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...
FEProblemBase & _fe_problem
Definition: Transfer.h:97
const std::shared_ptr< MultiApp > getToMultiApp() const
Get the MultiApp to transfer data to.
PostprocessorName _from_pp_name
The name of the postprocessor that the transfer originates.
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...
MultiAppPostprocessorToAuxScalarTransfer(const InputParameters &parameters)
void errorIfObjectExecutesOnTransferInSourceApp(const std::string &object_name) const
Error if executing this MooseObject on EXEC_TRANSFER in a source multiapp (from_multiapp, e.g.
Copies the value of a Postprocessor from one app to a scalar AuxVariable in another.
virtual const std::vector< dof_id_type > & dofIndices() const
Get local DoF indices.
void setValues(Number value)
Set all of the values of this scalar variable to the same value.
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()
const PostprocessorValue & getPostprocessorValueByName(const PostprocessorName &name, std::size_t t_index=0) const
Get a read-only reference to the value associated with a Postprocessor that exists.
virtual void close()=0
THREAD_ID _tid
Definition: Transfer.h:100
libMesh::Order order() const
Get the order of this variable Note: Order enum can be implicitly converted to unsigned int...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
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 insert(NumericVector< Number > &soln)
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
virtual void computeUserObjectByName(const ExecFlagType &type, const Moose::AuxGroup &group, const std::string &name)
Compute an user object with the given name.
SystemBase & sys()
Get the system this variable is part of.
virtual void checkSiblingsTransferSupported() const override
Whether the transfer supports siblings transfer.
const VariableValue & sln() const
registerMooseObject("MooseApp", MultiAppPostprocessorToAuxScalarTransfer)