www.mooseframework.org
MultiAppPostprocessorToAuxScalarTransfer.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 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  // Perform action based on the transfer direction
55  switch (_current_direction)
56  {
57  // MultiApp -> MultiApp
58  case BETWEEN_MULTIAPP:
59  {
60  for (unsigned int i = 0; i < getFromMultiApp()->numGlobalApps(); i++)
61  {
62  if (getFromMultiApp()->hasLocalApp(i))
63  {
64  // Extract the postprocessor that is being transferred
65  FEProblemBase & from_problem = getFromMultiApp()->appProblemBase(i);
66  Real pp_value = from_problem.getPostprocessorValueByName(_from_pp_name);
67 
68  if (getToMultiApp()->hasLocalApp(i))
69  {
70  // Get reference to the AuxVariable where the postprocessor will be passed
71  MooseVariableScalar & scalar =
72  getToMultiApp()->appProblemBase(i).getScalarVariable(_tid, _to_aux_name);
73 
74  scalar.reinit();
75 
76  // Set all values of the AuxVariable to the value of the postprocessor
77  scalar.setValues(pp_value);
78 
79  // Update the solution
80  scalar.insert(scalar.sys().solution());
81  scalar.sys().solution().close();
82  }
83  }
84  }
85  break;
86  }
87 
88  // main app -> MultiApp
89  case TO_MULTIAPP:
90  {
91  // Extract the postprocessor that is being transferred
92  FEProblemBase & from_problem = getToMultiApp()->problemBase();
93  Real pp_value = from_problem.getPostprocessorValueByName(_from_pp_name);
94 
95  // Loop through each of the sub apps
96  for (unsigned int i = 0; i < getToMultiApp()->numGlobalApps(); i++)
97  if (getToMultiApp()->hasLocalApp(i))
98  {
99  // Get reference to the AuxVariable where the postprocessor will be passed
100  MooseVariableScalar & scalar =
101  getToMultiApp()->appProblemBase(i).getScalarVariable(_tid, _to_aux_name);
102 
103  scalar.reinit();
104 
105  // Set all values of the AuxVariable to the value of the postprocessor
106  scalar.setValues(pp_value);
107 
108  // Update the solution
109  scalar.insert(scalar.sys().solution());
110  scalar.sys().solution().close();
111  }
112  break;
113  }
114 
115  // MultiApp -> main app
116  case FROM_MULTIAPP:
117  {
118  // The number of sub applications
119  unsigned int num_apps = getFromMultiApp()->numGlobalApps();
120 
121  // The AuxVariable for storing the postprocessor values from the sub app
122  MooseVariableScalar & scalar =
123  getFromMultiApp()->problemBase().getScalarVariable(_tid, _to_aux_name);
124 
125  // Ensure that the variable is up to date
126  scalar.reinit();
127 
128  // The dof indices for the scalar variable of interest
129  auto && dof = scalar.dofIndices();
130 
131  // Error if there is a size mismatch between the scalar AuxVariable and the number of sub apps
132  if (num_apps != scalar.sln().size())
133  mooseError("The number of sub apps (",
134  num_apps,
135  ") must be equal to the order of the scalar AuxVariable (",
136  scalar.order(),
137  ")");
138 
139  // Loop over each sub-app and populate the AuxVariable values from the postprocessors
140  for (unsigned int i = 0; i < getFromMultiApp()->numGlobalApps(); i++)
141  if (getFromMultiApp()->hasLocalApp(i) && getFromMultiApp()->isRootProcessor())
142  // Note: This can't be done using MooseScalarVariable::insert() because different
143  // processors will be setting dofs separately.
144  scalar.sys().solution().set(
145  dof[i],
146  getFromMultiApp()->appProblemBase(i).getPostprocessorValueByName(_from_pp_name));
147 
148  scalar.sys().solution().close();
149 
150  break;
151  }
152  }
153 }
154 
155 void
157 {
158  // Check that we are in the supported configuration: same number of source and target apps
159  // The allocation of the child apps on the processors must be the same
160  if (getFromMultiApp()->numGlobalApps() == getToMultiApp()->numGlobalApps())
161  {
162  for (const auto i : make_range(getToMultiApp()->numGlobalApps()))
163  if (getFromMultiApp()->hasLocalApp(i) + getToMultiApp()->hasLocalApp(i) == 1)
164  mooseError("Child application allocation on parallel processes must be the same to support "
165  "siblings postprocessor to scalar variable transfer");
166  }
167  else
168  mooseError("Number of source and target child apps must match for siblings transfer");
169 }
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: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.
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.
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)
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
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
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)