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  const InputParameters & parameters,
22  THREAD_ID tid,
23  const AddRemoveParamsKey)
24 {
25  // Error if the name contains "::"
26  if (name.find("::") != std::string::npos)
27  mooseError("The object name may not contain '::' in the name: ", name);
28 
29  // Create the actual InputParameters object that will be reference by the objects
30  std::shared_ptr<InputParameters> ptr = std::make_shared<InputParameters>(parameters);
31 
32  auto base = ptr->get<std::string>("_moose_base");
33 
34  // The object name defined by the base class name, this method of storing is used for
35  // determining the uniqueness of the name
36  MooseObjectName unique_name(base, name, "::");
37 
38  // Check that the Parameters do not already exist. We allow duplicate unique_names for
39  // MooseVariableBase objects because we require duplication of the variable for reference and
40  // displaced problems. We must also have std::pair(reference_var, reference_params) AND
41  // std::pair(displaced_var, displaced_params) elements because the two vars will have different
42  // values for _sys. It's a good thing we are using a multi-map as our underlying storage.
43  // We also allow duplicate unique_names for Action objects because it is allowed to have
44  // multiple [Materials] input blocks each of which can add an action but all of these actions
45  // will have the same unique name.
46  if (_input_parameters[tid].find(unique_name) != _input_parameters[tid].end() &&
47  base != "MooseVariableBase" && base != "Action")
48  mooseError("A '",
49  unique_name.tag(),
50  "' object already exists with the name '",
51  unique_name.name(),
52  "'.\n");
53 
54  // Store the parameters according to the base name
55  _input_parameters[tid].insert(
56  std::pair<MooseObjectName, std::shared_ptr<InputParameters>>(unique_name, ptr));
57 
58  // Build a list of object names
59  std::vector<MooseObjectName> object_names;
60  object_names.push_back(unique_name);
61 
62  // Store the object according to the control tags
63  if (ptr->isParamValid("control_tags"))
64  {
65  const std::vector<std::string> & tags = ptr->get<std::vector<std::string>>("control_tags");
66  for (const auto & tag : tags)
67  {
68  if (!tag.empty())
69  {
70  auto short_name = MooseUtils::shortName(name);
71  _input_parameters[tid].insert(std::pair<MooseObjectName, std::shared_ptr<InputParameters>>(
72  MooseObjectName(tag, short_name), ptr));
73  object_names.emplace_back(tag, short_name);
74  }
75  }
76  }
77 
78  // Store controllable parameters using all possible names
79  for (libMesh::Parameters::iterator map_iter = ptr->begin(); map_iter != ptr->end(); ++map_iter)
80  {
81  const std::string & pname = map_iter->first;
82  libMesh::Parameters::Value * value = MooseUtils::get(map_iter->second);
83 
84  if (ptr->isControllable(pname))
85  for (const auto & object_name : object_names)
86  {
87  MooseObjectParameterName param_name(object_name, pname);
88  _controllable_items[tid].emplace_back(std::make_shared<ControllableItem>(
89  param_name, value, ptr->getControllableExecuteOnTypes(pname)));
90  }
91  }
92 
93  // Set the name and tid parameters, and unique_name
94  std::stringstream oss;
95  oss << unique_name;
96 
97  ptr->addPrivateParam<std::string>("_unique_name", oss.str());
98  ptr->addPrivateParam<std::string>("_object_name", name);
99  ptr->addPrivateParam<THREAD_ID>("_tid", tid);
100 
101  // no more copies allowed
102  ptr->allowCopy(false);
103 
104  // Return a reference to the InputParameters object
105  return *ptr;
106 }
107 
108 void
110  THREAD_ID tid,
111  const AddRemoveParamsKey)
112 {
113  auto moose_object_name_string = moose_object.parameters().get<std::string>("_unique_name");
114  MooseObjectName moose_object_name(moose_object_name_string);
115  _input_parameters[tid].erase(moose_object_name);
116 }
117 
118 const InputParameters &
120 {
122 }
123 
124 const InputParameters &
126  const std::string & name,
127  THREAD_ID tid) const
128 {
129  return getInputParameters(MooseObjectName(tag, name), tid);
130 }
131 
132 const InputParameters &
134  THREAD_ID tid) const
135 {
136  return getInputParameters(object_name, tid);
137 }
138 
140 InputParameterWarehouse::getInputParameters(const std::string & name, THREAD_ID tid) const
141 {
143 }
144 
147  const std::string & name,
148  THREAD_ID tid) const
149 {
150  return getInputParameters(MooseObjectName(tag, name), tid);
151 }
152 
155  THREAD_ID tid) const
156 {
157  // Locate the InputParameters object and error if it was not located
158  const auto iter = _input_parameters[tid].find(object_name);
159  if (iter == _input_parameters[tid].end())
160  mooseError("Unknown InputParameters object ", object_name);
161 
162  // Return a reference to the parameter
163  return *(iter->second.get());
164 }
165 
166 const std::multimap<MooseObjectName, std::shared_ptr<InputParameters>> &
168 {
169  return _input_parameters[tid];
170 }
171 
172 void
174  const MooseObjectParameterName & primary,
175  const MooseObjectParameterName & secondary,
176  bool error_on_empty /*=true*/)
177 {
178  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
179  {
180  std::vector<ControllableItem *> primaries = getControllableItems(primary, tid);
181  if (primaries.empty() && error_on_empty && tid == 0) // some objects only exist on tid 0
182  mooseError("Unable to locate primary parameter with name ", primary);
183  else if (primaries.empty())
184  {
185  if (tid == 0)
186  return;
187  else
188  // try to connect non-threaded primary control to secondary controls of all threads
189  primaries = getControllableItems(primary, 0);
190  }
191 
192  std::vector<ControllableItem *> secondaries = getControllableItems(secondary, tid);
193  if (secondaries.empty() && error_on_empty && tid == 0) // some objects only exist on tid 0
194  mooseError("Unable to locate secondary parameter with name ", secondary);
195  else if (secondaries.empty())
196  return;
197 
198  for (auto primary_ptr : primaries)
199  for (auto secondary_ptr : secondaries)
200  if (primary_ptr != secondary_ptr)
201  primary_ptr->connect(secondary_ptr);
202  }
203 }
204 
205 void
207  const MooseObjectName & secondary)
208 {
209  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
210  {
211  std::vector<ControllableItem *> secondaries =
212  getControllableItems(MooseObjectParameterName(secondary, "*"), tid);
213  for (auto secondary_ptr : secondaries)
214  {
215  MooseObjectParameterName alias_param(alias, secondary_ptr->name().parameter());
216  MooseObjectParameterName secondary_param(secondary, secondary_ptr->name().parameter());
217  addControllableParameterAlias(alias_param, secondary_param);
218  }
219  }
220 }
221 
222 void
224  const MooseObjectParameterName & secondary)
225 {
226  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
227  {
228  std::vector<ControllableItem *> secondaries = getControllableItems(secondary, tid);
229  if (secondaries.empty() && tid == 0) // some objects only exist on tid 0
230  mooseError("Unable to locate secondary parameter with name ", secondary);
231 
232  for (auto secondary_ptr : secondaries)
233  _controllable_items[tid].emplace_back(
234  std::make_unique<ControllableAlias>(alias, secondary_ptr));
235  }
236 }
237 
238 std::vector<ControllableItem *>
240  THREAD_ID tid /*=0*/) const
241 {
242  std::vector<ControllableItem *> output;
243  for (auto & ptr : _controllable_items[tid])
244  if (ptr->name() == input)
245  output.push_back(ptr.get());
246  return output;
247 }
248 
251 {
252  ControllableParameter cparam;
253  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
254  for (auto it = _controllable_items[tid].begin(); it != _controllable_items[tid].end(); ++it)
255  if ((*it)->name() == input)
256  cparam.add(it->get());
257  return cparam;
258 }
259 
260 std::string
262 {
263  std::stringstream oss;
264  oss << std::left;
265 
266  for (const auto & item : _controllable_items[0])
267  if (item->isChanged())
268  {
269  oss << item->dump(4);
270  if (reset_changed)
271  item->resetChanged();
272  }
273  return oss.str();
274 }
275 
276 std::vector<MooseObjectParameterName>
278 {
279  std::vector<MooseObjectParameterName> names;
280  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
281  for (auto it = _controllable_items[tid].begin(); it != _controllable_items[tid].end(); ++it)
282  if ((*it)->name() == input)
283  names.push_back((*it)->name());
284  return names;
285 }
std::string name(const ElemQuality q)
void addControllableParameterConnection(const MooseObjectParameterName &primary, const MooseObjectParameterName &secondary, bool error_on_empty=true)
Method for linking control parameters of different names.
const std::string & name() const
Return the name.
std::vector< ControllableItem * > getControllableItems(const MooseObjectParameterName &desired, THREAD_ID tid=0) const
Returns a ControllableItem iterator, if the name is located.
unsigned int n_threads()
Class that is used as a parameter to [add/remove]InputParameters() to restrict access.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
The ControllableParameter class is simply a set of ControllableItem objects.
T * get(const std::unique_ptr< T > &u)
The MooseUtils::get() specializations are used to support making forwards-compatible code changes fro...
Definition: MooseUtils.h:1147
InputParameters & addInputParameters(const std::string &name, const InputParameters &parameters, THREAD_ID tid, const AddRemoveParamsKey)
Method for adding a new InputParameters object.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void addControllableObjectAlias(const MooseObjectName &alias, const MooseObjectName &secondary)
Method for creating alias for all shared controllable parameters between the two objects.
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
std::string dumpChangedControls(bool reset_changed) const
InputParameterWarehouse()
Class constructor.
std::string shortName(const std::string &name)
Function for stripping name after the file / in parser block.
Definition: MooseUtils.C:598
std::vector< MooseObjectParameterName > getControllableParameterNames(const MooseObjectParameterName &input) const
Return a vector of parameters names matching the supplied name.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
void removeInputParameters(const MooseObject &moose_object, THREAD_ID tid, const AddRemoveParamsKey)
Allows for the deletion and cleanup of an object while the simulation is running. ...
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:33
ControllableParameter getControllableParameter(const MooseObjectParameterName &input) const
Returns a ControllableParameter object that contains all matches to ControllableItem objects for the ...
void addControllableParameterAlias(const MooseObjectParameterName &alias, const MooseObjectParameterName &secondary)
Method for creating alias to an existing controllable parameters.
void add(ControllableItem *item)
Adds the supplied item with the other items within this object.
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.
const InputParameters & parameters() const
Get the parameters of the object.
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...
map_type::iterator iterator
unsigned int THREAD_ID
Definition: MooseTypes.h:198