ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_InputSpongeData.H
Go to the documentation of this file.
1 #ifndef ERF_INPUT_SPONGE_DATA_H_
2 #define ERF_INPUT_SPONGE_DATA_H_
3 
4 #include <string>
5 #include <iostream>
6 
7 #include <AMReX_ParmParse.H>
8 #include <AMReX_Print.H>
9 #include <AMReX_Gpu.H>
10 #include <AMReX_Geometry.H>
11 
12 #include <ERF_Constants.H>
13 #include <ERF_Interpolation_1D.H>
14 
15 /**
16  * Data structure storing input sponge data. Also
17  * handles reading the input file for sponge data
18  */
20 public:
22  {
23  // Text input_sounding file
24  amrex::ParmParse pp("erf");
25  pp.query("input_sponge_file", input_sponge_file);
26  }
27 
28  void read_from_file (const amrex::Geometry &geom,
29  const amrex::Vector<amrex::Real>& zlevels_stag)
30  {
31  const int klo = 0;
32  const int khi = geom.Domain().bigEnd()[AMREX_SPACEDIM-1];
33  const int Nz = geom.Domain().size()[AMREX_SPACEDIM-1];
34 
35  const amrex::Real zbot = zlevels_stag[klo];
36  const amrex::Real ztop = zlevels_stag[khi+1];
37 
38  z_inp_sponge.resize(Nz+2);
39  U_inp_sponge.resize(Nz+2);
40  V_inp_sponge.resize(Nz+2);
41 
42  // Read the input_sponge file
43  amrex::Print() << "input_sponge file location : " << input_sponge_file << std::endl;
44  std::ifstream input_sponge_reader(input_sponge_file);
45  if(!input_sponge_reader.is_open()) {
46  amrex::Error("Error opening the input_sponge file.\n");
47  }
48  else {
49  // Read the contents of the input_sponge file
50  amrex::Print() << "Successfully opened the input_sponge file. Now reading... " << std::endl;
51  std::string line;
52 
53  // First, read the input data into temp vectors; then, interpolate vectors to the
54  // domain lo/hi and cell centers (from level 0)
55  amrex::Vector<amrex::Real> z_inp_sponge_tmp, U_inp_sponge_tmp, V_inp_sponge_tmp;
56 
57  // Add surface
58  z_inp_sponge_tmp.push_back(zbot); // height above sea level [m]
59  U_inp_sponge_tmp.push_back(0);
60  V_inp_sponge_tmp.push_back(0);
61 
62  // Read the vertical profile at each given height
63  amrex::Real z, U, V;
64  while(std::getline(input_sponge_reader, line)) {
65  std::istringstream iss_z(line);
66  iss_z >> z >> U >> V;
67  if (z == zbot) {
68  U_inp_sponge_tmp[0] = U;
69  V_inp_sponge_tmp[0] = V;
70  } else {
71  AMREX_ALWAYS_ASSERT(z > z_inp_sponge_tmp[z_inp_sponge_tmp.size()-1]); // sounding is increasing in height
72  z_inp_sponge_tmp.push_back(z);
73  U_inp_sponge_tmp.push_back(U);
74  V_inp_sponge_tmp.push_back(V);
75  if (z >= ztop) break;
76  }
77  }
78 
79  // At this point, we have an input_sponge from zbot up to
80  // z_inp_sponge_tmp[N-1] >= ztop. Now, interpolate to grid level 0 heights
81  const int Ninp = z_inp_sponge_tmp.size();
82  z_inp_sponge[0] = zbot;
83  U_inp_sponge[0] = U_inp_sponge_tmp[0];
84  V_inp_sponge[0] = V_inp_sponge_tmp[0];
85  for (int k=0; k < Nz; ++k) {
86  z_inp_sponge[k+1] = 0.5 * (zlevels_stag[k] + zlevels_stag[k+1]);
87  U_inp_sponge[k+1] = interpolate_1d(z_inp_sponge_tmp.dataPtr(), U_inp_sponge_tmp.dataPtr(), z_inp_sponge[k+1], Ninp);
88  V_inp_sponge[k+1] = interpolate_1d(z_inp_sponge_tmp.dataPtr(), V_inp_sponge_tmp.dataPtr(), z_inp_sponge[k+1], Ninp);
89  }
90  z_inp_sponge[Nz+1] = ztop;
91  U_inp_sponge[Nz+1] = interpolate_1d(z_inp_sponge_tmp.dataPtr(), U_inp_sponge_tmp.dataPtr(), ztop, Ninp);
92  V_inp_sponge[Nz+1] = interpolate_1d(z_inp_sponge_tmp.dataPtr(), V_inp_sponge_tmp.dataPtr(), ztop, Ninp);
93  }
94 
95  amrex::Print() << "Successfully read the input_sponge file..." << std::endl;
96  input_sponge_reader.close();
97  }
98 
99  int size () const
100  {
101  AMREX_ALWAYS_ASSERT(z_inp_sponge.size() == U_inp_sponge.size());
102  AMREX_ALWAYS_ASSERT(z_inp_sponge.size() == V_inp_sponge.size());
103  return z_inp_sponge.size();
104  }
105 
106  // Members
107 
108  std::string input_sponge_file = "input_sponge_file.txt";
109 
110  // - read from file
111  amrex::Vector<amrex::Real> z_inp_sponge, U_inp_sponge, V_inp_sponge;
112 };
113 #endif
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real interpolate_1d(const amrex::Real *alpha, const amrex::Real *beta, const amrex::Real alpha_interp, const int alpha_size)
Definition: ERF_Interpolation_1D.H:12
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real pp(amrex::Real y)
Definition: ERF_MicrophysicsUtils.H:233
amrex::Real Real
Definition: ERF_ShocInterface.H:19
@ U
Definition: ERF_IndexDefines.H:108
@ V
Definition: ERF_IndexDefines.H:109
Definition: ERF_InputSpongeData.H:19
InputSpongeData()
Definition: ERF_InputSpongeData.H:21
void read_from_file(const amrex::Geometry &geom, const amrex::Vector< amrex::Real > &zlevels_stag)
Definition: ERF_InputSpongeData.H:28
amrex::Vector< amrex::Real > V_inp_sponge
Definition: ERF_InputSpongeData.H:111
std::string input_sponge_file
Definition: ERF_InputSpongeData.H:108
amrex::Vector< amrex::Real > z_inp_sponge
Definition: ERF_InputSpongeData.H:111
amrex::Vector< amrex::Real > U_inp_sponge
Definition: ERF_InputSpongeData.H:111
int size() const
Definition: ERF_InputSpongeData.H:99