static double s_integrand(double y) { double r; double pp=6; double s,pcmIn; s=sMin+pow(y,pp)*(sMax-sMin); pcmIn=decayPcm(sqrt(s),pmass[0], pmass[1]); if(pcmIn==0) return 0; pvect[0]=sqrt(pmass[0]*pmass[0]+pcmIn*pcmIn); pvect[1]=pcmIn; pvect[2]=0; pvect[3]=0; pvect[4]=sqrt(pmass[1]*pmass[1]+pcmIn*pcmIn); pvect[5]=-pcmIn; pvect[6]=0; pvect[7]=0; pcmOut=decayPcm(sqrt(s),pmass[2], pmass[3]); pvect[8]=sqrt(pmass[2]*pmass[2]+pcmOut*pcmOut); pvect[11]=0; pvect[12]=sqrt(pmass[3]*pmass[3]+pcmOut*pcmOut); pvect[15]=0; x0=s/sMax; r= 3.8937966E8*pcmOut/(32*M_PI*pcmIn*s)*simpson(cos_integrand,-1.,1.,1.E-3); r*=pp*pow(y,pp-1)*(sMax-sMin); return r; }
int main(int argc, char *argv[]) { if (argc != 5) { usage(argv[0], "wrong number of parameters"); } const double A = atof(argv[1]); const double B = atof(argv[2]); const int N = atoi(argv[3]); if (A >= B) { usage(argv[0], "B must be > A"); } if (N <= 0) { usage(argv[0], "N must be > 0"); } double result = simpson(A, B, N, &a_function); //double result = simpson(A, B, N, &another_function); printf("pid=%8d ID=%-8s A=%8.6lf B=%8.6lf N=%8d -> result=%8.6f\n", getpid(), argv[4], A, B, N, result); exit(0); }
void posiFluxTab(double Emin, double sigmav, double *tab, double *tabOut) { double flu,rho0; int i; double buff[NZ]; tab_ =tab; rho0=rhoDM/hProfile_(Rsun)/Mcdm; flu = Tau_dif*sigmav/(4*M_PI)*CELERITY_LIGHT; buildInterpolation(integral_cal_In,0., dKt(Mcdm,Emin),-Eps,&N_,&xa_,&ya_); //printf("N_=%d\n",N_); for(i=0;i<NZ;i++) { Eobs=Mcdm*exp(Zi(i)); if(Eobs<Emin*0.9) buff[i]=0; else buff[i]= (flu/Eobs)* simpson(SpectIntegrand, Eobs, Mcdm, Eps); } for(i=0;i<NZ;i++) tabOut[i]=rho0*rho0*buff[i]; free(xa_); free(ya_); }
void s() { clock_t start, finish; double xmin, xmax, value, time_taken; int intervals; get_conditionsS(xmin, xmax, intervals); start = clock(); // Do the calculation 50000 times so that the time taken is a few // seconds, rather than a few milliseconds. for (unsigned long i = 0 ; i < 50000; i++) { value = simpson (xmin, xmax, intervals); } finish = clock(); time_taken = (finish - start) / double(CLOCKS_PER_SEC); cout << setprecision(6) << "Successful integration, value = " << value << endl << "Time for 500 Simpson's rule calculations (" << intervals << " intervals) was " << time_taken << " seconds" << endl; }
static double sigmaEarth(double pA0, double nA0, double pA5, double nA5, double R3) { double si,cI,sumI,mu; /* O Na Mg Al Si P S Ca Cr, Fe Ni Cu */ double A[12] ={16, 23 ,24, 27, 28, 30, 32, 40, 52, 56, 58, 64}; double Z[12] ={ 8, 11 ,12, 13, 14, 15, 16, 20, 24, 26, 28, 29}; double*earthFractions[11]={OEarth,NaEarth,MgEarth,AlEarth,SiEarth,PEarth,SEarth,CaEarth,CrEarth,FeEarth,NiEarth }; int i; readEarthData(); for(sumI=0,i=0;i<11;i++) { MA=A[i]*mp_gev; mu=Mcdm*MA/(Mcdm+MA); si= (Z[i]*pA0+(A[i]-Z[i])*nA0)*mu; si=4/M_PI*si*si*3.8937966E8*1E-36; // cm^2 aFraction=earthFractions[i]; cI=si/A[i]*(4./3*M_PI)*simpson(sIntegrand,0,R3,1.E-3); sumI+=cI; } return sumI; }
long double asr(long double a, long double b, long double eps) { return asr(a, b, eps, simpson(a, b)); }
DB asr(DB l, DB r, DB eps, DB res) { DB m = (l+r)/2.0; DB ls = simpson(l, m), rs = simpson(m, r); if (fabs(ls+rs-res) < eps*15) return ls+rs+(ls+rs-res)/15; return asr(l, m, eps/2.0, ls)+asr(m, r, eps/2.0, rs); }
double asr(double a,double b,double eps,double A){ double c = a + (b-a)/2; double L = simpson(a,c), R = simpson(c,b); if(fabs(L+R-A) <= 15*eps) return (L+R-A)/15.0; return asr(a,c,eps/2,L) + asr(c,b,eps/2,R); }
PeakIntegrator::PeakArea PeakIntegrator::integratePeak_(const PeakContainerT& pc, double left, double right) const { PeakContainerT emg_pc; const PeakContainerT& p = EMGPreProcess_(pc, emg_pc, left, right); std::function<double(const double, const double)> compute_peak_area_trapezoid = [&p](const double left, const double right) { double peak_area { 0.0 }; for (typename PeakContainerT::ConstIterator it = p.PosBegin(left); it != p.PosEnd(right) - 1; ++it) { peak_area += ((it + 1)->getPos() - it->getPos()) * ((it->getIntensity() + (it + 1)->getIntensity()) / 2.0); } return peak_area; }; std::function<double(const double, const double)> compute_peak_area_intensity_sum = [&p](const double left, const double right) { // LOG_WARN << "WARNING: intensity_sum method is being used." << std::endl; double peak_area { 0.0 }; for (typename PeakContainerT::ConstIterator it = p.PosBegin(left); it != p.PosEnd(right); ++it) { peak_area += it->getIntensity(); } return peak_area; }; double peak_area(0.0), peak_height(0.0), peak_apex_pos(0.0); ConvexHull2D::PointArrayType hull_points; UInt n_points = std::distance(p.PosBegin(left), p.PosEnd(right)); for (auto it = p.PosBegin(left); it != p.PosEnd(right); ++it) { hull_points.push_back(DPosition<2>(it->getPos(), it->getIntensity())); if (peak_height < it->getIntensity()) { peak_height = it->getIntensity(); peak_apex_pos = it->getPos(); } } if (integration_type_ == INTEGRATION_TYPE_TRAPEZOID) { if (n_points >= 2) { peak_area = compute_peak_area_trapezoid(left, right); } } else if (integration_type_ == INTEGRATION_TYPE_SIMPSON) { if (n_points == 2) { LOG_WARN << std::endl << "PeakIntegrator::integratePeak:" "number of points is 2, falling back to `trapezoid`." << std::endl; peak_area = compute_peak_area_trapezoid(left, right); } else if (n_points > 2) { if (n_points % 2) { peak_area = simpson(p.PosBegin(left), p.PosEnd(right)); } else { double areas[4] = {-1.0, -1.0, -1.0, -1.0}; areas[0] = simpson(p.PosBegin(left), p.PosEnd(right) - 1); // without last point areas[1] = simpson(p.PosBegin(left) + 1, p.PosEnd(right)); // without first point if (p.begin() <= p.PosBegin(left) - 1) { areas[2] = simpson(p.PosBegin(left) - 1, p.PosEnd(right)); // with one more point on the left } if (p.PosEnd(right) < p.end()) { areas[3] = simpson(p.PosBegin(left), p.PosEnd(right) + 1); // with one more point on the right } UInt valids = 0; for (auto area : areas) { if (area != -1.0) { peak_area += area; ++valids; } } peak_area /= valids; } } } else if (integration_type_ == INTEGRATION_TYPE_INTENSITYSUM) { peak_area = compute_peak_area_intensity_sum(left, right); } else { throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Please set a valid value for the parameter \"integration_type\"."); } PeakArea pa; pa.area = peak_area; pa.height = peak_height; pa.apex_pos = peak_apex_pos; pa.hull_points = hull_points; return pa; }
DB asr(DB l, DB r, DB eps) { return asr(l, r, eps, simpson(l, r)); }
/* Main function */ int main(int argc, char* argv[]) { double j = 0.0; integral fun = &f1; /* Print the integrals of the first function using all of the available * methods including trapezoidal, rectangle, Simpson and Gauss */ printf("\nIntegrals of x^2 on [0,1]:\n\n"); j = trapezoid(10, 0, 1, fun); printf("Trapezoidal \t= %G\n", j); j = rect(10, 0, 1, fun); printf("Rectangle \t= %G\n", j); j = simpson(10, 0, 1, fun); printf("Simpson \t= %G\n", j); j = gauss(10, 0, 1, fun); printf("Gauss \t\t= %G\n", j); fun = &f2; /* Print the integrals of the second function using all of the available * methods including trapezoidal, rectangle, Simpson and Gauss */ printf("\nIntegrals of x^3 on [0,1]:\n\n"); j = trapezoid(10, 0, 1, fun); printf("Trapezoidal \t= %G\n", j); j = rect(10, 0, 1, fun); printf("Rectangle \t= %G\n", j); j = simpson(10, 0, 1, fun); printf("Simpson \t= %G\n", j); j = gauss(10, 0, 1, fun); printf("Gauss \t\t= %G\n", j); fun = &f3; /* Print the integrals of the third function using all of the available * methods including trapezoidal, rectangle, Simpson and Gauss */ printf("\nIntegrals of x^5 on [0,1]:\n\n"); j = trapezoid(10, 0, 1, fun); printf("Trapezoidal \t= %G\n", j); j = rect(10, 0, 1, fun); printf("Rectangle \t= %G\n", j); j = simpson(10, 0, 1, fun); printf("Simpson \t= %G\n", j); j = gauss(10, 0, 1, fun); printf("Gauss \t\t= %G\n", j); fun = &f4; /* Print the integrals of the fourth function using all of the available * methods including trapezoidal, rectangle, Simpson and Gauss */ printf("\nIntegrals of e^-x on [0,1]:\n\n"); j = trapezoid(10, 0, 1, fun); printf("Trapezoidal \t= %G\n", j); j = rect(10, 0, 1, fun); printf("Rectangle \t= %G\n", j); j = simpson(10, 0, 1, fun); printf("Simpson \t= %G\n", j); j = gauss(10, 0, 1, fun); printf("Gauss \t\t= %G\n", j); fun = &f5; /* Print the integrals of the fifth function using all of the available * methods including trapezoidal, rectangle, Simpson and Gauss */ printf("\nIntegrals of xe^-x on [0,2]:\n\n"); j = trapezoid(10, 0, 2, fun); printf("Trapezoidal \t= %G\n", j); j = rect(10, 0, 2, fun); printf("Rectangle \t= %G\n", j); j = simpson(10, 0, 2, fun); printf("Simpson \t= %G\n", j); j = gauss(10, 0, 2, fun); printf("Gauss \t\t= %G\n", j); fun = &f6; /* Print the integrals of the sixth function using all of the available * methods including trapezoidal, rectangle, Simpson and Gauss */ printf("\nIntegrals of x^-0.5 on [0,2]:\n\n"); j = trapezoid(10, 0, 2, fun); printf("Trapezoidal \t= %G\n", j); j = rect(10, 0, 2, fun); printf("Rectangle \t= %G\n", j); j = simpson(10, 0, 2, fun); printf("Simpson \t= %G\n", j); j = gauss(10, 0, 2, fun); printf("Gauss \t\t= %G\n", j); return 1; }
void isevect(double *t, int *delta, int *n, int *nboot, double *gridise, int *legridise, double *gridbw1, int *legridbw1, double *gridbw2, int *legridbw2, int *nkernel, int * dup, int *nestimand, double *phat, double *estim, int* presmoothing, double *isev){ int i, j, k, boot, *indices, *deltaboot, *pnull; double *pnull2, *ptemp, *estimboot, *tboot, *integrand, *isecomp, *deltabootdbl; indices = malloc(*n * sizeof(int)); ptemp = malloc(*n * sizeof(double)); estimboot = malloc(*legridise * sizeof(double)); tboot = malloc(*n * sizeof(double)); integrand = malloc(*legridise * sizeof(double)); isecomp = malloc(sizeof(double)); GetRNGstate(); if(*presmoothing == 1){ // with presmoothing deltaboot = malloc(*n * sizeof(int)); switch(*nestimand){ // S case 1: pnull = calloc(1, sizeof(int)); pnull2 = calloc(1, sizeof(double)); for (boot = 0; boot < *nboot; boot++){ R_FlushConsole(); R_ProcessEvents(); for (i = 0; i < *n; i++) indices[i] = (int)ftrunc(runif(0, 1) * (*n)); R_isort(indices, *n); for (i = 0; i < *n; i++){ tboot[i] = t[indices[i]]; deltaboot[i] = (int)rbinom(1, phat[indices[i]]); } for (i = 0; i < *legridbw1; i++){ nadarayawatson(tboot, n, tboot, deltaboot, n, &(gridbw1[i]), nkernel, ptemp); presmestim(gridise, legridise, tboot, n, pnull2, pnull, pnull, ptemp, pnull, nestimand, estimboot); for (j = 0; j < *legridise; j++) integrand[j] = (estimboot[j] - estim[j])*(estimboot[j] - estim[j]); simpson(integrand, legridise, isecomp); isev[i] += *isecomp; } } free(pnull); free(pnull2); break; // H case 2: pnull = calloc(1, sizeof(int)); for (boot = 0; boot < *nboot; boot++){ R_FlushConsole(); R_ProcessEvents(); for (i = 0; i < *n; i++) indices[i] = (int)ftrunc(runif(0, 1) * (*n)); R_isort(indices, *n); for (i = 0; i < *n; i++){ tboot[i] = t[indices[i]]; deltaboot[i] = (int)rbinom(1, phat[indices[i]]); } for (i = 0; i < *legridbw1; i++){ nadarayawatson(tboot, n, tboot, deltaboot, n, &(gridbw1[i]), nkernel, ptemp); presmestim(gridise, legridise, tboot, n, gridbw2, nkernel, pnull, ptemp, dup, nestimand, estimboot); for (j = 0; j < *legridise; j++) integrand[j] = (estimboot[j] - estim[j])*(estimboot[j] - estim[j]); simpson(integrand, legridise, isecomp); isev[i] += *isecomp; } } free(pnull); break; // f case 3: for (boot = 0; boot < *nboot; boot++){ R_FlushConsole(); R_ProcessEvents(); for (i = 0; i < *n; i++) indices[i] = (int)ftrunc(runif(0, 1) * (*n)); R_isort(indices, *n); for (i = 0; i < *n; i++){ tboot[i] = t[indices[i]]; deltaboot[i] = (int)rbinom(1, phat[indices[i]]); } for (j = 0; j < *legridbw2; j++) for (i = 0; i < *legridbw1; i++){ nadarayawatson(tboot, n, tboot, deltaboot, n, &(gridbw1[i]), nkernel, ptemp); presmdensfast(gridise, legridise, tboot, n, &(gridbw2[j]), nkernel, ptemp, estimboot); for (k = 0; k < *legridise; k++) integrand[k] = (estimboot[k] - estim[k])*(estimboot[k] - estim[k]); simpson(integrand, legridise, isecomp); isev[j * (*legridbw1) + i] += *isecomp; } } break; // h case 4: for (boot = 0; boot < *nboot; boot++){ R_FlushConsole(); R_ProcessEvents(); for (i = 0; i < *n; i++) indices[i] = (int)ftrunc(runif(0, 1) * (*n)); R_isort(indices, *n); for (i = 0; i < *n; i++){ tboot[i] = t[indices[i]]; deltaboot[i] = (int)rbinom(1, phat[indices[i]]); } for (j = 0; j < *legridbw2; j++) for (i = 0; i < *legridbw1; i++){ nadarayawatson(tboot, n, tboot, deltaboot, n, &(gridbw1[i]), nkernel, ptemp); presmtwfast(gridise, legridise, tboot, n, &(gridbw2[j]), nkernel, dup, ptemp, estimboot); for (k = 0; k < *legridise; k++) integrand[k] = (estimboot[k] - estim[k])*(estimboot[k] - estim[k]); simpson(integrand, legridise, isecomp); isev[j * (*legridbw1) + i] += *isecomp; } } break; default: break; } free(deltaboot); } else{ // without presmoothing deltabootdbl = malloc(*n * sizeof(double)); if(*nestimand == 3){ // f for (boot = 0; boot < *nboot; boot++){ R_FlushConsole(); R_ProcessEvents(); for (i = 0; i < *n; i++) indices[i] = (int)ftrunc(runif(0, 1) * (*n)); R_isort(indices, *n); for (i = 0; i < *n; i++){ tboot[i] = t[indices[i]]; deltabootdbl[i] = (double)delta[indices[i]]; } for (i = 0; i < *legridbw2; i++){ presmdensfast(gridise, legridise, tboot, n, &(gridbw2[i]), nkernel, deltabootdbl, estimboot); for (j = 0; j < *legridise; j++) integrand[j] = (estimboot[j] - estim[j])*(estimboot[j] - estim[j]); simpson(integrand, legridise, isecomp); isev[i] += *isecomp; } } } else{ // h for (boot = 0; boot < *nboot; boot++){ R_FlushConsole(); R_ProcessEvents(); for (i = 0; i < *n; i++) indices[i] = (int)ftrunc(runif(0, 1) * (*n)); R_isort(indices, *n); for (i = 0; i < *n; i++){ tboot[i] = t[indices[i]]; deltabootdbl[i] = (double)delta[indices[i]]; } for (i = 0; i < *legridbw2; i++){ presmtwfast(gridise, legridise, tboot, n, &(gridbw2[i]), nkernel, dup, deltabootdbl, estimboot); for (j = 0; j < *legridise; j++) integrand[j] = (estimboot[j] - estim[j])*(estimboot[j] - estim[j]); simpson(integrand, legridise, isecomp); isev[i] += *isecomp; } } } free(deltabootdbl); } PutRNGstate(); free(indices); free(ptemp); free(estimboot); free(tboot); free(integrand); free(isecomp); }
long double asr(long double a, long double b, long double eps, long double A) { long double c = a + (b - a) / 2; long double L = simpson(a, c), R = simpson(c, b); if (fabs(L + R - A) < 15 * eps) return L + R + (L + R - A) / 15.; return asr(a, c, eps / 2, L) + asr(c, b, eps / 2, R); }
int main(int argc, char** argv) { int my_rank; /* My process rank */ int p; /* The number of processes */ float a = 0.0; /* Left endpoint */ float b = 2.0; /* Right endpoint */ int n ; /* Number of trapezoids */ float h; /* Trapezoid base length */ float local_a; /* Left endpoint my process */ float local_b; /* Right endpoint my process */ int local_n; /* Number of trapezoids for */ /* my calculation */ float integral; /* Integral over my interval */ float total; /* Total integral */ int source; /* Process sending integral */ int dest = 0; /* All messages go to 0 */ int tag = 0; MPI_Status status; /* Let the system do what it needs to start up MPI */ MPI_Init(&argc, &argv); /* Get my process rank */ MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); /* Find out how many processes are being used */ MPI_Comm_size(MPI_COMM_WORLD, &p); n = atoi(argv[1]); h = (b-a)/n; /* h is the same for all processes */ local_n = n/p; /* So is the number of trapezoids */ local_a = a + my_rank*local_n*h; local_b = local_a + local_n*h; integral = simpson(local_a, local_b, local_n, h); /* Add up the integrals calculated by each process */ if (my_rank == 0) { total = integral; for (source = 1; source < p; source++) { MPI_Recv(&integral, 1, MPI_FLOAT, source, tag, MPI_COMM_WORLD, &status); total = total + integral; } } else { MPI_Send(&integral, 1, MPI_FLOAT, dest, tag, MPI_COMM_WORLD); } /* Print the result */ if (my_rank == 0) { printf("With n = %d trapezoids, our estimate\n", n); printf("of the integral from %f to %f = %f\n", a, b, total); } /* Shut down MPI */ MPI_Finalize(); } /* main */
double Circumference(){ return 4.0*a*simpson(0,PI/2.0,e); }
double simpson(double l,double r) { double simp=F(l,r),mid=(l+r)/2; if (fabs(F(l,mid)+F(mid,r)-simp)<eps) return simp; return simpson(l,mid)+simpson(mid,r); }
double vSigmaCC(double T,numout* cc) { int i,err,n,n0,m,w; char*s, *pname[6]; int pdg[6]; double msum; double a=0,factor,dMax=0; int spin2,cdim,neutral1,neutral2; double oldQ; double bEps=1.E-4; double dI; CI=cc->interface; T_=T; if(passParameters(cc)) return -1; if(Qaddress && CI->nout==2) { oldQ=*Qaddress; for(i=0;i<2;i++) pname[i]=CI->pinf(1,i+1,pmass+i,pdg+i); *Qaddress=pmass[0]+pmass[1]; calcMainFunc(); if(passParameters(cc)) return -1; } for(i=0;i<2+CI->nout;i++) pname[i]=CI->pinf(1,i+1,pmass+i,pdg+i); M1=pmass[0]; M2=pmass[1]; for(i=2,msum=0;i<CI->nout;i++) msum+=pmass[i]; sqrtSmin=M1+M2; if(msum > sqrtSmin) { if(T==0) return 0; else sqrtSmin=msum; } sqrtSmax=sqrtSmin-T*log(bEps); n0=0; if(CI->nout>2) for(n=1;(s=CI->den_info(1,n,&m,&w));n++) { double d=sing2(s,CI->nout,CI->va[m],CI->va[w]); if(!isfinite(d)) { printf("non-integrable pole\n"); return 0;} if(d>dMax){ dMax=d; n0=n;} } switch(CI->nout) { case 2: if(T==0) a=vcs22(cc,1,&err); else { double eps=1.E-3; sqme22=CI->sqme; nsub22=1; a=simpson(u_integrand_,0.,1.,eps)*3.8937966E8; } break; case 3: { if(n0) { s=CI->den_info(1,n0,&m,&w); for(i3=2;i3<5;i3++) if(i3!=s[0]-1 && i3!=s[1]-1) break; for(i4=2;i4<4;i4++) if(i4!=i3) break; for(i5=i4+1;i5<=4;i5++) if(i5!=i3) break; } else {i3=2;i4=3;i5=4;} printf("i3,i4,i5=%d %d %d\n",i3,i4,i5); if(T==0) a=vegas_chain(3, vsigma23integrand0 ,2000,1., 0.03,&dI); else a=vegas_chain(5, vsigma23integrandT ,2000,1., 0.03,&dI); break; } case 4: if(n0) { s=CI->den_info(1,n0,&m,&w); i3=s[0]-1; i4=s[1]-1; for(i5=2;i5<5;i5++) if(i5!=i3 && i5!=i4) break; for(i6=i5+1;i6<=5;i6++) if(i6!=i3 && i6!=i4) break; }else { i3=2;i4=3;i5=4;i6=5;} printf("i3,i4,i5,i6= %d %d %d %d\n", i3,i4,i5,i6); if(T==0) a=vegas_chain(6, vsigma24integrand0 ,4000,1., 0.03,&dI); else a=vegas_chain(8, vsigma24integrandT ,5000,1., 0.03,&dI); break; default: printf("Too many outgoing particles\n"); a=0; } // WIDTH_FOR_OMEGA=0; if(Qaddress && CI->nout==2) { *Qaddress=oldQ; calcMainFunc();} return a; }
int main(){ printf("%lf\n",romberg(test,0,1)); printf("%lf\n",simpson(test,0,1,(int)1e6)); }
void uniPP_kbden(uniPP *unipp, double ***kbden, double nproj){ int i, l, l_loc, j, j_max, proj; double pi = acos(-1.); double r, u, dv; double lambda, factor; double *f = (double *)malloc(unipp->m_mesh * sizeof(double)); l_loc = unipp->l_loc; for (l=0; l<unipp->l_max; l++){ if (l!=l_loc){ if (unipp->rel && l){ j_max = 2; factor = 4.*pi; }else{ j_max = 1; factor = 4.*pi*(2.*(double)l+1.); } for (j=0; j<j_max; j++){ for (proj=0; proj<nproj; proj++){ if (proj==0){ for (i=0; i<unipp->m_mesh; i++){ r = unipp->r_m[i]; u = unipp->u_ps[l][j][i]; dv = unipp->v_ps[l][j][i] - unipp->v_loc[i]; f[i] = r*u*u*dv; } kbden[l][j][0] = 1. / (simpson(unipp->m_mesh, f, log(unipp->a_mesh)) + .5*f[0]); }else if (proj==1){ for (i=0; i<unipp->m_mesh; i++){ r = unipp->r_m[i]; f[i] *= r*r; } lambda = simpson(unipp->m_mesh, f, log(unipp->a_mesh)) + .5*f[0]; for (i=0; i<unipp->m_mesh; i++){ r = unipp->r_m[i]; f[i] *= r*r; } kbden[l][j][1] = 1. / ((simpson(unipp->m_mesh, f, log(unipp->a_mesh)) + .5*f[0]) - lambda*lambda*kbden[l][j][0]); }else{ printf("uniPP_kbden: requested projector of order" " higher than two -> exit(0)\n"); exit(0); } } for (proj=0; proj<nproj; proj++){ kbden[l][j][proj] *= factor; printf("l=%d, j=%d, proj=%d, kbden=%g\n", l, j, proj, kbden[l][j][proj]); } } } } free(f); }
static double fiIntegrand(double fi) { dfi_=fi; return sin(fi)*simpson(psiIntegrand,-M_PI/2,M_PI/2,1.E-4)/M_PI; }
int do_qeupf(param_t *param, FILE *fp_param, char *logfile){ int i,j,k,ic,ncore; char filename[180]; FILE *fp,*fp2; FILE *fp_log; int kk=2; double zeff; double ztt; double xmin; double rmax; int nn,ll; int config=-1; char lc[4]; double occ; int nnl,ng; double dij[N0]; int nind[N0]; char sgn='+'; static double rscore[NPDM]; static double nlcore[NPDM]; static double rvcore[N0][NPDM]; static double rnl[N0][NPDM]; static double rr[NPDM]; static double rab[NPDM]; static double beta[NPDM]; static double ddd[NPDM]; fp_log = fopen(logfile, "a"); fprintf(fp_log,"<<<do_upf>>>\n"); fclose(fp_log); lc[0]='S'; lc[1]='P'; lc[2]='D'; lc[3]='F'; if ((!strcmp(param->reltype, "nrl")) || (!strcmp(param->reltype, "srl"))) { nrelorbnl(param,config,logfile); nrelsproj(param,logfile); } else { relorbnl(param,config,logfile); /*relsproj(param,logfile);*/ } ncore=aorb_.norb-aorb_.nval; for (i=0;i<param->ngrid;i++) rscore[i]=0.0; ztt = pow(param->z,2./3.); xmin = log(ztt * param->a); rmax = exp(param->b*(param->ngrid-1)+xmin)/param->z; for (i=0;i<param->ngrid;i++){ rr[i]=exp(xmin+i*param->b)/param->z; rab[i]=param->b*rr[i]; } for (j=0;j<param->nll;j++) for (i=0;i<param->ngrid;i++) if (aval_.rcall[j] < rr[i]) nind[j]=i; /* compute zeff */ zeff = param->z; for (i=0; i<param->norb - param->nval; i++) zeff -= param->wnl[i]; /* read in the local potential */ sprintf(filename, "%s.loc", param->name); fp = fopen(filename, "rb"); fread(nlcore, sizeof(double), param->ngrid, fp); fclose(fp); for (i=0; i<param->nll; i++) { if (!strcmp(param->reltype, "frl")) { sgn=(adat_.so[i] > 0) ? '+' : '-' ; sprintf(filename, "%s.pot.ps.l=%d%c", param->name,aorb_.lo[i],sgn); }else{ sprintf(filename, "%s.pot.ps.l=%d", param->name,aorb_.lo[i]); } fp = fopen(filename, "rb"); fread(rvcore[i], sizeof(double), param->ngrid, fp); fseek(fp,sizeof(double) ,param->ngrid); fclose(fp); } for (i=0; i<param->nll; i++) { if (!strcmp(param->reltype, "frl")) { sgn=(adat_.so[i] > 0) ? '+' : '-' ; sprintf(filename, "%s.psi.ps.l=%d%c", param->name,aorb_.lo[i],sgn); }else{ sprintf(filename, "%s.psi.ps.l=%d", param->name,aorb_.lo[i]); } fp = fopen(filename, "rb"); fread(rnl[i], sizeof(double), param->ngrid, fp); fclose(fp); } /* read in pcc */ if (param->rpcc > 0.){ sprintf(filename, "%s.rho_pcore", param->name); fp = fopen(filename, "rb"); fread(rscore, sizeof(double), param->ngrid, fp); fclose(fp); } /* First put in the param file: */ sprintf(filename, "%s.upf", param->name); fp = fopen(filename, "w"); fprintf(fp," <PP_INFO>\n"); writeparam(param, fp, fp_param); /* !! writeparam closes but doesnt open?*/ fp = fopen(filename, "a"); fprintf(fp," </PP_INFO>\n"); /* start header */ fprintf(fp,"\n\n\n<PP_HEADER>\n"); fprintf(fp," 0 Version Number\n"); fprintf(fp," %s Element\n",param->symbol); fprintf(fp," NC Norm - Conserving pseudopotential\n"); if (param->rpcc > 1e-6){ fprintf(fp," T Nonlinear Core Correction\n"); }else{ fprintf(fp," F Nonlinear Core Correction\n"); } if (param->ixc == 0) { fprintf(fp,"SLA PZ NOGX NOGC PZ Exchange-Correlation functional\n"); } else if (param->ixc == 2) { fprintf(fp,"SLA PW PBE PBE PBE Exchange-Correlation functional\n"); } else { printf("!!WARNING!!: Your choice of XC functional is not currently supported for this type of output (UPF), will print LDA Perdew-Zunger\n"); fprintf(fp,"SLA PZ NOGX NOGC PZ Exchange-Correlation functional\n"); } fprintf(fp," %lg Z valence\n",zeff); fprintf(fp," %lg Total energy\n",0.0); fprintf(fp," %f %f Suggested cutoff for wfc and rho\n",0.0,0.0); fprintf(fp," %d Max angular momentum component\n",aorb_.lo[param->nll-1]); fprintf(fp," %d Number of points in mesh\n",param->ngrid); if (param->nboxes > 0) { fprintf(fp," %d %d Number of Wavefuncitons, Number of Projectors\n",param->nll,param->nll); }else{ fprintf(fp," %d %d Number of Wavefuncitons, Number of Projectors\n",param->nll,param->nll-1); } fprintf(fp," Wavefunctions nl l occ\n"); for (j=0;j<param->nll;j++) { nn=aorb_.no[j]; ll=aorb_.lo[j]; fprintf(fp," %c %d %f\n",lc[ll],ll,adat_.wnl[j]); } fprintf(fp,"</PP_HEADER>\n"); fprintf(fp,"<PP_MESH>\n"); fprintf(fp," <PP_R>\n"); for (i=0;i<param->ngrid;i++){ fprintf(fp," %19.16le ",rr[i]); if( (i+1)%4 == 0 ) fprintf(fp,"\n"); } if(i%4 !=0 ) fprintf(fp,"\n"); fprintf(fp," </PP_R>\n"); fprintf(fp," <PP_RAB>\n"); for (i=0;i<param->ngrid;i++){ fprintf(fp," %19.16le ",rab[i]); if( (i+1)%4 == 0 ) fprintf(fp,"\n"); } if(i%4 !=0 ) fprintf(fp,"\n"); fprintf(fp," </PP_RAB>\n"); fprintf(fp,"</PP_MESH>\n"); if (param->rpcc > 1e-6){ fprintf(fp,"\n\n<PP_NLCC>\n"); for (i=0;i<param->ngrid;i++){ fprintf(fp,"%19.16le ",rscore[i]/rr[i]/rr[i]/4.0/M_PI); if( (i+1)%4 == 0 ) fprintf(fp,"\n"); } if(i%4 !=0 ) fprintf(fp,"\n"); fprintf(fp,"</PP_NLCC>\n"); } fprintf(fp,"\n\n<PP_LOCAL>\n"); for (i=0;i<param->ngrid;i++){ fprintf(fp,"%19.16le ",nlcore[i]/rr[i]); if( (i+1)%4 == 0 ) fprintf(fp,"\n"); } if(i%4 !=0 ) fprintf(fp,"\n"); fprintf(fp,"</PP_LOCAL>\n"); sprintf(filename, "%s.beta", param->name); fp2 = fopen(filename, "w"); fprintf(fp,"\n\n<PP_NONLOCAL>\n"); nnl=0; for (j=0;j<param->nll;j++){ nn=aorb_.no[j]; ll=aorb_.lo[j]; for (i=0;i<param->ngrid;i++){ fprintf(fp2," %19.16le %19.16le %d \n",rr[i],rnl[j][i],j); } fprintf(fp2,"&\n"); if ((param->localind != j) || (param->nboxes > 0)) { nnl+=1; fprintf(fp," <PP_BETA>\n"); for (i=0;i<param->ngrid;i++){ beta[i]=rnl[j][i]*(rvcore[j][i]-nlcore[i])/rr[i]; ddd[i]=beta[i]*rnl[j][i]; } fprintf(fp," %d %d Beta L\n",nnl,ll); fprintf(fp," %d \n",nind[j]); for (i=0;i<param->ngrid;i++){ fprintf(fp," %19.16le ",beta[i]); if( (i+1)%4 == 0 ) fprintf(fp,"\n"); } if(i%4 !=0 ) fprintf(fp,"\n"); fprintf(fp," </PP_BETA>\n"); ng=param->ngrid; if (ng%2 !=0) ng-=1; dij[nnl-1]=1.0/simpson(ddd,rab,ng); }else{ } } fprintf(fp," <PP_DIJ>\n"); fprintf(fp," %d Number of nonzero Dij\n",nnl); for (i=0;i<nnl;i++){ fprintf(fp," %d %d %19.16le \n",i+1,i+1,dij[i]); } fprintf(fp," </PP_DIJ>\n"); fprintf(fp,"</PP_NONLOCAL>\n"); fclose(fp2); fprintf(fp,"<PP_PSWFC>\n"); for (j=0;j<param->nll;j++){ nn=aorb_.no[j]; ll=aorb_.lo[j]; occ=adat_.wnl[j]; occ = MAX(occ,0); fprintf(fp,"%c %d %5.2f Wavefunction\n",lc[ll],ll,occ); for (i=0;i<param->ngrid;i++){ fprintf(fp," %19.16le ",rnl[j][i]); rscore[i]+=occ*rnl[j][i]*rnl[j][i]; if( (i+1)%4 == 0 ) fprintf(fp,"\n"); } if(i%4 !=0 ) fprintf(fp,"\n"); } fprintf(fp,"</PP_PSWFC>\n"); fprintf(fp,"\n\n<PP_RHOATOM>\n"); for (i=0;i<param->ngrid;i++){ fprintf(fp," %19.16le ",rscore[i]); if( (i+1)%4 == 0 ) fprintf(fp,"\n"); } if(i%4 !=0 ) fprintf(fp,"\n"); fprintf(fp,"</PP_RHOATOM>\n"); if (!strcmp(param->reltype, "frl")) { fprintf(fp,"\n\n<PP_ADDINFO>\n"); for (j=0;j<param->nll;j++){ nn=aorb_.no[j]; ll=aorb_.lo[j]; occ=adat_.wnl[j]; occ = MAX(occ,0); fprintf(fp," %d%c %d %d %5.2f %5.2f \n",nn,lc[ll],nn,ll,ll+adat_.so[j],occ); } for (j=0;j<param->nll;j++){ if ((param->localind != j) || (param->nboxes > 0)) { nn=aorb_.no[j]; ll=aorb_.lo[j]; occ=adat_.wnl[j]; occ = MAX(occ,0); fprintf(fp," %d %5.2f \n",ll,ll+adat_.so[j]); } } fprintf(fp,"%12.8f, %12.8f, %12.8f, %12.8f \n",xmin,rmax,param->z,param->b); fprintf(fp,"</PP_ADDINFO>\n"); } fclose(fp); return 0; }
// `自适应Simpson公式(主过程)` double asr(double a, double b, double eps) { return asr(a, b, eps, simpson(a, b)); }
void gen_disk(int nptcl, int myseed, float *buffer, int verbose) { dotalk = verbose; int i, j, k, nobj=10000; int seed= -123; int icofm=1; float q=0.9, rtrunc=5.0; float rhoran, massapp; float x, y, z, vx, vy, v, v2, R, vmax, vmax2; float phi, cph, sph, cth, sth, vR, vp, vz; float E, Lz, rad, rad2; float f0, frand, fmax, fmax1, vphimax1, psi, fnorm; float Fdisk(); float ran1(); float invu(); float dr, rhomax1; float t, mass; float u1, v1, u1max, v1max; float xcm, ycm, zcm, vxcm, vycm, vzcm; float zip = 0.0, psi0; float rhomax=0.15, rhomin, rhotst; float rhoguess, zcon; float stream=0.5; float con, outdisk, drtrunc; float omega, kappa; float dv, v0; float broadcon=1.0; float vfacR, vfacz; float f1; float gr, gp, gz, g2; float vsigmax, vmean, vsig2; float gasdev(); float diskdensf_(), sigr2_(), sigz2_(); float FindMax(), FindMax1(), Fmax(); float simpson(), massring(); float velocityfactors_(); // FILE *rhoout; // FILE *errfile; char harmfile[80]; // errfile = fopen("errmsg.dat","w"); if(verbose) fprintf(stderr,"The Disk\n"); strcpy(harmfile,"dbh.dat"); nobj = nptcl; seed = myseed; mysrand(myseed); icofm = 1; if (nobj <= 0) { iquery("Enter the number of particles",&nobj); iquery("Enter negative integer seed",&seed); iquery("Center the simulation (0=no,1=yes)",&icofm); cquery("Enter harmonics file",harmfile); } r = (phase *) calloc(nobj,sizeof(phase)); readdiskdf_(harmfile,&gparam); mdisk = gparam.mdisk; rdisk = gparam.rdisk; zdisk = gparam.zdisk; outdisk = gparam.outdisk; drtrunc = gparam.drtrunc; rd = 1.2*rdisk; zd = 1.2*zdisk; rtrunc = (outdisk + 2.0*drtrunc); diskmass = 4.0*M_PI*simpson(massring,0.0,rtrunc,128); mass = diskmass/nobj; dr = rtrunc/100.; rhomax = 0.0; // rhoout = fopen("rhotst.dat","w"); for(i=0; i<100; i++) { R = i*dr; z = 0.0; rhoguess = exp(-R/rd); rhotst = diskdensf_(&R,&z); rhotst /= rhoguess; if( rhotst > rhomax ) rhomax = rhotst; // fprintf(rhoout,"%g %g\n",R, rhotst); } rhomin = 0.0; rhomax *=1.2; #if 0 { FILE *disksig; float rad, vsigR, vsigp;; disksig = fopen("disksig.dat","w"); for(rad=0.1; rad<30.0; rad+=0.1) { omekap_(&rad,&omega,&kappa); vsigR = sqrt(sigr2_(&rad)); vsigp = kappa/(2.0*omega)*vsigR; fprintf(disksig,"%g %g %g %g %g\n",rad,vsigp,vsigR,omega,kappa); } fclose(disksig); } #endif /* tstFdisk(); */ if (verbose) fprintf(stderr,"Calculating disk positions and velocities\n"); vmean = 0; vsig2 = 0; for(i=0, j=0, k=0; i<nobj;) { u1 = -ran1(&seed); v1 = 2.0*(ran1(&seed) - 0.5); R = rd*invu(u1); z = zd*atanh(v1); zcon = cosh(z/zd); rhoguess = exp(-R/rd)/(zcon*zcon); /* Guess at the approximate functional form of the density */ rhotst = diskdensf_(&R,&z); rhotst /= rhoguess; k++; if( rhotst < rhomin ) continue; rhoran = (rhomax - rhomin)*ran1(&seed); if( rhoran > rhotst ) continue; phi = 2.0*M_PI*ran1(&seed); x = R*cos(phi); y = R*sin(phi); omekap_(&R, &omega, &kappa); vphimax = omega*R; vsigR = sqrt(sigr2_(&R)); vsigp = kappa/(2.0*omega)*vsigR; vsigz = sqrt(sigz2_(&R)); vsigmax = vsigR; if( vsigp > vsigmax ) vsigmax = vsigp; if( vsigz > vsigmax ) vsigmax = vsigz; fmax = 1.1*FindMax(R,z,&vphimax); /* fmax1 = 1.1*FindMax1(R,z,&vphimax1); fprintf(stderr,"fmax %g fmax1 %g vphimax %g vphimax1 %g\n", fmax, fmax1, vphimax, vphimax1); */ f0 = 0.0; frand = 1.0; /* dummy starters */ while( frand > f0 ) { /* g2 = 999.; while( g2 > 1.0) { gr = 2.*(ran1(&seed) - 0.5); gp = 2.*(ran1(&seed) - 0.5); gz = 2.*(ran1(&seed) - 0.5); g2 = (gr*gr + gp*gp + gz*gz); } */ gr = 6.0*(ran1(&seed) - 0.5)*vsigR; gp = 6.0*(ran1(&seed) - 0.5)*vsigp; gz = 6.0*(ran1(&seed) - 0.5)*vsigz; /* gp = (vphimax + 3.0*vsigp)*ran1(&seed) - vphimax; */ /* gr = 3.0*vsigmax; gp = 3.0*vsigmax; gz = 3.0*vsigmax; */ vR = gr; vp = vphimax + gp; vz = gz; f0 = Fdisk(vR, vp, vz, R, z); frand = fmax*ran1(&seed); #if 0 if( f0 > fmax ) { float vpmax; fprintf(errfile,"f0 > fmax at R=%g z=%g\nvr=%g vp=%g, vz=%g vphimax=%g f0=%g, fmax=%g\n", R,z, vR*broadcon/vsigR, (vp - vphimax)*broadcon/vsigp, vz*broadcon/vsigz, vphimax, f0, fmax); fflush(errfile); } #endif j++; } velocityfactors_(&R, &z, &vfacR, &vfacz); vphimax = vp - gp; vphimax *= vfacR; vp = vphimax + gp; vz *= vfacz; /* fprintf(stdout,"%g %g\n",vp,frand); */ cph = x/R; sph = y/R; vx = vR*cph - vp*sph; vy = vR*sph + vp*cph; /* vz stays the same */ /* sanity check */ if (isnan(x)) continue; if (isnan(y)) continue; if (isnan(z)) continue; if (isnan(vx)) continue; if (isnan(vy)) continue; if (isnan(vz)) continue; const float RMAX = 200; const float VMAX = 10; if (abs(x) > RMAX) continue; if (abs(y) > RMAX) continue; if (abs(z) > RMAX) continue; if (abs(vx) > VMAX) continue; if (abs(vy) > VMAX) continue; if (abs(vz) > VMAX) continue; r[i].x = (float) x; r[i].y = (float) y; r[i].z = (float) z; r[i].vx = (float)vx; r[i].vy = (float)vy; r[i].vz = (float)vz; i++; if (verbose) if( i % 1000 == 0 ) { fprintf(stderr,"."); fflush(stderr); } } if (verbose) { fprintf(stderr,"\n"); fprintf(stderr,"number of density trials %d\n",k); fprintf(stderr,"number of velocity trials %d\n",j); } if( icofm ) { xcm = ycm =zcm = vxcm =vycm =vzcm = 0; for(i=0; i<nobj; i++) { xcm += r[i].x; ycm += r[i].y; zcm += r[i].z; vxcm += r[i].vx; vycm += r[i].vy; vzcm += r[i].vz; } xcm /= nobj; ycm /=nobj; zcm /= nobj; vxcm /= nobj; vycm /=nobj; vzcm /= nobj; for(i=0; i<nobj; i++) { r[i].x -= xcm; r[i].y -= ycm; r[i].z -= zcm; r[i].vx -= vxcm; r[i].vy -= vycm; r[i].vz -= vzcm; } } if (buffer == NULL) { t = 0.0; #ifdef ASCII fprintf(stdout,"%d\n",nobj); for(i=0; i<nobj; i++) { fprintf(stdout,"% 15.7e % 15.7e % 15.7e % 15.7e % 15.7e % 15.7e % 15.7e\n", mass, r[i].x, r[i].y, r[i].z, r[i].vx, r[i].vy, r[i].vz); } #else for(i=0; i<nobj; i++) { fwrite(&mass,sizeof(float),1,stdout); fwrite(r+i,sizeof(phase),1,stdout); } #endif } else { const int min_disk = 000000000; const int max_disk = 100000000; int NEL = 8; int i,pc; for (i = 0, pc= 0; i < nobj; i++, pc += NEL) { *((int*)&buffer[pc]) = min_disk + (i%(max_disk-min_disk)); buffer[pc+1] = mass; buffer[pc+2] = r[i].x; buffer[pc+3] = r[i].y; buffer[pc+4] = r[i].z; buffer[pc+5] = r[i].vx; buffer[pc+6] = r[i].vy; buffer[pc+7] = r[i].vz; } } }
void termsmisenopresmooth(double *t, int *delta, int *n, double *esf, double *grid, int *legrid, double *step, double *bw, int *nkernel, int *nestimand, double *integral1, double *integral2){ int i, *temp, *pnull; double *pnull2, *p, *p1, *p2, *pt, *S, *f2, *h, *h1, *h2, *integrand1, *integrand2, *deltadbl; temp = calloc(1, sizeof(int)); p = malloc(*legrid * sizeof(double)); p1 = malloc(*legrid * sizeof(double)); p2 = malloc(*legrid * sizeof(double)); h = malloc(*legrid * sizeof(double)); integrand1 = malloc(*legrid * sizeof(double)); integrand2 = malloc(*legrid * sizeof(double)); nadarayawatsonder(grid, legrid, t, delta, n, &(bw[0]), nkernel, p, p1, p2); densuncens(grid, legrid, t, n, &(bw[1]), nkernel, temp, h); *temp = 1; if(*nestimand == 3){ //f pnull = calloc(1, sizeof(int)); pnull2 = calloc(1, sizeof(double)); pt = malloc(*n * sizeof(double)); S = malloc(*legrid * sizeof(double)); f2 = malloc(*legrid * sizeof(double)); deltadbl = malloc(*n * sizeof(double)); nadarayawatson(t, n, t, delta, n, &(bw[0]), nkernel, pt); // without presmoothing, deltas instead of pt for (i = 0; i < *n; i++) // type conversion from integer to double deltadbl[i] = (double)delta[i]; presmestim(grid, legrid, t, n, pnull2, pnull, pnull, deltadbl, pnull, temp, S); presmdens2der(grid, legrid, t, n, &(bw[2]), nkernel, pt, f2); for (i = 0; i < *legrid; i++){ integrand1[i] = pow(f2[i], 2); integrand2[i] = h[i] * p[i] * pow(S[i] / esf[i], 2); } free(pnull); free(pnull2); free(pt); free(S); free(f2); free(deltadbl); } else if (*nestimand == 4){ // h h1 = malloc(*legrid * sizeof(double)); h2 = malloc(*legrid * sizeof(double)); densuncens(grid, legrid, t, n, &(bw[1]), nkernel, temp, h1); *temp = 2; densuncens(grid, legrid, t, n, &(bw[1]), nkernel, temp, h2); for (i = 0; i < *legrid; i++){ integrand1[i] = pow(((h2[i] + 3*h[i]*h1[i]/esf[i] + 2*pow(h[i],3)/pow(esf[i],2))*p[i] + 2*(h1[i] + pow(h[i],2)/esf[i])*p1[i] + h[i]*p2[i])/esf[i], 2); integrand2[i] = h[i] * p[i] / pow(esf[i],2); } free(h1); free(h2); } simpson(integrand1, legrid, integral1); simpson(integrand2, legrid, integral2); *integral1 *= *step; *integral2 *= *step; free(temp); free(p); free(p1); free(p2); free(h); free(integrand1); free(integrand2); }
static double aRate(double X, int everage,int Fast, float ** wPrc) { double Sum=0.; int i,l1,l2; int nPrc=0; char* pname[5]; gridStr grid,grid1; double MassCutOut=MassCut+M*log(100.)/X; double Msmall,Mlarge; int nPrcTot=0; if(MassCutOut<M*(2+10/X)) MassCutOut=M*(2+10/X); xf_=X; exi=everage; if(wPrc) *wPrc=NULL; for(l1=0;l1<NC;l1++) { int k1=sort[l1]; if(M+inMass[k1]>MassCut) break; for(l2=0;l2<NC;l2++) { double Sumkk=0.; double x[2],f[2]; double factor; int k2=sort[l2]; CalcHEP_interface * CI; if(inMass[k1]+inMass[k2] > MassCut) break; if(inC[k1*NC+k2]<=0) continue; if(code22[k1*NC+k2]==NULL) new_code(k1,k2); if(inC[k1*NC+k2]<=0) continue; if(!code22[k1*NC+k2]->init) { numout * cd=code22[k1*NC+k2]; CalcHEP_interface *cdi=cd->interface; for(i=1;i<=cdi->nvar;i++) if(cd->link[i]) cdi->va[i]=*(cd->link[i]); if( cdi->calcFunc()>0 ) {FError=1; return -1;} cd->init=1; } if(wPrc) { nPrcTot+=code22[k1*NC+k2]->interface->nprc; *wPrc=(float*)realloc(*wPrc,sizeof(float)*(nPrcTot)); } sqme=code22[k1*NC+k2]->interface->sqme; DeltaXf=(inDelta[k1]+inDelta[k2])*X; inBuff=0; M1=inMass[k1]; M2=inMass[k2]; Msmall=M1>M2? M1-M*(1-sWidth): M2-M*(1-sWidth); Mlarge=M1>M2? M2+M*(1-sWidth): M1+M*(1-sWidth); u_max=m2u(MassCutOut); if(Fast) { if(Fast==1) { double c[4]; for(Npow=0;Npow<4;Npow++) c[Npow]=simpson(s_pow_integrand, 0. ,1. ,1.E-4); gaussC2(c,x,f); for(i=0;i<2;i++){ x[i]=sqrt(x[i]); f[i]*=2*x[i]/M;} }else { double c[2]; for(Npow=0;Npow<2;Npow++) c[Npow]=simpson(s_pow_integrand, 0. ,1. ,1.E-4); x[0]= sqrt(c[1]/c[0]); f[0]= c[0]*2*x[0]/M; } } factor=inC[k1*NC+k2]*inG[k1]*inG[k2]*exp(-DeltaXf); CI=code22[k1*NC+k2]->interface; for(nsub=1; nsub<= CI->nprc;nsub++,nPrc++) { double u_min=0.; double a=0; if(wPrc) (*wPrc)[nPrc]=0; for(i=0;i<4;i++) pname[i]=CI->pinf(nsub,i+1,pmass+i,NULL); if(pmass[2]+pmass[3]>MassCutOut) continue; if( (pmass[2]>Mlarge && pmass[3]<Msmall) ||(pmass[3]>Mlarge && pmass[2]<Msmall)) { *(CI->twidth)=1; *(CI->gtwidth)=1;} else { *(CI->twidth)=0; *(CI->gtwidth)=0;} *(CI->gswidth)=0; if(pmass[2]+pmass[3] > pmass[0]+pmass[1]) { double smin=pmass[2]+pmass[3]; if((pmass[0]!=M1 || pmass[1]!=M2)&&(pmass[0]!=M2 || pmass[1]!=M1)) { double ms=pmass[0]+pmass[1]; double md=pmass[0]-pmass[1]; double Pcm=sqrt((smin-ms)*(smin+ms)*(smin-md)*(smin+md))/(2*smin); smin=sqrt(M1*M1+Pcm*Pcm)+sqrt(M2*M2+Pcm*Pcm); } u_min=m2u(smin); }else u_min=0; repeat: neg_cs_flag=0; if(!Fast) a=simpson(s_integrand,u_min,1.,eps); else if(Fast!=1) a=f[0]*sigma(x[0]); else { int isPole=0; char * s; int m,w,n; double mass,width; for(n=1;(s=code22[k1*NC+k2]->interface->den_info(nsub,n,&m,&w));n++) if(m && w && strcmp(s,"\1\2")==0 ) { mass=code22[k1*NC+k2]->interface->va[m]; width=code22[k1*NC+k2]->interface->va[w]; if(mass<MassCutOut && mass+8*width > pmass[0]+pmass[1] && mass+8*width > pmass[2]+pmass[3]) { if((pmass[0]!=M1 || pmass[1]!=M2)&&(pmass[0]!=M2 || pmass[1]!=M1)) { double ms=pmass[0]+pmass[1]; double md=pmass[0]-pmass[1]; double Pcm=sqrt((mass-ms)*(mass+ms)*(mass-md)*(mass+md))/(2*mass); mass=sqrt(M1*M1+Pcm*Pcm)+sqrt(M2*M2+Pcm*Pcm); } grid1=makeGrid(mass,width); if(isPole) grid=crossGrids(&grid,&grid1); else grid=grid1; isPole++; } } if(isPole==0) { grid.n=1; grid.ul[0]=u_min; grid.ur[0]=u_max; grid.pow[0]=3; } if(grid.n==1 && pmass[0]+pmass[1]> 1.1*(pmass[2]+pmass[3])) a=f[0]*sigma(x[0])+f[1]*sigma(x[1]); else for(i=0;i<grid.n;i++)if(u_min<=grid.ur[i]) { double ul= u_min<grid.ul[i]? grid.ul[i]:u_min; double da=gauss(s_integrand,ul,grid.ur[i],grid.pow[i]); a+=da; } } if(neg_cs_flag && *(CI->gswidth)==0) { *(CI->gswidth)=1; goto repeat; } /* printf("X=%.2E (%d) %.3E %s %s %s %s\n",X,everage, a, pname[0],pname[1],pname[2],pname[3]); */ Sumkk+=a; if(wPrc) (*wPrc)[nPrc] = a*factor; } Sum+=factor*Sumkk; /* printf("Sum=%E\n",Sum); */ } } if(wPrc) for(i=0; i<nPrc;i++) (*wPrc)[i]/=Sum; if(!everage) { double gf=geff(X); Sum/=gf*gf;} /* exit(1); */ return Sum; }
void IntegraleDeterministico::exec(int n) { setIntervalli(n); print(n, abs(trapezi()-m_f), abs(simpson()-m_f), abs(m_f-gauss())); }
void uniPP_kbfnl(uniPP *unipp, dcomplex *****kbfnl, int nproj, double *gk_abs, double **gk, int nmax, int flag){ int i, j, j_max, ig, l, l_loc, proj; double r, u, dv, q, z, gka; double cosx, cosy, cosz; double eta, lambda_eta; double *g = (double *)malloc(unipp->m_mesh * sizeof(double)); double *f = (double *)malloc(unipp->m_mesh * sizeof(double)); l_loc = unipp->l_loc; for (l=0; l<unipp->l_max; l++){ if ((l!=l_loc) | (flag==0)){ if (unipp->rel && l>0) j_max=2; else j_max=1; for (j=0; j<j_max; j++){ for (proj=0; proj<nproj; proj++){ /****************************************************************** * Prepare the two arrays: f[] and g[] * ******************************************************************/ for (i=0; i<unipp->m_mesh; i++){ r = unipp->r_m[i]; u = unipp->u_ps[l][j][i]; if (flag==1) dv = unipp->v_ps[l][j][i] - unipp->v_loc[i]; /* [Hartree] */ else dv = 1.; /* [unitless] */ f[i] = r*u*u*dv; g[i] = r*r*u*dv; } /******************************************************************** * Handle different angular momenta: * ********************************************************************/ if (l==0){ /****************************************************************** * s-part * ******************************************************************/ if (proj==0){ for (ig=0; ig<nmax; ig++){ gka = gk_abs[ig] * 0.529177249; /* 1/A -> 1/au */ if (gka==0.) q = simpson(unipp->m_mesh, g, log(unipp->a_mesh)) + .5*g[0]; else{ for (i=0; i<unipp->m_mesh; i++){ r = unipp->r_m[i]; z = gka*r; f[i] = (sin(z)/z) * g[i]; } q = simpson(unipp->m_mesh, f, log(unipp->a_mesh)) + .5*f[0]; } kbfnl[0][j][0][0][ig].r = q; kbfnl[0][j][0][0][ig].i = 0.; } }else if (proj==1){ eta = simpson(unipp->m_mesh, f, log(unipp->a_mesh)) + .5*f[0]; for (i=0; i<unipp->m_mesh; i++){ r = unipp->r_m[i]; g[i] *= r*r; f[i] *= r*r; } lambda_eta = eta/ (simpson(unipp->m_mesh, f, log(unipp->a_mesh)) + .5*f[0]); for (ig=0; ig<nmax; ig++){ gka = gk_abs[ig] * 0.529177249; /* 1/A -> 1/au */ if (gka==0.) q = simpson(unipp->m_mesh, g, log(unipp->a_mesh)) + .5*g[0]; else{ for (i=0; i<unipp->m_mesh; i++){ r = unipp->r_m[i]; z = gka*r; f[i] = (sin(z)/z) * g[i]; } q = simpson(unipp->m_mesh, f, log(unipp->a_mesh)) + .5*f[0]; } kbfnl[0][j][1][0][ig].r = q -lambda_eta*kbfnl[0][j][0][0][ig].r; kbfnl[0][j][1][0][ig].i = 0.; } }else{ printf("uniPP_kbfnl: requested projector of order" " higher than two -> exit(0)\n"); exit(0); } }else if (l==1){ /****************************************************************** * p-part * ******************************************************************/ if (proj==0){ for (ig=0; ig<nmax; ig++){ gka = gk_abs[ig] * 0.529177249; /* 1/A -> 1/au */ if (gka==0.){ kbfnl[1][j][0][0][ig].r = kbfnl[1][j][0][0][ig].i = 0.; kbfnl[1][j][0][1][ig].r = kbfnl[1][j][0][1][ig].i = 0.; kbfnl[1][j][0][2][ig].r = kbfnl[1][j][0][2][ig].i = 0.; }else{ for (i=0; i<unipp->m_mesh; i++){ r = unipp->r_m[i]; z = gka*r; f[i] = ((sin(z)/z-cos(z))/z) * g[i]; } q = simpson(unipp->m_mesh, f, log(unipp->a_mesh)) + .5*f[0]; cosx = gk[ig][0]/gk_abs[ig]; cosy = gk[ig][1]/gk_abs[ig]; cosz = gk[ig][2]/gk_abs[ig]; kbfnl[1][j][0][0][ig].r = q * cosx / sqrt(2.); kbfnl[1][j][0][0][ig].i = - q * cosy / sqrt(2.); kbfnl[1][j][0][1][ig].r = q * cosz; kbfnl[1][j][0][1][ig].i = 0.; kbfnl[1][j][0][2][ig].r = - q * cosx / sqrt(2.); kbfnl[1][j][0][2][ig].i = - q * cosy / sqrt(2.); } } }else if (proj==1){ eta = simpson(unipp->m_mesh, f, log(unipp->a_mesh)) + .5*f[0]; /* printf("eta-p = %g\n", eta); */ for (i=0; i<unipp->m_mesh; i++){ r = unipp->r_m[i]; g[i] *= r*r; f[i] *= r*r; } lambda_eta = (simpson(unipp->m_mesh, f, log(unipp->a_mesh)) + .5*f[0])/eta; for (ig=0; ig<nmax; ig++){ gka = gk_abs[ig] * 0.529177249; /* 1/A -> 1/au */ if (gka==0.){ kbfnl[1][j][1][0][ig].r = kbfnl[1][j][1][0][ig].i = 0.; kbfnl[1][j][1][1][ig].r = kbfnl[1][j][1][1][ig].i = 0.; kbfnl[1][j][1][2][ig].r = kbfnl[1][j][1][2][ig].i = 0.; }else{ for (i=0; i<unipp->m_mesh; i++){ r = unipp->r_m[i]; z = gka*r; f[i] = ((sin(z)/z-cos(z))/z) * g[i]; } q = simpson(unipp->m_mesh, f, log(unipp->a_mesh)) + .5*f[0]; cosx = gk[ig][0]/gk_abs[ig]; cosy = gk[ig][1]/gk_abs[ig]; cosz = gk[ig][2]/gk_abs[ig]; kbfnl[1][j][1][0][ig].r = q * cosx / sqrt(2.) - lambda_eta * kbfnl[1][j][0][0][ig].r; kbfnl[1][j][1][0][ig].i = - q * cosy / sqrt(2.) - lambda_eta * kbfnl[1][j][0][0][ig].i; kbfnl[1][j][1][1][ig].r = q * cosz - lambda_eta * kbfnl[1][j][0][1][ig].r; kbfnl[1][j][1][1][ig].i = 0.; kbfnl[1][j][1][2][ig].r = - q * cosx / sqrt(2.) - lambda_eta * kbfnl[1][j][0][2][ig].r; kbfnl[1][j][1][2][ig].i = - q * cosy / sqrt(2.) - lambda_eta * kbfnl[1][j][0][2][ig].i; } } }else{ printf("uniPP_kbfnl: requested projector of order" " higher than two -> exit(0)\n"); exit(0); } }else if (l==2){ /****************************************************************** * d-part * ******************************************************************/ if (proj==0){ for (ig=0; ig<nmax; ig++){ gka = gk_abs[ig] * 0.529177249; /* 1/A -> 1/au */ if (gka==0.){ kbfnl[2][j][0][0][ig].r = kbfnl[2][j][0][0][ig].i = 0.; kbfnl[2][j][0][1][ig].r = kbfnl[2][j][0][1][ig].i = 0.; kbfnl[2][j][0][2][ig].r = kbfnl[2][j][0][2][ig].i = 0.; kbfnl[2][j][0][3][ig].r = kbfnl[2][j][0][3][ig].i = 0.; kbfnl[2][j][0][4][ig].r = kbfnl[2][j][0][4][ig].i = 0.; }else{ for (i=0; i<unipp->m_mesh; i++){ r = unipp->r_m[i]; z = gka*r; f[i] = (((3./(z*z)-1)*sin(z) - (3./z)*cos(z))/z) * g[i]; } q = simpson(unipp->m_mesh, f, log(unipp->a_mesh)) + .5*f[0]; cosx = gk[ig][0]/gk_abs[ig]; cosy = gk[ig][1]/gk_abs[ig]; cosz = gk[ig][2]/gk_abs[ig]; kbfnl[2][j][0][0][ig].r = q*(cosx*cosx-cosy*cosy)*sqrt(3./8.); kbfnl[2][j][0][0][ig].i = -q * (2.*cosx*cosy) * sqrt(3./8.); kbfnl[2][j][0][1][ig].r = q * (cosx*cosz) * sqrt(3./2.); kbfnl[2][j][0][1][ig].i = -q * (cosy*cosz) * sqrt(3./2.); kbfnl[2][j][0][2][ig].r = q * (3.*cosz*cosz-1.) * .5; kbfnl[2][j][0][2][ig].i = 0.; kbfnl[2][j][0][3][ig].r = -q * (cosx*cosz) * sqrt(3./2.); kbfnl[2][j][0][3][ig].i = -q * (cosy*cosz) * sqrt(3./2.); kbfnl[2][j][0][4][ig].r = q*(cosx*cosx-cosy*cosy)*sqrt(3./8.); kbfnl[2][j][0][4][ig].i = q * (2.*cosx*cosy) * sqrt(3./8.); } } }else if (proj==1){ eta = simpson(unipp->m_mesh, f, log(unipp->a_mesh)) + .5*f[0]; for (i=0; i<unipp->m_mesh; i++){ r = unipp->r_m[i]; g[i] *= r*r; f[i] *= r*r; } lambda_eta = (simpson(unipp->m_mesh, f, log(unipp->a_mesh)) + .5*f[0])/eta; for (ig=0; ig<nmax; ig++){ gka = gk_abs[ig] * 0.529177249; /* 1/A -> 1/au */ if (gka==0.){ kbfnl[2][j][1][0][ig].r = kbfnl[2][j][1][0][ig].i = 0.; kbfnl[2][j][1][1][ig].r = kbfnl[2][j][1][1][ig].i = 0.; kbfnl[2][j][1][2][ig].r = kbfnl[2][j][1][2][ig].i = 0.; kbfnl[2][j][1][3][ig].r = kbfnl[2][j][1][3][ig].i = 0.; kbfnl[2][j][1][4][ig].r = kbfnl[2][j][1][4][ig].i = 0.; }else{ for (i=0; i<unipp->m_mesh; i++){ r = unipp->r_m[i]; z = gka*r; f[i] = (((3./(z*z)-1)*sin(z) - (3./z)*cos(z))/z) * g[i]; } q = simpson(unipp->m_mesh, f, log(unipp->a_mesh)) + .5*f[0]; cosx = gk[ig][0]/gk_abs[ig]; cosy = gk[ig][1]/gk_abs[ig]; cosz = gk[ig][2]/gk_abs[ig]; kbfnl[2][j][1][0][ig].r =q*(cosx*cosx-cosy*cosy) * sqrt(3./8.) - lambda_eta * kbfnl[2][j][0][0][ig].r; kbfnl[2][j][1][0][ig].i = -q * (2.*cosx*cosy) * sqrt(3./8.) - lambda_eta * kbfnl[2][j][0][0][ig].i; kbfnl[2][j][1][1][ig].r = q * (cosx*cosz) * sqrt(3./2.) - lambda_eta * kbfnl[2][j][0][1][ig].r; kbfnl[2][j][1][1][ig].i = -q * (cosy*cosz) * sqrt(3./2.) - lambda_eta * kbfnl[2][j][0][1][ig].i; kbfnl[2][j][1][2][ig].r = q * (3.*cosz*cosz-1.) * .5 - lambda_eta * kbfnl[2][j][0][2][ig].r; kbfnl[2][j][1][2][ig].i = 0.; kbfnl[2][j][1][3][ig].r = -q * (cosx*cosz) * sqrt(3./2.) - lambda_eta * kbfnl[2][j][0][3][ig].r; kbfnl[2][j][1][3][ig].i = -q * (cosy*cosz) * sqrt(3./2.) - lambda_eta * kbfnl[2][j][0][3][ig].i; kbfnl[2][j][1][4][ig].r =q*(cosx*cosx-cosy*cosy) * sqrt(3./8.) - lambda_eta * kbfnl[2][j][0][4][ig].r; kbfnl[2][j][1][4][ig].i = q * (2.*cosx*cosy) * sqrt(3./8.) - lambda_eta * kbfnl[2][j][0][4][ig].i; } } }else{ printf("uniPP_kbfnl: requested projector of order" " higher than two -> exit(0)\n"); exit(0); } }else{ printf("uniPP_kbfnl.c: l=%d > 2 is not supported!\n", l); exit(0); } } } } } free(f); free(g); }