LCOV - code coverage report
Current view: top level - src/base - MooseBase.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 99787a Lines: 72 76 94.7 %
Date: 2025-10-14 20:01: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    39172188 : MooseBase::validParams()
      29             : {
      30    39172188 :   InputParameters params = emptyInputParameters();
      31    39172188 :   params.addPrivateParam<std::string>(MooseBase::type_param); // The name of the class being built
      32    39172188 :   params.addPrivateParam<std::string>(MooseBase::name_param); // The name passed the factory
      33    39172188 :   params.addPrivateParam<std::string>(MooseBase::unique_name_param); // The unique name
      34    39172188 :   return params;
      35           0 : }
      36             : 
      37     5923309 : MooseBase::MooseBase(MooseApp & app, const InputParameters & params)
      38             :   : ConsoleStreamInterface(app),
      39     5923309 :     _app(app),
      40    11846618 :     _type(params.getObjectType()),
      41     5923309 :     _name(params.getObjectName()),
      42     5923309 :     _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     5923309 : }
      50             : 
      51     5853502 : MooseBase::MooseBase(const InputParameters & params)
      52     5853502 :   : MooseBase(*params.getCheckedPointerParam<MooseApp *>(MooseBase::app_param), params)
      53             : {
      54     5853502 : }
      55             : 
      56             : std::string
      57       65945 : MooseBase::typeAndName() const
      58             : {
      59      263780 :   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     1477339 : MooseBase::uniqueName() const
      70             : {
      71     1477339 :   if (!_pars.have_parameter<std::string>(unique_name_param))
      72           0 :     mooseError("uniqueName(): Object does not have a unique name");
      73     1477339 :   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        1711 : MooseBase::callMooseError(std::string msg,
     106             :                           const bool with_prefix,
     107             :                           const hit::Node * node /* = nullptr */) const
     108             : {
     109        1711 :   callMooseError(&_app, _pars, msg, with_prefix, node);
     110             : }
     111             : 
     112             : [[noreturn]] void
     113        3395 : 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        3395 :   if (!node)
     120        1815 :     node = MooseBase::getHitNode(params);
     121             : 
     122        3395 :   std::string multiapp_prefix = "";
     123        3395 :   if (app)
     124             :   {
     125        3331 :     if (!app->isUltimateMaster())
     126          20 :       multiapp_prefix = app->name();
     127        3331 :     app->getOutputWarehouse().mooseConsole();
     128             :   }
     129             : 
     130        3395 :   if (with_prefix)
     131             :     // False here because the hit context will get processed by the node
     132        1799 :     msg = messagePrefix(params, false) + msg;
     133             : 
     134        3499 :   moose::internal::mooseErrorRaw(msg, multiapp_prefix, node);
     135         104 : }
     136             : 
     137             : std::string
     138       17698 : MooseBase::messagePrefix(const InputParameters & params, const bool hit_prefix)
     139             : {
     140       17698 :   std::string prefix = "";
     141             : 
     142       17698 :   if (hit_prefix)
     143       15899 :     if (const auto node = MooseBase::getHitNode(params))
     144       12669 :       prefix += Moose::hitMessagePrefix(*node);
     145             : 
     146             :   // Don't have context without type and name
     147       17698 :   if (!params.isMooseBaseObject())
     148          44 :     return prefix;
     149             : 
     150       17654 :   const auto & name = params.getObjectName();
     151       17654 :   const std::string base = params.hasBase() ? params.getBase() : "object";
     152       17654 :   const bool is_main_app = base == "Application" && name == AppFactory::main_app_name;
     153       17654 :   prefix += "The following occurred in the ";
     154       17654 :   if (is_main_app)
     155         108 :     prefix += "main " + base;
     156             :   else
     157       17546 :     prefix += base;
     158       17654 :   if (base != params.getObjectName() && name.size() && !is_main_app)
     159       12931 :     prefix += " '" + name + "'";
     160       17654 :   prefix += " of type " + params.getObjectType() + ".";
     161       17654 :   return prefix + "\n\n";
     162       17698 : }
     163             : 
     164             : const hit::Node *
     165       17714 : MooseBase::getHitNode(const InputParameters & params)
     166             : {
     167       17714 :   if (const auto hit_node = params.getHitNode())
     168       17514 :     if (!hit_node->isRoot())
     169       14045 :       return hit_node;
     170        3669 :   return nullptr;
     171             : }

Generated by: LCOV version 1.14