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