コード例 #1
0
ファイル: MC_Integration.cpp プロジェクト: rfok/MCMC
void MC_Integration::Integrate(vector<double> constants){
    size_t calls = numOfCalls;
    int dimension = (int) dim; //setting integral dimension
    int numOfConstants = constants.size();

	double a[numOfConstants];	
    double xl[dimension];
    double xu[dimension];
	for (int i = 0; i < numOfConstants; i++){ 
		a[i] = constants.at(i);
	}
	for (int i = 0; i < dimension; i++){ 
		xl[i] = lowerLimits.at(i);
		xu[i] = upperLimits.at(i);
	}
	
    struct paramstruct params = {a};
    
    gsl_monte_function G = {&Integrand,dim,&params};
 
    gsl_monte_vegas_state *s = gsl_monte_vegas_alloc (dim);
    gsl_monte_vegas_integrate (&G, xl, xu, dim, calls, r, s, &result, &error); //vegas warm up

    do{
    	gsl_monte_vegas_integrate (&G, xl, xu, dim, calls*10, r, s, &result, &error);
    }while (fabs (gsl_monte_vegas_chisq (s) - 1.0) > 0.5);
    
    if(fabs (gsl_monte_vegas_chisq (s) - 1.0) > 0.5) cout << "Chi-squared larger too far away from 1. Use more calls" << endl;
    gsl_monte_vegas_free (s);
    gsl_rng_free (r);
}
コード例 #2
0
int main (int argc, char *argv[])
{
    double psr;

    //////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////

    // number of potentially observable pulsars 
    double res_x,err_x;

    double xl_x[3]={0.0,0.0,-3.1415926/2.0};
    double xu_x[3]={13.0,2.0*3.1415926,3.1415926/2.0};

    const gsl_rng_type *T_x;
    gsl_rng *r_x;

    gsl_monte_function G_x={&g_psr,3,0};

    size_t calls_x=5000000;

    T_x=gsl_rng_default;

    // calculation
    
    r_x=gsl_rng_alloc(T_x);
        
    gsl_monte_vegas_state *s_x = gsl_monte_vegas_alloc (3);
    gsl_monte_vegas_integrate (&G_x, xl_x, xu_x, 3, 10000, r_x, s_x,&res_x, &err_x);

    do
    {
        gsl_monte_vegas_integrate (&G_x, xl_x, xu_x, 3, calls_x/5, r_x, s_x,&res_x, &err_x);
    }
    while (fabs (gsl_monte_vegas_chisq (s_x) - 1.0) > 0.5);

    /////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////

    psr = res_x;
    printf ("psr event rate: %e\n", psr);

    gsl_monte_vegas_free (s_x);

    gsl_rng_free (r_x);

    return 0;
}
コード例 #3
0
ファイル: calculate.c プロジェクト: shidai/ptime_jitter
double error (double x, void *p, size_t N)
// calculate the error of chi, then use it to derive the error of jitter parameter
{
	//printf ("Integrating...\n");
	struct my_params *fp=(struct my_params *)p;
	//printf ("error: %lf %lf\n", fp->a, fp->b);

	double F;
    double res,err;

    double xl[1]={0.0};
    double xu[1]={x};

    const gsl_rng_type *T;
    gsl_rng *r;

    gsl_monte_function G={&chi_distribution,1,fp};

    size_t calls = N;
    gsl_rng_env_setup ();

    T=gsl_rng_default;

    r = gsl_rng_alloc (T);

    gsl_monte_vegas_state *s = gsl_monte_vegas_alloc (1);
    gsl_monte_vegas_integrate (&G, xl, xu, 1, 10000, r, s, &res, &err);
	//printf ("result0: %lf %lf\n", res, err);

    do
    {
        gsl_monte_vegas_integrate (&G, xl, xu, 1, calls/5, r, s, &res, &err);
    }
    while (fabs (gsl_monte_vegas_chisq (s) - 1.0) > 0.5);

	//printf ("result of integration is: %e\n", res);

    gsl_monte_vegas_free (s);

    gsl_rng_free (r);

	F=res-0.682; // 1 sigma
	//F=res-0.9;

    return F;
}
コード例 #4
0
ファイル: vegas-bench.c プロジェクト: LuaDist2/lgsl
int
main (void)
   {
    double res, err;
    double a= 0.;
    double b= 2.;
    int dim=9;
    double xl[9] = { a,a,a,a,a,a,a,a,a};
    double xu[9] = { b,b,b,b,b,b,b,b,b};
    gsl_monte_function G = { &f, dim, 0 };
    size_t calls =1e6*dim;

    gsl_rng_env_setup ();
    gsl_rng *r = gsl_rng_alloc (gsl_rng_taus2);
    gsl_rng_set (r, 30776);
    
    gsl_monte_vegas_state *s = gsl_monte_vegas_alloc (dim);

    gsl_monte_vegas_integrate (&G, xl, xu, dim, 1e4, r, s,
			    &res, &err);
    //display_results ("vegas warm-up", res, err,0);

    //printf ("converging...\n");
    int i=0;
    do
    {
      gsl_monte_vegas_integrate (&G, xl, xu, dim, calls/5, r, s,
				&res, &err);
      //printf ("result = % .6f sigma = % .6f chisq/dof = %.1f\n",
      //	    res, err, gsl_monte_vegas_chisq (s));
      i=i+1;
    }
    while (fabs (gsl_monte_vegas_chisq (s) - 1.0) > 0.5);

    display_results ("vegas final", res, err, i);

    gsl_monte_vegas_free (s);
    return 0;
    }
コード例 #5
0
ファイル: v_beta-1.c プロジェクト: shidai/microlensing-pulsar
int main (int argc, char *argv[])
{
    double timescale,re,event;

    // neutron stars, pulsar to bulge
    double res_x,err_x;

    //double xl_x[4]={3.0,0.0,-1200.0,-1200.0};
    //double xu_x[4]={13.0,13.0,1200.0,1200.0};
    double xl_x[8]={4.5,0.0,-2000.0,-2000.0,-2000.0,-550.0,-450.0,-350.0};
    double xu_x[8]={11.5,11.5,2000.0,2000.0,2000.0,550.0,450.0,350.0};

    const gsl_rng_type *T_x;
    gsl_rng *r_x;

    gsl_monte_function G_x={&g_psr,8,0};

    size_t calls_x=50000000;

    gsl_rng_env_setup ();

    T_x=gsl_rng_default;

    // neutron stars, pulsar to disk 
    double res_xd,err_xd;

    //double xl_x[4]={3.0,0.0,-2000.0,-2000.0};
    //double xu_x[4]={13.0,13.0,2000.0,2000.0};
    double xl_xd[8]={0.0,0.0,-2000.0,-2000.0,-2000.0,-100.0,-150.0,-100.0};
    double xu_xd[8]={11.5,11.5,2000.0,2000.0,2000.0,100.0,150.0,100.0};

    const gsl_rng_type *T_xd;
    gsl_rng *r_xd;

    gsl_monte_function G_xd={&g_psr_disk,8,0};

    size_t calls_xd=50000000;

    T_xd=gsl_rng_default;
    
    ////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////////////

    r_x=gsl_rng_alloc(T_x);
        
    gsl_monte_vegas_state *s_x = gsl_monte_vegas_alloc (8);

    r_xd=gsl_rng_alloc(T_xd);
        
    gsl_monte_vegas_state *s_xd = gsl_monte_vegas_alloc (8);

    //////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////

    // timescale, re/v

    // neutron stars, pulsar to bulge
    double res_xr,err_xr;

    //double xl_x[4]={3.0,0.0,-1200.0,-1200.0};
    //double xu_x[4]={13.0,13.0,1200.0,1200.0};
    double xl_xr[8]={4.5,0.0,-2000.0,-2000.0,-2000.0,-550.0,-450.0,-350.0};
    double xu_xr[8]={11.5,11.5,2000.0,2000.0,2000.0,550.0,450.0,350.0};

    const gsl_rng_type *T_xr;
    gsl_rng *r_xr;

    gsl_monte_function G_xr={&g_psrr,8,0};

    size_t calls_xr=50000000;

    T_xr=gsl_rng_default;

    // neutron stars, pulsar to disk 
    double res_xdr,err_xdr;

    //double xl_x[4]={3.0,0.0,-2000.0,-2000.0};
    //double xu_x[4]={13.0,13.0,2000.0,2000.0};
    double xl_xdr[8]={0.0,0.0,-2000.0,-2000.0,-2000.0,-100.0,-150.0,-100.0};
    double xu_xdr[8]={11.5,11.5,2000.0,2000.0,2000.0,100.0,150.0,100.0};

    const gsl_rng_type *T_xdr;
    gsl_rng *r_xdr;

    gsl_monte_function G_xdr={&g_psr_diskr,8,0};

    size_t calls_xdr=50000000;

    T_xdr=gsl_rng_default;
    
    ////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////////////

    r_xr=gsl_rng_alloc(T_xr);
        
    gsl_monte_vegas_state *s_xr = gsl_monte_vegas_alloc (8);

    r_xdr=gsl_rng_alloc(T_xdr);
        
    gsl_monte_vegas_state *s_xdr = gsl_monte_vegas_alloc (8);

    //////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////
    // calculation
    
        gsl_monte_vegas_integrate (&G_x, xl_x, xu_x, 8, 10000, r_x, s_x,&res_x, &err_x);

        do
        {
            gsl_monte_vegas_integrate (&G_x, xl_x, xu_x, 8, calls_x/5, r_x, s_x,&res_x, &err_x);
        }
        while (fabs (gsl_monte_vegas_chisq (s_x) - 1.0) > 0.5);

    /////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////

        gsl_monte_vegas_integrate (&G_xd, xl_xd, xu_xd, 8, 10000, r_xd, s_xd, &res_xd, &err_xd);

        do
        {
            gsl_monte_vegas_integrate (&G_xd, xl_xd, xu_xd, 8, calls_xd/5, r_xd, s_xd, &res_xd, &err_xd);
        }
        while (fabs (gsl_monte_vegas_chisq (s_xd) - 1.0) > 0.5);

        //////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////

	// timescale, re/v

        gsl_monte_vegas_integrate (&G_xr, xl_xr, xu_xr, 8, 10000, r_xr, s_xr, &res_xr, &err_xr);

        do
        {
            gsl_monte_vegas_integrate (&G_xr, xl_xr, xu_xr, 8, calls_xr/5, r_xr, s_xr,&res_xr, &err_xr);
        }
        while (fabs (gsl_monte_vegas_chisq (s_xr) - 1.0) > 0.5);

    /////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////

        gsl_monte_vegas_integrate (&G_xdr, xl_xdr, xu_xdr, 8, 10000, r_xdr, s_xdr, &res_xdr, &err_xdr);

        do
        {
            gsl_monte_vegas_integrate (&G_xdr, xl_xdr, xu_xdr, 8, calls_xdr/5, r_xdr, s_xdr, &res_xdr, &err_xdr);
        }
        while (fabs (gsl_monte_vegas_chisq (s_xdr) - 1.0) > 0.5);

        //////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////

        re = res_xr+res_xdr;
        event = res_x+res_xd;
        timescale = re/event;
        printf ("%e %e %e\n", l, b, timescale);

    gsl_monte_vegas_free (s_x);

    gsl_rng_free (r_x);

    gsl_monte_vegas_free (s_xd);

    gsl_rng_free (r_xd);
     
    /////////////////////////////////////////////////////////////////////////////////////////////////

    gsl_monte_vegas_free (s_xr);

    gsl_rng_free (r_xr);

    gsl_monte_vegas_free (s_xdr);

    gsl_rng_free (r_xdr);
     
    return 0;
}
コード例 #6
0
int main (int argc, char *argv[])
{
    double ratio,ns;
    
    // neutron stars, pulsar to bulge
    double res_x,err_x;

    double xl_x[8]={4.5,0.0,-2000.0,-2000.0,-2000.0,-550.0,-450.0,-350.0};
    double xu_x[8]={11.5,11.5,2000.0,2000.0,2000.0,550.0,450.0,350.0};

    const gsl_rng_type *T_x;
    gsl_rng *r_x;

    gsl_monte_function G_x={&g_psr,8,0};

    size_t calls_x=5000000;

    gsl_rng_env_setup ();

    T_x=gsl_rng_default;

    // neutron stars, pulsar to disk 
    double res_xd,err_xd;

    double xl_xd[8]={0.0,0.0,-2000.0,-2000.0,-2000.0,-100.0,-150.0,-100.0};
    double xu_xd[8]={11.5,11.5,2000.0,2000.0,2000.0,100.0,150.0,100.0};

    const gsl_rng_type *T_xd;
    gsl_rng *r_xd;

    gsl_monte_function G_xd={&g_psr_disk,8,0};

    size_t calls_xd=5000000;

    T_xd=gsl_rng_default;
    
    ////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////////////

    // source bulge
    double res_s,err_s;

    double xl_s[1]={4.5};
    double xu_s[1]={11.5};

    const gsl_rng_type *T_s;
    gsl_rng *r_s;

    gsl_monte_function G_s={&source,1,0};

    size_t calls_s=5000000;

    T_s=gsl_rng_default;

    r_s = gsl_rng_alloc (T_s);

    gsl_monte_vegas_state *s_s = gsl_monte_vegas_alloc (1);
    gsl_monte_vegas_integrate (&G_s, xl_s, xu_s, 1, 10000, r_s, s_s, &res_s, &err_s);

    do
    {
        gsl_monte_vegas_integrate (&G_s, xl_s, xu_s, 1, calls_s/5, r_s, s_s, &res_s, &err_s);
    }
    while (fabs (gsl_monte_vegas_chisq (s_s) - 1.0) > 0.5);

    gsl_monte_vegas_free (s_s);

    gsl_rng_free (r_s);
    
    //////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////

    // source disk
    double res_sd,err_sd;

    double xl_sd[1]={0.0};
    double xu_sd[1]={11.5};

    const gsl_rng_type *T_sd;
    gsl_rng *r_sd;

    gsl_monte_function G_sd={&source_d,1,0};

    size_t calls_sd=5000000;

    T_sd=gsl_rng_default;

    r_sd = gsl_rng_alloc (T_sd);

    gsl_monte_vegas_state *s_sd = gsl_monte_vegas_alloc (1);
    gsl_monte_vegas_integrate (&G_sd, xl_sd, xu_sd, 1, 10000, r_sd, s_sd, &res_sd, &err_sd);

    do
    {
        gsl_monte_vegas_integrate (&G_sd, xl_sd, xu_sd, 1, calls_sd/5, r_sd, s_sd, &res_sd, &err_sd);
    }
    while (fabs (gsl_monte_vegas_chisq (s_sd) - 1.0) > 0.5);

    gsl_monte_vegas_free (s_sd);

    gsl_rng_free (r_sd);
    
    //////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////


    r_x=gsl_rng_alloc(T_x);

    gsl_monte_vegas_state *s_x = gsl_monte_vegas_alloc (8);

    r_xd=gsl_rng_alloc(T_xd);
        
    gsl_monte_vegas_state *s_xd = gsl_monte_vegas_alloc (8);

    // calculation
    
        gsl_monte_vegas_integrate (&G_x, xl_x, xu_x, 8, 10000, r_x, s_x,&res_x, &err_x);

        do
        {
            gsl_monte_vegas_integrate (&G_x, xl_x, xu_x, 8, calls_x/5, r_x, s_x,&res_x, &err_x);
        }
        while (fabs (gsl_monte_vegas_chisq (s_x) - 1.0) > 0.5);

    /////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////

        gsl_monte_vegas_integrate (&G_xd, xl_xd, xu_xd, 8, 10000, r_xd, s_xd, &res_xd, &err_xd);

        do
        {
            gsl_monte_vegas_integrate (&G_xd, xl_xd, xu_xd, 8, calls_xd/5, r_xd, s_xd, &res_xd, &err_xd);
        }
        while (fabs (gsl_monte_vegas_chisq (s_xd) - 1.0) > 0.5);

        //////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////

        ns = res_x+res_xd;
        ratio = ns/(res_s+res_sd);
        printf ("%e\n", ratio);

    gsl_monte_vegas_free (s_x);

    gsl_rng_free (r_x);

    gsl_monte_vegas_free (s_xd);

    gsl_rng_free (r_xd);

    return 0;
}
コード例 #7
0
int main (int argc, char *argv[])
{
    double psr;

    // source
    double res_s,err_s;

    double xl_s[3]={4.5,0.0,-3.1415926/2.0};
    double xu_s[3]={11.5,2.0*3.1415926,3.1415926/2.0};

    const gsl_rng_type *T_s;
    gsl_rng *r_s;

    gsl_monte_function G_s={&source,3,0};

    size_t calls_s=50000000;

    gsl_rng_env_setup ();

    T_s=gsl_rng_default;

    r_s = gsl_rng_alloc (T_s);

    gsl_monte_vegas_state *s_s = gsl_monte_vegas_alloc (3);
    gsl_monte_vegas_integrate (&G_s, xl_s, xu_s, 3, 10000, r_s, s_s, &res_s, &err_s);

    do
    {
        gsl_monte_vegas_integrate (&G_s, xl_s, xu_s, 3, calls_s/5, r_s, s_s, &res_s, &err_s);
    }
    while (fabs (gsl_monte_vegas_chisq (s_s) - 1.0) > 0.5);

    gsl_monte_vegas_free (s_s);

    gsl_rng_free (r_s);
    
    //////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////

    // neutron stars, pulsar to disk
    double res_x,err_x;

    //double xl_x[4]={3.0,0.0,-1200.0,-1200.0};
    //double xu_x[4]={13.0,13.0,1200.0,1200.0};
    double xl_x[10]={4.5,0.01,-2000.0,-2000.0,-2000.0,-550.0,-450.0,-350.0,0.0,-3.1415926/2.0};
    double xu_x[10]={11.5,8.0,2000.0,2000.0,2000.0,550.0,450.0,350.0,2.0*3.1415926,3.1415926/2.0};

    const gsl_rng_type *T_x;
    gsl_rng *r_x;

    gsl_monte_function G_x={&g_psr,10,0};

    size_t calls_x=50000000;

    T_x=gsl_rng_default;

    // calculation
    
    r_x=gsl_rng_alloc(T_x);
        
    gsl_monte_vegas_state *s_x = gsl_monte_vegas_alloc (10);
    gsl_monte_vegas_integrate (&G_x, xl_x, xu_x, 10, 10000, r_x, s_x,&res_x, &err_x);

    do
    {
        gsl_monte_vegas_integrate (&G_x, xl_x, xu_x, 10, calls_x/5, r_x, s_x,&res_x, &err_x);
    }
    while (fabs (gsl_monte_vegas_chisq (s_x) - 1.0) > 0.5);

    /////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////

    psr = fabs(res_x)/fabs(res_s);
    printf ("psr event rate:%e %e %e\n", res_x, res_s, psr);
    //printf ("psr event rate: %e\n", psr);

    gsl_monte_vegas_free (s_x);

    gsl_rng_free (r_x);

    return 0;
}
コード例 #8
0
ファイル: massfunc.c プロジェクト: shidai/microlensing_old
int main (void)
{
    //double bd,ms,wd,ns,bh;
    
    // mfunc
    double res,err;

    double xl[1]={0.03};
    double xu[1]={120.0};

    const gsl_rng_type *T;
    gsl_rng *r;

    gsl_monte_function G={&g_x,1,0};

    size_t calls=5000000;

    gsl_rng_env_setup ();

    T=gsl_rng_default;

    // mfunc: ns
    double resn,errn;

    double xln[1]={8.0};
    double xun[1]={40.0};

    const gsl_rng_type *Tn;
    gsl_rng *rn;

    gsl_monte_function Gn={&g,1,0};
    //gsl_monte_function Gn={&g_x,1,0};

    size_t callsn=5000000;

    Tn=gsl_rng_default;

    //////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////

    // calculation mfunc
    r = gsl_rng_alloc (T);

    gsl_monte_vegas_state *s = gsl_monte_vegas_alloc (1);
    gsl_monte_vegas_integrate (&G, xl, xu, 1, 10000, r, s, &res, &err);

    do
    {
        gsl_monte_vegas_integrate (&G, xl, xu, 1, calls/5, r, s,&res, &err);
    }
    while (fabs (gsl_monte_vegas_chisq (s) - 1.0) > 0.5);

    //////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////

    rn = gsl_rng_alloc (Tn);

    gsl_monte_vegas_state *sn = gsl_monte_vegas_alloc (1);
    gsl_monte_vegas_integrate (&Gn, xln, xun, 1, 10000, rn, sn, &resn, &errn);

    do
    {
        gsl_monte_vegas_integrate (&Gn, xln, xun, 1, callsn/5, rn, sn, &resn, &errn);
    }
    while (fabs (gsl_monte_vegas_chisq (sn) - 1.0) > 0.5);

    //////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////

    //ns = resn/res;
    printf ("resn: %e\n", resn);
    printf ("res: %e\n", res);
    fflush(stdout);        

    gsl_monte_vegas_free (s);

    gsl_rng_free (r);

    gsl_monte_vegas_free (sn);

    gsl_rng_free (rn);
    
    return 0;
}
コード例 #9
0
int main (int argc, char *argv[])
{
    double ns;
    int i;
    int id;  //  process rank
    int p;   //  number of processes

    MPI_Init (&argc, &argv);
    MPI_Comm_rank (MPI_COMM_WORLD, &id);
    MPI_Comm_size (MPI_COMM_WORLD, &p);
    
    // neutron stars, pulsar to bulge
    double res_x,err_x;

    //double xl_x[4]={3.0,0.0,-1200.0,-1200.0};
    //double xu_x[4]={13.0,13.0,1200.0,1200.0};
    double xl_x[8]={3.0,0.0,-2000.0,-2000.0,-2000.0,-550.0,-450.0,-350.0};
    double xu_x[8]={13.0,13.0,2000.0,2000.0,2000.0,550.0,450.0,350.0};

    const gsl_rng_type *T_x;
    gsl_rng *r_x;

    gsl_monte_function G_x={&g_psr,8,0};

    size_t calls_x=500000000;

    T_x=gsl_rng_default;

    // neutron stars, pulsar to disk 
    double res_xd,err_xd;

    //double xl_x[4]={3.0,0.0,-2000.0,-2000.0};
    //double xu_x[4]={13.0,13.0,2000.0,2000.0};
    double xl_xd[8]={0.0,0.0,-2000.0,-2000.0,-2000.0,-100.0,-150.0,-100.0};
    double xu_xd[8]={13.0,13.0,2000.0,2000.0,2000.0,100.0,150.0,100.0};

    const gsl_rng_type *T_xd;
    gsl_rng *r_xd;

    gsl_monte_function G_xd={&g_psr_disk,8,0};

    size_t calls_xd=500000000;

    T_xd=gsl_rng_default;
    
    ////////////////////////////////////////////////////////////////////////////////////////////
    // source
    double res_s,err_s;

    double xl_s[1]={3.0};
    double xu_s[1]={13.0};

    const gsl_rng_type *T_s;
    gsl_rng *r_s;

    gsl_monte_function G_s={&source,1,0};

    size_t calls_s=500000000;

    T_s=gsl_rng_default;

    r_s = gsl_rng_alloc (T_s);

    gsl_monte_vegas_state *s_s = gsl_monte_vegas_alloc (1);
    gsl_monte_vegas_integrate (&G_s, xl_s, xu_s, 1, 10000, r_s, s_s, &res_s, &err_s);

    do
    {
        gsl_monte_vegas_integrate (&G_s, xl_s, xu_s, 1, calls_s/5, r_s, s_s, &res_s, &err_s);
    }
    while (fabs (gsl_monte_vegas_chisq (s_s) - 1.0) > 0.5);

    gsl_monte_vegas_free (s_s);

    gsl_rng_free (r_s);
    
    //////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////

    // source disk
    double res_sd,err_sd;

    double xl_sd[1]={0.0};
    double xu_sd[1]={13.0};

    const gsl_rng_type *T_sd;
    gsl_rng *r_sd;

    gsl_monte_function G_sd={&source_disk,1,0};

    size_t calls_sd=500000000;

    T_sd=gsl_rng_default;

    r_sd = gsl_rng_alloc (T_sd);

    gsl_monte_vegas_state *s_sd = gsl_monte_vegas_alloc (1);
    gsl_monte_vegas_integrate (&G_sd, xl_sd, xu_sd, 1, 10000, r_sd, s_sd, &res_sd, &err_sd);

    do
    {
        gsl_monte_vegas_integrate (&G_sd, xl_sd, xu_sd, 1, calls_sd/5, r_sd, s_sd, &res_sd, &err_sd);
    }
    while (fabs (gsl_monte_vegas_chisq (s_sd) - 1.0) > 0.5);

    gsl_monte_vegas_free (s_sd);

    gsl_rng_free (r_sd);
    

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    r_x=gsl_rng_alloc(T_x);
        
    gsl_monte_vegas_state *s_x = gsl_monte_vegas_alloc (8);

    r_xd=gsl_rng_alloc(T_xd);
        
    gsl_monte_vegas_state *s_xd = gsl_monte_vegas_alloc (8);

    // calculation
    for (i=id;i<=200;i+=p)
    {
        //t=pow(10.0,0.015*i);    
        t=-0.5+0.0175*i; 
    
        gsl_monte_vegas_integrate (&G_x, xl_x, xu_x, 8, 10000, r_x, s_x,&res_x, &err_x);

        do
        {
            gsl_monte_vegas_integrate (&G_x, xl_x, xu_x, 8, calls_x/5, r_x, s_x,&res_x, &err_x);
        }
        while (fabs (gsl_monte_vegas_chisq (s_x) - 1.0) > 0.5);

    /////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////

        gsl_monte_vegas_integrate (&G_xd, xl_xd, xu_xd, 8, 10000, r_xd, s_xd, &res_xd, &err_xd);

        // display_results ("vegas warm-up", res, err);
        // printf ("converging...\n");
        do
        {
            gsl_monte_vegas_integrate (&G_xd, xl_xd, xu_xd, 8, calls_xd/5, r_xd, s_xd, &res_xd, &err_xd);
          //printf ("result = % .6ef sigma = % .6ef chisq/dof = %.1f\n", res, err, gsl_monte_vegas_chisq (s));
        }
        while (fabs (gsl_monte_vegas_chisq (s_xd) - 1.0) > 0.5);
        // display_results ("vegas final", res, err);

        //////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////

        ns = (fabs(res_x)+fabs(res_xd))/(fabs(res_s)+fabs(res_sd));
        printf ("%e %e\n", t, ns);
        fflush(stdout);        

    }

    gsl_monte_vegas_free (s_x);

    gsl_rng_free (r_x);

    gsl_monte_vegas_free (s_xd);

    gsl_rng_free (r_xd);

    fflush (stdout);
    MPI_Finalize ();
    return 0;
}
コード例 #10
0
ファイル: random_walk.c プロジェクト: saintfrank/inte
 int
 main (void)
 {
   double res, err;
 
   double xl[3] = { 0, 0, 0 };
   double xu[3] = { M_PI, M_PI, M_PI };
 
   const gsl_rng_type *T;
   gsl_rng *r;
 
   gsl_monte_function G = { &g, 3, 0 };
 
   size_t calls = 500000;
 
   gsl_rng_env_setup ();
 
   T = gsl_rng_default;
   r = gsl_rng_alloc (T);
 
   {
     gsl_monte_plain_state *s = gsl_monte_plain_alloc (3);
     gsl_monte_plain_integrate (&G, xl, xu, 3, calls, r, s, 
                                &res, &err);
     gsl_monte_plain_free (s);
 
     display_results ("plain", res, err);
   }
 
   {
     gsl_monte_miser_state *s = gsl_monte_miser_alloc (3);
     gsl_monte_miser_integrate (&G, xl, xu, 3, calls, r, s,
                                &res, &err);
     gsl_monte_miser_free (s);
 
     display_results ("miser", res, err);
   }
 
   {
     gsl_monte_vegas_state *s = gsl_monte_vegas_alloc (3);
 
     gsl_monte_vegas_integrate (&G, xl, xu, 3, 10000, r, s,
                                &res, &err);
     display_results ("vegas warm-up", res, err);
 
     printf ("converging...\n");
 
     do
       {
         gsl_monte_vegas_integrate (&G, xl, xu, 3, calls/5, r, s,
                                    &res, &err);
         printf ("result = % .6f sigma = % .6f "
                 "chisq/dof = %.1f\n", res, err, gsl_monte_vegas_chisq (s));
       }
     while (fabs (gsl_monte_vegas_chisq (s) - 1.0) > 0.5);
 
     display_results ("vegas final", res, err);
 
     gsl_monte_vegas_free (s);
   }
 
   gsl_rng_free (r);
 
   return 0;
 }
コード例 #11
0
int main (void)
{
    double bd,ms,wd,ns,bh;
    
    // mfunc
    double res,err;

    double xl[1]={0.03};
    double xu[1]={120.0};

    const gsl_rng_type *T;
    gsl_rng *r;

    gsl_monte_function G={&g_x,1,0};

    size_t calls=50000000;

    gsl_rng_env_setup ();

    T=gsl_rng_default;

    // mfunc: ns
    double resn,errn;

    double xln[1]={8.0};
    double xun[1]={40.0};

    const gsl_rng_type *Tn;
    gsl_rng *rn;

    gsl_monte_function Gn={&g_x,1,0};

    size_t callsn=50000000;

    Tn=gsl_rng_default;

    // mfunc: bd
    double resbd,errbd;

    double xlbd[1]={0.03};
    double xubd[1]={0.08};

    const gsl_rng_type *Tbd;
    gsl_rng *rbd;

    gsl_monte_function Gbd={&g_x,1,0};

    size_t callsbd=50000000;

    Tbd=gsl_rng_default;

    // mfunc: ms
    double resms,errms;

    double xlms[1]={0.08};
    double xums[1]={1.0};

    const gsl_rng_type *Tms;
    gsl_rng *rms;

    gsl_monte_function Gms={&g_x,1,0};

    size_t callsms=50000000;

    Tms=gsl_rng_default;

    // mfunc: wd
    double reswd,errwd;

    double xlwd[1]={1.0};
    double xuwd[1]={8.0};

    const gsl_rng_type *Twd;
    gsl_rng *rwd;

    gsl_monte_function Gwd={&g_x,1,0};

    size_t callswd=50000000;

    Twd=gsl_rng_default;

    // mfunc: bh
    double resbh,errbh;

    double xlbh[1]={40.0};
    double xubh[1]={120.0};

    const gsl_rng_type *Tbh;
    gsl_rng *rbh;

    gsl_monte_function Gbh={&g_x,1,0};

    size_t callsbh=50000000;

    Tbh=gsl_rng_default;

    //////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////

    // calculation mfunc
    r = gsl_rng_alloc (T);

    gsl_monte_vegas_state *s = gsl_monte_vegas_alloc (1);
    gsl_monte_vegas_integrate (&G, xl, xu, 1, 10000, r, s, &res, &err);

    do
    {
        gsl_monte_vegas_integrate (&G, xl, xu, 1, calls/5, r, s,&res, &err);
    }
    while (fabs (gsl_monte_vegas_chisq (s) - 1.0) > 0.5);

    //////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////

    rn = gsl_rng_alloc (Tn);

    gsl_monte_vegas_state *sn = gsl_monte_vegas_alloc (1);
    gsl_monte_vegas_integrate (&Gn, xln, xun, 1, 10000, rn, sn, &resn, &errn);

    do
    {
        gsl_monte_vegas_integrate (&Gn, xln, xun, 1, callsn/5, rn, sn, &resn, &errn);
    }
    while (fabs (gsl_monte_vegas_chisq (sn) - 1.0) > 0.5);

    //////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////

    rbd = gsl_rng_alloc (Tbd);

    gsl_monte_vegas_state *sbd = gsl_monte_vegas_alloc (1);
    gsl_monte_vegas_integrate (&Gbd, xlbd, xubd, 1, 10000, rbd, sbd, &resbd, &errbd);

    do
    {
        gsl_monte_vegas_integrate (&Gbd, xlbd, xubd, 1, callsbd/5, rbd, sbd, &resbd, &errbd);
    }
    while (fabs (gsl_monte_vegas_chisq (sbd) - 1.0) > 0.5);

    //////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////

    rms = gsl_rng_alloc (Tms);

    gsl_monte_vegas_state *sms = gsl_monte_vegas_alloc (1);
    gsl_monte_vegas_integrate (&Gms, xlms, xums, 1, 10000, rms, sms, &resms, &errms);

    do
    {
        gsl_monte_vegas_integrate (&Gms, xlms, xums, 1, callsms/5, rms, sms, &resms, &errms);
    }
    while (fabs (gsl_monte_vegas_chisq (sms) - 1.0) > 0.5);

    //////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////

    rwd = gsl_rng_alloc (Twd);

    gsl_monte_vegas_state *swd = gsl_monte_vegas_alloc (1);
    gsl_monte_vegas_integrate (&Gwd, xlwd, xuwd, 1, 10000, rwd, swd, &reswd, &errwd);

    do
    {
        gsl_monte_vegas_integrate (&Gwd, xlwd, xuwd, 1, callswd/5, rwd, swd, &reswd, &errwd);
    }
    while (fabs (gsl_monte_vegas_chisq (swd) - 1.0) > 0.5);

    //////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////

    rbh = gsl_rng_alloc (Tbh);

    gsl_monte_vegas_state *sbh = gsl_monte_vegas_alloc (1);
    gsl_monte_vegas_integrate (&Gbh, xlbh, xubh, 1, 10000, rbh, sbh, &resbh, &errbh);

    do
    {
        gsl_monte_vegas_integrate (&Gbh, xlbh, xubh, 1, callsbh/5, rbh, sbh, &resbh, &errbh);
    }
    while (fabs (gsl_monte_vegas_chisq (sbh) - 1.0) > 0.5);

    //////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////

    ns = resn/res;
    bd = resbd/res;
    ms = resms/res;
    wd = reswd/res;
    bh = resbh/res;
    printf ("ns: %e\n", ns);
    printf ("bd: %e\n", bd);
    printf ("ms: %e\n", ms);
    printf ("wd: %e\n", wd);
    printf ("bh: %e\n", bh);
    fflush(stdout);        

    gsl_monte_vegas_free (s);

    gsl_rng_free (r);

    gsl_monte_vegas_free (sn);

    gsl_rng_free (rn);
    
    gsl_monte_vegas_free (sbd);

    gsl_rng_free (rbd);
    
    gsl_monte_vegas_free (sms);

    gsl_rng_free (rms);
    
    gsl_monte_vegas_free (swd);

    gsl_rng_free (rwd);
    
    gsl_monte_vegas_free (sbh);

    gsl_rng_free (rbh);

    return 0;
}
コード例 #12
0
int main (int argc, char *argv[])
{
    double psr;

    // source bulge
    double res_s,err_s;

    double xl_s[1]={4.5};
    double xu_s[1]={11.5};

    const gsl_rng_type *T_s;
    gsl_rng *r_s;

    gsl_monte_function G_s={&source,1,0};

    size_t calls_s=50000000;

    gsl_rng_env_setup ();

    T_s=gsl_rng_default;

    r_s = gsl_rng_alloc (T_s);

    gsl_monte_vegas_state *s_s = gsl_monte_vegas_alloc (1);
    gsl_monte_vegas_integrate (&G_s, xl_s, xu_s, 1, 10000, r_s, s_s, &res_s, &err_s);

    do
    {
        gsl_monte_vegas_integrate (&G_s, xl_s, xu_s, 1, calls_s/5, r_s, s_s, &res_s, &err_s);
    }
    while (fabs (gsl_monte_vegas_chisq (s_s) - 1.0) > 0.5);

    gsl_monte_vegas_free (s_s);

    gsl_rng_free (r_s);
    
    //////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////

    // neutron stars, pulsar to bulge 
    double res_x,err_x;

    //double xl_x[4]={3.0,0.0,-1200.0,-1200.0};
    //double xu_x[4]={13.0,13.0,1200.0,1200.0};
    double xl_x[8]={4.5,0.01,-2000.0,-2000.0,-2000.0,-550.0,-450.0,-350.0};
    double xu_x[8]={11.5,8.0,2000.0,2000.0,2000.0,550.0,450.0,350.0};

    const gsl_rng_type *T_x;
    gsl_rng *r_x;

    gsl_monte_function G_x={&g_psr,8,0};

    size_t calls_x=50000000;

    T_x=gsl_rng_default;

    // calculation
    
    r_x=gsl_rng_alloc(T_x);
        
    gsl_monte_vegas_state *s_x = gsl_monte_vegas_alloc (8);
    gsl_monte_vegas_integrate (&G_x, xl_x, xu_x, 8, 10000, r_x, s_x,&res_x, &err_x);

    do
    {
        gsl_monte_vegas_integrate (&G_x, xl_x, xu_x, 8, calls_x/5, r_x, s_x,&res_x, &err_x);
    }
    while (fabs (gsl_monte_vegas_chisq (s_x) - 1.0) > 0.5);

    /////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////

    // source disk
    double res_sd,err_sd;

    double xl_sd[1]={0.0};
    double xu_sd[1]={11.5};

    const gsl_rng_type *T_sd;
    gsl_rng *r_sd;

    gsl_monte_function G_sd={&source_d,1,0};

    size_t calls_sd=50000000;

    T_sd=gsl_rng_default;

    r_sd = gsl_rng_alloc (T_sd);

    gsl_monte_vegas_state *s_sd = gsl_monte_vegas_alloc (1);
    gsl_monte_vegas_integrate (&G_sd, xl_sd, xu_sd, 1, 10000, r_sd, s_sd, &res_sd, &err_sd);

    do
    {
        gsl_monte_vegas_integrate (&G_sd, xl_sd, xu_sd, 1, calls_sd/5, r_sd, s_sd, &res_sd, &err_sd);
    }
    while (fabs (gsl_monte_vegas_chisq (s_sd) - 1.0) > 0.5);

    gsl_monte_vegas_free (s_sd);

    gsl_rng_free (r_sd);
    
    //////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////

    // neutron stars, pulsar to disk
    double res_xd,err_xd;

    //double xl_x[4]={3.0,0.0,-1200.0,-1200.0};
    //double xu_x[4]={13.0,13.0,1200.0,1200.0};
    double xl_xd[8]={0.0,0.01,-2000.0,-2000.0,-2000.0,-100.0,-150.0,-100.0};
    double xu_xd[8]={11.5,8.0,2000.0,2000.0,2000.0,100.0,150.0,100.0};

    const gsl_rng_type *T_xd;
    gsl_rng *r_xd;

    gsl_monte_function G_xd={&g_psr_d,8,0};

    size_t calls_xd=50000000;

    T_xd=gsl_rng_default;

    // calculation
    
    r_xd=gsl_rng_alloc(T_xd);
        
    gsl_monte_vegas_state *s_xd = gsl_monte_vegas_alloc (8);
    gsl_monte_vegas_integrate (&G_xd, xl_xd, xu_xd, 8, 10000, r_xd, s_xd, &res_xd, &err_xd);

    do
    {
        gsl_monte_vegas_integrate (&G_xd, xl_xd, xu_xd, 8, calls_xd/5, r_xd, s_xd, &res_xd, &err_xd);
    }
    while (fabs (gsl_monte_vegas_chisq (s_xd) - 1.0) > 0.5);

    /////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////
    psr = (fabs(res_x)+fabs(res_xd))/(fabs(res_s)+fabs(res_sd));
    printf ("bulge:%e %e %e\n", res_x, res_s, res_x/res_s);
    printf ("disk:%e %e %e\n", res_xd, res_sd, res_xd/res_sd);
    printf ("psr event rate:%e\n", psr);
    //printf ("psr event rate: %e\n", psr);

    gsl_monte_vegas_free (s_x);

    gsl_rng_free (r_x);

    gsl_monte_vegas_free (s_xd);

    gsl_rng_free (r_xd);

    return 0;
}