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)");
39 puts("-d <value> \t refresh display rate");
40 puts("-t <value> maximal temperature");
45 int main(int argc, char **argv)
55 int max_x,x_c,max_y,y_c;
78 strcpy(output_file,"");
88 max_x=atoi(argv[++i]);
91 max_y=atoi(argv[++i]);
100 strcpy(output_file,argv[++i]);
109 max_T=atof(argv[++i]);
118 /* prepare lattice */
119 d2_lattice_init(&argc,argv,&d2_l,max_x,max_y);
120 atom=(unsigned char *)(malloc(max_x*max_y*sizeof(unsigned char)));
123 if(strcmp(output_file,""))
125 of_fd=open(output_file,O_WRONLY|O_CREAT);
128 puts("can't open output file ...");
133 /* begin at T=0 M=1 situation */
134 memset(atom,0,max_x*max_y*sizeof(unsigned char));
136 if(max_T==0) max_T=3.0*s;
138 for(T=.05;T<max_T;T+=.05)
140 beta=1.0/T; /* k_B = 1 */
145 for(x_c=0;x_c<max_x;x_c++)
147 for(y_c=0;y_c<max_y;y_c++)
150 if((*(atom+x_c+((y_c+max_y+1)%max_y)*max_x))&1) ++count_p;
151 if((*(atom+x_c+((y_c+max_y-1)%max_y)*max_x))&1) ++count_p;
152 if((*(atom+((max_x+x_c+1)%max_x)+y_c*max_x))&1) ++count_p;
153 if((*(atom+((max_x+x_c-1)%max_x)+y_c*max_x))&1) ++count_p;
154 if(((*(atom+x_c+y_c*max_x))&1)==0) count_p=4-count_p;
155 delta_e=(2*count_p-4)*s;
156 if(delta_e<=0) *(atom+x_c+y_c*max_x)=(*(atom+x_c+y_c*max_x)+1)&1;
159 if(1.0*rand()/RAND_MAX<exp(-1.0*delta_e*beta))
160 *(atom+x_c+y_c*max_x)=(*(atom+x_c+y_c*max_x)+1)&1;
162 if((*(atom+x_c+y_c*max_x))&1) ++M;
168 sprintf(t_text," temp: %.3f",T);
170 sprintf(b_text," beta: %.3f",beta);
173 sprintf(s_text," interaction strength: %.3f",s);
176 sprintf(m_text," magnetization: %.3f",1.0-2.0*M/(max_x*max_y));
178 d2_lattice_draw(&d2_l,0,0,6,arg_v);
182 if(of_fd) dprintf(of_fd,"%f %f\n",T,1.0-2.0*M/(max_x*max_y));
185 for(T=max_T;T>0;T-=.05)
187 beta=1.0/T; /* k_B = 1 */
192 for(x_c=0;x_c<max_x;x_c++)
194 for(y_c=0;y_c<max_y;y_c++)
197 if((*(atom+x_c+((y_c+max_y+1)%max_y)*max_x))&1) ++count_p;
198 if((*(atom+x_c+((y_c+max_y-1)%max_y)*max_x))&1) ++count_p;
199 if((*(atom+((max_x+x_c+1)%max_x)+y_c*max_x))&1) ++count_p;
200 if((*(atom+((max_x+x_c-1)%max_x)+y_c*max_x))&1) ++count_p;
201 if(((*(atom+x_c+y_c*max_x))&1)==0) count_p=4-count_p;
202 delta_e=(2*count_p-4)*s;
203 if(delta_e<0) *(atom+x_c+y_c*max_x)=(*(atom+x_c+y_c*max_x)+1)&1;
206 if(1.0*rand()/RAND_MAX<exp(-1.0*delta_e*beta))
207 *(atom+x_c+y_c*max_x)=(*(atom+x_c+y_c*max_x)+1)&1;
209 if((*(atom+x_c+y_c*max_x))&1) ++M;
215 sprintf(t_text," temp = %.3f",T);
217 sprintf(b_text," beta = %.3f",beta);
220 sprintf(s_text," interaction strength: %.3f",s);
223 sprintf(m_text," magnetization: %.3f",1.0-2.0*M/(max_x*max_y));
225 d2_lattice_draw(&d2_l,0,0,6,arg_v);
229 if(of_fd) dprintf(of_fd,"%f %f\n",T,1.0-2.0*M/(max_x*max_y));
232 if(of_fd) close(of_fd);