Docsity

Monte Carlo Simulation of Ising Model: MCStep Function, Exercises of Computational Physics

Toggle sidebar
bg1
function [newLattice, dE] = MCStep(lattice, beta, J, Bz, spin)
%MCStep Monte Carlo step for simulation of the Ising Model
% Input:
% lattice - the lattice of spins
% beta - thermodynamic beta (=1/kT)
% where k is Boltzmann's constant
% and T is temperature
% J - (optional) coupling constant for spin/spin
% interaction. Defaults to 1.
% Bz - (optional) external magnetic field in +z
% direction. Defaults to 0.
% spin - (optional) Spin of particles in lattice.
% Defaults to 1/2.
% Output:
% newLattice - the lattice after the MC step
% dE - the change in energy of the lattice
% Use J = 1 for default
if (nargin < 3)
J = 1;
end
% Use no external magnetic field for default
if (nargin < 4)
Bz = 0;
end
% Use spin 1/2 for default
if (nargin < 5)
spin = 1/2;
end
% Get size of lattice
lsize = size(lattice);
if (length(lsize) > 3)
error('Lattice size limited to one, two, or three dimensions!');
end
if (length(lsize) < 3)
lsize(3) = 1;
end
% Possible spin states
Szs = -spin:spin;
% Iterate to potentially hit every lattice spot
nsteps = prod(lsize);
% Change in energy
dE = 0;
for istep=1:nsteps
% Pick our point to work on
site = ceil(rand(1,3).*lsize);
pf2

Partial preview of the text

Download Monte Carlo Simulation of Ising Model: MCStep Function and more Exercises Computational Physics in PDF only on Docsity!

function [newLattice, dE] = MCStep(lattice, beta, J, Bz, spin) %MCStep Monte Carlo step for simulation of the Ising Model % Input: % lattice - the lattice of spins % beta - thermodynamic beta (=1/kT) % where k is Boltzmann's constant % and T is temperature % J - (optional) coupling constant for spin/spin % interaction. Defaults to 1. % Bz - (optional) external magnetic field in +z % direction. Defaults to 0. % spin - (optional) Spin of particles in lattice. % Defaults to 1/2. % Output: % newLattice - the lattice after the MC step % dE - the change in energy of the lattice % Use J = 1 for default if (nargin < 3) J = 1; end % Use no external magnetic field for default if (nargin < 4) Bz = 0; end % Use spin 1/2 for default if (nargin < 5) spin = 1/2; end % Get size of lattice lsize = size(lattice); if (length(lsize) > 3) error('Lattice size limited to one, two, or three dimensions!'); end if (length(lsize) < 3) lsize(3) = 1; end % Possible spin states Szs = -spin:spin; % Iterate to potentially hit every lattice spot nsteps = prod(lsize); % Change in energy dE = 0; for istep=1:nsteps % Pick our point to work on site = ceil(rand(1,3).*lsize);

% Possible energies this site can have Es = siteEnergy(lattice, site, J, Bz, Szs); % Record old state oldstate = find(Szs==lattice(site(1),site(2),site(3))); oldstate = oldstate(1); % Thermodynamically pick new state newstate = thermoNewState(Es, beta); % Update lattice and record dE dE = dE + (Es(newstate) - Es(oldstate)); lattice(site(1),site(2),site(3)) = Szs(newstate); end newLattice = lattice; return