void initialize_prior(PARAM *param, PRIOR *prior, DATA *data, const gsl_rng *r) { int i; float mean; double MAXC = ((double) _MAX_COMP_); prior->m_beta = 0.0; prior->v_beta = 100.0; prior->atrue = 0.1; prior->afalse = 1.0 - prior->atrue; prior->rho_alpha_prey = 1.0; prior->m_alpha_prey = 0.0; prior->v_alpha_prey = 100.0; for(i=0;i<_MAX_COMP_;i++) prior->gamma_alpha_prey[i] = 1.0 / MAXC; for(i=0;i<_MAX_COMP_;i++) prior->theta_alpha_prey[i] = gsl_ran_gaussian(r, 2.0); for(i=0;i<data->nprey;i++) prior->w_alpha_prey[i] = ((int) gsl_ran_flat(r,0.0,MAXC)); mean = 0.0; for(i=0;i<_MAX_COMP_;i++) mean += prior->gamma_alpha_prey[i] * prior->theta_alpha_prey[i]; for(i=0;i<_MAX_COMP_;i++) prior->theta_alpha_prey[i] -= mean; prior->rho_alpha_IP = 1.0; prior->m_alpha_IP = 0.0; prior->v_alpha_IP = 100.0; for(i=0;i<_MAX_COMP_;i++) prior->gamma_alpha_IP[i] = 1.0 / MAXC; /* for(i=0;i<_MAX_COMP_;i++) prior->theta_alpha_IP[i] = gsl_ran_gaussian(r, 2.0); */ for(i=0;i<_MAX_COMP_;i++) prior->theta_alpha_IP[i] = 0.0; /* for(i=0;i<data->nIP;i++) prior->w_alpha_IP[i] = ((int) gsl_ran_flat(r,0.0,MAXC)); */ for(i=0;i<data->nIP;i++) prior->w_alpha_IP[i] = 0; mean = 0.0; for(i=0;i<_MAX_COMP_;i++) mean += prior->gamma_alpha_IP[i] * prior->theta_alpha_IP[i]; for(i=0;i<_MAX_COMP_;i++) prior->theta_alpha_IP[i] -= mean; prior->rho_mu = 1.0; prior->m_mu = 0.0; prior->v_mu = 100.0; for(i=0;i<_MAX_COMP_;i++) prior->gamma_mu[i] = 1.0 / MAXC; for(i=0;i<_MAX_COMP_;i++) prior->theta_mu[i] = gsl_ran_gaussian(r, 2.0); for(i=0;i<data->nprey;i++) prior->w_mu[i] = ((int) gsl_ran_flat(r,0.0,MAXC)); mean = 0.0; for(i=0;i<_MAX_COMP_;i++) mean += prior->gamma_mu[i] * prior->theta_mu[i]; for(i=0;i<_MAX_COMP_;i++) prior->theta_mu[i] -= mean; prior->rho_eta = 1.0; prior->mean_eta = 0.1; for(i=0;i<_MAX_COMP_;i++) prior->gamma_eta[i] = 1.0 / MAXC; for(i=0;i<_MAX_COMP_;i++) prior->theta_eta[i] = gsl_ran_exponential(r, prior->mean_eta) + 1.0; for(i=0;i<data->nprey;i++) prior->w_eta[i] = ((int) gsl_ran_flat(r,0.0,MAXC)); prior->rho_eta0 = 1.0; prior->mean_eta0 = 0.1; for(i=0;i<_MAX_COMP_;i++) prior->gamma_eta0[i] = 1.0 / MAXC; for(i=0;i<_MAX_COMP_;i++) prior->theta_eta0[i] = gsl_ran_exponential(r, prior->mean_eta0) + 1.0; for(i=0;i<data->nprey;i++) prior->w_eta0[i] = ((int) gsl_ran_flat(r,0.0,MAXC)); }
Photons AlexSim::AemAex(const double &rate, const double &tEnd) { Photons photonsTemp; t+=gsl_ran_exponential(r, 1/rateFirst); while(t<tEnd) { while((t<tEnd)&&(fmod((t+tDperiod()),tPeriod())<tAperiod())) { p.setFlags(AcceptorEm,AcceptorEx); p.time=t; photonsTemp.append(p); t+=gsl_ran_exponential(r, 1/rate); } t+=tAperiod(); } return photonsTemp; }
double gsl_ran_tdist (const gsl_rng * r, const double nu) { if (nu <= 2) { double Y1 = gsl_ran_ugaussian (r); double Y2 = gsl_ran_chisq (r, nu); double t = Y1 / sqrt (Y2 / nu); return t; } else { double Y1, Y2, Z, t; do { Y1 = gsl_ran_ugaussian (r); Y2 = gsl_ran_exponential (r, 1 / (nu/2 - 1)); Z = Y1 * Y1 / (nu - 2); } while (1 - Z < 0 || exp (-Y2 - Z) > (1 - Z)); /* Note that there is a typo in Knuth's formula, the line below is taken from the original paper of Marsaglia, Mathematics of Computation, 34 (1980), p 234-256 */ t = Y1 / sqrt ((1 - 2 / nu) * (1 - Z)); return t; } }
void DP_eta_theta(PARAM *param, PRIOR *prior, DATA *data, const gsl_rng *r, int pid, int *inuse) { int i, j, id, accept; float Delta, mhratio, newval, scale, tmp_lambda, tmp; scale = prior->gamma_eta[pid] / (1.0 - prior->gamma_eta[pid]); if(inuse[pid] == 0) { newval = gsl_ran_exponential(r, prior->mean_eta) + 1.0; Delta = newval - prior->theta_eta[pid]; prior->theta_eta[pid] = newval; } else { /* metropolis-hastings */ mhratio = 0.0; Delta = gsl_ran_gaussian(r, 1.0); if(prior->theta_eta[pid] + Delta <= 1.0 || prior->theta_eta[pid] + Delta > 100.0) { accept = 0; } else { for(i=0;i<data->nprey;i++) { if(prior->w_eta[i] == pid) { for(j=0;j<data->preyNinter[i];j++) { id = data->p2i[i][j]; if(param->Z[data->a2u[id]] && data->d[id] > 0.0) { tmp_lambda = param->lambda_true[id]; // else tmp_lambda = param->lambda_false[id]; /* if(param->Z[data->a2u[id]]) */ tmp = data->d[id] < exp(param->lambda_false[id]) && param->lambda_false[id] < param->lambda_true[id] ? exp(param->lambda_false[id]) : data->d[id]; if(lowMode) { mhratio += log_poisson_g_prop(GSL_MIN(_LM_,data->d[id]), exp(tmp_lambda), prior->theta_eta[pid]+Delta) - log_poisson_g_prop(GSL_MIN(_LM_,data->d[id]), exp(tmp_lambda), prior->theta_eta[pid]); } else { mhratio += log_poisson_g_prop(data->d[id], exp(tmp_lambda), prior->theta_eta[pid]+Delta) - log_poisson_g_prop(data->d[id], exp(tmp_lambda), prior->theta_eta[pid]); } /* mhratio += log_poisson_g_prop(tmp, exp(tmp_lambda), prior->theta_eta[pid]+Delta) - log_poisson_g_prop(tmp, exp(tmp_lambda), prior->theta_eta[pid]); */ } } } } mhratio += log(gsl_ran_exponential_pdf(prior->theta_eta[pid]+Delta-1.0, prior->mean_eta)) - log(gsl_ran_exponential_pdf(prior->theta_eta[pid]-1.0, prior->mean_eta)); // mhratio += -2.0 * (log(prior->theta_eta[pid]+ Delta) - log(prior->theta_eta[pid])); accept = gsl_ran_flat(r, 0.0, 1.0) <= GSL_MIN(1.0, exp(mhratio)) ? 1 : 0 ; } /* if accepted, update param and lambda */ if(accept) { prior->theta_eta[pid] += Delta; for(i=0;i<data->nprey;i++) { if(prior->w_eta[i] == pid) { param->eta[i] += Delta; } } } } }
Photons AlexSim::Dex(const double &rate, const double &tEnd, double directExc) { Photons photonsTemp; t+=gsl_ran_exponential(r, 1/rateFirst); while(t<tEnd) { while((t<tEnd)&&(fmod(t,tPeriod())<tDperiod())) { if(gsl_ran_flat(r,0,1)>directExc) p.setFlags(DonorEm,DonorEx); else p.setFlags(AcceptorEm,DonorEx); p.time=t; photonsTemp.append(p); t+=gsl_ran_exponential(r, 1/rate); } t+=tDperiod(); } return photonsTemp; }
//Returns the time of the next spike under an exponential distribution unsigned int PoissonSource::getTimestepsUntilNextEvent() { return gsl_ran_exponential(rng_r,mlamdaInTs); // This Does not Work Properly The Variance is not Big enough!! //->Draw a random from Poisson With a mean given as the number of timesteps until the next Event // double mu = (double)(1.0/(mlamda*h)); //Probability of spike event // double u = gsl_rng_uniform_pos (rng_r); //This returns a non-zero probability less that 1 // return -mu * log (u); }
int AlexSim::burstCount(const double tstart, const double tend) { t=tstart; int nPhotonsD=0,nPhotonsA=0; while(t<tend) { photonArrivalTimeDonor.push_back(t); nPhotonsD++; t+=gsl_ran_exponential(r, 1/rateDemDex); } t=tstart; while(t<tend) { photonArrivalTimeAcceptor.push_back(t); nPhotonsA++; t+=gsl_ran_exponential(r, 1/rateAemAex); } return nPhotonsD+nPhotonsA; }
extern double dist_exponential(struct _flow *flow, const double mu) { #ifdef HAVE_LIBGSL gsl_rng * r = flow->r; return gsl_ran_exponential(r, mu); #else return -log(rn_uniform(flow))+mu; #endif /* HAVE_LIBGSL */ }
void librdist_exponential(gsl_rng *rng, int argc, void *argv, int bufc, float *buf){ t_atom *av = (t_atom *)argv; if(argc != librdist_getnargs(ps_exponential)){ return; } const double mu = librdist_atom_getfloat(av); int i; for(i = 0; i < bufc; i++) buf[i] = (float)gsl_ran_exponential(rng, mu); }
double AlexSim::burstCount(QTextStream &out) { double tstart=t; // double burstDurationSample=gsl_ran_lognormal(r, mu, sigma); double burstDurationSample=gsl_ran_exponential(r, burstDuration); double tend = tstart + burstDurationSample; int nPhotons=burstCount(tstart, tend); out<<t*1e3<<"\t"<<burstDurationSample*1e3<<"\t"<<nPhotons<<"\n"; return burstDurationSample; }
QList<double> LayerThicknessVariation::vary(double depthToBedrock) const { // Need to convert the depth to bedrock into meters for the Toro (1997) // model. depthToBedrock *= Units::instance()->toMeters(); // The thickness of the layers QList<double> thicknesses; // The layering is generated using a non-homogenous Poisson process. The // following routine is used to generate the layering. The rate function, // \lambda(t), is integrated from 0 to t to generate cumulative rate // function, \Lambda(t). This function is then inverted producing // \Lambda^-1(t). Random variables are produced using the a exponential // random variation with mu = 1 and converted to the nonhomogenous // variables using the inverted function. // Random variable that is a sum of exponential random variables double sum = 0; // Previously computed depth double prevDepth = 0; while ( prevDepth < depthToBedrock ) { // Add a random increment sum += gsl_ran_exponential(m_rng, 1.0); // Convert between x and depth using the inverse of \Lambda(t) double depth = pow((m_exponent * sum) / m_coeff + sum / m_coeff + pow(m_initial, m_exponent + 1) , 1 / (m_exponent + 1) ) - m_initial; // Add the thickness thicknesses << depth - prevDepth; prevDepth = depth; } // Correct the last layer of thickness so that the total depth is equal to the maximum depth if (thicknesses.size()) thicknesses.last() = thicknesses.last() - (prevDepth - depthToBedrock); // Convert the thicknesses back into the target unit system for (int i = 0; i < thicknesses.size(); ++i) thicknesses[i] /= Units::instance()->toMeters(); return thicknesses; }
int main(int argc, char *argv[]) { const gsl_rng_type * T; gsl_rng * r; int i; int n = atoi(argv[2]); double mu = atof(argv[3]); gsl_rng_env_setup(); T = gsl_rng_default; r = gsl_rng_alloc (T); //select the poisson distribution if (strcmp(argv[1], "-poisson") == 0) { for (i = 0; i < n; i++) { unsigned int k = gsl_ran_poisson(r,mu); printf(" %u",k); } } //select the exponential distribution if (strcmp(argv[1], "-exponential") == 0) { for (i = 0; i < n; i++) { unsigned int k = gsl_ran_exponential(r,mu); printf(" %u",k); } } //select the gaussian distribution if (strcmp(argv[1], "-gaussian") == 0) { for (i = 0; i < n; i++) { unsigned int k = gsl_ran_gaussian(r,mu); printf(" %u",k); } } printf("\n"); gsl_rng_free(r); return 0; }
void AlexSim::burstBlinkingAcceptor() { double tstart=t; double burstDurationSample=gsl_ran_lognormal(r, mu, sigma); double tend = tstart + burstDurationSample; double tBlinkStart=gsl_ran_flat(r, tstart, tend); double tBlinkEnd=tBlinkStart + gsl_ran_exponential(r, lifetimeBlinking); if (tBlinkEnd>tend) tBlinkEnd=tend; qDebug()<<"burst at"<<t*1e3<<"ms for"<<burstDurationSample*1e3<<"ms. Acceptor blinks after"<<(tBlinkStart-tstart)*1e3<<"ms for"<<(tBlinkEnd-tBlinkStart)*1e3<<"ms"; Photons photonsTemp; photonsTemp<<burst(tstart, tBlinkStart); photonsTemp<<donorOnly(tBlinkStart, tBlinkEnd); photonsTemp<<burst(tBlinkEnd, tend); photonsTemp.sort(); photons<<photonsTemp; }
int gslalg_rng_exponential_VM ( Word* args, Word& result, int message, Word& local, Supplier s ) { result = qp->ResultStorage( s ); CcReal *res = (CcReal*) result.addr; CcReal *cMu = (CcReal*) args[0].addr; if( cMu->IsDefined() ) { double Mu = cMu->GetRealval(); double resD = gsl_ran_exponential(the_gsl_randomgenerator.GetGenerator(), Mu); res->Set(true, resD); } else res->Set(false, 0); return (0); }
void test_dirichlet_moments (void) { double alpha[DIRICHLET_K]; double theta[DIRICHLET_K]; double theta_sum[DIRICHLET_K]; double alpha_sum = 0.0; double mean, obs_mean, sd, sigma; int status, k, n; for (k = 0; k < DIRICHLET_K; k++) { alpha[k] = gsl_ran_exponential (r_global, 0.1); alpha_sum += alpha[k]; theta_sum[k] = 0.0; } for (n = 0; n < N; n++) { gsl_ran_dirichlet (r_global, DIRICHLET_K, alpha, theta); for (k = 0; k < DIRICHLET_K; k++) theta_sum[k] += theta[k]; } for (k = 0; k < DIRICHLET_K; k++) { mean = alpha[k] / alpha_sum; sd = sqrt ((alpha[k] * (1. - alpha[k] / alpha_sum)) / (alpha_sum * (alpha_sum + 1.))); obs_mean = theta_sum[k] / N; sigma = sqrt ((double) N) * fabs (mean - obs_mean) / sd; status = (sigma > 3.0); gsl_test (status, "test gsl_ran_dirichlet: mean (%g observed vs %g expected)", obs_mean, mean); } }
void AlexSim::burstBlinkingDonor() // The time a flourophore blinks during a burst is chosen at random (from a uniform distribtion) and its length is drawn from an exponential distribution with mean value of the triplet lifetime. { double tstart=t; double burstDurationSample=gsl_ran_lognormal(r, mu, sigma); double tend = tstart + burstDurationSample; double tBlinkStart=gsl_ran_flat(r, tstart, tend); double tBlinkEnd=tBlinkStart + gsl_ran_exponential(r, lifetimeBlinking); qDebug()<<"burst at"<<t*1e3<<"ms for"<<burstDurationSample*1e3<<"ms. Donor blinks after"<<(tBlinkStart-tstart)*1e3<<"ms for"<<(tBlinkEnd-tBlinkStart)*1e3<<"ms"; burst(tstart, tBlinkStart); acceptorOnly(tBlinkStart, tBlinkEnd); burst(tBlinkEnd, tend); Photons photonsTemp; photonsTemp<<burst(tstart, tBlinkStart); photonsTemp<<acceptorOnly(tBlinkStart, tBlinkEnd); photonsTemp<<burst(tBlinkEnd, tend); photonsTemp.sort(); photons<<photonsTemp; }
/** * Create a exponentially distributed noise value * \param [in] mu Mean value of the distribution * \param [in] ptrToGenerator Pointer to a gsl_rng generator * \return A random value drawn from the exponential distribution */ REAL8 expRandNum(REAL8 mu, gsl_rng *ptrToGenerator) { XLAL_CHECK_REAL8( mu > 0.0 && ptrToGenerator != NULL, XLAL_EINVAL ); return gsl_ran_exponential(ptrToGenerator, mu); } /* expRandNum() */
double GslRandGen::exponential(const double & mu) { return gsl_ran_exponential(r, mu); }
uint32_t get_exp(gsl_rng* gr, double mu, uint32_t a, uint32_t b) { double res = a + gsl_ran_exponential( gr, mu - a ); if ( res > b ) res = gsl_ran_flat( gr, a, b ); return (uint32_t)res; }
double test_exponential (void) { return gsl_ran_exponential (r_global, 2.0); }
int main() { atddtree_key min = 1; atddtree_key max;// = 1000; atddtree_key keys[10] = {2,3,5,8,16,23,26,35,48,50}; int i; atddtree* t; int nuser = 1024*1024-1; //1024*1024*64-1; double mean_ia = 205; gsl_rng *r; const gsl_rng_type *T; int n=5; double u; T=gsl_rng_ranlxs0; //设随机数生成器类型是 ranlxs0 //gen arrival gsl_rng_default_seed = ((unsigned long)(time(NULL))); //设seed值为当前时间 r=gsl_rng_alloc(T); //生成实例 double* exp_sample_ir = MALLOC(nuser, double); double abstemp = 0; for(i=0;i<nuser;i++) { exp_sample_ir[i] = gsl_ran_exponential(r, mean_ia); //exp_sample_ir[i] = 2+(i%10000)*0.3; #ifdef LOGISTIC abstemp = gsl_ran_logistic(r, 1); if(abstemp<0) { abstemp=0-abstemp; } exp_sample_ir[i] = abstemp; #endif //exp_sample_ir[i] = 5*gsl_ran_beta(r, 5, 1); //exp_sample_ir[i] = 5*gsl_ran_lognormal(r, 5, 0.25); //printf("exp: %f\n", exp_sample_ir[i]); } double* arrival_real = MALLOC(nuser, double); arrival_real[0] = 1.0; for(i=1;i<nuser;i++) { arrival_real[i] = arrival_real[i-1]+exp_sample_ir[i-1]; //printf("arrival_real: %f\n", arrival_real[i]); } atddtree_key* arrival = MALLOC(nuser, atddtree_key); for(i=0;i<nuser;i++) { arrival[i] = (atddtree_key)arrival_real[i]; //printf("arrival: %ld\n", arrival[i]); } max = 0; for(i=0;i<nuser;i++) { if(KEYCMP(arrival[i],max)>0) { KEYCPY(max,arrival[i]); } } printf("---max=%ld\n", max); t = atddtree_create(&min, &max); for(i=0;i<nuser;i++) { atddtree_insert(t, arrival+i); //printf("insert %ld, height=%d\n", arrival[i], t->h); } printf("height=%d\n", t->h); }
int main (int argc, char *argv[]) { size_t i,j; size_t n = 0; double mu = 0, nu = 0, nu1 = 0, nu2 = 0, sigma = 0, a = 0, b = 0, c = 0; double zeta = 0, sigmax = 0, sigmay = 0, rho = 0; double p = 0; double x = 0, y =0, z=0 ; unsigned int N = 0, t = 0, n1 = 0, n2 = 0 ; unsigned long int seed = 0 ; const char * name ; gsl_rng * r ; if (argc < 4) { printf ( "Usage: gsl-randist seed n DIST param1 param2 ...\n" "Generates n samples from the distribution DIST with parameters param1,\n" "param2, etc. Valid distributions are,\n" "\n" " beta\n" " binomial\n" " bivariate-gaussian\n" " cauchy\n" " chisq\n" " dir-2d\n" " dir-3d\n" " dir-nd\n" " erlang\n" " exponential\n" " exppow\n" " fdist\n" " flat\n" " gamma\n" " gaussian-tail\n" " gaussian\n" " geometric\n" " gumbel1\n" " gumbel2\n" " hypergeometric\n" " laplace\n" " landau\n" " levy\n" " levy-skew\n" " logarithmic\n" " logistic\n" " lognormal\n" " negative-binomial\n" " pareto\n" " pascal\n" " poisson\n" " rayleigh-tail\n" " rayleigh\n" " tdist\n" " ugaussian-tail\n" " ugaussian\n" " weibull\n") ; exit (0); } argv++ ; seed = atol (argv[0]); argc-- ; argv++ ; n = atol (argv[0]); argc-- ; argv++ ; name = argv[0] ; argc-- ; argc-- ; gsl_rng_env_setup() ; if (gsl_rng_default_seed != 0) { fprintf(stderr, "overriding GSL_RNG_SEED with command line value, seed = %ld\n", seed) ; } gsl_rng_default_seed = seed ; r = gsl_rng_alloc(gsl_rng_default) ; #define NAME(x) !strcmp(name,(x)) #define OUTPUT(x) for (i = 0; i < n; i++) { printf("%g\n", (x)) ; } #define OUTPUT1(a,x) for(i = 0; i < n; i++) { a ; printf("%g\n", x) ; } #define OUTPUT2(a,x,y) for(i = 0; i < n; i++) { a ; printf("%g %g\n", x, y) ; } #define OUTPUT3(a,x,y,z) for(i = 0; i < n; i++) { a ; printf("%g %g %g\n", x, y, z) ; } #define INT_OUTPUT(x) for (i = 0; i < n; i++) { printf("%d\n", (x)) ; } #define ARGS(x,y) if (argc != x) error(y) ; #define DBL_ARG(x) if (argc) { x=atof((++argv)[0]);argc--;} else {error( #x);}; #define INT_ARG(x) if (argc) { x=atoi((++argv)[0]);argc--;} else {error( #x);}; if (NAME("bernoulli")) { ARGS(1, "p = probability of success"); DBL_ARG(p) INT_OUTPUT(gsl_ran_bernoulli (r, p)); } else if (NAME("beta")) { ARGS(2, "a,b = shape parameters"); DBL_ARG(a) DBL_ARG(b) OUTPUT(gsl_ran_beta (r, a, b)); } else if (NAME("binomial")) { ARGS(2, "p = probability, N = number of trials"); DBL_ARG(p) INT_ARG(N) INT_OUTPUT(gsl_ran_binomial (r, p, N)); } else if (NAME("cauchy")) { ARGS(1, "a = scale parameter"); DBL_ARG(a) OUTPUT(gsl_ran_cauchy (r, a)); } else if (NAME("chisq")) { ARGS(1, "nu = degrees of freedom"); DBL_ARG(nu) OUTPUT(gsl_ran_chisq (r, nu)); } else if (NAME("erlang")) { ARGS(2, "a = scale parameter, b = order"); DBL_ARG(a) DBL_ARG(b) OUTPUT(gsl_ran_erlang (r, a, b)); } else if (NAME("exponential")) { ARGS(1, "mu = mean value"); DBL_ARG(mu) ; OUTPUT(gsl_ran_exponential (r, mu)); } else if (NAME("exppow")) { ARGS(2, "a = scale parameter, b = power (1=exponential, 2=gaussian)"); DBL_ARG(a) ; DBL_ARG(b) ; OUTPUT(gsl_ran_exppow (r, a, b)); } else if (NAME("fdist")) { ARGS(2, "nu1, nu2 = degrees of freedom parameters"); DBL_ARG(nu1) ; DBL_ARG(nu2) ; OUTPUT(gsl_ran_fdist (r, nu1, nu2)); } else if (NAME("flat")) { ARGS(2, "a = lower limit, b = upper limit"); DBL_ARG(a) ; DBL_ARG(b) ; OUTPUT(gsl_ran_flat (r, a, b)); } else if (NAME("gamma")) { ARGS(2, "a = order, b = scale"); DBL_ARG(a) ; DBL_ARG(b) ; OUTPUT(gsl_ran_gamma (r, a, b)); } else if (NAME("gaussian")) { ARGS(1, "sigma = standard deviation"); DBL_ARG(sigma) ; OUTPUT(gsl_ran_gaussian (r, sigma)); } else if (NAME("gaussian-tail")) { ARGS(2, "a = lower limit, sigma = standard deviation"); DBL_ARG(a) ; DBL_ARG(sigma) ; OUTPUT(gsl_ran_gaussian_tail (r, a, sigma)); } else if (NAME("ugaussian")) { ARGS(0, "unit gaussian, no parameters required"); OUTPUT(gsl_ran_ugaussian (r)); } else if (NAME("ugaussian-tail")) { ARGS(1, "a = lower limit"); DBL_ARG(a) ; OUTPUT(gsl_ran_ugaussian_tail (r, a)); } else if (NAME("bivariate-gaussian")) { ARGS(3, "sigmax = x std.dev., sigmay = y std.dev., rho = correlation"); DBL_ARG(sigmax) ; DBL_ARG(sigmay) ; DBL_ARG(rho) ; OUTPUT2(gsl_ran_bivariate_gaussian (r, sigmax, sigmay, rho, &x, &y), x, y); } else if (NAME("dir-2d")) { OUTPUT2(gsl_ran_dir_2d (r, &x, &y), x, y); } else if (NAME("dir-3d")) { OUTPUT3(gsl_ran_dir_3d (r, &x, &y, &z), x, y, z); } else if (NAME("dir-nd")) { double *xarr; ARGS(1, "n1 = number of dimensions of hypersphere"); INT_ARG(n1) ; xarr = (double *)malloc(n1*sizeof(double)); for(i = 0; i < n; i++) { gsl_ran_dir_nd (r, n1, xarr) ; for (j = 0; j < n1; j++) { if (j) putchar(' '); printf("%g", xarr[j]) ; } putchar('\n'); } ; free(xarr); } else if (NAME("geometric")) { ARGS(1, "p = bernoulli trial probability of success"); DBL_ARG(p) ; INT_OUTPUT(gsl_ran_geometric (r, p)); } else if (NAME("gumbel1")) { ARGS(2, "a = order, b = scale parameter"); DBL_ARG(a) ; DBL_ARG(b) ; OUTPUT(gsl_ran_gumbel1 (r, a, b)); } else if (NAME("gumbel2")) { ARGS(2, "a = order, b = scale parameter"); DBL_ARG(a) ; DBL_ARG(b) ; OUTPUT(gsl_ran_gumbel2 (r, a, b)); } else if (NAME("hypergeometric")) { ARGS(3, "n1 = tagged population, n2 = untagged population, t = number of trials"); INT_ARG(n1) ; INT_ARG(n2) ; INT_ARG(t) ; INT_OUTPUT(gsl_ran_hypergeometric (r, n1, n2, t)); } else if (NAME("laplace")) { ARGS(1, "a = scale parameter"); DBL_ARG(a) ; OUTPUT(gsl_ran_laplace (r, a)); } else if (NAME("landau")) { ARGS(0, "no arguments required"); OUTPUT(gsl_ran_landau (r)); } else if (NAME("levy")) { ARGS(2, "c = scale, a = power (1=cauchy, 2=gaussian)"); DBL_ARG(c) ; DBL_ARG(a) ; OUTPUT(gsl_ran_levy (r, c, a)); } else if (NAME("levy-skew")) { ARGS(3, "c = scale, a = power (1=cauchy, 2=gaussian), b = skew"); DBL_ARG(c) ; DBL_ARG(a) ; DBL_ARG(b) ; OUTPUT(gsl_ran_levy_skew (r, c, a, b)); } else if (NAME("logarithmic")) { ARGS(1, "p = probability"); DBL_ARG(p) ; INT_OUTPUT(gsl_ran_logarithmic (r, p)); } else if (NAME("logistic")) { ARGS(1, "a = scale parameter"); DBL_ARG(a) ; OUTPUT(gsl_ran_logistic (r, a)); } else if (NAME("lognormal")) { ARGS(2, "zeta = location parameter, sigma = scale parameter"); DBL_ARG(zeta) ; DBL_ARG(sigma) ; OUTPUT(gsl_ran_lognormal (r, zeta, sigma)); } else if (NAME("negative-binomial")) { ARGS(2, "p = probability, a = order"); DBL_ARG(p) ; DBL_ARG(a) ; INT_OUTPUT(gsl_ran_negative_binomial (r, p, a)); } else if (NAME("pareto")) { ARGS(2, "a = power, b = scale parameter"); DBL_ARG(a) ; DBL_ARG(b) ; OUTPUT(gsl_ran_pareto (r, a, b)); } else if (NAME("pascal")) { ARGS(2, "p = probability, n = order (integer)"); DBL_ARG(p) ; INT_ARG(N) ; INT_OUTPUT(gsl_ran_pascal (r, p, N)); } else if (NAME("poisson")) { ARGS(1, "mu = scale parameter"); DBL_ARG(mu) ; INT_OUTPUT(gsl_ran_poisson (r, mu)); } else if (NAME("rayleigh")) { ARGS(1, "sigma = scale parameter"); DBL_ARG(sigma) ; OUTPUT(gsl_ran_rayleigh (r, sigma)); } else if (NAME("rayleigh-tail")) { ARGS(2, "a = lower limit, sigma = scale parameter"); DBL_ARG(a) ; DBL_ARG(sigma) ; OUTPUT(gsl_ran_rayleigh_tail (r, a, sigma)); } else if (NAME("tdist")) { ARGS(1, "nu = degrees of freedom"); DBL_ARG(nu) ; OUTPUT(gsl_ran_tdist (r, nu)); } else if (NAME("weibull")) { ARGS(2, "a = scale parameter, b = exponent"); DBL_ARG(a) ; DBL_ARG(b) ; OUTPUT(gsl_ran_weibull (r, a, b)); } else { fprintf(stderr,"Error: unrecognized distribution: %s\n", name) ; } return 0 ; }
void simulation(float mu, float koff){ /*set up variables for RNG, generate seed that's time-dependent, * seed RNG with that value - guarantees that MPI instances will * actually be different from each other*/ const gsl_rng_type * T; gsl_rng_env_setup(); T = gsl_rng_default; gsl_rng *r; r = gsl_rng_alloc (T); unsigned long int seed; seed=gen_seed(); gsl_rng_set(r,seed); /*old legacy stuff //memset(lanes,0,NLANES*L*sizeof(int)); //lanes[8][48]=22; //FILE *fp;*/ /*define total time of simulation (in units of polymerizing events)*/ int totaltime=500; /*define number of ends in the system, necessary for depol * calculation and rates. Also, define a toggle for the first pol * event to reset*/ int ends=0; int newends=0; int first=1; /*just declare a bunch of stuff that we will use*/ float domega,kon; int accepted=0; int filaments,noconsts; int int_i; int j,k,size,currfils,currlen,gap,l,m; float i,p,coverage; int type=0; /*declare two matrices for the filaments per lane, one for the * "permanent" stuff and one for temporary testing in the * Metropolis scheme*/ int lanes[NLANES][L]={{0}}; int newlanes[NLANES][L]={{0}}; /*two floats to store the time of the next polymerization and depol * events, that will be initialized later*/ float nexton=0; float nextoff=VERYBIG; /*declare three GMP arbitrary precision values: one for storing the * current number of microstates, one for the new number when testing * in the Metropolis scheme and one to store the difference*/ mpz_t states; mpz_t newstates; mpz_t deltastates; mpz_init(states); mpz_init(newstates); mpz_init(deltastates); /*sets up: the value of the chemical potential of a monomer, the * on-rate (we're always taking it to be one), the off-rate * (defined in relation to the on-rate) and the average size of * a new polymer*/ kon=1; //float kon=1.0; //float koff=0.1; //try based on ends //printf("%f %f\n",kon,koff); int avg_size=3; p=1.0/avg_size; float inv_sites=1.0/(NLANES*(L-1)); clock_t t1 = clock(); /*sets up the I/O to a file - filename is dependent on MPI * instance number to avoid conflicts*/ char* filename[40]; //int my_rank=0; int my_rank; MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); sprintf(filename,"outputs_%1.1f_%1.1f/run%d.txt",mu,koff,my_rank); FILE *fp = fopen(filename, "w"); //strcat(filename,threadnum); //strcat(filename,".txt"); /*sets up the time for the first on event (first off only calculated * after we have filaments!)*/ nexton=gsl_ran_exponential(r,1.0/kon); //nextoff=gsl_ran_exponential(r,1.0/koff); //printf("nexton=%f nexoff=%f, avg on=%f avg off=%f thread=%d\n",nexton,nextoff,1.0/kon,1.0/koff,omp_get_thread_num()); /*start of the main loop*/ for (i=0;i<totaltime;i=i+0.01){ clock_t t2 = clock(); //printf("thread=%d i=%f\n",omp_get_thread_num(),i); /*before anything else, make a copy of the current lanes into * the matrix newlanes*/ memcpy(newlanes,lanes,sizeof(lanes)); newends=ends; /*calculate the current coverage (sites occupied divided by the * total number of sites) and output to file the current "time" * and coverage*/ coverage=0; for (k=0;k<NLANES;k++){ for (j=1;j<lanes[k][0]+1;j++){ coverage=coverage+lanes[k][j]; } } //printf("total length=%f\n",coverage); coverage=coverage*inv_sites; //printf("coverage=%f\n",coverage); fprintf(fp, "%f %f %f\n", i,coverage,ends,(double)(t2-t1)/CLOCKS_PER_SEC); //printf("%f %f\n",i,coverage); //printf("i=%f nexton=%f nextoff=%f type=%d\n",i,nexton,nextoff,type); /*tests for events, with priority for polymerizing - if both * are due, it will polymerize first and only depol in the next * timestep*/ if (i>=nextoff){ type=2;} if (i>=nexton){ type=1; } /*we will now calculate the number of filaments and constraints * in the system, going over each lane - relatively * straightforward!*/ filaments=0; noconsts=0; for (k=0;k<NLANES;k++){ //printf("according to main, %d filaments in lane %d\n",newlanes[k][0],k); filaments=filaments+newlanes[k][0]; noconsts=noconsts+2*newlanes[k][0]; if (newlanes[k][0]>1){ noconsts=noconsts+newlanes[k][0]; } } /*polymerization event!*/ if (type==1){ /*big legacy stuff - leave it alone!*/ //printf("lanes at beginning: filaments=%d noconsts=%d\n",filaments,noconsts); //for (k=0;k<NLANES;k++){ //printf("lane %d: ",k); //for (j=1;j<lanes[k][0]+1;j++){ //printf("%d ",lanes[k][j]); //} //printf("\n"); //} //if (i>0) count(states,argc, argv, lanes, filaments, noconsts); //gmp_printf("%Zd\n",states); /*sample exponential to get the size of the filament to be * added (integer exponential is geometric!)*/ size=gsl_ran_geometric(r,p); /*pick a lane at random to add the new filament*/ /*parentheses: in the system without stickers, as long as * this step of random sampling doesn't change, we're fine - * order inside lane doesn't really matter!*/ j=gsl_rng_uniform_int(r,NLANES); /*calculate the current number of filaments and occupied * length in the chosen lane*/ currfils=lanes[j][0]; currlen=0; for (k=1;k<currfils+1;k++){ currlen=currlen+lanes[j][k]; } //printf("currlen %d currfils %d\n",currlen,currfils); /*test if there's enough space to insert the new filament*/ if (currlen+currfils+size+2>L) continue; /*if there is, pick a "gap" to insert the filament in * (i.e. the place in the order of filaments of that lane)*/ else{ gap=1+gsl_rng_uniform_int(r,currfils+1); /*if it's not at the end of the order, need to move the other * ones in the lanes matrix to open space for the new one*/ if (gap!=currfils+1){ for (k=currfils;k>gap-1;k--){ newlanes[j][k+1]=newlanes[j][k]; } } /*insert new filament...*/ newlanes[j][gap]=size; /*...and update the number of filaments in the lane*/ newlanes[j][0]++; } /*calculate number of filaments and constraints for the * newlanes matrix - seems like a waste of a for loop, eh?*/ filaments=0; noconsts=0; for (k=0;k<NLANES;k++){ //printf("according to lanes, %d filaments in lane %d\n",newlanes[k][0],k); filaments=filaments+newlanes[k][0]; noconsts=noconsts+2*newlanes[k][0]; if (newlanes[k][0]>1){ noconsts=noconsts+newlanes[k][0]; } } //printf("lanes after adding: filaments=%d noconsts=%d\n",filaments,noconsts); //printf("proposed change:\n"); //for (k=0;k<NLANES;k++){ //printf("lane %d: ",k); //for (j=1;j<newlanes[k][0]+1;j++){ //printf("%d ",newlanes[k][j]); //} //printf("\n"); //} /*count microstates after addition!*/ count_new(&newstates, newlanes, filaments, noconsts); //fprintf(fp,"count="); //value_print(fp, P_VALUE_FMT, newstates); //fprintf(fp,"\n"); //gmp_printf("states=%Zd\n",states); //gmp_printf("newstates=%Zd\n",newstates); /*calculate delta omega for the addition - chemical potential * plus entropy difference - this should always work since we * set states to 0 in the very first iteration*/ domega=-mu*size-mpzln(newstates)+mpzln(states); //printf("delta omega: %f log10newstates=%f log10states=%f\n",domega, mpzln(newstates),mpzln(states)); /*metropolis test - if delta omega is negative, we accept * the addition*/ if (domega<0){ memcpy(lanes,newlanes,sizeof(lanes)); mpz_set(states,newstates); if (size==1){ ends=ends+1; }else if (size>1){ ends=ends+2; } if (first==1){ first=0; nextoff=gsl_ran_exponential(r,1.0/(koff*ends)); }else{ nextoff=i+gsl_ran_exponential(r,1.0/(koff*ends)); } } /*if it's positive, we calculate the probability of acceptance * and test for it*/ else{ double prob=exp(-1.0*domega); double fromthehat=gsl_rng_uniform(r); //printf("metropolising: rng=%f, prob=%f\n",fromthehat,prob); if (fromthehat<prob){ memcpy(lanes,newlanes,sizeof(lanes)); mpz_set(states,newstates); if (size==1){ ends=ends+1; }else if (size>1){ ends=ends+2; } if (first==1){ first=0; nextoff=gsl_ran_exponential(r,1.0/(koff*ends)); }else{ nextoff=i+gsl_ran_exponential(r,1.0/(koff*ends)); } //printf("accepted anyway!\n"); } } //printf("final result:\n"); //for (k=0;k<NLANES;k++){ //printf("lane %d: ",k); //for (j=1;j<lanes[k][0]+1;j++){ //printf("%d ",lanes[k][j]); //} //printf("\n"); //} /*calculate time of next polymerization event and reset type*/ nexton=nexton+gsl_ran_exponential(r,1.0/kon); //printf("nexton: %f\n",nexton); type=0; } /*Depol event! We need to check for number of filaments here * (note that "filaments" here is the one calculated at the * beginning of the for loop) because if there are no filaments * we just need to recalculate nextoff and keep going*/ if (type==2 && filaments>0){ //printf("entered depol\n"); //printf("filaments: %d\n",filaments); /*this is one of those conditions that shouldn't be necessary, * but I needed to add this at some point and now I'm afraid * to take it out and break the whole thing, so there you go*/ if (i>0 && filaments>0 ) { //count(states,argc, argv, lanes, filaments, noconsts); //gmp_printf("%Zd\n",states); /*pick a filament to decrease size - this will be changed * soon!*/ //j=gsl_rng_uniform_int(r,filaments)+1; //printf("j=%d\n",j); j=gsl_rng_uniform_int(r,ends)+1; //fprintf(fp,"j=%d out of %d\n",j,ends); /*need to replicate the routine below, but with more * intelligence to track ends - maybe create small * test program to try it in isolation?*/ /*go through lanes until you find the filament to be * decreased in size. Then, decrease size by one, check * whether the filament disappeared (in which case following * filaments need to be shifted left and number of filaments * in lane decreased). Finally, break from the for loop*/ int end_counter=0; int doneit=0; for (k=0;k<NLANES;k++){ //fprintf(fp,"k=%d nd_counter=%d, lane has %d filaments\n",k,end_counter,lanes[k][0]); for (l=1;l<lanes[k][0]+1;l++){ if (lanes[k][l]==1){ end_counter=end_counter+1; }else{ end_counter=end_counter+2; } if (j<=end_counter){ //fprintf(fp,"filaments=%d k=%d end_counter=%d thing to change=%d\n",filaments,k,end_counter,newlanes[k][l]); newlanes[k][l]=newlanes[k][l]-1; if (newlanes[k][l]==1){ newends=newends-1; } if (newlanes[k][l]==0){ for (m=l;m<newlanes[k][0];m++){ newlanes[k][m]=newlanes[k][m+1]; } newlanes[k][0]--; newends=newends-1; } doneit=1; break; } } if (doneit==1){ break; } } } //printf("nextoff=%f\n",nextoff); type=0; filaments=0; noconsts=0; for (k=0;k<NLANES;k++){ //printf("according to lanes, %d filaments in lane %d\n",newlanes[k][0],k); filaments=filaments+newlanes[k][0]; noconsts=noconsts+2*newlanes[k][0]; if (newlanes[k][0]>1){ noconsts=noconsts+newlanes[k][0]; } } //printf("rank %d is dumping previous lanes:\n",my_rank); //dump_lanes(lanes); //printf("rank %d is dumping its %d filaments and noconsts %d\n",my_rank,filaments,noconsts); //dump_lanes(newlanes); /*metropolis test should go in here*/ count_new(&newstates, newlanes, filaments, noconsts); //<-segfault is here! //fprintf(fp,"count="); //value_print(fp, P_VALUE_FMT, newstates); //fprintf(fp,"\n"); //gmp_printf("states=%Zd\n",states); //gmp_printf("newstates=%Zd\n",newstates); /*calculate delta omega for the addition - chemical potential * plus entropy difference - this should always work since we * set states to 0 in the very first iteration*/ domega=mu-mpzln(newstates)+mpzln(states); //fprintf(fp,"depol domega=%f, diff in mpzln=%f\n",domega,domega-1); if (domega<0){ memcpy(lanes,newlanes,sizeof(lanes)); mpz_set(states,newstates); ends=newends; } /*if it's positive, we calculate the probability of acceptance * and test for it*/ else{ double prob=exp(-1.0*domega); double fromthehat=gsl_rng_uniform(r); //printf("metropolising: rng=%f, prob=%f\n",fromthehat,prob); if (fromthehat<prob){ memcpy(lanes,newlanes,sizeof(lanes)); mpz_set(states,newstates); } } if (ends>0){ /*recalculate nextoff and reset type*/ nextoff=nextoff+gsl_ran_exponential(r,1.0/(koff*ends)); }else{ nextoff=VERYBIG; //first=1; } /*recalculate the number of filaments and constraints (could * be done more efficiently, but whatever)*/ filaments=0; noconsts=0; for (k=0;k<NLANES;k++){ //printf("according to lanes, %d filaments in lane %d\n",newlanes[k][0],k); filaments=filaments+lanes[k][0]; noconsts=noconsts+2*lanes[k][0]; if (lanes[k][0]>1){ noconsts=noconsts+lanes[k][0]; } //printf("before counting: filaments=%d noconsts=%d\n",filaments,noconsts); } /*recalculate total number of states - this will probably be * part of the Metropolis test further up when this is done*/ count_new(&states, lanes, filaments, noconsts); //fprintf(fp,"count="); //value_print(fp, P_VALUE_FMT, states); //fprintf(fp,"\n"); } /*in case there's nothing to depolimerize, we set nextoff as a big * float and reset the first flag*/ if (type==2 && filaments==0){ nextoff=VERYBIG; //first=1; type=0; } /*also, in any situation where there's no filament in the system, * we set states to zero just to be on the safe side*/ if (filaments==0){ mpz_set_si(states,0);} //else{ ////printf("pass/ng to count: %d %d \n",filaments, noconsts); //count_new(&states,lanes, filaments, noconsts);} ////for (k=0;k<NLANES;k++){ //////printf("lane %d: ",k); ////for (j=1;j<lanes[k][0]+1;j++){ //////printf("%d ",lanes[k][j]); ////} ////printf("\n"); //} //printf("%d %d\n",size,j); //printf("%d %f\n",omp_get_thread_num(),coverage); } /*clearing up - deallocating the arbitrary precision stuff, closing * the I/O file and returning!*/ mpz_clear(states); mpz_clear(newstates); mpz_clear(deltastates); gsl_rng_free (r); fclose(fp); return coverage; }
int GSLRNG_exponential(stEval *args, stEval *result, void *i) { gsl_rng *r = STPOINTER(&args[0]); double mu = STDOUBLE(&args[1]); STDOUBLE(result) = gsl_ran_exponential(r,mu); return EC_OK; }
void simulation_single(){ const int num_lanes=2; int lane_size=100; const gsl_rng_type * T; gsl_rng_env_setup(); T = gsl_rng_default; gsl_rng *r; r = gsl_rng_alloc (T); unsigned long int seed; seed=gen_seed(); gsl_rng_set(r,seed); int fil_length=150; int stickers=1; //glue-related! //int stickers=0; mpz_t states; mpz_t newstates; mpz_t deltastates; mpz_init(states); mpz_init(newstates); mpz_init(deltastates); const float timesteps=5000; int my_rank; MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); char* filename[60]; sprintf(filename,"./output_single_filament_compact/run%d.txt",my_rank); // sprintf(filename,"./output_single_filament_glue_%.2f/run%d.txt",kstick,my_rank); printf("%s\n",filename); FILE *fp = fopen(filename, "w"); if (fp == NULL) { printf("Error"); return; } float mu=6.0; float glue=1.0; float koff=1.0; float i; int j,k; float kstick=0.01; float kunstick=0.01; float stick_energy=3.0; int type=0; float nextoff=gsl_ran_exponential(r,1.0/koff); float nextstick=gsl_ran_exponential(r,1.0/((fil_length-lane_size-stickers)*kstick)); float nextunstick=gsl_ran_exponential(r,1.0/(stickers*kunstick)); //glue-related! // float nextstick=VERYBIG; // float nextunstick=VERYBIG; float peroverlap=kstick; double domega; float c0=1.0/lane_size; int att_depol=0; int acc_depol=0; int att_stick=0; int acc_stick=0; int att_unstick=0; int acc_unstick=0; int att_contr=0; int acc_contr=0; int att_expan=0; int acc_expan=0; int att_special=0; int acc_special=0; float eta=0.2; float fraction=0.0; float delta=0.0; clock_t t1 = clock(); //printf("before first count: %f fil_length: %d\n",kstick,fil_length); count_isl_single(&states,fil_length,stickers,lane_size); gmp_printf("states= %Zd\n",states); int currpar=0; for (i=0;i<timesteps;i=i+0.01){ fraction=fraction+0.01*delta; //printf("%f %f %d\n",fraction,delta,currpar); // if ((int)(fraction*20)%2!=currpar){ // fprintf(fp, "fraction=%f delta=%f\n",fraction,delta); // // } // currpar=(int)(fraction*20)%2; if (fabs(fraction)>1.0){ //fprintf(fp,"event occurring - i=%f delta=%f fraction=%f\n",i,delta,fraction); if (fraction>0){ delta=check_contraction(fil_length,stickers,lane_size+1,c0,states,r,fp); if (delta>VERYBIG/10){ delta=0; fraction=0; //fprintf(fp,"delta too big! delta is now %f\n",delta); }else{ lane_size++; } } else{ delta=check_contraction(fil_length,stickers,lane_size-1,c0,states,r,fp); if (delta>VERYBIG/10){ delta=0; fraction=0; //fprintf(fp,"delta too big! delta is now %f\n",delta); }else{ lane_size--; } } fraction=0; nextstick=i+gsl_ran_exponential(r,1.0/((fil_length-lane_size-stickers)*kstick)); } if (stickers==0){ nextunstick=VERYBIG; } if (fil_length-lane_size-stickers<=0){ nextstick=VERYBIG; } //fprintf(fp, "fraction=%f delta=%f\n",fraction,delta); //printf("%f %f %f %f %d %d %d %d %d %d %d %d %d %d %d %d %d\n", i,nextoff,nextstick,nextunstick,fil_length,stickers,lane_size,att_depol,acc_depol,att_stick,acc_stick,att_unstick,acc_unstick,att_contr,acc_contr,att_expan,acc_expan); if (fil_length==0){ break; } if ((int)(i*100)%(int)timesteps==0){ clock_t t2 = clock(); printf("%d percent %f\n",(int)(i*100)/(int)timesteps,(double)(t2-t1)/CLOCKS_PER_SEC); } //fprintf(fp,"i=%f\n",i); if ((int)(i*100)%50==0){ fprintf(fp, "%f %f %f %f %d %d %d %d %d %d %d %d %d %d %d %f %f %f %f\n", i,nextoff,nextstick,nextunstick,fil_length,stickers,lane_size,att_depol,acc_depol,att_stick,acc_stick,att_unstick,acc_unstick,att_special,acc_special,((fil_length-lane_size-stickers)*kstick),(stickers*kunstick),delta,fraction); //gmp_fprintf(fp,"states= %Zd\n",states); } if (i>=nextoff){ type=1;} if (i>=nextstick){ type=2; } if (i>=nextunstick){ type=3; } if (type==1){ int done=0; att_depol=att_depol+1; int newlength=fil_length-1; // clock_t t3 = clock(); count_isl_single(&newstates,newlength,stickers,lane_size); // clock_t t4 = clock(); // printf("depol time: %f\n",(double)(t4-t3)/CLOCKS_PER_SEC); if (mpz_cmp_d(newstates,0.0)){ domega=-mu+glue-mpzln(newstates)+mpzln(states); //glue-related! // domega=-mu+glue+peroverlap-mpzln(newstates)+mpzln(states); if (domega<0){ acc_depol++; fil_length=newlength; mpz_set(states,newstates); done=1; // fprintf(fp,"depol - domega=%f\n",domega); } /*if it's positive, we calculate the probability of acceptance * and test for it*/ else{ double prob=exp(-1.0*domega); double fromthehat=gsl_rng_uniform(r); //printf("metropolising: rng=%f, prob=%f\n",fromthehat,prob); if (fromthehat<prob){ fil_length=newlength; acc_depol++; mpz_set(states,newstates); done=1; // fprintf(fp,"depol - domega=%f\n",domega); }else{ // fprintf(fp,"removal not accepted - domega=%f\n",domega); } } } if (done==0){ att_special++; // clock_t t3 = clock(); count_isl_single(&newstates,newlength,stickers-1,lane_size); // clock_t t4 = clock(); // printf("special depol time: %f\n",(double)(t4-t3)/CLOCKS_PER_SEC); domega=stick_energy-mu+glue-mpzln(newstates)+mpzln(states); if (domega<0){ fil_length=newlength; stickers=stickers-1; acc_depol++; acc_special++; mpz_set(states,newstates); //fprintf(fp,"depol and removal - domega=%f\n",domega); } else{ double prob=exp(-1.0*domega); double fromthehat=gsl_rng_uniform(r); //printf("metropolising: rng=%f, prob=%f\n",fromthehat,prob); if (fromthehat<prob){ fil_length=newlength; stickers=stickers-1; acc_depol++; acc_special++; mpz_set(states,newstates); //fprintf(fp,"depol and removal - domega=%f\n",domega); }else{ // fprintf(fp,"depol w/unstick not accepted - domega=%f\n",domega); } } } att_contr++; att_expan++; delta=check_contraction(fil_length,stickers,lane_size,c0,states,r,fp); delta=delta/eta; // if (delta==-1){ // acc_contr++; // } // if (delta==+1){ // acc_expan++; // } // lane_size=lane_size+delta; nextoff=i+gsl_ran_exponential(r,1.0/koff); nextstick=i+gsl_ran_exponential(r,1.0/((fil_length-lane_size-stickers)*kstick)); type=0; } if (type==2){ att_stick++; int newstickers=stickers+1; // clock_t t3 = clock(); count_isl_single(&newstates,fil_length,newstickers,lane_size); // clock_t t4 = clock(); // printf("new sticker time: %f\n",(double)(t4-t3)/CLOCKS_PER_SEC); domega=-stick_energy-mpzln(newstates)+mpzln(states); if (domega<0){ stickers=newstickers; acc_stick++; mpz_set(states,newstates); // fprintf(fp,"added sticker - domega=%f\n",domega); } /*if it's positive, we calculate the probability of acceptance * and test for it*/ else{ double prob=exp(-1.0*domega); double fromthehat=gsl_rng_uniform(r); //printf("metropolising: rng=%f, prob=%f\n",fromthehat,prob); if (fromthehat<prob){ stickers=newstickers; acc_stick++; mpz_set(states,newstates); // fprintf(fp,"added sticker - domega=%f\n",domega); }else{ // fprintf(fp,"addition sticker rejected - domega=%f\n",domega); } } att_contr++; att_expan++; delta=check_contraction(fil_length,stickers,lane_size,c0,states,r,fp); delta=delta/eta; // if (delta==-1){ // acc_contr++; // } // if (delta==+1){ // acc_expan++; // } // lane_size=lane_size+delta; nextstick=i+gsl_ran_exponential(r,1.0/((fil_length-lane_size-stickers)*kstick)); nextunstick=i+gsl_ran_exponential(r,1.0/(stickers*kunstick)); type=0; } if (type==3){ att_unstick++; int newstickers=stickers-1; // clock_t t3 = clock(); count_isl_single(&newstates,fil_length,newstickers,lane_size); // clock_t t4 = clock(); // printf("remove sticker time: %f\n",(double)(t4-t3)/CLOCKS_PER_SEC); domega=stick_energy-mpzln(newstates)+mpzln(states); if (domega<0){ acc_unstick++; stickers=newstickers; mpz_set(states,newstates); //fprintf(fp,"removal - domega=%f\n",domega); } /*if it's positive, we calculate the probability of acceptance * and test for it*/ else{ double prob=exp(-1.0*domega); double fromthehat=gsl_rng_uniform(r); //printf("metropolising: rng=%f, prob=%f\n",fromthehat,prob); if (fromthehat<prob){ stickers=newstickers; acc_unstick++; mpz_set(states,newstates); //fprintf(fp,"removal - domega=%f\n",domega); }else{ // fprintf(fp,"removal of sticker not accepted - domega=%f\n",domega); } } att_contr++; att_expan++; delta=check_contraction(fil_length,stickers,lane_size,c0,states,r,fp); delta=delta/eta; // if (delta==-1){ // acc_contr++; // } // if (delta==+1){ // acc_expan++; // } // lane_size=lane_size+delta; nextunstick=i+gsl_ran_exponential(r,1.0/(stickers*kunstick)); nextstick=i+gsl_ran_exponential(r,1.0/((fil_length-lane_size-stickers)*kstick)); type=0; } } mpz_clear(states); mpz_clear(newstates); mpz_clear(deltastates); gsl_rng_free (r); fclose(fp); }
void simulation_single(float kstick){ const int num_lanes=2; int lane_size=1000; const gsl_rng_type * T; gsl_rng_env_setup(); T = gsl_rng_default; gsl_rng *r; r = gsl_rng_alloc (T); unsigned long int seed; seed=gen_seed(); gsl_rng_set(r,seed); int fil_length=1500; int stickers=1; //glue-related! //int stickers=0; mpz_t states; mpz_t newstates; mpz_t deltastates; mpz_init(states); mpz_init(newstates); mpz_init(deltastates); float timesteps=50000; int my_rank; MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); char* filename[60]; sprintf(filename,"./output_single_filament_strong_%.2f/run%d.txt",kstick,my_rank); // sprintf(filename,"./output_single_filament_glue_%.2f/run%d.txt",kstick,my_rank); printf("%s\n",filename); FILE *fp = fopen(filename, "w"); if (fp == NULL) { printf("Error"); return; } float mu=6.0; float glue=1.0; float koff=1.0; float i; int j,k; //float kstick=0.05; float kunstick=kstick; float stick_energy=8.0; int type=0; int delta=0; float nextoff=gsl_ran_exponential(r,1.0/koff); float nextstick=gsl_ran_exponential(r,1.0/kstick); float nextunstick=gsl_ran_exponential(r,1.0/kunstick); //glue-related! // float nextstick=VERYBIG; // float nextunstick=VERYBIG; float peroverlap=kstick; double domega; float c0=1.0/lane_size; int att_depol=0; int acc_depol=0; int att_stick=0; int acc_stick=0; int att_unstick=0; int acc_unstick=0; int att_contr=0; int acc_contr=0; int att_expan=0; int acc_expan=0; count_isl_single(&states,fil_length,stickers,lane_size); for (i=0;i<timesteps;i=i+0.01){ if (fil_length==0){ break; } if ((int)(i*100)%(int)timesteps==0){ printf("%d\%\n",(int)(i*100)/timesteps); } if ((int)(i*100)%50==0){ fprintf(fp, "%f %f %f %f %d %d %d %d %d %d %d %d %d %d %d %d %d\n", i,nextoff,nextstick,nextunstick,fil_length,stickers,lane_size,att_depol,acc_depol,att_stick,acc_stick,att_unstick,acc_unstick,att_contr,acc_contr,att_expan,acc_expan); } if (i>=nextoff){ type=1;} if (i>=nextstick){ type=2; } if (i>=nextunstick){ type=3; } if (type==1){ att_depol=att_depol+1; int newlength=fil_length-1; count_isl_single(&newstates,newlength,stickers,lane_size); if (!mpz_cmp_d(newstates,0.0)){ count_isl_single(&newstates,newlength,stickers-1,lane_size); domega=stick_energy-mu+glue-mpzln(newstates)+mpzln(states); if (domega<0){ fil_length=newlength; stickers=stickers-1; acc_depol++; mpz_set(states,newstates); //fprintf(fp,"depol and removal - domega=%f\n",domega); } else{ double prob=exp(-1.0*domega); double fromthehat=gsl_rng_uniform(r); //printf("metropolising: rng=%f, prob=%f\n",fromthehat,prob); if (fromthehat<prob){ fil_length=newlength; stickers=stickers-1; acc_depol++; mpz_set(states,newstates); //fprintf(fp,"depol and removal - domega=%f\n",domega); }else{ //fprintf(fp,"depol w/unstick not accepted - domega=%f\n",domega); } } }else{ domega=-mu+glue-mpzln(newstates)+mpzln(states); //glue-related! // domega=-mu+glue+peroverlap-mpzln(newstates)+mpzln(states); if (domega<0){ acc_depol++; fil_length=newlength; mpz_set(states,newstates); //fprintf(fp,"depol - domega=%f\n",domega); } /*if it's positive, we calculate the probability of acceptance * and test for it*/ else{ double prob=exp(-1.0*domega); double fromthehat=gsl_rng_uniform(r); //printf("metropolising: rng=%f, prob=%f\n",fromthehat,prob); if (fromthehat<prob){ fil_length=newlength; acc_depol++; mpz_set(states,newstates); //fprintf(fp,"depol - domega=%f\n",domega); }else{ //fprintf(fp,"removal not accepted - domega=%f\n",domega); } } } att_contr++; att_expan++; delta=check_contraction(fil_length,stickers,lane_size,c0,states,r,fp); if (delta==-1){ acc_contr++; } if (delta==+1){ acc_expan++; } lane_size=lane_size+delta; nextoff=i+gsl_ran_exponential(r,1.0/koff); type=0; } if (type==2){ att_stick++; int newstickers=stickers+1; count_isl_single(&newstates,fil_length,newstickers,lane_size); domega=-stick_energy-mpzln(newstates)+mpzln(states); if (domega<0){ stickers=newstickers; acc_stick++; mpz_set(states,newstates); //fprintf(fp,"added sticker - domega=%f\n",domega); } /*if it's positive, we calculate the probability of acceptance * and test for it*/ else{ double prob=exp(-1.0*domega); double fromthehat=gsl_rng_uniform(r); //printf("metropolising: rng=%f, prob=%f\n",fromthehat,prob); if (fromthehat<prob){ stickers=newstickers; acc_stick++; mpz_set(states,newstates); //fprintf(fp,"added sticker - domega=%f\n",domega); }else{ //fprintf(fp,"addition sticker rejected - domega=%f\n",domega); } } delta=check_contraction(fil_length,stickers,lane_size,c0,states,r,fp); if (delta==-1){ acc_contr++; } if (delta==+1){ acc_expan++; } lane_size=lane_size+delta; nextstick=i+gsl_ran_exponential(r,1.0/kstick); type=0; } if (type==3){ att_unstick++; int newstickers=stickers-1; count_isl_single(&newstates,fil_length,newstickers,lane_size); domega=stick_energy-mpzln(newstates)+mpzln(states); if (domega<0){ acc_unstick++; stickers=newstickers; mpz_set(states,newstates); //fprintf(fp,"removal - domega=%f\n",domega); } /*if it's positive, we calculate the probability of acceptance * and test for it*/ else{ double prob=exp(-1.0*domega); double fromthehat=gsl_rng_uniform(r); //printf("metropolising: rng=%f, prob=%f\n",fromthehat,prob); if (fromthehat<prob){ stickers=newstickers; acc_unstick++; mpz_set(states,newstates); //fprintf(fp,"removal - domega=%f\n",domega); }else{ //fprintf(fp,"removal of sticker not accepted - domega=%f\n",domega); } } delta=check_contraction(fil_length,stickers,lane_size,c0,states,r,fp); if (delta==-1){ acc_contr++; } if (delta==+1){ acc_expan++; } lane_size=lane_size+delta; nextunstick=i+gsl_ran_exponential(r,1.0/kunstick); type=0; } }
int main() { //atddtree_key min = 1; //atddtree_key max;// = 1000; //atddtree_key keys[10] = {2,3,5,8,16,23,26,35,48,50}; long max; int i; //atddtree* t; int nuser = 32*1024-1; //1024*1024*64-1; double mean_ia = 205; gsl_rng *r; const gsl_rng_type *T; int n=5; double u; T=gsl_rng_ranlxs0; //设随机数生成器类型是 ranlxs0 //gen arrival gsl_rng_default_seed = ((unsigned long)(time(NULL))); //设seed值为当前时间 r=gsl_rng_alloc(T); //生成实例 double* exp_sample_ir = MALLOC(nuser, double); double abstemp = 0; for(i=0;i<nuser;i++) { exp_sample_ir[i] = gsl_ran_exponential(r, mean_ia); //exp_sample_ir[i] = 2+(i%10000)*0.3; #ifdef LOGISTIC abstemp = gsl_ran_logistic(r, 1); if(abstemp<0) { abstemp=0-abstemp; } exp_sample_ir[i] = abstemp; #endif //exp_sample_ir[i] = 5*gsl_ran_beta(r, 5, 1); //exp_sample_ir[i] = 5*gsl_ran_lognormal(r, 5, 0.25); //printf("exp: %f\n", exp_sample_ir[i]); } double* arrival_real = MALLOC(nuser, double); arrival_real[0] = 1.0; for(i=1;i<nuser;i++) { arrival_real[i] = arrival_real[i-1]+exp_sample_ir[i-1]; //printf("arrival_real: %f\n", arrival_real[i]); } long* arrival = MALLOC(nuser, long); for(i=0;i<nuser;i++) { arrival[i] = (long)arrival_real[i]; //printf("arrival: %ld\n", arrival[i]); } max = 0; for(i=0;i<nuser;i++) { if(arrival[i]>max) { max = arrival[i]; } } printf("---max=%ld, sizeoflong=%d\n", max, sizeof(long)); BtDb* t = bt_open("btree.dat", BT_rw, 12, 65535); BTERR e; for(i=0;i<nuser;i++) { // atddtree_insert(t, arrival+i); e = bt_insertkey (t, arrival+i, 4, 0, i+1, i+1); //printf("insert %ld, height=%d\n", arrival[i], t->h); //printf("level=%d, e=%d\n", t->page->lvl, e); } // (char *name, uint mode, uint bits, uint cacheblk); // printf("level=%d\n", t->page->lvl); }