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:
21  /**
22  * @brief Construct input sponge metadata from the ERF input namespace.
23  */
25  {
26  // Text input_sounding file
27  amrex::ParmParse pp("erf");
28  pp.query("input_sponge_file", input_sponge_file);
29  }
30 
31  /**
32  * @brief Read and interpolate the input sponge profile onto level-0 heights.
33  * @param geom Geometry defining the level-0 vertical domain.
34  * @param zlevels_stag Staggered vertical coordinates used for interpolation.
35  */
36  void read_from_file (const amrex::Geometry &geom,
37  const amrex::Vector<amrex::Real>& zlevels_stag)
38  {
39  const int klo = 0;
40  const int khi = geom.Domain().bigEnd()[AMREX_SPACEDIM-1];
41  const int Nz = geom.Domain().size()[AMREX_SPACEDIM-1];
42 
43  const amrex::Real zbot = zlevels_stag[klo];
44  const amrex::Real ztop = zlevels_stag[khi+1];
45 
46  z_inp_sponge.resize(Nz+2);
47  U_inp_sponge.resize(Nz+2);
48  V_inp_sponge.resize(Nz+2);
49 
50  // Read the input_sponge file
51  amrex::Print() << "input_sponge file location : " << input_sponge_file << std::endl;
52  std::ifstream input_sponge_reader(input_sponge_file);
53  if(!input_sponge_reader.is_open()) {
54  amrex::Error("Error opening the input_sponge file.\n");
55  }
56  else {
57  // Read the contents of the input_sponge file
58  amrex::Print() << "Successfully opened the input_sponge file. Now reading... " << std::endl;
59  std::string line;
60 
61  // First, read the input data into temp vectors; then, interpolate vectors to the
62  // domain lo/hi and cell centers (from level 0)
63  amrex::Vector<amrex::Real> z_inp_sponge_tmp, U_inp_sponge_tmp, V_inp_sponge_tmp;
64 
65  // Add surface
66  z_inp_sponge_tmp.push_back(zbot); // height above sea level [m]
67  U_inp_sponge_tmp.push_back(0);
68  V_inp_sponge_tmp.push_back(0);
69 
70  // Read the vertical profile at each given height
71  amrex::Real z, U, V;
72  while(std::getline(input_sponge_reader, line)) {
73  std::istringstream iss_z(line);
74  iss_z >> z >> U >> V;
75  if (z == zbot) {
76  U_inp_sponge_tmp[0] = U;
77  V_inp_sponge_tmp[0] = V;
78  } else {
79  AMREX_ALWAYS_ASSERT(z > z_inp_sponge_tmp[z_inp_sponge_tmp.size()-1]); // sounding is increasing in height
80  z_inp_sponge_tmp.push_back(z);
81  U_inp_sponge_tmp.push_back(U);
82  V_inp_sponge_tmp.push_back(V);
83  if (z >= ztop) break;
84  }
85  }
86 
87  // At this point, we have an input_sponge from zbot up to
88  // z_inp_sponge_tmp[N-1] >= ztop. Now, interpolate to grid level 0 heights
89  const int Ninp = z_inp_sponge_tmp.size();
90  z_inp_sponge[0] = zbot;
91  U_inp_sponge[0] = U_inp_sponge_tmp[0];
92  V_inp_sponge[0] = V_inp_sponge_tmp[0];
93  for (int k=0; k < Nz; ++k) {
94  z_inp_sponge[k+1] = myhalf * (zlevels_stag[k] + zlevels_stag[k+1]);
95  U_inp_sponge[k+1] = interpolate_1d(z_inp_sponge_tmp.dataPtr(), U_inp_sponge_tmp.dataPtr(), z_inp_sponge[k+1], Ninp);
96  V_inp_sponge[k+1] = interpolate_1d(z_inp_sponge_tmp.dataPtr(), V_inp_sponge_tmp.dataPtr(), z_inp_sponge[k+1], Ninp);
97  }
98  z_inp_sponge[Nz+1] = ztop;
99  U_inp_sponge[Nz+1] = interpolate_1d(z_inp_sponge_tmp.dataPtr(), U_inp_sponge_tmp.dataPtr(), ztop, Ninp);
100  V_inp_sponge[Nz+1] = interpolate_1d(z_inp_sponge_tmp.dataPtr(), V_inp_sponge_tmp.dataPtr(), ztop, Ninp);
101  }
102 
103  amrex::Print() << "Successfully read the input_sponge file..." << std::endl;
104  input_sponge_reader.close();
105  }
106 
107  /**
108  * @brief Return the number of interpolated input sponge samples.
109  * @return Number of stored sponge profile levels.
110  */
111  int size () const
112  {
115  return z_inp_sponge.size();
116  }
117 
118  // Members
119 
120  std::string input_sponge_file = "input_sponge_file.txt";
121 
122  // - read from file
123  amrex::Vector<amrex::Real> z_inp_sponge, U_inp_sponge, V_inp_sponge;
124 };
125 
126 /**
127  * @var InputSpongeData::input_sponge_file
128  * @brief Path to the text file containing the sponge profile.
129  * @var InputSpongeData::z_inp_sponge
130  * @brief Interpolated sponge profile heights [m].
131  * @var InputSpongeData::U_inp_sponge
132  * @brief Interpolated target x velocity for sponge damping.
133  * @var InputSpongeData::V_inp_sponge
134  * @brief Interpolated target y velocity for sponge damping.
135  */
136 #endif
constexpr amrex::Real myhalf
Definition: ERF_Constants.H:13
const Real ztop
Definition: ERF_InitCustomPertVels_ParticleTests.H:4
ParmParse pp("prob")
const int khi
Definition: ERF_InitCustomPert_Bubble.H:21
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
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:14
amrex::Real Real
Definition: ERF_ShocInterface.H:19
@ U
Definition: ERF_IndexDefines.H:123
@ V
Definition: ERF_IndexDefines.H:124
Definition: ERF_InputSpongeData.H:19
InputSpongeData()
Construct input sponge metadata from the ERF input namespace.
Definition: ERF_InputSpongeData.H:24
amrex::Vector< amrex::Real > z_inp_sponge
Interpolated sponge profile heights [m].
Definition: ERF_InputSpongeData.H:123
std::string input_sponge_file
Path to the text file containing the sponge profile.
Definition: ERF_InputSpongeData.H:120
void read_from_file(const amrex::Geometry &geom, const amrex::Vector< amrex::Real > &zlevels_stag)
Read and interpolate the input sponge profile onto level-0 heights.
Definition: ERF_InputSpongeData.H:36
amrex::Vector< amrex::Real > V_inp_sponge
Interpolated target y velocity for sponge damping.
Definition: ERF_InputSpongeData.H:123
amrex::Vector< amrex::Real > U_inp_sponge
Interpolated target x velocity for sponge damping.
Definition: ERF_InputSpongeData.H:123
int size() const
Return the number of interpolated input sponge samples.
Definition: ERF_InputSpongeData.H:111