ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
erf_actuator::WakeLines Class Reference

#include <ERF_WakeLines.H>

Collaboration diagram for erf_actuator::WakeLines:

Public Member Functions

 WakeLines (std::string name, std::string output_root, const std::array< amrex::Real, 3 > &hub, const std::array< amrex::Real, 3 > &axis, amrex::Real diameter, const std::vector< amrex::Real > &lines_xD, amrex::Real half_width, int num_points, amrex::Real z_min)
 
 WakeLines (std::string name, std::string output_root, const std::array< amrex::Real, 3 > &hub, const std::array< amrex::Real, 3 > &axis, amrex::Real diameter, const std::vector< amrex::Real > &lines_xD, amrex::Real half_width, int num_points, const std::vector< amrex::Real > &z_min)
 
const std::string & name () const
 
int num_points () const
 
const std::vector< amrex::Real > & positions () const
 
const std::array< amrex::Real, 3 > & hub () const
 
const std::array< amrex::Real, 3 > & axis () const
 
amrex::Real diameter () const
 
int num_samples () const
 
void accumulate (const std::vector< amrex::Real > &vel)
 
std::vector< amrex::Real > average () const
 
void write_instantaneous (double time, const std::vector< amrex::Real > &vel, bool truncate) const
 
void write_average () const
 
void write_state (const std::string &dir) const
 
bool read_state (const std::string &dir)
 

Private Member Functions

void write_rows (std::ofstream &out, const std::vector< amrex::Real > &vel, const std::string &prefix) const
 

Private Attributes

std::string m_name
 
std::string m_output_root
 
std::array< amrex::Real, 3 > m_hub
 
std::array< amrex::Real, 3 > m_axis
 
std::array< amrex::Real, 3 > m_lateral
 
amrex::Real m_diameter {0.0}
 
std::vector< amrex::Real > m_xD
 
amrex::Real m_half_width {1.5}
 
int m_npts {0}
 
std::vector< amrex::Real > m_s
 n offsets along the lines, in diameters More...
 
std::vector< amrex::Real > m_pos
 3*n More...
 
std::vector< double > m_sum
 3*n running sums, in double whatever Real is More...
 
int m_count {0}
 

Detailed Description

Wake sampling lines behind one rotor: at each downstream distance x/D along the horizontal projection of the rotor axis through the hub (a wake follows the wind at hub height, not a tilted shaft), one lateral line (horizontal, normal to that direction) and one vertical line, each of num_points points spanning +-half_width diameters about the center. The sampled velocities are written as they are and accumulated into a running time average.

Point order: for each x/D, the lateral line then the vertical line, each from -half_width to +half_width; the vertical line is clipped at z_min (the ground), so its lowest point may sit above -half_width. Every point's offset s (in diameters) is recorded in the files. The lateral line runs along (-axis_y, axis_x, 0), from the right of the downstream direction to its left; the vertical line runs up. Positions in m, ERF domain coordinates; velocities in m/s.

Constructor & Destructor Documentation

◆ WakeLines() [1/2]

erf_actuator::WakeLines::WakeLines ( std::string  name,
std::string  output_root,
const std::array< amrex::Real, 3 > &  hub,
const std::array< amrex::Real, 3 > &  axis,
amrex::Real  diameter,
const std::vector< amrex::Real > &  lines_xD,
amrex::Real  half_width,
int  num_points,
amrex::Real  z_min 
)

Build the sampling points.

Parameters
hubthe rotor's hub (m)
axisthe rotor axis (any non-vertical vector; its horizontal projection is used)
diameterthe rotor diameter (m, > 0)
lines_xDdownstream distances of the lines, in diameters (at least one)
half_widthhalf the length of each line, in diameters (> 0)
num_pointspoints per line (>= 2)
z_minthe ground under the hub (m, finite): the vertical line starts no lower

◆ WakeLines() [2/2]

erf_actuator::WakeLines::WakeLines ( std::string  name,
std::string  output_root,
const std::array< amrex::Real, 3 > &  hub,
const std::array< amrex::Real, 3 > &  axis,
amrex::Real  diameter,
const std::vector< amrex::Real > &  lines_xD,
amrex::Real  half_width,
int  num_points,
const std::vector< amrex::Real > &  z_min 
)

The same with one ground height per downstream distance (z_min[n] under the center of the lines at lines_xD[n], from terrain_heights), so that on terrain rising or falling downstream each vertical line stops at its own ground. z_min must have lines_xD's size.

Member Function Documentation

◆ accumulate()

void erf_actuator::WakeLines::accumulate ( const std::vector< amrex::Real > &  vel)

Add one set of sampled velocities (3*n, m/s, finite) to the running average.

82 {
83  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(vel.size() == m_pos.size(), "WakeLines::accumulate: one velocity per point");
84  for (std::size_t i = 0; i < vel.size(); ++i) {
85  if (!std::isfinite(vel[i])) {
86  Abort("WakeLines " + m_name + ": the sampled velocity at point " + std::to_string(i / 3) + " (0-based) is not finite");
87  }
88  }
89  for (std::size_t i = 0; i < vel.size(); ++i) { m_sum[i] += static_cast<double>(vel[i]); }
90  ++m_count;
91 }
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
std::string m_name
Definition: ERF_WakeLines.H:97
std::vector< double > m_sum
3*n running sums, in double whatever Real is
Definition: ERF_WakeLines.H:105
std::vector< amrex::Real > m_pos
3*n
Definition: ERF_WakeLines.H:104
int m_count
Definition: ERF_WakeLines.H:106
Here is the call graph for this function:

◆ average()

std::vector< Real > erf_actuator::WakeLines::average ( ) const

The running mean velocities (3*n); zeros before the first sample.

95 {
96  std::vector<Real> avg(m_pos.size(), 0.0);
97  if (m_count > 0) {
98  for (std::size_t i = 0; i < avg.size(); ++i) { avg[i] = static_cast<Real>(m_sum[i] / m_count); }
99  }
100  return avg;
101 }

◆ axis()

const std::array<amrex::Real,3>& erf_actuator::WakeLines::axis ( ) const
inline

The unit downstream direction: the rotor axis projected on the horizontal plane.

62 { return m_axis; }
std::array< amrex::Real, 3 > m_axis
Definition: ERF_WakeLines.H:98

◆ diameter()

amrex::Real erf_actuator::WakeLines::diameter ( ) const
inline
63 { return m_diameter; }
amrex::Real m_diameter
Definition: ERF_WakeLines.H:99

◆ hub()

const std::array<amrex::Real,3>& erf_actuator::WakeLines::hub ( ) const
inline
60 { return m_hub; }
std::array< amrex::Real, 3 > m_hub
Definition: ERF_WakeLines.H:98

◆ name()

const std::string& erf_actuator::WakeLines::name ( ) const
inline
56 { return m_name; }

◆ num_points()

int erf_actuator::WakeLines::num_points ( ) const
inline
57 { return static_cast<int>(m_pos.size() / 3); }

◆ num_samples()

int erf_actuator::WakeLines::num_samples ( ) const
inline
64 { return m_count; }

◆ positions()

const std::vector<amrex::Real>& erf_actuator::WakeLines::positions ( ) const
inline

3*n positions of the sampling points (m).

59 { return m_pos; }

◆ read_state()

bool erf_actuator::WakeLines::read_state ( const std::string &  dir)

Restore the state written by write_state(), geometry included; false when there is none. Aborts, changing nothing, when the file is malformed: a negative count, a diameter that is not positive, an axis that is not a horizontal unit vector, or a non-finite value.

161 {
162  const std::string fname = dir + "/" + m_name + "_wake_avg.dat";
163  // decided on the I/O rank for every rank: the read below is collective
164  if (!file_exists_everywhere(fname)) {
165  Print() << "WakeLines " << m_name << ": no " << fname << " in the checkpoint; the wake average starts afresh\n";
166  return false;
167  }
168  Vector<char> chars;
169  ParallelDescriptor::ReadAndBcastFile(fname, chars);
170  std::istringstream in(std::string(chars.dataPtr(), chars.size()));
171  std::string key, eq;
172  std::size_t size = 0;
173  int count = 0;
174  std::array<Real,3> hub, axis;
175  Real diameter = 0.0;
176  if (!(in >> key >> eq >> count) || key != "count" || !(in >> key >> eq >> size) || key != "size" ||
177  !(in >> key >> eq >> hub[0] >> hub[1] >> hub[2]) || key != "hub" ||
178  !(in >> key >> eq >> axis[0] >> axis[1] >> axis[2]) || key != "axis" ||
179  !(in >> key >> eq >> diameter) || key != "diameter") {
180  Abort("malformed wake-average checkpoint '" + fname + "'");
181  }
182  if (size != m_sum.size()) {
183  Abort("the wake-average checkpoint '" + fname + "' holds " + std::to_string(size) + " values but the lines of " +
184  m_name + " have " + std::to_string(m_sum.size()) + "; the wake inputs must match the run being restarted");
185  }
186  const Real naxis = std::sqrt(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]);
187  const bool finite = std::isfinite(hub[0]) && std::isfinite(hub[1]) && std::isfinite(hub[2]) && std::isfinite(naxis);
188  if (count < 0 || !finite || !(std::isfinite(diameter) && diameter > 0.0) ||
189  !(std::abs(naxis - Real(1.0)) < Real(1.0e-4) && std::abs(axis[2]) < Real(1.0e-4))) {
190  Abort("malformed wake-average checkpoint '" + fname + "': the count must be >= 0, the diameter positive, "
191  "the axis a horizontal unit vector and every value finite");
192  }
193  // the geometry is taken over as it was: a body model may rebuild the hub and diameter from
194  // lower-precision data and differ at the last digits from one build of the lines to another
195  std::vector<Real> s(m_s.size()), pos(size);
196  std::vector<double> sum(size);
197  auto read_all = [&] (auto& v) {
198  for (auto& x : v) {
199  if (!(in >> x)) { Abort("truncated wake-average checkpoint '" + fname + "'"); }
200  if (!std::isfinite(x)) { Abort("malformed wake-average checkpoint '" + fname + "': a value is not finite"); }
201  }
202  };
203  read_all(s);
204  read_all(pos);
205  read_all(sum);
206  m_s = s;
207  m_pos = pos;
208  m_sum = sum;
209  m_hub = hub;
210  m_axis = axis;
212  m_count = count;
213  return true;
214 }
std::vector< std::array< Real, 3 > > pos
Definition: ERF_ActuatorSpreading.cpp:220
int count
Definition: ERF_ActuatorSpreading.cpp:264
std::vector< amrex::Real > m_s
n offsets along the lines, in diameters
Definition: ERF_WakeLines.H:103
const std::array< amrex::Real, 3 > & hub() const
Definition: ERF_WakeLines.H:60
const std::array< amrex::Real, 3 > & axis() const
Definition: ERF_WakeLines.H:62
amrex::Real diameter() const
Definition: ERF_WakeLines.H:63
bool file_exists_everywhere(const std::string &fname)
Definition: ERF_DiagnosticsLog.H:43
bool finite(amrex::Real value) noexcept
Definition: ERF_CloudChamber.H:362
Here is the call graph for this function:

◆ write_average()

void erf_actuator::WakeLines::write_average ( ) const

Rewrite <output_root>_wake_avg.csv with the running average (the file always holds the current mean).

136 {
137  if (!ParallelDescriptor::IOProcessor()) { return; }
138  std::ofstream out;
139  open_log(out, m_output_root + "_wake_avg.csv", true);
140  out << "samples,xD,line,s,x,y,z,u,v,w\n" << std::setprecision(10);
141  write_rows(out, average(), std::to_string(m_count) + ",");
142 }
std::vector< amrex::Real > average() const
Definition: ERF_WakeLines.cpp:94
std::string m_output_root
Definition: ERF_WakeLines.H:97
void write_rows(std::ofstream &out, const std::vector< amrex::Real > &vel, const std::string &prefix) const
Definition: ERF_WakeLines.cpp:105
bool open_log(std::ofstream &out, const std::string &fname, bool truncate)
Definition: ERF_DiagnosticsLog.H:22
Here is the call graph for this function:

◆ write_instantaneous()

void erf_actuator::WakeLines::write_instantaneous ( double  time,
const std::vector< amrex::Real > &  vel,
bool  truncate 
) const

Write the sampled velocities to <output_root>_wake.csv, one row per point: truncate starts the file afresh, otherwise the rows are appended; the header is written when open_log() asks for one. Units: time s, x y z m, u v w m/s.

121 {
122  if (!ParallelDescriptor::IOProcessor()) { return; }
123  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(vel.size() == m_pos.size(), "WakeLines::write_instantaneous: one velocity per point");
124  std::ofstream out;
125  if (open_log(out, m_output_root + "_wake.csv", truncate)) {
126  out << "time,xD,line,s,x,y,z,u,v,w\n";
127  }
128  out << std::setprecision(10);
129  std::ostringstream prefix;
130  prefix << std::setprecision(10) << time << ",";
131  write_rows(out, vel, prefix.str());
132 }
Here is the call graph for this function:

◆ write_rows()

void erf_actuator::WakeLines::write_rows ( std::ofstream &  out,
const std::vector< amrex::Real > &  vel,
const std::string &  prefix 
) const
private
106 {
107  int p = 0;
108  for (const Real xD : m_xD) {
109  for (const char* line : {"lateral", "vertical"}) {
110  for (int i = 0; i < m_npts; ++i, ++p) {
111  out << prefix << xD << "," << line << "," << m_s[p] << ","
112  << m_pos[3*p] << "," << m_pos[3*p+1] << "," << m_pos[3*p+2] << ","
113  << vel[3*p] << "," << vel[3*p+1] << "," << vel[3*p+2] << "\n";
114  }
115  }
116  }
117 }
std::vector< amrex::Real > m_xD
Definition: ERF_WakeLines.H:100
int m_npts
Definition: ERF_WakeLines.H:102
@ p
Definition: ERF_WSM6.H:303

◆ write_state()

void erf_actuator::WakeLines::write_state ( const std::string &  dir) const

The lines' state for a checkpoint, <dir>/<name>_wake_avg.dat: the sampling geometry (hub, axis, diameter, the points) and the running sums. The geometry is stored because a body model may rebuild the hub and diameter from lower-precision data after a restart and land a few ulps away; the stored points keep the average on the same positions.

146 {
147  if (!ParallelDescriptor::IOProcessor()) { return; }
148  std::ofstream out(dir + "/" + m_name + "_wake_avg.dat", std::ios::trunc);
149  if (!out) { Abort("cannot write the wake-average checkpoint for " + m_name + " in '" + dir + "'"); }
150  out << std::setprecision(17) << "count = " << m_count << "\nsize = " << m_sum.size() << "\n"
151  << "hub = " << m_hub[0] << " " << m_hub[1] << " " << m_hub[2] << "\n"
152  << "axis = " << m_axis[0] << " " << m_axis[1] << " " << m_axis[2] << "\n"
153  << "diameter = " << m_diameter << "\n";
154  for (const Real s : m_s) { out << s << "\n"; }
155  for (const Real x : m_pos) { out << x << "\n"; }
156  for (const double s : m_sum) { out << s << "\n"; }
157 }

Member Data Documentation

◆ m_axis

std::array<amrex::Real,3> erf_actuator::WakeLines::m_axis
private

Referenced by axis().

◆ m_count

int erf_actuator::WakeLines::m_count {0}
private

Referenced by num_samples().

◆ m_diameter

amrex::Real erf_actuator::WakeLines::m_diameter {0.0}
private

Referenced by diameter().

◆ m_half_width

amrex::Real erf_actuator::WakeLines::m_half_width {1.5}
private

◆ m_hub

std::array<amrex::Real,3> erf_actuator::WakeLines::m_hub
private

Referenced by hub().

◆ m_lateral

std::array<amrex::Real,3> erf_actuator::WakeLines::m_lateral
private

◆ m_name

std::string erf_actuator::WakeLines::m_name
private

Referenced by name().

◆ m_npts

int erf_actuator::WakeLines::m_npts {0}
private

◆ m_output_root

std::string erf_actuator::WakeLines::m_output_root
private

◆ m_pos

std::vector<amrex::Real> erf_actuator::WakeLines::m_pos
private

3*n

Referenced by num_points(), and positions().

◆ m_s

std::vector<amrex::Real> erf_actuator::WakeLines::m_s
private

n offsets along the lines, in diameters

◆ m_sum

std::vector<double> erf_actuator::WakeLines::m_sum
private

3*n running sums, in double whatever Real is

◆ m_xD

std::vector<amrex::Real> erf_actuator::WakeLines::m_xD
private

The documentation for this class was generated from the following files: