Alamo
Util.H
Go to the documentation of this file.
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  if (pass) return;
68 
69  std::ostringstream infostream;
70  infostream << Color::Bold << Color::FG::Red << "ABORT("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ": ";
71  infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
72  infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
73  infostream << "Assertion failed: " << Color::BG::DarkGray << Color::Underlined << Color::Bold << Color::FG::LightGreen << smt << Color::Reset;
74  std::string info = infostream.str();
75 
76 
77  std::ostringstream messagestream;
78  using List= int[];
79  (void)List{0, ( (void)(messagestream << args), 0 ) ... };
80  std::string message = messagestream.str();
81  String::ReplaceAll(message,'\n',"\n"+info);
82 
83  std::cout << info << message << std::endl;
84 
85  Abort("Fatal Error");
86 }
87 
88 
89 template<typename... Args>
90 void Abort (std::string file, std::string func, int line, Args const &... args)
91 {
92  if (amrex::ParallelDescriptor::IOProcessor())
93  {
94  std::ostringstream infostream;
95  infostream << Color::Bold << Color::FG::Red << "ABORT" << Color::Reset << ": ";
96  infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
97  infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
98  std::string info = infostream.str();
99 
100  std::ostringstream messagestream;
101  using List= int[];
102  (void)List{0, ( (void)(messagestream << args), 0 ) ... };
103  std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
104  //std::string messageorg = message;
105  String::ReplaceAll(message,'\n',"\n"+info);
106  std::cout << info << message << std::endl;
107  }
108  Abort("Fatal Error");
109 }
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 void Message (std::string file, std::string func, int line, Args const &... args)
134 {
135  if (amrex::ParallelDescriptor::IOProcessor())
136  {
137  std::ostringstream infostream;
138  infostream << Color::Bold << Color::FG::Blue << "MESSAGE" << Color::Reset << ": ";
139  infostream << Color::FG::LightBlue << file << Color::Reset << ":" << line << " ";
140  infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
141  std::string info = infostream.str();
142 
143  std::ostringstream messagestream;
144  using List= int[];
145  (void)List{0, ( (void)(messagestream << args), 0 ) ... };
146  std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
147 
148  String::ReplaceAll(message,'\n',"\n"+info);
149 
150  std::cout << info << message << std::endl;
151  }
152 }
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 void Warning (std::string file, std::string func, int line, Args const &... args)
172 {
173  if (amrex::ParallelDescriptor::IOProcessor())
174  {
175  std::ostringstream infostream;
176  infostream << Color::Bold << Color::FG::Yellow << "WARNING" << Color::Reset << ": ";
177  infostream << Color::FG::Yellow << file << Color::Reset << ":" << line << " ";
178  infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
179  std::string info = infostream.str();
180 
181  std::ostringstream messagestream;
182  using List= int[];
183  (void)List{0, ( (void)(messagestream << args), 0 ) ... };
184  std::string message = messagestream.str();///String::Wrap(messagestream.str(),150);
185 
186  String::ReplaceAll(message,'\n',"\n"+info);
187 
188  std::cout << info << message << std::endl;
189  }
190 }
191 
192 
193 template<typename... Args>
194 void Exception (std::string file, std::string func, int line, Args const &... args)
195 {
196  if (amrex::ParallelDescriptor::IOProcessor())
197  {
198  std::ostringstream infostream;
199  infostream << Color::Bold << Color::FG::Red << "EXCEPTION" << Color::Reset << ": ";
200  infostream << Color::FG::LightGray << file << ":" << line << " (" << func << Color::Reset << ") ";
201  std::string info = infostream.str();
202 
203  std::ostringstream messagestream;
204  messagestream << Color::FG::Red;
205  using List= int[];
206  (void)List{0, ( (void)(messagestream << args), 0 ) ... };
207  messagestream << Color::Reset;
208 
209  std::string message = messagestream.str();
210 
211  Util::String::ReplaceAll(message,'\n',"\n"+info);
212 
213  std::cout << info << message << std::endl;
214 
215  std::throw_with_nested(std::runtime_error("IO::Exception"));
216  }
217 }
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  if (pass) return;
224 
225  if (amrex::ParallelDescriptor::IOProcessor())
226  {
227  std::ostringstream infostream;
228  infostream << Color::Bold << Color::FG::Red << "ABORT("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ": ";
229  infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
230  infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
231  infostream << "Assertion failed: " << Color::BG::DarkGray << Color::Underlined << Color::Bold << Color::FG::LightGreen << smt << Color::Reset;
232  std::string info = infostream.str();
233 
234 
235  std::ostringstream messagestream;
236  using List= int[];
237  (void)List{0, ( (void)(messagestream << args), 0 ) ... };
238  std::string message = messagestream.str();
239  String::ReplaceAll(message,'\n',"\n"+info);
240 
241  std::cout << info << message << std::endl;
242 
243  Exception(file,func,line,"Fatal Error");
244  }
245 }
246 
247 
248 
249 template<typename... Args>
250 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  if (amrex::ParallelDescriptor::IOProcessor())
256  {
257  std::ostringstream infostream;
258  infostream << Color::Bold << Color::FG::Red << "EXCEPTION" << Color::Reset << ": ";
259  infostream << Color::FG::LightGray << file << ":" << line << " (" << func << Color::Reset << ") ";
260  std::string info = infostream.str();
261 
262  std::ostringstream messagestream;
263  messagestream << Color::FG::Red;
264  using List= int[];
265  (void)List{0, ( (void)(messagestream << args), 0 ) ... };
266  messagestream << Color::Reset;
267 
268  const std::size_t nargs = sizeof...(Args);
269 
270  if (nargs) messagestream << "\n";
271  messagestream << "while reading " << Color::FG::Yellow << fullname << Color::Reset;
272  if (line2 >= 0)
273  {
274  messagestream << " at " << file2 << ":" << line2;
275  }
276 
277  std::string message = messagestream.str();
278 
279  Util::String::ReplaceAll(message,'\n',"\n"+info);
280 
281  std::cout << info << message << std::endl;
282 
283  std::throw_with_nested(std::runtime_error("IO::ParmParse Exception"));
284  }
285 }
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  a_mf.setMultiGhost(true);
311  a_mf.FillBoundary();
312 }
313 AMREX_FORCE_INLINE
314 void RealFillBoundary(amrex::MultiFab &a_mf, const amrex::Geometry &/*a_geom*/)
315 {
316  BL_PROFILE("Util::RealFillBoundary");
317  a_mf.setMultiGhost(true);
318  a_mf.FillBoundary();
319 }
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
Util::Initialize
void Initialize()
Definition: Util.cpp:125
Util::RealFillBoundary
AMREX_FORCE_INLINE void RealFillBoundary(amrex::FabArray< amrex::BaseFab< T >> &a_mf, const amrex::Geometry &)
Definition: Util.H:307
Util::String::Parse
std::complex< int > Parse(std::string input)
Definition: Util.cpp:308
Util::GetFileName
std::string GetFileName()
Definition: Util.cpp:22
Util::Test::Message
int Message(std::string testname)
Definition: Util.cpp:337
Color::FG::Red
static std::string Red
Definition: Color.H:20
Util::AverageCellcenterToNode
void AverageCellcenterToNode(amrex::MultiFab &node_mf, const int &dcomp, const amrex::MultiFab &cell_mf, const int &scomp, const int &ncomp)
Definition: Util.cpp:404
Util::String::Split
std::vector< std::string > Split(std::string &str, const char delim)
Definition: Util.cpp:291
Color::FG::Yellow
static std::string Yellow
Definition: Color.H:22
Color::FG::LightBlue
static std::string LightBlue
Definition: Color.H:31
Util::String::ReplaceAll
int ReplaceAll(std::string &str, const std::string before, const std::string after)
Definition: Util.cpp:224
Util::ParmParseException
void ParmParseException(std::string file, std::string func, int line, std::string file2, std::string, int line2, std::string fullname, Args const &... args)
Definition: Util.H:250
Util::CreateCleanDirectory
std::pair< std::string, std::string > CreateCleanDirectory(const std::string &path, bool callbarrier)
Definition: Util.cpp:174
Util::AssertException
AMREX_FORCE_INLINE void AssertException(std::string file, std::string func, int line, std::string smt, bool pass, Args const &... args)
Definition: Util.H:221
Util
A collection of utility routines.
Definition: Set.cpp:7
Util::Finalize
void Finalize()
Definition: Util.cpp:154
Util::Terminate
void Terminate(const char *, int signal, bool)
Definition: Util.cpp:168
Util::ParallelAbort
void ParallelAbort(std::string file, std::string func, int line, Args const &... args)
Definition: Util.H:112
Util::Test::SubFinalMessage
int SubFinalMessage(int failed)
Definition: Util.cpp:385
Color::FG::LightGreen
static std::string LightGreen
Definition: Color.H:29
Util::GetOverwrittenFile
std::pair< std::string, std::string > GetOverwrittenFile()
Definition: Util.cpp:91
Util::Exception
void Exception(std::string file, std::string func, int line, Args const &... args)
Definition: Util.H:194
Util::Assert
AMREX_FORCE_INLINE void Assert(std::string file, std::string func, int line, std::string smt, bool pass, Args const &... args)
Definition: Util.H:65
Color::Bold
static std::string Bold
Definition: Color.H:9
Color::BG::DarkGray
static std::string DarkGray
Definition: Color.H:47
Util::Abort
void Abort(const char *msg)
Definition: Util.cpp:165
Util::ParallelMessage
void ParallelMessage(std::string file, std::string func, int line, Args const &... args)
Definition: Util.H:155
Color::Reset
static std::string Reset
Definition: Color.H:8
Util::String::Wrap
std::string Wrap(std::string text, unsigned per_line)
Definition: Util.cpp:255
Util::SignalHandler
void SignalHandler(int s)
Definition: Util.cpp:96
Util::Warning
void Warning(std::string file, std::string func, int line, Args const &... args)
Definition: Util.H:171
Util::CopyFileToOutputDir
void CopyFileToOutputDir(std::string a_path, bool fullpath, std::string prefix)
Definition: Util.cpp:49
Color::Underlined
static std::string Underlined
Definition: Color.H:11
Util::Test::SubMessage
int SubMessage(std::string testname, int failed)
Definition: Util.cpp:364
Color::FG::LightGray
static std::string LightGray
Definition: Color.H:26
Test
Definition: GB.H:7
Util::String::Contains
bool Contains(std::string &str, const std::string find)
Definition: Util.cpp:301
Color::FG::Blue
static std::string Blue
Definition: Color.H:23
Util::String::Join
std::string Join(std::vector< std::string > &vec, char separator)
Definition: Util.cpp:243
Color.H
Util::Message
void Message(std::string file, std::string func, int line, Args const &... args)
Definition: Util.H:133