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