/// 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; }
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; }
static int get_epsabs_epsrel_limit_workspace(int argc, VALUE *argv, int argstart, double *epsabs, double *epsrel, size_t *limit, gsl_integration_workspace **w) { int flag = 0, itmp; itmp = argstart; *epsabs = EPSABS_DEFAULT; *epsrel = EPSREL_DEFAULT; *limit = LIMIT_DEFAULT; switch (argc-itmp) { case 0: *w = gsl_integration_workspace_alloc(*limit); flag = 1; break; case 1: if (TYPE(argv[itmp]) == T_ARRAY) { get_epsabs_epsrel(argc, argv, itmp, epsabs, epsrel); *w = gsl_integration_workspace_alloc(*limit); flag = 1; } else { flag = get_limit_workspace(argc, argv, itmp, limit, w); } break; case 2: case 3: switch (TYPE(argv[itmp])) { case T_ARRAY: itmp = get_epsabs_epsrel(argc, argv, itmp, epsabs, epsrel); flag = get_limit_workspace(argc, argv, itmp, limit, w); break; case T_FLOAT: get_epsabs_epsrel(argc, argv, itmp, epsabs, epsrel); *w = gsl_integration_workspace_alloc(*limit); flag = 1; break; default: flag = get_limit_workspace(argc, argv, itmp, limit, w); break; } break; case 4: itmp = get_epsabs_epsrel(argc, argv, itmp, epsabs, epsrel); flag = get_limit_workspace(argc, argv, itmp, limit, w); break; default: rb_raise(rb_eArgError, "wrong number of arguments"); break; } if (*w == NULL) rb_raise(rb_eRuntimeError, "something wrong with workspace"); return flag; }
static int get_limit_key_workspace(int argc, VALUE *argv, int argstart, size_t *limit, int *key, gsl_integration_workspace **w) { int flag = 0; switch (argc-argstart) { case 3: CHECK_FIXNUM(argv[argstart]); CHECK_FIXNUM(argv[argstart+1]); CHECK_WORKSPACE(argv[argstart+2]); *limit = FIX2INT(argv[argstart]); *key = FIX2INT(argv[argstart+1]); Data_Get_Struct(argv[argstart+2], gsl_integration_workspace, *w); flag = 0; break; case 1: CHECK_FIXNUM(argv[argstart]); *key = FIX2INT(argv[argstart]); *limit = LIMIT_DEFAULT; *w = gsl_integration_workspace_alloc(*limit); flag = 1; break; case 2: if (TYPE(argv[argc-1]) == T_FIXNUM) { CHECK_FIXNUM(argv[argc-2]); *limit = FIX2INT(argv[argc-2]); *key = FIX2INT(argv[argc-1]); *w = gsl_integration_workspace_alloc(*limit); flag = 1; } else { CHECK_FIXNUM(argv[argc-2]); CHECK_WORKSPACE(argv[argc-1]); *key = FIX2INT(argv[argc-2]); Data_Get_Struct(argv[argc-1], gsl_integration_workspace, *w); *limit = (*w)->limit; flag = 0; } break; case 0: *key = KEY_DEFAULT; *limit = LIMIT_DEFAULT; *w = gsl_integration_workspace_alloc(*limit); flag = 1; break; default: rb_raise(rb_eArgError, "wrong number of arguments"); break; } if (*w == NULL) rb_raise(rb_eRuntimeError, "something wrong with workspace"); return flag; }
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; }
/* * 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; }
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; }
// ----------------------------------------------------------------- // 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; }
double V_1(double x, double y) { double * dargs = ml_alloc<double> (10); void * arg_ptr = (void *)dargs; dargs[0] = x; dargs[1] = y; double epsabs=1E-6; double epsrel=1E-10; double result,real_abserr,imag_result,imag_abserr; static gsl_integration_workspace * workspace = gsl_integration_workspace_alloc (1000000); gsl_function F; F.function = integrand; F.params = arg_ptr; gsl_integration_qag ( &F, 0, 1, epsabs, epsrel, 200000, 6, workspace, &result, &real_abserr ); return result; }
void smoothing_integrate(smoothing_params ¶ms, 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 = ¶ms; 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); }
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; }
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; }
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 = ¶ms; double base = washout_integrand(T,¶ms); double iT = 2; for(iT=2;fabs(washout_integrand(iT*T,¶ms)/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,¶ms)/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; }
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; }
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 }
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 = π 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; }
//------------------------------------------------------------------------------ IntegratorAdaptive::IntegratorAdaptive(double relPrecision, double absPrecision, size_t maxIterations, Integration::Rule rule) : _relPrecision(relPrecision), _absPrecision(absPrecision), _maxIterations(maxIterations), _rule(rule) { _workspace = static_cast<workspace*>(gsl_integration_workspace_alloc(_maxIterations)); }
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 = α 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; }
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 = β 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; }
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 = ∥ 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(); }
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; }
/*! 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); }
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; }
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 = π 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); }
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 = α 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; }
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; }
// angular average for Mhat in integration region I for given s (<z^n*M> with // n={0,1}) void M_avg_1(complex_spline M, complex *M_avg, double *s, double a, double b, double c, double d, char plusminus, char kappa_pm, int N, int n) { int i, N_int; double eps, error, s_minus, s_plus, sigma; N_int = 1500; eps = 1.0e-10; gsl_interp_accel *acc = gsl_interp_accel_alloc(); gsl_integration_workspace *w = gsl_integration_workspace_alloc(N_int); gsl_function F; gsl_function G; double f_re(double z, void *params) { double sigma = *(double *)params; double f; if (n == 0) { f = gsl_spline_eval(M.re, z, acc); } else if (n == 1) { f = (z - sigma) * gsl_spline_eval(M.re, z, acc); } else if (n == 2) { f = pow(z - sigma, 2.0) * gsl_spline_eval(M.re, z, acc); } // if (isnan(f)==1) { // printf("%d %.3e %.3e\n",n,z,sigma); // } return f; }
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; }
/*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 = ν 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; }