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