-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.cpp
204 lines (176 loc) · 5.47 KB
/
snapshot.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
#include "snapshot.h"
ENUM_DUMP get_dump( FILE* fp, Snapshot* snap) {
const char s_timestep[] = "ITEM: TIMESTEP";
const char s_n_atoms[] = "ITEM: NUMBER OF ATOMS";
const char s_box_bounds[] = "ITEM: BOX BOUNDS pp pp pp";
const char s_box_bounds_z[] = "ITEM: BOX BOUNDS pp pp ff";
const char s_box_bounds_xy[] = "ITEM: BOX BOUNDS ff ff pp";
const char s_box_bounds_xyz[] = "ITEM: BOX BOUNDS ff ff ff";
const char s_atoms_rm[] = "ITEM: ATOMS id type xu yu zu mux muy muz";
const char s_atoms_rvm[] = "ITEM: ATOMS id type xu yu zu vx vy vz mux muy muz";
const char s_atoms_pos_r[] = "ITEM: ATOMS id type xu yu zu";
const int i_timestep = strlen(s_timestep);
const int i_n_atoms = strlen(s_n_atoms);
const int i_box_bounds = strlen(s_box_bounds);
const int i_atoms_rm = strlen(s_atoms_rm);
const int i_atoms_pos_r = strlen(s_atoms_pos_r);
const int i_atoms_rvm = strlen(s_atoms_rvm);
const char delimeter[] = " ";
char errorstr[1000];
long timestep;
int n_atoms;
real xlow,xhigh,ylow,yhigh,zlow,zhigh;
int id,type;
real xu,yu,zu;
real vx,vy,vz;
real mux,muy,muz;
int i;
struct atom* p_atom;
read_lines(1,fp);
if( strncmp(s_timestep,line,i_timestep) !=0) {
sprintf(errorstr, "%s is not %s\n",s_timestep,line);
error(errorstr);
return ERR1;
}
read_lines(1,fp);
timestep = atol(line);
fprintf(stderr,"atol(line) = %ld\n"
"atoi(line) = %d\n"
, timestep,atoi(line));
read_lines(1,fp);
if( strncmp(s_n_atoms,line,i_n_atoms) !=0) {
return ERR1;
}
read_lines(1,fp);
n_atoms = atoi(line);
bool pbc[3];
read_lines(1,fp);
if( strncmp(s_box_bounds,line,i_box_bounds) ==0) {
pbc[0] = true; pbc[1]=true; pbc[2]=true;
}
else if( strncmp(s_box_bounds_z,line,i_box_bounds) ==0) {
pbc[0] = true; pbc[1]=true; pbc[2]=false;
}
else if( strncmp(s_box_bounds_xy,line,i_box_bounds) ==0) {
pbc[0] = false; pbc[1]=false; pbc[2]=true;
}
else if( strncmp(s_box_bounds_xyz,line,i_box_bounds) ==0) {
pbc[0] = false; pbc[1]=false; pbc[2]=false;
}
else {
(error("not ITEM: BOX BOUNDS pp pp pp(ff)"));
return ERR1;
}
read_lines(1,fp);
xlow = atof(strtok(line,delimeter));
xhigh = atof(strtok(NULL,delimeter));
read_lines(1,fp);
ylow = atof(strtok(line,delimeter));
yhigh = atof(strtok(NULL,delimeter));
read_lines(1,fp);
zlow = atof(strtok(line,delimeter));
zhigh = atof(strtok(NULL,delimeter));
struct box3 box = {xlow,xhigh,ylow,yhigh,zlow,zhigh, pbc[0], pbc[1], pbc[2]};
snap->init(timestep,n_atoms);
snap->setBox(box);
error(s_timestep);
// fprintf(stderr,"%ld\n", timestep);
error(s_n_atoms);
// fprintf(stderr,"%d\n", snap->n_atoms);
error(s_box_bounds);
// fprintf(stderr,"%f %f\n", snap->box.xlow,snap->box.xhigh);
// fprintf(stderr,"%f %f\n", snap->box.ylow,snap->box.yhigh);
// fprintf(stderr,"%f %f\n", snap->box.zlow,snap->box.zhigh);
read_lines(1,fp);
if( strncmp(s_atoms_rvm,line,i_atoms_rvm) ==0) {
for (i=0; i<n_atoms; i++){
read_lines(1,fp);
id =atoi(strtok(line, delimeter));
type = atoi(strtok(NULL,delimeter));
xu = atof(strtok(NULL,delimeter));
yu = atof(strtok(NULL,delimeter));
zu = atof(strtok(NULL,delimeter));
vx = atof(strtok(NULL,delimeter));
vy = atof(strtok(NULL,delimeter));
vz = atof(strtok(NULL,delimeter));
mux = atof(strtok(NULL,delimeter));
muy = atof(strtok(NULL,delimeter));
muz = atof(strtok(NULL,delimeter));
p_atom = &(*snap).atoms[i];
p_atom->init(id,type,xu,yu,zu,mux,muy,muz);
/* fprintf(stderr,"%d %d %f %f %f %f %f %f\n",
id,type,
xu,yu,zu,
mux,muy,muz);*/
}
}else if( strncmp(s_atoms_rm,line,i_atoms_rm) ==0) {
for (i=0; i<n_atoms; i++){
read_lines(1,fp);
id =atoi(strtok(line, delimeter));
type = atoi(strtok(NULL,delimeter));
xu = atof(strtok(NULL,delimeter));
yu = atof(strtok(NULL,delimeter));
zu = atof(strtok(NULL,delimeter));
mux = atof(strtok(NULL,delimeter));
muy = atof(strtok(NULL,delimeter));
muz = atof(strtok(NULL,delimeter));
p_atom = &(*snap).atoms[i];
p_atom->init(id,type,xu,yu,zu,mux,muy,muz);
/* fprintf(stderr,"%d %d %f %f %f %f %f %f\n",
id,type,
xu,yu,zu,
mux,muy,muz);*/
}
}
else if (strncmp(s_atoms_pos_r,line,i_atoms_pos_r) ==0 ) {
for (i=0; i<n_atoms; i++){
read_lines(1,fp);
id =atoi(strtok(line, delimeter));
type = atoi(strtok(NULL,delimeter));
xu = atof(strtok(NULL,delimeter));
yu = atof(strtok(NULL,delimeter));
zu = atof(strtok(NULL,delimeter));
mux = 0.;
muy = 0.;
muz = 0.;
p_atom = &(*snap).atoms[i];
p_atom->init(id,type,xu,yu,zu,mux,muy,muz);
/* fprintf(stderr,"%d %d %f %f %f %f %f %f\n",
id,type,
xu,yu,zu,
mux,muy,muz);*/
}
}
else {
error(line);
error("not ITEM: ATOMS id type xu yu zu mux muy muz"
"\n nor ITEM: ATOMS id type xu yu zu");
return ERR1;
}
return SUCCESS;
}
Snapshot* read_dump( FILE* fp) {
struct Snapshot* snap = new struct Snapshot;
ENUM_DUMP ret = get_dump(fp, snap);
if (ret != SUCCESS) return NULL;
return snap;
}
void* error( const char * string ) {
fputs(string, stderr);
fputs("\n", stderr);
return NULL;
// exit(1);
}
void read_lines(int n,FILE* fp) // from lammps reader_native.cpp
{
char *eof;
for (int i = 0; i < n; i++) eof = fgets(line,MAXLINE,fp);
if (eof == NULL) error("Unexpected end of dump file");
}
int compare_atom_type(const void * first, const void * second){
if ( ((atom*) first) ->type < ((atom*) second )->type )
return 1;
else if ( ((atom*) first) ->type > ((atom*) second )->type )
return -1;
else return 0;
}