| 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 "nwchemlog.h" |
| 7 |
|
| 8 |
#include <avogadro/core/elements.h> |
| 9 |
#include <avogadro/core/molecule.h> |
| 10 |
#include <avogadro/core/unitcell.h> |
| 11 |
#include <avogadro/core/utilities.h> |
| 12 |
|
| 13 |
#include <iostream> |
| 14 |
|
| 15 |
using std::string; |
| 16 |
using std::vector; |
| 17 |
|
| 18 |
namespace Avogadro::QuantumIO { |
| 19 |
|
| 20 |
using Core::Atom; |
| 21 |
|
| 22 |
NWChemLog::NWChemLog() {} |
| 23 |
|
| 24 |
NWChemLog::~NWChemLog() {} |
| 25 |
|
| 26 |
std::vector<std::string> NWChemLog::fileExtensions() const |
| 27 |
{ |
| 28 |
std::vector<std::string> extensions; |
| 29 |
extensions.emplace_back("nwchem"); |
| 30 |
return extensions; |
| 31 |
} |
| 32 |
|
| 33 |
std::vector<std::string> NWChemLog::mimeTypes() const |
| 34 |
{ |
| 35 |
return std::vector<std::string>(); |
| 36 |
} |
| 37 |
|
| 38 |
bool NWChemLog::read(std::istream& in, Core::Molecule& molecule) |
| 39 |
{ |
| 40 |
// Read the log file line by line, most sections are terminated by an empty |
| 41 |
// line, so they should be retained. |
| 42 |
while (!in.eof()) |
| 43 |
processLine(in, molecule); |
| 44 |
if (0 == molecule.atomCount()) { |
| 45 |
appendError("Could not find any atomic coordinates! Are you sure this is " |
| 46 |
"an NWChem output file?"); |
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
if (m_frequencies.size() > 0 && m_frequencies.size() == m_Lx.size() && |
| 51 |
m_frequencies.size() == m_intensities.size()) { |
| 52 |
molecule.setVibrationFrequencies(m_frequencies); |
| 53 |
molecule.setVibrationIRIntensities(m_intensities); |
| 54 |
molecule.setVibrationLx(m_Lx); |
| 55 |
} |
| 56 |
|
| 57 |
// GaussianSet *basis = new GaussianSet; |
| 58 |
|
| 59 |
// Do simple bond perception. |
| 60 |
molecule.perceiveBondsSimple(); |
| 61 |
molecule.perceiveBondOrders(); |
| 62 |
|
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
void NWChemLog::processLine(std::istream& in, Core::Molecule& mol) |
| 67 |
{ |
| 68 |
// First truncate the line, remove trailing white space and check |
| 69 |
string line; |
| 70 |
if (!getline(in, line) || Core::trimmed(line).empty()) |
| 71 |
return; |
| 72 |
|
| 73 |
string key = Core::trimmed(line); |
| 74 |
|
| 75 |
// Big switch statement checking for various things we are interested in |
| 76 |
if (Core::contains(key, "Output coordinates")) { |
| 77 |
if (mol.atomCount()) |
| 78 |
mol.clearAtoms(); |
| 79 |
|
| 80 |
// get conversion factor |
| 81 |
vector<string> list = Core::split(key, ' '); |
| 82 |
if (list.size() == 11) |
| 83 |
// conversion to Bohr |
| 84 |
m_coordinateScale = Core::lexicalCast<double>(list[6]).value_or(1.0); |
| 85 |
|
| 86 |
readAtoms(in, mol); |
| 87 |
} else if (Core::contains(key, "P.Frequency")) { |
| 88 |
readFrequencies(line, in, mol); |
| 89 |
} else if (Core::contains(key, "Projected Infra")) { |
| 90 |
readIntensities(in, mol); |
| 91 |
} else if (Core::contains(key, "lattice vectors") && |
| 92 |
!Core::contains(key, "reciprocal")) { |
| 93 |
// check the lattic conversion factor |
| 94 |
Real factor = 1.0; |
| 95 |
vector<string> list = Core::split(key, ' '); |
| 96 |
if (list.size() == 11) |
| 97 |
// conversion to Bohr |
| 98 |
factor = Core::lexicalCast<double>(list[6]).value_or(1.0); |
| 99 |
|
| 100 |
readLattice(in, mol, factor); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
void NWChemLog::readAtoms(std::istream& in, Core::Molecule& mol) |
| 105 |
{ |
| 106 |
string line; |
| 107 |
// Skip the next three lines, headers, blanks... |
| 108 |
for (int i = 0; i < 3; ++i) |
| 109 |
if (!getline(in, line)) |
| 110 |
return; |
| 111 |
|
| 112 |
// if m_coordinateScale is 1.0 NWChem output Bohr and we need to convert |
| 113 |
Real scale = 1.0; |
| 114 |
if (m_coordinateScale == 1.0) |
| 115 |
scale = BOHR_TO_ANGSTROM; |
| 116 |
|
| 117 |
while (true) { |
| 118 |
if (!getline(in, line)) |
| 119 |
return; |
| 120 |
vector<string> parts = Core::split(line, ' '); |
| 121 |
// Keep going until the expected number of components is not seen. |
| 122 |
if (parts.size() != 6) |
| 123 |
break; |
| 124 |
|
| 125 |
std::string symbol = parts[1]; |
| 126 |
// upper-case the first character |
| 127 |
symbol[0] = toupper(symbol[0]); |
| 128 |
|
| 129 |
unsigned char element = Core::Elements::atomicNumberFromSymbol(symbol); |
| 130 |
if (element == Avogadro::InvalidElement) { |
| 131 |
appendError("Invalid element encountered: " + parts[1]); |
| 132 |
return; |
| 133 |
} |
| 134 |
Vector3 p; |
| 135 |
for (int i = 0; i < 3; ++i) { |
| 136 |
bool ok = false; |
| 137 |
p[i] = Core::lexicalCast<double>(parts[i + 3], ok); |
| 138 |
if (!ok) { |
| 139 |
appendError("Couldn't convert coordinate component to double: " + |
| 140 |
parts[i + 3]); |
| 141 |
return; |
| 142 |
} |
| 143 |
} |
| 144 |
Core::Atom a = mol.addAtom(element); |
| 145 |
a.setPosition3d(p * scale); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
void NWChemLog::readLattice(std::istream& in, Core::Molecule& mol, |
| 150 |
double factor) |
| 151 |
{ |
| 152 |
Real scale = 1.0; |
| 153 |
if (factor == 1.0) |
| 154 |
scale = BOHR_TO_ANGSTROM; |
| 155 |
|
| 156 |
string line; |
| 157 |
if (!getline(in, line)) |
| 158 |
return; |
| 159 |
|
| 160 |
// next 3 lines should be a1, a2, a3 |
| 161 |
getline(in, line); |
| 162 |
vector<string> parts = Core::split(line, ' '); |
| 163 |
if (parts.size() != 5) |
| 164 |
return; |
| 165 |
Vector3 a1; |
| 166 |
a1[0] = Core::lexicalCast<double>(parts[1]).value_or(0.0); |
| 167 |
a1[1] = Core::lexicalCast<double>(parts[2]).value_or(0.0); |
| 168 |
a1[2] = Core::lexicalCast<double>(parts[3]).value_or(0.0); |
| 169 |
|
| 170 |
getline(in, line); |
| 171 |
parts = Core::split(line, ' '); |
| 172 |
if (parts.size() != 5) |
| 173 |
return; |
| 174 |
Vector3 a2; |
| 175 |
a2[0] = Core::lexicalCast<double>(parts[1]).value_or(0.0); |
| 176 |
a2[1] = Core::lexicalCast<double>(parts[2]).value_or(0.0); |
| 177 |
a2[2] = Core::lexicalCast<double>(parts[3]).value_or(0.0); |
| 178 |
|
| 179 |
getline(in, line); |
| 180 |
parts = Core::split(line, ' '); |
| 181 |
if (parts.size() != 5) |
| 182 |
return; |
| 183 |
Vector3 a3; |
| 184 |
a3[0] = Core::lexicalCast<double>(parts[1]).value_or(0.0); |
| 185 |
a3[1] = Core::lexicalCast<double>(parts[2]).value_or(0.0); |
| 186 |
a3[2] = Core::lexicalCast<double>(parts[3]).value_or(0.0); |
| 187 |
|
| 188 |
auto* cell = new Core::UnitCell(a1 * scale, a2 * scale, a3 * scale); |
| 189 |
mol.setUnitCell(cell); |
| 190 |
} |
| 191 |
|
| 192 |
void NWChemLog::readFrequencies(const std::string& firstLine, std::istream& in, |
| 193 |
Core::Molecule&) |
| 194 |
{ |
| 195 |
string line = firstLine; |
| 196 |
bool ok = false; |
| 197 |
vector<string> parts = Core::split(firstLine, ' '); |
| 198 |
if (parts.size() < 2) |
| 199 |
return; |
| 200 |
|
| 201 |
vector<double> frequencies; |
| 202 |
|
| 203 |
for (size_t i = 1; i < parts.size(); ++i) |
| 204 |
frequencies.push_back(Core::lexicalCast<double>(parts[i], ok)); |
| 205 |
if (!ok) { |
| 206 |
appendError("Error reading frequencies: " + firstLine); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
// Skip the blank line after the frequencies. |
| 211 |
if (!getline(in, line)) |
| 212 |
return; |
| 213 |
if (!getline(in, line)) |
| 214 |
return; |
| 215 |
parts = Core::split(line, ' '); |
| 216 |
if (parts.size() < 2) |
| 217 |
return; |
| 218 |
|
| 219 |
vector<vector<double>> cols; |
| 220 |
cols.resize(parts.size() - 1); |
| 221 |
|
| 222 |
// Main block of numbers. |
| 223 |
while (parts.size() >= 2) { |
| 224 |
for (size_t i = 1; i < parts.size(); ++i) { |
| 225 |
cols[i - 1].push_back(Core::lexicalCast<double>(parts[i], ok)); |
| 226 |
if (!ok) { |
| 227 |
appendError("Couldn't convert " + parts[i] + " to double."); |
| 228 |
return; |
| 229 |
} |
| 230 |
} |
| 231 |
if (!getline(in, line)) |
| 232 |
return; |
| 233 |
parts = Core::split(line, ' '); |
| 234 |
} |
| 235 |
for (size_t i = 0; i < frequencies.size(); ++i) { |
| 236 |
m_frequencies.push_back(frequencies[i]); |
| 237 |
Core::Array<Vector3> Lx; |
| 238 |
for (size_t j = 0; j < cols[i].size(); j += 3) { |
| 239 |
Lx.push_back(Vector3(cols[i][j + 0], cols[i][j + 1], cols[i][j + 2])); |
| 240 |
} |
| 241 |
m_Lx.push_back(Lx); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
void NWChemLog::readIntensities(std::istream& in, Core::Molecule&) |
| 246 |
{ |
| 247 |
string line; |
| 248 |
bool ok = false; |
| 249 |
// Skip the next two lines, headers, blanks... |
| 250 |
for (int i = 0; i < 2; ++i) |
| 251 |
if (!getline(in, line)) |
| 252 |
return; |
| 253 |
|
| 254 |
while (true) { |
| 255 |
if (!getline(in, line)) |
| 256 |
return; |
| 257 |
vector<string> parts = Core::split(line, ' '); |
| 258 |
// Keep going until the expected number of components is not seen. |
| 259 |
if (parts.size() != 7) |
| 260 |
break; |
| 261 |
m_intensities.push_back(Core::lexicalCast<double>(parts[5], ok)); |
| 262 |
if (!ok) { |
| 263 |
appendError("Couldn't convert " + parts[5] + " to double."); |
| 264 |
return; |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
} // namespace Avogadro::QuantumIO |