Example #1
0
ODEparams * init(int nap,double * alpha,double * talpha, int nbp, double* beta, double *tbeta,double gamma, double x0,double y0)
{
	ODEparams * pa= (ODEparams*)malloc(sizeof(ODEparams));

	pa->gamma=gamma;
	pa->x0=x0;
	pa->y0=y0;
	
	pa->nap=nap;
	pa->nbp=nbp;

	pa->alpha=calloc(nap,sizeof(double));
	pa->talpha=calloc(nap,sizeof(double));
	pa->beta=calloc(nbp,sizeof(double));
	pa->tbeta=calloc(nbp,sizeof(double));
	int i;
	memcpy(pa->alpha,alpha,nap*sizeof(double));
	memcpy(pa->talpha,talpha,nap*sizeof(double));
	memcpy(pa->beta,beta,nbp*sizeof(double));
	memcpy(pa->tbeta,tbeta,nbp*sizeof(double));

	pa->a_acc=gsl_interp_accel_alloc ();
	pa->b_acc=gsl_interp_accel_alloc ();
	pa->a_spline = gsl_spline_alloc (gsl_interp_cspline,nap);
	gsl_spline_init (pa->a_spline, pa->talpha, pa->alpha, pa->nap);
	pa->b_spline = gsl_spline_alloc (gsl_interp_cspline,nbp);
	gsl_spline_init (pa->b_spline, pa->tbeta, pa->beta, pa->nbp);

	pa->gamma=gamma;
	pa->x0=x0;
	pa->y0=y0;
	return pa;
}
Example #2
0
static float
interp_demData(float *demData, int nl, int ns, double l, double s)
{
  if (l<0 || l>=nl-1 || s<0 || s>=ns-1) {
    return 0;
  }

  int ix = (int)s;
  int iy = (int)l;

  int bilinear = l<3 || l>=nl-3 || s<3 || s>=ns-3;
  //int bilinear = 1;
  if (bilinear) {

    float p00 = demData[ix   + ns*(iy  )];
    float p10 = demData[ix+1 + ns*(iy  )];
    float p01 = demData[ix   + ns*(iy+1)];
    float p11 = demData[ix+1 + ns*(iy+1)];

    return (float)bilinear_interp_fn(s-ix, l-iy, p00, p10, p01, p11);
  }
  else {

    double ret, x[4], y[4], xi[4], yi[4];
    int ii;

    for (ii=0; ii<4; ++ii) {
      y[0] = demData[ix-1 + ns*(iy+ii-1)];
      y[1] = demData[ix   + ns*(iy+ii-1)];
      y[2] = demData[ix+1 + ns*(iy+ii-1)];
      y[3] = demData[ix+2 + ns*(iy+ii-1)];

      x[0] = ix - 1;
      x[1] = ix;
      x[2] = ix + 1;
      x[3] = ix + 2;

      gsl_interp_accel *acc = gsl_interp_accel_alloc ();
      gsl_spline *spline = gsl_spline_alloc (gsl_interp_cspline, 4);    
      gsl_spline_init (spline, x, y, 4);
      yi[ii] = gsl_spline_eval_check(spline, s, acc);
      gsl_spline_free (spline);
      gsl_interp_accel_free (acc);

      xi[ii] = iy + ii - 1;
    }

    gsl_interp_accel *acc = gsl_interp_accel_alloc ();
    gsl_spline *spline = gsl_spline_alloc (gsl_interp_cspline, 4);    
    gsl_spline_init (spline, xi, yi, 4);
    ret = gsl_spline_eval_check(spline, l, acc);
    gsl_spline_free (spline);
    gsl_interp_accel_free (acc);
 
    return (float)ret;
  }
  
  asfPrintError("Impossible.");
}
int load_lens(int l_size ,int* l_values){
	
	double* lens_data = malloc( sizeof(double)*MAXLINES*3);
	int* lens_len = malloc( sizeof(int));
	
	load_txt_dbl(lens_data_file, 3, lens_data, lens_len);
	
	int lens_size = *lens_len;
	int lmax = (int)get_lmax();
// 	printf("lmax %d\t%d\n",lmax,lens_size);
	double l_raw[lens_size];
	double cltt_raw[lens_size];
	double cltp_raw[lens_size];
	
	int i,j;
	double pt;
	
	j=0;
	for (i=0; i<lmax+1; i++){
		l_raw[i] = lens_data[j++];
		cltt_raw[i] = lens_data[j++];
		cltp_raw[i] = lens_data[j++];
	}
	
	if (l_values[l_size-1]>l_raw[lmax]){
		printf("lens do not contain enough l's, max data %d max lens: %d\n", l_values[l_size-1], (int)l_raw[lmax]);
		return 1;
		exit;
	}
	
	
	gsl_spline* sptt =  gsl_spline_alloc (gsl_interp_cspline, lmax+1);
	gsl_spline* sptp =  gsl_spline_alloc (gsl_interp_cspline, lmax+1);
	gsl_interp_accel* acctt = gsl_interp_accel_alloc();
	gsl_interp_accel* acctp = gsl_interp_accel_alloc();
	
	gsl_spline_init(sptt,l_raw,cltt_raw,lmax+1);
	gsl_spline_init(sptp,l_raw,cltp_raw,lmax+1);
	
	for (i=0; i<l_size; i++){
		pt = (double)l_values[i];
		lens_tt[i] = 0.0;
		lens_tp[i] = 0.0;
		if(pt!=0){
			lens_tt[i] = gsl_spline_eval(sptt,pt,acctt);
			lens_tp[i] = gsl_spline_eval(sptp,pt,acctp);
		}
	}
	
	gsl_spline_free(sptt);
	gsl_spline_free(sptp);
	gsl_interp_accel_free(acctt);
	gsl_interp_accel_free(acctp);
	
	return 0;
}
vector<carmen_ackerman_path_point_t>
simulate_car_from_parameters(TrajectoryLookupTable::TrajectoryDimensions &td,
		TrajectoryLookupTable::TrajectoryControlParameters &tcp, double v0, double i_phi,
		TrajectoryLookupTable::CarLatencyBuffer car_latency_buffer,	bool display_phi_profile)
{
	vector<carmen_ackerman_path_point_t> path;
	if (!tcp.valid)
	{
		printf("Warning: invalid TrajectoryControlParameters tcp in simulate_car_from_parameters()\n");
		return (path);
	}

	// Create phi profile

	gsl_interp_accel *acc;
	gsl_spline *phi_spline;
	if (tcp.has_k1)
	{
		double knots_x[4] = {0.0, tcp.tt / 4.0, tcp.tt / 2.0, tcp.tt};
		double knots_y[4] = {i_phi, tcp.k1, tcp.k2, tcp.k3};
		acc = gsl_interp_accel_alloc();
		const gsl_interp_type *type = gsl_interp_cspline;
		phi_spline = gsl_spline_alloc(type, 4);
		gsl_spline_init(phi_spline, knots_x, knots_y, 4);
	}
	else
	{
		double knots_x[3] = {0.0, tcp.tt / 2.0, tcp.tt};
		double knots_y[3] = {i_phi, tcp.k2, tcp.k3};
		acc = gsl_interp_accel_alloc();
		const gsl_interp_type *type = gsl_interp_cspline;
		phi_spline = gsl_spline_alloc(type, 3);
		gsl_spline_init(phi_spline, knots_x, knots_y, 3);
	}
	print_phi_profile_temp(phi_spline, acc, tcp.tt, display_phi_profile);

	Command command;
	Robot_State robot_state;

	double distance_traveled = compute_path_via_simulation(robot_state, command, path, tcp, phi_spline, acc, v0, i_phi, car_latency_buffer);

	gsl_spline_free(phi_spline);
	gsl_interp_accel_free(acc);
	td.dist = sqrt(robot_state.pose.x * robot_state.pose.x + robot_state.pose.y * robot_state.pose.y);
	td.theta = atan2(robot_state.pose.y, robot_state.pose.x);
	td.d_yaw = robot_state.pose.theta;
	td.phi_i = i_phi;
	td.v_i = v0;
	tcp.vf = command.v;
	tcp.sf = distance_traveled;
	td.control_parameters = tcp;

	return (path);
}
Example #5
0
void
ODEupdate(ODEparams * pa, double *alpha, double *beta, double *filter)
{

	memcpy(pa->alpha, alpha, pa->nap * sizeof(double));
	memcpy(pa->beta, beta, pa->nbp * sizeof(double));
	memcpy(pa->filter, filter, pa->nfp * sizeof(double));
	gsl_spline_init(pa->a_spline, pa->talpha, pa->alpha, pa->nap);
	gsl_spline_init(pa->b_spline, pa->tbeta, pa->beta, pa->nbp);
	gsl_spline_init(pa->f_spline, pa->freqs, pa->filter, pa->nfp);

}
Example #6
0
/// Initialize the spline for numerically calculating the variance
/// This is valid for redshift z=0.0. Should be appropriately multiplied by the
/// growth factor to extend to redshift z.
void cosmology::init_varM_TH_spline()
{
    if(verbose){
    std::cout<<"# "<<"============================================================="<<std::endl;
    std::cout<<"# "<<"The spline for numerical calculation of variance was not initialized. Initializing..."<<std::endl;
    }
    //Define the limits for which to calculate the variance
    double mmin=5.0;
    double mmax=18.0;

    double xx[Nsigma],yy[Nsigma];
    for (int i=0;i<Nsigma;i++)
    {
        double m=mmin+i/(Nsigma-1.)*(mmax-mmin);
        xx[i]=m;
        m=pow(10.,m);
        yy[i]=log(varM_TH(m,0.0))/log(10.);
        //std::cout<<xx[i]<<" "<<yy[i]<<std::endl;
    }
    varM_TH_num_acc = gsl_interp_accel_alloc ();
    varM_TH_num_spline = gsl_spline_alloc (gsl_interp_cspline, Nsigma);
    gsl_spline_init (varM_TH_num_spline,xx,yy,Nsigma);

    bool_init_varM_TH_spline=true;
    if(verbose){
    std::cout<<"# "<<"The spline for numerical calculation of variance is now initialized"<<std::endl;
    std::cout<<"# "<<"============================================================="<<std::endl;
    }
}
Example #7
0
/*! \fn void create_linear_spline(double (*func)(double, void *), double *x, double *&y, int n, double *params, gsl_spline *&spline, gsl_interp_accel *&acc)
 *  \brief Routine to create a spline, interpolated linear.
 */
void create_linear_spline(double (*func)(double, void *), double *x, double *&y, int n, double *params, gsl_spline *&spline, gsl_interp_accel *&acc)
{
	//x contains the locations at which
	//wish to evaluate the function func()

	//y is input unallocated
	//we will allocate it and it will
	//contain func(x)

	//n is the size of the array of x and y
	
	//params is the array of parameters to pass to
	//func(x,params)

	//spline is the unallocated gsl_spline

	//acc is the unallocated gsl_interp_accel

	//allocate y
	y = calloc_double_array(n);

	//evaluate func at x locations
	for(int i=0;i<n;i++)
		y[i] = func(x[i],params);


	//allocate interpolants
	spline = gsl_spline_alloc(gsl_interp_cspline,n);

	acc    = gsl_interp_accel_alloc();

	//create interpoolation
	gsl_spline_init(spline, x, y, n);
}
Example #8
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
}
Example #9
0
static VALUE rb_gsl_spline_new(int argc, VALUE *argv, VALUE klass)
{
  rb_gsl_spline *sp = NULL;
  const gsl_interp_type *T = NULL;
  double *ptrx = NULL, *ptry = NULL;
  size_t sizex = 0, sizey = 0, size = 0, stride = 1;
  int i;
  for (i = 0; i < argc; i++) {
    switch (TYPE(argv[i])) {
    case T_STRING:
      T = get_interp_type(argv[i]);
      break;
    case T_FIXNUM:
      if (T) size = FIX2INT(argv[i]);
      else T = get_interp_type(argv[i]);
      break;
    default:
      if (ptrx == NULL) {
	ptrx = get_vector_ptr(argv[i], &stride, &sizex);
      } else {
	ptry = get_vector_ptr(argv[i], &stride, &sizey);
	size = GSL_MIN_INT(sizex, sizey);
      }
      break;
    }
  }
  if (size == 0) rb_raise(rb_eRuntimeError, "spline size is not given.");
  sp = ALLOC(rb_gsl_spline);
  if (T == NULL) T = gsl_interp_cspline;
  sp->s = gsl_spline_alloc(T, size);
  sp->a = gsl_interp_accel_alloc();
  if (ptrx && ptry) gsl_spline_init(sp->s, ptrx, ptry, size);
  return Data_Wrap_Struct(klass, 0, rb_gsl_spline_free, sp);
}
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;
}
Example #11
0
/// Power spectra numerical implementation. Initializing spline. 
void cosmology::init_powerspectra_L()
{
    if(verbose){
    std::cout<<"# "<<"============================================================="<<std::endl;
    std::cout<<"# "<<"The spline for linear power spectra was not initialized. Initializing..."<<std::endl;
    }
    //Define the limits for which to calculate the variance

    double xx[Npower],yy[Npower];
    for (int i=0;i<Npower;i++)
    {
        double k=kmin+i/(Npower-1.)*(kmax-kmin);
        xx[i]=k;
	k=pow(10.0,k);
	/// Improvement possible here
        yy[i]=log10(Delta2_L(k,0.0));
    }
    PSL0_acc = gsl_interp_accel_alloc ();
    PSL0_spline = gsl_spline_alloc (gsl_interp_cspline, Npower);
    gsl_spline_init (PSL0_spline,xx,yy,Npower);
    PSL0_dlow=(yy[2]-yy[0])/(xx[2]-xx[0]);
    PSL0_xlow=xx[1]; PSL0_ylow=yy[1];
    PSL0_dhigh=(yy[Npower-3]-yy[Npower-1])/(xx[Npower-3]-xx[Npower-1]);
    PSL0_xhigh=xx[Npower-2]; PSL0_yhigh=yy[Npower-2];

    bool_init_PSL0=true;
    if(verbose){
    std::cout<<"# "<<"The spline for numerical calculation of linear power spectra is now initialized"<<std::endl;
    std::cout<<"# "<<"============================================================="<<std::endl;
    }
       
}
Example #12
0
void DosLifetimeSpline::dospline(){
    //calculate the spline coefficients
     gsl_spline_init (sp, &Evector[0], &Yvector[0], Evector.size());



}
void regrid_sed(double z,double *plam,double *pval,long finelength, \
				long sedlength,double *fineplam,double *finepval) {
	
    long ii;
    double x[sedlength],y[sedlength];
	
    for (ii=0;ii<sedlength;ii++) {
        x[ii] = *(plam+ii) * (1.0 + z);
        y[ii] = *(pval+ii);
    }
	
    gsl_interp_accel *acc = gsl_interp_accel_alloc();
    gsl_spline *spline    = gsl_spline_alloc(gsl_interp_cspline, sedlength);
    
    gsl_spline_init(spline, x, y, sedlength);
	
    for (ii=0; ii < finelength; ii++) {
        if (*(fineplam+ii)/(1.0 + z) < *(plam+0)) {
            *(finepval+ii) = 0.0;
        } else {
            *(finepval+ii) = gsl_spline_eval (spline, *(fineplam+ii), acc);
        }
        if (*(finepval+ii) < 0.0)
            *(finepval+ii) = 0.0;
    }
	
    gsl_spline_free(spline);
    gsl_interp_accel_free(acc);
}
Example #14
0
int
main (void)
{
  int i;
  double xi, yi;
  double x[10], y[10];

  printf ("#m=0,S=2\n");

  for (i = 0; i < 10; i++)
    {
      x[i] = i + 0.5 * sin (i);
      y[i] = i + cos (i * i);
      printf ("%g %g\n", x[i], y[i]);
    }

  printf ("#m=1,S=0\n");

  {
    gsl_interp_accel *acc = gsl_interp_accel_alloc ();
    gsl_spline *spline = gsl_spline_alloc (gsl_interp_cspline, 10);
    gsl_spline_init (spline, x, y, 10);

    for (xi = x[0]; xi < x[9]; xi += 0.01)
      {
        double yi = gsl_spline_eval (spline, xi, acc);
        printf ("%g %g\n", xi, yi);
      }
    gsl_spline_free (spline);
    gsl_interp_accel_free(acc);
  }
}
Example #15
0
scalar sasfit_sd_cspline8(scalar x, sasfit_param * param)
{
	scalar tmp, xcs[10], ycs[10];
	int i;
	SASFIT_ASSERT_PTR(param); // assert pointer param is valid

	if (x < XMIN) return 0;
	if (x > XMAX) return 0;

	if (XMIN > XMAX) {
		tmp = XMAX;
		XMAX = XMIN;
		XMIN = tmp;
	}
	xcs[0]=XMIN;
	ycs[0]=0;
	xcs[9]=XMAX;
	ycs[9]=0;
	for (i=1; i<=8; i++) {
		xcs[i] = XMIN+i*(XMAX-XMIN)/(8.0+1.0);
		ycs[i] = param->p[1+i];
	}
	gsl_spline_init(sdcspline8_T, xcs, ycs, 10);

	return gsl_spline_eval (sdcspline8_T, x, acc_cspline);
}
UNUSED static REAL8 XLALSimInspiralNRWaveformGetRefTimeFromRefFreq(
  UNUSED LALH5File* file,
  UNUSED REAL8 fRef
)
{
  #ifndef LAL_HDF5_ENABLED
  XLAL_ERROR(XLAL_EFAILED, "HDF5 support not enabled");
  #else
  /* NOTE: This is an internal function, it is expected that fRef is scaled
   * to correspond to the fRef with a total mass of 1 solar mass */
  LALH5File *curr_group = NULL;
  gsl_vector *omega_t_vec = NULL;
  gsl_vector *omega_w_vec = NULL;
  gsl_interp_accel *acc;
  gsl_spline *spline;
  REAL8 ref_time;
  curr_group = XLALH5GroupOpen(file, "Omega-vs-time");
  ReadHDF5RealVectorDataset(curr_group, "X", &omega_t_vec);
  ReadHDF5RealVectorDataset(curr_group, "Y", &omega_w_vec);
  acc = gsl_interp_accel_alloc();
  spline = gsl_spline_alloc(gsl_interp_cspline, omega_w_vec->size);
  gsl_spline_init(spline, omega_w_vec->data, omega_t_vec->data,
                  omega_t_vec->size);
  ref_time = gsl_spline_eval(spline, fRef * (LAL_MTSUN_SI * LAL_PI), acc);
  gsl_vector_free(omega_t_vec);
  gsl_vector_free(omega_w_vec);
  gsl_spline_free(spline);
  gsl_interp_accel_free(acc);
  return ref_time;
  #endif
}
Example #17
0
int
main (void)
{
  int N = 4;
  double x[4] = {0.00, 0.10,  0.27,  0.30};
  double y[4] = {0.15, 0.70, -0.10,  0.15}; /* Note: first = last 
                                               for periodic data */

  gsl_interp_accel *acc = gsl_interp_accel_alloc ();
  const gsl_interp_type *t = gsl_interp_cspline_periodic; 
  gsl_spline *spline = gsl_spline_alloc (t, N);

  int i; double xi, yi;

  printf ("#m=0,S=5\n");
  for (i = 0; i < N; i++)
    {
      printf ("%g %g\n", x[i], y[i]);
    }

  printf ("#m=1,S=0\n");
  gsl_spline_init (spline, x, y, N);

  for (i = 0; i <= 100; i++)
    {
      xi = (1 - i / 100.0) * x[0] + (i / 100.0) * x[N-1];
      yi = gsl_spline_eval (spline, xi, acc);
      printf ("%g %g\n", xi, yi);
    }
  
  gsl_spline_free (spline);
  gsl_interp_accel_free (acc);
  return 0;
}
UNUSED static REAL8 XLALSimInspiralNRWaveformGetInterpValueFromGroupAtPoint(
  UNUSED LALH5File* file,                 /**< Pointer to HDF5 file */
  UNUSED const char *groupName,           /**< Name of group in HDF file */
  UNUSED REAL8 ref_point                  /**< Point at which to evaluate */
  )
{
  #ifndef LAL_HDF5_ENABLED
  XLAL_ERROR(XLAL_EFAILED, "HDF5 support not enabled");
  #else
  LALH5File *curr_group = NULL;
  gsl_vector *curr_t_vec = NULL;
  gsl_vector *curr_y_vec = NULL;
  gsl_interp_accel *acc;
  gsl_spline *spline;
  REAL8 ret_val;
  curr_group = XLALH5GroupOpen(file, groupName);
  ReadHDF5RealVectorDataset(curr_group, "X", &curr_t_vec);
  ReadHDF5RealVectorDataset(curr_group, "Y", &curr_y_vec);
  acc = gsl_interp_accel_alloc();
  spline = gsl_spline_alloc(gsl_interp_cspline, curr_t_vec->size);
  gsl_spline_init(spline, curr_t_vec->data, curr_y_vec->data,
                  curr_t_vec->size);
  ret_val = gsl_spline_eval(spline, ref_point, acc);
  gsl_vector_free(curr_t_vec);
  gsl_vector_free(curr_y_vec);
  gsl_spline_free (spline);
  gsl_interp_accel_free (acc);
  return ret_val;
  #endif
}
Example #19
0
void spline(double *YY, 
		double *X, double *Y, double *XX, int m1, int m2) {

	// m1: length of discrete extremas
	// m2: length of original data points

	gsl_interp_accel *acc = gsl_interp_accel_alloc ();
  	const gsl_interp_type *t = gsl_interp_cspline; 
  	gsl_spline *spline = gsl_spline_alloc (t, m1);

	/* Core function */

  	gsl_spline_init (spline, X, Y, m1);

	double m;
	
	if (m1 > 2 ) {
		for (int j = 0; j < m2; j++) {
			YY[j] = gsl_spline_eval (spline, XX[j], acc);
		} // end of for-j
	} // end of if

	else {
		m = (Y[1] - Y[0]) / (X[1] - X[0]);
		for (int j = 0; j < m2; j++) {
			YY[j] = Y[0] + m * (XX[j] - X[0]);		
		} // end of for-j
	} //end of else

	// delete[] xd;
	gsl_spline_free (spline);
  	gsl_interp_accel_free (acc);

}
Example #20
0
double cMorph::AkimaInterpolate(const double factor, double v1, double v2, double v3, double v4,
		double v5, double v6, const bool angular)
{
	if (angular)
	{
		QList<double*> vals;
		vals << &v1 << &v2 << &v3 << &v4 << &v5 << &v6;
		NearestNeighbourAngle(vals);
	}

	double x[] = { -2, -1, 0, 1, 2, 3 };
	double y[] = { v1, v2, v3, v4, v5, v6 };
	// more info: http://www.alglib.net/interpolation/spline3.php
	gsl_spline_init(splineAkimaPeriodic, x, y, listSize);
	double value = gsl_spline_eval(splineAkimaPeriodic, factor, interpolationAccelerator);

	if (angular)
	{
		return LimitAngle(value);
	}
	else
	{
		return value;
	}
}
Example #21
0
void prep_et() {
    /* Set ups the spline to sample et. */
    double et;
    double gn0 = -1, gt0;

    slLimit = 6*mu - mu*kn/kt;

    double *x = (double*)calloc(100, sizeof(double));
    double *y = (double*)calloc(100, sizeof(double));

    /* Write first point. */
    x[0] = 0; y[0] = cos(sqrt(3*kt/meff)*t_col());
    /* Calculate intermediate points. */
    long i;
    for (i = 1; i < 100; i++) {
        gt0 = slLimit/100.0*i;
        double ratio = fabs(gt0/(double)gn0);
        et = calculate_et(gn0, gt0);
        x[i] = ratio;
        y[i] = et;
    }
    /* Write last point. */
    x[100] = 6*mu - mu*kn/kt;
    y[100] = 1 - 3*mu*(1 + calc_en())/x[100];

    /* Interpolation with cubic spline. */
    acc = gsl_interp_accel_alloc();
    spline = gsl_spline_alloc(gsl_interp_cspline, 101);
    gsl_spline_init(spline, x, y, 101);
}
void Interpolation::calculateOutputData(double *x, double *y)
{
	gsl_interp_accel *acc = gsl_interp_accel_alloc ();
	const gsl_interp_type *method;
	switch(d_method)
	{
		case 0:
			method = gsl_interp_linear;
			break;
		case 1:
			method = gsl_interp_cspline;
			break;
		case 2:
			method = gsl_interp_akima;
			break;
	}

	gsl_spline *interp = gsl_spline_alloc (method, d_n);
	gsl_spline_init (interp, d_x, d_y, d_n);

    double step = (d_to - d_from)/(double)(d_points - 1);
    for (int j = 0; j < d_points; j++)
	{
	   x[j] = d_from + j*step;
	   y[j] = gsl_spline_eval (interp, x[j], acc);
	}

	gsl_spline_free (interp);
	gsl_interp_accel_free (acc);
}
Example #23
0
double calc_corr_at_R(double R,double*k,double*P,int Nk,int N,double h){
  double zero,psi,x,t,dpsi,f,PIsinht;
  double PI_h = PI/h;
  double PI_2 = PI*0.5;
  gsl_spline*Pspl = gsl_spline_alloc(gsl_interp_cspline,Nk);
  gsl_spline_init(Pspl,k,P,Nk);
  gsl_interp_accel*acc= gsl_interp_accel_alloc();

  double sum = 0;
  int i;
  for(i=0;i<N;i++){
    zero = i+1;
    psi = h*zero*tanh(sinh(h*zero)*PI_2);
    x = psi*PI_h;
    t = h*zero;
    PIsinht = PI*sinh(t);
    dpsi = (PI*t*cosh(t)+sinh(PIsinht))/(1+cosh(PIsinht));
    if (dpsi!=dpsi) dpsi=1.0;
    f = x*get_P(x,R,k,P,Nk,Pspl,acc);
    sum += f*sin(x)*dpsi;
  }

  gsl_spline_free(Pspl),gsl_interp_accel_free(acc);
  return sum/(R*R*R*PI*2);
}
int core_oph_gsl_spline_multi(oph_multistring* byte_array, oph_multistring* result, oph_gsl_spline_param* spline)
{
	int j,k;
	double tmp, *pointer;
	char *in_string = byte_array->content, *out_string = result->content;
	char *in_pointer, *out_pointer;
	for (j=0;j<byte_array->num_measure;++j)
	{
		in_pointer = in_string;
		switch(byte_array->type[j])
		{
		        case OPH_DOUBLE:{
				pointer = (double*)in_string;
		                break;
		        }
			case OPH_FLOAT:{
				pointer = spline->tmp;
				for (k=0;k<byte_array->numelem;++k) { spline->tmp[k] = *(float*)in_pointer; in_pointer += byte_array->blocksize; }
		                break;
		        }
			case OPH_INT:{
				pointer = spline->tmp;
				for (k=0;k<byte_array->numelem;++k) { spline->tmp[k] = *(int*)in_pointer; in_pointer += byte_array->blocksize; }
		                break;
		        }
			case OPH_SHORT:{
				pointer = spline->tmp;
				for (k=0;k<byte_array->numelem;++k) { spline->tmp[k] = *(short*)in_pointer; in_pointer += byte_array->blocksize; }
		                break;
		        }
			case OPH_BYTE:{
				pointer = spline->tmp;
				for (k=0;k<byte_array->numelem;++k) { spline->tmp[k] = *(char*)in_pointer; in_pointer += byte_array->blocksize; }
		                break;
		        }
			case OPH_LONG:{
				pointer = spline->tmp;
				for (k=0;k<byte_array->numelem;++k) { spline->tmp[k] = *(long long*)in_pointer; in_pointer += byte_array->blocksize; }
		                break;
		        }
			default:
		                pmesg(1, __FILE__, __LINE__, "Type not recognized\n");
		                return  -1;
		}
		if (gsl_spline_init(spline->spline, spline->old_x, pointer, byte_array->numelem)) return -1;
		out_pointer = out_string;
		for (k=0;k<result->numelem;++k)
		{
			if ((spline->new_x[k] < spline->old_x[0]) || (spline->new_x[k] > spline->old_x[byte_array->numelem-1])) tmp = NAN;
			else tmp = gsl_spline_eval(spline->spline, spline->new_x[k], spline->acc);
			if (core_oph_type_cast(&tmp, out_pointer, OPH_DOUBLE, result->type[j])) return -1;
			out_pointer += result->blocksize;
		}
		in_string += byte_array->elemsize[j];
		out_string += result->elemsize[j];
	}
	return 0;
}
UNUSED static UINT4 XLALSimInspiralNRWaveformGetDataFromHDF5File(
  UNUSED REAL8Vector** output,            /**< Returned vector uncompressed */
  UNUSED LALH5File* pointer,              /**< Pointer to HDF5 file */
  UNUSED REAL8 totalMass,                 /**< Total mass of system for scaling */
  UNUSED REAL8 startTime,                 /**< Start time of veturn vector */
  UNUSED size_t length,                   /**< Length of returned vector */
  UNUSED REAL8 deltaT,                    /**< Sample rate of returned vector */
  UNUSED const char *keyName              /**< Name of vector to uncompress */
  )
{
  #ifndef LAL_HDF5_ENABLED
  XLAL_ERROR(XLAL_EFAILED, "HDF5 support not enabled");
  #else
  UINT4 idx;
  size_t comp_data_length;
  REAL8 massTime;
  gsl_interp_accel *acc;
  gsl_spline *spline;
  gsl_vector *knotsVector, *dataVector;
  LALH5File *group = XLALH5GroupOpen(pointer, keyName);
  knotsVector=dataVector=NULL;

  ReadHDF5RealVectorDataset(group, "X", &knotsVector);
  ReadHDF5RealVectorDataset(group, "Y", &dataVector);

  *output = XLALCreateREAL8Vector(length);

  comp_data_length = dataVector->size;
  /* SPLINE STUFF */
  acc = gsl_interp_accel_alloc();
  spline = gsl_spline_alloc(gsl_interp_cspline, comp_data_length);
  gsl_spline_init(spline, knotsVector->data, dataVector->data,
                  comp_data_length);

  for (idx = 0; idx < length; idx++)
  {
    massTime = (startTime + idx*deltaT) / (totalMass * LAL_MTSUN_SI);
    /* This if statement is used to catch the case where massTime at idx=0
     * ends up at double precision smaller than the first point in the
     * interpolation. In this case set it back to exactly the first point.
     * Sanity checking that we are not trying to use data below the
     * interpolation range is done elsewhere.
     */
    if ((idx == 0) && (massTime < knotsVector->data[0]))
    {
      massTime = knotsVector->data[0];
    }
    (*output)->data[idx] = gsl_spline_eval(spline, massTime, acc);
  }

  gsl_vector_free(knotsVector);
  gsl_vector_free(dataVector);
  gsl_spline_free (spline);
  gsl_interp_accel_free (acc);

  return XLAL_SUCCESS;
  #endif
}
void compute_correlation_func(int ObsNr, double *binsamdata, int snap, float mingalmass, float maxgalmass)
{
	double *r,*proj,*r_tmp,*proj_tmp;
	int ibin, ii, jj;
	char buf[1000];
	FILE *fa;
	gsl_spline *Proj_Spline;
	gsl_interp_accel *Proj_SplineAcc;

	NR=60;

	//printf("\ncalculating correlation function %0.2f < M < %0.2f\n",mingalmass,maxgalmass);

	r=malloc(NR*sizeof(double));
	proj=malloc(NR*sizeof(double));

	halomodel(r,proj,mingalmass,maxgalmass,snap);

	Proj_Spline=gsl_spline_alloc(gsl_interp_cspline,NR);
	Proj_SplineAcc=gsl_interp_accel_alloc();
	gsl_spline_init(Proj_Spline,r,proj,NR);

	//for(ii=0;ii<Nbins[snap][ObsNr]-1;ii++)
	//	printf("Nbins=%d r=%g\n",Nbins[snap][ObsNr], MCMC_Obs[ObsNr].Bin_low[snap][ii]+(MCMC_Obs[ObsNr].Bin_high[snap][ii]-MCMC_Obs[ObsNr].Bin_low[snap][ii])/2.)

	 for(ii=0;ii<Nbins[snap][ObsNr]-1;ii++)
		 binsamdata[ii]=gsl_spline_eval(Proj_Spline,MCMC_Obs[ObsNr].Bin_low[snap][ii]+(MCMC_Obs[ObsNr].Bin_high[snap][ii]-MCMC_Obs[ObsNr].Bin_low[snap][ii])/2.,Proj_SplineAcc);

#ifndef PARALLEL
	 //full3 - full PLANCK
	sprintf(buf, "%s/correlation_guo10_bug_fix_full_z0.02_%0.2f_%0.2f.txt",OutputDir, mingalmass,maxgalmass);
	if(!(fa = fopen(buf, "w")))
	{
		char sbuf[1000];
		sprintf(sbuf, "can't open file `%s'\n", buf);
		terminate(sbuf);
	}
	for(ii=0;ii<Nbins[snap][ObsNr]-1;ii++)
		fprintf(fa, "%g %g %g\n", MCMC_Obs[ObsNr].Bin_low[snap][ii]+(MCMC_Obs[ObsNr].Bin_high[snap][ii]-MCMC_Obs[ObsNr].Bin_low[snap][ii])/2.,binsamdata[ii],binsamdata[ii]*0.1);
	//for(ii=0;ii<NR;ii++)
	//fprintf(fa, "%g %g %g\n", r[ii],proj[ii],proj[ii]*0.1);
	fclose(fa);
#endif

	//original wrp calculation out of halo_model
	//printf("\n\n %g<M<%g",mingalmass,maxgalmass);
	//for(ii=0;ii<NR;ii++)
	//	printf("r=%g proj=%g\n",r[ii],proj[ii]);
  //interpolated into obs bins
	//for(ii=0;ii<Nbins[snap][ObsNr]-1;ii++)
	//	printf("%g %g %g\n", MCMC_Obs[ObsNr].Bin_low[snap][ii]+(MCMC_Obs[ObsNr].Bin_high[snap][ii]-MCMC_Obs[ObsNr].Bin_low[snap][ii])/2.,binsamdata[ii],binsamdata[ii]*0.1);

	 free(r);
	 free(proj);
	 gsl_spline_free(Proj_Spline);
	 gsl_interp_accel_free(Proj_SplineAcc);
}
Example #27
0
void FC_FUNC_(oct_spline_fit, OCT_SPLINE_FIT)
		 (const int *nrc, const double *x, const double *y, void **spl, void **acc)
{
  /* the GSL headers actually specify size_t instead of const int for nrc */
  *acc = (void *)gsl_interp_accel_alloc();
  *spl = (void *)gsl_spline_alloc(gsl_interp_cspline, *nrc);	
  gsl_spline_init((gsl_spline *)(*spl), x, y, *nrc);
  fflush(stdout);
}
Example #28
0
void read_red_dist(void)
{
  //////
  // Reads redshift distribution and creates
  // array for interpolation
  FILE *fdist;
  double *zarr,*dndz_dist_arr;
  int n_z_dist;
  int ii;

  fdist=fopen(fnamedNdz,"r");
  if(fdist==NULL) error_open_file(fnamedNdz);
  print_info("*** Reading redshift selection function ");
#ifdef _VERBOSE
  print_info("from file %s",fnamedNdz);
#endif //_VERBOSE
  print_info("\n");

  n_z_dist=linecount(fdist);
  rewind(fdist);
  
  dndz_dist_arr=(double *)my_malloc(n_z_dist*sizeof(double));
  zarr=(double *)my_malloc(n_z_dist*sizeof(double));

  for(ii=0;ii<n_z_dist;ii++) {
    int sr=fscanf(fdist,"%lf %lf",&(zarr[ii]),&(dndz_dist_arr[ii]));
    if(sr!=2) error_read_line(fnamedNdz,ii+1);
    if(dndz_dist_arr[ii]>=dist_max)
      dist_max=dndz_dist_arr[ii];
  }
  fclose(fdist);
  dist_max*=1.1;

  dndz_set=1;

#ifdef _DEBUG
  char dfname[64]="debug_dndz.dat";
  fdist=fopen(dfname,"w");
  if(fdist==NULL) error_open_file(dfname);
  for(ii=0;ii<n_z_dist;ii++)
    fprintf(fdist,"%lf %lf \n",zarr[ii],dndz_dist_arr[ii]);
  fclose(fdist);
#endif //_DEBUG

  redshift_0=zarr[0];
  redshift_f=zarr[n_z_dist-1];

  cute_intacc_dndz=gsl_interp_accel_alloc();
  cute_spline_dndz=gsl_spline_alloc(gsl_interp_cspline,n_z_dist);
  gsl_spline_init(cute_spline_dndz,zarr,dndz_dist_arr,n_z_dist);

  free(zarr);
  free(dndz_dist_arr);
  
  print_info("\n");
}
Example #29
0
ODEparams * init(int nap,double * alpha,double * talpha, int nbp, double* beta, double *tbeta,double gamma, double x0,double y0)
{
	ODEparams * pa= (ODEparams*)malloc(sizeof(ODEparams));

	pa->gamma=gamma;
	pa->x0=x0;
	pa->y0=y0;
	
	pa->nap=nap;
	pa->nbp=nbp;

	pa->alpha=calloc(nap,sizeof(double));
	pa->talpha=calloc(nap,sizeof(double));
	pa->beta=calloc(nbp,sizeof(double));
	pa->tbeta=calloc(nbp,sizeof(double));
	int i;
	memcpy(pa->alpha,alpha,nap*sizeof(double));
	memcpy(pa->talpha,talpha,nap*sizeof(double));
	memcpy(pa->beta,beta,nbp*sizeof(double));
	memcpy(pa->tbeta,tbeta,nbp*sizeof(double));
/*	int i;
/	for(i=0;i<nbp;i++) printf("%lg",pa->tb[i]);*/
    
	pa->a_acc=gsl_interp_accel_alloc ();
	pa->b_acc=gsl_interp_accel_alloc ();
	pa->a_spline = gsl_spline_alloc (gsl_interp_linear,nap);

	int err =	gsl_spline_init (pa->a_spline, pa->talpha, pa->alpha, pa->nap);
	if (err != GSL_SUCCESS)
		{
			mexErrMsgTxt("error: init vector interp - not good size / not increasing");
		}
	pa->b_spline = gsl_spline_alloc (gsl_interp_linear,nbp);
	err = gsl_spline_init (pa->b_spline, pa->tbeta, pa->beta, pa->nbp);
	if (err != GSL_SUCCESS)
		{
		mexErrMsgTxt("error: init vector interp - not good size / not increasing");
		}	
	pa->gamma=gamma;
	pa->x0=x0;
	pa->y0=y0;
	return pa;
}
Example #30
0
static VALUE rb_gsl_spline_init(VALUE obj, VALUE xxa, VALUE yya)
{
  rb_gsl_spline *sp = NULL;
  gsl_spline *p = NULL;
  gsl_vector *xa = NULL, *ya = NULL;
  size_t i, size;
  int flagx = 0, flagy = 0;
  double *ptr1 = NULL, *ptr2 = NULL;
#ifdef HAVE_NARRAY_H
  struct NARRAY *nax = NULL, *nay = NULL;
#endif
  Data_Get_Struct(obj, rb_gsl_spline, sp);
  p = sp->s;
  if (TYPE(xxa) == T_ARRAY) {
    size = RARRAY_LEN(xxa);
    xa = gsl_vector_alloc(size);
    for (i = 0; i < size; i++) gsl_vector_set(xa, i, NUM2DBL(rb_ary_entry(xxa, i)));
    ptr1 = xa->data;
    flagx = 1;
  } else if (VECTOR_P(xxa)) {
    Data_Get_Struct(xxa, gsl_vector, xa);
    size = xa->size;
    ptr1 = xa->data;
#ifdef HAVE_NARRAY_H
  } else if (NA_IsNArray(xxa)) {
      GetNArray(xxa, nax);
      size = nax->total;
      ptr1 = (double *) nax->ptr;
#endif
  } else {
    rb_raise(rb_eTypeError, "not a vector");
  }
  if (TYPE(yya) == T_ARRAY) {
    ya = gsl_vector_alloc(size);
    for (i = 0; i < size; i++) gsl_vector_set(ya, i, NUM2DBL(rb_ary_entry(yya, i)));
    ptr2 = ya->data;
    flagy = 1;
#ifdef HAVE_NARRAY_H
  } else if (NA_IsNArray(yya)) {
      GetNArray(yya, nay);
      ptr2 = (double *) nay->ptr;
#endif
  } else if (VECTOR_P(yya)) {
    Data_Get_Struct(yya, gsl_vector, ya);
    ptr2 = ya->data;
  } else {
    rb_raise(rb_eTypeError, "not a vector");
  }
  gsl_spline_init(p, ptr1, ptr2, size);
  if (flagx == 1) gsl_vector_free(xa);
  if (flagy == 1) gsl_vector_free(ya);
  return obj;
}