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