-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathparse_fcsxml.cpp
More file actions
349 lines (274 loc) · 11.3 KB
/
Copy pathparse_fcsxml.cpp
File metadata and controls
349 lines (274 loc) · 11.3 KB
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
/*
parse_fcsxml.cpp
Copyright (c) 2021 Terumasa Tadano
This file is distributed under the terms of the MIT license.
Please see the file 'LICENCE.txt' in the root directory
or http://opensource.org/licenses/mit-license.php for information.
*/
#include "parse_fcsxml.h"
#include "xml_parser.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <map>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/foreach.hpp>
#include <boost/version.hpp>
#include <boost/lexical_cast.hpp>
#include "memory.h"
#include "../include/mathfunctions.h"
using namespace std;
int main(int argc, char *argv[])
{
std::string file_xml;
int maxorder;
file_xml = argv[1];
maxorder = boost::lexical_cast<int>(argv[2]);
if (maxorder <= 1) {
std::cout << "Maxorder should be larger than 1. " << std::endl;
exit(EXIT_FAILURE);
}
maxorder -= 1;
std::vector<FcsArrayWithCell> *fc;
StructureProperty structure;
allocate(fc, maxorder);
load_fcs_xml(file_xml, maxorder, structure, fc);
std::string::size_type const p(file_xml.find_last_of('.'));
std::string prefix = file_xml.substr(0, p);
int **map_p2s;
double ***x_image;
allocate(map_p2s, structure.natmin, structure.ntran);
allocate(x_image, 27, structure.nat, 3);
auto icell = 0;
for (auto i = 0; i < structure.nat; ++i) {
x_image[0][i][0] = structure.atoms[i].x;
x_image[0][i][1] = structure.atoms[i].y;
x_image[0][i][2] = structure.atoms[i].z;
}
// Convert to Cartesian coordinate
frac2cart(x_image[0], structure.nat, structure.lattice_vector);
for (auto ia = -1; ia <= 1; ++ia) {
for (auto ja = -1; ja <= 1; ++ja) {
for (auto ka = -1; ka <= 1; ++ka) {
if (ia == 0 && ja == 0 && ka == 0) continue;
++icell;
for (auto i = 0; i < structure.nat; ++i) {
x_image[icell][i][0] = structure.atoms[i].x + static_cast<double>(ia);
x_image[icell][i][1] = structure.atoms[i].y + static_cast<double>(ja);
x_image[icell][i][2] = structure.atoms[i].z + static_cast<double>(ka);
}
// Convert to Cartesian coordinate
frac2cart(x_image[icell], structure.nat, structure.lattice_vector);
}
}
}
for (auto i = 0; i < structure.nat; ++i) {
map_p2s[structure.atoms[i].atom][structure.atoms[i].tran] = i;
}
for (auto order = 0; order < maxorder; ++order) {
std::string fname_fc = prefix + ".fc" + std::to_string(order + 2);
write_fcs_to_file(fname_fc,
order,
structure,
x_image,
map_p2s,
fc[order]);
}
deallocate(fc);
deallocate(x_image);
deallocate(map_p2s);
}
void load_fcs_xml(const std::string file_in,
const int maxorder,
StructureProperty &StructProp,
std::vector<FcsArrayWithCell> *force_constant_with_cell)
{
using namespace boost::property_tree;
ptree pt;
map<string, int> dict_atomic_kind;
std::stringstream ss;
try {
read_xml(file_in, pt);
}
catch (exception &e) {
cout << "Cannot open file " + file_in << endl;
exit(EXIT_FAILURE);
}
StructProp.nat = boost::lexical_cast<unsigned int>(
get_value_from_xml(pt,
"Data.Structure.NumberOfAtoms"));
StructProp.nspecies = boost::lexical_cast<unsigned int>(
get_value_from_xml(pt,
"Data.Structure.NumberOfElements"));
StructProp.ntran = boost::lexical_cast<unsigned int>(
get_value_from_xml(pt,
"Data.Symmetry.NumberOfTranslations"));
for (auto i = 0; i < 3; ++i) {
ss.str("");
ss.clear();
ss << get_value_from_xml(pt,
"Data.Structure.LatticeVector.a"
+ boost::lexical_cast<string>(i + 1));
ss >> StructProp.lattice_vector[0][i]
>> StructProp.lattice_vector[1][i]
>> StructProp.lattice_vector[2][i];
}
ss.str("");
ss.clear();
ss << get_value_from_xml(pt, "Data.Structure.Periodicity");
ss >> StructProp.is_periodic[0]
>> StructProp.is_periodic[1]
>> StructProp.is_periodic[2];
// Parse atomic elements and coordinates
StructProp.kd_symbol.resize(StructProp.nspecies);
StructProp.atoms.resize(StructProp.nat);
int i = 0;
BOOST_FOREACH(
const ptree::value_type &child_, pt.get_child("Data.Structure.AtomicElements")) {
const ptree &child = child_.second;
const unsigned int icount_kd = child.get<unsigned int>("<xmlattr>.number");
dict_atomic_kind[boost::lexical_cast<string>(child_.second.data())] = icount_kd - 1;
StructProp.kd_symbol[i++] = boost::lexical_cast<string>(child_.second.data());
}
unsigned int index;
BOOST_FOREACH(
const ptree::value_type &child_, pt.get_child("Data.Structure.Position")) {
const ptree &child = child_.second;
const string str_index = child.get<string>("<xmlattr>.index");
const string str_element = child.get<string>("<xmlattr>.element");
ss.str("");
ss.clear();
ss << child.data();
index = boost::lexical_cast<unsigned int>(str_index) - 1;
if (index >= StructProp.nat) {
cout << "index is out of range" << endl;
exit(EXIT_FAILURE);
}
StructProp.atoms[index].kind = dict_atomic_kind[str_element];
ss >> StructProp.atoms[index].x
>> StructProp.atoms[index].y
>> StructProp.atoms[index].z;
}
dict_atomic_kind.clear();
// Parse mapping information
StructProp.natmin = StructProp.nat / StructProp.ntran;
unsigned int tran, atom_p, atom_s;
BOOST_FOREACH(
const ptree::value_type &child_, pt.get_child("Data.Symmetry.Translations")) {
const ptree &child = child_.second;
const string str_tran = child.get<string>("<xmlattr>.tran");
const string str_atom = child.get<string>("<xmlattr>.atom");
tran = boost::lexical_cast<unsigned int>(str_tran) - 1;
atom_p = boost::lexical_cast<unsigned int>(str_atom) - 1;
atom_s = boost::lexical_cast<unsigned int>(child.data()) - 1;
if (tran >= StructProp.ntran || atom_p >= StructProp.natmin || atom_s >= StructProp.nat) {
cout << "index is out of range" << endl;
exit(EXIT_FAILURE);
}
StructProp.atoms[atom_s].atom = atom_p;
StructProp.atoms[atom_s].tran = tran;
}
// Parse force constants
std::vector<AtomCellSuper> ivec_with_cell;
std::string str_tag;
double fcs_val;
unsigned int atmn, xyz, cell_s;
std::string str_pairs;
std::string str_attr;
AtomCellSuper ivec_tmp;
for (auto order = 0; order < maxorder; ++order) {
if (order == 0) {
str_tag = "Data.ForceConstants.HARMONIC";
} else {
str_tag = "Data.ForceConstants.ANHARM" + std::to_string(order + 2);
}
boost::optional<ptree &> child_ = pt.get_child_optional(str_tag);
if (!child_) {
std::string str_tmp = str_tag + " flag not found in the XML file";
exit(EXIT_FAILURE);
}
BOOST_FOREACH(
const ptree::value_type &child_, pt.get_child(str_tag)) {
const ptree &child = child_.second;
fcs_val = boost::lexical_cast<double>(child.data());
ivec_with_cell.clear();
for (i = 0; i < order + 2; ++i) {
str_attr = "<xmlattr>.pair" + std::to_string(i + 1);
str_pairs = child.get<std::string>(str_attr);
ss.str("");
ss.clear();
ss << str_pairs;
if (i == 0) {
ss >> atmn >> xyz;
ivec_tmp.index = 3 * (atmn - 1) + xyz - 1;
ivec_tmp.cell_s = 0;
ivec_tmp.tran = 0; // dummy
ivec_with_cell.push_back(ivec_tmp);
} else {
ss >> atmn >> xyz >> cell_s;
ivec_tmp.index = 3 * (atmn - 1) + xyz - 1;
ivec_tmp.cell_s = cell_s - 1;
ivec_tmp.tran = 0; // dummy
ivec_with_cell.push_back(ivec_tmp);
}
}
force_constant_with_cell[order].emplace_back(fcs_val, ivec_with_cell);
}
}
}
void write_fcs_to_file(const std::string fname_fc,
const int order,
const StructureProperty &structure,
double ***x_image,
int **map_p2s,
const std::vector<FcsArrayWithCell> &fc_in)
{
std::ofstream ofs;
ofs.open(fname_fc, std::ios::out);
const auto nelems = order + 2;
ofs << '#';
for (auto i = 0; i < nelems; ++i) {
ofs << std::setw(6) << "atom" + std::to_string(i + 1);
ofs << std::setw(5) << "xyz" + std::to_string(i + 1);
}
ofs << std::setw(20) << "IFC" + std::to_string(nelems) + " (Ry/bohr^" + std::to_string(nelems) + ")";
ofs << std::setw(20) << "distances (bohr)";
ofs << '\n';
for (const auto &it: fc_in) {
ofs << std::setw(7) << map_p2s[it.pairs[0].index / 3][0] + 1;
ofs << std::setw(5) << it.pairs[0].index % 3 + 1;
for (auto i = 1; i < nelems; ++i) {
ofs << std::setw(6) << it.pairs[i].index / 3 + 1;
ofs << std::setw(5) << it.pairs[i].index % 3 + 1;
}
ofs << std::setw(20) << it.fcs_val;
double distance;
double xdiff[3];
for (auto i = 1; i < nelems; ++i) {
for (auto j = 0; j < 3; ++j) {
xdiff[j] = x_image[it.pairs[i].cell_s][it.pairs[i].index / 3][j]
- x_image[0][map_p2s[it.pairs[0].index / 3][0]][j];
}
distance = std::sqrt(xdiff[0] * xdiff[0] + xdiff[1] * xdiff[1] + xdiff[2] * xdiff[2]);
ofs << std::setw(10) << distance;
}
ofs << '\n';
}
ofs.close();
}
void frac2cart(double **xf,
const int nat,
const double lattice_vector[3][3])
{
// x_cartesian = A x_fractional
double *x_tmp;
allocate(x_tmp, 3);
for (size_t i = 0; i < nat; ++i) {
rotvec(x_tmp, xf[i], lattice_vector);
for (auto j = 0; j < 3; ++j) {
xf[i][j] = x_tmp[j];
}
}
deallocate(x_tmp);
}