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

Classes

class  Distance
 

Functions

double NormalDist (double x)
 
int main (int argc, char **argv)
 

Variables

namespace filesystem = std::experimental::filesystem
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)
62 {
63 if(argc < 3) {
64 printf("Usage: 1.This 2.Directory 3.Shell width\n");
65 exit(EXIT_FAILURE);
66 }
67 double const cutoff = 0;
68 double const binratio = 0.05;
69 std::string const d_str(argv[1]);
70 double const shellWidth = (double)std::atof(argv[2]);
71 std::cout << "Directory: " << d_str << "\n";
72
73 Integer_t n, ndata, Nmin, Nmax, bin, binMax, count=0, Nline;
74 // Integer_t ndata, itemp;
75 double relEnergy, expval, binwidth, ave, aveOld, var;
76 double Emin, Emax, StddevInShell, minFluc, maxFluc, Fluc, dtemp;
77 std::vector<Integer_t> Ndata, Nnan, NdataInShell;
78 std::vector<double> MCAverage, NormalizedFluc, histogram, ProbDensity, gauss;
79 std::vector<std::vector<double>> eigenEnergy, ExpValue;
80 Distance dist;
81 std::string line;
82
83
84 //********** Output fileを作成 ********** //
85 std::string OutDIR(d_str); OutDIR += "/Output";
86 filesystem::create_directory(OutDIR);
87 std::string OutDevFName(OutDIR); OutDevFName += "/EF_deviation.txt";
88 std::ofstream OutDevfs(OutDevFName);
89 OutDevfs.exceptions(std::ios::badbit | std::ios::failbit);
90 if( !OutDevfs.is_open() ) {
91 std::cerr << "Can't open a file " << OutDevFName << std::endl;
92 std::exit(EX_CANTCREAT);
93 }
94 OutDevfs << "# shellWidth = " << shellWidth << ", binratio = " << binratio << "\n"
95 << "# 1.(Sample No.) 2.(System Size) 3.(Histogram Intersection) 4.(KLdivergence) 5.(JSdivergence) 6.(L1norm) 7.(L2norm)\n"
96 << std::endl;
97 std::cout << "Output file: " << OutDevFName << std::endl;
98
99 OutDIR = d_str; OutDIR += "/ProcessedData";
100 filesystem::create_directory(OutDIR);
101 //********** (END)Output fileを作成 ********** //
102
103 std::string InDataDir(d_str); InDataDir += "/RawData";
104 for(const filesystem::directory_entry &data : filesystem::directory_iterator(InDataDir)) {
105 if( data.path().string().find("Sample_No") == std::string::npos ) continue;
106 if( data.path().extension() != ".txt" ) continue;
107 std::ifstream Infs(data.path());
108 Infs.exceptions(std::ios::badbit | std::ios::failbit);
109 if( !Infs.is_open() ) {
110 std::cerr << "Can't open a file \"" << data.path() << "\"" << std::endl;
111 continue;
112 }
113 try { skip_header(Infs); }
114 catch(std::exception& e) {
115 std::cerr << "Warning(skip_header): " << e.what() << std::endl;
116 std::cerr << "File: \"" << data.path() << "\" is empty." << std::endl;
117 continue;
118 }
119
120 //********** Create output file for Histogram of eigenstate fluctuation **********//
121 std::string OutHistFName(OutDIR);
122 OutHistFName += "/EFHistNormalized";
123 OutHistFName += data.path().string().substr( data.path().string().find("_No") );
124 std::ofstream OutHistfs(OutHistFName);
125 OutHistfs.exceptions(std::ios::badbit | std::ios::failbit);
126 if( !OutHistfs.is_open() ) {
127 std::cerr << "Can't open a file " << OutHistFName << std::endl;
128 std::exit(EX_CANTCREAT);
129 }
130 OutHistfs << "# shellWidth = " << shellWidth << ", binratio = " << binratio << "\n"
131 << "# 1.(System Size) 2.(Normalized fluctuation) 3.(Probability density) 4.(data# in bin) 5.(Gauss)\n"
132 << std::scientific
133 << std::endl;
134 std::cout << "OutEFHist: " << OutHistFName << std::endl;
135 //********** (END) Create output file for Histogram of eigenstate fluctuation **********//
136
137 //********** Read data **********//
138 ++count;
139 Nmin=INT_MAX; Nmax=INT_MIN;
140 std::fill(Ndata.begin(),Ndata.end(),0);
141 std::fill( Nnan.begin(), Nnan.end(),0);
142 for(n = 0;n < eigenEnergy.size(); ++n) eigenEnergy.at(n).resize(0);
143 for(n = 0;n < ExpValue.size(); ++n) ExpValue.at(n).resize(0);
144 try{
145 Nline = 0;
146 for(std::getline(Infs, line); !Infs.eof(); std::getline(Infs, line)) {
147 ++Nline;
148 std::cout << "(count=" << count << ", line=" << Nline << "): " << line << ": " << Infs.eof() << "\r";
149 std::istringstream inStream(line);
150 inStream >> n >> dtemp >> relEnergy >> expval;
151 if( isinf(expval) || isnan(expval) ) std::cerr << expval << std::endl;
152 if(n < Nmin) Nmin = n;
153 if(n > Nmax) Nmax = n;
154 if(n >= Ndata.size()) Ndata.resize(n+1);
155 if(n >= eigenEnergy.size()) eigenEnergy.resize(n+1);
156 if(n >= ExpValue.size()) ExpValue.resize(n+1);
157 Ndata.at(n) += 1;
158 eigenEnergy.at(n).push_back(relEnergy);
159 ExpValue.at(n).push_back(expval);
160 }
161 } catch(std::exception& e) {
162 if( !Infs.eof() ) {
163 std::cout << std::endl;
164 std::cerr << "Error(" << Infs.eof() << Infs.fail() << Infs.bad() << "): " << e.what() << std::endl;
165 }
166 }
167 Infs.close();
168 //********** (END) Read data **********//
169
170 Nnan.resize(Nmax+1); std::fill( Nnan.begin(), Nnan.end(), 0);
171 for(n = Nmax;n >= Nmin; --n){
172 if(Ndata[n] == 0) continue;
173 if(Ndata[n] != eigenEnergy[n].size()) {
174 std::cerr << "Error: Ndata[" << n << "](" << Ndata[n]
175 << ") != eigenEnergy[" << n << "].size (" << eigenEnergy[n].size() << ")"
176 << std::endl;
177 std::exit(EX_DATAERR);
178 }
179 if(Ndata[n] != ExpValue[n].size()) {
180 std::cerr << "Error: Ndata[" << n << "](" << Ndata[n]
181 << ") != ExpValue[" << n << "].size (" << ExpValue[n].size() << ")"
182 << std::endl;
183 std::exit(EX_DATAERR);
184 }
185
186 MCAverage.resize(Ndata[n]); NdataInShell.resize(Ndata[n]);
187 std::fill( MCAverage.begin(), MCAverage.end(), 0.0);
188 std::fill(NdataInShell.begin(), NdataInShell.end(), 0.0);
189 for(Integer_t i = 0;i < Ndata[n]; ++i) {
190 MCAverage[i] = MicroCanonicalAverage(NdataInShell[i], eigenEnergy[n][i], shellWidth, Ndata[n], eigenEnergy[n], ExpValue[n], i);
191 }
192
193 NormalizedFluc.resize(0);
194 minFluc = NAN; maxFluc = NAN;
195 for(int i = 0;i<Ndata[n] && eigenEnergy[n][i]<=1-cutoff; ++i) {
196 if(eigenEnergy[n][i] < cutoff) continue;
197 Emin = eigenEnergy[n][i] -shellWidth;
198 Emax = eigenEnergy[n][i] +shellWidth;
199 if((bin=MeasureOfETH(StddevInShell, dtemp, dtemp, Emin, Emax, Ndata[n], eigenEnergy[n], ExpValue[n], MCAverage)) != NdataInShell[i]) {
200 std::cerr << "Warning:(n,Nmax,i)=(" << n << "," << Nmax << "," << i << ") MeasureOfETH(" << bin << ") != NdataInShell[" << NdataInShell[i] << "]" << std::endl;
201 }
202 if( isinf(StddevInShell) ) StddevInShell = NAN;
203 if( isnan(StddevInShell) ) { Nnan[n] += 1; continue; }
204 dtemp = ( ExpValue[n][i] -MCAverage[i] ) / StddevInShell;
205 if(bin >= 1 && !isnan(dtemp)) {
206 NormalizedFluc.push_back(dtemp);
207 }
208 minFluc = std::fmin(minFluc, NormalizedFluc.back());
209 maxFluc = std::fmax(maxFluc, NormalizedFluc.back());
210 }
211 if(minFluc > maxFluc) continue;
212 if(NormalizedFluc.size() == 0) continue;
213
214 ndata = 0; ave = 0.0; var = 0.0;
215 for(int i = 0;i < NormalizedFluc.size(); ++i) {
216 if( isnan(NormalizedFluc[i]) ) continue;
217 ++ndata;
218 dtemp = 1/(double)ndata;
219 aveOld = ave;
220 ave = (1-dtemp)*aveOld + dtemp*NormalizedFluc[i];
221 var += (NormalizedFluc[i]-ave)*(NormalizedFluc[i]-aveOld);
222 }
223 var /= (double)(ndata-1);
224 if(ndata == 0) std::cerr << "ndata == 0, NormalizedFluc.size(" << NormalizedFluc.size() << ")" << std::endl;
225
226 // ave = std::reduce(NormalizedFluc.begin(), NormalizedFluc.end(), 0.0) / NormalizedFluc.size();
227 // var = std::accumulate(NormalizedFluc.begin(), NormalizedFluc.end(), 0.0, [ave](double sum, const auto& e){
228 // const auto temp = e - ave;
229 // return sum + temp * temp;
230 // }) / NormalizedFluc.size();
231
232 //********** Create histogram ********** //
233 if( var == 0 ) continue;
234 binwidth = binratio *sqrt(var);
235 binMax = (Integer_t)((maxFluc-minFluc)/binwidth) +1;
236 if(binMax <= 0) {
237 std::cerr << " # Warning(n=" << n << "): binMax(" << binMax << ") is not positive." << std::endl;
238 continue;
239 }
240 histogram.resize(binMax); std::fill( histogram.begin(), histogram.end(), 0);
241 ProbDensity.resize(binMax); std::fill(ProbDensity.begin(), ProbDensity.end(), 0);
242 gauss.resize(binMax); std::fill( gauss.begin(), gauss.end(), 0);
243
244 ndata = 0;
245 for(Integer_t i = 0;i < NormalizedFluc.size(); ++i) {
246 if( isnan(NormalizedFluc[i]) ) continue;
247 bin = (Integer_t)( (NormalizedFluc[i] -minFluc)/binwidth );
248 if(bin >= binMax || bin<0) {
249 std::cerr << " # Warning: bin(" << bin << ") > binMax(" << binMax << "): NormalizedFluc[" << i << "]=" << NormalizedFluc[i] << std::endl;
250 std::exit(EX_DATAERR);
251 }
252 histogram[bin] += 1;
253 ndata += 1;
254 }
255 // if(ndata != Ndata[n]) std::cerr << "# Error: ndata(" << ndata << ") != Ndata[" << n << "](" << Ndata[n] << ")" << std::endl;
256 for(bin = 0;bin < binMax; ++bin) {
257 ProbDensity[bin] = histogram[bin]/((double)Ndata[n]*binwidth);
258 if( isinf(ProbDensity[bin]) ) ProbDensity[bin] = NAN;
259 }
260 //********** (END) Create histogram ********** //
261
262 //********** Create discrete Gauss distribution ********** //
263 dtemp = 0;
264 for(bin = 0;bin < binMax; ++bin) {
265 Fluc = (bin+0.5)*binwidth +minFluc;
266 gauss[bin] = NormalDist(Fluc);
267 dtemp += gauss[bin];
268 }
269 for(bin = 0;bin < binMax; ++bin) gauss[bin] /= dtemp*binwidth;
270 //********** (END) Create discrete Gauss distribution ********** //
271
272 //********** Write histogram to a file ********** //
273 for(bin = 0;bin < binMax; ++bin) {
274 Fluc = (bin+0.5)*binwidth +minFluc;
275 OutHistfs << std::noshowpos
276 << std::setw(2) << n << " "
277 << std::showpos
278 << std::setw(13) << Fluc << " "
279 << std::setw(13) << ProbDensity[bin] << " "
280 << std::noshowpos
281 << std::setw(2) << histogram[bin] << " "
282 << std::showpos
283 << std::setw(13) << gauss[bin] << "\n";
284 }
285 OutHistfs << std::endl;
286 //********** (END) Write histogram to a file ********** //
287
288 //********** Write distance to a file ********** //
289 dist.calculate(ProbDensity, gauss, minFluc, binwidth);
290 OutDevfs << count << " "
291 << n << " "
292 << dist.HistIntersection << " "
293 << dist.KLDivergence << " "
294 << dist.JSDivergence << " "
295 << dist.L1norm << " "
296 << dist.L2norm << "\n";
297 //********** (END) Write distance to a file ********** //
298 }
299 OutDevfs << std::endl;
300 }
301
302 return 0;
303}
double NormalDist(double x)
Definition OpHistogram.cpp:58
namespace filesystem
Definition OpHistogram.cpp:18
Definition OpHistogram.cpp:24
double KLDivergence
Definition OpHistogram.cpp:27
double L1norm
Definition OpHistogram.cpp:29
double HistIntersection
Definition OpHistogram.cpp:26
double JSDivergence
Definition OpHistogram.cpp:28
void calculate(std::vector< double > pdf, std::vector< double > gauss, double minFluc, double binwidth)
Definition OpHistogram.cpp:34
double L2norm
Definition OpHistogram.cpp:30
Calculate the microcanonical averages with respect to a given sorted vector 'eigVal'.
Definition MicroCanonicalAverage.hpp:25
MKL_INT Integer_t
Definition mytypes.hpp:359
constexpr double Emin
Definition setVariablesForMCAverage.cpp:4
constexpr double cutoff
Definition setVariablesForMCAverage.cpp:3
constexpr double Emax
Definition setVariablesForMCAverage.cpp:5
Integer_t MeasureOfETH(double &Fluc_2, double &Fluc_inf, double &MaxPos, double const Emin, double const Emax, Integer_t const dim, const std::vector< double > &eigenEnergy, const std::vector< double > &EXPvalue, const std::vector< double > &MCAverage)
Definition statmech.cpp:244
__constant__ int size
Definition testEigenOnGPU.cu:4

◆ NormalDist()

double NormalDist ( double  x)
58 {
59 return 1.0/sqrt(2*M_PI)*exp(-x*x/2.0);
60}

Variable Documentation

◆ filesystem

namespace filesystem = std::experimental::filesystem