/* generates a random number on [0,0xffffffff]-interval */ unsigned long genrand_int32(void) { unsigned long y; /* mag01[x] = x * MATRIX_A for x=0,1 */ if(mti>=N) { /* generate N words at one time */ int kk; if(mti==N+1) /* if init_genrand() has not been called, */ InitializeRandomNumberGenerator(5489UL); /* a default initial seed is used */ for(kk=0;kk<N-M;kk++) { y=(mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); mt[kk]=mt[kk+M]^(y>>1)^mag01[y&0x1UL]; } for(;kk<N-1;kk++) { y=(mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); mt[kk]=mt[kk+(M-N)]^(y>>1)^mag01[y&0x1UL]; } y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); mt[N-1]=mt[M-1]^(y>>1)^mag01[y&0x1UL]; mti=0; }
/* generates a random number on [0, 2^64-1]-interval */ unsigned long long genrand64_int64(void) { int i; unsigned long long x; if(mti>=NN) { /* generate NN words at one time */ /* if init_genrand64() has not been called, */ /* a default initial seed is used */ if(mti==NN+1) InitializeRandomNumberGenerator(5489ULL); for(i=0;i<NN-MM;i++) { x=(mt[i]&UM)|(mt[i+1]&LM); mt[i]=mt[i+MM]^(x>>1)^mag01[(int)(x&1ULL)]; } for(;i<NN-1;i++) { x=(mt[i]&UM)|(mt[i+1]&LM); mt[i]=mt[i+(MM-NN)]^(x>>1)^mag01[(int)(x&1ULL)]; } x=(mt[NN-1]&UM)|(mt[0]&LM); mt[NN-1]=mt[MM-1]^(x>>1)^mag01[(int)(x&1ULL)]; mti=0; } x=mt[mti++]; x^=(x>>29)&0x5555555555555555ULL; x^=(x<<17)&0x71D67FFFEDA60000ULL; x^=(x<<37)&0xFFF7EEE000000000ULL; x^=(x>>43); return x; }
// definition of the main function int main(int argc, char **argv) // TODO: add arguments 1. input file name and 2. number of iterations { // declaration of local variables char *filename; // name of the input file, which the records have to be read from unsigned int iterations; // number of iterations, got as a program's input unsigned int records; // number of records (>= 1), to be read from the input file unsigned int i; // array indexes record_t recordset[MAX_NUMBER_OF_RECORDS]; // array of records, to be read from the input file unsigned int cumWeights[MAX_NUMBER_OF_RECORDS]; // array of cumulated record weights if(ParseArguments(argc, argv, &filename, &iterations) < 0) { return(-1); } if(ReadFile(filename, &records, recordset) < 0) { return(-1); } // initialization stuff InitializeRandomNumberGenerator(); PrepareArrayOfCumulatedRecordWeights(records, recordset, cumWeights); // loop up to the given number of iterations, // picking a random record and incrementing the related frequency for(i = 0; i < iterations; i++) { recordset[PickRandomRecordIndex(records, cumWeights)].frequency++; } // show the statistics on the stdout for(i = 0; i < records; i++) { printf( "%s:\n- prob. %3u%%, freq. %3u%%\n", recordset[i].label, 100 * recordset[i].weight / cumWeights[records - 1], 100 * recordset[i].frequency / iterations); } return 0; }
// divide N particles among p compartments int main(void) // takes in no parameters { int i,j,k,l,index; int P,N,Ncycle; double Distribution[MAX_COMPARTMENTS][MAX_PARTICLES]; //2D Array: what are the brackets doing here? Okay most likely they are to define the dimensionality of the array. int NumInComp[MAX_COMPARTMENTS]; //1D Array: declaring number of particles in compartment FILE *FilePtr; //created sufficient memory for FilePtr // initialize the random number generator with the system time InitializeRandomNumberGenerator(time(0l)); //most likely a function located in ran_uniform.h; I think time(01) is supposed to be the seed value for the generator // read the input parameters printf("Number of Particles N? "); fscanf(stdin,"%d",&N); //I REALLY DON'T Understand what fscanf is doing in this situation printf("Number of Compartments p? "); fscanf(stdin,"%d",&P); printf("Number of Cycles (x %d)? ",CycleMultiplication); fscanf(stdin,"%d",&Ncycle); if(P<2||P>MAX_COMPARTMENTS||N<2||N>MAX_PARTICLES) { printf("Error in input parameters\n"); exit(1); //what does exit do here (and what is the 1 for?) } // initialize: So you have to make sure every entry is zero? What is the default setting? for(i=0;i<N;i++) { for(j=0;j<P;j++) { Distribution[j][i]=0.0; NumInComp[j]=0; } } // loop over all cycles for(i=0;i<Ncycle;i++) for(k=0;k<CycleMultiplication;k++) { // Distribute particles over the compartments: // Loop over all particles, pick a random compartment, and add // a particle to it. // A random number in the interval [0,1> can be generated using // RandomNumber(). // NumInComp[index] = number of particles in compartment 'index'. // start modification for(l=0;l<N;l++) { index = round(RandomNumber()*(P)); NumInComp[index] += 1; } // end modification // make a histogram for(j=0;j<P;j++) { Distribution[j][NumInComp[j]]+=1.0; NumInComp[j]=0; } } // Write Results FilePtr=fopen("results.dat","w"); for(j=0;j<P;j++) { for(i=0;i<N;i++) { if(Distribution[j][i]>0.0) Distribution[j][i]/=(double)Ncycle*CycleMultiplication; fprintf(FilePtr,"%d %f\n",i,Distribution[j][i]); } fprintf(FilePtr,"\n"); } fclose(FilePtr); // write analytical distribution FilePtr=fopen("analytical.dat","w"); for(j=0;j<N;j++) { fprintf(FilePtr,"%d %f\n",j,exp(LnFactorial(N)-LnFactorial(j)- LnFactorial(N-j)-j*log(P)-(N-j)*log(P/((double)(P-1))))); } fclose(FilePtr); return 0; }
// Npt Simulation Of Hard-Spheres int main(void) { int i,j,k,m,n,Ipart; int NumberOfCycles,NumberOfInitializationCycles; int NumberOfDisplTrials,NumberOfDisplAccepted; int NumberOfVolumeTrials,NumberOfVolumeAccepted; int overlap; double MaximumDisplacement,MaximumVolumeChange; double Pressure,Beta,Vnew,Vold,Boxold; double VolumeSum,VolumeCount; double Box,r2; FILE *FilePtr; VECTOR dr,pos; // initialize the random number generator with the system time InitializeRandomNumberGenerator(time(0l)); // read parameters FilePtr=fopen("input","r"); fscanf(FilePtr,"%d %d %d %lf %lf %lf %lf\n", &NumberOfCycles,&NumberOfInitializationCycles,&NumberOfParticles, &MaximumDisplacement,&MaximumVolumeChange, &Beta,&Pressure); fclose(FilePtr); printf("Number of cycles (x %d) : %d\n",CycleMultiplication,NumberOfCycles); printf("Number of init cycles : %d\n",NumberOfInitializationCycles); printf("NumberOfParticles : %d\n",NumberOfParticles); printf("Maximum displacement : %f\n",MaximumDisplacement); printf("Maximum volume change : %f\n",MaximumVolumeChange); printf("Beta : %f\n",Beta); printf("Pressure : %f\n",Pressure); printf("\n"); printf("\n"); if(NumberOfParticles>MAX_NUMBER_OF_PARTICLES||NumberOfParticles<100) { printf("Error in the number of particles\n"); exit(-1); } // initialize stuff NumberOfDisplTrials=0; NumberOfDisplAccepted=0; NumberOfVolumeTrials=0; NumberOfVolumeAccepted=0; VolumeSum=0.0; VolumeCount=0.0; Box=10.0; FilePtr=fopen("movie.pdb","w"); // put particles on a lattice n=0; for(i=0;i<9;i++) for(j=0;j<9;j++) for(k=0;k<9;k++) { Positions[n].x=i*1.1; Positions[n].y=j*1.1; Positions[n].z=k*1.1; n++; } // start of the simulation WritePdb(FilePtr); for(m=0;m<NumberOfCycles;m++) { for(n=0;n<CycleMultiplication;n++) { // Select Particle And Move At Random Ipart=RandomNumber()*(NumberOfParticles+1); if(Ipart>=NumberOfParticles) { // volume change // random walk in Ln(V) NumberOfVolumeTrials++; Vold=CUBE(Box); Boxold=Box; Vnew=exp(log(Vold)+(RandomNumber()-0.5)*MaximumVolumeChange); Box=pow(Vnew,1.0/3.0); // transform coordinates for(i=0;i<NumberOfParticles;i++) { OldPositions[i].x=Positions[i].x; OldPositions[i].y=Positions[i].y; OldPositions[i].z=Positions[i].z; Positions[i].x*=(Box/Boxold); Positions[i].y*=(Box/Boxold); Positions[i].z*=(Box/Boxold); } // check for overlaps overlap=FALSE; for(i=0;i<NumberOfParticles-1;i++) { for(j=i+1;j<NumberOfParticles;j++) { dr.x=Positions[i].x-Positions[j].x; dr.y=Positions[i].y-Positions[j].y; dr.z=Positions[i].z-Positions[j].z; // apply boundary conditions dr.x-=Box*rint(dr.x/Box); dr.y-=Box*rint(dr.y/Box); dr.z-=Box*rint(dr.z/Box); // Nearest Image r2=SQR(dr.x)+SQR(dr.y)+SQR(dr.z); if(r2<1.0) overlap=TRUE; } } // no overlap... use acceptance rule if((!overlap)&&(RandomNumber()<exp(-Beta*Pressure*(Vnew-Vold)+log(Vnew/Vold)*(NumberOfParticles+1.0)))) { NumberOfVolumeAccepted++; } else { for(i=0;i<NumberOfParticles;i++) { Positions[i].x=OldPositions[i].x; Positions[i].y=OldPositions[i].y; Positions[i].z=OldPositions[i].z; } Box=Boxold; } } else { // displacement NumberOfDisplTrials++; pos.x=Positions[Ipart].x+(RandomNumber()-0.5)*MaximumDisplacement; pos.y=Positions[Ipart].y+(RandomNumber()-0.5)*MaximumDisplacement; pos.z=Positions[Ipart].z+(RandomNumber()-0.5)*MaximumDisplacement; // put particle back in the box pos.x-=Box*rint(pos.x/Box); pos.y-=Box*rint(pos.y/Box); pos.z-=Box*rint(pos.z/Box); // see if there is an overlap with any other particle overlap=FALSE; for(i=0;i<NumberOfParticles;i++) { if(i!=Ipart) { dr.x=pos.x-Positions[i].x; dr.y=pos.y-Positions[i].y; dr.z=pos.z-Positions[i].z; // apply boundary conditions dr.x-=Box*rint(dr.x/Box); dr.y-=Box*rint(dr.y/Box); dr.z-=Box*rint(dr.z/Box); r2=SQR(dr.x)+SQR(dr.y)+SQR(dr.z); if(r2<1.0) overlap=TRUE; } } // no overlap, accept if(!overlap) { NumberOfDisplAccepted++; Positions[Ipart].x=pos.x; Positions[Ipart].y=pos.y; Positions[Ipart].z=pos.z; } } } // make a movie file if((m%10)==0) { WritePdb(FilePtr); printf("Volume : %f Box : %f\n",CUBE(Box),Box); } // sample volume if(m>NumberOfInitializationCycles) { VolumeCount+=1.0; VolumeSum+=CUBE(Box); } } fclose(FilePtr); // print results printf("\n"); printf("\n"); printf("Fraction Succes (Displ.) : %f\n",(double)NumberOfDisplAccepted/NumberOfDisplTrials); printf("Fraction Succes (Volume) : %f\n",(double)NumberOfVolumeAccepted/NumberOfVolumeTrials); printf("Average Volume : %f\n",VolumeSum/VolumeCount); printf("Average Density : %f\n",NumberOfParticles/(VolumeSum/VolumeCount)); return 0; }
int main(void) { int NumberOfCycles,NumberOfInitializationCycles; int i,j,k,SelectedParticle,NumberOfSamples,overlap; int NumberOfAttemptedMoves,NumberOfAcceptedMoves; double r2,MaximumDisplacement; VECTOR PosNew,dr; FILE *FilePtr; // initialize the random number generator with the system time InitializeRandomNumberGenerator(time(0l)); FilePtr=fopen("movie.pdb","w"); // read the input parameters printf("How many cycles (x %d) ? ",CycleMultiplication); fscanf(stdin,"%d",&NumberOfCycles); printf("How many init. cycles ? (example: 100 ) "); fscanf(stdin,"%d",&NumberOfInitializationCycles); printf("How many particles ? (always 2 < i < 80 ) "); fscanf(stdin,"%d",&NumberOfParticles); printf("Maximum displacement ? (disp > 0 ) "); fscanf(stdin,"%lf",&MaximumDisplacement); printf("Period boundary conditions ? (0 or 1) "); fscanf(stdin,"%d",&PBC); if((NumberOfParticles>MAX_PARTICLES)||(NumberOfParticles<2)) { printf("input error\n"); exit(0); } Sample(INITIALIZE); NumberOfAttemptedMoves=0; NumberOfAcceptedMoves=0.0; NumberOfSamples=0; // put particles on a lattice k=0; for(i=0;i<(int)(BOXSIZE-1.0);i++) { for(j=0;j<(int)(BOXSIZE-1.0);j++) { Positions[k].x=i*BOXSIZE/(BOXSIZE-1.0); Positions[k].y=j*BOXSIZE/(BOXSIZE-1.0); k++; } } for(i=0;i<NumberOfCycles;i++) { for(j=0;j<CycleMultiplication;j++) { NumberOfAttemptedMoves++; // select particle at random SelectedParticle=RandomNumber()*NumberOfParticles; PosNew.x=Positions[SelectedParticle].x+(RandomNumber()-0.5)*MaximumDisplacement; PosNew.y=Positions[SelectedParticle].y+(RandomNumber()-0.5)*MaximumDisplacement; // always reject when particle is out of the box //if(PBC||(MIN(PosNew.x,PosNew.y)>=0.0&&MAX(PosNew.x,PosNew.y)<BOXSIZE)) if(PBC||((!PBC)&&(MIN(PosNew.x,PosNew.y)>=0.0&&MAX(PosNew.x,PosNew.y)<BOXSIZE))) { // see if there is an overlap with any other particle overlap=FALSE; for(k=0;((k<NumberOfParticles));k++) { if(k!=SelectedParticle) { dr.x=PosNew.x-Positions[k].x; dr.y=PosNew.y-Positions[k].y; // apply boundary conditions if(PBC) { dr.x-=BOXSIZE*rint(dr.x/BOXSIZE); dr.y-=BOXSIZE*rint(dr.y/BOXSIZE); } r2=SQR(dr.x)+SQR(dr.y); if(r2<1.0) overlap=TRUE; } } if(overlap==FALSE) { NumberOfAcceptedMoves++; Positions[SelectedParticle].x=PosNew.x; Positions[SelectedParticle].y=PosNew.y; } } if(((j%5)==0)&&(i>NumberOfInitializationCycles)) { Sample(SAMPLE); NumberOfSamples++; } } if((i%5)==0) WritePdb(FilePtr); } if(NumberOfSamples>4) Sample(WRITE_RESULTS); printf("Fraction Succes : %f\n",(double)NumberOfAcceptedMoves/NumberOfAttemptedMoves); fclose(FilePtr); return 0; }
int main(void) { int NumberOfJumps,NumberOfInitializationSteps; int i,j,k,l,dx,dy,xpos_new,ypos_new,xpos,ypos; int LatticeSize; double Sum,Count,Rm; // initialize the random number generator with the system time InitializeRandomNumberGenerator(time(0l)); Sample(INITIALIZE); printf("Number of jumps (x %d) ? ",CycleMultiplication); fscanf(stdin,"%d",&NumberOfJumps); printf("Lattice size ? "); fscanf(stdin,"%d",&LatticeSize); if(LatticeSize<10||LatticeSize>MAX_LATTICE) { printf("Input parameter error, should be\n"); printf("\tLattice size [10-%d]\n",MAX_LATTICE); exit(0); } printf("Number of particles ? "); fscanf(stdin,"%d",&NumberOfParticles); if(NumberOfParticles<1||NumberOfParticles>=MAX_PARTICLES) { printf("Input parameter error, should be\n"); printf("\tNumber of particles [1-%d]\n",MAX_PARTICLES); exit(0); } for(i=0;i<MAX_LATTICE;i++) for(j=0;j<MAX_LATTICE;j++) Lattice[i][j]=EMPTY; for(i=0;i<MAX_PARTICLES;i++) { Particle[i].posx=0; Particle[i].posy=0; Mxx[i]=0; Myy[i]=0; } Sum=0.0; Count=0.0; NumberOfInitializationSteps=NumberOfJumps/4; // put particles on the lattice randomly for(j=0;j<NumberOfParticles;j++) { do { xpos=RandomNumber()*LatticeSize; ypos=RandomNumber()*LatticeSize; } while(Lattice[xpos][ypos]>=0); Lattice[xpos][ypos]=j; Particle[j].posx=xpos; Particle[j].posy=ypos; Mxx[j]=xpos; Myy[j]=ypos; } // perform the random walk for(k=0;k<NumberOfJumps;k++) { for(l=0;l<CycleMultiplication;l++) { // Choose a random site (j) // Choose a random displacement (Rm) j=RandomNumber()*NumberOfParticles; Rm=4.0*RandomNumber(); // MODIFICATION // Question 3: to favour displacement of the diffusion in x direction, // change the cutoffs to 1.5, 3.0, 3.5, respectively // (for example). dx=dy=0; if(Rm<1.0) dx=RIGHT; else if(Rm<2.0) dx=LEFT; else if(Rm<3.0) dy=UP; else dy=DOWN; // new position // MODIFICATION // Question 5: if particle is fixed, don't move it: /* if(j<Nfixed) { xpos_new=Particle[j].posx; ypos_new=Particle[j].posy; } else { */ xpos_new=Particle[j].posx+dx; ypos_new=Particle[j].posy+dy; // } // Don't forget to declare Nfixed. // If the new position is outside of the lattice boundaries, // use periodic boundary conditions: put particle back on the lattice. if(xpos_new<0) xpos_new+=LatticeSize; else if(xpos_new>=LatticeSize) xpos_new-=LatticeSize; if(ypos_new<0) ypos_new+=LatticeSize; else if(ypos_new>=LatticeSize) ypos_new-=LatticeSize; // Check if new lattice site is occupied. // If it is empty, accept the new position. if(Lattice[xpos_new][ypos_new]==EMPTY) { Lattice[Particle[j].posx][Particle[j].posy]=EMPTY; Particle[j].posx=xpos_new; Particle[j].posy=ypos_new; Lattice[xpos_new][ypos_new]=j; Mxx[j]+=dx; Myy[j]+=dy; if(k>NumberOfInitializationSteps) Sum+=1.0; } // MODIFICATION // Question 4: instead of above code, use the following. // The periodic boundary conditions in the y direction have been removed, // and the displacement is only accepted for movements that keep the // particle on the lattice. // /* if(xpos_new<0) xpos_new+=LatticeSize; else if(xpos_new>=LatticeSize) xpos_new-=LatticeSize; if(Lattice[xpos_new][ypos_new]==EMPTY&&ypos_new<LatticeSize&&ypos_new>=0) { Lattice[Particle[j].posx][Particle[j].posy]=EMPTY; Particle[j].posx=xpos_new; Particle[j].posy=ypos_new; Lattice[xpos_new][ypos_new]=j; Mxx[j]+=dx; Myy[j]+=dy; if(k>NumberOfInitializationSteps) Sum+=1.0; } */ if(k>NumberOfInitializationSteps) { Count+=1.0; Sample(SAMPLE_WALK); } } } Sample(WRITE_RESULTS); printf("Fraction Accepted Jumps : %f\n",Sum/Count); printf("Lattice Occupation : %f\n",(double)NumberOfParticles/SQR(LatticeSize)); return 0; }
void ReadInputData(void) { int Ibeg,i; double Boxf,Rhof,Rho; FILE *FilePtr; FilePtr=fopen("input","r"); fscanf(FilePtr,"%*s %*s %*s %*s\n"); fscanf(FilePtr,"%d %d %d %d",&Ibeg,&NumberOfEquilibrationCycles,&NumberOfProductionCycles,&SamplingFrequency); fscanf(FilePtr,"%*s\n"); fscanf(FilePtr,"%lf\n",&MaximumDisplacement); fscanf(FilePtr,"%*s\n"); fscanf(FilePtr,"%d\n",&NumberOfDisplacementsPerCycle); fscanf(FilePtr,"%*s %*s %*s\n"); fscanf(FilePtr,"%d %lf %lf\n",&NumberOfParticles,&Temp,&Rho); fscanf(FilePtr,"%*s %*s %*s %*s\n"); fscanf(FilePtr,"%lf %lf %lf %lf\n",&Epsilon,&Sigma,&Mass,&CutOff); fclose(FilePtr); // initialize the random number generator with the system time InitializeRandomNumberGenerator(time(0l)); if(NumberOfParticles>Npmax) { printf("Error: number of particles too large\n"); exit(1); } Box=pow(NumberOfParticles/Rho,1.0/3.0); // read/generate configuration if(Ibeg==0) { // generate configuration from lattice Lattice(); } else { printf(" Read confs from disk\n"); FilePtr=fopen("restart.dat","r"); fscanf(FilePtr,"%lf\n",&Boxf); fscanf(FilePtr,"%d\n",&NumberOfParticles); fscanf(FilePtr,"%lf\n",&MaximumDisplacement); Rhof=NumberOfParticles/CUBE(Boxf); if(fabs(Boxf-Box)>1.0e-6) printf("%lf %lf\n",Rho,Rhof); for(i=0;i<NumberOfParticles;i++) { fscanf(FilePtr,"%lf %lf %lf\n",&Positions[i].x,&Positions[i].y,&Positions[i].z); Positions[i].x*=Box/Boxf; Positions[i].y*=Box/Boxf; Positions[i].z*=Box/Boxf; } fclose(FilePtr); } // write input data printf(" Number Of Equilibration Cycles : %d\n",NumberOfEquilibrationCycles); printf(" Number Of Production Cycles : %d\n",NumberOfProductionCycles); printf(" Sample Frequency : %d\n",SamplingFrequency); printf(" Number Of Att. To Displ. A Part. Per Cycle : %d\n",NumberOfDisplacementsPerCycle); printf(" Maximum Displacement : %lf\n",MaximumDisplacement); printf(" Number Of Particles : %d\n",NumberOfParticles); printf(" Temperature : %lf\n",Temp); printf(" Density : %lf\n",Rho); printf(" Box Length : %lf\n",Box); printf(" Model Parameters\n"); printf(" Sigma : %lf\n",Sigma); printf(" Epsilon : %lf\n",Epsilon); printf(" Mass : %lf\n",Mass); printf(" CutOff : %lf\n",CutOff); // calculate parameters: Beta=1.0/Temp; // calculate Cut-off radius potential CutOff=MIN(CutOff,0.5*Box); }