| 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 "molecularmodel.h" |
| 7 |
|
| 8 |
#include <avogadro/core/elements.h> |
| 9 |
#include <avogadro/core/gaussianset.h> |
| 10 |
#include <avogadro/core/residue.h> |
| 11 |
#include <avogadro/qtgui/molecule.h> |
| 12 |
|
| 13 |
#include <QtCore/QDebug> |
| 14 |
|
| 15 |
#include <limits> |
| 16 |
|
| 17 |
namespace Avogadro::QtPlugins { |
| 18 |
|
| 19 |
using Avogadro::Core::BasisSet; |
| 20 |
using Avogadro::Core::GaussianSet; |
| 21 |
using Avogadro::QtGui::Molecule; |
| 22 |
using QtGui::Molecule; |
| 23 |
|
| 24 |
// CODATA 2022 |
| 25 |
// https://physics.nist.gov/cgi-bin/cuu/Value?hrev |
| 26 |
#define AU_TO_EV 27.211386245981 |
| 27 |
|
| 28 |
MolecularModel::MolecularModel(QObject* parent) |
| 29 |
: QAbstractTableModel(parent), m_molecule(nullptr) |
| 30 |
{ |
| 31 |
} |
| 32 |
|
| 33 |
void MolecularModel::setMolecule(QtGui::Molecule* molecule) |
| 34 |
{ |
| 35 |
// Disconnect from the old molecule if set |
| 36 |
if (m_molecule) { |
| 37 |
disconnect(m_molecule, &QtGui::Molecule::changed, this, |
| 38 |
&MolecularModel::updateTable); |
| 39 |
} |
| 40 |
|
| 41 |
m_molecule = molecule; |
| 42 |
|
| 43 |
// make sure we know if the molecule changed |
| 44 |
if (m_molecule) { |
| 45 |
connect(m_molecule, &QtGui::Molecule::changed, this, |
| 46 |
&MolecularModel::updateTable); |
| 47 |
} |
| 48 |
updateTable(QtGui::Molecule::Added); |
| 49 |
} |
| 50 |
|
| 51 |
QString MolecularModel::name() const |
| 52 |
{ |
| 53 |
if (!m_molecule || m_molecule->atomCount() == 0) |
| 54 |
return QString(); |
| 55 |
|
| 56 |
// check if we have a markup_name |
| 57 |
if (!m_molecule->data("markup_name").toString().empty()) |
| 58 |
return QString::fromStdString(m_molecule->data("markup_name").toString()); |
| 59 |
|
| 60 |
// if not, check if there's any name |
| 61 |
if (!m_molecule->data("name").toString().empty()) |
| 62 |
return QString::fromStdString(m_molecule->data("name").toString()); |
| 63 |
|
| 64 |
return tr("(pending)"); |
| 65 |
} |
| 66 |
|
| 67 |
int MolecularModel::rowCount(const QModelIndex& parent) const |
| 68 |
{ |
| 69 |
Q_UNUSED(parent); |
| 70 |
|
| 71 |
if (!m_molecule) |
| 72 |
return 0; |
| 73 |
|
| 74 |
return m_propertiesCache.size(); |
| 75 |
} |
| 76 |
|
| 77 |
int MolecularModel::columnCount(const QModelIndex& parent) const |
| 78 |
{ |
| 79 |
Q_UNUSED(parent); |
| 80 |
return 1; // values |
| 81 |
} |
| 82 |
|
| 83 |
QString formatPointGroup(std::string pointgroup) |
| 84 |
{ |
| 85 |
// first character is in capital |
| 86 |
// then everything else is in subscript using <sub>...</sub> |
| 87 |
QString formatted = QString::fromStdString(pointgroup); |
| 88 |
QString output = formatted.at(0).toUpper(); |
| 89 |
output += QString("<sub>%1</sub>").arg(formatted.mid(1)); |
| 90 |
return output; |
| 91 |
} |
| 92 |
|
| 93 |
// Qt calls this for multiple "roles" across row / columns in the index |
| 94 |
// we also combine multiple types into this class, so lots of special cases |
| 95 |
QVariant MolecularModel::data(const QModelIndex& index, int role) const |
| 96 |
{ |
| 97 |
if (!index.isValid() || m_molecule == nullptr) |
| 98 |
return QVariant(); |
| 99 |
|
| 100 |
int row = index.row(); |
| 101 |
[[maybe_unused]] int col = index.column(); |
| 102 |
|
| 103 |
// Simple lambda to convert QFlags to variant as in Qt 6 this needs help. |
| 104 |
auto toVariant = [&](auto flags) { |
| 105 |
return static_cast<Qt::Alignment::Int>(flags); |
| 106 |
}; |
| 107 |
|
| 108 |
// handle text alignments |
| 109 |
if (role == Qt::TextAlignmentRole) { |
| 110 |
return toVariant(Qt::AlignRight); |
| 111 |
} |
| 112 |
|
| 113 |
if (role != Qt::UserRole && role != Qt::DisplayRole && role != Qt::EditRole) |
| 114 |
return QVariant(); |
| 115 |
|
| 116 |
const auto map = m_propertiesCache; |
| 117 |
auto it = map.begin(); |
| 118 |
|
| 119 |
switch (row) { |
| 120 |
case 0: |
| 121 |
return name(); |
| 122 |
case 1: |
| 123 |
return m_molecule->mass(); |
| 124 |
case 2: |
| 125 |
return m_molecule->monoisotopicMass(); |
| 126 |
case 3: |
| 127 |
return m_molecule->formattedFormula(); |
| 128 |
case 4: |
| 129 |
return QVariant::fromValue(m_molecule->atomCount()); |
| 130 |
case 5: |
| 131 |
return QVariant::fromValue(m_molecule->bondCount()); |
| 132 |
} |
| 133 |
|
| 134 |
std::advance(it, row); |
| 135 |
auto key = it->first; |
| 136 |
if (key == " 6coordinateSets") |
| 137 |
return QVariant::fromValue(m_molecule->coordinate3dCount()); |
| 138 |
else if (key == " 7residues") |
| 139 |
return QVariant::fromValue(m_molecule->residueCount()); |
| 140 |
else if (key == " 9totalCharge") |
| 141 |
return QVariant::fromValue(static_cast<int>(m_molecule->totalCharge())); |
| 142 |
else if (key == " 9totalSpinMultiplicity") |
| 143 |
return QVariant::fromValue( |
| 144 |
static_cast<int>(m_molecule->totalSpinMultiplicity())); |
| 145 |
else if (key == "pointgroup") |
| 146 |
return formatPointGroup(it->second.toString()); |
| 147 |
|
| 148 |
return QString::fromStdString(it->second.toString()); |
| 149 |
} |
| 150 |
|
| 151 |
QVariant MolecularModel::headerData(int section, Qt::Orientation orientation, |
| 152 |
int role) const |
| 153 |
{ |
| 154 |
// Simple lambda to convert QFlags to variant as in Qt 6 this needs help. |
| 155 |
auto toVariant = [&](auto flags) { |
| 156 |
return static_cast<Qt::Alignment::Int>(flags); |
| 157 |
}; |
| 158 |
|
| 159 |
// handle text alignments |
| 160 |
if (role == Qt::TextAlignmentRole) { |
| 161 |
if (orientation == Qt::Vertical) { |
| 162 |
return toVariant(Qt::AlignLeft); |
| 163 |
} |
| 164 |
return toVariant(Qt::AlignHCenter); |
| 165 |
} |
| 166 |
|
| 167 |
if (role != Qt::DisplayRole) |
| 168 |
return QVariant(); |
| 169 |
|
| 170 |
if (orientation == Qt::Horizontal) { |
| 171 |
return tr("Property"); |
| 172 |
} else if (orientation == Qt::Vertical) { |
| 173 |
|
| 174 |
const auto map = m_propertiesCache; |
| 175 |
auto it = map.begin(); |
| 176 |
std::advance(it, section); |
| 177 |
if (it->first == " 1name") |
| 178 |
return tr("Molecule Name"); |
| 179 |
else if (it->first == " 2mass") |
| 180 |
return tr("Molecular Mass (g/mol)"); |
| 181 |
else if (it->first == " 2monoisotopicMass") |
| 182 |
return tr("Monoisotopic Mass (g/mol)"); |
| 183 |
else if (it->first == " 3formula") |
| 184 |
return tr("Chemical Formula"); |
| 185 |
else if (it->first == " 4atoms") |
| 186 |
return tr("Number of Atoms"); |
| 187 |
else if (it->first == " 5bonds") |
| 188 |
return tr("Number of Bonds"); |
| 189 |
else if (it->first == " 6coordinateSets") |
| 190 |
return tr("Coordinate Sets"); |
| 191 |
else if (it->first == " 7residues") |
| 192 |
return tr("Number of Residues"); |
| 193 |
else if (it->first == " 8chains") |
| 194 |
return tr("Number of Chains"); |
| 195 |
else if (it->first == " 9totalCharge") |
| 196 |
return tr("Net Charge"); |
| 197 |
else if (it->first == " 9totalSpinMultiplicity") |
| 198 |
return tr("Net Spin Multiplicity"); |
| 199 |
else if (it->first == "dipoleMoment") |
| 200 |
return tr("Dipole Moment (Debye)"); |
| 201 |
else if (it->first == "homoEnergy") |
| 202 |
return tr("HOMO Energy (eV)", "highest occupied molecular orbital"); |
| 203 |
else if (it->first == "lumoEnergy") |
| 204 |
return tr("LUMO Energy (eV)", "lowest unoccupied molecular orbital"); |
| 205 |
else if (it->first == "somoEnergy") |
| 206 |
return tr("SOMO Energy (eV)", "singly-occupied molecular orbital"); |
| 207 |
else if (it->first == "totalEnergy") |
| 208 |
return tr("Total Energy (Hartree)", |
| 209 |
"total electronic energy in Hartrees"); |
| 210 |
else if (it->first == "zpe") |
| 211 |
return tr("Zero Point Energy (kcal/mol)", |
| 212 |
"zero point vibrational energy"); |
| 213 |
else if (it->first == "enthalpy") |
| 214 |
return tr("Enthalpy (kcal/mol)"); |
| 215 |
else if (it->first == "entropy") |
| 216 |
return tr("Entropy (kcal/mol•K)"); |
| 217 |
else if (it->first == "gibbs") |
| 218 |
return tr("Gibbs Free Energy (kcal/mol)"); |
| 219 |
else if (it->first == "pointgroup") |
| 220 |
return tr("Point Group", "point group symmetry"); |
| 221 |
else if (it != map.end()) |
| 222 |
return QString::fromStdString(it->first); |
| 223 |
|
| 224 |
return QVariant(); |
| 225 |
|
| 226 |
} else // row headers |
| 227 |
return QVariant(); |
| 228 |
|
| 229 |
return QVariant(); |
| 230 |
} |
| 231 |
|
| 232 |
Qt::ItemFlags MolecularModel::flags(const QModelIndex& index) const |
| 233 |
{ |
| 234 |
if (!index.isValid()) |
| 235 |
return Qt::ItemIsEnabled; |
| 236 |
|
| 237 |
// return QAbstractItemModel::flags(index) | Qt::ItemIsEditable |
| 238 |
// for the types and columns that can be edited |
| 239 |
auto editable = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable; |
| 240 |
|
| 241 |
int row = index.row(); |
| 242 |
|
| 243 |
if (row == 0) // name |
| 244 |
return editable; |
| 245 |
|
| 246 |
const auto map = m_propertiesCache; |
| 247 |
auto it = map.begin(); |
| 248 |
std::advance(it, row); |
| 249 |
auto key = it->first; |
| 250 |
if (key == " 9totalCharge" || key == " 9totalSpinMultiplicity") |
| 251 |
return editable; |
| 252 |
|
| 253 |
return QAbstractItemModel::flags(index); |
| 254 |
} |
| 255 |
|
| 256 |
bool MolecularModel::setData(const QModelIndex& index, const QVariant& value, |
| 257 |
int role) |
| 258 |
{ |
| 259 |
if (!index.isValid()) |
| 260 |
return false; |
| 261 |
|
| 262 |
if (role != Qt::EditRole) |
| 263 |
return false; |
| 264 |
|
| 265 |
int row = index.row(); |
| 266 |
|
| 267 |
if (row == 0) { // name should always be the first row |
| 268 |
m_molecule->setData("name", value.toString().toStdString()); |
| 269 |
m_molecule->setData("markup_name", value.toString().toStdString()); |
| 270 |
emit dataChanged(index, index); |
| 271 |
return true; |
| 272 |
} |
| 273 |
|
| 274 |
const auto map = m_propertiesCache; |
| 275 |
auto it = map.begin(); |
| 276 |
std::advance(it, row); |
| 277 |
auto key = it->first; |
| 278 |
if (key == " 9totalCharge") { |
| 279 |
m_molecule->setData("totalCharge", value.toInt()); |
| 280 |
emit dataChanged(index, index); |
| 281 |
return true; |
| 282 |
} else if (key == " 9totalSpinMultiplicity") { |
| 283 |
int spin = value.toInt(); |
| 284 |
if (spin < 1) |
| 285 |
return false; |
| 286 |
|
| 287 |
m_molecule->setData("totalSpinMultiplicity", value.toInt()); |
| 288 |
emit dataChanged(index, index); |
| 289 |
return true; |
| 290 |
} |
| 291 |
|
| 292 |
return false; |
| 293 |
} |
| 294 |
|
| 295 |
void MolecularModel::updateTable(unsigned int flags) |
| 296 |
{ |
| 297 |
// cache all the properties |
| 298 |
m_propertiesCache.clear(); |
| 299 |
if (m_molecule == nullptr) |
| 300 |
return; |
| 301 |
|
| 302 |
// we use internal key names here and |
| 303 |
// update the display names in the headerData method |
| 304 |
m_propertiesCache.setValue(" 1name", name().toStdString()); |
| 305 |
m_propertiesCache.setValue(" 2mass", m_molecule->mass()); |
| 306 |
m_propertiesCache.setValue(" 2monoisotopicMass", |
| 307 |
m_molecule->monoisotopicMass()); |
| 308 |
m_propertiesCache.setValue(" 3formula", |
| 309 |
m_molecule->formattedFormula().toStdString()); |
| 310 |
m_propertiesCache.setValue(" 4atoms", |
| 311 |
static_cast<int>(m_molecule->atomCount())); |
| 312 |
m_propertiesCache.setValue(" 5bonds", |
| 313 |
static_cast<int>(m_molecule->bondCount())); |
| 314 |
if (m_molecule->coordinate3dCount() > 0) |
| 315 |
m_propertiesCache.setValue( |
| 316 |
" 6coordinateSets", static_cast<int>(m_molecule->coordinate3dCount())); |
| 317 |
if (m_molecule->residueCount() > 0) { |
| 318 |
m_propertiesCache.setValue(" 7residues", |
| 319 |
static_cast<int>(m_molecule->residueCount())); |
| 320 |
|
| 321 |
// figure out if we have multiple chains |
| 322 |
unsigned int chainCount = 0; |
| 323 |
unsigned int offset = 0; |
| 324 |
for (Index i = 0; i < m_molecule->residueCount(); ++i) { |
| 325 |
char chainId = m_molecule->residue(i).chainId(); |
| 326 |
if (chainId >= 'A' && chainId <= 'Z') |
| 327 |
offset = chainId - 'A'; |
| 328 |
else if (chainId >= 'a' && chainId <= 'z') |
| 329 |
offset = chainId - 'a'; |
| 330 |
else if (chainId >= '0' && chainId <= '9') |
| 331 |
offset = chainId - '0' + 15; // starts at 'P' |
| 332 |
|
| 333 |
chainCount = std::max(chainCount, offset); |
| 334 |
} |
| 335 |
m_propertiesCache.setValue(" 8chains", static_cast<int>(chainCount)); |
| 336 |
} |
| 337 |
|
| 338 |
m_propertiesCache.setValue(" 9totalCharge", |
| 339 |
static_cast<int>(m_molecule->totalCharge())); |
| 340 |
m_propertiesCache.setValue( |
| 341 |
" 9totalSpinMultiplicity", |
| 342 |
static_cast<int>(m_molecule->totalSpinMultiplicity())); |
| 343 |
if (m_molecule->hasData("dipoleMoment")) { |
| 344 |
auto dipole = m_molecule->data("dipoleMoment").toVector3(); |
| 345 |
QString moment = QString::number(dipole.norm(), 'f', 3); |
| 346 |
m_propertiesCache.setValue("dipoleMoment", moment.toStdString()); |
| 347 |
} |
| 348 |
|
| 349 |
// TODO check for homo, lumo, or somo energies |
| 350 |
const auto* basis = m_molecule->basisSet(); |
| 351 |
const GaussianSet* gaussianSet = dynamic_cast<const GaussianSet*>(basis); |
| 352 |
if (gaussianSet != nullptr && gaussianSet->scfType() == Core::Rhf) { |
| 353 |
unsigned int homo = gaussianSet->homo(); |
| 354 |
unsigned int lumo = gaussianSet->lumo(); |
| 355 |
const auto moEnergies = gaussianSet->moEnergy(); |
| 356 |
if (moEnergies.size() > homo) { |
| 357 |
m_propertiesCache.setValue("homoEnergy", moEnergies[homo] * AU_TO_EV); |
| 358 |
} |
| 359 |
// look for the lumo if there's a degenerate HOMO |
| 360 |
const double threshold = 0.01 / AU_TO_EV; // 0.01 eV minimal separation |
| 361 |
while (moEnergies.size() > lumo && |
| 362 |
std::abs(moEnergies[lumo] - moEnergies[homo]) < threshold) { |
| 363 |
lumo += 1; |
| 364 |
} |
| 365 |
if (moEnergies.size() > lumo) |
| 366 |
m_propertiesCache.setValue("lumoEnergy", moEnergies[lumo] * AU_TO_EV); |
| 367 |
} |
| 368 |
// m_propertiesCache.setValue("somoEnergy", energy); |
| 369 |
|
| 370 |
// ignore potentially duplicate properties |
| 371 |
const auto& properties = m_molecule->dataMap(); |
| 372 |
for (const auto& key : properties.names()) { |
| 373 |
if (key == "formula" || key == "name" || key == "fileName" || |
| 374 |
key == "energies" || key == "markup_name" || key == "totalCharge" || |
| 375 |
key == "totalSpinMultiplicity") |
| 376 |
continue; // skip these |
| 377 |
|
| 378 |
if (properties.value(key).toString().empty()) |
| 379 |
continue; // don't bother with an empty value |
| 380 |
|
| 381 |
m_propertiesCache.setValue(key, properties.value(key)); |
| 382 |
} |
| 383 |
|
| 384 |
if (flags & Molecule::Added || flags & Molecule::Removed) { |
| 385 |
// tear it down and rebuild the model |
| 386 |
beginResetModel(); |
| 387 |
endResetModel(); |
| 388 |
} else { |
| 389 |
// we can just update the current data |
| 390 |
emit dataChanged( |
| 391 |
QAbstractItemModel::createIndex(0, 0), |
| 392 |
QAbstractItemModel::createIndex(rowCount(), columnCount())); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
} // namespace Avogadro::QtPlugins |