Debian Codegraph @

avogadro/qtplugins/yaehmop/yaehmopout.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 "yaehmopout.h"
7
8 #include <avogadro/core/vector.h>
9
10 #include <QDebug>
11 #include <QRegularExpression>
12 #include <QString>
13 #include <QVector>
14
15 namespace Avogadro::QtPlugins {
16
17 inline bool printAndReturnFalse(const QString& error)
18 {
19 qDebug() << "Error in YaehmopOut:" << error;
20 return false;
21 }
22
23 bool YaehmopOut::readBandData(const QString& data,
24 QVector<QVector<double>>& bands,
25 QVector<Vector3>& kpoints,
26 QVector<specialKPoint>& specialKPoints)
27 {
28 bands.clear();
29 kpoints.clear();
30 specialKPoints.clear();
31
32 QStringList lines =
33 data.split(QRegularExpression("[\r\n]"), Qt::SkipEmptyParts);
34
35 while (!lines.isEmpty() && !lines[0].contains("#BAND_DATA"))
36 lines.removeFirst();
37
38 if (lines.isEmpty())
39 return printAndReturnFalse("Band Data not found in readBandData()!");
40
41 // These get printed from the status file and are not needed...
42 foreach (const QString& line, lines) {
43 if (line.contains("Error value from Diagonalization"))
44 lines.removeOne(line);
45 if (line.contains(
46 "On entry to ZHEGV , parameter number 6 had an illegal value"))
47 lines.removeOne(line);
48 }
49
50 // Right here, lines.size() must be at least 8
51 if (lines.size() < 8)
52 return printAndReturnFalse("Band data is too few lines!");
53
54 long long ind = 2;
55 if (!lines[ind].contains("Special points"))
56 return printAndReturnFalse("Special points missing");
57 int numSpecialPoints = lines[ind].split(" ")[0].toInt();
58
59 // Now we know more details about how many lines should be here
60 if (lines.size() < 7 + numSpecialPoints)
61 return printAndReturnFalse("Too few lines of data in band data!");
62
63 ++ind;
64 if (!lines[ind].contains("k points"))
65 return printAndReturnFalse("k points missing");
66 size_t numKPoints = lines[ind].split(" ")[0].toInt();
67
68 ++ind;
69 if (!lines[ind].contains("orbitals in"))
70 return printAndReturnFalse("orbitals in missing");
71 ;
72 size_t numOrbitals = lines[ind].split(" ")[0].toInt();
73
74 for (int i = 0; i < numSpecialPoints; ++i) {
75 ++ind;
76 specialKPoint kp;
77 if (lines[ind].split(" ").size() < 4)
78 return printAndReturnFalse("Special points line too small");
79
80 kp.label = lines[ind].split(" ")[0];
81 // If the label is "GM", use "Γ" instead
82 // FIXME: our vtk can't currently display a unicode gamma like this. When
83 // we add a font that can, we can uncomment this.
84 // if (kp.label.toLower() == "gm")
85 // kp.label = QString::fromUtf8("Γ");
86 kp.coords = Vector3(lines[ind].split(" ")[1].toDouble(),
87 lines[ind].split(" ")[2].toDouble(),
88 lines[ind].split(" ")[3].toDouble());
89 specialKPoints.append(kp);
90 }
91
92 ++ind;
93 if (!lines[ind].contains("Begin band data"))
94 return printAndReturnFalse("Begin band data missing");
95
96 // This should be equal in size to the number of orbitals
97 bands.reserve(numOrbitals);
98 for (size_t i = 0; i < numOrbitals; ++i) {
99 bands.append(QVector<double>());
100 // This is how many points we should have in total
101 bands[i].reserve(numKPoints * (numSpecialPoints - 1) + 1);
102 }
103 // This is how many points we should have in total
104 kpoints.reserve(numKPoints * (numSpecialPoints - 1) + 1);
105
106 ++ind;
107 while (true) {
108 // Did we make it to the end without finishing the band data?
109 if (ind >= lines.size())
110 return printAndReturnFalse("END_BAND_DATA is missing!");
111
112 // Did we make it to the end?
113 if (lines[ind].contains("END_BAND_DATA"))
114 break;
115
116 // Read the k-point info
117 if (!lines[ind].contains("K point") || lines[ind].split(" ").size() < 6) {
118 return printAndReturnFalse("K point missing");
119 }
120
121 Vector3 kp = Vector3(lines[ind].split(" ")[3].toDouble(),
122 lines[ind].split(" ")[4].toDouble(),
123 lines[ind].split(" ")[5].toDouble());
124 kpoints.append(kp);
125
126 // Get the orbital energies
127 for (size_t j = 0; j < numOrbitals; ++j) {
128 ++ind;
129 bands[j].append(lines[ind].trimmed().toDouble());
130 }
131 ++ind;
132 }
133
134 // We made it!
135 return true;
136 }
137
138 } // namespace Avogadro::QtPlugins