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 class Convergence;
24 
28 class Component : public THMObject,
29  public LoggingInterface,
30  public NamingInterface,
31  public ADFunctorInterface
32 {
33 public:
35 
38  {
44  };
45 
47  std::string stringify(EComponentSetupStatus status) const;
48 
53  const std::string & cname() const;
54 
55  Component * parent() { return _parent; }
56 
63  const THMMesh & constMesh() const { return _mesh; }
64 
68  THMMesh & mesh();
69 
73  THMProblem & getTHMProblem() const;
74 
80  template <typename T>
81  bool hasParam(const std::string & name) const;
82 
86  const std::vector<std::string> & getDependencies() const { return _dependencies; }
87 
91  void executeInit();
92 
96  void executeInitSecondary();
97 
101  void executeCheck() const;
102 
106  void executeSetupMesh();
107 
111  virtual void addRelationshipManagers(Moose::RelationshipManagerType /*input_rm_type*/) {}
112 
113  virtual void addVariables() {}
114 
115  virtual void addMooseObjects() {}
116 
120  virtual Convergence * getNonlinearConvergence() const;
121 
127  template <typename T>
128  const T & getComponent(const std::string & name) const;
129 
135  template <typename T>
136  const T & getComponentByName(const std::string & cname) const;
137 
144  template <typename T>
145  bool hasComponent(const std::string & name) const;
146 
153  template <typename T>
154  bool hasComponentByName(const std::string & cname) const;
155 
167  void connectObject(const InputParameters & obj_params,
168  const std::string & obj_name,
169  const std::string & param) const;
181  void connectObject(const InputParameters & obj_params,
182  const std::string & obj_name,
183  const std::string & comp_param,
184  const std::string & obj_param) const;
185 
195  void checkSetupStatus(const EComponentSetupStatus & status) const;
196 
202  void checkComponentExistsByName(const std::string & comp_name) const;
203 
210  template <typename T>
211  void checkComponentOfTypeExists(const std::string & param) const;
212 
219  template <typename T>
220  void checkComponentOfTypeExistsByName(const std::string & comp_name) const;
221 
225  template <typename... Args>
226  void logError(Args &&... args) const
227  {
228  logComponentError(cname(), std::forward<Args>(args)...);
229  }
230 
234  template <typename... Args>
235  void logWarning(Args &&... args) const
236  {
237  logComponentWarning(cname(), std::forward<Args>(args)...);
238  }
239 
245  void addDependency(const std::string & dependency);
246 
258  template <typename T>
259  T getEnumParam(const std::string & param, bool log_error = true) const;
260 
264  bool problemIsTransient() const { return getTHMProblem().isTransient(); }
265 
269  const std::vector<dof_id_type> & getNodeIDs() const;
270 
274  const std::vector<dof_id_type> & getElementIDs() const;
275 
281  virtual const std::vector<SubdomainName> & getSubdomainNames() const;
282 
288  virtual const std::vector<Moose::CoordinateSystemType> & getCoordSysTypes() const;
289 
301  template <typename T>
302  void insistParameterExists(const std::string & function_name,
303  const std::string & param_name) const;
304 
312  template <typename T>
313  void checkParameterValueLessThan(const std::string & param, const T & value_max) const;
314 
322  template <typename T>
323  void checkSizeLessThan(const std::string & param, const unsigned int & n_entries) const;
324 
332  template <typename T>
333  void checkSizeGreaterThan(const std::string & param, const unsigned int & n_entries) const;
334 
343  template <typename T1, typename T2>
344  void checkEqualSize(const std::string & param1, const std::string & param2) const;
345 
355  template <typename T>
356  void checkSizeEqualsValue(const std::string & param, const unsigned int & n_entries) const;
357 
368  template <typename T>
369  void checkSizeEqualsValue(const std::string & param,
370  const unsigned int & n_entries,
371  const std::string & description) const;
372 
381  template <typename T1, typename T2>
382  void checkSizeEqualsParameterValue(const std::string & param1, const std::string & param2) const;
383 
390  void checkMutuallyExclusiveParameters(const std::vector<std::string> & params,
391  bool need_one_specified = true) const;
392 
393 protected:
405  virtual void init() {}
406 
411  virtual void initSecondary() {}
412 
416  virtual void check() const {}
417 
421  virtual void setupMesh() {}
422 
432  void addRelationshipManagersFromParameters(const InputParameters & moose_object_pars);
433 
434  Node * addNode(const Point & pt);
435  Elem * addNodeElement(dof_id_type node);
436 
444  virtual void
445  setSubdomainInfo(SubdomainID subdomain_id,
446  const std::string & subdomain_name,
447  const Moose::CoordinateSystemType & coord_system = Moose::COORD_XYZ);
448 
457  void addNonlinearStepFunctorMaterial(const std::string & functor_name,
458  const std::string & property,
459  bool functor_is_ad);
460 
469  void addMaximumFunctorPostprocessor(const std::string & functor_name,
470  const std::string & pp_name,
471  const Real normalization,
472  const std::vector<SubdomainName> & subdomains);
473 
481  void addMultiPostprocessorConvergence(const std::vector<PostprocessorName> & postprocessors,
482  const std::vector<std::string> & descriptions,
483  const std::vector<Real> & tolerances);
484 
486  std::string nonlinearConvergenceName() const { return genName(name(), "nlconv"); }
487 
490 
495 
498 
499  const Real & _zero;
500 
504 
506  std::vector<dof_id_type> _node_ids;
508  std::vector<dof_id_type> _elem_ids;
509 
511  std::vector<SubdomainID> _subdomain_ids;
513  std::vector<SubdomainName> _subdomain_names;
515  std::vector<Moose::CoordinateSystemType> _coord_sys;
516 
517 private:
531  void
532  addRelationshipManager(const InputParameters & moose_object_pars,
533  std::string rm_name,
535  Moose::RelationshipManagerInputParameterCallback rm_input_parameter_func,
536  Moose::RMSystemType sys_type = Moose::RMSystemType::NONE);
537 
540 
542  std::vector<std::string> _dependencies;
543 
544 public:
545  static InputParameters validParams();
546 };
547 
548 template <typename T>
549 bool
550 Component::hasParam(const std::string & name) const
551 {
552  return parameters().have_parameter<T>(name);
553 }
554 
555 template <typename T>
556 const T &
557 Component::getComponent(const std::string & pname) const
558 {
559  const std::string & comp_name = getParam<std::string>(pname);
560  return getComponentByName<T>(comp_name);
561 }
562 
563 template <typename T>
564 const T &
565 Component::getComponentByName(const std::string & comp_name) const
566 {
567  return _sim.getComponentByName<T>(comp_name);
568 }
569 
570 template <typename T>
571 bool
572 Component::hasComponent(const std::string & pname) const
573 {
574  const std::string & comp_name = getParam<std::string>(pname);
575  return hasComponentByName<T>(comp_name);
576 }
577 
578 template <typename T>
579 bool
580 Component::hasComponentByName(const std::string & comp_name) const
581 {
582  if (_sim.hasComponentOfType<T>(comp_name))
583  return true;
584  else
585  return false;
586 }
587 
588 template <typename T>
589 T
590 Component::getEnumParam(const std::string & param, bool log_error) const
591 {
592  const MooseEnum & moose_enum = getParam<MooseEnum>(param);
593  const T value = THM::stringToEnum<T>(moose_enum);
594  if (log_error && static_cast<int>(value) < 0) // cast necessary for scoped enums
595  {
596  // Get the keys from the MooseEnum. Unfortunately, this returns a list of
597  // *all* keys, including the invalid key that was supplied. Thus, that key
598  // needs to be manually excluded below.
599  const std::vector<std::string> & keys = moose_enum.getNames();
600 
601  // Create the string of keys to go in the error message. The last element of
602  // keys is skipped because the invalid key should always be last.
603  std::string keys_string = "{";
604  for (unsigned int i = 0; i < keys.size() - 1; ++i)
605  {
606  if (i != 0)
607  keys_string += ",";
608  keys_string += "'" + keys[i] + "'";
609  }
610  keys_string += "}";
611 
612  logError("The parameter '" + param + "' was given an invalid value ('" +
613  std::string(moose_enum) + "'). Valid values (case-insensitive) are " + keys_string);
614  }
615 
616  return value;
617 }
618 
619 template <typename T>
620 void
621 Component::insistParameterExists(const std::string & function_name,
622  const std::string & param_name) const
623 {
624  if (!hasParam<T>(param_name))
625  mooseError(name(),
626  ": Calling ",
627  function_name,
628  " failed, parameter '",
629  param_name,
630  "' does not exist or does not have the type you requested. Double check your "
631  "spelling and/or type of the parameter.");
632 }
633 
634 template <typename T>
635 void
636 Component::checkComponentOfTypeExists(const std::string & param) const
637 {
638  insistParameterExists<std::string>(__FUNCTION__, param);
639 
640  const std::string & comp_name = getParam<std::string>(param);
641  checkComponentOfTypeExistsByName<T>(comp_name);
642 }
643 
644 template <typename T>
645 void
646 Component::checkComponentOfTypeExistsByName(const std::string & comp_name) const
647 {
648  if (!_sim.hasComponentOfType<T>(comp_name))
649  {
650  if (_sim.hasComponent(comp_name))
651  logError("The component '", comp_name, "' is not of type '", demangle(typeid(T).name()), "'");
652  else
653  logError("The component '", comp_name, "' does not exist");
654  }
655 }
656 
657 template <typename T>
658 void
659 Component::checkParameterValueLessThan(const std::string & param, const T & value_max) const
660 {
661  insistParameterExists<T>(__FUNCTION__, param);
662 
663  const auto & value = getParam<T>(param);
664  if (value >= value_max)
665  logError("The value of parameter '", param, "' (", value, ") must be less than ", value_max);
666 }
667 
668 template <typename T>
669 void
670 Component::checkSizeLessThan(const std::string & param, const unsigned int & n_entries) const
671 {
672  insistParameterExists<std::vector<T>>(__FUNCTION__, param);
673 
674  const auto & value = getParam<std::vector<T>>(param);
675  if (value.size() >= n_entries)
676  logError("The number of entries in the parameter '",
677  param,
678  "' (",
679  value.size(),
680  ") must be less than ",
681  n_entries);
682 }
683 
684 template <typename T>
685 void
686 Component::checkSizeGreaterThan(const std::string & param, const unsigned int & n_entries) const
687 {
688  insistParameterExists<std::vector<T>>(__FUNCTION__, param);
689 
690  const auto & value = getParam<std::vector<T>>(param);
691  if (value.size() <= n_entries)
692  logError("The number of entries in the parameter '",
693  param,
694  "' (",
695  value.size(),
696  ") must be greater than ",
697  n_entries);
698 }
699 
700 template <typename T1, typename T2>
701 void
702 Component::checkEqualSize(const std::string & param1, const std::string & param2) const
703 {
704  insistParameterExists<std::vector<T1>>(__FUNCTION__, param1);
705  insistParameterExists<std::vector<T2>>(__FUNCTION__, param2);
706 
707  const auto & value1 = getParam<std::vector<T1>>(param1);
708  const auto & value2 = getParam<std::vector<T2>>(param2);
709  if (value1.size() != value2.size())
710  logError("The number of entries in parameter '",
711  param1,
712  "' (",
713  value1.size(),
714  ") must equal the number of entries of parameter '",
715  param2,
716  "' (",
717  value2.size(),
718  ")");
719 }
720 
721 template <typename T>
722 void
723 Component::checkSizeEqualsValue(const std::string & param, const unsigned int & n_entries) const
724 {
725  insistParameterExists<std::vector<T>>(__FUNCTION__, param);
726 
727  const auto & param_value = getParam<std::vector<T>>(param);
728  if (param_value.size() != n_entries)
729  logError("The number of entries in parameter '",
730  param,
731  "' (",
732  param_value.size(),
733  ") must be equal to ",
734  n_entries);
735 }
736 
737 template <typename T>
738 void
739 Component::checkSizeEqualsValue(const std::string & param,
740  const unsigned int & n_entries,
741  const std::string & description) const
742 {
743  insistParameterExists<std::vector<T>>(__FUNCTION__, param);
744 
745  const auto & param_value = getParam<std::vector<T>>(param);
746  if (param_value.size() != n_entries)
747  logError("The number of entries in parameter '",
748  param,
749  "' (",
750  param_value.size(),
751  ") must be equal to ",
752  description,
753  " (",
754  n_entries,
755  ")");
756 }
757 
758 template <typename T1, typename T2>
759 void
760 Component::checkSizeEqualsParameterValue(const std::string & param1,
761  const std::string & param2) const
762 {
763  insistParameterExists<std::vector<T1>>(__FUNCTION__, param1);
764  insistParameterExists<T2>(__FUNCTION__, param2);
765 
766  const auto & value1 = getParam<std::vector<T1>>(param1);
767  const auto & value2 = getParam<T2>(param2);
768  if (value1.size() != value2)
769  logError("The number of entries in parameter '",
770  param1,
771  "' (",
772  value1.size(),
773  ") must be equal to the value of parameter '",
774  param2,
775  "' (",
776  value2,
777  ")");
778 }
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.
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
const double T
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
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
const T & getComponent(const std::string &name) const
Return a reference to a component via a parameter name.
Definition: Component.h:557
const InputParameters & parameters() const
T getEnumParam(const std::string &param, bool log_error=true) const
Gets an enum parameter.
Definition: Component.h:590
const std::string & cname() const
Get the component name.
Definition: Component.C:51
void logWarning(Args &&... args) const
Logs a warning.
Definition: Component.h:235
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:489
virtual void addVariables()
Definition: Component.h:113
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
void checkParameterValueLessThan(const std::string &param, const T &value_max) const
Checks that a parameter value is less than a value.
Definition: Component.h:659
Interface class for logging errors and warnings.
Node * addNode(const Point &pt)
Definition: Component.C:203
static InputParameters validParams()
Definition: Component.C:18
Mesh for THM.
Definition: THMMesh.h:18
const Real & _zero
Definition: Component.h:499
Component * parent()
Definition: Component.h:55
std::vector< Moose::CoordinateSystemType > _coord_sys
List of coordinate system for each subdomain.
Definition: Component.h:515
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:670
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
MPI_Status status
virtual void check() const
Check the component integrity.
Definition: Component.h:416
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
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:636
const std::vector< std::string > & getDependencies() const
Returns a list of names of components that this component depends upon.
Definition: Component.h:86
bool hasComponentByName(const std::string &cname) const
Check the existence and type of a component given its name.
Definition: Component.h:580
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
const std::string name
Definition: Setup.h:21
bool problemIsTransient() const
Whether the problem is transient.
Definition: Component.h:264
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 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:723
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:28
subdomain_id_type SubdomainID
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:353
std::vector< std::string > getNames() const
virtual void initSecondary()
Perform secondary initialization, which relies on init() being called for all components.
Definition: Component.h:411
const THMMesh & constMesh() const
Const reference to mesh, which can be called at any point.
Definition: Component.h:63
bool hasParam(const std::string &name) const
Test if a parameter exists in the object&#39;s input parameters.
Definition: Component.h:550
void checkComponentOfTypeExistsByName(const std::string &comp_name) const
Checks that the component of a certain type exists.
Definition: Component.h:646
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:621
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
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:572
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
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:760
const T & getComponentByName(const std::string &cname) const
Return a reference to a component given its name.
Definition: Component.h:565
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 bool isTransient() const override
virtual const std::vector< SubdomainName > & getSubdomainNames() const
Gets the subdomain names for this component.
Definition: Component.C:345
void checkEqualSize(const std::string &param1, const std::string &param2) const
Checks that the size of two vector parameters are equal.
Definition: Component.h:702
void executeInit()
Wrapper function for init() that marks the function as being called.
Definition: Component.C:70
virtual void addMooseObjects()
Definition: Component.h:115
virtual void addRelationshipManagers(Moose::RelationshipManagerType)
Adds relationship managers for the component.
Definition: Component.h:111
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
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:284
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:686