LCOV - code coverage report
Current view: top level - src/Util - Util.H (source / functions) Coverage Total Hit
Test: coverage_merged.info Lines: 26.7 % 150 40
Test Date: 2026-02-24 04:46:08 Functions: 5.1 % 294 15

            Line data    Source code
       1              : #ifndef UTIL_UTIL_H
       2              : #define UTIL_UTIL_H
       3              : 
       4              : #include <iostream>
       5              : #include <iomanip>
       6              : #include <sys/ioctl.h>
       7              : #include <unistd.h>
       8              : #include <iterator>
       9              : #include <algorithm>
      10              : #include <complex>
      11              : #include <stdlib.h>
      12              : #include <string_view>
      13              : 
      14              : #include <string>
      15              : #include "Util/String.H"
      16              : 
      17              : #include "AMReX.H"
      18              : #include <AMReX_ParallelDescriptor.H>
      19              : #include <AMReX_PlotFileUtil.H>
      20              : #include <AMReX_iMultiFab.H>
      21              : #include <AMReX_Reduce.H>
      22              : 
      23              : #include "Color.H"
      24              : #define INFO __FILE__, __func__, __LINE__
      25              : #define TEST(x) #x, x
      26              : 
      27              : 
      28              : #ifndef ALAMO_SINGLE_DEFINITION
      29              : #define ALAMO_SINGLE_DEFINITION AMREX_ATTRIBUTE_WEAK
      30              : #endif
      31              : 
      32              : namespace Util
      33              : {
      34              : 
      35              : extern std::string globalprefix;
      36              : extern bool initialized;
      37              : extern bool finalized;
      38              : 
      39              : 
      40              : std::string GetFileName();
      41              : void CopyFileToOutputDir(std::string a_path, bool fullpath = true, std::string prefix = "");
      42              : 
      43              : std::pair<std::string,std::string> GetOverwrittenFile();
      44              : 
      45              : void SignalHandler(int s);
      46              : 
      47              : void Initialize (int argc, char* argv[]);
      48              : void Initialize ();
      49              : 
      50              : void Finalize ();
      51              : 
      52              : void Terminate(const char * msg, int signal, bool backtrace);
      53              : 
      54              : void Abort (const char * msg);
      55              : 
      56              : template<typename... Args>
      57              : AMREX_FORCE_INLINE
      58              : void Assert (std::string file, std::string func, int line, std::string smt, bool pass, Args const &... args)
      59              : {
      60      9751744 :     if (pass) return;
      61              : 
      62            0 :     std::ostringstream infostream;
      63            0 :     infostream << globalprefix;
      64            0 :     infostream << Color::Bold << Color::FG::Red << "ABORT("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ":   ";
      65            0 :     infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
      66            0 :     infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
      67            0 :     infostream << "Assertion failed: " << Color::BG::DarkGray << Color::Underlined << Color::Bold <<  Color::FG::LightGreen << smt << Color::Reset;
      68            0 :     std::string info = infostream.str();
      69              : 
      70              : 
      71            0 :     std::ostringstream messagestream;
      72              :     using List= int[];
      73            0 :     (void)List{0, ( (void)(messagestream << args), 0 ) ... };
      74            0 :     std::string message = messagestream.str();
      75            0 :     Util::String::ReplaceAll(message,'\n',"\n"+info);
      76              : 
      77            0 :     std::cout << info << message << std::endl;
      78              : 
      79            0 :     Abort("Fatal Error");
      80            0 : }
      81              : 
      82              : 
      83              : template<typename... Args>
      84            0 : void Abort (std::string file, std::string func, int line, Args const &... args)
      85              : {
      86            0 :     if (amrex::ParallelDescriptor::IOProcessor())
      87              :     {
      88            0 :         std::ostringstream infostream;
      89            0 :         infostream << globalprefix;
      90            0 :         infostream << Color::Bold << Color::FG::Red << "ABORT" << Color::Reset << ":   ";
      91            0 :         infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
      92            0 :         infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
      93            0 :         std::string info = infostream.str();
      94              : 
      95            0 :         std::ostringstream messagestream;
      96              :         using List= int[];
      97            0 :         (void)List{0, ( (void)(messagestream << args), 0 ) ... };
      98            0 :         std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
      99              :         //std::string messageorg = message;
     100            0 :         Util::String::ReplaceAll(message,'\n',"\n"+info);
     101            0 :         std::cout << info << message << std::endl;
     102            0 :     }
     103            0 :     Abort("Fatal Error");
     104            0 : }
     105              : 
     106              : template<typename... Args>
     107            0 : void ParallelAbort (std::string file, std::string func, int line, Args const &... args)
     108              : {
     109              : 
     110            0 :     std::ostringstream infostream;
     111            0 :     infostream << globalprefix;
     112            0 :     infostream << Color::Bold << Color::FG::Red << "ABORT("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ":   ";
     113            0 :     infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
     114            0 :     infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
     115            0 :     std::string info = infostream.str();
     116              : 
     117            0 :     std::ostringstream messagestream;
     118              :     using List= int[];
     119            0 :     (void)List{0, ( (void)(messagestream << args), 0 ) ... };
     120            0 :     std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
     121              :     //std::string messageorg = message;
     122            0 :     Util::String::ReplaceAll(message,'\n',"\n"+info);
     123            0 :     std::cout << info << message << std::endl;
     124              : 
     125            0 :     Abort("Fatal Error");
     126            0 : }
     127              : 
     128              : template<typename... Args>
     129         3334 : void Message (std::string file, std::string func, int line, Args const &... args)
     130              : {
     131         3334 :     if (amrex::ParallelDescriptor::IOProcessor())
     132              :     {
     133         3334 :         std::ostringstream infostream;
     134         3334 :         infostream << globalprefix;
     135         3334 :         infostream << Color::Bold << Color::FG::Blue << "MESSAGE" << Color::Reset << ": ";
     136         3334 :         infostream << Color::FG::LightBlue << file << Color::Reset << ":" << line << " ";
     137         3334 :         infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
     138         3334 :         std::string info = infostream.str();
     139              : 
     140         3334 :         std::ostringstream messagestream;
     141              :         using List= int[];
     142         3334 :         (void)List{0, ( (void)(messagestream << args), 0 ) ... };
     143         3334 :         std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
     144              : 
     145         6668 :         Util::String::ReplaceAll(message,'\n',"\n"+info);
     146              : 
     147         3334 :         std::cout << info << message << std::endl;
     148         3334 :     }
     149         3334 : }
     150              : 
     151              : #ifdef AMREX_DEBUG
     152              : template<typename... Args>
     153              : void DebugMessage (std::string file, std::string func, int line, Args const &... args)
     154              : {
     155              :     if (amrex::ParallelDescriptor::IOProcessor())
     156              :     {
     157              :         std::ostringstream infostream;
     158              :         infostream << globalprefix;
     159              :         infostream << Color::Bold << Color::FG::Blue << "MESSAGE" << Color::Reset << ": ";
     160              :         infostream << Color::FG::LightBlue << file << Color::Reset << ":" << line << " ";
     161              :         infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
     162              :         std::string info = infostream.str();
     163              : 
     164              :         std::ostringstream messagestream;
     165              :         using List= int[];
     166              :         (void)List{0, ( (void)(messagestream << args), 0 ) ... };
     167              :         std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
     168              : 
     169              :         Util::String::ReplaceAll(message,'\n',"\n"+info);
     170              : 
     171              :         std::cout << info << message << std::endl;
     172              :     }
     173              : }
     174              : #else
     175              : template<typename... Args>
     176          806 : void DebugMessage (std::string, std::string, int, Args const &... )
     177              : {
     178          806 : }
     179              : #endif
     180              : 
     181              : 
     182              : 
     183              : 
     184              : template<typename... Args>
     185            0 : void ParallelMessage (std::string file, std::string func, int line, Args const &... args)
     186              : {
     187            0 :     std::ostringstream infostream;
     188            0 :     infostream << globalprefix;
     189            0 :     infostream << Color::Bold << Color::FG::Blue << "MESSAGE("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ": ";
     190            0 :     infostream << Color::FG::LightBlue << file << Color::Reset << ":" << line << " ";
     191            0 :     infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
     192            0 :     std::string info = infostream.str();
     193            0 :     std::ostringstream messagestream;
     194              :     using List= int[];
     195            0 :     (void)List{0, ( (void)(messagestream << args), 0 ) ... };
     196            0 :     std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
     197            0 :     Util::String::ReplaceAll(message,'\n',"\n"+info);
     198            0 :     std::cout << info << message << std::endl;
     199            0 : }
     200              : 
     201              : template<typename... Args>
     202            6 : void Warning (std::string file, std::string func, int line, Args const &... args)
     203              : {
     204            6 :     if (amrex::ParallelDescriptor::IOProcessor())
     205              :     {
     206            6 :         std::ostringstream infostream;
     207            6 :         infostream << globalprefix;
     208            6 :         infostream << Color::Bold << Color::FG::Yellow << "WARNING" << Color::Reset << ": ";
     209            6 :         infostream << Color::FG::Yellow << file << Color::Reset << ":" << line << " ";
     210            6 :         infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
     211            6 :         std::string info = infostream.str();
     212              : 
     213            6 :         std::ostringstream messagestream;
     214              :         using List= int[];
     215            6 :         (void)List{0, ( (void)(messagestream << args), 0 ) ... };
     216            6 :         std::string message = messagestream.str();///String::Wrap(messagestream.str(),150);
     217              : 
     218           12 :         Util::String::ReplaceAll(message,'\n',"\n"+info);
     219              : 
     220            6 :         std::cout << info << message << std::endl;
     221            6 :     }
     222            6 : }
     223              : 
     224              : 
     225              : template<typename... Args>
     226            0 : void Exception (std::string file, std::string func, int line, Args const &... args)
     227              : {
     228            0 :     if (amrex::ParallelDescriptor::IOProcessor())
     229              :     {
     230            0 :         std::ostringstream infostream;
     231            0 :         infostream << globalprefix;
     232            0 :         infostream << Color::Bold << Color::FG::Red << "EXCEPTION" << Color::Reset << ": ";
     233            0 :         infostream << Color::FG::LightGray << file << ":" << line << " (" << func << Color::Reset << ") ";
     234            0 :         std::string info = infostream.str();
     235              : 
     236            0 :         std::ostringstream messagestream;
     237            0 :         messagestream << Color::FG::Red;
     238              :         using List= int[];
     239            0 :         (void)List{0, ( (void)(messagestream << args), 0 ) ... };
     240            0 :         messagestream << Color::Reset;
     241              : 
     242            0 :         std::string message = messagestream.str();
     243              : 
     244            0 :         Util::String::ReplaceAll(message,'\n',"\n"+info);
     245              : 
     246            0 :         std::cout << info << message << std::endl;
     247              : 
     248            0 :         std::throw_with_nested(std::runtime_error("IO::Exception"));
     249            0 :     }
     250            0 : }
     251              : 
     252              : template<typename... Args>
     253              : AMREX_FORCE_INLINE
     254              : void AssertException (std::string file, std::string func, int line, std::string smt, bool pass, Args const &... args)
     255              : {
     256           84 :     if (pass) return;
     257              : 
     258            0 :     if (amrex::ParallelDescriptor::IOProcessor())
     259              :     {
     260            0 :         std::ostringstream infostream;
     261            0 :         infostream << globalprefix;
     262            0 :         infostream << Color::Bold << Color::FG::Red << "ABORT("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ":   ";
     263            0 :         infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
     264            0 :         infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
     265            0 :         infostream << "Assertion failed: " << Color::BG::DarkGray << Color::Underlined << Color::Bold <<  Color::FG::LightGreen << smt << Color::Reset;
     266            0 :         std::string info = infostream.str();
     267              : 
     268              : 
     269            0 :         std::ostringstream messagestream;
     270              :         using List= int[];
     271            0 :         (void)List{0, ( (void)(messagestream << args), 0 ) ... };
     272            0 :         std::string message = messagestream.str();
     273            0 :         Util::String::ReplaceAll(message,'\n',"\n"+info);
     274              : 
     275            0 :         std::cout << info << message << std::endl;
     276              : 
     277            0 :         Exception(file,func,line,"Fatal Error");
     278            0 :     }
     279              : }
     280              : 
     281              : 
     282              : 
     283              : template<typename... Args>
     284            0 : void ParmParseException (   std::string file, std::string func, int line,
     285              :                             std::string fullname,
     286              :                             Args const &... args)
     287              : {
     288            0 :     if (amrex::ParallelDescriptor::IOProcessor())
     289              :     {
     290            0 :         std::ostringstream infostream;
     291            0 :         infostream << globalprefix;
     292            0 :         infostream << Color::Bold << Color::FG::Red << "EXCEPTION" << Color::Reset << ": ";
     293            0 :         infostream << Color::FG::LightGray << file << ":" << line << " (" << func << Color::Reset << ") ";
     294            0 :         std::string info = infostream.str();
     295              : 
     296            0 :         std::ostringstream messagestream;
     297            0 :         messagestream << Color::FG::Red;
     298              :         using List= int[];
     299            0 :         (void)List{0, ( (void)(messagestream << args), 0 ) ... };
     300            0 :         messagestream << Color::Reset;
     301              : 
     302            0 :         const std::size_t nargs = sizeof...(Args);
     303              : 
     304            0 :         if (nargs) messagestream << "\n";
     305            0 :         if (fullname != "")
     306            0 :             messagestream << "while reading " << Color::FG::Yellow << fullname << Color::Reset;
     307              : 
     308            0 :         std::string message = messagestream.str();
     309              : 
     310            0 :         Util::String::ReplaceAll(message,'\n',"\n"+info);
     311              : 
     312            0 :         std::cout << info << message << std::endl;
     313              : 
     314            0 :         std::throw_with_nested(std::runtime_error("IO::ParmParse Exception"));
     315            0 :     }
     316            0 : }
     317              : 
     318              : 
     319              : std::pair<std::string,std::string>
     320              : CreateCleanDirectory (const std::string &path, bool callbarrier = true);
     321              : 
     322              : 
     323              : /// \brief A collection of I/O routines for Unit Tests
     324              : namespace Test
     325              : {
     326              : int Message(std::string testname);
     327              : int Message(std::string testname, int passed);
     328              : int SubMessage(std::string testname, int passed);
     329              : void SubWarning(std::string testname);
     330              : int SubFinalMessage(int failed);
     331              : }
     332              : 
     333              : //
     334              : // These two functions are not really necessary anymore.
     335              : // TODO: remove RealFillBoundary and all references to them.
     336              : //
     337              : template<class T>
     338              : AMREX_FORCE_INLINE
     339              : void RealFillBoundary(amrex::FabArray<amrex::BaseFab<T>> &a_mf,const amrex::Geometry &/*a_geom*/, const int nghost=2)
     340              : {
     341              :     // We want the default to setMultiGhost(true) if nghost isn't explicitly passed, so nghost should default to a number greater than 1
     342              :     BL_PROFILE("Util::RealFillBoundary");
     343              :     if (nghost <= 1) {}
     344          572 :     else a_mf.setMultiGhost(true);
     345          572 :     a_mf.FillBoundary();
     346          572 : }
     347              : AMREX_FORCE_INLINE
     348              : void RealFillBoundary(amrex::MultiFab &a_mf, const amrex::Geometry &/*a_geom*/, const int nghost=2)
     349              : {
     350              :     // We want the default to setMultiGhost(true) if nghost isn't explicitly passed, so nghost should default to a number greater than 1
     351              :     BL_PROFILE("Util::RealFillBoundary");
     352              :     if (nghost <= 1) {}
     353            2 :     else a_mf.setMultiGhost(true);
     354            2 :     a_mf.FillBoundary();
     355            2 : }
     356              : 
     357              : void AverageCellcenterToNode(amrex::MultiFab& node_mf, const int &dcomp, const amrex::MultiFab &cell_mf, const int &scomp, const int &ncomp/*, const int ngrow=0*/);
     358              : 
     359              : // Prevent compiler from complaining about unused variables
     360              : template <class... Ts>
     361              : AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
     362              : void IgnoreUnused (const Ts&...) {}
     363              : 
     364              : }
     365              : 
     366              : #endif
        

Generated by: LCOV version 2.0-1