Line data Source code
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 :
29 : namespace Util
30 : {
31 :
32 : extern std::string globalprefix;
33 : extern bool initialized;
34 : extern bool finalized;
35 :
36 :
37 : std::string GetFileName();
38 : void CopyFileToOutputDir(std::string a_path, bool fullpath = true, std::string prefix = "");
39 :
40 : std::pair<std::string,std::string> GetOverwrittenFile();
41 :
42 : void SignalHandler(int s);
43 :
44 : void Initialize (int argc, char* argv[]);
45 : void Initialize ();
46 :
47 : void Finalize ();
48 :
49 : void Terminate(const char * msg, int signal, bool backtrace);
50 :
51 : void Abort (const char * msg);
52 :
53 : template<typename... Args>
54 : AMREX_FORCE_INLINE
55 : void Assert (std::string file, std::string func, int line, std::string smt, bool pass, Args const &... args)
56 : {
57 15802370 : if (pass) return;
58 :
59 0 : std::ostringstream infostream;
60 0 : infostream << globalprefix;
61 0 : infostream << Color::Bold << Color::FG::Red << "ABORT("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ": ";
62 0 : infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
63 0 : infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
64 0 : infostream << "Assertion failed: " << Color::BG::DarkGray << Color::Underlined << Color::Bold << Color::FG::LightGreen << smt << Color::Reset;
65 0 : std::string info = infostream.str();
66 :
67 :
68 0 : std::ostringstream messagestream;
69 : using List= int[];
70 0 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
71 0 : std::string message = messagestream.str();
72 0 : Util::String::ReplaceAll(message,'\n',"\n"+info);
73 :
74 0 : std::cout << info << message << std::endl;
75 :
76 0 : Abort("Fatal Error");
77 0 : }
78 :
79 :
80 : template<typename... Args>
81 0 : void Abort (std::string file, std::string func, int line, Args const &... args)
82 : {
83 0 : if (amrex::ParallelDescriptor::IOProcessor())
84 : {
85 0 : std::ostringstream infostream;
86 0 : infostream << globalprefix;
87 0 : infostream << Color::Bold << Color::FG::Red << "ABORT" << Color::Reset << ": ";
88 0 : infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
89 0 : infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
90 0 : std::string info = infostream.str();
91 :
92 0 : std::ostringstream messagestream;
93 : using List= int[];
94 0 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
95 0 : std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
96 : //std::string messageorg = message;
97 0 : Util::String::ReplaceAll(message,'\n',"\n"+info);
98 0 : std::cout << info << message << std::endl;
99 0 : }
100 0 : Abort("Fatal Error");
101 0 : }
102 :
103 : template<typename... Args>
104 : void 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 :
125 : template<typename... Args>
126 2680 : void Message (std::string file, std::string func, int line, Args const &... args)
127 : {
128 2680 : if (amrex::ParallelDescriptor::IOProcessor())
129 : {
130 2680 : std::ostringstream infostream;
131 2680 : infostream << globalprefix;
132 2680 : infostream << Color::Bold << Color::FG::Blue << "MESSAGE" << Color::Reset << ": ";
133 2680 : infostream << Color::FG::LightBlue << file << Color::Reset << ":" << line << " ";
134 2680 : infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
135 2680 : std::string info = infostream.str();
136 :
137 2680 : std::ostringstream messagestream;
138 : using List= int[];
139 2680 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
140 2680 : std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
141 :
142 5360 : Util::String::ReplaceAll(message,'\n',"\n"+info);
143 :
144 2680 : std::cout << info << message << std::endl;
145 2680 : }
146 2680 : }
147 :
148 : #ifdef AMREX_DEBUG
149 : template<typename... Args>
150 : void 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
172 : template<typename... Args>
173 605 : void DebugMessage (std::string, std::string, int, Args const &... )
174 : {
175 605 : }
176 : #endif
177 :
178 :
179 :
180 :
181 : template<typename... Args>
182 0 : void ParallelMessage (std::string file, std::string func, int line, Args const &... args)
183 : {
184 0 : std::ostringstream infostream;
185 0 : infostream << globalprefix;
186 0 : infostream << Color::Bold << Color::FG::Blue << "MESSAGE("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ": ";
187 0 : infostream << Color::FG::LightBlue << file << Color::Reset << ":" << line << " ";
188 0 : infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
189 0 : std::string info = infostream.str();
190 0 : std::ostringstream messagestream;
191 : using List= int[];
192 0 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
193 0 : std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
194 0 : Util::String::ReplaceAll(message,'\n',"\n"+info);
195 0 : std::cout << info << message << std::endl;
196 0 : }
197 :
198 : template<typename... Args>
199 6 : void Warning (std::string file, std::string func, int line, Args const &... args)
200 : {
201 6 : if (amrex::ParallelDescriptor::IOProcessor())
202 : {
203 6 : std::ostringstream infostream;
204 6 : infostream << globalprefix;
205 6 : infostream << Color::Bold << Color::FG::Yellow << "WARNING" << Color::Reset << ": ";
206 6 : infostream << Color::FG::Yellow << file << Color::Reset << ":" << line << " ";
207 6 : infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
208 6 : std::string info = infostream.str();
209 :
210 6 : std::ostringstream messagestream;
211 : using List= int[];
212 6 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
213 6 : std::string message = messagestream.str();///String::Wrap(messagestream.str(),150);
214 :
215 12 : Util::String::ReplaceAll(message,'\n',"\n"+info);
216 :
217 6 : std::cout << info << message << std::endl;
218 6 : }
219 6 : }
220 :
221 :
222 : template<typename... Args>
223 0 : void Exception (std::string file, std::string func, int line, Args const &... args)
224 : {
225 0 : if (amrex::ParallelDescriptor::IOProcessor())
226 : {
227 0 : std::ostringstream infostream;
228 0 : infostream << globalprefix;
229 0 : infostream << Color::Bold << Color::FG::Red << "EXCEPTION" << Color::Reset << ": ";
230 0 : infostream << Color::FG::LightGray << file << ":" << line << " (" << func << Color::Reset << ") ";
231 0 : std::string info = infostream.str();
232 :
233 0 : std::ostringstream messagestream;
234 0 : messagestream << Color::FG::Red;
235 : using List= int[];
236 0 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
237 0 : messagestream << Color::Reset;
238 :
239 0 : std::string message = messagestream.str();
240 :
241 0 : Util::String::ReplaceAll(message,'\n',"\n"+info);
242 :
243 0 : std::cout << info << message << std::endl;
244 :
245 0 : std::throw_with_nested(std::runtime_error("IO::Exception"));
246 0 : }
247 0 : }
248 :
249 : template<typename... Args>
250 : AMREX_FORCE_INLINE
251 : void AssertException (std::string file, std::string func, int line, std::string smt, bool pass, Args const &... args)
252 : {
253 80 : if (pass) return;
254 :
255 0 : if (amrex::ParallelDescriptor::IOProcessor())
256 : {
257 0 : std::ostringstream infostream;
258 0 : infostream << globalprefix;
259 0 : infostream << Color::Bold << Color::FG::Red << "ABORT("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ": ";
260 0 : infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
261 0 : infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
262 0 : infostream << "Assertion failed: " << Color::BG::DarkGray << Color::Underlined << Color::Bold << Color::FG::LightGreen << smt << Color::Reset;
263 0 : std::string info = infostream.str();
264 :
265 :
266 0 : std::ostringstream messagestream;
267 : using List= int[];
268 0 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
269 0 : std::string message = messagestream.str();
270 0 : Util::String::ReplaceAll(message,'\n',"\n"+info);
271 :
272 0 : std::cout << info << message << std::endl;
273 :
274 0 : Exception(file,func,line,"Fatal Error");
275 0 : }
276 : }
277 :
278 :
279 :
280 : template<typename... Args>
281 0 : void ParmParseException ( std::string file, std::string func, int line,
282 : std::string fullname,
283 : Args const &... args)
284 : {
285 0 : if (amrex::ParallelDescriptor::IOProcessor())
286 : {
287 0 : std::ostringstream infostream;
288 0 : infostream << globalprefix;
289 0 : infostream << Color::Bold << Color::FG::Red << "EXCEPTION" << Color::Reset << ": ";
290 0 : infostream << Color::FG::LightGray << file << ":" << line << " (" << func << Color::Reset << ") ";
291 0 : std::string info = infostream.str();
292 :
293 0 : std::ostringstream messagestream;
294 0 : messagestream << Color::FG::Red;
295 : using List= int[];
296 0 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
297 0 : messagestream << Color::Reset;
298 :
299 0 : const std::size_t nargs = sizeof...(Args);
300 :
301 0 : if (nargs) messagestream << "\n";
302 0 : if (fullname != "")
303 0 : messagestream << "while reading " << Color::FG::Yellow << fullname << Color::Reset;
304 :
305 0 : std::string message = messagestream.str();
306 :
307 0 : Util::String::ReplaceAll(message,'\n',"\n"+info);
308 :
309 0 : std::cout << info << message << std::endl;
310 :
311 0 : std::throw_with_nested(std::runtime_error("IO::ParmParse Exception"));
312 0 : }
313 0 : }
314 :
315 :
316 : std::pair<std::string,std::string>
317 : CreateCleanDirectory (const std::string &path, bool callbarrier = true);
318 :
319 :
320 : /// \brief A collection of I/O routines for Unit Tests
321 : namespace Test
322 : {
323 : int Message(std::string testname);
324 : int Message(std::string testname, int passed);
325 : int SubMessage(std::string testname, int passed);
326 : int 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 : //
333 : template<class T>
334 : AMREX_FORCE_INLINE
335 : void RealFillBoundary(amrex::FabArray<amrex::BaseFab<T>> &a_mf,const amrex::Geometry &/*a_geom*/)
336 : {
337 : BL_PROFILE("Util::RealFillBoundary");
338 6220 : a_mf.setMultiGhost(true);
339 6220 : a_mf.FillBoundary();
340 6220 : }
341 : AMREX_FORCE_INLINE
342 : void RealFillBoundary(amrex::MultiFab &a_mf, const amrex::Geometry &/*a_geom*/)
343 : {
344 : BL_PROFILE("Util::RealFillBoundary");
345 443014 : a_mf.setMultiGhost(true);
346 443014 : a_mf.FillBoundary();
347 443014 : }
348 :
349 :
350 : void 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
353 : template <class... Ts>
354 : AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
355 : void IgnoreUnused (const Ts&...) {}
356 :
357 : }
358 :
359 : #endif
|