08901c783452a006b638787351b735e20e2a56c3
[physik/posic.git] / moldyn.c
1 /*
2  * moldyn.c - molecular dynamics library main file
3  *
4  * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
5  *
6  */
7
8 #include "moldyn.h"
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <math.h>
13
14 #include "math/math.h"
15 #include "init/init.h"
16 #include "random/random.h"
17 #include "visual/visual.h"
18
19
20 int create_lattice(unsigned char type,int element,double mass,double lc,
21                    int a,int b,int c,t_atom **atom) {
22
23         int count;
24         int ret;
25         t_3dvec origin;
26
27         count=a*b*c;
28
29         if(type==FCC) count*=4;
30         if(type==DIAMOND) count*=8;
31
32         *atom=malloc(count*sizeof(t_atom));
33         if(*atom==NULL) {
34                 perror("malloc (atoms)");
35                 return -1;
36         }
37
38         v3_zero(&origin);
39
40         switch(type) {
41                 case FCC:
42                         ret=fcc_init(a,b,c,lc,*atom,&origin);
43                         break;
44                 case DIAMOND:
45                         ret=diamond_init(a,b,c,lc,*atom,&origin);
46                         break;
47                 default:
48                         printf("unknown lattice type (%02x)\n",type);
49                         return -1;
50         }
51
52         /* debug */
53         if(ret!=count) {
54                 printf("ok, there is something wrong ...\n");
55                 printf("calculated -> %d atoms\n",count);
56                 printf("created -> %d atoms\n",ret);
57                 return -1;
58         }
59
60         while(count) {
61                 (*atom)[count-1].element=element;
62                 (*atom)[count-1].mass=mass;
63                 count-=1;
64         }
65
66         return ret;
67 }
68
69 int destroy_lattice(t_atom *atom) {
70
71         if(atom) free(atom);
72
73         return 0;
74 }
75
76 int thermal_init(t_atom *atom,t_random *random,int count,double t) {
77
78         /*
79          * - gaussian distribution of velocities
80          * - zero total momentum
81          * - velocity scaling (E = 3/2 N k T), E: kinetic energy
82          */
83
84         int i;
85         double v,sigma;
86         t_3dvec p_total,delta;
87
88         /* gaussian distribution of velocities */
89         v3_zero(&p_total);
90         for(i=0;i<count;i++) {
91                 sigma=sqrt(2.0*K_BOLTZMANN*t/atom[i].mass);
92                 /* x direction */
93                 v=sigma*rand_get_gauss(random);
94                 atom[i].v.x=v;
95                 p_total.x+=atom[i].mass*v;
96                 /* y direction */
97                 v=sigma*rand_get_gauss(random);
98                 atom[i].v.y=v;
99                 p_total.y+=atom[i].mass*v;
100                 /* z direction */
101                 v=sigma*rand_get_gauss(random);
102                 atom[i].v.z=v;
103                 p_total.z+=atom[i].mass*v;
104         }
105
106         /* zero total momentum */
107         v3_scale(&p_total,&p_total,1.0/count);
108         for(i=0;i<count;i++) {
109                 v3_scale(&delta,&p_total,1.0/atom[i].mass);
110                 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
111         }
112
113         /* velocity scaling */
114         scale_velocity(atom,count,t);
115
116         return 0;
117 }
118
119 int scale_velocity(t_atom *atom,int count,double t) {
120
121         int i;
122         double e,c;
123
124         /*
125          * - velocity scaling (E = 3/2 N k T), E: kinetic energy
126          */
127         e=0.0;
128         for(i=0;i<count;i++)
129                 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
130         c=sqrt((2.0*e)/(3.0*count*K_BOLTZMANN*t));
131         for(i=0;i<count;i++)
132                 v3_scale(&(atom[i].v),&(atom[i].v),(1.0/c));
133
134         return 0;
135 }
136
137 double get_e_kin(t_atom *atom,int count) {
138
139         int i;
140         double e;
141
142         e=0.0;
143
144         for(i=0;i<count;i++) {
145                 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
146         }
147
148         return e;
149 }
150
151 double get_e_pot(t_moldyn *moldyn) {
152
153         return(moldyn->potential(moldyn));
154 }
155
156 double get_total_energy(t_moldyn *moldyn) {
157
158         double e;
159
160         e=get_e_kin(moldyn->atom,moldyn->count);
161         e+=get_e_pot(moldyn);
162
163         return e;
164 }
165
166 t_3dvec get_total_p(t_atom *atom, int count) {
167
168         t_3dvec p,p_total;
169         int i;
170
171         v3_zero(&p_total);
172         for(i=0;i<count;i++) {
173                 v3_scale(&p,&(atom[i].v),atom[i].mass);
174                 v3_add(&p_total,&p_total,&p);
175         }
176
177         return p_total;
178 }
179
180
181 /*
182  *
183  * 'integration of newtons equation' - algorithms
184  *
185  */
186
187 /* start the integration */
188
189 int moldyn_integrate(t_moldyn *moldyn) {
190
191         int i;
192
193         /* calculate initial forces */
194         moldyn->force(moldyn);
195
196         for(i=0;i<moldyn->time_steps;i++) {
197                 /* integration step */
198                 moldyn->integrate(moldyn);
199
200                 /* check for visualiziation */
201                 // to be continued ...
202                 if(!(i%100)) 
203                         visual_atoms(moldyn->visual,i*moldyn->tau,
204                                      moldyn->atom,moldyn->count);
205         }
206
207         return 0;
208 }
209
210 /* velocity verlet */
211
212 int velocity_verlet(t_moldyn *moldyn) {
213
214         int i,count;
215         double tau,tau_square;
216         t_3dvec delta;
217         t_atom *atom;
218
219         atom=moldyn->atom;
220         count=moldyn->count;
221         tau=moldyn->tau;
222
223         tau_square=tau*tau;
224
225         for(i=0;i<count;i++) {
226                 /* new positions */
227                 v3_scale(&delta,&(atom[i].v),tau);
228                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
229                 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
230                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
231                 v3_per_bound(&(atom[i].r),&(moldyn->dim));
232
233                 /* velocities */
234                 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
235                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
236         }
237
238         /* forces depending on chosen potential */
239         moldyn->force(moldyn);
240
241         for(i=0;i<count;i++) {
242                 /* again velocities */
243                 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
244                 v3_add(&(atom[i].v),&(atom[i].v),&delta);
245         }
246
247         return 0;
248 }
249
250
251 /*
252  *
253  * potentials & corresponding forces
254  * 
255  */
256
257 /* lennard jones potential & force for one sort of atoms */
258  
259 double potential_lennard_jones(t_moldyn *moldyn) {
260
261         t_lj_params *params;
262         t_atom *atom;
263         int i,j;
264         int count;
265         t_3dvec distance;
266         double d,help;
267         double u;
268         double eps,sig6,sig12;
269
270         params=moldyn->pot_params;
271         atom=moldyn->atom;
272         count=moldyn->count;
273         eps=params->epsilon;
274         sig6=params->sigma6;
275         sig12=params->sigma12;
276
277         u=0.0;
278         for(i=0;i<count;i++) {
279                 for(j=0;j<i;j++) {
280                         v3_sub(&distance,&(atom[j].r),&(atom[i].r));
281                         d=1.0/v3_absolute_square(&distance);    /* 1/r^2 */
282                         help=d*d;                               /* 1/r^4 */
283                         help*=d;                                /* 1/r^6 */
284                         d=help*help;                            /* 1/r^12 */
285                         u+=eps*(sig12*d-sig6*help);
286                 }
287         }
288         
289         return u;
290 }
291
292 int force_lennard_jones(t_moldyn *moldyn) {
293
294         t_lj_params *params;
295         int i,j,count;
296         t_atom *atom;
297         t_3dvec distance;
298         t_3dvec force;
299         double d,h1,h2;
300         double eps,sig6,sig12;
301
302         atom=moldyn->atom;      
303         count=moldyn->count;
304         params=moldyn->pot_params;
305         eps=params->epsilon;
306         sig6=params->sigma6;
307         sig12=params->sigma12;
308
309         for(i=0;i<count;i++) v3_zero(&(atom[i].f));
310
311         for(i=0;i<count;i++) {
312                 for(j=0;j<i;j++) {
313                         v3_sub(&distance,&(atom[j].r),&(atom[i].r));
314                         v3_per_bound(&distance,&(moldyn->dim));
315                         d=v3_absolute_square(&distance);
316                         if(d<=moldyn->cutoff_square) {
317                                 h1=1.0/d;                       /* 1/r^2 */
318                                 d=h1*h1;                        /* 1/r^4 */
319                                 h2=d*d;                         /* 1/r^8 */
320                                 h1*=d;                          /* 1/r^6 */
321                                 h1*=h2;                         /* 1/r^14 */
322                                 h1*=sig12;
323                                 h2*=sig6;
324                                 d=-12.0*h1+6.0*h2;
325                                 d*=eps;
326                                 v3_scale(&force,&distance,d);
327                                 v3_add(&(atom[j].f),&(atom[j].f),&force);
328                                 v3_sub(&(atom[i].f),&(atom[i].f),&force);
329                         }
330                 }
331         }
332
333         return 0;
334 }
335