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