https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MooseBase.C
Go to the documentation of this file.
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"
16#include "AppFactory.h"
17
18const std::string MooseBase::type_param = "_type";
19const std::string MooseBase::name_param = "_object_name";
20const std::string MooseBase::unique_name_param = "_unique_name";
21const std::string MooseBase::app_param = "_moose_app";
22const std::string MooseBase::moose_base_param = "_moose_base";
23#ifdef MOOSE_KOKKOS_ENABLED
24const std::string MooseBase::kokkos_object_param = "_kokkos_object";
25#endif
26
29{
31 params.addPrivateParam<std::string>(MooseBase::type_param); // The name of the class being built
32 params.addPrivateParam<std::string>(MooseBase::name_param); // The name passed the factory
33 params.addPrivateParam<std::string>(MooseBase::unique_name_param); // The unique name
34 return params;
35}
36
39 _app(app),
40 _type(params.getObjectType()),
41 _name(params.getObjectName()),
42 _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}
50
52 : MooseBase(*params.getCheckedPointerParam<MooseApp *>(MooseBase::app_param), params)
53{
54}
55
56std::string
58{
59 return type() + std::string(" \"") + name() + std::string("\"");
60}
61
63MooseBase::uniqueParameterName(const std::string & parameter_name) const
64{
65 return MooseObjectParameterName(getBase(), name(), parameter_name);
66}
67
70{
71 if (!_pars.have_parameter<std::string>(unique_name_param))
72 mooseError("uniqueName(): Object does not have a unique name");
73 return MooseObjectName(_pars.get<std::string>(unique_name_param));
74}
75
76void
77MooseBase::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 auto & factory = _app.getFactory();
83 auto & ip_warehouse = _app.getInputParameterWarehouse();
84
85 MooseObjectParameterName primary_name(uniqueName(), parameter);
86 const auto base_type = factory.getValidParams(object_type).getBase();
87 MooseObjectParameterName secondary_name(base_type, object_name, object_parameter);
88 ip_warehouse.addControllableParameterConnection(primary_name, secondary_name);
89
90 const auto & tags = _pars.get<std::vector<std::string>>("control_tags");
91 for (const auto & tag : tags)
92 {
93 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 MooseObjectParameterName tagged_name(tag, name(), parameter);
98 ip_warehouse.addControllableParameterConnection(
99 tagged_name, secondary_name, /*error_on_empty=*/false);
100 }
101 }
102}
103
104[[noreturn]] void
106 const bool with_prefix,
107 const hit::Node * node /* = nullptr */,
108 const bool show_trace /* = true */) const
109{
110 callMooseError(&_app, _pars, msg, with_prefix, node, show_trace);
111}
112
113[[noreturn]] void
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 if (!node)
122 node = MooseBase::getHitNode(params);
123
124 std::string multiapp_prefix = "";
125 if (app)
126 {
127 if (!app->isUltimateMaster())
128 multiapp_prefix = app->name();
130 }
131
132 if (with_prefix)
133 // False here because the hit context will get processed by the node
134 msg = messagePrefix(params, false) + msg;
135
136 moose::internal::mooseErrorRaw(msg, multiapp_prefix, node, show_trace);
137}
138
139std::string
140MooseBase::messagePrefix(const InputParameters & params, const bool hit_prefix)
141{
142 std::string prefix = "";
143
144 if (hit_prefix)
145 if (const auto node = MooseBase::getHitNode(params))
146 prefix += Moose::hitMessagePrefix(*node);
147
148 // Don't have context without type and name
149 if (!params.isMooseBaseObject())
150 return prefix;
151
152 const auto & name = params.getObjectName();
153 const std::string base = params.hasBase() ? params.getBase() : "object";
154 const bool is_main_app = base == "Application" && name == AppFactory::main_app_name;
155 prefix += "The following occurred in the ";
156 if (is_main_app)
157 prefix += "main " + base;
158 else
159 prefix += base;
160 if (base != params.getObjectName() && name.size() && !is_main_app)
161 prefix += " '" + name + "'";
162 prefix += " of type " + params.getObjectType() + ".";
163 return prefix + "\n\n";
164}
165
166const hit::Node *
168{
169 if (const auto hit_node = params.getHitNode())
170 if (!hit_node->isRoot())
171 return hit_node;
172 return nullptr;
173}
InputParameters emptyInputParameters()
static const std::string main_app_name
The name for the "main" moose application.
Definition AppFactory.h:68
An inteface for the _console for outputting to the Console object.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
bool isMooseBaseObject() const
void addPrivateParam(const std::string &name, const T &value)
These method add a parameter to the InputParameters object which can be retrieved like any other para...
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
bool have_parameter(std::string_view name) const
A wrapper around the Parameters base class method.
const std::string & getObjectType() const
bool isFinalized() const
const std::string & getObjectName() const
const hit::Node * getHitNode(const std::string &param) const
const std::string & getBase() const
bool hasBase() const
Base class for MOOSE-based applications.
Definition MooseApp.h:110
OutputWarehouse & getOutputWarehouse()
Get the OutputWarehouse objects.
Definition MooseApp.C:2409
Factory & getFactory()
Retrieve a writable reference to the Factory associated with this App.
Definition MooseApp.h:407
bool isUltimateMaster() const
Whether or not this app is the ultimate master app.
Definition MooseApp.h:866
InputParameterWarehouse & getInputParameterWarehouse()
Get the InputParameterWarehouse for MooseObjects.
Definition MooseApp.C:2867
Base class for everything in MOOSE with a name and a type.
Definition MooseBase.h:50
const std::string & getBase() const
Definition MooseBase.h:147
MooseObjectParameterName uniqueParameterName(const std::string &parameter_name) const
Definition MooseBase.C:63
const std::string & type() const
Get the type of this class.
Definition MooseBase.h:93
static const std::string kokkos_object_param
The name of the parameter that indicates an object is a Kokkos functor.
Definition MooseBase.h:64
const hit::Node * getHitNode() const
Definition MooseBase.h:136
std::string typeAndName() const
Get the class's combined type and name; useful in error handling.
Definition MooseBase.C:57
static InputParameters validParams()
Definition MooseBase.C:28
static const std::string unique_name_param
The name of the parameter that contains the unique object name.
Definition MooseBase.h:57
static const std::string moose_base_param
The name of the parameter that contains the moose system base.
Definition MooseBase.h:61
static const std::string name_param
The name of the parameter that contains the object name.
Definition MooseBase.h:55
static const std::string type_param
The name of the parameter that contains the object type.
Definition MooseBase.h:53
MooseObjectName uniqueName() const
Definition MooseBase.C:69
MooseBase(const InputParameters &params)
Primary constructor for general objects.
Definition MooseBase.C:51
MooseApp & _app
The MOOSE application this is associated with.
Definition MooseBase.h:375
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition MooseBase.h:271
const std::string & _type
The type of this class.
Definition MooseBase.h:378
void callMooseError(std::string msg, const bool with_prefix, const hit::Node *node=nullptr, const bool show_trace=true) const
External method for calling moose error with added object context.
Definition MooseBase.C:105
const InputParameters & _pars
The object's parameters.
Definition MooseBase.h:384
const std::string & _name
The name of this class.
Definition MooseBase.h:381
void connectControllableParams(const std::string &parameter, const std::string &object_type, const std::string &object_name, const std::string &object_parameter) const
Connect controllable parameter of this action with the controllable parameters of the objects added b...
Definition MooseBase.C:77
std::string messagePrefix(const bool hit_prefix=true) const
Definition MooseBase.h:256
static const std::string app_param
The name of the parameter that contains the MooseApp.
Definition MooseBase.h:59
A class for storing the names of MooseObject by tag and object name.
A class for storing an input parameter name.
void mooseConsole()
Send current output buffer to Console output objects.
std::string hitMessagePrefix(const hit::Node &node)
Get the prefix to be associated with a hit node for a message.
Definition Moose.C:883
void mooseErrorRaw(std::string msg, const std::string &prefix="", const hit::Node *node=nullptr, const bool show_trace=true)
Main callback for emitting a moose error.
Definition MooseError.C:53