LCOV - code coverage report
Current view: top level - include/base - Moose.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: d5e406 Lines: 9 9 100.0 %
Date: 2026-07-08 21:15:44 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

          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 <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;
      44             : class MooseVariableFieldBase;
      45             : 
      46             : namespace hit
      47             : {
      48             : class Node;
      49             : }
      50             : 
      51             : void MooseVecView(libMesh::NumericVector<libMesh::Number> & vector);
      52             : void MooseVecView(const libMesh::NumericVector<libMesh::Number> & vector);
      53             : void MooseMatView(libMesh::SparseMatrix<libMesh::Number> & mat);
      54             : void MooseMatView(const libMesh::SparseMatrix<libMesh::Number> & mat);
      55             : 
      56             : /**
      57             :  * MOOSE now contains C++17 code, so give a reasonable error message
      58             :  * stating what the user can do to address this in their environment if C++17
      59             :  * compatibility isn't found.
      60             :  */
      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             : 
      73             : /**
      74             :  * Testing a condition on a local CPU that need to be propagated across all processes.
      75             :  *
      76             :  * If the condition 'cond' is satisfied, it gets propagated across all processes, so the parallel
      77             :  * code take the same path (if that is requires).
      78             :  */
      79             : #define parallel_if                                                                                \
      80             :   (cond) bool __local_bool__ = (cond);                                                             \
      81             :   Parallel::max<bool>(__local_bool__);                                                             \
      82             :   if (__local_bool__)
      83             : 
      84             : /**
      85             :  * Wrap all fortran function calls in this.
      86             :  */
      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             : 
      93             : /**
      94             :  * Function to mirror the behavior of the C++17 std::map::try_emplace() method (no hint).
      95             :  * @param m The std::map
      96             :  * @param k The key use to insert the pair
      97             :  * @param args The value to be inserted. This can be a moveable type but won't be moved
      98             :  *             if the insertion is successful.
      99             :  */
     100             : template <class M, class... Args>
     101             : std::pair<typename M::iterator, bool>
     102       44594 : moose_try_emplace(M & m, const typename M::key_type & k, Args &&... args)
     103             : {
     104       44594 :   auto it = m.lower_bound(k);
     105       44594 :   if (it == m.end() || m.key_comp()(k, it->first))
     106             :   {
     107        5559 :     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        5559 :             true};
     112             :   }
     113       39035 :   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.
     122             : using ExecFlagType = MooseEnumItem;
     123             : extern const ExecFlagType EXEC_NONE;
     124             : extern const ExecFlagType EXEC_INITIAL;
     125             : extern const ExecFlagType EXEC_LINEAR_CONVERGENCE;
     126             : extern const ExecFlagType EXEC_LINEAR;
     127             : extern const ExecFlagType EXEC_NONLINEAR_CONVERGENCE;
     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;
     132             : extern const ExecFlagType EXEC_MULTIAPP_FIXED_POINT_ITERATION_END;
     133             : extern const ExecFlagType EXEC_MULTIAPP_FIXED_POINT_BEGIN;
     134             : extern const ExecFlagType EXEC_MULTIAPP_FIXED_POINT_END;
     135             : extern const ExecFlagType EXEC_MULTIAPP_FIXED_POINT_CONVERGENCE;
     136             : extern const ExecFlagType EXEC_MULTISYSTEM_FIXED_POINT_CONVERGENCE;
     137             : extern const ExecFlagType EXEC_FINAL;
     138             : extern const ExecFlagType EXEC_FORCED;
     139             : extern const ExecFlagType EXEC_FAILED;
     140             : extern const ExecFlagType EXEC_CUSTOM;
     141             : extern const ExecFlagType EXEC_SUBDOMAIN;
     142             : extern const ExecFlagType EXEC_PRE_DISPLACE;
     143             : extern const ExecFlagType EXEC_SAME_AS_MULTIAPP;
     144             : extern const ExecFlagType EXEC_PRE_MULTIAPP_SETUP;
     145             : extern const ExecFlagType EXEC_TRANSFER;
     146             : extern const ExecFlagType EXEC_PRE_KERNELS;
     147             : extern const ExecFlagType EXEC_ALWAYS;
     148             : #ifdef LIBMESH_ENABLE_AMR
     149             : extern const ExecFlagType EXEC_POST_ADAPTIVITY;
     150             : #endif
     151             : 
     152             : namespace Moose
     153             : {
     154             : // MOOSE is not tested with LIBMESH_DIM != 3
     155             : static_assert(LIBMESH_DIM == 3,
     156             :               "MOOSE must be built with a libmesh library compiled without --enable-1D-only "
     157             :               "or --enable-2D-only");
     158             : 
     159             : /**
     160             :  * This is the dimension of all vector and tensor datastructures used in MOOSE.
     161             :  * We enforce LIBMESH_DIM == 3 through a static assertion above.
     162             :  * Note that lower dimensional simulations embedded in 3D space can always be requested at runtime.
     163             :  */
     164             : static constexpr std::size_t dim = LIBMESH_DIM;
     165             : 
     166             : /// Value for invalid size_t indices
     167             : inline constexpr std::size_t invalid_size_t = std::numeric_limits<std::size_t>::max();
     168             : 
     169             : /**
     170             :  * Used by the signal handler to determine if we should write a checkpoint file out at any point
     171             :  * during operation.
     172             :  */
     173             : extern int interrupt_signal_number;
     174             : 
     175             : /**
     176             :  * Set to false (the default) to display an error message only once for each error call code
     177             :  * location (as opposed to every time the code is executed).
     178             :  */
     179             : extern bool show_multiple;
     180             : 
     181             : /**
     182             :  * Perflog to be used by applications.
     183             :  * If the application prints this in the end they will get performance info.
     184             :  *
     185             :  * This is no longer instantiated in the framework and will be removed in the future.
     186             :  */
     187             : extern libMesh::PerfLog perf_log;
     188             : 
     189             : /**
     190             :  * Variable indicating whether we will enable FPE trapping for this run.
     191             :  */
     192             : extern bool _trap_fpe;
     193             : 
     194             : /**
     195             :  * Variable to toggle any warning into an error (includes deprecated code warnings)
     196             :  */
     197             : extern bool _warnings_are_errors;
     198             : 
     199             : /**
     200             :  * Variable to toggle only deprecated warnings as errors.
     201             :  */
     202             : extern bool _deprecated_is_error;
     203             : 
     204             : /**
     205             :  * Variable to turn on exceptions during mooseError(), should only be used within MOOSE unit tests
     206             :  * or when about to perform threaded operations because exception throwing in threaded regions is
     207             :  * safe while aborting is inherently not when singletons are involved (e.g. what thread is
     208             :  * responsible for destruction, or what do you do about mutexes?)
     209             :  */
     210             : extern bool _throw_on_error;
     211             : 
     212             : /**
     213             :  * Variable to turn on exceptions during mooseWarning(), should
     214             :  * only be used in MOOSE unit tests.
     215             :  */
     216             : extern bool _throw_on_warning;
     217             : 
     218             : /**
     219             :  * Storage for the registered execute flags. This is needed for the ExecuteMooseObjectWarehouse
     220             :  * to create the necessary storage containers on a per flag basis. This isn't something that
     221             :  * should be used by application developers.
     222             :  */
     223             : extern ExecFlagEnum execute_flags;
     224             : 
     225             : /**
     226             :  * Macros for coloring any output stream (_console, std::ostringstream, etc.)
     227             :  */
     228             : #define COLOR_BLACK (Moose::colorConsole() ? XTERM_BLACK : "")
     229             : #define COLOR_RED (Moose::colorConsole() ? XTERM_RED : "")
     230             : #define COLOR_GREEN (Moose::colorConsole() ? XTERM_GREEN : "")
     231             : #define COLOR_YELLOW (Moose::colorConsole() ? XTERM_YELLOW : "")
     232             : #define COLOR_BLUE (Moose::colorConsole() ? XTERM_BLUE : "")
     233             : #define COLOR_MAGENTA (Moose::colorConsole() ? XTERM_MAGENTA : "")
     234             : #define COLOR_CYAN (Moose::colorConsole() ? XTERM_CYAN : "")
     235             : #define COLOR_WHITE (Moose::colorConsole() ? XTERM_WHITE : "")
     236             : #define COLOR_DEFAULT (Moose::colorConsole() ? XTERM_DEFAULT : "")
     237             : 
     238             : /// Returns whether Console coloring is turned on (default: true).
     239             : bool colorConsole();
     240             : 
     241             : /// Turns color escape sequences on/off for info written to stdout.
     242             : /// Returns the the set value which may be different than use_color.
     243             : bool setColorConsole(bool use_color, bool force = false);
     244             : 
     245             : /**
     246             :  * Import libMesh::out, and libMesh::err for use in MOOSE.
     247             :  */
     248             : using libMesh::err;
     249             : using libMesh::out;
     250             : 
     251             : /**
     252             :  * Register objects that are in MOOSE
     253             :  */
     254             : 
     255             : void registerAll(Factory & f, ActionFactory & af, Syntax & s);
     256             : 
     257             : void registerObjects(Factory & factory, const std::set<std::string> & obj_labels);
     258             : void addActionTypes(Syntax & syntax);
     259             : void registerActions(Syntax & syntax, ActionFactory & action_factory);
     260             : void registerActions(Syntax & syntax,
     261             :                      ActionFactory & action_factory,
     262             :                      const std::set<std::string> & obj_labels);
     263             : 
     264             : void associateSyntax(Syntax & syntax, ActionFactory & action_factory);
     265             : 
     266             : void setSolverDefaults(FEProblemBase & problem);
     267             : 
     268             : /**
     269             :  * Swap the libMesh MPI communicator out for ours.  Note that you should usually use
     270             :  * the Moose::ScopedCommSwapper class instead of calling this function.
     271             :  */
     272             : MPI_Comm swapLibMeshComm(MPI_Comm new_comm);
     273             : 
     274             : class ScopedCommSwapper
     275             : {
     276             : public:
     277             :   /// Swaps the current libmesh MPI communicator for new_comm.  new_comm will be automatically
     278             :   /// swapped back in as the current libmesh communicator when this object is destructed.
     279      585890 :   ScopedCommSwapper(MPI_Comm new_comm) : _orig(swapLibMeshComm(new_comm)) {}
     280      585815 :   virtual ~ScopedCommSwapper() { swapLibMeshComm(_orig); }
     281             :   /// Forcibly swap the currently swapped-out communicator back in to libmesh.  Calling this
     282             :   /// function twice in a row leaves communicators exactly as they were before this function
     283             :   /// was called.  Usually you should not need/use this function because MPI communicators
     284             :   /// are swapped automatically when this object is constructed/destructed.
     285       35507 :   void forceSwap() { _orig = swapLibMeshComm(_orig); }
     286             : 
     287             : private:
     288             :   MPI_Comm _orig;
     289             : };
     290             : 
     291             : /**
     292             :  * Scoped helper for setting Moose::_throw_on_error during this scope.
     293             :  *
     294             :  * Cannot be used within threads.
     295             :  */
     296             : class ScopedThrowOnError
     297             : {
     298             : public:
     299             :   /**
     300             :    * Default constructor, which sets Moose::_throw_on_error = true
     301             :    */
     302             :   ScopedThrowOnError();
     303             : 
     304             :   /**
     305             :    * Specialized constructor, which sets Moose::_throw_on_error
     306             :    * based on the argument \p throw_on_error
     307             :    */
     308             :   ScopedThrowOnError(const bool throw_on_error);
     309             : 
     310             :   /**
     311             :    * Destructor, which sets Moose::_throw_on_error to what it
     312             :    * was upon construction
     313             :    */
     314             :   ~ScopedThrowOnError();
     315             : 
     316             : private:
     317             :   /// The value of Moose::_throw_on_error at construction
     318             :   const bool _throw_on_error_before;
     319             : };
     320             : 
     321             : /**
     322             :  * Scoped helper for setting Moose::_deprecated_is_error during this scope.
     323             :  *
     324             :  * Cannot be used within threads.
     325             :  */
     326             : class ScopedDeprecatedIsError
     327             : {
     328             : public:
     329             :   /**
     330             :    * Default constructor, which sets Moose::_deprecated_is_error = true
     331             :    */
     332             :   ScopedDeprecatedIsError();
     333             : 
     334             :   /**
     335             :    * Specialized constructor, which sets Moose::_deprecated_is_error
     336             :    * based on the argument \p deprecated_is_error
     337             :    */
     338             :   ScopedDeprecatedIsError(const bool deprecated_is_error);
     339             : 
     340             :   /**
     341             :    * Destructor, which sets Moose::_deprecated_is_error to what it
     342             :    * was upon construction
     343             :    */
     344             :   ~ScopedDeprecatedIsError();
     345             : 
     346             : private:
     347             :   /// The value of Moose::_throw_on_error at construction
     348             :   const bool _deprecated_is_error_before;
     349             : };
     350             : 
     351             : /**
     352             :  * Get the prefix to be associated with a hit node for a message
     353             :  */
     354             : std::string hitMessagePrefix(const hit::Node & node);
     355             : 
     356             : // MOOSE Requires PETSc to run, this CPP check will cause a compile error if PETSc is not found
     357             : #ifndef LIBMESH_HAVE_PETSC
     358             : #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.
     359             : #endif
     360             : 
     361             : } // namespace Moose
     362             : 
     363             : // If we are using MFEM, in addition to the checks we do as part of the build system in moose.mk,
     364             : // we check at compile time if both MOOSE and MFEM were built in dbg mode or not
     365             : #ifdef MOOSE_MFEM_ENABLED
     366             : #include "libmesh/ignore_warnings.h"
     367             : #include <mfem.hpp>
     368             : #include "libmesh/restore_warnings.h"
     369             : #ifdef MFEM_DEBUG
     370             : static_assert(std::string_view(QUOTE(METHOD)) == "dbg",
     371             :               "MFEM was built in dbg mode, but not MOOSE. Try reinstalling "
     372             :               "MFEM using the scripts/update_and_rebuild_mfem.sh script.");
     373             : #else
     374             : static_assert(std::string_view(QUOTE(METHOD)) != "dbg",
     375             :               "MOOSE was built in dbg mode, but not MFEM. Try reinstalling "
     376             :               "MFEM using the scripts/update_and_rebuild_mfem.sh script.");
     377             : #endif
     378             : #endif

Generated by: LCOV version 1.14