https://mooseframework.inl.gov
Component.h
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 #pragma once
11 
12 #include "THMObject.h"
13 #include "FlowModel.h"
14 #include "THMProblem.h"
16 #include "LoggingInterface.h"
17 #include "NamingInterface.h"
18 #include "ADFunctorInterface.h"
19 
20 class THMProblem;
21 class THMMesh;
23 
27 class Component : public THMObject,
28  public LoggingInterface,
29  public NamingInterface,
30  public ADFunctorInterface
31 {
32 public:
34 
37  {
43  };
44 
46  std::string stringify(EComponentSetupStatus status) const;
47 
52  const std::string & cname() const;
53 
54  Component * parent() { return _parent; }
55 
62  const THMMesh & constMesh() const { return _mesh; }
63 
67  THMMesh & mesh();
68 
72  THMProblem & getTHMProblem() const;
73 
79  template <typename T>
80  bool hasParam(const std::string & name) const;
81 
85  const std::vector<std::string> & getDependencies() const { return _dependencies; }
86 
90  void executeInit();
91 
95  void executeInitSecondary();
96 
100  void executeCheck() const;
101 
105  void executeSetupMesh();
106 
110  virtual void addRelationshipManagers(Moose::RelationshipManagerType /*input_rm_type*/) {}
111 
112  virtual void addVariables() {}
113 
114  virtual void addMooseObjects() {}
115 
121  template <typename T>
122  const T & getComponent(const std::string & name) const;
123 
129  template <typename T>
130  const T & getComponentByName(const std::string & cname) const;
131 
138  template <typename T>
139  bool hasComponent(const std::string & name) const;
140 
147  template <typename T>
148  bool hasComponentByName(const std::string & cname) const;
149 
153  void connectObject(const InputParameters & params,
154  const std::string & mooseName,
155  const std::string & name) const;
159  void connectObject(const InputParameters & params,
160  const std::string & mooseName,
161  const std::string & name,
162  const std::string & par_name) const;
163 
171  void makeFunctionControllableIfConstant(const FunctionName & fn_name,
172  const std::string & control_name,
173  const std::string & param = "value") const;
174 
184  void checkSetupStatus(const EComponentSetupStatus & status) const;
185 
191  void checkComponentExistsByName(const std::string & comp_name) const;
192 
199  template <typename T>
200  void checkComponentOfTypeExists(const std::string & param) const;
201 
208  template <typename T>
209  void checkComponentOfTypeExistsByName(const std::string & comp_name) const;
210 
214  template <typename... Args>
215  void logError(Args &&... args) const
216  {
217  logComponentError(cname(), std::forward<Args>(args)...);
218  }
219 
223  template <typename... Args>
224  void logWarning(Args &&... args) const
225  {
226  logComponentWarning(cname(), std::forward<Args>(args)...);
227  }
228 
234  void addDependency(const std::string & dependency);
235 
247  template <typename T>
248  T getEnumParam(const std::string & param, bool log_error = true) const;
249 
253  bool problemIsTransient() const { return getTHMProblem().isTransient(); }
254 
258  const std::vector<dof_id_type> & getNodeIDs() const;
259 
263  const std::vector<dof_id_type> & getElementIDs() const;
264 
270  virtual const std::vector<SubdomainName> & getSubdomainNames() const;
271 
277  virtual const std::vector<Moose::CoordinateSystemType> & getCoordSysTypes() const;
278 
290  template <typename T>
291  void insistParameterExists(const std::string & function_name,
292  const std::string & param_name) const;
293 
301  template <typename T>
302  void checkParameterValueLessThan(const std::string & param, const T & value_max) const;
303 
311  template <typename T>
312  void checkSizeLessThan(const std::string & param, const unsigned int & n_entries) const;
313 
321  template <typename T>
322  void checkSizeGreaterThan(const std::string & param, const unsigned int & n_entries) const;
323 
332  template <typename T1, typename T2>
333  void checkEqualSize(const std::string & param1, const std::string & param2) const;
334 
344  template <typename T>
345  void checkSizeEqualsValue(const std::string & param, const unsigned int & n_entries) const;
346 
357  template <typename T>
358  void checkSizeEqualsValue(const std::string & param,
359  const unsigned int & n_entries,
360  const std::string & description) const;
361 
370  template <typename T1, typename T2>
371  void checkSizeEqualsParameterValue(const std::string & param1, const std::string & param2) const;
372 
379  void checkMutuallyExclusiveParameters(const std::vector<std::string> & params,
380  bool need_one_specified = true) const;
381 
382 protected:
394  virtual void init() {}
395 
400  virtual void initSecondary() {}
401 
405  virtual void check() const {}
406 
410  virtual void setupMesh() {}
411 
421  void addRelationshipManagersFromParameters(const InputParameters & moose_object_pars);
422 
423  Node * addNode(const Point & pt);
424  Elem * addNodeElement(dof_id_type node);
425 
433  virtual void
434  setSubdomainInfo(SubdomainID subdomain_id,
435  const std::string & subdomain_name,
436  const Moose::CoordinateSystemType & coord_system = Moose::COORD_XYZ);
437 
440 
445 
448 
449  const Real & _zero;
450 
454 
456  std::vector<dof_id_type> _node_ids;
458  std::vector<dof_id_type> _elem_ids;
459 
461  std::vector<SubdomainID> _subdomain_ids;
463  std::vector<SubdomainName> _subdomain_names;
465  std::vector<Moose::CoordinateSystemType> _coord_sys;
466 
467 private:
481  void
482  addRelationshipManager(const InputParameters & moose_object_pars,
483  std::string rm_name,
485  Moose::RelationshipManagerInputParameterCallback rm_input_parameter_func,
486  Moose::RMSystemType sys_type = Moose::RMSystemType::NONE);
487 
490 
492  std::vector<std::string> _dependencies;
493 
494 public:
495  static InputParameters validParams();
496 };
497 
498 template <typename T>
499 bool
500 Component::hasParam(const std::string & name) const
501 {
502  return parameters().have_parameter<T>(name);
503 }
504 
505 template <typename T>
506 const T &
507 Component::getComponent(const std::string & pname) const
508 {
509  const std::string & comp_name = getParam<std::string>(pname);
510  return getComponentByName<T>(comp_name);
511 }
512 
513 template <typename T>
514 const T &
515 Component::getComponentByName(const std::string & comp_name) const
516 {
517  return _sim.getComponentByName<T>(comp_name);
518 }
519 
520 template <typename T>
521 bool
522 Component::hasComponent(const std::string & pname) const
523 {
524  const std::string & comp_name = getParam<std::string>(pname);
525  return hasComponentByName<T>(comp_name);
526 }
527 
528 template <typename T>
529 bool
530 Component::hasComponentByName(const std::string & comp_name) const
531 {
532  if (_sim.hasComponentOfType<T>(comp_name))
533  return true;
534  else
535  return false;
536 }
537 
538 template <typename T>
539 T
540 Component::getEnumParam(const std::string & param, bool log_error) const
541 {
542  const MooseEnum & moose_enum = getParam<MooseEnum>(param);
543  const T value = THM::stringToEnum<T>(moose_enum);
544  if (log_error && static_cast<int>(value) < 0) // cast necessary for scoped enums
545  {
546  // Get the keys from the MooseEnum. Unfortunately, this returns a list of
547  // *all* keys, including the invalid key that was supplied. Thus, that key
548  // needs to be manually excluded below.
549  const std::vector<std::string> & keys = moose_enum.getNames();
550 
551  // Create the string of keys to go in the error message. The last element of
552  // keys is skipped because the invalid key should always be last.
553  std::string keys_string = "{";
554  for (unsigned int i = 0; i < keys.size() - 1; ++i)
555  {
556  if (i != 0)
557  keys_string += ",";
558  keys_string += "'" + keys[i] + "'";
559  }
560  keys_string += "}";
561 
562  logError("The parameter '" + param + "' was given an invalid value ('" +
563  std::string(moose_enum) + "'). Valid values (case-insensitive) are " + keys_string);
564  }
565 
566  return value;
567 }
568 
569 template <typename T>
570 void
571 Component::insistParameterExists(const std::string & function_name,
572  const std::string & param_name) const
573 {
574  if (!hasParam<T>(param_name))
575  mooseError(name(),
576  ": Calling ",
577  function_name,
578  " failed, parameter '",
579  param_name,
580  "' does not exist or does not have the type you requested. Double check your "
581  "spelling and/or type of the parameter.");
582 }
583 
584 template <typename T>
585 void
586 Component::checkComponentOfTypeExists(const std::string & param) const
587 {
588  insistParameterExists<std::string>(__FUNCTION__, param);
589 
590  const std::string & comp_name = getParam<std::string>(param);
591  checkComponentOfTypeExistsByName<T>(comp_name);
592 }
593 
594 template <typename T>
595 void
596 Component::checkComponentOfTypeExistsByName(const std::string & comp_name) const
597 {
598  if (!_sim.hasComponentOfType<T>(comp_name))
599  {
600  if (_sim.hasComponent(comp_name))
601  logError("The component '", comp_name, "' is not of type '", demangle(typeid(T).name()), "'");
602  else
603  logError("The component '", comp_name, "' does not exist");
604  }
605 }
606 
607 template <typename T>
608 void
609 Component::checkParameterValueLessThan(const std::string & param, const T & value_max) const
610 {
611  insistParameterExists<T>(__FUNCTION__, param);
612 
613  const auto & value = getParam<T>(param);
614  if (value >= value_max)
615  logError("The value of parameter '", param, "' (", value, ") must be less than ", value_max);
616 }
617 
618 template <typename T>
619 void
620 Component::checkSizeLessThan(const std::string & param, const unsigned int & n_entries) const
621 {
622  insistParameterExists<std::vector<T>>(__FUNCTION__, param);
623 
624  const auto & value = getParam<std::vector<T>>(param);
625  if (value.size() >= n_entries)
626  logError("The number of entries in the parameter '",
627  param,
628  "' (",
629  value.size(),
630  ") must be less than ",
631  n_entries);
632 }
633 
634 template <typename T>
635 void
636 Component::checkSizeGreaterThan(const std::string & param, const unsigned int & n_entries) const
637 {
638  insistParameterExists<std::vector<T>>(__FUNCTION__, param);
639 
640  const auto & value = getParam<std::vector<T>>(param);
641  if (value.size() <= n_entries)
642  logError("The number of entries in the parameter '",
643  param,
644  "' (",
645  value.size(),
646  ") must be greater than ",
647  n_entries);
648 }
649 
650 template <typename T1, typename T2>
651 void
652 Component::checkEqualSize(const std::string & param1, const std::string & param2) const
653 {
654  insistParameterExists<std::vector<T1>>(__FUNCTION__, param1);
655  insistParameterExists<std::vector<T2>>(__FUNCTION__, param2);
656 
657  const auto & value1 = getParam<std::vector<T1>>(param1);
658  const auto & value2 = getParam<std::vector<T2>>(param2);
659  if (value1.size() != value2.size())
660  logError("The number of entries in parameter '",
661  param1,
662  "' (",
663  value1.size(),
664  ") must equal the number of entries of parameter '",
665  param2,
666  "' (",
667  value2.size(),
668  ")");
669 }
670 
671 template <typename T>
672 void
673 Component::checkSizeEqualsValue(const std::string & param, const unsigned int & n_entries) const
674 {
675  insistParameterExists<std::vector<T>>(__FUNCTION__, param);
676 
677  const auto & param_value = getParam<std::vector<T>>(param);
678  if (param_value.size() != n_entries)
679  logError("The number of entries in parameter '",
680  param,
681  "' (",
682  param_value.size(),
683  ") must be equal to ",
684  n_entries);
685 }
686 
687 template <typename T>
688 void
689 Component::checkSizeEqualsValue(const std::string & param,
690  const unsigned int & n_entries,
691  const std::string & description) const
692 {
693  insistParameterExists<std::vector<T>>(__FUNCTION__, param);
694 
695  const auto & param_value = getParam<std::vector<T>>(param);
696  if (param_value.size() != n_entries)
697  logError("The number of entries in parameter '",
698  param,
699  "' (",
700  param_value.size(),
701  ") must be equal to ",
702  description,
703  " (",
704  n_entries,
705  ")");
706 }
707 
708 template <typename T1, typename T2>
709 void
710 Component::checkSizeEqualsParameterValue(const std::string & param1,
711  const std::string & param2) const
712 {
713  insistParameterExists<std::vector<T1>>(__FUNCTION__, param1);
714  insistParameterExists<T2>(__FUNCTION__, param2);
715 
716  const auto & value1 = getParam<std::vector<T1>>(param1);
717  const auto & value2 = getParam<T2>(param2);
718  if (value1.size() != value2)
719  logError("The number of entries in parameter '",
720  param1,
721  "' (",
722  value1.size(),
723  ") must be equal to the value of parameter '",
724  param2,
725  "' (",
726  value2,
727  ")");
728 }
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:444
EComponentSetupStatus
Component setup status type.
Definition: Component.h:36
virtual void setupMesh()
Performs mesh setup such as creating mesh or naming mesh sets.
Definition: Component.h:410
THMProblem & getTHMProblem() const
Gets the THM problem.
Definition: Component.C:135
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:463
const T & getComponent(const std::string &name) const
Return a reference to a component via a parameter name.
Definition: Component.h:507
T getEnumParam(const std::string &param, bool log_error=true) const
Gets an enum parameter.
Definition: Component.h:540
const std::string & cname() const
Get the component name.
Definition: Component.C:51
void logWarning(Args &&... args) const
Logs a warning.
Definition: Component.h:224
void logComponentWarning(const std::string &component_name, Args &&... args) const
Logs a warning for a component.
void logComponentError(const std::string &component_name, Args &&... args) const
Logs an error for a component.
Component * _parent
Pointer to a parent component (used in composed components)
Definition: Component.h:439
virtual void addVariables()
Definition: Component.h:112
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
void checkParameterValueLessThan(const std::string &param, const T &value_max) const
Checks that a parameter value is less than a value.
Definition: Component.h:609
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
const Real & _zero
Definition: Component.h:449
Component * parent()
Definition: Component.h:54
std::vector< Moose::CoordinateSystemType > _coord_sys
List of coordinate system for each subdomain.
Definition: Component.h:465
void checkSizeLessThan(const std::string &param, const unsigned int &n_entries) const
Checks that the size of a vector parameter is less than a value.
Definition: Component.h:620
std::vector< dof_id_type > _elem_ids
Element IDs of this component.
Definition: Component.h:458
void logError(Args &&... args) const
Logs an error.
Definition: Component.h:215
Elem * addNodeElement(dof_id_type node)
Definition: Component.C:221
MPI_Status status
virtual void check() const
Check the component integrity.
Definition: Component.h:405
bool hasComponent(const std::string &name) const
Find out if simulation has a component with the given name.
Definition: Simulation.C:1002
Component(const InputParameters &parameters)
Definition: Component.C:35
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
void checkComponentOfTypeExists(const std::string &param) const
Checks that the component of a certain type exists, where the name is given by a parameter.
Definition: Component.h:586
const std::vector< std::string > & getDependencies() const
Returns a list of names of components that this component depends upon.
Definition: Component.h:85
bool hasComponentByName(const std::string &cname) const
Check the existence and type of a component given its name.
Definition: Component.h:530
std::function< void(const InputParameters &, InputParameters &)> RelationshipManagerInputParameterCallback
std::vector< dof_id_type > _node_ids
Node IDs of this component.
Definition: Component.h:456
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
bool problemIsTransient() const
Whether the problem is transient.
Definition: Component.h:253
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:394
void checkSizeEqualsValue(const std::string &param, const unsigned int &n_entries) const
Checks that the size of a vector parameter equals a value.
Definition: Component.h:673
const T & getComponentByName(const std::string &name) const
Get component by its name.
Definition: Simulation.h:504
Base class for THM components.
Definition: Component.h:27
std::string demangle(const char *name)
virtual const std::vector< Moose::CoordinateSystemType > & getCoordSysTypes() const
Gets the coordinate system types for this component.
Definition: Component.C:315
std::vector< std::string > getNames() const
virtual void initSecondary()
Perform secondary initialization, which relies on init() being called for all components.
Definition: Component.h:400
const THMMesh & constMesh() const
Const reference to mesh, which can be called at any point.
Definition: Component.h:62
bool hasParam(const std::string &name) const
Test if a parameter exists in the object&#39;s input parameters.
Definition: Component.h:500
void checkComponentOfTypeExistsByName(const std::string &comp_name) const
Checks that the component of a certain type exists.
Definition: Component.h:596
bool have_parameter(std::string_view name) const
void insistParameterExists(const std::string &function_name, const std::string &param_name) const
Runtime check to make sure that a parameter of specified type exists in the component&#39;s input paramet...
Definition: Component.h:571
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:173
EComponentSetupStatus _component_setup_status
Component setup status.
Definition: Component.h:489
std::vector< std::string > _dependencies
List of names of components that this component depends upon.
Definition: Component.h:492
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
bool hasComponent(const std::string &name) const
Check the existence and type of a component via a parameter name.
Definition: Component.h:522
std::vector< SubdomainID > _subdomain_ids
List of subdomain IDs this components owns.
Definition: Component.h:461
Factory & _factory
The Factory associated with the MooseApp.
Definition: Component.h:447
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
void checkSizeEqualsParameterValue(const std::string &param1, const std::string &param2) const
Checks that the size of a vector parameter equals the value of another parameter. ...
Definition: Component.h:710
const T & getComponentByName(const std::string &cname) const
Return a reference to a component given its name.
Definition: Component.h:515
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:453
virtual bool isTransient() const override
virtual const std::vector< SubdomainName > & getSubdomainNames() const
Gets the subdomain names for this component.
Definition: Component.C:307
void checkEqualSize(const std::string &param1, const std::string &param2) const
Checks that the size of two vector parameters are equal.
Definition: Component.h:652
void executeInit()
Wrapper function for init() that marks the function as being called.
Definition: Component.C:70
virtual void addMooseObjects()
Definition: Component.h:114
virtual void addRelationshipManagers(Moose::RelationshipManagerType)
Adds relationship managers for the component.
Definition: Component.h:110
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
bool hasComponentOfType(const std::string &name) const
Find out if simulation has a component with the given name and specified type.
Definition: Simulation.h:493
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
void checkSizeGreaterThan(const std::string &param, const unsigned int &n_entries) const
Checks that the size of a vector parameter is greater than a value.
Definition: Component.h:636