ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_InflowProfile.cpp File Reference
#include <ERF_InflowProfile.H>
#include <ERF_Constants.H>
#include <AMReX.H>
#include <AMReX_Math.H>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <vector>
Include dependency graph for ERF_InflowProfile.cpp:

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, const Real ztop, InflowProfile &prof)
 

Function Documentation

◆ make_log_law_inflow_profile()

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

u = (u_star / kappa) ln((z + z0)/z0), so the wind is zero at the ground and speed at height, optionally capped at max_speed, blowing from direction. tke = u_star^2 / Cmu0^2 at the ground, the value erf.dirichlet_k holds in the first cell, tapering linearly to zero at u_star tke_zscale with the floor of SurfaceLayer::init_tke_from_ustar. The table has the ground and 399 heights spaced geometrically from 1 cm to ztop.

194 {
195  if (!(p.speed > Real(0.0))) { return "inflow_log_law.speed must be positive"; }
196  if (!(p.height > Real(0.0))) { return "inflow_log_law.height must be positive"; }
197  if (!(p.z0 > Real(0.0))) { return "the roughness length (inflow_log_law.z0 or erf.most.z0) must be positive"; }
198  if (!(p.tke_zscale > Real(0.0))) { return "inflow_log_law.tke_zscale must be positive"; }
199  if (!(p.Cmu0 > Real(0.0))) { return "Cmu0 must be positive"; }
200  if (!(ztop > Real(0.0))) { return "the domain height must be positive"; }
201 
202  const Real ustar = KAPPA * p.speed / std::log((p.height + p.z0) / p.z0);
203  const Real dir = p.direction * Math::pi<Real>() / Real(180.0);
204  const Real ex = -std::sin(dir);
205  const Real ey = -std::cos(dir);
206  const Real tke0 = ustar * ustar / (p.Cmu0 * p.Cmu0);
207  const Real small = Real(0.01);
208 
209  const int n = 400;
210  const Real zmin = std::min(Real(0.01), Real(0.5) * ztop);
211  const Real ratio = std::pow(ztop / zmin, Real(1.0) / static_cast<Real>(n - 2));
212 
213  Vector<Real> z(n), u(n), v(n), tke(n);
214  for (int m = 0; m < n; ++m) {
215  const Real zm = (m == 0) ? Real(0.0) : zmin * std::pow(ratio, static_cast<Real>(m - 1));
216  Real s = ustar / KAPPA * std::log((zm + p.z0) / p.z0);
217  if (p.max_speed > Real(0.0)) { s = std::min(s, p.max_speed); }
218  z[m] = zm;
219  u[m] = s * ex;
220  v[m] = s * ey;
221  tke[m] = tke0 * std::max((ustar * p.tke_zscale - zm) / (std::max(ustar, small) * p.tke_zscale), small);
222  }
223 
224  prof.z = z;
225  prof.u = u;
226  prof.v = v;
227  prof.theta.clear();
228  prof.tke = tke;
229  prof.has_theta = false;
230  prof.has_tke = true;
231  prof.active = true;
232  prof.source = "log_law";
233  return "";
234 }
constexpr amrex::Real KAPPA
Definition: ERF_Constants.H:71
const Real ztop
Definition: ERF_InitCustomPertVels_ParticleTests.H:4
@ ustar
Definition: ERF_SLM.H:30
@ p
Definition: ERF_WSM6.H:303
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
std::string source
"log_law" or the profile file name
Definition: ERF_InflowProfile.H:29
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 ERF::init_inflow_profile().

Here is the caller graph for this function:

◆ parse_inflow_profile()

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

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

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

Referenced by ERF::init_inflow_profile().

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