Docsity

Ising4-Computational Physics-Codes, Exercises of Computational Physics

Toggle sidebar
bg1
clear all;
n =10; nnn = 1; nbin = 200;
T =2.; beta = 1/T; JJ = -1.0; nmc = 1000;
rand('state', 0)
%%%% generate random spins for the first time
for ip= 2: n+1
for jp= 2: n+1
rr = 2.*rand - 1.0 ;
s(ip,jp) = sign(rr);
s(ip,jp) = 1;
end
end
% fix the boundary conditions as periodic
for ja=2: n+1
s(1,ja) = s(n+1,ja); s(n+2,ja) = s(2, ja); end
for ia=2: n+1
s(ia,1)= s(ia,n+1); s(ia,n+2) = s(ia,2); end
for kkk=1: nmc % loop over changes in spins
sum_en = 0.0; sum_mg = 0.0;
for j = 2: n+1 % loop over all mesh points
for i = 2: n+1 % loop over all mesh points
% Start: Metropolis Algorithm for spin flip -----
en_initial = JJ*s(i,j)*(s(i+1,j)+s(i-1,j)+s(i,j+1)+s(i,j-1));
s_old = s(i,j); % save this for possiblereverting
if(s(i,j)== +1) snew = -1; end
if(s(i,j)== -1) snew = +1; end
s(i,j) = snew; %(spin flip has been done)
% refresh the boundary values for the flip
for jj=2: n+1
s(1,jj) = s(n+1,jj); s(n+2,jj) = s(2, jj); end
for ii=2: n+1
s(ii,1) = s(ii,n+1); s(ii,n+2) = s(ii,2); end
en_final = JJ*s(i,j)*(s(i+1,j)+s(i-1,j)+s(i,j+1)+s(i,j-1));
delta_e = en_final - en_initial;
if(delta_e<=0) s(i,j) = snew; % keep the flip
%'spin is flipped; as delta_e<=0 '
end
if(delta_e>0)
rr = rand;
if(rr<= exp(-beta*delta_e))
s(i,j)= snew; % again keep the flip
%'spin is flipped as delta_e>0'
elseif(rr>exp(-beta*delta_e))
s(i,j) = s_old; % remove the flip
%'spin is not flipped';
pf2

Partial preview of the text

Download Ising4-Computational Physics-Codes and more Exercises Computational Physics in PDF only on Docsity!

clear all; n =10; nnn = 1; nbin = 200; T =2.; beta = 1/T; JJ = -1.0; nmc = 1000; rand('state', 0) %%%% generate random spins for the first time for ip= 2: n+ for jp= 2: n+ rr = 2.rand - 1.0 ; s(ip,jp) = sign(rr); s(ip,jp) = 1; end end % fix the boundary conditions as periodic for ja=2: n+ s(1,ja) = s(n+1,ja); s(n+2,ja) = s(2, ja); end for ia=2: n+ s(ia,1)= s(ia,n+1); s(ia,n+2) = s(ia,2); end for kkk=1: nmc % loop over changes in spins sum_en = 0.0; sum_mg = 0.0; for j = 2: n+1 % loop over all mesh points for i = 2: n+1 % loop over all mesh points % Start: Metropolis Algorithm for spin flip ----- en_initial = JJs(i,j)(s(i+1,j)+s(i-1,j)+s(i,j+1)+s(i,j-1)); s_old = s(i,j); % save this for possiblereverting if(s(i,j)== +1) snew = -1; end if(s(i,j)== -1) snew = +1; end s(i,j) = snew; %(spin flip has been done) % refresh the boundary values for the flip for jj=2: n+ s(1,jj) = s(n+1,jj); s(n+2,jj) = s(2, jj); end for ii=2: n+ s(ii,1) = s(ii,n+1); s(ii,n+2) = s(ii,2); end en_final = JJs(i,j)(s(i+1,j)+s(i-1,j)+s(i,j+1)+s(i,j-1)); delta_e = en_final - en_initial; if(delta_e<=0) s(i,j) = snew; % keep the flip %'spin is flipped; as delta_e<=0 ' end if(delta_e>0) rr = rand; if(rr<= exp(-betadelta_e)) s(i,j)= snew; % again keep the flip %'spin is flipped as delta_e>0' elseif(rr>exp(-beta*delta_e)) s(i,j) = s_old; % remove the flip %'spin is not flipped';

end end % refresh the boundary values for the flip for jk=2: n+ s(1,jk) = s(n+1,jk); s(n+2,jk) = s(2, jk); end for ik=2: n+ s(ik,1) = s(ik,n+1); s(ik,n+2) = s(ik,2); end % end of the Matropolis algo .................... end % end of i-loop end % end of j-loop % calculate energy for each mesh point (or atom) for jy=2:n+ for ix=2:n+ e(ix,jy) = 0.5JJs(ix,jy)(s(ix+1,jy)+s(ix-1,jy)... +s(ix,jy+1)+s(ix,jy-1)); % Calculate the total energy and total magnetization sum_en = sum_en + e(ix,jy); sum_mg = sum_mg + s(ix,jy); end end mc_step(kkk) = kkk; energy(kkk) = sum_en/(nn); % energy per atom magn(kkk) = (sum_mg)/(n*n); % magnitization per atom %kkk axis([0 1000 -1.1 1.1]) plot(mc_step, magn,'-r') hold on F(kkk) = getframe; kkk end % end of kkk loop