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 & mooseName,
100  const std::string & name) const
101 {
102  connectObject(params, mooseName, name, name);
103 }
104 
105 void
107  const std::string & mooseName,
108  const std::string & name,
109  const std::string & par_name) const
110 {
111  MooseObjectParameterName alias("component", this->name(), name, "::");
112  MooseObjectParameterName par_value(params.get<std::string>("_moose_base"), mooseName, par_name);
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
142  const std::string & control_name,
143  const std::string & param) const
144 {
145  const Function & fn = _sim.getFunction(fn_name);
146  if (dynamic_cast<const ConstantFunction *>(&fn) != nullptr)
147  connectObject(fn.parameters(), fn_name, control_name, param);
148 }
149 
150 void
151 Component::checkComponentExistsByName(const std::string & comp_name) const
152 {
153  if (!_sim.hasComponent(comp_name))
154  logError("The component '", comp_name, "' does not exist");
155 }
156 
157 void
159 {
160  const auto & buildable_types = moose_object_pars.getBuildableRelationshipManagerTypes();
161 
162  for (const auto & buildable_type : buildable_types)
163  {
164  auto & rm_name = std::get<0>(buildable_type);
165  auto & rm_type = std::get<1>(buildable_type);
166  auto rm_input_parameter_func = std::get<2>(buildable_type);
167 
168  addRelationshipManager(moose_object_pars, rm_name, rm_type, rm_input_parameter_func);
169  }
170 }
171 
172 void
174  const InputParameters & moose_object_pars,
175  std::string rm_name,
177  Moose::RelationshipManagerInputParameterCallback rm_input_parameter_func,
179 {
180  // These need unique names
181  static unsigned int unique_object_id = 0;
182 
183  auto new_name = moose_object_pars.get<std::string>("_moose_base") + '_' + name() + '_' + rm_name +
184  "_" + Moose::stringify(rm_type) + " " + std::to_string(unique_object_id);
185 
186  auto rm_params = _factory.getValidParams(rm_name);
187  rm_params.set<Moose::RelationshipManagerType>("rm_type") = rm_type;
188 
189  rm_params.set<std::string>("for_whom") = name();
190 
191  // If there is a callback for setting the RM parameters let's use it
192  if (rm_input_parameter_func)
193  rm_input_parameter_func(moose_object_pars, rm_params);
194 
195  rm_params.set<MooseMesh *>("mesh") = &_mesh;
196 
197  if (!rm_params.areAllRequiredParamsValid())
198  mooseError("Missing required parameters for RelationshipManager " + rm_name + " for object " +
199  name());
200 
201  auto rm_obj = _factory.create<RelationshipManager>(rm_name, new_name, rm_params);
202 
203  const bool added = _app.addRelationshipManager(rm_obj);
204 
205  // Delete the resources created on behalf of the RM if it ends up not being added to the App.
206  if (!added)
208  else // we added it
209  unique_object_id++;
210 }
211 
212 Node *
213 Component::addNode(const Point & pt)
214 {
215  auto node = mesh().addNode(pt);
216  _node_ids.push_back(node->id());
217  return node;
218 }
219 
220 Elem *
222 {
223  auto elem = mesh().addNodeElement(node);
224  _elem_ids.push_back(elem->id());
225  return elem;
226 }
227 
228 void
230  const std::string & subdomain_name,
231  const Moose::CoordinateSystemType & coord_system)
232 {
233  _subdomain_ids.push_back(subdomain_id);
234  _subdomain_names.push_back(subdomain_name);
235  _coord_sys.push_back(coord_system);
236  if (_parent)
237  {
238  _parent->_subdomain_ids.push_back(subdomain_id);
239  _parent->_subdomain_names.push_back(subdomain_name);
240  _parent->_coord_sys.push_back(coord_system);
241  }
242  mesh().setSubdomainName(subdomain_id, subdomain_name);
243 }
244 
245 void
246 Component::checkMutuallyExclusiveParameters(const std::vector<std::string> & params,
247  bool need_one_specified) const
248 {
249  unsigned int n_provided_params = 0;
250  for (const auto & param : params)
251  if (isParamValid(param))
252  n_provided_params++;
253 
254  if (n_provided_params != 1)
255  {
256  std::string params_list_string = "{'" + params[0] + "'";
257  for (unsigned int i = 1; i < params.size(); ++i)
258  params_list_string += ", '" + params[i] + "'";
259  params_list_string += "}";
260 
261  if (n_provided_params == 0 && need_one_specified)
262  logError("One of the parameters ", params_list_string, " must be provided");
263 
264  if (n_provided_params != 0)
265  logError("Only one of the parameters ", params_list_string, " can be provided");
266  }
267 }
268 
270 std::string
272 {
273  switch (status)
274  {
275  case CREATED:
276  return "component created";
277  case MESH_PREPARED:
278  return "component mesh set up";
279  case INITIALIZED_PRIMARY:
280  return "primary initialization completed";
282  return "secondary initialization completed";
283  case CHECKED:
284  return "component fully set up and checked";
285  default:
286  mooseError("Should not reach here");
287  }
288 }
289 
290 const std::vector<dof_id_type> &
292 {
294 
295  return _node_ids;
296 }
297 
298 const std::vector<dof_id_type> &
300 {
302 
303  return _elem_ids;
304 }
305 
306 const std::vector<SubdomainName> &
308 {
310 
311  return _subdomain_names;
312 }
313 
314 const std::vector<Moose::CoordinateSystemType> &
316 {
318 
319  return _coord_sys;
320 }
void executeCheck() const
Wrapper function for check() that marks the function as being called.
Definition: Component.C:84
only created
Definition: Component.h:38
Specialization of FEProblem to run with component subsystem.
Definition: THMProblem.h:18
Interface for handling names.
RelationshipManagerType
THMProblem & _sim
THM problem this component is part of TODO: make _sim private (applications need to switch to getters...
Definition: Component.h:443
EComponentSetupStatus
Component setup status type.
Definition: Component.h:36
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:306
THMProblem & getTHMProblem() const
Gets the THM problem.
Definition: Component.C:135
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
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:299
std::vector< SubdomainName > _subdomain_names
List of subdomain names this components owns.
Definition: Component.h:462
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
Component * _parent
Pointer to a parent component (used in composed components)
Definition: Component.h:438
std::string stringify(EComponentSetupStatus status) const
Return a string for the setup status.
Definition: Component.C:271
void makeFunctionControllableIfConstant(const FunctionName &fn_name, const std::string &control_name, const std::string &param="value") const
Makes a function controllable if it is constant.
Definition: Component.C:141
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:229
Interface class for logging errors and warnings.
Node * addNode(const Point &pt)
Definition: Component.C:213
static InputParameters validParams()
Definition: Component.C:18
virtual const std::string & name() 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:464
std::vector< dof_id_type > _elem_ids
Element IDs of this component.
Definition: Component.h:457
void logError(Args &&... args) const
Logs an error.
Definition: Component.h:215
bool isParamValid(const std::string &name) const
Elem * addNodeElement(dof_id_type node)
Definition: Component.C:221
void registerBase(const std::string &value)
MPI_Status status
virtual void check() const
Check the component integrity.
Definition: Component.h:301
virtual Function & getFunction(const std::string &name, const THREAD_ID tid=0)
bool hasComponent(const std::string &name) const
Find out if simulation has a component with the given name.
Definition: Simulation.C:1002
bool addRelationshipManager(std::shared_ptr< RelationshipManager > relationship_manager)
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:455
void checkComponentExistsByName(const std::string &comp_name) const
Checks that a component exists.
Definition: Component.C:151
const std::string name
Definition: Setup.h:20
mesh set up, called primary init
Definition: Component.h:40
mesh set up, called both inits
Definition: Component.h:41
virtual void init()
Initializes the component.
Definition: Component.h:290
void addControllableParameterAlias(const MooseObjectParameterName &alias, const MooseObjectParameterName &secondary)
Base class for THM components.
Definition: Component.h:27
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:315
virtual void initSecondary()
Perform secondary initialization, which relies on init() being called for all components.
Definition: Component.h:296
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
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:173
EComponentSetupStatus _component_setup_status
Component setup status.
Definition: Component.h:488
std::vector< std::string > _dependencies
List of names of components that this component depends upon.
Definition: Component.h:491
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:460
Factory & _factory
The Factory associated with the MooseApp.
Definition: Component.h:446
void connectObject(const InputParameters &params, const std::string &mooseName, const std::string &name) const
Connect with control logic.
Definition: Component.C:98
void mooseError(Args &&... args) const
const InputParameters & parameters() const
mesh set up, called both inits, checked
Definition: Component.h:42
void addRelationshipManagersFromParameters(const InputParameters &moose_object_pars)
Method to add a relationship manager for the objects being added to the system.
Definition: Component.C:158
THMMesh & _mesh
The THM mesh TODO: make _mesh private (applications need to switch to getters to avoid breaking) ...
Definition: Component.h:452
virtual const std::vector< SubdomainName > & getSubdomainNames() const
Gets the subdomain names for this component.
Definition: Component.C:307
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:291
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:246
Node * addNode(const Point &pt)
Add a new node into the mesh.
Definition: THMMesh.C:95