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>& latvec_h,
14  amrex::Vector<amrex::Real>& lonvec_h,
15  amrex::Vector<amrex::Real>& xvec_h,
16  amrex::Vector<amrex::Real>& yvec_h,
17  amrex::Vector<amrex::Real>& zvec_h,
18  amrex::Vector<amrex::Real>& rho_h,
19  amrex::Vector<amrex::Real>& uvel_h,
20  amrex::Vector<amrex::Real>& vvel_h,
21  amrex::Vector<amrex::Real>& wvel_h,
22  amrex::Vector<amrex::Real>& theta_h,
23  amrex::Vector<amrex::Real>& qv_h,
24  amrex::Vector<amrex::Real>& qc_h,
25  amrex::Vector<amrex::Real>& qr_h)
26 {
27  int nx, ny, nz, ndata;
28  float value;
29 
30  // Open the binary file in input mode
31  std::ifstream infile(filename, std::ios::binary);
32  if (!infile) {
33  std::cerr << "Error: Could not open file " << filename << std::endl;
34  }
35 
36  // Read the four integers
37  infile.read(reinterpret_cast<char*>(&nx), sizeof(int));
38  infile.read(reinterpret_cast<char*>(&ny), sizeof(int));
39  infile.read(reinterpret_cast<char*>(&nz), sizeof(int));
40  infile.read(reinterpret_cast<char*>(&ndata), sizeof(int));
41 
42  for(int i=0; i<nx*ny; i++) {
43  infile.read(reinterpret_cast<char*>(&value), sizeof(float));
44  latvec_h.emplace_back(value);
45  }
46 
47  for(int i=0; i<nx*ny; i++) {
48  infile.read(reinterpret_cast<char*>(&value), sizeof(float));
49  lonvec_h.emplace_back(value);
50  }
51 
52  for(int i=0; i<nx; i++) {
53  infile.read(reinterpret_cast<char*>(&value), sizeof(float));
54  xvec_h.emplace_back(value);
55  }
56 
57  for(int j=0; j<ny; j++) {
58  infile.read(reinterpret_cast<char*>(&value), sizeof(float));
59  yvec_h.emplace_back(value);
60  }
61 
62  for(int k=0; k<nz; k++) {
63  infile.read(reinterpret_cast<char*>(&value), sizeof(float));
64  zvec_h.emplace_back(value);
65  }
66 
67  amrex::Vector<amrex::Real>* data_h = nullptr; // Declare pointer outside the loop
68 
69  // Read the file
70  for(int idx=0; idx<ndata; idx++){
71  if(idx == 0){
72  data_h = &rho_h;
73  } else if (idx==1) {
74  data_h = &uvel_h;
75  } else if (idx==2) {
76  data_h = &vvel_h;
77  } else if (idx==3) {
78  data_h = &wvel_h;
79  } else if(idx==4) {
80  data_h = &theta_h;
81  } else if(idx==5) {
82  data_h = &qv_h;
83  } else if(idx==6) {
84  data_h = &qc_h;
85  } else if(idx==7) {
86  data_h = &qr_h;
87  }
88  for(int k=0; k<nz; k++) {
89  for(int j=0; j<ny; j++) {
90  for(int i=0; i<nx; i++) {
91  infile.read(reinterpret_cast<char*>(&value), sizeof(float));
92  data_h->emplace_back(value);
93  }
94  }
95  }
96  }
97  infile.close();
98 }
99 #endif
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:12