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