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 
26 template <>
29 {
31  params.addRequiredParam<AuxVariableName>(
32  "variable", "The auxiliary variable to store the transferred values in.");
33  params.addRequiredParam<VariableName>("source_variable", "The variable to transfer from.");
34  return params;
35 }
36 
38  const InputParameters & parameters)
39  : MultiAppTransfer(parameters),
40  _to_var_name(getParam<AuxVariableName>("variable")),
41  _from_var_name(getParam<VariableName>("source_variable"))
42 {
43 }
44 
45 void
47 {
49 
50  _multi_app->problemBase().mesh().errorIfDistributedMesh("MultiAppVariableValueSampleTransfer");
51 }
52 
53 void
55 {
56  _console << "Beginning VariableValueSampleTransfer " << name() << std::endl;
57 
58  switch (_direction)
59  {
60  case TO_MULTIAPP:
61  {
62  FEProblemBase & from_problem = _multi_app->problemBase();
63  MooseVariable & from_var = from_problem.getStandardVariable(0, _from_var_name);
64  SystemBase & from_system_base = from_var.sys();
65  SubProblem & from_sub_problem = from_system_base.subproblem();
66 
67  MooseMesh & from_mesh = from_problem.mesh();
68 
69  std::unique_ptr<PointLocatorBase> pl = from_mesh.getPointLocator();
70 
71  for (unsigned int i = 0; i < _multi_app->numGlobalApps(); i++)
72  {
73  Real value = -std::numeric_limits<Real>::max();
74 
75  { // Get the value of the variable at the point where this multiapp is in the master domain
76 
77  Point multi_app_position = _multi_app->position(i);
78 
79  std::vector<Point> point_vec(1, multi_app_position);
80 
81  // First find the element the hit lands in
82  const Elem * elem = (*pl)(multi_app_position);
83 
84  if (elem && elem->processor_id() == from_mesh.processor_id())
85  {
86  from_sub_problem.reinitElemPhys(elem, point_vec, 0);
87 
88  mooseAssert(from_var.sln().size() == 1, "No values in u!");
89  value = from_var.sln()[0];
90  }
91 
92  _communicator.max(value);
93 
94  if (value == -std::numeric_limits<Real>::max())
95  mooseError("Transfer failed to sample point value at point: ", multi_app_position);
96  }
97 
98  if (_multi_app->hasLocalApp(i))
99  {
100  Moose::ScopedCommSwapper swapper(_multi_app->comm());
101 
102  // Loop over the master nodes and set the value of the variable
103  System * to_sys = find_sys(_multi_app->appProblemBase(i).es(), _to_var_name);
104 
105  unsigned int sys_num = to_sys->number();
106  unsigned int var_num = to_sys->variable_number(_to_var_name);
107 
108  NumericVector<Real> & solution = _multi_app->appTransferVector(i, _to_var_name);
109 
110  MooseMesh & mesh = _multi_app->appProblemBase(i).mesh();
111 
112  for (const auto & node : as_range(mesh.localNodesBegin(), mesh.localNodesEnd()))
113  {
114  if (node->n_dofs(sys_num, var_num) > 0) // If this variable has dofs at this node
115  {
116  // The zero only works for LAGRANGE!
117  dof_id_type dof = node->dof_number(sys_num, var_num, 0);
118 
119  solution.set(dof, value);
120  }
121  }
122  solution.close();
123  _multi_app->appProblemBase(i).es().update();
124  }
125  }
126 
127  break;
128  }
129  case FROM_MULTIAPP:
130  {
131  mooseError("Doesn't make sense to transfer a sampled variable's value from a MultiApp!!");
132  break;
133  }
134  }
135 
136  _console << "Finished VariableValueSampleTransfer " << name() << std::endl;
137 }
registerMooseObject("MooseApp", MultiAppVariableValueSampleTransfer)
MeshBase::const_node_iterator localNodesBegin()
Calls local_nodes_begin/end() on the underlying libMesh mesh object.
Definition: MooseMesh.C:2205
MultiAppVariableValueSampleTransfer(const InputParameters &parameters)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const FieldVariableValue & sln() const
element solutions
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
Base class for a system (of equations)
Definition: SystemBase.h:92
std::shared_ptr< MultiApp > _multi_app
The MultiApp this Transfer is transferring data to or from.
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...
MeshBase::const_node_iterator localNodesEnd()
Definition: MooseMesh.C:2211
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition: MooseArray.h:259
InputParameters validParams< MultiAppVariableValueSampleTransfer >()
void variableIntegrityCheck(const AuxVariableName &var_name) const
Utility to verify that the vEariable in the destination system exists.
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:74
virtual void initialSetup() override
Method called at the beginning of the simulation for checking integrity or doing one-time setup...
virtual SubProblem & subproblem()
Definition: SystemBase.h:105
Samples a variable&#39;s value in the Master domain at the point where the MultiApp is.
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:59
InputParameters validParams< MultiAppTransfer >()
Base class for all MultiAppTransfer objects.
virtual MooseMesh & mesh() override
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
virtual MooseVariable & getStandardVariable(THREAD_ID tid, const std::string &var_name) override
Returns the variable reference for requested MooseVariable which may be in any system.
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
static System * find_sys(EquationSystems &es, const std::string &var_name)
Small helper function for finding the system containing the variable.
Definition: Transfer.C:65
const MooseEnum _direction
Whether we&#39;re transferring to or from the MultiApp.
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:2730
virtual void execute() override
Execute the transfer.
virtual void reinitElemPhys(const Elem *elem, const std::vector< Point > &phys_points_in_elem, THREAD_ID tid, bool suppress_displaced_init=false)=0