understand a code to simulate ising model in 2D using montecarlo method

9 Ansichten (letzte 30 Tage)
Julia Dulie
Julia Dulie am 25 Jun. 2020
Kommentiert: Alan Stevens am 25 Jun. 2020
Hello everyone,
I found this code in Russian book , to simulate ising model in 2D using montecarlo method, but Franky I don't understand the 22 lines , alhtough it gives 2d square grid with a vector in each site, would anyone have an idea about this code ? I will be highly grateful for any remarks or feedback
Thanks in advance
with best
-------------------------- The code is :
clear all
close all
Nspin=49; % number of spins. Must be square of integer
J=2; % constant of interaction
h=0; % external magnetic field
Esi=10; % energy of system (microcanonic distribution)
NTrial=1000; % number of trials
[Es,Ed,SpM,A,S]=Ising2(Nspin,J,h,Esi,NTrial);
% plot of energy of system
% plot of energy of daemon
[~,m]=size(Ed); % [~,m], that means that you just want the second output of your function, and do not care the first one.
% is equal to Nspin*Ntrial and considered as time
i=1:m;
figure(1);plot(i,Es(i))
figure(2);plot(i,Ed(i))
%%
% plot of spin coniguration
m1=floor(m/3);
m2=2*m1;
m3=3*m1;
i=1:sqrt(Nspin);
j=1:sqrt(Nspin);
V(i,j)=0;
U(i,j)=0;
Z(i,j)=0;
figure
subplot(4,1,1);quiver3(Z,U,V,S(:,:,1)); colormap white;
title('Spins initial configuration')
subplot(4,1,2);quiver3(Z,U,V,S(:,:,m1)); colormap white
title(['Spins configuration for time ', num2str(m1)])
subplot(4,1,3); quiver3(Z,U,V,S(:,:,m2)); colormap white
title(['Spins configuration for time ', num2str(m2)])
subplot(1,1,1); quiver3(Z,U,V,S(:,:,m)); colormap white
title(['Spins configuration for time ', num2str(m)])
mean(Es) % mean energy of system
----------------
Ising2.m
function [Es,Ed,SpM,A,S] = Ising2(Nspin,J,h,Esi,NTrial)
% input parameters are described in square_grid1.m
% output parameters
% Es is instantaneous energy of system after each step of walk in each trial
%SpM,A,S are spin configuration
Ns=Nspin.^0.5; % number of spin for one side of square
s=ones(Ns,Ns); % initial spin configuration. All spin projections are 1
Esystem=-(J+h)*Nspin; % initial system energy
Edemon=4*J*floor((Esi-Esystem)/(4*J)); % initial energy of daemon.
% on each trial daemon energy is compared with energy of all spin knots of grid
% and energy of knot and daemon is changed not changed according some
% condition
Es(1)=Esystem;
Ed(1)=Edemon;
S=s;
k=1;
for i=1:NTrial
Accept=0;
for j=1:Nspin
% random choice of knot
Ix=floor(Ns*rand(1)+1);
Iy=floor(Ns*rand(1)+1);
% border conditions
if Ix==1
Left=Ns;
else
Left=Ix-1;
end;
if Ix==Ns
Right=1;
else;
Right=Ix+1;
end;
if Iy==1
Down=Ns;
else;
Down=Iy-1;
end;
if Iy==Ns
Up=1;
else
Up=Iy+1;
end;
% energy change
de=2*s(Iy,Ix)*(-h+J*(s(Iy,Left)+s(Iy,Right)+s(Down,Ix)+s(Up,Ix)));
if de<=Edemon % energy change accepted
s(Iy,Ix)=-s(Iy,Ix);
Accept=Accept+1;
Edemon=Edemon-de;
Esystem=Esystem+de;
end;
k=k+1;
Es(k)=Esystem;
Ed(k)=Edemon;
A(k-1)=Accept;
s1=sum(s);
SpM(k)=sum(s1);
S=cat(3,S,s);
end;
end;
A=A/NTrial;
  7 Kommentare
Julia Dulie
Julia Dulie am 25 Jun. 2020
The code is from this book, starting from page 502 , the code is mentioned explicity in page 521
By the way , would you -if that is possible- reccomand some books (or any tutrial materials, such as youtube channel) to learn simulation of such systems using Matlab ? the level is between begginner up to above intermediate ...
Thank you very much Alan Steven, you are very nice , I highly appreciate your remarks, and help
wish you have very nice day, and stay safe
Alan Stevens
Alan Stevens am 25 Jun. 2020
Here are a couple of YouTube videos links:
https://www.youtube.com/watch?v=OgO1gpXSUzU
However, if you just search monte carlo simulation on YouTube you will find several.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Particle & Nuclear Physics finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by