Debian Codegraph @

avogadro/qtplugins/symmetry/symmetry.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "symmetry.h"
7
8 #include "symmetrywidget.h"
9
10 #include "symmetryutil.h"
11
12 // #include <avogadro/core/unitcell.h>
13 // #include <avogadro/core/crystaltools.h>
14
15 #include <avogadro/qtgui/molecule.h>
16
17 #include <QAction>
18 #include <QtWidgets/QMessageBox>
19
20 #include <QtCore/QDebug>
21 #include <QtCore/QStringList>
22
23 // using Avogadro::Core::CrystalTools;
24 // using Avogadro::Core::UnitCell;
25 using Avogadro::QtGui::Molecule;
26
27 using namespace msym;
28 using namespace Avogadro::QtPlugins::SymmetryUtil;
29
30 namespace Avogadro::QtPlugins {
31
32 Symmetry::Symmetry(QObject* parent_)
33 : Avogadro::QtGui::ExtensionPlugin(parent_), m_molecule(nullptr),
34 m_symmetryWidget(nullptr), m_viewSymmetryAction(new QAction(this))
35 {
36
37 m_ctx = msymCreateContext();
38
39 m_viewSymmetryAction->setText(tr("Symmetry…"));
40 m_viewSymmetryAction->setProperty("menu priority", -50);
41 connect(m_viewSymmetryAction, SIGNAL(triggered()), SLOT(viewSymmetry()));
42 m_actions.push_back(m_viewSymmetryAction);
43
44 /*
45 connect(m_symmetryWidget, SIGNAL(clicked()), this, SLOT(detectSymmetry()));
46 connect(m_ui->symmetrizeButton, SIGNAL(clicked()), this, SLOT(symmetrize()));
47 connect(m_ui->toleranceCombo, SIGNAL(currentIndexChanged(int)), this,
48 SLOT(toleranceChanged(int)));*/
49
50 /*
51 m_standardOrientationAction->setText(tr("Rotate to Standard &Orientation"));
52 connect(m_standardOrientationAction, SIGNAL(triggered()),
53 SLOT(standardOrientation()));
54 m_actions.push_back(m_standardOrientationAction);
55 m_standardOrientationAction->setProperty("menu priority", -250);*/
56
57 updateActions();
58 }
59
60 Symmetry::~Symmetry()
61 {
62 if (m_symmetryWidget)
63 m_symmetryWidget->deleteLater();
64
65 qDeleteAll(m_actions);
66 m_actions.clear();
67
68 if (m_ctx != nullptr) {
69 msymReleaseContext(m_ctx);
70 }
71 }
72
73 QList<QAction*> Symmetry::actions() const
74 {
75 return m_actions;
76 }
77
78 QStringList Symmetry::menuPath(QAction*) const
79 {
80 return QStringList() << tr("&Analyze") << tr("&Properties");
81 }
82
83 void Symmetry::setMolecule(QtGui::Molecule* mol)
84 {
85 if (m_molecule == mol)
86 return;
87
88 if (m_molecule)
89 m_molecule->disconnect(this);
90
91 m_molecule = mol;
92 if (m_symmetryWidget)
93 m_symmetryWidget->setMolecule(m_molecule);
94
95 if (m_molecule)
96 connect(m_molecule, SIGNAL(changed(uint)), SLOT(moleculeChanged(uint)));
97
98 updateActions();
99 m_dirty = true;
100 }
101
102 void Symmetry::moleculeChanged(unsigned int c)
103 {
104 Q_ASSERT(m_molecule == qobject_cast<Molecule*>(sender()));
105
106 auto changes = static_cast<Molecule::MoleculeChanges>(c);
107
108 if (changes & Molecule::Added || changes & Molecule::Removed)
109 updateActions();
110 /*
111 if (changes & Molecule::UnitCell) {
112 if (changes & Molecule::Added || changes & Molecule::Removed)
113 updateActions();
114 }*/
115
116 if ((changes & Molecule::Atoms) &&
117 (changes & Molecule::Modified || changes & Molecule::Added ||
118 changes & Molecule::Removed)) {
119 m_dirty = true;
120 }
121 }
122
123 void Symmetry::updateActions()
124 {
125 // Disable everything for NULL molecules.
126 if (!m_molecule) {
127 foreach (QAction* action, m_actions)
128 action->setEnabled(false);
129 return;
130 } else {
131 foreach (QAction* action, m_actions)
132 action->setEnabled(true);
133 }
134 /*
135 if (m_molecule->unitCell()) {
136 foreach (QAction *action, m_actions)
137 action->setEnabled(true);
138
139 m_toggleUnitCellAction->setText(tr("Remove &Unit Cell"));
140 }
141 else {
142 foreach (QAction *action, m_actions)
143 action->setEnabled(false);
144
145 m_toggleUnitCellAction->setEnabled(true);
146 m_toggleUnitCellAction->setText(tr("Add &Unit Cell"));
147 }*/
148 }
149
150 void Symmetry::viewSymmetry()
151 {
152 if (!m_symmetryWidget) {
153 m_symmetryWidget = new SymmetryWidget(qobject_cast<QWidget*>(parent()));
154 m_symmetryWidget->setMolecule(m_molecule);
155 connect(m_symmetryWidget, SIGNAL(detectSymmetry()), SLOT(detectSymmetry()));
156 connect(m_symmetryWidget, SIGNAL(symmetrizeMolecule()),
157 SLOT(symmetrizeMolecule()));
158 }
159
160 if (m_dirty) {
161 detectSymmetry();
162 }
163 m_symmetryWidget->show();
164 m_symmetryWidget->raise();
165 m_symmetryWidget->activateWindow();
166 }
167
168 void Symmetry::detectSymmetry()
169 {
170
171 unsigned int length = m_molecule->atomCount();
172
173 if (m_molecule == nullptr || m_molecule->atomPositions3d().size() != length ||
174 length < 2)
175 return; // if one atom = Kh
176
177 if (length == 1) {
178 m_symmetryWidget->setPointGroupSymbol(QString("K<sub>h</sub>"));
179 return;
180 }
181
182 // interface with libmsym
183 msym_error_t ret = MSYM_SUCCESS;
184 msym_element_t* elements = nullptr;
185 char point_group[6];
186 double cm[3], radius = 0.0;
187
188 /* Do not free these variables */
189 const msym_symmetry_operation_t* msops = nullptr;
190 const msym_subgroup_t* msg = nullptr;
191 const msym_equivalence_set_t* mes = nullptr;
192 int mesl = 0, msgl = 0, msopsl = 0;
193
194 // initialize the c-style array of atom names and coordinates
195 msym_element_t* a;
196 a = (msym_element_t*)malloc(length * sizeof(msym_element_t));
197 memset(a, 0, length * sizeof(msym_element_t));
198
199 for (Index i = 0; i < length; ++i) {
200 Vector3 ipos = m_molecule->atomPositions3d()[i];
201 // this is yucky, but msym uses type <void*> for id :-(
202 a[i].id = reinterpret_cast<void*>(i);
203 a[i].n = m_molecule->atomicNumbers()[i];
204 if (a[i].n < 1 || a[i].n > 118)
205 a[i].n = 1; // pretend to be an H atom for libmsym
206 a[i].v[0] = ipos[0];
207 a[i].v[1] = ipos[1];
208 a[i].v[2] = ipos[2];
209 }
210 elements = a;
211
212 if (m_ctx != nullptr) {
213 msymReleaseContext(m_ctx);
214 m_ctx = msymCreateContext();
215 }
216
217 // Set the thresholds
218 // switch (m_dock->toleranceCombo->currentIndex()) {
219 msym_thresholds_t* thresholds = m_symmetryWidget->getThresholds();
220 msymSetThresholds(m_ctx, thresholds);
221
222 // At any point, we'll set the text to NULL which will use C1 instead
223
224 if (MSYM_SUCCESS != (ret = msymSetElements(m_ctx, length, elements))) {
225 free(elements);
226 m_symmetryWidget->setPointGroupSymbol(pointGroupSymbol(nullptr));
227 m_symmetryWidget->setEquivalenceSets(0, nullptr);
228 m_symmetryWidget->setSymmetryOperations(0, nullptr);
229 m_symmetryWidget->setSubgroups(0, nullptr);
230 qDebug() << "Error:" << msymErrorString(ret) << " "
231 << msymGetErrorDetails();
232 return;
233 }
234
235 if (MSYM_SUCCESS != (ret = msymFindSymmetry(m_ctx))) {
236 free(elements);
237 m_symmetryWidget->setPointGroupSymbol(pointGroupSymbol(nullptr));
238 m_symmetryWidget->setEquivalenceSets(0, nullptr);
239 m_symmetryWidget->setSymmetryOperations(0, nullptr);
240 m_symmetryWidget->setSubgroups(0, nullptr);
241 qDebug() << "Error:" << msymErrorString(ret) << " "
242 << msymGetErrorDetails();
243 return;
244 }
245
246 /* Get the point group name */
247 if (MSYM_SUCCESS !=
248 (ret = msymGetPointGroupName(m_ctx, sizeof(char[6]), point_group))) {
249 free(elements);
250 m_symmetryWidget->setPointGroupSymbol(pointGroupSymbol(nullptr));
251 m_symmetryWidget->setEquivalenceSets(0, nullptr);
252 m_symmetryWidget->setSymmetryOperations(0, nullptr);
253 m_symmetryWidget->setSubgroups(0, nullptr);
254 qDebug() << "Error:" << msymErrorString(ret) << " "
255 << msymGetErrorDetails();
256 return;
257 }
258
259 if (MSYM_SUCCESS !=
260 (ret = msymGetSymmetryOperations(m_ctx, &msopsl, &msops))) {
261 free(elements);
262 m_symmetryWidget->setPointGroupSymbol(pointGroupSymbol(nullptr));
263 m_symmetryWidget->setEquivalenceSets(0, nullptr);
264 m_symmetryWidget->setSymmetryOperations(0, nullptr);
265 m_symmetryWidget->setSubgroups(0, nullptr);
266 qDebug() << "Error:" << msymErrorString(ret) << " "
267 << msymGetErrorDetails();
268 return;
269 }
270
271 if (MSYM_SUCCESS != (ret = msymGetEquivalenceSets(m_ctx, &mesl, &mes))) {
272 free(elements);
273 m_symmetryWidget->setPointGroupSymbol(pointGroupSymbol(nullptr));
274 m_symmetryWidget->setEquivalenceSets(0, nullptr);
275 m_symmetryWidget->setSymmetryOperations(0, nullptr);
276 m_symmetryWidget->setSubgroups(0, nullptr);
277 qDebug() << "Error:" << msymErrorString(ret) << " "
278 << msymGetErrorDetails();
279 return;
280 }
281
282 if (MSYM_SUCCESS != (ret = msymGetCenterOfMass(m_ctx, cm))) {
283 free(elements);
284 m_symmetryWidget->setPointGroupSymbol(pointGroupSymbol(nullptr));
285 m_symmetryWidget->setEquivalenceSets(0, nullptr);
286 m_symmetryWidget->setSymmetryOperations(0, nullptr);
287 m_symmetryWidget->setSubgroups(0, nullptr);
288 qDebug() << "Error:" << msymErrorString(ret) << " "
289 << msymGetErrorDetails();
290 return;
291 }
292
293 if (MSYM_SUCCESS != (ret = msymGetRadius(m_ctx, &radius))) {
294 free(elements);
295 m_symmetryWidget->setPointGroupSymbol(pointGroupSymbol(nullptr));
296 m_symmetryWidget->setEquivalenceSets(0, nullptr);
297 m_symmetryWidget->setSymmetryOperations(0, nullptr);
298 m_symmetryWidget->setSubgroups(0, nullptr);
299 qDebug() << "Error:" << msymErrorString(ret) << " "
300 << msymGetErrorDetails();
301 return;
302 }
303
304 if (point_group[1] != '0') {
305 if (MSYM_SUCCESS != (ret = msymGetSubgroups(m_ctx, &msgl, &msg))) {
306 free(elements);
307 m_symmetryWidget->setPointGroupSymbol(pointGroupSymbol(nullptr));
308 m_symmetryWidget->setEquivalenceSets(0, nullptr);
309 m_symmetryWidget->setSymmetryOperations(0, nullptr);
310 m_symmetryWidget->setSubgroups(0, nullptr);
311 qDebug() << "Error:" << msymErrorString(ret) << " "
312 << msymGetErrorDetails();
313 return;
314 }
315 } else {
316 m_symmetryWidget->setSubgroups(0, nullptr);
317 }
318
319 // TODO: Subgroups
320 // if(MSYM_SUCCESS != (ret = msymGetSubgroups(ctx, &msgl, &msg))) goto err;
321 // printf("Found point group [0] %s select subgroup\n",point_group);
322 // for(int i = 0; i < msgl;i++) printf("\t [%d] %s\n",i+1,msg[i].name);
323
324 m_symmetryWidget->setPointGroupSymbol(pointGroupSymbol(point_group));
325 m_symmetryWidget->setEquivalenceSets(mesl, mes);
326 m_symmetryWidget->setSymmetryOperations(msopsl, msops);
327 m_symmetryWidget->setSubgroups(msgl, msg);
328 m_symmetryWidget->setCenterOfMass(cm);
329 m_symmetryWidget->setRadius(radius);
330 // m_symmetryWidget->m_ui->pointGroupLabel->setText(pgSymbol(point_group));
331
332 qDebug() << "detected symmetry" << point_group;
333
334 free(elements);
335 m_dirty = false;
336 }
337
338 void Symmetry::symmetrizeMolecule()
339 {
340 qDebug() << "symmetrize";
341 unsigned int length = m_molecule->atomCount();
342
343 if (m_molecule == nullptr || m_molecule->atomPositions3d().size() != length ||
344 length < 2)
345 return; // if one atom = Kh
346
347 msym_element_t* melements = nullptr;
348 int mlength = 0;
349 double symerr = 0.0;
350 msym_error_t ret = MSYM_SUCCESS;
351
352 // detectSymmetry();
353 if (MSYM_SUCCESS != (ret = msymSymmetrizeElements(m_ctx, &symerr)))
354 return;
355
356 if (MSYM_SUCCESS != (ret = msymGetElements(m_ctx, &mlength, &melements)))
357 return;
358
359 if (mlength != static_cast<int>(length))
360 return;
361
362 for (Index i = 0; i < length; ++i) {
363 m_molecule->atomPositions3d()[i] = Vector3(melements[i].v);
364 }
365
366 m_molecule->emitChanged(QtGui::Molecule::Atoms | QtGui::Molecule::Modified);
367 }
368
369 /*
370 void Symmetry::standardOrientation()
371 {
372 CrystalTools::rotateToStandardOrientation(*m_molecule,
373 CrystalTools::TransformAtoms);
374 m_molecule->emitChanged(Molecule::Modified
375 | Molecule::Atoms | Molecule::UnitCell);
376 }*/
377
378 } // namespace Avogadro::QtPlugins