Debian Codegraph @

avogadro/core/gaussianset.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 "gaussianset.h"
7
8 #include "molecule.h"
9
10 #include <cmath>
11 #include <iostream>
12
13 using std::cout;
14 using std::endl;
15
16 using std::vector;
17
18 namespace Avogadro::Core {
19
20 GaussianSet::GaussianSet() : m_numMOs(0), m_init(false), m_scfType(Rhf) {}
21
22 unsigned int GaussianSet::addBasis(unsigned int atom, orbital type)
23 {
24 // Count the number of independent basis functions
25 switch (type) {
26 case S:
27 ++m_numMOs;
28 break;
29 case P:
30 m_numMOs += 3;
31 break;
32 case SP:
33 m_numMOs += 4;
34 break;
35 case D:
36 m_numMOs += 6;
37 break;
38 case D5:
39 m_numMOs += 5;
40 break;
41 case F:
42 m_numMOs += 10;
43 break;
44 case F7:
45 m_numMOs += 7;
46 break;
47 case G:
48 m_numMOs += 15;
49 break;
50 case G9:
51 m_numMOs += 9;
52 break;
53 case H:
54 m_numMOs += 21;
55 break;
56 case H11:
57 m_numMOs += 11;
58 break;
59 case I:
60 m_numMOs += 28;
61 break;
62 case I13:
63 m_numMOs += 13;
64 break;
65 default:
66 // Should never hit here
67 ;
68 }
69 m_init = false;
70
71 // Add to the new data structure, delete the old soon
72 m_symmetry.push_back(type);
73 m_atomIndices.push_back(atom);
74 return static_cast<unsigned int>(m_symmetry.size() - 1);
75 }
76
77 unsigned int GaussianSet::addGto(unsigned int basis, double c, double a)
78 {
79 if (m_gtoIndices.size() == basis) {
80 m_gtoIndices.push_back(static_cast<unsigned int>(m_gtoA.size()));
81 } else if (m_gtoIndices.size() < basis) {
82 cout << "Error, attempted to add a GTO for a basis too early. GTOs must be "
83 << "added in order to ensure correct indexing.\n";
84 return 69696969;
85 }
86 m_gtoA.push_back(a);
87 m_gtoC.push_back(c);
88
89 return static_cast<unsigned int>(m_gtoA.size() - 1);
90 }
91
92 void GaussianSet::setMolecularOrbitals(const vector<double>& MOs,
93 ElectronType type)
94 {
95 if (!m_numMOs)
96 return;
97
98 m_init = false;
99
100 size_t index(0);
101 if (type == Beta)
102 index = 1;
103
104 // Some programs don't output all MOs, so we take the amount of data
105 // and divide by the number of atomic orbital functions.
106 unsigned int columns = static_cast<unsigned int>(MOs.size()) / m_numMOs;
107 // cout << " Add MOs: " << m_numMOs << columns << endl;
108
109 m_moMatrix[index].resize(m_numMOs, columns);
110
111 for (unsigned int j = 0; j < columns; ++j)
112 for (unsigned int i = 0; i < m_numMOs; ++i)
113 m_moMatrix[index].coeffRef(i, j) = MOs[i + j * m_numMOs];
114 }
115
116 void GaussianSet::setMolecularOrbitals(const vector<double>& MOs,
117 ElectronType type, Index idx)
118 {
119 if (!m_numMOs)
120 return;
121
122 size_t index = 0;
123 if (type == Beta)
124 index = 1;
125
126 unsigned int columns = static_cast<unsigned int>(MOs.size()) / m_numMOs;
127
128 MatrixX moMatrix;
129 moMatrix.resize(m_numMOs, columns);
130
131 for (unsigned int j = 0; j < columns; ++j)
132 for (unsigned int i = 0; i < m_numMOs; ++i)
133 moMatrix.coeffRef(i, j) = MOs[i + j * m_numMOs];
134
135 if (idx <= m_moMatrixSet[index].size())
136 m_moMatrixSet[index].resize(idx + 1);
137
138 m_moMatrixSet[index][idx] = moMatrix;
139 }
140
141 bool GaussianSet::setActiveSetStep(int index)
142 {
143 if (index >= static_cast<int>(m_moMatrixSet[0].size()) ||
144 index >= static_cast<int>(m_moMatrixSet[1].size())) {
145 return false;
146 }
147
148 if (index >= m_molecule->coordinate3dCount())
149 return false;
150
151 m_moMatrix[0] = m_moMatrixSet[0][index];
152 m_moMatrix[1] = m_moMatrixSet[1][index];
153 m_molecule->setCoordinate3d(index);
154 return true;
155 }
156
157 void GaussianSet::setMolecularOrbitalNumber(const vector<unsigned int>& nums,
158 ElectronType type)
159 {
160 if (type == Beta)
161 m_moNumber[1] = nums;
162 else
163 m_moNumber[0] = nums;
164 }
165
166 bool GaussianSet::setDensityMatrix(const MatrixX& m)
167 {
168 m_density.resize(m.rows(), m.cols());
169 m_density = m;
170 return true;
171 }
172
173 bool GaussianSet::setSpinDensityMatrix(const MatrixX& m)
174 {
175 m_spinDensity.resize(m.rows(), m.cols());
176 m_spinDensity = m;
177 return true;
178 }
179
180 unsigned int GaussianSet::molecularOrbitalCount(ElectronType type) const
181 {
182 size_t index(0);
183 if (type == Beta)
184 index = 1;
185 return static_cast<unsigned int>(m_moMatrix[index].rows());
186 }
187
188 void GaussianSet::outputAll(ElectronType type)
189 {
190 size_t index(0);
191 if (type == Beta)
192 index = 1;
193
194 // Can be called to print out a summary of the basis set as read in
195 auto numAtoms = static_cast<unsigned int>(m_molecule->atomCount());
196 cout << "\nGaussian Basis Set\nNumber of atoms:" << numAtoms << endl;
197 switch (m_scfType) {
198 case Rhf:
199 cout << "RHF orbitals" << endl;
200 break;
201 case Uhf:
202 cout << "UHF orbitals" << endl;
203 break;
204 case Rohf:
205 cout << "ROHF orbitals" << endl;
206 break;
207 default:
208 cout << "Unknown orbitals" << endl;
209 }
210
211 initCalculation();
212
213 cout << "Number of electrons = " << m_electrons[index] << endl;
214
215 if (!isValid()) {
216 cout << "Basis set is marked as invalid." << endl;
217 return;
218 }
219
220 for (size_t i = 0; i < m_symmetry.size(); ++i) {
221 cout << i << "\tAtom Index: " << m_atomIndices[i]
222 << "\tSymmetry: " << m_symmetry[i] << "\tMO Index: " << m_moIndices[i]
223 << "\tGTO Index: " << m_gtoIndices[i] << endl;
224 }
225 cout << "Symmetry: " << m_symmetry.size()
226 << "\tgtoIndices: " << m_gtoIndices.size()
227 << "\tLast gtoIndex: " << m_gtoIndices[m_symmetry.size()]
228 << "\ngto size: " << m_gtoA.size() << " " << m_gtoC.size() << " "
229 << m_gtoCN.size() << endl;
230 for (size_t i = 0; i < m_symmetry.size(); ++i) {
231 switch (m_symmetry[i]) {
232 case S:
233 cout << "Shell " << i << "\tS\n MO 1\t"
234 << m_moMatrix[index](0, m_moIndices[i]) << "\t"
235 << m_moMatrix[index](m_moIndices[i], 0) << endl;
236 break;
237 case P:
238 cout << "Shell " << i << "\tP\n MO 1\t"
239 << m_moMatrix[index](0, m_moIndices[i]) << "\t"
240 << m_moMatrix[index](0, m_moIndices[i] + 1) << "\t"
241 << m_moMatrix[index](0, m_moIndices[i] + 2) << endl;
242 break;
243 case D:
244 cout << "Shell " << i << "\tD\n MO 1\t"
245 << m_moMatrix[index](0, m_moIndices[i]) << "\t"
246 << m_moMatrix[index](0, m_moIndices[i] + 1) << "\t"
247 << m_moMatrix[index](0, m_moIndices[i] + 2) << "\t"
248 << m_moMatrix[index](0, m_moIndices[i] + 3) << "\t"
249 << m_moMatrix[index](0, m_moIndices[i] + 4) << "\t"
250 << m_moMatrix[index](0, m_moIndices[i] + 5) << endl;
251 break;
252 case D5:
253 cout << "Shell " << i << "\tD5\n MO 1\t"
254 << m_moMatrix[index](0, m_moIndices[i]) << "\t"
255 << m_moMatrix[index](0, m_moIndices[i] + 1) << "\t"
256 << m_moMatrix[index](0, m_moIndices[i] + 2) << "\t"
257 << m_moMatrix[index](0, m_moIndices[i] + 3) << "\t"
258 << m_moMatrix[index](0, m_moIndices[i] + 4) << endl;
259 break;
260 case F:
261 cout << "Shell " << i << "\tF\n MO 1";
262 for (short j = 0; j < 10; ++j)
263 cout << "\t" << m_moMatrix[index](0, m_moIndices[i] + j);
264 cout << endl;
265 break;
266 case F7:
267 cout << "Shell " << i << "\tF7\n MO 1";
268 for (short j = 0; j < 7; ++j)
269 cout << "\t" << m_moMatrix[index](0, m_moIndices[i] + j);
270 cout << endl;
271 break;
272 default:
273 cout << "Error: unhandled type...\n";
274 }
275 unsigned int cIndex = m_gtoIndices[i];
276 for (size_t j = m_gtoIndices[i]; j < m_gtoIndices[i + 1]; ++j) {
277 if (j >= m_gtoA.size()) {
278 cout << "Error, j is too large!" << j << m_gtoA.size() << endl;
279 continue;
280 }
281 cout << cIndex << "\tc: " << m_gtoC[cIndex] << "\ta: " << m_gtoA[cIndex]
282 << endl;
283 ++cIndex;
284 }
285 }
286 cout << "\nEnd of orbital data...\n";
287 }
288
289 bool GaussianSet::isValid()
290 {
291 // TODO: Something useful here again - check the basis set makes sense...
292 return true;
293 }
294
295 void GaussianSet::initCalculation()
296 {
297 if (m_init)
298 return;
299
300 // This currently just involves normalising all contraction coefficients
301 m_gtoCN.clear();
302
303 // Initialise the new data structures that are hopefully more efficient
304 unsigned int indexMO = 0;
305 unsigned int skip = 0; // for unimplemented shells
306
307 m_moIndices.resize(m_symmetry.size());
308 // Add a final entry to the gtoIndices
309 m_gtoIndices.push_back(static_cast<unsigned int>(m_gtoA.size()));
310 for (unsigned int i = 0; i < m_symmetry.size(); ++i) {
311 switch (m_symmetry[i]) {
312 case S:
313 m_moIndices[i] = indexMO++;
314 m_cIndices.push_back(static_cast<unsigned int>(m_gtoCN.size()));
315 // Normalization of the S-type orbitals (normalization used in JMol)
316 // (8 * alpha^3 / pi^3)^0.25 * exp(-alpha * r^2)
317 for (unsigned j = m_gtoIndices[i]; j < m_gtoIndices[i + 1]; ++j) {
318 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 0.75) * 0.71270547);
319 }
320 break;
321 case P:
322 m_moIndices[i] = indexMO;
323 indexMO += 3;
324 m_cIndices.push_back(static_cast<unsigned int>(m_gtoCN.size()));
325 // Normalization of the P-type orbitals (normalization used in JMol)
326 // (128 alpha^5 / pi^3)^0.25 * [x|y|z]exp(-alpha * r^2)
327 for (unsigned j = m_gtoIndices[i]; j < m_gtoIndices[i + 1]; ++j) {
328 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 1.25) * 1.425410941);
329 m_gtoCN.push_back(m_gtoCN.back());
330 m_gtoCN.push_back(m_gtoCN.back());
331 }
332 break;
333 case D:
334 // Cartesian - 6 d components
335 // Order in xx, yy, zz, xy, xz, yz
336 m_moIndices[i] = indexMO;
337 indexMO += 6;
338 m_cIndices.push_back(static_cast<unsigned int>(m_gtoCN.size()));
339 // Normalization of the P-type orbitals (normalization used in JMol)
340 // xx|yy|zz: (2048 alpha^7/9pi^3)^0.25 [xx|yy|zz]exp(-alpha r^2)
341 // xy|xz|yz: (2048 alpha^7/pi^3)^0.25 [xy|xz|yz]exp(-alpha r^2)
342 for (unsigned j = m_gtoIndices[i]; j < m_gtoIndices[i + 1]; ++j) {
343 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 1.75) * 1.645922781);
344 m_gtoCN.push_back(m_gtoCN.back());
345 m_gtoCN.push_back(m_gtoCN.back());
346
347 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 1.75) * 2.850821881);
348 m_gtoCN.push_back(m_gtoCN.back());
349 m_gtoCN.push_back(m_gtoCN.back());
350 }
351 break;
352 case D5:
353 // Spherical - 5 d components
354 // Order in d0, d+1, d-1, d+2, d-2
355 // Form d(z^2-r^2), dxz, dyz, d(x^2-y^2), dxy
356 m_moIndices[i] = indexMO;
357 indexMO += 5;
358 m_cIndices.push_back(static_cast<unsigned int>(m_gtoCN.size()));
359 for (unsigned j = m_gtoIndices[i]; j < m_gtoIndices[i + 1]; ++j) {
360 m_gtoCN.push_back(
361 m_gtoC[j] *
362 pow(2048 * pow(m_gtoA[j], 7.0) / (9.0 * M_PI * M_PI * M_PI), 0.25));
363 m_gtoCN.push_back(
364 m_gtoC[j] *
365 pow(2048 * pow(m_gtoA[j], 7.0) / (M_PI * M_PI * M_PI), 0.25));
366 m_gtoCN.push_back(m_gtoCN.back());
367 // I think this is correct but really need to check...
368 m_gtoCN.push_back(
369 m_gtoC[j] *
370 pow(128 * pow(m_gtoA[j], 7.0) / (M_PI * M_PI * M_PI), 0.25));
371 m_gtoCN.push_back(
372 m_gtoC[j] *
373 pow(2048 * pow(m_gtoA[j], 7.0) / (M_PI * M_PI * M_PI), 0.25));
374 }
375 break;
376 case F:
377 /*
378 Thanks, Jmol
379 Cartesian forms for f (l = 3) basis functions:
380 Type Normalization
381 xxx [(32768 * alpha^9) / (225 * pi^3))]^(1/4)
382 xxy [(32768 * alpha^9) / (9 * pi^3))]^(1/4)
383 xxz [(32768 * alpha^9) / (9 * pi^3))]^(1/4)
384 xyy [(32768 * alpha^9) / (9 * pi^3))]^(1/4)
385 xyz [(32768 * alpha^9) / (1 * pi^3))]^(1/4)
386 xzz [(32768 * alpha^9) / (9 * pi^3))]^(1/4)
387 yyy [(32768 * alpha^9) / (225 * pi^3))]^(1/4)
388 yyz [(32768 * alpha^9) / (9 * pi^3))]^(1/4)
389 yzz [(32768 * alpha^9) / (9 * pi^3))]^(1/4)
390 zzz [(32768 * alpha^9) / (225 * pi^3))]^(1/4)
391
392 Thank you, Python
393 pi = 3.141592653589793
394 (32768./225./(pi**3.))**(0.25) = 1.4721580892990938
395 (32768./9./(pi**3.))**(0.25) = 3.291845561298979
396 (32768./(pi**3.))**(0.25) = 5.701643762839922
397 */
398 {
399 double norm1 = 1.4721580892990938;
400 double norm2 = 3.291845561298979;
401 double norm3 = 5.701643762839922;
402 m_moIndices[i] = indexMO;
403 indexMO += 10;
404 m_cIndices.push_back(static_cast<unsigned int>(m_gtoCN.size()));
405 for (unsigned j = m_gtoIndices[i]; j < m_gtoIndices[i + 1]; ++j) {
406 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm1); // xxx
407 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm2); // xxy
408 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm2); // xxz
409 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm2); // xyy
410 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm3); // xyz
411 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm2); // xzz
412 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm1); // yyy
413 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm2); // yyz
414 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm2); // yzz
415 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm1); // zzz
416 }
417 }
418 break;
419 case F7: {
420 // m-independent normalization factor
421 // math.sqrt(2.**(3.+3./2.))/(math.pi**(3./4.))*math.sqrt(2.**3. / 15.)
422 // same as norm1 above.
423 double norm = 1.4721580892990935;
424 m_moIndices[i] = indexMO;
425 indexMO += 7;
426 m_cIndices.push_back(static_cast<unsigned int>(m_gtoCN.size()));
427 for (unsigned j = m_gtoIndices[i]; j < m_gtoIndices[i + 1]; ++j) {
428 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm); // 0
429 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm); //+1
430 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm); //-1
431 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm); //+2
432 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm); //-2
433 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm); //+3
434 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.25) * norm); //-3
435 }
436 } break;
437 case G: {
438 // 16 * (2.0/pi)^0.75
439 double norm = 11.403287525679843;
440 double norm1 = norm / sqrt(7.0);
441 double norm2 = norm / sqrt(35.0 / 3.0);
442 double norm3 = norm / sqrt(35.0);
443 m_moIndices[i] = indexMO;
444 indexMO += 15;
445 m_cIndices.push_back(static_cast<unsigned int>(m_gtoCN.size()));
446 for (unsigned j = m_gtoIndices[i]; j < m_gtoIndices[i + 1]; ++j) {
447 // molden order
448 // xxxx yyyy zzzz xxxy xxxz yyyx yyyz zzzx zzzy,
449 // xxyy xxzz yyzz xxyz yyxz zzxy
450 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm); // xxxx
451 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm); // yyyy
452 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm); // zzzz
453 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm1); // xxxy
454 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm1); // xxxz
455 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm1); // yyyx
456 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm1); // yyyz
457 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm1); // zzzx
458 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm1); // zzzy
459 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm2); // xxyy
460 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm2); // xxzz
461 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm2); // yyzz
462 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm3); // xxyz
463 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm3); // yyxz
464 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm3); // zzxy
465 }
466 } break;
467 case G9: {
468 // 16 * (2.0/pi)^0.75
469 double norm = 11.403287525679843;
470 m_moIndices[i] = indexMO;
471 indexMO += 9;
472 m_cIndices.push_back(static_cast<unsigned int>(m_gtoCN.size()));
473 for (unsigned j = m_gtoIndices[i]; j < m_gtoIndices[i + 1]; ++j) {
474 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm); // 0
475 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm); //+1
476 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm); //-1
477 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm); //+2
478 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm); //-2
479 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm); //+3
480 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm); //-3
481 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm); //+4
482 m_gtoCN.push_back(m_gtoC[j] * pow(m_gtoA[j], 2.75) * norm); //-4
483 }
484 } break;
485 case H:
486 skip = 21;
487 break;
488 case H11:
489 skip = 11;
490 break;
491 case I:
492 skip = 28;
493 break;
494 case I13:
495 skip = 13;
496 break;
497 default:
498 cout << "Basis set not handled - results may be incorrect.\n";
499 }
500 if (skip) {
501 cout << "Basis set not handled - results may be incorrect.\n";
502 m_moIndices[i] = indexMO;
503 indexMO += skip;
504 m_cIndices.push_back(static_cast<unsigned int>(m_gtoCN.size()));
505 skip = 0;
506 }
507 }
508 m_init = true;
509 }
510
511 bool GaussianSet::generateDensityMatrix()
512 {
513 if (m_scfType == Unknown)
514 return false;
515
516 if (m_moMatrix[0].size() == 0)
517 return false;
518 // check if it's Rohf or Uhf too
519 if (m_scfType != Rhf && m_moMatrix[1].size() == 0)
520 return false;
521
522 m_density.resize(m_numMOs, m_numMOs);
523 m_density = MatrixX::Zero(m_numMOs, m_numMOs);
524 for (unsigned int iBasis = 0; iBasis < m_numMOs; ++iBasis) {
525 for (unsigned int jBasis = 0; jBasis <= iBasis; ++jBasis) {
526 switch (m_scfType) {
527 case Rhf:
528 for (unsigned int iMO = 0; iMO < m_electrons[0] / 2; ++iMO) {
529 double icoeff = m_moMatrix[0](iBasis, iMO);
530 double jcoeff = m_moMatrix[0](jBasis, iMO);
531 m_density(jBasis, iBasis) += 2.0 * icoeff * jcoeff;
532 m_density(iBasis, jBasis) = m_density(jBasis, iBasis);
533 }
534 // cout << iBasis << ", " << jBasis << ": " <<
535 // m_density(iBasis, jBasis)
536 // << endl;
537 break;
538 case Rohf: // ROHF is handled similarly to UHF
539 case Uhf:
540 for (unsigned int iaMO = 0; iaMO < m_electrons[0]; ++iaMO) {
541 double icoeff = m_moMatrix[0](iBasis, iaMO);
542 double jcoeff = m_moMatrix[0](jBasis, iaMO);
543 m_density(jBasis, iBasis) += icoeff * jcoeff;
544 m_density(iBasis, jBasis) = m_density(jBasis, iBasis);
545 }
546 for (unsigned int ibMO = 0; ibMO < m_electrons[1]; ibMO++) {
547 double icoeff = m_moMatrix[1](iBasis, ibMO);
548 double jcoeff = m_moMatrix[1](jBasis, ibMO);
549 m_density(jBasis, iBasis) += icoeff * jcoeff;
550 m_density(iBasis, jBasis) = m_density(jBasis, iBasis);
551 }
552 // cout << iBasis << ", " << jBasis << ": " <<
553 // m_density(iBasis, jBasis)
554 // << endl;
555 break;
556 default:
557 cout << "Unhandled scf type:" << m_scfType << endl;
558 }
559 }
560 }
561 return true;
562 }
563
564 bool GaussianSet::generateSpinDensityMatrix()
565 {
566 if (m_scfType != Uhf)
567 return false;
568
569 m_spinDensity.resize(m_numMOs, m_numMOs);
570 m_spinDensity = MatrixX::Zero(m_numMOs, m_numMOs);
571 for (unsigned int iBasis = 0; iBasis < m_numMOs; ++iBasis) {
572 for (unsigned int jBasis = 0; jBasis <= iBasis; ++jBasis) {
573 for (unsigned int iaMO = 0; iaMO < m_electrons[0]; ++iaMO) {
574 double icoeff = m_moMatrix[0](iBasis, iaMO);
575 double jcoeff = m_moMatrix[0](jBasis, iaMO);
576 m_spinDensity(jBasis, iBasis) += icoeff * jcoeff;
577 m_spinDensity(iBasis, jBasis) = m_spinDensity(jBasis, iBasis);
578 }
579 for (unsigned int ibMO = 0; ibMO < m_electrons[1]; ++ibMO) {
580 double icoeff = m_moMatrix[1](iBasis, ibMO);
581 double jcoeff = m_moMatrix[1](jBasis, ibMO);
582 m_spinDensity(jBasis, iBasis) -= icoeff * jcoeff;
583 m_spinDensity(iBasis, jBasis) = m_spinDensity(jBasis, iBasis);
584 }
585 cout << iBasis << ", " << jBasis << ": " << m_spinDensity(iBasis, jBasis)
586 << endl;
587 }
588 }
589 return true;
590 }
591
592 } // namespace Avogadro::Core