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