Debian Codegraph @

avogadro/core/slatersettools.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 "slatersettools.h"
7
8 #include "molecule.h"
9 #include "slaterset.h"
10
11 namespace Avogadro::Core {
12
13 SlaterSetTools::SlaterSetTools(Molecule* mol) : m_molecule(mol)
14 {
15 if (m_molecule)
16 m_basis = dynamic_cast<SlaterSet*>(m_molecule->basisSet());
17 }
18
19 double SlaterSetTools::calculateMolecularOrbital(const Vector3& position,
20 int mo) const
21 {
22 if (mo > static_cast<int>(m_basis->molecularOrbitalCount()))
23 return 0.0;
24
25 std::vector<double> values(calculateValues(position));
26
27 const MatrixX& matrix = m_basis->normalizedMatrix();
28 int matrixSize(static_cast<int>(matrix.rows()));
29 int indexMO(mo);
30
31 // Now calculate the value of the density at this point in space
32 double result(0.0);
33 for (int i = 0; i < matrixSize; ++i)
34 result += matrix(i, indexMO) * values[i];
35
36 return result;
37 }
38
39 double SlaterSetTools::calculateElectronDensity(const Vector3& position) const
40 {
41 const MatrixX& matrix = m_basis->densityMatrix();
42 int matrixSize(static_cast<int>(m_basis->normalizedMatrix().rows()));
43 if (matrix.rows() != matrixSize || matrix.cols() != matrixSize)
44 return 0.0;
45
46 std::vector<double> values(calculateValues(position));
47
48 // Now calculate the value of the density at this point in space
49 double rho(0.0);
50 for (int i = 0; i < matrixSize; ++i) {
51 // Calculate the off-diagonal parts of the matrix
52 for (int j = 0; j < i; ++j)
53 rho += 2.0 * matrix(i, j) * (values[i] * values[j]);
54 // Now calculate the matrix diagonal
55 rho += matrix(i, i) * (values[i] * values[i]);
56 }
57
58 return rho;
59 }
60
61 double SlaterSetTools::calculateSpinDensity(const Vector3&) const
62 {
63 return 0.0;
64 }
65
66 bool SlaterSetTools::isValid() const
67 {
68 return (m_molecule != nullptr) &&
69 (dynamic_cast<SlaterSet*>(m_molecule->basisSet()) != nullptr);
70 }
71
72 inline bool SlaterSetTools::isSmall(double val) const
73 {
74 return std::abs(val) < 1e-20;
75 }
76
77 std::vector<double> SlaterSetTools::calculateValues(
78 const Vector3& position) const
79 {
80 m_basis->initCalculation();
81
82 Index atomsSize = m_molecule->atomCount();
83 size_t basisSize = m_basis->zetas().size();
84
85 const std::vector<int>& slaterIndices = m_basis->slaterIndices();
86 const std::vector<int>& slaterTypes = m_basis->slaterTypes();
87 const std::vector<int>& PQNs = m_basis->PQNs();
88 const std::vector<double>& factors = m_basis->factors();
89 const std::vector<double>& zetas = m_basis->zetas();
90
91 std::vector<Vector3> deltas;
92 std::vector<double> dr2;
93 deltas.reserve(atomsSize);
94 dr2.reserve(atomsSize);
95
96 // Calculate the deltas for the position
97 for (Index i = 0; i < atomsSize; ++i) {
98 deltas.emplace_back(position - m_molecule->atom(i).position3d());
99 dr2.push_back(deltas[i].squaredNorm());
100 }
101
102 // Allocate space for the values to be calculated.
103 std::vector<double> values;
104 values.resize(basisSize);
105
106 // Now calculate the values at this point in space
107 for (size_t i = 0; i < basisSize; ++i) {
108 double dr(dr2[slaterIndices[i]]);
109 Vector3 delta(deltas[slaterIndices[i]]);
110 values[i] = factors[i] * exp(-zetas[i] * dr);
111 // Radial part with effective PQNs
112 for (int j = 0; j < PQNs[i]; ++j)
113 values[i] *= dr;
114 switch (slaterTypes[i]) {
115 case SlaterSet::S:
116 break;
117 case SlaterSet::PX:
118 values[i] *= delta.x();
119 break;
120 case SlaterSet::PY:
121 values[i] *= delta.y();
122 break;
123 case SlaterSet::PZ:
124 values[i] *= delta.z();
125 break;
126 case SlaterSet::X2: // (x^2 - y^2)r^n
127 values[i] *= delta.x() * delta.x() - delta.y() * delta.y();
128 break;
129 case SlaterSet::XZ: // xzr^n
130 values[i] *= delta.x() * delta.z();
131 break;
132 case SlaterSet::Z2: // (2z^2 - x^2 - y^2)r^n
133 values[i] *= 2.0 * delta.z() * delta.z() - delta.x() * delta.x() -
134 delta.y() * delta.y();
135 break;
136 case SlaterSet::YZ: // yzr^n
137 values[i] *= delta.y() * delta.z();
138 break;
139 case SlaterSet::XY: // xyr^n
140 values[i] *= delta.x() * delta.y();
141 break;
142 default:
143 values[i] = 0.0;
144 }
145 }
146
147 return values;
148 }
149
150 } // namespace Avogadro::Core