https://mooseframework.inl.gov
InputParameterWarehouse.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
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  const auto & base = ptr->getBase();
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>(MooseBase::unique_name_param, oss.str());
98  ptr->addPrivateParam<std::string>(MooseBase::name_param, 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 =
114  moose_object.parameters().get<std::string>(MooseBase::unique_name_param);
115  MooseObjectName moose_object_name(moose_object_name_string);
116  _input_parameters[tid].erase(moose_object_name);
117 }
118 
119 const InputParameters &
121 {
123 }
124 
125 const InputParameters &
127  const std::string & name,
128  THREAD_ID tid) const
129 {
130  return getInputParameters(MooseObjectName(tag, name), tid);
131 }
132 
133 const InputParameters &
135  THREAD_ID tid) const
136 {
137  return getInputParameters(object_name, tid);
138 }
139 
141 InputParameterWarehouse::getInputParameters(const std::string & name, THREAD_ID tid) const
142 {
144 }
145 
148  const std::string & name,
149  THREAD_ID tid) const
150 {
151  return getInputParameters(MooseObjectName(tag, name), tid);
152 }
153 
156  THREAD_ID tid) const
157 {
158  // Locate the InputParameters object and error if it was not located
159  const auto iter = _input_parameters[tid].find(object_name);
160  if (iter == _input_parameters[tid].end())
161  mooseError("Unknown InputParameters object ", object_name);
162 
163  // Return a reference to the parameter
164  return *(iter->second.get());
165 }
166 
167 const std::multimap<MooseObjectName, std::shared_ptr<InputParameters>> &
169 {
170  return _input_parameters[tid];
171 }
172 
173 void
175  const MooseObjectParameterName & primary,
176  const MooseObjectParameterName & secondary,
177  bool error_on_empty /*=true*/)
178 {
179  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
180  {
181  std::vector<ControllableItem *> primaries = getControllableItems(primary, tid);
182  if (primaries.empty() && error_on_empty && tid == 0) // some objects only exist on tid 0
183  mooseError("Unable to locate primary parameter with name ", primary);
184  else if (primaries.empty())
185  {
186  if (tid == 0)
187  return;
188  else
189  // try to connect non-threaded primary control to secondary controls of all threads
190  primaries = getControllableItems(primary, 0);
191  }
192 
193  std::vector<ControllableItem *> secondaries = getControllableItems(secondary, tid);
194  if (secondaries.empty() && error_on_empty && tid == 0) // some objects only exist on tid 0
195  mooseError("Unable to locate secondary parameter with name ", secondary);
196  else if (secondaries.empty())
197  return;
198 
199  for (auto primary_ptr : primaries)
200  for (auto secondary_ptr : secondaries)
201  if (primary_ptr != secondary_ptr)
202  primary_ptr->connect(secondary_ptr);
203  }
204 }
205 
206 void
208  const MooseObjectName & secondary)
209 {
210  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
211  {
212  std::vector<ControllableItem *> secondaries =
213  getControllableItems(MooseObjectParameterName(secondary, "*"), tid);
214  for (auto secondary_ptr : secondaries)
215  {
216  MooseObjectParameterName alias_param(alias, secondary_ptr->name().parameter());
217  MooseObjectParameterName secondary_param(secondary, secondary_ptr->name().parameter());
218  addControllableParameterAlias(alias_param, secondary_param);
219  }
220  }
221 }
222 
223 void
225  const MooseObjectParameterName & secondary)
226 {
227  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
228  {
229  std::vector<ControllableItem *> secondaries = getControllableItems(secondary, tid);
230  if (secondaries.empty() && tid == 0) // some objects only exist on tid 0
231  mooseError("Unable to locate secondary parameter with name ", secondary);
232 
233  for (auto secondary_ptr : secondaries)
234  _controllable_items[tid].emplace_back(
235  std::make_unique<ControllableAlias>(alias, secondary_ptr));
236  }
237 }
238 
239 std::vector<ControllableItem *>
241  THREAD_ID tid /*=0*/) const
242 {
243  std::vector<ControllableItem *> output;
244  for (auto & ptr : _controllable_items[tid])
245  if (ptr->name() == input)
246  output.push_back(ptr.get());
247  return output;
248 }
249 
252 {
253  ControllableParameter cparam;
254  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
255  for (auto it = _controllable_items[tid].begin(); it != _controllable_items[tid].end(); ++it)
256  if ((*it)->name() == input)
257  cparam.add(it->get());
258  return cparam;
259 }
260 
261 std::string
263 {
264  std::stringstream oss;
265  oss << std::left;
266 
267  for (const auto & item : _controllable_items[0])
268  if (item->isChanged())
269  {
270  oss << item->dump(4);
271  if (reset_changed)
272  item->resetChanged();
273  }
274  return oss.str();
275 }
276 
277 std::vector<MooseObjectParameterName>
279 {
280  std::vector<MooseObjectParameterName> names;
281  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
282  for (auto it = _controllable_items[tid].begin(); it != _controllable_items[tid].end(); ++it)
283  if ((*it)->name() == input)
284  names.push_back((*it)->name());
285  return names;
286 }
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.
static const std::string name_param
The name of the parameter that contains the object name.
Definition: MooseBase.h:55
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:333
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.
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:127
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:1155
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.
static const std::string unique_name_param
The name of the parameter that contains the unique object name.
Definition: MooseBase.h:57
std::string shortName(const std::string &name)
Function for stripping name after the file / in parser block.
Definition: MooseUtils.C:608
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:27
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.
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:209
const std::string & tag() const
Return the tag.