MADNESS 0.10.1
xcfunctional.h
Go to the documentation of this file.
1#ifndef MADNESS_CHEM_XCFUNCTIONAL_H__INCLUDED
2#define MADNESS_CHEM_XCFUNCTIONAL_H__INCLUDED
3
5
6MADNESS_PRAGMA_GCC(diagnostic push)
7MADNESS_PRAGMA_GCC(diagnostic ignored "-Wcomment")
8MADNESS_PRAGMA_CLANG(diagnostic push)
9MADNESS_PRAGMA_CLANG(diagnostic ignored "-Wcomment")
10
11/// \file moldft/xcfunctional.h
12/// \brief Defines interface for DFT XC functionals
13/// \ingroup chemistry
14
15#include <madness/tensor/tensor.h>
16#include <vector>
17#include <algorithm>
18#include <utility>
19#include <madness/mra/key.h>
23
24#ifdef MADNESS_HAS_LIBXC
25#include <xc.h>
26#endif
27
28namespace madness {
29/// Compute the spin-restricted LDA potential using unaryop (only for the initial guess)
32
33 void operator()(const Key<3> & key, Tensor<double>& t) const
34 {
35 int x_rks_s__(const double *r__, double *f, double * dfdra);
36 int c_rks_vwn5__(const double *r__, double *f, double * dfdra);
37 double* rho = t.ptr();
38 for (int i=0; i<t.size(); i++) {
39 double r = std::max(rho[i],1e-12);
40 double q, dq1, dq2;
41 x_rks_s__(&r, &q, &dq1);
42 c_rks_vwn5__(&r, &q, &dq2);
43 rho[i] = dq1 + dq2;
44 }
45 }
46};
47
48/// Simplified interface to XC functionals
50public:
51
52 /// The ordering of the intermediates is fixed, but the code can handle
53 /// non-initialized functions, so if e.g. no GGA is requested, all the
54 /// corresponding vector components may be left empty.
55 ///
56 /// Note the additional quantities \f$ \zeta \f$ and \f$ \chi \f$, which are defined as
57 /// \f[
58 /// \rho = \exp(\zeta)
59 /// \f]
60 /// and thus the derivative of rho is given by
61 /// \f[
62 /// \nabla_x\rho = \exp(\zeta)\nabla_x\zeta = \rho \nabla_x\zeta
63 /// \f]
64 /// The reduced gradients \sigma may then be expressed as
65 /// \f[
66 /// \sigma = |\nabla\rho|^2 = |\rho|^2 |\nabla\zeta|^2 = |\rho|^2 \chi
67 /// \f]
68 enum xc_arg {
69 enum_rhoa=0, ///< alpha density \f$ \rho_\alpha \f$
70 enum_rhob=1, ///< beta density \f$ \rho_\beta \f$
71 enum_rho_pt=2, ///< perturbed density (CPHF, TDKS) \f$ \rho_{pt} \f$
72 enum_taua=3, ///< alpha kinetic energy density \f$ \tau_\alpha = \frac{1}{2}\sum_i|\nabla\psi_{i\alpha}|^2 \f$
73 enum_taub=4, ///< beta kinetic energy density \f$ \tau_\beta \f$
74 enum_saa=10, ///< \f$ \sigma_{aa} = \nabla \rho_{\alpha}.\nabla \rho_{\alpha} \f$
75 enum_sab=11, ///< \f$ \sigma_{ab} = \nabla \rho_{\alpha}.\nabla \rho_{\beta} \f$
76 enum_sbb=12, ///< \f$ \sigma_{bb} = \nabla \rho_{\beta}.\nabla \rho_{\beta} \f$
77 enum_sigtot=13, ///< \f$ \sigma = \nabla \rho.\nabla \rho \f$
78 enum_sigma_pta_div_rho=14, ///< \f$ \zeta_{\alpha}.\nabla\rho_{pt} \f$
79 enum_sigma_ptb_div_rho=15, ///< \f$ \zeta_{\beta}.\nabla\rho_{pt} \f$
80 enum_zetaa_x=16, ///< \f$ \zeta_{a,x}=\partial/{\partial x} \ln(\rho_a) \f$
81 enum_zetaa_y=17, ///< \f$ \zeta_{a,y}=\partial/{\partial y} \ln(\rho_a) \f$
82 enum_zetaa_z=18, ///< \f$ \zeta_{a,z}=\partial/{\partial z} \ln(\rho_a) \f$
83 enum_zetab_x=19, ///< \f$ \zeta_{b,x} = \partial/{\partial x} \ln(\rho_b) \f$
84 enum_zetab_y=20, ///< \f$ \zeta_{b,y} = \partial/{\partial y} \ln(\rho_b) \f$
85 enum_zetab_z=21, ///< \f$ \zeta_{b,z} = \partial/{\partial z} \ln(\rho_b) \f$
86 // Slots 22-24 held chi_st = zeta_s.zeta_t as three separately represented
87 // functions. They are gone: a projected product is not pointwise consistent
88 // with the zeta components it is built from, so the sigma matrix handed to
89 // libxc was not the Gram matrix of the density gradients -- chi_aa, a sum of
90 // squares, could come out negative near the nuclear cusp, and the total sigma
91 // followed it. make_libxc_args contracts zeta pointwise instead. Left as a
92 // hole rather than reused, so the surviving indices keep their meaning.
93 enum_ddens_ptx=25, ///< \f$ \nabla\rho_{pt}\f$
94 enum_ddens_pty=26, ///< \f$ \nabla\rho_{pt}\f$
95 enum_ddens_ptz=27, ///< \f$ \nabla\rho_{pt}\f$
96
97 // ---- the nemo meta-gga decomposition, kept apart on purpose ----------
98 //
99 // With psi = R F the product rule gives
100 // |grad psi|^2 = R^2 ( |grad F|^2 - 2 F U1.grad F + |U1|^2 F^2 ).
101 // The first group below is everything in that expression which is SMOOTH:
102 // F is cusp-free by construction, so |grad F|^2, n and G are shallow (depth
103 // 8-9 on LiH) and belong in MRA. R^2 is smooth too.
104 //
105 // U1 = -grad(R)/R is not. U1_x ~ x/r is non-smooth at every nucleus
106 // componentwise, its direction smoothed only over eprec, and as a Function
107 // it costs depth ~20. Carrying it in MRA is bad twice over: the depth taxes
108 // every intermediate through refine_to_common_level, and any *product* with
109 // it has to be projected onto a fixed tree, which is where the oscillations
110 // come from. So it is never a Function. The second group is filled by the
111 // op from the analytic functor at the quadrature points of the box it is
112 // already working on -- exactly as make_libxc_args contracts zeta pointwise
113 // rather than carrying chi. See nemo_u1_functors.
114 enum_nemo_R2=28, ///< \f$ R^2 \f$, the ncf squared [MRA, smooth]
115 enum_gradfa=29, ///< \f$ \sum_i w_i|\nabla F_{i\alpha}|^2 \f$ [MRA, smooth]
116 enum_gradfb=30, ///< beta counterpart [MRA, smooth]
117 enum_na=31, ///< \f$ n_\alpha=\sum_i w_iF_{i\alpha}^2 \f$ [MRA, smooth]
118 enum_nb=32, ///< beta counterpart [MRA, smooth]
119 enum_Ga_x=33, ///< \f$ G_{\alpha,x}=\sum_i w_iF_i\partial_xF_i \f$ [MRA, smooth]
120 enum_Ga_y=34, ///< \f$ G_{\alpha,y} \f$ [MRA, smooth]
121 enum_Ga_z=35, ///< \f$ G_{\alpha,z} \f$ [MRA, smooth]
122 enum_Gb_x=36, ///< beta counterpart [MRA, smooth]
123 enum_Gb_y=37, ///< beta counterpart [MRA, smooth]
124 enum_Gb_z=38, ///< beta counterpart [MRA, smooth]
125
126 enum_u1_x=39, ///< \f$ U_{1,x} \f$ [functor, never MRA]
127 enum_u1_y=40, ///< \f$ U_{1,y} \f$ [functor, never MRA]
128 enum_u1_z=41, ///< \f$ U_{1,z} \f$ [functor, never MRA]
129 enum_u1sq=42 ///< \f$ |\mathbf U_1|^2 \f$ [functor, never MRA]
130 };
131 const static int number_xc_args=43; ///< max number of intermediates
132
133 /// return the munging threshold for the density
134 double get_rhotol() const {return rhotol;}
135
136 /// return the floor for the kinetic energy density
137
138 /// meta-gga functionals build the iso-orbital indicators alpha and z with tau
139 /// in the denominator, so tau needs a floor well above libxc's own 1e-20
140 /// default for a real-space code, where tau is a numerical derivative
141 double get_tautol() const {return tautol;}
142
143 /// return the margin by which the von Weizsaecker clamp overshoots
144 double get_tauwmargin() const {return tauwmargin;}
145
146 /// the von Weizsaecker lower bound on tau, as the clamp actually applies it
147
148 /// tau >= tau_W = |grad rho_s|^2/(8 rho_s) is exact for any wavefunction, and
149 /// meta-ggas need it: they are built on z = tau_W/tau, whose domain is [0,1],
150 /// and outside it the Fermi hole curvature turns negative and libxc's
151 /// correlation kernels return NaN.
152 ///
153 /// Build it from SIGMA, not from chi. libxc forms z from the sigma it is handed,
154 /// so the bound has to be built from that same sigma. The form rho*chi/8 equals
155 /// sigma/(8 rho) only when sigma is literally rho^2 chi, which is false: sigma
156 /// carries a positivity floor that chi does not.
157 ///
158 /// And overshoot it. Clamping to exactly tau_W puts z at 1 to within one ulp --
159 /// the endpoint of the domain, and the worst-conditioned point in it, where a
160 /// one-ulp change in tau moves de/dtau by a factor of 30. Landing strictly
161 /// inside costs nothing: the clamp only ever fires where tau was below a bound
162 /// it should have satisfied anyway. Same device as r2SCAN's eta*tau_W.
163 ///
164 /// Returns 0 where the density has been munged away, so the clamp is inert
165 /// there: sigma's floor divided by a vanishing rho would grow like 1/rho.
166 double tau_w_bound(const double sigma, const double rho) const {
167 if (rho <= 0.0) return 0.0;
168 return sigma/(8.0*rho*(1.0-tauwmargin));
169 }
170
171protected:
172
173 bool spin_polarized=false; ///< True if the functional is spin polarized
174 double hf_coeff=0.0; ///< Factor multiplying HF exchange (+1.0 gives HF)
175 int nderiv=0; ///< Jacob's ladder rung; 0: lda, 1: gga, 2: mgga
176
177#ifdef MADNESS_HAS_LIBXC
178 static constexpr double default_rhomin=0.0; ///< libxc can handle rho=0.0
179#else
180 static constexpr double default_rhomin=1.e-12; ///< our lda will divide by rho
181#endif
182 static constexpr double default_rhotol=1.e-7;
183 static constexpr double default_tautol=1.e-12;
184 static constexpr double default_tauwmargin=1.e-6;
185
186 double rhomin=default_rhomin; ///< what munge() puts in place of a density
187 double rhotol=default_rhotol; ///< See initialize and munge*
188 double tautol=default_tautol; ///< floor for the kinetic energy density, see initialize
189 double tauwmargin=default_tauwmargin; ///< von Weizsaecker clamp overshoot, see tau_w_bound
190
191 /// put the configurable screening thresholds back to their defaults
192
193 /// initialize() may be called more than once on the same object, and the xc
194 /// input line can override any of these (RHOMIN/RHOTOL/TAUTOL), so they have
195 /// to be restored before the next line is parsed -- otherwise one
196 /// functional's thresholds leak into the next one.
203
204#ifdef MADNESS_HAS_LIBXC
205 std::vector< std::pair<xc_func_type*,double> > funcs;
206#endif
207
208 /// convert the raw density (gradient) data to be used by the xc operators
209
210 /// Involves mainly munging of the densities and multiplying with 2
211 /// if the calculation is spin-restricted.
212 /// Response densities and density gradients are munged based on the
213 /// value of the ground state density, since they may become negative
214 /// and may also be much more diffuse.
215 /// dimensions of the output tensors are for spin-restricted and unrestricted
216 /// (with np the number of grid points in the box):
217 /// rho(np) or rho(2*np)
218 /// sigma(np) sigma(3*np)
219 /// rho_pt(np)
220 /// sigma_pt(2*np)
221 /// @param[in] t input density (gradients)
222 /// @param[out] rho ground state (spin) density, properly munged
223 /// @param[out] sigma ground state (spin) density gradients, properly munged
224 /// @param[out] tau ground state (spin) kinetic energy density, properly munged
225 /// @param[out] rho_pt response density, properly munged (no spin)
226 /// @param[out] sigma_pt response (spin) density gradients, properly munged
227 /// @param[out] drho density derivative, constructed from rho and zeta
228 /// @param[out] drho_pt response density derivative directly from xc_args
229 /// @param[in] need_response flag if rho_pt and sigma_pt need to be calculated
230 void make_libxc_args(const std::vector< madness::Tensor<double> >& t,
235 madness::Tensor<double>& sigma_pt,
236 std::vector<madness::Tensor<double> >& drho,
237 std::vector<madness::Tensor<double> >& drho_pt,
238 const bool need_response) const;
239
240
241
242 /// Smoothly switches between constant (x<xmin) and linear function (x>xmax)
243
244 /// \f[
245 /// f(x,x_{\mathrm{min}},x_{\mathrm{max}}) = \left\{
246 /// \begin{array}{ll}
247 /// x_{\mathrm{min}} & x < x_{\mathrm{min}} \\
248 /// p(x,x_{\mathrm{min}},x_{\mathrm{max}}) & x_{\mathrm{min}} \leq x_{\mathrm{max}} \\
249 /// x & x_{\mathrm{max}} < x
250 /// \end{array}
251 /// \right.
252 /// \f]
253 /// where \f$p(x)\f$ is the unique quintic polynomial that
254 /// satisfies \f$p(x_{min})=x_{min}\f$, \f$p(x_{max})=x_{max}\f$,
255 /// \f$dp(x_{max})/dx=1\f$, and
256 /// \f$dp(x_{min})/dx=d^2p(x_{min})/dx^2=d^2p(x_{max})/dx^2=0\f$.
257 static void polyn(const double x, double& p, double& dpdx) {
258 // All of the static const stuff is evaluated at compile time
259
260 static const double xmin = 1.e-6; // <<<< MINIMUM VALUE OF DENSITY
261 static const double xmax = 5.e-5; // <<<< DENSITY SMOOTHLY MODIFIED BELOW THIS VALUE
262
263 static const double xmax2 = xmax*xmax;
264 static const double xmax3 = xmax2*xmax;
265 static const double xmin2 = xmin*xmin;
266 static const double xmin3 = xmin2*xmin;
267 static const double r = 1.0/((xmax-xmin)*(-xmin3+(3.0*xmin2+(-3.0*xmin+xmax)*xmax)*xmax));
268 static const double a0 = xmax3*xmin*(xmax-4.0*xmin)*r;
269 static const double a = xmin2*(xmin2+(-4.0*xmin+18.0*xmax)*xmax)*r;
270 static const double b = -6.0*xmin*xmax*(3.0*xmax+2.0*xmin)*r;
271 static const double c = (4.0*xmin2+(20.0*xmin+6.0*xmax)*xmax)*r;
272 static const double d = -(8.0*xmax+7.0*xmin)*r;
273 static const double e = 3.0*r;
274
275 if (x > xmax) {
276 p = x;
277 dpdx = 1.0;
278 }
279 else if (x < xmin) {
280 p = xmin;
281 dpdx = 0.0;
282 }
283 else {
284 p = a0+(a+(b+(c+(d+e*x)*x)*x)*x)*x;
285 dpdx = a+(2.0*b+(3.0*c+(4.0*d+5.0*e*x)*x)*x)*x;
286 }
287 }
288public:
289 static double munge_old(double rho) {
290 double p, dpdx;
291 polyn(rho, p, dpdx);
292 return p;
293 }
294
295private:
296
297 /// simple munging for the density only (LDA)
298 double munge(double rho) const {
299 if (rho <= rhotol) rho=rhomin;
300 return rho;
301 }
302
303 /// zero a quantity where the reference density is small
304
305 /// Used for perturbed densities, which may be negative and much more diffuse
306 /// than the ground state, and for screening outputs. Only where the reference
307 /// density is large enough is DFT numerically well defined.
308 ///
309 /// Substitutes zero, not rhomin. The argument is not necessarily a density --
310 /// de/dtau and the semilocal response terms go through here too -- so a density
311 /// floor is the wrong thing to leave behind, and "screened" means "contributes
312 /// nothing". rhomin stays what munge() puts in place of a density.
313 /// @param[in] rho number to be munged
314 /// @param[in] refrho reference value for munging
315 /// @param[in] thresh threshold for munging
316 double binary_munge(double rho, double refrho, const double thresh) const {
317 if (refrho<thresh) rho=0.0;
318 return rho;
319 }
320
321public:
322 /// Default constructor is required
324
325 /// Initialize the object from the user input data
326
327 /// @param[in] input_line User input line (without beginning XC keyword)
328 /// @param[in] polarized Boolean flag indicating if the calculation is spin-polarized
329 void initialize(const std::string& input_line, bool polarized, World& world,
330 const bool verbose=false);
331
332 /// Destructor
334
335 /// Returns true if the potential is lda
336 bool is_lda() const;
337
338 /// Returns true if the potential is gga (needs first derivatives)
339 bool is_gga() const;
340
341 /// Returns true if the potential is meta gga (needs the kinetic energy density)
342 bool is_meta() const;
343
344 /// Returns true if the functional needs the reduced density gradients sigma
345
346 /// True for gga AND meta-gga -- a meta-gga needs the density gradients as
347 /// well as the kinetic energy density. Use this, not is_gga(), to decide
348 /// whether the gradient intermediates have to be computed.
349 bool needs_sigma() const;
350
351 /// Returns true if the functional needs the kinetic energy density tau
352 bool needs_tau() const;
353
354 /// Returns true if there is a DFT functional (false probably means Hatree-Fock exchange only)
355 bool is_dft() const;
356
357 /// Returns true when libxc is the active XC backend for this functional.
358 bool uses_libxc_backend() const {
359#ifdef MADNESS_HAS_LIBXC
360 return !funcs.empty();
361#else
362 return false;
363#endif
364 }
365
366 /// Returns true if the functional is spin_polarized
367 bool is_spin_polarized() const
368 {
369 return spin_polarized;
370 }
371
372 /// Returns true if the second derivative of the functional is available (not yet supported)
373 bool has_fxc() const;
374
375 /// Returns true if the third derivative of the functional is available (not yet supported)
376 bool has_kxc() const;
377
378 /// Returns the value of the hf exact exchange coefficient
380 {
381 return hf_coeff;
382 }
383
384 /// Computes the energy functional at given points
385
386 /// This uses the convention that the total energy is
387 /// \f$ E[\rho] = \int \epsilon[\rho(x)] dx\f$
388 /// Any HF exchange contribution must be separately computed. Items in the
389 /// vector argument \c t are interpreted similarly to the xc_arg enum.
390 /// @param[in] t The input densities and derivatives as required by the functional
391 /// @return The exchange-correlation energy functional
392 madness::Tensor<double> exc(const std::vector< madness::Tensor<double> >& t) const;
393
394 /// Computes components of the potential (derivative of the energy functional) at np points
395
396 /// Any HF exchange contribution must be separately computed. Items in the
397 /// vector argument \c t are interpreted similarly to the xc_arg enum.
398 ///
399 /// We define \f$ \sigma_{\mu \nu} = \nabla \rho_{\mu} . \nabla \rho_{\nu} \f$
400 /// with \f$ \mu, \nu = \alpha\f$ or \f$ \beta \f$.
401 ///
402 /// For unpolarized GGA, matrix elements of the potential are
403 /// \f[
404 /// < \phi | \hat V | \psi > = \int \left( \frac{\partial \epsilon}{\partial \rho} \phi \psi
405 /// + \left( 2 \frac{\partial \epsilon}{\partial \sigma} \right)
406 /// \nabla \rho \cdot \nabla \left( \phi \psi \right) \right) dx
407 /// \f]
408 ///
409 /// For polarized GGA, matrix elements of the potential are
410 /// \f[
411 /// < \phi_{\alpha} | \hat V | \psi_{\alpha} > = \int \left( \frac{\partial \epsilon}{\partial \rho_{\alpha}} \phi \psi
412 /// + \left( 2 \frac{\partial \epsilon}{\partial \sigma_{\alpha \alpha}} \nabla \rho_{\alpha}
413 /// + \frac{\partial \epsilon}{\partial \sigma_{\alpha \beta}} \nabla \rho_{\beta} \right) . \nabla \left( \phi \psi \right) \right) dx
414 /// \f]
415 ///
416 /// Integrating the above by parts and assuming free-space or periodic boundary conditions
417 /// we obtain that the local multiplicative form of the GGA potential is
418 /// \f[
419 /// V_{\alpha} = \frac{\partial \epsilon}{\partial \rho_{\alpha}}
420 /// - \left(\nabla . \left(2 \frac{\partial \epsilon}{\partial \sigma_{\alpha \alpha}} \nabla \rho_{\alpha}
421 /// + \frac{\partial \epsilon}{\partial \sigma_{\alpha \beta}} \nabla \rho_{\beta} \right) \right)
422 /// \f]
423 ///
424 /// Return the following quantities for RHF: (see Yanai2005, Eq. (12))
425 /// \f{eqnarray*}{
426 /// \mbox{result[0]} &:& \qquad \frac{\partial \epsilon}{\partial \rho} \\
427 /// \mbox{result[1-3]} &:& \qquad 2 \rho \frac{\partial \epsilon}{\partial \sigma} \nabla\rho
428 /// \f}
429 /// and for UHF same-spin and other-spin quantities
430 /// \f{eqnarray*}{
431 /// \mbox{result[0]} &:& \qquad \frac{\partial \epsilon}{\partial \rho_{\alpha}} \\
432 /// \mbox{result[1-3]} &:& \qquad \rho_\alpha \frac{\partial \epsilon}{\partial \sigma_{\alpha \alpha}} \nabla\rho_\alpha\\
433 /// \mbox{result[4-6]} &:& \qquad \rho_\alpha \frac{\partial \epsilon}{\partial \sigma_{\alpha \beta}} \nabla\rho_\beta
434 /// \f}
435 /// @param[in] t The input densities and derivatives as required by the functional
436 /// @param[in] ispin Specifies which component of the potential is to be computed as described above
437 /// @return the requested quantity, based on ispin (0: same spin, 1: other spin)
438 std::vector<madness::Tensor<double> > vxc(const std::vector< madness::Tensor<double> >& t,
439 const int ispin) const;
440
441
442 /// compute the second derivative of the XC energy wrt the density and apply
443
444 /// Return the following quantities (RHF only) (see Yanai2005, Eq. (13))
445 /// \f{eqnarray*}{
446 /// \mbox{result[0]} &:& \qquad \frac{\partial^2 \epsilon}{\partial \rho^2} \rho_\mathrm{pt}
447 /// + 2.0 * \frac{\partial^2 \epsilon}{\partial \rho\partial\sigma}\sigma_\mathrm{pt}\\
448 /// \mbox{result[1-3]} &:& \qquad 2.0 * \frac{\partial\epsilon}{\partial\sigma}\nabla\rho_\mathrm{pt}
449 /// + 2.0 * \frac{\partial^2\epsilon}{\partial\rho\partial\sigma} \rho_\mathrm{pt}\nabla\rho
450 /// + 4.0 * \frac{\partial^2\epsilon}{\partial^2\sigma} \sigma_\mathrm{pt}\nabla\rho
451 /// \f}
452 /// @param[in] t The input densities and derivatives as required by the functional,
453 /// as in the xc_arg enum
454 /// @param[in] ispin not referenced since only RHF is implemented, always 0
455 /// @return a vector of Functions containing the contributions to the kernel apply
456 std::vector<madness::Tensor<double> > fxc_apply(
457 const std::vector< madness::Tensor<double> >& t, const int ispin) const;
458
459
460 /// Crude function to plot the energy and potential functionals
461 void plot() const {
462 long npt = 1001;
463 double lo=1e-6, hi=1e+1, s=std::pow(hi/lo, 1.0/(npt-1));
464
466 for (int i=0; i<npt; i++) {
467 rho[i] = lo;
468 lo *= s;
469 }
470 std::vector< madness::Tensor<double> > t(13);
471 t[enum_rhoa]=(rho);
472 if (is_spin_polarized()) t[enum_rhob]=(rho);
473// if (is_gga()) t[enum_saa]=madness::Tensor<double>(npt); // sigma_aa=0
474 if (needs_sigma()) t[enum_saa]=0.5*rho; // sigma_aa=0
475 if (needs_tau()) t[enum_taua]=0.5*rho;
476 madness::Tensor<double> f = exc(t); //pending UGHHHHH
477 std::vector<madness::Tensor<double> > va = vxc(t,0);
478 for (long i=0; i<npt; i++) {
479 printf("%.3e %.3e %.3e\n", rho[i], f[i], va[0][i]);
480 }
481 }
482};
483
484/// the cuspy half of the nemo tau decomposition, supplied pointwise
485
486/// Holds the four analytic ncf quantities that must never become MRA functions --
487/// \f$ U_{1,x}, U_{1,y}, U_{1,z}, |\mathbf U_1|^2 \f$ -- and writes their values
488/// into the argument vector at the quadrature points of whatever box the caller is
489/// operating on. Empty unless a nuclear correlation factor is in play, in which case
490/// active() is true and the four enum_u1* slots are filled.
491///
492/// The point is that nothing is projected. A product of U1 with anything, formed as
493/// a Function, has to be represented on some tree; on a tree too coarse for U1's
494/// eprec-scale structure the polynomial fit rings across the whole box. Evaluating
495/// U1 here instead means its values go straight into the functional's pointwise
496/// arithmetic and only the *potential* is ever projected -- which the existing
497/// machinery already does, and already has to.
498///
499/// \f$ |\mathbf U_1|^2 \f$ comes from its own functor rather than from summing the
500/// squares of the three components: that functor treats its diagonal specially,
501/// because smoothed_unitvec has norm < 1 inside eprec while the exact diagonal is
502/// \f$ (S'/S)^2 \f$.
505
507 madness::FunctionDefaults<3>::get_k())) {}
508
509 /// @param[in] u1 x, y, z components of U1 followed by |U1|^2 -- four functors
510 explicit nemo_u1_functors(const std::vector<std::shared_ptr<functorT> >& u1)
511 : f(u1), cdata(madness::FunctionCommonData<double,3>::get(
512 madness::FunctionDefaults<3>::get_k())) {
513 MADNESS_CHECK_THROW(f.empty() or f.size()==4,
514 "nemo_u1_functors wants U1_{x,y,z} and |U1|^2, in that order");
515 }
516
517 bool active() const {return f.size()==4;}
518
519 /// write U1 and |U1|^2 at this box's quadrature points into t[enum_u1*]
520 void append(const madness::Key<3>& key,
521 std::vector<madness::Tensor<double> >& t) const {
522 if (not active()) return;
523 if (long(t.size()) < XCfunctional::number_xc_args)
525
527 const long npt = qx.dim(0);
528 // cdata was captured at construction from FunctionDefaults; if the functions
529 // actually carry a different k the quadrature points below are the wrong
530 // ones and every U1 value lands at the wrong place. Silent, and it would
531 // look like a physics error, so check rather than trust.
532 if (t[XCfunctional::enum_rhoa].size())
534 "nemo_u1_functors: quadrature order does not match "
535 "the xc arguments -- k changed after construction");
536 const double h = std::pow(0.5, double(key.level()));
539
540 const long dims[3] = {npt, npt, npt};
542 double* p[4];
543 for (int q = 0; q < 4; ++q) {
544 v[q] = madness::Tensor<double>(3L, dims);
545 p[q] = v[q].ptr();
546 }
547
548 // the same box-to-user-coordinate construction fcube() uses, written out so
549 // this header needs no mraimpl.h
550 long idx = 0;
552 for (long i = 0; i < npt; ++i) {
553 c[0] = cell(0,0) + h*cw[0]*(key.translation()[0] + qx(i));
554 for (long j = 0; j < npt; ++j) {
555 c[1] = cell(1,0) + h*cw[1]*(key.translation()[1] + qx(j));
556 for (long k = 0; k < npt; ++k, ++idx) {
557 c[2] = cell(2,0) + h*cw[2]*(key.translation()[2] + qx(k));
558 for (int q = 0; q < 4; ++q) p[q][idx] = (*f[q])(c);
559 }
560 }
561 }
566 }
567
568 std::vector<std::shared_ptr<functorT> > f;
570};
571
572/// Class to compute the energy functional
575 nemo_u1_functors u1; ///< empty without a nuclear correlation factor
576
579
581 const std::vector< madness::Tensor<double> >& t) const {
583 if (not u1.active()) return xc->exc(t);
584 std::vector<madness::Tensor<double> > tt(t); // Tensor copy is shallow
585 u1.append(key, tt);
586 return xc->exc(tt);
587 }
588};
589
590
591/// Class to compute terms of the potential
594 const int ispin;
595 nemo_u1_functors u1; ///< empty without a nuclear correlation factor
596
598 {}
600 : xc(&xc), ispin(ispin), u1(u1)
601 {}
602
603 std::size_t get_result_size() const {
604 // local terms, same spin
605 if (xc->is_lda()) return 1;
606 // local term + 3x semilocal terms (x,y,z), same spin
607 std::size_t result_size=4;
608 // 3x semilocal terms (x,y,z) for the opposite spin
609 if (xc->is_spin_polarized()) result_size+=3;
610 // de/dtau, same spin -- the non-multiplicative meta-gga term
611 if (xc->needs_tau()) result_size+=1;
612 return result_size;
613 }
614
615 std::vector<madness::Tensor<double> > operator()(const madness::Key<3> & key,
616 const std::vector< madness::Tensor<double> >& t) const {
618 if (not u1.active()) return xc->vxc(t, ispin);
619 // U1 is cuspy, so it arrives here as values rather than as a Function --
620 // see nemo_u1_functors. Nothing involving it is ever projected.
621 std::vector<madness::Tensor<double> > tt(t); // Tensor copy is shallow
622 u1.append(key, tt);
623 return xc->vxc(tt, ispin);
624 }
625};
626
627
628/// Class to compute terms of the kernel
631 const int ispin;
633
635 cdata(FunctionCommonData<double,3>::get(FunctionDefaults<3>::get_k())) {
636 MADNESS_ASSERT(ispin==0); // closed shell only!
637 }
638
639 std::size_t get_result_size() const {
640 // all spin-restricted
641 if (xc->is_gga()) return 4; // local terms, 3x semilocal terms (x,y,z)
642 return 1; // local terms only
643 }
644
645 std::vector<madness::Tensor<double> > operator()(const madness::Key<3> & key,
646 const std::vector< madness::Tensor<double> >& t) const {
648 std::vector<madness::Tensor<double> > r = xc->fxc_apply(t, ispin);
649 return r;
650 }
651};
652
653}
654
655MADNESS_PRAGMA_CLANG(diagnostic pop)
656MADNESS_PRAGMA_GCC(diagnostic pop)
657
658#endif
double q(double t)
Definition DKops.h:18
This header should include pretty much everything needed for the parallel runtime.
long dim(int i) const
Returns the size of dimension i.
Definition basetensor.h:147
long size() const
Returns the number of elements in the tensor.
Definition basetensor.h:138
FunctionCommonData holds all Function data common for given k.
Definition function_common_data.h:52
Tensor< double > quad_x
quadrature points
Definition function_common_data.h:99
FunctionDefaults holds default paramaters as static class members.
Definition funcdefaults.h:101
static const Tensor< double > & get_cell_width()
Returns the width of each user cell dimension.
Definition funcdefaults.h:390
static const Tensor< double > & get_cell()
Gets the user cell for the simulation.
Definition funcdefaults.h:352
Abstract base class interface required for functors used as input to Functions.
Definition function_interface.h:68
Key is the index for a node of the 2^NDIM-tree.
Definition key.h:70
Level level() const
Definition key.h:169
const Vector< Translation, NDIM > & translation() const
Definition key.h:174
A tensor is a multidimensional array.
Definition tensor.h:318
T * ptr()
Returns a pointer to the internal data.
Definition tensor.h:1841
A simple, fixed dimension vector.
Definition vector.h:64
A parallel world class.
Definition world.h:134
Simplified interface to XC functionals.
Definition xcfunctional.h:49
bool has_fxc() const
Returns true if the second derivative of the functional is available (not yet supported)
Definition xcfunctional_ldaonly.cc:76
bool is_dft() const
Returns true if there is a DFT functional (false probably means Hatree-Fock exchange only)
Definition xcfunctional_ldaonly.cc:72
double get_tautol() const
return the floor for the kinetic energy density
Definition xcfunctional.h:141
double rhomin
what munge() puts in place of a density
Definition xcfunctional.h:186
static constexpr double default_tauwmargin
Definition xcfunctional.h:184
std::vector< madness::Tensor< double > > vxc(const std::vector< madness::Tensor< double > > &t, const int ispin) const
Computes components of the potential (derivative of the energy functional) at np points.
Definition xcfunctional_ldaonly.cc:128
static constexpr double default_rhomin
our lda will divide by rho
Definition xcfunctional.h:180
bool is_lda() const
Returns true if the potential is lda.
Definition xcfunctional_ldaonly.cc:52
double tau_w_bound(const double sigma, const double rho) const
the von Weizsaecker lower bound on tau, as the clamp actually applies it
Definition xcfunctional.h:166
double get_rhotol() const
return the munging threshold for the density
Definition xcfunctional.h:134
static const int number_xc_args
max number of intermediates
Definition xcfunctional.h:131
void plot() const
Crude function to plot the energy and potential functionals.
Definition xcfunctional.h:461
madness::Tensor< double > exc(const std::vector< madness::Tensor< double > > &t) const
Computes the energy functional at given points.
Definition xcfunctional_ldaonly.cc:86
bool is_spin_polarized() const
Returns true if the functional is spin_polarized.
Definition xcfunctional.h:367
xc_arg
Definition xcfunctional.h:68
@ enum_Ga_y
[MRA, smooth]
Definition xcfunctional.h:120
@ enum_zetab_y
Definition xcfunctional.h:84
@ enum_zetab_z
Definition xcfunctional.h:85
@ enum_Ga_z
[MRA, smooth]
Definition xcfunctional.h:121
@ enum_Ga_x
[MRA, smooth]
Definition xcfunctional.h:119
@ enum_ddens_pty
Definition xcfunctional.h:94
@ enum_zetaa_y
Definition xcfunctional.h:81
@ enum_sigtot
Definition xcfunctional.h:77
@ enum_zetab_x
Definition xcfunctional.h:83
@ enum_u1_x
[functor, never MRA]
Definition xcfunctional.h:126
@ enum_taua
alpha kinetic energy density
Definition xcfunctional.h:72
@ enum_nb
beta counterpart [MRA, smooth]
Definition xcfunctional.h:118
@ enum_gradfb
beta counterpart [MRA, smooth]
Definition xcfunctional.h:116
@ enum_nemo_R2
, the ncf squared [MRA, smooth]
Definition xcfunctional.h:114
@ enum_taub
beta kinetic energy density
Definition xcfunctional.h:73
@ enum_rho_pt
perturbed density (CPHF, TDKS)
Definition xcfunctional.h:71
@ enum_Gb_z
beta counterpart [MRA, smooth]
Definition xcfunctional.h:124
@ enum_gradfa
[MRA, smooth]
Definition xcfunctional.h:115
@ enum_sbb
Definition xcfunctional.h:76
@ enum_saa
Definition xcfunctional.h:74
@ enum_u1sq
[functor, never MRA]
Definition xcfunctional.h:129
@ enum_rhob
beta density
Definition xcfunctional.h:70
@ enum_Gb_y
beta counterpart [MRA, smooth]
Definition xcfunctional.h:123
@ enum_u1_z
[functor, never MRA]
Definition xcfunctional.h:128
@ enum_Gb_x
beta counterpart [MRA, smooth]
Definition xcfunctional.h:122
@ enum_zetaa_x
Definition xcfunctional.h:80
@ enum_sab
Definition xcfunctional.h:75
@ enum_zetaa_z
Definition xcfunctional.h:82
@ enum_sigma_pta_div_rho
Definition xcfunctional.h:78
@ enum_na
[MRA, smooth]
Definition xcfunctional.h:117
@ enum_u1_y
[functor, never MRA]
Definition xcfunctional.h:127
@ enum_sigma_ptb_div_rho
Definition xcfunctional.h:79
@ enum_ddens_ptx
Definition xcfunctional.h:93
@ enum_rhoa
alpha density
Definition xcfunctional.h:69
@ enum_ddens_ptz
Definition xcfunctional.h:95
bool is_meta() const
Returns true if the potential is meta gga (needs the kinetic energy density)
Definition xcfunctional_ldaonly.cc:60
static double munge_old(double rho)
Definition xcfunctional.h:289
bool is_gga() const
Returns true if the potential is gga (needs first derivatives)
Definition xcfunctional_ldaonly.cc:56
std::vector< madness::Tensor< double > > fxc_apply(const std::vector< madness::Tensor< double > > &t, const int ispin) const
compute the second derivative of the XC energy wrt the density and apply
Definition xcfunctional_ldaonly.cc:176
double get_tauwmargin() const
return the margin by which the von Weizsaecker clamp overshoots
Definition xcfunctional.h:144
~XCfunctional()
Destructor.
Definition xcfunctional_ldaonly.cc:50
bool uses_libxc_backend() const
Returns true when libxc is the active XC backend for this functional.
Definition xcfunctional.h:358
static constexpr double default_tautol
Definition xcfunctional.h:183
double hf_exchange_coefficient() const
Returns the value of the hf exact exchange coefficient.
Definition xcfunctional.h:379
void make_libxc_args(const std::vector< madness::Tensor< double > > &t, madness::Tensor< double > &rho, madness::Tensor< double > &sigma, madness::Tensor< double > &tau, madness::Tensor< double > &rho_pt, madness::Tensor< double > &sigma_pt, std::vector< madness::Tensor< double > > &drho, std::vector< madness::Tensor< double > > &drho_pt, const bool need_response) const
convert the raw density (gradient) data to be used by the xc operators
Definition xcfunctional_ldaonly.cc:181
double hf_coeff
Factor multiplying HF exchange (+1.0 gives HF)
Definition xcfunctional.h:174
void reset_screening_defaults()
put the configurable screening thresholds back to their defaults
Definition xcfunctional.h:197
bool needs_sigma() const
Returns true if the functional needs the reduced density gradients sigma.
Definition xcfunctional_ldaonly.cc:64
bool needs_tau() const
Returns true if the functional needs the kinetic energy density tau.
Definition xcfunctional_ldaonly.cc:68
static void polyn(const double x, double &p, double &dpdx)
Smoothly switches between constant (x<xmin) and linear function (x>xmax)
Definition xcfunctional.h:257
bool has_kxc() const
Returns true if the third derivative of the functional is available (not yet supported)
Definition xcfunctional_ldaonly.cc:81
double tautol
floor for the kinetic energy density, see initialize
Definition xcfunctional.h:188
bool spin_polarized
True if the functional is spin polarized.
Definition xcfunctional.h:173
void initialize(const std::string &input_line, bool polarized, World &world, const bool verbose=false)
Initialize the object from the user input data.
Definition xcfunctional_ldaonly.cc:19
XCfunctional()
Default constructor is required.
Definition xcfunctional.h:323
double binary_munge(double rho, double refrho, const double thresh) const
zero a quantity where the reference density is small
Definition xcfunctional.h:316
int nderiv
Jacob's ladder rung; 0: lda, 1: gga, 2: mgga.
Definition xcfunctional.h:175
double munge(double rho) const
simple munging for the density only (LDA)
Definition xcfunctional.h:298
double rhotol
See initialize and munge*.
Definition xcfunctional.h:187
double tauwmargin
von Weizsaecker clamp overshoot, see tau_w_bound
Definition xcfunctional.h:189
static constexpr double default_rhotol
Definition xcfunctional.h:182
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition derivatives.cc:72
const double sigma
Definition dielectric.cc:185
static double lo
Definition dirac-hatom.cc:23
static const double v
Definition hatom_sf_dirac.cc:20
Multidimension Key for MRA tree and associated iterators.
Macros and tools pertaining to the configuration of MADNESS.
#define MADNESS_PRAGMA_CLANG(x)
Definition madness_config.h:200
#define MADNESS_PRAGMA_GCC(x)
Definition madness_config.h:205
#define MADNESS_ASSERT(condition)
Assert a condition that should be free of side-effects since in release builds this might be a no-op.
Definition madness_exception.h:134
#define MADNESS_CHECK_THROW(condition, msg)
Check a condition — even in a release build the condition is always evaluated so it can have side eff...
Definition madness_exception.h:207
Namespace for all elements and tools of MADNESS.
Definition DFConvergence.h:9
NDIM & f
Definition mra.h:2668
int x_rks_s__(const double *r__, double *f, double *dfdra)
Definition lda.cc:58
int c_rks_vwn5__(const double *r__, double *f, double *dfdra)
Definition lda.cc:116
static const double b
Definition nonlinschro.cc:119
static const double d
Definition nonlinschro.cc:121
static const double a
Definition nonlinschro.cc:118
static const double c
Definition relops.cc:10
static const double thresh
Definition rk.cc:45
static const long k
Definition rk.cc:44
the cuspy half of the nemo tau decomposition, supplied pointwise
Definition xcfunctional.h:503
madness::FunctionCommonData< double, 3 > cdata
Definition xcfunctional.h:569
std::vector< std::shared_ptr< functorT > > f
Definition xcfunctional.h:568
madness::FunctionFunctorInterface< double, 3 > functorT
Definition xcfunctional.h:504
bool active() const
Definition xcfunctional.h:517
nemo_u1_functors(const std::vector< std::shared_ptr< functorT > > &u1)
Definition xcfunctional.h:510
void append(const madness::Key< 3 > &key, std::vector< madness::Tensor< double > > &t) const
write U1 and |U1|^2 at this box's quadrature points into t[enum_u1*]
Definition xcfunctional.h:520
nemo_u1_functors()
Definition xcfunctional.h:506
Class to compute the energy functional.
Definition xcfunctional.h:573
nemo_u1_functors u1
empty without a nuclear correlation factor
Definition xcfunctional.h:575
xc_functional(const XCfunctional &xc)
Definition xcfunctional.h:577
madness::Tensor< double > operator()(const madness::Key< 3 > &key, const std::vector< madness::Tensor< double > > &t) const
Definition xcfunctional.h:580
const XCfunctional * xc
Definition xcfunctional.h:574
xc_functional(const XCfunctional &xc, const nemo_u1_functors &u1)
Definition xcfunctional.h:578
Class to compute terms of the kernel.
Definition xcfunctional.h:629
const FunctionCommonData< double, 3 > & cdata
Definition xcfunctional.h:632
const int ispin
Definition xcfunctional.h:631
std::size_t get_result_size() const
Definition xcfunctional.h:639
std::vector< madness::Tensor< double > > operator()(const madness::Key< 3 > &key, const std::vector< madness::Tensor< double > > &t) const
Definition xcfunctional.h:645
const XCfunctional * xc
Definition xcfunctional.h:630
xc_kernel_apply(const XCfunctional &xc, int ispin)
Definition xcfunctional.h:634
Compute the spin-restricted LDA potential using unaryop (only for the initial guess)
Definition xcfunctional.h:30
void operator()(const Key< 3 > &key, Tensor< double > &t) const
Definition xcfunctional.h:33
xc_lda_potential()
Definition xcfunctional.h:31
Class to compute terms of the potential.
Definition xcfunctional.h:592
xc_potential(const XCfunctional &xc, int ispin)
Definition xcfunctional.h:597
std::size_t get_result_size() const
Definition xcfunctional.h:603
xc_potential(const XCfunctional &xc, int ispin, const nemo_u1_functors &u1)
Definition xcfunctional.h:599
const XCfunctional * xc
Definition xcfunctional.h:593
std::vector< madness::Tensor< double > > operator()(const madness::Key< 3 > &key, const std::vector< madness::Tensor< double > > &t) const
Definition xcfunctional.h:615
const int ispin
Definition xcfunctional.h:594
nemo_u1_functors u1
empty without a nuclear correlation factor
Definition xcfunctional.h:595
void e()
Definition test_sig.cc:75
double h(const coord_1d &r)
Definition testgconv.cc:175