https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Component.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#include "Component.h"
11#include "THMMesh.h"
13#include "ConstantFunction.h"
14#include "Numerics.h"
15#include "RelationshipManager.h"
16
19{
22 params.addPrivateParam<THMProblem *>("_thm_problem");
23 params.addPrivateParam<Component *>("_parent", nullptr);
24 params.addPrivateParam<std::string>("built_by_action", "add_component");
25
26 params.registerBase("Component");
27
28 return params;
29}
30
31/*
32 * Component implementation
33 */
34
36 : THMObject(parameters),
37 LoggingInterface(getCheckedPointerParam<THMProblem *>("_thm_problem")->log()),
40
41 _parent(getParam<Component *>("_parent")),
42 _sim(*getCheckedPointerParam<THMProblem *>("_thm_problem")),
43 _factory(_app.getFactory()),
44 _zero(_sim._real_zero[0]),
45 _mesh(static_cast<THMMesh &>(_sim.mesh())),
46 _component_setup_status(CREATED)
47{
48}
49
50const std::string &
52{
53 if (_parent)
54 return _parent->cname();
55 else
56 return name();
57}
58
59THMMesh &
61{
64 "A non-const reference to the THM mesh cannot be obtained after mesh setup is complete.");
65 else
66 return _mesh;
67}
68
69void
75
76void
82
83void
89
90void
96
97void
99 const std::string & obj_name,
100 const std::string & param) const
101{
102 connectObject(obj_params, obj_name, param, param);
103}
104
105void
107 const std::string & obj_name,
108 const std::string & comp_param,
109 const std::string & obj_param) const
110{
111 MooseObjectParameterName alias("component", this->name(), comp_param, "::");
112 MooseObjectParameterName obj_controlled_param(obj_params.getBase(), obj_name, obj_param);
114}
115
116void
118{
119 if (_component_setup_status < status)
121 ": The component setup status (",
123 ") is less than the required status (",
124 stringify(status),
125 ")");
126}
127
128void
129Component::addDependency(const std::string & dependency)
130{
131 _dependencies.push_back(dependency);
132}
133
136{
137 return _sim;
138}
139
140void
141Component::checkComponentExistsByName(const std::string & comp_name) const
142{
143 if (!_sim.hasComponent(comp_name))
144 logError("The component '", comp_name, "' does not exist");
145}
146
147void
149{
150 const auto & buildable_types = moose_object_pars.getBuildableRelationshipManagerTypes();
151
152 for (const auto & buildable_type : buildable_types)
153 {
154 auto & rm_name = std::get<0>(buildable_type);
155 auto & rm_type = std::get<1>(buildable_type);
156 auto rm_input_parameter_func = std::get<2>(buildable_type);
157
158 addRelationshipManager(moose_object_pars, rm_name, rm_type, rm_input_parameter_func);
159 }
160}
161
162void
164 const InputParameters & moose_object_pars,
165 std::string rm_name,
169{
170 // These need unique names
171 static unsigned int unique_object_id = 0;
172
173 auto new_name = moose_object_pars.getBase() + '_' + name() + '_' + rm_name + "_" +
174 Moose::stringify(rm_type) + " " + std::to_string(unique_object_id);
175
176 auto rm_params = _factory.getValidParams(rm_name);
177 rm_params.set<Moose::RelationshipManagerType>("rm_type") = rm_type;
178
179 rm_params.set<std::string>("for_whom") = name();
180
181 // If there is a callback for setting the RM parameters let's use it
182 if (rm_input_parameter_func)
183 rm_input_parameter_func(moose_object_pars, rm_params);
184
185 rm_params.set<MooseMesh *>("mesh") = &_mesh;
186
187 if (!rm_params.areAllRequiredParamsValid())
188 mooseError("Missing required parameters for RelationshipManager " + rm_name + " for object " +
189 name());
190
191 auto rm_obj = _factory.create<RelationshipManager>(rm_name, new_name, rm_params);
192
193 const bool added = _app.addRelationshipManager(rm_obj);
194
195 // Delete the resources created on behalf of the RM if it ends up not being added to the App.
196 if (!added)
198 else // we added it
199 unique_object_id++;
200}
201
202Node *
203Component::addNode(const Point & pt)
204{
205 auto node = mesh().addNode(pt);
206 _node_ids.push_back(node->id());
207 return node;
208}
209
210Elem *
212{
213 auto elem = mesh().addNodeElement(node);
214 _elem_ids.push_back(elem->id());
215 return elem;
216}
217
218void
220 const std::string & subdomain_name,
221 const Moose::CoordinateSystemType & coord_system)
222{
223 _subdomain_ids.push_back(subdomain_id);
224 _subdomain_names.push_back(subdomain_name);
225 _coord_sys.push_back(coord_system);
226 if (_parent)
227 {
228 _parent->_subdomain_ids.push_back(subdomain_id);
229 _parent->_subdomain_names.push_back(subdomain_name);
230 _parent->_coord_sys.push_back(coord_system);
231 }
232 mesh().setSubdomainName(subdomain_id, subdomain_name);
233}
234
235void
236Component::addNonlinearStepFunctorMaterial(const std::string & functor_name,
237 const std::string & property,
238 bool functor_is_ad)
239{
240 const std::string class_name =
241 functor_is_ad ? "ADFunctorChangeFunctorMaterial" : "FunctorChangeFunctorMaterial";
242 InputParameters params = _factory.getValidParams(class_name);
243 params.set<std::vector<SubdomainName>>("block") = getSubdomainNames();
244 params.set<MooseFunctorName>("functor") = functor_name;
245 params.set<MooseEnum>("change_over") = "nonlinear";
246 params.set<std::string>("prop_name") = property;
247 params.set<bool>("take_absolute_value") = true;
248 getTHMProblem().addFunctorMaterial(class_name, genName(name(), property + "_fmat"), params);
249}
250
251void
252Component::addMaximumFunctorPostprocessor(const std::string & functor_name,
253 const std::string & pp_name,
254 const Real normalization,
255 const std::vector<SubdomainName> & subdomains)
256{
257 const std::string class_name = "ElementExtremeFunctorValue";
258 InputParameters params = _factory.getValidParams(class_name);
259 params.set<std::vector<SubdomainName>>("block") = subdomains;
260 params.set<MooseEnum>("value_type") = "max";
261 params.set<MooseFunctorName>("functor") = functor_name;
262 params.set<Real>("scale") = 1.0 / normalization;
263 params.set<ExecFlagEnum>("execute_on") = EXEC_NONLINEAR_CONVERGENCE;
264 params.set<std::vector<OutputName>>("outputs") = {"none"};
265 getTHMProblem().addPostprocessor(class_name, pp_name, params);
266}
267
268void
269Component::addMultiPostprocessorConvergence(const std::vector<PostprocessorName> & postprocessors,
270 const std::vector<std::string> & descriptions,
271 const std::vector<Real> & tolerances)
272{
273 const std::string class_name = "MultiPostprocessorConvergence";
274 InputParameters params = _factory.getValidParams(class_name);
275 params.set<std::vector<PostprocessorName>>("postprocessors") = postprocessors;
276 params.set<std::vector<std::string>>("descriptions") = descriptions;
277 params.set<std::vector<Real>>("tolerances") = tolerances;
278 params.set<unsigned int>("min_iterations") = 1;
279 params.set<unsigned int>("max_iterations") = std::numeric_limits<unsigned int>::max();
281}
282
283void
284Component::checkMutuallyExclusiveParameters(const std::vector<std::string> & params,
285 bool need_one_specified) const
286{
287 unsigned int n_provided_params = 0;
288 for (const auto & param : params)
289 if (isParamValid(param))
290 n_provided_params++;
291
292 if (n_provided_params != 1)
293 {
294 std::string params_list_string = "{'" + params[0] + "'";
295 for (unsigned int i = 1; i < params.size(); ++i)
296 params_list_string += ", '" + params[i] + "'";
297 params_list_string += "}";
298
299 if (n_provided_params == 0 && need_one_specified)
300 logError("One of the parameters ", params_list_string, " must be provided");
301
302 if (n_provided_params != 0)
303 logError("Only one of the parameters ", params_list_string, " can be provided");
304 }
305}
306
308std::string
310{
311 switch (status)
312 {
313 case CREATED:
314 return "component created";
315 case MESH_PREPARED:
316 return "component mesh set up";
318 return "primary initialization completed";
320 return "secondary initialization completed";
321 case CHECKED:
322 return "component fully set up and checked";
323 default:
324 mooseError("Should not reach here");
325 }
326}
327
328const std::vector<dof_id_type> &
330{
332
333 return _node_ids;
334}
335
336const std::vector<dof_id_type> &
338{
340
341 return _elem_ids;
342}
343
344const std::vector<SubdomainName> &
351
352const std::vector<Moose::CoordinateSystemType> &
359
362{
363 mooseError("getNonlinearConvergence() not implemented.");
364}
const ExecFlagType EXEC_NONLINEAR_CONVERGENCE
static InputParameters validParams()
Base class for THM components.
Definition Component.h:32
std::string stringify(EComponentSetupStatus status) const
Return a string for the setup status.
Definition Component.C:309
virtual void init()
Initializes the component.
Definition Component.h:405
virtual const std::vector< SubdomainName > & getSubdomainNames() const
Gets the subdomain names for this component.
Definition Component.C:345
void logError(Args &&... args) const
Logs an error.
Definition Component.h:226
const std::vector< dof_id_type > & getElementIDs() const
Gets the element IDs corresponding to this component.
Definition Component.C:337
Elem * addNodeElement(dof_id_type node)
Definition Component.C:211
void addDependency(const std::string &dependency)
Adds a component name to the list of dependencies.
Definition Component.C:129
void addMaximumFunctorPostprocessor(const std::string &functor_name, const std::string &pp_name, const Real normalization, const std::vector< SubdomainName > &subdomains)
Adds a Postprocessor to compute the maximum of a functor over some domain.
Definition Component.C:252
virtual const std::vector< Moose::CoordinateSystemType > & getCoordSysTypes() const
Gets the coordinate system types for this component.
Definition Component.C:353
void executeInit()
Wrapper function for init() that marks the function as being called.
Definition Component.C:70
THMProblem & getTHMProblem() const
Gets the THM problem.
Definition Component.C:135
Factory & _factory
The Factory associated with the MooseApp.
Definition Component.h:497
std::string nonlinearConvergenceName() const
Nonlinear Convergence name.
Definition Component.h:486
void addNonlinearStepFunctorMaterial(const std::string &functor_name, const std::string &property, bool functor_is_ad)
Adds a functor material to compute the absolute value of the change (step) of some functor between no...
Definition Component.C:236
virtual void check() const
Check the component integrity.
Definition Component.h:416
std::vector< dof_id_type > _elem_ids
Element IDs of this component.
Definition Component.h:508
Node * addNode(const Point &pt)
Definition Component.C:203
virtual void setSubdomainInfo(SubdomainID subdomain_id, const std::string &subdomain_name, const Moose::CoordinateSystemType &coord_system=Moose::COORD_XYZ)
Sets the next subdomain ID, name, and coordinate system.
Definition Component.C:219
void addRelationshipManagersFromParameters(const InputParameters &moose_object_pars)
Method to add a relationship manager for the objects being added to the system.
Definition Component.C:148
void checkComponentExistsByName(const std::string &comp_name) const
Checks that a component exists.
Definition Component.C:141
virtual Convergence * getNonlinearConvergence() const
Gets the Component's nonlinear Convergence object if it has one.
Definition Component.C:361
std::vector< SubdomainID > _subdomain_ids
List of subdomain IDs this components owns.
Definition Component.h:511
EComponentSetupStatus
Component setup status type.
Definition Component.h:38
@ CHECKED
mesh set up, called both inits, checked
Definition Component.h:43
@ MESH_PREPARED
mesh set up
Definition Component.h:40
@ INITIALIZED_PRIMARY
mesh set up, called primary init
Definition Component.h:41
@ INITIALIZED_SECONDARY
mesh set up, called both inits
Definition Component.h:42
@ CREATED
only created
Definition Component.h:39
std::vector< Moose::CoordinateSystemType > _coord_sys
List of coordinate system for each subdomain.
Definition Component.h:515
THMProblem & _sim
THM problem this component is part of TODO: make _sim private (applications need to switch to getters...
Definition Component.h:494
virtual void setupMesh()
Performs mesh setup such as creating mesh or naming mesh sets.
Definition Component.h:421
void executeInitSecondary()
Wrapper function for initSecondary() that marks the function as being called.
Definition Component.C:77
void addMultiPostprocessorConvergence(const std::vector< PostprocessorName > &postprocessors, const std::vector< std::string > &descriptions, const std::vector< Real > &tolerances)
Adds a MultiPostprocessorConvergence for nonlinear convergence for the component.
Definition Component.C:269
EComponentSetupStatus _component_setup_status
Component setup status.
Definition Component.h:539
std::vector< dof_id_type > _node_ids
Node IDs of this component.
Definition Component.h:506
std::vector< SubdomainName > _subdomain_names
List of subdomain names this components owns.
Definition Component.h:513
void executeCheck() const
Wrapper function for check() that marks the function as being called.
Definition Component.C:84
virtual void initSecondary()
Perform secondary initialization, which relies on init() being called for all components.
Definition Component.h:411
const std::string & cname() const
Get the component name.
Definition Component.C:51
void connectObject(const InputParameters &obj_params, const std::string &obj_name, const std::string &param) const
Connects a controllable parameter of the component to a controllable parameter of a constituent objec...
Definition Component.C:98
void checkSetupStatus(const EComponentSetupStatus &status) const
Throws an error if the supplied setup status of this component has not been reached.
Definition Component.C:117
THMMesh & mesh()
Non-const reference to THM mesh, which can only be called before the end of mesh setup.
Definition Component.C:60
std::vector< std::string > _dependencies
List of names of components that this component depends upon.
Definition Component.h:542
void checkMutuallyExclusiveParameters(const std::vector< std::string > &params, bool need_one_specified=true) const
Checks that exactly one parameter out of a list is provided.
Definition Component.C:284
const std::vector< dof_id_type > & getNodeIDs() const
Gets the node IDs corresponding to this component.
Definition Component.C:329
THMMesh & _mesh
The THM mesh TODO: make _mesh private (applications need to switch to getters to avoid breaking)
Definition Component.h:503
void executeSetupMesh()
Wrapper function for setupMesh() that marks the function as being called.
Definition Component.C:91
Component * _parent
Pointer to a parent component (used in composed components)
Definition Component.h:489
Component(const InputParameters &parameters)
Definition Component.C:35
void addRelationshipManager(const InputParameters &moose_object_pars, std::string rm_name, Moose::RelationshipManagerType rm_type, Moose::RelationshipManagerInputParameterCallback rm_input_parameter_func, Moose::RMSystemType sys_type=Moose::RMSystemType::NONE)
Method for adding a single relationship manager.
Definition Component.C:163
static InputParameters validParams()
Definition Component.C:18
virtual void addConvergence(const std::string &type, const std::string &name, InputParameters &parameters)
virtual void addPostprocessor(const std::string &pp_name, const std::string &name, InputParameters &parameters)
virtual void addFunctorMaterial(const std::string &functor_material_name, const std::string &name, InputParameters &parameters)
std::shared_ptr< MooseObject > create(const std::string &obj_name, const std::string &name, const InputParameters &parameters, THREAD_ID tid=0, bool print_deprecated=true)
InputParameters getValidParams(const std::string &name) const
void releaseSharedObjects(const MooseObject &moose_object, THREAD_ID tid=0)
void addControllableParameterAlias(const MooseObjectParameterName &alias, const MooseObjectParameterName &secondary)
const std::vector< std::tuple< std::string, Moose::RelationshipManagerType, Moose::RelationshipManagerInputParameterCallback > > & getBuildableRelationshipManagerTypes() const
void addPrivateParam(const std::string &name, const T &value)
void registerBase(const std::string &value)
T & set(const std::string &name, bool quiet_mode=false)
const std::string & getBase() const
Interface class for logging errors and warnings.
bool addRelationshipManager(std::shared_ptr< RelationshipManager > relationship_manager)
InputParameterWarehouse & getInputParameterWarehouse()
const std::string & name() const
void mooseError(Args &&... args) const
bool isParamValid(const std::string &name) const
void setSubdomainName(SubdomainID subdomain_id, const SubdomainName &name)
MooseApp & _app
Interface for handling names.
std::string genName(const std::string &prefix, unsigned int id, const std::string &suffix="") const
Build a name from a prefix, number and possible suffix.
bool hasComponent(const std::string &name) const
Find out if simulation has a component with the given name.
Mesh for THM.
Definition THMMesh.h:19
Elem * addNodeElement(dof_id_type node)
Definition THMMesh.C:116
Node * addNode(const Point &pt)
Add a new node into the mesh.
Definition THMMesh.C:95
static InputParameters validParams()
Definition THMObject.C:13
Specialization of FEProblem to run with component subsystem.
Definition THMProblem.h:19
MeshBase & mesh
std::function< void(const InputParameters &, InputParameters &)> RelationshipManagerInputParameterCallback
std::string stringify(const T &t)
RelationshipManagerType
CoordinateSystemType