votca 2026-dev
Loading...
Searching...
No Matches
vxc_potential.h
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 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#pragma once
21#ifndef VOTCA_XTP_VXC_POTENTIAL_H
22#define VOTCA_XTP_VXC_POTENTIAL_H
23
24#include <xc.h>
25
26#include <array>
27#include <vector>
28
29#include "grid_containers.h"
30#include "gridbox.h"
31
32#undef LOG
33
34namespace votca {
35namespace xtp {
36
37class AOBasis;
38class QMMolecule;
39
40template <class Grid>
42 public:
43 struct SpinResult {
44 double energy = 0.0;
45 Eigen::MatrixXd vxc_alpha;
46 Eigen::MatrixXd vxc_beta;
47 };
48
49 explicit Vxc_Potential(const Grid& grid) : grid_(grid) {};
51
52 static double getExactExchange(const std::string& functional);
53 void setXCfunctional(const std::string& functional);
54
55 Mat_p_Energy IntegrateVXC(const Eigen::MatrixXd& density_matrix) const;
56
62 std::vector<Eigen::MatrixXd> IntegrateFXC(
63 const Eigen::MatrixXd& density_matrix,
64 const std::vector<Eigen::MatrixXd>& response_matrices) const;
72 struct FXCKernel {
73 struct Box {
74 std::vector<Index> points; // indices in the box of the kept points
75 Eigen::VectorXd w, vsigma, v2rho2, v2rhosigma, v2sigma2;
76 Eigen::Matrix3Xd grad; // density gradient (GGA)
77 };
78 std::vector<Box> boxes;
80 };
81 FXCKernel PrepareFXC(const Eigen::MatrixXd& density_matrix) const;
83 std::vector<Eigen::MatrixXd> ApplyFXC(
84 const FXCKernel& kernel,
85 const std::vector<Eigen::MatrixXd>& response_matrices) const;
86
87 SpinResult IntegrateVXCSpin(const Eigen::MatrixXd& dmat_alpha,
88 const Eigen::MatrixXd& dmat_beta) const;
89
96 std::vector<std::array<Eigen::MatrixXd, 2>> IntegrateFXCSpin(
97 const Eigen::MatrixXd& dmat_alpha, const Eigen::MatrixXd& dmat_beta,
98 const std::vector<std::array<Eigen::MatrixXd, 2>>& response) const;
99
100 // ===========================================================================
101 // XC nuclear gradient at fixed density matrix, part 1: basis-function
102 // (Pulay) and grid-point translation terms, for LDA and GGA (the GGA sigma
103 // terms use AOShell::EvalAOspaceHessian and vanish for LDA, where
104 // df_dsigma stays 0). Despite the name it computes both terms, which
105 // share one AO evaluation per grid point. Derivation in vxc_potential.cc.
106 // ===========================================================================
107 Eigen::MatrixXd PulayGradient(const Eigen::MatrixXd& density_matrix,
108 const AOBasis& dftbasis) const;
109
110 // ===========================================================================
111 // XC nuclear gradient, part 2: derivative of the SSW grid weights. Only the
112 // sum PulayGradient + GridWeightGradient equals the derivative of the XC
113 // energy of IntegrateVXC (checked by finite differences in
114 // test_xcgradient.cc). Cost is O(Natoms^3) per grid point, one power of
115 // Natoms more than the SSW partition itself.
116 // ===========================================================================
117 Eigen::MatrixXd GridWeightGradient(const Eigen::MatrixXd& density_matrix,
118 const QMMolecule& atoms) const;
119
120 // ===========================================================================
121 // First-order change of PulayGradient + GridWeightGradient along a
122 // symmetric density direction P: d/dt [PulayGradient + GridWeightGradient]
123 // (D + t P) at t = 0, analytically in one grid pass (the XC part of
124 // relaxed excited-state gradients). Needs second functional derivatives
125 // from libxc. Checked against finite differences in test_xcgradient.cc.
126 // ===========================================================================
127 Eigen::MatrixXd GradientResponse(const Eigen::MatrixXd& density_matrix,
128 const Eigen::MatrixXd& direction,
129 const QMMolecule& atoms) const;
130
131 // ===========================================================================
132 // UKS (spin-polarized) analogs of PulayGradient/GridWeightGradient
133 // above, for LDA and GGA.
134 //
135 // Basis-type and translation-type rho terms, for each spin channel
136 // s in {a,b}:
137 // d(rho_s,p)/dR_A|_basis = -2 * sum_{mu in A} temp_s,mu * grad(chi_mu)
138 // d(rho_s,p)/dR_A|_translation = grad_s,p if A == owner(p), else 0
139 // with temp_s = dmat_s * ao.values and grad_s = 2*(ao.derivatives^T *
140 // temp_s), as in IntegrateVXCSpin (UKS spin density matrices are not
141 // doubled, unlike DMAT_here = 2*P for RKS), contracted with vrho_a/vrho_b
142 // and summed over both spins.
143 //
144 // GGA: the three sigma_aa/sigma_ab/sigma_bb terms reduce to the per-spin
145 // machinery of the restricted GGA case (Gmat_s, Hessian_rho_s) applied
146 // once per spin, with the effective gradient vectors
147 // V_a = 2*vsigma_aa*rho_a_grad + vsigma_ab*rho_b_grad
148 // V_b = 2*vsigma_bb*rho_b_grad + vsigma_ab*rho_a_grad
149 // Basis-type sigma contribution per mu in A:
150 // -[grad(chi_mu)*(Gmat_a.V_a)_mu + temp_a,mu*(Hessian_mu.V_a)
151 // + grad(chi_mu)*(Gmat_b.V_b)_mu + temp_b,mu*(Hessian_mu.V_b)]
152 // Translation-type sigma contribution (A==owner(p) only):
153 // Hessian_rho_a . V_a + Hessian_rho_b . V_b
154 // where Gmat_s = 2*dmat_s*ao.derivatives and Hessian_rho_s is the
155 // restricted Hessian_rho per spin channel.
156 //
157 // The weight-derivative term uses the same dw/dR as GridWeightGradient;
158 // only the energy density changes to (rho_a+rho_b)*f_xc, evaluated with
159 // sigma_aa/sigma_ab/sigma_bb from the density gradients, as in the energy
160 // of IntegrateVXCSpin.
161 Eigen::MatrixXd PulayGradientUKS(const Eigen::MatrixXd& dmat_alpha,
162 const Eigen::MatrixXd& dmat_beta,
163 const AOBasis& dftbasis) const;
164 Eigen::MatrixXd GridWeightGradientUKS(const Eigen::MatrixXd& dmat_alpha,
165 const Eigen::MatrixXd& dmat_beta,
166 const QMMolecule& atoms) const;
167 // ===========================================================================
168
169 private:
170 struct XC_entry {
171 double f_xc = 0;
172 double df_drho = 0;
173 double df_dsigma = 0;
174 };
175
177 double f_xc = 0;
178 double vrho_a = 0;
179 double vrho_b = 0;
180 double vsigma_aa = 0;
181 double vsigma_ab = 0;
182 double vsigma_bb = 0;
183 };
184
185 XC_entry EvaluateXC(double rho, double sigma) const;
186 XC_entry_spin EvaluateXCSpin(double rho_a, double rho_b, double sigma_aa,
187 double sigma_ab, double sigma_bb) const;
188
189 // libxc for n points at once, exchange and correlation summed. Unpolarized:
190 // rho, sigma, exc, vrho, vsigma have n entries. Polarized (libxc layout):
191 // rho and vrho 2n, sigma and vsigma 3n, exc n. vsigma is zero for LDA.
192 void EvaluateXCBatch(Index n, const double* rho, const double* sigma,
193 double* exc, double* vrho, double* vsigma) const;
194 void EvaluateXCSpinBatch(Index n, const double* rho, const double* sigma,
195 double* exc, double* vrho, double* vsigma) const;
196 // first and second derivatives for n points, unpolarized, exchange and
197 // correlation summed; vsigma, v2rhosigma, v2sigma2 are zero for LDA
198 void EvaluateFXCBatch(Index n, const double* rho, const double* sigma,
199 double* vsigma, double* v2rho2, double* v2rhosigma,
200 double* v2sigma2) const;
201 // the same with vrho as well
202 void EvaluateVXCFXCBatch(Index n, const double* rho, const double* sigma,
203 double* vrho, double* vsigma, double* v2rho2,
204 double* v2rhosigma, double* v2sigma2) const;
205 // the same, polarized (libxc layout): rho 2n, sigma and vsigma 3n,
206 // v2rho2 3n (aa, ab, bb), v2rhosigma 6n (a_aa, a_ab, a_bb, b_aa, b_ab,
207 // b_bb), v2sigma2 6n (aa_aa, aa_ab, aa_bb, ab_ab, ab_bb, bb_bb)
208 void EvaluateFXCSpinBatch(Index n, const double* rho, const double* sigma,
209 double* vsigma, double* v2rho2, double* v2rhosigma,
210 double* v2sigma2) const;
211
212 // Grid points processed together inside a box.
213 static constexpr Index kPointBlock = 128;
214
215 const Grid grid_;
217 bool setXC_ = false;
220 xc_func_type xfunc;
221 xc_func_type cfunc;
222 // Polarized handles for the UKS path, set up once with the unpolarized
223 // ones instead of per grid point.
224 xc_func_type xfunc_pol_;
225 xc_func_type cfunc_pol_;
226 bool is_gga_ = false;
227};
228
229} // namespace xtp
230} // namespace votca
231#endif // VOTCA_XTP_VXC_POTENTIAL_H
Container to hold Basisfunctions for all atoms.
Definition aobasis.h:42
static constexpr Index kPointBlock
void EvaluateXCSpinBatch(Index n, const double *rho, const double *sigma, double *exc, double *vrho, double *vsigma) const
Eigen::MatrixXd GridWeightGradient(const Eigen::MatrixXd &density_matrix, const QMMolecule &atoms) const
void EvaluateVXCFXCBatch(Index n, const double *rho, const double *sigma, double *vrho, double *vsigma, double *v2rho2, double *v2rhosigma, double *v2sigma2) const
Eigen::MatrixXd PulayGradient(const Eigen::MatrixXd &density_matrix, const AOBasis &dftbasis) const
void EvaluateFXCSpinBatch(Index n, const double *rho, const double *sigma, double *vsigma, double *v2rho2, double *v2rhosigma, double *v2sigma2) const
XC_entry_spin EvaluateXCSpin(double rho_a, double rho_b, double sigma_aa, double sigma_ab, double sigma_bb) const
void EvaluateXCBatch(Index n, const double *rho, const double *sigma, double *exc, double *vrho, double *vsigma) const
FXCKernel PrepareFXC(const Eigen::MatrixXd &density_matrix) const
std::vector< Eigen::MatrixXd > ApplyFXC(const FXCKernel &kernel, const std::vector< Eigen::MatrixXd > &response_matrices) const
IntegrateFXC with a prepared kernel (the same results).
Eigen::MatrixXd PulayGradientUKS(const Eigen::MatrixXd &dmat_alpha, const Eigen::MatrixXd &dmat_beta, const AOBasis &dftbasis) const
XC_entry EvaluateXC(double rho, double sigma) const
Eigen::MatrixXd GradientResponse(const Eigen::MatrixXd &density_matrix, const Eigen::MatrixXd &direction, const QMMolecule &atoms) const
void EvaluateFXCBatch(Index n, const double *rho, const double *sigma, double *vsigma, double *v2rho2, double *v2rhosigma, double *v2sigma2) const
Mat_p_Energy IntegrateVXC(const Eigen::MatrixXd &density_matrix) const
void setXCfunctional(const std::string &functional)
std::vector< std::array< Eigen::MatrixXd, 2 > > IntegrateFXCSpin(const Eigen::MatrixXd &dmat_alpha, const Eigen::MatrixXd &dmat_beta, const std::vector< std::array< Eigen::MatrixXd, 2 > > &response) const
Vxc_Potential(const Grid &grid)
static double getExactExchange(const std::string &functional)
std::vector< Eigen::MatrixXd > IntegrateFXC(const Eigen::MatrixXd &density_matrix, const std::vector< Eigen::MatrixXd > &response_matrices) const
Eigen::MatrixXd GridWeightGradientUKS(const Eigen::MatrixXd &dmat_alpha, const Eigen::MatrixXd &dmat_beta, const QMMolecule &atoms) const
SpinResult IntegrateVXCSpin(const Eigen::MatrixXd &dmat_alpha, const Eigen::MatrixXd &dmat_beta) const
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26
double vsigma_ab
double vrho_b
double vrho_a
double vsigma_aa
double vsigma_bb
double f_xc
double f_xc
double df_dsigma
double df_drho