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

Generated by: LCOV version 1.14