ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_ReadCustomBinaryIC.H
Go to the documentation of this file.
1 #ifndef ERF_READCUSTOMBINARYIC_H_
2 #define ERF_READCUSTOMBINARYIC_H_
3 
4 #include <array>
5 #include <fstream>
6 
7 #include "ERF_DataStruct.H"
8 
9 /*
10  * Routine to read in the custom written binary file into vectors
11  * of variables
12  */
13 
14 inline void
15 ReadCustomBinaryIC(const std::string filename,
16  amrex::Vector<amrex::Real>& latvec_h,
17  amrex::Vector<amrex::Real>& lonvec_h,
18  amrex::Vector<amrex::Real>& xvec_h,
19  amrex::Vector<amrex::Real>& yvec_h,
20  amrex::Vector<amrex::Real>& zvec_h,
21  amrex::Vector<amrex::Real>& rho_h,
22  amrex::Vector<amrex::Real>& uvel_h,
23  amrex::Vector<amrex::Real>& vvel_h,
24  amrex::Vector<amrex::Real>& wvel_h,
25  amrex::Vector<amrex::Real>& theta_h,
26  amrex::Vector<amrex::Real>& qv_h,
27  amrex::Vector<amrex::Real>& qc_h,
28  amrex::Vector<amrex::Real>& qr_h)
29 {
30  int nx = 0, ny = 0, nz = 0, ndata = 0;
31  float value;
32 
33  // Open the binary file in input mode
34  std::ifstream infile(filename, std::ios::binary);
35  if (!infile) {
36  amrex::Abort("Error: Could not open file " + filename);
37  }
38 
39  auto read_or_abort = [&](auto& dest, const std::string& field_name) {
40  infile.read(reinterpret_cast<char*>(&dest), sizeof(dest));
41  if (!infile) {
42  amrex::Abort("Error reading " + field_name + " from " + filename);
43  }
44  };
45 
46  auto read_float_vector = [&](amrex::Vector<amrex::Real>& dest,
47  int count,
48  const std::string& field_name)
49  {
50  if (count < 0) {
51  amrex::Abort("Error: negative element count for " + field_name + " in " + filename);
52  }
53 
54  dest.reserve(dest.size() + count);
55  for (int idx = 0; idx < count; ++idx) {
56  read_or_abort(value, field_name);
57  dest.emplace_back(value);
58  }
59  };
60 
61  // Read the four integers
62  read_or_abort(nx, "nx");
63  read_or_abort(ny, "ny");
64  read_or_abort(nz, "nz");
65  read_or_abort(ndata, "ndata");
66 
67  if (nx <= 0 || ny <= 0 || nz <= 0) {
68  amrex::Abort("Error: invalid custom binary dimensions in " + filename);
69  }
70 
71  if (ndata < 0 || ndata > 8) {
72  amrex::Abort("Error: unsupported number of data fields in " + filename);
73  }
74 
75  read_float_vector(latvec_h, nx*ny, "latitude");
76  read_float_vector(lonvec_h, nx*ny, "longitude");
77  read_float_vector(xvec_h, nx, "x coordinates");
78  read_float_vector(yvec_h, ny, "y coordinates");
79  read_float_vector(zvec_h, nz, "z coordinates");
80 
81  std::array<amrex::Vector<amrex::Real>*, 8> data_fields = {
82  &rho_h, &uvel_h, &vvel_h, &wvel_h, &theta_h, &qv_h, &qc_h, &qr_h
83  };
84 
85  // Read the file
86  for (int idx = 0; idx < ndata; ++idx) {
87  read_float_vector(*data_fields[idx], nx * ny * nz,
88  "data field " + std::to_string(idx));
89  }
90 }
91 #endif
amrex::Real value
Definition: ERF_HurricaneDiagnostics.H:20
void ReadCustomBinaryIC(const std::string filename, amrex::Vector< amrex::Real > &latvec_h, amrex::Vector< amrex::Real > &lonvec_h, amrex::Vector< amrex::Real > &xvec_h, amrex::Vector< amrex::Real > &yvec_h, amrex::Vector< amrex::Real > &zvec_h, amrex::Vector< amrex::Real > &rho_h, amrex::Vector< amrex::Real > &uvel_h, amrex::Vector< amrex::Real > &vvel_h, amrex::Vector< amrex::Real > &wvel_h, amrex::Vector< amrex::Real > &theta_h, amrex::Vector< amrex::Real > &qv_h, amrex::Vector< amrex::Real > &qc_h, amrex::Vector< amrex::Real > &qr_h)
Definition: ERF_ReadCustomBinaryIC.H:15
Vector< Real > rho_h(khi+1, zero)