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