www.mooseframework.org
SamplerTransfer.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
3 /* */
4 /* All contents are licensed under LGPL V2.1 */
5 /* See LICENSE for full restrictions */
6 /****************************************************************/
7 
8 // MOOSE includes
9 #include "SamplerTransfer.h"
10 #include "SamplerMultiApp.h"
11 #include "SamplerReceiver.h"
12 
13 template <>
14 InputParameters
16 {
17  InputParameters params = validParams<MultiAppTransfer>();
18  params.addClassDescription("Copies Sampler data to a SamplerReceiver object.");
19  params.set<MooseEnum>("direction") = "to_multiapp";
20  params.suppressParameter<MooseEnum>("direction");
21  params.addParam<std::vector<std::string>>(
22  "parameters",
23  "A list of parameters (on the sub application) to control "
24  "with the Sampler data. The order of the parameters listed "
25  "here should match the order of the items in the Sampler.");
26  params.addRequiredParam<std::string>("to_control",
27  "The name of the 'SamplerReceiver' on the sub application "
28  "to which the Sampler data will be transfered.");
29  return params;
30 }
31 
32 SamplerTransfer::SamplerTransfer(const InputParameters & parameters)
33  : MultiAppTransfer(parameters),
34  _parameter_names(getParam<std::vector<std::string>>("parameters")),
35  _receiver_name(getParam<std::string>("to_control"))
36 {
37 
38  // Determine the Sampler
39  std::shared_ptr<SamplerMultiApp> ptr = std::dynamic_pointer_cast<SamplerMultiApp>(_multi_app);
40  if (!ptr)
41  mooseError("The 'multi_app' parameter must provide a 'SamplerMultiApp' object.");
42  _sampler_ptr = &(ptr->getSampler());
43 
44  // Compute the matrix and row for each
45  std::vector<DenseMatrix<Real>> out = _sampler_ptr->getSamples();
46  for (auto mat = beginIndex(out); mat < out.size(); ++mat)
47  for (unsigned int row = 0; row < out[mat].m(); ++row)
48  _multi_app_matrix_row.push_back(std::make_pair(mat, row));
49 }
50 
51 void
53 {
54  // Get the Sampler data
55  const std::vector<DenseMatrix<Real>> samples = _sampler_ptr->getSamples();
56 
57  // Loop over all sub-apps
58  for (unsigned int app_index = 0; app_index < _multi_app->numGlobalApps(); app_index++)
59  {
60  // Do nothing if the sub-app is not local
61  if (!_multi_app->hasLocalApp(app_index))
62  continue;
63 
64  // Get the sub-app SamplerReceiver object and perform error checking
65  SamplerReceiver * ptr = getReceiver(app_index, samples);
66 
67  // Perform the transfer
68  std::pair<unsigned int, unsigned int> loc = _multi_app_matrix_row[app_index];
69  ptr->reset(); // clears existing parameter settings
70  for (auto j = beginIndex(_parameter_names); j < _parameter_names.size(); ++j)
71  {
72  const Real & data = samples[loc.first](loc.second, j);
74  }
75  }
76 }
77 
79 SamplerTransfer::getReceiver(unsigned int app_index, const std::vector<DenseMatrix<Real>> & samples)
80 {
81  // Test that the sub-application has the given Control object
82  FEProblemBase & to_problem = _multi_app->appProblemBase(app_index);
83  ExecuteMooseObjectWarehouse<Control> & control_wh = to_problem.getControlWarehouse();
84  if (!control_wh.hasActiveObject(_receiver_name))
85  mooseError("The sub-application (",
86  _multi_app->name(),
87  ") does not contain a Control object with the name '",
89  "'.");
90 
91  SamplerReceiver * ptr =
92  dynamic_cast<SamplerReceiver *>(control_wh.getActiveObject(_receiver_name).get());
93 
94  if (!ptr)
95  mooseError(
96  "The sub-application (",
97  _multi_app->name(),
98  ") Control object for the 'to_control' parameter must be of type 'SamplerReceiver'.");
99 
100  // Test the size of parameter list with the number of columns in Sampler matrix
101  std::pair<unsigned int, unsigned int> loc = _multi_app_matrix_row[app_index];
102  if (_parameter_names.size() != samples[loc.first].n())
103  mooseError("The number of parameters (",
104  _parameter_names.size(),
105  ") does not match the number of columns (",
106  samples[loc.first].n(),
107  ") in the Sampler data matrix with index ",
108  loc.first,
109  ".");
110 
111  return ptr;
112 }
std::vector< std::pair< unsigned int, unsigned int > > _multi_app_matrix_row
The matrix and row for each MultiApp.
void addControlParameter(const std::string &name, const Real &value)
Appends the list of parameters to modify.
virtual void execute() override
const std::vector< std::string > & _parameter_names
Storage for the list of parameters to control.
void reset()
Clears the list of parameters to modify.
InputParameters validParams< SamplerTransfer >()
SamplerReceiver * getReceiver(unsigned int app_index, const std::vector< DenseMatrix< Real >> &samples)
Return the SamplerReceiver object and perform error checking.
Sampler * _sampler_ptr
Pointer to the Sampler object used by the SamplerMultiApp.
A Control object for receiving data from a master application Sampler object.
SamplerTransfer(const InputParameters &parameters)
const std::string & _receiver_name
The name of the SamplerReceiver Control object on the sub-application.