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