nest::aeif_cond_exp::~aeif_cond_exp() { // GSL structs may not have been allocated, so we need to protect destruction if ( B_.s_ ) gsl_odeiv_step_free(B_.s_); if ( B_.c_ ) gsl_odeiv_control_free(B_.c_); if ( B_.e_ ) gsl_odeiv_evolve_free(B_.e_); }
nest::hh_psc_alpha::~hh_psc_alpha() { // GSL structs may not have been allocated, so we need to protect destruction if ( B_.s_ ) gsl_odeiv_step_free(B_.s_); if ( B_.c_ ) gsl_odeiv_control_free(B_.c_); if ( B_.e_ ) gsl_odeiv_evolve_free(B_.e_); }
int main(int argc, char** argv) { // Tamanho do sistema a ser resolvido size_t dim = 2; // Definicao dos erros por passo de tempo double abstol = 1.e-6; double reltol = 1.e-10; // Definindo o metodo a ser usado const gsl_odeiv_step_type* T = gsl_odeiv_step_rk8pd; // Alocando espaco para utilizar o metodo T e a dimensao do // numero de equacoes a serem resolvidas. gsl_odeiv_step* s = gsl_odeiv_step_alloc(T, dim); // Verifica e obtem o melhor passo na evolucao da variavel, de // acordo com os erros absoluto e relativo alimentados. gsl_odeiv_control* c = gsl_odeiv_control_y_new(abstol, reltol); // Alocando espaco para aplicar as operacoes de solucao numerica gsl_odeiv_evolve* e = gsl_odeiv_evolve_alloc(dim); // Definicao dos parametros usados e do sistema a ser resolvido // (funcao e seu jacobiano). double mu = 10; gsl_odeiv_system sys = {func, jac, 2, &mu}; // Definicao das variaveis e suas condicoes iniciais double t = 0.0, tf = 100.0; double h = 1e-2; double y[2] = { 1.0, 0.0 }; // C.I. das variaveis // Variaveis auxiliares int status; printf ("%.5e %.5e %.5e\n", t, y[0], y[1]); while (t < tf) { // Evolucao da funcao. // Importante!! t e h sao passados por referencia e seus valores // mudam a cada iteracao. Dessa maneira, o passo de tempo NAO eh // fixo!!! status = gsl_odeiv_evolve_apply(e, c, s, &sys, &t, tf, &h, y); if(status != GSL_SUCCESS) { printf ("error, return value=%d\n", status); break; } printf ("%.5e %.5e %.5e\n", t, y[0], y[1]); } // Desalocando a memoria... gsl_odeiv_evolve_free (e); gsl_odeiv_control_free (c); return (EXIT_SUCCESS); }
void destroy_ode_base(struct_ode_base * s) { free(s->x); gsl_odeiv_evolve_free(s->e); gsl_odeiv_control_free(s->c); gsl_odeiv_step_free(s->s); free(s); }
static void odesys_odegsl_dtor (odegsl_t * odegsl) { gsl_odeiv_evolve_free (odegsl->evolve); gsl_odeiv_control_free (odegsl->control); gsl_odeiv_step_free (odegsl->step); MEMORY_FREE (odegsl); }
static void PyGSL_odeiv_evolve_free(PyGSL_odeiv_evolve * self) { assert(PyGSL_ODEIV_EVOLVE_Check(self)); Py_DECREF(self->step); Py_DECREF(self->control); gsl_odeiv_evolve_free(self->evolve); PyMem_Free(self); }
void integrator_free(LALStatus *status, integrator_System *integrator) { INITSTATUS(status, "runge_kutta_free", INTEGRATORC); ATTATCHSTATUSPTR(status); gsl_odeiv_step_free(integrator->solver_step); gsl_odeiv_evolve_free(integrator->solver_evolve); gsl_odeiv_control_free(integrator->solver_control); DETATCHSTATUSPTR(status); RETURN (status); }
int sys_driver (const gsl_odeiv_step_type * T, const gsl_odeiv_system * sys, double t0, double t1, double hstart, double y[], double epsabs, double epsrel, const char desc[]) { /* This function evolves a system sys with stepper T from t0 to t1. Step length is varied via error control with possibly different absolute and relative error tolerances. */ int s = 0; int steps = 0; double t = t0; double h = hstart; gsl_odeiv_step * step = gsl_odeiv_step_alloc (T, sys->dimension); gsl_odeiv_control *c = gsl_odeiv_control_standard_new (epsabs, epsrel, 1.0, 0.0); gsl_odeiv_evolve *e = gsl_odeiv_evolve_alloc (sys->dimension); while (t < t1) { s = gsl_odeiv_evolve_apply (e, c, step, sys, &t, t1, &h, y); if (s != GSL_SUCCESS) { gsl_test(s, "sys_driver: %s evolve_apply returned %d", gsl_odeiv_step_name (step), s); break; } if (steps > 1e7) { gsl_test(GSL_EMAXITER, "sys_driver: %s evolve_apply reached maxiter at t=%g", gsl_odeiv_step_name (step), t); s = GSL_EMAXITER; break; } steps++; } gsl_test(s, "%s %s [%g,%g], %d steps completed", gsl_odeiv_step_name (step), desc, t0, t1, steps); gsl_odeiv_evolve_free (e); gsl_odeiv_control_free (c); gsl_odeiv_step_free (step); return s; }
nest::ht_neuron::~ht_neuron() { // GSL structs may not be initialized, so we need to protect destruction. if ( B_.e_ ) gsl_odeiv_evolve_free( B_.e_ ); if ( B_.c_ ) gsl_odeiv_control_free( B_.c_ ); if ( B_.s_ ) gsl_odeiv_step_free( B_.s_ ); }
struct_ode_base *new_ode_base( int NX, double *x_init, int (*func_ode_base)(), int (*jacobian)(), void * params) { int i; struct_ode_base *old=NULL; struct_ode_base * s; if (old == NULL) { s = malloc(sizeof (struct_ode_base)); s->NX = NX; s->x = malloc(s->NX * sizeof (double)); for (i = 0; i < s->NX; i++) { s->x[i] = 0; } s->sys.function = NULL; s->sys.jacobian = NULL; } else { s = old; } s->params = params; // relative errors and steps s->eps_rel = 1e-6; s->eps_abs = 1e-6; s->h = 1e-6; s->hmax = 1; if (x_init != NULL) { for (i = 0; i < s->NX; i++) { s->x[i] = x_init[i]; } } i = 0; 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; if (func_ode_base != NULL) { s->sys.function = func_ode_base; } if (jacobian != NULL) { s->sys.jacobian = jacobian; } s->sys.params = s; s->time_second = 0.0; s->nb_step = 0; if (old == NULL) { s->print_values = 0; } s->debug = 0; return s; }
MarkovGslSolver::~MarkovGslSolver() { if ( gslEvolve_ ) gsl_odeiv_evolve_free( gslEvolve_ ); if ( gslControl_ ) gsl_odeiv_control_free( gslControl_ ); if ( gslStep_ ) gsl_odeiv_step_free( gslStep_ ); if ( stateGsl_ ) delete[] stateGsl_; }
/* Many good algorithms don't need the Jacobian, so we'll just pass a null pointer */ void gslode(void * mypars, double max_time, FILE *theory) { /* Create our ODE system */ gsl_odeiv_system sys = {func, NULL, DIM, mypars}; /* Range of time to simulate*/ double t = 0.0, t1 = max_time; /* Initial step size, will be modified as needed by adaptive alogorithm */ double h = 1e-6; /* initial conditions, No */ double y[DIM] = { No, 0.0 }; /* Define method as Embedded Runge-Kutta Prince-Dormand (8,9) method */ const gsl_odeiv_step_type * T = gsl_odeiv_step_rk4; // = gsl_odeiv_step_rk8pd; /* allocate stepper for our method of correct dimension*/ gsl_odeiv_step * s = gsl_odeiv_step_alloc (T, DIM); /* control will maintain absolute and relative error */ gsl_odeiv_control * c = gsl_odeiv_control_y_new (1e-6, 0.0); /* allocate the evolver */ gsl_odeiv_evolve * e = gsl_odeiv_evolve_alloc (DIM); /*dummy to make easy to switch to regular printing */ double ti = t1; int i; /* Uncomment the outer for loop to print * * Npts sample pts at regular intervals */ for (i = 1; i <= NPTS; i++){ ti = i * t1 / NPTS; while (t < ti) { int status = gsl_odeiv_evolve_apply (e, c, s, &sys, &t, t1, &h, y); if (status != GSL_SUCCESS) break; // fprintf(theory,"%.6e %.6e %.6e\n",t,y[0],y[1]); //adaptive mesh } fprintf(theory,"%g %g %g\n",t,y[0],y[1]); } gsl_odeiv_evolve_free (e); gsl_odeiv_control_free (c); gsl_odeiv_step_free (s); }
nest::hh_cond_exp_traub::~hh_cond_exp_traub() { // GSL structs may not have been allocated, so we need to protect destruction if ( B_.s_ ) { gsl_odeiv_step_free( B_.s_ ); } if ( B_.c_ ) { gsl_odeiv_control_free( B_.c_ ); } if ( B_.e_ ) { gsl_odeiv_evolve_free( B_.e_ ); } }
nest::iaf_cond_alpha_mc::~iaf_cond_alpha_mc() { // GSL structs may not have been allocated, so we need to protect destruction if ( B_.s_ ) { gsl_odeiv_step_free( B_.s_ ); } if ( B_.c_ ) { gsl_odeiv_control_free( B_.c_ ); } if ( B_.e_ ) { gsl_odeiv_evolve_free( B_.e_ ); } }
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 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); }
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 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); }
/** * @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; }
void ode_free(odesolver_t* par) { gsl_odeiv_evolve_free(par->e); gsl_odeiv_control_free(par->c); gsl_odeiv_step_free(par->s); }
GslInternal::~GslInternal(){ gsl_odeiv_evolve_free (evolve_ptr); gsl_odeiv_control_free (control_ptr); gsl_odeiv_step_free (step_ptr); }
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; }
// 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; }
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; }
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; }
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]; }
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; }
void pert_ode_free (pert_ode * po) { gsl_odeiv_evolve_free (po->ee); gsl_odeiv_control_free (po->ce); gsl_odeiv_step_free (po->se); free(po); }
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]; }
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; }