-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
219 lines (97 loc) · 2.66 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
const int N=100;
const float J=1;
const float T=2;
unsigned long genrand_int32(); // declaring the mersene-twister functions
double genrand_real1(void);
void init_genrand(unsigned long );
/******magnetization********/
long double mag(int present[N][N])
{
long double mag=0;
for(int i=0;i<N;i++)
{ for(int j=0;j<N;j++)
{
mag=mag+present[i][j];
}
}
return(mag/(N*N));
}
//energy difference for each prespective spin flip
double deltaE(int lattice[N][N],int i,int j)
{
return( 2*J*lattice[i%N][j%N]*( lattice[i%N][(j+1)%N]+lattice[i%N][(j-1)%N]+lattice[(i+1)%N][j%N]+lattice[(i-1)%N][j%N] ) );
}
/**********evolve**********/
void evolve(int present[N][N])
{
long unsigned int i,j;
double ediff;
int counter;
long unsigned int k=1;
//long double kb=1.38*pow(10,-23);
int n=N-1;
long unsigned int max,rand,m;
max=4294967295;
long double kb=1;
long double prob;
//unsigned long s;
time_t seconds;
//FILE *file;
//file = fopen ("data450_T=1.txt","w");
while(k>0)
{
seconds = time(NULL); //seeding mtwister
//seconds=seconds+k;
init_genrand(seconds+k);
m=max%(n+1);
do{rand=genrand_int32(); } //casting the random no genearator to a range ,naive and wrong implementation ,needs refinement
while(rand>=max-m);
i=rand%(n+1);
do{rand=genrand_int32();}
while(rand>=max-m);
j=rand%(n+1);
ediff=deltaE( present,i,j);
//double r= genrand_real1(); //random real number in [0,1]
//prob=expl(-(Ediff)/(kb*T));
if( ediff < 0 || genrand_real1() < expl(-ediff/T))
{ present[i][j]=-present[i][j];}
//fprintf (file, "spins flipped= %lu\tmagnetization=%Lf\n",k,magnetization);
//printf("\n");
printf("\033[H\033[J"); //clearing the screen
for(i=0;i<N;i++) //printing the matrix
{ counter=0;
for(j=0;j<N;j++)
{if(present[i][j]==1) printf("\033[07m \033[m");
else printf(" "); //printf("\t%d",next[i][j]);
counter++;
if (counter==N){ printf("\n");} // printf("\n");printf("\n");}
}
}
//printf("i=%lu\tj=%lu\tgeneration=%lu\tEdiff=%Lf\tmagnetization=%Lf",i,j,k,Ediff,magnetization);
//count++;
k++;
usleep(9100);
}
//fclose(file);
}
int main()
{
time_t seconds;
seconds = time(NULL); //seeding mtwister
init_genrand(seconds);
int RANDOM[N][N];
int i,j;
for(i=0;i<N;i++)
{for(j=0;j<N;j++)
{ unsigned long ran=genrand_int32();
if(ran%2==0) RANDOM[i][j]=1;
else RANDOM[i][j]=-1;
}
}
//printf("%Lf",energy(RANDOM));
evolve(RANDOM);
}