Beispiel #1
0
void
nest::ht_neuron::init_buffers_()
{
  // Reset spike buffers.
  for ( std::vector< RingBuffer >::iterator it = B_.spike_inputs_.begin();
        it != B_.spike_inputs_.end();
        ++it )
  {
    it->clear(); // include resize
  }

  B_.currents_.clear(); // include resize

  B_.logger_.reset();

  Archiving_Node::clear_history();

  B_.step_ = Time::get_resolution().get_ms();
  B_.IntegrationStep_ = B_.step_;

  if ( B_.s_ == 0 )
    B_.s_ = gsl_odeiv_step_alloc( gsl_odeiv_step_rkf45, State_::STATE_VEC_SIZE );
  else
    gsl_odeiv_step_reset( B_.s_ );

  if ( B_.c_ == 0 )
    B_.c_ = gsl_odeiv_control_y_new( 1e-3, 0.0 );
  else
    gsl_odeiv_control_init( B_.c_, 1e-3, 0.0, 1.0, 0.0 );

  if ( B_.e_ == 0 )
    B_.e_ = gsl_odeiv_evolve_alloc( State_::STATE_VEC_SIZE );
  else
    gsl_odeiv_evolve_reset( B_.e_ );

  B_.sys_.function = ht_neuron_dynamics;
  B_.sys_.jacobian = 0;
  B_.sys_.dimension = State_::STATE_VEC_SIZE;
  B_.sys_.params = reinterpret_cast< void* >( this );

  B_.I_stim_ = 0.0;
}
Beispiel #2
0
int main(int argc, char** argv){
	if(argc < 3){
		fprintf(stderr, "%s gamma gmax\n", argv[0]);
		return EXIT_FAILURE;
	}

	const gsl_odeiv_step_type *T = gsl_odeiv_step_rkf45;
	gsl_odeiv_step 		*s = gsl_odeiv_step_alloc(T, 3);
	gsl_odeiv_control 	*c = gsl_odeiv_control_y_new(1e-6, 0.0); //(abs, rel)
	gsl_odeiv_evolve 	*e = gsl_odeiv_evolve_alloc(3); // 3-dimensional
	vdp_params par = {2, 4, atof(argv[1]), atof(argv[2]), 0, 0};

	gsl_odeiv_system sys = {vdp, jac_vdp, 3, &par};

	double t 	= 0.0;	//Start at this value
	double t1 	= 20;
	double interval = 0.1;
	double h	= 1e-6;	//Start at this timestep
	double y[3]	= {1.0, 0.1, 3.0}; //Initial condition

	while(t < t1){
		int status = gsl_odeiv_evolve_apply(e, c, s, &sys, &t, t1, &h, y);
		if(status != GSL_SUCCESS){
			fprintf(stderr, "Could not evaluate step.");
			break;
		}
//		if(t > t2 + interval){
			printf("%.5e %.5e %.5e %.5e\n", t, y[0], y[1], y[2]);
//			t2 = t;
//		}
	}

	//Print out data to stderr
	fprintf(stderr, "count: %d\n", par.count);
	fprintf(stderr, "jaccount: %d\n", par.jac_count);

	//Free up memory
	gsl_odeiv_evolve_free(e);
	gsl_odeiv_control_free(c);
	gsl_odeiv_step_free(s);
	return EXIT_SUCCESS;
}
void
nest::iaf_cond_alpha_mc::init_buffers_()
{
  B_.spikes_.resize( NUM_SPIKE_RECEPTORS );
  for ( size_t n = 0; n < NUM_SPIKE_RECEPTORS; ++n )
    B_.spikes_[ n ].clear(); // includes resize

  B_.currents_.resize( NUM_CURR_RECEPTORS );
  for ( size_t n = 0; n < NUM_CURR_RECEPTORS; ++n )
    B_.currents_[ n ].clear(); // includes resize

  B_.logger_.reset();
  Archiving_Node::clear_history();

  B_.step_ = Time::get_resolution().get_ms();
  B_.IntegrationStep_ = B_.step_;

  if ( B_.s_ == 0 )
    B_.s_ = gsl_odeiv_step_alloc( gsl_odeiv_step_rkf45, State_::STATE_VEC_SIZE );
  else
    gsl_odeiv_step_reset( B_.s_ );

  if ( B_.c_ == 0 )
    B_.c_ = gsl_odeiv_control_y_new( 1e-3, 0.0 );
  else
    gsl_odeiv_control_init( B_.c_, 1e-3, 0.0, 1.0, 0.0 );

  if ( B_.e_ == 0 )
    B_.e_ = gsl_odeiv_evolve_alloc( State_::STATE_VEC_SIZE );
  else
    gsl_odeiv_evolve_reset( B_.e_ );

  B_.sys_.function = iaf_cond_alpha_mc_dynamics;
  B_.sys_.jacobian = NULL;
  B_.sys_.dimension = State_::STATE_VEC_SIZE;
  B_.sys_.params = reinterpret_cast< void* >( this );

  for ( size_t n = 0; n < NCOMP; ++n )
    B_.I_stim_[ n ] = 0.0;
}
Beispiel #4
0
void ode(double  *params, double maxtime, double * sps)
{
    double *y;
    y = sps;
    const gsl_odeiv_step_type *T = gsl_odeiv_step_gear2;
    gsl_odeiv_step *stp = gsl_odeiv_step_alloc(T, 27);
    gsl_odeiv_control *c = gsl_odeiv_control_y_new(1e-4, 0.0);
    gsl_odeiv_evolve * e = gsl_odeiv_evolve_alloc(27);
    gsl_odeiv_system sys = {func, 0, 27, params};

    double t = 0.0, t1 = maxtime;
    double h = 1e-6;


    while (t < t1) {
        gsl_odeiv_evolve_apply (e, c, stp, &sys, &t, t1, &h, y);
    }
  
    gsl_odeiv_evolve_free (e);
    gsl_odeiv_control_free (c);
    gsl_odeiv_step_free (stp);
}
Beispiel #5
0
void runOde(double * data, double tmax, double dt,ODEparams * pa)
{

	const gsl_odeiv_step_type * T = gsl_odeiv_step_rk8pd;
  	gsl_odeiv_step * s = gsl_odeiv_step_alloc (T, 2);
  	gsl_odeiv_control * c = gsl_odeiv_control_y_new (1e-10, 0.0);
  	gsl_odeiv_evolve * e  = gsl_odeiv_evolve_alloc (2);
  	gsl_odeiv_system sys = {func, jac, 2, pa};
  	int totalframe=ceil(tmax/dt);
  	int i=0;
	double t1=dt,t=0.0;  		
	double h = 1e-10;
	double y[2] = { pa->x0,pa->y0};
	

	while(t1<tmax)
    {
       while (t < t1)
  	    {  
	               int status = gsl_odeiv_evolve_apply (e, c, s,
					       &sys, 
					       &t, t1,
					       &h, y);
	  
        	  if (status != GSL_SUCCESS)
	               break;
      	}	  
	    i++;	
      	t1+=dt;
      	data[i]=y[0];
/*    	printf("%lf %lf\n",t1,y[0]); */

	}
	gsl_odeiv_evolve_free (e);
  	gsl_odeiv_control_free (c);
	gsl_odeiv_step_free (s);

}
Beispiel #6
0
int
main (void)
{
  const gsl_odeiv_step_type * T 
    = gsl_odeiv_step_rk8pd;

  gsl_odeiv_step * s 
    = gsl_odeiv_step_alloc (T, 2);
  gsl_odeiv_control * c 
    = gsl_odeiv_control_y_new (1e-6, 0.0);
  gsl_odeiv_evolve * e 
    = gsl_odeiv_evolve_alloc (2);

  double mu = 10;
  gsl_odeiv_system sys = {func, jac, 2, &mu};

  double t = 0.0, t1 = 100.0;
  double h = 1e-6;
  double y[2] = { 1.0, 0.0 };

  while (t < t1)
    {
      int status = gsl_odeiv_evolve_apply (e, c, s,
                                           &sys, 
                                           &t, t1,
                                           &h, y);

      if (status != GSL_SUCCESS)
          break;

      printf ("%.5e %.5e %.5e\n", t, y[0], y[1]);
    }

  gsl_odeiv_evolve_free (e);
  gsl_odeiv_control_free (c);
  gsl_odeiv_step_free (s);
  return 0;
}
void
nest::iaf_cond_exp::init_buffers_()
{
  B_.spike_exc_.clear(); // includes resize
  B_.spike_inh_.clear(); // includes resize
  B_.currents_.clear();  // includes resize
  Archiving_Node::clear_history();

  B_.logger_.reset();

  B_.step_ = Time::get_resolution().get_ms();
  B_.IntegrationStep_ = B_.step_;

  if ( B_.s_ == 0 )
    B_.s_ =
      gsl_odeiv_step_alloc( gsl_odeiv_step_rkf45, State_::STATE_VEC_SIZE );
  else
    gsl_odeiv_step_reset( B_.s_ );

  if ( B_.c_ == 0 )
    B_.c_ = gsl_odeiv_control_y_new( 1e-3, 0.0 );
  else
    gsl_odeiv_control_init( B_.c_, 1e-3, 0.0, 1.0, 0.0 );

  if ( B_.e_ == 0 )
    B_.e_ = gsl_odeiv_evolve_alloc( State_::STATE_VEC_SIZE );
  else
    gsl_odeiv_evolve_reset( B_.e_ );

  B_.sys_.function = iaf_cond_exp_dynamics;
  B_.sys_.jacobian = NULL;
  B_.sys_.dimension = State_::STATE_VEC_SIZE;
  B_.sys_.params = reinterpret_cast< void* >( this );

  B_.I_stim_ = 0.0;
}
Beispiel #8
0
int
main(int argc, char **argv)
{
    /* DEFAULT VALUES */
    par.potentialonly = 0;
    par.dt = 1e-4;
    par.poincare = 1;
    par.nbpmax = 500;
    par.t1 = 1e2;
    par.r0 = 0.0;
    par.L0 = 0.0;
    par.E0 = 0.0;

    par.V_ex = 0.0;		/* excitation */
    par.omega_ex = 0.0;

    par.rmax = 1.0;		/* cutoff */

    /* PARSE */
    {
	int c;
	extern char *optarg;
	extern int optind;
	while((c = getopt(argc, argv, "Pd:n:t:V:W:")) != -1) {
	    switch(c) {
		case 'P':
		    par.potentialonly = 1;
		    break;
		case 'd':
		    par.dt = atof(optarg);
		    break;
		case 'n':
		    par.nbpmax = atoi(optarg);
		    par.poincare = 1;
		    par.t1 = 1e99;
		    break;
		case 't':
		    par.t1 = atof(optarg);
		    par.poincare = 0;
		    break;
		case 'V':
		    par.V_ex = atof(optarg);
		    break;
		case 'W':
		    par.omega_ex = atof(optarg);
		    break;
		case ':':
		case '?':
		    usage(argv);
		default:
		    fprintf(stderr, "Unknown error\n");
	    }
	}
	if (optind + (par.potentialonly?5:8) > argc)
	    usage(argv);

	V1 = atof(argv[optind++]);
	V2 = atof(argv[optind++]);
	V3 = atof(argv[optind++]);
	V4 = atof(argv[optind++]);
	V5 = atof(argv[optind++]);
	if (!par.potentialonly) {
	    par.E0 = atof(argv[optind++]);
	    par.L0 = atof(argv[optind++]);
	    par.r0 = atof(argv[optind++]);
	}
    }

    /* INIT TRAP */
    {
	size_t k;
	for(k=0; k<NPOINTS; k++) {
	    zi[k] += 0.1525;
	}

	trap = trap_alloc(zi, NPOINTS);
	trap->n = 11;
	trap->R = 0.008;
	trap->Rz = 0.013;
	trap_init(trap);

	Vi[0] = 0.0;
	Vi[1] = Vi[2] = V1;
	Vi[3] = Vi[4] = V2;
	Vi[5] = Vi[6] = V3;
	Vi[7] = Vi[8] = V4;
	Vi[9] = Vi[10] = 0.0;
	Vi[11] = Vi[12] = V5;
	Vi[13] = 0.0;
	assert(NPOINTS == 14);
	trap_set_pot(trap, Vi);


	excit = trap_alloc(zi, NPOINTS);
	excit->n = trap->n;
	excit->R = trap->R;
	excit->Rz = trap->Rz;
	trap_init(excit);
	{
	    double Vex[NPOINTS];
	    for(k=0; k<NPOINTS; k++) {
		Vex[k] = 0.0;
	    }
	    Vex[9] = Vex[10] = par.V_ex;
	    trap_set_pot(excit, Vex);
	}
    }

    /* the -P switch */
    if (par.potentialonly) {
	double z;
	printf("# static potential\n");
	for(z=0.0; z<0.2111; z+=1e-4) {
	    printf("%e %e %e %e %e\n", z,
		    trap_get_pot(trap, z),
		    trap_get_pot1(trap, z),
		    trap_get_pot2(trap, z),
		    trap_get_pot3(trap, z)
		  );
	}
	printf("\n");

	printf("# excitation potential\n");
	for(z=0.0; z<0.2111; z+=1e-4) {
	    printf("%e %e %e %e %e\n", z,
		    trap_get_pot(excit, z),
		    trap_get_pot1(excit, z),
		    trap_get_pot2(excit, z),
		    trap_get_pot3(excit, z)
		  );
	}
	exit(0);
    }

    /* rkck is good for energy conservation */
#define GSLODEIVTYPE	gsl_odeiv_step_rkck
#define GSLODEIVEPSREL	1e-8

    gsl_odeiv_step *s = gsl_odeiv_step_alloc(GSLODEIVTYPE, DIM);
    gsl_odeiv_system sys = {&func, NULL, DIM, trap};
    gsl_odeiv_control *c;
    gsl_odeiv_evolve *e;
    double t = 0.0, tprev, dt = par.dt;
    double y[DIM], yprev[DIM];
    int status;
    size_t iter = 0, npp = 0;

    y[0] = par.r0;		// x_i
    y[1] = 0.0;			// y_i
    y[2] = 0.0;			// z_i

    y[3] = 0.0;						// dot x_i
    y[4] = (par.L0 == 0.0) ? 0.0 : par.L0/par.r0;	// dot y_i
    y[5] = sqrt(2*par.E0 - y[3]*y[3] - y[4]*y[4]);	// dot z_i

    printf("\n");
    printf("#V[i]: %g %g %g %g %g\n", V1, V2, V3, V4, V5);
    printf("#excitation: %g %g\n", par.V_ex, par.omega_ex);
    printf("#E0,L0,r0: %g %g %g\n", par.E0, par.L0, par.r0);
    printf("#y0: %e %e %e %e\n", y[0], y[1], y[2], y[3]);
    printf("# 1:t 2:x 3:y 4:z 5:px 6:py 7:pz\n");

    c = gsl_odeiv_control_y_new(GSLODEIVEPSREL, 0.0);
    e = gsl_odeiv_evolve_alloc(DIM);

    while (t < par.t1)
    {
	tprev = t;
	memcpy(yprev, y, sizeof(y));

	/* ONE STEP */
	status = gsl_odeiv_evolve_apply (e, c, s, &sys, &t, par.t1, &dt, y);
	if (status != GSL_SUCCESS) {
	    fprintf(stderr, "GSL failure. Exiting\n");
	    break;
	}
	if (fabs(y[1]) >= 0.013 || fabs(y[2]) >= 0.24 || gsl_isnan(y[2])) {
	    fprintf(stderr, "Trajectory goes out of bound. Exiting.\n");
	    break;
	}
	if (dt > par.dt) dt = par.dt;

	/* OUTPUT */
	if (par.poincare) {
	    if (yprev[2]*y[2] <= 0.0) {
		double h0 = -yprev[2] / (y[2] - yprev[2]);
		printf("%e %e %e %e %e %e %e\n",
			tprev + h0*dt,
			yprev[0] + h0*(y[0]-yprev[0]),
			yprev[1] + h0*(y[1]-yprev[1]),
			yprev[2] + h0*(y[2]-yprev[2]),
			yprev[3] + h0*(y[3]-yprev[3]),
			yprev[4] + h0*(y[4]-yprev[4]),
			yprev[5] + h0*(y[5]-yprev[5])
		      );
		fflush(stdout);
		npp++;
		if (npp % 10 == 0) {
		    fprintf(stderr, "npp:%zu t:%.3e iter:%zuk dt:%.2e\n",
			    npp, t, iter/1000, dt);
		}
		if (npp >= par.nbpmax) {
		    fprintf(stderr, "Enough points on section. Exiting.\n");
		    break;
		}
	    }
	}
	else {
	    printf("%e %e %e %e %e %e %e\n",
		    t, y[0], y[1], y[2], y[3], y[4], y[5]);
	}
	if (iter % 10000 == 0) {
	    if (par.V_ex == 0.0) {
		double dE = (energy(y) - par.E0) / par.E0;
		fprintf(stderr, "t:%e iter:%zuk dt:%.2e dL0:%+.2e dE0:%+.2e\n",
			t, iter/1000, dt, kinmom(y)-par.L0, dE);
		if (fabs(dE) > 1e-3) {
		    fprintf(stderr, "dE is now too high. Exiting.\n");
		    break;
		}
	    }
	    if (isnan(y[0]) || isnan(y[1]) || hypot(y[0],y[1]) > par.rmax) {
		fprintf(stderr, "Diverging (x:%g, y:%g). Exiting.\n", y[0], y[1]);
		break;
	    }
	}
	iter++;
    }
    fprintf(stderr, "END t:%e (%zu iters) r:%e z:%e\n",
	    t, iter, hypot(y[0],y[1]), y[2]);

    /* */
    gsl_odeiv_evolve_free(e);
    gsl_odeiv_control_free(c);
    gsl_odeiv_step_free(s);
    trap_free(trap);
    return 0;
}
Beispiel #9
0
static gsl_odeiv_control* make_control_y(VALUE epsabs, VALUE epsrel)
{
  Need_Float(epsabs); Need_Float(epsrel);
  return gsl_odeiv_control_y_new(NUM2DBL(epsabs), NUM2DBL(epsrel));
}
Beispiel #10
0
int main()
{
	
  double E2=0.;
  double A,E,beta,te[11],Me[11],Mt[11];
  int num_data;
  FILE *inp, *outp;
	
  inp = fopen("global.dat","r");
  fscanf(inp,"%lf %lf %lf",&A,&E,&beta);    /* read kinetic parameters */
	
  beta/=60.;	/* Convert heating rate to K/s */
  
  double params[]={A,E,beta};
      	
  // read experimental data
  int i=0;
  int status;
  while (1){
    status = fscanf(inp,"%lf %lf",&te[i],&Me[i]);
    if (status == EOF) break;
    i++;
  }
  num_data = i;
  fclose(inp);
	
  // integration
  const gsl_odeiv_step_type * T = gsl_odeiv_step_gear1;
	
  gsl_odeiv_step *s = gsl_odeiv_step_alloc(T,1);
  gsl_odeiv_control *c = gsl_odeiv_control_y_new(1e-6,0.0);
  gsl_odeiv_evolve *e = gsl_odeiv_evolve_alloc(1);
	
  gsl_odeiv_system sys = {func, NULL, 1, (void *)params}; 
	
  double t = 0.1, t1 = 800.;
  double h = 1e-3;
  double M[1];
         M[0]=1.;
  
  outp = fopen("global.out","w");
  fprintf(outp,"Temp (K)    M\n------------------\n");

  i=0;
  double t_old=0., M_old[1];
  M_old[0]=1.;
	
  while (t < t1){
    int status = gsl_odeiv_evolve_apply(e, c, s, &sys, &t, t1, &h, M);
		
    if (status != GSL_SUCCESS) break;
			
    fprintf(outp,"%6.1lf %10.4lf\n",t,M[0]);
		
    if ( (t >= te[i]) && (t_old <= te[i]) ) {
      Mt[i] = (te[i]-t_old)*(M[0]-M_old[0])/(t-t_old) + M_old[0];
      E2+=(Me[i]-Mt[i])*(Me[i]-Mt[i]);
      // fprintf(outp,"%i %lf %lf %lf %lf\n",i,te[i],Me[i],Mt[i],E2);
      i++;
    }
		
    t_old=t;
    M_old[0]=M[0];
	
  }
	
  fprintf(outp,"For A = %g, E = %g, and beta = %.3lf\n",A,E,beta);
  fprintf(outp,"the sum of squared errors is %lf.",E2);

  gsl_odeiv_evolve_free(e);
  gsl_odeiv_control_free(c);
  gsl_odeiv_step_free(s);

  fclose(outp);
	
  return 0;
	
}
Beispiel #11
0
struct_all_ode *new_all_ode_fly(struct_all_ode *old, double *x_init) {
    int i;
    struct_all_ode * s;
    double deltaLs;
    if (old == NULL) {
        s = malloc(sizeof (struct_all_ode));
        s->NQ = 5;
        s->NX = 1 + 2 * s->NQ;
        s->x = malloc(s->NX * sizeof (double));
    } else {
        s = old;
    }
    s->mode = MODE_FLY;
    s->eps_rel = 1e-6;
    s->eps_abs = 1e-6;
    s->h = 1e-6;
    s->hmax = 1;
    //  double deltaLs;
    struct_inputs_fly_dynamics *sf = &(s->inputs_fly_dynamics);
    struct_inputs_sol_dynamics *ss = &(s->inputs_sol_dynamics);
    struct_inputs_extremity_fly *se = &(s->inputs_extremity_fly);
    struct_inputs_delta_vq_contact *sv = &(s->inputs_delta_vq_contact);
    struct_inputs_spring_force *ssf = &(s->inputs_spring_force);
    struct_inputs_fly_trajectory *sift = &s->inputs_fly_trajectory;
    struct_outputs_fly_trajectory *soft = &s->outputs_fly_trajectory;
    struct_inputs_get_energies *sige = &s->inputs_get_energies;
    struct_outputs_get_energies *soge = &s->outputs_get_energies;

    if (old == NULL) {
        init_physical_params(s);
        // initial state, must respect the following order:
        // x[0..2*Nq-1] =[ q[1..NQ; vq[1..NQ] ]
        i = 0;
        s->x[i++] = 0 * M_PI / 180;
        s->x[i++] = -20 * M_PI / 180;
        s->x[i++] = 0;
        s->x[i++] = 1.01 * s->eps_abs;
        s->x[i++] = sf->r;
        s->x[i++] = 0 * M_PI / 180;
        s->x[i++] = 40 * M_PI / 180;
        s->x[i++] = -5 * sin(sf->th);
        s->x[i++] = 5 * cos(sf->th);
        s->x[i++] = 0; // vr for flying model
    }
    if (x_init != NULL) {
        for (i = 0; i < s->NX; i++) {
            s->x[i] = x_init[i];
        }
    }
    i = 0;
    sift->ph = ssf->ph = sf->ph = s->x[i++];
    sift->th = ssf->th = se->th = sf->th = s->x[i++];
    sift->x = ssf->x = se->x = sf->x = s->x[i++];
    sift->z = ssf->z = se->z = sf->z = s->x[i++];
    sift->r = ssf->r = se->r = sf->r = s->x[i++];
    sift->vph = sf->vph = s->x[i++];
    sift->vth = sf->vth = s->x[i++];
    sift->vx = sf->vx = s->x[i++];
    sift->vz = sf->vz = s->x[i++];
    sf->vr = s->x[i++]; // vr for flying model

    //-- for zero crossing --
    s->x[i++] = 0;
    // cf claude samson: le ressort est precontraint a [MB+M3].G pour r0 => -[MB+M3].G = Ks. [ r0-LS0 ]
    s->T = gsl_odeiv_step_rkck;
    if (old != NULL) {
        gsl_odeiv_evolve_free(s->e);
        gsl_odeiv_control_free(s->c);
        gsl_odeiv_step_free(s->s);
    }
    s->s = gsl_odeiv_step_alloc(s->T, s->NX);
    s->c = gsl_odeiv_control_y_new(s->eps_abs, s->eps_rel);
    s->e = gsl_odeiv_evolve_alloc(s->NX);
    s->sys.dimension = s->NX;
    s->sys.function = func_all_ode;
    s->sys.jacobian = NULL;
    s->sys.params = s;
    s->time_second = 0.0;
    s->nb_step = 0;
    if (old == NULL) {
        s->print_values = 0;
        s->print_values_sol_to_fly = 1;
        s->print_values_fly_to_sol = 0;
    }
}
/*This is the main function of the integration program that calls the gsl Runge-Kutta integrator:*/
void integrator(function F, int D, void *params, double x[], double dxdt[], double x0[], double t[], int iters, double s_noise, double abstol, double reltol) {

	/*Temporary variables*/
    double *y = (double*) malloc( sizeof(double)*D ); /*state variable at time t-1 (input) and then at time t(output)*/
    double *dydt_in = (double*) malloc( sizeof(double)*D ); /*rate of change at time point t-1*/
    double *dydt_out= (double*) malloc( sizeof(double)*D ); /*rate of change at time point t*/
    double *yerr = (double*) malloc( sizeof(double)*D );/*error*/
    double t0,tf,tc,dt=(t[1]-t[0])/2,noise;/*initial time point, final time point, current time point, current time step, noise*/
    int j,ii;/*State variable and iteration indexes*/
    int status;/*integrator success flag*/
    
    
    /*Definitions and initializations of gsl integrator necessary inputs and parameters:*/
    
    /*Prepare noise generator*/
    const gsl_rng_type *Q;
    gsl_rng *r;
    gsl_rng_env_setup();
    Q = gsl_rng_default;
    r = gsl_rng_alloc(Q);

    /*Create a stepping function*/
    const gsl_odeiv_step_type *T = gsl_odeiv_step_rkf45;
    gsl_odeiv_step *s  = gsl_odeiv_step_alloc(T, D);
    /*Create an adaptive control function*/
    gsl_odeiv_control *c  = gsl_odeiv_control_y_new(abstol, reltol);
    /*Create the system to be integrated (with NULL jacobian)*/
    gsl_odeiv_system sys = {F, NULL, D, params};
    
    
    /*The integration loop:*/

    /*Initialize*/
    /*Calculate dx/dt for x0*/
    tc=t[0];
    /* initialise dydt_in from system parameters */
    /*GSL_ODEIV_FN_EVAL(&sys, t, y, dydt_in);*/
    GSL_ODEIV_FN_EVAL(&sys, tc, x0, dydt_in);
    for (j=0;j<D;j++) {
        y[j] = x[j] = x0[j];
    }
            
    /*Integration*/
    for (ii=1; ii<iters; ii++) {
        
        /*Call the integrator*/
        /*int gsl_odeiv_step_apply(gsl_odeiv_step * s, double t, double h, double y[], double yerr[], const double dydt_in[], double dydt_out[], const gsl_odeiv_system * dydt)*/        
        t0=t[ii-1];
        tf=t[ii];
        tc=t0;
        
        while (tc<tf) {
            
            /*Constraint time step h such as that tc+h<=tf*/
            if (tc+dt>tf) dt=tf-tc;
            
            /*Advance a h time step*/
            status=gsl_odeiv_step_apply(s, tc, dt, y, yerr, dydt_in, dydt_out, &sys);
            if (status != GSL_SUCCESS) break;
            
            /*Modify time sep*/
            gsl_odeiv_control_hadjust(c,s,y,yerr,dydt_in,&dt);

            /*Increase current time*/
            tc += dt;

            /*Add noise*/
            for (j=0;j<D;j++) {
                noise=gsl_ran_gaussian_ziggurat(r, s_noise);
                y[j] += sqrt(dt)*noise;
                //dydt_in[j]+=noise;
            }
        
        }
        
        /*Unpack and store result for this time point*/
       if (status != GSL_SUCCESS) break;
        
        for (j=0;j<D;j++) { 
            
            x[ii*D+j] = y[j];
            dxdt[(ii-1)*D+j] = dydt_in[j];
            
            /*Prepare next step*/
            dydt_in[j] = dydt_out[j];
            
        }
 
    }
    /*Get dxdt for the last time point*/
    for (j=0;j<D;j++) dxdt[(iters-1)*D+j] = dydt_out[j];
        
    
    /*Free dynamically allocated memory*/
    gsl_odeiv_control_free(c);
    printf("c freed\n");
    gsl_odeiv_step_free(s);
    printf("s freed\n");
    gsl_rng_free(r);
    printf("rng freed\n");
    free(yerr);
    printf("yerr freed\n");
    free(dydt_out);
    printf("dydt_out freed\n");
    free(dydt_in);
    printf("dydt_in freed\n");
    free(y);
    printf("y freed\n");

}
Beispiel #13
0
double
fevol_shrinker (double bisec_param, int print, char * filename, void * p)
{
 const gsl_odeiv_step_type * T
    = STEPPER;

  gsl_odeiv_step * s
    = gsl_odeiv_step_alloc (T, 2);
  gsl_odeiv_control * c
    = gsl_odeiv_control_y_new (STEPPER_ERROR, 0.0);
  gsl_odeiv_evolve * e
    = gsl_odeiv_evolve_alloc (2);

  double dt=PRINT_DT, t_last=0.;

  gsl_odeiv_system sys = {func_shrinker, jac_dummy, 2, p};

  double t = T0;
  double h = H0;
  double A = bisec_param;
  double y[2] = {
    A*t + ((3*A - 4*(-1 + k)*pow(A,3))*pow(2 + k,-1)*pow(t,3))/12. +
    (A*(15 - 40*(-1 + k)*pow(A,2) + 16*pow(A,4)*(1 - 3*k + 2*pow(k,2)))*
     pow(t,5)*pow(8 + 6*k + pow(k,2),-1))/160.,
    A + ((3*A - 4*(-1 + k)*pow(A,3))*pow(2 + k,-1)*pow(t,2))/4. +
    (A*(15 - 40*(-1 + k)*pow(A,2) + 16*pow(A,4)*(1 - 3*k + 2*pow(k,2)))*
     pow(t,4)*pow(8 + 6*k + pow(k,2),-1))/32.,
  };

  FILE * file;

  if (print){
    file = fopen(filename, "a");
    fprintf(file, "# A = %.15f\n", bisec_param );
  }

  while (t < T_MAX)
    {
      int status =
	gsl_odeiv_evolve_apply (e, c, s,
				&sys,
				&t, T_MAX,
				&h, y);

      if (status != GSL_SUCCESS)
	break;
      /* are we still in the strip [0,pi]? */
      if ( 0. > y[0] || y[0] > 3.15 )
	break;

      if (print /* && t_last+dt < t */)
	{
	  fprintf (file,
		   "%.15f %.15f %.15f\n",
		   t, y[0], y[1]/* , y[2], y[3] */);
	  t_last+=dt;
	  dt*=PRINT_DT_RATIO;
	}
      /* printf("%.15f\r",t); */
    }

  gsl_odeiv_evolve_free (e);
  gsl_odeiv_control_free (c);
  gsl_odeiv_step_free (s);
  if (print) {
    fprintf( file, "\n\n\n" );
    fclose( file );
  }

  return y[1];
}
Beispiel #14
0
/* combination EPS_ABS = 1e-12, EPS_REL=0.0, method = 1 = RK Cash-Karp
 is believed to be predictable and accurate; returns GSL_SUCCESS=0 if success */
int exactM_old(double r,
               double *alpha, double *Drr,
               const double EPS_ABS, const double EPS_REL, const int ode_method) {

   double ug = 100.0 / SperA;  /* velocity across grounding line is 100 m/a */
   double DrrRg, xx, xA, nu, aa, rr, myalf, step;
   const gsl_odeiv_step_type* T;
   int status = NOT_DONE;
   gsl_odeiv_step*    s;
   gsl_odeiv_control* c;
   gsl_odeiv_evolve*  e;
   gsl_odeiv_system   sys = {funcM_ode_G, NULL, 1, NULL};  /* Jac-free method and no params */

   if (r < 0) {
     return NEGATIVE_R;  /* only nonnegative radial coord allowed */
   } else if (r <= Rg/4.0) {
     *alpha = 0.0;  /* zero velocity near center */
     *Drr = 0.0;
     return GSL_SUCCESS;
   } else if (r <= Rg) {
     /* power law from alpha=0 to alpha=ug in   Rg/4 < r <= Rg;
        f(r) w: f(Rg/4)=f'(Rg/4)=0 and f(Rg)=ug and f(Rg) = DrrRg         */
     funcM_ode_G(Rg, &ug, &DrrRg, NULL);  /* first get Drr = alpha' at Rg where alpha=ug */
     /* printf("DrrRg=%e (1/a)\n",DrrRg*SperA); */
     xx = r - 0.25 * Rg;
     xA = 0.75 * Rg;
     nu = DrrRg * xA / ug;
     aa = ug / pow(xA, nu);
     /* printf("power nu=%e\n",nu); */
     *alpha = aa * pow(xx, nu);
     *Drr = aa * nu * pow(xx, nu - 1);
     return GSL_SUCCESS;
   } else if (r >= Rc + 1.0) {
     *alpha = 0.0;  /* zero velocity beyond calving front */
     *Drr = 0.0;
     return GSL_SUCCESS;
   }
   
   /* need to solve ODE to find alpha, so setup for GSL ODE solver  */
   switch (ode_method) {
     case 1:
       T = gsl_odeiv_step_rkck; /* RK Cash-Karp */
       break;
     case 2:
       T = gsl_odeiv_step_rk2;
       break;
     case 3:
       T = gsl_odeiv_step_rk4;
       break;
     case 4:
       T = gsl_odeiv_step_rk8pd;
       break;
     default:
       printf("INVALID ode_method in exactM(): must be 1,2,3,4\n");
       return INVALID_METHOD;
   }
   s = gsl_odeiv_step_alloc(T, (size_t)1);     /* one scalar ode */
   c = gsl_odeiv_control_y_new(EPS_ABS,EPS_REL);
   e = gsl_odeiv_evolve_alloc((size_t)1);    /* one scalar ode */

   /* initial conditions: (r,alf) = (Rg,ug);  r increases */
   rr = Rg; 
   myalf = ug;
   /* printf (" r (km)        alpha (m/a)\n");
      printf (" %11.5e   %11.5e\n", rr/1000.0, myalf * SperA); */
   while (rr < r) {
     /* step = r - rr;  try to get to solution in one step; trust stepping algorithm */
     step = MIN(r-rr,20.0e3);
     status = gsl_odeiv_evolve_apply(e, c, s, &sys, &rr, r, &step, &myalf);
     if (status != GSL_SUCCESS)   break;
     /* printf (" %11.5e   %11.5e\n", rr/1000.0, myalf * SperA); */
   }

   gsl_odeiv_evolve_free(e);
   gsl_odeiv_control_free(c);
   gsl_odeiv_step_free(s);

   *alpha = myalf;
   funcM_ode_G(r, alpha, Drr, NULL);
   return status;
}
Beispiel #15
0
// The actual (user defined) qoi routine
void qoiRoutine(const QUESO::GslVector&                    paramValues,
                const QUESO::GslVector*                    paramDirection,
                const void*                                functionDataPtr,
                      QUESO::GslVector&                    qoiValues,
                      QUESO::DistArray<QUESO::GslVector*>* gradVectors,
                      QUESO::DistArray<QUESO::GslMatrix*>* hessianMatrices,
                      QUESO::DistArray<QUESO::GslVector*>* hessianEffects)
{
  if (paramDirection  &&
      functionDataPtr && 
      gradVectors     &&
      hessianMatrices &&
      hessianEffects) {
    // Just to eliminate INTEL compiler warnings
  }

  double A             = paramValues[0];
  double E             = paramValues[1];
  double beta          = ((qoiRoutine_Data *) functionDataPtr)->m_beta;
  double criticalMass  = ((qoiRoutine_Data *) functionDataPtr)->m_criticalMass;
  double criticalTime  = ((qoiRoutine_Data *) functionDataPtr)->m_criticalTime;

  double params[]={A,E,beta};
      	
  // integration
  const gsl_odeiv_step_type *T   = gsl_odeiv_step_rkf45; //rkf45; //gear1;
        gsl_odeiv_step      *s   = gsl_odeiv_step_alloc(T,1);
        gsl_odeiv_control   *c   = gsl_odeiv_control_y_new(1e-6,0.0);
        gsl_odeiv_evolve    *e   = gsl_odeiv_evolve_alloc(1);
        gsl_odeiv_system     sys = {func, NULL, 1, (void *)params}; 
	
  double temperature = 0.1;
  double h = 1e-3;
  double Mass[1];
  Mass[0]=1.;
  
  double temperature_old = 0.;
  double M_old[1];
  M_old[0]=1.;
	
  double crossingTemperature = 0.;
  //unsigned int loopSize = 0;
  while ((temperature < criticalTime*beta) &&
         (Mass[0]     > criticalMass     )) {
    int status = gsl_odeiv_evolve_apply(e, c, s, &sys, &temperature, criticalTime*beta, &h, Mass);
    UQ_FATAL_TEST_MACRO((status != GSL_SUCCESS),
                        paramValues.env().fullRank(),
                        "qoiRoutine()",
                        "gsl_odeiv_evolve_apply() failed");
    //printf("t = %6.1lf, mass = %10.4lf\n",t,Mass[0]);
    //loopSize++;

    if (Mass[0] <= criticalMass) {
      crossingTemperature = temperature_old + (temperature - temperature_old) * (M_old[0]-criticalMass)/(M_old[0]-Mass[0]);
    }
		
    temperature_old=temperature;
    M_old[0]=Mass[0];
  }

  if (criticalMass > 0.) qoiValues[0] = crossingTemperature/beta; // QoI = time to achieve critical mass
  if (criticalTime > 0.) qoiValues[0] = Mass[0];                  // QoI = mass fraction remaining at critical time
	
  //printf("loopSize = %d\n",loopSize);
  if ((paramValues.env().displayVerbosity() >= 3) && (paramValues.env().fullRank() == 0)) {
    printf("In qoiRoutine(), A = %g, E = %g, beta = %.3lf, criticalTime = %.3lf, criticalMass = %.3lf: qoi = %lf.\n",A,E,beta,criticalTime,criticalMass,qoiValues[0]);
  }

  gsl_odeiv_evolve_free (e);
  gsl_odeiv_control_free(c);
  gsl_odeiv_step_free   (s);

  return;
}
Beispiel #16
0
MATRICEd *lsoda_oscillatore1(MATRICEd *ris, LISTA *params, VETTOREd *X0, VETTOREd *times, const char *metodo, double atol, double rtol, double stat_thr, double stat_width)
{
	gsl_odeiv_step_type *T =NULL;
	int i, j;
	MATRICEd *tmp = NULL;
	Params p;

	_Intestazione("\n*** lsoda_oscillatore1 ***\n");

	// M<-parms[[1]]
	CtrlLlst(params, 1);
	p.m = ACCEDIv_d(ACCEDIlst(params, 1, vd), 1);
	// R<-parms[[2]]
	// da parametro
	// N<-parms[[3]]
	CtrlLlst(params, 2);
	p.b = ACCEDIv_d(ACCEDIlst(params, 2, vd), 1);
	// k<-parms[[4]]
	CtrlLlst(params, 3);
	p.k = ACCEDIv_d(ACCEDIlst(params, 3, vd), 1);
	// alpha<-parms[[5]]
	CtrlLlst(params, 4);
	p.F = ACCEDIv_d(ACCEDIlst(params, 4, vd), 1);
	// theta<-parms[[6]]
	CtrlLlst(params, 5);
	p.o = ACCEDIv_d(ACCEDIlst(params, 5, vd), 1);

	if (!strncmp(metodo, "rkf45", 5))
		T = gsl_odeiv_step_rkf45;
	else if (!strncmp(metodo, "rkck", 4))
		T = gsl_odeiv_step_rkck;
	else if (!strncmp(metodo, "rk8pd", 5))
		T = gsl_odeiv_step_rk8pd;
	else if (!strncmp(metodo, "rk2imp", 6))
		T = gsl_odeiv_step_rk2imp;
	else if (!strncmp(metodo, "rk4imp", 6))
		T = gsl_odeiv_step_rk4imp;
	else if (!strncmp(metodo, "gear1", 5))
		T = gsl_odeiv_step_gear1;
	else if (!strncmp(metodo, "gear2", 5))
		T = gsl_odeiv_step_gear2;
	else
		error("The requested stepping function does not exist!\n");

	gsl_odeiv_step *s = gsl_odeiv_step_alloc (T, 2);
	gsl_odeiv_control *c = gsl_odeiv_control_y_new (atol, rtol);
	gsl_odeiv_evolve *e	= gsl_odeiv_evolve_alloc (2);

	gsl_odeiv_system sys = {funzione, NULL, 2, &p};

	double t = 0.0;
	double h = 1e-6;
	double y[2] = { ACCEDIv_d(X0, 1), ACCEDIv_d(X0, 2) };

	CREAm_d(tmp, LENGTHv_d(times), 3);
	int st = 0;
	double y1 = 0.0;
	double max_stat;
	if (stat_width == 0.0)
		max_stat = LENGTHv_d(times);
	else
		max_stat = (double) stat_width * LENGTHv_d(times);
	for (i = 1; (!stat_width || st < max_stat) && i <= LENGTHv_d(times); i++) {
		double ti = ACCEDIv_d(times, i);

		while (t < ti) {
			gsl_odeiv_evolve_apply (e, c, s,
			                        &sys,
			                        &t, ti, &h,
			                        y);
		}

		if (fabs(y[0] - y1) < stat_thr)
			st++;
	  ASSEGNAm_d(tmp, i, 1, t);
	  ASSEGNAm_d(tmp, i, 2, y[0]);
	  ASSEGNAm_d(tmp, i, 3, y[1]);
		y1 = y[0];
	}
	CREAm_d(ris, i - 1, 3);
	for (j = 1; j < i; j++)
		copia1_m_riga_d(ris, j, tmp, j);
	gsl_odeiv_evolve_free (e);
	gsl_odeiv_control_free (c);
	gsl_odeiv_step_free (s);

	StrBilanciam();

   return ris;
}
Beispiel #17
0
int ode(int method, double h, double eps_abs, double eps_rel,
        int f(double, int, const double*, int, double*),
        int jac(double, int, const double*, int, int, double*),
        KRVEC(xi), KRVEC(ts), RMAT(sol)) {

    const gsl_odeiv_step_type * T;

    switch(method) {
        case 0 : {T = gsl_odeiv_step_rk2; break; }
        case 1 : {T = gsl_odeiv_step_rk4; break; }
        case 2 : {T = gsl_odeiv_step_rkf45; break; }
        case 3 : {T = gsl_odeiv_step_rkck; break; }
        case 4 : {T = gsl_odeiv_step_rk8pd; break; }
        case 5 : {T = gsl_odeiv_step_rk2imp; break; }
        case 6 : {T = gsl_odeiv_step_rk4imp; break; }
        case 7 : {T = gsl_odeiv_step_bsimp; break; }
        case 8 : { printf("Sorry: ODE rk1imp not available in this GSL version\n"); exit(0); }
        case 9 : { printf("Sorry: ODE msadams not available in this GSL version\n"); exit(0); }
        case 10: { printf("Sorry: ODE msbdf not available in this GSL version\n"); exit(0); }
        default: ERROR(BAD_CODE);
    }

    gsl_odeiv_step * s = gsl_odeiv_step_alloc (T, xin);
    gsl_odeiv_control * c = gsl_odeiv_control_y_new (eps_abs, eps_rel);
    gsl_odeiv_evolve * e = gsl_odeiv_evolve_alloc (xin);

    Tode P;
    P.f = f;
    P.j = jac;
    P.n = xin;

    gsl_odeiv_system sys = {odefunc, odejac, xin, &P};

    double t = tsp[0];

    double* y = (double*)calloc(xin,sizeof(double));
    int i,j;
    for(i=0; i< xin; i++) {
        y[i] = xip[i];
        solp[i] = xip[i];
    }

       for (i = 1; i < tsn ; i++)
         {
           double ti = tsp[i];
           while (t < ti)
             {
               gsl_odeiv_evolve_apply (e, c, s,
                                       &sys,
                                       &t, ti, &h,
                                       y);
               // if (h < hmin) h = hmin;
             }
           for(j=0; j<xin; j++) {
               solp[i*xin + j] = y[j];
           }
         }

    free(y);
    gsl_odeiv_evolve_free (e);
    gsl_odeiv_control_free (c);
    gsl_odeiv_step_free (s);
    return 0;
}
Beispiel #18
0
void GslInternal::init(){
  // Init ODE rhs function and quadrature functions
  f_.init();
  casadi_assert(f_.getNumInputs()==DAE_NUM_IN);
  casadi_assert(f_.getNumOutputs()==DAE_NUM_OUT);
  if(!q_.isNull()){
    q_.init();
    casadi_assert(q_.getNumInputs()==DAE_NUM_IN);
    casadi_assert(q_.getNumOutputs()==DAE_NUM_OUT);
  }

  // Number of states
  int nx = f_.output(INTEGRATOR_XF).numel();

  // Add quadratures, if any
  if(!q_.isNull()) nx += q_.output().numel();

  // Number of parameters
  int np = f_.input(DAE_P).numel();

  setDimensions(nx,np);

  // If time was not specified, initialise it.
  if (f_.input(DAE_T).numel()==0) {
    std::vector<MX> in1(DAE_NUM_IN);
    in1[DAE_T] = MX("T");
    in1[DAE_Y] = MX("Y",f_.input(DAE_Y).size1(),f_.input(DAE_Y).size2());
    in1[DAE_YDOT] = MX("YDOT",f_.input(DAE_YDOT).size1(),f_.input(DAE_YDOT).size2());
    in1[DAE_P] = MX("P",f_.input(DAE_P).size1(),f_.input(DAE_P).size2());
    std::vector<MX> in2(in1);
    in2[DAE_T] = MX();
    f_ = MXFunction(in1,f_.call(in2));
    f_.init();
  }
  
  // We only allow for 0-D time
  casadi_assert_message(f_.input(DAE_T).numel()==1, "IntegratorInternal: time must be zero-dimensional, not (" <<  f_.input(DAE_T).size1() << 'x' << f_.input(DAE_T).size2() << ")");
  
  // ODE right hand side must be a dense matrix
  casadi_assert_message(f_.output(DAE_RES).dense(),"ODE right hand side must be dense: reformulate the problem");
  
  // States and RHS should match 
  casadi_assert_message(f_.output(DAE_RES).size()==f_.input(DAE_Y).size(),
    "IntegratorInternal: rhs of ODE is (" <<  f_.output(DAE_RES).size1() << 'x' << f_.output(DAE_RES).size2() << ") - " << f_.output(DAE_RES).size() << " non-zeros" << std::endl <<
    "              ODE state matrix is (" <<  f_.input(DAE_Y).size1() << 'x' << f_.input(DAE_Y).size2() << ") - " << f_.input(DAE_Y).size() << " non-zeros" << std::endl <<
    "Mismatch between number of non-zeros"
  );

  IntegratorInternal::init();
  
  jac_f_ = Jacobian(f_,DAE_Y,DAE_RES);
  dt_f_ = Jacobian(f_,DAE_T,DAE_RES);
  
  jac_f_.init();
  dt_f_.init();
  
  // define the type of routine for making steps: 
  type_ptr = gsl_odeiv_step_rkf45;
  // some other possibilities (see GSL manual):          
  //   = gsl_odeiv_step_rk4;
  //   = gsl_odeiv_step_rkck;
  //   = gsl_odeiv_step_rk8pd;
  //   = gsl_odeiv_step_rk4imp;
  //   = gsl_odeiv_step_bsimp;  
  //   = gsl_odeiv_step_gear1;
  //   = gsl_odeiv_step_gear2;
  
  step_ptr = gsl_odeiv_step_alloc (type_ptr, nx);
  control_ptr = gsl_odeiv_control_y_new (abstol_, reltol_);
  evolve_ptr = gsl_odeiv_evolve_alloc (nx);
  
  my_system.function = rhs_wrapper;	// the right-hand-side functions dy[i]/dt 
  my_system.jacobian = jac_wrapper;	// the Jacobian df[i]/dy[j] 
  my_system.dimension = nx;	// number of diffeq's 
  my_system.params = this;	// parameters to pass to rhs and jacobian
  
  is_init = true;
  
}
int
main (void)
{
  const gsl_odeiv_step_type * T 
    = gsl_odeiv_step_rk8pd;

  gsl_odeiv_step * s 
    = gsl_odeiv_step_alloc (T, 4);
  gsl_odeiv_control * c 
    = gsl_odeiv_control_y_new (1e-6, 0.0);
  gsl_odeiv_evolve * e 
    = gsl_odeiv_evolve_alloc (4);
  double d; 
  gsl_odeiv_system sys = {func, jac, 4, &d};
  int ii=1, num = 100; // minimum number of points to have 
  double t = 0.0, t1 =.140, ti;
  double h = 1e-6, velx, vely, vtot, vangle;
 
// initialization of all the shooter physical parameters. 
 pi = 4.0*atan(1.0); 
 m = 1.4;              //mass of the ball, Kg
 r = .3;               //radius of the ball, meters 
 Iball = 2.0*m/5.0*r*r;    // I of ball
 l0 = .45;            // distance between pivot and ball rest, m
 gamm = .5;         // angle of the wrist. radians
 Iarm = (1.75)*l0*l0/3.0; // moment of inertia of the catapult ar,
   k = 70;           // torsional spring constant of the catapult spring. j/rad/rad
 marmg_gam = 13.7;     // m_catapult_arm * g * cm distance to pivot. For the potential energy in the arm lift; 
 theta0 = 90*2*pi/360; // the zero of the force is at theta0. For this simulation, it is combined wiht k to get the starting torque and how fast that decreases with angle. 
 g = 9.8;            // gravity , m/s^2
 A =m*r*r+Iball;  
 B = Iball+Iarm+m*(l0*l0+r*r)-2*l0*r*sin(gamm);  // ignoring the non-linear terms here in the coef...not too systematic!!!
 C =  -Iball + m*r*(l0*sin(gamm)-r); 
 det = A*B-C*C; 
 // initial conditions in y = phi, phidot, theta, thetadot
  double y[4] = { 0.0, 0.0, -0.40, 0.0};// initial consditions (phi, phidot, theta, thetadot) 
  while (ii < num) 
    { 
      ti = ii*t1/num; 
      ii++;
  while (t < ti)
      {
      int status = gsl_odeiv_evolve_apply (e, c, s,
                                           &sys, 
                                           &t, ti,
                                           &h, y);

      if (status != GSL_SUCCESS)
          break;
      // have to use the release co-ordinates to determine the final velocity vector of the ball. 
      velx = -l0*sin(y[2])*y[3]+r*y[1]*cos(y[2]+gamm)-r*y[0]*y[3]*sin(y[2]+gamm)-r*y[3]*cos(y[2]+gamm); 
      vely = l0*y[3]*cos(y[2])+r*y[0]*y[3]*cos(y[2]+gamm)+r*y[1]*sin(y[2]+gamm)-r*y[3]*sin(y[2]+gamm); 
	//      printf("%.5e %.5e %.5e  %.5e  %.5e\n", t, y[0], y[1], y[2], y[3]);
      vtot = pow(velx*velx+vely*vely,.5); 
      vangle = -180*atan(vely/velx)/pi; 
      printf("%.5e %.5e %.5e  %.5e  %.5e  %.5e\n", t, y[0], 180*y[2]/pi, vtot, vangle, y[1]-y[3]);
      }
    }
  gsl_odeiv_evolve_free(e);
  gsl_odeiv_control_free(c);
  gsl_odeiv_step_free(s);
  return 0;
}
/**
 * @brief Integrates the Tolman-Oppenheimer-Volkov stellar structure equations.
 * @details
 * Solves the Tolman-Oppenheimer-Volkov stellar structure equations using the
 * pseudo-enthalpy formalism introduced in:
 * Lindblom (1992) "Determining the Nuclear Equation of State from Neutron-Star
 * Masses and Radii", Astrophys. J. 398 569.
 * @param[out] radius The radius of the star in m.
 * @param[out] mass The mass of the star in kg.
 * @param[out] love_number_k2 The k_2 tidal love number of the star.
 * @param[in] central_pressure_si The central pressure of the star in Pa.
 * @param eos Pointer to the Equation of State structure.
 * @retval 0 Success.
 * @retval <0 Failure.
 */
int XLALSimNeutronStarTOVODEIntegrate(double *radius, double *mass,
    double *love_number_k2, double central_pressure_si,
    LALSimNeutronStarEOS * eos)
{
    /* ode integration variables */
    const double epsabs = 0.0, epsrel = 1e-6;
    double y[TOV_ODE_VARS_DIM];
    double dy[TOV_ODE_VARS_DIM];
    struct tov_ode_vars *vars = tov_ode_vars_cast(y);
    gsl_odeiv_system sys = { tov_ode, NULL, TOV_ODE_VARS_DIM, eos };
    gsl_odeiv_step *step =
        gsl_odeiv_step_alloc(gsl_odeiv_step_rk8pd, TOV_ODE_VARS_DIM);
    gsl_odeiv_control *ctrl = gsl_odeiv_control_y_new(epsabs, epsrel);
    gsl_odeiv_evolve *evolv = gsl_odeiv_evolve_alloc(TOV_ODE_VARS_DIM);

    /* central values */
    /* note: will be updated with Lindblom's series expansion */
    /* geometrisized units for variables in length (m) */
    double pc = central_pressure_si * LAL_G_C4_SI;
    double ec =
        XLALSimNeutronStarEOSEnergyDensityOfPressureGeometerized(pc, eos);
    double hc =
        XLALSimNeutronStarEOSPseudoEnthalpyOfPressureGeometerized(pc, eos);
    double dedp_c =
        XLALSimNeutronStarEOSEnergyDensityDerivOfPressureGeometerized(pc, eos);
    double dhdp_c = 1.0 / (ec + pc);
    double dedh_c = dedp_c / dhdp_c;
    double dh = -1e-3 * hc;
    double h0 = hc + dh;
    double h1 = 0.0 - dh;
    double r0 = sqrt(-3.0 * dh / (2.0 * LAL_PI * (ec + 3.0 * pc)));
    double m0 = 4.0 * LAL_PI * r0 * r0 * r0 * ec / 3.0;
    double H0 = r0 * r0;
    double b0 = 2.0 * r0;

    double yy;
    double c;
    double h;
    size_t i;

    /* series expansion for the initial core */

    /* second factor of Eq. (7) of Lindblom (1992) */
    r0 *= 1.0 + 0.25 * dh * (ec - 3.0 * pc  - 0.6 * dedh_c) / (ec + 3.0 * pc);
    /* second factor of Eq. (8) of Lindblom (1992) */
    m0 *= 1.0 + 0.6 * dh * dedh_c / ec;

    /* perform integration */
    vars->r = r0;
    vars->m = m0;
    vars->H = H0;
    vars->b = b0;
    h = h0;
    while (h > h1) {
        int s =
            gsl_odeiv_evolve_apply(evolv, ctrl, step, &sys, &h, h1, &dh, y);
        if (s != GSL_SUCCESS)
            XLAL_ERROR(XLAL_EERR,
                "Error encountered in GSL's ODE integrator\n");
    }

    /* take one final Euler step to get to surface */
    tov_ode(h, y, dy, eos);
    for (i = 0; i < TOV_ODE_VARS_DIM; ++i)
        y[i] += dy[i] * (0.0 - h1);

    /* compute tidal Love number k2 */
    c = vars->m / vars->r;      /* compactness */
    yy = vars->r * vars->b / vars->H;

    /* convert from geometric units to SI units */
    *radius = vars->r;
    *mass = vars->m * LAL_MSUN_SI / LAL_MRSUN_SI;
    *love_number_k2 = tidal_Love_number_k2(c, yy);

    /* free ode memory */
    gsl_odeiv_evolve_free(evolv);
    gsl_odeiv_control_free(ctrl);
    gsl_odeiv_step_free(step);
    return 0;
}
Beispiel #21
0
double
Likelihood<V, M>::lnValue(const QUESO::GslVector & paramValues) const
{
  double resultValue = 0.;

  m_env.subComm().Barrier();
  //env.syncPrintDebugMsg("Entering likelihoodRoutine()",1,env.fullComm());

  // Compute likelihood for scenario 1
  double betaTest = m_beta1;
  if (betaTest) {
    double A                       = paramValues[0];
    double E                       = paramValues[1];
    double beta                    = m_beta1;
    double variance                = m_variance1;
    const std::vector<double>& Te  = m_Te1;
    const std::vector<double>& Me  = m_Me1;
    std::vector<double> Mt(Me.size(),0.);

    double params[]={A,E,beta};

    // integration
    const gsl_odeiv_step_type *T   = gsl_odeiv_step_rkf45; //rkf45; //gear1;
          gsl_odeiv_step      *s   = gsl_odeiv_step_alloc(T,1);
          gsl_odeiv_control   *c   = gsl_odeiv_control_y_new(1e-6,0.0);
          gsl_odeiv_evolve    *e   = gsl_odeiv_evolve_alloc(1);
          gsl_odeiv_system     sys = {func, NULL, 1, (void *)params};

    double t = 0.1, t_final = 1900.;
    double h = 1e-3;
    double Mass[1];
    Mass[0]=1.;

    unsigned int i = 0;
    double t_old = 0.;
    double M_old[1];
    M_old[0]=1.;

    double misfit=0.;
    //unsigned int loopSize = 0;
    while ((t < t_final) && (i < Me.size())) {
      int status = gsl_odeiv_evolve_apply(e, c, s, &sys, &t, t_final, &h, Mass);
      UQ_FATAL_TEST_MACRO((status != GSL_SUCCESS),
                          paramValues.env().fullRank(),
                          "likelihoodRoutine()",
                          "gsl_odeiv_evolve_apply() failed");
      //printf("t = %6.1lf, mass = %10.4lf\n",t,Mass[0]);
      //loopSize++;

      while ( (i < Me.size()) && (t_old <= Te[i]) && (Te[i] <= t) ) {
        Mt[i] = (Te[i]-t_old)*(Mass[0]-M_old[0])/(t-t_old) + M_old[0];
        misfit += (Me[i]-Mt[i])*(Me[i]-Mt[i]);
        //printf("%i %lf %lf %lf %lf\n",i,Te[i],Me[i],Mt[i],misfit);
        i++;
      }

      t_old=t;
      M_old[0]=Mass[0];
    }
    resultValue += misfit/variance;

    //printf("loopSize = %d\n",loopSize);
    if ((paramValues.env().displayVerbosity() >= 10) && (paramValues.env().fullRank() == 0)) {
      printf("In likelihoodRoutine(), A = %g, E = %g, beta = %.3lf: misfit = %lf, likelihood = %lf.\n",A,E,beta,misfit,resultValue);
    }

    gsl_odeiv_evolve_free (e);
    gsl_odeiv_control_free(c);
    gsl_odeiv_step_free   (s);
  }

  // Compute likelihood for scenario 2
  betaTest = m_beta2;
  if (betaTest > 0.) {
    double A                       = paramValues[0];
    double E                       = paramValues[1];
    double beta                    = m_beta2;
    double variance                = m_variance2;
    const std::vector<double>& Te  = m_Te2;
    const std::vector<double>& Me  = m_Me2;
    std::vector<double> Mt(Me.size(),0.);

    double params[]={A,E,beta};

    // integration
    const gsl_odeiv_step_type *T   = gsl_odeiv_step_rkf45; //rkf45; //gear1;
          gsl_odeiv_step      *s   = gsl_odeiv_step_alloc(T,1);
          gsl_odeiv_control   *c   = gsl_odeiv_control_y_new(1e-6,0.0);
          gsl_odeiv_evolve    *e   = gsl_odeiv_evolve_alloc(1);
          gsl_odeiv_system     sys = {func, NULL, 1, (void *)params};

    double t = 0.1, t_final = 1900.;
    double h = 1e-3;
    double Mass[1];
    Mass[0]=1.;

    unsigned int i = 0;
    double t_old = 0.;
    double M_old[1];
    M_old[0]=1.;

    double misfit=0.;
    //unsigned int loopSize = 0;
    while ((t < t_final) && (i < Me.size())) {
      int status = gsl_odeiv_evolve_apply(e, c, s, &sys, &t, t_final, &h, Mass);
      UQ_FATAL_TEST_MACRO((status != GSL_SUCCESS),
                          paramValues.env().fullRank(),
                          "likelihoodRoutine()",
                          "gsl_odeiv_evolve_apply() failed");
      //printf("t = %6.1lf, mass = %10.4lf\n",t,Mass[0]);
      //loopSize++;

      while ( (i < Me.size()) && (t_old <= Te[i]) && (Te[i] <= t) ) {
        Mt[i] = (Te[i]-t_old)*(Mass[0]-M_old[0])/(t-t_old) + M_old[0];
        misfit += (Me[i]-Mt[i])*(Me[i]-Mt[i]);
        //printf("%i %lf %lf %lf %lf\n",i,Te[i],Me[i],Mt[i],misfit);
        i++;
      }

      t_old=t;
      M_old[0]=Mass[0];
    }
    resultValue += misfit/variance;

    //printf("loopSize = %d\n",loopSize);
    if ((paramValues.env().displayVerbosity() >= 10) && (paramValues.env().fullRank() == 0)) {
      printf("In likelihoodRoutine(), A = %g, E = %g, beta = %.3lf: misfit = %lf, likelihood = %lf.\n",A,E,beta,misfit,resultValue);
    }

    gsl_odeiv_evolve_free (e);
    gsl_odeiv_control_free(c);
    gsl_odeiv_step_free   (s);
  }

  // Compute likelihood for scenario 3
  betaTest = m_beta3;
  if (betaTest > 0.) {
    double A                       = paramValues[0];
    double E                       = paramValues[1];
    double beta                    = m_beta3;
    double variance                = m_variance3;
    const std::vector<double>& Te  = m_Te3;
    const std::vector<double>& Me  = m_Me3;
    std::vector<double> Mt(Me.size(),0.);

    double params[]={A,E,beta};

    // integration
    const gsl_odeiv_step_type *T   = gsl_odeiv_step_rkf45; //rkf45; //gear1;
          gsl_odeiv_step      *s   = gsl_odeiv_step_alloc(T,1);
          gsl_odeiv_control   *c   = gsl_odeiv_control_y_new(1e-6,0.0);
          gsl_odeiv_evolve    *e   = gsl_odeiv_evolve_alloc(1);
          gsl_odeiv_system     sys = {func, NULL, 1, (void *)params};

    double t = 0.1, t_final = 1900.;
    double h = 1e-3;
    double Mass[1];
    Mass[0]=1.;

    unsigned int i = 0;
    double t_old = 0.;
    double M_old[1];
    M_old[0]=1.;

    double misfit=0.;
    //unsigned int loopSize = 0;
    while ((t < t_final) && (i < Me.size())) {
      int status = gsl_odeiv_evolve_apply(e, c, s, &sys, &t, t_final, &h, Mass);
      UQ_FATAL_TEST_MACRO((status != GSL_SUCCESS),
                          paramValues.env().fullRank(),
                          "likelihoodRoutine()",
                          "gsl_odeiv_evolve_apply() failed");
      //printf("t = %6.1lf, mass = %10.4lf\n",t,Mass[0]);
      //loopSize++;

      while ( (i < Me.size()) && (t_old <= Te[i]) && (Te[i] <= t) ) {
        Mt[i] = (Te[i]-t_old)*(Mass[0]-M_old[0])/(t-t_old) + M_old[0];
        misfit += (Me[i]-Mt[i])*(Me[i]-Mt[i]);
        //printf("%i %lf %lf %lf %lf\n",i,Te[i],Me[i],Mt[i],misfit);
        i++;
      }

      t_old=t;
      M_old[0]=Mass[0];
    }
    resultValue += misfit/variance;

    //printf("loopSize = %d\n",loopSize);
    if ((paramValues.env().displayVerbosity() >= 10) && (paramValues.env().fullRank() == 0)) {
      printf("In likelihoodRoutine(), A = %g, E = %g, beta = %.3lf: misfit = %lf, likelihood = %lf.\n",A,E,beta,misfit,resultValue);
    }

    gsl_odeiv_evolve_free (e);
    gsl_odeiv_control_free(c);
    gsl_odeiv_step_free   (s);
  }

  m_env.subComm().Barrier();
  //env.syncPrintDebugMsg("Leaving likelihoodRoutine()",1,env.fullComm());

  return -.5*resultValue;
}
Beispiel #22
0
double
fevol_shrinker_eigenproblem (double bisec_param, int print, char * filename, void * p)
{
 const gsl_odeiv_step_type * T
    = STEPPER;

  gsl_odeiv_step * s
    = gsl_odeiv_step_alloc (T, 4);
  gsl_odeiv_control * c
    = gsl_odeiv_control_y_new (STEPPER_ERROR, 0.0);
  gsl_odeiv_evolve * e
    = gsl_odeiv_evolve_alloc (4);

  double dt=PRINT_DT, t_last=0.;

  gsl_odeiv_system sys = {func_shrinker_eigenproblem, jac_dummy, 4, (void*)&bisec_param};

  double t = T0;
  double h = H0;
  double A = *(double*)p;
  double y[4] = {		      /* expressions derived using
					 ~/SeriesSolve.nb */
    A*t + ((3*A - 4*(-1 + k)*pow(A,3))*pow(2 + k,-1)*pow(t,3))/12. +
    (A*(15 - 40*(-1 + k)*pow(A,2) + 16*pow(A,4)*(1 - 3*k + 2*pow(k,2)))*
     pow(t,5)*pow(8 + 6*k + pow(k,2),-1))/160.,
    A + ((3*A - 4*(-1 + k)*pow(A,3))*pow(2 + k,-1)*pow(t,2))/4. +
    (A*(15 - 40*(-1 + k)*pow(A,2) + 16*pow(A,4)*(1 - 3*k + 2*pow(k,2)))*
     pow(t,4)*pow(8 + 6*k + pow(k,2),-1))/32.,
    t,
    1.
  };

  FILE * file;

  if (print){
    file = fopen(filename, "a");
    fprintf(file, "# A = %.15f\n# lambda = %.15f\n", A, bisec_param );
  }

  while (t < T_MAX)
    {
      int status =
	gsl_odeiv_evolve_apply (e, c, s,
				&sys,
				&t, T_MAX,
				&h, y);

      if (status != GSL_SUCCESS)
	break;
      /* are we still in the strip [0,pi]?  is the lienarized solution
       reasonably boundaed?*/
      if ( 0. > y[0]
	   || y[0] > 3.15
	   || fabs(y[2]) > 10. )
	break;

      if (print /* && t_last+dt < t */)
	{
	  fprintf (file,
		   "%.15E %.15E %.15E %.15E %.15E\n",
		   t, y[0], y[1], y[2], y[3]);
	  t_last+=dt;
	  dt*=PRINT_DT_RATIO;
	}
      /* printf("%.15f\r",t); */
    }

  gsl_odeiv_evolve_free (e);
  gsl_odeiv_control_free (c);
  gsl_odeiv_step_free (s);
  if (print) {
    fprintf( file, "\n\n\n" );
    fclose( file );
  }

  return y[2];
}
Beispiel #23
0
int
main (int argc, char **argv)
{
	struct experiment_params params;
	double dx, dy;
	double speed, angle;
	gsl_vector *solution;
	int c;
	double compute_start, delta;

	while ((c = getopt (argc, argv, "v")) != EOF) {
		switch (c) {
		case 'v':
			vflag = 1;
			break;
		default:
			usage ();
		}
	}


	memset (&params, 0, sizeof params);
	params.observed_hit[0] = 0;
	params.observed_hit[1] = 0;
	params.observed_hit[2] = 1;
	params.observed_bounce[0] = 25;
	params.observed_bounce[1] = 0;
	params.observed_bounce[2] = 0;
	params.observed_secs = 1.359;

	dx = params.observed_bounce[0] - params.observed_hit[0];
	dy = params.observed_bounce[1] - params.observed_hit[1];
	params.observed_dist = hypot (dy, dx);

	params.simulator_dimen = 4;

	params.odesys.function = sim_func;
	params.odesys.dimension = params.simulator_dimen;
	params.odesys.params = &params;

	params.stepper = gsl_odeiv_step_alloc (gsl_odeiv_step_rk8pd,
					       params.simulator_dimen);
	params.controller = gsl_odeiv_control_y_new (1e-6, 0.0);
	params.evolver = gsl_odeiv_evolve_alloc (params.simulator_dimen);
	
	params.minimizer_dimen = 2;
	params.starting_point = gsl_vector_alloc (params.minimizer_dimen);

	params.minimizer_step_sizes = gsl_vector_alloc (params.minimizer_dimen);
	params.minimizer = gsl_multimin_fminimizer_alloc
		(gsl_multimin_fminimizer_nmsimplex2, params.minimizer_dimen);

       gsl_vector_set_all (params.starting_point, 0);
       gsl_vector_set_all (params.minimizer_step_sizes, 1.0);
     
	if (0) {
		graph_error_func (&params);
	}

	compute_start = get_secs ();
	solve_by_simulation (&params);
	delta = get_secs () - compute_start;

	solution = gsl_multimin_fminimizer_x (params.minimizer);

	speed = gsl_vector_get (solution, 0);
	angle = gsl_vector_get (solution, 1);

	printf ("speed = %8.3f angle = %8.3f; compute time %.3fms\n",
		speed * METERS_PER_SEC_TO_MPH,
		RTOD (angle),
		delta * 1000);

	return (0);
}