ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/dp/dp.c
Revision: 581
Committed: Wed Jul 9 15:14:05 2003 UTC (20 years, 11 months ago) by xsun
Content type: text/plain
File size: 6779 byte(s)
Log Message:
This is the dp and read file

File Contents

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