-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReadPoscar.cpp
253 lines (213 loc) · 5.83 KB
/
ReadPoscar.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
#include <cstdlib>
#include <cstdio>
#include "CrystalFp.h"
#include "ReadPoscar.h"
static bool loadData(int aStep, FILE *aPoscarFp, FILE *aEnergyFp, bool aEnergyIsPerAtom, std::vector<unsigned int>& aAtomZ, CrystalFp& aCfp)
{
char linebuf[82];
unsigned int k;
unsigned int j;
float fx;
float fy;
float fz;
float scale;
float uc[16];
// Ignore the title
if(fgets(linebuf, sizeof linebuf, aPoscarFp) == NULL) return false;
// Read the scale factor
if(fgets(linebuf, sizeof linebuf, aPoscarFp) == NULL) return false;
sscanf(linebuf, "%f", &scale);
// Read the unit cell base vectors
for(j=0; j < 3; ++j)
{
fgets(linebuf, sizeof linebuf, aPoscarFp);
sscanf(linebuf, "%f %f %f", &uc[4*j+0], &uc[4*j+1], &uc[4*j+2]);
uc[4*j+0] *= scale;
uc[4*j+1] *= scale;
uc[4*j+2] *= scale;
uc[4*j+3] = 0.0F;
}
uc[12+0] = 0.0F;
uc[12+1] = 0.0F;
uc[12+2] = 0.0F;
uc[12+3] = 1.0F;
// Read the array of atoms numbers
if(fgets(linebuf, sizeof linebuf, aPoscarFp) == NULL) return false;
long int res;
char *endptr;
const char *next = linebuf;
unsigned int natoms = 0;
unsigned int ntypes = 0;
for(;;)
{
res = strtol(next, &endptr, 10);
if(endptr == next) break;
natoms += res;
++ntypes;
next = endptr;
}
int *natomtype = new int[ntypes];
next = linebuf;
for(k=0;;++k)
{
res = strtol(next, &endptr, 10);
if(endptr == next) break;
natomtype[k] = res;
next = endptr;
}
// Skip the "Selective Dynamics" line
if(fgets(linebuf, sizeof linebuf, aPoscarFp) == NULL) {delete [] natomtype; return false;}
if(linebuf[0] == 'S' || linebuf[0] == 's')
{
if(fgets(linebuf, sizeof linebuf, aPoscarFp) == NULL) {delete [] natomtype; return false;}
}
// Set the kind of coordinates (cartesian or fractional)
bool cartesian = false;
if(linebuf[0] == 'C' || linebuf[0] == 'c' || linebuf[0] == 'K' || linebuf[0] == 'k')
{
cartesian = true;
}
else if(linebuf[0] != 'D' && linebuf[0] != 'd')
{
delete [] natomtype;
return false;
}
// Sanity check
if(natoms < 1)
{
delete [] natomtype;
return false;
}
// Allocate the various array for AddStructure
unsigned int *z = new unsigned int[natoms];
float *coords = new float[3*natoms];
// Read the atom records
unsigned int currbnd = natomtype[0];
for(j=k=0; j < natoms; ++j)
{
fgets(linebuf, sizeof linebuf, aPoscarFp);
if(sscanf(linebuf, "%f %f %f", &fx, &fy, &fz) < 3)
{
delete [] natomtype;
delete [] z;
delete [] coords;
return false;
}
// Change atom type
if(j >= currbnd)
{
++k;
currbnd += natomtype[k];
}
// Add the atom converting coordinates from fractional
z[j] = (aAtomZ.size() > k) ? aAtomZ[k] : k+1;
if(cartesian)
{
coords[3*j+0] = fx;
coords[3*j+1] = fy;
coords[3*j+2] = fz;
}
else
{
coords[3*j+0] = fx*uc[0]+fy*uc[4]+fz*uc[8];
coords[3*j+1] = fx*uc[1]+fy*uc[5]+fz*uc[9];
coords[3*j+2] = fx*uc[2]+fy*uc[6]+fz*uc[10];
}
}
// Read the energy, if present and add the structure
if(aEnergyFp)
{
float energy = 0.0F;
fgets(linebuf, sizeof linebuf, aEnergyFp);
sscanf(linebuf, "%f", &energy);
aCfp.addStructureBatch(aStep, natoms, coords, z, uc, true, energy, aEnergyIsPerAtom);
}
else
{
aCfp.addStructureBatch(aStep, natoms, coords, z, uc, false, 0.0F, false);
}
delete [] natomtype;
delete [] z;
delete [] coords;
return true;
}
static bool skipStep(FILE *aPoscarFp, FILE *aEnergyFp)
{
char linebuf[82];
unsigned int j;
// Read and ignore the title
if(fgets(linebuf, sizeof linebuf, aPoscarFp) == NULL) return false;
// Read and ignore the scale factor
if(fgets(linebuf, sizeof linebuf, aPoscarFp) == NULL) return false;
// Read and ignore the unit cell base vectors
if(fgets(linebuf, sizeof linebuf, aPoscarFp) == NULL) return false;
if(fgets(linebuf, sizeof linebuf, aPoscarFp) == NULL) return false;
if(fgets(linebuf, sizeof linebuf, aPoscarFp) == NULL) return false;
// Read the array of atoms numbers
if(fgets(linebuf, sizeof linebuf, aPoscarFp) == NULL) return false;
long int res;
char *endptr;
const char *next = linebuf;
unsigned int natoms = 0;
for(;;)
{
res = strtol(next, &endptr, 10);
if(endptr == next) break;
natoms += res;
next = endptr;
}
// Skip the "Direct" line
if(fgets(linebuf, sizeof linebuf, aPoscarFp) == NULL) return false;
// Read the atom records
for(j=0; j < natoms; ++j)
{
if(fgets(linebuf, sizeof linebuf, aPoscarFp) == NULL) return false;
}
// Read the energy
if(aEnergyFp)
{
if(fgets(linebuf, sizeof linebuf, aEnergyFp) == NULL) return false;
}
return true;
}
void readPoscarAndEnergies(const char* aFilenamePoscar,
const char* aFilenameEnergies,
bool aEnergyIsPerAtom,
unsigned int aStartStep,
unsigned int aEndStep,
std::vector<unsigned int>& aAtomZ,
CrystalFp& aCfp)
{
if(!aFilenamePoscar)
{
throw CrystalFpFatal("No files given");
}
// Read POSCAR
FILE* fpp = fopen(aFilenamePoscar, "r");
if(!fpp)
{
fprintf(stderr, "Cannot open POSCAR file %s\n", aFilenamePoscar);
throw CrystalFpFatal();
}
FILE *fpe = 0;
if(aFilenameEnergies)
{
fpe = fopen(aFilenameEnergies, "r");
if(!fpe)
{
fprintf(stderr, "Cannot open energy file %s\n", aFilenameEnergies);
fclose(fpp);
throw CrystalFpFatal();
}
}
// Skip the inital steps
unsigned int n = 1;
for(; n < aStartStep; ++n) if(!skipStep(fpp, fpe)) {fclose(fpp); if(fpe) fclose(fpe); return;}
// Read requested steps
while((aEndStep < 1 || n <= aEndStep) && loadData(n, fpp, fpe, aEnergyIsPerAtom, aAtomZ, aCfp)) ++n;
// Finish the batch loading of structure
aCfp.addStructureBatchFinish();
// Close and return
fclose(fpp);
if(fpe) fclose(fpe);
}