LCOV - code coverage report
Current view: top level - src/base - MooseBase.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 3501bd Lines: 72 76 94.7 %
Date: 2025-09-04 20:01:23 Functions: 10 11 90.9 %
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 "MooseBase.h"
      11             : 
      12             : #include "MooseApp.h"
      13             : #include "Moose.h"
      14             : #include "Factory.h"
      15             : #include "InputParameterWarehouse.h"
      16             : #include "AppFactory.h"
      17             : 
      18             : const std::string MooseBase::type_param = "_type";
      19             : const std::string MooseBase::name_param = "_object_name";
      20             : const std::string MooseBase::unique_name_param = "_unique_name";
      21             : const std::string MooseBase::app_param = "_moose_app";
      22             : const std::string MooseBase::moose_base_param = "_moose_base";
      23             : 
      24             : InputParameters
      25    37449119 : MooseBase::validParams()
      26             : {
      27    37449119 :   InputParameters params = emptyInputParameters();
      28    37449119 :   params.addPrivateParam<std::string>(MooseBase::type_param); // The name of the class being built
      29    37449119 :   params.addPrivateParam<std::string>(MooseBase::name_param); // The name passed the factory
      30    37449119 :   params.addPrivateParam<std::string>(MooseBase::unique_name_param); // The unique name
      31    37449119 :   return params;
      32           0 : }
      33             : 
      34     5823022 : MooseBase::MooseBase(MooseApp & app, const InputParameters & params)
      35             :   : ConsoleStreamInterface(app),
      36     5823022 :     _app(app),
      37    11646044 :     _type(params.getObjectType()),
      38     5823022 :     _name(params.getObjectName()),
      39     5823022 :     _pars(params)
      40             : {
      41             :   mooseAssert(_type.size(), "Type is empty");
      42             :   mooseAssert(_name.size(), "Name is empty");
      43             : 
      44             :   // This enforces that we call finalizeParams (checkParams() and setting file paths)
      45             :   mooseAssert(_pars.isFinalized(), "Params are not finalized");
      46     5823022 : }
      47             : 
      48     5754562 : MooseBase::MooseBase(const InputParameters & params)
      49     5754562 :   : MooseBase(*params.getCheckedPointerParam<MooseApp *>(MooseBase::app_param), params)
      50             : {
      51     5754562 : }
      52             : 
      53             : std::string
      54       63749 : MooseBase::typeAndName() const
      55             : {
      56      254996 :   return type() + std::string(" \"") + name() + std::string("\"");
      57             : }
      58             : 
      59             : MooseObjectParameterName
      60           0 : MooseBase::uniqueParameterName(const std::string & parameter_name) const
      61             : {
      62           0 :   return MooseObjectParameterName(getBase(), name(), parameter_name);
      63             : }
      64             : 
      65             : MooseObjectName
      66     1451613 : MooseBase::uniqueName() const
      67             : {
      68     1451613 :   if (!_pars.have_parameter<std::string>(unique_name_param))
      69           0 :     mooseError("uniqueName(): Object does not have a unique name");
      70     1451613 :   return MooseObjectName(_pars.get<std::string>(unique_name_param));
      71             : }
      72             : 
      73             : void
      74          26 : MooseBase::connectControllableParams(const std::string & parameter,
      75             :                                      const std::string & object_type,
      76             :                                      const std::string & object_name,
      77             :                                      const std::string & object_parameter) const
      78             : {
      79          26 :   auto & factory = _app.getFactory();
      80          26 :   auto & ip_warehouse = _app.getInputParameterWarehouse();
      81             : 
      82          26 :   MooseObjectParameterName primary_name(uniqueName(), parameter);
      83          26 :   const auto base_type = factory.getValidParams(object_type).getBase();
      84          26 :   MooseObjectParameterName secondary_name(base_type, object_name, object_parameter);
      85          26 :   ip_warehouse.addControllableParameterConnection(primary_name, secondary_name);
      86             : 
      87          26 :   const auto & tags = _pars.get<std::vector<std::string>>("control_tags");
      88          52 :   for (const auto & tag : tags)
      89             :   {
      90          26 :     if (!tag.empty())
      91             :     {
      92             :       // Only adds the parameter with the different control tags if the derived class
      93             :       // properly registers the parameter to its own syntax
      94          26 :       MooseObjectParameterName tagged_name(tag, name(), parameter);
      95          26 :       ip_warehouse.addControllableParameterConnection(
      96             :           tagged_name, secondary_name, /*error_on_empty=*/false);
      97          26 :     }
      98             :   }
      99          26 : }
     100             : 
     101             : [[noreturn]] void
     102        1671 : MooseBase::callMooseError(std::string msg,
     103             :                           const bool with_prefix,
     104             :                           const hit::Node * node /* = nullptr */) const
     105             : {
     106        1671 :   callMooseError(&_app, _pars, msg, with_prefix, node);
     107             : }
     108             : 
     109             : [[noreturn]] void
     110        3332 : MooseBase::callMooseError(MooseApp * const app,
     111             :                           const InputParameters & params,
     112             :                           std::string msg,
     113             :                           const bool with_prefix,
     114             :                           const hit::Node * node)
     115             : {
     116        3332 :   if (!node)
     117        1749 :     node = MooseBase::getHitNode(params);
     118             : 
     119        3332 :   std::string multiapp_prefix = "";
     120        3332 :   if (app)
     121             :   {
     122        3288 :     if (!app->isUltimateMaster())
     123          20 :       multiapp_prefix = app->name();
     124        3288 :     app->getOutputWarehouse().mooseConsole();
     125             :   }
     126             : 
     127        3332 :   if (with_prefix)
     128             :     // False here because the hit context will get processed by the node
     129        1743 :     msg = messagePrefix(params, false) + msg;
     130             : 
     131        3385 :   moose::internal::mooseErrorRaw(msg, multiapp_prefix, node);
     132          53 : }
     133             : 
     134             : std::string
     135       17132 : MooseBase::messagePrefix(const InputParameters & params, const bool hit_prefix)
     136             : {
     137       17132 :   std::string prefix = "";
     138             : 
     139       17132 :   if (hit_prefix)
     140       15389 :     if (const auto node = MooseBase::getHitNode(params))
     141       12277 :       prefix += Moose::hitMessagePrefix(*node);
     142             : 
     143             :   // Don't have context without type and name
     144       17132 :   if (!params.isMooseBaseObject())
     145          28 :     return prefix;
     146             : 
     147       17104 :   const auto & name = params.getObjectName();
     148       17104 :   const std::string base = params.hasBase() ? params.getBase() : "object";
     149       17104 :   const bool is_main_app = base == "Application" && name == AppFactory::main_app_name;
     150       17104 :   prefix += "The following occurred in the ";
     151       17104 :   if (is_main_app)
     152         108 :     prefix += "main " + base;
     153             :   else
     154       16996 :     prefix += base;
     155       17104 :   if (base != params.getObjectName() && name.size() && !is_main_app)
     156       12487 :     prefix += " '" + name + "'";
     157       17104 :   prefix += " of type " + params.getObjectType() + ".";
     158       17104 :   return prefix + "\n\n";
     159       17132 : }
     160             : 
     161             : const hit::Node *
     162       17138 : MooseBase::getHitNode(const InputParameters & params)
     163             : {
     164       17138 :   if (const auto hit_node = params.getHitNode())
     165       16980 :     if (!hit_node->isRoot())
     166       13641 :       return hit_node;
     167        3497 :   return nullptr;
     168             : }

Generated by: LCOV version 1.14