https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
20class THMProblem;
21class THMMesh;
23class Convergence;
24
28class Component : public THMObject,
29 public LoggingInterface,
30 public NamingInterface,
32{
33public:
35
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
97
101 void executeCheck() const;
102
106 void executeSetupMesh();
107
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
393protected:
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
517private:
531 void
532 addRelationshipManager(const InputParameters & moose_object_pars,
533 std::string rm_name,
536 Moose::RMSystemType sys_type = Moose::RMSystemType::NONE);
537
540
542 std::vector<std::string> _dependencies;
543
544public:
546};
547
548template <typename T>
549bool
550Component::hasParam(const std::string & name) const
551{
552 return parameters().have_parameter<T>(name);
553}
554
555template <typename T>
556const T &
557Component::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
563template <typename T>
564const T &
565Component::getComponentByName(const std::string & comp_name) const
566{
567 return _sim.getComponentByName<T>(comp_name);
568}
569
570template <typename T>
571bool
572Component::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
578template <typename T>
579bool
580Component::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
588template <typename T>
589T
590Component::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
619template <typename T>
620void
621Component::insistParameterExists(const std::string & function_name,
622 const std::string & param_name) const
623{
624 if (!hasParam<T>(param_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
634template <typename T>
635void
636Component::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
644template <typename T>
645void
646Component::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
657template <typename T>
658void
659Component::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
668template <typename T>
669void
670Component::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
684template <typename T>
685void
686Component::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
700template <typename T1, typename T2>
701void
702Component::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
721template <typename T>
722void
723Component::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
737template <typename T>
738void
739Component::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
758template <typename T1, typename T2>
759void
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}
const double T
const std::string name
Definition Setup.h:21
Base class for THM components.
Definition Component.h:32
std::string stringify(EComponentSetupStatus status) const
Return a string for the setup status.
Definition Component.C:309
virtual void addMooseObjects()
Definition Component.h:115
const THMMesh & constMesh() const
Const reference to mesh, which can be called at any point.
Definition Component.h:63
virtual void init()
Initializes the component.
Definition Component.h:405
virtual const std::vector< SubdomainName > & getSubdomainNames() const
Gets the subdomain names for this component.
Definition Component.C:345
void logError(Args &&... args) const
Logs an error.
Definition Component.h:226
const std::vector< dof_id_type > & getElementIDs() const
Gets the element IDs corresponding to this component.
Definition Component.C:337
Elem * addNodeElement(dof_id_type node)
Definition Component.C:211
void addDependency(const std::string &dependency)
Adds a component name to the list of dependencies.
Definition Component.C:129
bool hasParam(const std::string &name) const
Test if a parameter exists in the object's input parameters.
Definition Component.h:550
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
virtual const std::vector< Moose::CoordinateSystemType > & getCoordSysTypes() const
Gets the coordinate system types for this component.
Definition Component.C:353
bool hasComponentByName(const std::string &cname) const
Check the existence and type of a component given its name.
Definition Component.h:580
void executeInit()
Wrapper function for init() that marks the function as being called.
Definition Component.C:70
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's input paramet...
Definition Component.h:621
THMProblem & getTHMProblem() const
Gets the THM problem.
Definition Component.C:135
Factory & _factory
The Factory associated with the MooseApp.
Definition Component.h:497
std::string nonlinearConvergenceName() const
Nonlinear Convergence name.
Definition Component.h:486
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
virtual void check() const
Check the component integrity.
Definition Component.h:416
std::vector< dof_id_type > _elem_ids
Element IDs of this component.
Definition Component.h:508
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
Node * addNode(const Point &pt)
Definition Component.C:203
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 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
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
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
void checkComponentExistsByName(const std::string &comp_name) const
Checks that a component exists.
Definition Component.C:141
const std::vector< std::string > & getDependencies() const
Returns a list of names of components that this component depends upon.
Definition Component.h:86
bool problemIsTransient() const
Whether the problem is transient.
Definition Component.h:264
virtual Convergence * getNonlinearConvergence() const
Gets the Component's nonlinear Convergence object if it has one.
Definition Component.C:361
std::vector< SubdomainID > _subdomain_ids
List of subdomain IDs this components owns.
Definition Component.h:511
EComponentSetupStatus
Component setup status type.
Definition Component.h:38
@ CHECKED
mesh set up, called both inits, checked
Definition Component.h:43
@ MESH_PREPARED
mesh set up
Definition Component.h:40
@ INITIALIZED_PRIMARY
mesh set up, called primary init
Definition Component.h:41
@ INITIALIZED_SECONDARY
mesh set up, called both inits
Definition Component.h:42
@ CREATED
only created
Definition Component.h:39
std::vector< Moose::CoordinateSystemType > _coord_sys
List of coordinate system for each subdomain.
Definition Component.h:515
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
THMProblem & _sim
THM problem this component is part of TODO: make _sim private (applications need to switch to getters...
Definition Component.h:494
virtual void setupMesh()
Performs mesh setup such as creating mesh or naming mesh sets.
Definition Component.h:421
void executeInitSecondary()
Wrapper function for initSecondary() that marks the function as being called.
Definition Component.C:77
virtual void addVariables()
Definition Component.h:113
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
EComponentSetupStatus _component_setup_status
Component setup status.
Definition Component.h:539
std::vector< dof_id_type > _node_ids
Node IDs of this component.
Definition Component.h:506
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
void executeCheck() const
Wrapper function for check() that marks the function as being called.
Definition Component.C:84
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
const T & getComponentByName(const std::string &cname) const
Return a reference to a component given its name.
Definition Component.h:565
Component * parent()
Definition Component.h:55
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
bool hasComponent(const std::string &name) const
Check the existence and type of a component via a parameter name.
Definition Component.h:572
virtual void initSecondary()
Perform secondary initialization, which relies on init() being called for all components.
Definition Component.h:411
const std::string & cname() const
Get the component name.
Definition Component.C:51
T getEnumParam(const std::string &param, bool log_error=true) const
Gets an enum parameter.
Definition Component.h:590
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 checkSetupStatus(const EComponentSetupStatus &status) const
Throws an error if the supplied setup status of this component has not been reached.
Definition Component.C:117
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
THMMesh & mesh()
Non-const reference to THM mesh, which can only be called before the end of mesh setup.
Definition Component.C:60
std::vector< std::string > _dependencies
List of names of components that this component depends upon.
Definition Component.h:542
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
const std::vector< dof_id_type > & getNodeIDs() const
Gets the node IDs corresponding to this component.
Definition Component.C:329
THMMesh & _mesh
The THM mesh TODO: make _mesh private (applications need to switch to getters to avoid breaking)
Definition Component.h:503
void executeSetupMesh()
Wrapper function for setupMesh() that marks the function as being called.
Definition Component.C:91
Component * _parent
Pointer to a parent component (used in composed components)
Definition Component.h:489
void checkComponentOfTypeExistsByName(const std::string &comp_name) const
Checks that the component of a certain type exists.
Definition Component.h:646
void logWarning(Args &&... args) const
Logs a warning.
Definition Component.h:235
virtual void addRelationshipManagers(Moose::RelationshipManagerType)
Adds relationship managers for the component.
Definition Component.h:111
const Real & _zero
Definition Component.h:499
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
static InputParameters validParams()
Definition Component.C:18
virtual bool isTransient() const override
bool have_parameter(std::string_view name) const
Interface class for logging errors and warnings.
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.
const InputParameters & parameters() const
const std::string & name() const
void mooseError(Args &&... args) const
std::vector< std::string > getNames() const
Interface for handling names.
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 T & getComponentByName(const std::string &name) const
Get component by its name.
Definition Simulation.h:504
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
bool hasComponent(const std::string &name) const
Find out if simulation has a component with the given name.
Mesh for THM.
Definition THMMesh.h:19
Specialization of FEProblem to run with component subsystem.
Definition THMProblem.h:19
std::function< void(const InputParameters &, InputParameters &)> RelationshipManagerInputParameterCallback
RelationshipManagerType
CoordinateSystemType