www.mooseframework.org
MultiAppVariableValueSampleTransfer.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 "MooseMesh.h"
15 #include "MooseTypes.h"
16 #include "MooseVariableFE.h"
17 #include "MultiApp.h"
18 #include "SystemBase.h"
19 
20 #include "libmesh/meshfree_interpolation.h"
21 #include "libmesh/numeric_vector.h"
22 #include "libmesh/system.h"
23 
25 
28 {
30  params.addClassDescription(
31  "Transfers the value of a variable within the master application at each sub-application "
32  "position and transfers the value to a field variable on the sub-application(s).");
33  params.addRequiredParam<AuxVariableName>(
34  "variable", "The auxiliary variable to store the transferred values in.");
35  params.addRequiredParam<VariableName>("source_variable", "The variable to transfer from.");
36  return params;
37 }
38 
40  const InputParameters & parameters)
41  : MultiAppTransfer(parameters),
42  _to_var_name(getParam<AuxVariableName>("variable")),
43  _from_var_name(getParam<VariableName>("source_variable"))
44 {
45  if (_directions.size() != 1 || (isParamValid("from_multi_app") && isParamValid("to_multi_app")))
46  paramError("direction", "This transfer is only unidirectional");
47 
48  if (hasFromMultiApp())
49  paramError("from_multi_app", "This transfer direction has not been implemented");
50 }
51 
52 void
54 {
56 
58 
59  if (isParamValid("from_multi_app"))
60  getFromMultiApp()->problemBase().mesh().errorIfDistributedMesh(
61  "MultiAppVariableValueSampleTransfer");
62  if (isParamValid("to_multi_app"))
63  getToMultiApp()->problemBase().mesh().errorIfDistributedMesh(
64  "MultiAppVariableValueSampleTransfer");
65 }
66 
67 void
69 {
70  TIME_SECTION(
71  "MultiAppVariableValueSampleTransfer::execute()", 5, "Sampling a variable for transfer");
72 
73  switch (_current_direction)
74  {
75  case TO_MULTIAPP:
76  {
77  FEProblemBase & from_problem = getToMultiApp()->problemBase();
78  MooseVariableField<Real> & from_var = static_cast<MooseVariableField<Real> &>(
79  from_problem.getActualFieldVariable(0, _from_var_name));
80  SystemBase & from_system_base = from_var.sys();
81  SubProblem & from_sub_problem = from_system_base.subproblem();
82 
83  MooseMesh & from_mesh = from_problem.mesh();
84 
85  std::unique_ptr<PointLocatorBase> pl = from_mesh.getPointLocator();
86 
87  for (unsigned int i = 0; i < getToMultiApp()->numGlobalApps(); i++)
88  {
90 
91  { // Get the value of the variable at the point where this multiapp is in the master domain
92 
93  Point multi_app_position = getToMultiApp()->position(i);
94 
95  std::vector<Point> point_vec(1, multi_app_position);
96 
97  // First find the element the hit lands in
98  const Elem * elem = (*pl)(multi_app_position);
99 
100  if (elem && elem->processor_id() == from_mesh.processor_id())
101  {
102  from_sub_problem.setCurrentSubdomainID(elem, 0);
103  from_sub_problem.reinitElemPhys(elem, point_vec, 0);
104 
105  mooseAssert(from_var.sln().size() == 1, "No values in u!");
106  value = from_var.sln()[0];
107  }
108 
110 
112  mooseError("Transfer failed to sample point value at point: ", multi_app_position);
113  }
114 
115  if (getToMultiApp()->hasLocalApp(i))
116  {
118 
119  // Loop over the master nodes and set the value of the variable
120  System * to_sys = find_sys(getToMultiApp()->appProblemBase(i).es(), _to_var_name);
121 
122  unsigned int sys_num = to_sys->number();
123  unsigned int var_num = to_sys->variable_number(_to_var_name);
124 
125  NumericVector<Real> & solution = getToMultiApp()->appTransferVector(i, _to_var_name);
126 
127  MooseMesh & mesh = getToMultiApp()->appProblemBase(i).mesh();
128 
129  for (const auto & node : as_range(mesh.localNodesBegin(), mesh.localNodesEnd()))
130  {
131  if (node->n_dofs(sys_num, var_num) > 0) // If this variable has dofs at this node
132  {
133  // The zero only works for LAGRANGE!
134  dof_id_type dof = node->dof_number(sys_num, var_num, 0);
135 
136  solution.set(dof, value);
137  }
138  }
139  solution.close();
140  getToMultiApp()->appProblemBase(i).es().update();
141  }
142  }
143 
144  break;
145  }
146  case FROM_MULTIAPP:
147  {
148  mooseError("Doesn't make sense to transfer a sampled variable's value from a MultiApp!!");
149  break;
150  }
151  }
152 }
registerMooseObject("MooseApp", MultiAppVariableValueSampleTransfer)
const std::shared_ptr< MultiApp > getFromMultiApp() const
Get the MultiApp to transfer data from.
MooseEnum _current_direction
Definition: Transfer.h:106
MultiAppVariableValueSampleTransfer(const InputParameters &parameters)
MeshBase & mesh
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 Parallel::Communicator & comm() const
virtual void setCurrentSubdomainID(const Elem *elem, const THREAD_ID tid)=0
const std::shared_ptr< MultiApp > getToMultiApp() const
Get the MultiApp to transfer data to.
const Parallel::Communicator & _communicator
AuxVariableName _to_var_name
Variable to sample in the parent application.
Base class for a system (of equations)
Definition: SystemBase.h:84
VariableName _from_var_name
Variable in the MultiApp to fill with the sampled value.
bool hasFromMultiApp() const
Whether the transfer owns a non-null from_multi_app.
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...
auto max(const L &left, const R &right)
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition: MooseArray.h:256
void variableIntegrityCheck(const AuxVariableName &var_name) const
Utility to verify that the variable in the destination system exists.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
void initialSetup() override
Method called at the beginning of the simulation for checking integrity or doing one-time setup...
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:88
virtual void initialSetup() override
Method called at the beginning of the simulation for checking integrity or doing one-time setup...
MooseVariableFieldBase & getActualFieldVariable(const THREAD_ID tid, const std::string &var_name) override
Returns the variable reference for requested MooseVariableField which may be in any system...
Samples a variable&#39;s value in the parent application domain at the point where the MultiApp (for each...
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 reinitElemPhys(const Elem *elem, const std::vector< Point > &phys_points_in_elem, const THREAD_ID tid)=0
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:75
virtual const FieldVariableValue & sln() const =0
MultiMooseEnum _directions
The directions this Transfer is to be executed on.
Definition: Transfer.h:110
void max(const T &r, T &o, Request &req) const
Base class for all MultiAppTransfer objects.
virtual MooseMesh & mesh() override
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...
static System * find_sys(EquationSystems &es, const std::string &var_name)
Small helper function for finding the system containing the variable.
Definition: Transfer.C:89
processor_id_type processor_id() const
SystemBase & sys()
Get the system this variable is part of.
virtual std::unique_ptr< PointLocatorBase > getPointLocator() const
Proxy function to get a (sub)PointLocator from either the underlying libMesh mesh (default)...
Definition: MooseMesh.C:3479
virtual void execute() override
Execute the transfer.
uint8_t dof_id_type