convmag.cpp 11.5 KB
Newer Older
1
2
3
4
5
6
/**
 * converts magnetic space group table
 * @author Tobias Weber <tweber@ill.fr>
 * @date 18-nov-17
 * @license GPLv3, see 'LICENSE' file
 * @desc The present version was forked on 8-Nov-2018 from the privately developed "magtools" project (https://github.com/t-weber/magtools).
Tobias WEBER's avatar
Tobias WEBER committed
7
8
 *
 * g++-8 -std=c++17 -fconcepts -I../../ -o convmag convmag.cpp
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 */

#include <iostream>
#include <fstream>
#include <tuple>

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/vector.hpp>
namespace ublas = boost::numeric::ublas;

#include <boost/property_tree/ptree.hpp>
//#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/info_parser.hpp>
namespace ptree = boost::property_tree;

#include <boost/algorithm/string/trim.hpp>
namespace algo = boost::algorithm;

Tobias WEBER's avatar
Tobias WEBER committed
27
#include "libs/math_algos.h"
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


using t_real = double;
using t_mat = ublas::matrix<t_real>;
using t_vec = ublas::vector<t_real>;


bool bSaveOG = false;

std::string to_str(const t_mat& mat)
{
	// special cases
	static const auto zero = m::zero<t_mat>(mat.size1(), mat.size2());
	static const auto unit = m::unit<t_mat>(mat.size1(), mat.size2());

	if(m::equals<t_mat>(mat, zero))
		return "0";
	else if(m::equals<t_mat>(mat, unit))
		return "1";

	// general case
	std::ostringstream ostr;

	for(std::size_t i=0; i<mat.size1(); ++i)
	{
		for(std::size_t j=0; j<mat.size2(); ++j)
		{
			ostr << mat(i,j);
			if(j < mat.size2()-1)
				ostr << " ";
		}

		if(i < mat.size2()-1)
			ostr << "\t";
	}

	return ostr.str();
}

std::string to_str(const t_vec& vec)
{
	// spacial cases
	static const auto zero = m::zero<t_vec>(vec.size());
	static const auto x = m::create<t_vec>({1,0,0});
	static const auto y = m::create<t_vec>({0,1,0});
	static const auto z = m::create<t_vec>({0,0,1});
	static const auto mx = m::create<t_vec>({-1,0,0});
	static const auto my = m::create<t_vec>({0,-1,0});
	static const auto mz = m::create<t_vec>({0,0,-1});

	if(m::equals<t_vec>(vec, zero))
		return "0";
	else if(m::equals<t_vec>(vec, x))
		return "x";
	else if(m::equals<t_vec>(vec, y))
		return "y";
	else if(m::equals<t_vec>(vec, z))
		return "z";
	else if(m::equals<t_vec>(vec, mx))
		return "-x";
	else if(m::equals<t_vec>(vec, my))
		return "-y";
	else if(m::equals<t_vec>(vec, mz))
		return "-z";

	// general case
	std::ostringstream ostr;

	for(std::size_t i=0; i<vec.size(); ++i)
	{
		ostr << vec[i];
		if(i < vec.size()-1)
			ostr << " ";
	}

	return ostr.str();
}


std::ostream& operator<<(std::ostream& ostr, const t_mat& mat)
{
	ostr << to_str(mat);
	return ostr;
}


std::ostream& operator<<(std::ostream& ostr, const t_vec& vec)
{
	ostr << to_str(vec);
	return ostr;
}


template<class T>
T get_num(std::istream& istr)
{
	T t;
	istr >> t;
	return t;
}


std::string get_string(std::istream& istr)
{
	// skip whitespace
	while(1)
	{
		char c = istr.get();
		if(c!=' ' && c!='\t')
		{
			istr.unget();
			break;
		}
	}

	// string start
	char c = istr.get();
	if(c != '\"')
	{
		istr.unget();
		std::cerr << "Expected a string." << std::endl;
	}

	// read string
	std::string str;
	while(1)
	{
		c = istr.get();
		// end of string?
		if(c == '\"')
			break;

		str += c;
	}

	algo::trim(str);
	return str;
}


t_mat get_matrix(std::size_t N, std::size_t M, std::istream& istr)
{
	t_mat mat(N,M);

	for(std::size_t i=0; i<N; ++i)
		for(std::size_t j=0; j<M; ++j)
			istr >> mat(i,j);

	return mat;
}


t_vec get_vector(std::size_t N, std::istream& istr)
{
	t_vec vec(N);

	for(std::size_t i=0; i<N; ++i)
		istr >> vec(i);

	return vec;
}


std::tuple<std::string, t_mat> get_pointgroup_op(std::istream& istr)
{
	int iNum = get_num<int>(istr);

	std::string strName = get_string(istr);
	std::string strOpXYZ = get_string(istr);
	t_mat matOp = get_matrix(3,3, istr);

	//std::cout << strName << ": " << matOp << "\n";
	return { strName, matOp };
}


void convert_spacegroup(std::istream& istr, ptree::ptree& prop, const std::string& strPath,
	const std::vector<std::tuple<std::string, t_mat>>* pPtOps = nullptr,
	const std::vector<std::tuple<std::string, t_mat>>* pHexPtOps = nullptr)
{
	int iNrBNS[2];
	istr >> iNrBNS[0] >> iNrBNS[1];
	std::string strNrBNS = get_string(istr);
	std::string strSGBNS = get_string(istr);

	int iNrOG[3];
	istr >> iNrOG[0] >> iNrOG[1] >> iNrOG[2];
	std::string strNrOG = get_string(istr);
	std::string strSGOG = get_string(istr);

	prop.put(strPath + "bns.id", strSGBNS);
	prop.put(strPath + "og.id", strSGOG);

	std::ostringstream ostrNrBNS, ostrNrOG;
	ostrNrBNS << iNrBNS[0] << "." << iNrBNS[1];
	ostrNrOG << iNrOG[0] << "." << iNrOG[1] << "." << iNrOG[2];

	prop.put(strPath + "bns.nr", ostrNrBNS.str());
	prop.put(strPath + "og.nr", ostrNrOG.str());

	// consistency check
	if(ostrNrBNS.str()!=strNrBNS || ostrNrOG.str()!=strNrOG)
		std::cerr << "Mismatch of space group number in " << strSGBNS << "." << std::endl;

	bool bIsHex = (iNrBNS[0]>=143 && iNrBNS[0]<=194);


	int iTy = get_num<int>(istr);
	if(iTy == 4)	// BNS -> OG trafo
	{
		t_mat matBNS2OG = get_matrix(3,3, istr);
		t_vec vecBNS2OG = get_vector(3, istr);
		t_real numBNS2OG = get_num<t_real>(istr);

		prop.put(strPath + "bns2og.R", to_str(matBNS2OG));
		prop.put(strPath + "bns2og.v", to_str(vecBNS2OG));
		prop.put(strPath + "bns2og.d", numBNS2OG);
	}


	//std::size_t iMaxOperBNS = 0;
	std::size_t iNumOpsBNS = get_num<std::size_t>(istr);
	for(std::size_t iPtOp=0; iPtOp<iNumOpsBNS; ++iPtOp)
	{
		std::size_t iOperBNS = get_num<std::size_t>(istr);
		//iMaxOperBNS = std::max(iOperBNS, iMaxOperBNS);
		t_vec vecBNS = get_vector(3, istr);
		t_real numBNS = get_num<t_real>(istr);
		int itInvBNS = get_num<int>(istr);

		if(pPtOps && !bIsHex)
			prop.put(strPath + "bns.ops.R" + std::to_string(iPtOp+1), to_str(std::get<1>(pPtOps->at(iOperBNS-1))));
		else if(pHexPtOps && bIsHex)
			prop.put(strPath + "bns.ops.R" + std::to_string(iPtOp+1), to_str(std::get<1>(pHexPtOps->at(iOperBNS-1))));
		else
		{
			std::cerr << "Invalid point group index for " << strSGBNS << "." << std::endl;
			prop.put(strPath + "bns.ops.R" + std::to_string(iPtOp+1), iOperBNS);
		}
		prop.put(strPath + "bns.ops.v" + std::to_string(iPtOp+1), to_str(vecBNS));
		prop.put(strPath + "bns.ops.d" + std::to_string(iPtOp+1), numBNS);
		prop.put(strPath + "bns.ops.t" + std::to_string(iPtOp+1), itInvBNS);
	}
	//std::cout << "\nmax op: " << iMaxOperBNS << std::endl;

	std::size_t iNumLattVecsBNS = get_num<std::size_t>(istr);
	for(std::size_t iVec=0; iVec<iNumLattVecsBNS; ++iVec)
	{
		t_vec vecBNS = get_vector(3, istr);
		t_real numBNS = get_num<t_real>(istr);

		prop.put(strPath + "bns.lat.v" + std::to_string(iVec+1), to_str(vecBNS));
		prop.put(strPath + "bns.lat.d" + std::to_string(iVec+1), numBNS);
	}

	std::size_t iNumWycBNS = get_num<std::size_t>(istr);
	for(std::size_t iWyc=0; iWyc<iNumWycBNS; ++iWyc)
	{
		std::size_t iNumPos = get_num<std::size_t>(istr);
		std::size_t iMult = get_num<std::size_t>(istr);
		std::string strWycName = get_string(istr);

		prop.put(strPath + "bns.wyc.s" + std::to_string(iWyc+1) + ".l", strWycName);
		prop.put(strPath + "bns.wyc.s" + std::to_string(iWyc+1) + ".m", iMult);

		for(std::size_t iPos=0; iPos<iNumPos; ++iPos)
		{
			t_vec vecWyc = get_vector(3, istr);
			t_real numWyc = get_num<t_real>(istr);
			t_mat matWycXYZ = get_matrix(3, 3, istr);
			t_mat matWycMXMYMZ = get_matrix(3, 3, istr);

			prop.put(strPath + "bns.wyc.s" + std::to_string(iWyc+1)
				+ ".v" + std::to_string(iPos+1), to_str(vecWyc));
			prop.put(strPath + "bns.wyc.s" + std::to_string(iWyc+1)
				+ ".d" + std::to_string(iPos+1), numWyc);
			prop.put(strPath + "bns.wyc.s" + std::to_string(iWyc+1)
				+ ".R" + std::to_string(iPos+1), to_str(matWycXYZ));
			prop.put(strPath + "bns.wyc.s" + std::to_string(iWyc+1)
				+ ".M" + std::to_string(iPos+1), to_str(matWycMXMYMZ));
		}
	}


	if(iTy == 4)	// OG
	{
		std::size_t iNumPtOpsOG = get_num<std::size_t>(istr);
		for(std::size_t iPtOp=0; iPtOp<iNumPtOpsOG; ++iPtOp)
		{
			std::size_t iOperOG = get_num<std::size_t>(istr);
			t_vec vecOG = get_vector(3, istr);
			t_real numOG = get_num<t_real>(istr);
			int itInvOG = get_num<int>(istr);

			if(bSaveOG)
			{
				if(pPtOps && !bIsHex)
					prop.put(strPath + "og.ops.R" + std::to_string(iPtOp+1), to_str(std::get<1>(pPtOps->at(iOperOG-1))));
				else if(pHexPtOps && bIsHex)
					prop.put(strPath + "og.ops.R" + std::to_string(iPtOp+1), to_str(std::get<1>(pHexPtOps->at(iOperOG-1))));
				else
					prop.put(strPath + "og.ops.R" + std::to_string(iPtOp+1), iOperOG);
				prop.put(strPath + "og.ops.v" + std::to_string(iPtOp+1), to_str(vecOG));
				prop.put(strPath + "og.ops.d" + std::to_string(iPtOp+1), numOG);
				prop.put(strPath + "og.ops.t" + std::to_string(iPtOp+1), itInvOG);
			}
		}

		std::size_t iNumLattVecsOG = get_num<std::size_t>(istr);
		for(std::size_t iVec=0; iVec<iNumLattVecsOG; ++iVec)
		{
			t_vec vecOG = get_vector(3, istr);
			t_real numOG = get_num<t_real>(istr);

			if(bSaveOG)
			{
				prop.put(strPath + "og.lat.v" + std::to_string(iVec+1), to_str(vecOG));
				prop.put(strPath + "og.lat.d" + std::to_string(iVec+1), numOG);
			}
		}

		std::size_t iNumWycOG = get_num<std::size_t>(istr);
		for(std::size_t iWyc=0; iWyc<iNumWycOG; ++iWyc)
		{
			std::size_t iNumPos = get_num<std::size_t>(istr);
			std::size_t iMult = get_num<std::size_t>(istr);
			std::string strWycName = get_string(istr);

			if(bSaveOG)
			{
				prop.put(strPath + "og.wyc.s" + std::to_string(iWyc+1) + ".l", strWycName);
				prop.put(strPath + "og.wyc.s" + std::to_string(iWyc+1) + ".m", iMult);
			}

			for(std::size_t iPos=0; iPos<iNumPos; ++iPos)
			{
				t_vec vecWyc = get_vector(3, istr);
				t_real numWyc = get_num<t_real>(istr);
				t_mat matWycXYZ = get_matrix(3, 3, istr);
				t_mat matWycMXMYMZ = get_matrix(3, 3, istr);

				if(bSaveOG)
				{
					prop.put(strPath + "og.wyc.s" + std::to_string(iWyc+1)
						+ ".v" + std::to_string(iPos+1) , to_str(vecWyc));
					prop.put(strPath + "og.wyc.s" + std::to_string(iWyc+1)
						+ ".d" + std::to_string(iPos+1) , numWyc);
					prop.put(strPath + "og.wyc.s" + std::to_string(iWyc+1)
						+ ".R" + std::to_string(iPos+1) , to_str(matWycXYZ));
					prop.put(strPath + "og.wyc.s" + std::to_string(iWyc+1)
						+ ".M" + std::to_string(iPos+1), to_str(matWycMXMYMZ));
				}
			}
		}
	}

	//std::cout << strSGBNS << ", " << strSGOG << "\n";
	//std::cout << "."; std::cout.flush();
}


Tobias WEBER's avatar
Tobias WEBER committed
389
void convert_table(const char* pcInFile, const char* pcOutFile)
390
{
Tobias WEBER's avatar
Tobias WEBER committed
391
	std::ifstream istr(pcInFile);
392
393
	if(!istr)
	{
Tobias WEBER's avatar
Tobias WEBER committed
394
		std::cerr << "Cannot open \"" << pcInFile << "\"." << std::endl;
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
		return;
	}

	std::vector<std::tuple<std::string, t_mat>> vecPtOps, vecHexPtOps;

	// read 48 point group operators
	std::cout << "Reading point group operators...\n";
	for(std::size_t i=0; i<48; ++i)
		vecPtOps.emplace_back(get_pointgroup_op(istr));

	// read 24 hexagonal point group operators
	std::cout << "Reading hexagonal point group operators...\n";
	for(std::size_t i=0; i<24; ++i)
		vecHexPtOps.emplace_back(get_pointgroup_op(istr));


	ptree::ptree prop;

	// convert the 1651 3D space groups
	std::cout << "Converting space groups...\n";
	for(std::size_t i=0; i<1651; ++i)
	{
		std::cout << "\rGroup " << (i+1) << " / 1651...     ";
		std::cout.flush();

		std::string strPath = "mag_groups.sg" + std::to_string(i+1) + ".";
		convert_spacegroup(istr, prop, strPath, &vecPtOps, &vecHexPtOps);
	}
	std::cout << "\n";


Tobias WEBER's avatar
Tobias WEBER committed
426
	/*ptree::write_xml(pcOutFile, prop,
427
428
		std::locale(),
		ptree::xml_writer_make_settings('\t', 1, std::string("utf-8")));*/
Tobias WEBER's avatar
Tobias WEBER committed
429
	ptree::write_info(pcOutFile, prop,
430
431
432
433
434
		std::locale(),
		ptree::info_writer_make_settings('\t', 1));
}


Tobias WEBER's avatar
Tobias WEBER committed
435
int main(int argc, char **argv)
436
{
Tobias WEBER's avatar
Tobias WEBER committed
437
438
439
440
441
442
443
	if(argc < 3)
	{
		std::cerr << "Usage: " << argv[0] << " <in.dat> <out.info>" << std::endl;
		return -1;
	}

	convert_table(argv[1], argv[2]);
444
445
	return 0;
}