StatMech
Loading...
Searching...
No Matches
Histogram.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 
)
20 {
21 if(argc != 4) {
22 std::cerr << "Usage: 1.This 2.n_min 3.n_max 4.Directory\n";
23 std::exit(EX_USAGE);
24 }
25 Integer_t const n_min = (Integer_t)atoll(argv[1]);
26 Integer_t const n_max = (Integer_t)atoll(argv[2]);
27 Integer_t const NMAX = n_max+1;
28 constexpr Integer_t MAX = 10000;
29 constexpr double binratio = 0.05;
30
31 std::vector<double> mean(NMAX,0), stddev(NMAX,0);
32 double Mean, Stddev, Fluc[2];
33
34 std::stringstream buff;
35
36 std::string const baseDir(argv[3]);
37 std::cout << "Directory: " << baseDir << "\n";
38 std::string const OutDIR(baseDir+"/Output");
39 filesystem::create_directory(OutDIR);
40
41 std::string const type[2] = {"Fluc", "MaxFluc"};
42 for(std::string const& Type : type) {
43 buff.str(""); buff.clear(std::stringstream::goodbit);
44 buff << "/Stat_" << Type << ".txt";
45 std::string const InStatName(OutDIR+buff.str());
46 std::ifstream InStat(InStatName); checkIsFileOpen(InStat, InStatName);
47 InStat.exceptions(std::ios::badbit | std::ios::failbit);
48 try { skip_header(InStat); }
49 catch(std::exception& e) {
50 std::cerr << "Error(skip_header): " << e.what() << std::endl;
51 std::cerr << "File: \"" << InStatName << "\" is empty." << std::endl;
52 std::exit(EXIT_FAILURE);
53 }
54
55 Integer_t n, bin, rep, ndata;
56 double fluc, maxE;
57 std::string line;
58 try{
59 for(getline(InStat, line); !InStat.eof(); getline(InStat, line)) {
60 std::istringstream inStream(line);
61 inStream >> n >> Mean >> Stddev;
62 if(n >= NMAX) {
63 std::cerr << "Error: n(" << n << ") >= NMAX(" << NMAX << ")" << std::endl;
64 std::exit(EX_USAGE);
65 }
66 mean[n] = Mean;
67 stddev[n] = Stddev;
68 }
69 } catch(std::exception& e) {
70 if( !InStat.eof() ) std::cerr << "Error(" << InStat.eof() << InStat.fail() << InStat.bad() << "):" << e.what() << std::endl;
71 }
72 InStat.close();
73
74 for(n = n_max;n >= n_min; --n) {
75 buff.str(""); buff.clear(std::stringstream::goodbit);
76 buff << "/MeasureOfETH_N" << n << ".txt";
77 std::string InFName(baseDir+buff.str());
78 std::ifstream InFile(InFName);
79 InFile.exceptions(std::ios::badbit | std::ios::eofbit | std::ios::failbit);
80 if( !InFile.is_open() ) {
81 std::cerr << "Can't open a file \"" << InFName << "\"" << std::endl;
82 continue;
83 }
84 try { skip_header(InFile); }
85 catch(std::exception& e) {
86 std::cerr << "Error(skip_header): " << e.what() << std::endl;
87 std::cerr << "File: \"" << InFName << "\" is empty." << std::endl;
88 continue;
89 }
90
91 double binwidth = binratio *mean[n]; if(binwidth <= 0) continue;
92 Integer_t total=0, binMax=0;
93 std::vector<Integer_t> count(MAX,0);
94
95 try{
96 for(getline(InFile, line); !InFile.eof(); getline(InFile, line)) {
97 fluc = std::nan("");
98 std::cout << line << "\r";
99 std::istringstream inStream(line);
100 inStream >> rep >> Fluc[0] >> Fluc[1] >> maxE >> ndata;
101 if(Type == "Fluc") fluc = Fluc[0];
102 else if(Type == "MaxFluc") fluc = Fluc[1];
103 if( isnan(fluc) || isinf(fluc) ) continue;
104 bin = (Integer_t)(fluc/binwidth); if(bin >= MAX) continue;
105 binMax = fmax(binMax, bin);
106 total += 1;
107 count[bin] += 1;
108 }
109 } catch(std::exception& e) {
110 if( !InFile.eof() ) std::cerr << "Error(" << InFile.eof() << InFile.fail() << InFile.bad() << "):" << e.what() << std::endl;
111 }
112 InFile.close();
113
114 buff.str("");
115 buff.clear(std::stringstream::goodbit);
116 buff << "/Hist_" << Type << "_n" << n << ".txt";
117 std::string OutFName(OutDIR); OutFName += buff.str();
118 std::ofstream OutFile(OutFName);
119 if( !OutFile.is_open() ) {
120 std::cerr << "Can't open a file " << OutFName << std::endl;
121 std::exit(EX_CANTCREAT);
122 }
123
124 std::cout << "Output file: " << OutFName;
125 OutFile << std::scientific
126 << "# Type=" << Type << "\n"
127 << "# total=" << total << "\n"
128 << "# binwidth = " << binratio << "*mean\n"
129 << "# FlucMax = " << (binMax+0.5)*binwidth << "\n"
130 << "# 1.(Fluctuation) 2.(Probability density) 3.(Cumulative distribution)\n\n";
131 if(total == 0) { OutFile << "0 1 1\n"; continue; }
132
133 Integer_t cumulative = 0;
134 for(bin = 0;bin<=binMax && bin<MAX; ++bin) {
135 cumulative += count[bin];
136 OutFile << (bin+0.5)*binwidth << " " << count[bin]/(total*binwidth) << " " << (double)cumulative/(double)total << "\n";
137 }
138 OutFile.close();
139 if(cumulative != total) std::cerr << "# Warning: cumulative(" << cumulative << ") != total(" << total << ")" << std::endl;
140 }
141 std::cout << "\n";
142 }
143
144 return 0;
145}
Integer_t const NMAX
Definition OpErgodicity.cpp:13
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