/****************************************************************************** This source file is part of the Avogadro project. This source code is released under the 3-Clause BSD License, (see "LICENSE"). ******************************************************************************/ #include "gaussianfchk.h" #include #include #include #include #include #include #include using std::cout; using std::endl; using std::string; using std::vector; namespace Avogadro::QuantumIO { using Core::Atom; using Core::BasisSet; using Core::GaussianSet; using Core::Rhf; using Core::Rohf; using Core::Uhf; namespace { constexpr size_t kMaxFchkArrayEntries = 16ull * 1024 * 1024; // 16M entries constexpr size_t kMaxFchkMatrixElements = 32ull * 1024 * 1024; // 32M elements bool hasMinimumRemainingBytes(std::istream& in, size_t minBytes) { if (minBytes == 0) return true; auto pos = in.tellg(); if (pos == std::streampos(-1)) return true; in.clear(); in.seekg(0, std::ios::end); auto end = in.tellg(); in.seekg(pos); if (end == std::streampos(-1)) return true; const size_t remaining = static_cast(end - pos); return remaining >= minBytes; } } // namespace // https://physics.nist.gov/cgi-bin/cuu/Value?hrev const double hartreeToEV = 27.211386245981; GaussianFchk::GaussianFchk() : m_scftype(Rhf) {} GaussianFchk::~GaussianFchk() {} std::vector GaussianFchk::fileExtensions() const { std::vector extensions; extensions.emplace_back("fchk"); return extensions; } std::vector GaussianFchk::mimeTypes() const { return std::vector(); } bool GaussianFchk::read(std::istream& in, Core::Molecule& molecule) { // Read the log file line by line, most sections are terminated by an empty // line, so they should be retained. while (!in.eof()) processLine(in); if (!error().empty()) return false; const size_t posCount = m_aPos.size() / 3; const size_t numCount = m_aNums.size(); size_t atomCount = std::min(posCount, numCount); if (m_numAtoms > 0) { atomCount = std::min(atomCount, static_cast(m_numAtoms)); } if (atomCount == 0) { appendError("Could not find any atomic coordinates! Are you sure this is a " "Gaussian formatted checkpoint file?"); return false; } auto* basis = new GaussianSet; for (size_t i = 0; i < atomCount; ++i) { Atom a = molecule.addAtom(static_cast(m_aNums[i])); const size_t offset = i * 3; a.setPosition3d(Vector3(m_aPos[offset] * BOHR_TO_ANGSTROM, m_aPos[offset + 1] * BOHR_TO_ANGSTROM, m_aPos[offset + 2] * BOHR_TO_ANGSTROM)); } if (m_frequencies.size() > 0 && m_frequencies.size() == m_vibDisplacements.size() && m_frequencies.size() == m_IRintensities.size()) { molecule.setVibrationFrequencies(m_frequencies); molecule.setVibrationIRIntensities(m_IRintensities); molecule.setVibrationLx(m_vibDisplacements); if (m_RamanIntensities.size()) molecule.setVibrationRamanIntensities(m_RamanIntensities); } // set the total charge molecule.setData("totalCharge", m_charge); // set the spin multiplicity molecule.setData("totalSpinMultiplicity", m_spin); // dipole moment Core::Variant dipole(m_dipoleMoment.x(), m_dipoleMoment.y(), m_dipoleMoment.z()); molecule.setData("dipoleMoment", dipole); // Do simple bond perception. molecule.perceiveBondsSimple(); molecule.perceiveBondOrders(); molecule.setBasisSet(basis); basis->setMolecule(&molecule); load(basis); // partial charges if (m_mullikenCharges.size() == m_aNums.size()) { Eigen::MatrixXd charges(m_aNums.size(), 1); charges.setZero(); for (unsigned int i = 0; i < m_aNums.size(); ++i) { charges(i, 0) = m_mullikenCharges[i]; } molecule.setPartialCharges("Mulliken", charges); } return true; } void GaussianFchk::processLine(std::istream& in) { // First truncate the line, remove trailing white space and check any line of // the required length. We are looking for keyword lines of the form, // Charge I 0 // If we are in any other kind of block that is not known skip through until // we find a recognized block. string line; if (!getline(in, line) || line.size() < 44) return; string key = line.substr(0, 42); // cout << "Key:\t" << key << endl; key = Core::trimmed(key); string tmp = line.substr(43); vector list = Core::split(tmp, ' '); std::vector tmpVec; // Big switch statement checking for various things we are interested in if (Core::contains(key, "RHF")) { m_scftype = Rhf; } else if (Core::contains(key, "UHF")) { m_scftype = Uhf; } else if (key == "Number of atoms" && list.size() > 1) { m_numAtoms = Core::lexicalCast(list[1]).value_or(0); } else if (key == "Charge" && list.size() > 1) { m_charge = Core::lexicalCast(list[1]).value_or(0); } else if (key == "Multiplicity" && list.size() > 1) { m_spin = Core::lexicalCast(list[1]).value_or(1); } else if (key == "Dipole Moment" && list.size() > 2) { vector dipole = readArrayD(in, Core::lexicalCast(list[2]).value_or(0)); m_dipoleMoment = Vector3(dipole[0], dipole[1], dipole[2]); // convert from au m_dipoleMoment *= 2.541746; } else if (key == "Number of electrons" && list.size() > 1) { m_electrons = Core::lexicalCast(list[1]).value_or(0); } else if (key == "Number of alpha electrons" && list.size() > 1) { m_electronsAlpha = Core::lexicalCast(list[1]).value_or(0); } else if (key == "Number of beta electrons" && list.size() > 1) { m_electronsBeta = Core::lexicalCast(list[1]).value_or(0); } else if (key == "Number of basis functions" && list.size() > 1) { m_numBasisFunctions = Core::lexicalCast(list[1]).value_or(0); // cout << "Number of basis functions = " << m_numBasisFunctions << endl; } else if (key == "Atomic numbers" && list.size() > 2) { m_aNums = readArrayI(in, Core::lexicalCast(list[2]).value_or(0)); if (static_cast(m_aNums.size()) != Core::lexicalCast(list[2])) cout << "Reading atomic numbers failed.\n"; } // Now we get to the meat of it - coordinates of the atoms else if (key == "Current cartesian coordinates" && list.size() > 2) { m_aPos = readArrayD(in, Core::lexicalCast(list[2]).value_or(0), 16); } else if (key == "Mulliken Charges" && list.size() > 2) { m_mullikenCharges = readArrayD(in, Core::lexicalCast(list[2]).value_or(0), 16); } // The real meat is here - basis sets etc! else if (key == "Shell types" && list.size() > 2) { m_shellTypes = readArrayI(in, Core::lexicalCast(list[2]).value_or(0)); } else if (key == "Number of primitives per shell" && list.size() > 2) { m_shellNums = readArrayI(in, Core::lexicalCast(list[2]).value_or(0)); } else if (key == "Shell to atom map" && list.size() > 2) { m_shelltoAtom = readArrayI(in, Core::lexicalCast(list[2]).value_or(0)); } // Now to get the exponents and coefficients( else if (key == "Primitive exponents" && list.size() > 2) { m_a = readArrayD(in, Core::lexicalCast(list[2]).value_or(0), 16); } else if (key == "Contraction coefficients" && list.size() > 2) { m_c = readArrayD(in, Core::lexicalCast(list[2]).value_or(0), 16); } else if (key == "P(S=P) Contraction coefficients" && list.size() > 2) { m_csp = readArrayD(in, Core::lexicalCast(list[2]).value_or(0), 16); } else if (key == "Alpha Orbital Energies" || key == "orbital energies") { if (m_scftype == Rhf) { m_orbitalEnergy = readArrayD( in, Core::lexicalCast(list[2]).value_or(0), 16, hartreeToEV); } else if (m_scftype == Uhf) { m_alphaOrbitalEnergy = readArrayD( in, Core::lexicalCast(list[2]).value_or(0), 16, hartreeToEV); } } else if (key == "Beta Orbital Energies") { if (m_scftype != Uhf) { m_scftype = Uhf; m_alphaOrbitalEnergy = m_orbitalEnergy; m_orbitalEnergy = vector(); m_alphaMOcoeffs = m_MOcoeffs; m_MOcoeffs = vector(); } m_betaOrbitalEnergy = readArrayD( in, Core::lexicalCast(list[2]).value_or(0), 16, hartreeToEV); } else if ((key == "Alpha MO coefficients" || key == "MO coefficients (C)") && list.size() > 2) { if (m_scftype == Rhf) { m_MOcoeffs = readArrayD(in, Core::lexicalCast(list[2]).value_or(0), 16); } else if (m_scftype == Uhf) { m_alphaMOcoeffs = readArrayD(in, Core::lexicalCast(list[2]).value_or(0), 16); } else { cout << "Error, alpha MO coefficients, n = " << m_MOcoeffs.size() << endl; } } else if (key == "Beta MO coefficients" && list.size() > 2) { m_betaMOcoeffs = readArrayD(in, Core::lexicalCast(list[2]).value_or(0), 16); } else if (key == "Total SCF Density" && list.size() > 2) { if (!readDensityMatrix(in, Core::lexicalCast(list[2]).value_or(0), 16)) cout << "Error reading in the SCF density matrix.\n"; } else if (key == "Spin SCF Density" && list.size() > 2) { if (!readSpinDensityMatrix(in, Core::lexicalCast(list[2]).value_or(0), 16)) cout << "Error reading in the SCF spin density matrix.\n"; } else if (key == "Number of Normal Modes" && list.size() > 1) { m_normalModes = Core::lexicalCast(list[1]).value_or(0); } else if (key == "Vib-E2" && list.size() > 2) { m_frequencies.clear(); m_IRintensities.clear(); m_RamanIntensities.clear(); unsigned threeN = m_numAtoms * 3; // degrees of freedom tmpVec = readArrayD(in, Core::lexicalCast(list[2]).value_or(0), 16); // read in the first 3N-6 elements as frequencies for (unsigned int i = 0; i < static_cast(m_normalModes); ++i) { m_frequencies.push_back(tmpVec[i]); } // skip to after threeN elements then read IR intensities for (unsigned int i = threeN; i < threeN + static_cast(m_normalModes); ++i) { m_IRintensities.push_back(tmpVec[i]); } // now check if we have Raman intensities if (tmpVec[threeN + m_normalModes] != 0.0) { for (unsigned int i = threeN + m_normalModes; i < threeN + 2 * static_cast(m_normalModes); ++i) { m_RamanIntensities.push_back(tmpVec[i]); } } } else if (key == "Vib-Modes" && list.size() > 2) { tmpVec = readArrayD(in, Core::lexicalCast(list[2]).value_or(0), 16); m_vibDisplacements.clear(); if (tmpVec.size() == static_cast(m_numAtoms) * 3 * static_cast(m_normalModes)) { for (unsigned int i = 0; i < static_cast(m_normalModes); ++i) { Core::Array mode; for (unsigned int j = 0; j < static_cast(m_numAtoms); ++j) { Vector3 v(tmpVec[i * m_numAtoms * 3 + j * 3], tmpVec[i * m_numAtoms * 3 + j * 3 + 1], tmpVec[i * m_numAtoms * 3 + j * 3 + 2]); mode.push_back(v); } m_vibDisplacements.push_back(mode); } } } } void GaussianFchk::load(GaussianSet* basis) { // Now load up our basis set basis->setScfType(m_scftype); basis->setElectronCount(m_electrons); // basis->setElectronCount(m_electronsAlpha, Core::GaussianSet::alpha); // basis->setElectronCount(m_electronsBeta, Core::GaussianSet::beta); if (m_shellTypes.empty()) { cout << "GaussianFchk: no shell types found.\n"; return; } if (m_shellNums.size() != m_shellTypes.size() || m_shelltoAtom.size() != m_shellTypes.size()) { cout << "GaussianFchk: inconsistent shell data sizes.\n"; return; } if (m_numAtoms <= 0) { cout << "GaussianFchk: invalid atom count.\n"; return; } size_t totalPrimitives = 0; size_t totalSpPrimitives = 0; const size_t maxSize = std::numeric_limits::max(); for (size_t i = 0; i < m_shellTypes.size(); ++i) { const int shellNum = m_shellNums[i]; if (shellNum <= 0) { cout << "GaussianFchk: invalid shell primitive count.\n"; return; } const int atomIndex = m_shelltoAtom[i]; if (atomIndex <= 0 || atomIndex > m_numAtoms) { cout << "GaussianFchk: invalid shell-to-atom map.\n"; return; } const size_t shellNumU = static_cast(shellNum); if (totalPrimitives > maxSize - shellNumU) { cout << "GaussianFchk: shell primitive count overflow.\n"; return; } totalPrimitives += shellNumU; if (m_shellTypes[i] == -1) { if (totalSpPrimitives > maxSize - shellNumU) { cout << "GaussianFchk: SP primitive count overflow.\n"; return; } totalSpPrimitives += shellNumU; } } if (m_a.size() < totalPrimitives || m_c.size() < totalPrimitives) { cout << "GaussianFchk: insufficient primitive exponent data.\n"; return; } if (totalSpPrimitives > 0 && m_csp.size() < totalSpPrimitives) { cout << "GaussianFchk: insufficient SP contraction data.\n"; return; } // Set up the GTO primitive counter, go through the shells and add them int nGTO = 0; for (unsigned int i = 0; i < m_shellTypes.size(); ++i) { // Handle the SP case separately - this should possibly be a distinct type if (m_shellTypes[i] == -1) { // SP orbital type - actually have to add two shells int s = basis->addBasis(m_shelltoAtom[i] - 1, GaussianSet::S); int tmpGTO = nGTO; for (unsigned int j = 0; j < static_cast(m_shellNums[i]); ++j) { if (static_cast(nGTO) >= m_a.size() || static_cast(nGTO) >= m_c.size()) { cout << "GaussianFchk: primitive index out of range.\n"; return; } basis->addGto(s, m_c[nGTO], m_a[nGTO]); ++nGTO; } int p = basis->addBasis(m_shelltoAtom[i] - 1, GaussianSet::P); for (unsigned int j = 0; j < static_cast(m_shellNums[i]); ++j) { if (static_cast(tmpGTO) >= m_a.size() || static_cast(tmpGTO) >= m_csp.size()) { cout << "GaussianFchk: SP primitive index out of range.\n"; return; } basis->addGto(p, m_csp[tmpGTO], m_a[tmpGTO]); ++tmpGTO; } } else { GaussianSet::orbital type; switch (m_shellTypes[i]) { case 0: type = GaussianSet::S; break; case 1: type = GaussianSet::P; break; case 2: type = GaussianSet::D; break; case -2: type = GaussianSet::D5; break; case 3: type = GaussianSet::F; break; case -3: type = GaussianSet::F7; break; case 4: type = GaussianSet::G; break; case -4: type = GaussianSet::G9; break; case 5: type = GaussianSet::H; break; case -5: type = GaussianSet::H11; break; case 6: type = GaussianSet::I; break; case -6: type = GaussianSet::I13; break; default: // If we encounter GTOs we do not understand, the basis is likely // invalid type = GaussianSet::UU; /// basis->setValid(false); } if (type != GaussianSet::UU) { int b = basis->addBasis(m_shelltoAtom[i] - 1, type); for (unsigned int j = 0; j < static_cast(m_shellNums[i]); ++j) { if (static_cast(nGTO) >= m_a.size() || static_cast(nGTO) >= m_c.size()) { cout << "GaussianFchk: primitive index out of range.\n"; return; } basis->addGto(b, m_c[nGTO], m_a[nGTO]); ++nGTO; } } } } // Now to load in the MO coefficients if (basis->isValid()) { if (m_MOcoeffs.size()) basis->setMolecularOrbitals(m_MOcoeffs); else cout << "Error no MO coefficients...\n"; if (m_alphaMOcoeffs.size()) basis->setMolecularOrbitals(m_alphaMOcoeffs, BasisSet::Alpha); if (m_betaMOcoeffs.size()) basis->setMolecularOrbitals(m_betaMOcoeffs, BasisSet::Beta); if (m_density.rows()) basis->setDensityMatrix(m_density); if (m_spinDensity.rows()) basis->setSpinDensityMatrix(m_spinDensity); if (m_orbitalEnergy.size()) // restricted calculation basis->setMolecularOrbitalEnergy(m_orbitalEnergy); else { if (m_alphaOrbitalEnergy.size()) basis->setMolecularOrbitalEnergy(m_alphaOrbitalEnergy, BasisSet::Alpha); if (m_betaOrbitalEnergy.size()) basis->setMolecularOrbitalEnergy(m_betaOrbitalEnergy, BasisSet::Beta); } } else { cout << "Basis set is not valid!\n"; } } vector GaussianFchk::readArrayI(std::istream& in, unsigned int n) { vector tmp; if (n == 0) return tmp; if (n > kMaxFchkArrayEntries) { appendError("Array data exceeds supported size."); return tmp; } const size_t nSize = static_cast(n); if (!hasMinimumRemainingBytes(in, nSize)) { appendError("Invalid array data."); return tmp; } tmp.reserve(n); bool ok(false); while (tmp.size() < n) { if (in.eof()) { cout << "GaussianFchk::readArrayI could not read all elements " << n << " expected " << tmp.size() << " parsed.\n"; return tmp; } string line; if (getline(in, line), line.empty()) return tmp; vector list = Core::split(line, ' '); for (auto& i : list) { if (tmp.size() >= n) { cout << "Too many variables read in. File may be inconsistent. " << tmp.size() << " of " << n << endl; return tmp; } tmp.push_back(Core::lexicalCast(i, ok)); if (!ok) { cout << "Warning: problem converting string to integer: " << i << " in GaussianFchk::readArrayI.\n"; return tmp; } } } return tmp; } vector GaussianFchk::readArrayD(std::istream& in, unsigned int n, int width, double factor) { vector tmp; if (n == 0) return tmp; if (n > kMaxFchkArrayEntries) { appendError("Array data exceeds supported size."); return tmp; } const size_t nSize = static_cast(n); const size_t maxSize = std::numeric_limits::max(); const size_t minBytesPerEntry = width > 0 ? static_cast(width) : 1ull; if (minBytesPerEntry > 0 && nSize > maxSize / minBytesPerEntry) { appendError("Array data exceeds supported size."); return tmp; } if (!hasMinimumRemainingBytes(in, nSize * minBytesPerEntry)) { appendError("Invalid array data."); return tmp; } tmp.reserve(n); bool ok(false); while (tmp.size() < n) { if (in.eof()) { cout << "GaussianFchk::readArrayD could not read all elements " << n << " expected " << tmp.size() << " parsed.\n"; return tmp; } string line; if (getline(in, line), line.empty()) return tmp; if (width == 0) { // we can split by spaces vector list = Core::split(line, ' '); for (auto& i : list) { if (tmp.size() >= n) { cout << "Too many variables read in. File may be inconsistent. " << tmp.size() << " of " << n << endl; return tmp; } tmp.push_back(Core::lexicalCast(i, ok) * factor); if (!ok) { cout << "Warning: problem converting string to integer: " << i << " in GaussianFchk::readArrayD.\n"; return tmp; } } } else { // Q-Chem files use 16 character fields size_t maxColumns = 80 / width; for (size_t i = 0; i < maxColumns; ++i) { string substring = line.substr(i * width, width); if (static_cast(substring.length()) != width) break; if (tmp.size() >= n) { cout << "Too many variables read in. File may be inconsistent. " << tmp.size() << " of " << n << endl; return tmp; } tmp.push_back(Core::lexicalCast(substring, ok) * factor); if (!ok) { cout << "Warning: problem converting string to double: " << substring << " in GaussianFchk::readArrayD.\n"; return tmp; } } } } return tmp; } bool GaussianFchk::readDensityMatrix(std::istream& in, unsigned int n, int width) { // This function reads in the lower triangular density matrix if (n == 0) return false; if (n > kMaxFchkArrayEntries) { appendError("Density matrix exceeds supported size."); return false; } if (m_numBasisFunctions <= 0) { appendError("Invalid basis function count."); return false; } const size_t basis = static_cast(m_numBasisFunctions); const size_t maxSize = std::numeric_limits::max(); if (basis > 0 && basis > maxSize / basis) { appendError("Density matrix exceeds supported size."); return false; } const size_t fullCount = basis * basis; if (fullCount > kMaxFchkMatrixElements) { appendError("Density matrix exceeds supported size."); return false; } if (basis > 0 && basis > maxSize / (basis + 1)) { appendError("Density matrix exceeds supported size."); return false; } const size_t expectedLower = basis * (basis + 1) / 2; if (static_cast(n) > expectedLower) { appendError("Invalid density matrix size."); return false; } const size_t minBytesPerEntry = width > 0 ? static_cast(width) : 1ull; if (minBytesPerEntry > 0 && static_cast(n) > maxSize / minBytesPerEntry) { appendError("Density matrix exceeds supported size."); return false; } if (!hasMinimumRemainingBytes(in, static_cast(n) * minBytesPerEntry)) { appendError("Invalid density matrix data."); return false; } m_density.resize(m_numBasisFunctions, m_numBasisFunctions); unsigned int cnt = 0; unsigned int i = 0, j = 0; unsigned int f = 1; bool ok = false; while (cnt < n) { if (in.eof()) { cout << "GaussianFchk::readDensityMatrix could not read all elements " << n << " expected " << cnt << " parsed.\n"; return false; } string line; if (getline(in, line), line.empty()) return false; if (width == 0) { // we can split by spaces vector list = Core::split(line, ' '); for (auto& k : list) { if (cnt >= n) { cout << "Too many variables read in. File may be inconsistent. " << cnt << " of " << n << endl; return false; } // Read in lower half matrix m_density(i, j) = Core::lexicalCast(k, ok); if (ok) { // Valid double converted, carry on ++j; ++cnt; if (j == f) { // We need to move down to the next row and increment f - lower tri j = 0; ++f; ++i; } } else { // Invalid conversion of a string to double cout << "Warning: problem converting string to double: " << k << "\nIn GaussianFchk::readDensityMatrix.\n"; return false; } } } else { // Q-Chem files use 16-character fields size_t maxColumns = 80 / width; for (size_t c = 0; c < maxColumns; ++c) { string substring = line.substr(c * width, width); if (static_cast(substring.length()) != width) { break; } else if (cnt >= n) { cout << "Too many variables read in. File may be inconsistent. " << cnt << " of " << n << endl; return false; } // Read in lower half matrix m_density(i, j) = Core::lexicalCast(substring, ok); if (ok) { // Valid double converted, carry on ++j; ++cnt; if (j == f) { // We need to move down to the next row and increment f - lower tri j = 0; ++f; ++i; } } else { // Invalid conversion of a string to double cout << "Warning: problem converting string to double: " << substring << "\nIn GaussianFchk::readDensityMatrix.\n"; return false; } } } } return true; } bool GaussianFchk::readSpinDensityMatrix(std::istream& in, unsigned int n, int width) { // This function reads in the lower triangular density matrix if (n == 0) return false; if (n > kMaxFchkArrayEntries) { appendError("Spin density matrix exceeds supported size."); return false; } if (m_numBasisFunctions <= 0) { appendError("Invalid basis function count."); return false; } const size_t basis = static_cast(m_numBasisFunctions); const size_t maxSize = std::numeric_limits::max(); if (basis > 0 && basis > maxSize / basis) { appendError("Spin density matrix exceeds supported size."); return false; } const size_t fullCount = basis * basis; if (fullCount > kMaxFchkMatrixElements) { appendError("Spin density matrix exceeds supported size."); return false; } if (basis > 0 && basis > maxSize / (basis + 1)) { appendError("Spin density matrix exceeds supported size."); return false; } const size_t expectedLower = basis * (basis + 1) / 2; if (static_cast(n) > expectedLower) { appendError("Invalid spin density matrix size."); return false; } const size_t minBytesPerEntry = width > 0 ? static_cast(width) : 1ull; if (minBytesPerEntry > 0 && static_cast(n) > maxSize / minBytesPerEntry) { appendError("Spin density matrix exceeds supported size."); return false; } if (!hasMinimumRemainingBytes(in, static_cast(n) * minBytesPerEntry)) { appendError("Invalid spin density matrix data."); return false; } m_spinDensity.resize(m_numBasisFunctions, m_numBasisFunctions); unsigned int cnt = 0; unsigned int i = 0, j = 0; unsigned int f = 1; bool ok = false; while (cnt < n) { if (in.eof()) { cout << "GaussianFchk::readSpinDensityMatrix could not read all elements " << n << " expected " << cnt << " parsed.\n"; return false; } string line; if (getline(in, line), line.empty()) return false; if (width == 0) { // we can split by spaces vector list = Core::split(line, ' '); for (auto& k : list) { if (cnt >= n) { cout << "Too many variables read in. File may be inconsistent. " << cnt << " of " << n << endl; return false; } // Read in lower half matrix m_spinDensity(i, j) = Core::lexicalCast(k, ok); if (ok) { // Valid double converted, carry on ++j; ++cnt; if (j == f) { // We need to move down to the next row and increment f - lower tri j = 0; ++f; ++i; } } else { // Invalid conversion of a string to double cout << "Warning: problem converting string to double: " << k << "\nIn GaussianFchk::readDensityMatrix.\n"; return false; } } } else { // Q-Chem files use 16-character fields size_t maxColumns = 80 / width; for (size_t c = 0; c < maxColumns; ++c) { string substring = line.substr(c * width, width); if (static_cast(substring.length()) != width) { break; } else if (cnt >= n) { cout << "Too many variables read in. File may be inconsistent. " << cnt << " of " << n << endl; return false; } // Read in lower half matrix m_spinDensity(i, j) = Core::lexicalCast(substring, ok); if (ok) { // Valid double converted, carry on ++j; ++cnt; if (j == f) { // We need to move down to the next row and increment f - lower tri j = 0; ++f; ++i; } } else { // Invalid conversion of a string to double cout << "Warning: problem converting string to double: " << substring << "\nIn GaussianFchk::readSpinDensityMatrix.\n"; return false; } } } } return true; } void GaussianFchk::outputAll() { switch (m_scftype) { case Rhf: cout << "SCF type = RHF\n"; break; case Uhf: cout << "SCF type = UHF\n"; break; case Rohf: cout << "SCF type = ROHF\n"; break; default: cout << "SCF type = Unknown\n"; } cout << "Shell mappings:\n"; for (unsigned int i = 0; i < m_shellTypes.size(); ++i) cout << i << " : type = " << m_shellTypes.at(i) << ", number = " << m_shellNums.at(i) << ", atom = " << m_shelltoAtom.at(i) << endl; if (m_MOcoeffs.size()) { cout << "MO coefficients:\n"; for (double m_MOcoeff : m_MOcoeffs) cout << m_MOcoeff << "\t"; cout << endl << endl; } if (m_alphaMOcoeffs.size()) { cout << "Alpha MO coefficients:\n"; for (double m_alphaMOcoeff : m_alphaMOcoeffs) cout << m_alphaMOcoeff << "\t"; cout << endl << endl; } if (m_betaMOcoeffs.size()) { cout << "Beta MO coefficients:\n"; for (double m_betaMOcoeff : m_betaMOcoeffs) cout << m_betaMOcoeff << "\t"; cout << endl << endl; } } } // namespace Avogadro::QuantumIO