| 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 "qcschema.h" |
| 7 |
|
| 8 |
#include <avogadro/io/fileformat.h> |
| 9 |
#include <avogadro/io/fileformatmanager.h> |
| 10 |
|
| 11 |
#include <avogadro/core/elements.h> |
| 12 |
#include <avogadro/core/molecule.h> |
| 13 |
#include <avogadro/core/unitcell.h> |
| 14 |
#include <avogadro/core/vector.h> |
| 15 |
|
| 16 |
#include <nlohmann/json.hpp> |
| 17 |
#include <iostream> |
| 18 |
|
| 19 |
using json = nlohmann::json; |
| 20 |
using std::string; |
| 21 |
|
| 22 |
namespace Avogadro::QuantumIO { |
| 23 |
|
| 24 |
using Core::Array; |
| 25 |
using Core::Atom; |
| 26 |
using Core::Elements; |
| 27 |
|
| 28 |
bool isNumericArray(json& j) |
| 29 |
{ |
| 30 |
if (j.is_array() && j.size() > 0) { |
| 31 |
for (const auto& v : j) { |
| 32 |
if (!v.is_number()) { |
| 33 |
return false; |
| 34 |
} |
| 35 |
} |
| 36 |
return true; |
| 37 |
} |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
bool isBooleanArray(json& j) |
| 42 |
{ |
| 43 |
if (j.is_array() && j.size() > 0) { |
| 44 |
for (const auto& v : j) { |
| 45 |
if (!v.is_boolean()) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
} |
| 49 |
return true; |
| 50 |
} |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
QCSchema::QCSchema() {} |
| 55 |
|
| 56 |
QCSchema::~QCSchema() {} |
| 57 |
|
| 58 |
std::vector<std::string> QCSchema::fileExtensions() const |
| 59 |
{ |
| 60 |
std::vector<std::string> extensions; |
| 61 |
extensions.emplace_back("qcjson"); |
| 62 |
return extensions; |
| 63 |
} |
| 64 |
|
| 65 |
std::vector<std::string> QCSchema::mimeTypes() const |
| 66 |
{ |
| 67 |
return std::vector<std::string>(); |
| 68 |
} |
| 69 |
|
| 70 |
bool QCSchema::read(std::istream& in, Core::Molecule& molecule) |
| 71 |
{ |
| 72 |
// This should be JSON so look for key attributes |
| 73 |
json root; |
| 74 |
try { |
| 75 |
in >> root; |
| 76 |
} catch (json::parse_error& e) { |
| 77 |
appendError("Error parsing JSON: " + string(e.what())); |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
if (!root.is_object()) { |
| 82 |
appendError("Error: Input is not a JSON object."); |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
// check for 'schema_name' |
| 87 |
if (root.find("schema_name") == root.end() || |
| 88 |
root["schema_name"].get<std::string>() != "QC_JSON") { |
| 89 |
appendError("Error: Input is not a QC_JSON object."); |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
// get the elements |
| 94 |
if (root.find("symbols") == root.end() || !root["symbols"].is_array()) { |
| 95 |
appendError("Error: no \"symbols\" array found."); |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
json elements = root["symbols"]; |
| 100 |
unsigned char atomicNum(0); |
| 101 |
for (const auto& element : elements) { |
| 102 |
// convert from a string to atomic number |
| 103 |
std::string symbol = element.get<std::string>(); |
| 104 |
atomicNum = Elements::atomicNumberFromSymbol(symbol); |
| 105 |
|
| 106 |
molecule.addAtom(atomicNum); |
| 107 |
} |
| 108 |
Index atomCount = molecule.atomCount(); |
| 109 |
if (atomCount == 0) { |
| 110 |
appendError("Error: no atoms found."); |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
// look for geometry for coordinates |
| 115 |
// stored as a numeric array of all coordinates |
| 116 |
// as [atom1x, atom1y, atom1z, atom2x, atom2y, atom2z, ...] |
| 117 |
if (root.find("geometry") == root.end() || !root["geometry"].is_array()) { |
| 118 |
appendError("Error: no \"geometry\" array found."); |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
json geometry = root["geometry"]; |
| 123 |
// check the length of the array |
| 124 |
if (geometry.size() != molecule.atomCount() * 3) { |
| 125 |
appendError("Error: \"geometry\" array has incorrect length."); |
| 126 |
return false; |
| 127 |
} |
| 128 |
for (Index i = 0; i < molecule.atomCount(); ++i) { |
| 129 |
auto a = molecule.atom(i); |
| 130 |
a.setPosition3d( |
| 131 |
Vector3(geometry[3 * i], geometry[3 * i + 1], geometry[3 * i + 2])); |
| 132 |
} |
| 133 |
|
| 134 |
// check for (optional) connectivity |
| 135 |
if (root.find("connectivity") != root.end() && |
| 136 |
root["connectivity"].is_array()) { |
| 137 |
// read the bonds and orders |
| 138 |
json connectivity = root["connectivity"]; |
| 139 |
// stored as an array of 3-value arrays start, end, order |
| 140 |
for (const auto& bond : connectivity) { |
| 141 |
Index start = bond[0].get<Index>() - 1; |
| 142 |
Index end = bond[1].get<Index>() - 1; |
| 143 |
unsigned char order = bond[2].get<unsigned char>(); |
| 144 |
molecule.addBond(start, end, order); |
| 145 |
} |
| 146 |
} else { |
| 147 |
// perceive connectivity |
| 148 |
molecule.perceiveBondsSimple(); |
| 149 |
molecule.perceiveBondOrders(); |
| 150 |
} |
| 151 |
|
| 152 |
// check for optional comment / name |
| 153 |
if (root.find("comment") != root.end()) |
| 154 |
molecule.setData("name", root["comment"].get<std::string>()); |
| 155 |
|
| 156 |
// check for molecular_charge and molecular_multiplicity |
| 157 |
if (root.find("molecular_charge") != root.end()) { |
| 158 |
molecule.setData("totalCharge", root["molecular_charge"].get<int>()); |
| 159 |
} |
| 160 |
if (root.find("molecular_multiplicity") != root.end()) { |
| 161 |
molecule.setData("totalSpinMultiplicity", |
| 162 |
root["molecular_multiplicity"].get<int>()); |
| 163 |
} |
| 164 |
|
| 165 |
// if the "properties object exists" look for properties |
| 166 |
if (root.find("properties") != root.end() && root["properties"].is_object()) { |
| 167 |
json properties = root["properties"]; |
| 168 |
|
| 169 |
if (properties.find("dipole_moment") != properties.end()) { |
| 170 |
// read the numeric array |
| 171 |
json dipole = properties["dipole_moment"]; |
| 172 |
if (dipole.size() == 3) { |
| 173 |
Core::Variant dipoleMoment(dipole[0].get<float>(), |
| 174 |
dipole[1].get<float>(), |
| 175 |
dipole[2].get<float>()); |
| 176 |
molecule.setData("dipoleMoment", dipoleMoment); |
| 177 |
} |
| 178 |
} |
| 179 |
if (properties.find("partial_charges") != properties.end() && |
| 180 |
properties["partial_charges"].is_object()) { |
| 181 |
// keys are types, values are arrays of charges |
| 182 |
json partialCharges = properties["partial_charges"]; |
| 183 |
for (auto& kv : partialCharges.items()) { |
| 184 |
MatrixX charges(atomCount, 1); |
| 185 |
if (isNumericArray(kv.value()) && kv.value().size() == atomCount) { |
| 186 |
for (size_t i = 0; i < kv.value().size(); ++i) { |
| 187 |
charges(i, 0) = kv.value()[i]; |
| 188 |
} |
| 189 |
molecule.setPartialCharges(kv.key(), charges); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
// energy |
| 194 |
// e.g. total_energy": { |
| 195 |
// "units": "Hartree", |
| 196 |
// "value": -26.173033542939 |
| 197 |
if (properties.find("total_energy") != properties.end() && |
| 198 |
properties["total_energy"].is_object()) { |
| 199 |
json totalEnergy = properties["total_energy"]; |
| 200 |
if (totalEnergy.find("value") != totalEnergy.end()) |
| 201 |
molecule.setData("totalEnergy", totalEnergy["value"].get<float>()); |
| 202 |
} |
| 203 |
|
| 204 |
// trajectory or geometry optimization |
| 205 |
if (properties.find("geometry_sequence") != properties.end() && |
| 206 |
properties["geometry_sequence"].is_object()) { |
| 207 |
json sequence = properties["geometry_sequence"]; |
| 208 |
// energies and geometries |
| 209 |
|
| 210 |
// energies should be a numeric array |
| 211 |
if (sequence.find("energies") != sequence.end() && |
| 212 |
sequence["energies"].is_array()) { |
| 213 |
std::vector<double> energies; |
| 214 |
for (unsigned int i = 0; i < sequence["energies"].size(); ++i) { |
| 215 |
energies.push_back(sequence["energies"][i].get<float>()); |
| 216 |
} |
| 217 |
molecule.setData("energies", energies); |
| 218 |
} |
| 219 |
|
| 220 |
json coordSets = properties["geometry_sequence"]["geometries"]; |
| 221 |
if (coordSets.is_array() && coordSets.size()) { |
| 222 |
for (unsigned int i = 0; i < coordSets.size(); ++i) { |
| 223 |
Array<Vector3> setArray; |
| 224 |
json set = coordSets[i]; |
| 225 |
if (isNumericArray(set)) { |
| 226 |
for (unsigned int j = 0; j < set.size() / 3; ++j) { |
| 227 |
setArray.push_back( |
| 228 |
Vector3(set[3 * j], set[3 * j + 1], set[3 * j + 2])); |
| 229 |
} |
| 230 |
molecule.setCoordinate3d(setArray, i); |
| 231 |
} |
| 232 |
} |
| 233 |
// Make sure the first step is active once we are done loading the sets. |
| 234 |
molecule.setCoordinate3d(0); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
// vibrations |
| 239 |
if (properties.find("vibrations") != properties.end() && |
| 240 |
properties["vibrations"].is_object()) { |
| 241 |
json vib = properties["vibrations"]; |
| 242 |
|
| 243 |
Array<double> freqs, irIntens, ramanIntens; |
| 244 |
Array<Array<Vector3>> disps; |
| 245 |
|
| 246 |
// frequencies |
| 247 |
if (vib.find("frequencies") != vib.end() && |
| 248 |
vib["frequencies"].is_array()) { |
| 249 |
json frequencies = vib["frequencies"]; |
| 250 |
if (isNumericArray(frequencies)) { |
| 251 |
for (auto& frequency : frequencies) { |
| 252 |
freqs.push_back(static_cast<double>(frequency)); |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
if (vib.find("intensities") != vib.end() && |
| 257 |
vib["intensities"].is_object()) { |
| 258 |
json ir = vib["intensities"]["IR"]; |
| 259 |
if (isNumericArray(ir)) { |
| 260 |
for (auto& i : ir) { |
| 261 |
irIntens.push_back(static_cast<double>(i)); |
| 262 |
} |
| 263 |
} |
| 264 |
json raman = vib["intensities"]["raman"]; |
| 265 |
if (isNumericArray(raman)) { |
| 266 |
for (auto& i : raman) { |
| 267 |
ramanIntens.push_back(static_cast<double>(i)); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
json displacements = vib["displacement"]; |
| 273 |
if (displacements.is_array()) { |
| 274 |
for (auto arr : displacements) { |
| 275 |
if (isNumericArray(arr)) { |
| 276 |
Array<Vector3> mode; |
| 277 |
mode.resize(arr.size() / 3); |
| 278 |
double* ptr = &mode[0][0]; |
| 279 |
for (auto& j : arr) { |
| 280 |
*(ptr++) = static_cast<double>(j); |
| 281 |
} |
| 282 |
disps.push_back(mode); |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
// sanity check |
| 288 |
// make sure these all have the same length |
| 289 |
Index size = freqs.size(); |
| 290 |
if (size > 0 && irIntens.size() == size && disps.size() == size) { |
| 291 |
molecule.setVibrationFrequencies(freqs); |
| 292 |
molecule.setVibrationIRIntensities(irIntens); |
| 293 |
molecule.setVibrationLx(disps); |
| 294 |
} |
| 295 |
// check to make sure raman intensities are not all zero |
| 296 |
bool allZero = true; |
| 297 |
for (auto& i : ramanIntens) { |
| 298 |
if (i != 0.0) { |
| 299 |
allZero = false; |
| 300 |
break; |
| 301 |
} |
| 302 |
} |
| 303 |
if (ramanIntens.size() == size && !allZero) |
| 304 |
molecule.setVibrationRamanIntensities(ramanIntens); |
| 305 |
} |
| 306 |
|
| 307 |
// excitation energies |
| 308 |
if (properties.find("excited_states") != properties.end() && |
| 309 |
properties["excited_states"].is_object()) { |
| 310 |
json excitedStates = properties["excited_states"]; |
| 311 |
// check units (defaults to nm) |
| 312 |
std::string units = "nm"; |
| 313 |
if (excitedStates.find("units") != excitedStates.end()) { |
| 314 |
units = excitedStates["units"].get<std::string>(); |
| 315 |
} |
| 316 |
std::vector<double> energies; |
| 317 |
std::vector<double> intensities; |
| 318 |
// transition_energies |
| 319 |
if (excitedStates.find("transition_energies") != excitedStates.end() && |
| 320 |
excitedStates["transition_energies"].is_array()) { |
| 321 |
json transition_energies = excitedStates["transition_energies"]; |
| 322 |
if (isNumericArray(transition_energies)) { |
| 323 |
for (auto& i : transition_energies) { |
| 324 |
if (units == "nm") |
| 325 |
// convert to eV, i.e. eV = 1239.8 / wavelength |
| 326 |
energies.push_back(1239.841984 / static_cast<double>(i)); |
| 327 |
else if (units == "cm^-1") |
| 328 |
energies.push_back(static_cast<double>(i) / 8065.544); |
| 329 |
else if (units == "eV") |
| 330 |
energies.push_back(static_cast<double>(i)); |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
if (excitedStates.find("intensities") != excitedStates.end() && |
| 335 |
excitedStates["intensities"].is_array()) { |
| 336 |
json intensities = excitedStates["intensities"]; |
| 337 |
if (isNumericArray(intensities)) { |
| 338 |
for (auto& i : intensities) { |
| 339 |
intensities.push_back(static_cast<double>(i)); |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
// sanity check |
| 345 |
// make sure these all have the same length |
| 346 |
Index size = energies.size(); |
| 347 |
if (size > 0 && intensities.size() == size) { |
| 348 |
// create the matrix |
| 349 |
MatrixX electronicData(energies.size(), 2); |
| 350 |
// copy the data |
| 351 |
for (std::size_t i = 0; i < energies.size(); ++i) { |
| 352 |
electronicData(i, 0) = energies[i]; |
| 353 |
electronicData(i, 1) = intensities[i]; |
| 354 |
} |
| 355 |
// set the data |
| 356 |
molecule.setSpectra("Electronic", electronicData); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
// NMR spectra |
| 361 |
if (properties.find("nmr_shifts") != properties.end() && |
| 362 |
properties["nmr_shifts"].is_object()) { |
| 363 |
json nmrShifts = properties["nmr_shifts"]; |
| 364 |
// get the isotropic shifts as an array |
| 365 |
std::vector<double> nmrShiftsIsotropic; |
| 366 |
if (nmrShifts.find("isotropic") != nmrShifts.end() && |
| 367 |
nmrShifts["isotropic"].is_array()) { |
| 368 |
json isotropic = nmrShifts["isotropic"]; |
| 369 |
if (isNumericArray(isotropic)) { |
| 370 |
for (auto& i : isotropic) { |
| 371 |
nmrShiftsIsotropic.push_back(static_cast<double>(i)); |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
MatrixX nmrData(nmrShiftsIsotropic.size(), 1); |
| 377 |
for (std::size_t i = 0; i < nmrShiftsIsotropic.size(); ++i) { |
| 378 |
nmrData(i, 0) = nmrShiftsIsotropic[i]; |
| 379 |
} |
| 380 |
molecule.setSpectra("NMR", nmrData); |
| 381 |
} |
| 382 |
|
| 383 |
// todo |
| 384 |
// - orbital energies |
| 385 |
// - other properties |
| 386 |
} |
| 387 |
|
| 388 |
return true; |
| 389 |
} |
| 390 |
|
| 391 |
} // namespace Avogadro::QuantumIO |