00001 #ifndef __INCLUDE_CRANDOM_CC
00002 #define __INCLUDE_CRANDOM_CC
00003
00004 #include "random.h"
00005
00006 using namespace std;
00007
00008 CRandom::CRandom(int seed){
00009
00010
00011
00012
00013
00014 randy=gsl_rng_alloc(gsl_rng_ranlxd1);
00015
00016 gsl_rng_set(randy,seed);
00017 }
00018
00019 void CRandom::reset(int seed){
00020 gsl_rng_set(randy,seed);
00021 }
00022
00023 double CRandom::ran(void){
00024 return gsl_rng_uniform(randy);
00025 }
00026
00027 long unsigned int CRandom::iran(unsigned long int imax){
00028 return gsl_rng_uniform_int(randy,imax);
00029 }
00030
00031 double CRandom::gauss(void){
00032 return gsl_ran_ugaussian(randy);
00033 }
00034
00035 double CRandom::ran_exp(void){
00036 return -log( ran() );
00037 }
00038
00039 void CRandom::gauss2(double *randy1,double *randy2){
00040 double x,y,r2,r,c,s;
00041 TRY_AGAIN:
00042 x=1.0-2.0*gsl_rng_uniform(randy);
00043 y=1.0-2.0*gsl_rng_uniform(randy);
00044 r2=x*x+y*y;
00045 if(r2>1.0) goto TRY_AGAIN;
00046 r=sqrt(r2);
00047 c=x/r;
00048 s=y/r;
00049 *randy1=c*sqrt(-2.0*log(r2));
00050 *randy2=(s/c)**randy1;
00051 }
00052
00053 void CRandom::generate_boltzmann(double mass,double T,double *p){
00054 const double PI=4.0*atan(1.0);
00055 double r1,r2,r3;
00056 double pmag,ctheta,stheta,phi,pgauss;
00057 const double NONRELCUTOFF=0.08;
00058 if(T/mass> NONRELCUTOFF){
00059 GB_TRYAGAIN:
00060 r1=ran();
00061 r2=ran();
00062 r3=ran();
00063 pmag=-T*log(r1*r2*r3);
00064 p[0]=sqrt(pmag*pmag+mass*mass);
00065 if(ran()>exp((pmag-p[0])/T)) goto GB_TRYAGAIN;
00066 ctheta=log(r1/r2)/log(r1*r2);
00067 stheta=sqrt(1.0-ctheta*ctheta);
00068 phi=T*T*pow(log(r1*r2),2)/(pmag*pmag);
00069 if(phi>1.0){
00070 printf("phi=%g, out of range\n",phi);
00071 exit(1);
00072 }
00073 phi=2.0*PI*phi;
00074 p[3]=pmag*ctheta;
00075 p[1]=pmag*stheta*cos(phi);
00076 p[2]=pmag*stheta*sin(phi);
00077 }
00078 else{
00079 pgauss=sqrt(mass*T);
00080 gauss2(&p[1],&p[2]);
00081 p[1]*=pgauss;
00082 p[2]*=pgauss;
00083 p[3]=pgauss*gauss();
00084 p[0]=sqrt(mass*mass+p[1]*p[1]+p[2]*p[2]+p[3]*p[3]);
00085 }
00086 }
00087
00088
00089 #endif