ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ERF_ReadCustomBinaryIC.H
Go to the documentation of this file.
1 #ifndef ERF_READCUSTOMBINARYIC_H_
2 #define ERF_READCUSTOMBINARYIC_H_
3 
4 #include "ERF_DataStruct.H"
5 
6 /*
7  * Routine to read in the custom written binary file into vectors
8  * of variables
9  */
10 
11 inline void
12 ReadCustomBinaryIC(const std::string filename,
13  amrex::Vector<amrex::Real>& xvec_h,
14  amrex::Vector<amrex::Real>& yvec_h,
15  amrex::Vector<amrex::Real>& zvec_h,
16  amrex::Vector<amrex::Real>& rho_h,
17  amrex::Vector<amrex::Real>& uvel_h,
18  amrex::Vector<amrex::Real>& vvel_h,
19  amrex::Vector<amrex::Real>& wvel_h,
20  amrex::Vector<amrex::Real>& theta_h,
21  amrex::Vector<amrex::Real>& qv_h,
22  amrex::Vector<amrex::Real>& qc_h,
23  amrex::Vector<amrex::Real>& qr_h)
24 {
25  int nx, ny, nz, ndata;
26  float value;
27 
28  // Open the binary file in input mode
29  std::ifstream infile(filename, std::ios::binary);
30  if (!infile) {
31  std::cerr << "Error: Could not open file " << filename << std::endl;
32  }
33 
34  // Read the four integers
35  infile.read(reinterpret_cast<char*>(&nx), sizeof(int));
36  infile.read(reinterpret_cast<char*>(&ny), sizeof(int));
37  infile.read(reinterpret_cast<char*>(&nz), sizeof(int));
38  infile.read(reinterpret_cast<char*>(&ndata), sizeof(int));
39 
40  for(int i=0; i<nx; i++) {
41  infile.read(reinterpret_cast<char*>(&value), sizeof(float));
42  xvec_h.emplace_back(value);
43  }
44 
45  for(int j=0; j<ny; j++) {
46  infile.read(reinterpret_cast<char*>(&value), sizeof(float));
47  yvec_h.emplace_back(value);
48  }
49 
50  for(int k=0; k<nz; k++) {
51  infile.read(reinterpret_cast<char*>(&value), sizeof(float));
52  zvec_h.emplace_back(value);
53  }
54 
55  amrex::Vector<amrex::Real>* data_h = nullptr; // Declare pointer outside the loop
56 
57  // Read the file
58  for(int idx=0; idx<ndata; idx++){
59  if(idx == 0){
60  data_h = &rho_h;
61  } else if (idx==1) {
62  data_h = &uvel_h;
63  } else if (idx==2) {
64  data_h = &vvel_h;
65  } else if (idx==3) {
66  data_h = &wvel_h;
67  } else if(idx==4) {
68  data_h = &theta_h;
69  } else if(idx==5) {
70  data_h = &qv_h;
71  } else if(idx==6) {
72  data_h = &qc_h;
73  } else if(idx==7) {
74  data_h = &qr_h;
75  }
76  for(int k=0; k<nz; k++) {
77  for(int j=0; j<ny; j++) {
78  for(int i=0; i<nx; i++) {
79  infile.read(reinterpret_cast<char*>(&value), sizeof(float));
80  data_h->emplace_back(value);
81  }
82  }
83  }
84  }
85  infile.close();
86 }
87 #endif
void ReadCustomBinaryIC(const std::string filename, 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:12