LCOV - code coverage report
Current view: top level - src/Util - Util.H (source / functions) Hit Total Coverage
Test: coverage_merged.info Lines: 33 108 30.6 %
Date: 2024-11-18 05:28:54 Functions: 9 209 4.3 %

          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             : 
      13             : #include <string>
      14             : 
      15             : #include "AMReX.H"
      16             : #include <AMReX_ParallelDescriptor.H>
      17             : #include <AMReX_PlotFileUtil.H>
      18             : 
      19             : #include "Color.H"
      20             : #define INFO __FILE__, __func__, __LINE__
      21             : #define TEST(x) #x, x
      22             : 
      23             : 
      24             : #ifndef ALAMO_SINGLE_DEFINITION
      25             : #define ALAMO_SINGLE_DEFINITION AMREX_ATTRIBUTE_WEAK
      26             : #endif
      27             : 
      28             : 
      29             : /// \brief A collection of utility routines
      30             : namespace Util
      31             : {
      32             : /// \brief A collection of string operations
      33             : namespace String
      34             : {
      35             : int ReplaceAll(std::string &str, const std::string before, const std::string after);
      36             : int ReplaceAll(std::string &str, const char before, const std::string after);
      37             : std::string Wrap(std::string text, unsigned per_line);
      38             : std::string Join(std::vector<std::string> & vec, char separator = '_');
      39             : std::vector<std::string> Split(std::string &str, const char token = ' ');
      40             : bool Contains(std::string &str, const std::string find);
      41             : template<class T>
      42             : T Parse(std::string);
      43             : template<>
      44             : std::complex<int> Parse(std::string);
      45             : }
      46             : 
      47             : std::string GetFileName();
      48             : void CopyFileToOutputDir(std::string a_path, bool fullpath = true, std::string prefix = "");
      49             : 
      50             : std::pair<std::string,std::string> GetOverwrittenFile();
      51             : 
      52             : void SignalHandler(int s);
      53             : 
      54             : void Initialize (int argc, char* argv[]);
      55             : void Initialize ();
      56             : 
      57             : void Finalize ();
      58             : 
      59             : void Terminate(const char * msg, int signal, bool backtrace);
      60             : 
      61             : void Abort (const char * msg);
      62             : 
      63             : template<typename... Args>
      64             : AMREX_FORCE_INLINE
      65             : void Assert (std::string file, std::string func, int line, std::string smt, bool pass, Args const &... args)
      66             : {
      67    15483081 :     if (pass) return;
      68             : 
      69           0 :     std::ostringstream infostream;
      70           0 :     infostream << Color::Bold << Color::FG::Red << "ABORT("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ":   ";
      71           0 :     infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
      72           0 :     infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
      73           0 :     infostream << "Assertion failed: " << Color::BG::DarkGray << Color::Underlined << Color::Bold <<  Color::FG::LightGreen << smt << Color::Reset;
      74           0 :     std::string info = infostream.str();
      75             : 
      76             : 
      77           0 :     std::ostringstream messagestream;
      78             :     using List= int[];
      79           0 :     (void)List{0, ( (void)(messagestream << args), 0 ) ... };
      80           0 :     std::string message = messagestream.str();
      81           0 :     String::ReplaceAll(message,'\n',"\n"+info);
      82             : 
      83           0 :     std::cout << info << message << std::endl;
      84             : 
      85           0 :     Abort("Fatal Error");
      86             : }
      87             : 
      88             : 
      89             : template<typename... Args>
      90           0 : void Abort (std::string file, std::string func, int line, Args const &... args)
      91             : {
      92           0 :     if (amrex::ParallelDescriptor::IOProcessor())
      93             :     {
      94           0 :         std::ostringstream infostream;
      95           0 :         infostream << Color::Bold << Color::FG::Red << "ABORT" << Color::Reset << ":   ";
      96           0 :         infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
      97           0 :         infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
      98           0 :         std::string info = infostream.str();
      99             : 
     100           0 :         std::ostringstream messagestream;
     101             :         using List= int[];
     102           0 :         (void)List{0, ( (void)(messagestream << args), 0 ) ... };
     103           0 :         std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
     104             :         //std::string messageorg = message;
     105           0 :         String::ReplaceAll(message,'\n',"\n"+info);
     106           0 :         std::cout << info << message << std::endl;
     107             :     }
     108           0 :     Abort("Fatal Error");
     109           0 : }
     110             : 
     111             : template<typename... Args>
     112             : void ParallelAbort (std::string file, std::string func, int line, Args const &... args)
     113             : {
     114             : 
     115             :     std::ostringstream infostream;
     116             :     infostream << Color::Bold << Color::FG::Red << "ABORT("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ":   ";
     117             :     infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
     118             :     infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
     119             :     std::string info = infostream.str();
     120             : 
     121             :     std::ostringstream messagestream;
     122             :     using List= int[];
     123             :     (void)List{0, ( (void)(messagestream << args), 0 ) ... };
     124             :     std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
     125             :     //std::string messageorg = message;
     126             :     String::ReplaceAll(message,'\n',"\n"+info);
     127             :     std::cout << info << message << std::endl;
     128             : 
     129             :     Abort("Fatal Error");
     130             : }
     131             : 
     132             : template<typename... Args>
     133        2709 : void Message (std::string file, std::string func, int line, Args const &... args)
     134             : {
     135        2709 :     if (amrex::ParallelDescriptor::IOProcessor())
     136             :     {
     137        5418 :         std::ostringstream infostream;
     138        2709 :         infostream << Color::Bold << Color::FG::Blue << "MESSAGE" << Color::Reset << ": ";
     139        2709 :         infostream << Color::FG::LightBlue << file << Color::Reset << ":" << line << " ";
     140        2709 :         infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
     141        5418 :         std::string info = infostream.str();
     142             : 
     143        5418 :         std::ostringstream messagestream;
     144             :         using List= int[];
     145        2709 :         (void)List{0, ( (void)(messagestream << args), 0 ) ... };
     146        5418 :         std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
     147             : 
     148        2709 :         String::ReplaceAll(message,'\n',"\n"+info);
     149             : 
     150        2709 :         std::cout << info << message << std::endl;
     151             :     }
     152        2709 : }
     153             : 
     154             : template<typename... Args>
     155             : void ParallelMessage (std::string file, std::string func, int line, Args const &... args)
     156             : {
     157             :     std::ostringstream infostream;
     158             :     infostream << Color::Bold << Color::FG::Blue << "MESSAGE("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ": ";
     159             :     infostream << Color::FG::LightBlue << file << Color::Reset << ":" << line << " ";
     160             :     infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
     161             :     std::string info = infostream.str();
     162             :     std::ostringstream messagestream;
     163             :     using List= int[];
     164             :     (void)List{0, ( (void)(messagestream << args), 0 ) ... };
     165             :     std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
     166             :     String::ReplaceAll(message,'\n',"\n"+info);
     167             :     std::cout << info << message << std::endl;
     168             : }
     169             : 
     170             : template<typename... Args>
     171           4 : void Warning (std::string file, std::string func, int line, Args const &... args)
     172             : {
     173           4 :     if (amrex::ParallelDescriptor::IOProcessor())
     174             :     {
     175           8 :         std::ostringstream infostream;
     176           4 :         infostream << Color::Bold << Color::FG::Yellow << "WARNING" << Color::Reset << ": ";
     177           4 :         infostream << Color::FG::Yellow << file << Color::Reset << ":" << line << " ";
     178           4 :         infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
     179           8 :         std::string info = infostream.str();
     180             : 
     181           8 :         std::ostringstream messagestream;
     182             :         using List= int[];
     183           4 :         (void)List{0, ( (void)(messagestream << args), 0 ) ... };
     184           8 :         std::string message = messagestream.str();///String::Wrap(messagestream.str(),150);
     185             : 
     186           4 :         String::ReplaceAll(message,'\n',"\n"+info);
     187             : 
     188           4 :         std::cout << info << message << std::endl;
     189             :     }
     190           4 : }
     191             : 
     192             : 
     193             : template<typename... Args>
     194           0 : void Exception (std::string file, std::string func, int line, Args const &... args)
     195             : {
     196           0 :     if (amrex::ParallelDescriptor::IOProcessor())
     197             :     {
     198           0 :         std::ostringstream infostream;
     199           0 :         infostream << Color::Bold << Color::FG::Red << "EXCEPTION" << Color::Reset << ": ";
     200           0 :         infostream << Color::FG::LightGray << file << ":" << line << " (" << func << Color::Reset << ") ";
     201           0 :         std::string info = infostream.str();
     202             : 
     203           0 :         std::ostringstream messagestream;
     204           0 :         messagestream << Color::FG::Red;
     205             :         using List= int[];
     206           0 :         (void)List{0, ( (void)(messagestream << args), 0 ) ... };
     207           0 :         messagestream << Color::Reset;
     208             : 
     209           0 :         std::string message = messagestream.str();
     210             : 
     211           0 :         Util::String::ReplaceAll(message,'\n',"\n"+info);
     212             : 
     213           0 :         std::cout << info << message << std::endl;
     214             : 
     215           0 :         std::throw_with_nested(std::runtime_error("IO::Exception"));
     216             :     }
     217           0 : }
     218             : 
     219             : template<typename... Args>
     220             : AMREX_FORCE_INLINE
     221             : void AssertException (std::string file, std::string func, int line, std::string smt, bool pass, Args const &... args)
     222             : {
     223           0 :     if (pass) return;
     224             : 
     225           0 :     if (amrex::ParallelDescriptor::IOProcessor())
     226             :     {
     227           0 :         std::ostringstream infostream;
     228           0 :         infostream << Color::Bold << Color::FG::Red << "ABORT("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ":   ";
     229           0 :         infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
     230           0 :         infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
     231           0 :         infostream << "Assertion failed: " << Color::BG::DarkGray << Color::Underlined << Color::Bold <<  Color::FG::LightGreen << smt << Color::Reset;
     232           0 :         std::string info = infostream.str();
     233             : 
     234             : 
     235           0 :         std::ostringstream messagestream;
     236             :         using List= int[];
     237           0 :         (void)List{0, ( (void)(messagestream << args), 0 ) ... };
     238           0 :         std::string message = messagestream.str();
     239           0 :         String::ReplaceAll(message,'\n',"\n"+info);
     240             : 
     241           0 :         std::cout << info << message << std::endl;
     242             : 
     243           0 :         Exception(file,func,line,"Fatal Error");
     244             :     }
     245             : }
     246             : 
     247             : 
     248             : 
     249             : template<typename... Args>
     250           0 : void ParmParseException (   std::string file, std::string func, int line,
     251             :                             std::string file2, std::string /*func2*/, int line2,
     252             :                             std::string fullname,
     253             :                             Args const &... args)
     254             : {
     255           0 :     if (amrex::ParallelDescriptor::IOProcessor())
     256             :     {
     257           0 :         std::ostringstream infostream;
     258           0 :         infostream << Color::Bold << Color::FG::Red << "EXCEPTION" << Color::Reset << ": ";
     259           0 :         infostream << Color::FG::LightGray << file << ":" << line << " (" << func << Color::Reset << ") ";
     260           0 :         std::string info = infostream.str();
     261             : 
     262           0 :         std::ostringstream messagestream;
     263           0 :         messagestream << Color::FG::Red;
     264             :         using List= int[];
     265           0 :         (void)List{0, ( (void)(messagestream << args), 0 ) ... };
     266           0 :         messagestream << Color::Reset;
     267             : 
     268           0 :         const std::size_t nargs = sizeof...(Args);
     269             : 
     270           0 :         if (nargs) messagestream << "\n";
     271           0 :         messagestream << "while reading " << Color::FG::Yellow << fullname << Color::Reset;
     272           0 :         if (line2 >= 0)
     273             :         {
     274           0 :             messagestream << " at " << file2 << ":" << line2;
     275             :         }
     276             : 
     277           0 :         std::string message = messagestream.str();
     278             : 
     279           0 :         Util::String::ReplaceAll(message,'\n',"\n"+info);
     280             : 
     281           0 :         std::cout << info << message << std::endl;
     282             : 
     283           0 :         std::throw_with_nested(std::runtime_error("IO::ParmParse Exception"));
     284             :     }
     285           0 : }
     286             : 
     287             : 
     288             : std::pair<std::string,std::string>
     289             : CreateCleanDirectory (const std::string &path, bool callbarrier = true);
     290             : 
     291             : 
     292             : /// \brief A collection of I/O routines for Unit Tests
     293             : namespace Test
     294             : {
     295             : int Message(std::string testname);
     296             : int Message(std::string testname, int passed);
     297             : int SubMessage(std::string testname, int passed);
     298             : int SubFinalMessage(int failed);
     299             : }
     300             : 
     301             : //
     302             : // These two functions are not really necessary anymore.
     303             : // TODO: remove RealFillBoundary and all references to them.
     304             : //
     305             : template<class T>
     306             : AMREX_FORCE_INLINE
     307             : void RealFillBoundary(amrex::FabArray<amrex::BaseFab<T>> &a_mf,const amrex::Geometry &/*a_geom*/)
     308             : {
     309             :     BL_PROFILE("Util::RealFillBoundary");
     310        6220 :     a_mf.setMultiGhost(true);
     311        6220 :     a_mf.FillBoundary();
     312        6220 : }
     313             : AMREX_FORCE_INLINE
     314             : void RealFillBoundary(amrex::MultiFab &a_mf, const amrex::Geometry &/*a_geom*/)
     315             : {
     316             :     BL_PROFILE("Util::RealFillBoundary");
     317      442766 :     a_mf.setMultiGhost(true);
     318      442766 :     a_mf.FillBoundary();
     319      442766 : }
     320             : 
     321             : 
     322             : void AverageCellcenterToNode(amrex::MultiFab& node_mf, const int &dcomp, const amrex::MultiFab &cell_mf, const int &scomp, const int &ncomp/*, const int ngrow=0*/);
     323             : 
     324             : 
     325             : 
     326             : 
     327             : }
     328             : #endif

Generated by: LCOV version 1.14