fixed delta e bug
[physik/ising.git] / ising.c
1 /*
2  * ising.c - visualization of ising spins in an N x N lattice
3  *
4  * ... actually N x M
5  *
6  * author: hackbard@hackdaworld.dyndns.org
7  *
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <math.h>
14 #include <time.h>
15 #include "dfbapi.h"
16
17 #define X 200
18 #define Y 200
19 #define MAX_T 100
20
21 int main(int argc, char **argv)
22 {
23  unsigned char *atom;
24  char *arg_v[4];
25  char m_text[30];
26  char t_text[30];
27  char b_text[30];
28  int max_x,x_c,max_y,y_c;
29  int i;
30  int count_p;
31  unsigned int T;
32  double S;
33  int M;
34  double beta;
35  double delta_e;
36
37  /* random stuff*/
38  srand(time(0));
39
40  /* display stuff */
41  d2_lattice d2_l;
42
43  /* we will parse argv later ... */
44  max_x=X;
45  max_y=Y;
46
47  d2_lattice_init(&argc,argv,&d2_l,max_x,max_y);
48
49  atom=(unsigned char *)(malloc(max_x*max_y*sizeof(unsigned char)));
50  
51  d2_l.status=atom;
52  
53  /* begin at T=0 M=1 situation */
54  memset(atom,0,max_x*max_y*sizeof(unsigned char));
55
56  S=1; /* i have no idea! */
57  
58  for(T=1;T<MAX_T;T++)
59  {
60   beta=20.0/T; /* k_B = 1 */
61   /* do 10 itterations, we will need more */
62   for(i=0;i<10;i++)
63   {
64
65  for(x_c=0;x_c<max_x;x_c++)
66  {
67   for(y_c=0;y_c<max_y;y_c++)
68   {
69    count_p=0;
70    M=0;
71    if((*(atom+x_c+((2*y_c+1)%max_y)*max_x))&1) ++count_p;
72    if((*(atom+x_c+((2*y_c-1)%max_y)*max_x))&1) ++count_p;
73    if((*(atom+((2*x_c+1)%max_x)+y_c*max_x))&1) ++count_p;
74    if((*(atom+((2*x_c-1)%max_x)+y_c*max_x))&1) ++count_p;
75    if(((*(atom+x_c+y_c*max_x))&1)==0) count_p=4-count_p;
76    delta_e=(2*count_p-4)*S;
77    if(delta_e<0) *(atom+x_c+y_c*max_x)=(*(atom+x_c+y_c*max_x)+1)&1;
78    else
79    {
80     if(1.0*rand()/RAND_MAX<exp(-1.0*delta_e*beta))
81      *(atom+x_c+y_c*max_x)=(*(atom+x_c+y_c*max_x)+1)&1;
82    }
83    if((*(atom+x_c+((2*y_c+1)%max_y)*max_x))&1) ++M;
84   }
85  }
86   sprintf(t_text,"T = %d",T);
87   arg_v[1]=t_text;
88   sprintf(b_text,"b = %f",beta);
89   arg_v[2]=b_text;
90   sprintf(m_text,"M = %f",1.0-2.0*M/(max_x*max_y));
91   arg_v[3]=m_text;
92   d2_lattice_draw(&d2_l,0,0,3,arg_v);
93   }
94  }
95
96  getchar();
97
98  return 1;
99 }