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 :
15 : #include "AMReX.H"
16 : #include <AMReX_ParallelDescriptor.H>
17 : #include <AMReX_PlotFileUtil.H>
18 :
19 : #include "Color.H"
20 : #define INFO __FILE__, __func__, __LINE__
21 : #define TEST(x) #x, x
22 :
23 :
24 : #ifndef ALAMO_SINGLE_DEFINITION
25 : #define ALAMO_SINGLE_DEFINITION AMREX_ATTRIBUTE_WEAK
26 : #endif
27 :
28 :
29 : /// \brief A collection of utility routines
30 : namespace Util
31 : {
32 : /// \brief A collection of string operations
33 : namespace String
34 : {
35 : int ReplaceAll(std::string &str, const std::string before, const std::string after);
36 : int ReplaceAll(std::string &str, const char before, const std::string after);
37 : std::string Wrap(std::string text, unsigned per_line);
38 : std::string Join(std::vector<std::string> & vec, char separator = '_');
39 : std::vector<std::string> Split(std::string &str, const char token = ' ');
40 : bool Contains(std::string &str, const std::string find);
41 : template<class T>
42 : T Parse(std::string);
43 : template<>
44 : std::complex<int> Parse(std::string);
45 : }
46 :
47 : extern std::string globalprefix;
48 : extern bool initialized;
49 : extern bool finalized;
50 :
51 :
52 : std::string GetFileName();
53 : void CopyFileToOutputDir(std::string a_path, bool fullpath = true, std::string prefix = "");
54 :
55 : std::pair<std::string,std::string> GetOverwrittenFile();
56 :
57 : void SignalHandler(int s);
58 :
59 : void Initialize (int argc, char* argv[]);
60 : void Initialize ();
61 :
62 : void Finalize ();
63 :
64 : void Terminate(const char * msg, int signal, bool backtrace);
65 :
66 : void Abort (const char * msg);
67 :
68 : template<typename... Args>
69 : AMREX_FORCE_INLINE
70 : void Assert (std::string file, std::string func, int line, std::string smt, bool pass, Args const &... args)
71 : {
72 36071087 : if (pass) return;
73 :
74 0 : std::ostringstream infostream;
75 0 : infostream << globalprefix;
76 0 : infostream << Color::Bold << Color::FG::Red << "ABORT("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ": ";
77 0 : infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
78 0 : infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
79 0 : infostream << "Assertion failed: " << Color::BG::DarkGray << Color::Underlined << Color::Bold << Color::FG::LightGreen << smt << Color::Reset;
80 0 : std::string info = infostream.str();
81 :
82 :
83 0 : std::ostringstream messagestream;
84 : using List= int[];
85 0 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
86 0 : std::string message = messagestream.str();
87 0 : String::ReplaceAll(message,'\n',"\n"+info);
88 :
89 0 : std::cout << info << message << std::endl;
90 :
91 0 : Abort("Fatal Error");
92 0 : }
93 :
94 :
95 : template<typename... Args>
96 0 : void Abort (std::string file, std::string func, int line, Args const &... args)
97 : {
98 0 : if (amrex::ParallelDescriptor::IOProcessor())
99 : {
100 0 : std::ostringstream infostream;
101 0 : infostream << globalprefix;
102 0 : infostream << Color::Bold << Color::FG::Red << "ABORT" << Color::Reset << ": ";
103 0 : infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
104 0 : infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
105 0 : std::string info = infostream.str();
106 :
107 0 : std::ostringstream messagestream;
108 : using List= int[];
109 0 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
110 0 : std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
111 : //std::string messageorg = message;
112 0 : String::ReplaceAll(message,'\n',"\n"+info);
113 0 : std::cout << info << message << std::endl;
114 0 : }
115 0 : Abort("Fatal Error");
116 0 : }
117 :
118 : template<typename... Args>
119 : void ParallelAbort (std::string file, std::string func, int line, Args const &... args)
120 : {
121 :
122 : std::ostringstream infostream;
123 : infostream << globalprefix;
124 : infostream << Color::Bold << Color::FG::Red << "ABORT("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ": ";
125 : infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
126 : infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
127 : std::string info = infostream.str();
128 :
129 : std::ostringstream messagestream;
130 : using List= int[];
131 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
132 : std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
133 : //std::string messageorg = message;
134 : String::ReplaceAll(message,'\n',"\n"+info);
135 : std::cout << info << message << std::endl;
136 :
137 : Abort("Fatal Error");
138 : }
139 :
140 : template<typename... Args>
141 3659 : void Message (std::string file, std::string func, int line, Args const &... args)
142 : {
143 3659 : if (amrex::ParallelDescriptor::IOProcessor())
144 : {
145 3659 : std::ostringstream infostream;
146 3659 : infostream << globalprefix;
147 3659 : infostream << Color::Bold << Color::FG::Blue << "MESSAGE" << Color::Reset << ": ";
148 3659 : infostream << Color::FG::LightBlue << file << Color::Reset << ":" << line << " ";
149 3659 : infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
150 3659 : std::string info = infostream.str();
151 :
152 3659 : std::ostringstream messagestream;
153 : using List= int[];
154 3659 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
155 3659 : std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
156 :
157 3659 : String::ReplaceAll(message,'\n',"\n"+info);
158 :
159 3659 : std::cout << info << message << std::endl;
160 3659 : }
161 3659 : }
162 :
163 : template<typename... Args>
164 : void ParallelMessage (std::string file, std::string func, int line, Args const &... args)
165 : {
166 : std::ostringstream infostream;
167 : infostream << globalprefix;
168 : infostream << Color::Bold << Color::FG::Blue << "MESSAGE("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ": ";
169 : infostream << Color::FG::LightBlue << file << Color::Reset << ":" << line << " ";
170 : infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
171 : std::string info = infostream.str();
172 : std::ostringstream messagestream;
173 : using List= int[];
174 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
175 : std::string message = messagestream.str();//String::Wrap(messagestream.str(),150);
176 : String::ReplaceAll(message,'\n',"\n"+info);
177 : std::cout << info << message << std::endl;
178 : }
179 :
180 : template<typename... Args>
181 18 : void Warning (std::string file, std::string func, int line, Args const &... args)
182 : {
183 18 : if (amrex::ParallelDescriptor::IOProcessor())
184 : {
185 18 : std::ostringstream infostream;
186 18 : infostream << globalprefix;
187 18 : infostream << Color::Bold << Color::FG::Yellow << "WARNING" << Color::Reset << ": ";
188 18 : infostream << Color::FG::Yellow << file << Color::Reset << ":" << line << " ";
189 18 : infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
190 18 : std::string info = infostream.str();
191 :
192 18 : std::ostringstream messagestream;
193 : using List= int[];
194 18 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
195 18 : std::string message = messagestream.str();///String::Wrap(messagestream.str(),150);
196 :
197 18 : String::ReplaceAll(message,'\n',"\n"+info);
198 :
199 18 : std::cout << info << message << std::endl;
200 18 : }
201 18 : }
202 :
203 :
204 : template<typename... Args>
205 0 : void Exception (std::string file, std::string func, int line, Args const &... args)
206 : {
207 0 : if (amrex::ParallelDescriptor::IOProcessor())
208 : {
209 0 : std::ostringstream infostream;
210 0 : infostream << globalprefix;
211 0 : infostream << Color::Bold << Color::FG::Red << "EXCEPTION" << Color::Reset << ": ";
212 0 : infostream << Color::FG::LightGray << file << ":" << line << " (" << func << Color::Reset << ") ";
213 0 : std::string info = infostream.str();
214 :
215 0 : std::ostringstream messagestream;
216 0 : messagestream << Color::FG::Red;
217 : using List= int[];
218 0 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
219 0 : messagestream << Color::Reset;
220 :
221 0 : std::string message = messagestream.str();
222 :
223 0 : Util::String::ReplaceAll(message,'\n',"\n"+info);
224 :
225 0 : std::cout << info << message << std::endl;
226 :
227 0 : std::throw_with_nested(std::runtime_error("IO::Exception"));
228 0 : }
229 0 : }
230 :
231 : template<typename... Args>
232 : AMREX_FORCE_INLINE
233 : void AssertException (std::string file, std::string func, int line, std::string smt, bool pass, Args const &... args)
234 : {
235 448 : if (pass) return;
236 :
237 0 : if (amrex::ParallelDescriptor::IOProcessor())
238 : {
239 0 : std::ostringstream infostream;
240 0 : infostream << globalprefix;
241 0 : infostream << Color::Bold << Color::FG::Red << "ABORT("<< amrex::ParallelDescriptor::MyProc()<<")" << Color::Reset << ": ";
242 0 : infostream << Color::FG::Red << file << Color::Reset << ":" << line << " ";
243 0 : infostream << "(" << Color::FG::LightGreen << func << Color::Reset << ") ";
244 0 : infostream << "Assertion failed: " << Color::BG::DarkGray << Color::Underlined << Color::Bold << Color::FG::LightGreen << smt << Color::Reset;
245 0 : std::string info = infostream.str();
246 :
247 :
248 0 : std::ostringstream messagestream;
249 : using List= int[];
250 0 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
251 0 : std::string message = messagestream.str();
252 0 : String::ReplaceAll(message,'\n',"\n"+info);
253 :
254 0 : std::cout << info << message << std::endl;
255 :
256 0 : Exception(file,func,line,"Fatal Error");
257 0 : }
258 : }
259 :
260 :
261 :
262 : template<typename... Args>
263 0 : void ParmParseException ( std::string file, std::string func, int line,
264 : std::string file2, std::string /*func2*/, int line2,
265 : std::string fullname,
266 : Args const &... args)
267 : {
268 0 : if (amrex::ParallelDescriptor::IOProcessor())
269 : {
270 0 : std::ostringstream infostream;
271 0 : infostream << globalprefix;
272 0 : infostream << Color::Bold << Color::FG::Red << "EXCEPTION" << Color::Reset << ": ";
273 0 : infostream << Color::FG::LightGray << file << ":" << line << " (" << func << Color::Reset << ") ";
274 0 : std::string info = infostream.str();
275 :
276 0 : std::ostringstream messagestream;
277 0 : messagestream << Color::FG::Red;
278 : using List= int[];
279 0 : (void)List{0, ( (void)(messagestream << args), 0 ) ... };
280 0 : messagestream << Color::Reset;
281 :
282 0 : const std::size_t nargs = sizeof...(Args);
283 :
284 0 : if (nargs) messagestream << "\n";
285 0 : if (fullname != "")
286 0 : messagestream << "while reading " << Color::FG::Yellow << fullname << Color::Reset;
287 0 : if (line2 >= 0)
288 : {
289 0 : messagestream << " at " << file2 << ":" << line2;
290 : }
291 :
292 0 : std::string message = messagestream.str();
293 :
294 0 : Util::String::ReplaceAll(message,'\n',"\n"+info);
295 :
296 0 : std::cout << info << message << std::endl;
297 :
298 0 : std::throw_with_nested(std::runtime_error("IO::ParmParse Exception"));
299 0 : }
300 0 : }
301 :
302 :
303 : std::pair<std::string,std::string>
304 : CreateCleanDirectory (const std::string &path, bool callbarrier = true);
305 :
306 :
307 : /// \brief A collection of I/O routines for Unit Tests
308 : namespace Test
309 : {
310 : int Message(std::string testname);
311 : int Message(std::string testname, int passed);
312 : int SubMessage(std::string testname, int passed);
313 : int SubFinalMessage(int failed);
314 : }
315 :
316 : //
317 : // These two functions are not really necessary anymore.
318 : // TODO: remove RealFillBoundary and all references to them.
319 : //
320 : template<class T>
321 : AMREX_FORCE_INLINE
322 : void RealFillBoundary(amrex::FabArray<amrex::BaseFab<T>> &a_mf,const amrex::Geometry &/*a_geom*/)
323 : {
324 : BL_PROFILE("Util::RealFillBoundary");
325 27245 : a_mf.setMultiGhost(true);
326 27245 : a_mf.FillBoundary();
327 27245 : }
328 : AMREX_FORCE_INLINE
329 : void RealFillBoundary(amrex::MultiFab &a_mf, const amrex::Geometry &/*a_geom*/)
330 : {
331 : BL_PROFILE("Util::RealFillBoundary");
332 1056467 : a_mf.setMultiGhost(true);
333 1056467 : a_mf.FillBoundary();
334 1056467 : }
335 :
336 :
337 : void AverageCellcenterToNode(amrex::MultiFab& node_mf, const int &dcomp, const amrex::MultiFab &cell_mf, const int &scomp, const int &ncomp/*, const int ngrow=0*/);
338 :
339 :
340 :
341 :
342 : }
343 : #endif
|