Exemplo n.º 1
0
void shearlayerkcrit_driver(char *input_file_name)
{
    PARAMS_STRUCT *params;
    GRID_STRUCT *grid;
    ROTATION_STRUCT *rotation;
    COMPRESSED_MATRIX *matrix;
    ARPACK_CONTROL *arpack_params;
    RESULTS_STRUCT *results;
    OUTPUT_CONTROL *output_control;

    double shear_width, shear_radius, E;

    //Parameters needed for the root-finding routine
    int status;
    int iter=0, max_iter=50;
    const gsl_root_fsolver_type *Troot;
    const gsl_min_fminimizer_type *Tmin;
    gsl_root_fsolver *sroot;
    gsl_min_fminimizer *smin;
    double k_low, k_high, k_guess;
    double k_min = NAN;
    double k_max = NAN;
    double k_peak = NAN;
    double gr_peak;
    double errabs, errrel;
    double width_prefactor;
    gsl_function F;
    struct FUNCTION_PARAMS function_params;

    //Get the physical parameters for the computation
    params = malloc(sizeof(PARAMS_STRUCT));
    probgen(input_file_name, params);

    //Set up the grid, based on the physical parameters
    grid = gridgen(params);

    //Set up the rotation profile of a shear layer. Derive the width
    //from the Ekman number, E=\nu/\Omega r^2, width = rE^(1/4)
    //Use r = (r2-r1) and Omega = (Omega1-Omega2)/2.

    shear_radius = get_dparam("shear_radius", input_file_name);
    /*
    width_prefactor = get_dparam("width_prefactor", input_file_name);
    E = params->nu/(0.5*fabs(params->omega1 - params->omega2) *
    	  pow((params->r2-params->r1),2));
    shear_width = width_prefactor*(params->r2-params->r1)*pow(E, 0.25);
    printf("Using shear layer width %g cm\n", shear_width);
    */
    shear_width = get_dparam("shear_width", input_file_name);
    rotation = shearlayer(params, grid, shear_width, shear_radius);

    //Set up the matrix structure for the computations.
    matrix = create_matrix(5*grid->numcells);

    //Setup the ARPACK parameters
    arpack_params = setup_arpack(input_file_name);

    //Setup the output control structure
    output_control = malloc(sizeof(OUTPUT_CONTROL));

    //Pull the error params from the input file to decide when
    //we have converged
    errabs = get_dparam("errabs", input_file_name);
    errrel = get_dparam("errrel", input_file_name);

    //Put pointers to all of our control structures in function_params
    function_params.params = params;
    function_params.grid = grid;
    function_params.rotation = rotation;
    function_params.matrix = matrix;
    function_params.arpack_params = arpack_params;

    //Assign the evaluation function and params structure to
    //the gsl_function
    F.function = &mindampingrate;
    F.params = &function_params;

    gsl_set_error_handler(&err_handler);

    /* Now we find the peak of the growth rate, by minimizing the
       damping rate. We set what we hope are reasonable numbers
       for the bounds and initial guess.
    */

    k_low = 0.01;
    k_high = 1000;
    k_guess = params->k;
    Tmin = gsl_min_fminimizer_brent;
    smin = gsl_min_fminimizer_alloc(Tmin);
    status = gsl_min_fminimizer_set(smin, &F, k_guess, k_low, k_high);
    //Make sure that we didn't thrown an error on initialization
    if (status == GSL_SUCCESS) {
        //Now iterate!
        iter = 0;
        do
        {
            iter++;
            status = gsl_min_fminimizer_iterate(smin);
            //Make sure that we didn't thrown an error in the iteration routine
            if (status != GSL_SUCCESS) {
                fprintf(stderr, "Aborted attempt to find k_peak.\n");
                break;
            }

            params->k = gsl_min_fminimizer_x_minimum(smin);
            k_low = gsl_min_fminimizer_x_lower(smin);
            k_high = gsl_min_fminimizer_x_upper(smin);
            status = gsl_min_test_interval(k_low, k_high, errabs, errrel);

            if(status == GSL_SUCCESS && params->VERBOSE) {
                printf("Converged with k_peak=%g\n", params->k);
            }
        }
        while (status == GSL_CONTINUE && iter < max_iter);
        //Save the peak growth rate for printing later, then free the solver
        gr_peak = -gsl_min_fminimizer_f_minimum(smin);
    } else {
        fprintf(stderr, "Aborted attempt to find k_peak.\n");
    }
    gsl_min_fminimizer_free(smin);

    //Check to make sure we converged. If not, don't save the results.
    if (status == GSL_SUCCESS) {
        k_peak = params->k;

        //Make sure everything is set up correctly for normal run
        params->kva = params->k*params->va;
        free(grid->r);
        free(grid->x);
        free(grid->r2inv);
        free(grid);
        grid = gridgen(params);

        //Now do a normal run with the chosen k
        arpack_params->sigma = find_sigma(matrix, params, grid, rotation,
                                          arpack_params);
        results = eigensolve(matrix, params, grid, rotation, arpack_params);

        //Setup the structures needed to output the data files, and write them.
        get_sparam("basefilename", input_file_name, output_control->basefilename);
        strcat(output_control->basefilename, "_kpeak");
        wnetcdf(params, grid, rotation, output_control, arpack_params, results);

        free(results->lambda);
        free(results->z);
        free(results->residual);
        free(results);
    }


    /* Now do a root finding search for k_min. */

    /*
    //Set up the root solver.
    Troot = gsl_root_fsolver_brent;
    sroot = gsl_root_fsolver_alloc(Troot);

    //Set the initial bounds for the search. We're searching for k_min,
    //so search from 0 up to k_peak.
    k_low = 0;
    k_high = k_peak;
    status = gsl_root_fsolver_set(sroot, &F, k_low, k_high);
    //Make sure that we didn't thrown an error on initialization
    if (status == GSL_SUCCESS) {
      //Now iterate!
      iter = 0;
      do
        {
    iter++;
    status = gsl_root_fsolver_iterate(sroot);
    //Make sure that we didn't thrown an error in the iteration routine
    if (status != GSL_SUCCESS) {
      fprintf(stderr, "Aborted attempt to find k_min.\n");
      break;
    }

    params->k = gsl_root_fsolver_root(sroot);
    k_low = gsl_root_fsolver_x_lower(sroot);
    k_high = gsl_root_fsolver_x_upper(sroot);
    status = gsl_root_test_interval(k_low, k_high, errabs, errrel);

    if(status == GSL_SUCCESS && params->VERBOSE) {
      printf("Converged with k_min=%g\n", params->k);
    }
        }
      while (status == GSL_CONTINUE && iter < max_iter);
    } else {
      fprintf(stderr, "Aborted attempt to find k_min.\n");
    }
    gsl_root_fsolver_free (sroot);

    //Check to make sure we converged. If not, don't save the results.
    if (status == GSL_SUCCESS) {
      k_min = params->k;

      //Make sure everything is set up correctly for the normal run
      params->kva = params->k*params->va;
      free(grid->r);
      free(grid->x);
      free(grid->r2inv);
      free(grid);
      grid = gridgen(params);

      //Now do a normal run with the chosen k
      arpack_params->sigma = find_sigma(matrix, params, grid, rotation,
    			      arpack_params);
      results = eigensolve(matrix, params, grid, rotation, arpack_params);

      //Set the new file name, and write the output
      get_sparam("basefilename", input_file_name, output_control->basefilename);
      strcat(output_control->basefilename, "_kmin");
      wnetcdf(params, grid, rotation, output_control, arpack_params, results);

      free(results->lambda);
      free(results->z);
      free(results->residual);
      free(results);
    }
    */

    /* Now move on to solving for k_max. */
    Troot = gsl_root_fsolver_brent;
    sroot = gsl_root_fsolver_alloc(Troot);

    //Set the initial bounds for the search. We're searching for k_max,
    //so search from k_peak to a large number
    k_low = k_peak;
    k_high = 10000;
    status = gsl_root_fsolver_set(sroot, &F, k_low, k_high);
    //Make sure that we didn't thrown an error on initialization
    if (status == GSL_SUCCESS) {
        //Now iterate!
        iter = 0;
        do
        {
            iter++;
            status = gsl_root_fsolver_iterate(sroot);
            //Make sure that we didn't thrown an error in the iteration routine
            if (status != GSL_SUCCESS) {
                fprintf(stderr, "Aborted attempt to find k_max.\n");
                break;
            }

            params->k = gsl_root_fsolver_root(sroot);
            k_low = gsl_root_fsolver_x_lower(sroot);
            k_high = gsl_root_fsolver_x_upper(sroot);
            status = gsl_root_test_interval(k_low, k_high, errabs, errrel);

            if(status == GSL_SUCCESS && params->VERBOSE) {
                printf("Converged with k_max=%g\n", params->k);
            }
        }
        while (status == GSL_CONTINUE && iter < max_iter);
    } else {
        fprintf(stderr, "Aborted attempt to find k_max.\n");
    }
    gsl_root_fsolver_free (sroot);

    //Check to make sure we converged. If not, don't save the results.
    if (status == GSL_SUCCESS) {
        k_max = params->k;

        //Make sure everything is set up correctly for the normal run
        params->kva = params->k*params->va;
        free(grid->r);
        free(grid->x);
        free(grid->r2inv);
        free(grid);
        grid = gridgen(params);

        //Now do a normal run with the chosen k
        arpack_params->sigma = find_sigma(matrix, params, grid, rotation,
                                          arpack_params);
        results = eigensolve(matrix, params, grid, rotation, arpack_params);

        //Set the new file name, and write the output
        get_sparam("basefilename", input_file_name, output_control->basefilename);
        strcat(output_control->basefilename, "_kmax");
        wnetcdf(params, grid, rotation, output_control, arpack_params, results);

        free(results->lambda);
        free(results->z);
        free(results->residual);
        free(results);
    }

    printf("Found k_min = %g, k_peak = %g, k_max = %g\n", k_min, k_peak, k_max);
    printf("Peak growth rate: %g\n", gr_peak);

    free(matrix->A);
    free(matrix->B);
    free(matrix->Bb);
    free(matrix);
    free(params);
    free(grid->r);
    free(grid->x);
    free(grid->r2inv);
    free(grid);
    free(rotation->omega);
    free(rotation);
    free(output_control);

    return;
}
Exemplo n.º 2
0
double eta(double q[2], double qdot[2], double t, double eta0) {

	// calculates the auxiliary variable eta by solving Kepler's equation
	// the equation is solved by searching for a root
	// using the brent's algorithm root bracketer in GSL
	
	// definitions for root solver
	
	const gsl_root_fsolver_type *T = gsl_root_fsolver_brent;
	gsl_root_fsolver *s;
	gsl_function keq;
	
	// iteration limits for root solver
	
	double epsabs=0.0;
	double epsrel = 1e-10;
	size_t max_iter=100;
	size_t iter=0;
	int status;
	double xlo,xhi;
	double r;
	
	//compute e, c, omega3, initial theta3
	
	double e = eccentricity(q, qdot);
	double c = c_aux(q, qdot);
	double omega3 = Omega3(q,qdot);
	double theta30 = theta3(eta0,q,qdot);
	
	// set up function parameters
	
	keplerEquationParamsType pkeq={ e * c / (c + B_ISO), omega3 * t + theta30};

	//set up function for rootfinding

	keq.function = &keplerEquation;
	keq.params = &pkeq;

	//initialize rootfinder

	s=gsl_root_fsolver_alloc(T);
	
	//set it to bracket root of kepler eqn.  The initial guess should be near eta=Omega3*t + theta30
	
	xlo = omega3 * t + theta30 - M_PI;
	xhi = omega3 * t + theta30 + M_PI;

	gsl_root_fsolver_set(s, &keq, xlo, xhi);

	//iterate till root is solved
	
	do {
		iter++;
		status=gsl_root_fsolver_iterate(s);
		xlo=gsl_root_fsolver_x_lower(s);
		xhi=gsl_root_fsolver_x_upper(s);
		status=gsl_root_test_interval(xlo,xhi,epsabs,epsrel);
	} while (status==GSL_CONTINUE && iter<max_iter);
	
	r = gsl_root_fsolver_root(s);
	
	gsl_root_fsolver_free(s);
	
	return r;

}							
Real GreensFunction3DSym::drawR(Real rnd, Real t) const
{
    // input parameter range checks.
    if ( !(rnd <= 1.0 && rnd >= 0.0 ) )
    {
        throw std::invalid_argument( ( boost::format( "GreensFunction3DSym: rnd <= 1.0 && rnd >= 0.0 : rnd=%.16g" ) % rnd ).str() );
    }

    if ( !(t >= 0.0 ) )
    {
        throw std::invalid_argument( ( boost::format( "GreensFunction3DSym: t >= 0.0 : t=%.16g" ) % t ).str() );
    }


    // t == 0 or D == 0 means no move.
    if( t == 0.0 || getD() == 0.0 )
    {
        return 0.0;
    }

    ip_r_params params = { this, t, rnd };

    gsl_function F = 
        {
            reinterpret_cast<double (*)(double, void*)>( &ip_r_F ),
            &params 
        };

    Real max_r( 4.0 * sqrt( 6.0 * getD() * t ) );

    while( GSL_FN_EVAL( &F, max_r ) < 0.0 )
    {
        max_r *= 10;
    }

    const gsl_root_fsolver_type* solverType( gsl_root_fsolver_brent );
    gsl_root_fsolver* solver( gsl_root_fsolver_alloc( solverType ) );
    gsl_root_fsolver_set( solver, &F, 0.0, max_r );

    const unsigned int maxIter( 100 );

    unsigned int i( 0 );
    while( true )
    {
        gsl_root_fsolver_iterate( solver );
        const Real low( gsl_root_fsolver_x_lower( solver ) );
        const Real high( gsl_root_fsolver_x_upper( solver ) );
        const int status( gsl_root_test_interval( low, high, 1e-15, 
                                                  this->TOLERANCE ) );

        if( status == GSL_CONTINUE )
        {
            if( i >= maxIter )
            {
                gsl_root_fsolver_free( solver );
                throw std::runtime_error("GreensFunction3DSym: drawR: failed to converge");
            }
        }
        else
        {
            break;
        }

        ++i;
    }
  
    //printf("%d\n", i );

    const Real r( gsl_root_fsolver_root( solver ) );
    gsl_root_fsolver_free( solver );
    
    return r;
}
Exemplo n.º 4
0
/**
 * Determine the 95% confidence level upper limit at a particular sky location from the loudest IHS value
 * \param [out] ul        Pointer to an UpperLimit struct
 * \param [in]  params    Pointer to UserInput_t
 * \param [in]  ffdata    Pointer to ffdataStruct
 * \param [in]  ihsmaxima Pointer to an ihsMaximaStruct
 * \param [in]  ihsfar    Pointer to an ihsfarStruct
 * \param [in]  fbinavgs  Pointer to a REAL4Vector of the 2nd FFT background powers
 * \return Status value
 */
INT4 skypoint95UL(UpperLimit *ul, UserInput_t *params, ffdataStruct *ffdata, ihsMaximaStruct *ihsmaxima, ihsfarStruct *ihsfar, REAL4Vector *fbinavgs)
{

    XLAL_CHECK( ul != NULL && params != NULL && ffdata != NULL && ihsmaxima != NULL && ihsfar!= NULL && fbinavgs != NULL, XLAL_EINVAL );

    INT4 ULdetermined = 0;

    INT4 minrows = (INT4)round(2.0*params->dfmin*params->Tsft)+1;

    //Allocate vectors
    XLAL_CHECK( (ul->fsig = XLALCreateREAL8Vector((ihsmaxima->rows-minrows)+1)) != NULL, XLAL_EFUNC );
    XLAL_CHECK( (ul->period = XLALCreateREAL8Vector((ihsmaxima->rows-minrows)+1)) != NULL, XLAL_EFUNC );
    XLAL_CHECK( (ul->moddepth = XLALCreateREAL8Vector((ihsmaxima->rows-minrows)+1)) != NULL, XLAL_EFUNC );
    XLAL_CHECK( (ul->ULval = XLALCreateREAL8Vector((ihsmaxima->rows-minrows)+1)) != NULL, XLAL_EFUNC );
    XLAL_CHECK( (ul->effSNRval = XLALCreateREAL8Vector((ihsmaxima->rows-minrows)+1)) != NULL, XLAL_EFUNC );

    //Initialize solver
    const gsl_root_fsolver_type *T = gsl_root_fsolver_brent;
    gsl_root_fsolver *s = NULL;
    XLAL_CHECK( (s = gsl_root_fsolver_alloc (T)) != NULL, XLAL_EFUNC );
    gsl_function F;
    switch (params->ULsolver) {
    case 1:
        F.function = &gsl_ncx2cdf_withouttinyprob_solver;           //double precision, without the extremely tiny probability part
        break;
    case 2:
        F.function = &gsl_ncx2cdf_float_solver;   //single precision
        break;
    case 3:
        F.function = &gsl_ncx2cdf_solver;         //double precision
        break;
    case 4:
        F.function = &ncx2cdf_float_withouttinyprob_withmatlabchi2cdf_solver;   //single precision, w/ Matlab-based chi2cdf function
        break;
    case 5:
        F.function = &ncx2cdf_withouttinyprob_withmatlabchi2cdf_solver;         //double precision, w/ Matlab-based chi2cdf function
        break;
    default:
        F.function = &gsl_ncx2cdf_float_withouttinyprob_solver;     //single precision, without the extremely tiny probability part
        break;
    }
    struct ncx2cdf_solver_params pars;

    //loop over modulation depths
    for (INT4 ii=minrows; ii<=ihsmaxima->rows; ii++) {
        REAL8 loudestoutlier = 0.0, loudestoutlierminusnoise = 0.0, loudestoutliernoise = 0.0;
        INT4 jjbinofloudestoutlier = 0, locationofloudestoutlier = -1;
        INT4 startpositioninmaximavector = (ii-2)*ffdata->numfbins - ((ii-1)*(ii-1)-(ii-1))/2;
        REAL8 moddepth = 0.5*(ii-1.0)/params->Tsft;                             //"Signal" modulation depth

        //loop over frequency bins
        for (INT4 jj=0; jj<ffdata->numfbins-(ii-1); jj++) {
            INT4 locationinmaximavector = startpositioninmaximavector + jj;      //Current location in IHS maxima vector
            REAL8 noise = ihsfar->expectedIHSVector->data[ihsmaxima->locations->data[locationinmaximavector] - 5];  //Expected noise

            //Sum across multiple frequency bins scaling noise each time with average noise floor
            REAL8 totalnoise = 0.0;
            for (INT4 kk=0; kk<ii; kk++) totalnoise += fbinavgs->data[jj+kk];
            totalnoise = noise*totalnoise;

            REAL8 ihsminusnoise = ihsmaxima->maxima->data[locationinmaximavector] - totalnoise;    //IHS value minus noise

            REAL8 fsig = params->fmin - params->dfmax + (0.5*(ii-1.0) + jj - 6.0)/params->Tsft;        //"Signal" frequency

            if (ihsminusnoise>loudestoutlierminusnoise &&
                    (fsig>=params->ULfmin && fsig<params->ULfmin+params->ULfspan) &&
                    (moddepth>=params->ULminimumDeltaf && moddepth<=params->ULmaximumDeltaf)) {
                loudestoutlier = ihsmaxima->maxima->data[locationinmaximavector];
                loudestoutliernoise = totalnoise;
                loudestoutlierminusnoise = ihsminusnoise;
                locationofloudestoutlier = ihsmaxima->locations->data[locationinmaximavector];
                jjbinofloudestoutlier = jj;
            }
        } /* for jj < ffdata->numfbins-(ii-1) */

        if (locationofloudestoutlier!=-1) {
            //We do a root finding algorithm to find the delta value required so that only 5% of a non-central chi-square
            //distribution lies below the maximum value.
            REAL8 initialguess = ncx2inv_float(0.95, 2.0*loudestoutliernoise, 2.0*loudestoutlierminusnoise);
            XLAL_CHECK( xlalErrno == 0, XLAL_EFUNC );

            REAL8 lo = 0.001*initialguess, hi = 10.0*initialguess;
            pars.val = 2.0*loudestoutlier;
            pars.dof = 2.0*loudestoutliernoise;
            pars.ULpercent = 0.95;
            F.params = &pars;
            XLAL_CHECK( gsl_root_fsolver_set(s, &F, lo, hi) == GSL_SUCCESS, XLAL_EFUNC );

            INT4 status = GSL_CONTINUE;
            INT4 max_iter = 100;
            REAL8 root = 0.0;
            INT4 jj = 0;
            while (status==GSL_CONTINUE && jj<max_iter) {
                jj++;
                status = gsl_root_fsolver_iterate(s);
                XLAL_CHECK( status == GSL_CONTINUE || status == GSL_SUCCESS, XLAL_EFUNC, "gsl_root_fsolver_iterate() failed with code %d\n", status );
                root = gsl_root_fsolver_root(s);
                lo = gsl_root_fsolver_x_lower(s);
                hi = gsl_root_fsolver_x_upper(s);
                status = gsl_root_test_interval(lo, hi, 0.0, 0.001);
                XLAL_CHECK( status == GSL_CONTINUE || status == GSL_SUCCESS, XLAL_EFUNC, "gsl_root_test_interval() failed with code %d\n", status );
            } /* while status==GSL_CONTINUE and jj<max_iter */
            XLAL_CHECK( status == GSL_SUCCESS, XLAL_EFUNC, "Root finding iteration (%d/%d) failed with code %d\n", jj, max_iter, status );

            //Convert the root value to an h0 value
            REAL8 h0 = ihs2h0(root, params);

            //Store values in the upper limit struct
            ul->fsig->data[ii-minrows] = params->fmin - params->dfmax + (0.5*(ii-1.0) + jjbinofloudestoutlier - 6.0)/params->Tsft;
            ul->period->data[ii-minrows] = params->Tobs/locationofloudestoutlier;
            ul->moddepth->data[ii-minrows] = 0.5*(ii-1.0)/params->Tsft;
            ul->ULval->data[ii-minrows] = h0;
            ul->effSNRval->data[ii-minrows] = unitGaussianSNR(root, pars.dof);
            ULdetermined++;
        } // if locationofloudestoutlier != -1
    } // for ii=minrows --> maximum rows

    //Signal an error if we didn't find something above the noise level
    XLAL_CHECK( ULdetermined != 0, XLAL_EFUNC, "Failed to reach a louder outlier minus noise greater than 0\n" );

    gsl_root_fsolver_free(s);

    return XLAL_SUCCESS;

}
Exemplo n.º 5
0
int calcola_equilibrio(double cost_Hamaker, double cost_Debye_quadra, double* risultato) {
	// da esempio: http://www.gnu.org/software/gsl/manual/html_node/Root-Finding-Examples.html
	int status;
    int iter = 0, max_iter = 100;
	const gsl_root_fsolver_type *T;
	gsl_root_fsolver *s;
	double r = 0;
	double x_lo = 1E-10, x_hi = 8E-9;
	gsl_function F;
	params parametri = {cost_Hamaker, cost_Debye_quadra, in.sigma, in.D_me, in.teta};
	
	F.function = &funzione_equilibrio;
	F.params = &parametri;
	
	//T = gsl_root_fsolver_brent;
	T = gsl_root_fsolver_bisection;
	s = gsl_root_fsolver_alloc (T);
	gsl_root_fsolver_set (s, &F, x_lo, x_hi);
	
	#ifdef DEBUG_ROOT
	printf ("using %s method\n", 
		   gsl_root_fsolver_name (s));
	
	printf ("%5s [%12s, %12s] %12s %12s\n",
		   "iter", "lower", "upper", "root", "err(est)");
	#endif
	
	do
	 {
	   iter++;
	   status = gsl_root_fsolver_iterate (s);
	   r = gsl_root_fsolver_root (s);
	   x_lo = gsl_root_fsolver_x_lower (s);
	   x_hi = gsl_root_fsolver_x_upper (s);
	   status = gsl_root_test_interval (x_lo, x_hi,
										0, 0.001);
										
		// controllo su dimensione minima
		if(x_hi < in.D_min){
			*risultato = in.D_min;
			#ifdef DEBUG_ROOT
			printf ("limite superiore= %e < %e=minimo consentito\n",x_hi,in.D_min);
			#endif
			gsl_root_fsolver_free (s);
			return GSL_SUCCESS;
		}
		
		#ifdef DEBUG_ROOT
		if (status == GSL_SUCCESS)
		 printf ("Converged:\n");

		
		printf ("%5d [%e, %e] %e %e\n",
                   iter, x_lo, x_hi,
                   r, 
                   x_hi - x_lo);
		#endif
	 }
	while (status == GSL_CONTINUE && iter < max_iter);
	
	gsl_root_fsolver_free (s);
	
	*risultato = r;
	return status;
}
Exemplo n.º 6
0
Real GreensFunction3D::drawTheta(Real rnd, Real r, Real t) const
{
    // input parameter range checks.
    if ( !(rnd <= 1.0 && rnd >= 0.0 ) )
    {
        throw std::invalid_argument( ( boost::format( "rnd <= 1.0 && rnd >= 0.0 : rnd=%.16g" ) % rnd ).str() );
    }

    if ( !(r >= 0.0 ) )
    {
        throw std::invalid_argument( ( boost::format( "r >= 0.0 : r=%.16g" ) % r ).str() );
    }

    if ( !(r0 >= 0.0 ) )
    {
        throw std::invalid_argument( ( boost::format( "r0 >= 0.0 : r0=%.16g" ) % r0 ).str() );
    }

    if ( !(t >= 0.0 ) )
    {
        throw std::invalid_argument( ( boost::format( "t >= 0.0 : t=%.16g" ) % t ).str() );
    }


    // t == 0 means no move.
    if( t == 0.0 )
    {
        return 0.0;
    }

    const Real ip_theta_pi( ip_theta( M_PI, r, t ) );

    ip_theta_params params = { this, r, t, rnd * ip_theta_pi };

    gsl_function F = 
        {
            reinterpret_cast<double (*)(double, void*)>( &ip_theta_F ),
            &params 
        };

    const gsl_root_fsolver_type* solverType( gsl_root_fsolver_brent );
    gsl_root_fsolver* solver( gsl_root_fsolver_alloc( solverType ) );
    gsl_root_fsolver_set( solver, &F, 0.0,
                          M_PI + std::numeric_limits<Real>::epsilon() );

    const unsigned int maxIter( 100 );

    unsigned int i( 0 );
    while( true )
    {
        gsl_root_fsolver_iterate( solver );
        const Real low( gsl_root_fsolver_x_lower( solver ) );
        const Real high( gsl_root_fsolver_x_upper( solver ) );
        const int status( gsl_root_test_interval( low, high, 1e-15, 
                                                  this->TOLERANCE ) );

        if( status == GSL_CONTINUE )
        {
            if( i >= maxIter )
            {
                gsl_root_fsolver_free( solver );
                throw std::runtime_error("drawTheta: failed to converge");
            }
        }
        else
        {
            break;
        }

        ++i;
    }
  
    const Real theta( gsl_root_fsolver_root( solver ) );
    gsl_root_fsolver_free( solver );
    

    return theta;
}
Exemplo n.º 7
0
bool three_body_system::find_characteristic_root(double &tar, vector<double> &new_orthonormal, vector<double> &h, int N){
  // find the Nth root of the characteristic function specified by new_orthonormal and h

  gsl_set_error_handler_off();

  int status;
  int iter = 0, max_iter = 100;
  const gsl_root_fsolver_type *T;
  gsl_root_fsolver *s;
  double r = 0;
  gsl_function F;

  F.function = &helper_function;
  characteristic_root_helper * params = new characteristic_root_helper(this, &new_orthonormal, &h);
  F.params = params;

  T = gsl_root_fsolver_brent;
  s = gsl_root_fsolver_alloc (T);

  double x_lo = 0.0;
  double x_hi = evals[N] - 0.00001;
  if (N > 0) x_lo = evals[N-1]*1.000000001;
  else{
    x_lo = x_hi - 1.0;
    while (true){
      double temp = GSL_FN_EVAL(&F, x_lo);
      //cout << x_lo << " " << temp << endl;
      if (temp < 0.0) break;
      x_lo -= 1.0;
      if (x_lo < x_hi - 10.0) break;

    }
    //if (evals[0] < 0.0) x_lo = evals[0] * 1.5;
    //else x_lo = min(0.0, evals[0] - 250.0);
  }


  status = gsl_root_fsolver_set (s, &F, x_lo, x_hi);
    //cout << x_lo << " " << x_hi << endl;
    //cout << GSL_FN_EVAL(&F, x_lo) << " " << GSL_FN_EVAL(&F, x_hi) << endl;
  if (status != GSL_SUCCESS){
    //cout << "Root solver failure?" << endl;
    return false;
  }

  do
    {
      iter++;
      status = gsl_root_fsolver_iterate (s);
      r = gsl_root_fsolver_root (s);
      x_lo = gsl_root_fsolver_x_lower (s);
      x_hi = gsl_root_fsolver_x_upper (s);
      status = gsl_root_test_interval (x_lo, x_hi,
                                       0, 1E-8);

    }
  while (status == GSL_CONTINUE && iter < max_iter);

  gsl_root_fsolver_free (s);

  tar = r;

  return true;
}
Exemplo n.º 8
0
    // maximize each model parameter given the hidden data estimated from
    // last iteration
    void MStep()
    {
        // optimization config
        double step_size = .01;
        double tol = .1;
        const double epsabs = .01;
        gsl_vector *init_x = gsl_vector_alloc(2);
        int status;
	double low = .0001, high = gene_nu * 20;
	int iter = 0;
	const int maxiter = 10;
        
        // optimize gene rate parameters
	gsl_root_fsolver_set(sol_gene_rate, &opt_gene_rate, low, high);

	status = GSL_CONTINUE;
	for (iter=0; iter<maxiter && status==GSL_CONTINUE; iter++) {
            // do one iteration
	    status = gsl_root_fsolver_iterate(sol_gene_rate);
            if (status)
                break;

            // check convergence
	    low = gsl_root_fsolver_x_lower(sol_gene_rate);
	    high = gsl_root_fsolver_x_upper(sol_gene_rate);
	    status = gsl_root_test_interval(low, high, 0, 0.01);
	}
	gene_nu = gsl_root_fsolver_root(sol_gene_rate);
	gene_alpha = gene_nu + 1;
        gene_beta = gene_nu;
        fprintf(stderr, "nu = %f (iter=%d)\n", gene_nu, iter);
        
        
        // optimize each species rate parmater set
        for (int i=0; i<nspecies; i++) {
            cur_species = i;
            gsl_vector_set(init_x, 0, sp_alpha[i]);
            gsl_vector_set(init_x, 1, sp_beta[i]);
            gsl_multimin_fdfminimizer_set(sol_sp_rate, &opt_sp_rate, init_x, 
                                          step_size, tol);            
            status = GSL_CONTINUE;

            
	    for (iter=0; iter<maxiter && status==GSL_CONTINUE; iter++) {
                // do one iteration
                status = gsl_multimin_fdfminimizer_iterate(sol_sp_rate);
                if (status)
                    break;        
                // get gradient
                status = gsl_multimin_test_gradient(sol_sp_rate->gradient, epsabs);
            }

            double lk = likelihood();
            fprintf(stderr, "species %d %d %f\n", i, iter, lk);

            sp_alpha[i] = gsl_vector_get(sol_sp_rate->x, 0);
            sp_beta[i] = gsl_vector_get(sol_sp_rate->x, 1);

            //printf("sp[%d] = (%f, %f)\n", i, sp_alpha[i], sp_beta[i]);
        }

        gsl_vector_free(init_x);
    }
Exemplo n.º 9
0
int main( int argc, char *argv[] )
{
  LALStatus             status = blank_status;
  const INT4            S2StartTime = 729273613; /* Feb 14 2003 16:00:00 UTC */
  const INT4            S2StopTime  = 734367613; /* Apr 14 2003 15:00:00 UTC */

  /* command line options */
  LIGOTimeGPS   gpsStartTime = {S2StartTime, 0};
  LIGOTimeGPS   gpsEndTime   = {S2StopTime, 0};
  REAL8         meanTimeStep = 2630 / LAL_PI;
  REAL8         timeInterval = 0;
  UINT4         randSeed = 1;
  CHAR         *userTag = NULL;
  REAL4         minMass = 0.1;
  REAL4         maxMass = 1.0;
  REAL4         r_core = 5.0;     /* kpc core radius */
  REAL4         r_max = 50.0;     /* kpc halo radius */
  REAL4         q = 1.0;          /* flatten halo */

  /* program variables */
  RandomParams *randParams = NULL;
  REAL4  u;
  REAL4  deltaM;

  int i, stat;
  const int maxIter = 1000;
  const double tol = 1e-6;
  const gsl_root_fsolver_type *solver_type;
  gsl_root_fsolver *solver;
  gsl_function pdf;

  struct halo_pdf_params pdf_params;

  double r_lo, r_hi, r;
  double cosphi, sinphi;
  double pdf_norm;

  GalacticInspiralParamStruc galacticPar;

  /* xml output data */
  CHAR                  fname[256];
  MetadataTable         proctable;
  MetadataTable         procparams;
  MetadataTable         injections;
  ProcessParamsTable   *this_proc_param;
  SimInspiralTable     *this_inj = NULL;
  LIGOLwXMLStream       xmlfp;
  UINT4                 outCompress = 0;

  /* LALgetopt arguments */
  struct LALoption long_options[] =
  {
    {"help",                    no_argument,       0,                'h'},
    {"verbose",                 no_argument,       &vrbflg,           1 },
    {"write-compress",          no_argument,       &outCompress,      1 },
    {"gps-start-time",          required_argument, 0,                'a'},
    {"gps-end-time",            required_argument, 0,                'b'},
    {"time-step",               required_argument, 0,                't'},
    {"time-interval",                required_argument, 0,                     'i'},
    {"seed",                    required_argument, 0,                's'},
    {"minimum-mass",            required_argument, 0,                'A'},
    {"maximum-mass",            required_argument, 0,                'B'},
    {"core-radius",             required_argument, 0,                'p'},
    {"flatten-halo",            required_argument, 0,                'q'},
    {"halo-radius",             required_argument, 0,                'r'},
    {"user-tag",                required_argument, 0,                'Z'},
    {"userTag",                 required_argument, 0,                'Z'},
    {0, 0, 0, 0}
  };
  int c;

  /* set up inital debugging values */
  lal_errhandler = LAL_ERR_EXIT;

  /* create the process and process params tables */
  proctable.processTable = (ProcessTable *) 
    calloc( 1, sizeof(ProcessTable) );
  XLALGPSTimeNow(&(proctable.processTable->start_time));
  if (strcmp(CVS_REVISION,"$Revi" "sion$"))
    {
      XLALPopulateProcessTable(proctable.processTable, PROGRAM_NAME,
          CVS_REVISION, CVS_SOURCE, CVS_DATE, 0);
    }
  else
    {
      XLALPopulateProcessTable(proctable.processTable, PROGRAM_NAME,
          lalappsGitCommitID, lalappsGitGitStatus, lalappsGitCommitDate, 0);
    }
  snprintf( proctable.processTable->comment, LIGOMETA_COMMENT_MAX, " " );
  this_proc_param = procparams.processParamsTable = (ProcessParamsTable *) 
    calloc( 1, sizeof(ProcessParamsTable) );
  

  /*
   *
   * parse command line arguments
   *
   */

     
  while ( 1 )
  {
    /* LALgetopt_long stores long option here */
    int option_index = 0;
    long int gpsinput;
    size_t LALoptarg_len;

    c = LALgetopt_long_only( argc, argv,
        "a:A:b:B:hi:p:q:r:s:t:vZ:", long_options, &option_index );

    /* detect the end of the options */
    if ( c == - 1 )
    {
      break;
    }

    switch ( c )
    {
      case 0:
        /* if this option set a flag, do nothing else now */
        if ( long_options[option_index].flag != 0 )
        {
          break;
        }
        else
        {
          fprintf( stderr, "error parsing option %s with argument %s\n",
              long_options[option_index].name, LALoptarg );
          exit( 1 );
        }
        break;

      case 'a':
        gpsinput = atol( LALoptarg );
        if ( gpsinput < 441417609 )
        {
          fprintf( stderr, "invalid argument to --%s:\n"
              "GPS start time is prior to " 
              "Jan 01, 1994  00:00:00 UTC:\n"
              "(%ld specified)\n",
              long_options[option_index].name, gpsinput );
          exit( 1 );
        }
        gpsStartTime.gpsSeconds = gpsinput;

        this_proc_param = this_proc_param->next = 
          next_process_param( long_options[option_index].name, "int", 
              "%ld", gpsinput );
        break;

      case 'b':
        gpsinput = atol( LALoptarg );
        if ( gpsinput < 441417609 )
        {
          fprintf( stderr, "invalid argument to --%s:\n"
              "GPS start time is prior to " 
              "Jan 01, 1994  00:00:00 UTC:\n"
              "(%ld specified)\n",
              long_options[option_index].name, gpsinput );
          exit( 1 );
        }
        gpsEndTime.gpsSeconds = gpsinput;
        this_proc_param = this_proc_param->next = 
          next_process_param( long_options[option_index].name, "int", 
              "%ld", gpsinput );
        break;

      case 's':
        randSeed = atoi( LALoptarg );
        this_proc_param = this_proc_param->next = 
          next_process_param( long_options[option_index].name, "int", 
              "%d", randSeed );
        break;

      case 't':
        meanTimeStep = (REAL8) atof( LALoptarg );
        if ( meanTimeStep <= 0 )
        {
          fprintf( stderr, "invalid argument to --%s:\n"
              "time step must be > 0: (%le seconds specified)\n",
              long_options[option_index].name, meanTimeStep );
          exit( 1 );
        }
        this_proc_param = this_proc_param->next = 
          next_process_param( long_options[option_index].name, "float", 
              "%le", meanTimeStep );
        break;

      case 'i':
        timeInterval = atof( LALoptarg );
        if ( timeInterval < 0 )
        {
          fprintf( stderr, "invalid argument to --%s:\n"
              "time interval must be >= 0: (%le seconds specified)\n",
              long_options[option_index].name, meanTimeStep );
          exit( 1 );
        }
        this_proc_param = this_proc_param->next = 
          next_process_param( long_options[option_index].name, 
              "float", "%le", timeInterval );
        break;

      case 'A':
        minMass = (REAL4) atof( LALoptarg );
        if ( minMass <= 0 )
        {
          fprintf( stderr, "invalid argument to --%s:\n"
              "miniumum component mass must be > 0: "
              "(%f solar masses specified)\n",
              long_options[option_index].name, minMass );
          exit( 1 );
        }
        this_proc_param = this_proc_param->next = 
          next_process_param( long_options[option_index].name, 
              "float", "%e", minMass );
        break;

      case 'B':
        maxMass = (REAL4) atof( LALoptarg );
        if ( maxMass <= 0 )
        {
          fprintf( stderr, "invalid argument to --%s:\n"
              "maxiumum component mass must be > 0: "
              "(%f solar masses specified)\n",
              long_options[option_index].name, maxMass );
          exit( 1 );
        }
        this_proc_param = this_proc_param->next = 
          next_process_param( long_options[option_index].name, 
              "float", "%e", maxMass );
        break;

      case 'p':
        /* core-radius */
        r_core = (REAL4) atof( LALoptarg );
        if ( r_core <= 0 )
        {
          fprintf( stderr, "invalid argument to --%s:\n"
              "galactic core radius must be > 0: "
              "(%f kpc specified)\n",
              long_options[option_index].name, r_core );
          exit( 1 );
        }
        this_proc_param = this_proc_param->next = 
          next_process_param( long_options[option_index].name, 
              "float", "%e", r_core );
        break;

      case 'q':
        /* flatten-halo */
        q = (REAL4) atof( LALoptarg );
        if ( q <= 0 || q > 1 )
        {
          fprintf( stderr, "invalid argument to --%s:\n"
              "halo flattening parameter must be in range (0,1]: "
              "(%f specified)\n",
              long_options[option_index].name, q );
          exit( 1 );
        }
        this_proc_param = this_proc_param->next = 
          next_process_param( long_options[option_index].name, 
              "float", "%e", q );
        break;

      case 'r':
        /* max halo radius */
        r_max = (REAL4) atof( LALoptarg );
        if ( r_max <= 0 )
        {
          fprintf( stderr, "invalid argument to --%s:\n"
              "halo radius must be greater than 0: "
              "(%f kpc specified)\n",
              long_options[option_index].name, r_max );
          exit( 1 );
        }
        this_proc_param = this_proc_param->next = 
          next_process_param( long_options[option_index].name, 
              "float", "%e", r_max );
        break;

      case 'Z':
        /* create storage for the usertag */
        LALoptarg_len = strlen( LALoptarg ) + 1;
        userTag = (CHAR *) calloc( LALoptarg_len, sizeof(CHAR) );
        memcpy( userTag, LALoptarg, LALoptarg_len );
        this_proc_param = this_proc_param->next = 
          next_process_param( long_options[option_index].name, 
              "string", "%s", LALoptarg );
        break;

      case 'v':
        vrbflg = 1;
        break;

      case 'h':
        fprintf( stderr, USAGE );
        exit( 0 );
        break;

      case '?':
        fprintf( stderr, USAGE );
        exit( 1 );
        break;

      default:
        fprintf( stderr, "unknown error while parsing options\n" );
        fprintf( stderr, USAGE );
        exit( 1 );
    }
  }

  if ( LALoptind < argc )
  {
    fprintf( stderr, "extraneous command line arguments:\n" );
    while ( LALoptind < argc )
    {
      fprintf ( stderr, "%s\n", argv[LALoptind++] );
    }
    exit( 1 );
  }


  /*
   *
   * initialization
   *
   */


  /* initialize the random number generator */
  LAL_CALL( LALCreateRandomParams( &status, &randParams, randSeed ), &status );

  /* initialize the gsl solver with the spatial pdf */
  pdf.function = &halo_pdf;
  pdf.params   = &pdf_params;
  solver_type = gsl_root_fsolver_bisection;
  solver = gsl_root_fsolver_alloc( solver_type );
  pdf_params.a = r_core;

  /* normalization for the spatial pdf */
  pdf_norm = r_max - r_core * atan2( r_max, r_core );

  /* mass range */
  deltaM = maxMass - minMass;

  /* null out the head of the linked list */
  injections.simInspiralTable = NULL;

  /* create the output file name */
  if ( userTag && outCompress )
  {
    snprintf( fname, sizeof(fname), "HL-INJECTIONS_%d_%s-%d-%d.xml.gz",
        randSeed, userTag, gpsStartTime.gpsSeconds,
        gpsEndTime.gpsSeconds - gpsStartTime.gpsSeconds );
  }
  else if ( userTag && !outCompress )
  {
    snprintf( fname, sizeof(fname), "HL-INJECTIONS_%d_%s-%d-%d.xml", 
        randSeed, userTag, gpsStartTime.gpsSeconds, 
        gpsEndTime.gpsSeconds - gpsStartTime.gpsSeconds );
  }
  else if ( !userTag && outCompress )
  {
    snprintf( fname, sizeof(fname), "HL-INJECTIONS_%d-%d-%d.xml.gz",
        randSeed, gpsStartTime.gpsSeconds,
        gpsEndTime.gpsSeconds - gpsStartTime.gpsSeconds );
  }
  else
  {
    snprintf( fname, sizeof(fname), "HL-INJECTIONS_%d-%d-%d.xml", 
        randSeed, gpsStartTime.gpsSeconds, 
        gpsEndTime.gpsSeconds - gpsStartTime.gpsSeconds );
  }


  /*
   *
   * loop over duration of desired output times
   *
   */


  while ( XLALGPSCmp( &gpsStartTime, &gpsEndTime ) < 0 )
  {
    /* uniformly distributed masses */
    LAL_CALL( LALUniformDeviate( &status, &u, randParams ), &status );
    galacticPar.m1 = minMass + u * deltaM;
    LAL_CALL( LALUniformDeviate( &status, &u, randParams ), &status );
    galacticPar.m2 = minMass + u * deltaM;
    
    /* spatial distribution */
    LAL_CALL( LALUniformDeviate( &status, &u, randParams ), &status );
    pdf_params.u = u * pdf_norm;

    r_lo = 0.0;
    r_hi = r_max;
    gsl_root_fsolver_set( solver, &pdf, r_lo, r_hi );

    for ( i = 0; i < maxIter; ++i )
    {
      gsl_root_fsolver_iterate( solver );
      r = gsl_root_fsolver_root( solver );
      r_lo = gsl_root_fsolver_x_lower( solver );
      r_hi = gsl_root_fsolver_x_upper( solver );
      stat = gsl_root_test_interval( r_lo, r_hi, 0, tol );
      if ( stat == GSL_SUCCESS )
        break;
    }

    if ( stat != GSL_SUCCESS )
    {
      fprintf( stderr, "could not find root after %d iterations\n", maxIter );
      exit( 1 );
    }

    LAL_CALL( LALUniformDeviate( &status, &u, randParams ), &status );
    sinphi = 2.0 * u - 1.0;
    cosphi = sqrt( 1.0 - sinphi*sinphi );
    
    galacticPar.rho = r * cosphi;
    galacticPar.z   = q * r * sinphi;

    LAL_CALL( LALUniformDeviate( &status, &u, randParams ), &status );
    galacticPar.lGal = LAL_TWOPI * u;    

    if ( vrbflg ) fprintf( stdout, "%e %e %e %e %e\n", 
        galacticPar.m1, galacticPar.m2,
        galacticPar.rho * cos( galacticPar.lGal ),
        galacticPar.rho * sin( galacticPar.lGal ),
        galacticPar.z );

    /* create the sim_inspiral table */
    if ( injections.simInspiralTable )
    {
      this_inj = this_inj->next = (SimInspiralTable *)
        LALCalloc( 1, sizeof(SimInspiralTable) );
    }
    else
    {
      injections.simInspiralTable = this_inj = (SimInspiralTable *)
        LALCalloc( 1, sizeof(SimInspiralTable) );
    }

    /* set the geocentric end time of the injection */
    galacticPar.geocentEndTime = gpsStartTime;
    if ( timeInterval )
    {
      LAL_CALL( LALUniformDeviate( &status, &u, randParams ), &status );
      XLALGPSAdd( &(galacticPar.geocentEndTime), u * timeInterval );
    }

    /* populate the sim_inspiral table */
    LAL_CALL( LALGalacticInspiralParamsToSimInspiralTable( &status,
          this_inj, &galacticPar, randParams ), &status );

    /* set the source and waveform fields */
    snprintf( this_inj->source, LIGOMETA_SOURCE_MAX, "MW" );
    snprintf( this_inj->waveform, LIGOMETA_WAVEFORM_MAX, 
        "GeneratePPNtwoPN" );

    /* increment the injection time */
    XLALGPSAdd( &gpsStartTime, meanTimeStep );
  } /* end loop over injection times */

  /* destroy random parameters */
  LAL_CALL( LALDestroyRandomParams( &status, &randParams ), &status );


  /*
   *
   * write output to LIGO_LW XML file
   *
   */


  /* open the xml file */
  memset( &xmlfp, 0, sizeof(LIGOLwXMLStream) );
  LAL_CALL( LALOpenLIGOLwXMLFile( &status, &xmlfp, fname ), &status );

  /* write the process table */
  snprintf( proctable.processTable->ifos, LIGOMETA_IFOS_MAX, "H1H2L1" );
  XLALGPSTimeNow(&(proctable.processTable->end_time));
  LAL_CALL( LALBeginLIGOLwXMLTable( &status, &xmlfp, process_table ), 
      &status );
  LAL_CALL( LALWriteLIGOLwXMLTable( &status, &xmlfp, proctable, 
        process_table ), &status );
  LAL_CALL( LALEndLIGOLwXMLTable ( &status, &xmlfp ), &status );
  free( proctable.processTable );

  /* free the unused process param entry */
  this_proc_param = procparams.processParamsTable;
  procparams.processParamsTable = procparams.processParamsTable->next;
  free( this_proc_param );

  /* write the process params table */
  if ( procparams.processParamsTable )
  {
    LAL_CALL( LALBeginLIGOLwXMLTable( &status, &xmlfp, process_params_table ), 
        &status );
    LAL_CALL( LALWriteLIGOLwXMLTable( &status, &xmlfp, procparams, 
          process_params_table ), &status );
    LAL_CALL( LALEndLIGOLwXMLTable ( &status, &xmlfp ), &status );
    while( procparams.processParamsTable )
    {
      this_proc_param = procparams.processParamsTable;
      procparams.processParamsTable = this_proc_param->next;
      free( this_proc_param );
    }
  }

  /* write the sim_inspiral table */
  if ( injections.simInspiralTable )
  {
    LAL_CALL( LALBeginLIGOLwXMLTable( &status, &xmlfp, sim_inspiral_table ), 
        &status );
    LAL_CALL( LALWriteLIGOLwXMLTable( &status, &xmlfp, injections, 
          sim_inspiral_table ), &status );
    LAL_CALL( LALEndLIGOLwXMLTable ( &status, &xmlfp ), &status );
  }
  while ( injections.simInspiralTable )
  {
    this_inj = injections.simInspiralTable;
    injections.simInspiralTable = injections.simInspiralTable->next;
    LALFree( this_inj );
  }

  /* close the injection file */
  LAL_CALL( LALCloseLIGOLwXMLFile ( &status, &xmlfp ), &status );

  /* check for memory leaks and exit */
  LALCheckMemoryLeaks();
  return 0;
}
Exemplo n.º 10
0
Real GreensFunction3DRadInf::drawTheta(Real rnd, Real r, Real t) const
{
    Real theta;

    const Real sigma(this->getSigma());

    // input parameter range checks.
    if (!(rnd < 1.0 && rnd >= 0.0))
    {
        throw std::invalid_argument((boost::format("rnd < 1.0 && rnd >= 0.0 : rnd=%.16g") % rnd).str());
    }

    if (!(r >= sigma))
    {
        throw std::invalid_argument((boost::format("r >= sigma : r=%.16g, sigma=%.16g") % r % sigma).str());
    }

    if (!(r0 >= sigma))
    {
        throw std::invalid_argument((boost::format("r0 >= sigma : r0=%.16g, sigma=%.16g") % r0 % sigma).str());
    }

    if (!(t >= 0.0))
    {
        throw std::invalid_argument((boost::format("t >= 0.0 : t=%.16g") % t).str());
    }


    // t == 0 means no move.
    if(t == 0.0)
    {
        return 0.0;
    }

    RealVector RnTable;
    makeRnTable(RnTable, r, t);


    // root finding with the integrand form.

    const Real ip_theta_pi(ip_theta_table(M_PI, r, t, RnTable));

    p_theta_params params = { this, r, t, RnTable, rnd * ip_theta_pi };

    gsl_function F = 
        {
            reinterpret_cast<double (*)(double, void*)>(&ip_theta_F),
            &params 
        };

    const gsl_root_fsolver_type* solverType(gsl_root_fsolver_brent);
    gsl_root_fsolver* solver(gsl_root_fsolver_alloc(solverType));
    gsl_root_fsolver_set(solver, &F, 0.0, M_PI);

    const unsigned int maxIter(100);

    unsigned int i(0);
    for (;;)
    {
        gsl_root_fsolver_iterate(solver);
        const Real low(gsl_root_fsolver_x_lower(solver));
        const Real high(gsl_root_fsolver_x_upper(solver));
        const int status(gsl_root_test_interval(low, high, 1e-15,
                                                  THETA_TOLERANCE));

        if(status == GSL_CONTINUE)
        {
            if(i >= maxIter)
            {
                gsl_root_fsolver_free(solver);
                throw std::runtime_error("drawTheta: failed to converge");
            }
        }
        else
        {
            break;
        }

        ++i;
    }
  
    theta = gsl_root_fsolver_root(solver);
    gsl_root_fsolver_free(solver);

    return theta;
}
Exemplo n.º 11
0
Real GreensFunction3DRadInf::drawR(Real rnd, Real t) const
{
    const Real sigma(this->getSigma());
    const Real D(this->getD());

    if (!(rnd < 1.0 && rnd >= 0.0))
    {
        throw std::invalid_argument((boost::format("rnd < 1.0 && rnd >= 0.0 : rnd=%.16g") % rnd).str());
    }

    if (!(r0 >= sigma))
    {
        throw std::invalid_argument((boost::format("r0 >= sigma : r0=%.16g, sigma=%.16g") % r0 % sigma).str());
    }

    if (!(t >= 0.0))
    {
        throw std::invalid_argument((boost::format("t >= 0.0 : t=%.16g") % t).str());
    }


    if(t == 0.0)
    {
        return r0;
    }

    const Real psurv(p_survival(t));

    p_int_r_params params = { this, t, rnd * psurv };

    gsl_function F = 
        {
            reinterpret_cast<double (*)(double, void*)>(&p_int_r_F),
            &params 
        };


    // adjust low and high starting from r0.
    // this is necessary to avoid root finding in the long tails where
    // numerics can be unstable.

    Real low(r0);
    Real high(r0);

    const Real sqrt6Dt(sqrt(6.0 * D * t));
    if(GSL_FN_EVAL(&F, r0) < 0.0)
    {
        // low = r0
        unsigned int H(3);

        for (;;)
        {
            high = r0 + H * sqrt6Dt;

            const Real value(GSL_FN_EVAL(&F, high));
            if(value > 0.0)
            {
                break;
            }

            ++H;

            if(H > 20)
            {
                throw std::runtime_error("drawR: H > 20 while adjusting upper bound of r");
            }
        }

    }
    else
    {
        // high = r0
        unsigned int H(3);

        for (;;)
        {
            low = r0 - H * sqrt6Dt;
            if(low < sigma)
            {
                if(GSL_FN_EVAL(&F, sigma) > 0.0)
                {
                    log_.info("drawR: p_int_r(sigma) > 0.0. "
                              "returning sigma.");
                    return sigma;
                }

                low = sigma;
                break;
            }

            const Real value(GSL_FN_EVAL(&F, low));
            if(value < 0.0)
            {
                break;
            }

            ++H;
        }
    }


    // root finding by iteration.

    const gsl_root_fsolver_type* solverType(gsl_root_fsolver_brent);
    gsl_root_fsolver* solver(gsl_root_fsolver_alloc(solverType));
    gsl_root_fsolver_set(solver, &F, low, high);

    const unsigned int maxIter(100);

    unsigned int i(0);
    for (;;)
    {
        gsl_root_fsolver_iterate(solver);
        low = gsl_root_fsolver_x_lower(solver);
        high = gsl_root_fsolver_x_upper(solver);
        const int status(gsl_root_test_interval(low, high, 1e-15,
                                                  this->TOLERANCE));

        if(status == GSL_CONTINUE)
        {
            if(i >= maxIter)
            {
                gsl_root_fsolver_free(solver);
                throw std::runtime_error("drawR: failed to converge");
            }
        }
        else
        {
            break;
        }

        ++i;
    }
  
    const Real r(gsl_root_fsolver_root(solver));
    gsl_root_fsolver_free(solver);

    return r;
}
Exemplo n.º 12
0
Real GreensFunction3DRadInf::drawTime(Real rnd) const
{
    const Real sigma(this->getSigma());

    if (!(rnd < 1.0 && rnd >= 0.0))
    {
        throw std::invalid_argument((boost::format("rnd < 1.0 && rnd >= 0.0 : rnd=%.16g") % rnd).str());
    }

    if (!(r0 >= sigma))
    {
        throw std::invalid_argument((boost::format("r0 >= sigma : r0=%.16g, sigma=%.16g") % r0 % sigma).str());
    }


    Real low(1e-100);
    Real high(100);

    {
        const Real maxp(p_reaction(INFINITY));

        if(rnd >= maxp)
        {
            return INFINITY;
        }
    }

    p_reaction_params params = { this, rnd };

    gsl_function F = 
        {
            reinterpret_cast<double (*)(double, void*)>(&p_reaction_F),
            &params 
        };

    const gsl_root_fsolver_type* solverType(gsl_root_fsolver_brent);
    gsl_root_fsolver* solver(gsl_root_fsolver_alloc(solverType));
    gsl_root_fsolver_set(solver, &F, low, high);

    const unsigned int maxIter(100);

    unsigned int i(0);
    for (;;)
    {
        gsl_root_fsolver_iterate(solver);

        low = gsl_root_fsolver_x_lower(solver);
        high = gsl_root_fsolver_x_upper(solver);
        int status(gsl_root_test_interval(low, high, 1e-18, 1e-12));

        if(status == GSL_CONTINUE)
        {
            if(i >= maxIter)
            {
                gsl_root_fsolver_free(solver);
                throw std::runtime_error("drawTime: failed to converge");
            }
        }
        else
        {
            break;
        }

        ++i;
    }
  
    const Real r(gsl_root_fsolver_root(solver));
    gsl_root_fsolver_free(solver);

    return r;
} 
Exemplo n.º 13
0
void cosmology::pevolve_fixed(double cdel, int opt, double z, double zstart, double
&cdelz, double &fdelz){

    double fac;
    if(opt==1){
        fac=pow(cdel*(1.+zstart)/(1.+z),3)/munfw(cdel);
    }else if(opt==2){
        fac=pow(cdel,3)/munfw(cdel);
        fac=fac*pow(Eofz(zstart)/Eofz(z),2);
        fac=fac*Delta_crit(zstart)/Delta_crit(z);
    }else if(opt==3){
        fac=pow(cdel,3)/munfw(cdel);
        fac=fac*pow(Eofz(zstart)/Eofz(z),2);
    }else{
        fprintf(stderr,"Option %d not supported yet, bailing out...",opt);
        exit(100);
    }

    int status;
    int iter = 0, max_iter = 100;
    double res;

    const gsl_root_fsolver_type *T;
    gsl_root_fsolver *s;

    double c_lo = 0.01*cdel, c_hi = 100000.0*cdel;

    gsl_function F;
    march_params p;
    p.fac = fac;

    F.function = &findcmarch;
    F.params = &p;
   
    T = gsl_root_fsolver_brent;
    s = gsl_root_fsolver_alloc (T);
    gsl_root_fsolver_set (s, &F, c_lo, c_hi);

    do
    {
        iter++;
        status = gsl_root_fsolver_iterate (s);
        res = gsl_root_fsolver_root (s);
        c_lo = gsl_root_fsolver_x_lower (s);
        c_hi = gsl_root_fsolver_x_upper (s);
        status = gsl_root_test_interval (c_lo, c_hi,0, 1e-6);

        if (status == GSL_SUCCESS)
        {
            //std::cout<<"# "<<"zcollapse:Brent converged after "<< iter<<" iterations"<<std::endl;
        }


    }while (status == GSL_CONTINUE && iter < max_iter);

    gsl_root_fsolver_free (s);

    cdelz=res;
    fdelz=munfw(cdelz)/munfw(cdel);

}