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 19357257 : MooseBase::validParams() 29 : { 30 19357257 : InputParameters params = emptyInputParameters(); 31 19357257 : params.addPrivateParam<std::string>(MooseBase::type_param); // The name of the class being built 32 19357257 : params.addPrivateParam<std::string>(MooseBase::name_param); // The name passed the factory 33 19357257 : params.addPrivateParam<std::string>(MooseBase::unique_name_param); // The unique name 34 19357257 : return params; 35 0 : } 36 : 37 5742358 : MooseBase::MooseBase(MooseApp & app, const InputParameters & params) 38 : : ConsoleStreamInterface(app), 39 5742358 : _app(app), 40 11484716 : _type(params.getObjectType()), 41 5742358 : _name(params.getObjectName()), 42 5742358 : _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 5742358 : } 50 : 51 5675363 : MooseBase::MooseBase(const InputParameters & params) 52 5675363 : : MooseBase(*params.getCheckedPointerParam<MooseApp *>(MooseBase::app_param), params) 53 : { 54 5675363 : } 55 : 56 : std::string 57 64221 : MooseBase::typeAndName() const 58 : { 59 256884 : 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 1400019 : MooseBase::uniqueName() const 70 : { 71 1400019 : if (!_pars.have_parameter<std::string>(unique_name_param)) 72 0 : mooseError("uniqueName(): Object does not have a unique name"); 73 1400019 : return MooseObjectName(_pars.get<std::string>(unique_name_param)); 74 : } 75 : 76 : void 77 24 : 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 24 : auto & factory = _app.getFactory(); 83 24 : auto & ip_warehouse = _app.getInputParameterWarehouse(); 84 : 85 24 : MooseObjectParameterName primary_name(uniqueName(), parameter); 86 24 : const auto base_type = factory.getValidParams(object_type).getBase(); 87 24 : MooseObjectParameterName secondary_name(base_type, object_name, object_parameter); 88 24 : ip_warehouse.addControllableParameterConnection(primary_name, secondary_name); 89 : 90 24 : const auto & tags = _pars.get<std::vector<std::string>>("control_tags"); 91 48 : for (const auto & tag : tags) 92 : { 93 24 : 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 24 : MooseObjectParameterName tagged_name(tag, name(), parameter); 98 24 : ip_warehouse.addControllableParameterConnection( 99 : tagged_name, secondary_name, /*error_on_empty=*/false); 100 24 : } 101 : } 102 24 : } 103 : 104 : [[noreturn]] void 105 1362 : MooseBase::callMooseError(std::string msg, 106 : const bool with_prefix, 107 : const hit::Node * node /* = nullptr */, 108 : const bool show_trace /* = true */) const 109 : { 110 1362 : callMooseError(&_app, _pars, msg, with_prefix, node, show_trace); 111 : } 112 : 113 : [[noreturn]] void 114 2728 : MooseBase::callMooseError(MooseApp * const app, 115 : const InputParameters & params, 116 : std::string msg, 117 : const bool with_prefix, 118 : const hit::Node * node, 119 : const bool show_trace /* = true */) 120 : { 121 2728 : if (!node) 122 1482 : node = MooseBase::getHitNode(params); 123 : 124 2728 : std::string multiapp_prefix = ""; 125 2728 : if (app) 126 : { 127 2646 : if (!app->isUltimateMaster()) 128 15 : multiapp_prefix = app->name(); 129 2646 : app->getOutputWarehouse().mooseConsole(); 130 : } 131 : 132 2728 : if (with_prefix) 133 : // False here because the hit context will get processed by the node 134 1438 : msg = messagePrefix(params, false) + msg; 135 : 136 2878 : moose::internal::mooseErrorRaw(msg, multiapp_prefix, node, show_trace); 137 150 : } 138 : 139 : std::string 140 17511 : MooseBase::messagePrefix(const InputParameters & params, const bool hit_prefix) 141 : { 142 17511 : std::string prefix = ""; 143 : 144 17511 : if (hit_prefix) 145 16073 : if (const auto node = MooseBase::getHitNode(params)) 146 12651 : prefix += Moose::hitMessagePrefix(*node); 147 : 148 : // Don't have context without type and name 149 17511 : if (!params.isMooseBaseObject()) 150 41 : return prefix; 151 : 152 17470 : const auto & name = params.getObjectName(); 153 17470 : const std::string base = params.hasBase() ? params.getBase() : "object"; 154 17470 : const bool is_main_app = base == "Application" && name == AppFactory::main_app_name; 155 17470 : prefix += "The following occurred in the "; 156 17470 : if (is_main_app) 157 132 : prefix += "main " + base; 158 : else 159 17338 : prefix += base; 160 17470 : if (base != params.getObjectName() && name.size() && !is_main_app) 161 12474 : prefix += " '" + name + "'"; 162 17470 : prefix += " of type " + params.getObjectType() + "."; 163 17470 : return prefix + "\n\n"; 164 17511 : } 165 : 166 : const hit::Node * 167 17555 : MooseBase::getHitNode(const InputParameters & params) 168 : { 169 17555 : if (const auto hit_node = params.getHitNode()) 170 17301 : if (!hit_node->isRoot()) 171 13751 : return hit_node; 172 3804 : return nullptr; 173 : }