1 |
xsun |
581 |
#include<stdio.h> |
2 |
|
|
#include<math.h> |
3 |
|
|
#include<stdlib.h> |
4 |
xsun |
587 |
#define max_sites 15000 |
5 |
xsun |
581 |
#define MYSEED 8973247 |
6 |
|
|
|
7 |
|
|
int nsites, xlat, ylat; |
8 |
|
|
double beta; |
9 |
|
|
double theta[max_sites], x[max_sites], y[max_sites], z[max_sites], phi[max_sites], mu[max_sites]; |
10 |
|
|
double domsizex, domsizey; |
11 |
|
|
double magx, magy, magz, magmag, kz, ktheta, strength, z0, theta0; |
12 |
|
|
double en,dtheta,deltaz,deltaphi; |
13 |
|
|
int attemp, nacc; |
14 |
|
|
|
15 |
|
|
|
16 |
|
|
int main() |
17 |
|
|
{ |
18 |
xsun |
587 |
void getmag(); |
19 |
|
|
double eneri(double xi, double yi, double zi, double phii, double thetai, int i, int jb); |
20 |
|
|
double toterg(); |
21 |
|
|
void adjust(); |
22 |
|
|
void mcmove(); |
23 |
|
|
void store(FILE *outFile); |
24 |
|
|
|
25 |
|
|
double twopi, nnd, myran, kb; |
26 |
|
|
double ent; |
27 |
|
|
int icycl, ncycl, imove, nmoves, nsamp; |
28 |
|
|
int nx, ny, which, i, j; |
29 |
|
|
|
30 |
|
|
FILE *outFile; |
31 |
|
|
outFile=fopen("dp_file","w"); |
32 |
|
|
|
33 |
|
|
srand48(MYSEED); |
34 |
|
|
|
35 |
|
|
// These two parameters set the size of the system: |
36 |
xsun |
581 |
|
37 |
xsun |
587 |
nnd = 7.0; |
38 |
|
|
ny = 100; |
39 |
xsun |
581 |
|
40 |
xsun |
587 |
// we want the domains to be roughly square: |
41 |
xsun |
581 |
|
42 |
xsun |
587 |
nx = (int) ((double)ny / sqrt(3.0)); |
43 |
xsun |
581 |
|
44 |
|
|
|
45 |
xsun |
587 |
// periodic box boundaries: |
46 |
xsun |
581 |
|
47 |
xsun |
587 |
domsizex = sqrt(3.0) * nx * nnd; |
48 |
|
|
domsizey = ny * nnd; |
49 |
xsun |
581 |
|
50 |
xsun |
587 |
strength = 10.0; |
51 |
|
|
ncycl = 1e7; |
52 |
|
|
nmoves = 100; |
53 |
|
|
nsamp = 1e4; |
54 |
|
|
twopi = 2.0 * M_PI; |
55 |
|
|
dtheta = 0.1; |
56 |
|
|
kb = 0.0019872198; |
57 |
|
|
beta = 1.0 / (kb * 300.0); |
58 |
|
|
z0 = 0.0; |
59 |
|
|
deltaz = 0.1; |
60 |
|
|
deltaphi = 0.1; |
61 |
|
|
kz = kb; |
62 |
|
|
ktheta = kb; |
63 |
|
|
theta0 = M_PI / 2.0; |
64 |
|
|
|
65 |
|
|
which = 0; |
66 |
|
|
xlat = 0; |
67 |
|
|
|
68 |
|
|
for(i=0; i < nx; i++) { |
69 |
|
|
|
70 |
|
|
xlat = xlat + 2; |
71 |
|
|
ylat = 0; |
72 |
|
|
|
73 |
|
|
for(j=0; j<ny; j++) { |
74 |
|
|
|
75 |
|
|
which = which + 1; |
76 |
|
|
x[which] = i * sqrt(3.0) * nnd; |
77 |
|
|
y[which] = j * nnd; |
78 |
|
|
myran = drand48(); |
79 |
|
|
phi[which] = myran * twopi; |
80 |
|
|
myran = drand48(); |
81 |
|
|
theta[which] = acos(2.0*myran-1.0); |
82 |
|
|
myran = drand48(); |
83 |
|
|
z[which]=z0 + (2.0*myran-1.0)*deltaz; |
84 |
|
|
mu[which] = strength; |
85 |
|
|
|
86 |
|
|
which = which + 1; |
87 |
|
|
|
88 |
|
|
printf("%d\n", which); |
89 |
|
|
|
90 |
|
|
x[which] = nnd* sqrt(3.0) * (2.0*i + 1.0) / 2.0; |
91 |
|
|
y[which] = nnd* ( 2.0*j+1.0 ) / 2.0; |
92 |
|
|
myran = drand48(); |
93 |
|
|
phi[which] = myran * twopi; |
94 |
|
|
myran = drand48(); |
95 |
|
|
theta[which] = acos(2.0*myran-1.0); |
96 |
|
|
myran = drand48(); |
97 |
|
|
z[which] = z0 + (2.0*myran-1.0) * deltaz; |
98 |
|
|
mu[which] = strength; |
99 |
|
|
ylat = ylat + 2; |
100 |
|
|
} |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
nsites= which; |
104 |
|
|
|
105 |
|
|
en=toterg(); |
106 |
|
|
printf("\nthe initial energy is : \t%f\n\n",en); |
107 |
|
|
|
108 |
|
|
attemp = 0; |
109 |
|
|
nacc = 0; |
110 |
|
|
|
111 |
|
|
adjust(); |
112 |
|
|
|
113 |
|
|
for(icycl=1;icycl<=ncycl;icycl++) |
114 |
xsun |
581 |
{ |
115 |
|
|
for(imove=1;imove<=nmoves;imove++) |
116 |
|
|
{ |
117 |
|
|
mcmove(); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
if((icycl%nsamp) == 0) |
121 |
|
|
{ |
122 |
|
|
store(outFile); |
123 |
|
|
getmag(); |
124 |
|
|
printf("mag=%f\t%f\t%f\t%f\n", magx, magy, magz, magmag); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
if( (icycl%(ncycl/5))==0 ) |
128 |
|
|
{ |
129 |
|
|
printf("\n=====> Done\t%d\tout of\t%d\n", icycl, ncycl); |
130 |
|
|
adjust(); |
131 |
|
|
} |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
if(ncycl!=0) |
135 |
|
|
{ |
136 |
|
|
if(attemp!=0) |
137 |
|
|
{ |
138 |
|
|
printf("Number of att. to disl. a part. :%d\nsuccess: %d(=%f%)\n\n", attemp, nacc, 100*(double)(nacc)/(double)(attemp)); |
139 |
|
|
} |
140 |
|
|
ent=toterg(); |
141 |
|
|
if(fabs(ent-en)>1e-6) printf("\n###### PROBLEMS ENERGY ###############\n"); |
142 |
|
|
printf("\nTotal energy end of simulation : %f\nrunning energy : %f\ndifference : %f\n\n", ent, en, ent-en); |
143 |
|
|
} |
144 |
|
|
fclose(outFile); |
145 |
|
|
return 0; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
double toterg() |
149 |
|
|
{ |
150 |
|
|
double eneri(double, double, double, double, double, int, int); |
151 |
|
|
|
152 |
|
|
double xi, yi, zi, phii, thetai, eni; |
153 |
|
|
int i, jb; |
154 |
|
|
double ener=0.0; |
155 |
|
|
for(i=1;i<=nsites;i++) |
156 |
|
|
{ |
157 |
|
|
xi = x[i]; |
158 |
|
|
yi = y[i]; |
159 |
|
|
zi = z[i]; |
160 |
|
|
phii = phi[i]; |
161 |
|
|
thetai = theta[i]; |
162 |
|
|
jb = i; |
163 |
|
|
eni=eneri(xi, yi, zi, phii, thetai, i, jb); |
164 |
|
|
ener = ener + eni; |
165 |
|
|
} |
166 |
|
|
return ener; |
167 |
|
|
|
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
void getmag( ) |
171 |
|
|
{ |
172 |
|
|
double thetai, phii; |
173 |
|
|
int i; |
174 |
|
|
|
175 |
|
|
magx = 0.0; |
176 |
|
|
magy = 0.0; |
177 |
|
|
magz = 0.0; |
178 |
|
|
|
179 |
|
|
for(i=1;i<=nsites;i++) |
180 |
|
|
{ |
181 |
|
|
phii = phi[i]; |
182 |
|
|
thetai = theta[i]; |
183 |
|
|
magx = magx + mu[i] * cos(phii) * sin(thetai); |
184 |
|
|
magy = magy + mu[i] * sin(phii) * sin(thetai); |
185 |
|
|
magz = magz + mu[i] * cos(thetai); |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
magx = magx / (double)(nsites); |
189 |
|
|
magy = magy / (double)(nsites); |
190 |
|
|
magz = magz / (double)(nsites); |
191 |
|
|
magmag = sqrt (magx*magx+magy*magy+magz*magz); |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
double eneri(double xi, double yi, double zi, double phii, double thetai, int i, int jb) |
195 |
|
|
{ |
196 |
|
|
|
197 |
|
|
double uxi, uyi, uzi, dx, dy, dz, r, r2, r3, r5, uxj, uyj, uzj, rcut; |
198 |
|
|
double udotu, rdotui, rdotuj, vij, pre, vint; |
199 |
|
|
double eni; |
200 |
|
|
int j; |
201 |
|
|
|
202 |
|
|
pre = 14.38362; |
203 |
|
|
|
204 |
|
|
rcut = 30.0; |
205 |
|
|
|
206 |
|
|
eni = 0.0; |
207 |
|
|
uxi = mu[i] * cos(phii) * sin(thetai); |
208 |
|
|
uyi = mu[i] * sin(phii) * sin(thetai); |
209 |
|
|
uzi = mu[i] * cos(thetai); |
210 |
|
|
|
211 |
|
|
for(j=jb;j<=nsites;j++) |
212 |
|
|
{ |
213 |
|
|
if(j!=i) |
214 |
|
|
{ |
215 |
xsun |
587 |
dx = x[j]-xi; |
216 |
|
|
if(fabs(dx)>(domsizex/2.0)) dx = dx - domsizex*copysign(1.0,dx)*(double)(int)(fabs(dx /domsizex)+0.5); |
217 |
|
|
dy = y[j]-yi; |
218 |
|
|
if(fabs(dy)>(domsizey/2.0)) dy = dy - domsizey*copysign(1.0,dy)*(double)(int)(fabs(dy /domsizey)+0.5); |
219 |
|
|
dz = z[j]-zi; |
220 |
|
|
|
221 |
|
|
r2 = dx*dx+dy*dy+dz*dz; |
222 |
|
|
r = sqrt(r2); |
223 |
|
|
if(r<rcut) |
224 |
|
|
{ |
225 |
|
|
r3 = r2*r; |
226 |
|
|
r5 = r2*r3; |
227 |
|
|
|
228 |
|
|
uxj = mu[j]*cos(phi[j]) * sin(theta[j]); |
229 |
|
|
uyj = mu[j]*sin(phi[j]) * sin(theta[j]); |
230 |
|
|
uzj = mu[j] * cos(theta[j]); |
231 |
|
|
udotu = uxi*uxj + uyi*uyj + uzi*uzj; |
232 |
|
|
rdotui = dx*uxi + dy*uyi + dz*uzi; |
233 |
|
|
rdotuj = dx*uxj + dy*uyj + dz*uzj; |
234 |
|
|
|
235 |
|
|
vij = pre*(udotu/r3 - 3.0*rdotui*rdotuj/r5); |
236 |
|
|
eni = eni + vij; |
237 |
|
|
} |
238 |
xsun |
581 |
} |
239 |
|
|
} |
240 |
|
|
vint = 0.5 * kz * (zi-z0) * (zi-z0) + 0.5 * ktheta * (thetai-theta0) * (thetai-theta0); |
241 |
|
|
eni = eni + vint; |
242 |
|
|
return eni; |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
void adjust() |
246 |
|
|
{ |
247 |
|
|
static int attempp, naccp; |
248 |
|
|
double dzo, dphio, dthetao, frac; |
249 |
|
|
|
250 |
|
|
if((attemp==0)||(attempp>=attemp)) |
251 |
|
|
{ |
252 |
|
|
naccp = nacc; |
253 |
|
|
attempp = attemp; |
254 |
|
|
} |
255 |
|
|
else |
256 |
|
|
{ |
257 |
|
|
frac = (double)(nacc-naccp)/(double)(attemp-attempp); |
258 |
|
|
dthetao = dtheta; |
259 |
|
|
dzo = deltaz; |
260 |
|
|
dphio = deltaphi; |
261 |
|
|
dtheta = dtheta * fabs(frac/0.5); |
262 |
|
|
deltaz = deltaz * fabs(frac/0.5); |
263 |
|
|
deltaphi = deltaphi * fabs(frac/0.5); |
264 |
|
|
|
265 |
|
|
if((dtheta/dthetao)>1.5) dtheta = dthetao*1.5; |
266 |
|
|
if((dtheta/dthetao)<0.5) dtheta = dthetao*0.5; |
267 |
|
|
if((deltaz/dzo)>1.5) deltaz = dzo * 1.5; |
268 |
|
|
if((deltaz/dzo)<0.5) deltaz = dzo * 0.5; |
269 |
|
|
if((deltaphi/dphio)>1.5) deltaphi = dphio * 1.5; |
270 |
|
|
if((deltaphi/dphio)<0.5) deltaphi = dphio * 0.5; |
271 |
|
|
printf("Max. displ. set to :%f\t%f\t%f\t(old :%f\t%f\t%f)\nFrac. acc.:%f\nattempts:%d\nsucces:%d\n\n", deltaz, deltaphi, dtheta, dzo, dphio, dthetao, frac, attemp-attempp, nacc - naccp); |
272 |
|
|
naccp = nacc; |
273 |
|
|
attempp=attemp; |
274 |
|
|
} |
275 |
|
|
|
276 |
|
|
} |
277 |
|
|
|
278 |
|
|
void mcmove() |
279 |
|
|
{ |
280 |
|
|
int o; |
281 |
|
|
double eno, zn, phin, thetan, enn, myran; |
282 |
|
|
int jb; |
283 |
|
|
|
284 |
|
|
attemp = attemp + 1; |
285 |
|
|
jb = 1; |
286 |
|
|
myran = drand48(); |
287 |
|
|
o = (int)(nsites*myran) + 1; |
288 |
|
|
eno=eneri(x[o], y[o], z[o], phi[o], theta[o], o, jb); |
289 |
|
|
myran = drand48(); |
290 |
|
|
zn = z[o] + (2.0*myran-1.0) * deltaz; |
291 |
|
|
myran = drand48(); |
292 |
|
|
phin = phi[o] + (2.0*myran-1.0) * deltaphi; |
293 |
|
|
myran = drand48(); |
294 |
|
|
thetan = theta[o] + (2.0*myran-1.0)*dtheta; |
295 |
|
|
enn=eneri(x[o], y[o], zn, phin, thetan, o, jb); |
296 |
|
|
myran = drand48(); |
297 |
|
|
if(myran<=(exp(-beta*(enn-eno)))) |
298 |
|
|
{ |
299 |
|
|
nacc = nacc + 1; |
300 |
|
|
en = en + (enn-eno); |
301 |
|
|
z[o] = zn; |
302 |
|
|
phi[o] = phin; |
303 |
|
|
theta[o] = thetan; |
304 |
|
|
} |
305 |
|
|
} |
306 |
|
|
|
307 |
|
|
|
308 |
|
|
void store(FILE* outFile) |
309 |
|
|
{ |
310 |
|
|
double uxi, uyi, uzi; |
311 |
|
|
int i; |
312 |
|
|
|
313 |
|
|
fprintf(outFile,"%d\n",nsites); |
314 |
|
|
fprintf(outFile,"%d\t%d\n",xlat, ylat); |
315 |
|
|
|
316 |
|
|
for(i=1;i<=nsites;i++) |
317 |
|
|
{ |
318 |
|
|
uxi = cos(phi[i])*sin(theta[i]); |
319 |
|
|
uyi = sin(phi[i])*sin(theta[i]); |
320 |
|
|
uzi = cos(theta[i]); |
321 |
|
|
fprintf(outFile,"%s\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n","Ar",x[i],y[i],z[i],phi[i],uxi,uyi,uzi); |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
} |