nearly finished tersoff 2 body part and new api
[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(u8 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         u8 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 int potential_force_calc(t_moldyn *moldyn) {
698
699         int i,count;
700         t_atom *atom;
701         t_linkcell *lc;
702         t_list neighbour[27];
703         t_list *this;
704         double u;
705         u8 bc,bc3;
706         int countn,dnlc;
707
708         count=moldyn->count;
709         atom=moldyn->atom;
710         lc=&(moldyn->lc);
711
712         /* reset energy */
713         moldyn->energy=0.0;
714
715         for(i=0;i<count;i++) {
716         
717                 /* reset force */
718                 v3_zero(&(atom[i].f));
719
720                 /* single particle potential/force */
721                 if(atom[i].attr&ATOM_ATTR_1BP)
722                         moldyn->pf_func1b(moldyn,&(atom[i]));
723
724                 /* 2 body pair potential/force */
725                 if(atom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)) {
726                 
727                         link_cell_neighbour_index(moldyn,
728                                 (atom[i].r.x+moldyn->dim.x/2)/lc->x,
729                                 (atom[i].r.y+moldyn->dim.y/2)/lc->y,
730                                 (atom[i].r.z+moldyn->dim.z/2)/lc->z,
731                                 neighbour);
732
733                         countn=lc->countn;
734                         dnlc=lc->dnlc;
735
736                         for(j=0;j<countn;j++) {
737
738                                 this=&(neighbour[j]);
739                                 list_reset(this);
740
741                                 if(this->start==NULL)
742                                         continue;
743
744                                 bc=(j<dnlc)?0:1;
745
746                                 do {
747                                         btom=this->current->data;
748
749                                         if(btom==&(atom[i]))
750                                                 continue;
751
752                                         if((btom->attr&ATOM_ATTR_2BP)&
753                                            (atom[i].attr&ATOM_ATTR_2BP))
754                                                 moldyn->pf_func2b(moldyn,
755                                                                   &(atom[i]),
756                                                                   btom,
757                                                                   bc);
758
759                                         /* 3 body potential/force */
760
761                                         if(!(atom[i].attr&ATOM_ATTR_3BP)||
762                                            !(btom->attr&ATOM_ATTR_3BP))
763                                                 continue;
764
765                                         link_cell_neighbour_index(moldyn,
766                                            (btom->r.x+moldyn->dim.x/2)/lc->x,
767                                            (btom->r.y+moldyn->dim.y/2)/lc->y,
768                                            (btom->r.z+moldyn->dim.z/2)/lc->z,
769                                            neighbourk);
770
771                                         for(k=0;k<lc->countn;k++) {
772
773                                                 thisk=&(neighbourk[k]);
774                                                 list_reset(thisk);
775                                         
776                                                 if(thisk->start==NULL)
777                                                         continue;
778
779                                                 bck=(k<lc->dnlc)?0:1;
780
781                                                 do {
782
783                         ktom=thisk->current->data;
784
785                         if(!(ktom->attr&ATOM_ATTR_3BP))
786                                 continue;
787
788                         if(ktom==btom)
789                                 continue;
790
791                         if(ktom==&(atom[i]))
792                                 continue;
793
794                         moldyn->pf_func3b(moldyn,&(atom[i]),btom,ktom,bck);
795
796                                                 } while(list_next(thisk)!=\
797                                                         L_NO_NEXT_ELEMENT);
798                                         
799                                 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
800                         }
801                 }
802         }
803
804         return 0;
805 }
806
807 /*
808  * periodic boundayr checking
809  */
810
811 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
812         
813         double x,y,z;
814
815         x=0.5*dim->x;
816         y=0.5*dim->y;
817         z=0.5*dim->z;
818
819         if(moldyn->MOLDYN_ATTR_PBX)
820                 if(a->x>=x) a->x-=dim->x;
821                 else if(-a->x>x) a->x+=dim->x;
822         if(moldyn->MOLDYN_ATTR_PBY)
823                 if(a->y>=y) a->y-=dim->y;
824                 else if(-a->y>y) a->y+=dim->y;
825         if(moldyn->MOLDYN_ATTR_PBZ)
826                 if(a->z>=z) a->z-=dim->z;
827                 else if(-a->z>z) a->z+=dim->z;
828
829         return 0;
830 }
831         
832
833 /*
834  * example potentials
835  */
836
837 /* harmonic oscillator potential and force */
838
839 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc)) {
840
841         t_ho_params *params;
842         t_3dvec force,distance;
843         double d;
844         double sc,equi_dist;
845
846         params=moldyn->pot2b_params;
847         sc=params->spring_constant;
848         equi_dist=params->equilibrium_distance;
849
850         v3_sub(&distance,&(ai->r),&(aj->r);
851         
852         v3_per_bound(&distance,&(moldyn->dim));
853         if(bc) check_per_bound(moldyn,&distance);
854         d=v3_norm(&distance);
855         if(d<=moldyn->cutoff) {
856                 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
857                 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
858                 v3_scale(&force,&distance,-sc*(1.0-(equi_dist/d)));
859                 v3_add(&(ai->f),&(ai->f),&force);
860         }
861
862         return 0;
863 }
864
865 /* lennard jones potential & force for one sort of atoms */
866  
867 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
868
869         t_lj_params *params;
870         t_3dvec force,distance;
871         double d,h1,h2,u;
872         double eps,sig6,sig12;
873
874         params=moldyn->pot_params;
875         eps=params->epsilon4;
876         sig6=params->sigma6;
877         sig12=params->sigma12;
878
879         v3_sub(&distance,&(ai->r),&(aj->r));
880         if(bc) check_per_bound(moldyn,&distance);
881         d=v3_absolute_square(&distance);        /* 1/r^2 */
882         if(d<=moldyn->cutoff_square) {
883                 d=1.0/d;                        /* 1/r^2 */
884                 h2=d*d;                         /* 1/r^4 */
885                 h2*=d;                          /* 1/r^6 */
886                 h1=h2*h2;                       /* 1/r^12 */
887                 /* energy is eps*..., but we will add this twice ... */
888                 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
889                 h2*=d;                          /* 1/r^8 */
890                 h1*=d;                          /* 1/r^14 */
891                 h2*=6*sig6;
892                 h1*=12*sig12;
893                 d=+h1-h2;
894                 d*=eps;
895                 v3_scale(&force,&distance,d);
896                 v3_add(&(ai->f),&(aj->f),&force);
897         }
898
899         return 0;
900 }
901
902 /*
903  * tersoff potential & force for 2 sorts of atoms
904  */
905
906 /* tersoff 2 body part */
907
908 int tersoff_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
909
910         t_tersoff_params *params;
911         t_3dvec dist_ij;
912         double d_ij;
913
914         params=moldyn->pot_params;
915         
916         /*
917          * we need: f_c, df_c, f_r, df_r
918          *
919          * therefore we need: R, S
920          */
921
922         v3_sub(&dist_ij,&(ai->r),&(aj->r));
923
924         if(bc) check_per_bound(moldyn,&dist_ij);
925
926         if(ai->bnum==aj->bnum) {
927                 S=params->S[ai->bnum];
928                 R=params->R[ai->bnum];
929         }
930         else {
931                 S=params->Smixed;
932                 R=params->Rmixed;
933         }
934
935         d_ij=v3_norm(&dist_ij);
936
937         if(d_ij<=S) {
938                 f_r=A*exp(-lamda*d_ij);
939                 df_r=-lambda*f_r/d_ij;
940                 if(d_ij<R) {
941                         /* f_c = 1, df_c = 0 */
942                         v3_scale(&force,&dist_ij,df_r);
943                 }
944                 else {
945                         s_r=S-R;
946                         arg=PI*(d_ij-R)/s_r;
947                         f_c=0.5+0.5*cos(arg);
948                         df_c=-0.5*sin(arg)*(PI/(s_r*d_ij));
949                         scale=df_c*f_r+df_r*f_c;
950                         v3_scale(&force,&dist_ij,scale);
951                         v3_add(&(ai->f),&(ai->f),&force);
952                 }
953                 moldyn->energy+=(f_r*f_c);
954         }
955
956         return 0;
957 }
958
959 /* tersoff 3 body part */
960
961 int tersoff(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc,u8 bck) {
962
963         t_tersoff_params *params;
964         t_3dvec dist_ij;
965         double d_ij;
966
967         params=moldyn->pot_params;
968         
969         /* 2 body part of the tersoff potential */
970
971         v3_sub(&dist_ij,&(ai->r),&(aj->r));
972         if(bc) check_per_bound(moldyn,&dist_ij);
973         d_ij=v3_norm(&dist_ij);
974                 if(d_ij<=S) {
975
976                                 /* determine the tersoff parameters */
977                                 if(atom[i].element!=btom->element) {
978                                 S=sqrt(TERSOFF_S[e1]*TERSOFF_S[e2]);
979                                 R=R_m;
980                                 A=;
981                                 lambda=;
982                                 B=;
983                                 mu=;
984                                 chi=;
985                                 beta=;
986                                 betaN=;
987
988                                 if(d_ij<=R) {
989                                         df_r=-lambda*A*exp(-lambda*d_ij)/d_ij;
990                                         v3_scale(&force,&dist_ij,df_r);
991                                         v3_add(&(atom[i].f),&(atom[i].f),
992                                                 &force);
993                                 }
994                                 else {
995                                         s_r=S-R;
996                                         arg1=PI*(d_ij-R)/s_r;
997                                         f_c=0.5+0.5*cos(arg1);
998                                         df_c=-0.5*sin(arg1)*(PI/(s_r*d_ij));
999                                         f_r=A*exp(-lambda*d_ij);
1000                                         df_r=-lambda*f_r/d_ij;
1001                                         scale=df_c*f_r+df_r*f_c;
1002                                         v3_scale(&force,&dist_ij,scale);
1003                                         v3_add(&(atom[i].f),&(atom[i].f),
1004                                                &force);
1005                                 }
1006                         }
1007                         else 
1008                                 continue;               
1009
1010                         
1011                         /* end 2 body stuff */ 
1012
1013                         /* determine cell neighbours of btom */
1014                         ki=(btom->r.x+(moldyn->dim.x/2))/lc->x;
1015                         kj=(btom->r.y+(moldyn->dim.y/2))/lc->y;
1016                         kk=(btom->r.z+(moldyn->dim.z/2))/lc->z;
1017                         ck=link_cell_neighbour_index(moldyn,ki,kj,kk,
1018                                                      neighbourk);
1019
1020                         /* go for zeta - 3 body stuff! */
1021                         zeta=0.0;
1022                         d_ij2=d_ij*d_ij;
1023
1024                         /* cell of btom */
1025                         thisk=&(neighbourk[0]);
1026                         list_reset(thisk);
1027                         do {
1028                                 ktom=thisk->current->data;
1029                                 if(ktom==btom)
1030                                         continue;
1031                                 if(ktom==&(atom[i]))
1032                                         continue;
1033                                 
1034                                 /* 3 body stuff (1) */
1035                                 
1036                                 v3_sub(&dist_ik,ktom,&(atom[i]));
1037                                 d_ik=v3_norm(&dist_ik);
1038                                 if(d_ik<=Sik) {
1039
1040                                 Rik=;
1041                                 Sik=;
1042                                 Aik=;
1043                                 lambda_ik=;
1044                                 Bik=;
1045                                 mu_ik=;
1046                                 omega_ik=;
1047                                 c_i=;
1048                                 d_i=;
1049                                 h_i=;
1050                         
1051
1052                                         if(d_ik<=Rik) {
1053                                                 f_cik=1.0;
1054                                                 df_cik=0.0;
1055                                         }
1056                                         else {
1057                                                 sik_rik=Sik-Rik;
1058                                                 arg1ik=PI*(d_ik-Rik)/sik_rik;
1059                                                 f_cik=0.5+0.5*cos(arg1ik);
1060                                                 df_cik=-0.5*sin(arg1ik)* \
1061                                                        (PI/(sik_rik*d_ik));
1062                                                 f_rik=Aik*exp(-lambda_ik*d_ik);
1063                                                 f_aik=-Bik*exp(-mu_ik*d_ik);
1064                                         }
1065                         
1066                                         v3_sub(&distance_jk,ktom,btom);
1067                                         cos_theta=(d_ij2+d_ik*d_ik-d_jk*d_jk)/\
1068                                                   (2*d_ij*d_ik);
1069                                         sin_theta=sqrt(1.0/\
1070                                                   (cos_theta*cos_theta));
1071                                         theta=arccos(cos_theta);
1072
1073                                         
1074                                 }
1075                                 else
1076                                         continue;
1077
1078                                 /* end 3 body stuff (1) */
1079
1080
1081                         } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1082
1083                         /* direct neighbours of btom cell */
1084                         for(k=1;k<ck;k++) {
1085                                 thisk=&(neighbourk[k]);
1086                                 list_reset(thisk);
1087                                 if(thisk->start!=NULL) {
1088
1089                                 do {
1090                                         ktom=thisk->current->data;
1091                                         if(ktom==&(atom[i]))
1092                                                 continue;
1093
1094                                 /* 3 body stuff (2) */
1095
1096                                 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1097
1098                                 }
1099                         }
1100
1101                         /* indirect neighbours of btom cell */
1102                         for(k=ck;k<27;k++) {
1103                                 thisk=&(neighbourk[k]);
1104                                 list_reset(thisk);
1105                                 if(thisk->start!=NULL) {
1106
1107                                 do {
1108                                         ktom=thisk->current->data;
1109                                         if(ktom==&(atom[i]))
1110                                                 continue;
1111
1112                                 /* 3 body stuff */
1113
1114                                 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1115
1116                                 }
1117                         }
1118
1119
1120                 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1121
1122                 /*
1123                  * direct neighbour cells of atom i
1124                  */
1125                 for(j=1;j<c;j++) {
1126                         this=&(neighbour[j]);
1127                         list_reset(this);
1128                         if(this->start!=NULL) {
1129
1130                         do {
1131                                 btom=this->current->data;
1132
1133                                 /* 2 body stuff */
1134
1135
1136                         /* determine cell neighbours of btom */
1137                         ki=(btom->r.x+(moldyn->dim.x/2))/lc->x;
1138                         kj=(btom->r.y+(moldyn->dim.y/2))/lc->y;
1139                         kk=(btom->r.z+(moldyn->dim.z/2))/lc->z;
1140                         ck=link_cell_neighbour_index(moldyn,ki,kj,kk,
1141                                                      neighbourk);
1142
1143                         /* cell of btom */
1144                         thisk=&(neighbourk[0]);
1145                         list_reset(thisk);
1146                         do {
1147                                 ktom=thisk->current->data;
1148                                 if(ktom==btom)
1149                                         continue;
1150                                 if(ktom==&(atom[i]))
1151                                         continue;
1152                                 
1153                                 /* 3 body stuff (1) */
1154
1155                         } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1156
1157                         /* direct neighbours of btom cell */
1158                         for(k=1;k<ck;k++) {
1159                                 thisk=&(neighbourk[k]);
1160                                 list_reset(thisk);
1161                                 if(thisk->start!=NULL) {
1162
1163                                 do {
1164                                         ktom=thisk->current->data;
1165                                         if(ktom==&(atom[i]))
1166                                                 continue;
1167
1168                                 /* 3 body stuff (2) */
1169
1170                                 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1171
1172                                 }
1173                         }
1174
1175                         /* indirect neighbours of btom cell */
1176                         for(k=ck;k<27;k++) {
1177                                 thisk=&(neighbourk[k]);
1178                                 list_reset(thisk);
1179                                 if(thisk->start!=NULL) {
1180
1181                                 do {
1182                                         ktom=thisk->current->data;
1183                                         if(ktom==&(atom[i]))
1184                                                 continue;
1185
1186                                 /* 3 body stuff (3) */
1187
1188                                 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1189
1190                                 }
1191                         }
1192
1193
1194                         } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1195
1196                         }
1197                 }
1198
1199                 /*
1200                  * indirect neighbour cells of atom i
1201                  */
1202                 for(j=c;j<27;j++) {
1203                         this=&(neighbour[j]);
1204                         list_reset(this);
1205                         if(this->start!=NULL) {
1206
1207                         do {
1208                                 btom=this->current->data;
1209
1210                                 /* 2 body stuff */
1211
1212
1213                         /* determine cell neighbours of btom */
1214                         ki=(btom->r.x+(moldyn->dim.x/2))/lc->x;
1215                         kj=(btom->r.y+(moldyn->dim.y/2))/lc->y;
1216                         kk=(btom->r.z+(moldyn->dim.z/2))/lc->z;
1217                         ck=link_cell_neighbour_index(moldyn,ki,kj,kk,
1218                                                      neighbourk);
1219
1220                         /* cell of btom */
1221                         thisk=&(neighbourk[0]);
1222                         list_reset(thisk);
1223                         do {
1224                                 ktom=thisk->current->data;
1225                                 if(ktom==btom)
1226                                         continue;
1227                                 if(ktom==&(atom[i]))
1228                                         continue;
1229                                 
1230                                 /* 3 body stuff (1) */
1231
1232                         } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1233
1234                         /* direct neighbours of btom cell */
1235                         for(k=1;k<ck;k++) {
1236                                 thisk=&(neighbourk[k]);
1237                                 list_reset(thisk);
1238                                 if(thisk->start!=NULL) {
1239
1240                                 do {
1241                                         ktom=thisk->current->data;
1242                                         if(ktom==&(atom[i]))
1243                                                 continue;
1244
1245                                 /* 3 body stuff (2) */
1246
1247                                 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1248
1249                                 }
1250                         }
1251
1252                         /* indirect neighbours of btom cell */
1253                         for(k=ck;k<27;k++) {
1254                                 thisk=&(neighbourk[k]);
1255                                 list_reset(thisk);
1256                                 if(thisk->start!=NULL) {
1257
1258                                 do {
1259                                         ktom=thisk->current->data;
1260                                         if(ktom==&(atom[i]))
1261                                                 continue;
1262
1263                                 /* 3 body stuff (3) */
1264
1265                                 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1266
1267                                 }
1268                         }
1269
1270
1271                         } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1272
1273                         }
1274                 }
1275                 
1276         }
1277
1278         moldyn->energy=0.5*u;
1279
1280         return 0;
1281 }
1282