3d669d223401ddd8fe9277e9ea07654186e7018e
[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%1)) {
203                         visual_atoms(moldyn->visual,i*moldyn->tau,
204                                      moldyn->atom,moldyn->count);
205                 }
206         }
207
208         return 0;
209 }
210
211 /* velocity verlet */
212
213 int velocity_verlet(t_moldyn *moldyn) {
214
215         int i,count;
216         double tau,tau_square;
217         t_3dvec delta;
218         t_atom *atom;
219
220         atom=moldyn->atom;
221         count=moldyn->count;
222         tau=moldyn->tau;
223
224         tau_square=tau*tau;
225
226         for(i=0;i<count;i++) {
227                 /* new positions */
228                 v3_scale(&delta,&(atom[i].v),tau);
229                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
230                 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
231                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
232                 v3_per_bound(&(atom[i].r),&(moldyn->dim));
233
234                 /* velocities */
235                 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
236                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
237         }
238
239         /* forces depending on chosen potential */
240         moldyn->force(moldyn);
241
242         for(i=0;i<count;i++) {
243                 /* again velocities */
244                 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
245                 v3_add(&(atom[i].v),&(atom[i].v),&delta);
246         }
247
248         return 0;
249 }
250
251
252 /*
253  *
254  * potentials & corresponding forces
255  * 
256  */
257
258 /* lennard jones potential & force for one sort of atoms */
259  
260 double potential_lennard_jones(t_moldyn *moldyn) {
261
262         t_lj_params *params;
263         t_atom *atom;
264         int i,j;
265         int count;
266         t_3dvec distance;
267         double d,help;
268         double u;
269         double eps,sig6,sig12;
270
271         params=moldyn->pot_params;
272         atom=moldyn->atom;
273         count=moldyn->count;
274         eps=params->epsilon;
275         sig6=params->sigma6;
276         sig12=params->sigma12;
277
278         u=0.0;
279         for(i=0;i<count;i++) {
280                 for(j=0;j<i;j++) {
281                         v3_sub(&distance,&(atom[j].r),&(atom[i].r));
282                         d=1.0/v3_absolute_square(&distance);    /* 1/r^2 */
283                         help=d*d;                               /* 1/r^4 */
284                         help*=d;                                /* 1/r^6 */
285                         d=help*help;                            /* 1/r^12 */
286                         u+=eps*(sig12*d-sig6*help);
287                 }
288         }
289         
290         return u;
291 }
292
293 int force_lennard_jones(t_moldyn *moldyn) {
294
295         t_lj_params *params;
296         int i,j,count;
297         t_atom *atom;
298         t_3dvec distance;
299         t_3dvec force;
300         double d,h1,h2;
301         double eps,sig6,sig12;
302
303         atom=moldyn->atom;      
304         count=moldyn->count;
305         params=moldyn->pot_params;
306         eps=params->epsilon;
307         sig6=params->sigma6;
308         sig12=params->sigma12;
309
310         for(i=0;i<count;i++) v3_zero(&(atom[i].f));
311
312         for(i=0;i<count;i++) {
313                 for(j=0;j<i;j++) {
314                         v3_sub(&distance,&(atom[j].r),&(atom[i].r));
315                         v3_per_bound(&distance,&(moldyn->dim));
316                         d=v3_absolute_square(&distance);
317                         if(d<=moldyn->cutoff_square) {
318                                 h1=1.0/d;                       /* 1/r^2 */
319                                 d=h1*h1;                        /* 1/r^4 */
320                                 h2=d*d;                         /* 1/r^8 */
321                                 h1*=d;                          /* 1/r^6 */
322                                 h1*=h2;                         /* 1/r^14 */
323                                 h1*=sig12;
324                                 h2*=sig6;
325                                 d=12.0*h1-6.0*h2;
326                                 d*=eps;
327                                 v3_scale(&force,&distance,d);
328                                 v3_add(&(atom[j].f),&(atom[j].f),&force);
329                                 v3_sub(&(atom[i].f),&(atom[i].f),&force);
330                         }
331                 }
332         }
333
334         return 0;
335 }
336