-
Notifications
You must be signed in to change notification settings - Fork 5
/
single_atom_vacf.c
227 lines (208 loc) · 6.46 KB
/
single_atom_vacf.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "math.h"
#define MAXNAME 250
#define MAXL 250
#define MAXW 650
#define LONGS 5000
// Functions
int parse_string(char* s, char* a[]);
void char_to_float(char* in[], double out[], int nw);
/* Program for computing components of the velocity autocorrelation
* function from a LAMMPS dump file for a single atom
* Syntax: single_atom_vacf <dumpfile> <file_x> <file_y> <file_z> #atom
* Notes: - <dumpfile> is the standard LAMMPS .d file;
* <file_x>, <file_y>, and <file_z> are output file names for
* x, y, and z components of the single atom VACF;
* #atom is the ID number of the atom in question
* - Number of atoms assumed fixed.
- Structure of the file - order and names of
variables in the dumpfile - is hardcoded.
To change, need to modify the code.
- Outputs 3 files - for x, y, and z diretion with VACF
inner sum components for the atom i.e. each of the files
has a form:
v(t0)v(t0+t0)|v(t1)v(t1+t0)|...|v(tN)v(tN+t0)
.
.
v(t0)v(t0+tM)|v(t1)v(t1+tM)|...|v(tN)v(tN+tM)
where v is atom's velocity, ts are the time steps,
N is the total number of time steps and M is N-dt
i.e. - one time step before N
- Array sizes above (#define ...) may have to be
made larger based on the input file
Last modified: December 19 2015
*/
int main(int argc, char *argv[])
{
// Miscelaneous variables
char *file_in, *file_out_1, *file_out_2, *file_out_3;
char *sp;
char s[MAXL];
int frame=0, nat, k, itm, ktm, jtm, k2;
int iat,nw,ind,tmp;
char *wa[MAXW];
double w[MAXW];
// Pointers to files
FILE *fpi, *fpo1, *fpo2, *fpo3;
// Usage check
if(argc!=6){
printf("Error\n");
printf("Syntax single_atom_vacf <dumpfile> <file_x> <file_y> <file_z> #atom\n");
exit(1);
}
// Get the names and open the dump file
file_in=argv[1];
file_out_1=argv[2];
file_out_2=argv[3];
file_out_3=argv[4];
ktm=atoi(argv[5]);
//
fpi=fopen(file_in,"r");
// Get the number of frames and the number
// of atoms
while((sp = fgets(s,MAXL,fpi))!= NULL){
// Collect the timestep
if(!strcmp(sp,"ITEM: TIMESTEP\n"))
frame++;
if(!strcmp(sp,"ITEM: NUMBER OF ATOMS\n"))
nat = atoi(fgets(s,MAXL,fpi));
}
// Print the information
printf("Number of frames: %d and number of atoms %d\n",frame, nat);
printf("Chosen atom: %d \n", ktm);
rewind(fpi);
// Allocate space for arrays
// ----------------------------------------------------------
// VACF_x components
double** VACF_x= (double**) malloc(frame*sizeof(double*));
for(k=0;k<frame;k++)
VACF_x[k]= (double*) malloc(frame*sizeof(double));
// Initialize
for(k=0;k<frame;k++)
for(k2=0;k2<frame;k2++)
VACF_x[k][k2]=0.0;
// VACF_y components
double** VACF_y= (double**) malloc(frame*sizeof(double*));
for(k=0;k<frame;k++)
VACF_y[k]= (double*) malloc(frame*sizeof(double));
// Initialize
for(k=0;k<frame;k++)
for(k2=0;k2<frame;k2++)
VACF_y[k][k2]=0.0;
// VACF_z components
double** VACF_z= (double**) malloc(frame*sizeof(double*));
for(k=0;k<frame;k++)
VACF_z[k]= (double*) malloc(frame*sizeof(double));
// Initialize
for(k=0;k<frame;k++)
for(k2=0;k2<frame;k2++)
VACF_z[k][k2]=0.0;
// Temporary velocity storage
double** vel_temp= (double**) malloc(frame*sizeof(double*));
for(k=0;k<frame;k++)
vel_temp[k]= (double*) malloc(3*sizeof(double));
// Main computation
// ----------------------------------------------------------
// VACF components for a single atom
// Loop through the whol file, store velocity components
// from each frame, than proceed with computations
// for that one atom
tmp=0;
while((sp = fgets(s,MAXL,fpi))!= NULL){
// Change the header here if needed
if(!strcmp(sp,"ITEM: ATOMS id x y z vx vy vz c_myPE c_myKE c_myStress[1] c_myStress[2] c_myStress[3] c_myStress[4] c_myStress[5] c_myStress[6] \n")){
iat=0;
// Get the data as a string, parse it into words and
// then convert each word to a float
while(iat<nat){
sp = fgets(s,MAXL,fpi);
nw=parse_string(s,wa);
char_to_float(wa,w,nw);
ind=w[0]-1;
// If it is the current atom ktm
// save the velocity data, break
// the inner while loop and move on
// to the next frame
if(ind+1==ktm){
vel_temp[tmp][0]=w[4];
vel_temp[tmp][1]=w[5];
vel_temp[tmp][2]=w[6];
tmp++;
break;
}
iat++;
}
}
}
for(itm=0;itm<frame;itm++){
for(jtm=0;jtm<=frame-itm-1;jtm++){
VACF_x[itm][jtm]=vel_temp[jtm][0]*vel_temp[jtm+itm][0];
VACF_y[itm][jtm]=vel_temp[jtm][1]*vel_temp[jtm+itm][1];
VACF_z[itm][jtm]=vel_temp[jtm][2]*vel_temp[jtm+itm][2];
}
}
// Write output
// ----------------------------------------------------------
fpo1=fopen(file_out_1,"w");
for(jtm=0;jtm<frame;jtm++){
for(itm=0;itm<frame;itm++)
fprintf(fpo1, "%f ", VACF_x[jtm][itm]);
fprintf(fpo1,"\n");
}
fclose(fpo1);
fpo2=fopen(file_out_2,"w");
for(jtm=0;jtm<frame;jtm++){
for(itm=0;itm<frame;itm++)
fprintf(fpo2, "%f ", VACF_y[jtm][itm]);
fprintf(fpo2,"\n");
}
fclose(fpo2);
fpo3=fopen(file_out_3,"w");
for(jtm=0;jtm<frame;jtm++){
for(itm=0;itm<frame;itm++)
fprintf(fpo3, "%f ", VACF_z[jtm][itm]);
fprintf(fpo3,"\n");
}
fclose(fpo3);
// Free arrays
// ----------------------------------------------------------
for(k=0;k<frame;k++)
free(VACF_x[k]);
free(VACF_x);
for(k=0;k<frame;k++)
free(VACF_y[k]);
free(VACF_y);
for(k=0;k<frame;k++)
free(VACF_z[k]);
free(VACF_z);
for(k=0;k<frame;k++)
free(vel_temp[k]);
free(vel_temp);
// Close the input file
// ----------------------------------------------------------
fclose(fpi);
}
/* Function to parse a line of input into an aray of words */
/* s - string to be parsed
* a - string with parsed elements */
int parse_string(char* s,char* a[])
{
int nw,j;
a[0] = strtok(s," \t\n\r\v\f");
nw = 1;
while((a[nw]= strtok(NULL," \t\n\r\v\f"))!=NULL)
nw++;
return nw;
}
/* Function to convert array of words to array
* of doubles
* in[] - string with pointers to words
* out[] - string with doubles */
void char_to_float(char* in[], double out[], int nw)
{
int k;
for(k=0;k<nw;k++)
out[k]=atof(in[k]);
}