StatMech
Loading...
Searching...
No Matches
Stat.cpp File Reference

Functions

int main (int argc, char **argv)
 

Variables

namespace filesystem = std::experimental::filesystem
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)
22 {
23 if(argc != 4) {
24 std::cerr << "Usage: 1.This 2.n_min 3.n_max 4.Directory\n";
25 std::exit(EX_USAGE);
26 }
27 Integer_t const n_min = std::atoll(argv[1]);
28 Integer_t const n_max = std::atoll(argv[2]);
29 Integer_t const DataMin = 1;
30
31 std::vector<Integer_t> histDShell(10000);
32 Integer_t rep, ndata;
33 double Fluc[2], maxE;
34
35 std::stringstream buff;
36
37 std::string const baseDir(argv[3]);
38 std::cout << "Directory: " << baseDir << "\n";
39 std::string const OutDIR(baseDir+"/Output");
40 filesystem::create_directory(OutDIR);
41
42 std::string OutStatName;
43 std::ofstream OutStat[2];
44 OutStatName = OutDIR +"/Stat_Fluc.txt";
45 OutStat[0].open(OutStatName); checkIsFileOpen(OutStat[0], OutStatName);
46 OutStat[0].exceptions(std::ios::badbit | std::ios::failbit);
47 OutStatName = OutDIR +"/Stat_MaxFluc.txt";
48 OutStat[1].open(OutStatName); checkIsFileOpen(OutStat[1], OutStatName);
49 OutStat[1].exceptions(std::ios::badbit | std::ios::failbit);
50 for(int i = 0;i <= 1; ++i) OutStat[i] << "# 1.(System Size) 2.(Mean) 3.(Stddev) 4.(Stddev_err) 5.(Ndata)\n\n" << std::scientific << std::right << std::endl;
51
52 for(Integer_t n = n_max;n >= n_min; --n) {
53 buff.str(""); buff.clear(std::stringstream::goodbit);
54 buff << "/MeasureOfETH_N" << n << ".txt";
55 std::string const InFName(baseDir+buff.str());
56 std::ifstream InFile(InFName); checkIsFileOpen(InFile, InFName);
57 InFile.exceptions(std::ios::badbit | std::ios::eofbit | std::ios::failbit);
58 try { skip_header(InFile); }
59 catch(std::exception& e) {
60 std::cerr << "Error(skip_header): " << e.what() << std::endl;
61 std::cerr << "File: \"" << InFName << "\" is empty." << std::endl;
62 continue;
63 }
64 std::ifstream::pos_type head = InFile.tellg();
65
66 Integer_t Count=0, count[2]={0,0}, maxDShell=INT_MIN, minDShell=INT_MAX;
67 double meanDShell = 0;
68 double mean[2] = {0,0};
69 double variance[2] = {0,0};
70 double mu4[2] = {0,0};
71 std::fill(histDShell.begin(), histDShell.end(), 0);
72
73 std::string line;
74 // Calculate mean values //
75 try{
76 for(getline(InFile, line); !InFile.eof(); getline(InFile, line)) {
77 // std::cout << line << std::endl;
78 std::cout << "(n=" << std::setw(2) << std::right << n
79 << ", Count=" << std::setw(6) << std::right << Count << ") "
80 << line << "\r" << std::flush;
81 std::istringstream inStream(line);
82 inStream >> rep >> Fluc[0] >> Fluc[1] >> maxE >> ndata;
83 if(ndata < DataMin) continue;
84 minDShell = std::min(minDShell, ndata);
85 maxDShell = std::max(maxDShell, ndata);
86 meanDShell += ndata;
87 Count += 1;
88 if((size_t)ndata >= histDShell.size()) histDShell.resize(ndata+1);
89 histDShell.at(ndata) += 1;
90 for(int i = 0;i <= 1; ++i) if(!isnan(Fluc[i]) && !isinf(Fluc[i])) {
91 count[i] += 1; mean[i] += Fluc[i];
92 }
93 }
94 } catch(std::exception& e) {
95 if( !InFile.eof() ) std::cerr << "Error(" << InFile.eof() << InFile.fail() << InFile.bad() << "):" << e.what() << std::endl;
96 }
97 meanDShell /= (double)Count;
98 mean[0]/=(double)count[0]; mean[1]/=(double)count[1];
99 std::cout << "\r \r";
100 std::cout << "DimShell:(" << minDShell << "," << maxDShell << ") n=" << n
101 << ", count=(" << count[0] << ", " << count[1] << ")\n";
102
103 InFile.clear(); InFile.seekg(head);
104 // Calculate standard deviation and its error //
105 try{
106 for(getline(InFile, line); !InFile.eof(); getline(InFile, line)) {
107 std::istringstream inStream(line);
108 inStream >> rep >> Fluc[0] >> Fluc[1] >> maxE >> ndata;
109 if(ndata < DataMin) continue;
110 for(int i = 0;i <= 1; ++i) if( !isnan(Fluc[i]) && !isinf(Fluc[i]) ) {
111 variance[i] += pow(Fluc[i] -mean[i], 2)/(double)(count[i]-1);
112 mu4[i] += pow(Fluc[i] -mean[i], 4)/(double)count[i];
113 }
114 }
115 } catch(std::exception& e) {
116 if( !InFile.eof() ) std::cerr << "Error(" << InFile.eof() << InFile.fail() << InFile.bad() << "):" << e.what() << std::endl;
117 }
118
119 for(int i = 0;i <= 1; ++i) {
120 double variance_err = sqrt( (mu4[i] -variance[i]*variance[i])/count[i] );
121 OutStat[i] << std::setw(2) << n << " " << std::showpos
122 << std::setw(13) << mean[i] << " "
123 << std::setw(13) << sqrt(variance[i]) << " "
124 << std::setw(13) << 0.5*variance_err/sqrt(variance[i]) << " " << std::noshowpos
125 << count[i] << "\n";
126 }
127
128
129 buff.str("");
130 buff.clear(std::stringstream::goodbit);
131 buff << "/Hist_DimShell_n" << n << ".txt";
132 std::string OutDSName(OutDIR); OutDSName += buff.str();
133 std::ofstream OutDShell(OutDSName);
134 if( !OutDShell.is_open() ) {
135 std::cerr << "Can't open a file " << OutDSName << std::endl;
136 std::exit(EX_CANTCREAT);
137 }
138 OutDShell << std::scientific;
139 OutDShell << "# total = " << Count << "\n";
140 OutDShell << "# minDShell = " << minDShell << "; maxDShell=" << maxDShell << "; meanDShell=" << meanDShell << "\n";
141 OutDShell << "# 1.(Dim. of shell) 2.(Count) 3.(Cumulative Distribution Function)\n\n";
142 if(Count == 0) OutDShell << "0 0 1\n";
143 else {
144 Integer_t itemp = 0;
145 for(Integer_t i = minDShell;i <= maxDShell; ++i) {
146 itemp += histDShell.at(i);
147 OutDShell << std::setw(4) << i << " "
148 << std::setw(5) << histDShell.at(i) << " "
149 << std::setw(13) << (double)itemp/(double)Count << "\n";
150 }
151 if(itemp != Count) std::cerr << "Warning: itemp(" << itemp << ") != Count(" << Count << ")." << std::endl;
152 }
153 OutDShell.close();
154 }
155 std::cout << std::endl;
156 for(int i = 0;i <= 1; ++i) OutStat[i].close();
157
158 return 0;
159}
bool checkIsFileOpen(std::ifstream &file, std::string const &filename)
Definition file_util.hpp:22
MKL_INT Integer_t
Definition mytypes.hpp:359
Integer_t const n_min
Definition setVariablesForEnsemble.cpp:28
Integer_t const n_max
Definition setVariablesForEnsemble.cpp:27
std::stringstream buff("")

Variable Documentation

◆ filesystem

namespace filesystem = std::experimental::filesystem