Debian Codegraph @

avogadro/core/gaussiansettools.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 "gaussiansettools.h"
7
8 #include "cube.h"
9 #include "gaussianset.h"
10 #include "molecule.h"
11
12 #include <iostream>
13
14 using std::vector;
15
16 namespace Avogadro::Core {
17
18 GaussianSetTools::GaussianSetTools(Molecule* mol) : m_molecule(mol)
19 {
20 if (m_molecule) {
21 m_basis = dynamic_cast<GaussianSet*>(m_molecule->basisSet());
22 m_cutoffDistances.resize(7, 0.0); // s, p, d, f, g, h, i (for now)
23
24 // Pre-compute atom positions in Bohr as 3 x N matrix for vectorized ops
25 Index atomsSize = m_molecule->atomCount();
26 m_atomPositionsBohr.resize(3, atomsSize);
27 for (Index i = 0; i < atomsSize; ++i) {
28 m_atomPositionsBohr.col(i) =
29 m_molecule->atom(i).position3d() * ANGSTROM_TO_BOHR;
30 }
31
32 // Initialize the basis set calculation once (normalizes coefficients, etc.)
33 // This avoids repeated initCalculation() calls in calculateValues()
34 if (m_basis) {
35 m_basis->initCalculation();
36 // Pre-compute cutoff distances to ignore v. small exp(-alpha * r^2) terms
37 calculateCutoffs();
38 }
39 }
40 }
41
42 bool GaussianSetTools::calculateMolecularOrbital(Cube& cube, int moNumber) const
43 {
44 for (size_t i = 0; i < cube.data()->size(); ++i) {
45 Vector3 pos = cube.position(i);
46 cube.setValue(i, calculateMolecularOrbital(pos, moNumber));
47 }
48 return true;
49 }
50
51 double GaussianSetTools::calculateMolecularOrbital(const Vector3& position,
52 int mo) const
53 {
54 if (mo > static_cast<int>(m_basis->molecularOrbitalCount()))
55 return 0.0;
56
57 Eigen::VectorXd values;
58 calculateValues(position, values);
59
60 const MatrixX& matrix = m_basis->moMatrix(m_type);
61
62 // Use Eigen's optimized dot product
63 return matrix.col(mo).dot(values);
64 }
65
66 bool GaussianSetTools::calculateElectronDensity(Cube& cube) const
67 {
68 const MatrixX& matrix = m_basis->densityMatrix();
69 if (matrix.rows() == 0 || matrix.cols() == 0) {
70 // we don't have a density matrix, so generate one
71 m_basis->generateDensityMatrix();
72 }
73
74 for (size_t i = 0; i < cube.data()->size(); ++i) {
75 Vector3 pos = cube.position(i);
76 cube.setValue(i, calculateElectronDensity(pos));
77 }
78 return true;
79 }
80
81 double GaussianSetTools::calculateElectronDensity(const Vector3& position) const
82 {
83 const MatrixX& matrix = m_basis->densityMatrix();
84 int matrixSize(static_cast<int>(m_basis->moMatrix().rows()));
85
86 if (matrix.rows() != matrixSize || matrix.cols() != matrixSize) {
87 return 0.0;
88 }
89
90 Eigen::VectorXd values;
91 calculateValues(position, values);
92
93 // Compute the quadratic form: v^T * D * v
94 // The density matrix is symmetric, so we can use the full matrix multiply
95 return values.dot(matrix * values);
96 }
97
98 bool GaussianSetTools::calculateSpinDensity(Cube& cube) const
99 {
100 for (size_t i = 0; i < cube.data()->size(); ++i) {
101 Vector3 pos = cube.position(i);
102 cube.setValue(i, calculateSpinDensity(pos));
103 }
104 return true;
105 }
106
107 double GaussianSetTools::calculateSpinDensity(const Vector3& position) const
108 {
109 const MatrixX& matrix = m_basis->spinDensityMatrix();
110 int matrixSize(static_cast<int>(m_basis->moMatrix().rows()));
111 if (matrix.rows() != matrixSize || matrix.cols() != matrixSize) {
112 return 0.0;
113 }
114
115 Eigen::VectorXd values;
116 calculateValues(position, values);
117
118 // Compute the quadratic form: v^T * D * v
119 // The spin density matrix is symmetric, so we can use the full matrix
120 // multiply
121 return values.dot(matrix * values);
122 }
123
124 bool GaussianSetTools::isValid() const
125 {
126 return (m_molecule != nullptr) &&
127 (dynamic_cast<GaussianSet*>(m_molecule->basisSet()) != nullptr);
128 }
129
130 inline bool GaussianSetTools::isSmall(double val) const
131 {
132 return std::abs(val) < 1e-12;
133 }
134
135 inline void GaussianSetTools::calculateCutoffs()
136 {
137 // Guesstimate a distance we can ignore the exp(-alpha * r^2) term
138 // .. because it's negligible
139 // This will depend on the angular momentum of the basis function
140 // .. so we calculate it for whatever L values in this basis set
141
142 const double threshold = 0.03 * 0.001; // 0.1% of a typical isovalue
143 const double maxDistance = 100.0; // just in case (diffuse functions)
144
145 // get the exponents and normalized coefficients
146 const std::vector<double>& exponents = m_basis->gtoA();
147 const std::vector<double>& coefficients = m_basis->gtoCN();
148 const std::vector<int>& sym = m_basis->symmetry();
149
150 // we loop through the "symmetry" (i.e., L values in this basis set)
151 for (size_t i = 0; i < sym.size(); ++i) {
152 int L = symToL[sym[i]];
153
154 // this is a hack, since not all coefficients will be the same
155 // .. but it's a good approximation since they'll be similar
156 unsigned int cIndex = m_basis->cIndices()[i];
157 const double coeff = std::abs(coefficients[cIndex]);
158
159 // now loop through all exponents for this L value
160 // (e.g., multiple terms - we don't know which is the most diffuse)
161 for (unsigned int j = m_basis->gtoIndices()[i];
162 j < m_basis->gtoIndices()[i + 1]; ++j) {
163 double alpha = exponents[j];
164 // except for S, we don't want to start at the origin
165 double r = std::min(maxDistance, std::sqrt(L / (2 * alpha)));
166 double value = coeff * std::pow(r, L) * std::exp(-alpha * r * r);
167
168 while (value > threshold && r < maxDistance) {
169 r += 0.25;
170 value = coeff * std::pow(r, L) * std::exp(-alpha * r * r);
171 }
172
173 m_cutoffDistances[L] = std::max(m_cutoffDistances[L], r * r);
174 }
175 }
176 }
177
178 inline void GaussianSetTools::calculateValues(const Vector3& position,
179 Eigen::VectorXd& values) const
180 {
181 // Note: initCalculation() is called once in the constructor
182 Index atomsSize = static_cast<Index>(m_atomPositionsBohr.cols());
183 size_t basisSize = m_basis->symmetry().size();
184 const std::vector<int>& basis = m_basis->symmetry();
185 const std::vector<unsigned int>& atomIndices = m_basis->atomIndices();
186
187 // Calculate our position
188 Vector3 pos(position * ANGSTROM_TO_BOHR);
189
190 // Vectorized computation of deltas and squared distances
191 // deltas: 3 x N matrix where each column is (pos - atomPos[i])
192 Eigen::Matrix<double, 3, Eigen::Dynamic> deltas =
193 (-m_atomPositionsBohr).colwise() + pos;
194 // dr2: vector of squared distances for each atom
195 Eigen::VectorXd dr2 = deltas.colwise().squaredNorm();
196
197 // Resize and zero the output vector
198 Index matrixSize = m_basis->moMatrix().rows();
199 values.setZero(matrixSize);
200
201 // Now calculate the values at this point in space
202 for (unsigned int i = 0; i < basisSize; ++i) {
203 // bail early if the distance is too big
204 unsigned int atomIdx = atomIndices[i];
205 double cutoff = m_cutoffDistances[symToL[basis[i]]];
206 if (dr2(atomIdx) > cutoff)
207 continue;
208
209 // Get delta vector for this atom (column of the deltas matrix)
210 Vector3 delta = deltas.col(atomIdx);
211 double dr2_i = dr2(atomIdx);
212
213 switch (basis[i]) {
214 case GaussianSet::S:
215 pointS(i, dr2_i, values);
216 break;
217 case GaussianSet::P:
218 pointP(i, delta, dr2_i, values);
219 break;
220 case GaussianSet::D:
221 pointD(i, delta, dr2_i, values);
222 break;
223 case GaussianSet::D5:
224 pointD5(i, delta, dr2_i, values);
225 break;
226 case GaussianSet::F:
227 pointF(i, delta, dr2_i, values);
228 break;
229 case GaussianSet::F7:
230 pointF7(i, delta, dr2_i, values);
231 break;
232 case GaussianSet::G:
233 pointG(i, delta, dr2_i, values);
234 break;
235 case GaussianSet::G9:
236 pointG9(i, delta, dr2_i, values);
237 break;
238 default:
239 // Not handled - return a zero contribution
240 ;
241 }
242 }
243 }
244
245 inline void GaussianSetTools::pointS(unsigned int moIndex, double dr2,
246 Eigen::VectorXd& values) const
247 {
248 // S type orbitals - the simplest of the calculations with one component
249 const vector<double>& gtoA = m_basis->gtoA();
250 const vector<double>& gtoCN = m_basis->gtoCN();
251 const vector<unsigned int>& gtoIndices = m_basis->gtoIndices();
252
253 double tmp = 0.0;
254 unsigned int cIndex = m_basis->cIndices()[moIndex];
255 for (unsigned int i = gtoIndices[moIndex]; i < gtoIndices[moIndex + 1]; ++i) {
256 tmp += gtoCN[cIndex++] * exp(-gtoA[i] * dr2);
257 }
258 // There is one MO coefficient per S shell basis.
259 values[m_basis->moIndices()[moIndex]] = tmp;
260 }
261
262 inline void GaussianSetTools::pointP(unsigned int moIndex, const Vector3& delta,
263 double dr2, Eigen::VectorXd& values) const
264 {
265 // P type orbitals have three components and each component has a different
266 // independent MO weighting. Many things can be cached to save time though.
267 const vector<double>& gtoA = m_basis->gtoA();
268 const vector<double>& gtoCN = m_basis->gtoCN();
269 const vector<unsigned int>& gtoIndices = m_basis->gtoIndices();
270
271 unsigned int baseIndex = m_basis->moIndices()[moIndex];
272 Vector3 components(Vector3::Zero());
273
274 // Now iterate through the GTOs and sum their contributions
275 unsigned int cIndex = m_basis->cIndices()[moIndex];
276 double tmpGTO = 0.0;
277 for (unsigned int i = gtoIndices[moIndex]; i < gtoIndices[moIndex + 1]; ++i) {
278 tmpGTO = exp(-gtoA[i] * dr2);
279 for (unsigned int j = 0; j < 3; ++j) {
280 components[j] += gtoCN[cIndex++] * tmpGTO;
281 }
282 }
283 for (unsigned int i = 0; i < 3; ++i)
284 values[baseIndex + i] = components[i] * delta[i];
285 }
286
287 inline void GaussianSetTools::pointD(unsigned int moIndex, const Vector3& delta,
288 double dr2, Eigen::VectorXd& values) const
289 {
290 // D type orbitals have six components and each component has a different
291 // independent MO weighting. Many things can be cached to save time though.
292 const vector<double>& gtoA = m_basis->gtoA();
293 const vector<double>& gtoCN = m_basis->gtoCN();
294 const vector<unsigned int>& gtoIndices = m_basis->gtoIndices();
295
296 unsigned int baseIndex = m_basis->moIndices()[moIndex];
297
298 double components[6] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
299
300 // Now iterate through the GTOs and sum their contributions
301 unsigned int cIndex = m_basis->cIndices()[moIndex];
302 double tmpGTO = 0.0;
303 for (unsigned int i = gtoIndices[moIndex]; i < gtoIndices[moIndex + 1]; ++i) {
304 // Calculate the common factor
305 tmpGTO = exp(-gtoA[i] * dr2);
306 for (double& component : components)
307 component += gtoCN[cIndex++] * tmpGTO;
308 }
309
310 double componentsD[6] = { delta.x() * delta.x(), // xx
311 delta.y() * delta.y(), // yy
312 delta.z() * delta.z(), // zz
313 delta.x() * delta.y(), // xy
314 delta.x() * delta.z(), // xz
315 delta.y() * delta.z() }; // yz
316
317 for (int i = 0; i < 6; ++i)
318 values[baseIndex + i] += components[i] * componentsD[i];
319 }
320
321 inline void GaussianSetTools::pointD5(unsigned int moIndex,
322 const Vector3& delta, double dr2,
323 Eigen::VectorXd& values) const
324 {
325 // D type orbitals have five components and each component has a different
326 // MO weighting. Many things can be cached to save time.
327 const vector<double>& gtoA = m_basis->gtoA();
328 const vector<double>& gtoCN = m_basis->gtoCN();
329 const vector<unsigned int>& gtoIndices = m_basis->gtoIndices();
330
331 unsigned int baseIndex = m_basis->moIndices()[moIndex];
332 double components[5] = { 0.0, 0.0, 0.0, 0.0, 0.0 };
333
334 // Now iterate through the D type GTOs and sum their contributions
335 unsigned int cIndex = m_basis->cIndices()[moIndex];
336 double tmpGTO = 0.0;
337 for (unsigned int i = gtoIndices[moIndex]; i < gtoIndices[moIndex + 1]; ++i) {
338 // Calculate the common factor
339 tmpGTO = exp(-gtoA[i] * dr2);
340 for (double& component : components)
341 component += gtoCN[cIndex++] * tmpGTO;
342 }
343
344 // Calculate the prefactors
345 double xx = delta.x() * delta.x();
346 double yy = delta.y() * delta.y();
347 double zz = delta.z() * delta.z();
348 double xy = delta.x() * delta.y();
349 double xz = delta.x() * delta.z();
350 double yz = delta.y() * delta.z();
351
352 double componentsD[5] = { zz - dr2, // 0
353 xz, // 1p
354 yz, // 1n
355 xx - yy, // 2p
356 xy }; // 2n
357
358 for (int i = 0; i < 5; ++i)
359 values[baseIndex + i] += componentsD[i] * components[i];
360 }
361 inline void GaussianSetTools::pointF(unsigned int moIndex, const Vector3& delta,
362 double dr2, Eigen::VectorXd& values) const
363 {
364 // F type orbitals have 10 components and each component has a different
365 // independent MO weighting. Many things can be cached to save time though.
366 const vector<double>& gtoA = m_basis->gtoA();
367 const vector<double>& gtoCN = m_basis->gtoCN();
368 const vector<unsigned int>& gtoIndices = m_basis->gtoIndices();
369
370 unsigned int baseIndex = m_basis->moIndices()[moIndex];
371
372 double components[10] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
373
374 // Now iterate through the GTOs and sum their contributions
375 unsigned int cIndex = m_basis->cIndices()[moIndex];
376 double tmpGTO = 0.0;
377 for (unsigned int i = gtoIndices[moIndex]; i < gtoIndices[moIndex + 1]; ++i) {
378 // Calculate the common factor
379 tmpGTO = exp(-gtoA[i] * dr2);
380 for (double& component : components)
381 component += gtoCN[cIndex++] * tmpGTO;
382 }
383
384 double xxx = delta.x() * delta.x() * delta.x(); // xxx
385 double xxy = delta.x() * delta.x() * delta.y(); // xxy
386 double xxz = delta.x() * delta.x() * delta.z(); // xxz
387 double xyy = delta.x() * delta.y() * delta.y(); // xyy
388 double xyz = delta.x() * delta.y() * delta.z(); // xyz
389 double xzz = delta.x() * delta.z() * delta.z(); // xzz
390 double yyy = delta.y() * delta.y() * delta.y(); // yyy
391 double yyz = delta.y() * delta.y() * delta.z(); // yyz
392 double yzz = delta.y() * delta.z() * delta.z(); // yzz
393 double zzz = delta.z() * delta.z() * delta.z(); // zzz
394
395 double componentsF[10] = {
396 // molden order
397 // e.g https://gau2grid.readthedocs.io/en/latest/order.html
398 xxx, yyy, zzz, xyy, xxy, xxz, xzz, yzz, yyz, xyz
399 };
400
401 for (int i = 0; i < 10; ++i)
402 values[baseIndex + i] += components[i] * componentsF[i];
403 }
404
405 inline void GaussianSetTools::pointF7(unsigned int moIndex,
406 const Vector3& delta, double dr2,
407 Eigen::VectorXd& values) const
408 {
409 // F type orbitals have 7 components and each component has a different
410 // independent MO weighting. Many things can be cached to save time though.
411 const vector<double>& gtoA = m_basis->gtoA();
412 const vector<double>& gtoCN = m_basis->gtoCN();
413 const vector<unsigned int>& gtoIndices = m_basis->gtoIndices();
414
415 unsigned int baseIndex = m_basis->moIndices()[moIndex];
416
417 double components[7] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
418
419 // Now iterate through the F type GTOs and sum their contributions
420 unsigned int cIndex = m_basis->cIndices()[moIndex];
421 double tmpGTO = 0.0;
422 for (unsigned int i = gtoIndices[moIndex]; i < gtoIndices[moIndex + 1]; ++i) {
423 // Calculate the common factor
424 tmpGTO = exp(-gtoA[i] * dr2);
425 for (double& component : components)
426 component += gtoCN[cIndex++] * tmpGTO;
427 }
428
429 double xxx = delta.x() * delta.x() * delta.x(); // xxx
430 double xxy = delta.x() * delta.x() * delta.y(); // xxy
431 double xxz = delta.x() * delta.x() * delta.z(); // xxz
432 double xyy = delta.x() * delta.y() * delta.y(); // xyy
433 double xyz = delta.x() * delta.y() * delta.z(); // xyz
434 double xzz = delta.x() * delta.z() * delta.z(); // xzz
435 double yyy = delta.y() * delta.y() * delta.y(); // yyy
436 double yyz = delta.y() * delta.y() * delta.z(); // yyz
437 double yzz = delta.y() * delta.z() * delta.z(); // yzz
438 double zzz = delta.z() * delta.z() * delta.z(); // zzz
439
440 /*
441 spherical combinations borrowed from CASINO/Crystal documentation
442
443 linear combination
444 3,0 z^3 - 3/2 * (x^2z + y^2z) 2z^3 - 3 * (x^2z + y^2z) * 2
445 3,1 6 * xz^2 - 3/2 * (x^3 + xy^2) 4xz^2 - x^3 - xy^2 * 2/3
446 3,-1 6 * yz^2 - 3/2 * (x^2y + y^3) 4yz^2 - x^2y - y^3 * 2/3
447 3,2 15 * (x^2z - y^2z) x^2z - y^2z * 1/15
448 3,-2 30 * xyz xyz * 1/30
449 3,3 15 * x^3 - 45 * xy^2 x^3 - 3xy^2 * 1/15
450 3,-3 45 * x^2y - 15 * y^3 3x^2y - y^3 * 1/15
451
452 final normalization
453 (2 - delta_m,0) * (l - |m|)!
454 * root ------------------------------ (m-dependent)
455 (l + m)!
456 */
457 double root6 = 2.449489742783178;
458 double root60 = 7.745966692414834;
459 double root360 = 18.973665961010276;
460 double componentsF[7] = { zzz - 3.0 / 2.0 * (xxz + yyz),
461 (6.0 * xzz - 3.0 / 2.0 * (xxx + xyy)) / root6,
462 (6.0 * yzz - 3.0 / 2.0 * (xxy + yyy)) / root6,
463 (15.0 * (xxz - yyz)) / root60,
464 (30.0 * xyz) / root60,
465 (15.0 * xxx - 45.0 * xyy) / root360,
466 (45.0 * xxy - 15.0 * yyy) / root360 };
467
468 for (int i = 0; i < 7; ++i)
469 values[baseIndex + i] += components[i] * componentsF[i];
470 }
471
472 inline void GaussianSetTools::pointG(unsigned int moIndex, const Vector3& delta,
473 double dr2, Eigen::VectorXd& values) const
474 {
475 // G type orbitals have 15 components and each component has a different
476 // independent MO weighting. Many things can be cached to save time though.
477 const vector<double>& gtoA = m_basis->gtoA();
478 const vector<double>& gtoCN = m_basis->gtoCN();
479 const vector<unsigned int>& gtoIndices = m_basis->gtoIndices();
480
481 unsigned int baseIndex = m_basis->moIndices()[moIndex];
482
483 double components[15] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
484 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
485
486 // Now iterate through the G type GTOs and sum their contributions
487 unsigned int cIndex = m_basis->cIndices()[moIndex];
488 double tmpGTO = 0.0;
489 for (unsigned int i = gtoIndices[moIndex]; i < gtoIndices[moIndex + 1]; ++i) {
490 // Calculate the common factor
491 tmpGTO = exp(-gtoA[i] * dr2);
492 for (double& component : components)
493 component += gtoCN[cIndex++] * tmpGTO;
494 }
495
496 // e.g. XXXX YYYY ZZZZ XXXY XXXZ XYYY YYYZ ZZZX ZZZY XXYY XXZZ YYZZ XXYZ XYYZ
497 // XYZZ
498 const double xxxx = delta.x() * delta.x() * delta.x() * delta.x();
499 const double yyyy = delta.y() * delta.y() * delta.y() * delta.y();
500 const double zzzz = delta.z() * delta.z() * delta.z() * delta.z();
501 const double xxxy = delta.x() * delta.x() * delta.x() * delta.y();
502 const double xxxz = delta.x() * delta.x() * delta.x() * delta.z();
503 const double yyyx = delta.y() * delta.y() * delta.y() * delta.x();
504 const double yyyz = delta.y() * delta.y() * delta.y() * delta.z();
505 const double zzzx = delta.z() * delta.z() * delta.z() * delta.x();
506 const double zzzy = delta.z() * delta.z() * delta.z() * delta.y();
507 const double xxyy = delta.x() * delta.x() * delta.y() * delta.y();
508 const double xxzz = delta.x() * delta.x() * delta.z() * delta.z();
509 const double yyzz = delta.y() * delta.y() * delta.z() * delta.z();
510 const double xxyz = delta.x() * delta.x() * delta.y() * delta.z();
511 const double yyxz = delta.y() * delta.y() * delta.x() * delta.z();
512 const double zzxy = delta.z() * delta.z() * delta.x() * delta.y();
513
514 // molden order
515 // https://www.theochem.ru.nl/molden/molden_format.html
516 // https://gau2grid.readthedocs.io/en/latest/order.html
517 // xxxx yyyy zzzz xxxy xxxz yyyx yyyz zzzx zzzy,
518 // xxyy xxzz yyzz xxyz yyxz zzxy
519 double componentsG[15] = { xxxx, yyyy, zzzz, xxxy, xxxz, yyyx, yyyz, zzzx,
520 zzzy, xxyy, xxzz, yyzz, xxyz, yyxz, zzxy };
521
522 for (int i = 0; i < 15; ++i)
523 values[baseIndex + i] += components[i] * componentsG[i];
524 }
525
526 inline void GaussianSetTools::pointG9(unsigned int moIndex,
527 const Vector3& delta, double dr2,
528 Eigen::VectorXd& values) const
529 {
530 // G type orbitals have 9 spherical components and each component
531 // has a different independent MO weighting.
532 // Many things can be cached to save time though.
533 const vector<double>& gtoA = m_basis->gtoA();
534 const vector<double>& gtoCN = m_basis->gtoCN();
535 const vector<unsigned int>& gtoIndices = m_basis->gtoIndices();
536
537 unsigned int baseIndex = m_basis->moIndices()[moIndex];
538
539 double components[9] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
540
541 // Now iterate through the GTOs and sum their contributions
542 unsigned int cIndex = m_basis->cIndices()[moIndex];
543 double tmpGTO = 0.0;
544 for (unsigned int i = gtoIndices[moIndex]; i < gtoIndices[moIndex + 1]; ++i) {
545 // Calculate the common factor
546 tmpGTO = exp(-gtoA[i] * dr2);
547 for (double& component : components)
548 component += gtoCN[cIndex++] * tmpGTO;
549 }
550
551 double x2(delta.x() * delta.x()), y2(delta.y() * delta.y()),
552 z2(delta.z() * delta.z());
553
554 double componentsG[9] = {
555 3.0 * dr2 * dr2 - 30.0 * dr2 * z2 + 35.0 * z2 * z2 * (1.0 / 8.0),
556 delta.x() * delta.z() * (7.0 * z2 - 3.0 * dr2) * (sqrt(5.0) / 8.0),
557 delta.y() * delta.z() * (7.0 * z2 - 3.0 * dr2) * (sqrt(5.0) / 8.0),
558 (x2 - y2) * (7.0 * z2 - dr2) * (sqrt(5.0) / 4.0),
559 delta.x() * delta.y() * (7.0 * z2 - dr2) * (sqrt(5.0) / 2.0),
560 delta.x() * delta.z() * (x2 - 3.0 * y2) * (sqrt(7.0) / 4.0),
561 delta.y() * delta.z() * (3.0 * x2 - y2) * (sqrt(7.0) / 4.0),
562 (x2 * x2 - 6.0 * x2 * y2 + y2 * y2) * (sqrt(35.0) / 8.0),
563 delta.x() * delta.y() * (x2 - y2) * (sqrt(35.0) / 2.0)
564 };
565
566 for (int i = 0; i < 9; ++i)
567 values[baseIndex + i] += components[i] * componentsG[i];
568 }
569
570 } // namespace Avogadro::Core