-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf.cpp
305 lines (254 loc) · 7.08 KB
/
pdf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "cellFile.h"
#include "input.h"
#include "pdf.h"
#include "math.h"
void PDF::Routine()
{
TITLE("PDF","Routine");
cal();
return;
}
void PDF::cal()
{
TITLE("PDF","cal");
// (1) set the basic parameters.
// delta r in real space.
this->dr = INPUT.pdf_dr;
// radius cutoff in real space, usually choose a/2,
// where a is the lattice constant.
this->rcut = INPUT.pdf_rcut;
// number of radial mesh.
this->nmesh = int(rcut / dr) + 1;
// pair distribution function.
double* gr = new double[nmesh];
double* gr_tmp = new double[nmesh];
for(int i=0; i<nmesh; ++i) gr[i] = 0;
for(int i=0; i<nmesh; ++i) gr_tmp[i] = 0;
cout << " dr = " << dr << " Angstrom" << endl;
cout << " rcut = " << rcut << " Angstrom" << endl;
cout << " nmesh = " << nmesh << endl;
/*
// concentration for ntype=2.
double concentration = 1.0;
if(INPUT.ntype==2)
{
if(INPUT.pdf_ele1 == 1)
{
concentration = (double)INPUT.natom1 / (double)INPUT.natom;
}
else if(INPUT.pdf_ele1 == 2)
{
concentration = (double)INPUT.natom2 / (double)INPUT.natom;
}
}
cout << " concentration is " << concentration << endl;
*/
// ionic density
assert(INPUT.geo_interval>0);
int count_geometry_number=0;
for(int igeo=INPUT.geo_1; igeo<=INPUT.geo_2; ++igeo)
{
//cout << " igeo=" << igeo << " igeo%INPUT.geo_interval=" << igeo%INPUT.geo_interval << endl;
if(igeo%INPUT.geo_interval!=0) continue;
CellFile cel;
stringstream ss; ss << igeo;
cel.file_name = ss.str();
// cel : input geometry file
if( !CellFile::ReadGeometry( cel ) ) continue;
++count_geometry_number;
// calculate the ionic density
const double rho_ion = INPUT.natom / cel.volume;
if(count_geometry_number==1)
{
cout << " Volume of the input cell = " << cel.volume << " A^3" << endl;
cout << " Average ion density = " << rho_ion << endl;
// Fermi vector
double kf = pow(3*INPUT.pi*INPUT.pi*rho_ion,1.0/3.0);
cout << " Fermi vector = " << kf << endl;
cout << " pi=" << INPUT.pi << endl;
assert(kf > 0.0);
}
// add this 2014-05-30
for(int i=0; i<nmesh; ++i) gr_tmp[i] = 0;
// (2)
int option = 1;
this->periodic_pairs( cel, gr_tmp, option );
// (3) calculate the pair distribution function
//
// dV, equals to 4 * pi * r^2 * dr
const double prec = 4.0/3.0*INPUT.pi;
for(int i=0; i<nmesh; ++i)
{
// volume
double vv = prec*(pow((i+1)*dr,3)-pow(i*dr,3));
if(INPUT.ntype==1)
{
gr_tmp[i] = gr_tmp[i] / INPUT.natom / rho_ion / vv;
}
else if(INPUT.ntype>1)
{
const int n1 = cel.atom[INPUT.pdf_ele1-1].na;
const int n2 = cel.atom[INPUT.pdf_ele2-1].na;
gr_tmp[i] = gr_tmp[i] * INPUT.natom / n1 /n2 / rho_ion / vv;
}
/* else if(INPUT.ntype==3)
{
const int n1 = cel.atom[INPUT.pdf_ele1-1].na;
const int n2 = cel.atom[INPUT.pdf_ele2-1].na;
gr_tmp[i] = gr_tmp[i] * INPUT.natom / n1/n2/ rho_ion/vv;
}*/
}
for(int i=0; i<nmesh; ++i)
{
gr[i]+=gr_tmp[i];
}
}
// do average of 3D ssf.
assert(count_geometry_number>0);
cout << " count_geometry_number = " << count_geometry_number << endl;
if(count_geometry_number>0)
{
for(int ir=0; ir<nmesh; ++ir)
{
gr[ir] /= count_geometry_number;
}
}
// output the final pair distribution function
ofstream ofs(INPUT.geo_out.c_str());
// we output the pair correlation function and static structrue factor
for(int i=0; i<nmesh-1; ++i)
{
ofs << (i+0.5)*dr << " " << gr[i] << endl;
}
ofs.close();
// calculate the SSF via FFT,
/*
if(ng>0 && dg>0)
{
// calculate the static structure factor
this->dg = INPUT.struf_dg;
this->ng = INPUT.struf_ng;
double *sf = new double[ng];
for(int ig=0; ig<ng; ++ig) sf[ig] = 0.0;
this->static_structure_factor( cel, rho_ion, gr, sf );
// output the static structure factor.
ofstream ofss(INPUT.ssf_out.c_str());
for(int ig=0; ig<ng; ++ig)
{
double k = ig*dg;
if( k >= 2*INPUT.pi/(rcut*2.0) )
{
ofss << ig*dg << " " << sf[ig] << endl;
}
}
ofss.close();
delete[] sf;
}
*/
// --> clean <--
delete[] gr;
return;
}
// calculate the static structure factor using direct Fourier Transform.
/*
void PDF::static_structure_factor( const Cell &cel, const double &rho_ion, const double *gr, double *sf) const
{
cout << " Begin calculate the structure factor" << endl;
for(int ig=1; ig<ng; ++ig)
{
double k = ig*dg;
// cout << " k=" << k << endl;
double v = 0.0;
for(int ir=0; ir<nmesh-1; ++ir)
{
double r = ((double)ir+0.5)*dr;
double kr = r * k;
v += r*r*(gr[ir]-1.0)*sin(kr)/kr*dr;
//v += r*r*gr[ir]*sin(kr)/kr*dr;
}
sf[ig]=1+4*INPUT.pi*rho_ion*v;
}
return;
}
*/
void PDF::periodic_pairs( const Cell &cel, double *gr, const int &option)const
{
// (1) calculate the norm of each lattice vectors.
double a1 = cel.a1.norm();
double a2 = cel.a2.norm();
double a3 = cel.a3.norm();
cout << " norms of lattice vectors = " << a1 << " " << a2 << " " << a3 << endl;
// (2) calculate how many more cells need to be convered.
assert(rcut>0);
const int ncell_1 = int(rcut/a1)+1;
const int ncell_2 = int(rcut/a2)+1;
const int ncell_3 = int(rcut/a3)+1;
cout << " ncell is " << ncell_1 << " " << ncell_2 << " " << ncell_3 << endl;
// (3) calculate the distance between atoms.
double x2, y2, z2; // atom positions for atom 2.
double dx, dy, dz; // delta x,y,z between atom1, atom2.
double dis;
int which;
for(int it=0; it<INPUT.ntype; ++it)
{
// mohan add 2014-05-29
if((it+1)!=INPUT.pdf_ele1 ) continue;
for(int ia=0; ia<cel.atom[it].na; ++ia)
{
// cout << cel.atom[it].pos[ia].x << " "
// << cel.atom[it].pos[ia].y << " "
// << cel.atom[it].pos[ia].z << endl;
for(int it2=0; it2<INPUT.ntype; ++it2)
{
// mohan add 2014-05-29
if(( it2+1)!=INPUT.pdf_ele2 ) continue;
for(int ia2=0; ia2<cel.atom[it2].na; ++ia2)
{
if(it==it2 && ia==ia2) continue;
// search for 27 cells.
bool within_distance=false;
for(int i=-ncell_1; i<=ncell_1; ++i)
{
for(int j=-ncell_2; j<=ncell_2; ++j)
{
for(int k=-ncell_3; k<=ncell_3; ++k)
{
if(within_distance) continue;
// add cell length
cel.add_cell_length(it2, ia2, i, j, k, x2, y2, z2);
// calculate the distance between two atoms |r_1 - r_2|
dx = cel.atom[it].pos[ia].x - x2;
dy = cel.atom[it].pos[ia].y - y2;
dz = cel.atom[it].pos[ia].z - z2;
dis = sqrt( dx*dx + dy*dy + dz*dz );
if( dis <= rcut )
{
if(option==1)
{
which = int(dis / dr);
//cout << which; int ok; cin >> ok;
// 2 accounts fro i-j and j-i types
// gr[which] += 2.00;
gr[which] += 1.00;
}
// used to calculate the SSF.
/*
else if(option==2)
{
for(int ig=0; ig<this->ng; ++ig)
{
gr[ig]+=cos(ig*dg*dx)*2.0;
}
}
*/
within_distance=true;
}
}
}
}
}
}
}
}
return;
}