https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MooseError.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 "MooseError.h"
11#include "MooseUtils.h"
12#include "MooseVariable.h"
13#include "Registry.h"
14
15#include "libmesh/string_to_enum.h"
16
17namespace moose
18{
19
20namespace internal
21{
22
23std::string
25{
26 std::stringstream ss;
27 ss << libMesh::Utility::enum_to_string<FEFamily>(var1.feType().family) << ",ORDER"
28 << var1.feType().order
29 << " != " << libMesh::Utility::enum_to_string<FEFamily>(var2.feType().family) << ",ORDER"
30 << var2.feType().order;
31 return ss.str();
32}
33
34std::string
35mooseMsgFmt(const std::string & msg, const std::string & title, const std::string & color)
36{
37 std::ostringstream oss;
38 oss << "\n" << color << "\n" << title << "\n" << msg << COLOR_DEFAULT << "\n";
39 return oss.str();
40}
41
42std::string
43mooseMsgFmt(const std::string & msg, const std::string & color)
44{
45 std::ostringstream oss;
46 oss << "\n" << color << "\n" << msg << COLOR_DEFAULT << "\n";
47 return oss.str();
48}
49
50[[noreturn]] void
51mooseErrorRaw(std::string msg,
52 const std::string & prefix /* = "" */,
53 const hit::Node * node /* = nullptr */,
54 const bool show_trace /* = true */)
55{
57 throw MooseRuntimeError(msg, node);
58
59 // Atomic that will be set as soon as any thread hits this method
60 static std::atomic_flag aborting = ATOMIC_FLAG_INIT;
61
62 // Per-thread bool that will avoid us getting stuck in pause() if one
63 // thread hits this method twice.
64 thread_local static bool this_thread_aborting = false;
65
66 // This branch will be hit after another thread has already set the atomic.
67 // MPI_Abort, despite its name, does not behave like std::abort but instead
68 // calls exit handlers and destroys statics. So we don't want to touch
69 // anything static at this point. We'll just wait until the winning thread
70 // has incurred program exit
71 if (aborting.test_and_set(std::memory_order_acq_rel) && !this_thread_aborting)
72 {
73 // Waiting for the other thread(s), not burning CPU
74 for (;;)
75 pause();
76 }
77 // We're the first thread to hit this method (we set the atomic), so we're
78 // responsible for dumping the error and trace(s) while the remaining
79 // threads wait for us to exit (via libmesh_abort())
80 else
81 {
82 // No, really, terminate. libmesh_abort() may throw an
83 // exception, and if we catch that exception (or a bad_alloc, or
84 // anything thrown) and mooseError because we didn't understand
85 // it, we just want to continue up the stack.
86 if (this_thread_aborting)
88
89 this_thread_aborting = true;
90
91 // Output the message if there is one, but flush it without the trace
92 // as trace retrieval can be slow in some circumstances and we want to
93 // get the error message out ASAP
94 if (!msg.empty())
95 {
96 // If we have a node available, add in the hit context (file location)
97 if (node)
98 msg = Moose::hitMessagePrefix(*node) + msg;
99
100 msg = mooseMsgFmt(msg, "*** ERROR ***", COLOR_RED) + "\n";
101 if (!prefix.empty()) // multiapp prefix
102 MooseUtils::indentMessage(prefix, msg);
103
104 {
105 Threads::spin_mutex::scoped_lock lock(moose_stream_lock);
106 Moose::err << msg << std::flush;
107 }
108 }
109
110 // Print the trace if enabled and on a single rank
111 if (show_trace && libMesh::global_n_processors() == 1)
112 {
113 std::ostringstream oss;
115 auto trace = oss.str();
116 if (!prefix.empty()) // multiapp prefix
117 MooseUtils::indentMessage(prefix, trace);
118
119 {
120 Threads::spin_mutex::scoped_lock lock(moose_stream_lock);
121 Moose::err << trace << std::flush;
122 }
123 }
124
125 // In parallel with libMesh configured with --enable-tracefiles, this will
126 // dump a trace for each rank to file
129
131 }
132}
133
134void
135mooseStreamAll(std::ostringstream &)
136{
137}
138
139std::string
140formatMooseDocumentedError(const std::string & repo_name,
141 const unsigned int issue_num,
142 const std::string & msg)
143{
144 const auto & repo_url = Registry::getRepositoryURL(repo_name);
145 std::stringstream oss;
146 oss << msg << "\n\nThis error is documented at " << repo_url << "/issues/" << issue_num << ".";
147 return oss.str();
148}
149
150} // namespace internal
151
152void
153translateMetaPhysicLError(const MetaPhysicL::LogicError &)
154{
156 "We caught a MetaPhysicL error in while performing element or face loops. This is "
157 "potentially due to AD not having a sufficiently large derivative container size. To "
158 "increase the AD container size, you can run configure in the MOOSE root directory with the "
159 "'--with-derivative-size=<n>' option and then recompile. Other causes of MetaPhysicL logic "
160 "errors include evaluating functions where they are not defined or differentiable like sqrt "
161 "(which gets called for vector norm functions) or log with arguments <= 0");
162}
163
164} // namespace moose
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
Exception to be thrown whenever we have _throw_on_error set and a mooseError() is emitted.
Definition MooseError.h:118
const libMesh::FEType & feType() const
Get the type of finite element object.
This class provides an interface for common operations on field variables of both FE and FV types wit...
static const std::string & getRepositoryURL(const std::string &repo_name)
Returns the repository URL associated with repo_name.
Definition Registry.C:189
OrderWrapper order
void indentMessage(const std::string &prefix, std::string &message, const char *color, bool indent_first_line, const std::string &post_prefix)
Definition MooseUtils.C:749
bool _throw_on_error
Variable to turn on exceptions during mooseError(), should only be used within MOOSE unit tests or wh...
Definition Moose.C:911
std::string hitMessagePrefix(const hit::Node &node)
Get the prefix to be associated with a hit node for a message.
Definition Moose.C:898
void write_traceout()
processor_id_type global_n_processors()
void libmesh_abort()
void print_trace(std::ostream &out_stream=std::cerr)
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:51
libMesh::Threads::spin_mutex moose_stream_lock
Definition MooseError.h:140
std::string formatMooseDocumentedError(const std::string &repo_name, const unsigned int issue_num, const std::string &msg)
Formats a documented error.
Definition MooseError.C:140
std::string mooseMsgFmt(const std::string &msg, const std::string &title, const std::string &color)
Format a message for output with a title.
Definition MooseError.C:35
void mooseStreamAll(std::ostringstream &ss)
All of the following are not meant to be called directly - they are called by the normal macros (moos...
Definition MooseError.C:135
std::string incompatVarMsg(MooseVariableFieldBase &var1, MooseVariableFieldBase &var2)
Builds and returns a string of the form:
Definition MooseError.C:24
void translateMetaPhysicLError(const MetaPhysicL::LogicError &)
emit a relatively clear error message when we catch a MetaPhysicL logic error
Definition MooseError.C:153