ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_InflowProfile.H File Reference
#include <istream>
#include <string>
#include <AMReX_Array4.H>
#include <AMReX_Gpu.H>
#include <AMReX_GpuContainers.H>
#include <AMReX_REAL.H>
#include <AMReX_Vector.H>
Include dependency graph for ERF_InflowProfile.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  InflowProfile
 
struct  InflowLogLaw
 

Functions

std::string parse_inflow_profile (std::istream &is, InflowProfile &prof)
 
void read_inflow_profile_file (const std::string &fname, InflowProfile &prof)
 
std::string make_log_law_inflow_profile (const InflowLogLaw &p, amrex::Real ztop, InflowProfile &prof)
 
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real inflow_profile_value (const amrex::Real *z, const amrex::Real *f, const int n, const amrex::Real zq) noexcept
 
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real height_above_ground (const amrex::Array4< const amrex::Real > &z_nd, int ilo, int ihi, int jlo, int jhi, const int k, const int klo) noexcept
 

Function Documentation

◆ height_above_ground()

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real height_above_ground ( const amrex::Array4< const amrex::Real > &  z_nd,
int  ilo,
int  ihi,
int  jlo,
int  jhi,
const int  k,
const int  klo 
)
noexcept

Mean height above the ground of the nodes [ilo,ihi] x [jlo,jhi] between levels k and k+1 of the terrain-fitted mesh: a cell center (two nodes in each lateral direction), an x-face (ilo == ihi) or a y-face (jlo == jhi). The ground is the same nodes on level klo. Indices are clamped to the array.

88 {
89  const auto lo = amrex::lbound(z_nd);
90  const auto hi = amrex::ubound(z_nd);
91  ilo = amrex::max(lo.x, amrex::min(ilo, hi.x));
92  ihi = amrex::max(lo.x, amrex::min(ihi, hi.x));
93  jlo = amrex::max(lo.y, amrex::min(jlo, hi.y));
94  jhi = amrex::max(lo.y, amrex::min(jhi, hi.y));
95  const int kc = amrex::max(lo.z, amrex::min(k, hi.z - 1));
96  const int kg = amrex::max(lo.z, amrex::min(klo, hi.z));
97  amrex::Real sum = amrex::Real(0.0);
98  int count = 0;
99  for (int j = jlo; j <= jhi; ++j) {
100  for (int i = ilo; i <= ihi; ++i) {
101  sum += amrex::Real(0.5) * (z_nd(i,j,kc) + z_nd(i,j,kc+1)) - z_nd(i,j,kg);
102  ++count;
103  }
104  }
105  return sum / static_cast<amrex::Real>(count);
106 }
int count
Definition: ERF_ActuatorSpreading.cpp:264
const int klo
Definition: ERF_InitCustomPert_KE.H:9

Referenced by ERFPhysBCFunct_cons::impose_inflow_profile_cons(), ERFPhysBCFunct_u::impose_inflow_profile_xvel(), and ERFPhysBCFunct_v::impose_inflow_profile_yvel().

Here is the caller graph for this function:

◆ inflow_profile_value()

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real inflow_profile_value ( const amrex::Real *  z,
const amrex::Real *  f,
const int  n,
const amrex::Real  zq 
)
noexcept

Linear interpolation in a strictly increasing table, held constant beyond both ends.

65 {
66  if (zq <= z[0]) { return f[0]; }
67  if (zq >= z[n-1]) { return f[n-1]; }
68  int lo = 0;
69  int hi = n - 1;
70  while (hi - lo > 1) {
71  const int mid = (lo + hi) / 2;
72  if (z[mid] <= zq) { lo = mid; } else { hi = mid; }
73  }
74  const amrex::Real w = (zq - z[lo]) / (z[hi] - z[lo]);
75  return f[lo] + w * (f[hi] - f[lo]);
76 }

Referenced by ERFPhysBCFunct_cons::impose_inflow_profile_cons(), ERFPhysBCFunct_u::impose_inflow_profile_xvel(), and ERFPhysBCFunct_v::impose_inflow_profile_yvel().

Here is the caller graph for this function:

◆ make_log_law_inflow_profile()

std::string make_log_law_inflow_profile ( const InflowLogLaw &  p,
amrex::Real  ztop,
InflowProfile &  prof 
)

Tabulate the log law from the ground to ztop; returns an empty string on success.

◆ parse_inflow_profile()

std::string parse_inflow_profile ( std::istream &  is,
InflowProfile &  prof 
)

Parse a profile table; returns an empty string on success, otherwise the error.

The layout of kynema-sgf's TabulatedProfile: one row per height, heights strictly increasing. A comment line whose first word is "z" names the columns (z u v w T tke, in any order; theta and k are accepted for T and tke); other comment lines are ignored. Without such a header, four columns are z u v T and five are z u v T tke. A w column is accepted and not used: the vertical velocity on the face keeps its boundary value. Heights are above the local ground.

54 {
55  std::vector<int> cols;
56  std::vector<std::vector<Real>> rows;
57  std::vector<int> row_line;
58 
59  std::string line;
60  int lineno = 0;
61  while (std::getline(is, line)) {
62  ++lineno;
63  const auto first = line.find_first_not_of(" \t\r");
64  if (first == std::string::npos) { continue; }
65 
66  if (line[first] == '#') {
67  std::istringstream ws(line.substr(first + 1));
68  std::vector<std::string> words;
69  for (std::string w; ws >> w; ) {
70  std::transform(w.begin(), w.end(), w.begin(),
71  [] (unsigned char c) { return static_cast<char>(std::tolower(c)); });
72  words.push_back(w);
73  }
74  if (words.empty() || words[0] != "z") { continue; }
75  if (!cols.empty() || !rows.empty()) {
76  return at_line(lineno) + "the column header must come once, before the data";
77  }
78  for (const auto& w : words) {
79  int c;
80  if (w == "z") { c = col_z; }
81  else if (w == "u") { c = col_u; }
82  else if (w == "v") { c = col_v; }
83  else if (w == "w") { c = col_w; }
84  else if (w == "t" || w == "theta") { c = col_theta; }
85  else if (w == "tke" || w == "k") { c = col_tke; }
86  else {
87  return at_line(lineno) + "unknown column '" + w + "' (accepted: z u v w T tke)";
88  }
89  if (std::find(cols.begin(), cols.end(), c) != cols.end()) {
90  return at_line(lineno) + "column '" + w + "' appears twice";
91  }
92  cols.push_back(c);
93  }
94  continue;
95  }
96 
97  std::istringstream ls(line);
98  std::vector<Real> vals;
99  for (std::string tok; ls >> tok; ) {
100  char* end = nullptr;
101  const double d = std::strtod(tok.c_str(), &end);
102  if (end == tok.c_str() || *end != '\0' || !std::isfinite(d)) {
103  return at_line(lineno) + "'" + tok + "' is not a number";
104  }
105  vals.push_back(static_cast<Real>(d));
106  }
107  rows.push_back(vals);
108  row_line.push_back(lineno);
109  }
110 
111  if (rows.empty()) { return "no data rows"; }
112  if (cols.empty()) {
113  const auto nc = rows[0].size();
114  if (nc == 4) {
115  cols = {col_z, col_u, col_v, col_theta};
116  } else if (nc == 5) {
117  cols = {col_z, col_u, col_v, col_theta, col_tke};
118  } else {
119  return "a table without a '# z ...' header needs 4 columns (z u v T) or 5 (z u v T tke), not "
120  + std::to_string(nc);
121  }
122  }
123  auto has = [&cols] (int c) { return std::find(cols.begin(), cols.end(), c) != cols.end(); };
124  if (!has(col_z) || !has(col_u) || !has(col_v)) {
125  return "the columns must include z, u and v";
126  }
127  if (rows.size() < 2) { return "at least two heights are needed"; }
128 
129  Vector<Real> z, u, v, theta, tke;
130  for (std::size_t r = 0; r < rows.size(); ++r) {
131  if (rows[r].size() != cols.size()) {
132  return at_line(row_line[r]) + "expected " + std::to_string(cols.size()) +
133  " columns, found " + std::to_string(rows[r].size());
134  }
135  for (std::size_t c = 0; c < cols.size(); ++c) {
136  const Real x = rows[r][c];
137  switch (cols[c]) {
138  case col_z: z.push_back(x); break;
139  case col_u: u.push_back(x); break;
140  case col_v: v.push_back(x); break;
141  case col_theta: theta.push_back(x); break;
142  case col_tke: tke.push_back(x); break;
143  default: break;
144  }
145  }
146  }
147  for (Long r = 1; r < z.size(); ++r) {
148  if (!(z[r] > z[r-1])) {
149  return at_line(row_line[r]) + "heights must increase strictly";
150  }
151  }
152  for (Long r = 0; r < theta.size(); ++r) {
153  if (!(theta[r] > Real(0.0))) { return at_line(row_line[r]) + "T must be positive"; }
154  }
155  for (Long r = 0; r < tke.size(); ++r) {
156  if (tke[r] < Real(0.0)) { return at_line(row_line[r]) + "tke must not be negative"; }
157  }
158 
159  prof.z = z;
160  prof.u = u;
161  prof.v = v;
162  prof.theta = theta;
163  prof.tke = tke;
164  prof.has_theta = !theta.empty();
165  prof.has_tke = !tke.empty();
166  prof.active = true;
167  return "";
168 }
@ theta
Definition: ERF_Kessler.H:26
@ nc
Definition: ERF_Morrison.H:47
amrex::Vector< amrex::Real > tke
Definition: ERF_InflowProfile.H:32
bool has_tke
Definition: ERF_InflowProfile.H:28
amrex::Vector< amrex::Real > theta
Definition: ERF_InflowProfile.H:32
amrex::Vector< amrex::Real > v
Definition: ERF_InflowProfile.H:32
amrex::Vector< amrex::Real > z
Host tables against height above the ground (strictly increasing)
Definition: ERF_InflowProfile.H:32
bool has_theta
Definition: ERF_InflowProfile.H:27
bool active
Definition: ERF_InflowProfile.H:26
amrex::Vector< amrex::Real > u
Definition: ERF_InflowProfile.H:32

Referenced by read_inflow_profile_file().

Here is the caller graph for this function:

◆ read_inflow_profile_file()

void read_inflow_profile_file ( const std::string &  fname,
InflowProfile &  prof 
)

Read a profile file, aborting with the file name on any error.

172 {
173  std::ifstream is(fname);
174  if (!is.is_open()) {
175  Abort("inflow_profile_file '" + fname + "' could not be opened");
176  }
177  const std::string err = parse_inflow_profile(is, prof);
178  if (!err.empty()) {
179  Abort("inflow_profile_file '" + fname + "': " + err);
180  }
181  prof.source = fname;
182 }
std::string parse_inflow_profile(std::istream &is, InflowProfile &prof)
Definition: ERF_InflowProfile.cpp:53
std::string source
"log_law" or the profile file name
Definition: ERF_InflowProfile.H:29

Referenced by ERF::init_inflow_profile().

Here is the call graph for this function:
Here is the caller graph for this function: