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

Functions

double HistogramIntersection (double Dist1[Nbin], Integer_t Dist1Ndata[Nbin], double Dist2[Nbin])
 
double KLDivergence (double Dist1[Nbin], Integer_t Dist1Ndata[Nbin], double Dist2[Nbin])
 
double JSDivergence (double Dist1[Nbin], Integer_t Dist1Ndata[Nbin], double Dist2[Nbin], Integer_t Dist2Ndata[Nbin])
 
double L1Norm (double Dist1[Nbin], Integer_t Dist1Ndata[Nbin], double Dist2[Nbin])
 
double L2Norm (double Dist1[Nbin], Integer_t Dist1Ndata[Nbin], double Dist2[Nbin])
 
int main (int argc, char **argv)
 

Variables

Integer_t const Nbin = 100
 
Integer_t const SistemSizeMAX = 20
 
Integer_t const Nitem = 4
 

Function Documentation

◆ HistogramIntersection()

double HistogramIntersection ( double  Dist1[Nbin],
Integer_t  Dist1Ndata[Nbin],
double  Dist2[Nbin] 
)
17 {
18 Integer_t i;
19 double count = 0;
20 double res = 0;
21 for(i = 0;i < Nbin; ++i) {
22 if( Dist1Ndata[i] == 0 ) continue;
23 if( Dist1[i] > Dist2[i] ) res += Dist2[i];
24 else res += Dist1[i];
25 count += Dist1[i];
26 // if( res ) printf("KLDivergence: Dist1[%lld]=%f, Dist2[%lld]=%f\n", i, Dist1[i], i, Dist2[i]);
27 }
28 return res/count;
29}
Integer_t const Nbin
Definition OpErgodicity_Compare.cpp:13
MKL_INT Integer_t
Definition mytypes.hpp:359

◆ JSDivergence()

double JSDivergence ( double  Dist1[Nbin],
Integer_t  Dist1Ndata[Nbin],
double  Dist2[Nbin],
Integer_t  Dist2Ndata[Nbin] 
)
44 {
45 double res = KLDivergence(Dist1, Dist1Ndata, Dist2);
46 res += KLDivergence(Dist2, Dist2Ndata, Dist1);
47 return res /= 2.0;
48}
double KLDivergence(double Dist1[Nbin], Integer_t Dist1Ndata[Nbin], double Dist2[Nbin])
Definition OpErgodicity_Compare.cpp:31

◆ KLDivergence()

double KLDivergence ( double  Dist1[Nbin],
Integer_t  Dist1Ndata[Nbin],
double  Dist2[Nbin] 
)
31 {
32 Integer_t i;
33 double count = 0;
34 double res = 0;
35 for(i = 0;i < Nbin; ++i) {
36 if( Dist1[i] == 0 || Dist2[i] == 0 ) continue;
37 res += Dist1[i]*log(Dist1[i]/Dist2[i]);
38 // if( isnan(res) ) printf("KLDivergence: Dist1[%lld]=%f, Dist2[%lld]=%f\n", i, Dist1[i], i, Dist2[i]);
39 count += Dist1[i];
40 }
41 return res/(double)count;
42}

◆ L1Norm()

double L1Norm ( double  Dist1[Nbin],
Integer_t  Dist1Ndata[Nbin],
double  Dist2[Nbin] 
)
50 {
51 Integer_t i;
52 double count = 0;
53 double res = 0;
54 for(i = 0;i < Nbin; ++i) {
55 if( Dist1Ndata[i] == 0 ) continue;
56 res += fabs(Dist1[i] -Dist2[i]);
57 // if( isnan(res) ) printf("L1Norm: Dist1[%lld]=%f, Dist2[%lld]=%f\n", i, Dist1[i], i, Dist2[i]);
58 count += 1.0;
59 }
60 // printf("L1Norm: res=%f, count=%f\n", res, count);
61 return res/count;
62}

◆ L2Norm()

double L2Norm ( double  Dist1[Nbin],
Integer_t  Dist1Ndata[Nbin],
double  Dist2[Nbin] 
)
64 {
65 Integer_t i;
66 double count = 0;
67 double res = 0;
68 for(i = 0;i < Nbin; ++i) {
69 if( Dist1Ndata[i] == 0 ) continue;
70 res += (Dist1[i] -Dist2[i])*(Dist1[i] -Dist2[i]);
71 // if( isnan(res) ) printf("L2Norm: Dist1[%lld]=%f, Dist2[%lld]=%f\n", i, Dist1[i], i, Dist2[i]);
72 count += 1.0;
73 }
74 // printf("L2Norm: res=%f, count=%f\n", res, count);
75 return sqrt(res/count);
76}

◆ main()

int main ( int  argc,
char **  argv 
)
78 {
79 if(argc < 2) {
80 printf("Usage: 1.This 2.Directory\n");
81 exit(EXIT_FAILURE);
82 }
83 Integer_t i,j;
84 Integer_t Nsample = 0;
85 Integer_t n, bin, dimShell, itemp;
88 double ratio, expval, stddev, fE, dos, MCave, dtemp;
89
90 struct stat statBuf;
91 struct Distance {
92 double HI;
93 double KLdivergence;
94 double JSdivergence;
95 double L1norm;
96 double L2norm;
97 };
98 struct Distance distance[SistemSizeMAX][Nitem]; // 分布間の距離の指標
99
100 struct EnsembleAverage {
101 Integer_t N[Nbin];
102 double ExpValue[Nbin];
103 double Stddev[Nbin];
104 double funcF[Nbin];
105 double DOS[Nbin];
106 };
107 struct EnsembleAverage Average[SistemSizeMAX];
108 for(i = 0; i < SistemSizeMAX; ++i) for(j = 0;j < Nbin; ++j) {
109 Average[i].N[j] = 0;
110 Average[i].ExpValue[j] = 0;
111 Average[i].Stddev[j] = 0;
112 Average[i].funcF[j] = 0;
113 Average[i].DOS[j] = 0;
114 }
115
116 Integer_t const BUFSIZE = 1000;
117 char dstr[BUFSIZE], str[BUFSIZE], stemp[BUFSIZE];
118 char *filename = str;
119 char *ext;
120 strcpy(dstr, argv[1]);
121 DIR *dir;
122 FILE *Input, *Output;
123 struct dirent *dp;
124
125 //********** アンサンブル平均の読み込み ********** //
126 strcpy(stemp, dstr);
127 strcat(stemp, "/Output");
128 strcpy(str, stemp);
129 strcat(str, "/");
130 strcat(str, "EnsembleAverage.txt");
131 printf("Input file: %s\n", filename);
132 Input = fopen(filename, "r");
133 if(Input == NULL) {
134 printf("Can't open a file %s\n", filename);
135 exit(EXIT_FAILURE);
136 }
137 skip_header(Input);
138 while( fscanf(Input, "%lld %lf %le %le %le %le %lld", &n, &ratio, &expval, &stddev, &fE, &dos, &itemp) != EOF ) {
139 if(ratio == 1) bin = Nbin-1; else bin = (Integer_t)(ratio*Nbin);
140 Average[n].N[bin] = itemp;
141 Average[n].ExpValue[bin] = expval;
142 Average[n].Stddev[bin] = stddev;
143 Average[n].funcF[bin] = fE;
144 Average[n].DOS[bin] = dos;
145 }
146 fclose(Input);
147 //********** (END)アンサンブル平均の読み込み ********** //
148
149 //********** Output fileを作成 ********** //
150 strcpy(stemp, dstr);
151 strcat(stemp, "/Output");
152 mkdir(stemp, 0777);
153 strcpy(str, stemp);
154 strcat(str, "/");
155 strcat(str, "ErgodicityFluctuation_Compare.txt");
156 strcpy(stemp, str);
157 printf("Output file: %s\n", filename);
158 Output = fopen(filename, "w");
159 if(Output == NULL) {
160 printf("Can't open a file %s\n", filename);
161 exit(EXIT_FAILURE);
162 }
163 fprintf(Output, "# 1.Nsample 2.N 3.funcF L1norm 4.funcF L2norm\n\n");
164 //********** (END)Output fileを作成 ********** //
165
166 dir = opendir(dstr);
167 for(dp = readdir(dir);dp != NULL;dp = readdir(dir)) {
168 if( strstr(dp->d_name, "Operators_") == NULL ) continue;
169 ext = strrchr(dp->d_name, '.');
170 if( strcmp(ext, ".txt") != 0 ) continue;
171
172 strcpy(str, dstr);
173 strcat(str, "/");
174 strcat(str, dp->d_name);
175 if( stat(filename, &statBuf) == -1 ){
176 printf("Can't get file information: %s.", filename);
177 continue;
178 }
179 printf("Input file: %s, size: %lld\n", filename, statBuf.st_size);
180 if( statBuf.st_size==0 ) continue;
181 Input = fopen(filename, "r");
182 if(Input == NULL) {
183 printf("Can't open a file %s\n", filename);
184 continue;
185 }
186 //********** 変数の初期化 **********//
187 for(n = 0;n < SistemSizeMAX; ++n) {
188 Ntotal[n] = 0;
189 NnanfE[n] = 0;
190 }
191 for(n = 0;n < SistemSizeMAX; ++n) for(j = 0;j < Nitem; ++j) {
192 distance[n][j].HI = 0;
193 distance[n][j].KLdivergence = 0;
194 distance[n][j].JSdivergence = 0;
195 distance[n][j].L1norm = 0;
196 distance[n][j].L2norm = 0;
197 }
198 //********** (END)変数の初期化 **********//
199
200 Nsample += 1;
201 skip_header(Input);
202 //********** 関数の間の距離を計算 **********//
203 while( fscanf(Input, "%lld %lld %le %le %le %le %le %le %lld", &itemp, &n, &dtemp, &ratio, &expval, &MCave, &fE, &dtemp, &dimShell) != EOF ) {
204 if( isnan(fE) ) {
205 NnanfE[n] += 1;
206 continue;
207 }
208 Ntotal[n] += 1;
209 if(ratio == 1) bin = Nbin-1; else bin = (Integer_t)(ratio*Nbin);
210 // funcF
211 distance[n][2].L1norm += fabs(fE -Average[n].funcF[bin]);
212 distance[n][2].L2norm += (fE -Average[n].funcF[bin])*(fE -Average[n].funcF[bin]);
213 }
214 //********** (END)関数の間の距離を計算 **********//
215 fclose(Input);
216
217 //********** ファイルへの書き込み **********//
218 for(n = 0; n < SistemSizeMAX; ++n) {
219 if( Ntotal[n] == 0) continue;
220 fprintf(Output, "%lld %lld ", Nsample, n);
221 fprintf(Output, "%le ", distance[n][2].L1norm/(double)( Ntotal[n] ) );
222 fprintf(Output, "%le ", sqrt( distance[n][2].L2norm/(double)( Ntotal[n] ) ) );
223 fprintf(Output, "\n");
224 printf("Ntotal[%lld]=%lld\n", n, Ntotal[n]);
225 }
226 //********** (END)ファイルへの書き込み **********//
227
228 }
229
230 printf("Output file: %s\n", stemp);
231 fclose(Output);
232
233 return 0;
234}
Integer_t const Nitem
Definition OpErgodicity_Compare.cpp:15
Integer_t const SistemSizeMAX
Definition OpErgodicity_Compare.cpp:14
Definition OpHistogram.cpp:24
double L1norm
Definition OpHistogram.cpp:29
double L2norm
Definition OpHistogram.cpp:30

Variable Documentation

◆ Nbin

Integer_t const Nbin = 100

◆ Nitem

Integer_t const Nitem = 4

◆ SistemSizeMAX

Integer_t const SistemSizeMAX = 20