int main() {
    const double probability_step = 1.0 / (N_STEPS - 1);
    Counter counters[N_STEPS];
    for (size_t i = 0; i < N_STEPS; i++)
        counters[i] = (Counter) {
        i * probability_step, 0
    };

    bool sample_shown = false;
    static Grid grid;
    srand(time(NULL));

    for (size_t i = 0; i < N_STEPS; i++) {
        for (size_t t = 0; t < N_TRIES; t++) {
            initialize(grid, counters[i].prob);
            if (percolate(grid)) {
                counters[i].count++;
                if (!sample_shown) {
                    printf("Percolating sample (%dx%d,"
                           " probability =%5.2f):\n",
                           N_COLS, N_ROWS, counters[i].prob);
                    show(grid);
                    sample_shown = true;
                }
            }
        }
    }

    printf("\nFraction of %d tries that percolate through:\n", N_TRIES);
    for (size_t i = 0; i < N_STEPS; i++)
        printf("%1.1f %1.3f\n", counters[i].prob,
               counters[i].count / (double)N_TRIES);

    return 0;
}
void invperc (const int size, const int nfill) {
	
	int i;
	for (i = 0; i < nfill; ++i){
		struct found_point point = percolate(size);
		if(set_new_point(size, point)) { break; }
	}
}
int main(void)
{
    make_grid(.5, 10, 10);
    percolate();
    show_grid();

    int cnt, i, p;

    puts("\nrunning 10x10 grids 10000 times for each p:");
    for (p = 1; p < 10; p++) {
        for (cnt = i = 0; i < 10000; i++) {
            make_grid(p / 10., 10, 10);
            cnt += percolate();
            //show_grid(); // don't
        }
        printf("p = %3g: %.4f\n", p / 10., (double)cnt / i);
    }

    free(start);
    return 0;
}
void invperc (const int size, const int nfill, const int numThreads) {
	
	ThreadPool pool(numThreads);

	int i;
	for (i = 0; i < nfill; ++i){
		
		FoundPoint point = percolate(size, pool);
		if(setNewPoint(size, point)) { break; }
	
	}
}
std::string ConfigFile::_ReadString(std::string section,std::string entry,std::string defaultString)
{
/*
  功能:读取指定的section的指定entry的值到buffer中,未找到则使用缺省配置
  参数:
  section[IN]:指定的section
  entry[IN]:指定的entry
  defaultstring[IN]:缺省配置
  buffer[IN]:存放entry值的缓冲区
  bufLen[IN]:缓冲区长度
  返回值:
  成功返回buffer中的值的长度
  失败返回-1
*/

    int len = -1;

    fseek(fin,0,SEEK_SET);

    char buffer[1024];
    memset(buffer,0,1024);
    int bufLen = sizeof(buffer) -1;
    if( gotoSection( section.c_str() ) )
    {
        len = _ReadEntry(entry.c_str(), buffer, bufLen, true);
    }


	//后面的空格也要去。前面的空格等于没去.
    //hauk 2013-10-29 modified
    //percolate(buffer);
	char *szTmp = percolate(buffer);
	if(szTmp && strlen(szTmp) != strlen(buffer)) UTILITY::Snprintf(buffer, sizeof(buffer), "%s", szTmp);
	//end of modification
    
	len = strlen(buffer);

    if( len <= 0 ) //can not read entry, use default string
    {
        strncpy( buffer, defaultString.c_str(), bufLen-1 );
        buffer[bufLen-1] = 0;
        len = strlen(buffer);
    }

/**
 * 支持配置文件值中环境变量扩展
 * 环境变量格式 ${环境变量}
 */
    string str(buffer);
    return _expand_vars(str);
//    return buffer;
}
Example #6
0
Any_Type
remove_min(struct Heap *h)
{
	if (is_heap_empty(h)) {
		Any_Type temp = {0};
		return temp;
	}
	else {
		Any_Type        min = h->storage[1];
		h->storage[1] = h->storage[h->num_elements--];
		percolate(h, 1);

		return min;
	}
}
char * ConfigFile::FGetS(char *pbuf, int size, FILE *fp)
{
   char tmp[1024];
   memset(tmp,0,1024);
   char *pc;
   if (fgets(tmp,1023,fp)==NULL)
      return(NULL);
   pc = percolate(tmp);
   if (pc==NULL)
   {
      pbuf[0]=0;
   }
   else
   {
      strncpy(pbuf,pc,size);
   }
   return(pbuf);
}
Example #8
0
/* Function bushfireSpread() simulates a bushfire spread. A forest is modeled
 * as a square point lattice in which each lattice point represents either a
 * tree or a treeless location. For detailed information, see Requirement#6.
 * This function will be used in menu option 5.
 */
int bushfireSpread(char forest[ ][ Side ], float density )
{
    int count; /* count of trees in forest */

    do {
        /* Prompt for density percentage. */
        density = get_density();
        /* Initialize forest cells to Tree or NoTree. */
        count = plant_forest( forest, density);
        /* Display forest in initial state. */
        display( forest, "Initial state" );
        /* Run simulation. */
        percolate( forest );
        /* Display forest in final state. */
        display( forest, "Final state" );
        /* Report statistics. */
        report( forest, density, count );
    } while ( go_again() );

    return 0;
}
int main()
{
    int i;
    int nn = 4;
    double* biases =  malloc(nn*sizeof(double));
    double* oldbiases =  malloc(nn*sizeof(double));
    double* bonds = calloc(nn*nn, sizeof(double));
    int* ptr = malloc(nn*sizeof(int));
    double* flips = malloc(nn*sizeof(double));
    double* cprobs = malloc(nn*sizeof(double));

    /* testcase 1 */
    biases[0] = -2.;
    biases[1] = -3.;
    biases[2] = -5.;
    biases[3] = -7.;
    for (i=0; i < nn; ++i)
        oldbiases[i] = biases[i];
    bonds[ind(0, 3, nn)] = 1;
    bonds[ind(1, 2, nn)] = 1;
    percolate(bonds, ptr, biases, nn);
    flip_clusters(flips, cprobs, ptr, biases, nn);
    for (i=0; i < nn; ++i) {
        printf("i=%d, flips[%d]=%g, ptr[%d]=%d, biases[%d]=%g\n", i, i, flips[i], i, ptr[i], i, biases[i]);
    }
    printf("\n");
    assert(ptr[0]==-2);
    assert(biases[0]==oldbiases[0]+oldbiases[3]);
    assert(ptr[1]==-2);
    assert(biases[1]==oldbiases[1]+oldbiases[2]);
    assert(ptr[2]== 1);
    assert(ptr[3]== 0);

    /* testcase 2 */
    biases[0] = 2.;
    biases[1] = 3.;
    biases[2] = 5.;
    biases[3] = 7.;
    bonds[ind(0, 3, nn)] = 0;
    bonds[ind(1, 2, nn)] = 0;
    bonds[ind(0, 3, nn)] = 1;
    bonds[ind(1, 3, nn)] = 1;
    percolate(bonds, ptr, biases, nn);
    flip_clusters(flips, cprobs, ptr, biases, nn);
    for (i=0; i < nn; ++i) {
        printf("i=%d, flips[%d]=%g, ptr[%d]=%d, biases[%d]=%g\n", i, i, flips[i], i, ptr[i], i, biases[i]);
    }
    printf("\n");
    assert(ptr[0]==-3);
    assert(biases[0]==12.);
    assert(ptr[2]==-1);
    assert(biases[2]==5.);
    assert(ptr[1]== 0);
    assert(ptr[3]== 0);

    free(biases);
    free(oldbiases);
    free(bonds);
    free(flips);
    free(cprobs);
    free(ptr);
    return 0;
}
Example #10
0
main ()
{
  unsigned long long int i;
   int timestart;
   char name[50];
   
//  init_genrand(1); //seed MT random number 
  timestart=time(NULL); 
  init_genrand(timestart); //seed with current time in seconds since 1970 Unix Epoch
   
  empty_lattice (); //clears lattice
   fprintf(stderr,"Empty Lattice E: %f\n",lattice_energy());
  fill_snakes (DENSITY);
//  print_lattice();
//   sleep(1);
  //print_snakes();
  //print_a_snake(0);
   
  fprintf (stderr, "Snakes filled\nNum Snakes: %d Lattice Points: %d\n",
	   num_snakes, X * Y * Z);
//  fprintf(stderr,"Initial Snake Filled Lattice Energy: %f\n",lattice_energy());   
   
//   printf("#X,Y,Z: %d %d %d\n#Density: %f\n#Beta: %f\n#l_0: %d\n#sigma: %f\n#iE[1,1]: %f\n#Snakes: %d\n#Slithers: %d\n",
//	  X,Y,Z,DENSITY,beta,l_0,sigma, iE[1][1], num_snakes,TOTAL_SLITHERS);

   for (i = 0; i < (long long) TOTAL_SLITHERS; i++)
    {
      wriggle (); //this function is the heart of the simulation - executed millions of times per second
       //might be worth unrolling this directly into this loop rather than have it as a subroutine...
       
       if (i%SLITHER_PRINT==0) //display information for monitoring the progression of the simulation
	 {
	 fprintf(stderr,"Slithers: %lld Snakes: %d Lattice Points: %d \n",i,num_snakes,X*Y*Z);
//	    lattice_energy();
	    percolate();

//	    generate_df3(i/SLITHER_PRINT);
//
	    	    print_lattice();
	    
//	    print_povray ();
//	    fprintf(stderr,"Lattice Energy: %f\n",lattice_energy());
//        usleep(1000000);	    
//	print_lattice_pnm_file((int)(beta*1000.00));    
	 }
    }
   
  percolate();   //tests for percolation - and sets electric snakes as 'live' if part of percolating cluster
                 //its a recursive algorithm though - so will crash if too many lattice sites!
//  print_lattice();
//  print_xmakemol();
   print_povray("snakes.pov");
//  print_lattice_pnm_file((int)(beta*1000.00));    
   print_lattice_pnm_file(1);

  fprintf (stderr, "Num Snakes: %d Lattice Points: %d\n", num_snakes,
	   X * Y * Z);
//   fprintf(stderr,"\nStats:\n\tRandom Numbers used: %lld\n\t Steps: %lld\n\tRand per Step: %f\n",
//	   rand_count,i,((double)rand_count/(double)i) );

//   scanf("%s",&name);
//   save_lattice_file(name);   
   
//   gorgophone();
//   printf("#Time Taken: %d seconds\n",time(NULL)-timestart);
}