-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_RDF_for_xyz_movie.sh
256 lines (175 loc) · 4.9 KB
/
get_RDF_for_xyz_movie.sh
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
#!/bin/bash
#
# Compute g(r) from a given xyz movie file for a given element
# pair. If no element pair is given, calculate total g(r) over all
# atoms.
#
# Eero Holmstrom, 2015
#
# usage
if [[ -z "$5" ]]
then
echo "Usage: $(basename $0) [xyz file] [dr] [rmin] [rmax] [skip this many steps from beginning] [e1] [e2]"
exit 1;
fi
# assign input arguments
infile=$1
dr=$2
rmin=$3
rmax=$4
nskip=$5
# get number of atoms
natoms=$(awk '{print $1; exit}' $infile)
#
# if e1 and e2 are given by the user, then compute the g(r) for e1-e2
#
if [[ -n "$7" ]];
then
e1=$6
e2=$7
echo ""
echo "Computing g(r) for $e1-$e2..."
#
# calculate partial e1-e2 g(r)
#
awk -v nskip=$nskip -v e1=$e1 -v e2=$e2 -v natoms=$natoms -v dr=$dr -v rmin=$rmin -v rmax=$rmax 'BEGIN{n=0; nframesprocessed=0; nframestotal=0; pi=3.14159265359;};
# keep track of line number within frame
{n++;};
# get size of cell for later calculating total density
(n==2){xsize=$(NF-2); ysize=$(NF-1); zsize=$NF;};
# read in atomic coordinates for e1 and e2 for this frame
(n>2){
if ($1==e1) {i++; xe1[i]=$2; ye1[i]=$3; ze1[i]=$4; };
if ($1==e2) {j++; xe2[j]=$2; ye2[j]=$3; ze2[j]=$4; };
};
# compute the RDF once you reach the end of this frame
(n==natoms+2){
nframestotal++;
if(nframestotal > nskip){
ne1=i;
ne2=j;
# calculate relevant total density of particles
totalrho=ne2/(xsize*ysize*zsize);
#
# calculate g = g(r)
#
rcounter=0;
# loop over r
for(r=rmin+dr; r<=rmax; r=r+dr){
rcounter++;
rarray[rcounter] = r;
garray[rcounter] = 0;
# loop over all atom pairs
for (i=1; i<=ne1; i++){
for (j=1; j<=ne2; j++){
# calculate distance between atoms i and j, taking periodic boundary
# conditions into account
distx=xe1[i]-xe2[j];
disty=ye1[i]-ye2[j];
distz=ze1[i]-ze2[j];
if(distx < 0.0){distx=-1*distx;};
if(disty < 0.0){disty=-1*disty;};
if(distz < 0.0){distz=-1*distz;};
if(distx > xsize/2){distx=xsize-distx;}
if(disty > ysize/2){disty=ysize-disty;}
if(distz > zsize/2){distz=zsize-distz;}
thisr = sqrt( distx^2 + disty^2 + distz^2 );
if(thisr >= r-dr/2 && thisr < r+dr/2){garray[rcounter]++;}
};
};
# calculate density in this radial shell
Vr=4.0/3.0*pi*((r+dr/2)^3-(r-dr/2)^3);
# normalize density in this shell by total density and number of atoms used
garray[rcounter] = garray[rcounter]/Vr;
garray[rcounter] = garray[rcounter]/totalrho/ne1;
};
# update cumulative mean of RDF
for(i=1; i<=rcounter; i++){
totalrarray[i] = rarray[i];
totalgarray[i] = totalgarray[i] + garray[i];
}
# update relevant counters
nframesprocessed++;
};
# set relevant counters to zero for starting next frame
n=0; i=0; j=0;
};
# print out total cumulative mean of RDF
END{
for(i=1; i<=rcounter; i++){print totalrarray[i], totalgarray[i]/nframesprocessed};
}' $infile > this.rdf
mv this.rdf "RDF."$e1"_"$e2".dat"
echo "Done. Results output to" "RDF."$e1"_"$e2".dat"
echo ""
else
echo ""
echo "Computing total g(r) over all atoms..."
#
# calculate total g(r) over all atoms
#
awk -v nskip=$nskip -v e1=$e1 -v e2=$e2 -v natoms=$natoms -v dr=$dr -v rmin=$rmin -v rmax=$rmax 'BEGIN{n=0; i=0; nframesprocessed=0; nframestotal=0; pi=3.14159265359;};
# keep track of line number within frame
{n++;};
# get size of cell for later calculating total density
(n==2){xsize=$(NF-2); ysize=$(NF-1); zsize=$NF; totalrho=natoms/(xsize*ysize*zsize);};
# read in atomic coordinates for this frame
(n>2){ {i++; x[i]=$2; y[i]=$3; z[i]=$4;} };
# compute the RDF once you reach the end of this frame
(n==natoms+2){
nframestotal++;
if(nframestotal > nskip){
#
# calculate g = g(r)
#
rcounter=0;
# loop over r
for(r=rmin+dr; r<=rmax; r=r+dr){
rcounter++;
rarray[rcounter] = r;
garray[rcounter] = 0;
# loop over all atom pairs
for (i=1; i<=natoms; i++){
for (j=1; j<=natoms; j++){
if(i!=j){
# calculate distance between atoms i and j, taking periodic boundary
# conditions into account
distx=x[i]-x[j];
disty=y[i]-y[j];
distz=z[i]-z[j];
if(distx < 0.0){distx=-1*distx;};
if(disty < 0.0){disty=-1*disty;};
if(distz < 0.0){distz=-1*distz;};
if(distx > xsize/2){distx=xsize-distx;}
if(disty > ysize/2){disty=ysize-disty;}
if(distz > zsize/2){distz=zsize-distz;}
thisr = sqrt( distx^2 + disty^2 + distz^2 );
if(thisr >= r-dr/2 && thisr < r+dr/2){garray[rcounter]++;}
};
};
};
# calculate density in this radial shell
Vr=4.0/3.0*pi*((r+dr/2)^3-(r-dr/2)^3);
# normalize density in this shell by total density and number of atoms used
garray[rcounter] = garray[rcounter]/Vr;
garray[rcounter] = garray[rcounter]/totalrho/natoms;
};
# update cumulative mean of RDF
for(i=1; i<=rcounter; i++){
totalrarray[i] = rarray[i];
totalgarray[i] = totalgarray[i] + garray[i];
}
# update relevant counters
nframesprocessed++;
};
# set relevant counters to zero for starting next frame
n=0; i=0;
};
# print out total cumulative mean of RDF
END{
for(i=1; i<=rcounter; i++){print totalrarray[i], totalgarray[i]/nframesprocessed};
}' $infile > this.rdf
mv this.rdf "RDF.total.dat"
echo "Done. Results output to" "RDF.total.dat"
echo ""
fi
exit 0;