https://mooseframework.inl.gov
Moose.h
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 #pragma once
11 
13 #include "libmesh/perf_log.h"
14 #include "libmesh/libmesh_common.h"
15 #include "XTermConstants.h"
16 
17 #include <limits>
18 #include <memory>
19 #include <set>
20 #include <string>
21 
22 #define QUOTE(macro) stringifyName(macro)
23 #define stringifyName(name) #name
24 
25 namespace libMesh
26 {
27 template <typename>
28 class NumericVector;
29 template <typename>
30 class SparseMatrix;
31 
32 // This was deprecated in libMesh a year ago! It was obsolete 5 years
33 // ago! How are 6 apps in CI still using it!?
34 #ifdef LIBMESH_ENABLE_DEPRECATED
35 template <typename T>
36 using UniquePtr = std::unique_ptr<T>;
37 #endif
38 }
39 
40 class ActionFactory;
41 class Factory;
42 class MooseEnumItem;
43 class ExecFlagEnum;
45 
46 namespace hit
47 {
48 class Node;
49 }
50 
55 
61 namespace Moose
62 {
63 static_assert(__cplusplus >= 201703L,
64  "MOOSE requires a C++17 compatible compiler (GCC >= 7.5.0, Clang >= 5.0.2). Please "
65  "update your compiler or, if compatible, add '-std=c++17' to your compiler flags "
66  "and try again. If using the MOOSE conda package, please attempt a MOOSE environment "
67  "update (using `mamba update moose-dev`). If this update is not successful, please "
68  "create a new MOOSE environment (see "
69  "https://mooseframework.inl.gov/getting_started/installation/"
70  "conda.html#uninstall-conda-moose-environment).");
71 }
72 
79 #define parallel_if \
80  (cond) bool __local_bool__ = (cond); \
81  Parallel::max<bool>(__local_bool__); \
82  if (__local_bool__)
83 
87 #ifdef __bg__ // On Blue Gene Architectures there is no underscore
88 #define FORTRAN_CALL(name) name
89 #else // One underscore everywhere else
90 #define FORTRAN_CALL(name) name##_
91 #endif
92 
100 template <class M, class... Args>
101 std::pair<typename M::iterator, bool>
102 moose_try_emplace(M & m, const typename M::key_type & k, Args &&... args)
103 {
104  auto it = m.lower_bound(k);
105  if (it == m.end() || m.key_comp()(k, it->first))
106  {
107  return {m.emplace_hint(it,
108  std::piecewise_construct,
109  std::forward_as_tuple(k),
110  std::forward_as_tuple(std::forward<Args>(args)...)),
111  true};
112  }
113  return {it, false};
114 }
115 
116 // forward declarations
117 class Syntax;
118 class FEProblemBase;
119 
120 // Define MOOSE execution flags, this cannot be done in MooseTypes because the registration calls
121 // must be in Moose.C to remain consistent with other registration calls.
123 extern const ExecFlagType EXEC_NONE;
124 extern const ExecFlagType EXEC_INITIAL;
126 extern const ExecFlagType EXEC_LINEAR;
128 extern const ExecFlagType EXEC_NONLINEAR;
129 extern const ExecFlagType EXEC_POSTCHECK;
130 extern const ExecFlagType EXEC_TIMESTEP_END;
131 extern const ExecFlagType EXEC_TIMESTEP_BEGIN;
136 extern const ExecFlagType EXEC_FINAL;
137 extern const ExecFlagType EXEC_FORCED;
138 extern const ExecFlagType EXEC_FAILED;
139 extern const ExecFlagType EXEC_CUSTOM;
140 extern const ExecFlagType EXEC_SUBDOMAIN;
141 extern const ExecFlagType EXEC_PRE_DISPLACE;
144 extern const ExecFlagType EXEC_TRANSFER;
145 extern const ExecFlagType EXEC_PRE_KERNELS;
146 extern const ExecFlagType EXEC_ALWAYS;
147 #ifdef LIBMESH_ENABLE_AMR
149 #endif
150 
151 namespace Moose
152 {
153 // MOOSE is not tested with LIBMESH_DIM != 3
154 static_assert(LIBMESH_DIM == 3,
155  "MOOSE must be built with a libmesh library compiled without --enable-1D-only "
156  "or --enable-2D-only");
157 
163 static constexpr std::size_t dim = LIBMESH_DIM;
164 
166 inline constexpr std::size_t invalid_size_t = std::numeric_limits<std::size_t>::max();
167 
172 extern int interrupt_signal_number;
173 
178 extern bool show_multiple;
179 
187 
191 extern bool _trap_fpe;
192 
196 extern bool _warnings_are_errors;
197 
201 extern bool _deprecated_is_error;
202 
209 extern bool _throw_on_error;
210 
215 extern bool _throw_on_warning;
216 
223 
227 #define COLOR_BLACK (Moose::colorConsole() ? XTERM_BLACK : "")
228 #define COLOR_RED (Moose::colorConsole() ? XTERM_RED : "")
229 #define COLOR_GREEN (Moose::colorConsole() ? XTERM_GREEN : "")
230 #define COLOR_YELLOW (Moose::colorConsole() ? XTERM_YELLOW : "")
231 #define COLOR_BLUE (Moose::colorConsole() ? XTERM_BLUE : "")
232 #define COLOR_MAGENTA (Moose::colorConsole() ? XTERM_MAGENTA : "")
233 #define COLOR_CYAN (Moose::colorConsole() ? XTERM_CYAN : "")
234 #define COLOR_WHITE (Moose::colorConsole() ? XTERM_WHITE : "")
235 #define COLOR_DEFAULT (Moose::colorConsole() ? XTERM_DEFAULT : "")
236 
238 bool colorConsole();
239 
242 bool setColorConsole(bool use_color, bool force = false);
243 
247 using libMesh::err;
248 using libMesh::out;
249 
254 void registerAll(Factory & f, ActionFactory & af, Syntax & s);
255 
256 void registerObjects(Factory & factory, const std::set<std::string> & obj_labels);
257 void addActionTypes(Syntax & syntax);
258 void registerActions(Syntax & syntax, ActionFactory & action_factory);
259 void registerActions(Syntax & syntax,
260  ActionFactory & action_factory,
261  const std::set<std::string> & obj_labels);
262 
263 void associateSyntax(Syntax & syntax, ActionFactory & action_factory);
264 
265 void setSolverDefaults(FEProblemBase & problem);
266 
271 MPI_Comm swapLibMeshComm(MPI_Comm new_comm);
272 
274 {
275 public:
278  ScopedCommSwapper(MPI_Comm new_comm) : _orig(swapLibMeshComm(new_comm)) {}
285 
286 private:
287  MPI_Comm _orig;
288 };
289 
296 {
297 public:
302 
307  ScopedThrowOnError(const bool throw_on_error);
308 
314 
315 private:
318 };
319 
326 {
327 public:
332 
337  ScopedDeprecatedIsError(const bool deprecated_is_error);
338 
344 
345 private:
348 };
349 
353 std::string hitMessagePrefix(const hit::Node & node);
354 
355 // MOOSE Requires PETSc to run, this CPP check will cause a compile error if PETSc is not found
356 #ifndef LIBMESH_HAVE_PETSC
357 #error PETSc has not been detected, please ensure your environment is set up properly then rerun the libmesh build script and try to compile MOOSE again.
358 #endif
359 
360 } // namespace Moose
361 
362 // If we are using MFEM, in addition to the checks we do as part of the build system in moose.mk,
363 // we check at compile time if both MOOSE and MFEM were built in dbg mode or not
364 #ifdef MOOSE_MFEM_ENABLED
365 #include "libmesh/ignore_warnings.h"
366 #include <mfem.hpp>
367 #include "libmesh/restore_warnings.h"
368 #ifdef MFEM_DEBUG
369 static_assert(std::string_view(QUOTE(METHOD)) == "dbg",
370  "MFEM was built in dbg mode, but not MOOSE. Try reinstalling "
371  "MFEM using the scripts/update_and_rebuild_mfem.sh script.");
372 #else
373 static_assert(std::string_view(QUOTE(METHOD)) != "dbg",
374  "MOOSE was built in dbg mode, but not MFEM. Try reinstalling "
375  "MFEM using the scripts/update_and_rebuild_mfem.sh script.");
376 #endif
377 #endif
const ExecFlagType EXEC_TRANSFER
Definition: Moose.C:55
const ExecFlagType EXEC_FAILED
Definition: Moose.C:48
ScopedCommSwapper(MPI_Comm new_comm)
Swaps the current libmesh MPI communicator for new_comm.
Definition: Moose.h:278
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:21
bool colorConsole()
Returns whether Console coloring is turned on (default: true).
Definition: Moose.C:794
const ExecFlagType EXEC_NONLINEAR_CONVERGENCE
Definition: Moose.C:34
const ExecFlagType EXEC_CUSTOM
Definition: Moose.C:49
MPI_Comm swapLibMeshComm(MPI_Comm new_comm)
Swap the libMesh MPI communicator out for ours.
Definition: Moose.C:784
const ExecFlagType EXEC_FORCED
Definition: Moose.C:47
Generic factory class for build all sorts of objects.
Definition: Factory.h:28
void setSolverDefaults(FEProblemBase &problem)
Definition: Moose.C:777
const ExecFlagType EXEC_LINEAR_CONVERGENCE
Definition: Moose.C:32
bool _warnings_are_errors
Variable to toggle any warning into an error (includes deprecated code warnings)
Definition: Moose.C:843
~ScopedDeprecatedIsError()
Destructor, which sets Moose::_deprecated_is_error to what it was upon construction.
Definition: Moose.C:826
void registerActions(Syntax &syntax, ActionFactory &action_factory)
Multiple Action class can be associated with a single input file section, in which case all associate...
Definition: Moose.C:523
const ExecFlagType EXEC_NONE
Definition: Moose.C:29
libMesh::PerfLog perf_log
Perflog to be used by applications.
const bool _throw_on_error_before
The value of Moose::_throw_on_error at construction.
Definition: Moose.h:317
ScopedDeprecatedIsError()
Default constructor, which sets Moose::_deprecated_is_error = true.
Definition: Moose.C:824
void associateSyntax(Syntax &syntax, ActionFactory &action_factory)
Definition: Moose.C:770
const bool _deprecated_is_error_before
The value of Moose::_throw_on_error at construction.
Definition: Moose.h:347
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition: Moose.h:163
const ExecFlagType EXEC_MULTIAPP_FIXED_POINT_END
Definition: Moose.C:40
This class provides an interface for common operations on field variables of both FE and FV types wit...
const ExecFlagType EXEC_TIMESTEP_END
Definition: Moose.C:36
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
const ExecFlagType EXEC_ALWAYS
Definition: Moose.C:51
std::pair< typename M::iterator, bool > moose_try_emplace(M &m, const typename M::key_type &k, Args &&... args)
Function to mirror the behavior of the C++17 std::map::try_emplace() method (no hint).
Definition: Moose.h:102
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
auto max(const L &left, const R &right)
std::unique_ptr< T > UniquePtr
Definition: Moose.h:36
void MooseVecView(libMesh::NumericVector< libMesh::Number > &vector)
ScopedThrowOnError()
Default constructor, which sets Moose::_throw_on_error = true.
Definition: Moose.C:813
void forceSwap()
Forcibly swap the currently swapped-out communicator back in to libmesh.
Definition: Moose.h:284
bool _deprecated_is_error
Variable to toggle only deprecated warnings as errors.
Definition: Moose.C:844
const ExecFlagType EXEC_TIMESTEP_BEGIN
Definition: Moose.C:37
const ExecFlagType EXEC_PRE_MULTIAPP_SETUP
Definition: Moose.C:54
~ScopedThrowOnError()
Destructor, which sets Moose::_throw_on_error to what it was upon construction.
Definition: Moose.C:815
Specialized factory for generic Action System objects.
Definition: ActionFactory.h:48
virtual ~ScopedCommSwapper()
Definition: Moose.h:279
OStreamProxy err(std::cerr)
const ExecFlagType EXEC_LINEAR
Definition: Moose.C:31
Definition: Moose.h:46
const ExecFlagType EXEC_PRE_DISPLACE
Definition: Moose.C:52
const ExecFlagType EXEC_POSTCHECK
Definition: Moose.C:35
const ExecFlagType EXEC_NONLINEAR
Definition: Moose.C:33
void MooseMatView(libMesh::SparseMatrix< libMesh::Number > &mat)
bool _trap_fpe
Variable indicating whether we will enable FPE trapping for this run.
bool show_multiple
Set to false (the default) to display an error message only once for each error call code location (a...
Definition: Moose.C:848
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
const ExecFlagType EXEC_MULTIAPP_FIXED_POINT_BEGIN
Definition: Moose.C:42
bool _throw_on_warning
Variable to turn on exceptions during mooseWarning(), should only be used in MOOSE unit tests...
Definition: Moose.C:846
void registerAll(Factory &f, ActionFactory &af, Syntax &s)
Register objects that are in MOOSE.
Definition: Moose.C:67
void registerObjects(Factory &factory, const std::set< std::string > &obj_labels)
Definition: Moose.C:77
bool setColorConsole(bool use_color, bool force=false)
Turns color escape sequences on/off for info written to stdout.
Definition: Moose.C:800
Scoped helper for setting Moose::_throw_on_error during this scope.
Definition: Moose.h:295
const ExecFlagType EXEC_MULTIAPP_FIXED_POINT_ITERATION_END
Definition: Moose.C:38
Holding syntax for parsing input files.
Definition: Syntax.h:21
const ExecFlagType EXEC_SAME_AS_MULTIAPP
Definition: Moose.C:53
int interrupt_signal_number
Used by the signal handler to determine if we should write a checkpoint file out at any point during ...
Definition: Moose.C:847
const ExecFlagType EXEC_PRE_KERNELS
Definition: Moose.C:56
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
const ExecFlagType EXEC_SUBDOMAIN
Definition: Moose.C:50
const ExecFlagType EXEC_MULTIAPP_FIXED_POINT_CONVERGENCE
Definition: Moose.C:44
std::string hitMessagePrefix(const hit::Node &node)
Get the prefix to be associated with a hit node for a message.
Definition: Moose.C:832
OStreamProxy out(std::cout)
bool _throw_on_error
Variable to turn on exceptions during mooseError(), should only be used within MOOSE unit tests or wh...
Definition: Moose.C:845
const ExecFlagType EXEC_FINAL
Definition: Moose.C:46
Scoped helper for setting Moose::_deprecated_is_error during this scope.
Definition: Moose.h:325
void addActionTypes(Syntax &syntax)
Definition: Moose.C:83
constexpr std::size_t invalid_size_t
Value for invalid size_t indices.
Definition: Moose.h:166
const ExecFlagType EXEC_POST_ADAPTIVITY
Definition: Moose.C:58
ExecFlagEnum execute_flags
Storage for the registered execute flags.
const ExecFlagType EXEC_INITIAL
Definition: Moose.C:30