35#ifndef MADNESS_MRA_GFIT_H__INCLUDED
36#define MADNESS_MRA_GFIT_H__INCLUDED
58template<
typename T, std::
size_t NDIM>
70 MADNESS_CHECK_THROW(hi>0,
"hi must be positive in gfit: U need to set it manually in operator.h");
71 double eps =
info.thresh;
86 print(
"Operator type not implemented: ",
type);
134 if (eps >= 1
e-4) TT = 10.0;
135 else if (eps >= 1
e-6) TT = 14.0;
136 else if (eps >= 1
e-8) TT = 18.0;
137 else if (eps >= 1
e-10) TT = 22.0;
138 else if (eps >= 1
e-12) TT = 26.0;
140 double slo = std::log(eps/hi) - 1.0;
141 double shi = 0.5 * std::log(TT/(
lo*
lo));
142 double h = 1.0/(0.2 - 0.5*std::log10(eps));
143 h = std::floor(64.0*
h)/64.0;
144 shi = std::ceil(shi/
h)*
h;
145 slo = std::floor(slo/
h)*
h;
146 long npt = long((shi - slo)/
h + 0.5);
148 std::vector<double> c_vec, a_vec;
149 for (
int i=0; i<npt; ++i) {
150 double s = slo +
h*(npt - i);
151 double alpha = std::exp(2.0*s);
152 double coeff = 2.0*
h*
alpha;
153 c_vec.push_back(coeff);
154 a_vec.push_back(
alpha);
158 for (
size_t i=0; i<c_vec.size(); ++i) {
159 fit.coeffs_[i] = c_vec[i];
160 fit.exponents_[i] = a_vec[i];
163 print(
"inverse-square fit");
164 auto exact = [](
const double r) ->
double {
return 1.0/(r*r); };
179 static GFit BSHFit(
double mu,
double lo,
double hi,
double eps,
bool prnt=
false) {
181 bool fix_interval=
false;
202 static GFit SlaterFit(
double gamma,
double lo,
double hi,
double eps,
bool prnt=
false) {
207 auto exact = [&gamma](
const double r) ->
double {
return exp(-gamma * r); };
222 static GFit GaussFit(
double gamma,
double lo,
double hi,
double eps,
bool prnt=
false) {
227 fit.exponents_=gamma;
230 auto exact = [&gamma](
const double r) ->
double {
return exp(-gamma * r*r); };
245 static GFit F12Fit(
double gamma,
double lo,
double hi,
double eps,
bool prnt=
false) {
248 fit.coeffs_*=(0.5/gamma);
251 auto exact=[&gamma](
const double r) ->
double {
return 0.5/gamma*(1.0-exp(-gamma*r));};
266 static GFit F12sqFit(
double gamma,
double lo,
double hi,
double eps,
bool prnt=
false) {
269 fit.coeffs_*=(0.25/(gamma*gamma));
272 auto exact=[&gamma](
const double r) ->
double {
return std::pow(0.5/gamma*(1.0-exp(-gamma*r)),2.0);};
289 static GFit FGFit(
double gamma,
double lo,
double hi,
double eps,
bool prnt=
false) {
290 GFit bshfit,coulombfit;
294 bool fix_interval=
true;
300 auto diffcoefficients=(coulombfit.
coeffs() - bshfit.
coeffs());
308 auto exact=[&gamma](
const double r) ->
double {
return 0.5/gamma*(1.0-exp(-gamma*r))/r;};
323 static GFit F2GFit(
double gamma,
double lo,
double hi,
double eps,
bool prnt=
false) {
324 GFit bshfit,coulombfit,bsh2fit;
328 bool fix_interval=
true;
344 double fourmu2=4.0*gamma*gamma;
345 f2gfit.
coeffs_=fourpi/fourmu2*coefficients;
350 auto exact=[&gamma](
const double r) ->
double {
351 return 0.25/(gamma*gamma)*(1.0-2.0*exp(-gamma*r)+exp(-2.0*gamma*r))/r;
380 const long first_prunable = 1) {
381 double mid =
lo + (hi-
lo)*0.5;
382 long npt=coeff.
size();
384 for (i=npt-1; i>=std::max(first_prunable, 1L); --i) {
385 double cnew = coeff[i]*exp(-(expnt[i]-expnt[i-1])*mid*mid);
386 double errlo = coeff[i]*exp(-expnt[i]*
lo*
lo) -
387 cnew*exp(-expnt[i-1]*
lo*
lo);
388 double errhi = coeff[i]*exp(-expnt[i]*hi*hi) -
389 cnew*exp(-expnt[i-1]*hi*hi);
392 coeff[i-1] = coeff[i-1] + cnew;
394 coeff = coeff(
Slice(0,npt-1));
395 expnt = expnt(
Slice(0,npt-1));
417 const std::array<LatticeRange, NDIM>& lattice_ranges,
419 double lo,
double hi_fin,
double eps) {
420 const bool infinite_any = std::any_of(lattice_ranges.begin(), lattice_ranges.end(), [](
const auto&
b) { return b.infinite(); });
421 const bool infinite_all = std::all_of(lattice_ranges.begin(), lattice_ranges.end(), [](
const auto&
b) { return b.infinite(); });
422 if (!infinite_any)
return;
426 double max_infinite_width = 0;
427 for (std::size_t
d = 0;
d !=
NDIM; ++
d)
428 if (lattice_ranges[
d].infinite()) max_infinite_width = std::max(max_infinite_width,
cell_width(
long(
d)));
429 const double tcut = 0.25 / (max_infinite_width * max_infinite_width);
433 for (
long i = 0; i <
e.dim(0); ++i) {
434 if (
e(i) < tcut) { icut = i;
break; }
436 if (icut < 0)
return;
455 template<
typename funcT>
465 template<
typename opT>
468 std::cout <<
"weights and roots" << std::endl;
469 for (
int i=0; i<
coeffs_.size(); ++i)
473 std::cout <<
" x value abserr relerr" << std::endl;
474 std::cout <<
" ------------ ------- -------- -------- " << std::endl;
475 double step = exp(log(hi/
lo)/(npt+1));
476 for (
int i=0; i<=npt; ++i) {
477 double r =
lo*(
pow(step,i+0.5));
481 for (
int j=0; j<
coeffs_.dim(0); ++j)
507 if (
mu < 0.0)
throw "cannot handle negative mu in bsh_fit";
511 if ((
mu > 0) and (not fix_interval)) {
522 if (eps >= 1
e-2) TT = 5;
523 else if (eps >= 1
e-4) TT = 10;
524 else if (eps >= 1
e-6) TT = 14;
525 else if (eps >= 1
e-8) TT = 18;
526 else if (eps >= 1
e-10) TT = 22;
527 else if (eps >= 1
e-12) TT = 26;
530 if ((
mu > 0) and (not fix_interval)) {
532 slo = -0.5*log(4.0*TT/(
mu*
mu));
535 slo = log(eps/hi) - 1.0;
537 shi = 0.5*log(TT/(
lo*
lo));
538 if (shi <= slo)
throw "bsh_fit: logic error in slo,shi";
541 double h = 1.0/(0.2-.50*log10(eps));
548 h = floor(64.0*
h)/64.0;
553 slo = floor(slo/
h)*
h;
555 long npt = long((shi-slo)/
h+0.5);
562 for (
int i=0; i<npt; ++i) {
563 double s = slo +
h*(npt-i);
566 expnt[i] = exp(2.0*s);
573 expnt[0] = exp(2.0*s);
574 coeff=coeff(
Slice(0,0));
575 expnt=expnt(
Slice(0,0));
576 print(
"only one term in gfit",s,coeff[0],expnt[0]);
589 if ((
mu == 0.0) and (not fix_interval)) {
614 double range = sqrt(-log(1
e-6)/expnt[nmom-1]);
615 if (prnt)
print(
"exponent(nmom-1)",expnt[nmom-1],
"has range", range);
619 for (
int i=nmom; i<npt; ++i) {
626 qg = qg(
Slice(1,nmom));
630 print(
"moments", qg);
633 for (
int j=0; j<nmom; ++j) {
636 if (nmom != 4) qt = qt(
Slice(1,nmom));
637 for (
int i=0; i<nmom; ++i) {
646 print(
"new coeffs", ncoeff);
649 coeff(
Slice(0,nmom-1)) = ncoeff;
668 if (eps >= 1
e-2) TT = 5;
669 else if (eps >= 1
e-4) TT = 10;
670 else if (eps >= 1
e-6) TT = 14;
671 else if (eps >= 1
e-8) TT = 18;
672 else if (eps >= 1
e-10) TT = 22;
673 else if (eps >= 1
e-12) TT = 26;
678 double slo=0.5 * log(eps) - 1.0;
679 double shi=log(TT/(
lo*
lo))*0.5;
682 double h = 1.0/(0.2-.5*log10(eps));
689 h = floor(64.0*
h)/64.0;
693 slo = floor(slo/
h)*
h;
695 long npt = long((shi-slo)/
h+0.5);
699 for (
int i=0; i<npt; ++i) {
700 const double s = slo +
h*(npt-i);
701 coeff[i] =
h*exp(-gamma*gamma*exp(2.0*s) + s);
703 expnt[i] = 0.25*exp(-2.0*s);
707 std::cout <<
"weights and roots for a Slater function with gamma=" << gamma << std::endl;
708 for (
int i=0; i<npt; ++i)
709 std::cout << i <<
" " << coeff[i] <<
" " << expnt[i] << std::endl;
714 std::cout <<
" x value abserr relerr" << std::endl;
715 std::cout <<
" ------------ ------- -------- -------- " << std::endl;
716 double step = exp(log(hi/
lo)/(npt+1));
717 for (
int i=0; i<=npt; ++i) {
718 double r =
lo*(
pow(step,i+0.5));
719 double exact = exp(-gamma*r);
721 for (
int j=0; j<coeff.dim(0); ++j)
722 test += coeff[j]*exp(-r*r*expnt[j]);
736 static void f12_fit(
double gamma,
double lo,
double hi,
double eps,
742 pcoeff(
Slice(1,-1,1))=-coeff(
_);
744 pexpnt(
Slice(1,-1,1))=expnt(
_);
755 static void f12sq_fit(
double gamma,
double lo,
double hi,
double eps,
759 slater_fit(2.0*gamma,
lo, hi, eps, coeff2, expnt2, prnt);
765 auto coeff=coeff2-2.0*coeff1;
767 pcoeff(
Slice(1,-1,1))=coeff(
_);
769 pexpnt(
Slice(1,-1,1))=expnt1(
_);
793 slo = -0.5*log(4.0*100.0/(
mu*
mu));
794 slo = -0.5*log(4.0*(slo*ndim - 2.0*slo + 100.0)/(
mu*
mu));
797 slo = log(eps/hi) - 1.0;
799 shi = 0.5*log(100.0/(
lo*
lo));
802 double h = 1.0/(0.2-.50*log10(eps));
809 h = floor(64.0*
h)/64.0;
814 slo = floor(slo/
h)*
h;
816 long npt = long((shi-slo)/
h+0.5);
819 std::cout <<
"bsh: mu " <<
mu <<
" lo " <<
lo <<
" hi " << hi
820 <<
" eps " << eps <<
" slo " << slo <<
" shi " << shi
821 <<
" npt " << npt <<
" h " <<
h << std::endl;
827 for (
int i=0; i<npt; ++i) {
828 double s = slo +
h*(npt-i);
830 double p = exp(2.0*s);
832 if (
c*exp(-
p*
lo*
lo) > eps) {
843 expnt[0] = exp(2.0*s);
844 coeff=coeff(
Slice(0,0));
845 expnt=expnt(
Slice(0,0));
846 print(
"only one term in gfit",s,coeff[0],expnt[0]);
858 double mid =
lo + (hi-
lo)*0.5;
860 for (i=npt-1; i>0; --i) {
861 double cnew = coeff[i]*exp(-(expnt[i]-expnt[i-1])*mid*mid);
862 double errlo = coeff[i]*exp(-expnt[i]*
lo*
lo) -
863 cnew*exp(-expnt[i-1]*
lo*
lo);
864 double errhi = coeff[i]*exp(-expnt[i]*hi*hi) -
865 cnew*exp(-expnt[i-1]*hi*hi);
868 coeff[i-1] = coeff[i-1] + cnew;
873 coeff = coeff(
Slice(0,npt-1));
874 expnt = expnt(
Slice(0,npt-1));
878 for (
int i=0; i<npt; ++i)
879 std::cout << i <<
" " << coeff[i] <<
" " << expnt[i] << std::endl;
882 std::cout <<
" x value" << std::endl;
883 std::cout <<
" ------------ ---------------------" << std::endl;
884 double step = exp(log(hi/
lo)/(npt+1));
885 for (
int i=0; i<=npt; ++i) {
886 double r =
lo*(
pow(step,i+0.5));
888 for (
int j=0; j<coeff.dim(0); ++j)
889 test += coeff[j]*exp(-r*r*expnt[j]);
890 printf(
" %.6e %20.10e\n",r,
test);
923 * exp(-
mu *
R) / 0.4e1;
924 q[2] = -(-0.2e1 * exp(
mu *
R) + 0.2e1 + 0.2e1 *
mu *
R +
R*
R *
926 q[3] = -(-0.6e1 * exp(
mu *
R) + 0.6e1 + 0.6e1 *
mu *
R + 0.3e1 *
R*
R
double q(double t)
Definition DKops.h:18
long size() const
Returns the number of elements in the tensor.
Definition basetensor.h:138
Tensor< T > coeffs() const
return the coefficients of the fit
Definition gfit.h:369
void print_accuracy(opT op, const double lo, const double hi) const
print coefficients and exponents, and values and errors
Definition gfit.h:466
static GFit InverseRSqFit(double lo, double hi, double eps, bool prnt=false)
return a fit for the inverse-square function 1/r^2
Definition gfit.h:128
Tensor< T > exponents() const
return the exponents of the fit
Definition gfit.h:372
static void prune_small_coefficients(const double eps, const double lo, const double hi, Tensor< double > &coeff, Tensor< double > &expnt, const long first_prunable=1)
Definition gfit.h:378
static GFit GaussFit(double gamma, double lo, double hi, double eps, bool prnt=false)
return a (trivial) fit for a single Gauss function
Definition gfit.h:222
GFit()=default
default ctor does nothing
static GFit F2GFit(double gamma, double lo, double hi, double eps, bool prnt=false)
return a fit for the F2G function
Definition gfit.h:323
GFit(const funcT &f)
ctor taking an isotropic function
Definition gfit.h:456
static void f12_fit(double gamma, double lo, double hi, double eps, Tensor< double > &pcoeff, Tensor< double > &pexpnt, bool prnt)
fit a correlation factor (1- exp(-mu r))
Definition gfit.h:736
static void slater_fit(double gamma, double lo, double hi, double eps, Tensor< double > &pcoeff, Tensor< double > &pexpnt, bool prnt)
fit a Slater function using a sum of Gaussians
Definition gfit.h:662
static void bsh_spherical_moments(double mu, double R, Tensor< double > &q)
Definition gfit.h:913
static GFit BSHFit(double mu, double lo, double hi, double eps, bool prnt=false)
return a fit for the bound-state Helmholtz function
Definition gfit.h:179
static GFit GeneralFit()
return a fit for a general isotropic function
Definition gfit.h:363
Tensor< T > exponents_
the exponents of the expansion f(x) = \sum_m coeffs[m] exp(-exponents[m] * x^2)
Definition gfit.h:493
static GFit FGFit(double gamma, double lo, double hi, double eps, bool prnt=false)
return a fit for the FG function
Definition gfit.h:289
static void f12sq_fit(double gamma, double lo, double hi, double eps, Tensor< double > &pcoeff, Tensor< double > &pexpnt, bool prnt)
fit a correlation factor f12^2 = (1- exp(-mu r))^2 = 1 - 2 exp(-mu r) + exp(-2 mu r)
Definition gfit.h:755
static GFit F12sqFit(double gamma, double lo, double hi, double eps, bool prnt=false)
return a fit for the F12^2 correlation factor
Definition gfit.h:266
static void bsh_fit(double mu, double lo, double hi, double eps, Tensor< double > &pcoeff, Tensor< double > &pexpnt, bool prnt, bool fix_interval)
fit the function exp(-mu r)/r
Definition gfit.h:504
static void gaussian_spherical_moments(double alpha, double R, Tensor< double > &q)
Definition gfit.h:899
static GFit F12Fit(double gamma, double lo, double hi, double eps, bool prnt=false)
return a fit for the F12 correlation factor
Definition gfit.h:245
GFit & operator=(const GFit &other)
assignment operator
Definition gfit.h:96
static GFit SlaterFit(double gamma, double lo, double hi, double eps, bool prnt=false)
return a fit for the Slater function
Definition gfit.h:202
static GFit CoulombFit(double lo, double hi, double eps, bool prnt=false)
return a fit for the Coulomb function
Definition gfit.h:105
static void truncate_mixed_expansion(Tensor< double > &c, Tensor< double > &e, const std::array< LatticeRange, NDIM > &lattice_ranges, const Tensor< double > &cell_width, double lo, double hi_fin, double eps)
Truncate the fit of a kernel that is lattice-summed along some axes.
Definition gfit.h:416
static void bsh_fit_ndim(int ndim, double mu, double lo, double hi, double eps, Tensor< double > &pcoeff, Tensor< double > &pexpnt, bool prnt)
Definition gfit.h:777
GFit(const GFit &other)=default
copy constructor
GFit(OperatorInfo info)
Definition gfit.h:66
Tensor< T > coeffs_
the coefficients of the expansion f(x) = \sum_m coeffs[m] exp(-exponents[m] * x^2)
Definition gfit.h:490
A slice defines a sub-range or patch of a dimension.
Definition slice.h:103
A tensor is a multidimensional array.
Definition tensor.h:313
Defines common mathematical and physical constants.
static const double R
Definition csqrt.cc:46
void test(World &world, bool doloadbal=false)
Definition dataloadbal.cc:224
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition derivatives.cc:72
static double lo
Definition dirac-hatom.cc:23
Tensor< double > op(const Tensor< double > &x)
Definition kain.cc:508
static double pow(const double *a, const double *b)
Definition lda.h:74
Defines madness::MadnessException for exception handling.
#define MADNESS_CHECK(condition)
Check a condition — even in a release build the condition is always evaluated so it can have side eff...
Definition madness_exception.h:182
#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
constexpr double pi
Mathematical constant .
Definition constants.h:48
Runtime details on the configuration and version of MADNESS.
Definition madness.h:69
Namespace for all elements and tools of MADNESS.
Definition DFConvergence.h:9
static const Slice _(0,-1, 1)
OpType
operator types
Definition operatorinfo.h:11
@ OT_FG12
1-exp(-r)
Definition operatorinfo.h:18
@ OT_SLATER
1/r
Definition operatorinfo.h:15
@ OT_GAUSS
exp(-r)
Definition operatorinfo.h:16
@ OT_BSH
(1-exp(-r))^2/r = 1/r + exp(-2r)/r - 2 exp(-r)/r
Definition operatorinfo.h:21
@ OT_INVRSQ
exp(-r)/r
Definition operatorinfo.h:22
@ OT_F12
exp(-r2)
Definition operatorinfo.h:17
@ OT_F212
(1-exp(-r))/r
Definition operatorinfo.h:19
@ OT_G12
indicates the identity
Definition operatorinfo.h:14
@ OT_F2G12
(1-exp(-r))^2
Definition operatorinfo.h:20
void gesv(const Tensor< T > &a, const Tensor< T > &b, Tensor< T > &x)
Solve Ax = b for general A using the LAPACK *gesv routines.
Definition lapack.cc:804
Tensor< double > FunctionDefaults< NDIM >::cell_width
Definition mraimpl.h:3893
bool FunctionDefaults< NDIM >::debug
Definition mraimpl.h:3886
NDIM & f
Definition mra.h:2817
void print(const T &t, Ts &&... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition print.h:232
static long abs(long a)
Definition tensor.h:214
const double mu
Definition navstokes_cosines.cc:95
static const double b
Definition nonlinschro.cc:119
static const double d
Definition nonlinschro.cc:121
Defines simple templates for printing to std::cout "a la Python".
static const double c
Definition relops.cc:10
Declares and implements Slice.
Definition operatorinfo.h:61
Defines and implements most of Tensor.
Prototypes for a partial interface from Tensor to LAPACK.
void e()
Definition test_sig.cc:75
static const double alpha
Definition testcosine.cc:10
double(* exact)(double, double, double)
Definition testfuns.cc:6
std::vector< double > fit(size_t m, size_t n, const std::vector< double > N, const std::vector< double > &f)
Definition testfuns.cc:36
constexpr std::size_t NDIM
Definition testgconv.cc:54
double h(const coord_1d &r)
Definition testgconv.cc:175