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

#include <ERF_RunningStats.H>

Collaboration diagram for erf_actuator::RunningStats:

Public Member Functions

 RunningStats (std::string name, std::string output_root, std::vector< std::string > quantities)
 
const std::string & name () const
 
const std::vector< std::string > & quantities () const
 
void renamed_from (const std::string &old_name, const std::string &now, bool same)
 
int num_samples () const
 
double t_first () const
 
double t_last () const
 
void accumulate (double t, const std::vector< amrex::Real > &values)
 
amrex::Real mean (std::size_t i) const
 
amrex::Real rms (std::size_t i) const
 
amrex::Real min (std::size_t i) const
 
amrex::Real max (std::size_t i) const
 
void write () const
 
void write_state (const std::string &dir) const
 
bool read_state (const std::string &dir)
 

Private Attributes

std::string m_name
 
std::string m_output_root
 
std::vector< std::string > m_names
 
std::vector< double > m_sum
 
std::vector< double > m_sum2
 in double: a float sum over many steps would lose the samples More...
 
std::vector< amrex::Real > m_min
 
std::vector< amrex::Real > m_max
 
int m_count {0}
 
double m_t_first {0.0}
 
double m_t_last {0.0}
 
std::map< std::string, std::pair< std::string, bool > > m_renamed
 old name -> (name now, same quantity) More...
 

Detailed Description

One set of running statistics: the mean, the root mean square, the minimum and the maximum of each named scalar quantity over every sample added since the caller's averaging start. write() rewrites <output_root>_stats.csv with the current values; the sums are part of the checkpoint, so the statistics continue across a restart. A non-finite sample aborts, naming the statistics and the quantity.

Constructor & Destructor Documentation

◆ RunningStats()

erf_actuator::RunningStats::RunningStats ( std::string  name,
std::string  output_root,
std::vector< std::string >  quantities 
)
inline
38  : m_name(std::move(name)), m_output_root(std::move(output_root)), m_names(std::move(quantities)),
39  m_sum(m_names.size(), 0.0), m_sum2(m_names.size(), 0.0),
40  m_min(m_names.size(), std::numeric_limits<amrex::Real>::max()),
41  m_max(m_names.size(), std::numeric_limits<amrex::Real>::lowest()) {}
std::vector< double > m_sum2
in double: a float sum over many steps would lose the samples
Definition: ERF_RunningStats.H:180
const std::vector< std::string > & quantities() const
Definition: ERF_RunningStats.H:44
const std::string & name() const
Definition: ERF_RunningStats.H:43
std::vector< double > m_sum
Definition: ERF_RunningStats.H:180
std::vector< amrex::Real > m_min
Definition: ERF_RunningStats.H:181
std::vector< std::string > m_names
Definition: ERF_RunningStats.H:179
std::vector< amrex::Real > m_max
Definition: ERF_RunningStats.H:181
std::string m_name
Definition: ERF_RunningStats.H:178
std::string m_output_root
Definition: ERF_RunningStats.H:178

Member Function Documentation

◆ accumulate()

void erf_actuator::RunningStats::accumulate ( double  t,
const std::vector< amrex::Real > &  values 
)
inline

Add one sample of every quantity, taken at time t (s).

61  {
62  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(values.size() == m_names.size(), "RunningStats::accumulate: one value per quantity");
63  for (std::size_t i = 0; i < values.size(); ++i) {
64  if (!std::isfinite(values[i])) {
65  std::ostringstream msg;
66  msg << "RunningStats " << m_name << ": quantity " << m_names[i] << " is not finite at t = " << t << " s";
67  amrex::Abort(msg.str());
68  }
69  }
70  if (m_count == 0) { m_t_first = t; }
71  m_t_last = t;
72  for (std::size_t i = 0; i < values.size(); ++i) {
73  const double v = static_cast<double>(values[i]);
74  m_sum[i] += v;
75  m_sum2[i] += v * v;
76  m_min[i] = std::min(m_min[i], values[i]);
77  m_max[i] = std::max(m_max[i], values[i]);
78  }
79  ++m_count;
80  }
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
double m_t_last
Definition: ERF_RunningStats.H:183
int m_count
Definition: ERF_RunningStats.H:182
double m_t_first
Definition: ERF_RunningStats.H:183
@ t
Definition: ERF_WSM6.H:295
Here is the call graph for this function:

◆ max()

amrex::Real erf_actuator::RunningStats::max ( std::size_t  i) const
inline
87 { return (m_count > 0) ? m_max[i] : amrex::Real(0.0); }

Referenced by write().

Here is the caller graph for this function:

◆ mean()

amrex::Real erf_actuator::RunningStats::mean ( std::size_t  i) const
inline
81 { return (m_count > 0) ? static_cast<amrex::Real>(m_sum[i] / m_count) : amrex::Real(0.0); }

Referenced by write().

Here is the caller graph for this function:

◆ min()

amrex::Real erf_actuator::RunningStats::min ( std::size_t  i) const
inline
86 { return (m_count > 0) ? m_min[i] : amrex::Real(0.0); }

Referenced by write().

Here is the caller graph for this function:

◆ name()

const std::string& erf_actuator::RunningStats::name ( ) const
inline
43 { return m_name; }

◆ num_samples()

int erf_actuator::RunningStats::num_samples ( ) const
inline
55 { return m_count; }

◆ quantities()

const std::vector<std::string>& erf_actuator::RunningStats::quantities ( ) const
inline
44 { return m_names; }

◆ read_state()

bool erf_actuator::RunningStats::read_state ( const std::string &  dir)
inline

Restore the state written by write_state(); false when there is none. Aborts, changing nothing, when the file is malformed: a negative count, t_last before t_first, a non-finite value, or quantities other than this object's (but those accepted by renamed_from).

120  {
121  const std::string fname = dir + "/" + m_name + "_stats.dat";
122  if (!file_exists_everywhere(fname)) {
123  amrex::Print() << "RunningStats " << m_name << ": no " << fname << " in the checkpoint; the statistics start afresh\n";
124  return false;
125  }
126  amrex::Vector<char> chars;
127  amrex::ParallelDescriptor::ReadAndBcastFile(fname, chars);
128  std::istringstream in(std::string(chars.dataPtr(), chars.size()));
129  std::string key, eq, qname;
130  std::size_t size = 0;
131  int count = 0;
132  double t0 = 0.0, t1 = 0.0;
133  if (!(in >> key >> eq >> count) || key != "count" || !(in >> key >> eq >> size) || key != "size" ||
134  !(in >> key >> eq >> t0) || key != "t_first" || !(in >> key >> eq >> t1) || key != "t_last") {
135  amrex::Abort("malformed statistics checkpoint '" + fname + "'");
136  }
137  if (size != m_names.size()) {
138  amrex::Abort("the statistics checkpoint '" + fname + "' holds " + std::to_string(size) + " quantities but " +
139  m_name + " has " + std::to_string(m_names.size()));
140  }
141  if (count < 0 || !std::isfinite(t0) || !std::isfinite(t1) || (count > 0 && t1 < t0)) {
142  amrex::Abort("malformed statistics checkpoint '" + fname + "': the count must be >= 0 and t_first <= t_last, both finite");
143  }
144  std::vector<double> sum(size), sum2(size);
145  std::vector<amrex::Real> mn(size), mx(size);
146  std::string afresh;
147  for (std::size_t i = 0; i < size; ++i) {
148  if (!(in >> qname >> sum[i] >> sum2[i] >> mn[i] >> mx[i])) {
149  amrex::Abort("the statistics checkpoint '" + fname + "' is truncated: " + m_names[i] + " is missing or incomplete");
150  }
151  if (qname != m_names[i]) {
152  const auto r = m_renamed.find(qname);
153  if (r == m_renamed.end() || r->second.first != m_names[i]) {
154  amrex::Abort("the statistics checkpoint '" + fname + "' does not list " + m_names[i] + " where expected");
155  }
156  if (r->second.second) {
157  amrex::Print() << "RunningStats " << m_name << ": the checkpoint's " << qname << " statistics continue as "
158  << m_names[i] << "\n";
159  } else if (afresh.empty()) {
160  afresh = qname;
161  }
162  }
163  if (!std::isfinite(sum[i]) || !std::isfinite(sum2[i]) || !std::isfinite(mn[i]) || !std::isfinite(mx[i])) {
164  amrex::Abort("malformed statistics checkpoint '" + fname + "': a value of " + m_names[i] + " is not finite");
165  }
166  }
167  if (!afresh.empty()) {
168  amrex::Print() << "RunningStats " << m_name << ": the checkpoint lists " << afresh << ", another quantity than "
169  << m_renamed[afresh].first << " here; all statistics of " << m_name << " start afresh at the restart\n";
170  return true;
171  }
172  m_sum = sum; m_sum2 = sum2; m_min = mn; m_max = mx;
173  m_count = count; m_t_first = t0; m_t_last = t1;
174  return true;
175  }
int count
Definition: ERF_ActuatorSpreading.cpp:264
std::map< std::string, std::pair< std::string, bool > > m_renamed
old name -> (name now, same quantity)
Definition: ERF_RunningStats.H:184
bool file_exists_everywhere(const std::string &fname)
Definition: ERF_DiagnosticsLog.H:43
real(c_double), parameter t0
Definition: ERF_module_model_constants.F90:39
Here is the call graph for this function:

◆ renamed_from()

void erf_actuator::RunningStats::renamed_from ( const std::string &  old_name,
const std::string &  now,
bool  same 
)
inline

Accept a checkpoint listing quantity old_name where now stands: an earlier name of the same quantity (same true: its statistics continue), or of another quantity (same false: the statistics start afresh at the restart, and say so).

52  {
53  m_renamed[old_name] = std::make_pair(now, same);
54  }

◆ rms()

amrex::Real erf_actuator::RunningStats::rms ( std::size_t  i) const
inline
83  {
84  return (m_count > 0) ? static_cast<amrex::Real>(std::sqrt(std::max(m_sum2[i] / m_count, 0.0))) : amrex::Real(0.0);
85  }

Referenced by write().

Here is the caller graph for this function:

◆ t_first()

double erf_actuator::RunningStats::t_first ( ) const
inline
56 { return m_t_first; }

◆ t_last()

double erf_actuator::RunningStats::t_last ( ) const
inline
57 { return m_t_last; }

◆ write()

void erf_actuator::RunningStats::write ( ) const
inline

Rewrite <output_root>_stats.csv: one row per quantity with the current statistics.

91  {
92  if (!amrex::ParallelDescriptor::IOProcessor()) { return; }
93  std::ofstream out;
94  open_log(out, m_output_root + "_stats.csv", true);
95  out << "samples,t_first,t_last,quantity,mean,rms,min,max\n" << std::setprecision(10);
96  for (std::size_t i = 0; i < m_names.size(); ++i) {
97  out << m_count << "," << m_t_first << "," << m_t_last << "," << m_names[i] << ","
98  << mean(i) << "," << rms(i) << "," << min(i) << "," << max(i) << "\n";
99  }
100  }
amrex::Real min(std::size_t i) const
Definition: ERF_RunningStats.H:86
amrex::Real mean(std::size_t i) const
Definition: ERF_RunningStats.H:81
amrex::Real rms(std::size_t i) const
Definition: ERF_RunningStats.H:82
amrex::Real max(std::size_t i) const
Definition: ERF_RunningStats.H:87
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_state()

void erf_actuator::RunningStats::write_state ( const std::string &  dir) const
inline

The state for a checkpoint: <dir>/<name>_stats.dat.

104  {
105  if (!amrex::ParallelDescriptor::IOProcessor()) { return; }
106  std::ofstream out(dir + "/" + m_name + "_stats.dat", std::ios::trunc);
107  if (!out) { amrex::Abort("cannot write the statistics checkpoint for " + m_name + " in '" + dir + "'"); }
108  out << std::setprecision(17) << "count = " << m_count << "\nsize = " << m_names.size()
109  << "\nt_first = " << m_t_first << "\nt_last = " << m_t_last << "\n";
110  for (std::size_t i = 0; i < m_names.size(); ++i) {
111  out << m_names[i] << " " << m_sum[i] << " " << m_sum2[i] << " " << m_min[i] << " " << m_max[i] << "\n";
112  }
113  }

Member Data Documentation

◆ m_count

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

◆ m_max

std::vector<amrex::Real> erf_actuator::RunningStats::m_max
private

◆ m_min

std::vector<amrex::Real> erf_actuator::RunningStats::m_min
private

◆ m_name

std::string erf_actuator::RunningStats::m_name
private

◆ m_names

std::vector<std::string> erf_actuator::RunningStats::m_names
private

◆ m_output_root

std::string erf_actuator::RunningStats::m_output_root
private

Referenced by write().

◆ m_renamed

std::map<std::string, std::pair<std::string, bool> > erf_actuator::RunningStats::m_renamed
private

old name -> (name now, same quantity)

Referenced by read_state(), and renamed_from().

◆ m_sum

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

◆ m_sum2

std::vector<double> erf_actuator::RunningStats::m_sum2
private

in double: a float sum over many steps would lose the samples

Referenced by accumulate(), read_state(), rms(), and write_state().

◆ m_t_first

double erf_actuator::RunningStats::m_t_first {0.0}
private

◆ m_t_last

double erf_actuator::RunningStats::m_t_last {0.0}
private

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