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