Exemplo n.º 1
0
/// Calculate the variance of the power spectrum on a scale R in h^{-1} Mpc
/// using a Gaussian filter. 
double cosmology::var_G(double R, double z)
{
    //Smith et al. 2003 Eq. 54
    //Int_0^{\infty} dk/k Delta^2(k) exp(-(kR)^2)
    double result1, result2, error;

    gsl_function F;
    F.function = &(dvar_G);

    //Initialize parameter block
    cvar_params p;
    p.cptr = this;
    p.R=&R;
    p.z=&z;

    F.params = &p;

    gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);
    gsl_integration_qags (&F, 0., 1./R, 0, 1e-6, 1000, w, &result1, &error); 
    gsl_integration_workspace_free (w);

    gsl_integration_workspace * v = gsl_integration_workspace_alloc (1000);
    gsl_integration_qagiu (&F, 1./R, 0, 1e-6, 1000, v, &result2, &error); 
    gsl_integration_workspace_free (v);

    //printf ("result          = % .18f\n", result);
    //printf ("estimated error = % .18f\n", error);
    //printf ("intervals =  %d\n", w->size);


    return result1+result2;
    
}
Exemplo n.º 2
0
int
main() {
  body saturn, jupiter;
  const size_t ws_size = 10000;
  gsl_integration_workspace *ws1, *ws2;
  int status = 0;
  double sat_rhs[BODY_VECTOR_SIZE], jup_rhs[BODY_VECTOR_SIZE];
  double sat_deltdt[NELEMENTS], jup_deltdt[NELEMENTS];
  double spin[3] = {0.0, 0.0, 0.0};

  ws1 = gsl_integration_workspace_alloc(ws_size);
  ws2 = gsl_integration_workspace_alloc(ws_size);
  assert(ws1 != 0);
  assert(ws2 != 0);
  
  init_body_from_elements(&jupiter, 9.54786e-4, 5.202545, 0.0474622, 1.30667, 100.0381, 13.983865, spin, 1.0/0.0, 0.0, 0.0, 0.0);
  init_body_from_elements(&saturn, 2.85837e-4, 9.554841, 0.0575481, 2.48795, 113.1334, 88.719425, spin, 1.0/0.0, 0.0, 0.0, 0.0);

  raw_average_rhs(0.0, &jupiter, &saturn, ws1, ws_size, ws2, ws_size, EPS, EPS, jup_rhs);
  raw_average_rhs(0.0, &saturn, &jupiter, ws1, ws_size, ws2, ws_size, EPS, EPS, sat_rhs);

  body_derivs_to_orbital_elements(&jupiter, jup_rhs, jup_deltdt);
  body_derivs_to_orbital_elements(&saturn, sat_rhs, sat_deltdt);

  /* fprintf(stderr, "Jupiter edot (yr^-1): %g, Idot (rad/yr): %g\n", */
  /*         jup_deltdt[E_INDEX]*2.0*M_PI, jup_deltdt[I_INDEX]*2.0*M_PI*M_PI/180.0); */
  /* fprintf(stderr, "Saturn  edot (yr^-1): %g, Idot (rad/yr): %g\n", */
  /*         sat_deltdt[E_INDEX]*2.0*M_PI, sat_deltdt[I_INDEX]*2.0*M_PI*M_PI/180.0); */

  if (fabs(jup_deltdt[E_INDEX]*2.0*M_PI - 1.2920470269576777e-6) >= 5e-7) {
    fprintf(stderr, "raw-average-jupiter-saturn: bad Jupiter edot\n");
    status = 1;
  }

  if (fabs(jup_deltdt[I_INDEX]*2.0*M_PI*M_PI/180.0 - -3.501018965845696e-7) >= 5e-8) {
    fprintf(stderr, "raw-average-jupiter-saturn: bad Jupiter Idot\n");
    status = 2;
  }

  if (fabs(sat_deltdt[E_INDEX]*2.0*M_PI - -2.620812070178865e-6) >= 5e-7) {
    fprintf(stderr, "raw-average-jupiter-saturn: bad Saturn edot\n");
    status = 3;
  }

  if (fabs(sat_deltdt[I_INDEX]*2.0*M_PI*M_PI/180.0 - 4.5322043030781326e-7) >= 5e-8) {
    fprintf(stderr, "raw-average-jupiter-saturn: bad Saturn Idot\n");
    status = 4;
  }

  gsl_integration_workspace_free(ws1);
  gsl_integration_workspace_free(ws2);
  return status;
}
Exemplo n.º 3
0
int
main() {
  gsl_integration_workspace *ws1, *ws2;
  const size_t ws_size = 100000;
  gsl_rng *rng;
  int status = 0;
  body b1, b2;
  const double m1 = 1.01e-3;
  const double m2 = 1.998e-3;
  const double a1 = 1.02;
  const double a2 = 10.3;
  const double eps = 1e-3;
  double rhs[BODY_VECTOR_SIZE];
  double numerical_rhs[BODY_VECTOR_SIZE];

  rng = gsl_rng_alloc(gsl_rng_ranlxd2);
  assert(rng != 0);
  seed_random(rng);

  ws1 = gsl_integration_workspace_alloc(ws_size);
  ws2 = gsl_integration_workspace_alloc(ws_size);
  assert(ws1 != 0);
  assert(ws2 != 0);

  init_random_body(rng, &b1, m1, a1, 1.0/0.0, 0.0, 0.0, 0.0);
  init_random_body(rng, &b2, m2, a2, 1.0/0.0, 0.0, 0.0, 0.0);

  raw_average_rhs(eps, &b1, &b2, ws1, ws_size, ws2, ws_size, EPS, EPS, numerical_rhs);
  average_rhs(eps, &b1, &b2, EPS, rhs);

  if (!check_vector_close(10.0*EPS, 10.0*EPS, BODY_VECTOR_SIZE, rhs, numerical_rhs)) {
    fprintf(stderr, "average-rhs-test: numerical average doesn't agree with analytical average\n");
    fprintf(stderr, "Analytic:  %15g %15g %15g %15g %15g %15g %15g %15g %15g %15g %15g\n",
            rhs[0], rhs[1], rhs[2], rhs[3], rhs[4], rhs[5], rhs[6],
            rhs[7], rhs[8], rhs[9], rhs[10]);
    fprintf(stderr, "Numerical: %15g %15g %15g %15g %15g %15g %15g %15g %15g %15g %15g\n",
            numerical_rhs[0], numerical_rhs[1], numerical_rhs[2], numerical_rhs[3], 
            numerical_rhs[4], numerical_rhs[5], numerical_rhs[6],
            numerical_rhs[7], numerical_rhs[8], numerical_rhs[9], numerical_rhs[10]);

    status = 1;
  }

  gsl_rng_free(rng);
  gsl_integration_workspace_free(ws1);
  gsl_integration_workspace_free(ws2);

  return status;
}
ANACalculatorCoplanarTriple::~ANACalculatorCoplanarTriple()
{
	delete[] m_z1;
	delete[] m_integrand_values;

	gsl_interp_free (m_interp);
	gsl_interp_accel_free(m_accel);

	if(m_qawo_table)
		gsl_integration_qawo_table_free(m_qawo_table);
	if(m_workspace)
		gsl_integration_workspace_free(m_workspace);
	if(m_cyclic_workspace)
		gsl_integration_workspace_free(m_cyclic_workspace);
}
Exemplo n.º 5
0
static VALUE rb_gsl_integration_qawo(int argc, VALUE *argv, VALUE obj)
{
  double a, epsabs, epsrel;
  double result, abserr;
  size_t limit;
  gsl_function *F = NULL;
  gsl_integration_workspace *w = NULL;
  gsl_integration_qawo_table *t = NULL;
  int status, intervals, itmp, flag = 0, flagt = 0;
  switch (TYPE(obj)) {
  case T_MODULE:  case T_CLASS:  case T_OBJECT:
    if (argc < 2) rb_raise(rb_eArgError, "too few arguments");
    CHECK_FUNCTION(argv[0]);
    Data_Get_Struct(argv[0], gsl_function, F);
    itmp = 1;
    break;
  default:
    if (argc < 1) rb_raise(rb_eArgError, "too few arguments");
    Data_Get_Struct(obj, gsl_function, F);
    itmp = 0;
    break;
  }
  Need_Float(argv[itmp]);
  a = NUM2DBL(argv[itmp]);
  flagt = get_qawo_table(argv[argc-1], &t);
  flag = get_epsabs_epsrel_limit_workspace(argc-1, argv, itmp+1, &epsabs, &epsrel,
                                           &limit, &w);
  status = gsl_integration_qawo(F, a, epsabs, epsrel, limit, w, t, &result, &abserr);
  intervals = w->size;
  if (flag == 1) gsl_integration_workspace_free(w);
  if (flagt == 1) gsl_integration_qawo_table_free(t);
  return rb_ary_new3(4, rb_float_new(result), rb_float_new(abserr), INT2FIX(intervals),
                     INT2FIX(status));
}
Exemplo n.º 6
0
/* (-infty --- b) */
static VALUE rb_gsl_integration_qagil(int argc, VALUE *argv, VALUE obj)
{
  double b, epsabs, epsrel;
  double result, abserr;
  size_t limit;
  gsl_function *F = NULL;
  gsl_integration_workspace *w = NULL;
  int status, intervals, flag = 0, itmp;
  switch (TYPE(obj)) {
  case T_MODULE:  case T_CLASS:  case T_OBJECT:
    CHECK_FUNCTION(argv[0]);
    Data_Get_Struct(argv[0], gsl_function, F);
    itmp = 1;
    break;
  default:
    Data_Get_Struct(obj, gsl_function, F);
    itmp = 0;
    break;
  }
  Need_Float(argv[itmp]);
  b = NUM2DBL(argv[itmp]);
  flag = get_epsabs_epsrel_limit_workspace(argc, argv, itmp+1, &epsabs, &epsrel,
                                           &limit, &w);
  Data_Get_Struct(obj, gsl_function, F);

  status = gsl_integration_qagil(F, b, epsabs, epsrel, limit, w,
                                 &result, &abserr);
  intervals = w->size;
  if (flag == 1) gsl_integration_workspace_free(w);
  return rb_ary_new3(4, rb_float_new(result), rb_float_new(abserr),
                     INT2FIX(intervals), INT2FIX(status));
}
Exemplo n.º 7
0
void smoothing_integrate(smoothing_params &params, int n) {
  int n_int = 2*n;

  params.acc_y = gsl_interp_accel_alloc();
  params.int_y = gsl_interp_alloc(gsl_interp_linear, n);
  params.n     = n;
  gsl_interp_init(params.int_y, params.arr_x, params.arr_y, n);

  /* setup integration workspace */

  gsl_integration_workspace *w = gsl_integration_workspace_alloc(n_int);
  gsl_function F; 
  F.function = &smoothing_integrand;
  F.params   = &params;
  
  double eps_abs = 0.0, eps_rel = 0.01;
  double result, error;
  gsl_set_error_handler_off();
 
  for (int i = 0; i < n; i++) {
    params.x = params.arr_x[i];
    params.h = 0.1; //0.1*params.x + 1.0e-4;

    gsl_integration_qag(&F, 
			params.x - 2*params.h, params.x + 2*params.h,
			eps_abs, eps_rel,
			n_int, 1,
			w, &result, &error);
    params.smoothed_y[i] = result;
  }
  
  gsl_integration_workspace_free(w);
  gsl_interp_accel_free(params.acc_y);
  gsl_interp_free(params.int_y);
}
Exemplo n.º 8
0
double gslInt_jsyn(double r){ 				// int over E

	///////////
	std::clock_t start;
	double duration;
	start = std::clock();
	///////////

	gsl_integration_workspace * w 
		= gsl_integration_workspace_alloc (1000);

	double result, error;

	gsl_function F;
	F.function = &djsyn;
	F.params = &r;

	gsl_integration_qags (&F, me, p.mx, 0, 1e-2, 1000,
	                    w, &result, &error); 

	gsl_integration_workspace_free (w);

	duration = (std::clock()  -  start)/(double) CLOCKS_PER_SEC;
	if (duration > 1)
	std::cout << "alpha = "<< c.alpha << ", jsyn( r = " << r/kpc2cm <<" ) = "<< result <<" duration: " << duration <<std::endl;  //      ~30s-60s

	return result;

}
Exemplo n.º 9
0
double gslInt_diffusion( double E, double r){			// int over Ep
	double E_scaled = (int)(E/c.vscale);

	double vE = c.vlookup[E_scaled];

	gsl_integration_workspace * w 
		= gsl_integration_workspace_alloc (1000);

	double result, error;

	std::vector<double> diffusionParams (3);

	diffusionParams[0] = E;
	diffusionParams[1] = vE;
	diffusionParams[2] = r;

	gsl_function F;
	F.function = &ddiffusion;
	F.params = &diffusionParams; 								//pass Ep to rootdv(), pass r from dndeeq as well, 
	gsl_set_error_handler_off();
	gsl_integration_qags (&F, E, p.mx, 0, 1e-2, 1000, 
	                    w, &result, &error); 

	gsl_integration_workspace_free (w);

	return result;

}
Exemplo n.º 10
0
static void comp_lens_power_spectrum(lensPowerSpectra lps)
{
#define WORKSPACE_NUM 100000
#define ABSERR 1e-12
#define RELERR 1e-12
#define TABLE_LENGTH 1000
  
  gsl_integration_workspace *workspace;
  gsl_function F;
  double result,abserr;
  double logltab[TABLE_LENGTH];
  double logpkltab[TABLE_LENGTH];
  double chimax;
  int i;
  
  //fill in bin information
  chiLim = lps->chiLim;
  if(lps->chis1 > lps->chis2)
    chimax = lps->chis1;
  else
    chimax = lps->chis2;
  
  fprintf(stderr,"doing lens pk - chiLim = %lf, chiMax = %lf\n",chiLim,chimax);
  
  //init
  workspace = gsl_integration_workspace_alloc((size_t) WORKSPACE_NUM);
  F.function = &lenspk_integrand;
  F.params = lps;
  
  //make table
  double lnlmin = log(wlData.lmin);
  double lnlmax = log(wlData.lmax);
  for(i=0;i<TABLE_LENGTH;++i)
    {
      logltab[i] = i*(lnlmax-lnlmin)/(TABLE_LENGTH-1) + lnlmin;
      
      lps->ell = exp(logltab[i]);
      gsl_integration_qag(&F,0.0,chimax,ABSERR,RELERR,(size_t) WORKSPACE_NUM,GSL_INTEG_GAUSS51,workspace,&result,&abserr);
      
      logpkltab[i] = log(result);
    }
  
  //free
  gsl_integration_workspace_free(workspace);
  
  //init splines and accels
  if(lps->spline != NULL)
    gsl_spline_free(lps->spline);
  lps->spline = gsl_spline_alloc(gsl_interp_akima,(size_t) (TABLE_LENGTH));
  gsl_spline_init(lps->spline,logltab,logpkltab,(size_t) (TABLE_LENGTH));
  if(lps->accel != NULL)
    gsl_interp_accel_reset(lps->accel);
  else
    lps->accel = gsl_interp_accel_alloc();
    
#undef TABLE_LENGTH
#undef ABSERR
#undef RELERR
#undef WORKSPACE_NUM
}
Exemplo n.º 11
0
double gslInt_jsyn(double mx, double r){
		//std::cout << "jsyn " <<std::endl;
	gsl_integration_workspace * w 
		= gsl_integration_workspace_alloc (5000);

	double result, error;

	std::vector<double> alpha (2);

	alpha[0] = mx;
	alpha[1] = r;


	gsl_function F;
	F.function = &djsyn;
	F.params = &alpha;

	gsl_integration_qags (&F, me, mx, 0, 1e-3, 5000,
	                    w, &result, &error); 


	gsl_integration_workspace_free (w);

	return result;

	//std::cout << "gslInt_jsyn: " << std::setprecision(19) << result << std::endl;
}
Exemplo n.º 12
0
double gslInt_psyn(  double E, double r){
		
	gsl_integration_workspace * w 
		= gsl_integration_workspace_alloc (5000);

	double result, error;

	std::vector<double> beta (2);

	beta[0] = E;
	beta[1] = r;


	gsl_function F;
	F.function = &dpsyn;
	F.params = &beta;

	gsl_integration_qags (&F, 1e-16, pi, 0, 1e-3, 5000, //x?
	                    w, &result, &error); 

	gsl_integration_workspace_free (w);

	//std::cout << "psyn(5): " << result <<std::endl; 

	return result;

}
Exemplo n.º 13
0
int cfacdb_crates(cfacdb_t *cdb, double T,
                  cfacdb_crates_sink_t sink, void *udata)
{
    int rc;

    struct {
        double T;
        cfacdb_crates_sink_t sink;
        void *udata;
        gsl_integration_workspace *w;
    } rdata;

    rdata.sink  = sink;
    rdata.udata = udata;
    rdata.T = T;

    rdata.w = gsl_integration_workspace_alloc(1000);
    if (!rdata.w) {
        return CFACDB_FAILURE;
    }

    rc = cfacdb_ctrans(cdb, crates_sink, &rdata);

    gsl_integration_workspace_free(rdata.w);

    return rc;
}
Exemplo n.º 14
0
Arquivo: disc.cpp Projeto: latrop/GRCF
double
  intz (double u, void* params)
{
  struct intz_params *p
    = (struct intz_params *)params;

  double z0 = (p->z0);
  double  r = (p->r);

   gsl_function FUN;
   FUN.function = &fdz_integrate;
   struct fdz_integrate_params pi;
   pi.z0 = z0;
   pi.u = u;
   pi.r = r;
   FUN.params = &pi;

   gsl_integration_workspace* w =
     gsl_integration_workspace_alloc(100000);

  double rez;
  double rezerr;

   gsl_integration_qagiu(&FUN, 0, 0, 1e-6, 100000, w, &rez, &rezerr);

   gsl_integration_workspace_free (w);

   return rez;
}
Exemplo n.º 15
0
Arquivo: disc.cpp Projeto: latrop/GRCF
double
vd2 (double r, void* params)
{
  struct vd2_params *p
    = (struct vd2_params *)params;

  double z0 = (p->z0);

   gsl_function F;
   F.function = &dForced;
   struct dForced_params pi;
   pi.z0 = z0;
   pi.r = r;
   F.params = &pi;

   gsl_integration_workspace* w =
     gsl_integration_workspace_alloc(100000);

  double rez;
  double rezerr;

   gsl_integration_qagiu(&F, 0, 0, 1e-6, 100000, w, &rez, &rezerr);

   gsl_integration_workspace_free (w);

   return rez*sqrt(r);
}
Exemplo n.º 16
0
/*
 * Start the integration.
 */
int gmdi_multi_dimensional_integration(gmdi_inte_handle handle)
{
    int                                         ret, i;
    gmdi_multi_dim_inte_param *                 params = (gmdi_multi_dim_inte_param *) handle;

    params->intern.x = calloc(params->n, sizeof(double));
    params->intern.results = calloc(params->n, sizeof(double));
    params->intern.abserrs = calloc(params->n, sizeof(double));
    params->intern.dim = 0;

    for (i = 0; i < params->n; ++ i)
    {
        params->oip[i].intern.giw = gsl_integration_workspace_alloc(params->oip[i].limit);
        params->oip[i].f.n = i + 1;

        if (params->oip[i].x0.type == GMDI_FUNCTION_OR_CONSTANT_TYPE_MULTI_VAR_FUNCTION)
            params->oip[i].x0.content.mf.n = i;
        if (params->oip[i].x1.type == GMDI_FUNCTION_OR_CONSTANT_TYPE_MULTI_VAR_FUNCTION)
            params->oip[i].x1.content.mf.n = i;
    }

    ret = call_integration_func(params);

    params->result = params->intern.results[0];
    params->abserr = params->intern.abserrs[0];

    for (i = 0; i < params->n; ++ i)
        gsl_integration_workspace_free(params->oip[i].intern.giw);

    free(params->intern.x);
    free(params->intern.results);
    free(params->intern.abserrs);

    return ret;
}
Exemplo n.º 17
0
void normalize_sigma8()
{
	int WORKSP = 5000; 
	double s8, k_min, k_max, result, error, par[2]; 
	gsl_integration_workspace *w = gsl_integration_workspace_alloc(WORKSP);

	s8 = Cosmo.sigma8;
	par[0]=8.; 
	PK_INDEX=0;

	k_min = Pks[0].k[0]; 
	k_max = Pks[0].k[Pks[0].npts-1];

		gsl_function F;
		F.function = &sigma_integ;
		F.params = &par;

		gsl_integration_qags(&F, k_min, k_max, 0, 1e-6, WORKSP, w, &result, &error);
		result*=(1./(2*PI*PI));

		Cosmo.norm_sigma8 = s8*s8/result;

			fprintf(stdout, "Original sigma8:%lf, required sigma8: %lf, normalization factor:%lf\n", 
				sqrt(result), s8, Cosmo.norm_sigma8);

		gsl_integration_workspace_free (w);

	normalize_all_power_spectra_to_sigma8();
}
Exemplo n.º 18
0
     int
     main (void)
     {
       gsl_integration_workspace * w 
         = gsl_integration_workspace_alloc (4000);
  
       size_t    neval;     
       double result, error;
       double a=0.0, b=cos(1.27);    

       gsl_function F;
       F.function = &f;

/*     
       gsl_integration_qags (&F, a, b, 1.0e-9, 1e-9, 4000,
                             w, &result, &error); 
*/
/*      
       gsl_integration_qag(&F, a, b, 1.0e-7, 1.0e-7, 4000, 1, w, &result, &error); 
*/

       gsl_integration_qng(&F, a, b, 1.0e-6, 1.0e-6, &result, &error, &neval);

    
       printf ("result          = % .18f\n", result);
       printf ("estimated error = % .18f\n", error);
     
       gsl_integration_workspace_free (w);
     
       return 0;
     }
Exemplo n.º 19
0
double gslInt_Greens(double ri , double r, double root_dv, double rh){


	gsl_integration_workspace * w 
		= gsl_integration_workspace_alloc (1000);

	double result, error;

	std::vector<double> greenParam (3);


	greenParam[0] = ri;
	greenParam[1] = r;
	greenParam[2] = root_dv;

	gsl_function F;
	F.function = &dGreens;
	F.params = &greenParam;
	gsl_set_error_handler_off();
	gsl_integration_qags (&F, 1e-16, rh, 0, 1e-3, 1000, //x?
	                w, &result, &error); 

	gsl_integration_workspace_free (w);

	return result;

}
Exemplo n.º 20
0
double washout(double eta,double T, double M)
{
	double Y1=1.0;

	gsl_integration_workspace * w = gsl_integration_workspace_alloc (5000);
	struct washoutParams params = {T,M};
	double result,error;
	gsl_function F;
	F.function = &washout_integrand;
	F.params = &params;
	
	
	double base = washout_integrand(T,&params);
	double iT = 2;

	for(iT=2;fabs(washout_integrand(iT*T,&params)/base)>=T_TRESHOLD;iT=iT+1) 
	{
		if(DEBUG_MODE){	std::cout<<std::setprecision(9)<<"# washout integral base "<<base<<" iT: "<<iT<<" rat:  "<<fabs(washout_integrand(iT*T,&params)/base)<<std::endl;}
	}


	gsl_integration_qags(&F,0.0, iT*T, ABS, REL, 5000, w, &result, &error); 
	gsl_integration_workspace_free (w);

	return -3*Y1*Y1*M*M*eta/(8*PI*PI*PI*T)*result;

}
Exemplo n.º 21
0
double gslInt_psyn(  double E, double r){			//int over theta

		
	gsl_integration_workspace * w 
		= gsl_integration_workspace_alloc (1000);

	double result, error;

	std::vector<double> psynParams (2);

	psynParams[0] = E;
	psynParams[1] = r;


	gsl_function F;
	F.function = &dpsyn;
	F.params = &psynParams;

	gsl_integration_qags (&F, 1e-16, pi, 0, 1e-3, 1000, //x?
	                    w, &result, &error); 

	gsl_integration_workspace_free (w);

	return result;

}
Exemplo n.º 22
0
/*! This function computes look-up tables for factors needed in
 *  cosmological integrations. The (simple) integrations are carried out
 *  with the GSL library.  Separate factors are computed for the "drift",
 *  and the gravitational and hydrodynamical "kicks".  The lookup-table is
 *  used for reasons of speed.
 */
void init_drift_table(void)
{
#define WORKSIZE 100000
  int i;
  double result, abserr;
  gsl_function F;
  gsl_integration_workspace *workspace;

  logTimeBegin = log(All.TimeBegin);
  logTimeMax = log(All.TimeMax);

  workspace = gsl_integration_workspace_alloc(WORKSIZE);

  for(i = 0; i < DRIFT_TABLE_LENGTH; i++)
    {
      F.function = &drift_integ;
      gsl_integration_qag(&F, exp(logTimeBegin), exp(logTimeBegin + ((logTimeMax - logTimeBegin) / DRIFT_TABLE_LENGTH) * (i + 1)), 0,
			  1.0e-8, WORKSIZE, GSL_INTEG_GAUSS41, workspace, &result, &abserr);
      DriftTable[i] = result;


      F.function = &gravkick_integ;
      gsl_integration_qag(&F, exp(logTimeBegin), exp(logTimeBegin + ((logTimeMax - logTimeBegin) / DRIFT_TABLE_LENGTH) * (i + 1)), 0,
			  1.0e-8, WORKSIZE, GSL_INTEG_GAUSS41, workspace, &result, &abserr);
      GravKickTable[i] = result;


      F.function = &hydrokick_integ;
      gsl_integration_qag(&F, exp(logTimeBegin), exp(logTimeBegin + ((logTimeMax - logTimeBegin) / DRIFT_TABLE_LENGTH) * (i + 1)), 0,
			  1.0e-8, WORKSIZE, GSL_INTEG_GAUSS41, workspace, &result, &abserr);
      HydroKickTable[i] = result;
    }

  gsl_integration_workspace_free(workspace);
}
Exemplo n.º 23
0
int
main (void)
{
  gsl_integration_workspace * w 
    = gsl_integration_workspace_alloc (1000);
  
  double result, error;
  double expected = -4.0;
  double alpha = 1.0;

  gsl_function F;
  F.function = &f;
  F.params = &alpha;

  gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000,
                        w, &result, &error); 

  printf ("result          = % .18f\n", result);
  printf ("exact result    = % .18f\n", expected);
  printf ("estimated error = % .18f\n", error);
  printf ("actual error    = % .18f\n", result - expected);
  printf ("intervals       = %zu\n", w->size);

  gsl_integration_workspace_free (w);

  return 0;
}
Exemplo n.º 24
0
gsprof *gsp_expd(double mtot, double alpha, double zdisk,
		 int np, double rmin, double rmax)
{
  gsprof *gsp = (gsprof *) allocate(sizeof(gsprof));
  double lgrs = log2(rmax / rmin) / (np - 1), x;
  gsl_integration_workspace *wksp;

  assert(np > 0 && mtot > 0.0 && alpha > 0.0 && zdisk >= 0);
  gsp->npoint = np;
  gsp->radius  = (double *) allocate(np * sizeof(double));
  gsp->density = (double *) allocate(np * sizeof(double));
  gsp->mass    = (double *) allocate(np * sizeof(double));
  if (zdisk > 0)				// will need integration?
    wksp = gsl_integration_workspace_alloc(NWKSP);
  for (int i = 0; i < np; i++) {
    gsp->radius[i] = rmin * exp2(lgrs * i);
    x = - alpha * gsp->radius[i];
    gsp->density[i] = (zdisk > 0 ?
		       dsphr(gsp->radius[i], mtot, alpha, zdisk, wksp) :
		       mtot * exp(x) * alpha*alpha / (4*M_PI*gsp->radius[i]));
    gsp->mass[i]    = (zdisk > 0 ?
		       msphr(gsp->radius[i], mtot, alpha, zdisk, wksp) :
		       mtot * (x * exp(x) - gsl_expm1(x)));
  }
  if (zdisk > 0)
    gsl_integration_workspace_free(wksp);
  for (int i = 1; i < np; i++) {		// check strict monotonicity
    assert(gsp->mass[i-1] < gsp->mass[i]);	// OK if alpha*rmax <= ~32
    assert(gsp->density[i-1] > gsp->density[i]);
  }
  gsp->alpha = (zdisk == 0 ? -1.0 : 0.0);
  gsp->beta = - (1 + alpha * rmax);
  gsp->mtot = mtot;
  return gsp;
}
Exemplo n.º 25
0
static VALUE rb_gsl_integration_qags(int argc, VALUE *argv, VALUE obj)
{
  double a, b, epsabs = EPSABS_DEFAULT, epsrel = EPSREL_DEFAULT;
  double result, abserr;
  size_t limit = LIMIT_DEFAULT;
  gsl_function *F = NULL;
  gsl_integration_workspace *w = NULL;
  int status, intervals, flag = 0, itmp;
  switch (TYPE(obj)) {
  case T_MODULE:  case T_CLASS:  case T_OBJECT:
    CHECK_FUNCTION(argv[0]);
    Data_Get_Struct(argv[0], gsl_function, F);
    itmp = get_a_b(argc, argv, 1, &a, &b);
    break;
  default:
    Data_Get_Struct(obj, gsl_function, F);
    itmp = get_a_b(argc, argv, 0, &a, &b);
    break;
  }
  flag = get_epsabs_epsrel_limit_workspace(argc, argv, itmp, &epsabs, &epsrel,
                                           &limit, &w);

  status = gsl_integration_qags(F, a, b, epsabs, epsrel, limit, w,
                                &result, &abserr);
  intervals = w->size;
  if (flag == 1) gsl_integration_workspace_free(w);
  return rb_ary_new3(4, rb_float_new(result), rb_float_new(abserr),
                     INT2FIX(intervals), INT2FIX(status));
}
Exemplo n.º 26
0
// -----------------------------------------------------------------
// Returns ratio (Omega / Omega_*)^(2(alpha + 1))
double star(double delta, double epsilon, int order, double *c,
            double alpha) {

  double pow = alpha + 1.0;
  double intres, interr, ret, temp, seps = sqrt(epsilon);
  error2_pars_type error2_pars;
  error2_pars.epsilon = epsilon;
  error2_pars.order = order;
  error2_pars.c = c;
  error2_pars.alpha = alpha;

  gsl_function gsl_error2 = {&error2, &error2_pars};
  gsl_integration_workspace *gsl_ws_int
                            = gsl_integration_workspace_alloc(1000000);

  gsl_integration_qag(&gsl_error2, -seps, seps, 1.e-2, 0, 1000000,
                      GSL_INTEG_GAUSS15, gsl_ws_int, &intres, &interr);

  temp = gsl_sf_exp(pow * gsl_sf_log((1.0 - seps) / (1.0 + seps)) / 2.0);
  ret = temp + pow * intres;
  temp = gsl_sf_exp(gsl_sf_log(ret) / pow);     // A**B = exp[B log A]
  ret = temp * temp;                            // Squared!
  gsl_integration_workspace_free(gsl_ws_int);

  return ret;
}
Exemplo n.º 27
0
static VALUE rb_gsl_integration_qawc(int argc, VALUE *argv, VALUE obj)
{
  double a, b, c, epsabs, epsrel;
  double result, abserr;
  size_t limit;
  gsl_function *F = NULL;
  gsl_integration_workspace *w = NULL;
  int status, intervals, itmp, flag = 0;
  switch (TYPE(obj)) {
  case T_MODULE:  case T_CLASS:  case T_OBJECT:
    CHECK_FUNCTION(argv[0]);
    Data_Get_Struct(argv[0], gsl_function, F);
    itmp = 1;
    break;
  default:
    Data_Get_Struct(obj, gsl_function, F);
    itmp = 0;
    break;
  }
  itmp = get_a_b(argc, argv, itmp, &a, &b);
  if (argc-itmp <= 0) rb_raise(rb_eArgError, "The pole is not given");
  Need_Float(argv[itmp]);
  c = NUM2DBL(argv[itmp]);
  flag = get_epsabs_epsrel_limit_workspace(argc, argv, itmp+1, &epsabs, &epsrel,
                                           &limit, &w);
  status = gsl_integration_qawc(F, a, b, c, epsabs, epsrel, limit, w, &result, &abserr);
  intervals = w->size;
  if (flag == 1) gsl_integration_workspace_free(w);
  return rb_ary_new3(4, rb_float_new(result), rb_float_new(abserr), INT2FIX(intervals),
                     INT2FIX(status));
}
Exemplo n.º 28
0
double growth(const double a)
{
  // Compute integral of D_tilda(a)
  // growth_factor = D_tilda(a)/D_tilda(1.0)

  const double om= cosmology_omega_m();
  const double hubble_a= sqrt(om/(a*a*a) + 1.0 - om);

  const int worksize= 1000;
  double result, abserr;

  gsl_integration_workspace *workspace=
    gsl_integration_workspace_alloc(worksize);

  gsl_function F;
  F.function = &growth_integrand;
  F.params= (void*) &om;

  gsl_integration_qag(&F, 0, a, 0, 1.0e-8, worksize, GSL_INTEG_GAUSS41,
		      workspace, &result, &abserr);

  gsl_integration_workspace_free(workspace);

  return hubble_a * result;
}
Exemplo n.º 29
0
/*gsl_integrate: wrapper for the GSL QAG integration routine
 *@param min: Input, lower bound of integral
 *@param max: Input, upper bound of integral
 *@param n: Input, harmonic number and index of sum over n
 *@param nu: Input, frequency of emission/absorption
 *@returns: Output, result of either gamma integral or n integral
 */
double gsl_integrate(double min, double max, double n, double nu)
{
  double nu_c = (electron_charge * B_field)
               /(2. * M_PI * mass_electron * speed_light);
  if (nu/nu_c > 1.e6) {
    gsl_set_error_handler_off();
  }
  gsl_integration_workspace * w = gsl_integration_workspace_alloc (5000);
  double result, error;
  gsl_function F;
  if (n < 0) { //do n integration
    F.function = &gamma_integration_result;
    F.params = &nu;
    gsl_integration_qag(&F, min, max, 0., 1.e-3, 200,
                         3,  w, &result, &error);
  }
  else {  //do gamma integration
  struct parameters n_and_nu;
  n_and_nu.n = n;
  n_and_nu.nu = nu;
  F.function = &gamma_integrand;
  F.params = &n_and_nu;
  gsl_integration_qag(&F, min, max, 0., 1.e-3, 200,
                       3,  w, &result, &error);
  }

  gsl_integration_workspace_free (w);
  return result;
}
int calc_ave_delta_sigma_in_bin(double*R,int NR,double*delta_sigma,
				double lRlow,double lRhigh,
				double*ave_delta_sigma){
  int status = 0;

  gsl_spline*spline = gsl_spline_alloc(gsl_interp_cspline,NR);
  gsl_spline_init(spline,R,delta_sigma,NR);
  gsl_interp_accel*acc= gsl_interp_accel_alloc();
  gsl_integration_workspace * workspace
    = gsl_integration_workspace_alloc(workspace_size);

  integrand_params*params=malloc(sizeof(integrand_params));
  params->acc=acc;
  params->spline=spline;
  params->workspace=workspace;

  double Rlow=exp(lRlow),Rhigh=exp(lRhigh);

  do_integral(ave_delta_sigma,lRlow,lRhigh,params);
  *ave_delta_sigma *= 2./(Rhigh*Rhigh-Rlow*Rlow);

  gsl_spline_free(spline),gsl_interp_accel_free(acc);
  gsl_integration_workspace_free(workspace);
  free(params);
  return 0;
}