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