added harmonic potntial + bugfixes + boundings
[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 double estimate_time_step(t_moldyn *moldyn,double nn_dist,double t) {
181
182         double tau;
183
184         tau=0.05*nn_dist/(sqrt(3.0*K_BOLTZMANN*t/moldyn->atom[0].mass));
185         tau*=1.0E-9;
186         if(tau<moldyn->tau)
187                 printf("[moldyn] warning: time step  (%f > %.15f)\n",
188                        moldyn->tau,tau);
189
190         return tau;     
191 }
192
193
194 /*
195  *
196  * 'integration of newtons equation' - algorithms
197  *
198  */
199
200 /* start the integration */
201
202 int moldyn_integrate(t_moldyn *moldyn) {
203
204         int i;
205         int write;
206
207         write=moldyn->write;
208
209         /* calculate initial forces */
210         moldyn->force(moldyn);
211
212         for(i=0;i<moldyn->time_steps;i++) {
213                 /* integration step */
214                 moldyn->integrate(moldyn);
215
216                 /* check for visualiziation */
217                 if(!(i%write)) {
218                         visual_atoms(moldyn->visual,i*moldyn->tau,
219                                      moldyn->atom,moldyn->count);
220                 }
221         }
222
223         return 0;
224 }
225
226 /* velocity verlet */
227
228 int velocity_verlet(t_moldyn *moldyn) {
229
230         int i,count;
231         double tau,tau_square;
232         t_3dvec delta;
233         t_atom *atom;
234
235         atom=moldyn->atom;
236         count=moldyn->count;
237         tau=moldyn->tau;
238
239         tau_square=tau*tau;
240
241         for(i=0;i<count;i++) {
242                 /* new positions */
243                 v3_scale(&delta,&(atom[i].v),tau);
244                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
245                 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
246                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
247                 v3_per_bound(&(atom[i].r),&(moldyn->dim));
248
249                 /* velocities */
250                 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
251                 v3_add(&(atom[i].v),&(atom[i].v),&delta);
252         }
253
254         /* forces depending on chosen potential */
255         moldyn->force(moldyn);
256
257         for(i=0;i<count;i++) {
258                 /* again velocities */
259                 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
260                 v3_add(&(atom[i].v),&(atom[i].v),&delta);
261         }
262
263         return 0;
264 }
265
266
267 /*
268  *
269  * potentials & corresponding forces
270  * 
271  */
272
273 /* harmonic oscillator potential and force */
274
275 double potential_harmonic_oscillator(t_moldyn *moldyn) {
276
277         t_ho_params *params;
278         t_atom *atom;
279         int i,j;
280         int count;
281         t_3dvec distance;
282         double d,u;
283         double sc,equi_dist;
284
285         params=moldyn->pot_params;
286         atom=moldyn->atom;
287         sc=params->spring_constant;
288         equi_dist=params->equilibrium_distance;
289         count=moldyn->count;
290
291         u=0.0;
292         for(i=0;i<count;i++) {
293                 for(j=0;j<i;j++) {
294                         v3_sub(&distance,&(atom[i].r),&(atom[j].r));
295                         d=v3_norm(&distance);
296                         u+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
297                 }
298         }
299
300         return u;
301 }
302
303 int force_harmonic_oscillator(t_moldyn *moldyn) {
304
305         t_ho_params *params;
306         int i,j,count;
307         t_atom *atom;
308         t_3dvec distance;
309         t_3dvec force;
310         double d;
311         double sc,equi_dist;
312
313         atom=moldyn->atom;      
314         count=moldyn->count;
315         params=moldyn->pot_params;
316         sc=params->spring_constant;
317         equi_dist=params->equilibrium_distance;
318
319         for(i=0;i<count;i++) v3_zero(&(atom[i].f));
320
321         for(i=0;i<count;i++) {
322                 for(j=0;j<i;j++) {
323                         v3_sub(&distance,&(atom[i].r),&(atom[j].r));
324                         v3_per_bound(&distance,&(moldyn->dim));
325                         d=v3_norm(&distance);
326                         if(d<=moldyn->cutoff) {
327                                 v3_scale(&force,&distance,
328                                          (-sc*(1.0-(equi_dist/d))));
329                                 v3_add(&(atom[i].f),&(atom[i].f),&force);
330                                 v3_sub(&(atom[j].f),&(atom[j].f),&force);
331                         }
332                 }
333         }
334
335         return 0;
336 }
337
338
339 /* lennard jones potential & force for one sort of atoms */
340  
341 double potential_lennard_jones(t_moldyn *moldyn) {
342
343         t_lj_params *params;
344         t_atom *atom;
345         int i,j;
346         int count;
347         t_3dvec distance;
348         double d,help;
349         double u;
350         double eps,sig6,sig12;
351
352         params=moldyn->pot_params;
353         atom=moldyn->atom;
354         count=moldyn->count;
355         eps=params->epsilon;
356         sig6=params->sigma6;
357         sig12=params->sigma12;
358
359         u=0.0;
360         for(i=0;i<count;i++) {
361                 for(j=0;j<i;j++) {
362                         v3_sub(&distance,&(atom[j].r),&(atom[i].r));
363                         d=1.0/v3_absolute_square(&distance);    /* 1/r^2 */
364                         help=d*d;                               /* 1/r^4 */
365                         help*=d;                                /* 1/r^6 */
366                         d=help*help;                            /* 1/r^12 */
367                         u+=eps*(sig12*d-sig6*help);
368                 }
369         }
370         
371         return u;
372 }
373
374 int force_lennard_jones(t_moldyn *moldyn) {
375
376         t_lj_params *params;
377         int i,j,count;
378         t_atom *atom;
379         t_3dvec distance;
380         t_3dvec force;
381         double d,h1,h2;
382         double eps,sig6,sig12;
383
384         atom=moldyn->atom;      
385         count=moldyn->count;
386         params=moldyn->pot_params;
387         eps=params->epsilon;
388         sig6=params->sigma6;
389         sig12=params->sigma12;
390
391         for(i=0;i<count;i++) v3_zero(&(atom[i].f));
392
393         for(i=0;i<count;i++) {
394                 for(j=0;j<i;j++) {
395                         v3_sub(&distance,&(atom[j].r),&(atom[i].r));
396                         v3_per_bound(&distance,&(moldyn->dim));
397                         d=v3_absolute_square(&distance);
398                         if(d<=moldyn->cutoff_square) {
399                                 h1=1.0/d;                       /* 1/r^2 */
400                                 d=h1*h1;                        /* 1/r^4 */
401                                 h2=d*d;                         /* 1/r^8 */
402                                 h1*=d;                          /* 1/r^6 */
403                                 h1*=h2;                         /* 1/r^14 */
404                                 h1*=sig12;
405                                 h2*=sig6;
406                                 d=12.0*h1-6.0*h2;
407                                 d*=eps;
408                                 v3_scale(&force,&distance,d);
409                                 v3_add(&(atom[j].f),&(atom[j].f),&force);
410                                 v3_sub(&(atom[i].f),&(atom[i].f),&force);
411                         }
412                 }
413         }
414
415         return 0;
416 }
417