-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmontecarlo.m
93 lines (93 loc) · 2.97 KB
/
montecarlo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
close all;clear all;
N = 8^3; % total number of spheres (best 9^3 or more)
phi = .45; % volume fraction
zeta = .005; % maximum displacement (best zeta=.005 for phi=.45 and zeta=.033 for phi=.2)
iter = 100; % number of passes to make over all particles (best 7000 or more)
coords=zeros(N,3); % list of the 3D positions of all particles
overlap=(6*phi/(pi*N))^(1/3); % set this to diam
% create initial, evenly-spaced lattice
numcell=(ceil(N^(1/3)));
cellside=1/numcell;
hh=linspace(-.5+.5*cellside,.5-.5*cellside,numcell);
for ii=1:N
coords(ii,1)=hh(ceil(ii/ceil(N/numcell)));
coords(ii,2)=hh(floor(mod(ii,ceil(N/numcell))/numcell)+1);
coords(ii,3)=hh(mod(ii,numcell)+1);
end
% scatter3(coords(:,1),coords(:,2),coords(:,3),'filled');
% uncomment line above to show final lattice
%% metropolis section
accepts=0;
rejects=0;
for ii=1:iter
for jj=1:N
newcoords=coords;
x=coords(jj,1);
y=coords(jj,2);
z=coords(jj,3);
dx=2*zeta*rand-zeta;
dy=2*zeta*rand-zeta;
dz=2*zeta*rand-zeta;
x=x+dx;
y=y+dy;
z=z+dz;
if x>.5
x=x-1;
end
if x<-.5
x=x+1;
end
if y>.5
y=y-1;
end
if y<-.5
y=y+1;
end
if z>.5
z=z-1;
end
if z<-.5
z=z+1;
end
newcoords(jj,1)=x;
newcoords(jj,2)=y;
newcoords(jj,3)=z;
if isoverlap2(newcoords, overlap, jj)==0 % only accept if there is no overlap
coords(jj,1)=x;
coords(jj,2)=y;
coords(jj,3)=z;
accepts=accepts+1;
else rejects=rejects+1;
end
end
end
disp(accepts/(accepts+rejects));
% scatter3(coords(:,1),coords(:,2),coords(:,3),'filled');
% uncomment line above to show final lattice
%% compute pairwise distribution function
coos=buildlattice(coords); % build an array of cubes to implement periodic BC
rmax=.5; % because of boundary condition design, g2(r) will not be accurate past here
nbins=180; % number of bins (tweak for smoothest curve)
totg2=zeros(nbins,1);
for jj=1:N % average over all particles
g2=zeros(nbins,1);
dists=sqrt(sum(bsxfun(@minus,coords(jj,:),coos).^2,2));
dists(jj)=[]; % don't want to include tagged particle
for ii=1:nbins
isBin=((rmax*(ii-1)/nbins)<dists).*(dists<(ii/nbins)*rmax); %pairwise multiply is logical AND of binary vectors
g2(ii)=sum(isBin)/(4*pi*(1/nbins)*(ii/nbins)^2*rmax^3);
end
totg2=totg2+g2;
end
totg2=totg2/N^2;
f1=figure();
plot(linspace(0,rmax/overlap,length(totg2)),totg2) % express in units of diameters
xlabel('r/D');
ylabel('g2(r)');
title(['packing fraction = ' num2str(phi)]);
print(f1,'g2hardsphere.png','-dpng',['-r',num2str(600)],'-opengl') % save an image
kk=[linspace(0,rmax/overlap,length(totg2))' totg2];
fileID = fopen('g2hardsphere.txt','w'); % save a list of values
fprintf(fileID,'%6s %12s\n','r/D','g2');
fprintf(fileID,'%6.6f %12.8f\n',kk');
fclose(fileID);