https://mooseframework.inl.gov
Loading...
Searching...
No Matches
SamplerReceiver.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
10// MOOSE includes
11#include "SamplerReceiver.h"
12#include "Function.h"
13
14registerMooseObject("StochasticToolsApp", SamplerReceiver);
15
18{
21 "Control for receiving data from a Sampler via SamplerParameterTransfer.");
22 params.set<ExecFlagEnum>("execute_on") = EXEC_TIMESTEP_BEGIN;
23 return params;
24}
25
26SamplerReceiver::SamplerReceiver(const InputParameters & parameters) : Control(parameters) {}
27
28void
30{
31 // Send parameters from root
34 if (_parameters.size() != _values.size())
35 mooseError("Internal error: Number of parameters does not match number of values.");
36
37 // Loop through all the parameters and set the controllable values for each parameter.
38 for (const auto & i : index_range(_parameters))
39 {
40 const std::string & param_name = _parameters[i];
41 const std::vector<Real> & value = _values[i];
42
43 ControllableParameter control_param = getControllableParameterByName(param_name);
44
45 // Real
46 if (control_param.check<Real>())
47 {
48 // There must be enough data to populate the controlled parameter
49 if (value.size() != 1)
50 mooseError("The Real parameter '",
51 param_name,
52 "' expects a single value, but the vector supplying its values has a size of ",
53 value.size(),
54 ".");
55 control_param.set<Real>(value[0]);
56 }
57 else if (control_param.check<std::vector<Real>>())
58 control_param.set<std::vector<Real>>(value);
59 else
60 // If the loop gets here it failed to find what it was looking for
61 mooseError("Unable to locate a Real or std::vector<Real> parameter with the name '",
62 param_name,
63 ".'");
64 }
65}
66
67void
68SamplerReceiver::transfer(const std::map<std::string, std::vector<Real>> & param_values)
69{
70 // We'll only transfer on the root processor and gather later
71 if (processor_id() == 0)
72 {
73 _parameters.clear();
74 _values.clear();
75 for (const auto & pv : param_values)
76 {
77 _parameters.push_back(pv.first);
78 _values.push_back(pv.second);
79 }
80 }
81}
const ExecFlagType EXEC_TIMESTEP_BEGIN
registerMooseObject("StochasticToolsApp", SamplerReceiver)
static InputParameters validParams()
ControllableParameter getControllableParameterByName(const std::string &param_name)
void set(const T &value, bool type_check=true)
void addClassDescription(const std::string &doc_string)
T & set(const std::string &name, bool quiet_mode=false)
void mooseError(Args &&... args) const
A Control object for receiving data from a master application Sampler object.
std::vector< std::vector< Real > > _values
Values to use when modifying parameters.
static InputParameters validParams()
virtual void execute() override
std::vector< std::string > _parameters
Parameter names to modify.
SamplerReceiver(const InputParameters &parameters)
void transfer(const std::map< std::string, std::vector< Real > > &param_values)
Update the parameters and associated values via _parameters and _values.
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
const Parallel::Communicator & _communicator
processor_id_type processor_id() const