| 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 "xyzformat.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 |
#include <avogadro/core/vector.h> |
| 13 |
|
| 14 |
#include <nlohmann/json.hpp> |
| 15 |
|
| 16 |
#include <iomanip> |
| 17 |
#include <iostream> |
| 18 |
#include <sstream> |
| 19 |
#include <string> |
| 20 |
|
| 21 |
using json = nlohmann::json; |
| 22 |
|
| 23 |
using std::endl; |
| 24 |
using std::getline; |
| 25 |
using std::string; |
| 26 |
|
| 27 |
namespace Avogadro::Io { |
| 28 |
|
| 29 |
using Core::Array; |
| 30 |
using Core::Atom; |
| 31 |
using Core::Elements; |
| 32 |
using Core::lexicalCast; |
| 33 |
using Core::split; |
| 34 |
using Core::trimmed; |
| 35 |
|
| 36 |
#ifndef _WIN32 |
| 37 |
using std::isalpha; |
| 38 |
#endif |
| 39 |
|
| 40 |
std::optional<double> findEnergy(const std::string& buffer) |
| 41 |
{ |
| 42 |
// Check for energy in the comment line |
| 43 |
// orca uses E -680.044112849966 (with spaces) |
| 44 |
// xtb uses energy: -680.044112849966 |
| 45 |
// Open Babel uses Energy: -680.044112849966 |
| 46 |
// crest uses Epot = -680.044112849966 |
| 47 |
std::size_t energyStart = buffer.find("energy:"); |
| 48 |
std::size_t offset = 7; |
| 49 |
if (energyStart == std::string::npos) { |
| 50 |
energyStart = buffer.find("Energy:"); |
| 51 |
} |
| 52 |
if (energyStart == std::string::npos) { |
| 53 |
energyStart = buffer.find(" E "); |
| 54 |
offset = 3; |
| 55 |
} |
| 56 |
if (energyStart == std::string::npos) { |
| 57 |
energyStart = buffer.find("Epot = "); |
| 58 |
offset = 6; |
| 59 |
} |
| 60 |
|
| 61 |
if (energyStart != std::string::npos) { |
| 62 |
// pick the next token |
| 63 |
std::string energy = buffer.substr(energyStart + offset); |
| 64 |
return lexicalCast<double>(energy); |
| 65 |
} |
| 66 |
|
| 67 |
// also check if the comment just starts with a number |
| 68 |
// try to split out the first token and look for a number |
| 69 |
std::vector<string> tokens(split(buffer, ' ')); |
| 70 |
if (!tokens.empty()) { |
| 71 |
return lexicalCast<double>(tokens[0]); |
| 72 |
} |
| 73 |
|
| 74 |
return std::nullopt; |
| 75 |
} |
| 76 |
|
| 77 |
bool XyzFormat::read(std::istream& inStream, Core::Molecule& mol) |
| 78 |
{ |
| 79 |
json opts; |
| 80 |
if (!options().empty()) |
| 81 |
opts = json::parse(options(), nullptr, false); |
| 82 |
else |
| 83 |
opts = json::object(); |
| 84 |
|
| 85 |
size_t numAtoms = 0; |
| 86 |
if (!(inStream >> numAtoms)) { |
| 87 |
appendError("Error parsing number of atoms."); |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
string buffer; |
| 92 |
getline(inStream, buffer); // Finish the first line |
| 93 |
if (!inStream.good()) { |
| 94 |
appendError("Error reading first line."); |
| 95 |
return false; |
| 96 |
} |
| 97 |
getline(inStream, buffer); // comment or name or energy |
| 98 |
if (!buffer.empty()) |
| 99 |
mol.setData("name", trimmed(buffer)); |
| 100 |
|
| 101 |
std::vector<double> energies; |
| 102 |
if (auto energy = findEnergy(buffer)) { |
| 103 |
mol.setData("totalEnergy", *energy); |
| 104 |
energies.push_back(*energy); |
| 105 |
} |
| 106 |
|
| 107 |
// check for Lattice= in an extended XYZ from ASE and company |
| 108 |
// e.g. Lattice="H11 H21 H31 H12 H22 H32 H13 H23 H33" |
| 109 |
// https://atomsk.univ-lille.fr/doc/en/format_xyz.html |
| 110 |
// https://gitlab.com/ase/ase/-/merge_requests/62 |
| 111 |
std::size_t start = buffer.find("Lattice="); |
| 112 |
if (start != std::string::npos) { |
| 113 |
// step through bit by bit until we hit the next quote character |
| 114 |
start = start + 8; |
| 115 |
// skip over the first quote |
| 116 |
if (buffer[start] == '\"') { |
| 117 |
start++; |
| 118 |
} |
| 119 |
std::size_t end = buffer.find('\"', start); |
| 120 |
std::string lattice = buffer.substr(start, (end - start)); |
| 121 |
|
| 122 |
std::vector<string> tokens(split(lattice, ' ')); |
| 123 |
|
| 124 |
// check for size |
| 125 |
std::cout << "Lattice size: " << tokens.size() << std::endl; |
| 126 |
|
| 127 |
if (tokens.size() >= 9) { |
| 128 |
if (auto tmp = lexicalCast<double>(tokens.begin(), tokens.begin() + 9)) { |
| 129 |
Vector3 v1(tmp->at(0), tmp->at(1), tmp->at(2)); |
| 130 |
Vector3 v2(tmp->at(3), tmp->at(4), tmp->at(5)); |
| 131 |
Vector3 v3(tmp->at(6), tmp->at(7), tmp->at(8)); |
| 132 |
|
| 133 |
auto* cell = new Core::UnitCell(v1, v2, v3); |
| 134 |
std::cout << " Lattice: " << cell->aVector() << " " << cell->bVector() |
| 135 |
<< " " << cell->cVector() << std::endl; |
| 136 |
if (!cell->isRegular()) { |
| 137 |
appendError("Lattice vectors are not linear independent"); |
| 138 |
delete cell; |
| 139 |
} else { |
| 140 |
mol.setUnitCell(cell); |
| 141 |
} |
| 142 |
} else { |
| 143 |
appendError("Lattice vectors are malformed"); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
// check to see if there's an extended XYZ Properties= line |
| 148 |
// e.g. Properties=species:S:1:pos:R:3 |
| 149 |
// https://gitlab.com/ase/ase/-/merge_requests/62 |
| 150 |
start = buffer.find("Properties="); |
| 151 |
unsigned int chargeColumn = 0; |
| 152 |
unsigned int forceColumn = 0; |
| 153 |
std::vector<double> charges; |
| 154 |
if (start != std::string::npos) { |
| 155 |
start = start + 11; // skip over "Properties=" |
| 156 |
unsigned int stop = buffer.find(' ', start); |
| 157 |
unsigned int length = stop - start; |
| 158 |
// we want to track columns after the position |
| 159 |
// (esp. charge, spin, force, velocity, etc.) |
| 160 |
std::string properties = buffer.substr(start, length); |
| 161 |
std::vector<string> tokens(split(properties, ':')); |
| 162 |
unsigned int column = 0; |
| 163 |
for (size_t i = 0; i < tokens.size(); i += 3) { |
| 164 |
// we can safely assume species and pos are present |
| 165 |
if (tokens[i] == "charge") { |
| 166 |
chargeColumn = column; |
| 167 |
} else if (tokens[i] == "force" || tokens[i] == "forces") { |
| 168 |
forceColumn = column; |
| 169 |
} // TODO other properties (velocity, spin, selection, etc.) |
| 170 |
|
| 171 |
// increment column based on the count of the property |
| 172 |
if (i + 2 < tokens.size()) { |
| 173 |
if (auto c = lexicalCast<unsigned int>(tokens[i + 2])) { |
| 174 |
column += *c; |
| 175 |
} else { |
| 176 |
appendError("Error reading property column: " + tokens[i + 2]); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
if (!inStream.good()) { |
| 183 |
appendError("Error reading comment line."); |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
// Parse atoms |
| 188 |
for (size_t i = 0; i < numAtoms; ++i) { |
| 189 |
getline(inStream, buffer); |
| 190 |
if (buffer.empty()) { |
| 191 |
appendError("Error reading atom at index " + std::to_string(i) + "."); |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
std::vector<string> tokens; |
| 196 |
// check for tabs PR#1512 |
| 197 |
if (buffer.find('\t') != std::string::npos) |
| 198 |
tokens = split(buffer, '\t'); |
| 199 |
else |
| 200 |
tokens = split(buffer, ' '); |
| 201 |
|
| 202 |
if (tokens.size() < 4) { |
| 203 |
appendError("Not enough tokens in this line: " + buffer); |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
unsigned char atomicNum(0); |
| 208 |
if (isalpha(tokens[0][0])) { |
| 209 |
atomicNum = Elements::atomicNumberFromSymbol(tokens[0]); |
| 210 |
if (tokens[0] == "D") |
| 211 |
atomicNum = 1; |
| 212 |
else if (tokens[0] == "T") |
| 213 |
atomicNum = 1; |
| 214 |
} else |
| 215 |
atomicNum = static_cast<unsigned char>( |
| 216 |
lexicalCast<short int>(tokens[0]).value_or(0)); |
| 217 |
|
| 218 |
Vector3 pos; |
| 219 |
if (auto tmp = |
| 220 |
lexicalCast<double>(tokens.begin() + 1, tokens.begin() + 4)) { |
| 221 |
pos << tmp->at(0), tmp->at(1), tmp->at(2); |
| 222 |
} else { |
| 223 |
appendError("Error reading atom position"); |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
Atom newAtom = mol.addAtom(atomicNum); |
| 228 |
if (atomicNum == 1 && tokens[0] == "D") |
| 229 |
newAtom.setIsotope(2); |
| 230 |
else if (atomicNum == 1 && tokens[0] == "T") |
| 231 |
newAtom.setIsotope(3); |
| 232 |
newAtom.setPosition3d(pos); |
| 233 |
|
| 234 |
// check for charge and force columns |
| 235 |
if (chargeColumn > 0 && chargeColumn < tokens.size()) { |
| 236 |
if (auto c = lexicalCast<double>(tokens[chargeColumn])) { |
| 237 |
charges.push_back(*c); |
| 238 |
} else { |
| 239 |
appendError("Error reading charge"); |
| 240 |
} |
| 241 |
// we set the charges after all atoms are added |
| 242 |
} |
| 243 |
if (forceColumn > 0 && forceColumn < tokens.size()) { |
| 244 |
if (auto tmp = lexicalCast<double>(tokens.begin() + forceColumn, |
| 245 |
tokens.begin() + forceColumn + 3)) { |
| 246 |
Vector3 force(tmp->at(0), tmp->at(1), tmp->at(2)); |
| 247 |
newAtom.setForceVector(force); |
| 248 |
} else { |
| 249 |
appendError("Error reading force"); |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
// Check that all atoms were handled. |
| 255 |
if (mol.atomCount() != numAtoms) { |
| 256 |
std::ostringstream errorStream; |
| 257 |
errorStream << "Error parsing atom at index " << mol.atomCount() |
| 258 |
<< " (line " << 3 + mol.atomCount() << ").\n" |
| 259 |
<< buffer; |
| 260 |
appendError(errorStream.str()); |
| 261 |
return false; |
| 262 |
} |
| 263 |
|
| 264 |
// Do we have an animation? |
| 265 |
// check if the next frame has the same number of atoms |
| 266 |
getline(inStream, buffer); // should be the number of atoms |
| 267 |
if (buffer.size() == 0 || buffer[0] == '>') { |
| 268 |
getline(inStream, buffer); // Orca 6 prints ">" separators |
| 269 |
} |
| 270 |
|
| 271 |
auto numAtoms2 = lexicalCast<int>(buffer); |
| 272 |
if (numAtoms2 && *numAtoms2 > 0 && |
| 273 |
numAtoms == static_cast<size_t>(*numAtoms2)) { |
| 274 |
getline(inStream, buffer); // comment line |
| 275 |
// check for properties in the comment line |
| 276 |
if (auto energy = findEnergy(buffer)) { |
| 277 |
energies.push_back(*energy); |
| 278 |
} |
| 279 |
|
| 280 |
mol.setCoordinate3d(mol.atomPositions3d(), 0); |
| 281 |
int coordSet = 1; |
| 282 |
bool done = false; |
| 283 |
while (numAtoms2 && *numAtoms2 > 0 && |
| 284 |
numAtoms == static_cast<size_t>(*numAtoms2)) { |
| 285 |
Array<Vector3> positions; |
| 286 |
positions.reserve(numAtoms); |
| 287 |
|
| 288 |
for (size_t i = 0; i < numAtoms; ++i) { |
| 289 |
getline(inStream, buffer); |
| 290 |
if (inStream.eof()) { |
| 291 |
numAtoms2 = 0; |
| 292 |
done = true; |
| 293 |
break; // break this inner loop |
| 294 |
} |
| 295 |
|
| 296 |
std::vector<string> tokens(split(buffer, ' ')); |
| 297 |
if (tokens.size() < 4) { |
| 298 |
appendError("Not enough tokens in this line: " + buffer); |
| 299 |
return false; |
| 300 |
} |
| 301 |
if (auto tmp = |
| 302 |
lexicalCast<double>(tokens.begin() + 1, tokens.begin() + 4)) { |
| 303 |
Vector3 pos(tmp->at(0), tmp->at(1), tmp->at(2)); |
| 304 |
positions.push_back(pos); |
| 305 |
} else { |
| 306 |
appendError("Error reading position"); |
| 307 |
return false; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
if (!done) |
| 312 |
mol.setCoordinate3d(positions, coordSet++); |
| 313 |
|
| 314 |
if (getline(inStream, buffer)) { |
| 315 |
if (inStream.eof()) { |
| 316 |
numAtoms2 = 0; |
| 317 |
break; // break this inner loop |
| 318 |
} |
| 319 |
|
| 320 |
if (buffer.size() == 0 || buffer[0] == '>') |
| 321 |
getline(inStream, buffer); // Orca 6 prints ">" separators |
| 322 |
if (inStream.eof()) { |
| 323 |
numAtoms2 = 0; |
| 324 |
break; // break this inner loop |
| 325 |
} |
| 326 |
|
| 327 |
numAtoms2 = lexicalCast<int>(buffer); |
| 328 |
if (!numAtoms2 || *numAtoms2 <= 0 || |
| 329 |
numAtoms != static_cast<size_t>(*numAtoms2)) |
| 330 |
break; |
| 331 |
} else { |
| 332 |
break; |
| 333 |
} |
| 334 |
|
| 335 |
std::getline(inStream, buffer); // Skip the blank |
| 336 |
// check for energies |
| 337 |
if (auto energy = findEnergy(buffer)) { |
| 338 |
energies.push_back(*energy); |
| 339 |
} |
| 340 |
positions.clear(); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
// This format has no connectivity information, so perceive basics at least. |
| 345 |
if (opts.value("perceiveBonds", true)) { |
| 346 |
mol.perceiveBondsSimple(); |
| 347 |
mol.perceiveBondOrders(); |
| 348 |
} |
| 349 |
|
| 350 |
// have to set the charges after creating bonds |
| 351 |
// (since modifying bonds invalidates the partial charges) |
| 352 |
if (!charges.empty()) { |
| 353 |
MatrixX chargesMatrix = MatrixX::Zero(mol.atomCount(), 1); |
| 354 |
for (size_t i = 0; i < charges.size(); ++i) { |
| 355 |
chargesMatrix(i, 0) = charges[i]; |
| 356 |
} |
| 357 |
mol.setPartialCharges("From File", chargesMatrix); |
| 358 |
} |
| 359 |
|
| 360 |
if (energies.size() > 1) |
| 361 |
mol.setData("energies", energies); |
| 362 |
|
| 363 |
return true; |
| 364 |
} |
| 365 |
|
| 366 |
bool XyzFormat::write(std::ostream& outStream, const Core::Molecule& mol) |
| 367 |
{ |
| 368 |
size_t numAtoms = mol.atomCount(); |
| 369 |
|
| 370 |
outStream << numAtoms << std::endl; |
| 371 |
|
| 372 |
if (mol.unitCell()) { |
| 373 |
// default to including Lattice for extended XYZ if present |
| 374 |
// https://atomsk.univ-lille.fr/doc/en/format_xyz.html |
| 375 |
// https://gitlab.com/ase/ase/-/merge_requests/62 |
| 376 |
outStream << "Lattice=\""; |
| 377 |
outStream << mol.unitCell()->aVector().x() << ' '; |
| 378 |
outStream << mol.unitCell()->aVector().y() << ' '; |
| 379 |
outStream << mol.unitCell()->aVector().z() << ' '; |
| 380 |
|
| 381 |
outStream << mol.unitCell()->bVector().x() << ' '; |
| 382 |
outStream << mol.unitCell()->bVector().y() << ' '; |
| 383 |
outStream << mol.unitCell()->bVector().z() << ' '; |
| 384 |
|
| 385 |
outStream << mol.unitCell()->cVector().x() << ' '; |
| 386 |
outStream << mol.unitCell()->cVector().y() << ' '; |
| 387 |
outStream << mol.unitCell()->cVector().z(); |
| 388 |
|
| 389 |
outStream << "\" Properties=species:S:1:pos:R:3" << endl; |
| 390 |
} else { |
| 391 |
if (mol.data("name").toString().length()) |
| 392 |
outStream << mol.data("name").toString() << endl; |
| 393 |
else |
| 394 |
outStream << "XYZ file generated by Avogadro.\n"; |
| 395 |
} |
| 396 |
|
| 397 |
for (size_t i = 0; i < numAtoms; ++i) { |
| 398 |
Atom atom = mol.atom(i); |
| 399 |
if (!atom.isValid()) { |
| 400 |
appendError("Internal error: Atom invalid."); |
| 401 |
return false; |
| 402 |
} |
| 403 |
|
| 404 |
outStream << std::setw(3) << std::left |
| 405 |
<< Elements::symbol(atom.atomicNumber()) << " " << std::setw(15) |
| 406 |
<< std::right << std::fixed << std::setprecision(10) |
| 407 |
<< atom.position3d().x() << " " << std::setw(15) << std::right |
| 408 |
<< std::fixed << std::setprecision(10) << atom.position3d().y() |
| 409 |
<< " " << std::setw(15) << std::right << std::fixed |
| 410 |
<< std::setprecision(10) << atom.position3d().z() << "\n"; |
| 411 |
} |
| 412 |
|
| 413 |
return true; |
| 414 |
} |
| 415 |
|
| 416 |
std::vector<std::string> XyzFormat::fileExtensions() const |
| 417 |
{ |
| 418 |
std::vector<std::string> ext; |
| 419 |
ext.emplace_back("xyz"); |
| 420 |
ext.emplace_back("exyz"); |
| 421 |
ext.emplace_back("extxyz"); |
| 422 |
ext.emplace_back("allxyz"); |
| 423 |
return ext; |
| 424 |
} |
| 425 |
|
| 426 |
std::vector<std::string> XyzFormat::mimeTypes() const |
| 427 |
{ |
| 428 |
std::vector<std::string> mime; |
| 429 |
mime.emplace_back("chemical/x-xyz"); |
| 430 |
return mime; |
| 431 |
} |
| 432 |
|
| 433 |
} // namespace Avogadro::Io |