www.mooseframework.org
MultiAppCopyTransfer.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 
10 // MOOSE includes
11 #include "FEProblem.h"
12 #include "MooseTypes.h"
13 #include "MooseVariableFE.h"
14 #include "MultiApp.h"
15 #include "NonlinearSystem.h"
16 #include "MultiAppCopyTransfer.h"
17 #include "DisplacedProblem.h"
18 #include "MooseMesh.h"
19 
20 #include "libmesh/system.h"
21 #include "libmesh/id_types.h"
22 #include "libmesh/string_to_enum.h"
23 
25 
26 template <>
29 {
31  params.addRequiredParam<VariableName>("variable",
32  "The variable to store the transferred values in.");
33  params.addRequiredParam<VariableName>("source_variable", "The variable to transfer from.");
34  params.addClassDescription(
35  "Copies variables (nonlinear and auxiliary) between multiapps that have identical meshes.");
36  return params;
37 }
38 
40  : MultiAppTransfer(parameters),
41  _to_var_name(getParam<VariableName>("variable")),
42  _from_var_name(getParam<VariableName>("source_variable"))
43 {
44 }
45 
46 void
48 {
50 }
51 
52 void
53 MultiAppCopyTransfer::transferDofObject(libMesh::DofObject * to_object,
54  libMesh::DofObject * from_object,
55  MooseVariableFEBase & to_var,
56  MooseVariableFEBase & from_var)
57 {
58  if (to_object->n_dofs(to_var.sys().number(), to_var.number()) >
59  0) // If this variable has dofs at this node
60  for (unsigned int comp = 0; comp < to_object->n_comp(to_var.sys().number(), to_var.number());
61  ++comp)
62  {
63  dof_id_type dof = to_object->dof_number(to_var.sys().number(), to_var.number(), comp);
64  dof_id_type from_dof =
65  from_object->dof_number(from_var.sys().number(), from_var.number(), comp);
66  Real from_value = from_var.sys().solution()(from_dof);
67  to_var.sys().solution().set(dof, from_value);
68  }
69 }
70 
71 void
73 {
74  // Populate the to/from variables needed to perform the transfer
75  MooseVariableFEBase & to_var = to_problem.getVariable(
77  MeshBase & to_mesh = to_problem.mesh().getMesh();
78 
79  MooseVariableFEBase & from_var = from_problem.getVariable(
81  MeshBase & from_mesh = from_problem.mesh().getMesh();
82 
83  // Check integrity
84  if (to_var.feType() != from_var.feType())
85  paramError("variable",
86  "'variable' and 'source_variable' must be the same type (order and family): ",
87  libMesh::Utility::enum_to_string<FEFamily>(to_var.feType().family),
88  moose::internal::incompatVarMsg(to_var, from_var));
89 
90  if ((to_mesh.n_nodes() != from_mesh.n_nodes()) || (to_mesh.n_elem() != from_mesh.n_elem()))
91  mooseError("The meshes must be identical to utilize MultiAppCopyTransfer.");
92 
93  // Transfer node dofs
94  for (const auto & node : as_range(to_mesh.local_nodes_begin(), to_mesh.local_nodes_end()))
95  transferDofObject(node, from_mesh.node_ptr(node->id()), to_var, from_var);
96 
97  // Transfer elem dofs
98  for (auto & to_elem : as_range(to_mesh.local_elements_begin(), to_mesh.local_elements_end()))
99  {
100  Elem * from_elem = from_mesh.elem_ptr(to_elem->id());
101  mooseAssert(to_elem->type() == from_elem->type(), "The elements must be the same type.");
102  transferDofObject(to_elem, from_elem, to_var, from_var);
103  }
104 
105  to_var.sys().solution().close();
106  to_var.sys().update();
107 }
108 
109 void
111 {
112  _console << "Beginning MultiAppCopyTransfer " << name() << std::endl;
113 
114  if (_direction == TO_MULTIAPP)
115  {
116  FEProblemBase & from_problem = _multi_app->problemBase();
117  for (unsigned int i = 0; i < _multi_app->numGlobalApps(); i++)
118  if (_multi_app->hasLocalApp(i))
119  transfer(_multi_app->appProblemBase(i), from_problem);
120  }
121 
122  else if (_direction == FROM_MULTIAPP)
123  {
124  FEProblemBase & to_problem = _multi_app->problemBase();
125  for (unsigned int i = 0; i < _multi_app->numGlobalApps(); i++)
126  if (_multi_app->hasLocalApp(i))
127  transfer(to_problem, _multi_app->appProblemBase(i));
128  }
129 
130  _console << "Finished MultiAppCopyTransfer " << name() << std::endl;
131 }
const VariableName & _to_var_name
The name of the variable to transfer to.
void transferDofObject(libMesh::DofObject *to_object, libMesh::DofObject *from_object, MooseVariableFEBase &to_var, MooseVariableFEBase &from_var)
Performs the transfer of values between a node or element.
std::string incompatVarMsg(MooseVariableFEBase &var1, MooseVariableFEBase &var2)
Builds and returns a string of the form:
Definition: MooseError.C:22
virtual MooseVariableFEBase & getVariable(THREAD_ID tid, const std::string &var_name, Moose::VarKindType expected_var_type=Moose::VarKindType::VAR_ANY, Moose::VarFieldType expected_var_field_type=Moose::VarFieldType::VAR_FIELD_ANY) override
Returns the variable reference for requested variable which must be of the expected_var_type (Nonline...
unsigned int number() const
Get variable number coming from libMesh.
void transfer(FEProblemBase &to_problem, FEProblemBase &from_problem)
Performs the transfer of a variable between two problems.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Copy the value to the target domain from the nearest node in the source domain.
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
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.
const FEType & feType() const
Get the type of finite element object.
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 update()
Update the system (doing libMesh magic)
Definition: SystemBase.C:1020
void variableIntegrityCheck(const AuxVariableName &var_name) const
Utility to verify that the vEariable in the destination system exists.
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2567
virtual void initialSetup() override
Performs basic error checking that the variable exists on MultiApp.
virtual unsigned int number() const
Gets the number of this system.
Definition: SystemBase.C:926
MultiAppCopyTransfer(const InputParameters &parameters)
void paramError(const std::string &param, Args... args)
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition: MooseObject.h:108
virtual void execute() override
Performs the transfer of a variable (Nonlinear or Auxiliary) to/from the Multiapp.
InputParameters validParams< MultiAppTransfer >()
Base class for all MultiAppTransfer objects.
InputParameters validParams< MultiAppCopyTransfer >()
virtual MooseMesh & mesh() override
virtual NumericVector< Number > & solution()=0
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
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...
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
const MooseEnum _direction
Whether we&#39;re transferring to or from the MultiApp.
const VariableName & _from_var_name
Name of variable transfering from.
SystemBase & sys()
Get the system this variable is part of.
registerMooseObject("MooseApp", MultiAppCopyTransfer)