2 * ising.c - visualization of ising spins in an N x N lattice
6 * author: hackbard@hackdaworld.dyndns.org
16 #include <sys/types.h>
32 puts("-h \t show this help");
33 puts("-o <file> \t save T - M values to file");
34 puts("-i <value> \t specify itteration value");
35 puts("-x <value> \t # x lattice sites");
36 puts("-y <value> \t # y lattice sites");
37 puts("-s <value> \t spin interaction strength");
38 puts("-r \t run in interactive mode (still in work)");
43 int main(int argc, char **argv)
53 int max_x,x_c,max_y,y_c;
75 strcpy(output_file,"");
84 max_x=atoi(argv[++i]);
87 max_y=atoi(argv[++i]);
96 strcpy(output_file,argv[++i]);
108 /* prepare lattice */
109 d2_lattice_init(&argc,argv,&d2_l,max_x,max_y);
110 atom=(unsigned char *)(malloc(max_x*max_y*sizeof(unsigned char)));
113 if(strcmp(output_file,""))
115 of_fd=open(output_file,O_WRONLY|O_CREAT);
118 puts("can't open output file ...");
123 /* begin at T=0 M=1 situation */
124 memset(atom,0,max_x*max_y*sizeof(unsigned char));
128 for(T=.05;T<max_T;T+=.05)
130 beta=1.0/T; /* k_B = 1 */
135 for(x_c=0;x_c<max_x;x_c++)
137 for(y_c=0;y_c<max_y;y_c++)
140 if((*(atom+x_c+((y_c+max_y+1)%max_y)*max_x))&1) ++count_p;
141 if((*(atom+x_c+((y_c+max_y-1)%max_y)*max_x))&1) ++count_p;
142 if((*(atom+((max_x+x_c+1)%max_x)+y_c*max_x))&1) ++count_p;
143 if((*(atom+((max_x+x_c-1)%max_x)+y_c*max_x))&1) ++count_p;
144 if(((*(atom+x_c+y_c*max_x))&1)==0) count_p=4-count_p;
145 delta_e=(2*count_p-4)*s;
146 if(delta_e<0) *(atom+x_c+y_c*max_x)=(*(atom+x_c+y_c*max_x)+1)&1;
149 if(1.0*rand()/RAND_MAX<exp(-1.0*delta_e*beta))
150 *(atom+x_c+y_c*max_x)=(*(atom+x_c+y_c*max_x)+1)&1;
152 if((*(atom+x_c+y_c*max_x))&1) ++M;
156 sprintf(t_text," temp: %.3f",T);
158 sprintf(b_text," beta: %.3f",beta);
161 sprintf(s_text," interaction strength: %.3f",s);
164 sprintf(m_text," magnetization: %.3f",1.0-2.0*M/(max_x*max_y));
166 d2_lattice_draw(&d2_l,0,0,6,arg_v);
169 if(of_fd) dprintf(of_fd,"%f %f\n",T,1.0-2.0*M/(max_x*max_y));
172 for(T=max_T;T>0;T-=.05)
174 beta=1.0/T; /* k_B = 1 */
179 for(x_c=0;x_c<max_x;x_c++)
181 for(y_c=0;y_c<max_y;y_c++)
184 if((*(atom+x_c+((y_c+max_y+1)%max_y)*max_x))&1) ++count_p;
185 if((*(atom+x_c+((y_c+max_y-1)%max_y)*max_x))&1) ++count_p;
186 if((*(atom+((max_x+x_c+1)%max_x)+y_c*max_x))&1) ++count_p;
187 if((*(atom+((max_x+x_c-1)%max_x)+y_c*max_x))&1) ++count_p;
188 if(((*(atom+x_c+y_c*max_x))&1)==0) count_p=4-count_p;
189 delta_e=(2*count_p-4)*s;
190 if(delta_e<0) *(atom+x_c+y_c*max_x)=(*(atom+x_c+y_c*max_x)+1)&1;
193 if(1.0*rand()/RAND_MAX<exp(-1.0*delta_e*beta))
194 *(atom+x_c+y_c*max_x)=(*(atom+x_c+y_c*max_x)+1)&1;
196 if((*(atom+x_c+y_c*max_x))&1) ++M;
200 sprintf(t_text," temp = %.3f",T);
202 sprintf(b_text," beta = %.3f",beta);
205 sprintf(s_text," interaction strength: %.3f",s);
208 sprintf(m_text," magnetization: %.3f",1.0-2.0*M/(max_x*max_y));
210 d2_lattice_draw(&d2_l,0,0,6,arg_v);
213 if(of_fd) dprintf(of_fd,"%f %f\n",T,1.0-2.0*M/(max_x*max_y));
216 if(of_fd) close(of_fd);