MADNESS 0.10.1
oep.h
Go to the documentation of this file.
1/*
2 * oep.h
3 *
4 * Created on: Nov 6, 2019
5 * Author: fbischoff
6 */
7
8#ifndef SRC_APPS_CHEM_OEP_H_
9#define SRC_APPS_CHEM_OEP_H_
10
11
12
13#include<madness/chem/nemo.h>
17
18namespace madness {
19
20/// Class to compute terms of the potential
22
23 double thresh_high=1.e-5;
24 double thresh_low=1.e-7;
25 double eps_regularize=1.e-8;
28
32
33 std::size_t get_result_size() const {
34 return 1;
35 }
36
37 /// @param[in] t vector containing: oep, refdensity, longrange
38 /// @return (num1/denom1 - num2/denom2) * mask + (1-mask)*longrange
39 std::vector<Tensor<double> > operator()(const Key<3> & key,
40 const std::vector<Tensor<double> >& t) const {
41
42 const Tensor<double>& refdens=t[0];
43 const Tensor<double>& num1=t[1];
44 const Tensor<double>& denom1=t[2];
45 const Tensor<double>& num2=t[3];
46 const Tensor<double>& denom2=t[4];
47 const Tensor<double>& longrange=t[5];
48
49 // set 1 if above munge interval (dens > thresh_high) or 0 if below munge interval (dens < thresh_low)
50 // interpolate inbetween with logarithmic form of (r - lo)/(hi - lo) because this yields near linear behavior
51 Tensor<double> U = copy(refdens); // copy refdens in oder to have the same dimension in the tensor
52 U.fill(1.0); // start with a fuction that is 1.0 everywhere
54 U,
55 double r = refdens(IND);
56 double result=num1(IND)/(denom1(IND)+eps_regularize)
57 - num2(IND)/(denom2(IND)+eps_regularize);
58 if (square_denominator) result=num1(IND)/(denom1(IND)*denom1(IND)+eps_regularize)
59 - num2(IND)/(denom2(IND)*denom2(IND)+eps_regularize);
60 if (r > thresh_high) {
61 U(IND) = result;
62 } else if (r < thresh_low) {
63 U(IND) = longrange(IND);
64 } else {
65 // mask = 1 if refdens>hi, 0 if refdens < lo, interpolate in between
66 double mask=(log10(refdens(IND)) - log_low)/(log_high - log_low);
67 U(IND)=mask*result + (1.0-mask)*longrange(IND);
68 }
69 );
70 return std::vector<Tensor<double> > (1,U);
71 }
72};
73
74/// TODO:
75/// - change algorithm to handle localized orbitals
76/// - do not recompute the OEP potentials that don't depend on the orbital energies/Fock matrix elements
77/// - do not recompute the HF contributions to the OEP potentials
78/// - think about the long-range part of the Slater potential (or medium-range)
80public:
81 static constexpr char const *tag = "oep";
82
84 initialize<std::vector<std::string> >("model",{"dcep"},"comment on this: oaep ocep dcep mrks");
85 initialize<unsigned int>("maxiter",150,"maximum number of iterations in OEP algorithm");
86 initialize<bool>("restart",false,"restart from previous OEP calculation");
87 initialize<bool>("no_compute",false,"read from previous OEP calculation, no computation");
88 initialize<double>("levelshift",0.0,"shift occupied orbital energies in the BSH operator");
89// initialize<double>("conv_threshold",1.e-5,"comment on this");
90 initialize<double>("density_threshold_high",1.e-6,"comment on this");
91 initialize<double>("density_threshold_low",1.e-8,"comment on this");
92 initialize<double>("density_threshold_inv",1.e-9,"comment on this");
93 initialize<std::vector<double> >("kain_param",{1.0e-8, 3.0},"comment on this");
94
95// std::vector<bool> oep_model = {false, false, false, false};
96 initialize<unsigned int>("saving_amount",0,"choose level 0, 1, 2 or 3 for saving functions");
97 initialize<unsigned int>("save_iter_orbs",0,"if > 0 save all orbitals every ... iterations (needs a lot of storage!");
98 initialize<unsigned int>("save_iter_density",0,"if > 0 save KS density every ... iterations");
99 initialize<unsigned int>("save_iter_IKS",0,"if > 0 save IKS every ... iterations");
100 initialize<unsigned int>("save_iter_kin_tot_KS",0,"if > 0 save kin_tot_KS every ... iterations");
101 initialize<unsigned int>("save_iter_kin_P_KS",0,"if > 0 save kin_P_KS every ... iterations");
102 initialize<unsigned int>("save_iter_corrections",0,"if > 0 save OEP correction(s) every ... iterations");
103 initialize<unsigned int>("save_iter_effective_potential",0,"if > 0 save effective potential every ... iterations");
104 }
105
107 read_input_and_commandline_options(world, parser, "oep");
108 }
109
110 OEP_Parameters(const OEP_Parameters& other) = default;
111
112
114 set_derived_value("density_threshold_high",10.0*nparam.econv());
115 set_derived_value("density_threshold_low",0.01*get<double>("density_threshold_high"));
117 MADNESS_EXCEPTION("confused thresholds for the long-range transition",1);
118 }
119 set_derived_value("density_threshold_inv",0.1*get<double>("density_threshold_low"));
120
121 if (levelshift()>0.0) {
122 print("levelshift > 0.0 in oep parameters\n\n");
123 MADNESS_EXCEPTION("levelshift > 0.0 in oep parameters",1);
124 }
125 }
126
127 // convenience functions
128 std::vector<std::string> model() const {return get<std::vector<std::string> >("model");}
129
130 bool restart() const {return get<bool>("restart");}
131 bool no_compute() const {return get<bool>("no_compute");}
132 unsigned int maxiter() const {return get<unsigned int>("maxiter");}
133 double levelshift() const {return get<double>("levelshift");}
134// double conv_thresh() const {return get<double>("conv_threshold");}
135 double dens_thresh_hi() const {return get<double>("density_threshold_high");}
136 double dens_thresh_lo() const {return get<double>("density_threshold_low");}
137 double dens_thresh_inv() const{return get<double>("density_threshold_inv");}
138 unsigned int saving_amount() const {return get<unsigned int>("saving_amount");}
139 unsigned int save_iter_corrections () const {return get<unsigned int>("save_iter_corrections");}
140
141 std::vector<double> kain_param() const {return get<std::vector<double> >("kain_param");}
142
143};
144
145
146class OEP : public Nemo {
147
148private:
149
150 /// parameters for this OEP calculation
152
153 /// the wave function reference that determines the local potential
154 std::shared_ptr<const Nemo> reference;
155
156 /// the final local potential
158
159public:
160
161 OEP(World& world, const OEP_Parameters& oepparam, const std::shared_ptr<const Nemo>& reference)
162 : Nemo(world, reference->get_param(), reference->molecule()), // OEP is a nemo calculation, ctor will set param from reference
163 oep_param(oepparam),
165
166 // add tight convergence criteria
167 std::vector<std::string> convergence_crit=param.get<std::vector<std::string> >("convergence_criteria");
168 if (std::find(convergence_crit.begin(),convergence_crit.end(),"each_energy")==convergence_crit.end()) {
169 convergence_crit.push_back("each_energy");
170 }
171 param.set_derived_value("convergence_criteria",convergence_crit);
172 calc->param.set_derived_value("convergence_criteria",convergence_crit);
173
175
176 // check that reference is well converged
177 MADNESS_CHECK_THROW(reference->get_param().converge_each_energy(),"need tightly converged reference for OEP calculation");
178
179 }
180
181
183 : Nemo(world, parser),
184 oep_param(world, parser) {
185
186 // add tight convergence criteria
187 std::vector<std::string> convergence_crit=param.get<std::vector<std::string> >("convergence_criteria");
188 if (std::find(convergence_crit.begin(),convergence_crit.end(),"each_energy")==convergence_crit.end()) {
189 convergence_crit.push_back("each_energy");
190 }
191 param.set_derived_value("convergence_criteria",convergence_crit);
192 calc->param.set_derived_value("convergence_criteria",convergence_crit);
193
194 // set reference
195 auto ref=std::make_shared<Nemo>(world,parser);
196 ref->param.set_derived_value("convergence_criteria",convergence_crit);
197 ref->get_calc()->param.set_derived_value("convergence_criteria",convergence_crit);
198 set_reference(ref);
199
201 }
202
203 std::string name() const {return "oep";}
204
205 static void help() {
206 print_header2("help page for oep");
207 print("The oep code computes local exchange potentials based on a Hartree-Fock calculation from nemo");
208 print("oep --print_parameters\n");
209 print("You can perform a simple calculation by running\n");
210 print("oep --geometry=h2o.xyz\n");
211 print("provided you have an xyz file in your directory.");
212
213 }
214
215 static void print_parameters() {
217 print("default parameters for the oep program are");
218 param.print("oep", "end");
219 print("\n\nthe molecular geometry must be specified in a separate block:");
221 }
222
223 void set_reference(const std::shared_ptr<Nemo> reference1) {
224 reference=reference1;
225 }
226
227 std::shared_ptr<const Nemo> get_reference() const {
228 return reference;
229 }
230
231 void print_parameters(std::vector<std::string> what) const {
232 for (auto w : what) {
233 if (w=="oep") oep_param.print("oep");
234 else if (w=="reference") reference->param.print("dft");
235 else if (w=="oep_calc") param.print("oep_calc");
236 else {MADNESS_EXCEPTION(std::string("unknown parameter set to print "+w).c_str(),1);}
237 }
238 }
239
241 return Vfinal;
242 }
243
244 virtual double value() {
245 return value(calc->molecule.get_all_coords());
246 }
247
248 /// update the json file with calculation input and output
249 void output_calc_info_schema(const double& energy) const;
250
251
252 virtual double value(const Tensor<double>& x) {
253 MADNESS_CHECK_THROW(reference->check_converged(x),"OEP reference is not converged at this geometry");
255 calc->copy_data(world,*(reference->get_calc()));
256 double energy=0.0;
257 Tensor<double> fock;
259 if (load_mos) load_restartdata(fock);
260
261 if (not oep_param.no_compute()) energy=solve(reference->get_calc()->get_amo());
263 return energy;
264 };
265
266 /// Iterative energy calculation for approximate OEP with EXACT EXCHANGE functional
267 /// for other functionals, slater potential must be modified
268 /// HF orbitals and eigenvalues are used as the guess here
269 /// note that KS_nemo is a reference and changes oep->get_calc()->amo orbitals
270 /// same for orbital energies (eigenvalues) KS_eigvals which is oep->get_calc()->aeps
271 /// converged if norm, total energy difference and orbital energy differences (if not OAEP) are converged
272 double solve(const vecfuncT& HF_nemo);
273
274 void analyze();
275
276 double iterate(const std::string model, const vecfuncT& HF_nemo, const tensorT& HF_eigvals,
277 vecfuncT& KS_nemo, tensorT& KS_Fock, real_function_3d& Voep,
278 const real_function_3d Vs) const;
279
280 virtual std::shared_ptr<Fock<double,3>> make_fock_operator() const;
281
282// MolecularOrbitals<double,3> to_MO() const {
283// std::vector<std::string> str_irreps;
284// vecfuncT aaa=symmetry_projector(calc->amo,R_square,str_irreps);
285// return MolecularOrbitals<double,3>(aaa,this->get_calc()->aeps,str_irreps,this->get_calc()->aocc,this->get_calc()->aset);
286// }
287
288 void save_restartdata(const Tensor<double>& fock) const;
289
291
292 std::tuple<Tensor<double>, vecfuncT> recompute_HF(const vecfuncT& HF_nemo) const;
293
294 /// The following function tests all essential parts of the OEP program qualitatively and some also quantitatively
295 bool selftest();
296
297 bool need_ocep_correction(const std::string& model) const {
298 return (model=="ocep") or (model=="dcep") or (model=="mrks");
299 }
300
301 bool need_dcep_correction(const std::string& model) const {
302 return (model=="dcep");
303 }
304
305 bool need_mrks_correction(const std::string& model) const {
306 return (model=="mrks");
307 }
308
309 /// print orbital energies in reverse order with optional shift
310 void print_orbens(const tensorT orbens) const {
311 for (long i = orbens.size() - 1; i >= 0; i--) {
312 printf(" e%2.2lu = %12.8f Eh\n", i, orbens(i));
313 }
314 }
315
316 double compute_and_print_final_energies(const std::string model, const real_function_3d& Voep,
317 const vecfuncT& KS_nemo, const tensorT& KS_Fock,
318 const vecfuncT& HF_nemo, const tensorT& HF_Fock) const;
319
320 /// compute density from orbitals with ragularization (Bischoff, 2014_1, equation (19))
322 real_function_3d density = 2.0*R_square*dot(world, nemo, nemo); // 2 because closed shell
323 return density;
324 }
325
326 /// compute Delta rho as an indicator for the result's quality
327 double compute_delta_rho(const real_function_3d rho_HF, const real_function_3d rho_KS) const {
328 // from Ospadov_2017, equation (26)
329 real_function_3d rho_diff = abs(rho_KS - rho_HF);
330 double Drho = rho_diff.trace();
331 return Drho;
332 }
333
334 /// compute Slater potential (Kohut, 2014, equation (15))
336
337 Exchange<double,3> K(world,reference->get_calc()->param.lo());
338 K.set_bra_and_ket(R_square * nemo, nemo);
339 const vecfuncT Knemo = K(nemo);
340 // 2.0*R_square in numerator and density (rho) cancel out upon division
341 real_function_3d numerator = -1.0*dot(world, nemo, Knemo);
342 real_function_3d rho = dot(world, nemo, nemo);
343
344 // long-range asymptotic behavior for Slater potential is \int 1/|r-r'| * |phi_HOMO|^2 dr'
345 // in order to compute this lra, use Coulomb potential with only HOMO density (= |phi_HOMO|^2)
346// Coulomb J(world);
347// J.reset_poisson_operator_ptr(param.lo(),param.econv());
348// real_function_3d lra = -1.0*J.compute_potential(R_square*square(nemo[homo_ind]));
349 Coulomb<double,3> J(world,this);
351// print("compute long-range part of the Slater potential from the full molecular density");
352 if (oep_param.saving_amount() >= 3) save(lra, "lra_slater");
353
355 real_function_3d one=real_factory_3d(world).functor([](const coord_3d& r){return 1.0;});
356 std::vector<real_function_3d> args={R_square*rho,numerator,rho,zero,one,lra};
358
362 return VSlater;
363 }
364
365
366 /// compute the total kinetic energy density of equation (6) from Kohut
367
368 /// return without the NCF and factor 2 for closed shell !
370
371 // compute tau = 1/2 * sum |\nabla phi_i|^2
372 // = 1/2 * sum {(\nabla R)^2 * nemo_i^2 + 2 * R * nemo_i * (\nabla R) * (\nabla nemo_i)) + R^2 * (\nabla nemo_i)^2}
373 // = 1/2 * R^2 * sum {U1dot * nemo_i^2 + 2 * nemo_i * U1 * (\nabla nemo_i)) + (\nabla nemo_i)^2}
374
375 // get \nabla R and (\nabla R)^2 via and U1 = -1/R \nabla R and U1dot = (1/R \nabla R)^2
376 const vecfuncT U1 = this->ncf->U1vec();
378 const real_function_3d U1dot = real_factory_3d(world).functor(u1_dot_u1).truncate_on_project();
379
380 real_function_3d u1u1term=U1dot*dot(world,nemo,nemo);
383
384 for (int idim=0; idim<3; ++idim) {
386 if(param.dft_deriv() == "bspline") D.set_bspline1();
387 vecfuncT nemo_copy=copy(world,nemo);
388 refine(world,nemo_copy);
389 std::vector<real_function_3d> dnemo=apply(world,D,nemo_copy);
390 u1dnterm+=U1[idim]*dot(world,nemo_copy,dnemo);
391 refine(world,dnemo);
392 dndnterm+=dot(world,dnemo,dnemo);
393 }
394
395 real_function_3d tau=0.5*(u1u1term - 2.0*u1dnterm + dndnterm);
396
397 return tau;
398
399 }
400
401 /// compute the Pauli kinetic energy density divided by the density tau_P/rho with equation (16) from Ospadov, 2017
403
404 // compute the numerator tau_P
405
406 // get \nabla nemo
407 std::vector<vecfuncT> grad_nemo(nemo.size());
408 for (size_t i = 0; i < nemo.size(); i++) {
409 vecfuncT nemo_copy=copy(world,nemo);
410 refine(world,nemo_copy);
411
412 if(param.dft_deriv() == "bspline") grad_nemo[i] = grad_bspline_one(nemo_copy[i]); // gradient using b-spline
413 else grad_nemo[i] = grad(nemo_copy[i]); // default gradient using abgv
414 }
415
416 vecfuncT grad_nemo_term;
417 for (size_t i = 0; i < nemo.size(); i++) {
418 for (size_t j = i + 1; j < nemo.size(); j++) {
419 vecfuncT tmp = nemo[i]*grad_nemo[j] - nemo[j]*grad_nemo[i];
420 grad_nemo_term.push_back(dot(world, tmp, tmp));
421 }
422 }
423 real_function_3d numerator = sum(world, grad_nemo_term); // numerator = tau_P * 2 * rho / R^4
424
425 return numerator;
426
427 }
428
429 /// compute correction of the given model
430
431 /// shift of the diagonal elements of the fock matrix results in a global shift
432 /// of the potential
433 /// \f[
434 /// \frac{\bar \epsilon}{\rho} = \frac{1}{\rho}\sum_{ij}\phi_i(F_ij+\delta_ij s)\phi_j
435 /// = \frac{1}{\rho} ( \sum_{ij}\phi_i F_ij \phi_j + s\sum_i\phi_i\phi_i )
436 /// = s + \frac{1}{\rho} \sum_{ij}\phi_i F_ij \phi_j
437 /// \f]
439 const vecfuncT& nemoHF, const vecfuncT& nemoKS,
440 const tensorT& fockHF, const tensorT& fockKS) const {
441
442 if (fockKS.normf()<1.e-10) {
444 return zero;
445 }
446
447 // compute HOMO energies and the long-range asymptotics
448 auto [eval, evec] = syev(fockKS);
449 double homoKS = -eval.max();
450
451 auto [eval1, evec1] = syev(fockHF);
452 double homoHF = -eval1.max();
453
454 double longrange=homoHF-homoKS;
455
456 tensorT fock1=copy(fockKS);
457 for (int i=0; i<fock1.dim(0); ++i) fock1(i,i)-=longrange;
458
459 // 2.0*R_square in numerator and density (rho) cancel out upon division
460// real_function_3d numeratorHF=-1.0*compute_energy_weighted_density_local(nemoHF,fockHF);
461// real_function_3d numeratorHF=-1.0*compute_energy_weighted_density(nemoHF,eigvalsHF);
462 real_function_3d numeratorKS=-1.0*compute_energy_weighted_density_local(nemoKS,fock1);
463// real_function_3d numeratorKS=-1.0*compute_energy_weighted_density(nemoKS,eval);
464
465 // 2.0*R_square in numerator and density (rho) cancel out upon division
466 real_function_3d densityKS = dot(world, nemoKS, nemoKS);
467 real_function_3d densityHF = dot(world, nemoHF, nemoHF);
468
470 std::vector<real_function_3d> args={densityKS,ocep_numerator_HF,densityHF,
471 numeratorKS,densityKS,lra_func};
473
476 real_function_3d correction=multi_to_multi_op_values(op,args)[0];
477
478
479 return correction;
480
481 }
482
483 /// return without the NCF and factor 2 for closed shell !
484
485 /// follow Eq. (4) of Ryabinkin2014
487 const tensorT& fock) const {
488 return dot(world,nemo,transform(world,nemo,fock));
489 }
490
491 /// compute correction of the given model
493 const vecfuncT& nemoHF, const vecfuncT& nemoKS) const {
494
495 // 2.0*R_square in numerator and density (rho) cancel out upon division
496// real_function_3d numeratorHF=compute_total_kinetic_density(nemoHF);
498
499 // 2.0*R_square in numerator and density (rho) cancel out upon division
500 real_function_3d densityKS = dot(world, nemoKS, nemoKS);
501 real_function_3d densityHF = dot(world, nemoHF, nemoHF);
502
503 real_function_3d lra_func=real_factory_3d(world).functor([](const coord_3d& r)
504 {return 0.0;});
505
506 std::vector<real_function_3d> args={densityKS,dcep_numerator_HF,densityHF,
507 numeratorKS,densityKS,lra_func};
509
512 real_function_3d correction=multi_to_multi_op_values(op,args)[0];
513
514 return correction;
515 }
516
517 /// compute correction of the given model
519 const vecfuncT& nemoHF, const vecfuncT& nemoKS) const {
520
521 // 2.0*R_square in numerator and density (rho) cancel out upon division
522// real_function_3d numeratorHF=compute_Pauli_kinetic_density(nemoHF);
524
525 // 2.0*R_square in numerator and density (rho) cancel out upon division
526 real_function_3d densityKS = dot(world, nemoKS, nemoKS);
527 real_function_3d densityHF = dot(world, nemoHF, nemoHF);
528
529 // longrange correction is zero
530 real_function_3d lra_func=real_factory_3d(world).functor([](const coord_3d& r) {return 0.0;});
531
532 real_function_3d denssq=square(densityKS);
533 std::vector<real_function_3d> args={densityKS,mrks_numerator_HF,densityHF,
534 numeratorKS,densityKS,lra_func};
536
539 op.square_denominator=true;
540 real_function_3d correction=0.5*multi_to_multi_op_values(op,args)[0];
541
542 return correction;
543 }
544
545 /// compute all potentials from given nemos except kinetic energy
546 void compute_nemo_potentials(const vecfuncT& nemo, vecfuncT& Jnemo, vecfuncT& Unemo) const {
547
548 // compute Coulomb part
549 compute_coulomb_potential(nemo, Jnemo);
550
551 // compute nuclear potential part
552 Nuclear<double,3> Unuc(world, this->ncf);
553 Unemo = Unuc(nemo);
554
555 }
556
557 /// compute Coulomb potential
558 void compute_coulomb_potential(const vecfuncT& nemo, vecfuncT& Jnemo) const {
559
560 Coulomb<double,3> J(world, this);
563 Jnemo = J(nemo);
564 truncate(world, Jnemo);
565
566 }
567
568 /// compute exchange potential (needed for Econv)
569 void compute_exchange_potential(const vecfuncT& nemo, vecfuncT& Knemo) const {
570
572 K.set_bra_and_ket(R_square * nemo, nemo);
573 Knemo = K(nemo);
574 truncate(world, Knemo);
575
576 }
577
578 /// compute Evir using Levy-Perdew virial relation (Kohut_2014, (43) or Ospadov_2017, (25))
579 double compute_exchange_energy_vir(const vecfuncT& nemo, const real_function_3d Vx) const {
580
581 // make vector of functions r = (x, y, z)
582 auto monomial_x = [] (const coord_3d& r) {return r[0];};
583 auto monomial_y = [] (const coord_3d& r) {return r[1];};
584 auto monomial_z = [] (const coord_3d& r) {return r[2];};
585 vecfuncT r(3);
586 r[0]=real_factory_3d(world).functor(monomial_x);
587 r[1]=real_factory_3d(world).functor(monomial_y);
588 r[2]=real_factory_3d(world).functor(monomial_z);
589
590 real_function_3d rhonemo = 2.0*dot(world, nemo, nemo); // 2 because closed shell
591
592 real_function_3d rhoterm = 3*rhonemo + dot(world, r, -2.0*ncf->U1vec()*rhonemo + grad(rhonemo,true));
593 double Ex = inner(Vx, R_square*rhoterm);
594 return Ex;
595
596 }
597
598 /// compute exchange energy using the expectation value of the exchange operator
599 double compute_exchange_energy_conv(const vecfuncT phi, const vecfuncT Kphi) const {
600
601 double Ex = -1.0*inner(world, phi, Kphi).sum();
602 return Ex;
603
604 }
605
606 /// compute all energy contributions except exchange and sum up for total energy
607 /// the exchange energy must be computed priorly with the compute_exchange_energy_... methods
608 std::vector<double> compute_energy(const vecfuncT& nemo, const double E_X) const {
609
610 // compute kinetic energy
611 double E_kin = compute_kinetic_energy(nemo);
612
614
615 // compute external potential (nuclear attraction)
616 real_function_3d Vext = calc->potentialmanager->vnuclear();
617 Coulomb<double,3> J(world,this);
619
620 // compute remaining energies: nuclear attraction, Coulomb, nuclear repulsion
621 // computed as expectation values (see Szabo, Ostlund (3.81))
622 const double E_ext = inner(Vext,density); // 2 because closed shell
623 const double E_J = 0.5*inner(density,Jpotential);
624 const double E_nuc = calc->molecule.nuclear_repulsion_energy();
625 double energy = E_kin + E_ext + E_J + E_X + E_nuc;
626
627 if (world.rank() == 0) {
628 printf("\n kinetic energy %15.8f Eh\n", E_kin);
629 printf(" electron-nuclear attraction energy %15.8f Eh\n", E_ext);
630 printf(" Coulomb energy %15.8f Eh\n", E_J);
631 printf(" exchange energy %15.8f Eh\n", E_X);
632 printf(" nuclear-nuclear repulsion energy %15.8f Eh\n", E_nuc);
633 printf(" total energy %15.8f Eh\n\n", energy);
634 }
635 return {energy,E_kin,E_ext,E_J,E_X};
636
637 }
638
639 /// compute diagonal elements of Fock matrix
641 const vecfuncT& phi, const vecfuncT& Kphi, const real_function_3d& Vx) const {
642 return KS_eigvals - inner(world, phi, Kphi) - inner(world, phi, Vx*phi);
643 }
644
645 /// cumpute E^(0) = \sum_i \epsilon_i^KS
646 double compute_E_zeroth(const tensorT eigvals) const {
647 double E_0 = 0.0;
648 for (long i = 0; i < eigvals.size(); i++) {
649 E_0 += eigvals(i);
650 }
651 E_0 *= 2.0; // closed shell: every orbital energy must be counted twice
652 return E_0;
653 }
654
655 /// compute E^(1) = 1/2*\sum_ij <ij||ij> - \sum_i <i|J + Vx|i> = \sum_i <i|- 0.5*J - 0.5*K - Vx|i>
656 double compute_E_first(const vecfuncT phi, const vecfuncT Jphi, const vecfuncT Kphi, const real_function_3d Vx) const {
657
658 //compute expectation values:
659 const double E_J = inner(world, phi, Jphi).sum();
660 const double E_K = inner(world, phi, Kphi).sum();
661 const double E_Vx = inner(world, phi, Vx*phi).sum();
662 printf(" E_J = %15.8f Eh\n", E_J);
663 printf(" E_K = %15.8f Eh\n", E_K);
664 printf(" E_Vx = %15.8f Eh\n", E_Vx);
665 const double E_nuc = calc->molecule.nuclear_repulsion_energy();
666
667 double E_1 = -1.0*(E_J + E_K + 2.0*E_Vx) + E_nuc;
668 return E_1;
669
670 }
671
672};
673
674
675
676} /* namespace madness */
677
678#endif /* SRC_APPS_CHEM_OEP_H_ */
double w(double t, double eps)
Definition DKops.h:22
Operators for the molecular HF and DFT code.
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
Definition SCFOperators.h:334
Function< T, NDIM > compute_potential(const Function< T, NDIM > &density) const
given a density compute the Coulomb potential
Definition SCFOperators.h:432
const real_function_3d & potential() const
getter for the Coulomb potential
Definition SCFOperators.h:421
Implements derivatives operators with variety of boundary conditions on simulation domain.
Definition derivative.h:266
Definition SCFOperators.h:104
Exchange & set_bra_and_ket(const vecfuncT &bra, const vecfuncT &ket)
Definition SCFOperators.cc:712
T trace() const
Returns global value of int(f(x),x) ... global comm required.
Definition mra.h:1154
bool compressed
Definition mra.h:1168
Key is the index for a node of the 2^NDIM-tree.
Definition key.h:69
static void print_parameters()
Definition molecule.cc:115
real_function_3d R_square
the square of the nuclear correlation factor
Definition nemo.h:319
double compute_kinetic_energy(const std::vector< Function< T, NDIM > > &nemo) const
compute kinetic energy as square of the "analytical" expectation value
Definition nemo.h:183
World & world
Definition nemo.h:310
The Nemo class.
Definition nemo.h:326
void set_protocol(const double thresh)
adapt the thresholds consistently to a common value
Definition nemo.h:631
std::shared_ptr< SCF > get_calc() const
Definition nemo.h:496
NemoCalculationParameters param
Definition nemo.h:585
const NemoCalculationParameters & get_param() const
Definition nemo.h:498
std::shared_ptr< SCF > calc
Definition nemo.h:582
void load_mos(World &w)
Definition nemo.h:412
Molecule & molecule()
return a reference to the molecule
Definition nemo.h:506
functor for a local U1 dot U1 potential
Definition correlationfactor.h:532
Definition SCFOperators.h:455
Definition oep.h:79
OEP_Parameters(World &world, const commandlineparser &parser)
Definition oep.h:106
static constexpr char const * tag
Definition oep.h:81
std::vector< std::string > model() const
Definition oep.h:128
unsigned int maxiter() const
Definition oep.h:132
double dens_thresh_inv() const
Definition oep.h:137
bool restart() const
Definition oep.h:130
OEP_Parameters(const OEP_Parameters &other)=default
bool no_compute() const
Definition oep.h:131
unsigned int save_iter_corrections() const
Definition oep.h:139
void set_derived_values(const Nemo::NemoCalculationParameters &nparam)
Definition oep.h:113
unsigned int saving_amount() const
Definition oep.h:138
double dens_thresh_hi() const
Definition oep.h:135
OEP_Parameters()
Definition oep.h:83
std::vector< double > kain_param() const
Definition oep.h:141
double levelshift() const
Definition oep.h:133
double dens_thresh_lo() const
Definition oep.h:136
Definition oep.h:146
bool selftest()
The following function tests all essential parts of the OEP program qualitatively and some also quant...
Definition madness/chem/oep.cc:402
bool need_mrks_correction(const std::string &model) const
Definition oep.h:305
void set_reference(const std::shared_ptr< Nemo > reference1)
Definition oep.h:223
OEP_Parameters oep_param
parameters for this OEP calculation
Definition oep.h:151
static void help()
Definition oep.h:205
double compute_exchange_energy_vir(const vecfuncT &nemo, const real_function_3d Vx) const
compute Evir using Levy-Perdew virial relation (Kohut_2014, (43) or Ospadov_2017, (25))
Definition oep.h:579
real_function_3d compute_total_kinetic_density(const vecfuncT &nemo) const
compute the total kinetic energy density of equation (6) from Kohut
Definition oep.h:369
void save_restartdata(const Tensor< double > &fock) const
Definition madness/chem/oep.cc:84
virtual std::shared_ptr< Fock< double, 3 > > make_fock_operator() const
the OEP Fock operator is the HF Fock operator without exchange but with the OEP
Definition madness/chem/oep.cc:132
Tensor< double > compute_fock_diagonal_elements(const Tensor< double > &KS_eigvals, const vecfuncT &phi, const vecfuncT &Kphi, const real_function_3d &Vx) const
compute diagonal elements of Fock matrix
Definition oep.h:640
double compute_and_print_final_energies(const std::string model, const real_function_3d &Voep, const vecfuncT &KS_nemo, const tensorT &KS_Fock, const vecfuncT &HF_nemo, const tensorT &HF_Fock) const
Definition madness/chem/oep.cc:140
double compute_exchange_energy_conv(const vecfuncT phi, const vecfuncT Kphi) const
compute exchange energy using the expectation value of the exchange operator
Definition oep.h:599
double compute_delta_rho(const real_function_3d rho_HF, const real_function_3d rho_KS) const
compute Delta rho as an indicator for the result's quality
Definition oep.h:327
void print_parameters(std::vector< std::string > what) const
Definition oep.h:231
void compute_nemo_potentials(const vecfuncT &nemo, vecfuncT &Jnemo, vecfuncT &Unemo) const
compute all potentials from given nemos except kinetic energy
Definition oep.h:546
real_function_3d compute_slater_potential(const vecfuncT &nemo) const
compute Slater potential (Kohut, 2014, equation (15))
Definition oep.h:335
virtual double value()
Definition oep.h:244
bool need_dcep_correction(const std::string &model) const
Definition oep.h:301
real_function_3d compute_energy_weighted_density_local(const vecfuncT &nemo, const tensorT &fock) const
return without the NCF and factor 2 for closed shell !
Definition oep.h:486
real_function_3d compute_mrks_correction(const real_function_3d &mrks_numerator_HF, const vecfuncT &nemoHF, const vecfuncT &nemoKS) const
compute correction of the given model
Definition oep.h:518
std::tuple< Tensor< double >, vecfuncT > recompute_HF(const vecfuncT &HF_nemo) const
Definition madness/chem/oep.cc:120
std::string name() const
Definition oep.h:203
virtual double value(const Tensor< double > &x)
Should return the value of the objective function.
Definition oep.h:252
double compute_E_first(const vecfuncT phi, const vecfuncT Jphi, const vecfuncT Kphi, const real_function_3d Vx) const
compute E^(1) = 1/2*\sum_ij <ij||ij> - \sum_i <i|J + Vx|i> = \sum_i <i|- 0.5*J - 0....
Definition oep.h:656
real_function_3d compute_Pauli_kinetic_density(const vecfuncT &nemo) const
compute the Pauli kinetic energy density divided by the density tau_P/rho with equation (16) from Osp...
Definition oep.h:402
OEP(World &world, const OEP_Parameters &oepparam, const std::shared_ptr< const Nemo > &reference)
Definition oep.h:161
double iterate(const std::string model, const vecfuncT &HF_nemo, const tensorT &HF_eigvals, vecfuncT &KS_nemo, tensorT &KS_Fock, real_function_3d &Voep, const real_function_3d Vs) const
Definition madness/chem/oep.cc:214
void compute_coulomb_potential(const vecfuncT &nemo, vecfuncT &Jnemo) const
compute Coulomb potential
Definition oep.h:558
std::shared_ptr< const Nemo > get_reference() const
Definition oep.h:227
void print_orbens(const tensorT orbens) const
print orbital energies in reverse order with optional shift
Definition oep.h:310
void load_restartdata(Tensor< double > &fock)
Definition madness/chem/oep.cc:102
double compute_E_zeroth(const tensorT eigvals) const
cumpute E^(0) = \sum_i \epsilon_i^KS
Definition oep.h:646
real_function_3d compute_density(const vecfuncT &nemo) const
compute density from orbitals with ragularization (Bischoff, 2014_1, equation (19))
Definition oep.h:321
static void print_parameters()
Definition oep.h:215
real_function_3d get_final_potential() const
Definition oep.h:240
real_function_3d Vfinal
the final local potential
Definition oep.h:157
std::shared_ptr< const Nemo > reference
the wave function reference that determines the local potential
Definition oep.h:154
OEP(World &world, const commandlineparser &parser)
Definition oep.h:182
void analyze()
Definition madness/chem/oep.cc:67
void compute_exchange_potential(const vecfuncT &nemo, vecfuncT &Knemo) const
compute exchange potential (needed for Econv)
Definition oep.h:569
void output_calc_info_schema(const double &energy) const
update the json file with calculation input and output
Definition madness/chem/oep.cc:58
std::vector< double > compute_energy(const vecfuncT &nemo, const double E_X) const
Definition oep.h:608
bool need_ocep_correction(const std::string &model) const
Definition oep.h:297
real_function_3d compute_dcep_correction(const real_function_3d &dcep_numerator_HF, const vecfuncT &nemoHF, const vecfuncT &nemoKS) const
compute correction of the given model
Definition oep.h:492
real_function_3d compute_ocep_correction(const real_function_3d &ocep_numerator_HF, const vecfuncT &nemoHF, const vecfuncT &nemoKS, const tensorT &fockHF, const tensorT &fockKS) const
compute correction of the given model
Definition oep.h:438
double solve(const vecfuncT &HF_nemo)
Definition madness/chem/oep.cc:22
class for holding the parameters for calculation
Definition QCCalculationParametersBase.h:294
virtual void read_input_and_commandline_options(World &world, const commandlineparser &parser, const std::string tag)
Definition QCCalculationParametersBase.h:328
T get(const std::string key) const
Definition QCCalculationParametersBase.h:302
void print(const std::string header="", const std::string footer="") const
print all parameters
Definition QCCalculationParametersBase.cc:22
void set_derived_value(const std::string &key, const T &value)
Definition QCCalculationParametersBase.h:406
virtual real_function_3d density() const
Definition QCPropertyInterface.h:25
A tensor is a multidimensional array.
Definition tensor.h:317
float_scalar_type normf() const
Returns the Frobenius norm of the tensor.
Definition tensor.h:1726
Tensor< T > & fill(T x)
Inplace fill with a scalar (legacy name)
Definition tensor.h:562
A parallel world class.
Definition world.h:132
ProcessID rank() const
Returns the process rank in this World (same as MPI_Comm_rank()).
Definition world.h:320
double(* energy)()
Definition derivatives.cc:58
static double lo
Definition dirac-hatom.cc:23
real_function_3d mask
Definition dirac-hatom.cc:27
vecfuncT K(vecfuncT &ket, vecfuncT &bra, vecfuncT &vf)
Tensor< double > op(const Tensor< double > &x)
Definition kain.cc:508
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition madness_exception.h:119
#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 DFParameters.h:10
void print_header2(const std::string &s)
medium section heading
Definition print.cc:54
double abs(double x)
Definition complexfun.h:48
Function< T, NDIM > square(const Function< T, NDIM > &f, bool fence=true)
Create a new function that is the square of f - global comm only if not reconstructed.
Definition mra.h:2746
std::vector< Function< T, NDIM > > grad_bspline_one(const Function< T, NDIM > &f, bool refine=false, bool fence=true)
Definition vmra.h:2093
void truncate(World &world, response_space &v, double tol, bool fence)
Definition basic_operators.cc:31
std::vector< Function< TENSOR_RESULT_TYPE(T, R), NDIM > > transform(World &world, const std::vector< Function< T, NDIM > > &v, const Tensor< R > &c, bool fence=true)
Transforms a vector of functions according to new[i] = sum[j] old[j]*c[j,i].
Definition vmra.h:707
std::vector< Function< T, NDIM > > multi_to_multi_op_values(const opT &op, const std::vector< Function< T, NDIM > > &vin, const bool fence=true)
apply op on the input vector yielding an output vector of functions
Definition vmra.h:1882
FunctionFactory< double, 3 > real_factory_3d
Definition functypedefs.h:108
void print(const T &t, const Ts &... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition print.h:225
response_space apply(World &world, std::vector< std::vector< std::shared_ptr< real_convolution_3d > > > &op, response_space &f)
Definition basic_operators.cc:43
void refine(World &world, const std::vector< Function< T, NDIM > > &vf, bool fence=true)
refine the functions according to the autorefine criteria
Definition vmra.h:196
Function< TENSOR_RESULT_TYPE(T, R), NDIM > dot(World &world, const std::vector< Function< T, NDIM > > &a, const std::vector< Function< R, NDIM > > &b, bool fence=true)
Multiplies and sums two vectors of functions r = \sum_i a[i] * b[i].
Definition vmra.h:1565
double inner(response_space &a, response_space &b)
Definition response_functions.h:640
vector< functionT > vecfuncT
Definition corepotential.cc:58
void refine_to_common_level(World &world, std::vector< Function< T, NDIM > > &vf, bool fence=true)
refine all functions to a common (finest) level
Definition vmra.h:206
std::vector< Function< T, NDIM > > grad(const Function< T, NDIM > &f, bool refine=false, bool fence=true)
shorthand gradient operator
Definition vmra.h:2033
void save(const Function< T, NDIM > &f, const std::string name)
Definition mra.h:2835
Function< T, NDIM > copy(const Function< T, NDIM > &f, const std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > &pmap, bool fence=true)
Create a new copy of the function with different distribution and optional fence.
Definition mra.h:2066
void syev(const Tensor< T > &A, Tensor< T > &V, Tensor< typename Tensor< T >::scalar_type > &e)
Real-symmetric or complex-Hermitian eigenproblem.
Definition lapack.cc:969
Definition test_ar.cc:204
double econv() const
Definition CalculationParameters.h:145
int nalpha() const
Definition CalculationParameters.h:166
std::string dft_deriv() const
Definition CalculationParameters.h:198
int nbeta() const
Definition CalculationParameters.h:167
double lo() const
Definition CalculationParameters.h:181
class holding parameters for a nemo calculation beyond the standard dft parameters from moldft
Definition nemo.h:333
very simple command line parser
Definition commandlineparser.h:15
Class to compute terms of the potential.
Definition oep.h:21
double log_high
Definition oep.h:26
std::vector< Tensor< double > > operator()(const Key< 3 > &key, const std::vector< Tensor< double > > &t) const
Definition oep.h:39
double log_low
Definition oep.h:26
bool square_denominator
Definition oep.h:27
std::size_t get_result_size() const
Definition oep.h:33
double eps_regularize
Definition oep.h:25
divide_add_interpolate(double hi, double lo, double eps_regularize)
Definition oep.h:29
double thresh_high
Definition oep.h:23
double thresh_low
Definition oep.h:24
Definition dirac-hatom.cc:108
#define ITERATOR(t, exp)
Definition tensor_macros.h:249
#define IND
Definition tensor_macros.h:204
AtomicInt sum
Definition test_atomicint.cc:46
constexpr coord_t one(1.0)