void count_test(void) { struct count *count = count_new(); struct timeval tv; int i; gettimeofday(&tv, NULL); count_internal_increment(count, &tv, 3); if (count_get_sum(&count->seconds) != 3) errx(1, "second count should be 1"); tv.tv_sec += 61; count_internal_increment(count, &tv, 2); count_internal_increment(count, &tv, 0); if (count_get_sum(&count->seconds) != 2) errx(1, "second count should be 1"); if (count_get_sum(&count->minutes) != 3) errx(1, "minute count should be 1"); tv.tv_sec += 3540; count_internal_increment(count, &tv, 1); if (count_get_sum(&count->seconds) != 1) errx(1, "second count should be 1"); if (count_get_sum(&count->minutes) != 2) errx(1, "minute count should be 1"); if (count_get_sum(&count->hours) != 3) errx(1, "hour count should be 1"); count_internal_print(stderr, count, "test-count"); for (i = 0; i < 24; i++) { tv.tv_sec += 3600; count_internal_increment(count, &tv, 0); } count_internal_print(stderr, count, "test-count"); if (count_get_sum(&count->seconds) || count_get_sum(&count->minutes) || count_get_sum(&count->hours)) errx(1, "all counts should be zero"); fprintf(stderr, "\t%s: OK\n", __func__); }
int main(int argc, char *argv[]) { struct Map_info in, out, vis; struct GModule *module; /* GRASS module for parsing arguments */ struct Option *input, *output; /* The input map */ struct Option *coor, *ovis; char *mapset; struct Point *points; struct Line *lines; int num_points, num_lines; int n = 0; /* initialize GIS environment */ G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */ /* initialize module */ module = G_define_module(); module->keywords = _("vector, path, visibility"); module->description = _("Visibility graph construction."); /* define the arguments needed */ input = G_define_standard_option(G_OPT_V_INPUT); output = G_define_standard_option(G_OPT_V_OUTPUT); coor = G_define_option(); coor->key = "coordinate"; coor->key_desc = "x,y"; coor->type = TYPE_STRING; coor->required = NO; coor->multiple = YES; coor->description = _("One or more coordinates"); ovis = G_define_option(); ovis->key = "vis"; ovis->type = TYPE_STRING; ovis->required = NO; ovis->description = _("Add points after computing the vis graph"); /* options and flags parser */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); Vect_check_input_output_name(input->answer, output->answer, GV_FATAL_EXIT); Vect_set_open_level(2); mapset = G_find_vector2(input->answer, NULL); /* finds the map */ if (mapset == NULL) G_fatal_error("Vector map <%s> not found", input->answer); if (Vect_open_old(&in, input->answer, mapset) < 1) /* opens the map */ G_fatal_error(_("Unable to open vector map <%s>"), G_fully_qualified_name(input->answer, mapset)); if (Vect_open_new(&out, output->answer, WITHOUT_Z) < 0) { Vect_close(&in); G_fatal_error(_("Unable to create vector map <%s>"), output->answer); } if (ovis->answer != NULL) { mapset = G_find_vector2(ovis->answer, NULL); if (Vect_open_old(&vis, ovis->answer, mapset) < 1) G_fatal_error(_("Unable to open vector map <%s>"), G_fully_qualified_name(ovis->answer, mapset)); if (Vect_copy_map_lines(&vis, &out) > 0) G_fatal_error(_("Unable to copy elements from vector map <%s>"), G_fully_qualified_name(ovis->answer, mapset)); } if (G_projection() == PROJECTION_LL) G_warning(_("Lat-long projection")); /* counting how many points and lines we have to allocate */ count(&in, &num_points, &num_lines); /* modify the number if we have new points to add */ if (coor->answers != NULL) num_points += count_new(coor->answers); /* and allocate */ points = G_malloc(num_points * sizeof(struct Point)); lines = G_malloc(num_lines * sizeof(struct Line)); /* and finally set the lines */ load_lines(&in, &points, &num_points, &lines, &num_lines); if (coor->answers != NULL) add_points(coor->answers, &points, &num_points); if (ovis->answer == NULL) construct_visibility(points, num_points, lines, num_lines, &out); else visibility_points(points, num_points, lines, num_lines, &out, n); G_free(points); G_free(lines); Vect_build(&out); Vect_close(&out); Vect_close(&in); exit(EXIT_SUCCESS); }
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 main(int argc, char *argv[]) { struct Map_info in, out, vis; struct GModule *module; /* GRASS module for parsing arguments */ struct Option *input, *output; /* The input map */ struct Option *coor, *ovis; struct Point *points; struct Line *lines; int num_points, num_lines; int n = 0; /* initialize GIS environment */ G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */ /* initialize module */ module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("network")); G_add_keyword(_("shortest path")); G_add_keyword(_("visibility")); module->description = _("Performs visibility graph construction."); /* define the arguments needed */ input = G_define_standard_option(G_OPT_V_INPUT); output = G_define_standard_option(G_OPT_V_OUTPUT); coor = G_define_standard_option(G_OPT_M_COORDS); ovis = G_define_standard_option(G_OPT_V_MAP); ovis->key = "visibility"; ovis->required = NO; ovis->label = _("Name of input vector map containing visible points"); ovis->description = _("Add points after computing the visibility graph"); /* options and flags parser */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); Vect_check_input_output_name(input->answer, output->answer, G_FATAL_EXIT); Vect_set_open_level(2); if (Vect_open_old(&in, input->answer, "") < 1) /* opens the map */ G_fatal_error(_("Unable to open vector map <%s>"), input->answer); if (Vect_open_new(&out, output->answer, WITHOUT_Z) < 0) { Vect_close(&in); G_fatal_error(_("Unable to create vector map <%s>"), output->answer); } if (ovis->answer != NULL) { if (Vect_open_old(&vis, ovis->answer, "") < 1) G_fatal_error(_("Unable to open vector map <%s>"), ovis->answer); if (Vect_copy_map_lines(&vis, &out) > 0) G_fatal_error(_("Unable to copy elements from vector map <%s>"), ovis->answer); } if (G_projection() == PROJECTION_LL) G_warning(_("Lat-long projection")); /* counting how many points and lines we have to allocate */ count(&in, &num_points, &num_lines); /* modify the number if we have new points to add */ if (coor->answers != NULL) num_points += count_new(coor->answers); /* and allocate */ points = G_malloc(num_points * sizeof(struct Point)); lines = G_malloc(num_lines * sizeof(struct Line)); /* and finally set the lines */ load_lines(&in, &points, &num_points, &lines, &num_lines); if (coor->answers != NULL) add_points(coor->answers, &points, &num_points); if (ovis->answer == NULL) construct_visibility(points, num_points, lines, num_lines, &out); else visibility_points(points, num_points, lines, num_lines, &out, n); G_free(points); G_free(lines); Vect_copy_head_data(&in, &out); Vect_hist_copy(&in, &out); Vect_hist_command(&out); Vect_build(&out); Vect_close(&out); Vect_close(&in); exit(EXIT_SUCCESS); }