void PIC::inject_beam(double boundary, double vel, double angle, double ndensity, double Tempxy, double Tempz, double dt, long *d1) { double Ninj=vel*ndensity*PI*pow(boundary,2)*dt/macroN; double gamma=1.0/sqrt(1.0-pow(vel/clight,2)); double utherm=clight*sqrt(pow(charge_p*Tempxy/(mass_p*clight*clight)+1.0,2)-1.0); double r_amp, phase; double u, v, w; for(int j=0; j<Ninj; j++) { Particle part_inj; r_amp=boundary*sqrt(ran1(d1)); phase=2.0*PI*ran1(d1); part_inj.x=r_amp*cos(phase); part_inj.y=r_amp*sin(phase); part_inj.z=-0.5*length; do { u = 2.0 * ran1(d1) - 1.0 ; v = 2.0 * ran1(d1) - 1.0 ; w = u*u + v*v ; } while (w >= 1.0) ; part_inj.ux = part_inj.x/r_amp*vel*angle + utherm * u * sqrt(-2.0*log(w)/w) ; part_inj.uy = part_inj.y/r_amp*vel*angle + utherm * v * sqrt(-2.0*log(w)/w) ; part_inj.uz = gamma*vel; part.push_back(part_inj); } }
float poidev(float xm, long int *idum) { static float sq,alxm,g,oldm=(-1.0); float em,t,y; if (xm < 12.0) { if (xm != oldm) { oldm=xm; g=exp(-xm); } em = -1; t=1.0; do { ++em; t *= ran1(idum); } while (t > g); } else { if (xm != oldm) { oldm=xm; sq=sqrt(2.0*xm); alxm=log(xm); g=xm*alxm-gammln(xm+1.0); } do { do { y=tan(PI*ran1(idum)); em=sq*y+xm; } while (em < 0.0); em=floor(em); t=0.9*(1.0+y*y)*exp(em*alxm-gammln(em+1.0)-g); } while (ran1(idum) > t); } return em; }
/* ********************************************************/ double gasdev(long int *idum) { /* * Gaussian randon number generator. Uses ran1. ***********************************************************/ double ran1(long int *idum); static int iset = 0; static double gset; double fac, rsq, v1, v2; if (*idum < 0) iset = 0; if (iset == 0) { do { v1 = 2.0 * ran1(idum) - 1.0; v2 = 2.0 * ran1(idum) - 1.0; rsq = v1 * v1 + v2 * v2; } while (rsq >= 1.0 || rsq == 0.0); fac = sqrt(-2.0 * log(rsq) / rsq); gset = v1 * fac; iset = 1; return v2 * fac; } else { iset = 0; return gset; } }
void gen_Ak(double *Ak, int kx, int ky, int kz, double Pk, int dots) { int nkx, nky, nkz; double phi, rnd1, rnd2, r; rnd1 = ran1(&iiseed); phi = 2 * PI * rnd1; /* phase */ rnd2 = ran1(&iiseed); r = sqrt(-2 * log(rnd2)*Pk); /* amplitude */ Ak[2*(kx*dots*dots + ky*dots + kz)] = r*cos(phi); /* real part */ Ak[2*(kx*dots*dots + ky*dots + kz)+1] = r*sin(phi); /* imag. part */ if((kx != 0) && (ky != 0) && (kz != 0)) { nkx = dots - kx; nky = dots - ky; nkz = dots - kz; Ak[2*(nkx*dots*dots + nky*dots + nkz)] = r*cos(phi); /* real part */ Ak[2*(nkx*dots*dots + nky*dots + nkz)+1] = -r*sin(phi); /* imag. part */ } }
void PIC::set_const_density_xy(double boundary, double T0, long *d1) { double u,v,w; double ut=clight*sqrt(pow(fabs(charge_p)*T0/(mass_p*clight*clight)+1.0,2)-1.0); for (int j=0 ; j < part.size(); j++) { do { u = boundary*(2.0*ran1(d1)-1.0); v = boundary*(2.0*ran1(d1)-1.0); } while (sqrt(pow(u,2)+pow(v,2)) > boundary); part[j].x=u; part[j].y=v; part[j].z=length * ran1(d1); do { u = 2.0 * ran1(d1) - 1.0 ; v = 2.0 * ran1(d1) - 1.0 ; w = u*u + v*v ; } while (w >= 1.0) ; part[j].ux = ut * u * sqrt(-2.0*log(w)/w) ; part[j].uy = ut * v * sqrt(-2.0*log(w)/w) ; part[j].uz = 0.0; } }
void Pic::waterbag_xy(double emittance_x, double emittance_y, double alpha_x, double alpha_y, double beta_x, double beta_y, double D0, double Ds0, double x0, double xs0, double y0, double ys0, int size, long *d){ emittance_x *= 6.; emittance_y *= 6.; long j; double x, y, xs, ys; double xmax = sqrt(emittance_x*beta_x); double xsmax = sqrt(emittance_x * (1.0+alpha_x*alpha_x) / beta_x); double ymax = sqrt(emittance_y*beta_y); double ysmax = sqrt(emittance_y * (1.0+alpha_y*alpha_y) / beta_y); for(j=size; j<pics.size(); ++j){ do{ x = xmax*(2.0*ran1(d)-1.0); y = ymax*(2.0*ran1(d)-1.0); xs = xsmax*(2.0*ran1(d)-1.0); ys = ysmax*(2.0*ran1(d)-1.0); }while((x*x/beta_x + beta_x * pow(xs+alpha_x/beta_x*x, 2)) / emittance_x + (y*y/beta_y + beta_y * pow(ys+alpha_y/beta_y*y, 2)) / emittance_y > 1.0); pics[j].x = x + D0*pics[j].dp + x0; // stuff for head tail modes removed; SP pics[j].xs = xs + Ds0*pics[j].dp + xs0; // stuff for head tail modes removed; SP pics[j].y = y+y0; // offset in vertrical space SA pics[j].ys = ys+ys0; } init_old_coord(); }
float gasdev(long *idum) /* Returns a normally distributed variate with zero mean and a unit variance, using ran1(idum) as the source of uniform deviates */ { float ran1(long *idum); static int iset=0; static float gset; float fac, rsq,v1,v2; if (*idum < 0) iset = 0; if (iset == 0) { do { v1 = 2.0*ran1(idum)-1.0; v2 = 2.0*ran1(idum)-1.0; rsq = v1*v1+v2*v2; } while (rsq >= 1.0 || rsq == 0.0); fac = sqrt(-2.0*log(rsq)/rsq); /*Now amke the Box-Muller transformation to get two normal deviates. Return one and save the other for next time */ gset = v1*fac; iset = 1; return (v2*fac); } else { iset = 0; return (gset); } }
/*-------------------* * Function gasdev | *-------------------* | a slight modification of crecipes version */ double gasdev(double m, double v) // m=v=u (mean poisson >30) { static int iset=0; static float gset=0.0; float fac=0.0, r=0.0, v1=0.0, v2=0.0; double ran1(); if(iset==0) { do // Compute probability r=v1^2+v2^2 { v1=2.0*ran1()-1.0; v2=2.0*ran1()-1.0; r=v1*v1+v2*v2; } while(r>=1.0); fac=sqrt(-2.0*log(r)/r); gset=v1*fac; iset=1; return(m+sqrt(v)*v2*fac); } else { iset=0; return(m+sqrt(v)*gset); } }
void _gaussdev(float *xmv, long n) { /* Returns a normally distributed deviate with zero mean and unit variance, using ran1() as the source of uniform deviates. */ /* float ran1(long *idum); */ static int iset=0; static float gset; float fac,rsq,v1,v2; long i; for (i=0;i<n;i++) { if (iset == 0) { do { v1=2.0*ran1()-1.0; v2=2.0*ran1()-1.0; rsq=v1*v1+v2*v2; } while (rsq >= 1.0 || rsq == 0.0); fac=sqrt(-2.0*log(rsq)/rsq); gset=v1*fac; iset=1; xmv[i] = v2*fac; } else { iset=0; xmv[i] = gset; } } }
/*---------* * Pick2 | *---------* | Pick two numbers (i and j) from n | Called in street.c by pick2_chrom */ int pick2(int n, int *i, int *j) { double ran1(); *i=n*ran1(); while((*j=n*ran1())==*i); return(0); }
float gasdev(long *idum) /************************************************************ * Code of this function is taken from the book * * "Neumerical Recepies in C", 1992, p289. * * Returns a normally distributed deviate with zero mean * * and unit variance, using ran1(idum) as the source of * * uniform deviates. * ************************************************************/ { float ran1(long *idum); static int iset=0; static float gset; float fac, rsq, v1, v2; if (*idum <0) iset=0; if (iset ==0){ do{ v1=2.0*ran1(idum)-1.0; v2=2.0*ran1(idum)-1.0; rsq=v1*v1+v2*v2; } while (rsq>=1.0 || rsq ==0.0); fac=sqrt(-2.0*log(rsq)/rsq); gset=v1*fac; iset=1; return v2*fac; } else{ iset=0; return gset; } }
void Pic::langevin(double beta_fxy, double beta_fz, double Dxy, double Dz, double ds, double beta_x0, double beta_y0, long* d){ double Rx, Ry, Rz; double beta0 = SP->beta0, gamma0 = SP->gamma0, A = SP->A, Z = SP->Z; for(long j=0; j<pics.size(); ++j){ Rx = 0.0; Ry = 0.0; Rz = 0.0; for(int l=1; l <= 10; ++l){ Rx += sqrt(24.0/10.0)*(ran1(d)-0.5); Ry += sqrt(24.0/10.0)*(ran1(d)-0.5); Rz += sqrt(24.0/10.0)*(ran1(d)-0.5); } pics[j].xs += -beta_fxy/(beta0*clight)*pics[j].xs*ds +Rx*sqrt(Dxy*ds/(beta_x0*beta0*clight)); pics[j].x += -beta_fxy/(beta0*clight)*pics[j].x*ds +Rx*sqrt(beta_x0*Dxy*ds/(beta0*clight)); pics[j].ys += -beta_fxy/(beta0*clight)*pics[j].ys*ds +Ry*sqrt(Dxy*ds/(beta_y0*beta0*clight)); pics[j].y += -beta_fxy/(beta0*clight)*pics[j].y*ds +Ry*sqrt(beta_y0*Dxy*ds/(beta0*clight)); pics[j].dp += -beta_fz/(beta0*clight)*pics[j].dp*ds +Rz*sqrt(Dz*ds/(beta0*clight)); } }
/* *---------------------------------------------------------------- * Gamma Distribution * Returns a deviate distributed as a gamma distribution * of integer order ia, i.e., a waiting time to the iath * event in a Poisson process of unit mean, using ran1(idum) * as the source of uniform deviates * p. 292 of the book *---------------------------------------------------------------- */ float gamdev(int ia, int *idum) { int j; float am,e,s,v1,v2,x,y; if (ia < 1){ printf("Error in routine GAMDEV!"); exit ( 0 ); } if (ia < 6) { x=1.0; for (j=1;j<=ia;j++) x *= ran1(idum); x = -log(x); } else { do { do { do { v1=2.0*ran1(idum)-1.0; v2=2.0*ran1(idum)-1.0; } while (v1*v1+v2*v2 > 1.0); y=v2/v1; am=ia-1; s=sqrt(2.0*am+1.0); x=s*y+am; } while (x <= 0.0); e=(1.0+y*y)*exp(am*log(x/am)-s*y); } while (ran1(idum) > e); } return x; }
void Pic::SG(double emittance_x, double emittance_y, double alpha_x, double alpha_y, double beta_x, double beta_y, double D0, double Ds0, double x0, double xs0, double y0, double ys0, int size, long *d){ long j; double u, v, w, s, rx, ry; double xsrms = sqrt(emittance_x*(1.0+pow(alpha_x, 2))/beta_x); double ysrms = sqrt(emittance_y*(1.0+pow(alpha_y, 2))/beta_y); double xmax = 4.*emittance_x*beta_x; double ymax = 4.*emittance_y*beta_y; for(j=size; j<pics.size(); ++j){ u = ran1(d); v = ran1(d); w = ran1(d); rx = sqrt(u*xmax); ry = sqrt(u*ymax); pics[j].x = rx * cos(2.0*PI*v) + D0*pics[j].dp + x0; pics[j].y = ry * sin(2.0*PI*v)+y0; do{ u = 2.0 * ran1(d) - 1.0; v = 2.0 * ran1(d) - 1.0; s = u*u + v*v; }while (s >= 1.0); pics[j].xs = xsrms * u * sqrt(-2.0*log(s)/s) + Ds0*pics[j].dp + xs0; pics[j].ys = ysrms * v * sqrt(-2.0*log(s)/s) + ys0; } init_old_coord(); }
// normal random variate generator using box-muller double Random::gran(double s, double m) { /* mean m, standard deviation s */ double x1, x2, w, y1; static double y2; static int use_last = 0; if (use_last) /* use value from previous call */ { y1 = y2; use_last = 0; } else { do { x1 = 2.0 * ran1() - 1.0; x2 = 2.0 * ran1() - 1.0; w = x1 * x1 + x2 * x2; } while ( w >= 1.0 ); w = sqrt( (-2.0 * log( w ) ) / w ); y1 = x1 * w; y2 = x2 * w; use_last = 1; } return( m + y1 * s ); }
int16_t awgn(awgn_state_t *s) { double fac; double r; double v1; double v2; double amp; if (s->iset == 0) { do { v1 = 2.0*ran1(s) - 1.0; v2 = 2.0*ran1(s) - 1.0; r = v1*v1 + v2*v2; } while (r >= 1.0); fac = sqrt(-2.0*log(r)/r); s->gset = v1*fac; s->iset = 1; amp = v2*fac*s->rms; } else { s->iset = 0; amp = s->gset*s->rms; } return fsaturate(amp); }
/** * Tries to flip spin i, j. If accepted, the energy and magnetization is updated * * @param i * @param j */ void IsingLattice2D::flipSpin(int i, int j, bool force) { double dE = 2*spin_matrix(i,j)*( spin_matrix(i, periodic(j+1)) + spin_matrix(i ,periodic(j-1)) + spin_matrix(periodic(i+1), j) + spin_matrix(periodic(i-1), j)); if (dE <= 0 || force) { M += -2*spin_matrix(i, j); E += dE; spin_matrix(i,j) *= -1; accepted ++; } else { // Check for acceptance; if (dE == 4) { if (ran1(&idum) <= p4) { // accept move M += -2*spin_matrix(i, j); E += dE; spin_matrix(i, j) *= -1; accepted ++; } else rejected ++; } if (dE == 8) { if (ran1(&idum) <= p8) { // Accept move M += -2*spin_matrix(i, j); E += dE; spin_matrix(i, j) *= -1; accepted ++; } else rejected ++; } } }
void IsingLattice2D::Metropolis() { for (int n = 0; n<N*N; n++) { int i = (int) (ran1(&idum)*N); int j = (int) (ran1(&idum)*N); flipSpin(i, j, false); } }
/*! * Numerical Recipes Gaussian random number generator */ double gasdev(long *idum) { static int iset = 0; static double gset; double fac, rsq, v1, v2; int i; double sum; if (0) { if (*idum<0) iset = 0; if (iset==0) { do { v1 = 2.0*ran1(idum)-1.0; v2 = 2.0*ran1(idum)-1.0; rsq = v1*v1 + v2*v2; } while ((rsq>=1.0) || (rsq==0.0)); fac = sqrt(-2.0*log(rsq)/rsq); gset = v1*fac; iset = 1; return v2*fac; } else { iset = 0; return gset; } } else { sum = 0; for (i=0; i<12; ++i) { sum += ran1(idum)-0.5; } return sum; } }
/* Next function is from numerical recipes in C. */ float randn_internal(long *idum, int reset) { static int iset=0; static float gset; float fac, rsq, v1, v2; if (reset) { iset = 0; } if (iset==0) { do { v1 = 2.0*ran1(idum, reset)-1.0; v2 = 2.0*ran1(idum, reset)-1.0; rsq = v1*v1+v2*v2; } while(rsq >= 1.0 || rsq == 0.0); fac = sqrt(-2.0*log(rsq)/rsq); gset = v1*fac; iset = 1; return v2*fac; } else { iset = 0; return gset; } }
int main(void) { float d,**a,**al,*b,*x; unsigned long i,j,*indx; long idum=(-1); a=matrix(1,7,1,4); x=vector(1,7); b=vector(1,7); al=matrix(1,7,1,2); indx=lvector(1,7); for (i=1;i<=7;i++) { x[i]=ran1(&idum); for (j=1;j<=4;j++) { a[i][j]=ran1(&idum); } } banmul(a,7,2,1,x,b); for (i=1;i<=7;i++) printf("%ld %12.6f %12.6f\n",i,b[i],x[i]); bandec(a,7,2,1,al,indx,&d); banbks(a,7,2,1,al,indx,b); for (i=1;i<=7;i++) printf("%ld %12.6f %12.6f\n",i,b[i],x[i]); free_lvector(indx,1,7); free_matrix(al,1,7,1,2); free_vector(b,1,7); free_vector(x,1,7); free_matrix(a,1,7,1,4); return 0; }
float gamdev(int ia, long *idum) { float ran1(long *idum); void nrerror(char error_text[]); int j; float am,e,s,v1,v2,x,y; if (ia < 1) nrerror("Error in routine gamdev"); if (ia < 6) { x=1.0; for (j=1;j<=ia;j++) x *= ran1(idum); x = -log(x); } else { do { do { do { v1=ran1(idum); v2=2.0*ran1(idum)-1.0; } while (v1*v1+v2*v2 > 1.0); y=v2/v1; am=ia-1; s=sqrt(2.0*am+1.0); x=s*y+am; } while (x <= 0.0); e=(1.0+y*y)*exp(am*log(x/am)-s*y); } while (ran1(idum) > e); } return x; }
void Pic::KV_xy(double emittance_x, double emittance_y, double alpha_x, double alpha_y, double beta_x, double beta_y, double D0, double Ds0, double x0, double xs0, double y0, double ys0, int size, long *d){ emittance_x *= 4.; emittance_y *= 4.; long j; double u, v, w, rx, rsx, ry, rsy; for(j=size; j<pics.size(); ++j){ u = ran1(d); v = ran1(d); w = ran1(d); rx = sqrt(u*emittance_x*beta_x); ry = sqrt((1.-u)*emittance_y*beta_y); rsx = rx/beta_x; rsy = ry/beta_y; pics[j].x = rx * cos(2.*PI*v) + D0*pics[j].dp + x0; pics[j].y = ry * cos(2.*PI*w) + y0; pics[j].xs = -rsx * (alpha_x*cos(2.*PI*v) + sin(2.*PI*v)) + Ds0*pics[j].dp + xs0; pics[j].ys = -rsy * (alpha_y*cos(2.*PI*w) + sin(2.*PI*w)) + ys0; } init_old_coord(); }
//! generate random # w/ 0 mean & 1.0 variance LIBRARY_API double gRand (long *idum) { static int iset = 0; static double gset; double fac, rsq, v1, v2; if (*idum < 0) { iset = 0; } if (iset == 0) { do { v1 = 2.0 * ran1(idum)-1.0; v2 = 2.0 * ran1(idum)-1.0; rsq = (v1 * v1) + (v2 * v2); } while (rsq >= 1.0 || rsq == 0.0); fac = sqrt(-2.0 * log(rsq) / rsq); gset = v1 * fac; iset = 1; return (v2 * fac); } else { iset = 0; return gset; } }
void Pic::Gauss_xy(double emittance_x, double emittance_y, double alpha_x, double alpha_y, double beta_x, double beta_y, double D0, double Ds0, double x0, double xs0, double y0, double ys0, int size, long *d){ long j; double u, v, w, s, rx, ry, rxs, rys; for(j=size; j<pics.size(); ++j){ do{ u = 2.0 * ran1(d) - 1.0; v = 2.0 * ran1(d) - 1.0; w = u*u + v*v; }while (w >= 1.0); rx = sqrt(emittance_x*beta_x) * u * sqrt(-2.0*log(w)/w); ry = sqrt(emittance_y*beta_y) * v * sqrt(-2.0*log(w)/w); do{ u = 2.0 * ran1(d) - 1.0; v = 2.0 * ran1(d) - 1.0; w = u*u + v*v; }while (w >= 1.0); rxs = sqrt(emittance_x/beta_x) * u * sqrt(-2.0*log(w)/w); rys = sqrt(emittance_y/beta_y) * v * sqrt(-2.0*log(w)/w); pics[j].x = rx + D0*pics[j].dp + x0; pics[j].y = ry + y0; pics[j].xs = rxs - alpha_x/beta_x*rx + Ds0*pics[j].dp + xs0; pics[j].ys = rys - alpha_y/beta_y*ry + ys0; } init_old_coord(); }
double normalRan(void) { static int iset = 0; static double gset; double fac, rsq, v1, v2; if (seed < 0) iset = 0; if (iset == 0) { do { v1 = 2.0 * ran1() - 1.0; v2 = 2.0 * ran1() - 1.0; rsq = v1*v1 + v2*v2; } while (rsq >= 1.0 || rsq == 0); fac = sqrt(-2.0 * log(rsq) / rsq); gset = v1 * fac; iset = 1; return v2 * fac; } else { iset = 0; return gset; } }
float gasdev(int *idum) { static int iset=0; static float gset; float fac, r, v1, v2; if (iset == 0) { do { v1=2.0*ran1(idum)-1.0; v2=2.0*ran1(idum)-1.0; r=v1*v1+v2*v2; } while ((r >= 1.0) || (r == 0.0)); fac=sqrt(-2.0*log(r)/r); gset=v1*fac; iset=1; return(v2*fac); } else { iset=0; return(gset); } }
/*gaussian random number generator from numerical recipes*/ float gasdev(long *idum) { float ran1(long *idum); static int iset=0; static float gset; float fac, rsq, v1, v2; if(*idum < 0) iset = 0; if(iset == 0) { do { v1 = 2.0*ran1(idum)-1.; v2 = 2.0*ran1(idum)-1.; rsq = v1*v1+v2*v2; } while(rsq >= 1. || rsq == 0.); fac = sqrt(-2.0*log(rsq)/rsq); gset = v1*fac; iset = 1; return v2*fac; } else { iset = 0; return gset; } }
/*** bnldev() ***/ int bnldev(float pp, int n, long *idum) { int j; int bnl; static int nold = (-1); float am, em, g, angle, p, sq, t, y; static float pold = (-1.0), pc, plog, pclog, en, oldg; p = (pp <= 0.5 ? pp : 1.0 - pp); am = n * p; if (n < 0) { /* original code was n < 25, but this is faster */ bnl = 0; for (j = 1; j <= n; j++) if (ran1(idum) < p) bnl++; } else if (am < 1.0) { g = exp(-am); t = 1.0; for (j = 0; j <= n; j++) { t *= ran1(idum); if (t < g) break; } bnl = (j <= n ? j : n); } else { if (n != nold) { en = n; oldg = gammln(en + 1.0); nold = n; } if (p != pold) { pc = 1.0 - p; plog = log(p); pclog = log(pc); pold = p; } sq = sqrt(2.0 * am * pc); do { do { angle = PI * ran1(idum); y = tan(angle); em = sq * y + am; } while (em < 0.0 || em >= (en + 1.0)); em = floor(em); t = 1.2 * sq * (1.0 + y * y) * exp(oldg - gammln(em + 1.0) - gammln(en - em + 1.0) + em * plog + (en - em) * pclog); } while (ran1(idum) > t); bnl = (int) em; } if (p != pp) bnl = n - bnl; return bnl; } /* bnldev() */
double suijibianli()//随机遍历 { double i=ran1(a); while(i>zhizhenjuli) { i=ran1(a); } //cout<<i<<endl;////////////// return i; }