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");
44 int main(int argc, char **argv)
54 int max_x,x_c,max_y,y_c;
77 strcpy(output_file,"");
86 max_x=atoi(argv[++i]);
89 max_y=atoi(argv[++i]);
98 strcpy(output_file,argv[++i]);
113 /* prepare lattice */
114 d2_lattice_init(&argc,argv,&d2_l,max_x,max_y);
115 atom=(unsigned char *)(malloc(max_x*max_y*sizeof(unsigned char)));
118 if(strcmp(output_file,""))
120 of_fd=open(output_file,O_WRONLY|O_CREAT);
123 puts("can't open output file ...");
128 /* begin at T=0 M=1 situation */
129 memset(atom,0,max_x*max_y*sizeof(unsigned char));
133 for(T=.05;T<max_T;T+=.05)
135 beta=1.0/T; /* k_B = 1 */
140 for(x_c=0;x_c<max_x;x_c++)
142 for(y_c=0;y_c<max_y;y_c++)
145 if((*(atom+x_c+((y_c+max_y+1)%max_y)*max_x))&1) ++count_p;
146 if((*(atom+x_c+((y_c+max_y-1)%max_y)*max_x))&1) ++count_p;
147 if((*(atom+((max_x+x_c+1)%max_x)+y_c*max_x))&1) ++count_p;
148 if((*(atom+((max_x+x_c-1)%max_x)+y_c*max_x))&1) ++count_p;
149 if(((*(atom+x_c+y_c*max_x))&1)==0) count_p=4-count_p;
150 delta_e=(2*count_p-4)*s;
151 if(delta_e<0) *(atom+x_c+y_c*max_x)=(*(atom+x_c+y_c*max_x)+1)&1;
154 if(1.0*rand()/RAND_MAX<exp(-1.0*delta_e*beta))
155 *(atom+x_c+y_c*max_x)=(*(atom+x_c+y_c*max_x)+1)&1;
157 if((*(atom+x_c+y_c*max_x))&1) ++M;
163 sprintf(t_text," temp: %.3f",T);
165 sprintf(b_text," beta: %.3f",beta);
168 sprintf(s_text," interaction strength: %.3f",s);
171 sprintf(m_text," magnetization: %.3f",1.0-2.0*M/(max_x*max_y));
173 d2_lattice_draw(&d2_l,0,0,6,arg_v);
177 if(of_fd) dprintf(of_fd,"%f %f\n",T,1.0-2.0*M/(max_x*max_y));
180 for(T=max_T;T>0;T-=.05)
182 beta=1.0/T; /* k_B = 1 */
187 for(x_c=0;x_c<max_x;x_c++)
189 for(y_c=0;y_c<max_y;y_c++)
192 if((*(atom+x_c+((y_c+max_y+1)%max_y)*max_x))&1) ++count_p;
193 if((*(atom+x_c+((y_c+max_y-1)%max_y)*max_x))&1) ++count_p;
194 if((*(atom+((max_x+x_c+1)%max_x)+y_c*max_x))&1) ++count_p;
195 if((*(atom+((max_x+x_c-1)%max_x)+y_c*max_x))&1) ++count_p;
196 if(((*(atom+x_c+y_c*max_x))&1)==0) count_p=4-count_p;
197 delta_e=(2*count_p-4)*s;
198 if(delta_e<0) *(atom+x_c+y_c*max_x)=(*(atom+x_c+y_c*max_x)+1)&1;
201 if(1.0*rand()/RAND_MAX<exp(-1.0*delta_e*beta))
202 *(atom+x_c+y_c*max_x)=(*(atom+x_c+y_c*max_x)+1)&1;
204 if((*(atom+x_c+y_c*max_x))&1) ++M;
210 sprintf(t_text," temp = %.3f",T);
212 sprintf(b_text," beta = %.3f",beta);
215 sprintf(s_text," interaction strength: %.3f",s);
218 sprintf(m_text," magnetization: %.3f",1.0-2.0*M/(max_x*max_y));
220 d2_lattice_draw(&d2_l,0,0,6,arg_v);
224 if(of_fd) dprintf(of_fd,"%f %f\n",T,1.0-2.0*M/(max_x*max_y));
227 if(of_fd) close(of_fd);