more updates
[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 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <math.h>
17
18 #include "moldyn.h"
19
20 #include "math/math.h"
21 #include "init/init.h"
22 #include "random/random.h"
23 #include "visual/visual.h"
24 #include "list/list.h"
25
26 int moldyn_usage(char **argv) {
27
28         printf("\n%s usage:\n\n",argv[0]);
29         printf("--- general options ---\n");
30         printf("-E <steps> <file> (log total energy)\n");
31         printf("-M <steps> <file> (log total momentum)\n");
32         printf("-D <steps> <file> (dump total information)\n");
33         printf("-S <steps> <filebase> (single save file)\n");
34         printf("-V <steps> <filebase> (rasmol file)\n");
35         printf("--- physics options ---\n");
36         printf("-T <temperature> [K] (%f)\n",MOLDYN_TEMP);
37         printf("-t <timestep tau> [s] (%.15f)\n",MOLDYN_TAU);
38         printf("-C <cutoff radius> [m] (%.15f)\n",MOLDYN_CUTOFF);
39         printf("-R <runs> (%d)\n",MOLDYN_RUNS);
40         printf(" -- integration algo --\n");
41         printf("  -I <number> (%d)\n",MOLDYN_INTEGRATE_DEFAULT);
42         printf("     0: velocity verlet\n");
43         printf(" -- potential --\n");
44         printf("  -P <number> <param1 param2 ...>\n");
45         printf("     0: harmonic oscillator\n");
46         printf("        param1: spring constant\n");
47         printf("        param2: equilibrium distance\n");
48         printf("     1: lennard jones\n");
49         printf("        param1: epsilon\n");
50         printf("        param2: sigma\n");
51         printf("\n");
52
53         return 0;
54 }
55
56 int moldyn_parse_argv(t_moldyn *moldyn,int argc,char **argv) {
57
58         int i;
59         t_ho_params hop;
60         t_lj_params ljp;
61         t_tersoff_params tp;
62         double s,e;
63
64         memset(moldyn,0,sizeof(t_moldyn));
65
66         /* default values */
67         moldyn->t=MOLDYN_TEMP;
68         moldyn->tau=MOLDYN_TAU;
69         moldyn->time_steps=MOLDYN_RUNS;
70         moldyn->integrate=velocity_verlet;
71         moldyn->potential_force_function=lennard_jones;
72
73         /* parse argv */
74         for(i=1;i<argc;i++) {
75                 if(argv[i][0]=='-') {
76                         switch(argv[i][1]){
77                                 case 'E':
78                                         moldyn->ewrite=atoi(argv[++i]);
79                                         strncpy(moldyn->efb,argv[++i],64);
80                                         break;
81                                 case 'M':
82                                         moldyn->mwrite=atoi(argv[++i]);
83                                         strncpy(moldyn->mfb,argv[++i],64);
84                                         break;
85                                 case 'S':
86                                         moldyn->swrite=atoi(argv[++i]);
87                                         strncpy(moldyn->sfb,argv[++i],64);
88                                         break;
89                                 case 'V':
90                                         moldyn->vwrite=atoi(argv[++i]);
91                                         strncpy(moldyn->vfb,argv[++i],64);
92                                         break;
93                                 case 'T':
94                                         moldyn->t=atof(argv[++i]);
95                                         break;
96                                 case 't':
97                                         moldyn->tau=atof(argv[++i]);
98                                         break;
99                                 case 'C':
100                                         moldyn->cutoff=atof(argv[++i]);
101                                         break;
102                                 case 'R':
103                                         moldyn->time_steps=atoi(argv[++i]);
104                                         break;
105                                 case 'I':
106         /* integration algorithm */
107         switch(atoi(argv[++i])) {
108                 case MOLDYN_INTEGRATE_VERLET:
109                         moldyn->integrate=velocity_verlet;
110                         break;
111                 default:
112                         printf("unknown integration algo %s\n",argv[i]);
113                         moldyn_usage(argv);
114                         return -1;
115         }
116
117                                 case 'P':
118         /* potential + params */
119         switch(atoi(argv[++i])) {
120                 case MOLDYN_POTENTIAL_HO:
121                         hop.spring_constant=atof(argv[++i]);
122                         hop.equilibrium_distance=atof(argv[++i]);
123                         moldyn->pot_params=malloc(sizeof(t_ho_params));
124                         memcpy(moldyn->pot_params,&hop,sizeof(t_ho_params));
125                         moldyn->potential_force_function=harmonic_oscillator;
126                         break;
127                 case MOLDYN_POTENTIAL_LJ:
128                         e=atof(argv[++i]);
129                         s=atof(argv[++i]);
130                         ljp.epsilon4=4*e;
131                         ljp.sigma6=s*s*s*s*s*s;
132                         ljp.sigma12=ljp.sigma6*ljp.sigma6;
133                         moldyn->pot_params=malloc(sizeof(t_lj_params));
134                         memcpy(moldyn->pot_params,&ljp,sizeof(t_lj_params));
135                         moldyn->potential_force_function=lennard_jones;
136                         break;
137                 default:
138                         printf("unknown potential %s\n",argv[i]);
139                         moldyn_usage(argv);
140                         return -1;
141         }
142
143                                 default:
144                                         printf("unknown option %s\n",argv[i]);
145                                         moldyn_usage(argv);
146                                         return -1;
147                         }
148                 } else {
149                         moldyn_usage(argv);
150                         return -1;
151                 }
152         }
153
154         return 0;
155 }
156
157 int moldyn_log_init(t_moldyn *moldyn) {
158
159         moldyn->lvstat=0;
160         t_visual *vis;
161
162         vis=&(moldyn->vis);
163
164         if(moldyn->ewrite) {
165                 moldyn->efd=open(moldyn->efb,O_WRONLY|O_CREAT|O_TRUNC);
166                 if(moldyn->efd<0) {
167                         perror("[moldyn] efd open");
168                         return moldyn->efd;
169                 }
170                 dprintf(moldyn->efd,"# moldyn total energy logfile\n");
171                 moldyn->lvstat|=MOLDYN_LVSTAT_TOTAL_E;
172         }
173
174         if(moldyn->mwrite) {
175                 moldyn->mfd=open(moldyn->mfb,O_WRONLY|O_CREAT|O_TRUNC);
176                 if(moldyn->mfd<0) {
177                         perror("[moldyn] mfd open");
178                         return moldyn->mfd;
179                 }
180                 dprintf(moldyn->mfd,"# moldyn total momentum logfile\n");
181                 moldyn->lvstat|=MOLDYN_LVSTAT_TOTAL_M;
182         }
183
184         if(moldyn->swrite)
185                 moldyn->lvstat|=MOLDYN_LVSTAT_SAVE;
186
187         if((moldyn->vwrite)&&(vis)) {
188                 moldyn->visual=vis;
189                 visual_init(vis,moldyn->vfb);
190                 moldyn->lvstat|=MOLDYN_LVSTAT_VISUAL;
191         }
192
193         moldyn->lvstat|=MOLDYN_LVSTAT_INITIALIZED;
194
195         return 0;
196 }
197
198 int moldyn_log_shutdown(t_moldyn *moldyn) {
199
200         if(moldyn->efd) close(moldyn->efd);
201         if(moldyn->mfd) close(moldyn->efd);
202         if(moldyn->dfd) close(moldyn->efd);
203         if(moldyn->visual) visual_tini(moldyn->visual);
204
205         return 0;
206 }
207
208 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
209
210         int ret;
211
212         ret=moldyn_parse_argv(moldyn,argc,argv);
213         if(ret<0) return ret;
214
215         ret=moldyn_log_init(moldyn);
216         if(ret<0) return ret;
217
218         rand_init(&(moldyn->random),NULL,1);
219         moldyn->random.status|=RAND_STAT_VERBOSE;
220
221         moldyn->status=0;
222
223         return 0;
224 }
225
226 int moldyn_shutdown(t_moldyn *moldyn) {
227
228         moldyn_log_shutdown(moldyn);
229         rand_close(&(moldyn->random));
230         free(moldyn->atom);
231
232         return 0;
233 }
234
235 int create_lattice(unsigned char type,int element,double mass,double lc,
236                    int a,int b,int c,t_atom **atom) {
237
238         int count;
239         int ret;
240         t_3dvec origin;
241
242         count=a*b*c;
243
244         if(type==FCC) count*=4;
245         if(type==DIAMOND) count*=8;
246
247         *atom=malloc(count*sizeof(t_atom));
248         if(*atom==NULL) {
249                 perror("malloc (atoms)");
250                 return -1;
251         }
252
253         v3_zero(&origin);
254
255         switch(type) {
256                 case FCC:
257                         ret=fcc_init(a,b,c,lc,*atom,&origin);
258                         break;
259                 case DIAMOND:
260                         ret=diamond_init(a,b,c,lc,*atom,&origin);
261                         break;
262                 default:
263                         printf("unknown lattice type (%02x)\n",type);
264                         return -1;
265         }
266
267         /* debug */
268         if(ret!=count) {
269                 printf("ok, there is something wrong ...\n");
270                 printf("calculated -> %d atoms\n",count);
271                 printf("created -> %d atoms\n",ret);
272                 return -1;
273         }
274
275         while(count) {
276                 (*atom)[count-1].element=element;
277                 (*atom)[count-1].mass=mass;
278                 count-=1;
279         }
280
281         return ret;
282 }
283
284 int destroy_lattice(t_atom *atom) {
285
286         if(atom) free(atom);
287
288         return 0;
289 }
290
291 int thermal_init(t_moldyn *moldyn) {
292
293         /*
294          * - gaussian distribution of velocities
295          * - zero total momentum
296          * - velocity scaling (E = 3/2 N k T), E: kinetic energy
297          */
298
299         int i;
300         double v,sigma;
301         t_3dvec p_total,delta;
302         t_atom *atom;
303         t_random *random;
304
305         atom=moldyn->atom;
306         random=&(moldyn->random);
307
308         /* gaussian distribution of velocities */
309         v3_zero(&p_total);
310         for(i=0;i<moldyn->count;i++) {
311                 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t/atom[i].mass);
312                 /* x direction */
313                 v=sigma*rand_get_gauss(random);
314                 atom[i].v.x=v;
315                 p_total.x+=atom[i].mass*v;
316                 /* y direction */
317                 v=sigma*rand_get_gauss(random);
318                 atom[i].v.y=v;
319                 p_total.y+=atom[i].mass*v;
320                 /* z direction */
321                 v=sigma*rand_get_gauss(random);
322                 atom[i].v.z=v;
323                 p_total.z+=atom[i].mass*v;
324         }
325
326         /* zero total momentum */
327         v3_scale(&p_total,&p_total,1.0/moldyn->count);
328         for(i=0;i<moldyn->count;i++) {
329                 v3_scale(&delta,&p_total,1.0/atom[i].mass);
330                 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
331         }
332
333         /* velocity scaling */
334         scale_velocity(moldyn);
335
336         return 0;
337 }
338
339 int scale_velocity(t_moldyn *moldyn) {
340
341         int i;
342         double e,c;
343         t_atom *atom;
344
345         atom=moldyn->atom;
346
347         /*
348          * - velocity scaling (E = 3/2 N k T), E: kinetic energy
349          */
350         e=0.0;
351         for(i=0;i<moldyn->count;i++)
352                 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
353         c=sqrt((2.0*e)/(3.0*moldyn->count*K_BOLTZMANN*moldyn->t));
354         for(i=0;i<moldyn->count;i++)
355                 v3_scale(&(atom[i].v),&(atom[i].v),(1.0/c));
356
357         return 0;
358 }
359
360 double get_e_kin(t_atom *atom,int count) {
361
362         int i;
363         double e;
364
365         e=0.0;
366
367         for(i=0;i<count;i++) {
368                 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
369         }
370
371         return e;
372 }
373
374 double get_e_pot(t_moldyn *moldyn) {
375
376         return moldyn->energy;
377 }
378
379 double get_total_energy(t_moldyn *moldyn) {
380
381         double e;
382
383         e=get_e_kin(moldyn->atom,moldyn->count);
384         e+=get_e_pot(moldyn);
385
386         return e;
387 }
388
389 t_3dvec get_total_p(t_atom *atom, int count) {
390
391         t_3dvec p,p_total;
392         int i;
393
394         v3_zero(&p_total);
395         for(i=0;i<count;i++) {
396                 v3_scale(&p,&(atom[i].v),atom[i].mass);
397                 v3_add(&p_total,&p_total,&p);
398         }
399
400         return p_total;
401 }
402
403 double estimate_time_step(t_moldyn *moldyn,double nn_dist,double t) {
404
405         double tau;
406
407         tau=0.05*nn_dist/(sqrt(3.0*K_BOLTZMANN*t/moldyn->atom[0].mass));
408         tau*=1.0E-9;
409         if(tau<moldyn->tau)
410                 printf("[moldyn] warning: time step  (%f > %.15f)\n",
411                        moldyn->tau,tau);
412
413         return tau;     
414 }
415
416 /*
417  * numerical tricks
418  */
419
420 /* linked list / cell method */
421
422 int link_cell_init(t_moldyn *moldyn) {
423
424         t_linkcell *lc;
425         int i;
426
427         lc=&(moldyn->lc);
428
429         /* list log fd */
430         lc->listfd=open("/dev/null",O_WRONLY);
431
432         /* partitioning the md cell */
433         lc->nx=moldyn->dim.x/moldyn->cutoff;
434         lc->x=moldyn->dim.x/lc->nx;
435         lc->ny=moldyn->dim.y/moldyn->cutoff;
436         lc->y=moldyn->dim.y/lc->ny;
437         lc->nz=moldyn->dim.z/moldyn->cutoff;
438         lc->z=moldyn->dim.z/lc->nz;
439
440         lc->cells=lc->nx*lc->ny*lc->nz;
441         lc->subcell=malloc(lc->cells*sizeof(t_list));
442
443         printf("initializing linked cells (%d)\n",lc->cells);
444
445         for(i=0;i<lc->cells;i++)
446                 //list_init(&(lc->subcell[i]),1);
447                 list_init(&(lc->subcell[i]));
448
449         link_cell_update(moldyn);
450         
451         return 0;
452 }
453
454 int link_cell_update(t_moldyn *moldyn) {
455
456         int count,i,j,k;
457         int nx,ny,nz;
458         t_atom *atom;
459         t_linkcell *lc;
460
461         atom=moldyn->atom;
462         lc=&(moldyn->lc);
463
464         nx=lc->nx;
465         ny=lc->ny;
466         nz=lc->nz;
467
468         for(i=0;i<lc->cells;i++)
469                 list_destroy(&(moldyn->lc.subcell[i]));
470         
471         for(count=0;count<moedyn->count;count++) {
472                 i=(atom[count].r.x+(moldyn->dim.x/2))/lc->x;
473                 j=(atom[count].r.y+(moldyn->dim.y/2))/lc->y;
474                 k=(atom[count].r.z+(moldyn->dim.z/2))/lc->z;
475                 list_add_immediate_ptr(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
476                                        &(atom[count]));
477         }
478
479         return 0;
480 }
481
482 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
483
484         t_linkcell *lc;
485         int a;
486         int count1,count2;
487         int ci,cj,ck;
488         int nx,ny,nz;
489         int x,y,z;
490         unsigned char bx,by,bz;
491
492         lc=&(moldyn->lc);
493         nx=lc->nx;
494         ny=lc->ny;
495         nz=lc->nz;
496         count1=1;
497         count2=27;
498         a=nx*ny;
499
500
501         cell[0]=lc->subcell[i+j*nx+k*a];
502         for(ci=-1;ci<=1;ci++) {
503                 bx=0;
504                 x=i+ci;
505                 if((x<0)||(x>=nx)) {
506                         x=(x+nx)%nx;
507                         bx=1;
508                 }
509                 for(cj=-1;cj<=1;cj++) {
510                         by=0;
511                         y=j+cj;
512                         if((y<0)||(y>=ny)) {
513                                 y=(y+ny)%ny;
514                                 by=1;
515                         }
516                         for(ck=-1;ck<=1;ck++) {
517                                 bz=0;
518                                 z=k+ck;
519                                 if((z<0)||(z>=nz)) {
520                                         z=(z+nz)%nz;
521                                         bz=1;
522                                 }
523                                 if(!(ci|cj|ck)) continue;
524                                 if(bx|by|bz) {
525                                         cell[--count2]=lc->subcell[x+y*nx+z*a];
526                                 }
527                                 else {
528                                         cell[count1++]=lc->subcell[x+y*nx+z*a];
529                                 }
530                         }
531                 }
532         }
533
534         lc->dnlc=count2;
535         lc->countn=27;
536
537         return count2;
538 }
539
540 int link_cell_shutdown(t_moldyn *moldyn) {
541
542         int i;
543         t_linkcell *lc;
544
545         lc=&(moldyn->lc);
546
547         for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
548                 list_shutdown(&(moldyn->lc.subcell[i]));
549
550         if(lc->listfd) close(lc->listfd);
551
552         return 0;
553 }
554
555 /*
556  *
557  * 'integration of newtons equation' - algorithms
558  *
559  */
560
561 /* start the integration */
562
563 int moldyn_integrate(t_moldyn *moldyn) {
564
565         int i;
566         unsigned int e,m,s,d,v;
567         t_3dvec p;
568
569         int fd;
570         char fb[128];
571
572         /* initialize linked cell method */
573         link_cell_init(moldyn);
574
575         /* logging & visualization */
576         e=moldyn->ewrite;
577         m=moldyn->mwrite;
578         s=moldyn->swrite;
579         d=moldyn->dwrite;
580         v=moldyn->vwrite;
581
582         if(!(moldyn->lvstat&MOLDYN_LVSTAT_INITIALIZED)) {
583                 printf("[moldyn] warning, lv system not initialized\n");
584                 return -1;
585         }
586
587         /* sqaure of some variables */
588         moldyn->tau_square=moldyn->tau*moldyn->tau;
589         moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
590
591         /* calculate initial forces */
592         moldyn->potential_force_function(moldyn);
593
594         for(i=0;i<moldyn->time_steps;i++) {
595
596                 /* integration step */
597                 moldyn->integrate(moldyn);
598
599                 /* check for log & visualization */
600                 if(e) {
601                         if(!(i%e))
602                                 dprintf(moldyn->efd,
603                                         "%.15f %.45f\n",i*moldyn->tau,
604                                         get_total_energy(moldyn));
605                 }
606                 if(m) {
607                         if(!(i%m)) {
608                                 p=get_total_p(moldyn->atom,moldyn->count);
609                                 dprintf(moldyn->mfd,
610                                         "%.15f %.45f\n",i*moldyn->tau,
611                                         v3_norm(&p));
612                         }
613                 }
614                 if(s) {
615                         if(!(i%s)) {
616                                 snprintf(fb,128,"%s-%f-%.15f.save",moldyn->sfb,
617                                          moldyn->t,i*moldyn->tau);
618                                 fd=open(fb,O_WRONLY|O_TRUNC|O_CREAT);
619                                 if(fd<0) perror("[moldyn] save fd open");
620                                 else {
621                                         write(fd,moldyn,sizeof(t_moldyn));
622                                         write(fd,moldyn->atom,
623                                               moldyn->count*sizeof(t_atom));
624                                 }
625                                 close(fd);
626                         }       
627                 }
628                 if(v) {
629                         if(!(i%v)) {
630                                 visual_atoms(moldyn->visual,i*moldyn->tau,
631                                              moldyn->atom,moldyn->count);
632                                 printf("\rsteps: %d",i);
633                                 fflush(stdout);
634                         }
635                 }
636         }
637
638         return 0;
639 }
640
641 /* velocity verlet */
642
643 int velocity_verlet(t_moldyn *moldyn) {
644
645         int i,count;
646         double tau,tau_square;
647         t_3dvec delta;
648         t_atom *atom;
649
650         atom=moldyn->atom;
651         count=moldyn->count;
652         tau=moldyn->tau;
653         tau_square=moldyn->tau_square;
654
655         for(i=0;i<count;i++) {
656                 /* new positions */
657                 v3_scale(&delta,&(atom[i].v),tau);
658                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
659                 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
660                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
661                 v3_per_bound(&(atom[i].r),&(moldyn->dim));
662
663                 /* velocities */
664                 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
665                 v3_add(&(atom[i].v),&(atom[i].v),&delta);
666         }
667
668         /* neighbour list update */
669 printf("list update ...\n");
670         link_cell_update(moldyn);
671 printf("done\n");
672
673         /* forces depending on chosen potential */
674 printf("calc potential/force ...\n");
675         potential_force_calc(moldyn);
676         //moldyn->potential_force_function(moldyn);
677 printf("done\n");
678
679         for(i=0;i<count;i++) {
680                 /* again velocities */
681                 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
682                 v3_add(&(atom[i].v),&(atom[i].v),&delta);
683         }
684
685         return 0;
686 }
687
688
689 /*
690  *
691  * potentials & corresponding forces
692  * 
693  */
694
695 /* generic potential and force calculation */
696
697 #define         CREATE_CELL_LIST(nb_list) \
698         link_cell_neighbour_index(moldyn,\
699                                   (atom[i].r.x+moldyn->dim.x/2)/lc->x,\
700                                   (atom[i].r.y+moldyn->dim.y/2)/lc->y,\
701                                   (atom[i].r.z+moldyn->dim.z/2)/lc->z,\
702                                   nb_list);
703
704
705 int potential_force_calc(t_moldyn *moldyn) {
706
707         int i,count;
708         t_atom *atom;
709         t_linkcell *lc;
710         t_list neighbour[27];
711         t_list *this;
712         double u;
713
714         count=moldyn->count;
715         atom=moldyn->atom;
716         lc=&(moldyn->lc);
717
718         /* reset energy */
719         u=0.0;
720
721         for(i=0;i<count;i++) {
722         
723                 /* reset force */
724                 v3_zero(&(atom[i].f));
725
726                 /* single particle potential/force */
727                 if(moldyn->status&MOLDYN_STAT_1BP)
728                         moldyn->pf_func1b(moldyn,&(atom[i]));
729
730                 /* 2 body pair potential/force */
731                 if(moldyn->status&MOLDYN_STAT_2BP) {
732                 
733                         CREATE_CELL_LIST(neighbour);
734
735                         /*
736                          * processing cell of atom i
737                          * => no need to check for empty list
738                          *    (1 element at minimum)
739                          */
740
741                         this=&(neighbour[0]);
742                         list_reset(this);
743                         do {
744                                 btom=this->current->data;
745                                 if(btom!=&(atom[i]))
746                                         moldyn->pf_func2b(moldyn,
747                                                           &(atom[i]),btom);
748                         } while(list_next(this)!=L_NO_NEXT_ELEMENT);
749
750                         /*
751                          * direct neighbour cells
752                          * => no boundary condition check necessary
753                          */
754                         for(j=0;j<lc->dnlc;j++) {
755                                 this=&(neighbour[j]);
756                                 list_reset(this);
757                                 if(this->start!=NULL) {
758                                         do {
759                                                 btom=this->current->data;
760                                                 moldyn->pf_func2b(moldyn,
761                                                                   &(atom[i]),
762                                                                   btom);
763                                         } while(list_next(this)!=\
764                                                 L_NO_NEXT_ELEMENT);
765                         }
766
767                         /*
768                          * neighbour cells due to periodic bc
769                          * => check boundary conditions
770                          */
771                         for(j=lc->dnlc;j<lc->countn;j++) {
772                                 this=&(neighbour[j]);
773                                 list_reset(this);
774                                 if(this->start!=NULL) {
775                                         do {
776                                                 btom=this->current->data;
777                                                 moldyn->pf_func2b(moldyn,
778                                                                   &(atom[i]),
779                                                                   btom);
780
781                         }
782
783                 }
784
785         return 0;
786 }
787
788
789 /* harmonic oscillator potential and force */
790
791 int harmonic_oscillator(t_moldyn *moldyn) {
792
793         t_ho_params *params;
794         t_atom *atom,*btom;
795         t_linkcell *lc;
796         t_list *this,neighbour[27];
797         int i,j,c;
798         int count;
799         t_3dvec force,distance;
800         double d,u;
801         double sc,equi_dist;
802         int ni,nj,nk;
803
804         params=moldyn->pot_params;
805         atom=moldyn->atom;
806         lc=&(moldyn->lc);
807         sc=params->spring_constant;
808         equi_dist=params->equilibrium_distance;
809         count=moldyn->count;
810
811         /* reset energy counter */
812         u=0.0;
813
814         for(i=0;i<count;i++) {
815                 /* reset force */
816                 v3_zero(&(atom[i].f));
817
818                 /* determine cell + neighbours */
819                 ni=(atom[i].r.x+(moldyn->dim.x/2))/lc->x;
820                 nj=(atom[i].r.y+(moldyn->dim.y/2))/lc->y;
821                 nk=(atom[i].r.z+(moldyn->dim.z/2))/lc->z;
822                 c=link_cell_neighbour_index(moldyn,ni,nj,nk,neighbour);
823
824                 /*
825                  * processing cell of atom i
826                  * => no need to check for empty list (1 element at minimum)
827                  */
828                 this=&(neighbour[0]);
829                 list_reset(this);
830                 do {
831                         btom=this->current->data;
832                         if(btom==&(atom[i]))
833                                 continue;
834                         v3_sub(&distance,&(atom[i].r),&(btom->r));
835                         d=v3_norm(&distance);
836                         if(d<=moldyn->cutoff) {
837                                 u+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
838                                 v3_scale(&force,&distance,
839                                          -sc*(1.0-(equi_dist/d)));
840                                 v3_add(&(atom[i].f),&(atom[i].f),&force);
841                         }
842                 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
843
844                 /*
845                  * direct neighbour cells
846                  * => no boundary condition check necessary
847                  */
848                 for(j=1;j<c;j++) {
849                         this=&(neighbour[j]);
850                         list_reset(this); /* there might not be a single atom */
851                         if(this->start!=NULL) {
852
853                         do {
854                                 btom=this->current->data;
855                                 v3_sub(&distance,&(atom[i].r),&(btom->r));
856                                 d=v3_norm(&distance);
857                                 if(d<=moldyn->cutoff) {
858                                         u+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
859                                         v3_scale(&force,&distance,
860                                                  -sc*(1.0-(equi_dist/d)));
861                                         v3_add(&(atom[i].f),&(atom[i].f),
862                                                &force);
863                                 }
864                         } while(list_next(this)!=L_NO_NEXT_ELEMENT);
865
866                         }
867                 }
868
869                 /*
870                  * indirect neighbour cells
871                  * => check boundary conditions
872                  */
873                 for(j=c;j<27;j++) {
874                         this=&(neighbour[j]);
875                         list_reset(this); /* check boundary conditions */
876                         if(this->start!=NULL) {
877
878                         do {
879                                 btom=this->current->data;
880                                 v3_sub(&distance,&(atom[i].r),&(btom->r));
881                                 v3_per_bound(&distance,&(moldyn->dim));
882                                 d=v3_norm(&distance);
883                                 if(d<=moldyn->cutoff) {
884                                         u+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
885                                         v3_scale(&force,&distance,
886                                                  -sc*(1.0-(equi_dist/d)));
887                                         v3_add(&(atom[i].f),&(atom[i].f),
888                                                &force);
889                                 }
890                         } while(list_next(this)!=L_NO_NEXT_ELEMENT);
891
892                         }
893                 }
894         }
895
896         moldyn->energy=0.5*u;
897
898         return 0;
899 }
900
901 /* lennard jones potential & force for one sort of atoms */
902  
903 int lennard_jones(t_moldyn *moldyn) {
904
905         t_lj_params *params;
906         t_atom *atom,*btom;
907         t_linkcell *lc;
908         t_list *this,neighbour[27];
909         int i,j,c;
910         int count;
911         t_3dvec force,distance;
912         double d,h1,h2,u;
913         double eps,sig6,sig12;
914         int ni,nj,nk;
915
916         params=moldyn->pot_params;
917         atom=moldyn->atom;
918         lc=&(moldyn->lc);
919         count=moldyn->count;
920         eps=params->epsilon4;
921         sig6=params->sigma6;
922         sig12=params->sigma12;
923
924         /* reset energy counter */
925         u=0.0;
926
927         for(i=0;i<count;i++) {
928                 /* reset force */
929                 v3_zero(&(atom[i].f));
930
931                 /* determine cell + neighbours */
932                 ni=(atom[i].r.x+(moldyn->dim.x/2))/lc->x;
933                 nj=(atom[i].r.y+(moldyn->dim.y/2))/lc->y;
934                 nk=(atom[i].r.z+(moldyn->dim.z/2))/lc->z;
935                 c=link_cell_neighbour_index(moldyn,ni,nj,nk,neighbour);
936
937                 /* processing cell of atom i */
938                 this=&(neighbour[0]);
939                 list_reset(this); /* list has 1 element at minimum */
940                 do {
941                         btom=this->current->data;
942                         if(btom==&(atom[i]))
943                                 continue;
944                         v3_sub(&distance,&(atom[i].r),&(btom->r));
945                         d=v3_absolute_square(&distance);        /* 1/r^2 */
946                         if(d<=moldyn->cutoff_square) {
947                                 d=1.0/d;        /* 1/r^2 */
948                                 h2=d*d;         /* 1/r^4 */
949                                 h2*=d;          /* 1/r^6 */
950                                 h1=h2*h2;       /* 1/r^12 */
951                                 u+=eps*(sig12*h1-sig6*h2);
952                                 h2*=d;          /* 1/r^8 */
953                                 h1*=d;          /* 1/r^14 */
954                                 h2*=6*sig6;
955                                 h1*=12*sig12;
956                                 d=+h1-h2;
957                                 d*=eps;
958                                 v3_scale(&force,&distance,d);
959                                 v3_add(&(atom[i].f),&(atom[i].f),&force);
960                         }
961                 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
962
963                 /* neighbours not doing boundary condition overflow */
964                 for(j=1;j<c;j++) {
965                         this=&(neighbour[j]);
966                         list_reset(this); /* there might not be a single atom */
967                         if(this->start!=NULL) {
968
969                         do {
970                                 btom=this->current->data;
971                                 v3_sub(&distance,&(atom[i].r),&(btom->r));
972                                 d=v3_absolute_square(&distance); /* r^2 */
973                                 if(d<=moldyn->cutoff_square) {
974                                         d=1.0/d;        /* 1/r^2 */
975                                         h2=d*d;         /* 1/r^4 */
976                                         h2*=d;          /* 1/r^6 */
977                                         h1=h2*h2;       /* 1/r^12 */
978                                         u+=eps*(sig12*h1-sig6*h2);
979                                         h2*=d;          /* 1/r^8 */
980                                         h1*=d;          /* 1/r^14 */
981                                         h2*=6*sig6;
982                                         h1*=12*sig12;
983                                         d=+h1-h2;
984                                         d*=eps;
985                                         v3_scale(&force,&distance,d);
986                                         v3_add(&(atom[i].f),&(atom[i].f),
987                                                &force);
988                                 }
989                         } while(list_next(this)!=L_NO_NEXT_ELEMENT);
990                                 
991                         }
992                 }
993
994                 /* neighbours due to boundary conditions */
995                 for(j=c;j<27;j++) {
996                         this=&(neighbour[j]);
997                         list_reset(this); /* check boundary conditions */
998                         if(this->start!=NULL) {
999
1000                         do {
1001                                 btom=this->current->data;
1002                                 v3_sub(&distance,&(atom[i].r),&(btom->r));
1003                                 v3_per_bound(&distance,&(moldyn->dim));
1004                                 d=v3_absolute_square(&distance); /* r^2 */
1005                                 if(d<=moldyn->cutoff_square) {
1006                                         d=1.0/d;        /* 1/r^2 */
1007                                         h2=d*d;         /* 1/r^4 */
1008                                         h2*=d;          /* 1/r^6 */
1009                                         h1=h2*h2;       /* 1/r^12 */
1010                                         u+=eps*(sig12*h1-sig6*h2);
1011                                         h2*=d;          /* 1/r^8 */
1012                                         h1*=d;          /* 1/r^14 */
1013                                         h2*=6*sig6;
1014                                         h1*=12*sig12;
1015                                         d=+h1-h2;
1016                                         d*=eps;
1017                                         v3_scale(&force,&distance,d);
1018                                         v3_add(&(atom[i].f),&(atom[i].f),
1019                                                &force);
1020                                 }
1021                         } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1022
1023                         }
1024                 }
1025         }
1026
1027         moldyn->energy=0.5*u;
1028         
1029         return 0;
1030 }
1031
1032 /* tersoff potential & force for 2 sorts of atoms */
1033
1034 int tersoff(t_moldyn *moldyn) {
1035
1036         t_tersoff_params *params;
1037         t_atom *atom,*btom,*ktom;
1038         t_linkcell *lc;
1039         t_list *this,*thisk,neighbour[27],neighbourk[27];
1040         int i,j,k,c,ck;
1041         int count;
1042         double u;
1043         int ni,nj,nk;
1044         int ki,kj,kk;
1045         
1046
1047         params=moldyn->pot_params;
1048         atom=moldyn->atom;
1049         lc=&(moldyn->lc);
1050         count=moldyn->count;
1051         
1052         /* reset energy counter */
1053         u=0.0;
1054
1055         for(i=0;i<count;i++) {
1056                 /* reset force */
1057                 v3_zero(&(atom[i].f));
1058
1059                 /* determin cell neighbours */
1060                 ni=(atom[i].r.x+(moldyn->dim.x/2))/lc->x;
1061                 nj=(atom[i].r.y+(moldyn->dim.y/2))/lc->y;
1062                 nk=(atom[i].r.z+(moldyn->dim.z/2))/lc->z;
1063                 c=link_cell_neighbour_index(moldyn,ni,nj,nk,neighbour);
1064
1065                 /*
1066                  * processing cell of atom i
1067                  * => no need to check for empty list (1 element at minimum)
1068                  */
1069                 this=&(neighbour[0]);
1070                 list_reset(this);
1071                 do {
1072                         btom=this->current->data;
1073                         if(btom==&(atom[i]))
1074                                 continue;
1075
1076                         /* 2 body stuff */
1077
1078                         /* we need: f_c, df_c, f_r, df_r */
1079
1080                         v3_sub(&dist_ij,btom,&(atom[i]));
1081                         d_ij=v3_norm(&dist_ij);
1082                         if(d_ij<=S) {
1083
1084                                 /* determine the tersoff parameters */
1085                                 if(atom[i].element!=btom->element) {
1086                                 S=sqrt(TERSOFF_S[e1]*TERSOFF_S[e2]);
1087                                 R=R_m;
1088                                 A=;
1089                                 lambda=;
1090                                 B=;
1091                                 mu=;
1092                                 chi=;
1093                                 beta=;
1094                                 betaN=;
1095
1096                                 if(d_ij<=R) {
1097                                         df_r=-lambda*A*exp(-lambda*d_ij)/d_ij;
1098                                         v3_scale(&force,&dist_ij,df_r);
1099                                         v3_add(&(atom[i].f),&(atom[i].f),
1100                                                 &force);
1101                                 }
1102                                 else {
1103                                         s_r=S-R;
1104                                         arg1=PI*(d_ij-R)/s_r;
1105                                         f_c=0.5+0.5*cos(arg1);
1106                                         df_c=-0.5*sin(arg1)*(PI/(s_r*d_ij));
1107                                         f_r=A*exp(-lambda*d_ij);
1108                                         df_r=-lambda*f_r/d_ij;
1109                                         scale=df_c*f_r+df_r*f_c;
1110                                         v3_scale(&force,&dist_ij,scale);
1111                                         v3_add(&(atom[i].f),&(atom[i].f),
1112                                                &force);
1113                                 }
1114                         }
1115                         else 
1116                                 continue;               
1117
1118                         
1119                         /* end 2 body stuff */ 
1120
1121                         /* determine cell neighbours of btom */
1122                         ki=(btom->r.x+(moldyn->dim.x/2))/lc->x;
1123                         kj=(btom->r.y+(moldyn->dim.y/2))/lc->y;
1124                         kk=(btom->r.z+(moldyn->dim.z/2))/lc->z;
1125                         ck=link_cell_neighbour_index(moldyn,ki,kj,kk,
1126                                                      neighbourk);
1127
1128                         /* go for zeta - 3 body stuff! */
1129                         zeta=0.0;
1130                         d_ij2=d_ij*d_ij;
1131
1132                         /* cell of btom */
1133                         thisk=&(neighbourk[0]);
1134                         list_reset(thisk);
1135                         do {
1136                                 ktom=thisk->current->data;
1137                                 if(ktom==btom)
1138                                         continue;
1139                                 if(ktom==&(atom[i]))
1140                                         continue;
1141                                 
1142                                 /* 3 body stuff (1) */
1143                                 
1144                                 v3_sub(&dist_ik,ktom,&(atom[i]));
1145                                 d_ik=v3_norm(&dist_ik);
1146                                 if(d_ik<=Sik) {
1147
1148                                 Rik=;
1149                                 Sik=;
1150                                 Aik=;
1151                                 lambda_ik=;
1152                                 Bik=;
1153                                 mu_ik=;
1154                                 omega_ik=;
1155                                 c_i=;
1156                                 d_i=;
1157                                 h_i=;
1158                         
1159
1160                                         if(d_ik<=Rik) {
1161                                                 f_cik=1.0;
1162                                                 df_cik=0.0;
1163                                         }
1164                                         else {
1165                                                 sik_rik=Sik-Rik;
1166                                                 arg1ik=PI*(d_ik-Rik)/sik_rik;
1167                                                 f_cik=0.5+0.5*cos(arg1ik);
1168                                                 df_cik=-0.5*sin(arg1ik)* \
1169                                                        (PI/(sik_rik*d_ik));
1170                                                 f_rik=Aik*exp(-lambda_ik*d_ik);
1171                                                 f_aik=-Bik*exp(-mu_ik*d_ik);
1172                                         }
1173                         
1174                                         v3_sub(&distance_jk,ktom,btom);
1175                                         cos_theta=(d_ij2+d_ik*d_ik-d_jk*d_jk)/\
1176                                                   (2*d_ij*d_ik);
1177                                         sin_theta=sqrt(1.0/\
1178                                                   (cos_theta*cos_theta));
1179                                         theta=arccos(cos_theta);
1180
1181                                         
1182                                 }
1183                                 else
1184                                         continue;
1185
1186                                 /* end 3 body stuff (1) */
1187
1188
1189                         } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1190
1191                         /* direct neighbours of btom cell */
1192                         for(k=1;k<ck;k++) {
1193                                 thisk=&(neighbourk[k]);
1194                                 list_reset(thisk);
1195                                 if(thisk->start!=NULL) {
1196
1197                                 do {
1198                                         ktom=thisk->current->data;
1199                                         if(ktom==&(atom[i]))
1200                                                 continue;
1201
1202                                 /* 3 body stuff (2) */
1203
1204                                 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1205
1206                                 }
1207                         }
1208
1209                         /* indirect neighbours of btom cell */
1210                         for(k=ck;k<27;k++) {
1211                                 thisk=&(neighbourk[k]);
1212                                 list_reset(thisk);
1213                                 if(thisk->start!=NULL) {
1214
1215                                 do {
1216                                         ktom=thisk->current->data;
1217                                         if(ktom==&(atom[i]))
1218                                                 continue;
1219
1220                                 /* 3 body stuff */
1221
1222                                 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1223
1224                                 }
1225                         }
1226
1227
1228                 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1229
1230                 /*
1231                  * direct neighbour cells of atom i
1232                  */
1233                 for(j=1;j<c;j++) {
1234                         this=&(neighbour[j]);
1235                         list_reset(this);
1236                         if(this->start!=NULL) {
1237
1238                         do {
1239                                 btom=this->current->data;
1240
1241                                 /* 2 body stuff */
1242
1243
1244                         /* determine cell neighbours of btom */
1245                         ki=(btom->r.x+(moldyn->dim.x/2))/lc->x;
1246                         kj=(btom->r.y+(moldyn->dim.y/2))/lc->y;
1247                         kk=(btom->r.z+(moldyn->dim.z/2))/lc->z;
1248                         ck=link_cell_neighbour_index(moldyn,ki,kj,kk,
1249                                                      neighbourk);
1250
1251                         /* cell of btom */
1252                         thisk=&(neighbourk[0]);
1253                         list_reset(thisk);
1254                         do {
1255                                 ktom=thisk->current->data;
1256                                 if(ktom==btom)
1257                                         continue;
1258                                 if(ktom==&(atom[i]))
1259                                         continue;
1260                                 
1261                                 /* 3 body stuff (1) */
1262
1263                         } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1264
1265                         /* direct neighbours of btom cell */
1266                         for(k=1;k<ck;k++) {
1267                                 thisk=&(neighbourk[k]);
1268                                 list_reset(thisk);
1269                                 if(thisk->start!=NULL) {
1270
1271                                 do {
1272                                         ktom=thisk->current->data;
1273                                         if(ktom==&(atom[i]))
1274                                                 continue;
1275
1276                                 /* 3 body stuff (2) */
1277
1278                                 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1279
1280                                 }
1281                         }
1282
1283                         /* indirect neighbours of btom cell */
1284                         for(k=ck;k<27;k++) {
1285                                 thisk=&(neighbourk[k]);
1286                                 list_reset(thisk);
1287                                 if(thisk->start!=NULL) {
1288
1289                                 do {
1290                                         ktom=thisk->current->data;
1291                                         if(ktom==&(atom[i]))
1292                                                 continue;
1293
1294                                 /* 3 body stuff (3) */
1295
1296                                 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1297
1298                                 }
1299                         }
1300
1301
1302                         } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1303
1304                         }
1305                 }
1306
1307                 /*
1308                  * indirect neighbour cells of atom i
1309                  */
1310                 for(j=c;j<27;j++) {
1311                         this=&(neighbour[j]);
1312                         list_reset(this);
1313                         if(this->start!=NULL) {
1314
1315                         do {
1316                                 btom=this->current->data;
1317
1318                                 /* 2 body stuff */
1319
1320
1321                         /* determine cell neighbours of btom */
1322                         ki=(btom->r.x+(moldyn->dim.x/2))/lc->x;
1323                         kj=(btom->r.y+(moldyn->dim.y/2))/lc->y;
1324                         kk=(btom->r.z+(moldyn->dim.z/2))/lc->z;
1325                         ck=link_cell_neighbour_index(moldyn,ki,kj,kk,
1326                                                      neighbourk);
1327
1328                         /* cell of btom */
1329                         thisk=&(neighbourk[0]);
1330                         list_reset(thisk);
1331                         do {
1332                                 ktom=thisk->current->data;
1333                                 if(ktom==btom)
1334                                         continue;
1335                                 if(ktom==&(atom[i]))
1336                                         continue;
1337                                 
1338                                 /* 3 body stuff (1) */
1339
1340                         } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1341
1342                         /* direct neighbours of btom cell */
1343                         for(k=1;k<ck;k++) {
1344                                 thisk=&(neighbourk[k]);
1345                                 list_reset(thisk);
1346                                 if(thisk->start!=NULL) {
1347
1348                                 do {
1349                                         ktom=thisk->current->data;
1350                                         if(ktom==&(atom[i]))
1351                                                 continue;
1352
1353                                 /* 3 body stuff (2) */
1354
1355                                 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1356
1357                                 }
1358                         }
1359
1360                         /* indirect neighbours of btom cell */
1361                         for(k=ck;k<27;k++) {
1362                                 thisk=&(neighbourk[k]);
1363                                 list_reset(thisk);
1364                                 if(thisk->start!=NULL) {
1365
1366                                 do {
1367                                         ktom=thisk->current->data;
1368                                         if(ktom==&(atom[i]))
1369                                                 continue;
1370
1371                                 /* 3 body stuff (3) */
1372
1373                                 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1374
1375                                 }
1376                         }
1377
1378
1379                         } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1380
1381                         }
1382                 }
1383                 
1384         }
1385
1386         moldyn->energy=0.5*u;
1387
1388         return 0;
1389 }
1390