StatMech
Loading...
Searching...
No Matches
MultiFractalDimension.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 
)
21 {
22 if(argc < 6) {
23 std::cerr << "Usage: 1.This 2.Directory 3.Nmax 4.Nmin 5.dE 6.Type (Op1).SampleMin (Op2).SampleMax" << std::endl;
24 std::cerr << " : Type should be either (a)vsOp, (b)MF, or (c)btw2samples." << std::endl;
25 std::exit(EX_USAGE);
26 }
27 constexpr int MaxDeg = 8;
28 int const Nmax = (argc>=3 ? std::stoi(argv[2]) :16);
29 int const Nmin = (argc>=4 ? std::stoi(argv[3]) :16);
30 double const dE = (argc>=5 ? std::stod(argv[4]) :0.05);
31 double const Emin = 0.5-dE;
32 double const Emax = 0.5+dE;
33 int const SampleMin = (argc>=7 ? std::stod(argv[6]) :-1);
34 int const SampleMax = (argc>=8 ? std::stod(argv[7]) :-1);
35 std::string const Type(argv[5]);
36
37 std::string const baseDir(argv[1]);
38 std::cout << "# Directory: " << baseDir << "\n";
39 std::string const InDataDir(baseDir+"/ProcessedData");
40 std::vector<std::string> dirList;
41 {
42 std::string str;
43 int dataNo;
44 int const idShift=(SampleMin>0? SampleMin: 0);
45 if(SampleMin>0 && SampleMax>0) dirList.resize(SampleMax-idShift+1);
46 for(const filesystem::directory_entry &entry : filesystem::directory_iterator(InDataDir)) {
47 if ( entry.is_directory() ) {
48 str = entry.path().string();
49 dataNo = std::atoi(str.substr(str.find("_No")+3).c_str());
50 if((SampleMin>0 || SampleMax>0) && (dataNo<SampleMin || SampleMax<dataNo)) continue;
51 debug_print("# dir=" << str << ", dataNo=" << dataNo);
52 if( (SampleMin<=0 || SampleMax<=0) && dirList.size()<=dataNo) dirList.resize(dataNo+1);
53 debug_print("# dataNo-idShift=" << dataNo-idShift << ", dataNo=" << dataNo << ", idShift=" << idShift);
54 dirList.at(dataNo-idShift) = str;
55 }
56 }
57 }
58
59 std::vector<std::vector<std::vector<double>>> FractalDimension(Nmax+1, std::vector<std::vector<double>>(MaxDeg+1, std::vector<double>(dirList.size(), NAN)));
60 #pragma omp parallel for schedule(dynamic, 10)
61 for(size_t sample = 0;sample < dirList.size(); ++sample) {
62 double dtemp;
63 std::string InFname, str;
64 std::ifstream InFs;
65 std::vector<std::string> candidateFileNames;
66 bool flag = false;
67 #pragma omp critical
68 std::cout << "# " << dirList[sample] << std::endl;
69 for(int n = Nmax;n >= Nmin ; --n) {
70 std::stringstream buff(""); buff << "_N" << n << ".txt";
71 { //-------------------- Load EigenEnergies --------------------//
72 candidateFileNames.resize(3);
73 candidateFileNames[0] = dirList[sample] + "/MultiFractalZ_" + Type + buff.str();
74 candidateFileNames[1] = dirList[sample] + "/MultiFractalC_" + Type + buff.str();
75 candidateFileNames[2] = dirList[sample] + "/MultiFractal_" + Type + buff.str();
76 if( InFs.is_open() ) { InFs.close(); InFs.clear(); }
77 for(auto name : candidateFileNames) {
78 InFname = name;
79 InFs.open(InFname);
80 if( InFs.is_open() ) break;
81 }
82 if( !InFs.is_open() ) continue;
83 InFs.exceptions(std::ios::badbit);
84 flag = true;
85 std::stringstream ss;
86 double MinEigenEnergy=NAN, MaxEigenEnergy=NAN;
87 try{
88 skip_header(InFs);
89 for(getline(InFs, str); !InFs.eof(); getline(InFs, str)) {
90 ss.str(str); ss.clear(std::stringstream::goodbit);
91 ss >> dtemp;
92 MinEigenEnergy = std::fmin(dtemp, MinEigenEnergy);
93 MaxEigenEnergy = std::fmax(dtemp, MaxEigenEnergy);
94 }
95 } catch(std::exception& e) {
96 if( !InFs.eof() )
97 std::cerr << "Error(" << InFs.eof() << InFs.fail() << InFs.bad() << "):" << e.what() << std::endl;
98 }
99 double EnergyRange=MaxEigenEnergy-MinEigenEnergy;
100 // #pragma omp critical
101 // std::cout << "# " << dirList[sample] << ", EnergyRange=" << EnergyRange << std::endl;
102 InFs.clear(); InFs.seekg(0, std::ios::beg);
103 try {
104 skip_header(InFs);
105 for(getline(InFs, str); !InFs.eof(); getline(InFs, str)) {
106 ss.str(str); ss.clear(std::stringstream::goodbit);
107 ss >> dtemp; dtemp = (dtemp-MinEigenEnergy)/EnergyRange;
108 if(dtemp<Emin || Emax<dtemp) continue;
109 for(int deg=2;deg <= MaxDeg; ++deg) {
110 ss >> dtemp;
111 FractalDimension[n][deg][sample] = std::fmin(dtemp, FractalDimension[n][deg][sample]);
112 }
113 }
114 } catch(std::exception& e) {
115 if( !InFs.eof() )
116 std::cerr << "Error(" << InFs.eof() << InFs.fail() << InFs.bad() << "):" << e.what() << std::endl;
117 }
118 InFs.close(); InFs.clear();
119 }
120 }
121
122 if(!flag) continue;
123 {
124 std::string OutDIR(dirList[sample]);
125 std::filesystem::create_directories(OutDIR);
126 std::stringstream buff("");
127 buff << "/MultiFractal_" +Type+ "_dE" << std::setprecision(2) << dE << ".txt";
128 std::string OutFName(OutDIR + buff.str());
129 std::ofstream OutFs(OutFName); checkIsFileOpen(OutFs, OutFName);
130 OutFs << "# 1.(N) 2.(deg 2) 3.(deg 3) 4.(deg 4) 5.(deg 5) 6.(deg 6) 7.(deg 7) 8.(deg 8) 9.(deg 9)" << std::endl;
131 OutFs << std::scientific;
132 for(int n = Nmax;n >= Nmin ; --n) {
133 OutFs << n << " ";
134 for(int deg=2;deg <= MaxDeg; ++deg) OutFs << FractalDimension[n][deg][sample] << " ";
135 OutFs << std::endl;
136 }
137 }
138 }
139
140 std::string OutDIR(baseDir+"/Output");
141 std::filesystem::create_directories(OutDIR);
142 std::cout << "# OutDIR: " << OutDIR << "\n";
143
144 #pragma omp parallel for
145 for(int deg=2; deg<=MaxDeg; ++deg) {
146 std::stringstream buff("");
147 buff << "/AverageMultiFractalDim_"+Type+"_dE" << std::setprecision(2) << dE << "_deg" << deg << ".txt";
148 std::string OutFName(OutDIR+buff.str());
149 std::ofstream OutFs(OutFName); checkIsFileOpen(OutFs, OutFName);
150 OutFs.exceptions(std::ios::badbit);
151 OutFs << "# 1.(N) 2.(Ensemble Average) 3.(Ensemble Stddev) 4.(ndata)\n" << std::endl;
152 OutFs << std::scientific;
153 for(int n = Nmax;n >=Nmin ; --n) {
154 double ave=0.0, var=0.0, dtemp;
155 int ndata = 0;
156 for(size_t sample = 0;sample < dirList.size(); ++sample) {
157 if( isnan(FractalDimension[n][deg][sample]) || isinf(FractalDimension[n][deg][sample]) ) continue;
158 ndata += 1;
159 ave += FractalDimension[n][deg][sample];
160 }
161 ave /= (double)ndata;
162 for(size_t sample = 0;sample < dirList.size(); ++sample) {
163 if( isnan(FractalDimension[n][deg][sample]) || isinf(FractalDimension[n][deg][sample]) ) continue;
164 dtemp = FractalDimension[n][deg][sample] - ave;
165 var += dtemp * dtemp;
166 }
167 var /= (double)ndata;
168 OutFs << n << " "
169 << ave << " "
170 << std::sqrt(var) << " "
171 << ndata
172 << std::endl;
173 }
174 }
175
176
177 return 0;
178}
namespace filesystem
Definition MultiFractalDimension.cpp:13
bool checkIsFileOpen(std::ifstream &file, std::string const &filename)
Definition file_util.hpp:22
debug_print("# Determining GPU configuration.")
constexpr double Emin
Definition setVariablesForMCAverage.cpp:4
constexpr double Emax
Definition setVariablesForMCAverage.cpp:5
double const dE
Definition setVariablesForMCAverage.cpp:2
std::stringstream buff("")

Variable Documentation

◆ filesystem

namespace filesystem = std::experimental::filesystem