| 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 <QAction> |
| 7 |
#include <QDialog> |
| 8 |
#include <QMessageBox> |
| 9 |
#include <QString> |
| 10 |
|
| 11 |
#include <avogadro/core/crystaltools.h> |
| 12 |
#include <avogadro/core/unitcell.h> |
| 13 |
#include <avogadro/qtgui/molecule.h> |
| 14 |
#include <avogadro/qtgui/chartdialog.h> |
| 15 |
#include <avogadro/qtgui/chartwidget.h> |
| 16 |
|
| 17 |
#include "pdfoptionsdialog.h" |
| 18 |
#include "plotpdf.h" |
| 19 |
|
| 20 |
using Avogadro::Core::CrystalTools; |
| 21 |
using Avogadro::Core::UnitCell; |
| 22 |
using Avogadro::QtGui::Molecule; |
| 23 |
|
| 24 |
using std::map; |
| 25 |
|
| 26 |
namespace Avogadro::QtPlugins { |
| 27 |
|
| 28 |
using Core::Array; |
| 29 |
|
| 30 |
PlotPdf::PlotPdf(QObject* parent_) |
| 31 |
: Avogadro::QtGui::ExtensionPlugin(parent_), m_actions(QList<QAction*>()), |
| 32 |
m_molecule(nullptr), |
| 33 |
m_pdfOptionsDialog(new PdfOptionsDialog(qobject_cast<QWidget*>(parent()))), |
| 34 |
m_displayDialogAction(new QAction(this)) |
| 35 |
{ |
| 36 |
m_displayDialogAction->setText(tr("Plot Pair Distribution Function…")); |
| 37 |
connect(m_displayDialogAction.data(), &QAction::triggered, this, |
| 38 |
&PlotPdf::displayDialog); |
| 39 |
m_actions.push_back(m_displayDialogAction.data()); |
| 40 |
m_displayDialogAction->setProperty("menu priority", 70); |
| 41 |
|
| 42 |
updateActions(); |
| 43 |
} |
| 44 |
|
| 45 |
PlotPdf::~PlotPdf() = default; |
| 46 |
|
| 47 |
QList<QAction*> PlotPdf::actions() const |
| 48 |
{ |
| 49 |
return m_actions; |
| 50 |
} |
| 51 |
|
| 52 |
QStringList PlotPdf::menuPath(QAction*) const |
| 53 |
{ |
| 54 |
return QStringList() << tr("&Crystal"); |
| 55 |
} |
| 56 |
|
| 57 |
void PlotPdf::setMolecule(QtGui::Molecule* mol) |
| 58 |
{ |
| 59 |
if (m_molecule == mol) |
| 60 |
return; |
| 61 |
|
| 62 |
if (m_molecule) |
| 63 |
m_molecule->disconnect(this); |
| 64 |
|
| 65 |
m_molecule = mol; |
| 66 |
|
| 67 |
if (m_molecule) |
| 68 |
connect(m_molecule, SIGNAL(changed(uint)), SLOT(moleculeChanged(uint))); |
| 69 |
|
| 70 |
updateActions(); |
| 71 |
} |
| 72 |
|
| 73 |
void PlotPdf::moleculeChanged(unsigned int c) |
| 74 |
{ |
| 75 |
Q_ASSERT(m_molecule == qobject_cast<Molecule*>(sender())); |
| 76 |
|
| 77 |
auto changes = static_cast<Molecule::MoleculeChanges>(c); |
| 78 |
|
| 79 |
if (changes & Molecule::UnitCell) { |
| 80 |
if (changes & Molecule::Added || changes & Molecule::Removed) |
| 81 |
updateActions(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
void PlotPdf::updateActions() |
| 86 |
{ |
| 87 |
// Disable everything for nullptr molecules. |
| 88 |
if (!m_molecule) { |
| 89 |
foreach (QAction* action, m_actions) |
| 90 |
action->setEnabled(false); |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
// Only display the actions if there is a unit cell |
| 95 |
if (m_molecule->unitCell()) { |
| 96 |
foreach (QAction* action, m_actions) |
| 97 |
action->setEnabled(true); |
| 98 |
} else { |
| 99 |
foreach (QAction* action, m_actions) |
| 100 |
action->setEnabled(false); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
void PlotPdf::displayDialog() |
| 105 |
{ |
| 106 |
// Do nothing if the user cancels |
| 107 |
if (m_pdfOptionsDialog->exec() != QDialog::Accepted) |
| 108 |
return; |
| 109 |
|
| 110 |
// Otherwise, fetch the options and perform the run |
| 111 |
double maxRadius = m_pdfOptionsDialog->maxRadius(); |
| 112 |
double step = m_pdfOptionsDialog->step(); |
| 113 |
// size_t numpoints = m_pdfOptionsDialog->numDataPoints(); |
| 114 |
// double max2theta = m_pdfOptionsDialog->max2Theta(); |
| 115 |
|
| 116 |
PdfData results; |
| 117 |
QString err; |
| 118 |
if (!generatePdfPattern(*m_molecule, results, err, maxRadius, step)) { |
| 119 |
QMessageBox::critical(qobject_cast<QWidget*>(parent()), |
| 120 |
tr("Failed to generate PDF pattern"), |
| 121 |
tr("Error message: ") + err); |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
// Now generate a plot with the data |
| 126 |
std::vector<float> xData; |
| 127 |
std::vector<float> yData; |
| 128 |
for (const auto& item : results) { |
| 129 |
xData.push_back(item.first); |
| 130 |
yData.push_back(item.second); |
| 131 |
} |
| 132 |
|
| 133 |
const char* xTitle = "r (Ă…)"; |
| 134 |
const char* yTitle = "g(r)"; |
| 135 |
const char* windowName = "Pair Distribution Function"; |
| 136 |
|
| 137 |
if (!m_chartDialog) |
| 138 |
m_chartDialog.reset( |
| 139 |
new QtGui::ChartDialog(qobject_cast<QWidget*>(this->parent()))); |
| 140 |
|
| 141 |
m_chartDialog->setWindowTitle(windowName); |
| 142 |
auto* chart = m_chartDialog->chartWidget(); |
| 143 |
chart->clearPlots(); |
| 144 |
chart->addPlot(xData, yData, QtGui::color4ub{ 255, 0, 0, 255 }); |
| 145 |
chart->setXAxisTitle(xTitle); |
| 146 |
chart->setYAxisTitle(yTitle); |
| 147 |
m_chartDialog->show(); |
| 148 |
m_chartDialog->raise(); |
| 149 |
m_chartDialog->activateWindow(); |
| 150 |
} |
| 151 |
|
| 152 |
bool PlotPdf::generatePdfPattern(QtGui::Molecule& mol, PdfData& results, |
| 153 |
QString& err, double maxRadius, double step) |
| 154 |
{ |
| 155 |
Array<Vector3> refAtomCoords = mol.atomPositions3d(); |
| 156 |
|
| 157 |
size_t i, j; |
| 158 |
|
| 159 |
UnitCell* uc = mol.unitCell(); |
| 160 |
if (!uc) { |
| 161 |
err = "No unit cell found."; |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
size_t a = static_cast<size_t>(maxRadius / uc->a()) + 1; |
| 166 |
size_t b = static_cast<size_t>(maxRadius / uc->b()) + 1; |
| 167 |
size_t c = static_cast<size_t>(maxRadius / uc->c()) + 1; |
| 168 |
|
| 169 |
Vector3 disp = a * uc->aVector() + b * uc->bVector() + c * uc->cVector(); |
| 170 |
for (i = 0; i < refAtomCoords.size(); ++i) { |
| 171 |
refAtomCoords[i] += disp; |
| 172 |
} |
| 173 |
|
| 174 |
Molecule newMolecule = mol; |
| 175 |
CrystalTools::buildSupercell(newMolecule, 2 * a + 1, 2 * b + 1, 2 * c + 1); |
| 176 |
|
| 177 |
Array<Vector3> newAtomCoords = newMolecule.atomPositions3d(); |
| 178 |
|
| 179 |
map<size_t, size_t> pdfCount; |
| 180 |
double dist, rStep = step; |
| 181 |
size_t k, binIdx; |
| 182 |
|
| 183 |
for (i = 0; i < refAtomCoords.size(); ++i) { |
| 184 |
for (j = 0; j < newAtomCoords.size(); ++j) { |
| 185 |
dist = (refAtomCoords.at(i) - newAtomCoords.at(j)).norm(); |
| 186 |
binIdx = static_cast<size_t>(dist / rStep); |
| 187 |
if (pdfCount.find(binIdx) == pdfCount.end()) { |
| 188 |
pdfCount.insert(std::make_pair(binIdx, 1)); |
| 189 |
} else { |
| 190 |
pdfCount[binIdx]++; |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
for (k = 0; k < static_cast<size_t>(maxRadius / rStep); k++) { |
| 196 |
if (pdfCount.find(k) == pdfCount.end()) { |
| 197 |
results.push_back(std::make_pair(k * rStep, 0.0)); |
| 198 |
} else { |
| 199 |
results.push_back(std::make_pair( |
| 200 |
k * rStep, pdfCount[k] * newMolecule.unitCell()->volume() / |
| 201 |
(4 * M_PI * pow(k * rStep, 2) * rStep * |
| 202 |
refAtomCoords.size() * newAtomCoords.size()))); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
return true; |
| 207 |
} |
| 208 |
|
| 209 |
} // namespace Avogadro::QtPlugins |