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 : #pragma once 11 : 12 : #include "libMeshReducedNamespace.h" 13 : #include "libmesh/perf_log.h" 14 : #include "libmesh/libmesh_common.h" 15 : #include "XTermConstants.h" 16 : 17 : #include <memory> 18 : #include <set> 19 : #include <string> 20 : 21 : namespace libMesh 22 : { 23 : template <typename> 24 : class NumericVector; 25 : template <typename> 26 : class SparseMatrix; 27 : 28 : // This was deprecated in libMesh a year ago! It was obsolete 5 years 29 : // ago! How are 6 apps in CI still using it!? 30 : #ifdef LIBMESH_ENABLE_DEPRECATED 31 : template <typename T> 32 : using UniquePtr = std::unique_ptr<T>; 33 : #endif 34 : } 35 : 36 : class ActionFactory; 37 : class Factory; 38 : class MooseEnumItem; 39 : class ExecFlagEnum; 40 : class MooseVariableFieldBase; 41 : 42 : void MooseVecView(libMesh::NumericVector<libMesh::Number> & vector); 43 : void MooseVecView(const libMesh::NumericVector<libMesh::Number> & vector); 44 : void MooseMatView(libMesh::SparseMatrix<libMesh::Number> & mat); 45 : void MooseMatView(const libMesh::SparseMatrix<libMesh::Number> & mat); 46 : 47 : /** 48 : * MOOSE now contains C++17 code, so give a reasonable error message 49 : * stating what the user can do to address this in their environment if C++17 50 : * compatibility isn't found. 51 : */ 52 : namespace Moose 53 : { 54 : static_assert(__cplusplus >= 201703L, 55 : "MOOSE requires a C++17 compatible compiler (GCC >= 7.5.0, Clang >= 5.0.2). Please " 56 : "update your compiler or, if compatible, add '-std=c++17' to your compiler flags " 57 : "and try again. If using the MOOSE conda package, please attempt a MOOSE environment " 58 : "update (using `mamba update moose-dev`). If this update is not successful, please " 59 : "create a new MOOSE environment (see " 60 : "https://mooseframework.inl.gov/getting_started/installation/" 61 : "conda.html#uninstall-conda-moose-environment)."); 62 : } 63 : 64 : /** 65 : * Testing a condition on a local CPU that need to be propagated across all processes. 66 : * 67 : * If the condition 'cond' is satisfied, it gets propagated across all processes, so the parallel 68 : * code take the same path (if that is requires). 69 : */ 70 : #define parallel_if \ 71 : (cond) bool __local_bool__ = (cond); \ 72 : Parallel::max<bool>(__local_bool__); \ 73 : if (__local_bool__) 74 : 75 : /** 76 : * Wrap all fortran function calls in this. 77 : */ 78 : #ifdef __bg__ // On Blue Gene Architectures there is no underscore 79 : #define FORTRAN_CALL(name) name 80 : #else // One underscore everywhere else 81 : #define FORTRAN_CALL(name) name##_ 82 : #endif 83 : 84 : /** 85 : * Function to mirror the behavior of the C++17 std::map::try_emplace() method (no hint). 86 : * @param m The std::map 87 : * @param k The key use to insert the pair 88 : * @param args The value to be inserted. This can be a moveable type but won't be moved 89 : * if the insertion is successful. 90 : */ 91 : template <class M, class... Args> 92 : std::pair<typename M::iterator, bool> 93 42479 : moose_try_emplace(M & m, const typename M::key_type & k, Args &&... args) 94 : { 95 42479 : auto it = m.lower_bound(k); 96 42479 : if (it == m.end() || m.key_comp()(k, it->first)) 97 : { 98 4800 : return {m.emplace_hint(it, 99 : std::piecewise_construct, 100 : std::forward_as_tuple(k), 101 : std::forward_as_tuple(std::forward<Args>(args)...)), 102 4800 : true}; 103 : } 104 37679 : return {it, false}; 105 : } 106 : 107 : // forward declarations 108 : class Syntax; 109 : class FEProblemBase; 110 : 111 : // Define MOOSE execution flags, this cannot be done in MooseTypes because the registration calls 112 : // must be in Moose.C to remain consistent with other registration calls. 113 : using ExecFlagType = MooseEnumItem; 114 : extern const ExecFlagType EXEC_NONE; 115 : extern const ExecFlagType EXEC_INITIAL; 116 : extern const ExecFlagType EXEC_LINEAR_CONVERGENCE; 117 : extern const ExecFlagType EXEC_LINEAR; 118 : extern const ExecFlagType EXEC_NONLINEAR_CONVERGENCE; 119 : extern const ExecFlagType EXEC_NONLINEAR; 120 : extern const ExecFlagType EXEC_POSTCHECK; 121 : extern const ExecFlagType EXEC_TIMESTEP_END; 122 : extern const ExecFlagType EXEC_TIMESTEP_BEGIN; 123 : extern const ExecFlagType EXEC_MULTIAPP_FIXED_POINT_ITERATION_END; 124 : extern const ExecFlagType EXEC_MULTIAPP_FIXED_POINT_BEGIN; 125 : extern const ExecFlagType EXEC_MULTIAPP_FIXED_POINT_END; 126 : extern const ExecFlagType EXEC_MULTIAPP_FIXED_POINT_CONVERGENCE; 127 : extern const ExecFlagType EXEC_FINAL; 128 : extern const ExecFlagType EXEC_FORCED; 129 : extern const ExecFlagType EXEC_FAILED; 130 : extern const ExecFlagType EXEC_CUSTOM; 131 : extern const ExecFlagType EXEC_SUBDOMAIN; 132 : extern const ExecFlagType EXEC_PRE_DISPLACE; 133 : extern const ExecFlagType EXEC_SAME_AS_MULTIAPP; 134 : extern const ExecFlagType EXEC_PRE_MULTIAPP_SETUP; 135 : extern const ExecFlagType EXEC_TRANSFER; 136 : extern const ExecFlagType EXEC_PRE_KERNELS; 137 : extern const ExecFlagType EXEC_ALWAYS; 138 : #ifdef LIBMESH_ENABLE_AMR 139 : extern const ExecFlagType EXEC_POST_ADAPTIVITY; 140 : #endif 141 : 142 : namespace Moose 143 : { 144 : // MOOSE is not tested with LIBMESH_DIM != 3 145 : static_assert(LIBMESH_DIM == 3, 146 : "MOOSE must be built with a libmesh library compiled without --enable-1D-only " 147 : "or --enable-2D-only"); 148 : 149 : /** 150 : * This is the dimension of all vector and tensor datastructures used in MOOSE. 151 : * We enforce LIBMESH_DIM == 3 through a static assertion above. 152 : * Note that lower dimensional simulations embedded in 3D space can always be requested at runtime. 153 : */ 154 : static constexpr std::size_t dim = LIBMESH_DIM; 155 : 156 : /** 157 : * Used by the signal handler to determine if we should write a checkpoint file out at any point 158 : * during operation. 159 : */ 160 : extern int interrupt_signal_number; 161 : 162 : /** 163 : * Set to true (the default) to print the stack trace with error and warning 164 : * messages - false to omit it. 165 : */ 166 : extern bool show_trace; 167 : 168 : /** 169 : * Set to false (the default) to display an error message only once for each error call code 170 : * location (as opposed to every time the code is executed). 171 : */ 172 : extern bool show_multiple; 173 : 174 : /** 175 : * Perflog to be used by applications. 176 : * If the application prints this in the end they will get performance info. 177 : * 178 : * This is no longer instantiated in the framework and will be removed in the future. 179 : */ 180 : extern libMesh::PerfLog perf_log; 181 : 182 : /** 183 : * Variable indicating whether we will enable FPE trapping for this run. 184 : */ 185 : extern bool _trap_fpe; 186 : 187 : /** 188 : * Variable to toggle any warning into an error (includes deprecated code warnings) 189 : */ 190 : extern bool _warnings_are_errors; 191 : 192 : /** 193 : * Variable to toggle only deprecated warnings as errors. 194 : */ 195 : extern bool _deprecated_is_error; 196 : 197 : /** 198 : * Variable to turn on exceptions during mooseError(), should only be used within MOOSE unit tests 199 : * or when about to perform threaded operations because exception throwing in threaded regions is 200 : * safe while aborting is inherently not when singletons are involved (e.g. what thread is 201 : * responsible for destruction, or what do you do about mutexes?) 202 : */ 203 : extern bool _throw_on_error; 204 : 205 : /** 206 : * Variable to turn on exceptions during mooseWarning(), should 207 : * only be used in MOOSE unit tests. 208 : */ 209 : extern bool _throw_on_warning; 210 : 211 : /** 212 : * Storage for the registered execute flags. This is needed for the ExecuteMooseObjectWarehouse 213 : * to create the necessary storage containers on a per flag basis. This isn't something that 214 : * should be used by application developers. 215 : */ 216 : extern ExecFlagEnum execute_flags; 217 : 218 : /** 219 : * Macros for coloring any output stream (_console, std::ostringstream, etc.) 220 : */ 221 : #define COLOR_BLACK (Moose::colorConsole() ? XTERM_BLACK : "") 222 : #define COLOR_RED (Moose::colorConsole() ? XTERM_RED : "") 223 : #define COLOR_GREEN (Moose::colorConsole() ? XTERM_GREEN : "") 224 : #define COLOR_YELLOW (Moose::colorConsole() ? XTERM_YELLOW : "") 225 : #define COLOR_BLUE (Moose::colorConsole() ? XTERM_BLUE : "") 226 : #define COLOR_MAGENTA (Moose::colorConsole() ? XTERM_MAGENTA : "") 227 : #define COLOR_CYAN (Moose::colorConsole() ? XTERM_CYAN : "") 228 : #define COLOR_WHITE (Moose::colorConsole() ? XTERM_WHITE : "") 229 : #define COLOR_DEFAULT (Moose::colorConsole() ? XTERM_DEFAULT : "") 230 : 231 : /// Returns whether Console coloring is turned on (default: true). 232 : bool colorConsole(); 233 : 234 : /// Turns color escape sequences on/off for info written to stdout. 235 : /// Returns the the set value which may be different than use_color. 236 : bool setColorConsole(bool use_color, bool force = false); 237 : 238 : /** 239 : * Import libMesh::out, and libMesh::err for use in MOOSE. 240 : */ 241 : using libMesh::err; 242 : using libMesh::out; 243 : 244 : /** 245 : * Register objects that are in MOOSE 246 : */ 247 : 248 : void registerAll(Factory & f, ActionFactory & af, Syntax & s); 249 : 250 : void registerObjects(Factory & factory, const std::set<std::string> & obj_labels); 251 : void addActionTypes(Syntax & syntax); 252 : void registerActions(Syntax & syntax, ActionFactory & action_factory); 253 : void registerActions(Syntax & syntax, 254 : ActionFactory & action_factory, 255 : const std::set<std::string> & obj_labels); 256 : 257 : void associateSyntax(Syntax & syntax, ActionFactory & action_factory); 258 : 259 : void setSolverDefaults(FEProblemBase & problem); 260 : 261 : /** 262 : * Swap the libMesh MPI communicator out for ours. Note that you should usually use 263 : * the Moose::ScopedCommSwapper class instead of calling this function. 264 : */ 265 : MPI_Comm swapLibMeshComm(MPI_Comm new_comm); 266 : 267 : class ScopedCommSwapper 268 : { 269 : public: 270 : /// Swaps the current libmesh MPI communicator for new_comm. new_comm will be automatically 271 : /// swapped back in as the current libmesh communicator when this object is destructed. 272 641802 : ScopedCommSwapper(MPI_Comm new_comm) : _orig(swapLibMeshComm(new_comm)) {} 273 641703 : virtual ~ScopedCommSwapper() { swapLibMeshComm(_orig); } 274 : /// Forcibly swap the currently swapped-out communicator back in to libmesh. Calling this 275 : /// function twice in a row leaves communicators exactly as they were before this function 276 : /// was called. Usually you should not need/use this function because MPI communicators 277 : /// are swapped automatically when this object is constructed/destructed. 278 39948 : void forceSwap() { _orig = swapLibMeshComm(_orig); } 279 : 280 : private: 281 : MPI_Comm _orig; 282 : }; 283 : 284 : // MOOSE Requires PETSc to run, this CPP check will cause a compile error if PETSc is not found 285 : #ifndef LIBMESH_HAVE_PETSC 286 : #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. 287 : #endif 288 : 289 : } // namespace Moose