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 
246  template <typename T>
247  T getEnumParam(const std::string & param) const;
248 
252  bool problemIsTransient() const { return getTHMProblem().isTransient(); }
253 
257  const std::vector<dof_id_type> & getNodeIDs() const;
258 
262  const std::vector<dof_id_type> & getElementIDs() const;
263 
269  virtual const std::vector<SubdomainName> & getSubdomainNames() const;
270 
276  virtual const std::vector<Moose::CoordinateSystemType> & getCoordSysTypes() const;
277 
278 protected:
290  virtual void init() {}
291 
296  virtual void initSecondary() {}
297 
301  virtual void check() const {}
302 
306  virtual void setupMesh() {}
307 
317  void addRelationshipManagersFromParameters(const InputParameters & moose_object_pars);
318 
319  Node * addNode(const Point & pt);
320  Elem * addNodeElement(dof_id_type node);
321 
329  virtual void
330  setSubdomainInfo(SubdomainID subdomain_id,
331  const std::string & subdomain_name,
332  const Moose::CoordinateSystemType & coord_system = Moose::COORD_XYZ);
333 
345  template <typename T>
346  void insistParameterExists(const std::string & function_name,
347  const std::string & param_name) const;
348 
356  template <typename T>
357  void checkParameterValueLessThan(const std::string & param, const T & value_max) const;
358 
366  template <typename T>
367  void checkSizeLessThan(const std::string & param, const unsigned int & n_entries) const;
368 
376  template <typename T>
377  void checkSizeGreaterThan(const std::string & param, const unsigned int & n_entries) const;
378 
387  template <typename T1, typename T2>
388  void checkEqualSize(const std::string & param1, const std::string & param2) const;
389 
399  template <typename T>
400  void checkSizeEqualsValue(const std::string & param, const unsigned int & n_entries) const;
401 
412  template <typename T>
413  void checkSizeEqualsValue(const std::string & param,
414  const unsigned int & n_entries,
415  const std::string & description) const;
416 
425  template <typename T1, typename T2>
426  void checkSizeEqualsParameterValue(const std::string & param1, const std::string & param2) const;
427 
434  void checkMutuallyExclusiveParameters(const std::vector<std::string> & params,
435  bool need_one_specified = true) const;
436 
439 
444 
447 
448  const Real & _zero;
449 
453 
455  std::vector<dof_id_type> _node_ids;
457  std::vector<dof_id_type> _elem_ids;
458 
460  std::vector<SubdomainID> _subdomain_ids;
462  std::vector<SubdomainName> _subdomain_names;
464  std::vector<Moose::CoordinateSystemType> _coord_sys;
465 
466 private:
480  void
481  addRelationshipManager(const InputParameters & moose_object_pars,
482  std::string rm_name,
484  Moose::RelationshipManagerInputParameterCallback rm_input_parameter_func,
485  Moose::RMSystemType sys_type = Moose::RMSystemType::NONE);
486 
489 
491  std::vector<std::string> _dependencies;
492 
493 public:
494  static InputParameters validParams();
495 };
496 
497 template <typename T>
498 bool
499 Component::hasParam(const std::string & name) const
500 {
501  return parameters().have_parameter<T>(name);
502 }
503 
504 template <typename T>
505 const T &
506 Component::getComponent(const std::string & pname) const
507 {
508  const std::string & comp_name = getParam<std::string>(pname);
509  return getComponentByName<T>(comp_name);
510 }
511 
512 template <typename T>
513 const T &
514 Component::getComponentByName(const std::string & comp_name) const
515 {
516  return _sim.getComponentByName<T>(comp_name);
517 }
518 
519 template <typename T>
520 bool
521 Component::hasComponent(const std::string & pname) const
522 {
523  const std::string & comp_name = getParam<std::string>(pname);
524  return hasComponentByName<T>(comp_name);
525 }
526 
527 template <typename T>
528 bool
529 Component::hasComponentByName(const std::string & comp_name) const
530 {
531  if (_sim.hasComponentOfType<T>(comp_name))
532  return true;
533  else
534  return false;
535 }
536 
537 template <typename T>
538 T
539 Component::getEnumParam(const std::string & param) const
540 {
541  const MooseEnum & moose_enum = getParam<MooseEnum>(param);
542  const T value = THM::stringToEnum<T>(moose_enum);
543  if (static_cast<int>(value) < 0) // cast necessary for scoped enums
544  {
545  // Get the keys from the MooseEnum. Unfortunately, this returns a list of
546  // *all* keys, including the invalid key that was supplied. Thus, that key
547  // needs to be manually excluded below.
548  const std::vector<std::string> & keys = moose_enum.getNames();
549 
550  // Create the string of keys to go in the error message. The last element of
551  // keys is skipped because the invalid key should always be last.
552  std::string keys_string = "{";
553  for (unsigned int i = 0; i < keys.size() - 1; ++i)
554  {
555  if (i != 0)
556  keys_string += ",";
557  keys_string += "'" + keys[i] + "'";
558  }
559  keys_string += "}";
560 
561  logError("The parameter '" + param + "' was given an invalid value ('" +
562  std::string(moose_enum) + "'). Valid values (case-insensitive) are " + keys_string);
563  }
564 
565  return value;
566 }
567 
568 template <typename T>
569 void
570 Component::insistParameterExists(const std::string & function_name,
571  const std::string & param_name) const
572 {
573  if (!hasParam<T>(param_name))
574  mooseError(name(),
575  ": Calling ",
576  function_name,
577  " failed, parameter '",
578  param_name,
579  "' does not exist or does not have the type you requested. Double check your "
580  "spelling and/or type of the parameter.");
581 }
582 
583 template <typename T>
584 void
585 Component::checkComponentOfTypeExists(const std::string & param) const
586 {
587  insistParameterExists<std::string>(__FUNCTION__, param);
588 
589  const std::string & comp_name = getParam<std::string>(param);
590  checkComponentOfTypeExistsByName<T>(comp_name);
591 }
592 
593 template <typename T>
594 void
595 Component::checkComponentOfTypeExistsByName(const std::string & comp_name) const
596 {
597  if (!_sim.hasComponentOfType<T>(comp_name))
598  {
599  if (_sim.hasComponent(comp_name))
600  logError("The component '", comp_name, "' is not of type '", demangle(typeid(T).name()), "'");
601  else
602  logError("The component '", comp_name, "' does not exist");
603  }
604 }
605 
606 template <typename T>
607 void
608 Component::checkParameterValueLessThan(const std::string & param, const T & value_max) const
609 {
610  insistParameterExists<T>(__FUNCTION__, param);
611 
612  const auto & value = getParam<T>(param);
613  if (value >= value_max)
614  logError("The value of parameter '", param, "' (", value, ") must be less than ", value_max);
615 }
616 
617 template <typename T>
618 void
619 Component::checkSizeLessThan(const std::string & param, const unsigned int & n_entries) const
620 {
621  insistParameterExists<std::vector<T>>(__FUNCTION__, param);
622 
623  const auto & value = getParam<std::vector<T>>(param);
624  if (value.size() >= n_entries)
625  logError("The number of entries in the parameter '",
626  param,
627  "' (",
628  value.size(),
629  ") must be less than ",
630  n_entries);
631 }
632 
633 template <typename T>
634 void
635 Component::checkSizeGreaterThan(const std::string & param, const unsigned int & n_entries) const
636 {
637  insistParameterExists<std::vector<T>>(__FUNCTION__, param);
638 
639  const auto & value = getParam<std::vector<T>>(param);
640  if (value.size() <= n_entries)
641  logError("The number of entries in the parameter '",
642  param,
643  "' (",
644  value.size(),
645  ") must be greater than ",
646  n_entries);
647 }
648 
649 template <typename T1, typename T2>
650 void
651 Component::checkEqualSize(const std::string & param1, const std::string & param2) const
652 {
653  insistParameterExists<std::vector<T1>>(__FUNCTION__, param1);
654  insistParameterExists<std::vector<T2>>(__FUNCTION__, param2);
655 
656  const auto & value1 = getParam<std::vector<T1>>(param1);
657  const auto & value2 = getParam<std::vector<T2>>(param2);
658  if (value1.size() != value2.size())
659  logError("The number of entries in parameter '",
660  param1,
661  "' (",
662  value1.size(),
663  ") must equal the number of entries of parameter '",
664  param2,
665  "' (",
666  value2.size(),
667  ")");
668 }
669 
670 template <typename T>
671 void
672 Component::checkSizeEqualsValue(const std::string & param, const unsigned int & n_entries) const
673 {
674  insistParameterExists<std::vector<T>>(__FUNCTION__, param);
675 
676  const auto & param_value = getParam<std::vector<T>>(param);
677  if (param_value.size() != n_entries)
678  logError("The number of entries in parameter '",
679  param,
680  "' (",
681  param_value.size(),
682  ") must be equal to ",
683  n_entries);
684 }
685 
686 template <typename T>
687 void
688 Component::checkSizeEqualsValue(const std::string & param,
689  const unsigned int & n_entries,
690  const std::string & description) const
691 {
692  insistParameterExists<std::vector<T>>(__FUNCTION__, param);
693 
694  const auto & param_value = getParam<std::vector<T>>(param);
695  if (param_value.size() != n_entries)
696  logError("The number of entries in parameter '",
697  param,
698  "' (",
699  param_value.size(),
700  ") must be equal to ",
701  description,
702  " (",
703  n_entries,
704  ")");
705 }
706 
707 template <typename T1, typename T2>
708 void
709 Component::checkSizeEqualsParameterValue(const std::string & param1,
710  const std::string & param2) const
711 {
712  insistParameterExists<std::vector<T1>>(__FUNCTION__, param1);
713  insistParameterExists<T2>(__FUNCTION__, param2);
714 
715  const auto & value1 = getParam<std::vector<T1>>(param1);
716  const auto & value2 = getParam<T2>(param2);
717  if (value1.size() != value2)
718  logError("The number of entries in parameter '",
719  param1,
720  "' (",
721  value1.size(),
722  ") must be equal to the value of parameter '",
723  param2,
724  "' (",
725  value2,
726  ")");
727 }
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
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
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
const T & getComponent(const std::string &name) const
Return a reference to a component via a parameter name.
Definition: Component.h:506
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:438
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:608
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:448
Component * parent()
Definition: Component.h:54
std::vector< Moose::CoordinateSystemType > _coord_sys
List of coordinate system for each subdomain.
Definition: Component.h:464
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:619
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
Elem * addNodeElement(dof_id_type node)
Definition: Component.C:221
MPI_Status status
virtual void check() const
Check the component integrity.
Definition: Component.h:301
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:585
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:529
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
bool problemIsTransient() const
Whether the problem is transient.
Definition: Component.h:252
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 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:672
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:296
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:499
void checkComponentOfTypeExistsByName(const std::string &comp_name) const
Checks that the component of a certain type exists.
Definition: Component.h:595
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:570
THMMesh & mesh()
Non-const reference to THM mesh, which can only be called before the end of mesh setup.
Definition: Component.C:60
T getEnumParam(const std::string &param) const
Gets an enum parameter.
Definition: Component.h:539
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:488
std::vector< std::string > _dependencies
List of names of components that this component depends upon.
Definition: Component.h:491
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:521
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
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:709
const T & getComponentByName(const std::string &cname) const
Return a reference to a component given its name.
Definition: Component.h:514
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 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:651
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:635