https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MultiAppVariableValueSampleTransfer.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 "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
24using namespace libMesh;
25
27
30{
33 "Transfers the value of a variable within the master application at each sub-application "
34 "position and transfers the value to a field variable on the sub-application(s).");
35 params.addRequiredParam<AuxVariableName>(
36 "variable", "The auxiliary variable to store the transferred values in.");
37 params.addRequiredParam<VariableName>("source_variable", "The variable to transfer from.");
38 return params;
39}
40
42 const InputParameters & parameters)
43 : MultiAppTransfer(parameters),
44 _to_var_name(getParam<AuxVariableName>("variable")),
45 _from_var_name(getParam<VariableName>("source_variable"))
46{
47 if (_directions.size() != 1 || (isParamValid("from_multi_app") && isParamValid("to_multi_app")))
48 paramError("direction", "This transfer is only unidirectional");
49
50 if (hasFromMultiApp())
51 paramError("from_multi_app", "This transfer direction has not been implemented");
52}
53
54void
56{
58
60
61 if (isParamValid("from_multi_app"))
62 getFromMultiApp()->problemBase().mesh().errorIfDistributedMesh(
63 "MultiAppVariableValueSampleTransfer");
64 if (isParamValid("to_multi_app"))
65 getToMultiApp()->problemBase().mesh().errorIfDistributedMesh(
66 "MultiAppVariableValueSampleTransfer");
67}
68
69void
71{
72 TIME_SECTION(
73 "MultiAppVariableValueSampleTransfer::execute()", 5, "Sampling a variable for transfer");
74
75 switch (_current_direction)
76 {
77 case TO_MULTIAPP:
78 {
79 FEProblemBase & from_problem = getToMultiApp()->problemBase();
80 MooseVariableField<Real> & from_var = static_cast<MooseVariableField<Real> &>(
81 from_problem.getActualFieldVariable(0, _from_var_name));
82 SystemBase & from_system_base = from_var.sys();
83 SubProblem & from_sub_problem = from_system_base.subproblem();
84
85 MooseMesh & from_mesh = from_problem.mesh();
86
87 std::unique_ptr<PointLocatorBase> pl = from_mesh.getPointLocator();
88
89 for (unsigned int i = 0; i < getToMultiApp()->numGlobalApps(); i++)
90 {
91 Real value = -std::numeric_limits<Real>::max();
92
93 { // Get the value of the variable at the point where this multiapp is in the master domain
94
95 Point multi_app_position = getToMultiApp()->position(i);
96
97 std::vector<Point> point_vec(1, multi_app_position);
98
99 // First find the element the hit lands in
100 const Elem * elem = (*pl)(multi_app_position);
101
102 if (elem && elem->processor_id() == from_mesh.processor_id())
103 {
104 from_sub_problem.setCurrentSubdomainID(elem, 0);
105 from_sub_problem.reinitElemPhys(elem, point_vec, 0);
106
107 mooseAssert(from_var.sln().size() == 1, "No values in u!");
108 value = from_var.sln()[0];
109 }
110
111 _communicator.max(value);
112
113 if (value == -std::numeric_limits<Real>::max())
114 mooseError("Transfer failed to sample point value at point: ", multi_app_position);
115 }
116
117 if (getToMultiApp()->hasLocalApp(i))
118 {
120
121 // Loop over the master nodes and set the value of the variable
122 System * to_sys = find_sys(getToMultiApp()->appProblemBase(i).es(), _to_var_name);
123
124 unsigned int sys_num = to_sys->number();
125 unsigned int var_num = to_sys->variable_number(_to_var_name);
126
127 NumericVector<Real> & solution = getToMultiApp()->appTransferVector(i, _to_var_name);
128
129 MooseMesh & mesh = getToMultiApp()->appProblemBase(i).mesh();
130
131 for (const auto & node : as_range(mesh.localNodesBegin(), mesh.localNodesEnd()))
132 {
133 if (node->n_dofs(sys_num, var_num) > 0) // If this variable has dofs at this node
134 {
135 // The zero only works for LAGRANGE!
136 dof_id_type dof = node->dof_number(sys_num, var_num, 0);
137
138 solution.set(dof, value);
139 }
140 }
141 solution.close();
142 getToMultiApp()->appProblemBase(i).es().update();
143 }
144 }
145
146 break;
147 }
148 case FROM_MULTIAPP:
149 {
150 mooseError("Doesn't make sense to transfer a sampled variable's value from a MultiApp!!");
151 break;
152 }
153 }
154}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
registerMooseObject("MooseApp", MultiAppVariableValueSampleTransfer)
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
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.
virtual MooseMesh & mesh() override
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
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...
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.
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition MooseArray.h:259
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 ...
Definition MooseBase.h:457
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition MooseBase.h:199
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition MooseMesh.h:95
virtual std::unique_ptr< libMesh::PointLocatorBase > getPointLocator() const
Proxy function to get a (sub)PointLocator from either the underlying libMesh mesh (default),...
Definition MooseMesh.C:3838
SystemBase & sys()
Get the system this variable is part of.
Class for stuff related to variables.
virtual const FieldVariableValue & sln() const =0
Base class for all MultiAppTransfer objects.
void initialSetup() override
Method called at the beginning of the simulation for checking integrity or doing one-time setup.
const std::shared_ptr< MultiApp > getToMultiApp() const
Get the MultiApp to transfer data to.
static InputParameters validParams()
const std::shared_ptr< MultiApp > getFromMultiApp() const
Get the MultiApp to transfer data from.
void variableIntegrityCheck(const AuxVariableName &var_name, bool is_from_multiapp) const
Utility to verify that the variable in the destination system exists.
bool hasFromMultiApp() const
Whether the transfer owns a non-null from_multi_app.
Samples a variable's value in the parent application domain at the point where the MultiApp (for each...
VariableName _from_var_name
Variable in the MultiApp to fill with the sampled value.
virtual void execute() override
Execute the transfer.
MultiAppVariableValueSampleTransfer(const InputParameters &parameters)
virtual void initialSetup() override
Method called at the beginning of the simulation for checking integrity or doing one-time setup.
AuxVariableName _to_var_name
Variable to sample in the parent application.
unsigned int size() const
Return the number of active items in the MultiMooseEnum.
Generic class for solving transient nonlinear problems.
Definition SubProblem.h:79
virtual void reinitElemPhys(const Elem *elem, const std::vector< Point > &phys_points_in_elem, const THREAD_ID tid)=0
virtual void setCurrentSubdomainID(const Elem *elem, const THREAD_ID tid)=0
Base class for a system (of equations)
Definition SystemBase.h:87
void max(const T &r, T &o, Request &req) const
@ FROM_MULTIAPP
Definition Transfer.h:71
@ TO_MULTIAPP
Definition Transfer.h:70
MultiMooseEnum _directions
The directions this Transfer is to be executed on.
Definition Transfer.h:110
MooseEnum _current_direction
Definition Transfer.h:106
static libMesh::System * find_sys(libMesh::EquationSystems &es, const std::string &var_name)
Small helper function for finding the system containing the variable.
Definition Transfer.C:91
processor_id_type processor_id() const
virtual void set(const numeric_index_type i, const T value)=0
virtual void close()=0
const Parallel::Communicator & _communicator
processor_id_type processor_id() const
const Parallel::Communicator & comm() const
unsigned int variable_number(std::string_view var) const
unsigned int number() const
MeshBase & mesh
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
uint8_t dof_id_type
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real