https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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,
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
108void
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
119const InputParameters &
121{
122 return getInputParameters(MooseObjectName(name), tid);
123}
124
125const InputParameters &
127 const std::string & name,
128 THREAD_ID tid) const
129{
130 return getInputParameters(MooseObjectName(tag, name), tid);
131}
132
133const InputParameters &
135 THREAD_ID tid) const
136{
137 return getInputParameters(object_name, tid);
138}
139
141InputParameterWarehouse::getInputParameters(const std::string & name, THREAD_ID tid) const
142{
143 return getInputParameters(MooseObjectName(name), tid);
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
167const std::multimap<MooseObjectName, std::shared_ptr<InputParameters>> &
172
173void
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
206void
208 const MooseObjectName & secondary)
209{
210 for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
211 {
212 std::vector<ControllableItem *> secondaries =
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
223void
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
239std::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{
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
261std::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
277std::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}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
unsigned int THREAD_ID
Definition MooseTypes.h:237
The ControllableParameter class is simply a set of ControllableItem objects.
void add(ControllableItem *item)
Adds the supplied item with the other items within this object.
std::string dump() const
Return a string that lists the parameters stored by this object.
Class that is used as a parameter to [add/remove]InputParameters() to restrict access.
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...
std::vector< MooseObjectParameterName > getControllableParameterNames(const MooseObjectParameterName &input) const
Return a vector of parameters names matching the supplied name.
ControllableParameter getControllableParameter(const MooseObjectParameterName &input) const
Returns a ControllableParameter object that contains all matches to ControllableItem objects for the ...
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.
std::string dumpChangedControls(bool reset_changed) const
std::vector< ControllableItem * > getControllableItems(const MooseObjectParameterName &desired, THREAD_ID tid=0) const
Returns a ControllableItem iterator, if the name is located.
const InputParameters & getInputParametersObject(const std::string &name, THREAD_ID tid=0) const
Return a const reference to the InputParameters for the named object.
const std::multimap< MooseObjectName, std::shared_ptr< InputParameters > > & getInputParameters(THREAD_ID tid=0) const
Return const reference to the map containing the InputParameter objects.
InputParameterWarehouse()
Class constructor.
void addControllableObjectAlias(const MooseObjectName &alias, const MooseObjectName &secondary)
Method for creating alias for all shared controllable parameters between the two objects.
std::vector< std::multimap< MooseObjectName, std::shared_ptr< InputParameters > > > _input_parameters
Storage for the InputParameters objects TODO: Remove multimap.
void addControllableParameterConnection(const MooseObjectParameterName &primary, const MooseObjectParameterName &secondary, bool error_on_empty=true)
Method for linking control parameters of different names.
void addControllableParameterAlias(const MooseObjectParameterName &alias, const MooseObjectParameterName &secondary)
Method for creating alias to an existing controllable parameters.
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.
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.
const InputParameters & parameters() const
Get the parameters of the object.
Definition MooseBase.h:131
static const std::string unique_name_param
The name of the parameter that contains the unique object name.
Definition MooseBase.h:57
static const std::string name_param
The name of the parameter that contains the object name.
Definition MooseBase.h:55
A class for storing the names of MooseObject by tag and object name.
const std::string & name() const
Return the name.
const std::string & tag() const
Return the tag.
A class for storing an input parameter name.
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
std::string shortName(const std::string &name)
Definition MooseUtils.C:623
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
unsigned int n_threads()