www.mooseframework.org
Moose.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
12 #include "libmesh/perf_log.h"
13 #include "libmesh/libmesh_common.h"
14 #include "XTermConstants.h"
15 
16 #include <memory>
17 #include <set>
18 #include <string>
19 
20 namespace libMesh
21 {
22 template <typename>
23 class NumericVector;
24 template <typename>
25 class SparseMatrix;
26 
27 // This was deprecated in libMesh a year ago! It was obsolete 5 years
28 // ago! How are 6 apps in CI still using it!?
29 #ifdef LIBMESH_ENABLE_DEPRECATED
30 template <typename T>
31 using UniquePtr = std::unique_ptr<T>;
32 #endif
33 }
34 
35 using namespace libMesh;
36 
37 class ActionFactory;
38 class Factory;
39 class MooseEnumItem;
40 class ExecFlagEnum;
42 
43 void MooseVecView(NumericVector<Number> & vector);
44 void MooseVecView(const NumericVector<Number> & vector);
46 void MooseMatView(const SparseMatrix<Number> & mat);
47 
53 namespace Moose
54 {
55 static_assert(__cplusplus >= 201703L,
56  "MOOSE requires a C++17 compatible compiler (GCC >= 7.5.0, Clang >= 5.0.2). Please "
57  "update your compiler or, if compatible, add '-std=c++17' to your compiler flags "
58  "and try again. If using the MOOSE conda package, please attempt a MOOSE environment "
59  "update (using `mamba update moose-dev`). If this update is not successful, please "
60  "create a new MOOSE environment (see "
61  "https://mooseframework.inl.gov/getting_started/installation/"
62  "conda.html#uninstall-conda-moose-environment).");
63 }
64 
71 #define parallel_if \
72  (cond) bool __local_bool__ = (cond); \
73  Parallel::max<bool>(__local_bool__); \
74  if (__local_bool__)
75 
79 #ifdef __bg__ // On Blue Gene Architectures there is no underscore
80 #define FORTRAN_CALL(name) name
81 #else // One underscore everywhere else
82 #define FORTRAN_CALL(name) name##_
83 #endif
84 
92 template <class M, class... Args>
93 std::pair<typename M::iterator, bool>
94 moose_try_emplace(M & m, const typename M::key_type & k, Args &&... args)
95 {
96  auto it = m.lower_bound(k);
97  if (it == m.end() || m.key_comp()(k, it->first))
98  {
99  return {m.emplace_hint(it,
100  std::piecewise_construct,
101  std::forward_as_tuple(k),
102  std::forward_as_tuple(std::forward<Args>(args)...)),
103  true};
104  }
105  return {it, false};
106 }
107 
108 // forward declarations
109 class Syntax;
110 class FEProblemBase;
111 
112 // Define MOOSE execution flags, this cannot be done in MooseTypes because the registration calls
113 // must be in Moose.C to remain consistent with other registration calls.
115 extern const ExecFlagType EXEC_NONE;
116 extern const ExecFlagType EXEC_INITIAL;
117 extern const ExecFlagType EXEC_LINEAR;
118 extern const ExecFlagType EXEC_NONLINEAR;
119 extern const ExecFlagType EXEC_POSTCHECK;
120 extern const ExecFlagType EXEC_TIMESTEP_END;
121 extern const ExecFlagType EXEC_TIMESTEP_BEGIN;
124 extern const ExecFlagType EXEC_FINAL;
125 extern const ExecFlagType EXEC_FORCED;
126 extern const ExecFlagType EXEC_FAILED;
127 extern const ExecFlagType EXEC_CUSTOM;
128 extern const ExecFlagType EXEC_SUBDOMAIN;
129 extern const ExecFlagType EXEC_PRE_DISPLACE;
132 extern const ExecFlagType EXEC_TRANSFER;
133 extern const ExecFlagType EXEC_PRE_KERNELS;
134 extern const ExecFlagType EXEC_ALWAYS;
135 
136 namespace Moose
137 {
138 // MOOSE is not tested with LIBMESH_DIM != 3
139 static_assert(LIBMESH_DIM == 3,
140  "MOOSE must be built with a libmesh library compiled without --enable-1D-only "
141  "or --enable-2D-only");
142 
148 static constexpr std::size_t dim = LIBMESH_DIM;
149 
154 extern int interrupt_signal_number;
155 
160 extern bool show_trace;
161 
166 extern bool show_multiple;
167 
174 extern PerfLog perf_log;
175 
179 extern bool _trap_fpe;
180 
184 extern bool _warnings_are_errors;
185 
189 extern bool _deprecated_is_error;
190 
197 extern bool _throw_on_error;
198 
203 extern bool _throw_on_warning;
204 
211 
215 #define COLOR_BLACK (Moose::colorConsole() ? XTERM_BLACK : "")
216 #define COLOR_RED (Moose::colorConsole() ? XTERM_RED : "")
217 #define COLOR_GREEN (Moose::colorConsole() ? XTERM_GREEN : "")
218 #define COLOR_YELLOW (Moose::colorConsole() ? XTERM_YELLOW : "")
219 #define COLOR_BLUE (Moose::colorConsole() ? XTERM_BLUE : "")
220 #define COLOR_MAGENTA (Moose::colorConsole() ? XTERM_MAGENTA : "")
221 #define COLOR_CYAN (Moose::colorConsole() ? XTERM_CYAN : "")
222 #define COLOR_WHITE (Moose::colorConsole() ? XTERM_WHITE : "")
223 #define COLOR_DEFAULT (Moose::colorConsole() ? XTERM_DEFAULT : "")
224 
226 bool colorConsole();
227 
230 bool setColorConsole(bool use_color, bool force = false);
231 
235 using libMesh::err;
236 using libMesh::out;
237 
242 void registerAll(Factory & f, ActionFactory & af, Syntax & s);
243 
244 void registerObjects(Factory & factory);
245 void registerObjects(Factory & factory, const std::set<std::string> & obj_labels);
246 void addActionTypes(Syntax & syntax);
247 void registerActions(Syntax & syntax, ActionFactory & action_factory);
248 void registerActions(Syntax & syntax,
249  ActionFactory & action_factory,
250  const std::set<std::string> & obj_labels);
251 
252 void associateSyntax(Syntax & syntax, ActionFactory & action_factory);
253 
254 void setSolverDefaults(FEProblemBase & problem);
255 
260 MPI_Comm swapLibMeshComm(MPI_Comm new_comm);
261 
263 {
264 public:
267  ScopedCommSwapper(MPI_Comm new_comm) : _orig(swapLibMeshComm(new_comm)) {}
268  virtual ~ScopedCommSwapper() { swapLibMeshComm(_orig); }
273  void forceSwap() { _orig = swapLibMeshComm(_orig); }
274 
275 private:
276  MPI_Comm _orig;
277 };
278 
279 // MOOSE Requires PETSc to run, this CPP check will cause a compile error if PETSc is not found
280 #ifndef LIBMESH_HAVE_PETSC
281 #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.
282 #endif
283 
284 } // namespace Moose
const ExecFlagType EXEC_TRANSFER
Definition: Moose.C:47
bool show_trace
Set to true (the default) to print the stack trace with error and warning messages - false to omit it...
Definition: Moose.C:642
const ExecFlagType EXEC_FAILED
Definition: Moose.C:40
ScopedCommSwapper(MPI_Comm new_comm)
Swaps the current libmesh MPI communicator for new_comm.
Definition: Moose.h:267
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:625
const ExecFlagType EXEC_CUSTOM
Definition: Moose.C:41
MPI_Comm swapLibMeshComm(MPI_Comm new_comm)
Swap the libMesh MPI communicator out for ours.
Definition: Moose.C:615
const ExecFlagType EXEC_FORCED
Definition: Moose.C:39
void setSolverDefaults(FEProblemBase &problem)
Definition: Moose.C:608
bool _warnings_are_errors
Variable to toggle any warning into an error (includes deprecated code warnings)
Definition: Moose.C:637
const ExecFlagType EXEC_NONE
Definition: Moose.C:27
void associateSyntax(Syntax &syntax, ActionFactory &action_factory)
Definition: Moose.C:601
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition: Moose.h:148
const ExecFlagType EXEC_MULTIAPP_FIXED_POINT_END
Definition: Moose.C:34
void MooseVecView(NumericVector< Number > &vector)
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:32
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
const ExecFlagType EXEC_ALWAYS
Definition: Moose.C:43
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:94
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
std::unique_ptr< T > UniquePtr
Definition: Moose.h:31
void forceSwap()
Forcibly swap the currently swapped-out communicator back in to libmesh.
Definition: Moose.h:273
bool _deprecated_is_error
Variable to toggle only deprecated warnings as errors.
Definition: Moose.C:638
const ExecFlagType EXEC_TIMESTEP_BEGIN
Definition: Moose.C:33
const ExecFlagType EXEC_PRE_MULTIAPP_SETUP
Definition: Moose.C:46
Specialized factory for generic Action System objects.
Definition: ActionFactory.h:50
PerfLog perf_log
Perflog to be used by applications.
virtual ~ScopedCommSwapper()
Definition: Moose.h:268
OStreamProxy err(std::cerr)
const ExecFlagType EXEC_LINEAR
Definition: Moose.C:29
const ExecFlagType EXEC_PRE_DISPLACE
Definition: Moose.C:44
const ExecFlagType EXEC_POSTCHECK
Definition: Moose.C:31
const ExecFlagType EXEC_NONLINEAR
Definition: Moose.C:30
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:643
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
const ExecFlagType EXEC_MULTIAPP_FIXED_POINT_BEGIN
Definition: Moose.C:36
bool _throw_on_warning
Variable to turn on exceptions during mooseWarning(), should only be used in MOOSE unit tests...
Definition: Moose.C:640
void registerAll(Factory &f, ActionFactory &af, Syntax &s)
Register objects that are in MOOSE.
void registerObjects(Factory &factory, const std::set< std::string > &obj_labels)
bool setColorConsole(bool use_color, bool force=false)
Turns color escape sequences on/off for info written to stdout.
Definition: Moose.C:631
Holding syntax for parsing input files.
Definition: Syntax.h:21
const ExecFlagType EXEC_SAME_AS_MULTIAPP
Definition: Moose.C:45
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:641
const ExecFlagType EXEC_PRE_KERNELS
Definition: Moose.C:48
void MooseMatView(SparseMatrix< Number > &mat)
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:42
void registerActions(Syntax &syntax, ActionFactory &action_factory, const std::set< std::string > &obj_labels)
Definition: Moose.C:409
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:639
const ExecFlagType EXEC_FINAL
Definition: Moose.C:38
void addActionTypes(Syntax &syntax)
Definition: Moose.C:78
ExecFlagEnum execute_flags
Storage for the registered execute flags.
const ExecFlagType EXEC_INITIAL
Definition: Moose.C:28