LCOV - code coverage report
Current view: top level - src/components - Component.C (source / functions) Hit Total Coverage
Test: idaholab/moose thermal_hydraulics: #30301 (3b550b) with base 2ad78d Lines: 106 145 73.1 %
Date: 2025-07-30 13:02:48 Functions: 23 26 88.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : #include "Component.h"
      11             : #include "THMMesh.h"
      12             : #include "ThermalHydraulicsApp.h"
      13             : #include "ConstantFunction.h"
      14             : #include "Numerics.h"
      15             : #include "RelationshipManager.h"
      16             : 
      17             : InputParameters
      18       31290 : Component::validParams()
      19             : {
      20       31290 :   InputParameters params = THMObject::validParams();
      21       31290 :   params += ADFunctorInterface::validParams();
      22       31290 :   params.addPrivateParam<THMProblem *>("_thm_problem");
      23       31290 :   params.addPrivateParam<Component *>("_parent", nullptr);
      24       62580 :   params.addPrivateParam<std::string>("built_by_action", "add_component");
      25             : 
      26       31290 :   params.registerBase("Component");
      27             : 
      28       31290 :   return params;
      29           0 : }
      30             : 
      31             : /*
      32             :  * Component implementation
      33             :  */
      34             : 
      35       15640 : Component::Component(const InputParameters & parameters)
      36             :   : THMObject(parameters),
      37       31280 :     LoggingInterface(getCheckedPointerParam<THMProblem *>("_thm_problem")->log()),
      38             :     NamingInterface(),
      39             :     ADFunctorInterface(this),
      40             : 
      41       15640 :     _parent(getParam<Component *>("_parent")),
      42       31280 :     _sim(*getCheckedPointerParam<THMProblem *>("_thm_problem")),
      43       15640 :     _factory(_app.getFactory()),
      44       15640 :     _zero(_sim._real_zero[0]),
      45       15640 :     _mesh(static_cast<THMMesh &>(_sim.mesh())),
      46       46920 :     _component_setup_status(CREATED)
      47             : {
      48       15640 : }
      49             : 
      50             : const std::string &
      51         513 : Component::cname() const
      52             : {
      53         513 :   if (_parent)
      54             :     return _parent->cname();
      55             :   else
      56         513 :     return name();
      57             : }
      58             : 
      59             : THMMesh &
      60     1385928 : Component::mesh()
      61             : {
      62     1385928 :   if (_component_setup_status >= MESH_PREPARED)
      63           0 :     mooseError(
      64             :         "A non-const reference to the THM mesh cannot be obtained after mesh setup is complete.");
      65             :   else
      66     1385928 :     return _mesh;
      67             : }
      68             : 
      69             : void
      70       15542 : Component::executeInit()
      71             : {
      72       15542 :   init();
      73       15540 :   _component_setup_status = INITIALIZED_PRIMARY;
      74       15540 : }
      75             : 
      76             : void
      77       15540 : Component::executeInitSecondary()
      78             : {
      79       15540 :   initSecondary();
      80       15540 :   _component_setup_status = INITIALIZED_SECONDARY;
      81       15540 : }
      82             : 
      83             : void
      84       15270 : Component::executeCheck() const
      85             : {
      86       15270 :   check();
      87       15270 :   _component_setup_status = CHECKED;
      88       15270 : }
      89             : 
      90             : void
      91       15638 : Component::executeSetupMesh()
      92             : {
      93       15638 :   setupMesh();
      94       15638 :   _component_setup_status = MESH_PREPARED;
      95       15638 : }
      96             : 
      97             : void
      98        5910 : Component::connectObject(const InputParameters & params,
      99             :                          const std::string & mooseName,
     100             :                          const std::string & name) const
     101             : {
     102        5910 :   connectObject(params, mooseName, name, name);
     103        5910 : }
     104             : 
     105             : void
     106       28785 : Component::connectObject(const InputParameters & params,
     107             :                          const std::string & mooseName,
     108             :                          const std::string & name,
     109             :                          const std::string & par_name) const
     110             : {
     111       57570 :   MooseObjectParameterName alias("component", this->name(), name, "::");
     112       28785 :   MooseObjectParameterName par_value(params.get<std::string>("_moose_base"), mooseName, par_name);
     113       28785 :   _app.getInputParameterWarehouse().addControllableParameterAlias(alias, par_value);
     114       28785 : }
     115             : 
     116             : void
     117      293474 : Component::checkSetupStatus(const EComponentSetupStatus & status) const
     118             : {
     119      293474 :   if (_component_setup_status < status)
     120           2 :     mooseError(name(),
     121             :                ": The component setup status (",
     122           2 :                stringify(_component_setup_status),
     123             :                ") is less than the required status (",
     124           2 :                stringify(status),
     125             :                ")");
     126      293472 : }
     127             : 
     128             : void
     129       10786 : Component::addDependency(const std::string & dependency)
     130             : {
     131       10786 :   _dependencies.push_back(dependency);
     132       10786 : }
     133             : 
     134             : THMProblem &
     135      194780 : Component::getTHMProblem() const
     136             : {
     137      194780 :   return _sim;
     138             : }
     139             : 
     140             : void
     141       18024 : Component::makeFunctionControllableIfConstant(const FunctionName & fn_name,
     142             :                                               const std::string & control_name,
     143             :                                               const std::string & param) const
     144             : {
     145       18024 :   const Function & fn = _sim.getFunction(fn_name);
     146       18024 :   if (dynamic_cast<const ConstantFunction *>(&fn) != nullptr)
     147       17547 :     connectObject(fn.parameters(), fn_name, control_name, param);
     148       18024 : }
     149             : 
     150             : void
     151           0 : Component::checkComponentExistsByName(const std::string & comp_name) const
     152             : {
     153           0 :   if (!_sim.hasComponent(comp_name))
     154           0 :     logError("The component '", comp_name, "' does not exist");
     155           0 : }
     156             : 
     157             : void
     158          27 : Component::addRelationshipManagersFromParameters(const InputParameters & moose_object_pars)
     159             : {
     160          27 :   const auto & buildable_types = moose_object_pars.getBuildableRelationshipManagerTypes();
     161             : 
     162          27 :   for (const auto & buildable_type : buildable_types)
     163             :   {
     164             :     auto & rm_name = std::get<0>(buildable_type);
     165             :     auto & rm_type = std::get<1>(buildable_type);
     166           0 :     auto rm_input_parameter_func = std::get<2>(buildable_type);
     167             : 
     168           0 :     addRelationshipManager(moose_object_pars, rm_name, rm_type, rm_input_parameter_func);
     169             :   }
     170          27 : }
     171             : 
     172             : void
     173           0 : Component::addRelationshipManager(
     174             :     const InputParameters & moose_object_pars,
     175             :     std::string rm_name,
     176             :     Moose::RelationshipManagerType rm_type,
     177             :     Moose::RelationshipManagerInputParameterCallback rm_input_parameter_func,
     178             :     Moose::RMSystemType)
     179             : {
     180             :   // These need unique names
     181             :   static unsigned int unique_object_id = 0;
     182             : 
     183           0 :   auto new_name = moose_object_pars.get<std::string>("_moose_base") + '_' + name() + '_' + rm_name +
     184           0 :                   "_" + Moose::stringify(rm_type) + " " + std::to_string(unique_object_id);
     185             : 
     186           0 :   auto rm_params = _factory.getValidParams(rm_name);
     187           0 :   rm_params.set<Moose::RelationshipManagerType>("rm_type") = rm_type;
     188             : 
     189           0 :   rm_params.set<std::string>("for_whom") = name();
     190             : 
     191             :   // If there is a callback for setting the RM parameters let's use it
     192           0 :   if (rm_input_parameter_func)
     193           0 :     rm_input_parameter_func(moose_object_pars, rm_params);
     194             : 
     195           0 :   rm_params.set<MooseMesh *>("mesh") = &_mesh;
     196             : 
     197           0 :   if (!rm_params.areAllRequiredParamsValid())
     198           0 :     mooseError("Missing required parameters for RelationshipManager " + rm_name + " for object " +
     199           0 :                name());
     200             : 
     201           0 :   auto rm_obj = _factory.create<RelationshipManager>(rm_name, new_name, rm_params);
     202             : 
     203           0 :   const bool added = _app.addRelationshipManager(rm_obj);
     204             : 
     205             :   // Delete the resources created on behalf of the RM if it ends up not being added to the App.
     206           0 :   if (!added)
     207           0 :     _factory.releaseSharedObjects(*rm_obj);
     208             :   else // we added it
     209           0 :     unique_object_id++;
     210           0 : }
     211             : 
     212             : Node *
     213      445053 : Component::addNode(const Point & pt)
     214             : {
     215      445053 :   auto node = mesh().addNode(pt);
     216      445053 :   _node_ids.push_back(node->id());
     217      445053 :   return node;
     218             : }
     219             : 
     220             : Elem *
     221        1099 : Component::addNodeElement(dof_id_type node)
     222             : {
     223        1099 :   auto elem = mesh().addNodeElement(node);
     224        1099 :   _elem_ids.push_back(elem->id());
     225        1099 :   return elem;
     226             : }
     227             : 
     228             : void
     229        7927 : Component::setSubdomainInfo(SubdomainID subdomain_id,
     230             :                             const std::string & subdomain_name,
     231             :                             const Moose::CoordinateSystemType & coord_system)
     232             : {
     233        7927 :   _subdomain_ids.push_back(subdomain_id);
     234        7927 :   _subdomain_names.push_back(subdomain_name);
     235        7927 :   _coord_sys.push_back(coord_system);
     236        7927 :   if (_parent)
     237             :   {
     238           0 :     _parent->_subdomain_ids.push_back(subdomain_id);
     239           0 :     _parent->_subdomain_names.push_back(subdomain_name);
     240           0 :     _parent->_coord_sys.push_back(coord_system);
     241             :   }
     242        7927 :   mesh().setSubdomainName(subdomain_id, subdomain_name);
     243        7927 : }
     244             : 
     245             : void
     246        1947 : Component::checkMutuallyExclusiveParameters(const std::vector<std::string> & params,
     247             :                                             bool need_one_specified) const
     248             : {
     249             :   unsigned int n_provided_params = 0;
     250        5841 :   for (const auto & param : params)
     251        3894 :     if (isParamValid(param))
     252        1796 :       n_provided_params++;
     253             : 
     254        1947 :   if (n_provided_params != 1)
     255             :   {
     256         151 :     std::string params_list_string = "{'" + params[0] + "'";
     257         302 :     for (unsigned int i = 1; i < params.size(); ++i)
     258         302 :       params_list_string += ", '" + params[i] + "'";
     259             :     params_list_string += "}";
     260             : 
     261         151 :     if (n_provided_params == 0 && need_one_specified)
     262           0 :       logError("One of the parameters ", params_list_string, " must be provided");
     263             : 
     264         151 :     if (n_provided_params != 0)
     265           0 :       logError("Only one of the parameters ", params_list_string, " can be provided");
     266             :   }
     267        1947 : }
     268             : 
     269             : /// Return a string for the setup status
     270             : std::string
     271           4 : Component::stringify(EComponentSetupStatus status) const
     272             : {
     273           4 :   switch (status)
     274             :   {
     275             :     case CREATED:
     276           0 :       return "component created";
     277             :     case MESH_PREPARED:
     278           2 :       return "component mesh set up";
     279             :     case INITIALIZED_PRIMARY:
     280           2 :       return "primary initialization completed";
     281             :     case INITIALIZED_SECONDARY:
     282           0 :       return "secondary initialization completed";
     283             :     case CHECKED:
     284           0 :       return "component fully set up and checked";
     285           0 :     default:
     286           0 :       mooseError("Should not reach here");
     287             :   }
     288             : }
     289             : 
     290             : const std::vector<dof_id_type> &
     291           0 : Component::getNodeIDs() const
     292             : {
     293           0 :   checkSetupStatus(MESH_PREPARED);
     294             : 
     295           0 :   return _node_ids;
     296             : }
     297             : 
     298             : const std::vector<dof_id_type> &
     299         458 : Component::getElementIDs() const
     300             : {
     301         458 :   checkSetupStatus(MESH_PREPARED);
     302             : 
     303         458 :   return _elem_ids;
     304             : }
     305             : 
     306             : const std::vector<SubdomainName> &
     307      234460 : Component::getSubdomainNames() const
     308             : {
     309      234460 :   checkSetupStatus(MESH_PREPARED);
     310             : 
     311      234460 :   return _subdomain_names;
     312             : }
     313             : 
     314             : const std::vector<Moose::CoordinateSystemType> &
     315       15548 : Component::getCoordSysTypes() const
     316             : {
     317       15548 :   checkSetupStatus(MESH_PREPARED);
     318             : 
     319       15548 :   return _coord_sys;
     320             : }

Generated by: LCOV version 1.14