https://mooseframework.inl.gov
MultiAppPostprocessorInterpolationTransfer.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 "MultiApp.h"
17 #include "MooseAppCoordTransform.h"
18 
19 #include "libmesh/meshfree_interpolation.h"
20 #include "libmesh/numeric_vector.h"
21 #include "libmesh/system.h"
22 #include "libmesh/radial_basis_interpolation.h"
23 
24 using namespace libMesh;
25 
27 
30 {
32  params.addClassDescription("Transfer postprocessor data from sub-application into field data on "
33  "the parent application.");
34  params.addRequiredParam<AuxVariableName>(
35  "variable", "The auxiliary variable to store the transferred values in.");
36  params.addRequiredParam<PostprocessorName>("postprocessor", "The Postprocessor to interpolate.");
37  params.addParam<unsigned int>(
38  "num_points", 3, "The number of nearest points to use for interpolation.");
39  params.addParam<Real>(
40  "power", 2, "The polynomial power to use for calculation of the decay in the interpolation.");
41 
42  MooseEnum interp_type("inverse_distance radial_basis", "inverse_distance");
43 
44  params.addParam<MooseEnum>("interp_type", interp_type, "The algorithm to use for interpolation.");
45 
46  params.addParam<Real>("radius",
47  -1,
48  "Radius to use for radial_basis interpolation. If negative "
49  "then the radius is taken as the max distance between "
50  "points.");
51  return params;
52 }
53 
55  const InputParameters & parameters)
56  : MultiAppTransfer(parameters),
57  _postprocessor(getParam<PostprocessorName>("postprocessor")),
58  _to_var_name(getParam<AuxVariableName>("variable")),
59  _num_points(getParam<unsigned int>("num_points")),
60  _power(getParam<Real>("power")),
61  _interp_type(getParam<MooseEnum>("interp_type")),
62  _radius(getParam<Real>("radius")),
63  _nodal(false)
64 {
65  if (isParamValid("to_multi_app"))
66  paramError("to_multi_app", "Unused parameter; only from-MultiApp transfers are implemented");
67 
68  auto & to_fe_type =
69  getFromMultiApp()->problemBase().getStandardVariable(0, _to_var_name).feType();
70  if ((to_fe_type.order != CONSTANT || to_fe_type.family != MONOMIAL) &&
71  (to_fe_type.order != FIRST || to_fe_type.family != LAGRANGE))
72  paramError("variable", "Must be either CONSTANT MONOMIAL or FIRST LAGRANGE");
73 
74  _nodal = to_fe_type.family == LAGRANGE;
75 }
76 
77 void
79 {
80  TIME_SECTION("MultiAppPostprocessorInterpolationTransfer::execute()",
81  5,
82  "Transferring/interpolating postprocessors");
83 
84  switch (_current_direction)
85  {
86  case TO_MULTIAPP:
87  {
88  mooseError("Interpolation from a variable to a MultiApp postprocessors has not been "
89  "implemented. Use MultiAppVariableValueSamplePostprocessorTransfer!");
90  break;
91  }
92  case FROM_MULTIAPP:
93  {
95  std::unique_ptr<InverseDistanceInterpolation<LIBMESH_DIM>> idi;
96 
97  switch (_interp_type)
98  {
99  case 0:
100  idi = std::make_unique<InverseDistanceInterpolation<LIBMESH_DIM>>(
102  break;
103  case 1:
104  idi = std::make_unique<RadialBasisInterpolation<LIBMESH_DIM>>(_communicator, _radius);
105  break;
106  default:
107  mooseError("Unknown interpolation type!");
108  }
109 
110  std::vector<Point> & src_pts(idi->get_source_points());
111  std::vector<Number> & src_vals(idi->get_source_vals());
112 
113  std::vector<std::string> field_vars;
114  field_vars.push_back(_to_var_name);
115  idi->set_field_variables(field_vars);
116 
117  {
118  mooseAssert(_to_transforms.size() == 1, "There should only be one transform here");
119  const auto & to_coord_transform = *_to_transforms[0];
120  for (unsigned int i = 0; i < getFromMultiApp()->numGlobalApps(); i++)
121  {
122  if (getFromMultiApp()->hasLocalApp(i) && getFromMultiApp()->isRootProcessor())
123  {
124  // Evaluation of the _from_transform at the origin yields the transformed position of
125  // the from multi-app
126  if (!getFromMultiApp()->runningInPosition())
127  src_pts.push_back(to_coord_transform.mapBack((*_from_transforms[i])(Point(0))));
128  else
129  // if running in position, the subapp mesh has been transformed so the translation
130  // is no longer applied by the transform
131  src_pts.push_back(to_coord_transform.mapBack(
132  (*_from_transforms[i])(getFromMultiApp()->position(i))));
133  src_vals.push_back(getFromMultiApp()->appPostprocessorValue(i, _postprocessor));
134  }
135  }
136  }
137 
138  // We have only set local values - prepare for use by gathering remote data
139  idi->prepare_for_use();
140 
141  // Loop over the parent app nodes and set the value of the variable
142  {
143  System * to_sys = find_sys(getFromMultiApp()->problemBase().es(), _to_var_name);
144 
145  unsigned int sys_num = to_sys->number();
146  unsigned int var_num = to_sys->variable_number(_to_var_name);
147 
148  NumericVector<Real> & solution = *to_sys->solution;
149 
150  MooseMesh & mesh = getFromMultiApp()->problemBase().mesh();
151 
152  std::vector<std::string> vars;
153 
154  vars.push_back(_to_var_name);
155 
156  if (_nodal)
157  {
158  // handle linear lagrange shape functions
159  for (const auto & node : as_range(mesh.localNodesBegin(), mesh.localNodesEnd()))
160  {
161  if (node->n_dofs(sys_num, var_num) > 0) // If this variable has dofs at this node
162  {
163  std::vector<Point> pts;
164  std::vector<Number> vals;
165 
166  pts.push_back(*node);
167  vals.resize(1);
168 
169  idi->interpolate_field_data(vars, pts, vals);
170 
171  Real value = vals.front();
172 
173  // The zero only works for LAGRANGE!
174  dof_id_type dof = node->dof_number(sys_num, var_num, 0);
175 
176  solution.set(dof, value);
177  }
178  }
179  }
180  else
181  {
182  // handle constant monomial shape functions
183  for (const auto & elem :
184  as_range(mesh.getMesh().local_elements_begin(), mesh.getMesh().local_elements_end()))
185  {
186  // Exclude the elements without dofs
187  if (elem->n_dofs(sys_num, var_num) > 0)
188  {
189  std::vector<Point> pts;
190  std::vector<Number> vals;
191 
192  pts.push_back(elem->vertex_average());
193  vals.resize(1);
194 
195  idi->interpolate_field_data(vars, pts, vals);
196 
197  Real value = vals.front();
198 
199  dof_id_type dof = elem->dof_number(sys_num, var_num, 0);
200  solution.set(dof, value);
201  }
202  }
203  }
204 
205  solution.close();
206  }
207 
208  getFromMultiApp()->problemBase().es().update();
209 
210  break;
211  }
212  }
213 }
LAGRANGE
std::vector< std::unique_ptr< MultiAppCoordTransform > > _to_transforms
const std::shared_ptr< MultiApp > getFromMultiApp() const
Get the MultiApp to transfer data from.
MooseEnum _current_direction
Definition: Transfer.h:106
registerMooseObject("MooseApp", MultiAppPostprocessorInterpolationTransfer)
FIRST
char ** vars
MeshBase & mesh
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Real _radius
Radius to consider for inverse interpolation.
const Parallel::Communicator & _communicator
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
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...
Transfers from postprocessors in child apps of a MultiApp in different locations in parent app mesh...
unsigned int variable_number(std::string_view var) const
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
CONSTANT
unsigned int number() const
void errorIfObjectExecutesOnTransferInSourceApp(const std::string &object_name) const
Error if executing this MooseObject on EXEC_TRANSFER in a source multiapp (from_multiapp, e.g.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
AuxVariableName _to_var_name
Variable in the main application to fill with the interpolation of the postprocessors.
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
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
std::unique_ptr< NumericVector< Number > > solution
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:88
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:33
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 close()=0
MONOMIAL
Real _power
Exponent for power-law decrease of the interpolation coefficients.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Base class for all MultiAppTransfer objects.
std::vector< std::unique_ptr< MultiAppCoordTransform > > _from_transforms
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...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
unsigned int _num_points
Number of points to consider for the interpolation.
virtual void set(const numeric_index_type i, const T value)=0
PostprocessorName _postprocessor
Postprocessor in the child apps to transfer the data from.
MultiAppPostprocessorInterpolationTransfer(const InputParameters &parameters)
void ErrorVector unsigned int
bool _nodal
Whether the target variable is nodal or elemental.
uint8_t dof_id_type