https://mooseframework.inl.gov
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"
12 #include "ThermalHydraulicsApp.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()),
39  ADFunctorInterface(this),
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 
50 const std::string &
52 {
53  if (_parent)
54  return _parent->cname();
55  else
56  return name();
57 }
58 
59 THMMesh &
61 {
63  mooseError(
64  "A non-const reference to the THM mesh cannot be obtained after mesh setup is complete.");
65  else
66  return _mesh;
67 }
68 
69 void
71 {
72  init();
74 }
75 
76 void
78 {
79  initSecondary();
81 }
82 
83 void
85 {
86  check();
88 }
89 
90 void
92 {
93  setupMesh();
95 }
96 
97 void
99  const std::string & obj_name,
100  const std::string & param) const
101 {
102  connectObject(obj_params, obj_name, param, param);
103 }
104 
105 void
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);
113  _app.getInputParameterWarehouse().addControllableParameterAlias(alias, obj_controlled_param);
114 }
115 
116 void
118 {
120  mooseError(name(),
121  ": The component setup status (",
123  ") is less than the required status (",
124  stringify(status),
125  ")");
126 }
127 
128 void
129 Component::addDependency(const std::string & dependency)
130 {
131  _dependencies.push_back(dependency);
132 }
133 
134 THMProblem &
136 {
137  return _sim;
138 }
139 
140 void
141 Component::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 
147 void
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 
162 void
164  const InputParameters & moose_object_pars,
165  std::string rm_name,
167  Moose::RelationshipManagerInputParameterCallback rm_input_parameter_func,
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 
202 Node *
203 Component::addNode(const Point & pt)
204 {
205  auto node = mesh().addNode(pt);
206  _node_ids.push_back(node->id());
207  return node;
208 }
209 
210 Elem *
212 {
213  auto elem = mesh().addNodeElement(node);
214  _elem_ids.push_back(elem->id());
215  return elem;
216 }
217 
218 void
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 
235 void
236 Component::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 
251 void
252 Component::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 
268 void
269 Component::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();
280  getTHMProblem().addConvergence(class_name, nonlinearConvergenceName(), params);
281 }
282 
283 void
284 Component::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 
308 std::string
310 {
311  switch (status)
312  {
313  case CREATED:
314  return "component created";
315  case MESH_PREPARED:
316  return "component mesh set up";
317  case INITIALIZED_PRIMARY:
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 
328 const std::vector<dof_id_type> &
330 {
332 
333  return _node_ids;
334 }
335 
336 const std::vector<dof_id_type> &
338 {
340 
341  return _elem_ids;
342 }
343 
344 const std::vector<SubdomainName> &
346 {
348 
349  return _subdomain_names;
350 }
351 
352 const std::vector<Moose::CoordinateSystemType> &
354 {
356 
357  return _coord_sys;
358 }
359 
360 Convergence *
362 {
363  mooseError("getNonlinearConvergence() not implemented.");
364 }
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 executeCheck() const
Wrapper function for check() that marks the function as being called.
Definition: Component.C:84
only created
Definition: Component.h:39
Specialization of FEProblem to run with component subsystem.
Definition: THMProblem.h:18
Interface for handling names.
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
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.
const ExecFlagType EXEC_NONLINEAR_CONVERGENCE
RelationshipManagerType
THMProblem & _sim
THM problem this component is part of TODO: make _sim private (applications need to switch to getters...
Definition: Component.h:494
EComponentSetupStatus
Component setup status type.
Definition: Component.h:37
void addPrivateParam(const std::string &name, const T &value)
const std::vector< std::tuple< std::string, Moose::RelationshipManagerType, Moose::RelationshipManagerInputParameterCallback > > & getBuildableRelationshipManagerTypes() const
virtual void setupMesh()
Performs mesh setup such as creating mesh or naming mesh sets.
Definition: Component.h:421
THMProblem & getTHMProblem() const
Gets the THM problem.
Definition: Component.C:135
InputParameterWarehouse & getInputParameterWarehouse()
void addDependency(const std::string &dependency)
Adds a component name to the list of dependencies.
Definition: Component.C:129
const std::vector< dof_id_type > & getElementIDs() const
Gets the element IDs corresponding to this component.
Definition: Component.C:337
std::vector< SubdomainName > _subdomain_names
List of subdomain names this components owns.
Definition: Component.h:513
T & set(const std::string &name, bool quiet_mode=false)
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)
static InputParameters validParams()
MeshBase & mesh
InputParameters getValidParams(const std::string &name) const
const std::string & cname() const
Get the component name.
Definition: Component.C:51
virtual void addFunctorMaterial(const std::string &functor_material_name, const std::string &name, InputParameters &parameters)
Component * _parent
Pointer to a parent component (used in composed components)
Definition: Component.h:489
std::string stringify(EComponentSetupStatus status) const
Return a string for the setup status.
Definition: Component.C:309
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
Interface class for logging errors and warnings.
Node * addNode(const Point &pt)
Definition: Component.C:203
static InputParameters validParams()
Definition: Component.C:18
const std::string & getBase() const
Mesh for THM.
Definition: THMMesh.h:18
Elem * addNodeElement(dof_id_type node)
Definition: THMMesh.C:116
std::vector< Moose::CoordinateSystemType > _coord_sys
List of coordinate system for each subdomain.
Definition: Component.h:515
std::vector< dof_id_type > _elem_ids
Element IDs of this component.
Definition: Component.h:508
void logError(Args &&... args) const
Logs an error.
Definition: Component.h:226
Elem * addNodeElement(dof_id_type node)
Definition: Component.C:211
void registerBase(const std::string &value)
MPI_Status status
virtual void check() const
Check the component integrity.
Definition: Component.h:416
virtual void addPostprocessor(const std::string &pp_name, const std::string &name, InputParameters &parameters)
bool hasComponent(const std::string &name) const
Find out if simulation has a component with the given name.
Definition: Simulation.C:1002
const std::string & name() const
bool addRelationshipManager(std::shared_ptr< RelationshipManager > relationship_manager)
virtual void addConvergence(const std::string &type, const std::string &name, InputParameters &parameters)
Component(const InputParameters &parameters)
Definition: Component.C:35
void setSubdomainName(SubdomainID subdomain_id, const SubdomainName &name)
std::function< void(const InputParameters &, InputParameters &)> RelationshipManagerInputParameterCallback
std::vector< dof_id_type > _node_ids
Node IDs of this component.
Definition: Component.h:506
void checkComponentExistsByName(const std::string &comp_name) const
Checks that a component exists.
Definition: Component.C:141
std::string nonlinearConvergenceName() const
Nonlinear Convergence name.
Definition: Component.h:486
mesh set up, called primary init
Definition: Component.h:41
mesh set up, called both inits
Definition: Component.h:42
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
virtual void init()
Initializes the component.
Definition: Component.h:405
void addControllableParameterAlias(const MooseObjectParameterName &alias, const MooseObjectParameterName &secondary)
Base class for THM components.
Definition: Component.h:28
std::string stringify(const T &t)
auto log(const T &)
virtual const std::vector< Moose::CoordinateSystemType > & getCoordSysTypes() const
Gets the coordinate system types for this component.
Definition: Component.C:353
virtual void initSecondary()
Perform secondary initialization, which relies on init() being called for all components.
Definition: Component.h:411
void releaseSharedObjects(const MooseObject &moose_object, THREAD_ID tid=0)
THMMesh & mesh()
Non-const reference to THM mesh, which can only be called before the end of mesh setup.
Definition: Component.C:60
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
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
EComponentSetupStatus _component_setup_status
Component setup status.
Definition: Component.h:539
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
std::vector< std::string > _dependencies
List of names of components that this component depends upon.
Definition: Component.h:542
MooseApp & _app
CoordinateSystemType
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
std::vector< SubdomainID > _subdomain_ids
List of subdomain IDs this components owns.
Definition: Component.h:511
Factory & _factory
The Factory associated with the MooseApp.
Definition: Component.h:497
void mooseError(Args &&... args) const
virtual Convergence * getNonlinearConvergence() const
Gets the Component&#39;s nonlinear Convergence object if it has one.
Definition: Component.C:361
bool isParamValid(const std::string &name) const
mesh set up, called both inits, checked
Definition: Component.h:43
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
THMMesh & _mesh
The THM mesh TODO: make _mesh private (applications need to switch to getters to avoid breaking) ...
Definition: Component.h:503
virtual const std::vector< SubdomainName > & getSubdomainNames() const
Gets the subdomain names for this component.
Definition: Component.C:345
static InputParameters validParams()
Definition: THMObject.C:13
void executeInit()
Wrapper function for init() that marks the function as being called.
Definition: Component.C:70
void executeSetupMesh()
Wrapper function for setupMesh() that marks the function as being called.
Definition: Component.C:91
const std::vector< dof_id_type > & getNodeIDs() const
Gets the node IDs corresponding to this component.
Definition: Component.C:329
uint8_t dof_id_type
void executeInitSecondary()
Wrapper function for initSecondary() that marks the function as being called.
Definition: Component.C:77
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
Node * addNode(const Point &pt)
Add a new node into the mesh.
Definition: THMMesh.C:95