LCOV - code coverage report
Current view: top level - src/transfers - SamplerTransfer.C (source / functions) Hit Total Coverage
Test: stochastic_tools Test Coverage Lines: 47 47 100.0 %
Date: 2017-11-21 14:52:30 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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
      15          15 : validParams<SamplerTransfer>()
      16             : {
      17          15 :   InputParameters params = validParams<MultiAppTransfer>();
      18          30 :   params.addClassDescription("Copies Sampler data to a SamplerReceiver object.");
      19          45 :   params.set<MooseEnum>("direction") = "to_multiapp";
      20          30 :   params.suppressParameter<MooseEnum>("direction");
      21          45 :   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          15 :       "here should match the order of the items in the Sampler.");
      26          45 :   params.addRequiredParam<std::string>("to_control",
      27             :                                        "The name of the 'SamplerReceiver' on the sub application "
      28          15 :                                        "to which the Sampler data will be transfered.");
      29          15 :   return params;
      30             : }
      31             : 
      32          15 : SamplerTransfer::SamplerTransfer(const InputParameters & parameters)
      33             :   : MultiAppTransfer(parameters),
      34          30 :     _parameter_names(getParam<std::vector<std::string>>("parameters")),
      35          75 :     _receiver_name(getParam<std::string>("to_control"))
      36             : {
      37             : 
      38             :   // Determine the Sampler
      39          15 :   std::shared_ptr<SamplerMultiApp> ptr = std::dynamic_pointer_cast<SamplerMultiApp>(_multi_app);
      40          15 :   if (!ptr)
      41           3 :     mooseError("The 'multi_app' parameter must provide a 'SamplerMultiApp' object.");
      42          12 :   _sampler_ptr = &(ptr->getSampler());
      43             : 
      44             :   // Compute the matrix and row for each
      45          24 :   std::vector<DenseMatrix<Real>> out = _sampler_ptr->getSamples();
      46          60 :   for (auto mat = beginIndex(out); mat < out.size(); ++mat)
      47         200 :     for (unsigned int row = 0; row < out[mat].m(); ++row)
      48          88 :       _multi_app_matrix_row.push_back(std::make_pair(mat, row));
      49          12 : }
      50             : 
      51             : void
      52          11 : SamplerTransfer::execute()
      53             : {
      54             :   // Get the Sampler data
      55          16 :   const std::vector<DenseMatrix<Real>> samples = _sampler_ptr->getSamples();
      56             : 
      57             :   // Loop over all sub-apps
      58         103 :   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          52 :     if (!_multi_app->hasLocalApp(app_index))
      62             :       continue;
      63             : 
      64             :     // Get the sub-app SamplerReceiver object and perform error checking
      65          52 :     SamplerReceiver * ptr = getReceiver(app_index, samples);
      66             : 
      67             :     // Perform the transfer
      68          92 :     std::pair<unsigned int, unsigned int> loc = _multi_app_matrix_row[app_index];
      69          46 :     ptr->reset(); // clears existing parameter settings
      70         368 :     for (auto j = beginIndex(_parameter_names); j < _parameter_names.size(); ++j)
      71             :     {
      72         184 :       const Real & data = samples[loc.first](loc.second, j);
      73          92 :       ptr->addControlParameter(_parameter_names[j], data);
      74             :     }
      75             :   }
      76           5 : }
      77             : 
      78             : SamplerReceiver *
      79          52 : 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          52 :   FEProblemBase & to_problem = _multi_app->appProblemBase(app_index);
      83             :   ExecuteMooseObjectWarehouse<Control> & control_wh = to_problem.getControlWarehouse();
      84          52 :   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 '",
      88             :                _receiver_name,
      89           2 :                "'.");
      90             : 
      91             :   SamplerReceiver * ptr =
      92         100 :       dynamic_cast<SamplerReceiver *>(control_wh.getActiveObject(_receiver_name).get());
      93             : 
      94          50 :   if (!ptr)
      95             :     mooseError(
      96             :         "The sub-application (",
      97             :         _multi_app->name(),
      98           2 :         ") 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          96 :   std::pair<unsigned int, unsigned int> loc = _multi_app_matrix_row[app_index];
     102         144 :   if (_parameter_names.size() != samples[loc.first].n())
     103             :     mooseError("The number of parameters (",
     104           2 :                _parameter_names.size(),
     105             :                ") does not match the number of columns (",
     106           2 :                samples[loc.first].n(),
     107             :                ") in the Sampler data matrix with index ",
     108             :                loc.first,
     109           2 :                ".");
     110             : 
     111          46 :   return ptr;
     112        1812 : }

Generated by: LCOV version 1.11