discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

The trouble to run code for motecaro method by GNU


From: Yuichiro Nakada
Subject: The trouble to run code for motecaro method by GNU
Date: Thu, 16 Apr 2009 06:07:24 -0700

To whom may concern
 
I am Yuichiro
 
I am trying to use GNU for Windows
I installed GNU step system and core for 0.220
and I also download comiler from here
http://www.mingw.org/ 
I think I download  sourcenavigator-NG4.tar.bz2 from http://www.mingw.org/  as compiler.
But I am not sure if that is right one to download for my window xp.  
 
What I want to run is this code, which I attatched the file in this email
 
But I dont know how I checked if I sart to run the program. 
I am really a beginner for programming.
Let me know the procedure from scratch in detail.
 
Thank you
 
It seems the file does not attatched so I will just copied the code here.
Please paste the code to notepad because it will be more organized.
 
//***************************************************************************** //An initial attempt at Markov Chain Monte Carlo using the metropolis algorithm //Create one-dimensional gaussian droplets //Randel Cotta 7/31/06 //***************************************************************************** #include <stdio.h> #include <math.h> #include <gsl/gsl_rng.h> #include <gsl/gsl_statistics.h> int main (int argc, const char * argv[]) { int i, nparts; //indices and associated ranges... for example, int j, burns; //the index i takes integer values in 0,...,nparts int k, loops; int m, drops; int b, bins, binned; double length = 10; //The initial range of positions and the width of the double w = 1.0; //gaussian distribution, both fixed for now double delta,delta5, arg, pxoverpt, trpos, rn; //various real variables int accept = 0; FILE *outp,*bintp; outp = fopen("./DropSim.txt", "w"); bintp = fopen("./Binned.txt", "w"); printf("\nThis is my 1D Gaussian Droplet file\n\n"); //Questions for the user: printf("How many particles? >> "); scanf("%d", &nparts); printf("# of Burn Loops? >> "); scanf("%d", &burns); printf("# of Droplets? >> "); scanf("%d", &drops); printf("# of Loops per Droplet? >> "); scanf("%d", &loops); printf("Delta size? >> "); scanf("%lf", &delta); printf("Number of bins? >>"); scanf("%d",&bins); printf("\n\n"); delta5=delta/25; double meanarray[drops]; double devarray[drops]; //setting up a random number generator: const gsl_rng_type * T; gsl_rng * r; gsl_rng_env_setup(); T = gsl_rng_default; r = gsl_rng_alloc (T); printf ("generator type: %s\n", gsl_rng_name (r)); printf ("seed = %lu\n", gsl_rng_default_seed); printf ("first value = %lu\n", gsl_rng_get (r)); //Making the initial (random) particle distribution: double xpos[nparts]; for(i = 0; i<nparts; i++) { xpos[i] = length * (gsl_rng_uniform(r) - 0.5); } //This is the "Burn" loop, the particles are moved until the initial configuration //is forgotten: for(j = 0; j < burns; j++){ for(i = 0; i < nparts; i++) { //generate a random number to be added to xpos[i] //tune the size (delta) of this addition to get desired acceptance rate (r) trpos = xpos[i] + delta * (gsl_rng_uniform(r) - 0.5); if( xpos[i]*xpos[i] > trpos*trpos){ xpos[i] = trpos; accept++; } else { //generate another random number for Metropolis criterion... arg = trpos*trpos - xpos[i]*xpos[i]; pxoverpt = exp( -arg / (w*w)); rn = gsl_rng_uniform(r); if(pxoverpt > rn) { xpos[i] = trpos; accept++; } else; } } } /****** Use this to tune the step size ************* printf("\nAccepted moves: %d", accept); printf("\nProposed moves: %d", burns * nparts); double ratio = (double) accept / (burns * nparts); printf("\nThe Acceptance Ratio was: %f\n\n", ratio); *******************************************************/ //These are the data loops from which we calculate the statistical properties of the //droplets, each droplet is generated from the burned in state: double calcpos[nparts]; for(i = 0; i < nparts; i++) { calcpos[i] = xpos[i]; } for(m = 0; m < drops; m++){ for(k = 0; k < loops; k++) { for(i = 0; i < nparts; i++) { trpos = calcpos[i] + delta * (gsl_rng_uniform(r) - 0.5); if(calcpos[i]*calcpos[i] > trpos*trpos){ calcpos[i] = trpos; accept++; } else { arg = trpos*trpos - calcpos[i]*calcpos[i]; pxoverpt = exp( -arg / (w*w)); rn = gsl_rng_uniform(r); if(pxoverpt > rn) { calcpos[i] = trpos; accept++; } else; } } } fprintf(outp, "drop%d ,", m + 1); for(i = 0; i < nparts; i++){ fprintf(outp, "%f ,", calcpos[i]); } fprintf(outp, "\n"); for(b=-bins+1;b<bins;b++){ binned=0; for(i = 0; i < nparts; i++){ if(calcpos[i]<b*delta5&& (b-1)*delta5<calcpos[i]){binned++;} } fprintf(bintp,"%d,", binned); } fprintf(bintp,"\n"); //printf("\nDrop %d mean: %f Variance: %f", m + 1, gsl_stats_mean(calcpos,1,nparts), gsl_stats_variance(calcpos,1,nparts)); meanarray[m] = gsl_stats_mean(calcpos,1,nparts); devarray[m] = sqrt(gsl_stats_variance(calcpos,1,nparts)); } printf("\n\nThe mean of the droplet means was: %f\n", gsl_stats_mean(meanarray,1,drops)); printf("The deviation of the drop means was: %f\n", sqrt(gsl_stats_variance(meanarray,1,drops))); printf("\n\nThe mean of the drop half-widths was: %f\n", gsl_stats_mean(devarray,1,drops)); printf("The deviation of the drop half-widths was: %f\n\n", sqrt(gsl_stats_variance(devarray,1,drops))); fprintf(outp, "\n\n"); fprintf(outp, "\n\nThe mean of the droplet means was: %f\n", gsl_stats_mean(meanarray,1,drops)); fprintf(outp, "The deviation of the drop means was: %f\n", sqrt(gsl_stats_variance(meanarray,1,drops))); fprintf(outp, "\n\nThe mean of the drop half-widths was: %f\n", gsl_stats_mean(devarray,1,drops)); fprintf(outp, "The deviation of the drop half-widths was: %f\n\n", sqrt(gsl_stats_variance(devarray,1,drops))); gsl_rng_free (r); //This frees the memory used by the RNG return 0; }
 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]