22#include "libmesh/meshfree_interpolation.h"
23#include "libmesh/system.h"
34 "Samples the value of a variable within the main application at each sub-application "
35 "position and transfers the value to a postprocessor on the sub-application(s) when "
36 "performing the to-multiapp transfer. Reconstructs the value of a CONSTANT MONOMIAL "
37 "variable associating the value of each element to the value of the postprocessor "
38 "in the closest sub-application whem performing the from-multiapp transfer.");
41 "The name of the postprocessor in the MultiApp to transfer the value to. "
42 "This should most likely be a Reciever Postprocessor.");
43 params.
addRequiredParam<VariableName>(
"source_variable",
"The variable to transfer from.");
45 "source_variable_component",
47 "The component of source variable, may be non-zero for array variables.");
49 "map_array_variable_components_to_child_apps",
51 "When true, groups of sub-applications will be associated with different components of the "
52 "supplied array variable in 'source_variable'. For instance, if there are 9 sub-applications "
53 "and 3 components in the variable, sub-apps 0-2 will go to component 0, 3-5 will go to 1, "
54 "and 6-8 will go to 2.");
66 _postprocessor_name(getParam<PostprocessorName>(
"postprocessor")),
67 _var_name(getParam<VariableName>(
"source_variable")),
68 _comp(getParam<unsigned
int>(
"source_variable_component")),
69 _var(_fe_problem.getVariable(0, _var_name)),
70 _map_comp_to_child(getParam<bool>(
"map_array_variable_components_to_child_apps"))
73 paramError(
"direction",
"This transfer is only unidirectional");
79 if (fe_type.order != CONSTANT || fe_type.family != MONOMIAL)
81 "Variable must be in CONSTANT MONOMIAL when transferring from a postprocessor "
85 paramError(
"source_variable",
"Variable must be an auxiliary variable");
88 mooseError(
"MultiAppVariableValueSamplePostprocessorTransfer has not been made to support "
92 paramError(
"map_array_variable_components_to_child_apps",
93 "'source_variable' must be an array variable when mapping array variable components "
94 "to child applications.");
96 paramError(
"map_array_variable_components_to_child_apps",
97 "'source_variable_component' is invalid when mapping array variable components to "
98 "child applications.");
111 std::numeric_limits<processor_id_type>::max());
112 for (
const auto i : make_range(num_global_apps))
118 for (
const auto i : make_range(num_global_apps))
121 "Every element in the vector should have been set.");
124 "If I owned this app, then the processor id value should be my own");
137 unsigned int multiapp_pos_id = 0;
138 for (
auto & elem : as_range(
mesh.active_local_elements_begin(),
mesh.active_local_elements_end()))
147 Real
distance = std::numeric_limits<Real>::max();
148 unsigned int count = 0;
152 Real current_distance = (
getFromMultiApp()->position(j) - elem->true_centroid()).norm();
153 if (MooseUtils::absoluteFuzzyLessThan(current_distance,
distance))
159 else if (MooseUtils::absoluteFuzzyEqual(current_distance,
distance))
164 "The distances of an element to more than one sub-applications are too close "
167 "'. The code chooses the sub-application with the smallest ID to set "
168 "the variable on the element, which may created undesired variable solutions."
169 "\nHaving different positions for sub-applications, "
170 "a centroid-based MultiApp or adding block restriction to the variable can "
171 "be used to resolve this warning.");
190 paramError(
"map_array_variable_components_to_child_apps",
191 "The number of sub-applications (",
193 ") is not divisible by the number of components in '",
213 TIME_SECTION(
"MultiAppVariableValueSamplePostprocessorTransfer::execute()",
215 "Transferring a variable to a postprocessor through sampling");
226 standard_var = cast_ptr<MooseVariableField<Real> *>(&
_var);
228 mooseError(
"MultiAppVariableValueSamplePostprocessorTransfer does not support transfer of "
240 pl->enable_out_of_mesh_mode();
242 for (
unsigned int i = 0; i <
getToMultiApp()->numGlobalApps(); i++)
244 Real value = -std::numeric_limits<Real>::max();
250 std::vector<Point> point_vec(1, multi_app_position);
253 const Elem * elem = (*pl)(multi_app_position);
255 if (elem && elem->processor_id() == from_mesh.
processor_id())
265 "Component must be smaller than the number of components of array variable!");
266 mooseAssert(array_var->
sln().size() == 1,
"No values in u!");
270 value = standard_var->
sln()[0];
271 mooseAssert(standard_var->
sln().
size() == 1,
"No values in u!");
294 std::vector<Real> pp_values(n_subapps, std::numeric_limits<Real>::max());
295 for (
const auto i : make_range(n_subapps))
300 std::unordered_map<processor_id_type, std::vector<unsigned int>> postprocessor_queries;
305 postprocessor_queries[proc_id].push_back(needed_postprocessor);
308 auto gather_data = [&pp_values
313 ](processor_id_type libmesh_dbg_var(pid),
314 const std::vector<unsigned int> & postprocessor_ids,
315 std::vector<Real> & postprocessor_values)
317 mooseAssert(pid != this->
processor_id(),
"Should not be pulling from self");
318 postprocessor_values.resize(postprocessor_ids.size());
319 for (
const auto i : index_range(postprocessor_ids))
321 const auto pp_id = postprocessor_ids[i];
322 const auto pp_value = pp_values[pp_id];
324 pp_value != std::numeric_limits<Real>::max(),
325 "If we are getting queried for postprocessor data, then we better have a valid"
326 "postprocesor value.");
327 postprocessor_values[i] = pp_value;
331 auto act_on_data = [&pp_values
336 ](processor_id_type libmesh_dbg_var(pid),
337 const std::vector<unsigned int> & postprocessor_ids,
338 const std::vector<Real> & postprocessor_values)
340 mooseAssert(pid != this->
processor_id(),
"Should not be returning a query from self");
341 mooseAssert(postprocessor_ids.size() == postprocessor_values.size(),
342 "should be a 1-to-1 query-to-response");
343 for (
const auto i : index_range(postprocessor_ids))
345 const auto pp_id = postprocessor_ids[i];
346 const auto pp_value = postprocessor_values[i];
347 mooseAssert(pp_value != std::numeric_limits<Real>::max(),
348 "If we are returning postprocessor data, then we better have a valid"
349 "postprocesor value.");
350 pp_values[pp_id] = pp_value;
354 constexpr Real example = 0;
356 _communicator, postprocessor_queries, gather_data, act_on_data, &example);
361 as_range(
mesh.active_local_elements_begin(),
mesh.active_local_elements_end()))
366 std::vector<dof_id_type> dof_indices;
368 mooseAssert(dof_indices.size() ==
_var.
count(),
369 "The variable must be a constant monomial with one DoF on an element per "
372 "We should have pulled all the data we needed.");
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
registerMooseObject("MooseApp", MultiAppVariableValueSamplePostprocessorTransfer)
void ErrorVector unsigned int
bool hasBlocks(const SubdomainName &name) const
Test if the supplied block name is valid for this object.
virtual void reinitElemPhys(const Elem *elem, const std::vector< Point > &phys_points_in_elem, const THREAD_ID tid) override
virtual ArrayMooseVariable & getArrayVariable(const THREAD_ID tid, const std::string &var_name) override
Returns the variable reference for requested ArrayMooseVariable which may be in any system.
AuxiliarySystem & getAuxiliarySystem()
virtual void setCurrentSubdomainID(const Elem *elem, const THREAD_ID tid) override
virtual MooseMesh & mesh() override
virtual void setActiveFEVariableCoupleableVectorTags(std::set< TagID > &vtags, const THREAD_ID tid) override
Interface for notifications that the mesh has changed.
unsigned int size() const
The number of elements that can currently be stored in the array.
const InputParameters & parameters() const
Get the parameters of the object.
const std::string & name() const
Get the name of the class.
void paramError(const std::string ¶m, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
virtual std::unique_ptr< libMesh::PointLocatorBase > getPointLocator() const
Proxy function to get a (sub)PointLocator from either the underlying libMesh mesh (default),...
const libMesh::FEType & feType() const
Get the type of finite element object.
virtual bool isArray() const
SystemBase & sys()
Get the system this variable is part of.
virtual void getDofIndices(const Elem *, std::vector< dof_id_type > &) const
unsigned int count() const
Get the number of components Note: For standard and vector variables, the number is one.
const FieldVariableValue & sln() const override
element solutions
virtual bool isVector() const =0
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.
Samples a variable's value in the parent application domain at the point where the MultiApp (for each...
std::vector< processor_id_type > _postprocessor_to_processor_id
Entries in this vector correspond to the processor ID that owns the application/postprocessor corresp...
const bool _map_comp_to_child
Whether or not to map groups of child applications to each component of an array variable.
MultiAppVariableValueSamplePostprocessorTransfer(const InputParameters ¶meters)
virtual void execute() override
Execute the transfer.
unsigned int getVariableComponent(unsigned int index) const
Maps the child application index to the parent application variable component.
void setupPostprocessorCommunication()
Sets up the postprocessor to processor ID communication pattern data member _postprocessor_to_process...
virtual void initialSetup() override
Method called at the beginning of the simulation for checking integrity or doing one-time setup.
void meshChanged() override
Called on this object when the mesh changes.
AuxVariableName _var_name
the name of the variable on the main-application
static InputParameters validParams()
PostprocessorName _postprocessor_name
the name of the postprocessor on the sub-applications
void cacheElemToPostprocessorData()
Method that caches data regarding the element to postprocess relationship.
std::vector< unsigned int > _cached_multiapp_pos_ids
Sub-application ids of all local active elements in the main-application When _map_comp_to_child == t...
MooseVariableFieldBase & _var
the moose variable
unsigned int _apps_per_component
The number of applications associated with a component of the variable when doing array variable samp...
std::unordered_set< unsigned int > _needed_postprocessors
The postprocessors that this process needs for its active local elements.
bool isValueSet(const std::string &value) const
Methods for seeing if a value is set in the MultiMooseEnum.
unsigned int size() const
Return the number of active items in the MultiMooseEnum.
virtual TagID getVectorTagID(const TagName &tag_name) const
Get a TagID from a TagName.
const std::set< TagID > & getActiveFEVariableCoupleableVectorTags(const THREAD_ID tid) const
virtual bool hasVariable(const std::string &var_name) const
Query a system for a variable.
NumericVector< Number > & solution()
void max(const T &r, T &o, Request &req) const
void min(const T &r, T &o, Request &req) const
MultiMooseEnum _directions
The directions this Transfer is to be executed on.
FEProblemBase & _fe_problem
MooseEnum _current_direction
const Parallel::Communicator & _communicator
processor_id_type processor_id() const
const TagName SOLUTION_TAG
void pull_parallel_vector_data(const Communicator &comm, const MapToVectors &queries, GatherFunctor &gather_data, const ActionFunctor &act_on_data, const datum *example)
Real distance(const Point &p)