ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_DiagnosticsLog.H
Go to the documentation of this file.
1 #ifndef ERF_DIAGNOSTICS_LOG_H
2 #define ERF_DIAGNOSTICS_LOG_H
3 
4 /** Opening of the moving-bodies diagnostics files for a fresh or a restarted run. */
5 
6 #include <fstream>
7 #include <string>
8 
9 #include <AMReX.H>
10 #include <AMReX_ParallelDescriptor.H>
11 #include <AMReX_Utility.H>
12 
13 namespace erf_actuator {
14 
15 /**
16  * Open a diagnostics text file for writing (the caller writes CSV or space-separated rows),
17  * creating its directory. With truncate (a fresh run) the file starts empty; without it (a
18  * restarted run) the rows are appended to what the run it continues wrote. Returns whether the
19  * caller must write the header: always after truncating, and on append only when the file is
20  * new or empty (a restart in a clean directory).
21  */
22 inline bool open_log (std::ofstream& out, const std::string& fname, bool truncate)
23 {
24  const auto slash = fname.rfind('/');
25  if (slash != std::string::npos && !amrex::UtilCreateDirectory(fname.substr(0, slash), 0755)) {
26  amrex::Abort("cannot create the directory of the diagnostics file '" + fname + "'");
27  }
28  bool need_header = truncate;
29  if (!truncate) {
30  std::ifstream probe(fname, std::ios::ate);
31  need_header = !probe || probe.tellg() <= 0;
32  }
33  out.open(fname, truncate ? std::ios::trunc : std::ios::app);
34  if (!out) { amrex::Abort("cannot open moving-bodies diagnostics file '" + fname + "'"); }
35  return need_header;
36 }
37 
38 /**
39  * Whether fname exists, as the I/O rank sees it, on every rank: a checkpoint read that every rank
40  * enters (ParallelDescriptor::ReadAndBcastFile) must be decided alike everywhere, whatever a
41  * parallel file system shows each node.
42  */
43 inline bool file_exists_everywhere (const std::string& fname)
44 {
45  int exists = amrex::ParallelDescriptor::IOProcessor() ? (amrex::FileExists(fname) ? 1 : 0) : 0;
46  amrex::ParallelDescriptor::Bcast(&exists, 1, amrex::ParallelDescriptor::IOProcessorNumber());
47  return exists != 0;
48 }
49 
50 } // namespace erf_actuator
51 
52 #endif
Definition: ERF_ActuatorDeviceBuffers.H:14
bool file_exists_everywhere(const std::string &fname)
Definition: ERF_DiagnosticsLog.H:43
bool open_log(std::ofstream &out, const std::string &fname, bool truncate)
Definition: ERF_DiagnosticsLog.H:22