/* inline */ void XC(fast_fzeta)(const FLOAT x, const int nspin, const int order, FLOAT * fz){ FLOAT aa, bb, aa2, bb2; if(nspin != XC_UNPOLARIZED){ aa = CBRT(1.0 + x); bb = CBRT(1.0 - x); aa2 = aa*aa; bb2 = bb*bb; fz[0] = (aa2*aa2 + bb2*bb2 - 2.0)/FZETAFACTOR; if(order < 1) return; fz[1] = (aa - bb)*(4.0/3.0)/FZETAFACTOR; if(order < 2) return; fz[2] = ((4.0/9.0)/FZETAFACTOR)*(ABS(x)==1.0 ? (FLT_MAX) : (pow(1.0 + (x), -2.0/3.0) + pow(1.0 - (x), -2.0/3.0))); if(order < 3) return; fz[3] = (-(8.0/27.0)/FZETAFACTOR)*(ABS(x)==1.0 ? (FLT_MAX) : (pow(1.0 + (x), -5.0/3.0) - pow(1.0 - (x), -5.0/3.0))); } else { fz[0] = 0.0; fz[1] = 0.0; fz[2] = (8.0/9.0)/FZETAFACTOR; fz[3] = 0.0; } }
/* initialization */ static void init_vwn_constants(vwn_consts_type *X) { int i; X->A[2] = -1.0/(6.0*M_PI*M_PI); for(i=0; i<3; i++){ X->Q[i] = SQRT(4.0*X->c[i] - X->b[i]*X->b[i]); } X->fpp = 4.0/(9.0*(CBRT(2.0) - 1)); }
inline TPoint2<TYPE> DistortPointR1(const TPoint2<TYPE>& pt, const REAL& k1) { if (k1 == 0) return pt; const REAL y(pt.y == 0 ? REAL(1.e-12) : REAL(pt.y)); const REAL t2(y*y); const REAL t3(t2*t2*t2); const REAL t4(pt.x*pt.x); const REAL t7(k1*(t2+t4)); const REAL t9(1.0/t7); const REAL t10(t2*t9*y*0.5); const REAL t11(t3*t9*t9*(0.25+t9/27.0)); #ifndef _RELEASE TPoint2<TYPE> upt; #endif if (k1 > 0) { const REAL t17(CBRT(t10+SQRT(t11))); const REAL t18(t17-t2*t9/(t17*3)); #ifndef _RELEASE upt = #else return #endif TPoint2<TYPE>(TYPE(t18*pt.x/y), TYPE(t18)); } else {
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include "util.h" #define XC_GGA_X_AM05 120 /* Armiento & Mattsson 05 exchange */ static inline void func(const XC(func_type) *p, int order, FLOAT x, FLOAT *f, FLOAT *dfdx, FLOAT *d2fdx2) { const FLOAT am05_c = 0.7168; const FLOAT am05_alpha = 2.804; const FLOAT z_tt_factor = POW(CBRT(4.0/3.0) * 2.0*M_PI/3.0, 4); FLOAT ss, ss2, lam_x, dlam_x, d2lam_x; FLOAT ww, z_t, z_t2, z_tt, fx_b, xx, flaa_1, flaa_2, flaa; FLOAT dww, dz_t, dz_tt, dfx_b, dxx, dflaa_1, dflaa_2, dflaa; FLOAT d2ww, d2z_t, d2z_tt, d2fx_b, d2xx, d2flaa_1, d2flaa_2, d2flaa;; if(x < p->info->min_grad){ *f = 1.0; return; } ss = X2S*x; ss2 = ss*ss; lam_x = POW(ss, 1.5)/(2.0*SQRT(6.0));
#include <stdio.h> #include "util.h" /************************************************************************ Wigner's parametrization from the low density limit ************************************************************************/ #define XC_LDA_C_GOMBAS 24 /* Gombas parametrization */ static inline void func(const XC(func_type) *p, XC(lda_work_t) *r) { static FLOAT a1=-0.0357, a2=0.0562, b1=-0.0311, b2=2.39; FLOAT t1, t2, cnst_rs, x; cnst_rs = CBRT(4.0*M_PI/3.0); x = cnst_rs*r->rs[1]; t1 = 1.0 + a2*x; t2 = x + b2; r->zk = a1/t1 + b1*LOG(t2/x); if(r->order < 1) return; r->dedrs = -a1*a2/(t1*t1) - b1*b2/(x*t2); r->dedrs*= cnst_rs; r->dedz = 0.0; if(r->order < 2) return;
int cubic(double A[4], double X[3], int* L) { const double PI = 3.1415926535897932; const double THIRD = 1./3.; double U[3],W, P, Q, DIS, PHI; int i; //define cubic root as statement function // In C, the function is defined outside of the cubic fct // ====determine the degree of the polynomial ==== if (A[3] != 0.0) { //cubic problem W = A[2]/A[3]*THIRD; P = pow((A[1]/A[3]*THIRD - (W*W)), 3); Q = -.5*(2.0*(W*W*W)-(A[1]*W-A[0])/A[3] ); DIS = (Q*Q)+P; if ( DIS < 0.0 ) { //three real solutions! //Confine the argument of ACOS to the interval [-1;1]! PHI = acos(min(1.0,max(-1.0,Q/sqrt(-P)))); P=2.0*pow((-P),(5.e-1*THIRD)); for (i=0;i<3;i++) U[i] = P*cos((PHI+2*((double)i)*PI)*THIRD)-W; X[0] = min(U[0], min(U[1], U[2])); X[1] = max(min(U[0], U[1]),max( min(U[0], U[2]), min(U[1], U[2]))); X[2] = max(U[0], max(U[1], U[2])); *L = 3; } else { // only one real solution! DIS = sqrt(DIS); X[0] = CBRT(Q+DIS)+CBRT(Q-DIS)-W; *L=1; } } else if (A[2] != 0.0) { // quadratic problem P = 0.5*A[1]/A[2]; DIS = (P*P)-A[0]/A[2]; if (DIS > 0.0) { // 2 real solutions const double sq_dis = sqrt(DIS); X[0] = -P - sq_dis; X[1] = -P + sq_dis; *L=2; } else { // no real solution *L=0; } } else if (A[1] != 0.0) { //linear equation X[0] =A[0]/A[1]; *L=1; } else { //no equation *L=0; } // // ==== perform one step of a newton iteration in order to minimize // round-off errors ==== // for (i=0;i<*L;i++) { X[i] = X[i] - (A[0]+X[i]*(A[1]+X[i]*(A[2]+X[i]*A[3])))/(A[1]+X[i]*(2.0*A[2]+X[i]*3.0*A[3])); // printf("\n X inside cubic %.15e\n", X[i]); } return 0; }
static FLOAT pw91_nu, pw91_beta; static const FLOAT pw91_C_c0 = 4.235e-3, pw91_alpha = 0.09; static void gga_c_pw91_init(XC(func_type) *p) { p->n_func_aux = 1; p->func_aux = (XC(func_type) **) malloc(1*sizeof(XC(func_type) *)); p->func_aux[0] = (XC(func_type) *) malloc( sizeof(XC(func_type))); XC(func_init)(p->func_aux[0], XC_LDA_C_PW, p->nspin); pw91_nu = 16.0/M_PI * CBRT(3.0*M_PI*M_PI); pw91_beta = pw91_nu*pw91_C_c0; } static void A_eq14(int order, FLOAT ec, FLOAT g, FLOAT *A, FLOAT *dAec, FLOAT *dAg, FLOAT *d2Aec2, FLOAT *d2Ag2, FLOAT *d2Aecg) { FLOAT xx, expxx, g2, g3; FLOAT dAdxx, dxxdec, dxxdg; FLOAT d2Adxx2, d2xxdecg, d2xxdg2; g2 = g*g; g3 = g*g2;
unsigned int Math::solveCubic(DOUBLE coeffs[4], DOUBLE roots[3]) { unsigned int nb = 0; /* x^3 + A * x^2 + B * x + C = 0 */ DOUBLE A = coeffs[2] / coeffs[3]; DOUBLE B = coeffs[1] / coeffs[3]; DOUBLE C = coeffs[0] / coeffs[3]; /* transform to x^3 + p * x + q = 0 */ DOUBLE sqrd_A = A * A; DOUBLE p = 1.0 / 3.0 * (-1.0 / 3.0 * sqrd_A + B); DOUBLE q = 1.0 / 2.0 * (2.0 / 27.0 * A * sqrd_A - 1.0 / 3.0 * A * B + C); /* use Cardano's formula */ DOUBLE cardano_p = p * p * p; DOUBLE delta = q * q + cardano_p; if (IS_ZERO(delta)) { if (IS_ZERO(q)) { roots[0] = 0; nb = 1; } else { DOUBLE u = CBRT(-q); roots[0] = 2 * u; roots[1] = -u; nb = 2; } } else if (delta < 0.0) { DOUBLE phi = 1.0 / 3.0 * acos(-q / sqrt(-cardano_p)); DOUBLE t = 2.0 * sqrt(-p); roots[0] = t * cos(phi); roots[1] = -t * cos(phi + M_PI / 3); roots[2] = -t * cos(phi - M_PI / 3); nb = 3; } else { DOUBLE sqrt_delta = sqrt(delta); roots[0] = cbrt(sqrt_delta - q) - cbrt(sqrt_delta + q); nb = 1; } /* retransform to original equation */ for (unsigned int i = 0; i < nb; i++) roots[i] -= 1.0 / 3.0 * A; return (nb); }
#include "util.h" #define XC_GGA_X_KT1 145 /* Keal and Tozer version 1 */ #define XC_GGA_XC_KT2 146 /* Keal and Tozer version 2 */ #define HEADER 3 static inline void func(const XC(gga_type) *p, int order, FLOAT x, FLOAT ds, FLOAT *f, FLOAT *dfdx, FLOAT *lvrho) { FLOAT gamma = -0.006, delta = 0.1; FLOAT dd, n13, n43; n13 = CBRT(ds); n43 = ds*n13; dd = 1.0/(n43 + delta); *f = 1.0 - gamma/X_FACTOR_C * x*x * n43*dd; if(order < 1) return; *dfdx = - gamma/X_FACTOR_C * 2.0*x * n43*dd; *lvrho = - gamma/X_FACTOR_C * x*x * (4.0/3.0)*n13 * delta * dd*dd; if(order < 2) return; /* to be done */ }