votca 2026-dev
Loading...
Searching...
No Matches
dftgradient.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2024 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20// Local VOTCA includes
22#include "votca/xtp/aomatrix.h"
25
26namespace votca {
27namespace xtp {
28
30 Index natoms = mol.size();
31 Eigen::MatrixXd deriv = Eigen::MatrixXd::Zero(natoms, 3);
32
33 // dE_nn/dR_A = sum_{B != A} Z_A Z_B * d(1/R_AB)/dR_A
34 // = -sum_{B != A} Z_A Z_B (R_A - R_B) / |R_A - R_B|^3
35 //
36 // Returns the gradient dE/dR, not the force.
37 for (Index a = 0; a < natoms; ++a) {
38 double Za = static_cast<double>(mol[a].getNuccharge());
39 const Eigen::Vector3d& Ra = mol[a].getPos();
40 Eigen::Vector3d sum = Eigen::Vector3d::Zero();
41 for (Index b = 0; b < natoms; ++b) {
42 if (b == a) {
43 continue;
44 }
45 double Zb = static_cast<double>(mol[b].getNuccharge());
46 const Eigen::Vector3d& Rb = mol[b].getPos();
47 Eigen::Vector3d Rab_vec = Ra - Rb;
48 double Rab = Rab_vec.norm();
49 sum += Za * Zb * Rab_vec / (Rab * Rab * Rab);
50 }
51 deriv.row(a) = -sum.transpose();
52 }
53 return deriv;
54}
55
56namespace {
57// V^-1 B for the Coulomb metric V = (P|Q), Cholesky with LDLT as fallback
58// (plain solve, without the eigenvalue truncation of Pseudo_InvSqrt used in
59// the SCF; may need revisiting for nearly linearly dependent aux bases).
60Eigen::MatrixXd SolveMetric(const AOBasis& auxbasis, const Eigen::MatrixXd& B) {
61 AOCoulomb aocoulomb;
62 aocoulomb.Fill(auxbasis);
63 const Eigen::MatrixXd& V = aocoulomb.Matrix();
64 const CholeskyFactor V_chol(V);
65 if (V_chol.ok()) {
66 return V_chol.Solve(B);
67 }
68 return V.ldlt().solve(B);
69}
70} // namespace
71
72Eigen::MatrixXd DFTGradient::RIJGradient(const Eigen::MatrixXd& density,
73 const AOBasis& auxbasis,
74 const AOBasis& dftbasis) {
75 Index n_aux_bf = auxbasis.AOBasisSize();
76
77 // d_P = sum_{mu,nu} D_{mu,nu} (mu,nu|P), c = V^-1 d
78 Eigen::VectorXd d(n_aux_bf);
79 ForEachThreeCenterIntegral(auxbasis, dftbasis,
80 [&density, &d](Index P, const Eigen::MatrixXd& I) {
81 d(P) = (density.array() * I.array()).sum();
82 });
83 const Eigen::VectorXd c = SolveMetric(auxbasis, d);
84
85 // dE_J/dR = sum_P c_P d(d_P)/dR - 1/2 sum_PQ c_P c_Q d(V_PQ)/dR with
86 // d(d_P)/dR = sum_{mu,nu} D_{mu,nu} d(mu,nu|P)/dR at fixed density (see
87 // dftgradient.h), i.e. Gamma^P = c_P D and G = c c^T.
88 Eigen::MatrixXd grad = ContractThreeCenterDerivatives(
89 auxbasis, dftbasis, [&density, &c](Index P, Eigen::MatrixXd& Gamma) {
90 Gamma = c(P) * density;
91 });
92 grad -= 0.5 * ContractCoulombMetricDerivatives(auxbasis, c * c.transpose());
93 return grad;
94}
95
96Eigen::MatrixXd DFTGradient::RIKGradient(const Eigen::MatrixXd& occ_mo_coeffs,
97 const AOBasis& auxbasis,
98 const AOBasis& dftbasis) {
99 // E_K = -sum_{i,j} c_ij . d_ij with d_ij(P) = C_i^T (mu nu|P) C_j,
100 // c_ij = V^-1 d_ij over all ordered pairs of occupied MOs, equal to the
101 // exchange energy of ERIs::CalculateEXX_mos (symmetric V^-1/2 fit; the
102 // V^-1 fit used here gives the same energy, |V^-1/2 x|^2 = x^T V^-1 x, so
103 // no derivative of V^-1/2 is needed).
104 const Eigen::VectorXd ones = Eigen::VectorXd::Ones(occ_mo_coeffs.cols());
105 return -RIPairGradient(occ_mo_coeffs, ones, occ_mo_coeffs, auxbasis,
106 dftbasis);
107}
108
109Eigen::MatrixXd DFTGradient::RIPairGradient(const Eigen::MatrixXd& U,
110 const Eigen::VectorXd& w,
111 const Eigen::MatrixXd& C,
112 const AOBasis& auxbasis,
113 const AOBasis& dftbasis) {
114 Index n_aux_bf = auxbasis.AOBasisSize();
115 Index nk = U.cols();
116 Index ni = C.cols();
117
118 // d_ki(P) as the rows of dT (Naux x nk*ni, column k + nk*i), c_ki =
119 // V^-1 d_ki for all pairs in one multi-RHS solve. Memory O(Naux nk ni).
120 Eigen::MatrixXd dT(n_aux_bf, nk * ni);
122 auxbasis, dftbasis,
123 [&U, &C, &dT, nk, ni](Index P, const Eigen::MatrixXd& I) {
124 Eigen::MatrixXd dki = U.transpose() * (I * C);
125 dT.row(P) = Eigen::Map<const Eigen::RowVectorXd>(dki.data(), nk * ni);
126 });
127 const Eigen::MatrixXd cT = SolveMetric(auxbasis, dT); // Naux x nk*ni
128 dT.resize(0, 0);
129
130 // dE/dR = sum_ki w_k [2 c_ki . d(d_ki)/dR - c_ki . dV/dR c_ki]
131 // with Gamma^P = U diag(w) c^P C^T (c^P the nk x ni matrix of c_ki(P))
132 // and G = sum_ki w_k c_ki c_ki^T.
133 Eigen::MatrixXd grad = ContractThreeCenterDerivatives(
134 auxbasis, dftbasis,
135 [&U, &w, &C, &cT, nk, ni](Index P, Eigen::MatrixXd& Gamma) {
136 Eigen::Map<const Eigen::MatrixXd, 0, Eigen::InnerStride<>> cP(
137 cT.data() + P, nk, ni, Eigen::InnerStride<>(cT.rows()));
138 Gamma = U * (w.asDiagonal() * Eigen::MatrixXd(cP) * C.transpose());
139 });
140 Eigen::MatrixXd cw = cT;
141 for (Index i = 0; i < ni; ++i) {
142 cw.middleCols(i * nk, nk) *= w.asDiagonal();
143 }
144 const Eigen::MatrixXd G = cw * cT.transpose();
145 return 2.0 * grad - ContractCoulombMetricDerivatives(auxbasis, G);
146}
147
148} // namespace xtp
149} // namespace votca
Container to hold Basisfunctions for all atoms.
Definition aobasis.h:42
Index AOBasisSize() const
Definition aobasis.h:46
void Fill(const AOBasis &aobasis) final
const Eigen::MatrixXd & Matrix() const
Definition aomatrix.h:76
const Eigen::Vector3d & getPos() const
static Eigen::MatrixXd RIKGradient(const Eigen::MatrixXd &occ_mo_coeffs, const AOBasis &auxbasis, const AOBasis &dftbasis)
static Eigen::MatrixXd NuclearRepulsionDerivative(const QMMolecule &mol)
static Eigen::MatrixXd RIJGradient(const Eigen::MatrixXd &density, const AOBasis &auxbasis, const AOBasis &dftbasis)
static Eigen::MatrixXd RIPairGradient(const Eigen::MatrixXd &U, const Eigen::VectorXd &w, const Eigen::MatrixXd &C, const AOBasis &auxbasis, const AOBasis &dftbasis)
Charge transport classes.
Definition ERIs.h:28
void ForEachThreeCenterIntegral(const AOBasis &auxbasis, const AOBasis &dftbasis, const ThreeCenterVisitor &use)
Eigen::MatrixXd ContractCoulombMetricDerivatives(const AOBasis &auxbasis, const Eigen::MatrixXd &G)
Eigen::MatrixXd ContractThreeCenterDerivatives(const AOBasis &auxbasis, const AOBasis &dftbasis, const AuxDensityFn &gamma)
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26