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 14860311 : 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 : template<typename... Args>
149 0 : void ParallelMessage (std::string file, std::string func, int line, Args const &... args)
150 : {
151 0 : std::ostringstream infostream;
152 0 : infostream << globalprefix;
153 0 : infostream << Color::Bold << Color::FG::Blue << "MESSAGE("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ": ";
154 0 : infostream << Color::FG::LightBlue << file << Color::Reset << ":" << line << " ";
155 0 : infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
156 0 : std::string info = infostream.str();
157 0 : std::ostringstream messagestream;
158 : using List= int[];
159 0 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
160 0 : std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
161 0 : Util::String::ReplaceAll(message,'\n',"\n"+info);
162 0 : std::cout << info << message << std::endl;
163 0 : }
164 :
165 : template<typename... Args>
166 6 : void Warning (std::string file, std::string func, int line, Args const &... args)
167 : {
168 6 : if (amrex::ParallelDescriptor::IOProcessor())
169 : {
170 6 : std::ostringstream infostream;
171 6 : infostream << globalprefix;
172 6 : infostream << Color::Bold << Color::FG::Yellow << "WARNING" << Color::Reset << ": ";
173 6 : infostream << Color::FG::Yellow << file << Color::Reset << ":" << line << " ";
174 6 : infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
175 6 : std::string info = infostream.str();
176 :
177 6 : std::ostringstream messagestream;
178 : using List= int[];
179 6 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
180 6 : std::string message = messagestream.str();///String::Wrap(messagestream.str(),150);
181 :
182 12 : Util::String::ReplaceAll(message,'\n',"\n"+info);
183 :
184 6 : std::cout << info << message << std::endl;
185 6 : }
186 6 : }
187 :
188 :
189 : template<typename... Args>
190 0 : void Exception (std::string file, std::string func, int line, Args const &... args)
191 : {
192 0 : if (amrex::ParallelDescriptor::IOProcessor())
193 : {
194 0 : std::ostringstream infostream;
195 0 : infostream << globalprefix;
196 0 : infostream << Color::Bold << Color::FG::Red << "EXCEPTION" << Color::Reset << ": ";
197 0 : infostream << Color::FG::LightGray << file << ":" << line << " (" << func << Color::Reset << ") ";
198 0 : std::string info = infostream.str();
199 :
200 0 : std::ostringstream messagestream;
201 0 : messagestream << Color::FG::Red;
202 : using List= int[];
203 0 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
204 0 : messagestream << Color::Reset;
205 :
206 0 : std::string message = messagestream.str();
207 :
208 0 : Util::String::ReplaceAll(message,'\n',"\n"+info);
209 :
210 0 : std::cout << info << message << std::endl;
211 :
212 0 : std::throw_with_nested(std::runtime_error("IO::Exception"));
213 0 : }
214 0 : }
215 :
216 : template<typename... Args>
217 : AMREX_FORCE_INLINE
218 : void AssertException (std::string file, std::string func, int line, std::string smt, bool pass, Args const &... args)
219 : {
220 57 : if (pass) return;
221 :
222 0 : if (amrex::ParallelDescriptor::IOProcessor())
223 : {
224 0 : std::ostringstream infostream;
225 0 : infostream << globalprefix;
226 0 : infostream << Color::Bold << Color::FG::Red << "ABORT("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ": ";
227 0 : infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
228 0 : infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
229 0 : infostream << "Assertion failed: " << Color::BG::DarkGray << Color::Underlined << Color::Bold << Color::FG::LightGreen << smt << Color::Reset;
230 0 : std::string info = infostream.str();
231 :
232 :
233 0 : std::ostringstream messagestream;
234 : using List= int[];
235 0 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
236 0 : std::string message = messagestream.str();
237 0 : Util::String::ReplaceAll(message,'\n',"\n"+info);
238 :
239 0 : std::cout << info << message << std::endl;
240 :
241 0 : Exception(file,func,line,"Fatal Error");
242 0 : }
243 : }
244 :
245 :
246 :
247 : template<typename... Args>
248 0 : void ParmParseException ( std::string file, std::string func, int line,
249 : std::string file2, std::string /*func2*/, int line2,
250 : std::string fullname,
251 : Args const &... args)
252 : {
253 0 : if (amrex::ParallelDescriptor::IOProcessor())
254 : {
255 0 : std::ostringstream infostream;
256 0 : infostream << globalprefix;
257 0 : infostream << Color::Bold << Color::FG::Red << "EXCEPTION" << Color::Reset << ": ";
258 0 : infostream << Color::FG::LightGray << file << ":" << line << " (" << func << Color::Reset << ") ";
259 0 : std::string info = infostream.str();
260 :
261 0 : std::ostringstream messagestream;
262 0 : messagestream << Color::FG::Red;
263 : using List= int[];
264 0 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
265 0 : messagestream << Color::Reset;
266 :
267 0 : const std::size_t nargs = sizeof...(Args);
268 :
269 0 : if (nargs) messagestream << "\n";
270 0 : if (fullname != "")
271 0 : messagestream << "while reading " << Color::FG::Yellow << fullname << Color::Reset;
272 0 : if (line2 >= 0)
273 : {
274 0 : messagestream << " at " << file2 << ":" << line2;
275 : }
276 :
277 0 : std::string message = messagestream.str();
278 :
279 0 : Util::String::ReplaceAll(message,'\n',"\n"+info);
280 :
281 0 : std::cout << info << message << std::endl;
282 :
283 0 : std::throw_with_nested(std::runtime_error("IO::ParmParse Exception"));
284 0 : }
285 0 : }
286 :
287 :
288 : std::pair<std::string,std::string>
289 : CreateCleanDirectory (const std::string &path, bool callbarrier = true);
290 :
291 :
292 : /// \brief A collection of I/O routines for Unit Tests
293 : namespace Test
294 : {
295 : int Message(std::string testname);
296 : int Message(std::string testname, int passed);
297 : int SubMessage(std::string testname, int passed);
298 : int SubFinalMessage(int failed);
299 : }
300 :
301 : //
302 : // These two functions are not really necessary anymore.
303 : // TODO: remove RealFillBoundary and all references to them.
304 : //
305 : template<class T>
306 : AMREX_FORCE_INLINE
307 : void RealFillBoundary(amrex::FabArray<amrex::BaseFab<T>> &a_mf,const amrex::Geometry &/*a_geom*/)
308 : {
309 : BL_PROFILE("Util::RealFillBoundary");
310 6220 : a_mf.setMultiGhost(true);
311 6220 : a_mf.FillBoundary();
312 6220 : }
313 : AMREX_FORCE_INLINE
314 : void RealFillBoundary(amrex::MultiFab &a_mf, const amrex::Geometry &/*a_geom*/)
315 : {
316 : BL_PROFILE("Util::RealFillBoundary");
317 443014 : a_mf.setMultiGhost(true);
318 443014 : a_mf.FillBoundary();
319 443014 : }
320 :
321 :
322 : void AverageCellcenterToNode(amrex::MultiFab& node_mf, const int &dcomp, const amrex::MultiFab &cell_mf, const int &scomp, const int &ncomp/*, const int ngrow=0*/);
323 :
324 : // Prevent compiler from complaining about unused variables
325 : template <class... Ts>
326 : AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
327 : void IgnoreUnused (const Ts&...) {}
328 :
329 : }
330 :
331 : #endif
|