Hey folks,
I'm very new to all this and I'm struggling to understand why my programme keeps printing the same array twice when I've got a section between that changes the array values. Please could I ask you all not to give me any code just point out what I've done wrong.
The premise is using an array to simulate a set of spins or magnetons, and to investigate using the metropolis monte carlo method how spins change at ceratin temperatures, whether they spontaneously align or continue to flip randomly, known as spontaneous magnetisation. Note, the temp is in J/kB so below an input value of 2.269 the array should spontaneously align and above this input value it should continue to flip randomly.
Many Thanks in advance, if more info is needed just yell.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
FILE *outputfile;
int i, j, N=8, spins[N][N], north_spin, south_spin, east_spin, west_spin, iterations=1000*(N*N); //Define counting variables and spin storage array.
double rand_num_between_0_100, i_selector, j_selector, delta_energy, Temp, R, changed_spin_state; //Define variables for spin selection and generation.
outputfile=fopen("/home/sam/Documents/SpinOutputData.txt","w"); //Open output file for write.
if(outputfile==NULL) //Fail to open message.
{
printf("+++THERE WAS AN ERROR IN OPENING THE WRITE FILE+++");
}
srand(time(NULL)); //Seed random number using clock.
fprintf(outputfile,"\n\t\t\t+++HERE FOLLOWS THE SPIN ARRAY+++\n"); //Inform user of programme function and how to read display
fprintf(outputfile,"\n\t\t+++THIS MICROSTATE MATRIX IS SURROUNDED BY OTHER MICROSTATES+++\n");
fprintf(outputfile,"\n\t\t+++NORTH IS UPWARDS IN THE PLANE OF YOUR DISPLAY+++\n");
fprintf(outputfile,"\n\t\t+++THE BRACKETED VALUES DENOTE ROWS AND COLUMNS+++\n");
fprintf(outputfile,"\n\t\t+++THE FINAL SET OF BRACKETED NUMBERS ARE THE ROWS+++\n\n");
for(i = 0; i<N; i++) //Initialise Spins Array.
{
for(j = 0; j<N; j++)
{
spins[i][j] = 0;
}
}
for(i = 0; i<N; i++) //Randomise spins and store on array.
{
for(j = 0; j<N; j++)
{
rand_num_between_0_100 = (rand() % 100)+1; //Define random number between 1 and 100
if(rand_num_between_0_100<50)
{
spins[i][j] = 1;
}
else
{
spins[i][j] = -1;
}
}
}
for(i = 0; i<N; i++) //Display Spins Array.
{
for(j = 0; j<N; j++)
{
fprintf(outputfile,"\t[%d] %d", j, spins[i][j]);
}
fprintf(outputfile,"\t[%d]\n", i);
}
printf("\n\t+++PLEASE ENTER THE TEMPERATURE, IN J/BoltzmannConstant, THAT YOU WISH TO RUN THE SIMULATON AT:");
scanf("%d", &Temp); //Prompt and scan for input.
fprintf(outputfile,"\n\n");
for(i=0; i<iterations; i++) //Find random spins perform energy comp. and write to file.
{
i_selector = N*(rand()-1)/RAND_MAX+1; //Select random i point in array.
j_selector = N*(rand()-1)/RAND_MAX+1; //Select random j point in array.
if(i_selector==0) //Define spin above selected spin.
{
north_spin= spins[(int)(i_selector+7)][(int)j_selector]; //This point lies outisde of array therfore equal to other edge.
}
else
{
north_spin = spins[(int)(i_selector-1)][(int)j_selector];
}
if(i_selector==7) //Define spin below selected spin.
{
south_spin=spins[(int)(i_selector-7)][(int)j_selector]; //This point lies outisde of array therfore equal to other edge.
}
else
{
south_spin = spins[(int)(i_selector+1)][(int)j_selector];
}
if(j_selector==0) //Define spin west selected spin.
{
west_spin=spins[(int)i_selector][(int)(j_selector+7)]; //This point lies outisde of array therfore equal to other edge.
}
else
{
west_spin = spins[(int)i_selector][(int)(j_selector-1)];
}
if(j_selector==7) //Define spin east selected spin.
{
east_spin=spins[(int)i_selector][(int)(j_selector-7)]; //This point lies outisde of array therfore equal to other edge.
}
else
{
east_spin = spins[(int)i_selector][(int)(j_selector+1)];
}
delta_energy=-(((spins[(int)(i_selector)][(int)(j_selector)]*north_spin)+
(spins[(int)(i_selector)][(int)(j_selector)]*south_spin)+
(spins[(int)(i_selector)][(int)(j_selector)]*west_spin)+
(spins[(int)(i_selector)][(int)(j_selector)]*east_spin))-
((-spins[(int)(i_selector)][(int)(j_selector)]*north_spin)+
(-spins[(int)(i_selector)][(int)(j_selector)]*south_spin)+
(-spins[(int)(i_selector)][(int)(j_selector)]*west_spin)+
(-spins[(int)(i_selector)][(int)(j_selector)]*east_spin))); //Calculate Energy Change
R = rand()/RAND_MAX; //Define variable for spin change-up
if((delta_energy<0)||(R<=exp(-delta_energy/Temp))) //Change spin state if delEnergy is less than 0
{
spins[(int)i_selector][(int)j_selector]=-spins[(int)i_selector][(int)j_selector];
}
}
for(i = 0; i<N; i++) //Display New Spins Array.
{
for(j = 0; j<N; j++)
{
fprintf(outputfile,"\t[%d] %d", j, spins[i][j]);
}
fprintf(outputfile,"\t[%d]\n", i);
}
printf("\n\n+++JOB DONE+++\n\n"); //Notify user process is complete.
return 0;
}