Alamo
Util.cpp
Go to the documentation of this file.
1#include "Util.H"
2#include "Color.H"
3
4#include <chrono>
5#include <cstdlib>
6#include <filesystem>
7
8#include "AMReX_ParallelDescriptor.H"
9#include "AMReX_Utility.H"
10
11#include "IO/ParmParse.H"
12#include "IO/WriteMetaData.H"
13#include "IO/FileNameParse.H"
14#include "Color.H"
15#include "Numeric/Stencil.H"
16
17namespace Util
18{
19
20std::string filename = "";
21std::string globalprefix = "";
22std::pair<std::string,std::string> file_overwrite;
23bool initialized = false;
24bool finalized = false;
25
26std::string GetFileName()
27{
28 if (filename == "")
29 {
31 IO::ParmParse pp_amr("amr");
32
33 if (pp_amr.contains("plot_file") && pp.contains("plot_file"))
34 Util::Abort("plot_file specified in too many locations");
35 else if (pp_amr.contains("plot_file"))
36 {
37 if (amrex::ParallelDescriptor::IOProcessor())
38 amrex::Warning("amr.plot_file will be depricated; use plot_file instead");
39 pp_amr.query("plot_file", filename);
40
41 }
42 else if (pp.contains("plot_file"))
43 {
44 pp_query("plot_file", filename); // Name of directory containing all output data
45 }
47 // else
48 // if (amrex::ParallelDescriptor::IOProcessor())
49 // Util::Abort("No plot file specified! (Specify plot_file = \"plot_file_name\" in input file");
50 }
51 return filename;
52}
53void CopyFileToOutputDir(std::string a_path, bool fullpath, std::string prefix)
54{
55 try
56 {
57 if (filename == "")
58 Util::Exception(INFO,"Cannot back up files yet because the output directory has not been specified");
59
60 std::string basefilename = std::filesystem::path(a_path).filename();
61 std::string absolutepath = std::filesystem::absolute(std::filesystem::path(a_path)).string();
62 std::string abspathfilename = absolutepath;
63 std::replace(abspathfilename.begin(),abspathfilename.end(),'/','_');
64 if (prefix != "")
65 {
66 abspathfilename = prefix + "__" + abspathfilename;
67 basefilename = prefix + "__" + abspathfilename;
68 }
69
70 if (amrex::ParallelDescriptor::IOProcessor())
71 {
72 std::string destinationpath;
73 if (fullpath) destinationpath = filename+"/"+abspathfilename;
74 else destinationpath = filename+"/"+basefilename;
75
76 // Copy the file where the file name is the absolute path, with / replaced with _
77 if (std::filesystem::exists(destinationpath))
78 Util::Exception(INFO,"Trying to copy ",destinationpath," but it already exists.");
79 std::filesystem::copy_file(a_path,destinationpath);
80 }
81 }
82 catch (std::filesystem::filesystem_error const& ex)
83 {
85 "file system error: \n",
86 " what(): " , ex.what() , '\n',
87 " path1(): " , ex.path1() , '\n',
88 " path2(): " , ex.path2() , '\n',
89 " code().value(): " , ex.code().value() , '\n',
90 " code().message(): " , ex.code().message() , '\n',
91 " code().category(): " , ex.code().category().name());
92 }
93}
94
95std::pair<std::string,std::string> GetOverwrittenFile()
96{
97 return file_overwrite;
98}
99
100void SignalHandler(int s)
101{
102 if (amrex::ParallelDescriptor::IOProcessor())
103 {
104 std::string filename = GetFileName();
106 if (s == SIGSEGV) status = IO::Status::Segfault;
107 else if (s == SIGINT) status = IO::Status::Interrupt;
108 if (s == SIGABRT) status = IO::Status::Abort;
109 if (filename != "")
111 }
112
113#ifdef MEME
114 IO::ParmParse pp;
115 if (!pp.contains("nomeme"))
116 {
117 time_t timer; time(&timer);
118 std::stringstream cmd;
119 cmd << "xdg-open " << BUILD_DIR << "/src/Util/Meme/cat0" << (1+((int)timer)%6) << ".gif &";
120 std::system(cmd.str().c_str());
121 std::cout << Color::Bold << Color::FG::Red << "PROGRAM FAILED!" << Color::Reset << " (Compile without -DMEME, or set nomeme = 1 in the input file to disable this!)";
122 }
123#endif
124
125 amrex::BLBackTrace::handler(s);
126}
127
128
130{
131 int argc = 0;
132 char **argv = nullptr;
133 Initialize(argc,argv);
134 initialized = true;
135}
136void Initialize (int argc, char* argv[])
137{
138 srand (time(NULL));
139
140 amrex::Initialize(argc, argv);
141
142 IO::ParmParse pp_amrex("amrex");
143 pp_amrex.add("throw_exception",1);
144 //amrex.throw_exception=1
145
146 signal(SIGSEGV, Util::SignalHandler);
147 signal(SIGINT, Util::SignalHandler);
148 signal(SIGABRT, Util::SignalHandler);
149
150 std::string filename = GetFileName();
151
152 if (amrex::ParallelDescriptor::IOProcessor() && filename != "")
153 {
156 }
157}
158
160{
161 std::string filename = GetFileName();
162 if (filename != "")
164 amrex::Finalize();
165 finalized = true;
166}
167
168
169
170void
171Abort (const char * msg) { Terminate(msg, SIGABRT, true); }
172
173void
174Terminate(const char * /* msg */, int signal, bool /*backtrace*/)
175{
176 SignalHandler(signal);
177}
178
179std::pair<std::string,std::string>
180CreateCleanDirectory (const std::string &path, bool callbarrier)
181{
182 std::pair<std::string,std::string> ret("","");
183
184 if(amrex::ParallelDescriptor::IOProcessor()) {
185 if(amrex::FileExists(path)) {
186 std::time_t t = std::time(0);
187 std::tm * now = std::localtime(&t);
188 int year = now->tm_year+1900;
189 int month = now->tm_mon+1;
190 int day = now->tm_mday;
191 int hour = now->tm_hour;
192 int minute = now->tm_min;
193 int second = now->tm_sec;
194
195 std::stringstream ss;
196 ss << year
197 << std::setfill('0') << std::setw(2) << month
198 << std::setfill('0') << std::setw(2) << day
199 << std::setfill('0') << std::setw(2) << hour
200 << std::setfill('0') << std::setw(2) << minute
201 << std::setfill('0') << std::setw(2) << second;
202
203 std::string newoldname(path + ".old." + ss.str());
204 if (amrex::system::verbose) {
205 amrex::Print() << "Util::CreateCleanDirectory(): " << path
206 << " exists. Renaming to: " << newoldname << std::endl;
207 }
208 std::rename(path.c_str(), newoldname.c_str());
209 ret.first = path;
210 ret.second = newoldname;
211 }
212 if( ! amrex::UtilCreateDirectory(path, 0755)) {
213 amrex::CreateDirectoryFailed(path);
214 }
215 }
216 if(callbarrier) {
217 // Force other processors to wait until directory is built.
218 amrex::ParallelDescriptor::Barrier("amrex::UtilCreateCleanDirectory");
219 }
220 return ret;
221}
222
223
224namespace Test
225{
226int Message(std::string testname)
227{
228 if (amrex::ParallelDescriptor::IOProcessor())
229 std::cout << std::left
230 << Color::FG::White << Color::Bold << testname << Color::Reset << std::endl;
231 return 0;
232}
233int Message(std::string testname, int failed)
234{
235 if (amrex::ParallelDescriptor::IOProcessor())
236 {
237 winsize w;
238 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
239 std::stringstream ss;
240 if (!failed)
241 ss << "[" << Color::FG::Green << Color::Bold << "PASS" << Color::Reset << "]";
242 else
243 ss << "[" << Color::FG::Red << Color::Bold << "FAIL" << Color::Reset << "]";
244
245 int terminalwidth = 80; //std::min(w.ws_col,(short unsigned int) 100);
246
247 std::cout << std::left
248 << testname
249 << std::setw(terminalwidth - testname.size() + ss.str().size() - 6) << std::right << std::setfill('.') << ss.str() << std::endl;
250 }
251 return failed;
252}
253int SubMessage(std::string testname, int failed)
254{
255 if (amrex::ParallelDescriptor::IOProcessor())
256 {
257 winsize w;
258 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
259 std::stringstream ss;
260 if (!failed)
261 ss << "[" << Color::FG::LightGreen << Color::Bold << "PASS" << Color::Reset << "]";
262 else
263 ss << "[" << Color::FG::Red << Color::Bold << "FAIL" << Color::Reset << "]";
264
265 int terminalwidth = 80;
266
267 std::cout << std::left
268 << " ├ "
269 << testname
270 << std::setw(terminalwidth - testname.size() + ss.str().size() - 12) << std::right << std::setfill('.') << ss.str() << std::endl;
271 }
272 return failed;
273}
274int SubFinalMessage(int failed)
275{
276 if (amrex::ParallelDescriptor::IOProcessor())
277 {
278 winsize w;
279 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
280 std::stringstream ss;
281 std::cout << std::left << " └ ";
282
283 if (!failed)
284 std::cout << Color::FG::Green << Color::Bold << failed << " tests failed" << Color::Reset << std::endl;
285 else
286 std::cout << Color::FG::Red << Color::Bold << failed << " tests failed" << Color::Reset << std::endl;
287 }
288 return failed;
289}
290
291}
292
293void AverageCellcenterToNode(amrex::MultiFab& node_mf, const int &dcomp, const amrex::MultiFab &cell_mf, const int &scomp, const int &ncomp/*, const int ngrow=0*/)
294{
295 Util::Assert(INFO,TEST(dcomp + ncomp <= node_mf.nComp()));
296 Util::Assert(INFO,TEST(scomp + ncomp <= cell_mf.nComp()));
297 //Util::Assert(INFO,TEST(cell_mf.boxArray() == node_mf.boxArray()));
298 Util::Assert(INFO,TEST(cell_mf.DistributionMap() == cell_mf.DistributionMap()));
299 Util::Assert(INFO,TEST(cell_mf.nGrow() > 0));
300 for (amrex::MFIter mfi(node_mf,amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi)
301 {
302 amrex::Box bx = mfi.nodaltilebox();
303 amrex::Array4<Set::Scalar> const& node = node_mf.array(mfi);
304 amrex::Array4<const Set::Scalar> const& cell = cell_mf.array(mfi);
305 for (int n = 0; n < ncomp; n++)
306 amrex::ParallelFor (bx,[=] AMREX_GPU_DEVICE(int i, int j, int k) {
307 node(i,j,k,dcomp+n) = Numeric::Interpolate::CellToNodeAverage(cell,i,j,k,scomp+n);
308 });
309 }
310}
311
312
313}
std::time_t t
#define pp_query(...)
Definition ParmParse.H:106
#define TEST(x)
Definition Util.H:22
#define INFO
Definition Util.H:21
bool contains(std::string name)
Definition ParmParse.H:172
static std::string Green
Definition Color.H:21
static std::string LightGreen
Definition Color.H:29
static std::string Red
Definition Color.H:20
static std::string White
Definition Color.H:34
static std::string Bold
Definition Color.H:9
static std::string Reset
Definition Color.H:8
void WriteMetaData(std::string plot_file, Status status, int per)
void FileNameParse(std::string &filename)
Internal function to do processing of the file name.
@ Segfault
@ Complete
@ Running
@ Interrupt
@ Abort
Definition GB.H:8
int Message(std::string testname)
Definition Util.cpp:226
int SubMessage(std::string testname, int failed)
Definition Util.cpp:253
int SubFinalMessage(int failed)
Definition Util.cpp:274
A collection of utility routines.
Definition Set.cpp:8
std::pair< std::string, std::string > CreateCleanDirectory(const std::string &path, bool callbarrier)
Definition Util.cpp:180
std::string GetFileName()
Definition Util.cpp:26
bool finalized
Definition Util.cpp:24
void Abort(const char *msg)
Definition Util.cpp:171
std::string globalprefix
Definition Util.cpp:21
void Finalize()
Definition Util.cpp:159
std::string filename
Definition Util.cpp:20
void AverageCellcenterToNode(amrex::MultiFab &node_mf, const int &dcomp, const amrex::MultiFab &cell_mf, const int &scomp, const int &ncomp)
Definition Util.cpp:293
bool initialized
Definition Util.cpp:23
AMREX_FORCE_INLINE void Assert(std::string file, std::string func, int line, std::string smt, bool pass, Args const &... args)
Definition Util.H:55
std::pair< std::string, std::string > file_overwrite
Definition Util.cpp:22
void CopyFileToOutputDir(std::string a_path, bool fullpath, std::string prefix)
Definition Util.cpp:53
void Initialize()
Definition Util.cpp:129
std::pair< std::string, std::string > GetOverwrittenFile()
Definition Util.cpp:95
void Exception(std::string file, std::string func, int line, Args const &... args)
Definition Util.H:190
void Terminate(const char *, int signal, bool)
Definition Util.cpp:174
void SignalHandler(int s)
Definition Util.cpp:100
static AMREX_FORCE_INLINE T CellToNodeAverage(const amrex::Array4< const T > &f, const int &i, const int &j, const int &k, const int &m, std::array< StencilType, AMREX_SPACEDIM > stencil=DefaultType)
Definition Stencil.H:1313