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 /* =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  auto base = ptr->get<std::string>("_moose_base");
32 
33  // The object name defined by the base class name, this method of storing is used for
34  // determining the uniqueness of the name
35  MooseObjectName unique_name(base, name, "::");
36 
37  // Check that the Parameters do not already exist. We allow duplicate unique_names for
38  // MooseVariableBase objects because we require duplication of the variable for reference and
39  // displaced problems. We must also have std::pair(reference_var, reference_params) AND
40  // std::pair(displaced_var, displaced_params) elements because the two vars will have different
41  // values for _sys. It's a good thing we are using a multi-map as our underlying storage.
42  // We also allow duplicate unique_names for Action objects because it is allowed to have
43  // multiple [Materials] input blocks each of which can add an action but all of these actions
44  // will have the same unique name.
45  if (_input_parameters[tid].find(unique_name) != _input_parameters[tid].end() &&
46  base != "MooseVariableBase" && base != "Action")
47  mooseError("A '",
48  unique_name.tag(),
49  "' object already exists with the name '",
50  unique_name.name(),
51  "'.\n");
52 
53  // Store the parameters according to the base name
54  _input_parameters[tid].insert(
55  std::pair<MooseObjectName, std::shared_ptr<InputParameters>>(unique_name, ptr));
56 
57  // Build a list of object names
58  std::vector<MooseObjectName> object_names;
59  object_names.push_back(unique_name);
60 
61  // Store the object according to the control tags
62  if (ptr->isParamValid("control_tags"))
63  {
64  const std::vector<std::string> & tags = ptr->get<std::vector<std::string>>("control_tags");
65  for (const auto & tag : tags)
66  {
67  if (!tag.empty())
68  {
69  auto short_name = MooseUtils::shortName(name);
70  _input_parameters[tid].insert(std::pair<MooseObjectName, std::shared_ptr<InputParameters>>(
71  MooseObjectName(tag, short_name), ptr));
72  object_names.emplace_back(tag, short_name);
73  }
74  }
75  }
76 
77  // Store controllable parameters using all possible names
78  for (libMesh::Parameters::iterator map_iter = ptr->begin(); map_iter != ptr->end(); ++map_iter)
79  {
80  const std::string & pname = map_iter->first;
81  libMesh::Parameters::Value * value = MooseUtils::get(map_iter->second);
82 
83  if (ptr->isControllable(pname))
84  for (const auto & object_name : object_names)
85  {
86  MooseObjectParameterName param_name(object_name, pname);
87  _controllable_items[tid].emplace_back(std::make_shared<ControllableItem>(
88  param_name, value, ptr->getControllableExecuteOnTypes(pname)));
89  }
90  }
91 
92  // Set the name and tid parameters, and unique_name
93  std::stringstream oss;
94  oss << unique_name;
95 
96  ptr->addPrivateParam<std::string>("_unique_name", oss.str());
97  ptr->addPrivateParam<std::string>("_object_name", name);
98  ptr->addPrivateParam<THREAD_ID>("_tid", tid);
99 
100  // no more copies allowed
101  ptr->allowCopy(false);
102 
103  // Return a reference to the InputParameters object
104  return *ptr;
105 }
106 
107 void
109 {
110  auto moose_object_name_string = moose_object.parameters().get<std::string>("_unique_name");
111  MooseObjectName moose_object_name(moose_object_name_string);
112  _input_parameters[tid].erase(moose_object_name);
113 }
114 
115 const InputParameters &
117 {
119 }
120 
121 const InputParameters &
123  const std::string & name,
124  THREAD_ID tid) const
125 {
126  return getInputParameters(MooseObjectName(tag, name), tid);
127 }
128 
129 const InputParameters &
131  THREAD_ID tid) const
132 {
133  return getInputParameters(object_name, tid);
134 }
135 
137 InputParameterWarehouse::getInputParameters(const std::string & name, THREAD_ID tid) const
138 {
140 }
141 
144  const std::string & name,
145  THREAD_ID tid) const
146 {
147  return getInputParameters(MooseObjectName(tag, name), tid);
148 }
149 
152  THREAD_ID tid) const
153 {
154  // Locate the InputParameters object and error if it was not located
155  const auto iter = _input_parameters[tid].find(object_name);
156  if (iter == _input_parameters[tid].end())
157  mooseError("Unknown InputParameters object ", object_name);
158 
159  // Return a reference to the parameter
160  return *(iter->second.get());
161 }
162 
163 const std::multimap<MooseObjectName, std::shared_ptr<InputParameters>> &
165 {
166  return _input_parameters[tid];
167 }
168 
169 void
171  const MooseObjectParameterName & primary,
172  const MooseObjectParameterName & secondary,
173  bool error_on_empty /*=true*/)
174 {
175  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
176  {
177  std::vector<ControllableItem *> primaries = getControllableItems(primary, tid);
178  if (primaries.empty() && error_on_empty && tid == 0) // some objects only exist on tid 0
179  mooseError("Unable to locate primary parameter with name ", primary);
180  else if (primaries.empty())
181  {
182  if (tid == 0)
183  return;
184  else
185  // try to connect non-threaded primary control to secondary controls of all threads
186  primaries = getControllableItems(primary, 0);
187  }
188 
189  std::vector<ControllableItem *> secondaries = getControllableItems(secondary, tid);
190  if (secondaries.empty() && error_on_empty && tid == 0) // some objects only exist on tid 0
191  mooseError("Unable to locate secondary parameter with name ", secondary);
192  else if (secondaries.empty())
193  return;
194 
195  for (auto primary_ptr : primaries)
196  for (auto secondary_ptr : secondaries)
197  if (primary_ptr != secondary_ptr)
198  primary_ptr->connect(secondary_ptr);
199  }
200 }
201 
202 void
204  const MooseObjectName & secondary)
205 {
206  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
207  {
208  std::vector<ControllableItem *> secondaries =
209  getControllableItems(MooseObjectParameterName(secondary, "*"), tid);
210  for (auto secondary_ptr : secondaries)
211  {
212  MooseObjectParameterName alias_param(alias, secondary_ptr->name().parameter());
213  MooseObjectParameterName secondary_param(secondary, secondary_ptr->name().parameter());
214  addControllableParameterAlias(alias_param, secondary_param);
215  }
216  }
217 }
218 
219 void
221  const MooseObjectParameterName & secondary)
222 {
223  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
224  {
225  std::vector<ControllableItem *> secondaries = getControllableItems(secondary, tid);
226  if (secondaries.empty() && tid == 0) // some objects only exist on tid 0
227  mooseError("Unable to locate secondary parameter with name ", secondary);
228 
229  for (auto secondary_ptr : secondaries)
230  _controllable_items[tid].emplace_back(
231  std::make_unique<ControllableAlias>(alias, secondary_ptr));
232  }
233 }
234 
235 std::vector<ControllableItem *>
237  THREAD_ID tid /*=0*/) const
238 {
239  std::vector<ControllableItem *> output;
240  for (auto & ptr : _controllable_items[tid])
241  if (ptr->name() == input)
242  output.push_back(ptr.get());
243  return output;
244 }
245 
248 {
249  ControllableParameter cparam;
250  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
251  for (auto it = _controllable_items[tid].begin(); it != _controllable_items[tid].end(); ++it)
252  if ((*it)->name() == input)
253  cparam.add(it->get());
254  return cparam;
255 }
256 
257 std::string
259 {
260  std::stringstream oss;
261  oss << std::left;
262 
263  for (const auto & item : _controllable_items[0])
264  if (item->isChanged())
265  {
266  oss << item->dump(4);
267  if (reset_changed)
268  item->resetChanged();
269  }
270  return oss.str();
271 }
272 
273 std::vector<MooseObjectParameterName>
275 {
276  std::vector<MooseObjectParameterName> names;
277  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
278  for (auto it = _controllable_items[tid].begin(); it != _controllable_items[tid].end(); ++it)
279  if ((*it)->name() == input)
280  names.push_back((*it)->name());
281  return names;
282 }
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()
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
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:595
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)
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.
void removeInputParameters(const MooseObject &moose_object, THREAD_ID tid=0)
Allows for the deletion and cleanup of an object while the simulation is running. ...
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.
InputParameters & addInputParameters(const std::string &name, const InputParameters &parameters, THREAD_ID tid=0)
Method for adding a new InputParameters object.
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