| 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 "gamessus.h" |
| 7 |
|
| 8 |
#include <avogadro/core/molecule.h> |
| 9 |
#include <avogadro/core/utilities.h> |
| 10 |
|
| 11 |
#include <iostream> |
| 12 |
|
| 13 |
using std::cout; |
| 14 |
using std::endl; |
| 15 |
using std::string; |
| 16 |
using std::vector; |
| 17 |
|
| 18 |
namespace Avogadro::QuantumIO { |
| 19 |
|
| 20 |
using Core::Atom; |
| 21 |
using Core::BasisSet; |
| 22 |
using Core::GaussianSet; |
| 23 |
using Core::Rhf; |
| 24 |
using Core::Rohf; |
| 25 |
using Core::Uhf; |
| 26 |
|
| 27 |
GAMESSUSOutput::GAMESSUSOutput() : m_coordFactor(1.0), m_scftype(Rhf) {} |
| 28 |
|
| 29 |
GAMESSUSOutput::~GAMESSUSOutput() {} |
| 30 |
|
| 31 |
std::vector<std::string> GAMESSUSOutput::fileExtensions() const |
| 32 |
{ |
| 33 |
std::vector<std::string> extensions; |
| 34 |
extensions.emplace_back("gamout"); |
| 35 |
extensions.emplace_back("gamess"); |
| 36 |
return extensions; |
| 37 |
} |
| 38 |
|
| 39 |
std::vector<std::string> GAMESSUSOutput::mimeTypes() const |
| 40 |
{ |
| 41 |
return std::vector<std::string>(); |
| 42 |
} |
| 43 |
|
| 44 |
bool GAMESSUSOutput::read(std::istream& in, Core::Molecule& molecule) |
| 45 |
{ |
| 46 |
// Read the log file line by line, most sections are terminated by an empty |
| 47 |
// line, so they should be retained. |
| 48 |
string buffer; |
| 49 |
while (getline(in, buffer)) { |
| 50 |
if (Core::contains(buffer, "COORDINATES (BOHR)")) { |
| 51 |
readAtomBlock(in, molecule, false); |
| 52 |
} else if (Core::contains(buffer, "COORDINATES OF ALL ATOMS ARE (ANGS)")) { |
| 53 |
readAtomBlock(in, molecule, true); |
| 54 |
} else if (Core::contains(buffer, "ATOMIC BASIS SET")) { |
| 55 |
readBasisSet(in); |
| 56 |
} else if (Core::contains(buffer, "CHARGE OF MOLECULE")) { |
| 57 |
vector<string> parts = Core::split(buffer, '='); |
| 58 |
if (parts.size() == 2) |
| 59 |
molecule.setData("totalCharge", |
| 60 |
Core::lexicalCast<int>(parts[1]).value_or(0)); |
| 61 |
} else if (Core::contains(buffer, "SPIN MULTIPLICITY")) { |
| 62 |
vector<string> parts = Core::split(buffer, '='); |
| 63 |
if (parts.size() == 2) |
| 64 |
molecule.setData("totalSpinMultiplicity", |
| 65 |
Core::lexicalCast<int>(parts[1]).value_or(1)); |
| 66 |
} else if (Core::contains(buffer, "NUMBER OF ELECTRONS")) { |
| 67 |
vector<string> parts = Core::split(buffer, '='); |
| 68 |
if (parts.size() == 2) |
| 69 |
m_electrons = Core::lexicalCast<int>(parts[1]).value_or(0); |
| 70 |
else |
| 71 |
cout << "error" << buffer << endl; |
| 72 |
} |
| 73 |
/*else if (Core::contains(buffer, "NUMBER OF OCCUPIED ORBITALS (ALPHA)")) { |
| 74 |
cout << "Found alpha orbitals\n"; |
| 75 |
} |
| 76 |
else if (Core::contains(buffer, "NUMBER OF OCCUPIED ORBITALS (BETA )")) { |
| 77 |
cout << "Found alpha orbitals\n"; |
| 78 |
} |
| 79 |
else if (Core::contains(buffer, "SCFTYP=")) { |
| 80 |
cout << "Found SCF type\n"; |
| 81 |
}*/ |
| 82 |
else if (Core::contains(buffer, "EIGENVECTORS")) { |
| 83 |
readEigenvectors(in); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// f functions and beyond need to be reordered |
| 88 |
reorderMOs(); |
| 89 |
|
| 90 |
molecule.perceiveBondsSimple(); |
| 91 |
molecule.perceiveBondOrders(); |
| 92 |
auto* basis = new GaussianSet; |
| 93 |
load(basis); |
| 94 |
molecule.setBasisSet(basis); |
| 95 |
basis->setMolecule(&molecule); |
| 96 |
|
| 97 |
// outputAll(); |
| 98 |
|
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
void GAMESSUSOutput::readAtomBlock(std::istream& in, Core::Molecule& molecule, |
| 103 |
bool angs) |
| 104 |
{ |
| 105 |
// We read the atom block in until it terminates with a blank line. |
| 106 |
double coordFactor = angs ? 1.0 : BOHR_TO_ANGSTROM_D; |
| 107 |
string buffer; |
| 108 |
|
| 109 |
bool atomsExist = molecule.atomCount() > 0; |
| 110 |
Index index = 0; |
| 111 |
//@TODO - store all the coordinates |
| 112 |
while (getline(in, buffer)) { |
| 113 |
if (Core::contains(buffer, "CHARGE") || Core::contains(buffer, "------")) |
| 114 |
continue; |
| 115 |
else if (buffer.length() == 0 || buffer == "\n") // Our work here is done. |
| 116 |
return; |
| 117 |
vector<string> parts = Core::split(buffer, ' '); |
| 118 |
if (parts.size() != 5) { |
| 119 |
appendError("Poorly formed atom line: " + buffer); |
| 120 |
return; |
| 121 |
} |
| 122 |
bool ok(false); |
| 123 |
Vector3 pos; |
| 124 |
auto atomicNumber( |
| 125 |
static_cast<unsigned char>(Core::lexicalCast<int>(parts[1], ok))); |
| 126 |
if (!ok) |
| 127 |
appendError("Failed to cast to int for atomic number: " + parts[1]); |
| 128 |
pos.x() = Core::lexicalCast<Real>(parts[2], ok) * coordFactor; |
| 129 |
if (!ok) |
| 130 |
appendError("Failed to cast to double for position: " + parts[2]); |
| 131 |
pos.y() = Core::lexicalCast<Real>(parts[3], ok) * coordFactor; |
| 132 |
if (!ok) |
| 133 |
appendError("Failed to cast to double for position: " + parts[3]); |
| 134 |
pos.z() = Core::lexicalCast<Real>(parts[4], ok) * coordFactor; |
| 135 |
if (!ok) |
| 136 |
appendError("Failed to cast to double for position: " + parts[4]); |
| 137 |
|
| 138 |
Atom atom; |
| 139 |
if (!atomsExist) { |
| 140 |
atom = molecule.addAtom(atomicNumber, pos); |
| 141 |
} else { |
| 142 |
atom = molecule.atom(index); |
| 143 |
atom.setPosition3d(pos); |
| 144 |
index++; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
void GAMESSUSOutput::readBasisSet(std::istream& in) |
| 150 |
{ |
| 151 |
// Basic strategy is to use the number of parts in a line to determine the |
| 152 |
// type, where atom has 1 part, and a GTO has 5 (or 6 for SP/L). Termination |
| 153 |
// of the block when we hit the summary information at the end. |
| 154 |
string buffer; |
| 155 |
int currentAtom(0); |
| 156 |
bool header(true); |
| 157 |
while (getline(in, buffer)) { |
| 158 |
if (header) { // Skip the header lines until we hit the last header line. |
| 159 |
if (Core::contains(buffer, "SHELL")) |
| 160 |
header = false; |
| 161 |
continue; |
| 162 |
} |
| 163 |
vector<string> parts = Core::split(buffer, ' '); |
| 164 |
if (Core::contains(buffer, "TOTAL NUMBER OF BASIS SET SHELLS")) { |
| 165 |
// End of the basis set block. |
| 166 |
return; |
| 167 |
} else if (parts.size() == 1) { |
| 168 |
// Currently just incrememt the current atom, we should probably at least |
| 169 |
// verify the element matches in the future too. |
| 170 |
++currentAtom; |
| 171 |
} else if (parts.size() == 5 || parts.size() == 6) { |
| 172 |
if (parts[1].size() != 1) { |
| 173 |
appendError("Error parsing basis set line, unrecognized type" + |
| 174 |
parts[1]); |
| 175 |
continue; |
| 176 |
} |
| 177 |
// Determine the shell type. |
| 178 |
GaussianSet::orbital shellType(GaussianSet::UU); |
| 179 |
switch (parts[1][0]) { |
| 180 |
case 'S': |
| 181 |
shellType = GaussianSet::S; |
| 182 |
break; |
| 183 |
case 'L': |
| 184 |
shellType = GaussianSet::SP; |
| 185 |
break; |
| 186 |
case 'P': |
| 187 |
shellType = GaussianSet::P; |
| 188 |
break; |
| 189 |
case 'D': |
| 190 |
shellType = GaussianSet::D; |
| 191 |
break; |
| 192 |
case 'F': |
| 193 |
shellType = GaussianSet::F; |
| 194 |
break; |
| 195 |
default: |
| 196 |
shellType = GaussianSet::UU; |
| 197 |
appendError("Unrecognized shell type: " + parts[1]); |
| 198 |
} |
| 199 |
// Read in the rest of the shell, terminate when the number of tokens |
| 200 |
// is not 5 or 6 in a line. |
| 201 |
int numGTOs(0); |
| 202 |
while (parts.size() == 5 || parts.size() == 6) { |
| 203 |
++numGTOs; |
| 204 |
m_a.push_back(Core::lexicalCast<double>(parts[3]).value_or(0.0)); |
| 205 |
m_c.push_back(Core::lexicalCast<double>(parts[4]).value_or(0.0)); |
| 206 |
if (shellType == GaussianSet::SP && parts.size() == 6) |
| 207 |
m_csp.push_back(Core::lexicalCast<double>(parts[5]).value_or(0.0)); |
| 208 |
if (!getline(in, buffer)) |
| 209 |
break; |
| 210 |
parts = Core::split(buffer, ' '); |
| 211 |
} |
| 212 |
// Now add this to our data structure. |
| 213 |
m_shellNums.push_back(numGTOs); |
| 214 |
m_shellTypes.push_back(shellType); |
| 215 |
m_shelltoAtom.push_back(currentAtom); |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
void GAMESSUSOutput::readEigenvectors(std::istream& in) |
| 221 |
{ |
| 222 |
string buffer; |
| 223 |
getline(in, buffer); |
| 224 |
getline(in, buffer); |
| 225 |
getline(in, buffer); |
| 226 |
vector<string> parts = Core::split(buffer, ' '); |
| 227 |
vector<vector<double>> eigenvectors; |
| 228 |
bool ok(false); |
| 229 |
size_t numberOfMos(0); |
| 230 |
bool newBlock(true); |
| 231 |
while (!Core::contains(buffer, "END OF") || |
| 232 |
Core::contains(buffer, "--------")) { |
| 233 |
// Any line with actual information in it will contain >= 5 parts. |
| 234 |
if (parts.size() > 5 && buffer.substr(0, 16) != " ") { |
| 235 |
if (newBlock) { |
| 236 |
// Reorder the columns/rows, add them and then prepare |
| 237 |
for (auto& eigenvector : eigenvectors) |
| 238 |
for (double j : eigenvector) |
| 239 |
m_MOcoeffs.push_back(j); |
| 240 |
eigenvectors.clear(); |
| 241 |
eigenvectors.resize(parts.size() - 4); |
| 242 |
numberOfMos += eigenvectors.size(); |
| 243 |
newBlock = false; |
| 244 |
} |
| 245 |
for (size_t i = 0; i < parts.size() - 4; ++i) { |
| 246 |
eigenvectors[i].push_back(Core::lexicalCast<double>(parts[i + 4], ok)); |
| 247 |
if (!ok) |
| 248 |
appendError("Failed to cast to double for eigenvector: " + parts[i]); |
| 249 |
} |
| 250 |
} else { |
| 251 |
// Note that we are either ending or entering a new block of orbitals. |
| 252 |
newBlock = true; |
| 253 |
} |
| 254 |
if (!getline(in, buffer)) |
| 255 |
break; |
| 256 |
parts = Core::split(buffer, ' '); |
| 257 |
} |
| 258 |
m_nMOs = numberOfMos; |
| 259 |
for (auto& eigenvector : eigenvectors) |
| 260 |
for (double j : eigenvector) |
| 261 |
m_MOcoeffs.push_back(j); |
| 262 |
|
| 263 |
// Now we just need to transpose the matrix, as GAMESS uses a different order. |
| 264 |
// We know the number of columns (MOs), and the number of rows (primitives). |
| 265 |
if (eigenvectors.size() != numberOfMos * m_a.size()) { |
| 266 |
appendError("Incorrect number of eigenvectors loaded."); |
| 267 |
return; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
void GAMESSUSOutput::load(GaussianSet* basis) |
| 272 |
{ |
| 273 |
// Now load up our basis set |
| 274 |
basis->setElectronCount(m_electrons); |
| 275 |
|
| 276 |
// Set up the GTO primitive counter, go through the shells and add them |
| 277 |
int nGTO = 0; |
| 278 |
int nSP = 0; // number of SP shells |
| 279 |
for (unsigned int i = 0; i < m_shellTypes.size(); ++i) { |
| 280 |
// Handle the SP case separately - this should possibly be a distinct type |
| 281 |
if (m_shellTypes.at(i) == GaussianSet::SP) { |
| 282 |
// SP orbital type - currently have to unroll into two shells |
| 283 |
int tmpGTO = nGTO; |
| 284 |
int s = basis->addBasis(m_shelltoAtom.at(i) - 1, GaussianSet::S); |
| 285 |
for (int j = 0; j < m_shellNums.at(i); ++j) { |
| 286 |
basis->addGto(s, m_c.at(nGTO), m_a.at(nGTO)); |
| 287 |
++nGTO; |
| 288 |
} |
| 289 |
int p = basis->addBasis(m_shelltoAtom.at(i) - 1, GaussianSet::P); |
| 290 |
for (int j = 0; j < m_shellNums.at(i); ++j) { |
| 291 |
basis->addGto(p, m_csp.at(nSP), m_a.at(tmpGTO)); |
| 292 |
++tmpGTO; |
| 293 |
++nSP; |
| 294 |
} |
| 295 |
} else { |
| 296 |
int b = basis->addBasis(m_shelltoAtom.at(i) - 1, m_shellTypes.at(i)); |
| 297 |
for (int j = 0; j < m_shellNums.at(i); ++j) { |
| 298 |
basis->addGto(b, m_c.at(nGTO), m_a.at(nGTO)); |
| 299 |
++nGTO; |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
// qDebug() << " loading MOs " << m_MOcoeffs.size(); |
| 304 |
|
| 305 |
// Now to load in the MO coefficients |
| 306 |
if (m_MOcoeffs.size()) |
| 307 |
basis->setMolecularOrbitals(m_MOcoeffs); |
| 308 |
if (m_alphaMOcoeffs.size()) |
| 309 |
basis->setMolecularOrbitals(m_alphaMOcoeffs, BasisSet::Alpha); |
| 310 |
if (m_betaMOcoeffs.size()) |
| 311 |
basis->setMolecularOrbitals(m_betaMOcoeffs, BasisSet::Beta); |
| 312 |
|
| 313 |
// generateDensity(); |
| 314 |
// if (m_density.rows()) |
| 315 |
// basis->setDensityMatrix(m_density); |
| 316 |
|
| 317 |
basis->setScfType(m_scftype); |
| 318 |
} |
| 319 |
|
| 320 |
void GAMESSUSOutput::reorderMOs() |
| 321 |
{ |
| 322 |
unsigned int GTOcounter = 0; |
| 323 |
for (int iMO = 0; iMO < m_nMOs; iMO++) { |
| 324 |
// loop over the basis set shells |
| 325 |
for (auto& m_shellType : m_shellTypes) { |
| 326 |
// The angular momentum of the shell |
| 327 |
// determines the number of primitive GTOs. |
| 328 |
// GAMESS always prints the full cartesian set. |
| 329 |
double yyy, zzz, xxy, xxz, yyx, yyz, zzx, zzy, xyz; |
| 330 |
unsigned int nPrimGTOs = 0; |
| 331 |
switch (m_shellType) { |
| 332 |
case GaussianSet::S: |
| 333 |
nPrimGTOs = 1; |
| 334 |
GTOcounter += nPrimGTOs; |
| 335 |
break; |
| 336 |
case GaussianSet::P: |
| 337 |
nPrimGTOs = 3; |
| 338 |
GTOcounter += nPrimGTOs; |
| 339 |
break; |
| 340 |
// L? |
| 341 |
case GaussianSet::D: |
| 342 |
nPrimGTOs = 6; |
| 343 |
GTOcounter += nPrimGTOs; |
| 344 |
break; |
| 345 |
case GaussianSet::F: |
| 346 |
nPrimGTOs = 10; |
| 347 |
// f functions are the first set to be reordered. |
| 348 |
// double xxx = m_MOcoeffs.at(GTOcounter); |
| 349 |
yyy = m_MOcoeffs.at(GTOcounter + 1); |
| 350 |
zzz = m_MOcoeffs.at(GTOcounter + 2); |
| 351 |
xxy = m_MOcoeffs.at(GTOcounter + 3); |
| 352 |
xxz = m_MOcoeffs.at(GTOcounter + 4); |
| 353 |
yyx = m_MOcoeffs.at(GTOcounter + 5); |
| 354 |
yyz = m_MOcoeffs.at(GTOcounter + 6); |
| 355 |
zzx = m_MOcoeffs.at(GTOcounter + 7); |
| 356 |
zzy = m_MOcoeffs.at(GTOcounter + 8); |
| 357 |
xyz = m_MOcoeffs.at(GTOcounter + 9); |
| 358 |
// xxx is unchanged |
| 359 |
m_MOcoeffs.at(GTOcounter + 1) = xxy; // xxy |
| 360 |
m_MOcoeffs.at(GTOcounter + 2) = xxz; // xxz |
| 361 |
m_MOcoeffs.at(GTOcounter + 3) = yyx; // xyy |
| 362 |
m_MOcoeffs.at(GTOcounter + 4) = xyz; // xyz |
| 363 |
m_MOcoeffs.at(GTOcounter + 5) = zzx; // xzz |
| 364 |
m_MOcoeffs.at(GTOcounter + 6) = yyy; // yyy |
| 365 |
m_MOcoeffs.at(GTOcounter + 7) = yyz; // yyz |
| 366 |
m_MOcoeffs.at(GTOcounter + 8) = zzy; // yzz |
| 367 |
m_MOcoeffs.at(GTOcounter + 9) = zzz; // zzz |
| 368 |
|
| 369 |
GTOcounter += nPrimGTOs; |
| 370 |
break; |
| 371 |
case GaussianSet::G: |
| 372 |
nPrimGTOs = 15; |
| 373 |
GTOcounter += nPrimGTOs; |
| 374 |
break; |
| 375 |
case GaussianSet::H: |
| 376 |
nPrimGTOs = 21; |
| 377 |
GTOcounter += nPrimGTOs; |
| 378 |
break; |
| 379 |
case GaussianSet::I: |
| 380 |
nPrimGTOs = 28; |
| 381 |
GTOcounter += nPrimGTOs; |
| 382 |
break; |
| 383 |
default: |
| 384 |
cout << "Basis set not handled - results may be incorrect.\n"; |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
void GAMESSUSOutput::outputAll() |
| 391 |
{ |
| 392 |
switch (m_scftype) { |
| 393 |
case Rhf: |
| 394 |
cout << "SCF type = RHF" << endl; |
| 395 |
break; |
| 396 |
case Uhf: |
| 397 |
cout << "SCF type = UHF" << endl; |
| 398 |
break; |
| 399 |
case Rohf: |
| 400 |
cout << "SCF type = ROHF" << endl; |
| 401 |
break; |
| 402 |
default: |
| 403 |
cout << "SCF typ = Unknown" << endl; |
| 404 |
} |
| 405 |
cout << "Shell mappings\n"; |
| 406 |
for (unsigned int i = 0; i < m_shellTypes.size(); ++i) { |
| 407 |
cout << i << ": type = " << m_shellTypes.at(i) |
| 408 |
<< ", number = " << m_shellNums.at(i) |
| 409 |
<< ", atom = " << m_shelltoAtom.at(i) << endl; |
| 410 |
} |
| 411 |
int nGTOs = 0; |
| 412 |
if (m_MOcoeffs.size()) { |
| 413 |
nGTOs = m_MOcoeffs.size() / m_nMOs; |
| 414 |
cout << m_nMOs << " MOs, " << nGTOs << " GTOs" << endl; |
| 415 |
} |
| 416 |
|
| 417 |
for (unsigned int iMO = 0; iMO < 10; iMO++) { |
| 418 |
for (unsigned int i = iMO * nGTOs; i < nGTOs * iMO + 10; |
| 419 |
++i) // m_MOcoeffs.size(); ++i) |
| 420 |
cout << m_MOcoeffs.at(i) << "\t"; |
| 421 |
cout << "\n"; |
| 422 |
} |
| 423 |
|
| 424 |
if (m_alphaMOcoeffs.size()) |
| 425 |
cout << "Alpha MO coefficients.\n"; |
| 426 |
for (double m_alphaMOcoeff : m_alphaMOcoeffs) |
| 427 |
cout << m_alphaMOcoeff; |
| 428 |
if (m_betaMOcoeffs.size()) |
| 429 |
cout << "Beta MO coefficients.\n"; |
| 430 |
for (double m_betaMOcoeff : m_betaMOcoeffs) |
| 431 |
cout << m_betaMOcoeff; |
| 432 |
cout << std::flush; |
| 433 |
} |
| 434 |
} // namespace Avogadro::QuantumIO |