www.mooseframework.org
InputParameterWarehouse.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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
12 #include "InputParameters.h"
13 
15  : _input_parameters(libMesh::n_threads()), _controllable_items(libMesh::n_threads())
16 {
17 }
18 
21  InputParameters parameters,
22  THREAD_ID tid /* =0 */)
23 {
24  // Error if the name contains "::"
25  if (name.find("::") != std::string::npos)
26  mooseError("The object name may not contain '::' in the name: ", name);
27 
28  // Create the actual InputParameters object that will be reference by the objects
29  std::shared_ptr<InputParameters> ptr = std::make_shared<InputParameters>(parameters);
30 
31  // The object name defined by the base class name, this method of storing is used for
32  // determining the uniqueness of the name
33  MooseObjectName unique_name(ptr->get<std::string>("_moose_base"), name, "::");
34 
35  // Check that the Parameters do not already exist
36  if (_input_parameters[tid].find(unique_name) != _input_parameters[tid].end())
37  mooseError("A '",
38  unique_name.tag(),
39  "' object already exists with the name '",
40  unique_name.name(),
41  "'.\n");
42 
43  // Store the parameters according to the base name
44  _input_parameters[tid].insert(
45  std::pair<MooseObjectName, std::shared_ptr<InputParameters>>(unique_name, ptr));
46 
47  // Build a list of object names
48  std::vector<MooseObjectName> object_names;
49  object_names.push_back(unique_name);
50 
51  // Store the object according to the control tags
52  if (ptr->isParamValid("control_tags"))
53  {
54  const std::vector<std::string> & tags = ptr->get<std::vector<std::string>>("control_tags");
55  for (const auto & tag : tags)
56  {
57  if (!tag.empty())
58  {
59  _input_parameters[tid].insert(std::pair<MooseObjectName, std::shared_ptr<InputParameters>>(
60  MooseObjectName(tag, name), ptr));
61  object_names.emplace_back(tag, name);
62  }
63  }
64  }
65 
66  // Store controllable parameters using all possible names
67  for (libMesh::Parameters::iterator map_iter = ptr->begin(); map_iter != ptr->end(); ++map_iter)
68  {
69  const std::string & name = map_iter->first;
70  libMesh::Parameters::Value * value = map_iter->second;
71 
72  if (ptr->isControllable(name))
73  for (const auto & object_name : object_names)
74  {
75  MooseObjectParameterName param_name(object_name, name);
76  _controllable_items[tid].emplace_back(std::make_shared<ControllableItem>(
77  param_name, value, ptr->getControllableExecuteOnTypes(name)));
78  }
79  }
80 
81  // Set the name and tid parameters, and unique_name
82  std::stringstream oss;
83  oss << unique_name;
84 
85  ptr->addPrivateParam<std::string>("_unique_name", oss.str());
86  ptr->addPrivateParam<std::string>("_object_name", name);
87  ptr->addPrivateParam<THREAD_ID>("_tid", tid);
88  ptr->allowCopy(false); // no more copies allowed
89 
90  // Return a reference to the InputParameters object
91  return *ptr;
92 }
93 
94 void
96 {
97  auto moose_object_name_string = moose_object.parameters().get<std::string>("_unique_name");
98  MooseObjectName moose_object_name(moose_object_name_string);
99  _input_parameters[tid].erase(moose_object_name);
100 }
101 
102 const InputParameters &
104 {
105  return getInputParameters(MooseObjectName(name), tid);
106 }
107 
108 const InputParameters &
110  const std::string & name,
111  THREAD_ID tid) const
112 {
113  return getInputParameters(MooseObjectName(tag, name), tid);
114 }
115 
116 const InputParameters &
118  THREAD_ID tid) const
119 {
120  return getInputParameters(object_name, tid);
121 }
122 
124 InputParameterWarehouse::getInputParameters(const std::string & name, THREAD_ID tid) const
125 {
126  return getInputParameters(MooseObjectName(name), tid);
127 }
128 
131  const std::string & name,
132  THREAD_ID tid) const
133 {
134  return getInputParameters(MooseObjectName(tag, name), tid);
135 }
136 
139  THREAD_ID tid) const
140 {
141  // Locate the InputParameters object and error if it was not located
142  const auto iter = _input_parameters[tid].find(object_name);
143  if (iter == _input_parameters[tid].end())
144  mooseError("Unknown InputParameters object ", object_name);
145 
146  // Return a reference to the parameter
147  return *(iter->second.get());
148 }
149 
150 const std::multimap<MooseObjectName, std::shared_ptr<InputParameters>> &
152 {
153  return _input_parameters[tid];
154 }
155 
156 void
158  const MooseObjectParameterName & slave,
159  bool error_on_empty /*=true*/)
160 {
161  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
162  {
163  std::vector<ControllableItem *> masters = getControllableItems(master, tid);
164  if (masters.empty() && error_on_empty && tid == 0) // some objects only exist on tid 0
165  mooseError("Unable to locate master parameter with name ", master);
166  else if (masters.empty())
167  return;
168 
169  std::vector<ControllableItem *> slaves = getControllableItems(slave, tid);
170  if (slaves.empty() && error_on_empty && tid == 0) // some objects only exist on tid 0
171  mooseError("Unable to locate slave parameter with name ", slave);
172  else if (slaves.empty())
173  return;
174 
175  for (auto master_ptr : masters)
176  for (auto slave_ptr : slaves)
177  if (master_ptr != slave_ptr)
178  master_ptr->connect(slave_ptr);
179  }
180 }
181 
182 void
184  const MooseObjectParameterName & slave)
185 {
186  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
187  {
188  std::vector<ControllableItem *> slaves = getControllableItems(slave, tid);
189  if (slaves.empty() && tid == 0) // some objects only exist on tid 0
190  mooseError("Unable to locate slave parameter with name ", slave);
191 
192  for (auto slave_ptr : slaves)
193  _controllable_items[tid].emplace_back(
194  libmesh_make_unique<ControllableAlias>(alias, slave_ptr));
195  }
196 }
197 
198 std::vector<ControllableItem *>
200  THREAD_ID tid /*=0*/) const
201 {
202  std::vector<ControllableItem *> output;
203  for (auto & ptr : _controllable_items[tid])
204  if (ptr->name() == input)
205  output.push_back(ptr.get());
206  return output;
207 }
208 
211 {
212  ControllableParameter cparam;
213  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
214  for (auto it = _controllable_items[tid].begin(); it != _controllable_items[tid].end(); ++it)
215  if ((*it)->name() == input)
216  cparam.add(it->get());
217  return cparam;
218 }
219 
220 std::string
222 {
223  std::stringstream oss;
224  oss << std::left;
225 
226  for (const auto & item : _controllable_items[0])
227  if (item->isChanged())
228  {
229  oss << item->dump(4);
230  if (reset_changed)
231  item->resetChanged();
232  }
233  return oss.str();
234 }
std::vector< ControllableItem * > getControllableItems(const MooseObjectParameterName &desired, THREAD_ID tid=0) const
Returns a ControllableItem iterator, if the name is located.
void addControllableParameterAlias(const MooseObjectParameterName &alias, const MooseObjectParameterName &slave)
Method for creating alias to an existing controllable parameters.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
The ControllableParameter class is simply a set of ControllableItem objects.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
std::string dumpChangedControls(bool reset_changed) const
InputParameterWarehouse()
Class constructor.
const std::multimap< MooseObjectName, std::shared_ptr< InputParameters > > & getInputParameters(THREAD_ID tid=0) const
Return const reference to the map containing the InputParameter objects.
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:42
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseObject.h:65
ControllableParameter getControllableParameter(const MooseObjectParameterName &input) const
Returns a ControllableParameter object that contains all matches to ControllableItem objects for the ...
void add(ControllableItem *item)
Adds the supplied item with the other items within this object.
InputParameters & addInputParameters(const std::string &name, InputParameters parameters, THREAD_ID tid=0)
Method for adding a new InputParameters object.
void removeInputParameters(const MooseObject &moose_object, THREAD_ID tid=0)
Allows for the deletion and cleanup of an object while the simulation is running. ...
void addControllableParameterConnection(const MooseObjectParameterName &master, const MooseObjectParameterName &slave, bool error_on_empty=true)
Method for linking control parameters of different names.
const InputParameters & getInputParametersObject(const std::string &name, THREAD_ID tid=0) const
Return a const reference to the InputParameters for the named object.
std::vector< std::multimap< MooseObjectName, std::shared_ptr< InputParameters > > > _input_parameters
Storage for the InputParameters objects TODO: Remove multimap.
A class for storing an input parameter name.
A class for storing the names of MooseObject by tag and object name.
std::vector< std::vector< std::shared_ptr< ControllableItem > > > _controllable_items
Storage for controllable parameters via ControllableItem objects, a unique_ptr is used to avoid creat...
unsigned int THREAD_ID
Definition: MooseTypes.h:161