nearly finished tersoff potential ...
[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 1 body part */
907 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
908
909         int num;
910         t_tersoff_mult_params *params;
911         t_tersoff_exchange *exchange;
912         
913         num=ai->bnum;
914         params=moldyn->pot1b_params;
915         exchange=&(params->exchange);
916
917         /*
918          * simple: point constant parameters only depending on atom i to
919          *         their right values
920          */
921
922         exchange->beta=&(params->beta[num]);
923         exchange->n=&(params->n[num]);
924         exchange->c=&(params->c[num]);
925         exchange->d=&(params->d[num]);
926         exchange->h=&(params->h[num]);
927
928         exchange->c2=params->c[num]*params->c[num];
929         exchange->d2=params->d[num]*params->d[num];
930         exchange->c2d2=exchange->c2/exchange->d2;
931
932         return 0;
933 }
934         
935 /* tersoff 2 body part */
936 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
937
938         t_tersoff_mult_params *params;
939         t_tersoff_exchange *exchange;
940         t_3dvec dist_ij;
941         double d_ij;
942         double A,B,R,S,lambda;
943         int num;
944
945         params=moldyn->pot_params;
946         num=ai->bnum;
947         exchange=&(params->exchange);
948
949         exchange->run3bp=0;
950         
951         /*
952          * we need: f_c, df_c, f_r, df_r
953          *
954          * therefore we need: R, S, A, lambda
955          */
956
957         v3_sub(&dist_ij,&(ai->r),&(aj->r));
958
959         if(bc) check_per_bound(moldyn,&dist_ij);
960
961         /* save for use in 3bp */ /* REALLY ?!?!?! */
962         exchange->dist_ij=dist_ij;
963
964         /* constants */
965         if(num==aj->bnum) {
966                 S=params->S[num];
967                 R=params->R[num];
968                 A=params->A[num];
969                 lambda=params->lambda[num];
970                 /* more constants depending of atoms i and j, needed in 3bp */
971                 params->exchange.B=&(params->B[num]);
972                 params->exchange.mu=params->mu[num];
973                 params->exchange.chi=1.0;
974         }
975         else {
976                 S=params->Smixed;
977                 R=params->Rmixed;
978                 A=params->Amixed;
979                 lambda=params->lambda_m;
980                 /* more constants depending of atoms i and j, needed in 3bp */
981                 params->exchange.B=&(params->Bmixed);
982                 params->exchange.mu=&(params->mu_m);
983                 params->exchange.chi=params->chi;
984         }
985
986         d_ij=v3_norm(&dist_ij);
987
988         /* save for use in 3bp */
989         exchange->d_ij=d_ij;
990
991         if(d_ij>S)
992                 return 0;
993
994         f_r=A*exp(-lamda*d_ij);
995         df_r=-lambda*f_r/d_ij;
996
997         /* f_a, df_a calc + save for 3bp use */
998         exchange->f_a=-B*exp(-mu*d_ij);
999         exchange->df_a=-mu*exchange->f_a/d_ij;
1000
1001         if(d_ij<R) {
1002                 /* f_c = 1, df_c = 0 */
1003                 f_c=1.0;
1004                 df_c=0.0;
1005                 v3_scale(&force,&dist_ij,df_r);
1006         }
1007         else {
1008                 s_r=S-R;
1009                 arg=PI*(d_ij-R)/s_r;
1010                 f_c=0.5+0.5*cos(arg);
1011                 df_c=-0.5*sin(arg)*(PI/(s_r*d_ij));
1012                 scale=df_c*f_r+df_r*f_c;
1013                 v3_scale(&force,&dist_ij,scale);
1014         }
1015
1016         /* add forces */
1017         v3_add(&(ai->f),&(ai->f),&force);
1018         /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1019         moldyn->energy+=(0.25*f_r*f_c);
1020
1021         /* save for use in 3bp */
1022         exchange->f_c=f_c;
1023         exchange->df_c=df_c;
1024
1025         /* enable the run of 3bp function */
1026         exchange->run3bp=1;
1027
1028         return 0;
1029 }
1030
1031 /* tersoff 3 body part */
1032
1033 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1034
1035         t_tersoff_mult_params *params;
1036         t_tersoff_exchange *exchange;
1037         t_3dvec dist_ij,dist_ik,dist_jk;
1038         t_3dvec db_ij,temp,force;
1039         double R,S,s_r;
1040         double d_ij,d_ik,d_jk;
1041         double f_c,df_c,b_ij,f_a,df_a;
1042         double B,mu;
1043         int num;
1044
1045         params=moldyn->pot_params;
1046         num=ai->bnum;
1047         exchange=params->exchange;
1048
1049         if(!(exchange->run3bp))
1050                 return 0;
1051
1052         /*
1053          * we need: f_c, d_fc, b_ij, db_ij, f_a, df_a
1054          *
1055          * we got f_c, df_c, f_a, df_a from 2bp calculation
1056          */
1057
1058         d_ij=exchange->d_ij;
1059         d_ij2=exchange->d_ij2;
1060
1061         B=*(params->exchange.B);
1062         mu=*(params->exchange.mu);
1063
1064         f_a=params->exchange.f_a;
1065         df_a=params->exchange.df_a;
1066         
1067         /* d_ij is <= S, as we didn't return so far! */
1068
1069         /*
1070          * calc of b_ij (scalar) and db_ij (vector)
1071          *
1072          * - for b_ij: chi, beta, f_c_ik, w(=1), c, d, h, n, cos_theta
1073          *
1074          * - for db_ij: d_theta, sin_theta, cos_theta, f_c_ik, df_c_ik,
1075          *              w_ik,
1076          *
1077          */
1078
1079         
1080         v3_sub(&dist_ik,&(aj->i),&(ak->r));
1081         if(bc) check_per_bound(moldyn,&dist_ik);
1082         d_ik=v3_norm(&dist_ik);
1083
1084         /* constants for f_c_ik calc */
1085         if(num==ak->bnum) {
1086                 R=params->R[num];
1087                 S=params->S[num];
1088         }
1089         else {
1090                 R=params->Rmixed;
1091                 S=params->Smixed;
1092         }
1093
1094         /* calc of f_c_ik */
1095         if(d_ik>S)
1096                 return 0;
1097
1098         if(d_ik<R) {
1099                 /* f_c_ik = 1, df_c_ik = 0 */
1100                 f_c_ik=1.0;
1101                 df_c_ik=0.0;
1102         }
1103         else {
1104                 s_r=S-R;
1105                 arg=PI*(d_ik-R)/s_r;
1106                 f_c_ik=0.5+0.5*cos(arg);
1107                 df_c_ik=-0.5*sin(arg)*(PI/(s_r*d_ik));
1108         }
1109         
1110         v3_sub(&dist_jk,&(aj->r),&(ak->r));
1111         if(bc) check_per_bound(moldyn,&dist_jk);
1112         d_jk=v3_norm(&dist_jk);
1113
1114
1115         // GO ON HERE !!!
1116
1117         cos_theta=(d_ij2+d_ik*d_ik-d_jk*d_jk)/(2*d_ij*d_ik);
1118         sin_theta=sqrt(1.0-(cos_theta*cos_theta));
1119         theta=arccos(cos_theta);
1120
1121         h_cos=(h-cos_theta);
1122         h_cos2=h_cos*h_cos;
1123         d2_h_cos2=d2-h_cos2;
1124
1125         /* add forces */
1126         v3_add(&(ai->f),&(ai->f),&force);
1127         /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1128         moldyn->energy+=(0.25*f_a*b_ij*f_c);
1129                                 
1130         return 0;
1131 }
1132