1 #ifndef ERF_RUNNING_STATS_H
2 #define ERF_RUNNING_STATS_H
16 #include <AMReX_BLassert.H>
17 #include <AMReX_ParallelDescriptor.H>
18 #include <AMReX_Print.H>
19 #include <AMReX_REAL.H>
20 #include <AMReX_Utility.H>
21 #include <AMReX_Vector.H>
51 void renamed_from (
const std::string& old_name,
const std::string& now,
bool same)
53 m_renamed[old_name] = std::make_pair(now, same);
60 void accumulate (
double t,
const std::vector<amrex::Real>& values)
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());
72 for (std::size_t i = 0; i < values.size(); ++i) {
73 const double v =
static_cast<double>(values[i]);
81 amrex::Real
mean (std::size_t i)
const {
return (
m_count > 0) ?
static_cast<amrex::Real
>(
m_sum[i] /
m_count) : amrex::Real(0.0); }
82 amrex::Real
rms (std::size_t i)
const
84 return (
m_count > 0) ?
static_cast<amrex::Real
>(std::sqrt(std::max(
m_sum2[i] /
m_count, 0.0))) : amrex::Real(0.0);
86 amrex::Real
min (std::size_t i)
const {
return (
m_count > 0) ?
m_min[i] : amrex::Real(0.0); }
87 amrex::Real
max (std::size_t i)
const {
return (
m_count > 0) ?
m_max[i] : amrex::Real(0.0); }
92 if (!amrex::ParallelDescriptor::IOProcessor()) {
return; }
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) {
98 <<
mean(i) <<
"," <<
rms(i) <<
"," <<
min(i) <<
"," <<
max(i) <<
"\n";
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()
110 for (std::size_t i = 0; i <
m_names.size(); ++i) {
121 const std::string fname = dir +
"/" +
m_name +
"_stats.dat";
123 amrex::Print() <<
"RunningStats " <<
m_name <<
": no " << fname <<
" in the checkpoint; the statistics start afresh\n";
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;
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 +
"'");
138 amrex::Abort(
"the statistics checkpoint '" + fname +
"' holds " + std::to_string(size) +
" quantities but " +
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");
144 std::vector<double> sum(size), sum2(size);
145 std::vector<amrex::Real> mn(size), mx(size);
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");
154 amrex::Abort(
"the statistics checkpoint '" + fname +
"' does not list " +
m_names[i] +
" where expected");
156 if (r->second.second) {
157 amrex::Print() <<
"RunningStats " <<
m_name <<
": the checkpoint's " << qname <<
" statistics continue as "
159 }
else if (afresh.empty()) {
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");
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";
184 std::map<std::string, std::pair<std::string, bool>>
m_renamed;
int count
Definition: ERF_ActuatorSpreading.cpp:264
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
Definition: ERF_RunningStats.H:35
void renamed_from(const std::string &old_name, const std::string &now, bool same)
Definition: ERF_RunningStats.H:51
int num_samples() const
Definition: ERF_RunningStats.H:55
std::vector< double > m_sum2
in double: a float sum over many steps would lose the samples
Definition: ERF_RunningStats.H:180
double m_t_last
Definition: ERF_RunningStats.H:183
int m_count
Definition: ERF_RunningStats.H:182
const std::vector< std::string > & quantities() const
Definition: ERF_RunningStats.H:44
const std::string & name() const
Definition: ERF_RunningStats.H:43
void accumulate(double t, const std::vector< amrex::Real > &values)
Definition: ERF_RunningStats.H:60
std::vector< double > m_sum
Definition: ERF_RunningStats.H:180
RunningStats(std::string name, std::string output_root, std::vector< std::string > quantities)
Definition: ERF_RunningStats.H:37
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
void write_state(const std::string &dir) const
Definition: ERF_RunningStats.H:103
std::map< std::string, std::pair< std::string, bool > > m_renamed
old name -> (name now, same quantity)
Definition: ERF_RunningStats.H:184
double t_last() const
Definition: ERF_RunningStats.H:57
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
void write() const
Definition: ERF_RunningStats.H:90
bool read_state(const std::string &dir)
Definition: ERF_RunningStats.H:119
double m_t_first
Definition: ERF_RunningStats.H:183
amrex::Real rms(std::size_t i) const
Definition: ERF_RunningStats.H:82
std::string m_name
Definition: ERF_RunningStats.H:178
double t_first() const
Definition: ERF_RunningStats.H:56
std::string m_output_root
Definition: ERF_RunningStats.H:178
amrex::Real max(std::size_t i) const
Definition: ERF_RunningStats.H:87
@ t
Definition: ERF_WSM6.H:295
Definition: ERF_ConsoleIO.cpp:17
Definition: ERF_ActuatorDeviceBuffers.H:14
bool file_exists_everywhere(const std::string &fname)
Definition: ERF_DiagnosticsLog.H:43
bool open_log(std::ofstream &out, const std::string &fname, bool truncate)
Definition: ERF_DiagnosticsLog.H:22
real(c_double), parameter t0
Definition: ERF_module_model_constants.F90:39