MADNESS  version 0.9
chem/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 
9 
10 #include <madness/tensor/tensor.h>
11 #include <vector>
12 #include <algorithm>
13 #include <utility>
14 #include <madness/mra/key.h>
15 
16 #ifdef MADNESS_HAS_LIBXC
17 #include <xc.h>
18 #endif
19 
20 namespace madness {
24 
25  void operator()(const Key<3> & key, Tensor<double>& t) const
26  {
27  int x_rks_s__(const double *r__, double *f, double * dfdra);
28  int c_rks_vwn5__(const double *r__, double *f, double * dfdra);
29  double* rho = t.ptr();
30  for (int i=0; i<t.size(); i++) {
31  double r = std::max(rho[i],1e-12);
32  double q, dq1, dq2;
33  x_rks_s__(&r, &q, &dq1);
34  c_rks_vwn5__(&r, &q, &dq2);
35  rho[i] = dq1 + dq2;
36  }
37  }
38 };
39 
41 class XCfunctional {
42 protected:
44  double hf_coeff;
45  double rhomin, rhotol, sigmin, sigtol; // See initialize and munge*
46 
47 #ifdef MADNESS_HAS_LIBXC
48  std::vector< std::pair<xc_func_type*,double> > funcs;
49  void make_libxc_args(const std::vector< madness::Tensor<double> >& t,
50  madness::Tensor<double>& rho,
51  madness::Tensor<double>& sigma) const;
52  int nderiv;
53 #endif
54 
55 
57 
71  static void polyn(const double x, double& p, double& dpdx) {
72  // All of the static const stuff is evaluated at compile time
73 
74  static const double xmin = 1e-10; // <<<< MINIMUM VALUE OF DENSITY
75  static const double xmax = 1e-8; // <<<< DENSITY SMOOTHLY MODIFIED BELOW THIS VALUE
76 
77  static const double xmax2 = xmax*xmax;
78  static const double xmax3 = xmax2*xmax;
79  static const double xmin2 = xmin*xmin;
80  static const double xmin3 = xmin2*xmin;
81  static const double r = 1.0/((xmax-xmin)*(-xmin3+(3.0*xmin2+(-3.0*xmin+xmax)*xmax)*xmax));
82  static const double a0 = xmax3*xmin*(xmax-4.0*xmin)*r;
83  static const double a = xmin2*(xmin2+(-4.0*xmin+18.0*xmax)*xmax)*r;
84  static const double b = -6.0*xmin*xmax*(3.0*xmax+2.0*xmin)*r;
85  static const double c = (4.0*xmin2+(20.0*xmin+6.0*xmax)*xmax)*r;
86  static const double d = -(8.0*xmax+7.0*xmin)*r;
87  static const double e = 3.0*r;
88 
89  if (x > xmax) {
90  p = x;
91  dpdx = 1.0;
92  }
93  else if (x < xmin) {
94  p = xmin;
95  dpdx = 0.0;
96  }
97  else {
98  p = a0+(a+(b+(c+(d+e*x)*x)*x)*x)*x;
99  dpdx = a+(2.0*b+(3.0*c+(4.0*d+5.0*e*x)*x)*x)*x;
100  }
101  }
102 
103  static double munge_old(double rho) {
104  double p, dpdx;
105  polyn(rho, p, dpdx);
106  return p;
107  }
108 
109 
110  double munge(double rho) const {
111  if (rho <= rhotol) rho=rhomin;
112  return rho;
113  }
114 
115  void munge2(double& rho, double& sigma) const {
116  if (rho < rhotol) rho=rhomin;
117  if (rho < rhotol || sigma < sigtol) sigma=sigmin;
118  }
119 
120  void munge5(double& rhoa, double& rhob, double& saa, double& sab, double& sbb) const {
121  if (rhoa < rhotol || rhob < rhotol || sab < sigtol) sab=sigmin; // ??????????
122 
123  if (rhoa < rhotol) rhoa=rhomin;
124  if (rhoa < rhotol || saa < sigtol) saa=sigmin;
125 
126  if (rhob < rhotol) rhob=rhomin;
127  if (rhob < rhotol || sbb < sigtol) sbb=sigmin;
128  }
129 
130 public:
132  XCfunctional();
133 
135 
138  void initialize(const std::string& input_line, bool polarized);
139 
141  ~XCfunctional();
142 
144  bool is_lda() const;
145 
147  bool is_gga() const;
148 
150  bool is_meta() const;
151 
153  bool is_dft() const;
154 
156  bool is_spin_polarized() const
157  {
158  return spin_polarized;
159  }
160 
162  bool has_fxc() const;
163 
165  bool has_kxc() const;
166 
168  double hf_exchange_coefficient() const
169  {
170  return hf_coeff;
171  }
172 
174 
192  madness::Tensor<double> exc(const std::vector< madness::Tensor<double> >& t , const int ispin) const;
193 
195 
238 
239  madness::Tensor<double> vxc(const std::vector< madness::Tensor<double> >& t, const int ispin, const int what) const;
240 
241  madness::Tensor<double> fxc(const std::vector< madness::Tensor<double> >& t, const int ispin, const int what) const;
242 
244  void plot() const {
245  long npt = 1001;
246  double lo=1e-6, hi=1e+1, s=std::pow(hi/lo, 1.0/(npt-1));
247 
248  madness::Tensor<double> rho(npt);
249  for (int i=0; i<npt; i++) {
250  rho[i] = lo;
251  lo *= s;
252  }
253  std::vector< madness::Tensor<double> > t;
254  t.push_back(rho);
255  if (is_spin_polarized()) t.push_back(rho);
256  madness::Tensor<double> f = exc(t,0); //pending UGHHHHH
257  madness::Tensor<double> va = vxc(t,0,0);
258  madness::Tensor<double> vb = vxc(t,0,1);
259  for (long i=0; i<npt; i++) {
260  printf("%.3e %.3e %.3e %.3e\n", rho[i], f[i], va[i], vb[i]);
261  }
262  }
263 };
264 
267  const XCfunctional* xc;
268  const int ispin;
269 
270  xc_functional(const XCfunctional& xc, int ispin)
271  : xc(&xc), ispin(ispin)
272  {}
273 
274  madness::Tensor<double> operator()(const madness::Key<3> & key, const std::vector< madness::Tensor<double> >& t) const
275  {
276  MADNESS_ASSERT(xc);
277  return xc->exc(t,ispin);
278  }
279 };
280 
282 struct xc_potential {
283  const XCfunctional* xc;
284  const int what;
285  const int ispin;
286 
287  xc_potential(const XCfunctional& xc, int ispin,int what)
288  : xc(&xc), what(what), ispin(ispin)
289  {}
290 
291  madness::Tensor<double> operator()(const madness::Key<3> & key, const std::vector< madness::Tensor<double> >& t) const
292  {
293  MADNESS_ASSERT(xc);
294  madness::Tensor<double> r = xc->vxc(t, ispin, what);
295  return r;
296  }
297 };
298 
300 struct xc_kernel {
301  const XCfunctional* xc;
302  const int what;
303  const int ispin;
304 
305  xc_kernel(const XCfunctional& xc, int ispin,int what)
306  : xc(&xc), what(what), ispin(ispin)
307  {}
308 
309  madness::Tensor<double> operator()(const madness::Key<3> & key, const std::vector< madness::Tensor<double> >& t) const
310  {
311  MADNESS_ASSERT(xc);
312  madness::Tensor<double> r = xc->fxc(t, ispin, what);
313  return r;
314  }
315 };
316 }
317 
318 #endif
int x_rks_s__(const double *r__, double *f, double *dfdra)
Definition: chem/lda.cc:58
void operator()(const Key< 3 > &key, Tensor< double > &t) const
Definition: chem/xcfunctional.h:25
void plot() const
Crude function to plot the energy and potential functionals.
Definition: chem/xcfunctional.h:244
const int what
Definition: chem/xcfunctional.h:302
Compute the spin-restricted LDA potential using unaryop (only for the initial guess) ...
Definition: chem/xcfunctional.h:22
~XCfunctional()
Destructor.
Definition: chem/xcfunctional_ldaonly.cc:55
const double sigma
Definition: dielectric.cc:187
void initialize(const std::string &input_line, bool polarized)
Initialize the object from the user input data.
Definition: chem/xcfunctional_ldaonly.cc:19
bool is_lda() const
Returns true if the potential is lda.
Definition: chem/xcfunctional_ldaonly.cc:57
::std::string string
Definition: gtest-port.h:872
xc_kernel(const XCfunctional &xc, int ispin, int what)
Definition: chem/xcfunctional.h:305
double rhomin
Definition: chem/xcfunctional.h:45
xc_potential(const XCfunctional &xc, int ispin, int what)
Definition: chem/xcfunctional.h:287
static void polyn(const double x, double &p, double &dpdx)
Smoothly switches between constant (xxmax)
Definition: chem/xcfunctional.h:71
const int ispin
Definition: chem/xcfunctional.h:268
NDIM & f
Definition: mra.h:2179
XCfunctional()
Default constructor is required.
Definition: chem/xcfunctional_ldaonly.cc:17
const XCfunctional * xc
Definition: chem/xcfunctional.h:267
Class to compute terms of the potential.
Definition: chem/xcfunctional.h:282
xc_lda_potential()
Definition: chem/xcfunctional.h:23
const int what
Definition: chem/xcfunctional.h:284
madness::Tensor< double > operator()(const madness::Key< 3 > &key, const std::vector< madness::Tensor< double > > &t) const
Definition: chem/xcfunctional.h:274
Defines and implements most of Tensor.
madness::Tensor< double > operator()(const madness::Key< 3 > &key, const std::vector< madness::Tensor< double > > &t) const
Definition: chem/xcfunctional.h:309
#define max(a, b)
Definition: lda.h:53
bool has_kxc() const
Returns true if the third derivative of the functional is available (not yet supported) ...
Definition: chem/xcfunctional_ldaonly.cc:78
bool spin_polarized
True if the functional is spin polarized.
Definition: chem/xcfunctional.h:43
bool has_fxc() const
Returns true if the second derivative of the functional is available (not yet supported) ...
Definition: chem/xcfunctional_ldaonly.cc:73
double munge(double rho) const
Definition: chem/xcfunctional.h:110
FLOAT a(int j, FLOAT z)
Definition: y1.cc:86
xc_functional(const XCfunctional &xc, int ispin)
Definition: chem/xcfunctional.h:270
double hf_coeff
Factor multiplying HF exchange (+1.0 gives HF)
Definition: chem/xcfunctional.h:44
Class to compute terms of the kernel.
Definition: chem/xcfunctional.h:300
static double munge_old(double rho)
Definition: chem/xcfunctional.h:103
void munge5(double &rhoa, double &rhob, double &saa, double &sab, double &sbb) const
Definition: chem/xcfunctional.h:120
const int ispin
Definition: chem/xcfunctional.h:303
double sigmin
Definition: chem/xcfunctional.h:45
madness::Tensor< double > fxc(const std::vector< madness::Tensor< double > > &t, const int ispin, const int what) const
Definition: chem/xcfunctional_ldaonly.cc:150
bool is_meta() const
Returns true if the potential is meta gga (needs second derivatives ... not yet supported) ...
Definition: chem/xcfunctional_ldaonly.cc:65
const int ispin
Definition: chem/xcfunctional.h:285
double hf_exchange_coefficient() const
Returns the value of the hf exact exchange coefficient.
Definition: chem/xcfunctional.h:168
madness::Tensor< double > vxc(const std::vector< madness::Tensor< double > > &t, const int ispin, const int what) const
Computes components of the potential (derivative of the energy functional) at np points.
Definition: chem/xcfunctional_ldaonly.cc:115
const XCfunctional * xc
Definition: chem/xcfunctional.h:283
int c_rks_vwn5__(const double *r__, double *f, double *dfdra)
Definition: chem/lda.cc:116
double sigtol
Definition: chem/xcfunctional.h:45
double rhotol
Definition: chem/xcfunctional.h:45
Class to compute the energy functional.
Definition: chem/xcfunctional.h:266
const XCfunctional * xc
Definition: chem/xcfunctional.h:301
void munge2(double &rho, double &sigma) const
Definition: chem/xcfunctional.h:115
madness::Tensor< double > exc(const std::vector< madness::Tensor< double > > &t, const int ispin) const
Computes the energy functional at given points.
Definition: chem/xcfunctional_ldaonly.cc:83
bool is_gga() const
Returns true if the potential is gga (needs first derivatives)
Definition: chem/xcfunctional_ldaonly.cc:61
Multidimension Key for MRA tree and associated iterators.
Simplified interface to XC functionals.
Definition: chem/xcfunctional.h:41
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
bool is_dft() const
Returns true if there is a DFT functional (false probably means Hatree-Fock exchange only) ...
Definition: chem/xcfunctional_ldaonly.cc:69
const double c
Definition: gfit.cc:200
FLOAT b(int j, FLOAT z)
Definition: y1.cc:79
madness::Tensor< double > operator()(const madness::Key< 3 > &key, const std::vector< madness::Tensor< double > > &t) const
Definition: chem/xcfunctional.h:291
Key is the index for a node of the 2^NDIM-tree.
Definition: key.h:69
bool is_spin_polarized() const
Returns true if the functional is spin_polarized.
Definition: chem/xcfunctional.h:156