-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.cpp
487 lines (376 loc) · 18.2 KB
/
reader.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//
// Created by Jon on 21/08/2015.
//
#include "reader.h"
#include <iostream>
namespace CIF {
Reader::Reader(std::string filePath, bool attempt_fixes) {
file_path = filePath;
fix = attempt_fixes;
// http://stackoverflow.com/questions/2602013/read-whole-ascii-file-into-c-stdstring
std::ifstream filestream(filePath);
std::string filecontents;
filestream.seekg(0, std::ios::end);
filecontents.reserve(filestream.tellg());
filestream.seekg(0, std::ios::beg);
filecontents.assign((std::istreambuf_iterator<char>(filestream)), std::istreambuf_iterator<char>());
filecontents = Utilities::stripCommentsWhitespace(filecontents);
std::string errors;
// read in the symmetry elements
try {
readSymmetryOperations(filecontents);
} catch (std::runtime_error& e) {
errors += e.what();
}
// read in atom positions
try {
readAtoms(filecontents);
} catch (std::runtime_error& e) {
errors += e.what();
}
// read in unit cell parameters
try {
readCellGeometry(filecontents);
} catch (std::runtime_error& e) {
errors += e.what();
}
if (!errors.empty())
throw std::runtime_error(errors);
}
void Reader::readSymmetryOperations(const std::string &input) {
symmetrylist = std::vector<Symmetry>();
// first we want to know how columns we have and where the things we want are
std::regex rgxheaders("(?:loop_\\n)((?:_symmetry_equiv_pos_\\w+?\\n)+)");
std::smatch match;
std::string match_string = "";
if (!std::regex_search(input, match, rgxheaders)) {
if (fix) {
// this simple fix assumes we have a primitive cell if the symmetry is not defined
// could try and interpret the space group line, but that is a while other can of worms
auto temp_symmetry = Symmetry();
std::vector<std::string> temp_terms = {"x", "y", "z"};
temp_symmetry.setOperation(0, temp_terms);
symmetrylist = {temp_symmetry};
return;
} else
throw std::runtime_error("Cannot find _symmetry_equiv_pos_ block\n");
}
std::vector<std::string> headerlines = Utilities::split(match[1], '\n');
int xyzcol = Utilities::vectorSearch(headerlines, std::string("_symmetry_equiv_pos_as_xyz"));
if (xyzcol >= headerlines.size())
throw std::runtime_error("Could not find _symmetry_equiv_pos_as_xyz\n");
std::string symmetrypattern = "";
// this is a bit more tricky as all the columns don't have the same regex
for (int i = 0; i < headerlines.size(); ++i) {
if (i == xyzcol)
symmetrypattern += "(?:['\"]?.+?,.+?,.+?['\"]?)";
else
symmetrypattern += "\\S+";
if (i != headerlines.size() - 1)
symmetrypattern += "[ \\t]+";
}
std::regex rgxsymmetry(symmetrypattern);
std::istringstream op_ss(match.suffix().str());
std::string symmetry_line;
bool isValid = true;
// Now read through the following lines, check they match the operator regex and process each one
while (std::getline(op_ss, symmetry_line)) {
isValid = std::regex_match(symmetry_line, rgxsymmetry);
if (!isValid)
break;
std::regex rgxsymmetry_element("(['\"].+?['\"]|[\\S]+)");
std::vector<std::string> column;
while (std::regex_search(symmetry_line, match, rgxsymmetry_element)) {
column.push_back(match[1].str());
symmetry_line = match.suffix().str();
}
std::string symmetryxyz = column[xyzcol];
// this first regex splits the lines into the individual operations
std::regex rgxxyz("['\"]*\\s*([^\\s'\"]+)\\s*,\\s*([^\\s'\"]+)\\s*,\\s*([^\\s'\"]+)\\s*['\"]*");
if (!std::regex_search(symmetryxyz, match, rgxxyz))
throw std::runtime_error("Problem parsing _symmetry_equiv_pos_ line\n");
auto matched = match;
Symmetry ops;
// loop through each operation ( we know there will be 3)
for (int i = 1; i < 4; ++i) {
// the operation string
std::string op = matched[i];
std::regex rgxoperation("([+-]?[^+-]+)");
// TODO: maybe check that we have one match first so we can throw an error?
// TODO: check we are reading in 3 operations for each line?
// TODO: preallocate this vector
// match all individual terms in the one operation
// use a while loop now as we expect more than one match per string
std::vector<std::string> termstrings;
while (std::regex_search(op, match, rgxoperation)) {
for (int i = 1; i < match.size(); ++i)
termstrings.push_back(match[i].str());
// trim the string to make sure we don't find the same match
op = match.suffix().str();
}
if (termstrings.size() < 1)
throw std::runtime_error("Problem parsing symmetry operation.");
if (termstrings.size() < 1)
throw std::runtime_error("Problem parsing symmetry operation.");
ops.setOperation(i - 1, termstrings);
}
// append ops to vector to return
symmetrylist.push_back(ops);
}
if (symmetrylist.size() < 1)
throw std::runtime_error("Problem parsing _symmetry_equiv_pos_ block.");
}
void Reader::readAtoms(const std::string &input) {
atomsites = std::vector<AtomSite>();
// first we need to know where the positions we need are kept (column wise)
// we do this through the header declaration just after the "loop_"
std::regex rgxheaders(R"((?:\nloop_\n)((?:_atom_site_\w+?\n)+))");
std::smatch match;
auto start = input;
bool found = false;
// these are needed so we can parse this again for the aniso part
std::vector<std::vector<std::string>> header_strings;
std::vector<std::vector<std::string>> data_strings;
while(std::regex_search(start, match, rgxheaders)) {
found = true;
// headers
std::vector<std::string> headerlines = Utilities::split(match[1], '\n');
header_strings.push_back(headerlines);
std::string positionpattern = "((?:\\s*";
for (int i = 0; i < headerlines.size() - 1; ++i)
positionpattern += "\\S+[ \\t]+";
// the last column needs to be different to close off the regex
positionpattern += "\\S+[ \\t]*)+)";
std::regex rgxpositions(positionpattern);
std::istringstream at_ss(match.suffix().str());
std::string atom_line;
std::vector<std::string> atomlines;
while (std::getline(at_ss, atom_line)) {
if(!std::regex_match(atom_line, rgxpositions) || atom_line == "loop_") // could also test if line starts with _ ?
break;
atomlines.push_back(atom_line);
}
data_strings.push_back(atomlines);
start = match.suffix().str();
}
if (!found)
throw std::runtime_error("Could not find _atom_site_ block(s)\n");
// now process the atomc positions
std::vector<std::string> errors;
for (int i = 0; i < header_strings.size(); ++i) {
try {
readAtomPositions(header_strings[i], data_strings[i]);
errors.emplace_back("");
} catch (std::runtime_error& e) {
errors.emplace_back(e.what());
}
}
// we just need one block to not have errors for this to have 'passed'
found = errors.empty();
std::string error_out;
for (auto &er : errors) {
if (er.empty()) {
found = true;
break;
} else {
error_out += er;
}
}
if (!found)
throw std::runtime_error(error_out);
// Now the displacement stuff
for (int i = 0; i < header_strings.size(); ++i)
readThermalParameters(header_strings[i], data_strings[i]);
if (atomsites.empty())
throw std::runtime_error("Could not parse atom information");
}
void Reader::readCellGeometry(const std::string &input) {
double a = Utilities::regexFindDoubleTag(input, "_cell_length_a\\s+([\\d.]+)");
double b = Utilities::regexFindDoubleTag(input, "_cell_length_b\\s+([\\d.]+)");
double c = Utilities::regexFindDoubleTag(input, "_cell_length_c\\s+([\\d.]+)");
double alpha = Utilities::regexFindDoubleTag(input, "_cell_angle_alpha\\s+([\\d.]+)");
double beta = Utilities::regexFindDoubleTag(input, "_cell_angle_beta\\s+([\\d.]+)");
double gamma = Utilities::regexFindDoubleTag(input, "_cell_angle_gamma\\s+([\\d.]+)");
cell = CellGeometry(a, b, c, alpha, beta, gamma);
}
void Reader::readAtomPositions(const std::vector<std::string> &headers, const std::vector<std::string> &entries) {
// these are required!
int labelcol = Utilities::vectorSearch(headers, std::string("_atom_site_label"));
bool foundlabel = labelcol != headers.size();
int xcol = Utilities::vectorSearch(headers, std::string("_atom_site_fract_x"));
bool foundx = xcol != headers.size();
int ycol = Utilities::vectorSearch(headers, std::string("_atom_site_fract_y"));
bool foundy = ycol != headers.size();
int zcol = Utilities::vectorSearch(headers, std::string("_atom_site_fract_z"));
bool foundz = zcol != headers.size();
int symbolcol = Utilities::vectorSearch(headers, std::string("_atom_site_type_symbol"));
bool foundsymbol = symbolcol != headers.size();
// this is not absolutely needed (assume occupancy = 1 otherwise
int occupancycol = Utilities::vectorSearch(headers, std::string("_atom_site_occupancy"));
bool foundoccupancy = occupancycol != headers.size();
// this fix will try to get a atom symbol from the atom label
// they are often just a type symbol with a number
if (fix && !foundsymbol && foundlabel) {
symbolcol = labelcol;
foundsymbol = true;
}
std::string errors = "";
if(!foundlabel)
errors += "Could not find _atom_site_label\n";
if(!foundsymbol)
errors += "Could not find _atom_site_type_symbol\n";
if(!foundx)
errors += "Could not find _atom_site_fract_x\n";
if(!foundy)
errors += "Could not find _atom_site_fract_y\n";
if(!foundz)
errors += "Could not find _atom_site_fract_z\n";
if (!errors.empty())
throw std::runtime_error(errors);
std::smatch match;
std::regex rgxcolumns("([^\\s]+)");
for (auto &row : entries) {
std::vector<std::string> columns;
std::string line = row;
// split our line by columns
while (std::regex_search(line, match, rgxcolumns)) {
// extract column into list of vectors
for (int j = 1; j < match.size(); ++j)
columns.push_back(match[j].str());
line = match.suffix().str();
}
// get the label (This can be anything, it is not the atom type)
// this is the only simple one...
std::string label = columns[labelcol];
// type symbol can have valence etc attached to it
// There is probably a safe, non-regex way to do this
std::string symbol = "";
std::regex rgxname("([a-zA-Z]{1,2})");
if (std::regex_search(columns[symbolcol], match, rgxname))
symbol = match[1];
else
errors += "Could not find acceptable format _atom_site_type_symbol (need 1-2 letters) in: " + columns[symbolcol] + "\n";
if (fix) {
// this fix deals with the case of the symbol string
symbol[0] = std::toupper(symbol[0]);
if (symbol.length() == 2)
symbol[1] = std::tolower(symbol[1]);
}
if (!CIF::Utilities::isAcceptedAtom(symbol))
errors += "Could not detect valid atom from: " + columns[symbolcol] + " (sub string: " + symbol + ")\n";
// get the x, y, z positions of our atom site
// the split here is to get rid of uncertainties in brackets
double x, y, z;
try {
x = Utilities::stod(Utilities::split(columns[xcol], '(')[0]);
} catch (std::invalid_argument& e) {
errors += "Could not get number from position values (not a number): " + columns[xcol] + "\n";
} catch (std::out_of_range& e) {
errors += "Could not get number from position values (value too large): " + columns[xcol] + "\n";
}
try {
y = Utilities::stod(Utilities::split(columns[ycol], '(')[0]);
} catch (std::invalid_argument& e) {
errors += "Could not get number from position values (not a number): " + columns[ycol] + "\n";
} catch (std::out_of_range& e) {
errors += "Could not get number from position values (value too large): " + columns[ycol] + "\n";
}
try {
z = Utilities::stod(Utilities::split(columns[zcol], '(')[0]);
} catch (std::invalid_argument& e) {
errors += "Could not get number from position values (not a number): " + columns[zcol] + "\n";
} catch (std::out_of_range& e) {
errors += "Could not get number from position values (value too large): " + columns[zcol] + "\n";
}
// default to 1.0
double occupancy = 1.0;
if (foundoccupancy)
try {
occupancy = Utilities::stod(Utilities::split(columns[occupancycol], '(')[0]);
} catch (std::invalid_argument& e) {
errors += "Could not get number from occupancy value (not a number): " + columns[occupancycol] + "\n";
} catch (std::out_of_range& e) {
errors += "Could not get number from occupancy (value too large): " + columns[occupancycol] + "\n";
}
// if we have errrors, now is when we quit!
if (!errors.empty())
continue;
// here we are checking if the atom is on the same site
std::vector<double> postemp({x, y, z});
bool isNew = true;
for (auto &site : atomsites) {
auto positions = site.getPositions();
int ind = Utilities::vectorSearch(positions, postemp);
if (ind >= positions.size())
continue;
else {
site.addAtom(symbol, label, occupancy);
isNew = false;
break;
}
}
if (isNew)
atomsites.emplace_back(symmetrylist, symbol, label, x, y, z, occupancy);
}
if (!errors.empty())
throw std::runtime_error(errors);
}
void Reader::readThermalParameters(const std::vector<std::string> &headers, const std::vector<std::string> &entries){
// find label, normal one takes precedence
int labelcol = Utilities::vectorSearch(headers, std::string("_atom_site_label"));
bool foundlabel = labelcol < headers.size();
// then look for our aniso label
if (!foundlabel) {
labelcol = Utilities::vectorSearch(headers, std::string("_atom_site_aniso_label"));
foundlabel = labelcol < headers.size();
}
if (!foundlabel)
return;
// could be u iso?
int isocol = Utilities::vectorSearch(headers, std::string("_atom_site_B_iso_or_equiv"));
bool foundiso = isocol < headers.size();
// could be B?
int uxcol = Utilities::vectorSearch(headers, std::string("_atom_site_aniso_U_11"));
bool foundux = uxcol < headers.size();
int uycol = Utilities::vectorSearch(headers, std::string("_atom_site_aniso_U_22"));
bool founduy = uycol < headers.size();
int uzcol = Utilities::vectorSearch(headers, std::string("_atom_site_aniso_U_33"));
bool founduz = uzcol < headers.size();
std::smatch match;
std::regex rgxcolumns("([^\\s]+)");
for (auto &row : entries) {
std::vector<std::string> columns;
std::string line = row;
// split our line by columns
while (std::regex_search(line, match, rgxcolumns)) {
// extract column into list of vectors
for (int j = 1; j < match.size(); ++j)
columns.push_back(match[j].str());
line = match.suffix().str();
}
std::string lbl = columns[labelcol];
double u_iso = 0.0;
if (foundiso)
u_iso = Utilities::stod(Utilities::split(columns[isocol], '(')[0]);
double u_x = 0.0;
if(foundux)
u_x = Utilities::stod(Utilities::split(columns[uxcol], '(')[0]);
double u_y = 0.0;
if(foundux)
u_y = Utilities::stod(Utilities::split(columns[uycol], '(')[0]);
double u_z = 0.0;
if(foundux)
u_z = Utilities::stod(Utilities::split(columns[uzcol], '(')[0]);
for(auto &a : atomsites) {
if (foundiso)
a.setIsoU(lbl, u_iso);
if (foundux)
a.setU(lbl, u_x, 0);
if (founduy)
a.setU(lbl, u_y, 1);
if (founduz)
a.setU(lbl, u_z, 2);
}
}
}
}