| 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 "molden.h" |
| 7 |
|
| 8 |
#include <avogadro/core/elements.h> |
| 9 |
#include <avogadro/core/gaussianset.h> |
| 10 |
#include <avogadro/core/molecule.h> |
| 11 |
#include <avogadro/core/utilities.h> |
| 12 |
|
| 13 |
#include <iomanip> |
| 14 |
#include <iostream> |
| 15 |
#include <sstream> |
| 16 |
|
| 17 |
using std::cout; |
| 18 |
using std::endl; |
| 19 |
using std::string; |
| 20 |
using std::vector; |
| 21 |
|
| 22 |
namespace Avogadro::QuantumIO { |
| 23 |
|
| 24 |
using Core::Atom; |
| 25 |
using Core::BasisSet; |
| 26 |
using Core::GaussianSet; |
| 27 |
|
| 28 |
MoldenFile::MoldenFile() |
| 29 |
: m_coordFactor(1.0), m_electrons(0), m_mode(Unrecognized) |
| 30 |
{ |
| 31 |
} |
| 32 |
|
| 33 |
MoldenFile::~MoldenFile() {} |
| 34 |
|
| 35 |
std::vector<std::string> MoldenFile::fileExtensions() const |
| 36 |
{ |
| 37 |
std::vector<std::string> extensions; |
| 38 |
extensions.emplace_back("mold"); |
| 39 |
extensions.emplace_back("molf"); |
| 40 |
extensions.emplace_back("molden"); |
| 41 |
return extensions; |
| 42 |
} |
| 43 |
|
| 44 |
std::vector<std::string> MoldenFile::mimeTypes() const |
| 45 |
{ |
| 46 |
return std::vector<std::string>(); |
| 47 |
} |
| 48 |
|
| 49 |
bool MoldenFile::read(std::istream& in, Core::Molecule& molecule) |
| 50 |
{ |
| 51 |
// Read the log file line by line, most sections are terminated by an empty |
| 52 |
// line, so they should be retained. |
| 53 |
while (!in.eof() && in.good()) { |
| 54 |
processLine(in); |
| 55 |
} |
| 56 |
|
| 57 |
if (m_aPos.size() % 3 != 0) { |
| 58 |
appendError("Invalid atom coordinate data in Molden file."); |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
const size_t atomCount = m_aPos.size() / 3; |
| 63 |
if (m_aNums.size() < atomCount) { |
| 64 |
appendError("Invalid atom list in Molden file."); |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
for (size_t i = 0; i < atomCount; ++i) { |
| 69 |
Atom a = molecule.addAtom(static_cast<unsigned char>(m_aNums[i])); |
| 70 |
const size_t posIndex = i * 3; |
| 71 |
a.setPosition3d( |
| 72 |
Vector3(m_aPos[posIndex], m_aPos[posIndex + 1], m_aPos[posIndex + 2])); |
| 73 |
} |
| 74 |
// Do simple bond perception. |
| 75 |
molecule.perceiveBondsSimple(); |
| 76 |
molecule.perceiveBondOrders(); |
| 77 |
|
| 78 |
auto* basis = new GaussianSet; |
| 79 |
if (!load(basis, atomCount)) { |
| 80 |
delete basis; |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
molecule.setBasisSet(basis); |
| 85 |
basis->setMolecule(&molecule); |
| 86 |
|
| 87 |
if (m_frequencies.size() > 0 && |
| 88 |
m_frequencies.size() == m_vibDisplacements.size()) { |
| 89 |
molecule.setVibrationFrequencies(m_frequencies); |
| 90 |
molecule.setVibrationLx(m_vibDisplacements); |
| 91 |
|
| 92 |
// if we don't have intensities, set them all to zero |
| 93 |
if (m_IRintensities.size() != m_frequencies.size()) { |
| 94 |
m_IRintensities.resize(m_frequencies.size()); |
| 95 |
for (unsigned int i = 0; i < m_frequencies.size(); i++) |
| 96 |
m_IRintensities[i] = 0.0; |
| 97 |
} |
| 98 |
molecule.setVibrationIRIntensities(m_IRintensities); |
| 99 |
|
| 100 |
if (m_RamanIntensities.size()) |
| 101 |
molecule.setVibrationRamanIntensities(m_RamanIntensities); |
| 102 |
} |
| 103 |
|
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
void MoldenFile::processLine(std::istream& in) |
| 108 |
{ |
| 109 |
// First truncate the line, remove trailing white space and check for blanks. |
| 110 |
string line; |
| 111 |
if (!getline(in, line) || Core::trimmed(line).empty()) |
| 112 |
return; |
| 113 |
|
| 114 |
vector<string> list = Core::split(line, ' '); |
| 115 |
|
| 116 |
// Big switch statement checking for various things we are interested in. The |
| 117 |
// Molden file format uses sections, each starts with a header line of the |
| 118 |
// form [Atoms], and the beginning of a new section denotes the end of the |
| 119 |
// last. |
| 120 |
if (Core::contains(line, "[Atoms]") || Core::contains(line, "[ATOMS]")) { |
| 121 |
if (list.size() > 1 && Core::contains(list[1], "AU")) |
| 122 |
m_coordFactor = BOHR_TO_ANGSTROM_D; |
| 123 |
m_mode = Atoms; |
| 124 |
} else if (Core::contains(line, "[GTO]")) { |
| 125 |
m_mode = GTO; |
| 126 |
} else if (Core::contains(line, "[MO]")) { |
| 127 |
m_mode = MO; |
| 128 |
} else if (Core::contains(line, "[FREQ]")) { |
| 129 |
m_mode = Frequencies; |
| 130 |
} else if (Core::contains(line, "[FR-NORM-COORD]")) { |
| 131 |
m_mode = VibrationalModes; |
| 132 |
} else if (Core::contains(line, "[INT]")) { |
| 133 |
m_mode = Intensities; |
| 134 |
} else if (Core::contains(line, "[5D]")) { |
| 135 |
m_cartesianD = false; |
| 136 |
} else if (Core::contains(line, "[7F]")) { |
| 137 |
m_cartesianF = false; |
| 138 |
} else if (Core::contains(line, "[9G]")) { |
| 139 |
m_cartesianG = false; |
| 140 |
} else if (Core::contains(line, "[")) { // unknown section |
| 141 |
m_mode = Unrecognized; |
| 142 |
} else { |
| 143 |
// We are in a section, and must parse the lines in that section. |
| 144 |
string shell; |
| 145 |
GaussianSet::orbital shellType; |
| 146 |
|
| 147 |
std::streampos currentPos = in.tellg(); |
| 148 |
|
| 149 |
// Parsing a line of data in a section - what mode are we in? |
| 150 |
switch (m_mode) { |
| 151 |
case Atoms: |
| 152 |
readAtom(list); |
| 153 |
break; |
| 154 |
case GTO: { |
| 155 |
// TODO: detect dead files and make bullet-proof |
| 156 |
int atom = Core::lexicalCast<int>(list[0]).value_or(0); |
| 157 |
|
| 158 |
getline(in, line); |
| 159 |
line = Core::trimmed(line); |
| 160 |
while (!line.empty()) { // Read the shell types in this GTO. |
| 161 |
list = Core::split(line, ' '); |
| 162 |
if (list.size() < 1) |
| 163 |
break; |
| 164 |
shell = list[0]; |
| 165 |
std::transform(shell.begin(), shell.end(), shell.begin(), tolower); |
| 166 |
shellType = GaussianSet::UU; |
| 167 |
if (shell == "sp") |
| 168 |
shellType = GaussianSet::SP; |
| 169 |
else if (shell == "s") |
| 170 |
shellType = GaussianSet::S; |
| 171 |
else if (shell == "p") |
| 172 |
shellType = GaussianSet::P; |
| 173 |
else if (shell == "d") { |
| 174 |
if (m_cartesianD) |
| 175 |
shellType = GaussianSet::D; |
| 176 |
else |
| 177 |
shellType = GaussianSet::D5; |
| 178 |
} else if (shell == "f") { |
| 179 |
if (m_cartesianF) |
| 180 |
shellType = GaussianSet::F; |
| 181 |
else |
| 182 |
shellType = GaussianSet::F7; |
| 183 |
} else if (shell == "g") { |
| 184 |
if (m_cartesianG) |
| 185 |
shellType = GaussianSet::G; |
| 186 |
else |
| 187 |
shellType = GaussianSet::G9; |
| 188 |
} |
| 189 |
// TODO if Molden ever supports h, i, j |
| 190 |
if (shellType != GaussianSet::UU) { |
| 191 |
m_shellTypes.push_back(shellType); |
| 192 |
m_shelltoAtom.push_back(atom); |
| 193 |
} else { |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
int numGTOs = Core::lexicalCast<int>(list[1]).value_or(0); |
| 198 |
m_shellNums.push_back(numGTOs); |
| 199 |
|
| 200 |
// Now read all the exponents and contraction coefficients. |
| 201 |
for (int gto = 0; gto < numGTOs; ++gto) { |
| 202 |
getline(in, line); |
| 203 |
line = Core::trimmed(line); |
| 204 |
list = Core::split(line, ' '); |
| 205 |
if (list.size() > 1) { |
| 206 |
m_a.push_back(Core::lexicalCast<double>(list[0]).value_or(0.0)); |
| 207 |
m_c.push_back(Core::lexicalCast<double>(list[1]).value_or(0.0)); |
| 208 |
} |
| 209 |
if (shellType == GaussianSet::SP && list.size() > 2) |
| 210 |
m_csp.push_back(Core::lexicalCast<double>(list[2]).value_or(0.0)); |
| 211 |
} |
| 212 |
// Start reading the next shell. |
| 213 |
getline(in, line); |
| 214 |
line = Core::trimmed(line); |
| 215 |
} |
| 216 |
} break; |
| 217 |
|
| 218 |
case MO: { |
| 219 |
// Buffer for orbital header fields - we need to wait for Spin line |
| 220 |
// before committing, since Ene/Sym may appear before Spin |
| 221 |
double pendingEnergy = 0.0; |
| 222 |
bool havePendingEnergy = false; |
| 223 |
string pendingSymmetry; |
| 224 |
bool havePendingSymmetry = false; |
| 225 |
bool pendingSpinBeta = m_currentSpinBeta; // default to current state |
| 226 |
|
| 227 |
// Parse the occupation, spin, energy, etc (Occup, Spin, Ene). |
| 228 |
while (!line.empty() && Core::contains(line, "=")) { |
| 229 |
if (Core::contains(line, "Occup")) |
| 230 |
m_electrons += Core::lexicalCast<int>(list.back()).value_or(0); |
| 231 |
else if (Core::contains(line, "Ene")) { |
| 232 |
pendingEnergy = |
| 233 |
Core::lexicalCast<double>(list.back()).value_or(0.0) * |
| 234 |
HARTREE_TO_EV_D; |
| 235 |
havePendingEnergy = true; |
| 236 |
} else if (Core::contains(line, "Spin")) { |
| 237 |
// Check for Beta spin - handle both "Spin= Beta" and "Spin=Beta" |
| 238 |
if (Core::contains(line, "Beta")) { |
| 239 |
pendingSpinBeta = true; |
| 240 |
m_openShell = true; |
| 241 |
} else { |
| 242 |
pendingSpinBeta = false; |
| 243 |
} |
| 244 |
} else if (Core::contains(line, "Sym")) { |
| 245 |
pendingSymmetry = list.back(); |
| 246 |
havePendingSymmetry = true; |
| 247 |
} |
| 248 |
getline(in, line); |
| 249 |
line = Core::trimmed(line); |
| 250 |
list = Core::split(line, ' '); |
| 251 |
} |
| 252 |
|
| 253 |
// Now commit the buffered values with the correct spin |
| 254 |
m_currentSpinBeta = pendingSpinBeta; |
| 255 |
if (havePendingEnergy) { |
| 256 |
if (m_currentSpinBeta) |
| 257 |
m_betaOrbitalEnergy.push_back(pendingEnergy); |
| 258 |
else |
| 259 |
m_orbitalEnergy.push_back(pendingEnergy); |
| 260 |
} |
| 261 |
if (havePendingSymmetry) { |
| 262 |
if (m_currentSpinBeta) |
| 263 |
m_betaSymmetryLabels.push_back(pendingSymmetry); |
| 264 |
else |
| 265 |
m_symmetryLabels.push_back(pendingSymmetry); |
| 266 |
} |
| 267 |
|
| 268 |
// Parse the molecular orbital coefficients. |
| 269 |
while (!line.empty() && !Core::contains(line, "=") && |
| 270 |
!Core::contains(line, "[")) { |
| 271 |
list = Core::split(line, ' '); |
| 272 |
if (list.size() < 2) |
| 273 |
break; |
| 274 |
|
| 275 |
if (m_currentSpinBeta) |
| 276 |
m_betaMOcoeffs.push_back( |
| 277 |
Core::lexicalCast<double>(list[1]).value_or(0.0)); |
| 278 |
else |
| 279 |
m_MOcoeffs.push_back( |
| 280 |
Core::lexicalCast<double>(list[1]).value_or(0.0)); |
| 281 |
|
| 282 |
// we might go too far ahead |
| 283 |
currentPos = in.tellg(); |
| 284 |
getline(in, line); |
| 285 |
line = Core::trimmed(line); |
| 286 |
list = Core::split(line, ' '); |
| 287 |
} |
| 288 |
// go back one line |
| 289 |
in.seekg(currentPos); |
| 290 |
} break; |
| 291 |
|
| 292 |
case Frequencies: |
| 293 |
// Parse the frequencies. |
| 294 |
m_frequencies.clear(); |
| 295 |
while (!line.empty() && !Core::contains(line, "[")) { |
| 296 |
line = Core::trimmed(line); |
| 297 |
m_frequencies.push_back( |
| 298 |
Core::lexicalCast<double>(line).value_or(0.0)); |
| 299 |
currentPos = in.tellg(); |
| 300 |
getline(in, line); |
| 301 |
} |
| 302 |
// go back to previous line |
| 303 |
in.seekg(currentPos); |
| 304 |
break; |
| 305 |
|
| 306 |
case VibrationalModes: |
| 307 |
// Parse the vibrational modes. |
| 308 |
// should be "vibration 1" etc. |
| 309 |
// then the normal mode displacements |
| 310 |
m_vibDisplacements.clear(); |
| 311 |
// shouldn't be more than the number of frequencies |
| 312 |
while (!line.empty() && !Core::contains(line, "[")) { |
| 313 |
if (Core::contains(line, "vibration")) { |
| 314 |
m_vibDisplacements.push_back(Core::Array<Vector3>()); |
| 315 |
getline(in, line); |
| 316 |
line = Core::trimmed(line); |
| 317 |
while (!line.empty() && !Core::contains(line, "[") && |
| 318 |
!Core::contains(line, "vibration")) { |
| 319 |
list = Core::split(line, ' '); |
| 320 |
if (list.size() < 3) |
| 321 |
break; |
| 322 |
|
| 323 |
m_vibDisplacements.back().push_back( |
| 324 |
Vector3(Core::lexicalCast<double>(list[0]).value_or(0.0) * |
| 325 |
BOHR_TO_ANGSTROM_D, |
| 326 |
Core::lexicalCast<double>(list[1]).value_or(0.0) * |
| 327 |
BOHR_TO_ANGSTROM_D, |
| 328 |
Core::lexicalCast<double>(list[2]).value_or(0.0) * |
| 329 |
BOHR_TO_ANGSTROM_D)); |
| 330 |
|
| 331 |
currentPos = in.tellg(); |
| 332 |
getline(in, line); |
| 333 |
line = Core::trimmed(line); |
| 334 |
} |
| 335 |
} else { |
| 336 |
// we shouldn't hit this, but better to be safe |
| 337 |
break; |
| 338 |
} |
| 339 |
|
| 340 |
// okay, we're either done reading |
| 341 |
// or we're at the next vibration |
| 342 |
if (m_vibDisplacements.size() == m_frequencies.size()) { |
| 343 |
// reset to make sure we don't miss any other sections |
| 344 |
// (e.g., intensities) |
| 345 |
in.seekg(currentPos); |
| 346 |
break; |
| 347 |
} |
| 348 |
} |
| 349 |
break; |
| 350 |
|
| 351 |
case Intensities: |
| 352 |
// could be just IR or two pieces including Raman |
| 353 |
while (!line.empty() && !Core::contains(line, "[")) { |
| 354 |
list = Core::split(line, ' '); |
| 355 |
m_IRintensities.push_back( |
| 356 |
Core::lexicalCast<double>(list[0]).value_or(0.0)); |
| 357 |
if (list.size() == 2) |
| 358 |
m_RamanIntensities.push_back( |
| 359 |
Core::lexicalCast<double>(list[1]).value_or(0.0)); |
| 360 |
|
| 361 |
if (m_IRintensities.size() == m_frequencies.size()) { |
| 362 |
// we're done |
| 363 |
break; |
| 364 |
} |
| 365 |
|
| 366 |
currentPos = in.tellg(); |
| 367 |
getline(in, line); |
| 368 |
line = Core::trimmed(line); |
| 369 |
} |
| 370 |
break; |
| 371 |
default: |
| 372 |
break; |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
void MoldenFile::readAtom(const vector<string>& list) |
| 378 |
{ |
| 379 |
// element_name number atomic_number x y z |
| 380 |
if (list.size() < 6) |
| 381 |
return; |
| 382 |
m_aNums.push_back(Core::lexicalCast<int>(list[2]).value_or(0)); |
| 383 |
m_aPos.push_back(Core::lexicalCast<double>(list[3]).value_or(0.0) * |
| 384 |
m_coordFactor); |
| 385 |
m_aPos.push_back(Core::lexicalCast<double>(list[4]).value_or(0.0) * |
| 386 |
m_coordFactor); |
| 387 |
m_aPos.push_back(Core::lexicalCast<double>(list[5]).value_or(0.0) * |
| 388 |
m_coordFactor); |
| 389 |
} |
| 390 |
|
| 391 |
bool MoldenFile::load(GaussianSet* basis, size_t atomCount) |
| 392 |
{ |
| 393 |
// Now load up our basis set |
| 394 |
basis->setElectronCount(m_electrons); |
| 395 |
|
| 396 |
if (m_shellTypes.size() != m_shellNums.size() || |
| 397 |
m_shellTypes.size() != m_shelltoAtom.size()) { |
| 398 |
appendError("Invalid basis set data in Molden file."); |
| 399 |
return false; |
| 400 |
} |
| 401 |
|
| 402 |
// Set up the GTO primitive counter, go through the shells and add them |
| 403 |
int nGTO = 0; |
| 404 |
int nSP = 0; // number of SP shells |
| 405 |
for (unsigned int i = 0; i < m_shellTypes.size(); ++i) { |
| 406 |
if (m_shelltoAtom[i] <= 0 || |
| 407 |
static_cast<size_t>(m_shelltoAtom[i]) > atomCount) { |
| 408 |
appendError("Invalid basis set atom index in Molden file."); |
| 409 |
return false; |
| 410 |
} |
| 411 |
|
| 412 |
// Handle the SP case separately - this should possibly be a distinct type |
| 413 |
if (m_shellTypes.at(i) == GaussianSet::SP) { |
| 414 |
// SP orbital type - currently have to unroll into two shells |
| 415 |
int s = basis->addBasis(m_shelltoAtom[i] - 1, GaussianSet::S); |
| 416 |
int p = basis->addBasis(m_shelltoAtom[i] - 1, GaussianSet::P); |
| 417 |
for (int j = 0; j < m_shellNums[i]; ++j) { |
| 418 |
if (static_cast<size_t>(nGTO) >= m_c.size() || |
| 419 |
static_cast<size_t>(nGTO) >= m_a.size()) { |
| 420 |
appendError("Invalid basis set coefficients in Molden file."); |
| 421 |
return false; |
| 422 |
} |
| 423 |
if (static_cast<size_t>(nSP) >= m_csp.size()) { |
| 424 |
appendError("Invalid SP coefficients in Molden file."); |
| 425 |
return false; |
| 426 |
} |
| 427 |
basis->addGto(s, m_c[nGTO], m_a[nGTO]); |
| 428 |
basis->addGto(p, m_csp[nSP], m_a[nGTO]); |
| 429 |
++nSP; |
| 430 |
++nGTO; |
| 431 |
} |
| 432 |
} else { |
| 433 |
// adjust the shell types if needed |
| 434 |
if (!m_cartesianD && m_shellTypes[i] == GaussianSet::D) |
| 435 |
m_shellTypes[i] = GaussianSet::D5; |
| 436 |
else if (m_cartesianD && m_shellTypes[i] == GaussianSet::D5) |
| 437 |
m_shellTypes[i] = GaussianSet::D; |
| 438 |
|
| 439 |
if (!m_cartesianF && m_shellTypes[i] == GaussianSet::F) |
| 440 |
m_shellTypes[i] = GaussianSet::F7; |
| 441 |
else if (m_cartesianF && m_shellTypes[i] == GaussianSet::F7) |
| 442 |
m_shellTypes[i] = GaussianSet::F; |
| 443 |
|
| 444 |
if (!m_cartesianG && m_shellTypes[i] == GaussianSet::G) |
| 445 |
m_shellTypes[i] = GaussianSet::G9; |
| 446 |
else if (m_cartesianG && m_shellTypes[i] == GaussianSet::G9) |
| 447 |
m_shellTypes[i] = GaussianSet::G; |
| 448 |
|
| 449 |
int b = basis->addBasis(m_shelltoAtom[i] - 1, m_shellTypes[i]); |
| 450 |
for (int j = 0; j < m_shellNums[i]; ++j) { |
| 451 |
if (static_cast<size_t>(nGTO) >= m_c.size() || |
| 452 |
static_cast<size_t>(nGTO) >= m_a.size()) { |
| 453 |
appendError("Invalid basis set coefficients in Molden file."); |
| 454 |
return false; |
| 455 |
} |
| 456 |
basis->addGto(b, m_c[nGTO], m_a[nGTO]); |
| 457 |
++nGTO; |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
// Now to load in the MO coefficients |
| 462 |
if (m_openShell) { |
| 463 |
// Set SCF type to UHF for open-shell calculations |
| 464 |
basis->setScfType(Core::Uhf); |
| 465 |
|
| 466 |
// Alpha orbitals (stored in m_MOcoeffs) |
| 467 |
if (m_MOcoeffs.size()) |
| 468 |
basis->setMolecularOrbitals(m_MOcoeffs, BasisSet::Alpha); |
| 469 |
if (m_orbitalEnergy.size()) |
| 470 |
basis->setMolecularOrbitalEnergy(m_orbitalEnergy, BasisSet::Alpha); |
| 471 |
if (m_symmetryLabels.size()) |
| 472 |
basis->setSymmetryLabels(m_symmetryLabels, BasisSet::Alpha); |
| 473 |
|
| 474 |
// Beta orbitals |
| 475 |
if (m_betaMOcoeffs.size()) |
| 476 |
basis->setMolecularOrbitals(m_betaMOcoeffs, BasisSet::Beta); |
| 477 |
if (m_betaOrbitalEnergy.size()) |
| 478 |
basis->setMolecularOrbitalEnergy(m_betaOrbitalEnergy, BasisSet::Beta); |
| 479 |
if (m_betaSymmetryLabels.size()) |
| 480 |
basis->setSymmetryLabels(m_betaSymmetryLabels, BasisSet::Beta); |
| 481 |
} else { |
| 482 |
// Closed-shell (RHF) - use Paired type |
| 483 |
basis->setScfType(Core::Rhf); |
| 484 |
if (m_MOcoeffs.size()) |
| 485 |
basis->setMolecularOrbitals(m_MOcoeffs); |
| 486 |
if (m_orbitalEnergy.size()) |
| 487 |
basis->setMolecularOrbitalEnergy(m_orbitalEnergy); |
| 488 |
if (m_symmetryLabels.size()) |
| 489 |
basis->setSymmetryLabels(m_symmetryLabels); |
| 490 |
} |
| 491 |
|
| 492 |
return true; |
| 493 |
} |
| 494 |
|
| 495 |
void MoldenFile::outputAll() |
| 496 |
{ |
| 497 |
cout << "Shell mappings:\n"; |
| 498 |
for (unsigned int i = 0; i < m_shellTypes.size(); ++i) |
| 499 |
cout << i << ": type = " << m_shellTypes.at(i) |
| 500 |
<< ", number = " << m_shellNums.at(i) |
| 501 |
<< ", atom = " << m_shelltoAtom.at(i) << endl; |
| 502 |
cout << "MO coefficients:\n"; |
| 503 |
for (double m_MOcoeff : m_MOcoeffs) |
| 504 |
cout << m_MOcoeff << "\t"; |
| 505 |
cout << endl; |
| 506 |
} |
| 507 |
|
| 508 |
bool MoldenFile::write(std::ostream& out, const Core::Molecule& molecule) |
| 509 |
{ |
| 510 |
// Set output precision for floating point numbers |
| 511 |
out << std::setprecision(10) << std::scientific; |
| 512 |
|
| 513 |
// Write the Molden format header |
| 514 |
out << "[Molden Format]\n"; |
| 515 |
|
| 516 |
// Get the basis set if available |
| 517 |
const auto* basis = dynamic_cast<const GaussianSet*>(molecule.basisSet()); |
| 518 |
|
| 519 |
// Check for spherical basis functions and write appropriate flags |
| 520 |
if (basis != nullptr) { |
| 521 |
// We need to check what types of shells are present |
| 522 |
std::vector<int> symmetry = basis->symmetry(); |
| 523 |
bool hasD5 = false, hasF7 = false, hasG9 = false; |
| 524 |
|
| 525 |
for (int sym : symmetry) { |
| 526 |
if (sym == GaussianSet::D5) |
| 527 |
hasD5 = true; |
| 528 |
else if (sym == GaussianSet::F7) |
| 529 |
hasF7 = true; |
| 530 |
else if (sym == GaussianSet::G9) |
| 531 |
hasG9 = true; |
| 532 |
} |
| 533 |
|
| 534 |
if (hasD5) |
| 535 |
out << "[5D]\n"; |
| 536 |
if (hasF7) |
| 537 |
out << "[7F]\n"; |
| 538 |
if (hasG9) |
| 539 |
out << "[9G]\n"; |
| 540 |
} |
| 541 |
|
| 542 |
// Write atoms section |
| 543 |
writeAtoms(out, molecule); |
| 544 |
|
| 545 |
// Write GTO basis set if available |
| 546 |
if (basis != nullptr) { |
| 547 |
writeGTO(out, basis); |
| 548 |
writeMO(out, basis); |
| 549 |
} |
| 550 |
|
| 551 |
// Write multiple coordinate sets if available |
| 552 |
if (molecule.coordinate3dCount() > 1) { |
| 553 |
writeGeometries(out, molecule); |
| 554 |
} |
| 555 |
|
| 556 |
// Write vibrational data if available |
| 557 |
if (molecule.vibrationFrequencies().size() > 0) { |
| 558 |
writeFrequencies(out, molecule); |
| 559 |
} |
| 560 |
|
| 561 |
return out.good(); |
| 562 |
} |
| 563 |
|
| 564 |
void MoldenFile::writeAtoms(std::ostream& out, const Core::Molecule& molecule) |
| 565 |
{ |
| 566 |
out << "[Atoms] Angs\n"; |
| 567 |
|
| 568 |
for (Index i = 0; i < molecule.atomCount(); ++i) { |
| 569 |
unsigned char atomicNum = molecule.atomicNumber(i); |
| 570 |
Vector3 pos = molecule.atomPosition3d(i); |
| 571 |
|
| 572 |
// Format: element_name number atomic_number x y z |
| 573 |
out << std::left << std::setw(4) << Core::Elements::symbol(atomicNum) |
| 574 |
<< std::right << std::setw(6) << (i + 1) << std::setw(6) |
| 575 |
<< static_cast<int>(atomicNum) << " " << std::setw(18) << pos.x() |
| 576 |
<< std::setw(18) << pos.y() << std::setw(18) << pos.z() << "\n"; |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
void MoldenFile::writeGTO(std::ostream& out, const Core::GaussianSet* basis) |
| 581 |
{ |
| 582 |
out << "[GTO]\n"; |
| 583 |
|
| 584 |
std::vector<int> symmetry = basis->symmetry(); |
| 585 |
std::vector<unsigned int> atomIndices = basis->atomIndices(); |
| 586 |
std::vector<unsigned int> gtoIndices = basis->gtoIndices(); |
| 587 |
std::vector<double> gtoA = basis->gtoA(); |
| 588 |
std::vector<double> gtoC = basis->gtoC(); |
| 589 |
|
| 590 |
// Group shells by atom |
| 591 |
unsigned int currentAtom = UINT_MAX; |
| 592 |
for (unsigned int i = 0; i < symmetry.size(); ++i) { |
| 593 |
unsigned int atomIdx = atomIndices[i]; |
| 594 |
|
| 595 |
// Start a new atom block if needed |
| 596 |
if (atomIdx != currentAtom) { |
| 597 |
if (currentAtom != UINT_MAX) { |
| 598 |
out << "\n"; // Blank line between atoms |
| 599 |
} |
| 600 |
currentAtom = atomIdx; |
| 601 |
out << (atomIdx + 1) |
| 602 |
<< " 0\n"; // atom number (1-indexed) and unused field |
| 603 |
} |
| 604 |
|
| 605 |
// Get shell type string |
| 606 |
string shellType; |
| 607 |
int shellSym = symmetry[i]; |
| 608 |
switch (shellSym) { |
| 609 |
case GaussianSet::S: |
| 610 |
shellType = "s"; |
| 611 |
break; |
| 612 |
case GaussianSet::P: |
| 613 |
shellType = "p"; |
| 614 |
break; |
| 615 |
case GaussianSet::D: |
| 616 |
case GaussianSet::D5: |
| 617 |
shellType = "d"; |
| 618 |
break; |
| 619 |
case GaussianSet::F: |
| 620 |
case GaussianSet::F7: |
| 621 |
shellType = "f"; |
| 622 |
break; |
| 623 |
case GaussianSet::G: |
| 624 |
case GaussianSet::G9: |
| 625 |
shellType = "g"; |
| 626 |
break; |
| 627 |
case GaussianSet::H: |
| 628 |
case GaussianSet::H11: |
| 629 |
shellType = "h"; |
| 630 |
break; |
| 631 |
case GaussianSet::I: |
| 632 |
case GaussianSet::I13: |
| 633 |
shellType = "i"; |
| 634 |
break; |
| 635 |
default: |
| 636 |
shellType = "s"; // fallback |
| 637 |
break; |
| 638 |
} |
| 639 |
|
| 640 |
// Get the number of primitives in this shell |
| 641 |
unsigned int startGTO = gtoIndices[i]; |
| 642 |
unsigned int endGTO = |
| 643 |
(i + 1 < gtoIndices.size()) ? gtoIndices[i + 1] : gtoA.size(); |
| 644 |
unsigned int numPrimitives = endGTO - startGTO; |
| 645 |
|
| 646 |
out << shellType << " " << numPrimitives << " 1.00\n"; |
| 647 |
|
| 648 |
// Write exponents and contraction coefficients |
| 649 |
for (unsigned int j = startGTO; j < endGTO; ++j) { |
| 650 |
out << " " << std::setw(18) << gtoA[j] << std::setw(18) << gtoC[j] |
| 651 |
<< "\n"; |
| 652 |
} |
| 653 |
} |
| 654 |
out << "\n"; // Final blank line to end GTO section |
| 655 |
} |
| 656 |
|
| 657 |
void MoldenFile::writeMO(std::ostream& out, const Core::GaussianSet* basis) |
| 658 |
{ |
| 659 |
out << "[MO]\n"; |
| 660 |
|
| 661 |
Core::ScfType scfType = basis->scfType(); |
| 662 |
bool isOpenShell = (scfType == Core::Uhf || scfType == Core::Rohf); |
| 663 |
|
| 664 |
// Helper lambda to write orbitals for a given electron type |
| 665 |
auto writeOrbitals = [&](BasisSet::ElectronType type, |
| 666 |
const string& spinLabel) { |
| 667 |
MatrixX moMatrix = basis->moMatrix(type); |
| 668 |
std::vector<double> energies = basis->moEnergy(type); |
| 669 |
std::vector<std::string> symLabels = basis->symmetryLabels(type); |
| 670 |
std::vector<unsigned char> occupancies = basis->moOccupancy(type); |
| 671 |
|
| 672 |
if (moMatrix.cols() == 0) |
| 673 |
return; |
| 674 |
|
| 675 |
unsigned int numMOs = moMatrix.cols(); |
| 676 |
unsigned int numBasis = moMatrix.rows(); |
| 677 |
|
| 678 |
for (unsigned int mo = 0; mo < numMOs; ++mo) { |
| 679 |
// Write symmetry label |
| 680 |
string symLabel = (mo < symLabels.size() && !symLabels[mo].empty()) |
| 681 |
? symLabels[mo] |
| 682 |
: "a1"; |
| 683 |
out << " Sym= " << symLabel << "\n"; |
| 684 |
|
| 685 |
// Write energy (convert from eV back to Hartree for Molden format) |
| 686 |
double energy = |
| 687 |
(mo < energies.size()) ? energies[mo] / HARTREE_TO_EV_D : 0.0; |
| 688 |
out << " Ene= " << std::setw(18) << energy << "\n"; |
| 689 |
|
| 690 |
// Write spin |
| 691 |
out << " Spin= " << spinLabel << "\n"; |
| 692 |
|
| 693 |
// Write occupation |
| 694 |
int occup = 0; |
| 695 |
if (mo < occupancies.size()) { |
| 696 |
occup = occupancies[mo]; |
| 697 |
} else if (!isOpenShell) { |
| 698 |
// For closed shell, assume doubly occupied up to HOMO |
| 699 |
occup = (mo < basis->lumo(type)) ? 2 : 0; |
| 700 |
} else { |
| 701 |
// For open shell, assume singly occupied up to HOMO |
| 702 |
occup = (mo < basis->lumo(type)) ? 1 : 0; |
| 703 |
} |
| 704 |
out << " Occup= " << occup << "\n"; |
| 705 |
|
| 706 |
// Write MO coefficients |
| 707 |
for (unsigned int bf = 0; bf < numBasis; ++bf) { |
| 708 |
out << std::setw(6) << (bf + 1) << std::setw(18) << moMatrix(bf, mo) |
| 709 |
<< "\n"; |
| 710 |
} |
| 711 |
} |
| 712 |
}; |
| 713 |
|
| 714 |
if (isOpenShell) { |
| 715 |
// Write alpha and beta orbitals separately |
| 716 |
writeOrbitals(BasisSet::Alpha, "Alpha"); |
| 717 |
writeOrbitals(BasisSet::Beta, "Beta"); |
| 718 |
} else { |
| 719 |
// Write paired orbitals as Alpha (standard convention) |
| 720 |
writeOrbitals(BasisSet::Paired, "Alpha"); |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
void MoldenFile::writeFrequencies(std::ostream& out, |
| 725 |
const Core::Molecule& molecule) |
| 726 |
{ |
| 727 |
Core::Array<double> frequencies = molecule.vibrationFrequencies(); |
| 728 |
Core::Array<double> irIntensities = molecule.vibrationIRIntensities(); |
| 729 |
Core::Array<double> ramanIntensities = molecule.vibrationRamanIntensities(); |
| 730 |
|
| 731 |
if (frequencies.size() == 0) |
| 732 |
return; |
| 733 |
|
| 734 |
// Write frequencies |
| 735 |
out << "[FREQ]\n"; |
| 736 |
for (size_t i = 0; i < frequencies.size(); ++i) { |
| 737 |
out << std::setw(18) << frequencies[i] << "\n"; |
| 738 |
} |
| 739 |
|
| 740 |
// Write coordinates for vibrations (in Bohr) |
| 741 |
out << "[FR-COORD]\n"; |
| 742 |
for (Index i = 0; i < molecule.atomCount(); ++i) { |
| 743 |
unsigned char atomicNum = molecule.atomicNumber(i); |
| 744 |
Vector3 pos = molecule.atomPosition3d(i) * ANGSTROM_TO_BOHR_D; |
| 745 |
|
| 746 |
out << std::left << std::setw(4) << Core::Elements::symbol(atomicNum) |
| 747 |
<< std::right << std::setw(18) << pos.x() << std::setw(18) << pos.y() |
| 748 |
<< std::setw(18) << pos.z() << "\n"; |
| 749 |
} |
| 750 |
|
| 751 |
// Write normal mode displacements (in Bohr) |
| 752 |
out << "[FR-NORM-COORD]\n"; |
| 753 |
for (size_t mode = 0; mode < frequencies.size(); ++mode) { |
| 754 |
out << "vibration " << (mode + 1) << "\n"; |
| 755 |
Core::Array<Vector3> lx = molecule.vibrationLx(static_cast<int>(mode)); |
| 756 |
for (size_t atom = 0; atom < lx.size(); ++atom) { |
| 757 |
// Convert from Angstrom to Bohr for Molden format |
| 758 |
Vector3 disp = lx[atom] * ANGSTROM_TO_BOHR_D; |
| 759 |
out << std::setw(18) << disp.x() << std::setw(18) << disp.y() |
| 760 |
<< std::setw(18) << disp.z() << "\n"; |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
// Write intensities if available |
| 765 |
if (irIntensities.size() > 0) { |
| 766 |
out << "[INT]\n"; |
| 767 |
for (size_t i = 0; i < irIntensities.size(); ++i) { |
| 768 |
out << std::setw(18) << irIntensities[i]; |
| 769 |
if (i < ramanIntensities.size()) { |
| 770 |
out << std::setw(18) << ramanIntensities[i]; |
| 771 |
} |
| 772 |
out << "\n"; |
| 773 |
} |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
void MoldenFile::writeGeometries(std::ostream& out, |
| 778 |
const Core::Molecule& molecule) |
| 779 |
{ |
| 780 |
size_t numCoordSets = molecule.coordinate3dCount(); |
| 781 |
if (numCoordSets <= 1) |
| 782 |
return; |
| 783 |
|
| 784 |
out << "[GEOMETRIES] XYZ\n"; |
| 785 |
|
| 786 |
for (size_t coordIdx = 0; coordIdx < numCoordSets; ++coordIdx) { |
| 787 |
Core::Array<Vector3> coords = molecule.coordinate3d(coordIdx); |
| 788 |
|
| 789 |
// Write number of atoms and a comment line (required for XYZ format) |
| 790 |
out << molecule.atomCount() << "\n"; |
| 791 |
out << "Frame " << (coordIdx + 1) << "\n"; |
| 792 |
|
| 793 |
for (Index i = 0; i < molecule.atomCount(); ++i) { |
| 794 |
unsigned char atomicNum = molecule.atomicNumber(i); |
| 795 |
Vector3 pos = (i < coords.size()) ? coords[i] : Vector3::Zero(); |
| 796 |
|
| 797 |
out << std::left << std::setw(4) << Core::Elements::symbol(atomicNum) |
| 798 |
<< std::right << std::setw(18) << pos.x() << std::setw(18) << pos.y() |
| 799 |
<< std::setw(18) << pos.z() << "\n"; |
| 800 |
} |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
} // namespace Avogadro::QuantumIO |