dd538b34bd51424fb2a19da036dd0885ef599574
[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 "dfbapi.h"
15
16 #define X 100
17 #define Y 100
18 #define MAX_T 200
19
20 int main(int argc, char **argv)
21 {
22  unsigned char *atom;
23  char *arg_v[4];
24  char m_text[30];
25  char t_text[30];
26  char b_text[30];
27  int max_x,x_c,max_y,y_c;
28  int i;
29  int count_p;
30  unsigned int T;
31  double S;
32  int M;
33  double beta;
34  double delta_e;
35
36  /* display stuff */
37  d2_lattice d2_l;
38
39  /* we will parse argv later ... */
40  max_x=X;
41  max_y=Y;
42
43  d2_lattice_init(&argc,argv,&d2_l,max_x,max_y);
44
45  atom=(unsigned char *)(malloc(max_x*max_y*sizeof(unsigned char)));
46  
47  d2_l.status=atom;
48  
49  /* begin at T=0 M=1 situation */
50  memset(atom,0,max_x*max_y*sizeof(unsigned char));
51
52  S=1; /* i have no idea! */
53  
54  for(T=1;T<MAX_T;T++)
55  {
56   beta=1.0/T; /* k_B = 1 */
57   /* do 100 itterations, we will need more */
58   for(i=0;i<100;i++)
59   {
60
61  for(x_c=0;x_c<max_x;x_c++)
62  {
63   for(y_c=0;y_c<max_y;y_c++)
64   {
65    count_p=0;
66    M=0;
67    if((*(atom+x_c+((2*y_c+1)%max_y)*max_x))&1) ++count_p;
68    if((*(atom+x_c+((2*y_c-1)%max_y)*max_x))&1) ++count_p;
69    if((*(atom+((2*x_c+1)%max_x)+y_c*max_x))&1) ++count_p;
70    if((*(atom+((2*x_c-1)%max_x)+y_c*max_x))&1) ++count_p;
71    if(((*(atom+x_c+y_c*max_x))&1)==0) count_p=4-count_p;
72    delta_e=(4-2*count_p)*S;
73    if(delta_e<0) *(atom+x_c+y_c*max_x)=(*(atom+x_c+y_c*max_x)+1)&1;
74    else
75    {
76     if(1.0*rand()/RAND_MAX<exp(-1.0*delta_e*beta))
77      *(atom+x_c+y_c*max_x)=(*(atom+x_c+y_c*max_x)+1)&1;
78     else M+=1;
79    }
80   }
81  }
82   sprintf(t_text,"T = %d",T);
83   arg_v[1]=t_text;
84   sprintf(b_text,"b = %f",beta);
85   arg_v[2]=b_text;
86   sprintf(m_text,"M = %f",1.0-2*M/(max_x*max_y));
87   arg_v[3]=m_text;
88   d2_lattice_draw(&d2_l,0,0,3,arg_v);
89   }
90  }
91
92  getchar();
93
94  return 1;
95 }