browser.cpp 11.6 KB
Newer Older
1
2
3
4
5
/**
 * space group browser
 * @author Tobias Weber <tweber@ill.fr>
 * @date Apr-2018
 * @license GPLv3, see 'LICENSE' file
Tobias WEBER's avatar
Tobias WEBER committed
6
 * @desc The present version was forked on 8-Nov-2018 from my privately developed "magtools" project (https://github.com/t-weber/magtools).
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * ----------------------------------------------------------------------------
 * mag-core (part of the Takin software suite)
 * Copyright (C) 2018-2021  Tobias WEBER (Institut Laue-Langevin (ILL),
 *                          Grenoble, France).
 * "magtools" project
 * Copyright (C) 2017-2018  Tobias WEBER (privately developed).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ----------------------------------------------------------------------------
27
28
29
 */

#include "browser.h"
Tobias WEBER's avatar
Tobias WEBER committed
30
#include "../structfact/loadcif.h"
31
#include <QtWidgets/QMenuBar>
Tobias WEBER's avatar
Tobias WEBER committed
32
#include <sstream>
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


// ----------------------------------------------------------------------------


SgBrowserDlg::SgBrowserDlg(QWidget* pParent, QSettings *pSett)
	: QDialog{pParent}, m_pSettings{pSett}
{
	// ------------------------------------------------------------------------
	// setup UI
	this->setupUi(this);
	auto *pLayout = this->layout();

	// menu
	auto *pMenuBar = new QMenuBar(this);
	auto *pMenuOptions = new QMenu("Options", pMenuBar);

	auto *pShowBNS = new QAction("Show BNS", pMenuOptions);
	pShowBNS->setCheckable(true);
	pShowBNS->setChecked(true);
	pMenuOptions->addAction(pShowBNS);

	pMenuBar->addMenu(pMenuOptions);
	pLayout->setMenuBar(pMenuBar);
	// ------------------------------------------------------------------------


	// ------------------------------------------------------------------------
	// restore settings
	if(m_pSettings)
	{
		if(m_pSettings->contains("sgbrowser/geo"))
			this->restoreGeometry(m_pSettings->value("sgbrowser/geo").toByteArray());
		//if(m_pSettings->contains("sgbrowser/state"))
		//	this->restoreState(m_pSettings->value("sgbrowser/state").toByteArray());
	}
	// ------------------------------------------------------------------------


	// load data
Tobias WEBER's avatar
Tobias WEBER committed
73
74
	SetupMagSpaceGroups();
	SetupNuclSpaceGroups();
75
76
77
78


	// ------------------------------------------------------------------------
	// connections
Tobias WEBER's avatar
Tobias WEBER committed
79
80
	connect(treeMagSG, &QTreeWidget::currentItemChanged, this, &SgBrowserDlg::MagSpaceGroupSelected);
	connect(treeNuclSG, &QTreeWidget::currentItemChanged, this, &SgBrowserDlg::NuclSpaceGroupSelected);
81
	connect(pShowBNS, &QAction::toggled, this, &SgBrowserDlg::SwitchToBNS);
Tobias WEBER's avatar
Tobias WEBER committed
82
83
84
85
86
87
88
89
90
91

	// synchronise symops when switching the sg tab
	connect(tabSGs, &QTabWidget::currentChanged, this, [this](int idx) -> void
	{
		switch(idx)
		{
			case 0: MagSpaceGroupSelected(treeMagSG->currentItem()); break;
			case 1: NuclSpaceGroupSelected(treeNuclSG->currentItem()); break;
		}
	});
92
93
94
95
96
97
	// ------------------------------------------------------------------------
}


// ----------------------------------------------------------------------------
/**
Tobias WEBER's avatar
Tobias WEBER committed
98
 * load magnetic space group list
99
 */
Tobias WEBER's avatar
Tobias WEBER committed
100
void SgBrowserDlg::SetupMagSpaceGroups()
101
{
Tobias WEBER's avatar
Tobias WEBER committed
102
	std::cerr << "Loading magnetic space groups ... ";
Tobias WEBER's avatar
Tobias WEBER committed
103
	m_magsgs.Load("res/data/magsg.info");
104
105
	std::cerr << "Done." << std::endl;

Tobias WEBER's avatar
Tobias WEBER committed
106
107
	const auto *pSgs = m_magsgs.GetSpacegroups();
	if(!pSgs) return;
108

Tobias WEBER's avatar
Tobias WEBER committed
109
	for(const auto& sg : *pSgs)
110
	{
Tobias WEBER's avatar
Tobias WEBER committed
111
112
113
114
115
		int iNrStruct = sg.GetStructNumber();
		int iNrMag = sg.GetMagNumber();

		// find top-level item with given structural sg number
		auto get_topsg = [](QTreeWidget *pTree, int iStructNr) -> QTreeWidgetItem*
116
		{
Tobias WEBER's avatar
Tobias WEBER committed
117
118
119
120
			for(int item=0; item<pTree->topLevelItemCount(); ++item)
			{
				QTreeWidgetItem *pItem = pTree->topLevelItem(item);
				if(!pItem) continue;
121

Tobias WEBER's avatar
Tobias WEBER committed
122
123
124
				if(pItem->data(0, Qt::UserRole) == iStructNr)
					return pItem;
			}
125

Tobias WEBER's avatar
Tobias WEBER committed
126
127
			return nullptr;
		};
128
129


Tobias WEBER's avatar
Tobias WEBER committed
130
131
132
133
134
		// find existing top-level structural space group or insert new one
		auto *pTopItem = get_topsg(treeMagSG, iNrStruct);
		if(!pTopItem)
		{
			QString structname = ("(" + std::to_string(iNrStruct) + ") " + sg.GetName()).c_str();
135

Tobias WEBER's avatar
Tobias WEBER committed
136
137
138
139
140
141
			pTopItem = new QTreeWidgetItem();
			pTopItem->setText(0, structname);
			pTopItem->setData(0, Qt::UserRole, iNrStruct);
			pTopItem->setData(0, Qt::UserRole+1, iNrMag);
			treeMagSG->addTopLevelItem(pTopItem);
		}
142
143


Tobias WEBER's avatar
Tobias WEBER committed
144
145
		// create magnetic group and add it as sub-item for the corresponding structural group
		QString magname = ("(" + sg.GetNumber() + ") " + sg.GetName()).c_str();
146

Tobias WEBER's avatar
Tobias WEBER committed
147
148
149
150
151
152
		auto *pSubItem = new QTreeWidgetItem();
		pSubItem->setText(0, magname);
		pSubItem->setData(0, Qt::UserRole, iNrStruct);
		pSubItem->setData(0, Qt::UserRole+1, iNrMag);
		pTopItem->addChild(pSubItem);
	}
153
154
155
156
157
158
159
160
161
}


/**
 * switched to show BNS (otherwise OG)
 */
void SgBrowserDlg::SwitchToBNS(bool bBNS)
{
	m_showBNS = bBNS;
Tobias WEBER's avatar
Tobias WEBER committed
162
	MagSpaceGroupSelected(treeMagSG->currentItem());
163
164
165
166
}


/**
Tobias WEBER's avatar
Tobias WEBER committed
167
 * magnetic space group selected
168
 */
Tobias WEBER's avatar
Tobias WEBER committed
169
void SgBrowserDlg::MagSpaceGroupSelected(QTreeWidgetItem *pItem)
170
171
172
173
{
	if(!pItem) return;

	// clean up
Tobias WEBER's avatar
Tobias WEBER committed
174
175
176
	listSymOps->clear();
	listWyc->clear();

177
178
179
180

	int iNrStruct = pItem->data(0, Qt::UserRole).toInt();
	int iNrMag = pItem->data(0, Qt::UserRole+1).toInt();

Tobias WEBER's avatar
Tobias WEBER committed
181
	const auto* pSg = m_magsgs.GetSpacegroupByNumber(iNrStruct, iNrMag);
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
	if(!pSg) return;


	// print a symmetry operator
	auto print_sym = [](const t_mat_sg& mat, const t_vec_sg& vec, t_real_sg inv) -> std::string
	{
		std::ostringstream ostr;

		for(std::size_t i=0; i<mat.size1(); ++i)
		{
			// rotation matrix
			ostr << "( ";
			for(std::size_t j=0; j<mat.size2(); ++j)
				ostr << std::setw(ostr.precision()*1.5) << std::right << mat(i,j);
			ostr << " )";

			// translation vector
			ostr << std::setw(ostr.precision()*2) << std::right << "( ";
			ostr << std::setw(ostr.precision()*1.5) << std::right << vec[i]; 
			ostr << " )";

			// time inversion
			if(i==mat.size1()/2)
			{
				ostr << std::setw(ostr.precision()*2) << std::right << "t = ";
				ostr << inv;
			}

			if(i < mat.size1()-1)
				ostr << "\n";
		}

		return ostr.str();
	};


	// print a wyckoff position
	auto print_wyc = [](const t_mat_sg& mat, const t_mat_sg& matRot,  const t_vec_sg& vec) -> std::string
	{
		std::ostringstream ostr;

		for(std::size_t i=0; i<mat.size1(); ++i)
		{
			// rotation matrices
			ostr << "( ";
			for(std::size_t j=0; j<mat.size2(); ++j)
				ostr << std::setw(ostr.precision()*1.5) << std::right << mat(i,j);

			ostr  << " | ";
			for(std::size_t j=0; j<matRot.size2(); ++j)
				ostr << std::setw(ostr.precision()*1.5) << std::right << matRot(i,j);
			ostr << " )";

			// translation vector
			ostr << std::setw(ostr.precision()*2) << std::right << "( ";
			ostr << std::setw(ostr.precision()*1.5) << std::right << vec[i]; 
			ostr << " )";

			if(i < mat.size1()-1)
				ostr << "\n";
		}

		return ostr.str();
	};


	// iterate over symmetries
	const auto *symms = pSg->GetSymmetries(m_showBNS);
	if(symms)
	{
		for(std::size_t iOp=0; iOp<symms->GetRotations().size(); ++iOp)
		{
			const auto& rot = symms->GetRotations()[iOp];
			const auto& trans = symms->GetTranslations()[iOp];
			const auto inv = symms->GetInversions()[iOp];

			auto *pOpItem = new QListWidgetItem();
			pOpItem->setText(print_sym(rot, trans, inv).c_str());
Tobias WEBER's avatar
Tobias WEBER committed
260
			listSymOps->addItem(pOpItem);
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
		}
	}


	// iterate over wyckoff positions
	const auto *wycs = pSg->GetWycPositions(m_showBNS);
	if(wycs)
	{
		for(const auto &wyc : *wycs)
		{
			auto *pWycItem = new QTreeWidgetItem();
			pWycItem->setText(0, wyc.GetName().c_str());

			// iterate over trafos
			for(std::size_t iOp=0; iOp<wyc.GetRotations().size(); ++iOp)
			{
				const auto& rot = wyc.GetRotations()[iOp];
				const auto& rotMag = wyc.GetRotationsMag()[iOp];
				const auto& trans = wyc.GetTranslations()[iOp];

				auto *pSubItem = new QTreeWidgetItem();
				pSubItem->setText(0, print_wyc(rot, rotMag, trans).c_str());
				pWycItem->addChild(pSubItem);
			}

Tobias WEBER's avatar
Tobias WEBER committed
286
			listWyc->addTopLevelItem(pWycItem);
287
288
289
290
			pWycItem->setExpanded(true);
		}
	}
}
Tobias WEBER's avatar
Tobias WEBER committed
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
// ----------------------------------------------------------------------------




// ----------------------------------------------------------------------------
/**
 * load nuclear space group list
 */
void SgBrowserDlg::SetupNuclSpaceGroups()
{
	m_nuclsgs = get_sgs<t_mat44_sg>(false, false);

	for(int iSG=0; iSG<static_cast<int>(m_nuclsgs.size()); ++iSG)
	{
		int iNrStruct = std::get<0>(m_nuclsgs[iSG]);
		const std::string& strName = std::get<1>(m_nuclsgs[iSG]);

		// find top-level item with given structural sg number
		auto get_topsg = [](QTreeWidget *pTree, int iStructNr) -> QTreeWidgetItem*
		{
			for(int item=0; item<pTree->topLevelItemCount(); ++item)
			{
				QTreeWidgetItem *pItem = pTree->topLevelItem(item);
				if(!pItem) continue;

				if(pItem->data(0, Qt::UserRole) == iStructNr)
					return pItem;
			}

			return nullptr;
		};


		// find existing top-level structural space group or insert new one
		auto *pTopItem = get_topsg(treeNuclSG, iNrStruct);
		if(!pTopItem)
		{
			QString structname = ("(" + std::to_string(iNrStruct) + ") " + strName).c_str();

			pTopItem = new QTreeWidgetItem();
			pTopItem->setText(0, structname);
			pTopItem->setData(0, Qt::UserRole, iNrStruct);
			pTopItem->setData(0, Qt::UserRole+1, iSG);
			treeNuclSG->addTopLevelItem(pTopItem);
		}


		// create nuclear group config and add it as sub-item for the corresponding structural group
		QString magname = ("(" + std::to_string(iNrStruct) + 
			"-" + std::to_string(pTopItem->childCount()+1) + ") " + strName).c_str();

		auto *pSubItem = new QTreeWidgetItem();
		pSubItem->setText(0, magname);
		pSubItem->setData(0, Qt::UserRole, iNrStruct);
		pSubItem->setData(0, Qt::UserRole+1, iSG);
		pTopItem->addChild(pSubItem);
	}
}


/**
 * nuclear space group selected
 */
void SgBrowserDlg::NuclSpaceGroupSelected(QTreeWidgetItem *pItem)
{
	if(!pItem) return;

	// clean up
	listSymOps->clear();
	listWyc->clear();


	int iNrStruct = pItem->data(0, Qt::UserRole).toInt();
	int iNrSG = pItem->data(0, Qt::UserRole+1).toInt();
	if(iNrSG < 0 || iNrSG >= m_nuclsgs.size())
		return;

	const auto& sg = m_nuclsgs[iNrSG];
	const auto& trafos = std::get<2>(sg);


	// print a symmetry operator
	auto print_sym = [](const t_mat44_sg& mat) -> std::string
	{
		std::ostringstream ostr;

		for(std::size_t i=0; i<3; ++i)
		{
			// rotation matrix
			ostr << "( ";
			for(std::size_t j=0; j<3; ++j)
				ostr << std::setw(ostr.precision()*1.5) << std::right << mat(i,j);
			ostr << " )";

			// translation vector
			ostr << std::setw(ostr.precision()*2) << std::right << "( ";
			ostr << std::setw(ostr.precision()*1.5) << std::right << mat(i, 3); 
			ostr << " )";

			if(i < mat.size1()-2)
				ostr << "\n";
		}

		return ostr.str();
	};


	// iterate over symmetries
	for(std::size_t iOp=0; iOp<trafos.size(); ++iOp)
	{
		const auto& trafo = trafos[iOp];

		auto *pOpItem = new QListWidgetItem();
		pOpItem->setText(print_sym(trafo).c_str());
		listSymOps->addItem(pOpItem);
	}
}
// ----------------------------------------------------------------------------


412
413


Tobias WEBER's avatar
Tobias WEBER committed
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
void SgBrowserDlg::showEvent(QShowEvent *pEvt)
{
	QDialog::showEvent(pEvt);
}


void SgBrowserDlg::hideEvent(QHideEvent *pEvt)
{
	QDialog::hideEvent(pEvt);
}


void SgBrowserDlg::closeEvent(QCloseEvent *pEvt)
{
	if(m_pSettings)
	{
		m_pSettings->setValue("sgbrowser/geo", this->saveGeometry());
		//m_pSettings->setValue("sgbrowser/state", this->saveState());
	}

	QDialog::closeEvent(pEvt);
}

// ----------------------------------------------------------------------------