-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculation_util.cpp
More file actions
180 lines (158 loc) · 4.71 KB
/
calculation_util.cpp
File metadata and controls
180 lines (158 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "calculation_util.h"
#include "element.h"
#include <QDebug>
int calc_util::overallCharge(MolStruct const &mol)
{
int charge = 0;
for (Atom const &a : mol.atoms)
charge += a.charge;
return charge;
}
int calc_util::overallSpin(MolStruct const &mol)
{
QVector<int> bondCounts;
bondCounts.fill(0, mol.atoms.size());
for (auto const &b: mol.bonds)
{
bondCounts[b.to] += b.order;
bondCounts[b.from] += b.order;
}
int spin = 0;
bool spinValid = true;
for (int i = 0; i < mol.atoms.size(); ++i)
{
int charge = mol.atoms[i].charge;
Element element(mol.atoms[i].element);
if (element.number - charge < bondCounts[i])
{
// The atom has more bonds than it has total electrons
return 0;
continue;
}
int effectiveNumber = element.number + bondCounts[i] - charge;
if (effectiveNumber == 0) // H+
continue;
Element effectiveElement(effectiveNumber);
if (!effectiveElement.isValid())
{
qWarning() << "Could not determine effective element for spin: charge =" << effectiveNumber;
return 0;
}
int effectiveGroup = effectiveElement.group();
int atomSpin = -1;
if (effectiveGroup >= 16)
atomSpin = 18 - effectiveGroup;
else if (effectiveGroup >= 13)
atomSpin = effectiveGroup - 12;
else if (effectiveGroup >= 8)
{
spinValid = false;
atomSpin = 12 - effectiveGroup;
}
else if (effectiveGroup >= 3)
{
spinValid = false;
atomSpin = effectiveGroup - 2;
}
else if (effectiveGroup == 2)
atomSpin = 0;
else if (effectiveGroup == 1)
atomSpin = 1;
else if (effectiveGroup < 0 && effectiveGroup >= -7)
{
spinValid = false;
atomSpin = -effectiveGroup;
}
else if (effectiveGroup < -7 && effectiveGroup >= -14)
{
spinValid = false;
atomSpin = 14 + effectiveGroup;
}
else
{
qWarning() << "Could not determine effective group for spin: group =" << effectiveGroup;
return 0;
}
if (atomSpin < 0)
spinValid = false;
else
spin += atomSpin;
}
spin = spin + 1;
if (!spinValid)
return -spin;
return spin;
}
QVector<int> calc_util::atomSpins(const MolStruct &mol)
{
QVector<int> result;
QVector<int> bondCounts;
bondCounts.fill(0, mol.atoms.size());
result.reserve(mol.atoms.size());
for (auto const &b: mol.bonds)
{
bondCounts[b.to] += b.order;
bondCounts[b.from] += b.order;
}
for (int i = 0; i < mol.atoms.size(); ++i)
{
int charge = mol.atoms[i].charge;
Element element(mol.atoms[i].element);
if (element.number - charge < bondCounts[i])
{
// The atom has more bonds than it has total electrons
result.push_back(0);
continue;
}
int effectiveNumber = element.number + bondCounts[i] - charge;
if (effectiveNumber == 0) // H+
{
result.push_back(1);
continue;
}
Element effectiveElement(effectiveNumber);
if (!effectiveElement.isValid())
{
qWarning() << "Could not determine effective element for spin: charge =" << effectiveNumber;
result.push_back(0);
continue;
}
int effectiveGroup = effectiveElement.group();
int atomSpin = -1;
if (effectiveGroup >= 16)
atomSpin = 18 - effectiveGroup;
else if (effectiveGroup >= 13)
atomSpin = effectiveGroup - 12;
else if (effectiveGroup >= 8)
{
// spinValid = false;
atomSpin = 12 - effectiveGroup;
}
else if (effectiveGroup >= 3)
{
// spinValid = false;
atomSpin = effectiveGroup - 2;
}
else if (effectiveGroup == 2)
atomSpin = 0;
else if (effectiveGroup == 1)
atomSpin = 1;
else if (effectiveGroup < 0 && effectiveGroup >= -7)
{
// spinValid = false;
atomSpin = -effectiveGroup;
}
else if (effectiveGroup < -7 && effectiveGroup >= -14)
{
// spinValid = false;
atomSpin = 14 + effectiveGroup;
}
else
{
qWarning() << "Could not determine effective group for spin: group =" << effectiveGroup;
atomSpin = 0;
}
result.push_back(atomSpin + 1);
}
return result;
}