-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwscfData.cpp
387 lines (352 loc) · 11.8 KB
/
pwscfData.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <sstream>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <fstream>
#include <cmath>
#include "types.h"
#include "pwscfData.h"
using namespace std;
void testPwscfParser(int argc, char* argv[]) {
string inFileName = "undef";
string outFileName = "undef";
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i],"--infile")) {
inFileName = string(argv[i+1]);
} else if (!strcmp(argv[i],"--outfile")) {
outFileName = string(argv[i+1]);
}
}
if (inFileName == "undef") {
cout << "No pwscf input filename was given" << endl;
} else {
pwscfString ps(inFileName);
pwscfData pd(ps);
cout << "Scraped Data in input file: " << inFileName << endl;
cout << "Prefix = " << pd.prefix << endl;
cout << "Pseudopotential Directory = " << pd.ppDir << endl;
cout << "Output Directory = " << pd.outputDir << endl;
cout << "There are " << pd.ntyp << " types of ion" << endl;
cout << "The ion types are:" << endl;
for(int i = 0; i < pd.atNames.size(); i++) {
cout << " " << pd.atNames[i] << ", with pseudopotential at: " << pd.atNameToPPFile[pd.atNames[i]] << endl;
}
cout << "There are " << pd.numAt << " ions in total" << endl;
cout << "The ions are at: " << endl;
for (int i = 0; i < pd.ionNames.size(); i++) {
cout << " " << pd.ionNames[i] << " " << pd.atpos[i][0] << " " << pd.atpos[i][1] << " " << pd.atpos[i][2] << endl;
}
cout << "The primitive translation vectors for the cell are:" << endl;
cout << " " << pd.ptv[0] << " " << pd.ptv[1] << " " << pd.ptv[2] << endl;
cout << " " << pd.ptv[3] << " " << pd.ptv[4] << " " << pd.ptv[5] << endl;
cout << " " << pd.ptv[6] << " " << pd.ptv[7] << " " << pd.ptv[8] << endl;
}
if (outFileName == "undef") {
cout << "No pwscf output filename was given" << endl;
} else {
pwscfOutString pos(outFileName);
cout << endl;
cout << "Scraped Data in output file: " << outFileName << endl;
cout << "The final magnetization of the calculation was: " << pos.magnetization << endl;
cout << "Details on the pseudopotentials in the output: " << endl;
for (map<string, int>::iterator it = pos.atNamesToValence.begin(); it != pos.atNamesToValence.end(); it++) {
cout << " " << it->first << ", valence = " << it->second << ", mass = " << pos.atNamesToMass[it->first] << endl;
}
}
}
// for the moment, always asking values to be set in bohr and
// atom positions to be given in crystal units
pwscfData::pwscfData(const pwscfString& ps) {
prefix = ps.getPwscfToken("prefix");
ppDir = ps.getPwscfToken("pseudo_dir");
outputDir = ps.getPwscfToken("outdir");
ntyp = atoi((ps.getPwscfToken("ntyp")).c_str());
numAt = atoi((ps.getPwscfToken("nat")).c_str());
numSpin = atoi((ps.getPwscfToken("nspin")).c_str());
setupPtv(ps);
handleAtomicSpecies(ps);
handleAtomicPositions(ps);
}
void pwscfData::setupPtv(const pwscfString& ps) {
// set up primitive translation vectors
const double scale = atof((ps.getPwscfToken("celldm(1)")).c_str());
const int ibrav = atoi((ps.getPwscfToken("ibrav")).c_str());
for (int i = 0; i < 9; i++) {
ptv[i] = 0.0;
}
if (ibrav == 0) {
string ptvstr = ps.getPwscfCard("CELL_PARAMETERS");
stringstream ss(ptvstr);
string value;
for (int i = 0; i < 9; i++) {
ss >> value;
ptv[i] = atof(value.c_str());
}
} else if (ibrav == 1) {
ptv[0] = scale;
ptv[4] = scale;
ptv[8] = scale;
} else if (ibrav == 2) {
ptv[0] = -scale*0.5;
ptv[2] = scale*0.5;
ptv[4] = scale*0.5;
ptv[5] = scale*0.5;
ptv[6] = -scale*0.5;
ptv[7] = scale*0.5;
} else if (ibrav == 3) {
for (int i = 0; i < 9; i++) {
ptv[i] = 0.5*scale;
}
ptv[3] *= -1;
ptv[6] *= -1;
ptv[7] *= -1;
} else if (ibrav == 4) {
const double covera = atof((ps.getPwscfToken("celldm(1)")).c_str());
ptv[0] = scale;
ptv[3] = -0.5*scale;
ptv[4] = sqrt(3.0)*0.5*scale;
ptv[8] = scale*covera;
}
}
void pwscfData::handleAtomicSpecies(const pwscfString& ps) {
string asstr = ps.getPwscfCard("ATOMIC_SPECIES");
stringstream ss(asstr);
string line;
while(getline(ss, line)) {
string atSymbol;
string atWeight;
string ppName;
stringstream liness(line);
liness >> atSymbol;
liness >> atWeight;
liness >> ppName;
atNames.push_back(atSymbol);
string fullPPName = ppDir;
if (fullPPName.at(fullPPName.length()-1) != '/') {
fullPPName.append("/");
}
fullPPName.append(ppName);
atNameToPPFile[atSymbol] = fullPPName;
}
}
void pwscfData::handleAtomicPositions(const pwscfString& ps) {
string apstr = ps.getPwscfCard("ATOMIC_POSITIONS");
stringstream ss(apstr);
string line;
while(getline(ss, line)) {
string atSymbol;
location temploc;
stringstream liness(line);
liness >> atSymbol;
liness >> temploc[0];
liness >> temploc[1];
liness >> temploc[2];
ionNames.push_back(atSymbol);
atpos.push_back(temploc);
}
}
pwscfOutString::pwscfOutString(const string& outFileName) {
ifstream t(outFileName.c_str());
stringstream buffer;
if (t.is_open()) {
buffer << t.rdbuf();
outputFileText = buffer.str();
} else {
cout << "Unable to open pwscf input file: " << outFileName << endl;
}
magnetization = 0.0;
string line;
int grabPPdata = 0;
const string ppHeader = "atomic species valence";
const string magString = "total magnetization";
while(getline(buffer, line)) {
size_t magindex = line.find(magString);
if (magindex != string::npos) {
stringstream ss(line);
string dummy;
ss >> dummy;
ss >> dummy;
ss >> dummy;
ss >> magnetization;
}
if (grabPPdata == 1) {
if(line.find_first_not_of(' ') == string::npos) {
// if we grab a blank line then we have left the pp definitions
grabPPdata = 0;
} else {
stringstream ss(line);
string name;
double valence;
int intValence;
double mass;
ss >> name;
ss >> valence;
valence += 0.5;
intValence = static_cast<int>(valence);
ss >> mass;
atNamesToValence[name] = intValence;
atNamesToMass[name] = mass;
}
}
size_t index = line.find(ppHeader);
if (index != string::npos) {
grabPPdata = 1;
}
}
}
pwscfString::pwscfString(const string& pwscfFileName) {
ifstream t(pwscfFileName.c_str());
if (t.is_open()) {
stringstream buffer;
buffer << t.rdbuf();
inputFileText = buffer.str();
doctoredText = inputFileText;
} else {
cout << "Unable to open pwscf input file: " << pwscfFileName << endl;
}
}
string pwscfString::getPwscfToken (const string& tokenName, const string& searchText) const {
stringstream buffer(searchText);
string line;
string outval;
string outval2;
while(getline(buffer, line)) {
size_t index = line.find(tokenName);
if (index != string::npos) {
outval = line.substr(line.find(tokenName)+tokenName.size()); // get everything after token
outval = outval.substr(outval.find('=')+1); // get everything after equals sign
// next two lines are a hacky way to get only the first set of characters in outval
stringstream temp(outval);
temp >> outval2;
outval2.erase(remove(outval2.begin(), outval2.end(), ','),outval2.end()); //get rid of commas
outval2.erase(remove(outval2.begin(), outval2.end(), '\''),outval2.end()); //get rid of quotes
}
}
return outval2;
}
string pwscfString::getRawPwscfToken (const string& tokenName, const string& searchText) const {
stringstream buffer(searchText);
string line;
string outval;
string outval2;
while(getline(buffer, line)) {
size_t index = line.find(tokenName);
if (index != string::npos) {
outval = line.substr(line.find(tokenName)+tokenName.size()); // get everything after token
outval = outval.substr(outval.find('=')+1); // get everything after equals sign
// next two lines are a hacky way to get only the first set of characters in outval
stringstream temp(outval);
temp >> outval2;
outval2.erase(remove(outval2.begin(), outval2.end(), ','),outval2.end()); //get rid of quotes
}
}
return outval2;
}
void pwscfString::changePwscfToken(const string& tokenName, const string& newValue) {
string valueToChange = getRawPwscfToken(tokenName, doctoredText);
size_t index = doctoredText.find(valueToChange);
if (index == string::npos) {
} else {
doctoredText.replace(index, valueToChange.size(), newValue);
}
}
void pwscfString::addToPwscfSection(const string& secName, const string& tokenName, const string& valName) {
string secNameUpper;
string secNameLower;
secNameUpper.resize(secName.size());
secNameLower.resize(secName.size());
transform(secName.begin(), secName.end(), secNameUpper.begin(), ::toupper);
transform(secName.begin(), secName.end(), secNameLower.begin(), ::tolower);
stringstream buffer(doctoredText);
stringstream outbuffer;
string line;
while(getline(buffer, line)) {
outbuffer << line << endl;
size_t lowerindex = line.find(secNameUpper);
size_t upperindex = line.find(secNameLower);
if (lowerindex != string::npos || upperindex != string::npos) {
outbuffer << " " << tokenName << " = " << valName << endl;
}
}
doctoredText = outbuffer.str();
}
void pwscfString::removePwscfToken(const string& tokenName) {
stringstream buffer(doctoredText);
stringstream outbuffer;
string line;
while(getline(buffer, line)) {
size_t index = line.find(tokenName);
if (index == string::npos) {
outbuffer << line << endl;
} else {
// if there is no comma, just remove the line
// if there is a comma and there is something after it
// just remove the section of the string that is the token
size_t endindex = line.find(',');
if (endindex != string::npos) {
if (endindex != line.size()) {
string newline = line.erase(index, endindex-index);
outbuffer << newline;
}
}
}
}
doctoredText = outbuffer.str();
}
string pwscfString::getWholePwscfCard(const string& cardName, const string& searchText) const {
stringstream buffer(searchText);
stringstream outbuffer;
string line;
int start = 0;
int stop = 0;
while(getline(buffer, line)) {
size_t index = line.find(cardName);
if (start == 1) {
size_t asindex = line.find("ATOMIC_POSITIONS");
size_t kpindex = line.find("K_POINTS");
size_t cpindex = line.find("CELL_PARAMETERS");
size_t consindex = line.find("CONSTRAINTS");
size_t occindex = line.find("OCCUPATIONS");
size_t afindex = line.find("ATOMIC_FORCES");
if (asindex != string::npos || kpindex != string::npos || cpindex != string::npos
|| consindex != string::npos || occindex != string::npos || afindex != string::npos) {
stop = 1;
}
}
if (index != string::npos) { start = 1; }
if (start == 1 && stop == 0) {
if(line.find_first_not_of(' ') != string::npos) {
outbuffer << line << endl;
}
}
}
return outbuffer.str();
}
string pwscfString::getPwscfCard(const string& cardName, const string& searchText) const {
string str = getWholePwscfCard(cardName, searchText);
stringstream buffer(str);
stringstream outbuffer;
string line;
getline(buffer,line);
while(getline(buffer, line)) {
outbuffer << line << endl;
}
return outbuffer.str();
}
void pwscfString::replacePwscfCard(const string& cardName, const string& newText) {
string valueToChange = getPwscfCard(cardName);
size_t index = doctoredText.find(valueToChange);
if (index == string::npos) {
} else {
doctoredText.replace(index, valueToChange.size(), newText);
}
}
void pwscfString::replaceWholePwscfCard(const string& cardName, const string& newText) {
string valueToChange = getWholePwscfCard(cardName);
size_t index = doctoredText.find(valueToChange);
if (index == string::npos) {
} else {
doctoredText.replace(index, valueToChange.size(), newText);
}
}