| 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 "orbitaltablemodel.h" |
| 7 |
#include "orbitalwidget.h" |
| 8 |
|
| 9 |
#include <avogadro/core/basisset.h> |
| 10 |
#include <avogadro/core/gaussianset.h> |
| 11 |
|
| 12 |
#include <QBrush> |
| 13 |
#include <QColor> |
| 14 |
#include <QDebug> |
| 15 |
#include <QGuiApplication> |
| 16 |
#include <QPalette> |
| 17 |
|
| 18 |
namespace Avogadro::QtPlugins { |
| 19 |
|
| 20 |
OrbitalTableModel::OrbitalTableModel(QWidget* parent) |
| 21 |
: QAbstractTableModel(parent) |
| 22 |
{ |
| 23 |
m_orbitals.clear(); |
| 24 |
} |
| 25 |
|
| 26 |
OrbitalTableModel::~OrbitalTableModel() {} |
| 27 |
|
| 28 |
int OrbitalTableModel::columnCount(const QModelIndex&) const |
| 29 |
{ |
| 30 |
return COUNT; |
| 31 |
} |
| 32 |
|
| 33 |
QVariant OrbitalTableModel::data(const QModelIndex& index, int role) const |
| 34 |
{ |
| 35 |
if (!index.isValid()) |
| 36 |
return QVariant(); |
| 37 |
|
| 38 |
const Orbital* orb = m_orbitals.at(index.row()); |
| 39 |
|
| 40 |
// Simple lambda to convert QFlags to variant as in Qt 6 this needs help. |
| 41 |
auto toVariant = [&](auto flags) { |
| 42 |
return static_cast<Qt::Alignment::Int>(flags); |
| 43 |
}; |
| 44 |
|
| 45 |
if (role == Qt::BackgroundRole) { |
| 46 |
// Tinted background for occupied orbitals |
| 47 |
if (orb->occupation >= 0.5f) { |
| 48 |
// Get default background and darken/lighten based on luminance |
| 49 |
QColor base = QGuiApplication::palette().color(QPalette::Base); |
| 50 |
// Use luminance to detect dark mode (dark mode has low luminance) |
| 51 |
int luminance = |
| 52 |
(base.red() * 299 + base.green() * 587 + base.blue() * 114) / 1000; |
| 53 |
if (luminance < 128) { |
| 54 |
// Dark mode: lighten the background |
| 55 |
return QBrush(base.lighter(200)); |
| 56 |
} else { |
| 57 |
// Light mode: darken the background |
| 58 |
return QBrush(base.darker(120)); |
| 59 |
} |
| 60 |
} |
| 61 |
return QVariant(); |
| 62 |
} |
| 63 |
|
| 64 |
if (role == Qt::TextAlignmentRole) { |
| 65 |
switch (Column(index.column())) { |
| 66 |
case C_Energy: |
| 67 |
return toVariant(Qt::AlignRight | |
| 68 |
Qt::AlignVCenter); // numeric alignment |
| 69 |
case C_Occupation: // everything else can be centered |
| 70 |
case C_Description: |
| 71 |
case C_Symmetry: |
| 72 |
default: |
| 73 |
return toVariant(Qt::AlignHCenter | Qt::AlignVCenter); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if (role != Qt::DisplayRole) |
| 78 |
return QVariant(); |
| 79 |
QString symbol; // use subscripts |
| 80 |
int subscriptStart; |
| 81 |
|
| 82 |
switch (Column(index.column())) { |
| 83 |
case C_Description: |
| 84 |
return orb->description; |
| 85 |
case C_Energy: |
| 86 |
return QString("%L1").arg(orb->energy, 0, 'f', 3); |
| 87 |
case C_Occupation: { |
| 88 |
// Show occupation arrows based on electron type and occupation |
| 89 |
if (orb->occupation < 0.5f) |
| 90 |
return QString(); // unoccupied - show nothing |
| 91 |
|
| 92 |
switch (orb->electronType) { |
| 93 |
case Core::BasisSet::Paired: |
| 94 |
return QString::fromUtf8("⇅"); // paired electrons |
| 95 |
case Core::BasisSet::Alpha: |
| 96 |
return QString::fromUtf8("↑ "); // alpha (spin up) |
| 97 |
case Core::BasisSet::Beta: |
| 98 |
return QString::fromUtf8(" ↓"); // beta (spin down) |
| 99 |
default: |
| 100 |
return QString(); |
| 101 |
} |
| 102 |
} |
| 103 |
case C_Symmetry: |
| 104 |
symbol = orb->symmetry; |
| 105 |
if (symbol.length() > 1) { |
| 106 |
subscriptStart = 1; |
| 107 |
if (symbol[0] == '?') |
| 108 |
subscriptStart++; |
| 109 |
symbol.insert(subscriptStart, QString("<sub>")); |
| 110 |
symbol.append(QString("</sub>")); |
| 111 |
} |
| 112 |
symbol.replace('\'', QString("<sup>'</sup>")); |
| 113 |
symbol.replace('"', QString("<sup>\"</sup>")); |
| 114 |
return symbol; |
| 115 |
case C_ElectronType: |
| 116 |
// Return integer value for hidden column (used internally) |
| 117 |
return static_cast<int>(orb->electronType); |
| 118 |
default: |
| 119 |
case COUNT: |
| 120 |
return QVariant(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
QVariant OrbitalTableModel::headerData(int section, Qt::Orientation orientation, |
| 125 |
int role) const |
| 126 |
{ |
| 127 |
if (role != Qt::DisplayRole) |
| 128 |
return QVariant(); |
| 129 |
|
| 130 |
if (orientation == Qt::Horizontal) { |
| 131 |
switch (Column(section)) { |
| 132 |
case C_Description: |
| 133 |
return tr("Orbital"); |
| 134 |
case C_Energy: |
| 135 |
return tr("Energy (eV)"); |
| 136 |
case C_Symmetry: |
| 137 |
return tr("Symmetry"); |
| 138 |
case C_Occupation: |
| 139 |
return tr("Occupation"); |
| 140 |
case C_ElectronType: |
| 141 |
return tr("Spin"); |
| 142 |
default: |
| 143 |
case COUNT: |
| 144 |
return QVariant(); |
| 145 |
} |
| 146 |
} else |
| 147 |
return QString::number(section + 1); |
| 148 |
} |
| 149 |
|
| 150 |
QModelIndex OrbitalTableModel::HOMO() const |
| 151 |
{ |
| 152 |
QString homoStr = tr("HOMO", "Highest Occupied MO"); |
| 153 |
// First try to find alpha-HOMO (for open-shell) or plain HOMO (for closed) |
| 154 |
for (int i = 0; i < m_orbitals.size(); i++) { |
| 155 |
const QString& desc = m_orbitals.at(i)->description; |
| 156 |
if (desc == homoStr || desc == QString::fromUtf8("α-") + homoStr) |
| 157 |
return index(i, 0); |
| 158 |
} |
| 159 |
return QModelIndex(); |
| 160 |
} |
| 161 |
|
| 162 |
QModelIndex OrbitalTableModel::LUMO() const |
| 163 |
{ |
| 164 |
QString lumoStr = tr("LUMO", "Lowest Unoccupied MO"); |
| 165 |
// First try to find alpha-LUMO (for open-shell) or plain LUMO (for closed) |
| 166 |
for (int i = 0; i < m_orbitals.size(); i++) { |
| 167 |
const QString& desc = m_orbitals.at(i)->description; |
| 168 |
if (desc == lumoStr || desc == QString::fromUtf8("α-") + lumoStr) |
| 169 |
return index(i, 0); |
| 170 |
} |
| 171 |
return QModelIndex(); |
| 172 |
} |
| 173 |
|
| 174 |
int OrbitalTableModel::orbitalIndex(int row) const |
| 175 |
{ |
| 176 |
if (row < 0 || row >= m_orbitals.size()) |
| 177 |
return -1; |
| 178 |
return m_orbitals.at(row)->index; |
| 179 |
} |
| 180 |
|
| 181 |
Core::BasisSet::ElectronType OrbitalTableModel::electronType(int row) const |
| 182 |
{ |
| 183 |
if (row < 0 || row >= m_orbitals.size()) |
| 184 |
return Core::BasisSet::Paired; |
| 185 |
return m_orbitals.at(row)->electronType; |
| 186 |
} |
| 187 |
|
| 188 |
// predicate for sorting by energy (for interleaving alpha/beta) |
| 189 |
bool orbitalEnergyLessThan(const Orbital* o1, const Orbital* o2) |
| 190 |
{ |
| 191 |
return (o1->energy < o2->energy); |
| 192 |
} |
| 193 |
|
| 194 |
// Helper to create orbital description string |
| 195 |
// e.g., "HOMO-1" or "alpha-HOMO" |
| 196 |
// prefix is supplied for alpha/beta orbitals as UTF-8 characters |
| 197 |
QString makeOrbitalDescription(unsigned int index, unsigned int homo, |
| 198 |
unsigned int lumo, const QString& homoStr, |
| 199 |
const QString& lumoStr, const QString& prefix) |
| 200 |
{ |
| 201 |
QString num = ""; |
| 202 |
bool leqHOMO = (index + 1 <= homo); |
| 203 |
|
| 204 |
if (index + 1 != homo && index + 1 != lumo) { |
| 205 |
if (leqHOMO) { |
| 206 |
num = "-" + QString::number(homo - index - 1); |
| 207 |
} else { |
| 208 |
num = "+" + QString::number(index + 1 - lumo); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
QString desc = prefix; |
| 213 |
desc += (leqHOMO) ? homoStr + num : lumoStr + num; |
| 214 |
return desc; |
| 215 |
} |
| 216 |
|
| 217 |
bool OrbitalTableModel::setOrbitals(const Core::BasisSet* basis) |
| 218 |
{ |
| 219 |
clearOrbitals(); |
| 220 |
|
| 221 |
auto* gaussianBasis = dynamic_cast<const Core::GaussianSet*>(basis); |
| 222 |
|
| 223 |
// Check if this is an open-shell calculation (UHF or ROHF) |
| 224 |
bool isOpenShell = false; |
| 225 |
if (gaussianBasis != nullptr) { |
| 226 |
Core::ScfType scfType = gaussianBasis->scfType(); |
| 227 |
isOpenShell = (scfType == Core::Uhf || scfType == Core::Rohf); |
| 228 |
} |
| 229 |
|
| 230 |
// Also check if beta orbitals actually exist |
| 231 |
if (isOpenShell) { |
| 232 |
auto betaEnergies = basis->moEnergy(Core::BasisSet::Beta); |
| 233 |
if (betaEnergies.empty()) { |
| 234 |
isOpenShell = false; // No beta data, treat as closed-shell |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
QString homoStr = tr("HOMO", "Highest Occupied MO"); |
| 239 |
QString lumoStr = tr("LUMO", "Lowest Unoccupied MO"); |
| 240 |
|
| 241 |
if (isOpenShell) { |
| 242 |
// Open-shell: create both alpha and beta orbitals |
| 243 |
unsigned int alphaHomo = basis->homo(Core::BasisSet::Alpha); |
| 244 |
unsigned int alphaLumo = basis->lumo(Core::BasisSet::Alpha); |
| 245 |
unsigned int betaHomo = basis->homo(Core::BasisSet::Beta); |
| 246 |
unsigned int betaLumo = basis->lumo(Core::BasisSet::Beta); |
| 247 |
|
| 248 |
auto alphaEnergies = basis->moEnergy(Core::BasisSet::Alpha); |
| 249 |
auto betaEnergies = basis->moEnergy(Core::BasisSet::Beta); |
| 250 |
auto alphaSymmetries = basis->symmetryLabels(Core::BasisSet::Alpha); |
| 251 |
auto betaSymmetries = basis->symmetryLabels(Core::BasisSet::Beta); |
| 252 |
auto alphaOccupancy = basis->moOccupancy(Core::BasisSet::Alpha); |
| 253 |
auto betaOccupancy = basis->moOccupancy(Core::BasisSet::Beta); |
| 254 |
|
| 255 |
unsigned int alphaCount = |
| 256 |
basis->molecularOrbitalCount(Core::BasisSet::Alpha); |
| 257 |
unsigned int betaCount = basis->molecularOrbitalCount(Core::BasisSet::Beta); |
| 258 |
|
| 259 |
// Generate orbital occupation from energy order if no occupancy is set |
| 260 |
if (alphaOccupancy.empty() && betaOccupancy.empty()) { |
| 261 |
|
| 262 |
const unsigned int nElec = alphaHomo + betaHomo; // total electrons |
| 263 |
alphaOccupancy.assign(alphaCount, 0.0); |
| 264 |
betaOccupancy.assign(betaCount, 0.0); |
| 265 |
|
| 266 |
unsigned int ia = 0; |
| 267 |
unsigned int ib = 0; |
| 268 |
|
| 269 |
// Fill the lowest-energy orbitals across alpha/beta ("merge" two sorted lists) |
| 270 |
for (unsigned int e = 0; e < nElec; ++e) { |
| 271 |
|
| 272 |
// Compare next orbital energies (assumed already sorted by index) |
| 273 |
const double ea = (ia < alphaEnergies.size()) ? alphaEnergies[ia] |
| 274 |
: std::numeric_limits<double>::infinity(); |
| 275 |
const double eb = (ib < betaEnergies.size()) ? betaEnergies[ib] |
| 276 |
: std::numeric_limits<double>::infinity(); |
| 277 |
|
| 278 |
if (ea < eb) { |
| 279 |
alphaOccupancy[ia] = 1.0; |
| 280 |
++ia; |
| 281 |
} else if (eb < ea) { |
| 282 |
betaOccupancy[ib] = 1.0; |
| 283 |
++ib; |
| 284 |
} else { |
| 285 |
// Fill less occupied spin orbitals if degenrate. In same conditions, alpha first |
| 286 |
if (ia <= ib) { |
| 287 |
alphaOccupancy[ia] = 1.0; |
| 288 |
++ia; |
| 289 |
} else { |
| 290 |
betaOccupancy[ib] = 1.0; |
| 291 |
++ib; |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
// Update HOMO/LUMO consistent with the generated occupations. |
| 297 |
alphaHomo = ia; |
| 298 |
betaHomo = ib; |
| 299 |
alphaLumo = ia + 1; |
| 300 |
betaLumo = ib + 1; |
| 301 |
} |
| 302 |
|
| 303 |
// Create alpha orbitals |
| 304 |
for (unsigned int i = 0; i < alphaCount; i++) { |
| 305 |
Orbital* orb = new Orbital; |
| 306 |
orb->energy = (i < alphaEnergies.size()) ? alphaEnergies[i] : 0.0; |
| 307 |
orb->index = i; |
| 308 |
orb->electronType = Core::BasisSet::Alpha; |
| 309 |
orb->description = makeOrbitalDescription( |
| 310 |
i, alphaHomo, alphaLumo, homoStr, lumoStr, QString::fromUtf8("α-")); |
| 311 |
if (i < alphaSymmetries.size()) |
| 312 |
orb->symmetry = QString::fromStdString(alphaSymmetries[i]); |
| 313 |
orb->queueEntry = nullptr; |
| 314 |
// Use occupancy data if available, otherwise fall back to HOMO-based |
| 315 |
if (i < alphaOccupancy.size()) |
| 316 |
orb->occupation = static_cast<float>(alphaOccupancy[i]); |
| 317 |
else |
| 318 |
orb->occupation = (i + 1 <= alphaHomo) ? 1.0f : 0.0f; |
| 319 |
m_orbitals.append(orb); |
| 320 |
} |
| 321 |
|
| 322 |
// Create beta orbitals |
| 323 |
for (unsigned int i = 0; i < betaCount; i++) { |
| 324 |
Orbital* orb = new Orbital; |
| 325 |
orb->energy = (i < betaEnergies.size()) ? betaEnergies[i] : 0.0; |
| 326 |
orb->index = i; |
| 327 |
orb->electronType = Core::BasisSet::Beta; |
| 328 |
orb->description = makeOrbitalDescription( |
| 329 |
i, betaHomo, betaLumo, homoStr, lumoStr, QString::fromUtf8("β-")); |
| 330 |
if (i < betaSymmetries.size()) |
| 331 |
orb->symmetry = QString::fromStdString(betaSymmetries[i]); |
| 332 |
orb->queueEntry = nullptr; |
| 333 |
// Use occupancy data if available, otherwise fall back to HOMO-based |
| 334 |
if (i < betaOccupancy.size()) |
| 335 |
orb->occupation = static_cast<float>(betaOccupancy[i]); |
| 336 |
else |
| 337 |
orb->occupation = (i + 1 <= betaHomo) ? 1.0f : 0.0f; |
| 338 |
m_orbitals.append(orb); |
| 339 |
} |
| 340 |
|
| 341 |
// Sort by energy to interleave alpha and beta orbitals |
| 342 |
std::sort(m_orbitals.begin(), m_orbitals.end(), orbitalEnergyLessThan); |
| 343 |
|
| 344 |
} else { |
| 345 |
// Closed-shell (RHF): original behavior with paired orbitals |
| 346 |
unsigned int homo = basis->homo(); |
| 347 |
unsigned int lumo = basis->lumo(); |
| 348 |
|
| 349 |
auto moEnergies = basis->moEnergy(Core::BasisSet::Paired); |
| 350 |
auto symmetryLabels = basis->symmetryLabels(Core::BasisSet::Paired); |
| 351 |
auto moOccupancy = basis->moOccupancy(Core::BasisSet::Paired); |
| 352 |
|
| 353 |
for (unsigned int i = 0; i < basis->molecularOrbitalCount(); i++) { |
| 354 |
Orbital* orb = new Orbital; |
| 355 |
orb->energy = (i < moEnergies.size()) ? moEnergies[i] : 0.0; |
| 356 |
orb->index = i; |
| 357 |
orb->electronType = Core::BasisSet::Paired; |
| 358 |
orb->description = |
| 359 |
makeOrbitalDescription(i, homo, lumo, homoStr, lumoStr, QString()); |
| 360 |
if (i < symmetryLabels.size()) |
| 361 |
orb->symmetry = QString::fromStdString(symmetryLabels[i]); |
| 362 |
orb->queueEntry = nullptr; |
| 363 |
// Use occupancy data if available, otherwise fall back to HOMO-based |
| 364 |
if (i < moOccupancy.size()) |
| 365 |
orb->occupation = static_cast<float>(moOccupancy[i]); |
| 366 |
else |
| 367 |
orb->occupation = (i + 1 <= homo) ? 2.0f : 0.0f; |
| 368 |
m_orbitals.append(orb); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
// add the rows for all the new orbitals |
| 373 |
if (!m_orbitals.isEmpty()) { |
| 374 |
beginInsertRows(QModelIndex(), 0, m_orbitals.size() - 1); |
| 375 |
endInsertRows(); |
| 376 |
} |
| 377 |
return true; |
| 378 |
} |
| 379 |
|
| 380 |
bool OrbitalTableModel::clearOrbitals() |
| 381 |
{ |
| 382 |
if (m_orbitals.size() > 0) { |
| 383 |
beginRemoveRows(QModelIndex(), 0, m_orbitals.size() - 1); |
| 384 |
m_orbitals.clear(); |
| 385 |
endRemoveRows(); |
| 386 |
} |
| 387 |
|
| 388 |
return true; |
| 389 |
} |
| 390 |
|
| 391 |
} // namespace Avogadro::QtPlugins |