Debian Codegraph @

avogadro/quantumio/orca.cpp

raw
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 "orca.h"
7
8 #include <avogadro/core/elements.h>
9 #include <avogadro/core/molecule.h>
10 #include <avogadro/core/utilities.h>
11
12 #include <algorithm>
13 #include <cstddef>
14 #include <memory>
15 #include <iostream>
16 #include <fstream>
17 #include <regex>
18
19 using std::regex;
20 using std::string;
21 using std::vector;
22
23 namespace Avogadro::QuantumIO {
24
25 using Core::Array;
26 using Core::Atom;
27 using Core::GaussianSet;
28
29 ORCAOutput::ORCAOutput() {}
30
31 ORCAOutput::~ORCAOutput()
32 {
33 clearBasisFunctions();
34 }
35
36 void ORCAOutput::clearBasisFunctions()
37 {
38 for (auto* atomShells : m_basisFunctions) {
39 if (!atomShells)
40 continue;
41 for (auto* shell : *atomShells) {
42 delete shell;
43 }
44 delete atomShells;
45 }
46 m_basisFunctions.clear();
47 }
48
49 constexpr double BOHR_TO_ANGSTROM = 0.529177210544;
50 constexpr double HARTREE_TO_EV = 27.211386245981;
51
52 std::vector<std::string> ORCAOutput::fileExtensions() const
53 {
54 std::vector<std::string> extensions;
55 extensions.emplace_back("orca");
56 return extensions;
57 }
58
59 std::vector<std::string> ORCAOutput::mimeTypes() const
60 {
61 return std::vector<std::string>();
62 }
63
64 bool ORCAOutput::read(std::istream& in, Core::Molecule& molecule)
65 {
66 // Read the log file line by line
67 auto basis = std::make_unique<GaussianSet>();
68
69 while (!in.eof())
70 processLine(in, basis.get());
71
72 // Set up the molecule
73 int nAtom = 0;
74 for (unsigned int i = 0; i < m_atomNums.size(); i++) {
75 Vector3 pos = m_atomPos[i] * BOHR_TO_ANGSTROM;
76 molecule.addAtom(static_cast<unsigned char>(m_atomNums[nAtom++]), pos);
77 }
78
79 if (0 == molecule.atomCount()) {
80 appendError("Could not find any atomic coordinates! Are you sure this is "
81 "an ORCA output file?");
82 return false;
83 }
84
85 // this should be the final coordinate set (e.g. the optimized geometry)
86 molecule.setCoordinate3d(molecule.atomPositions3d(), 0);
87 if (m_coordSets.size() > 1) {
88 for (unsigned int i = 0; i < m_coordSets.size(); i++) {
89 Array<Vector3> positions;
90 positions.reserve(molecule.atomCount());
91 for (size_t j = 0; j < molecule.atomCount(); ++j) {
92 positions.push_back(m_coordSets[i][j] * BOHR_TO_ANGSTROM);
93 }
94 molecule.setCoordinate3d(positions, i + 1);
95 }
96 }
97
98 // guess bonds and bond orders
99 molecule.perceiveBondsSimple();
100 molecule.perceiveBondOrders();
101
102 if (m_frequencies.size() > 0 &&
103 m_frequencies.size() == m_vibDisplacements.size() &&
104 m_frequencies.size() == m_IRintensities.size()) {
105 molecule.setVibrationFrequencies(m_frequencies);
106 molecule.setVibrationIRIntensities(m_IRintensities);
107 molecule.setVibrationLx(m_vibDisplacements);
108 if (m_RamanIntensities.size())
109 molecule.setVibrationRamanIntensities(m_RamanIntensities);
110 }
111
112 if (m_vcdIntensities.size() > 0 &&
113 m_vcdIntensities.size() == m_frequencies.size()) {
114 MatrixX vcdData(m_frequencies.size(), 2);
115 for (size_t i = 0; i < m_frequencies.size(); ++i) {
116 vcdData(i, 0) = m_frequencies[i];
117 vcdData(i, 1) = m_vcdIntensities[i];
118 }
119 molecule.setSpectra("VibrationalCD", vcdData);
120 }
121
122 if (m_electronicTransitions.size() > 0 &&
123 m_electronicTransitions.size() == m_electronicIntensities.size()) {
124 MatrixX electronicData(m_electronicTransitions.size(), 2);
125 for (size_t i = 0; i < m_electronicTransitions.size(); ++i) {
126 electronicData(i, 0) = m_electronicTransitions[i];
127 electronicData(i, 1) = m_electronicIntensities[i];
128 }
129 molecule.setSpectra("Electronic", electronicData);
130
131 if (m_electronicRotations.size() == m_electronicTransitions.size()) {
132 MatrixX electronicRotations(m_electronicTransitions.size(), 2);
133 for (size_t i = 0; i < m_electronicTransitions.size(); ++i) {
134 electronicRotations(i, 0) = m_electronicTransitions[i];
135 electronicRotations(i, 1) = m_electronicRotations[i];
136 }
137 molecule.setSpectra("CircularDichroism", electronicRotations);
138 }
139 }
140
141 if (m_magneticTransitions.size() > 0 &&
142 m_magneticTransitions.size() == m_magneticCD.size()) {
143 MatrixX magneticData(m_magneticTransitions.size(), 2);
144 for (size_t i = 0; i < m_magneticTransitions.size(); ++i) {
145 magneticData(i, 0) = m_magneticTransitions[i];
146 magneticData(i, 1) = m_magneticCD[i];
147 }
148 molecule.setSpectra("MagneticCD", magneticData);
149 }
150
151 if (m_nmrShifts.size() > 0) {
152 MatrixX nmrData(m_nmrShifts.size(), 2);
153 // nmr_shifts has an entry for every atom even if not computed
154 for (size_t i = 0; i < m_nmrShifts.size(); ++i) {
155 nmrData(i, 0) = m_nmrShifts[i];
156 nmrData(i, 1) = 1.0;
157 }
158 molecule.setSpectra("NMR", nmrData);
159 }
160
161 // check bonds from calculated bond orders
162 if (m_bondOrders.size() > 0) {
163 for (unsigned int i = 0; i < m_bondOrders.size(); i++) {
164 // m_bondOrders[i][0] is the first atom
165 // m_bondOrders[i][1] is the second atom
166 // m_bondOrders[i][2] is the bond order
167 if (m_bondOrders[i].size() > 2) {
168 auto bond = molecule.bond(m_bondOrders[i][0], m_bondOrders[i][1]);
169 if (bond.isValid() && bond.order() != m_bondOrders[i][2]) {
170 // if the bond order is different, change it
171 bond.setOrder(static_cast<unsigned char>(m_bondOrders[i][2]));
172 }
173 }
174 }
175 }
176
177 molecule.setBasisSet(basis.release());
178 molecule.basisSet()->setMolecule(&molecule);
179 load(static_cast<GaussianSet*>(molecule.basisSet()));
180
181 // we have to do a few things *after* any modifications to bonds / atoms
182 // because those automatically clear partial charges and data
183
184 // add the partial charges
185 if (m_partialCharges.size() > 0) {
186 for (auto it = m_partialCharges.begin(); it != m_partialCharges.end();
187 ++it) {
188 molecule.setPartialCharges(it->first, it->second);
189 }
190 }
191
192 molecule.setData("totalCharge", m_charge);
193 molecule.setData("totalSpinMultiplicity", m_spin);
194 molecule.setData("dipoleMoment", m_dipoleMoment);
195 molecule.setData("totalEnergy", m_totalEnergy);
196 if (m_energies.size() > 1)
197 molecule.setData("energies", m_energies);
198
199 return true;
200 }
201
202 void ORCAOutput::processLine(std::istream& in,
203 [[maybe_unused]] GaussianSet* basis)
204 {
205 // First truncate the line, remove trailing white space and check
206 string line;
207 if (!getline(in, line) || Core::trimmed(line).empty())
208 return;
209
210 string key = Core::trimmed(line);
211 vector<string> list;
212 int nGTOs = 0;
213 [[maybe_unused]] float vibScaling = 1.0f;
214
215 if (Core::contains(key, "CARTESIAN COORDINATES (A.U.)")) {
216 m_coordFactor = 1.; // leave the coords in BOHR ....
217 m_currentMode = Atoms;
218 // if there are any current coordinates, push them back
219 if (m_atomPos.size() > 0) {
220 m_coordSets.push_back(m_atomPos);
221 }
222 m_atomPos.clear();
223 m_atomNums.clear();
224 m_atomLabel.clear();
225 getline(in, key); // skip ----- line
226 getline(in, key); // column titles
227 } else if (Core::contains(key, "BASIS SET INFORMATION")) {
228 if (!Core::contains(key, "AUXILIARY")) { // skip auxiliary basis set infos
229 m_currentMode = GTO;
230 getline(in, key); // skip ----- line
231
232 // Number of groups of distinct atoms
233 getline(in, key);
234 list = Core::split(key, ' ');
235 if (list.size() > 3) {
236 m_nGroups = Core::lexicalCast<int>(list[2]).value_or(0);
237 } else {
238 return;
239 }
240 getline(in, key); // skip blank line
241 for (int i = 0; i < m_nGroups; ++i) {
242 getline(in, key); // skip group information
243 }
244 getline(in, key); // skip blank line
245 for (unsigned int i = 0; i < m_atomNums.size(); ++i) {
246 getline(in, key); // skip group information
247 }
248
249 // now skip
250 // blank line
251 // ----------------------------
252 // # Basis set for element : x
253 // ----------------------------
254 // blank line
255 for (unsigned int i = 0; i < 6; ++i) {
256 getline(in, key);
257 }
258 }
259 } else if (Core::contains(key, "Total Charge")) {
260 list = Core::split(key, ' ');
261 if (list.size() > 4)
262 m_charge = Core::lexicalCast<int>(list[4]).value_or(0);
263 } else if (Core::contains(key, "Multiplicity")) {
264 list = Core::split(key, ' ');
265 if (list.size() > 3)
266 m_spin = Core::lexicalCast<int>(list[3]).value_or(1);
267 } else if (Core::contains(key, "FINAL SINGLE POINT ENERGY")) {
268 list = Core::split(key, ' ');
269 if (list.size() > 4)
270 m_totalEnergy = Core::lexicalCast<double>(list[4]).value_or(0.0);
271 m_energies.push_back(m_totalEnergy);
272 } else if (Core::contains(key, "TOTAL NUMBER OF BASIS SET")) {
273 m_currentMode = NotParsing; // no longer reading GTOs
274 } else if (Core::contains(key, "NUMBER OF CARTESIAN GAUSSIAN BASIS")) {
275 m_currentMode = NotParsing; // no longer reading GTOs
276 } else if (Core::contains(key, "Number of Electrons")) {
277 list = Core::split(key, ' ');
278 m_electrons = Core::lexicalCast<int>(list[5]).value_or(0);
279 } else if (Core::contains(key, "Total Dipole Moment")) {
280 list = Core::split(key, ' ');
281 m_dipoleMoment =
282 Eigen::Vector3d(Core::lexicalCast<double>(list[4]).value_or(0.0),
283 Core::lexicalCast<double>(list[5]).value_or(0.0),
284 Core::lexicalCast<double>(list[6]).value_or(0.0));
285 // convert from atomic units to Debye
286 // e.g. https://en.wikipedia.org/wiki/Debye
287 m_dipoleMoment *= 2.54174628;
288 } else if (Core::contains(key, "Mayer bond orders")) {
289 m_currentMode = BondOrders;
290 // starts at the next line
291 } else if (Core::contains(
292 key,
293 "ABSORPTION SPECTRUM VIA TRANSITION ELECTRIC DIPOLE MOMENTS")) {
294 m_currentMode = Electronic;
295 for (int i = 0; i < 4; ++i) {
296 getline(in, key); // skip header
297 }
298 // starts at the next line
299 } else if (Core::contains(key, "CD SPECTRUM") &&
300 !Core::contains(key, "VCD SPECTRUM") &&
301 !Core::contains(key, "TRANSITION VELOCITY DIPOLE")) {
302 m_currentMode = ECD;
303 for (int i = 0; i < 4; ++i) {
304 getline(in, key); // skip header
305 }
306 } else if (Core::contains(key, "ORBITAL ENERGIES")) {
307 m_currentMode = OrbitalEnergies;
308 getline(in, key); // skip ------------
309 getline(in, key); // check if SPIN UP ORBITALS are present
310 if (Core::contains(key, "SPIN UP ORBITALS")) {
311 m_openShell = true;
312 m_readBeta = false;
313 } else {
314 m_openShell = false;
315 m_readBeta = false;
316 }
317 getline(in, key); // skip column titles
318 } else if (Core::contains(key, "SPIN DOWN ORBITALS")) {
319 m_currentMode = OrbitalEnergies;
320 m_openShell = true;
321 m_readBeta = true;
322 getline(in, key); // skip column headers
323 } else if (Core::contains(key, "MOLECULAR ORBITALS")) {
324 m_currentMode = MO;
325 getline(in, key); //------------
326 } else if (Core::contains(key, "HIRSHFELD ANALYSIS")) {
327 m_currentMode = HirshfeldCharges;
328 m_chargeType = "Hirshfeld";
329 for (unsigned int i = 0; i < 6; ++i) {
330 getline(in, key); // skip header
331 }
332 } else if (Core::contains(key, "MBIS ANALYSIS")) {
333 // MBIS analysis is similar to Hirshfeld, but with different headers
334 m_currentMode = HirshfeldCharges;
335 m_chargeType = "MBIS";
336 for (unsigned int i = 0; i < 9; ++i) {
337 getline(in, key); // skip header
338 }
339 } else if (Core::contains(key, "CHELPG Charges")) {
340 // similar to standard charges
341 m_currentMode = Charges;
342 m_chargeType = "CHELPG";
343 getline(in, key); // skip ------------
344 } else if (Core::contains(key, "RESP Charges")) {
345 m_currentMode = Charges;
346 m_chargeType = "RESP";
347 getline(in, key); // skip ------------
348 } else if (Core::contains(key, "ATOMIC CHARGES")) {
349 m_currentMode = Charges;
350 // figure out what type of charges we have
351 list = Core::split(key, ' ');
352 if (list.size() > 2) {
353 m_chargeType = Core::trimmed(list[0]); // e.g. MULLIKEN or LOEWDIN
354 }
355 // lowercase everything except the first letter
356 for (unsigned int i = 1; i < m_chargeType.size(); ++i) {
357 m_chargeType[i] = tolower(m_chargeType[i]);
358 }
359 getline(in, key); // skip ------------
360 } else if (Core::contains(key, "VIBRATIONAL FREQUENCIES")) {
361 m_currentMode = Frequencies;
362 getline(in, key); // skip ------------
363 getline(in, key); // skip blank line
364 getline(in, key); // scaling factor
365 // Scaling factor for frequencies = 1.000000000
366 list = Core::split(key, ' ');
367 if (list.size() > 6)
368 vibScaling = Core::lexicalCast<float>(list[5]).value_or(0);
369 getline(in, key); // skip blank line
370 } else if (Core::contains(key, "NORMAL MODES")) {
371 m_currentMode = VibrationalModes;
372
373 getline(in, key); // skip ------------
374 getline(in, key); // skip blank line
375 getline(in, key); // skip comment
376 getline(in, key); // skip more comments
377 getline(in, key); // skip even more comment
378 getline(in, key); // skip blank line
379 } else if (Core::contains(key, "IR SPECTRUM")) {
380 m_currentMode = IR;
381 getline(in, key); // skip ------------
382 getline(in, key); // skip blank line
383 getline(in, key); // skip column titles
384 getline(in, key); // skip more column titles
385 getline(in, key); // skip ------------
386 } else if (Core::contains(key, "RAMAN SPECTRUM")) {
387 m_currentMode = Raman;
388 getline(in, key); // skip ------------
389 getline(in, key); // skip blank line
390 getline(in, key); // skip column titles
391 getline(in, key); // skip ------------
392 } else if (Core::contains(key, "VCD SPECTRUM")) {
393 m_currentMode = VCD;
394 // look for "Mode" and "Freq"
395 int maxLines = 10; // reasonable limit for header lines
396 while (!Core::contains(key, "Mode") && !Core::contains(key, "Freq") &&
397 maxLines-- > 0 && !in.eof())
398 getline(in, key);
399 if (maxLines <= 0 || in.eof()) {
400 m_currentMode = NotParsing;
401 return;
402 }
403 // units
404 getline(in, key);
405 getline(in, key); // skip ------------
406 } else if (Core::contains(key, "MCD Transitions")) {
407 parseMCD();
408 } else if (Core::contains(key, "CHEMICAL SHIELDING SUMMARY (ppm)")) {
409 m_currentMode = NMR;
410 for (int i = 0; i < 4; ++i) {
411 getline(in, key); // skip header
412 }
413 } else {
414
415 vector<vector<double>> columns;
416 unsigned int numColumns, numRows;
417 numColumns = 0;
418 numRows = 0;
419 // parsing a line -- what mode are we in?
420
421 switch (m_currentMode) {
422 case Atoms: {
423 if (key.empty())
424 break;
425 list = Core::split(key, ' ');
426 while (!key.empty()) {
427 if (list.size() < 8) {
428 break;
429 }
430 Eigen::Vector3d pos(
431 Core::lexicalCast<double>(list[5]).value_or(0.0) * m_coordFactor,
432 Core::lexicalCast<double>(list[6]).value_or(0.0) * m_coordFactor,
433 Core::lexicalCast<double>(list[7]).value_or(0.0) * m_coordFactor);
434
435 unsigned char atomicNum =
436 Core::Elements::atomicNumberFromSymbol(Core::trimmed(list[1]));
437 m_atomNums.push_back(atomicNum);
438 m_atomPos.push_back(pos);
439 m_atomLabel.push_back(Core::trimmed(list[1]));
440 getline(in, key);
441 key = Core::trimmed(key);
442 list = Core::split(key, ' ');
443 }
444 m_currentMode = NotParsing;
445 break;
446 }
447 case HirshfeldCharges: {
448 // should start at the first atom
449 if (key.empty())
450 break;
451
452 if (m_atomNums.empty())
453 break;
454
455 Eigen::MatrixXd charges(m_atomNums.size(), 1);
456 charges.setZero();
457
458 list = Core::split(key, ' ');
459 bool invalidIndex = false;
460 while (!key.empty()) {
461 if (list.size() < 4) {
462 break;
463 }
464 // e.g. index atom charge spin
465 // e.g. 0 O -0.714286 0.000
466 int atomIndex = Core::lexicalCast<int>(list[0]).value_or(0);
467 double charge = Core::lexicalCast<double>(list[2]).value_or(0.0);
468 if (atomIndex < 0 || atomIndex >= charges.rows()) {
469 invalidIndex = true;
470 break;
471 }
472 charges(atomIndex, 0) = charge;
473
474 getline(in, key);
475 key = Core::trimmed(key);
476 list = Core::split(key, ' ');
477 }
478
479 if (!invalidIndex)
480 m_partialCharges[m_chargeType] = charges;
481 m_currentMode = NotParsing;
482 break;
483 }
484 case Charges: {
485 // should start at the first atom
486 if (key.empty())
487 break;
488
489 if (m_atomNums.empty())
490 break;
491
492 Eigen::MatrixXd charges(m_atomNums.size(), 1);
493 charges.setZero();
494
495 list = Core::split(key, ' ');
496 bool invalidIndex = false;
497 while (!key.empty()) {
498 if (list.size() != 4) {
499 break;
500 }
501 // e.g. 0 O : -0.714286
502 int atomIndex = Core::lexicalCast<int>(list[0]).value_or(0);
503 double charge = Core::lexicalCast<double>(list[3]).value_or(0.0);
504 if (atomIndex < 0 || atomIndex >= charges.rows()) {
505 invalidIndex = true;
506 break;
507 }
508 charges(atomIndex, 0) = charge;
509
510 getline(in, key);
511 key = Core::trimmed(key);
512 list = Core::split(key, ' ');
513 }
514 if (!invalidIndex)
515 m_partialCharges[m_chargeType] = charges;
516 m_currentMode = NotParsing;
517 break;
518 }
519 case BondOrders: {
520 if (key.empty())
521 break;
522
523 m_bondOrders.clear();
524 constexpr size_t kMinBondOrderLineLength = 27;
525 while (!key.empty() && key[0] == 'B') {
526 if (key.size() < kMinBondOrderLineLength || key[1] != '(') {
527 getline(in, key);
528 key = Core::trimmed(key);
529 continue;
530 }
531 // @todo .. parse the bonds based on character position
532 // e.g. B( 0-Ru, 1-C ) : 0.4881 B( 0-Ru, 4-C ) : 0.6050
533 Index firstAtom =
534 Core::lexicalCast<Index>(key.substr(2, 3)).value_or(0);
535 Index secondAtom =
536 Core::lexicalCast<Index>(key.substr(9, 3)).value_or(0);
537 double bondOrder =
538 Core::lexicalCast<double>(key.substr(18, 9)).value_or(0.0);
539
540 if (bondOrder > 1.6) {
541 std::vector<int> bond;
542 bond.push_back(static_cast<int>(firstAtom));
543 bond.push_back(static_cast<int>(secondAtom));
544 bond.push_back(static_cast<int>(std::round(bondOrder)));
545 m_bondOrders.push_back(bond);
546 }
547
548 if (key.size() > 54 && key[28] == 'B') {
549 firstAtom = Core::lexicalCast<Index>(key.substr(30, 3)).value_or(0);
550 secondAtom =
551 Core::lexicalCast<Index>(key.substr(37, 3)).value_or(0);
552 bondOrder =
553 Core::lexicalCast<double>(key.substr(46, 9)).value_or(0.0);
554
555 if (bondOrder > 1.6) {
556 std::vector<int> bond;
557 bond.push_back(static_cast<int>(firstAtom));
558 bond.push_back(static_cast<int>(secondAtom));
559 bond.push_back(static_cast<int>(std::round(bondOrder)));
560 m_bondOrders.push_back(bond);
561 }
562 }
563 if (key.size() > 82 && key[56] == 'B') {
564 firstAtom = Core::lexicalCast<Index>(key.substr(58, 3)).value_or(0);
565 secondAtom =
566 Core::lexicalCast<Index>(key.substr(65, 3)).value_or(0);
567 bondOrder =
568 Core::lexicalCast<double>(key.substr(74, 9)).value_or(0.0);
569
570 if (bondOrder > 1.6) {
571 std::vector<int> bond;
572 bond.push_back(static_cast<int>(firstAtom));
573 bond.push_back(static_cast<int>(secondAtom));
574 bond.push_back(static_cast<int>(std::round(bondOrder)));
575 m_bondOrders.push_back(bond);
576 }
577 }
578
579 getline(in, key);
580 key = Core::trimmed(key);
581 }
582
583 m_currentMode = NotParsing;
584 break;
585 }
586 case OrbitalEnergies: {
587 if (key.empty())
588 break;
589
590 // should start at the first orbital
591 if (!m_readBeta)
592 m_orbitalEnergy.clear();
593 else
594 m_betaOrbitalEnergy.clear();
595
596 list = Core::split(key, ' ');
597 while (!key.empty()) {
598 if (list.size() != 4) {
599 break;
600 }
601
602 // energy in Hartree in 3rd column in eV in 4th column
603 double energy = Core::lexicalCast<double>(list[3]).value_or(0.0);
604 if (!m_readBeta)
605 m_orbitalEnergy.push_back(energy);
606 else
607 m_betaOrbitalEnergy.push_back(energy);
608
609 getline(in, key);
610 key = Core::trimmed(key);
611 list = Core::split(key, ' ');
612 }
613 m_currentMode = NotParsing;
614 break;
615 }
616 case Frequencies: {
617 // should start at the first frequency - include zeros
618 if (key.empty())
619 break;
620 list = Core::split(key, ' ');
621 while (!key.empty()) {
622 // imaginary frequencies can have an additional comment:
623 // ***imaginary mode***
624 if (list.size() != 3 &&
625 (list.size() != 5 || list[3] != "***imaginary" ||
626 list[4] != "mode***")) {
627 break;
628 }
629 // e.g. 0: 0.00 cm**-1
630 double freq = Core::lexicalCast<double>(list[1]).value_or(0.0);
631 m_frequencies.push_back(freq);
632
633 getline(in, key);
634 key = Core::trimmed(key);
635 list = Core::split(key, ' ');
636 }
637
638 // okay, now set up the normal mode arrays
639 m_vibDisplacements.resize(m_frequencies.size());
640 m_IRintensities.resize(m_frequencies.size());
641 // we don't bother with Raman, because that's less common
642 for (unsigned int i = 0; i < m_frequencies.size(); i++) {
643 m_IRintensities[i] = 0.0;
644 m_vibDisplacements[i].resize(m_atomNums.size());
645 for (unsigned int j = 0; j < m_atomNums.size(); j++)
646 m_vibDisplacements[i].push_back(Eigen::Vector3d());
647 }
648
649 m_currentMode = NotParsing;
650 break;
651 }
652 case VCD: {
653 // should be mode, frequency, IR intensity
654 if (key.empty())
655 break;
656 list = Core::split(key, ' ');
657 m_vcdIntensities.resize(m_frequencies.size(), 0.0);
658
659 while (!key.empty()) {
660 // e.g. 0: 0.00 cm**-1
661
662 if (list.size() != 3) {
663 break;
664 }
665 unsigned index = Core::lexicalCast<std::size_t>(list[0]).value_or(0);
666 if (index >= m_frequencies.size()) {
667 break;
668 }
669 m_vcdIntensities[index] =
670 Core::lexicalCast<double>(list[2]).value_or(0.0);
671
672 getline(in, key);
673 key = Core::trimmed(key);
674 list = Core::split(key, ' ');
675 }
676
677 m_currentMode = NotParsing;
678 break;
679 }
680 case VibrationalModes: {
681 if (key.empty())
682 break;
683 list = Core::split(key, ' ');
684 vector<std::size_t> modeIndex;
685 bool invalid_index = false;
686 while (!key.empty()) {
687 // first we get a set of column numbers
688 // e.g. 1 2 3 4 5 6 7 8 9 10
689 modeIndex.clear();
690 for (const auto& index_str : list) {
691 auto index = Core::lexicalCast<std::size_t>(index_str).value_or(0);
692 if (index >= m_frequencies.size()) {
693 invalid_index = true;
694 break;
695 }
696 modeIndex.push_back(index);
697 }
698 // invalid column index
699 if (invalid_index)
700 break;
701
702 // now we read the displacements .. there should be 3N lines
703 // x,y,z for each atom
704 getline(in, key);
705 key = Core::trimmed(key);
706 list = Core::split(key, ' ');
707 for (unsigned int i = 0; i < 3 * m_atomNums.size(); i++) {
708 unsigned int atomIndex = i / 3;
709 unsigned int coordIndex = i % 3;
710 for (unsigned int j = 0; j < modeIndex.size(); j++) {
711 m_vibDisplacements[modeIndex[j]][atomIndex][coordIndex] =
712 Core::lexicalCast<double>(list[j + 1]).value_or(0.0);
713 }
714
715 getline(in, key);
716 key = Core::trimmed(key);
717 list = Core::split(key, ' ');
718 }
719 }
720
721 m_currentMode = NotParsing;
722 break;
723 }
724 case IR: {
725 if (key.empty())
726 break;
727 list = Core::split(key, ' ');
728 while (!key.empty()) {
729 // e.g. 6: 1711.76 0.014736 74.47 0.002686 (-0.021704 0.027180
730 // 0.038427)
731 if (list.size() < 7) {
732 break;
733 }
734 // the first entry might be 5 or 6 because of removed rotations /
735 // translations
736 auto index = Core::lexicalCast<std::size_t>(list[0]).value_or(0);
737 // invalid index
738 if (index >= m_frequencies.size())
739 break;
740
741 double intensity = Core::lexicalCast<double>(list[3]).value_or(0.0);
742 m_IRintensities[index] = intensity;
743
744 getline(in, key);
745 key = Core::trimmed(key);
746 list = Core::split(key, ' ');
747 }
748
749 m_currentMode = NotParsing;
750 break;
751 }
752 case Raman: {
753 if (key.empty())
754 break;
755 list = Core::split(key, ' ');
756 while (!key.empty()) {
757 // e.g. 6: 76.62 0.000000 0.465517
758 if (list.size() != 4) {
759 break;
760 }
761 // the first entry might be 5 or 6 because of removed rotations /
762 // translations
763 auto index = Core::lexicalCast<std::size_t>(list[0]).value_or(0);
764 // invalid index
765 if (index >= m_frequencies.size())
766 break;
767 if (m_RamanIntensities.empty()) {
768 while (m_RamanIntensities.size() < index) {
769 m_RamanIntensities.push_back(0.0);
770 }
771 }
772 // index, frequency, activity, depolarization
773 double activity = Core::lexicalCast<double>(list[2]).value_or(0.0);
774 m_RamanIntensities.push_back(activity);
775
776 getline(in, key);
777 key = Core::trimmed(key);
778 list = Core::split(key, ' ');
779 }
780
781 m_currentMode = NotParsing;
782 break;
783 }
784 case Electronic: {
785 if (key.empty())
786 break;
787 list = Core::split(key, ' ');
788 double wavenumbers;
789 while (!key.empty()) {
790 // should have 8 columns
791 if (list.size() < 8) {
792 getline(in, key);
793 key = Core::trimmed(key);
794 list = Core::split(key, ' ');
795 continue; // skip any spin-forbidden transitions
796 }
797
798 if (list.size() == 8) {
799 wavenumbers = Core::lexicalCast<double>(list[1]).value_or(0.0);
800 // convert to eV
801 m_electronicTransitions.push_back(wavenumbers / 8065.544);
802 m_electronicIntensities.push_back(
803 Core::lexicalCast<double>(list[3]).value_or(0.0));
804 } else if (list.size() == 11) {
805 // directly use the eV
806 m_electronicTransitions.push_back(
807 Core::lexicalCast<double>(list[3]).value_or(0.0));
808 m_electronicIntensities.push_back(
809 Core::lexicalCast<double>(list[6]).value_or(0.0));
810 }
811
812 getline(in, key);
813 key = Core::trimmed(key);
814 list = Core::split(key, ' ');
815 if (list.size() < 2)
816 break; // hit the blank line
817 }
818 m_currentMode = NotParsing;
819 break;
820 }
821 case ECD: {
822 if (key.empty())
823 break;
824 list = Core::split(key, ' ');
825
826 while (!key.empty()) {
827 // should have 7 columns
828 if (list.size() < 7) {
829 getline(in, key);
830 key = Core::trimmed(key);
831 list = Core::split(key, ' ');
832 continue; // skip any spin-forbidden transitions
833 }
834
835 if (list.size() == 7)
836 m_electronicRotations.push_back(
837 Core::lexicalCast<double>(list[3]).value_or(0.0));
838 else if (list.size() == 10)
839 m_electronicRotations.push_back(
840 Core::lexicalCast<double>(list[6]).value_or(0.0));
841
842 getline(in, key);
843 key = Core::trimmed(key);
844 list = Core::split(key, ' ');
845 if (list.size() < 2)
846 break; // hit the blank line
847 }
848 m_currentMode = NotParsing;
849 break;
850 }
851 case NMR: {
852 if (key.empty())
853 break;
854 list = Core::split(key, ' ');
855 // default to filling m_nmrShifts with zeros
856 m_nmrShifts.resize(m_atomNums.size(), 0.0);
857 while (!key.empty()) {
858 // need at least index, element, value
859 if (list.size() < 3) {
860 break;
861 }
862
863 // e.g. 1 C 0.0000 0.0000 0.0000 0.0000
864 int atomIndex = Core::lexicalCast<int>(list[0]).value_or(0);
865 double shift = Core::lexicalCast<double>(list[2]).value_or(0.0);
866 // ignore the anisotropy for now
867 if (!m_nmrShifts.empty()) {
868 const int maxIndex = static_cast<int>(m_nmrShifts.size());
869 if (atomIndex >= 1 && atomIndex <= maxIndex) {
870 m_nmrShifts[atomIndex - 1] = shift; // ORCA uses 1-based indices
871 } else if (atomIndex >= 0 && atomIndex < maxIndex) {
872 m_nmrShifts[atomIndex] = shift; // tolerate 0-based indices
873 }
874 }
875
876 getline(in, key);
877 key = Core::trimmed(key);
878 list = Core::split(key, ' ');
879 }
880
881 m_currentMode = NotParsing;
882 break;
883 }
884 case GTO: {
885 // // should start at the first newGTO
886 if (key.empty())
887 break;
888 nGTOs = 0;
889 list = Core::split(key, ' ');
890 int nShells;
891 // init all vectors etc.
892 m_basisAtomLabel.clear();
893 m_orcaNumShells.resize(0);
894 clearBasisFunctions();
895 m_orcaShellTypes.resize(0);
896
897 m_a.resize(0);
898 m_c.resize(0);
899 m_shellNums.resize(0);
900 m_shellTypes.resize(0);
901 m_shelltoAtom.resize(0);
902 while (Core::trimmed(list[0]) == "NewGTO") {
903 m_basisAtomLabel.push_back(Core::trimmed(list[1]));
904
905 getline(in, key);
906 key = Core::trimmed(key);
907
908 list = Core::split(key, ' ');
909
910 nShells = 0;
911 m_basisFunctions.push_back(
912 new std::vector<std::vector<Eigen::Vector2d>*>);
913 shellFunctions.resize(0);
914 shellTypes.resize(0);
915 while (Core::trimmed(list[0]) != "end;") {
916
917 int nFunc =
918 Core::lexicalCast<int>(Core::trimmed(list[1])).value_or(0);
919 shellTypes.push_back(orbitalIdx(Core::trimmed(list[0])));
920 shellFunctions.push_back(nFunc);
921 m_basisFunctions.at(nGTOs)->push_back(
922 new std::vector<Eigen::Vector2d>(nFunc));
923
924 for (int i = 0; i < nFunc; i++) {
925 getline(in, key);
926 key = Core::trimmed(key);
927
928 list = Core::split(key, ' ');
929 m_basisFunctions.at(nGTOs)->at(nShells)->at(i).x() =
930 Core::lexicalCast<double>(list[1]).value_or(0.0); // exponent
931 m_basisFunctions.at(nGTOs)->at(nShells)->at(i).y() =
932 Core::lexicalCast<double>(list[2]).value_or(0.0); // coeff
933 }
934
935 nShells++;
936 getline(in, key);
937 key = Core::trimmed(key);
938
939 list = Core::split(key, ' ');
940 }
941 m_orcaShellTypes.push_back(
942 std::vector<GaussianSet::orbital>(shellTypes.size()));
943 m_orcaShellTypes.at(nGTOs) = shellTypes;
944 m_orcaNumShells.push_back(std::vector<int>(shellFunctions.size()));
945 m_orcaNumShells.at(nGTOs) = shellFunctions;
946 nGTOs++;
947
948 getline(in, key);
949 getline(in, key);
950 getline(in, key);
951 key = Core::trimmed(key);
952
953 list = Core::split(key, ' ');
954 if (list.size() == 0)
955 break; // unexpected structure - suppose no more NewGTOs
956 }
957
958 // create input for gaussian basisset
959 int nBasis = nGTOs;
960 int nAtoms = m_atomLabel.size();
961 m_currentAtom = 0;
962 for (int i = 0; i < nAtoms; i++) {
963 m_currentAtom++;
964 for (int j = 0; j < nBasis; j++) {
965 if (m_atomLabel.at(i) == m_basisAtomLabel.at(j)) {
966 for (unsigned int k = 0; k < m_orcaNumShells.at(j).size(); k++) {
967 for (int l = 0; l < m_orcaNumShells.at(j).at(k); l++) {
968 m_a.push_back(m_basisFunctions.at(j)->at(k)->at(l).x());
969 m_c.push_back(m_basisFunctions.at(j)->at(k)->at(l).y());
970 }
971 m_shellNums.push_back(m_orcaNumShells.at(j).at(k));
972 m_shellTypes.push_back(m_orcaShellTypes.at(j).at(k));
973 m_shelltoAtom.push_back(m_currentAtom);
974 }
975 break;
976 }
977 }
978 }
979 m_currentMode = NotParsing;
980 break;
981 }
982 case MO: {
983
984 m_MOcoeffs.clear(); // if the orbitals were punched multiple times
985 m_orbitalEnergy.clear(); // we can get the energies here
986 std::vector<std::string> orcaOrbitals;
987
988 while (!Core::trimmed(key).empty()) {
989 // currently reading the sequence number
990 getline(in, key); // energies
991 list = Core::split(key, ' ');
992 // convert these all to double and add to m_orbitalEnergy
993 for (unsigned int i = 0; i < list.size(); i++) {
994 // convert from Hartree to eV
995 m_orbitalEnergy.push_back(
996 Core::lexicalCast<double>(list[i]).value_or(0.0) * HARTREE_TO_EV);
997 }
998
999 getline(in, key); // occupations
1000 getline(in, key); // skip -----------
1001 getline(in, key); // now we've got coefficients
1002
1003 // coefficients are optionally a -, one or two digits, a decimal
1004 // point, and then 6 digits or just one or two digits a decimal point
1005 // and then 6 digits we can use a regex to split the line
1006 regex rx("[-]?[0-9]{1,2}[.][0-9]{6}");
1007
1008 auto key_begin = std::sregex_iterator(key.begin(), key.end(), rx);
1009 auto key_end = std::sregex_iterator();
1010 list.clear();
1011 for (std::sregex_iterator i = key_begin; i != key_end; ++i) {
1012 list.push_back(i->str());
1013 }
1014
1015 numColumns = list.size();
1016 columns.resize(numColumns);
1017 while (list.size() > 0) {
1018 // get the '2s' or '1dx2y2' piece from the line
1019 // so we can re-order the orbitals later
1020 std::vector<std::string> pieces = Core::split(key, ' ');
1021 orcaOrbitals.push_back(pieces[1]);
1022
1023 for (unsigned int i = 0; i < numColumns; ++i) {
1024 columns[i].push_back(
1025 Core::lexicalCast<double>(list[i]).value_or(0.0));
1026 }
1027
1028 getline(in, key);
1029 key_begin = std::sregex_iterator(key.begin(), key.end(), rx);
1030 key_end = std::sregex_iterator();
1031 list.clear();
1032 for (std::sregex_iterator i = key_begin; i != key_end; ++i) {
1033 list.push_back(i->str());
1034 }
1035
1036 if (list.size() != numColumns)
1037 break;
1038
1039 } // ok, we've finished one batch of MO coeffs
1040 // now reorder the p orbitals from "orcaStyle" (pz, px,py)
1041 // to expected Avogadro (px,py,pz)
1042 std::size_t idx = 0;
1043 while (idx < orcaOrbitals.size()) {
1044 if (Core::contains(orcaOrbitals.at(idx), "pz")) {
1045 for (unsigned int i = 0; i < numColumns; i++) {
1046 if (idx + 1 >= columns[i].size())
1047 break;
1048 std::swap(columns[i].at(idx), columns[i].at(idx + 1));
1049 }
1050 idx++;
1051 for (unsigned int i = 0; i < numColumns; i++) {
1052 if (idx + 1 >= columns[i].size())
1053 break;
1054 std::swap(columns[i].at(idx), columns[i].at(idx + 1));
1055 }
1056 idx++;
1057 idx++;
1058 } else {
1059 idx++;
1060 }
1061 }
1062
1063 // Now we need to re-order the MO coeffs, so we insert one MO at a
1064 // time
1065 for (unsigned int i = 0; i < numColumns; ++i) {
1066 numRows = columns[i].size();
1067 for (unsigned int j = 0; j < numRows; ++j) {
1068 m_MOcoeffs.push_back(columns[i][j]);
1069 }
1070 }
1071 columns.clear();
1072 orcaOrbitals.clear();
1073
1074 } // finished parsing MOs
1075 if (m_MOcoeffs.size() != numRows * numRows) {
1076 m_orcaSuccess = false;
1077 }
1078 m_numBasisFunctions = numRows;
1079 if (m_openShell) {
1080 // TODO: parse both alpha and beta orbitals
1081 m_BetaMOcoeffs.clear(); // if the orbitals were punched multiple times
1082 m_betaOrbitalEnergy.clear(); // we can get the energies here
1083 getline(in, key);
1084 while (!Core::trimmed(key).empty()) {
1085 // currently reading the sequence number
1086 getline(in, key); // energies
1087
1088 list = Core::split(key, ' ');
1089 // convert these all to double and add to m_betaOrbitalEnergy
1090 for (unsigned int i = 0; i < list.size(); i++) {
1091 // convert from Hartree to eV
1092 m_betaOrbitalEnergy.push_back(
1093 Core::lexicalCast<double>(list[i]).value_or(0.0) *
1094 HARTREE_TO_EV);
1095 }
1096
1097 getline(in, key); // symmetries
1098 getline(in, key); // skip -----------
1099 getline(in, key); // now we've got coefficients
1100
1101 regex rx("[-]?[0-9]{1,2}[.][0-9]{6}");
1102 auto key_begin = std::sregex_iterator(key.begin(), key.end(), rx);
1103 auto key_end = std::sregex_iterator();
1104 list.clear();
1105 for (std::sregex_iterator i = key_begin; i != key_end; ++i) {
1106 list.push_back(i->str());
1107 }
1108
1109 numColumns = list.size();
1110 columns.resize(numColumns);
1111 while (list.size() > 0) {
1112 // get the '2s' or '1dx2y2' piece from the line
1113 // so we can re-order the orbitals later
1114 std::vector<std::string> pieces = Core::split(key, ' ');
1115 orcaOrbitals.push_back(pieces[1]);
1116
1117 for (unsigned int i = 0; i < numColumns; ++i) {
1118 columns[i].push_back(
1119 Core::lexicalCast<double>(list[i]).value_or(0.0));
1120 }
1121
1122 getline(in, key);
1123 auto inner_key_begin =
1124 std::sregex_iterator(key.begin(), key.end(), rx);
1125 auto inner_key_end = std::sregex_iterator();
1126 list.clear();
1127 for (std::sregex_iterator i = inner_key_begin; i != inner_key_end;
1128 ++i) {
1129 list.push_back(i->str());
1130 }
1131
1132 if (list.size() != numColumns)
1133 break;
1134
1135 } // ok, we've finished one batch of MO coeffs
1136 // now reorder the p orbitals from "orcaStyle" (pz, px,py) to
1137 // expected Avogadro (px,py,pz)
1138
1139 std::size_t idx = 0;
1140 while (idx < orcaOrbitals.size()) {
1141 if (Core::contains(orcaOrbitals.at(idx), "pz")) {
1142 for (unsigned int i = 0; i < numColumns; i++) {
1143 if (idx + 1 >= columns[i].size())
1144 break;
1145 std::swap(columns[i].at(idx), columns[i].at(idx + 1));
1146 }
1147 idx++;
1148 for (unsigned int i = 0; i < numColumns; i++) {
1149 if (idx + 1 >= columns[i].size())
1150 break;
1151 std::swap(columns[i].at(idx), columns[i].at(idx + 1));
1152 }
1153 idx++;
1154 idx++;
1155 } else {
1156 idx++;
1157 }
1158 }
1159
1160 // Now we need to re-order the MO coeffs, so we insert one MO at a
1161 // time
1162 for (unsigned int i = 0; i < numColumns; ++i) {
1163 numRows = columns[i].size();
1164 for (unsigned int j = 0; j < numRows; ++j) {
1165 m_BetaMOcoeffs.push_back(columns[i][j]);
1166 }
1167 }
1168 columns.clear();
1169 orcaOrbitals.clear();
1170
1171 if (Core::trimmed(key).empty())
1172 getline(in, key); // skip the blank line after the MOs
1173 } // finished parsing 2nd. MOs
1174 if (m_MOcoeffs.size() != numRows * numRows) {
1175 m_orcaSuccess = false;
1176 }
1177 m_numBasisFunctions = numRows;
1178 }
1179
1180 m_currentMode = NotParsing;
1181 break;
1182 }
1183 default:;
1184 } // end switch
1185 } // end if (mode)
1186 }
1187
1188 void ORCAOutput::load(GaussianSet* basis)
1189 {
1190 // Now load up our basis set
1191 basis->setElectronCount(m_electrons);
1192 if (m_openShell)
1193 basis->setScfType(Core::Uhf);
1194
1195 // Set up the GTO primitive counter, go through the shells and add them
1196 int nGTO = 0;
1197 int nSP = 0; // number of SP shells
1198 for (unsigned int i = 0; i < m_shellTypes.size(); ++i) {
1199 // Handle the SP case separately - this should possibly be a distinct type
1200 if (m_shellTypes.at(i) == GaussianSet::SP) {
1201 // SP orbital type - currently have to unroll into two shells
1202 int tmpGTO = nGTO;
1203 int s = basis->addBasis(m_shelltoAtom.at(i) - 1, GaussianSet::S);
1204 for (int j = 0; j < m_shellNums.at(i); ++j) {
1205 basis->addGto(s, m_c.at(nGTO), m_a.at(nGTO));
1206 ++nGTO;
1207 }
1208 int p = basis->addBasis(m_shelltoAtom.at(i) - 1, GaussianSet::P);
1209 for (int j = 0; j < m_shellNums.at(i); ++j) {
1210 basis->addGto(p, m_csp.at(nSP), m_a.at(tmpGTO));
1211 ++tmpGTO;
1212 ++nSP;
1213 }
1214 } else {
1215 int b = basis->addBasis(m_shelltoAtom.at(i) - 1, m_shellTypes.at(i));
1216 for (int j = 0; j < m_shellNums.at(i); ++j) {
1217 basis->addGto(b, m_c.at(nGTO), m_a.at(nGTO));
1218 ++nGTO;
1219 }
1220 }
1221 }
1222
1223 // Now to load in the MO coefficients
1224 if (m_MOcoeffs.size())
1225 basis->setMolecularOrbitals(m_MOcoeffs);
1226 if (m_BetaMOcoeffs.size())
1227 basis->setMolecularOrbitals(m_BetaMOcoeffs, Core::BasisSet::Beta);
1228
1229 if (m_orbitalEnergy.size())
1230 basis->setMolecularOrbitalEnergy(m_orbitalEnergy);
1231 if (m_betaOrbitalEnergy.size())
1232 basis->setMolecularOrbitalEnergy(m_betaOrbitalEnergy, Core::BasisSet::Beta);
1233
1234 // TODO: set orbital symmetries
1235 // TODO: set vibrational symmetries
1236
1237 m_homo = ceil(m_electrons / 2.0);
1238 if (m_MOcoeffs.size() > 0)
1239 basis->generateDensityMatrix();
1240 }
1241
1242 void ORCAOutput::parseMCD()
1243 {
1244 // look for files with "dipole-length.1.mcd"
1245 std::string filename = fileName();
1246 // probably something like test.out
1247 // we need to look for test.cis-el.dipole-length.1.mcd
1248 // remove the extension
1249 size_t pos = filename.find_last_of('.');
1250 if (pos != std::string::npos) {
1251 filename = filename.substr(0, pos);
1252 }
1253 filename += ".cis-el.dipole-length.1.mcd";
1254 std::ifstream in(filename.c_str());
1255 if (!in) {
1256 std::cerr << "Cannot open: " << filename << "\n";
1257 return;
1258 }
1259
1260 // read in line by line
1261 // frequency, mcd, absorption
1262 Real frequency, mcd, absorption;
1263 frequency = mcd = absorption = 0.0;
1264 while (in) {
1265 in >> frequency >> mcd >> absorption;
1266 if (!in)
1267 break;
1268
1269 // if frequency goes down, we are done
1270 // (or we hit the end of the file)
1271 if (m_magneticTransitions.size() > 0 &&
1272 frequency < m_magneticTransitions.back())
1273 break;
1274
1275 m_magneticTransitions.push_back(frequency);
1276 m_magneticCD.push_back(mcd);
1277 }
1278
1279 in.close();
1280 }
1281
1282 GaussianSet::orbital ORCAOutput::orbitalIdx(std::string txt)
1283 {
1284 if (txt == "S")
1285 return GaussianSet::S;
1286 if (txt == "SP")
1287 return GaussianSet::SP;
1288 if (txt == "P")
1289 return GaussianSet::P;
1290 if (txt == "D")
1291 return GaussianSet::D5; //// orca only uses Spherical - 5 d components
1292 if (txt == "D5")
1293 return GaussianSet::D5;
1294 if (txt == "F")
1295 return GaussianSet::F7; //// orca only uses Spherical - 7 f components
1296 if (txt == "F7")
1297 return GaussianSet::F7;
1298 if (txt == "G")
1299 return GaussianSet::G9; //// orca only uses Spherical - 9 g components
1300 if (txt == "G9")
1301 return GaussianSet::G9;
1302 if (txt == "H")
1303 return GaussianSet::H11; //// orca only uses Spherical - 11 h
1304 /// components
1305 if (txt == "H11")
1306 return GaussianSet::H11;
1307 if (txt == "I")
1308 return GaussianSet::I13; //// orca only uses Spherical - 13 i
1309 /// components
1310 if (txt == "I13")
1311 return GaussianSet::I13;
1312 return GaussianSet::UU;
1313 }
1314
1315 } // namespace Avogadro::QuantumIO