bugfix dg of dV_ji,jk
[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 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
21
22         memset(moldyn,0,sizeof(t_moldyn));
23
24         rand_init(&(moldyn->random),NULL,1);
25         moldyn->random.status|=RAND_STAT_VERBOSE;
26
27         return 0;
28 }
29
30 int moldyn_shutdown(t_moldyn *moldyn) {
31
32         printf("[moldyn] shutdown\n");
33         moldyn_log_shutdown(moldyn);
34         link_cell_shutdown(moldyn);
35         rand_close(&(moldyn->random));
36         free(moldyn->atom);
37
38         return 0;
39 }
40
41 int set_int_alg(t_moldyn *moldyn,u8 algo) {
42
43         switch(algo) {
44                 case MOLDYN_INTEGRATE_VERLET:
45                         moldyn->integrate=velocity_verlet;
46                         break;
47                 default:
48                         printf("unknown integration algorithm: %02x\n",algo);
49                         return -1;
50         }
51
52         return 0;
53 }
54
55 int set_cutoff(t_moldyn *moldyn,double cutoff) {
56
57         moldyn->cutoff=cutoff;
58
59         return 0;
60 }
61
62 int set_temperature(t_moldyn *moldyn,double t_ref) {
63
64         moldyn->t_ref=t_ref;
65
66         return 0;
67 }
68
69 int set_pressure(t_moldyn *moldyn,double p_ref) {
70
71         moldyn->p_ref=p_ref;
72
73         return 0;
74 }
75
76 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
77
78         moldyn->pt_scale=(ptype|ttype);
79         moldyn->t_tc=ttc;
80         moldyn->p_tc=ptc;
81
82         return 0;
83 }
84
85 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
86
87         moldyn->dim.x=x;
88         moldyn->dim.y=y;
89         moldyn->dim.z=z;
90
91         moldyn->volume=x*y*z;
92
93         if(visualize) {
94                 moldyn->vis.dim.x=x;
95                 moldyn->vis.dim.y=y;
96                 moldyn->vis.dim.z=z;
97         }
98
99         printf("[moldyn] dimensions in A and A^3 respectively:\n");
100         printf("  x: %f\n",moldyn->dim.x);
101         printf("  y: %f\n",moldyn->dim.y);
102         printf("  z: %f\n",moldyn->dim.z);
103         printf("  volume: %f\n",moldyn->volume);
104         printf("  visualize simulation box: %s\n",visualize?"on":"off");
105
106         return 0;
107 }
108
109 int set_nn_dist(t_moldyn *moldyn,double dist) {
110
111         moldyn->nnd=dist;
112
113         return 0;
114 }
115
116 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
117
118         if(x)
119                 moldyn->status|=MOLDYN_STAT_PBX;
120
121         if(y)
122                 moldyn->status|=MOLDYN_STAT_PBY;
123
124         if(z)
125                 moldyn->status|=MOLDYN_STAT_PBZ;
126
127         return 0;
128 }
129
130 int set_potential1b(t_moldyn *moldyn,pf_func1b func,void *params) {
131
132         moldyn->func1b=func;
133         moldyn->pot1b_params=params;
134
135         return 0;
136 }
137
138 int set_potential2b(t_moldyn *moldyn,pf_func2b func,void *params) {
139
140         moldyn->func2b=func;
141         moldyn->pot2b_params=params;
142
143         return 0;
144 }
145
146 int set_potential2b_post(t_moldyn *moldyn,pf_func2b_post func,void *params) {
147
148         moldyn->func2b_post=func;
149         moldyn->pot2b_params=params;
150
151         return 0;
152 }
153
154 int set_potential3b(t_moldyn *moldyn,pf_func3b func,void *params) {
155
156         moldyn->func3b=func;
157         moldyn->pot3b_params=params;
158
159         return 0;
160 }
161
162 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
163
164         strncpy(moldyn->vlsdir,dir,127);
165
166         return 0;
167 }
168         
169 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
170
171         char filename[128];
172         int ret;
173
174         switch(type) {
175                 case LOG_TOTAL_ENERGY:
176                         moldyn->ewrite=timer;
177                         snprintf(filename,127,"%s/energy",moldyn->vlsdir);
178                         moldyn->efd=open(filename,
179                                          O_WRONLY|O_CREAT|O_EXCL,
180                                          S_IRUSR|S_IWUSR);
181                         if(moldyn->efd<0) {
182                                 perror("[moldyn] energy log fd open");
183                                 return moldyn->efd;
184                         }
185                         dprintf(moldyn->efd,"# total energy log file\n");
186                         break;
187                 case LOG_TOTAL_MOMENTUM:
188                         moldyn->mwrite=timer;
189                         snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
190                         moldyn->mfd=open(filename,
191                                          O_WRONLY|O_CREAT|O_EXCL,
192                                          S_IRUSR|S_IWUSR);
193                         if(moldyn->mfd<0) {
194                                 perror("[moldyn] momentum log fd open");
195                                 return moldyn->mfd;
196                         }
197                         dprintf(moldyn->efd,"# total momentum log file\n");
198                         break;
199                 case SAVE_STEP:
200                         moldyn->swrite=timer;
201                         break;
202                 case VISUAL_STEP:
203                         moldyn->vwrite=timer;
204                         ret=visual_init(&(moldyn->vis),moldyn->vlsdir);
205                         if(ret<0) {
206                                 printf("[moldyn] visual init failure\n");
207                                 return ret;
208                         }
209                         break;
210                 default:
211                         printf("[moldyn] unknown log mechanism: %02x\n",type);
212                         return -1;
213         }
214
215         return 0;
216 }
217
218 int moldyn_log_shutdown(t_moldyn *moldyn) {
219
220         printf("[moldyn] log shutdown\n");
221         if(moldyn->efd) close(moldyn->efd);
222         if(moldyn->mfd) close(moldyn->mfd);
223         if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
224
225         return 0;
226 }
227
228 /*
229  * creating lattice functions
230  */
231
232 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
233                    u8 attr,u8 brand,int a,int b,int c) {
234
235         int new,count;
236         int ret;
237         t_3dvec origin;
238         void *ptr;
239         t_atom *atom;
240
241         new=a*b*c;
242         count=moldyn->count;
243
244         /* how many atoms do we expect */
245         if(type==FCC) new*=4;
246         if(type==DIAMOND) new*=8;
247
248         /* allocate space for atoms */
249         ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
250         if(!ptr) {
251                 perror("[moldyn] realloc (create lattice)");
252                 return -1;
253         }
254         moldyn->atom=ptr;
255         atom=&(moldyn->atom[count]);
256                 
257         v3_zero(&origin);
258
259         switch(type) {
260                 case FCC:
261                         ret=fcc_init(a,b,c,lc,atom,&origin);
262                         break;
263                 case DIAMOND:
264                         ret=diamond_init(a,b,c,lc,atom,&origin);
265                         break;
266                 default:
267                         printf("unknown lattice type (%02x)\n",type);
268                         return -1;
269         }
270
271         /* debug */
272         if(ret!=new) {
273                 printf("[moldyn] creating lattice failed\n");
274                 printf("  amount of atoms\n");
275                 printf("  - expected: %d\n",new);
276                 printf("  - created: %d\n",ret);
277                 return -1;
278         }
279
280         moldyn->count+=new;
281         printf("[moldyn] created lattice with %d atoms\n",new);
282
283         for(ret=0;ret<new;ret++) {
284                 atom[ret].element=element;
285                 atom[ret].mass=mass;
286                 atom[ret].attr=attr;
287                 atom[ret].brand=brand;
288                 atom[ret].tag=count+ret;
289                 check_per_bound(moldyn,&(atom[ret].r));
290         }
291
292         return ret;
293 }
294
295 /* fcc lattice init */
296 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
297
298         int count;
299         int i,j;
300         t_3dvec o,r,n;
301         t_3dvec basis[3];
302         double help[3];
303         double x,y,z;
304
305         x=a*lc;
306         y=b*lc;
307         z=c*lc;
308
309         if(origin) v3_copy(&o,origin);
310         else v3_zero(&o);
311
312         /* construct the basis */
313         for(i=0;i<3;i++) {
314                 for(j=0;j<3;j++) {
315                         if(i!=j) help[j]=0.5*lc;
316                         else help[j]=.0;
317                 }
318                 v3_set(&basis[i],help);
319         }
320
321         v3_zero(&r);
322         count=0;
323         
324         /* fill up the room */
325         r.x=o.x;
326         while(r.x<x) {
327                 r.y=o.y;
328                 while(r.y<y) {
329                         r.z=o.z;
330                         while(r.z<z) {
331                                 v3_copy(&(atom[count].r),&r);
332                                 atom[count].element=1;
333                                 count+=1;
334                                 for(i=0;i<3;i++) {
335                                         v3_add(&n,&r,&basis[i]);
336                                         if((n.x<x+o.x)&&
337                                            (n.y<y+o.y)&&
338                                            (n.z<z+o.z)) {
339                                                 v3_copy(&(atom[count].r),&n);
340                                                 count+=1;
341                                         }
342                                 }
343                                 r.z+=lc;        
344                         }
345                         r.y+=lc;
346                 }
347                 r.x+=lc;
348         }
349
350         /* coordinate transformation */
351         help[0]=x/2.0;
352         help[1]=y/2.0;
353         help[2]=z/2.0;
354         v3_set(&n,help);
355         for(i=0;i<count;i++)
356                 v3_sub(&(atom[i].r),&(atom[i].r),&n);
357                 
358         return count;
359 }
360
361 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
362
363         int count;
364         t_3dvec o;
365
366         count=fcc_init(a,b,c,lc,atom,origin);
367
368         o.x=0.25*lc;
369         o.y=0.25*lc;
370         o.z=0.25*lc;
371
372         if(origin) v3_add(&o,&o,origin);
373
374         count+=fcc_init(a,b,c,lc,&atom[count],&o);
375
376         return count;
377 }
378
379 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
380              t_3dvec *r,t_3dvec *v) {
381
382         t_atom *atom;
383         void *ptr;
384         int count;
385         
386         atom=moldyn->atom;
387         count=(moldyn->count)++;
388
389         ptr=realloc(atom,(count+1)*sizeof(t_atom));
390         if(!ptr) {
391                 perror("[moldyn] realloc (add atom)");
392                 return -1;
393         }
394         moldyn->atom=ptr;
395
396         atom=moldyn->atom;
397         atom[count].r=*r;
398         atom[count].v=*v;
399         atom[count].element=element;
400         atom[count].mass=mass;
401         atom[count].brand=brand;
402         atom[count].tag=count;
403         atom[count].attr=attr;
404
405         return 0;
406 }
407
408 int destroy_atoms(t_moldyn *moldyn) {
409
410         if(moldyn->atom) free(moldyn->atom);
411
412         return 0;
413 }
414
415 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
416
417         /*
418          * - gaussian distribution of velocities
419          * - zero total momentum
420          * - velocity scaling (E = 3/2 N k T), E: kinetic energy
421          */
422
423         int i;
424         double v,sigma;
425         t_3dvec p_total,delta;
426         t_atom *atom;
427         t_random *random;
428
429         atom=moldyn->atom;
430         random=&(moldyn->random);
431
432         /* gaussian distribution of velocities */
433         v3_zero(&p_total);
434         for(i=0;i<moldyn->count;i++) {
435                 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
436                 /* x direction */
437                 v=sigma*rand_get_gauss(random);
438                 atom[i].v.x=v;
439                 p_total.x+=atom[i].mass*v;
440                 /* y direction */
441                 v=sigma*rand_get_gauss(random);
442                 atom[i].v.y=v;
443                 p_total.y+=atom[i].mass*v;
444                 /* z direction */
445                 v=sigma*rand_get_gauss(random);
446                 atom[i].v.z=v;
447                 p_total.z+=atom[i].mass*v;
448         }
449
450         /* zero total momentum */
451         v3_scale(&p_total,&p_total,1.0/moldyn->count);
452         for(i=0;i<moldyn->count;i++) {
453                 v3_scale(&delta,&p_total,1.0/atom[i].mass);
454                 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
455         }
456
457         /* velocity scaling */
458         scale_velocity(moldyn,equi_init);
459
460         return 0;
461 }
462
463 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
464
465         int i;
466         double e,scale;
467         t_atom *atom;
468         int count;
469
470         atom=moldyn->atom;
471
472         /*
473          * - velocity scaling (E = 3/2 N k T), E: kinetic energy
474          */
475
476         /* get kinetic energy / temperature & count involved atoms */
477         e=0.0;
478         count=0;
479         for(i=0;i<moldyn->count;i++) {
480                 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
481                         e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
482                         count+=1;
483                 }
484         }
485         if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
486         else return 0;  /* no atoms involved in scaling! */
487         
488         /* (temporary) hack for e,t = 0 */
489         if(e==0.0) {
490         moldyn->t=0.0;
491                 if(moldyn->t_ref!=0.0) {
492                         thermal_init(moldyn,equi_init);
493                         return 0;
494                 }
495                 else
496                         return 0; /* no scaling needed */
497         }
498
499
500         /* get scaling factor */
501         scale=moldyn->t_ref/moldyn->t;
502         if(equi_init&TRUE)
503                 scale*=2.0;
504         else
505                 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
506                         scale=1.0+(scale-1.0)/moldyn->t_tc;
507         scale=sqrt(scale);
508
509         /* velocity scaling */
510         for(i=0;i<moldyn->count;i++) {
511                 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
512                         v3_scale(&(atom[i].v),&(atom[i].v),scale);
513         }
514
515         return 0;
516 }
517
518 int scale_volume(t_moldyn *moldyn) {
519
520         t_atom *atom;
521         t_3dvec *dim,*vdim;
522         double scale,v;
523         t_virial virial;
524         t_linkcell *lc;
525         int i;
526
527         atom=moldyn->atom;
528         dim=&(moldyn->dim);
529         vdim=&(moldyn->vis.dim);
530         lc=&(moldyn->lc);
531
532         memset(&virial,0,sizeof(t_virial));
533
534         for(i=0;i<moldyn->count;i++) {
535                 virial.xx+=atom[i].virial.xx;
536                 virial.yy+=atom[i].virial.yy;
537                 virial.zz+=atom[i].virial.zz;
538                 virial.xy+=atom[i].virial.xy;
539                 virial.xz+=atom[i].virial.xz;
540                 virial.yz+=atom[i].virial.yz;
541         }
542
543         /* just a guess so far ... */
544         v=virial.xx+virial.yy+virial.zz;
545
546 printf("%f\n",v);
547         /* get pressure from virial */
548         moldyn->p=moldyn->count*K_BOLTZMANN*moldyn->t+ONE_THIRD*v;
549         moldyn->p/=moldyn->volume;
550 printf("%f | %f\n",moldyn->p/(ATM),moldyn->p_ref/ATM);
551
552         /* scale factor */
553         if(moldyn->pt_scale&P_SCALE_BERENDSEN)
554                 scale=3*sqrt(1-(moldyn->p_ref-moldyn->p)/moldyn->p_tc);
555         else 
556                 /* should actually never be used */
557                 scale=pow(moldyn->p/moldyn->p_ref,1.0/3.0);
558
559 printf("scale = %f\n",scale);
560         /* actual scaling */
561         dim->x*=scale;
562         dim->y*=scale;
563         dim->z*=scale;
564         if(vdim->x) vdim->x=dim->x;
565         if(vdim->y) vdim->y=dim->y;
566         if(vdim->z) vdim->z=dim->z;
567         moldyn->volume*=(scale*scale*scale);
568
569         /* check whether we need a new linkcell init */
570         if((dim->x/moldyn->cutoff!=lc->nx)||
571            (dim->y/moldyn->cutoff!=lc->ny)||
572            (dim->z/moldyn->cutoff!=lc->nx)) {
573                 link_cell_shutdown(moldyn);
574                 link_cell_init(moldyn);
575         }
576
577         return 0;
578
579 }
580
581 double get_e_kin(t_moldyn *moldyn) {
582
583         int i;
584         t_atom *atom;
585
586         atom=moldyn->atom;
587         moldyn->ekin=0.0;
588
589         for(i=0;i<moldyn->count;i++)
590                 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
591
592         return moldyn->ekin;
593 }
594
595 double get_e_pot(t_moldyn *moldyn) {
596
597         return moldyn->energy;
598 }
599
600 double update_e_kin(t_moldyn *moldyn) {
601
602         return(get_e_kin(moldyn));
603 }
604
605 double get_total_energy(t_moldyn *moldyn) {
606
607         return(moldyn->ekin+moldyn->energy);
608 }
609
610 t_3dvec get_total_p(t_moldyn *moldyn) {
611
612         t_3dvec p,p_total;
613         int i;
614         t_atom *atom;
615
616         atom=moldyn->atom;
617
618         v3_zero(&p_total);
619         for(i=0;i<moldyn->count;i++) {
620                 v3_scale(&p,&(atom[i].v),atom[i].mass);
621                 v3_add(&p_total,&p_total,&p);
622         }
623
624         return p_total;
625 }
626
627 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
628
629         double tau;
630
631         /* nn_dist is the nearest neighbour distance */
632
633         tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
634
635         return tau;     
636 }
637
638 /*
639  * numerical tricks
640  */
641
642 /* linked list / cell method */
643
644 int link_cell_init(t_moldyn *moldyn) {
645
646         t_linkcell *lc;
647         int i;
648
649         lc=&(moldyn->lc);
650
651         /* partitioning the md cell */
652         lc->nx=moldyn->dim.x/moldyn->cutoff;
653         lc->x=moldyn->dim.x/lc->nx;
654         lc->ny=moldyn->dim.y/moldyn->cutoff;
655         lc->y=moldyn->dim.y/lc->ny;
656         lc->nz=moldyn->dim.z/moldyn->cutoff;
657         lc->z=moldyn->dim.z/lc->nz;
658
659         lc->cells=lc->nx*lc->ny*lc->nz;
660         lc->subcell=malloc(lc->cells*sizeof(t_list));
661
662         printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
663
664         for(i=0;i<lc->cells;i++)
665                 list_init_f(&(lc->subcell[i]));
666
667         link_cell_update(moldyn);
668         
669         return 0;
670 }
671
672 int link_cell_update(t_moldyn *moldyn) {
673
674         int count,i,j,k;
675         int nx,ny;
676         t_atom *atom;
677         t_linkcell *lc;
678         double x,y,z;
679
680         atom=moldyn->atom;
681         lc=&(moldyn->lc);
682
683         nx=lc->nx;
684         ny=lc->ny;
685
686         x=moldyn->dim.x/2;
687         y=moldyn->dim.y/2;
688         z=moldyn->dim.z/2;
689
690         for(i=0;i<lc->cells;i++)
691                 list_destroy_f(&(lc->subcell[i]));
692         
693         for(count=0;count<moldyn->count;count++) {
694                 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
695                 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
696                 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
697                 list_add_immediate_f(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
698                                      &(atom[count]));
699         }
700
701         return 0;
702 }
703
704 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
705
706         t_linkcell *lc;
707         int a;
708         int count1,count2;
709         int ci,cj,ck;
710         int nx,ny,nz;
711         int x,y,z;
712         u8 bx,by,bz;
713
714         lc=&(moldyn->lc);
715         nx=lc->nx;
716         ny=lc->ny;
717         nz=lc->nz;
718         count1=1;
719         count2=27;
720         a=nx*ny;
721
722         cell[0]=lc->subcell[i+j*nx+k*a];
723         for(ci=-1;ci<=1;ci++) {
724                 bx=0;
725                 x=i+ci;
726                 if((x<0)||(x>=nx)) {
727                         x=(x+nx)%nx;
728                         bx=1;
729                 }
730                 for(cj=-1;cj<=1;cj++) {
731                         by=0;
732                         y=j+cj;
733                         if((y<0)||(y>=ny)) {
734                                 y=(y+ny)%ny;
735                                 by=1;
736                         }
737                         for(ck=-1;ck<=1;ck++) {
738                                 bz=0;
739                                 z=k+ck;
740                                 if((z<0)||(z>=nz)) {
741                                         z=(z+nz)%nz;
742                                         bz=1;
743                                 }
744                                 if(!(ci|cj|ck)) continue;
745                                 if(bx|by|bz) {
746                                         cell[--count2]=lc->subcell[x+y*nx+z*a];
747                                 }
748                                 else {
749                                         cell[count1++]=lc->subcell[x+y*nx+z*a];
750                                 }
751                         }
752                 }
753         }
754
755         lc->dnlc=count1;
756
757         return count1;
758 }
759
760 int link_cell_shutdown(t_moldyn *moldyn) {
761
762         int i;
763         t_linkcell *lc;
764
765         lc=&(moldyn->lc);
766
767         for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
768                 list_destroy_f(&(moldyn->lc.subcell[i]));
769
770         free(lc->subcell);
771
772         return 0;
773 }
774
775 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
776
777         int count;
778         void *ptr;
779         t_moldyn_schedule *schedule;
780
781         schedule=&(moldyn->schedule);
782         count=++(schedule->total_sched);
783
784         ptr=realloc(schedule->runs,count*sizeof(int));
785         if(!ptr) {
786                 perror("[moldyn] realloc (runs)");
787                 return -1;
788         }
789         schedule->runs=ptr;
790         schedule->runs[count-1]=runs;
791
792         ptr=realloc(schedule->tau,count*sizeof(double));
793         if(!ptr) {
794                 perror("[moldyn] realloc (tau)");
795                 return -1;
796         }
797         schedule->tau=ptr;
798         schedule->tau[count-1]=tau;
799
800         return 0;
801 }
802
803 int moldyn_set_schedule_hook(t_moldyn *moldyn,void *hook,void *hook_params) {
804
805         moldyn->schedule.hook=hook;
806         moldyn->schedule.hook_params=hook_params;
807         
808         return 0;
809 }
810
811 /*
812  *
813  * 'integration of newtons equation' - algorithms
814  *
815  */
816
817 /* start the integration */
818
819 int moldyn_integrate(t_moldyn *moldyn) {
820
821         int i;
822         unsigned int e,m,s,v;
823         t_3dvec p;
824         t_moldyn_schedule *sched;
825         t_atom *atom;
826         int fd;
827         char dir[128];
828         double ds;
829
830         sched=&(moldyn->schedule);
831         atom=moldyn->atom;
832
833         /* initialize linked cell method */
834         link_cell_init(moldyn);
835
836         /* logging & visualization */
837         e=moldyn->ewrite;
838         m=moldyn->mwrite;
839         s=moldyn->swrite;
840         v=moldyn->vwrite;
841
842         /* sqaure of some variables */
843         moldyn->tau_square=moldyn->tau*moldyn->tau;
844         moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
845
846         /* calculate initial forces */
847         potential_force_calc(moldyn);
848
849         /* some stupid checks before we actually start calculating bullshit */
850         if(moldyn->cutoff>0.5*moldyn->dim.x)
851                 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
852         if(moldyn->cutoff>0.5*moldyn->dim.y)
853                 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
854         if(moldyn->cutoff>0.5*moldyn->dim.z)
855                 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
856         ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
857         if(ds>0.05*moldyn->nnd)
858                 printf("[moldyn] warning: forces too high / tau too small!\n");
859
860         /* zero absolute time */
861         moldyn->time=0.0;
862
863         /* debugging, ignore */
864         moldyn->debug=0;
865
866         /* executing the schedule */
867         for(sched->count=0;sched->count<sched->total_sched;sched->count++) {
868
869                 /* setting amount of runs and finite time step size */
870                 moldyn->tau=sched->tau[sched->count];
871                 moldyn->tau_square=moldyn->tau*moldyn->tau;
872                 moldyn->time_steps=sched->runs[sched->count];
873
874         /* integration according to schedule */
875
876         for(i=0;i<moldyn->time_steps;i++) {
877
878                 /* integration step */
879                 moldyn->integrate(moldyn);
880
881                 /* p/t scaling */
882                 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
883                         scale_velocity(moldyn,FALSE);
884                 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
885                         scale_volume(moldyn);
886
887                 /* check for log & visualization */
888                 if(e) {
889                         if(!(i%e))
890                                 dprintf(moldyn->efd,
891                                         "%f %f %f %f\n",
892                                         moldyn->time,update_e_kin(moldyn),
893                                         moldyn->energy,
894                                         get_total_energy(moldyn));
895                 }
896                 if(m) {
897                         if(!(i%m)) {
898                                 p=get_total_p(moldyn);
899                                 dprintf(moldyn->mfd,
900                                         "%f %f\n",moldyn->time,v3_norm(&p));
901                         }
902                 }
903                 if(s) {
904                         if(!(i%s)) {
905                                 snprintf(dir,128,"%s/s-%07.f.save",
906                                          moldyn->vlsdir,moldyn->time);
907                                 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT);
908                                 if(fd<0) perror("[moldyn] save fd open");
909                                 else {
910                                         write(fd,moldyn,sizeof(t_moldyn));
911                                         write(fd,moldyn->atom,
912                                               moldyn->count*sizeof(t_atom));
913                                 }
914                                 close(fd);
915                         }       
916                 }
917                 if(v) {
918                         if(!(i%v)) {
919                                 visual_atoms(&(moldyn->vis),moldyn->time,
920                                              moldyn->atom,moldyn->count);
921                                 printf("\rsched: %d, steps: %d, debug: %d",
922                                        sched->count,i,moldyn->debug);
923                                 fflush(stdout);
924                         }
925                 }
926
927                 /* increase absolute time */
928                 moldyn->time+=moldyn->tau;
929
930         }
931
932                 /* check for hooks */
933                 if(sched->hook)
934                         sched->hook(moldyn,sched->hook_params);
935
936                 /* get a new info line */
937                 printf("\n");
938
939         }
940
941         return 0;
942 }
943
944 /* velocity verlet */
945
946 int velocity_verlet(t_moldyn *moldyn) {
947
948         int i,count;
949         double tau,tau_square;
950         t_3dvec delta;
951         t_atom *atom;
952
953         atom=moldyn->atom;
954         count=moldyn->count;
955         tau=moldyn->tau;
956         tau_square=moldyn->tau_square;
957
958         for(i=0;i<count;i++) {
959                 /* new positions */
960                 v3_scale(&delta,&(atom[i].v),tau);
961                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
962                 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
963                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
964                 check_per_bound(moldyn,&(atom[i].r));
965
966                 /* velocities */
967                 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
968                 v3_add(&(atom[i].v),&(atom[i].v),&delta);
969         }
970
971         /* neighbour list update */
972         link_cell_update(moldyn);
973
974         /* forces depending on chosen potential */
975         potential_force_calc(moldyn);
976
977         for(i=0;i<count;i++) {
978                 /* again velocities */
979                 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
980                 v3_add(&(atom[i].v),&(atom[i].v),&delta);
981         }
982
983         return 0;
984 }
985
986
987 /*
988  *
989  * potentials & corresponding forces
990  * 
991  */
992
993 /* generic potential and force calculation */
994
995 int potential_force_calc(t_moldyn *moldyn) {
996
997         int i,j,k,count;
998         t_atom *itom,*jtom,*ktom;
999         t_virial *virial;
1000         t_linkcell *lc;
1001         t_list neighbour_i[27];
1002         t_list neighbour_i2[27];
1003         t_list *this,*that;
1004         u8 bc_ij,bc_ik;
1005         int dnlc;
1006
1007         count=moldyn->count;
1008         itom=moldyn->atom;
1009         lc=&(moldyn->lc);
1010
1011         /* reset energy */
1012         moldyn->energy=0.0;
1013         
1014         /* get energy and force of every atom */
1015         for(i=0;i<count;i++) {
1016
1017                 /* reset force */
1018                 v3_zero(&(itom[i].f));
1019
1020                 /* reset viral of atom i */
1021                 virial=&(itom[i].virial);
1022                 virial->xx=0.0;
1023                 virial->yy=0.0;
1024                 virial->zz=0.0;
1025                 virial->xy=0.0;
1026                 virial->xz=0.0;
1027                 virial->yz=0.0;
1028
1029                 /* reset site energy */
1030                 itom[i].e=0.0;
1031
1032                 /* single particle potential/force */
1033                 if(itom[i].attr&ATOM_ATTR_1BP)
1034                         moldyn->func1b(moldyn,&(itom[i]));
1035
1036                 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1037                         continue;
1038
1039                 /* 2 body pair potential/force */
1040         
1041                 link_cell_neighbour_index(moldyn,
1042                                           (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1043                                           (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1044                                           (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1045                                           neighbour_i);
1046
1047                 dnlc=lc->dnlc;
1048
1049                 for(j=0;j<27;j++) {
1050
1051                         this=&(neighbour_i[j]);
1052                         list_reset_f(this);
1053
1054                         if(this->start==NULL)
1055                                 continue;
1056
1057                         bc_ij=(j<dnlc)?0:1;
1058
1059                         do {
1060                                 jtom=this->current->data;
1061
1062                                 if(jtom==&(itom[i]))
1063                                         continue;
1064
1065                                 if((jtom->attr&ATOM_ATTR_2BP)&
1066                                    (itom[i].attr&ATOM_ATTR_2BP)) {
1067                                         moldyn->func2b(moldyn,
1068                                                        &(itom[i]),
1069                                                        jtom,
1070                                                        bc_ij);
1071                                 }
1072
1073                                 /* 3 body potential/force */
1074
1075                                 if(!(itom[i].attr&ATOM_ATTR_3BP)||
1076                                    !(jtom->attr&ATOM_ATTR_3BP))
1077                                         continue;
1078
1079                                 /* copy the neighbour lists */
1080                                 memcpy(neighbour_i2,neighbour_i,
1081                                        27*sizeof(t_list));
1082
1083                                 /* get neighbours of i */
1084                                 for(k=0;k<27;k++) {
1085
1086                                         that=&(neighbour_i2[k]);
1087                                         list_reset_f(that);
1088                                         
1089                                         if(that->start==NULL)
1090                                                 continue;
1091
1092                                         bc_ik=(k<dnlc)?0:1;
1093
1094                                         do {
1095
1096                                                 ktom=that->current->data;
1097
1098                                                 if(!(ktom->attr&ATOM_ATTR_3BP))
1099                                                         continue;
1100
1101                                                 if(ktom==jtom)
1102                                                         continue;
1103
1104                                                 if(ktom==&(itom[i]))
1105                                                         continue;
1106
1107                                                 moldyn->func3b(moldyn,
1108                                                                &(itom[i]),
1109                                                                jtom,
1110                                                                ktom,
1111                                                                bc_ik|bc_ij);
1112
1113                                         } while(list_next_f(that)!=\
1114                                                 L_NO_NEXT_ELEMENT);
1115
1116                                 }
1117
1118                                 /* 2bp post function */
1119                                 if(moldyn->func2b_post) {
1120                                         moldyn->func2b_post(moldyn,
1121                                                             &(itom[i]),
1122                                                             jtom,bc_ij);
1123                                 }
1124                                         
1125                         } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1126                 
1127                 }
1128
1129         }
1130 #ifdef DEBUG
1131 printf("\n\n");
1132 #endif
1133 #ifdef VDEBUG
1134 printf("\n\n");
1135 #endif
1136
1137         return 0;
1138 }
1139
1140 /*
1141  * periodic boundayr checking
1142  */
1143
1144 inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1145         
1146         double x,y,z;
1147         t_3dvec *dim;
1148
1149         dim=&(moldyn->dim);
1150
1151         x=dim->x/2;
1152         y=dim->y/2;
1153         z=dim->z/2;
1154
1155         if(moldyn->status&MOLDYN_STAT_PBX) {
1156                 if(a->x>=x) a->x-=dim->x;
1157                 else if(-a->x>x) a->x+=dim->x;
1158         }
1159         if(moldyn->status&MOLDYN_STAT_PBY) {
1160                 if(a->y>=y) a->y-=dim->y;
1161                 else if(-a->y>y) a->y+=dim->y;
1162         }
1163         if(moldyn->status&MOLDYN_STAT_PBZ) {
1164                 if(a->z>=z) a->z-=dim->z;
1165                 else if(-a->z>z) a->z+=dim->z;
1166         }
1167
1168         return 0;
1169 }
1170         
1171
1172 /*
1173  * example potentials
1174  */
1175
1176 /* harmonic oscillator potential and force */
1177
1178 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1179
1180         t_ho_params *params;
1181         t_3dvec force,distance;
1182         double d;
1183         double sc,equi_dist;
1184
1185         params=moldyn->pot2b_params;
1186         sc=params->spring_constant;
1187         equi_dist=params->equilibrium_distance;
1188
1189         v3_sub(&distance,&(aj->r),&(ai->r));
1190         
1191         if(bc) check_per_bound(moldyn,&distance);
1192         d=v3_norm(&distance);
1193         if(d<=moldyn->cutoff) {
1194                 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
1195                 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
1196                 /* f = -grad E; grad r_ij = -1 1/r_ij distance */
1197                 v3_scale(&force,&distance,sc*(1.0-(equi_dist/d)));
1198                 v3_add(&(ai->f),&(ai->f),&force);
1199         }
1200
1201         return 0;
1202 }
1203
1204 /* lennard jones potential & force for one sort of atoms */
1205  
1206 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1207
1208         t_lj_params *params;
1209         t_3dvec force,distance;
1210         double d,h1,h2;
1211         double eps,sig6,sig12;
1212
1213         params=moldyn->pot2b_params;
1214         eps=params->epsilon4;
1215         sig6=params->sigma6;
1216         sig12=params->sigma12;
1217
1218         v3_sub(&distance,&(aj->r),&(ai->r));
1219         if(bc) check_per_bound(moldyn,&distance);
1220         d=v3_absolute_square(&distance);        /* 1/r^2 */
1221         if(d<=moldyn->cutoff_square) {
1222                 d=1.0/d;                        /* 1/r^2 */
1223                 h2=d*d;                         /* 1/r^4 */
1224                 h2*=d;                          /* 1/r^6 */
1225                 h1=h2*h2;                       /* 1/r^12 */
1226                 /* energy is eps*..., but we will add this twice ... */
1227                 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
1228                 h2*=d;                          /* 1/r^8 */
1229                 h1*=d;                          /* 1/r^14 */
1230                 h2*=6*sig6;
1231                 h1*=12*sig12;
1232                 d=+h1-h2;
1233                 d*=eps;
1234                 v3_scale(&force,&distance,-1.0*d); /* f = - grad E */
1235                 v3_add(&(ai->f),&(ai->f),&force);
1236         }
1237
1238         return 0;
1239 }
1240
1241 /*
1242  * tersoff potential & force for 2 sorts of atoms
1243  */
1244
1245 /* create mixed terms from parameters and set them */
1246 int tersoff_mult_complete_params(t_tersoff_mult_params *p) {
1247
1248         printf("[moldyn] tersoff parameter completion\n");
1249         p->S2[0]=p->S[0]*p->S[0];
1250         p->S2[1]=p->S[1]*p->S[1];
1251         p->Smixed=sqrt(p->S[0]*p->S[1]);
1252         p->S2mixed=p->Smixed*p->Smixed;
1253         p->Rmixed=sqrt(p->R[0]*p->R[1]);
1254         p->Amixed=sqrt(p->A[0]*p->A[1]);
1255         p->Bmixed=sqrt(p->B[0]*p->B[1]);
1256         p->lambda_m=0.5*(p->lambda[0]+p->lambda[1]);
1257         p->mu_m=0.5*(p->mu[0]+p->mu[1]);
1258
1259         printf("[moldyn] tersoff mult parameter info:\n");
1260         printf("  S (A)  | %f | %f | %f\n",p->S[0],p->S[1],p->Smixed);
1261         printf("  R (A)  | %f | %f | %f\n",p->R[0],p->R[1],p->Rmixed);
1262         printf("  A (eV) | %f | %f | %f\n",p->A[0]/EV,p->A[1]/EV,p->Amixed/EV);
1263         printf("  B (eV) | %f | %f | %f\n",p->B[0]/EV,p->B[1]/EV,p->Bmixed/EV);
1264         printf("  lambda | %f | %f | %f\n",p->lambda[0],p->lambda[1],
1265                                           p->lambda_m);
1266         printf("  mu     | %f | %f | %f\n",p->mu[0],p->mu[1],p->mu_m);
1267         printf("  beta   | %.10f | %.10f\n",p->beta[0],p->beta[1]);
1268         printf("  n      | %f | %f\n",p->n[0],p->n[1]);
1269         printf("  c      | %f | %f\n",p->c[0],p->c[1]);
1270         printf("  d      | %f | %f\n",p->d[0],p->d[1]);
1271         printf("  h      | %f | %f\n",p->h[0],p->h[1]);
1272         printf("  chi    | %f \n",p->chi);
1273
1274         return 0;
1275 }
1276
1277 /* tersoff 1 body part */
1278 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
1279
1280         int brand;
1281         t_tersoff_mult_params *params;
1282         t_tersoff_exchange *exchange;
1283         
1284         brand=ai->brand;
1285         params=moldyn->pot1b_params;
1286         exchange=&(params->exchange);
1287
1288         /*
1289          * simple: point constant parameters only depending on atom i to
1290          *         their right values
1291          */
1292
1293         exchange->beta_i=&(params->beta[brand]);
1294         exchange->n_i=&(params->n[brand]);
1295         exchange->c_i=&(params->c[brand]);
1296         exchange->d_i=&(params->d[brand]);
1297         exchange->h_i=&(params->h[brand]);
1298
1299         exchange->betaini=pow(*(exchange->beta_i),*(exchange->n_i));
1300         exchange->ci2=params->c[brand]*params->c[brand];
1301         exchange->di2=params->d[brand]*params->d[brand];
1302         exchange->ci2di2=exchange->ci2/exchange->di2;
1303
1304         return 0;
1305 }
1306         
1307 /* tersoff 2 body part */
1308 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1309
1310         t_tersoff_mult_params *params;
1311         t_tersoff_exchange *exchange;
1312         t_3dvec dist_ij,force;
1313         double d_ij,d_ij2;
1314         double A,B,R,S,S2,lambda,mu;
1315         double f_r,df_r;
1316         double f_c,df_c;
1317         int brand;
1318         double s_r;
1319         double arg;
1320
1321         params=moldyn->pot2b_params;
1322         brand=aj->brand;
1323         exchange=&(params->exchange);
1324
1325         /* clear 3bp and 2bp post run */
1326         exchange->run3bp=0;
1327         exchange->run2bp_post=0;
1328
1329         /* reset S > r > R mark */
1330         exchange->d_ij_between_rs=0;
1331         
1332         /*
1333          * calc of 2bp contribution of V_ij and dV_ij/ji
1334          *
1335          * for Vij and dV_ij we need:
1336          * - f_c_ij, df_c_ij
1337          * - f_r_ij, df_r_ij
1338          *
1339          * for dV_ji we need:
1340          * - f_c_ji = f_c_ij, df_c_ji = df_c_ij
1341          * - f_r_ji = f_r_ij; df_r_ji = df_r_ij
1342          *
1343          */
1344
1345         /* constants */
1346         if(brand==ai->brand) {
1347                 S=params->S[brand];
1348                 S2=params->S2[brand];
1349                 R=params->R[brand];
1350                 A=params->A[brand];
1351                 B=params->B[brand];
1352                 lambda=params->lambda[brand];
1353                 mu=params->mu[brand];
1354                 exchange->chi=1.0;
1355         }
1356         else {
1357                 S=params->Smixed;
1358                 S2=params->S2mixed;
1359                 R=params->Rmixed;
1360                 A=params->Amixed;
1361                 B=params->Bmixed;
1362                 lambda=params->lambda_m;
1363                 mu=params->mu_m;
1364                 params->exchange.chi=params->chi;
1365         }
1366
1367         /* dist_ij, d_ij */
1368         v3_sub(&dist_ij,&(aj->r),&(ai->r));
1369         if(bc) check_per_bound(moldyn,&dist_ij);
1370         d_ij2=v3_absolute_square(&dist_ij);
1371
1372         /* if d_ij2 > S2 => no force & potential energy contribution */
1373         if(d_ij2>S2)
1374                 return 0;
1375
1376         /* now we will need the distance */
1377         //d_ij=v3_norm(&dist_ij);
1378         d_ij=sqrt(d_ij2);
1379
1380         /* save for use in 3bp */
1381         exchange->d_ij=d_ij;
1382         exchange->d_ij2=d_ij2;
1383         exchange->dist_ij=dist_ij;
1384
1385         /* more constants */
1386         exchange->beta_j=&(params->beta[brand]);
1387         exchange->n_j=&(params->n[brand]);
1388         exchange->c_j=&(params->c[brand]);
1389         exchange->d_j=&(params->d[brand]);
1390         exchange->h_j=&(params->h[brand]);
1391         if(brand==ai->brand) {
1392                 exchange->betajnj=exchange->betaini;
1393                 exchange->cj2=exchange->ci2;
1394                 exchange->dj2=exchange->di2;
1395                 exchange->cj2dj2=exchange->ci2di2;
1396         }
1397         else {
1398                 exchange->betajnj=pow(*(exchange->beta_j),*(exchange->n_j));
1399                 exchange->cj2=params->c[brand]*params->c[brand];
1400                 exchange->dj2=params->d[brand]*params->d[brand];
1401                 exchange->cj2dj2=exchange->cj2/exchange->dj2;
1402         }
1403
1404         /* f_r_ij = f_r_ji, df_r_ij = df_r_ji */
1405         f_r=A*exp(-lambda*d_ij);
1406         df_r=lambda*f_r/d_ij;
1407
1408         /* f_a, df_a calc (again, same for ij and ji) | save for later use! */
1409         exchange->f_a=-B*exp(-mu*d_ij);
1410         exchange->df_a=mu*exchange->f_a/d_ij;
1411
1412         /* f_c, df_c calc (again, same for ij and ji) */
1413         if(d_ij<R) {
1414                 /* f_c = 1, df_c = 0 */
1415                 f_c=1.0;
1416                 df_c=0.0;
1417                 /* two body contribution (ij, ji) */
1418                 v3_scale(&force,&dist_ij,-df_r);
1419         }
1420         else {
1421                 s_r=S-R;
1422                 arg=M_PI*(d_ij-R)/s_r;
1423                 f_c=0.5+0.5*cos(arg);
1424                 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
1425                 /* two body contribution (ij, ji) */
1426                 v3_scale(&force,&dist_ij,-df_c*f_r-df_r*f_c);
1427                 /* tell 3bp that S > r > R */
1428                 exchange->d_ij_between_rs=1;
1429         }
1430
1431         /* add forces of 2bp (ij, ji) contribution
1432          * dVij = dVji and we sum up both: no 1/2) */
1433         v3_add(&(ai->f),&(ai->f),&force);
1434
1435         /* virial */
1436         ai->virial.xx-=force.x*dist_ij.x;
1437         ai->virial.yy-=force.y*dist_ij.y;
1438         ai->virial.zz-=force.z*dist_ij.z;
1439         ai->virial.xy-=force.x*dist_ij.y;
1440         ai->virial.xz-=force.x*dist_ij.z;
1441         ai->virial.yz-=force.y*dist_ij.z;
1442
1443 #ifdef DEBUG
1444 if(ai==&(moldyn->atom[0])) {
1445         printf("dVij, dVji (2bp) contrib:\n");
1446         printf("%f | %f\n",force.x,ai->f.x);
1447         printf("%f | %f\n",force.y,ai->f.y);
1448         printf("%f | %f\n",force.z,ai->f.z);
1449 }
1450 #endif
1451 #ifdef VDEBUG
1452 if(ai==&(moldyn->atom[0])) {
1453         printf("dVij, dVji (2bp) contrib:\n");
1454         printf("%f | %f\n",force.x*dist_ij.x,ai->virial.xx);
1455         printf("%f | %f\n",force.y*dist_ij.y,ai->virial.yy);
1456         printf("%f | %f\n",force.z*dist_ij.z,ai->virial.zz);
1457 }
1458 #endif
1459
1460         /* energy 2bp contribution (ij, ji) is 0.5 f_r f_c ... */
1461         moldyn->energy+=(0.5*f_r*f_c);
1462
1463         /* save for use in 3bp */
1464         exchange->f_c=f_c;
1465         exchange->df_c=df_c;
1466
1467         /* enable the run of 3bp function and 2bp post processing */
1468         exchange->run3bp=1;
1469         exchange->run2bp_post=1;
1470
1471         /* reset 3bp sums */
1472         exchange->zeta_ij=0.0;
1473         exchange->zeta_ji=0.0;
1474         v3_zero(&(exchange->dzeta_ij));
1475         v3_zero(&(exchange->dzeta_ji));
1476
1477         return 0;
1478 }
1479
1480 /* tersoff 2 body post part */
1481
1482 int tersoff_mult_post_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1483
1484         /*
1485          * here we have to allow for the 3bp sums
1486          *
1487          * that is:
1488          * - zeta_ij, dzeta_ij
1489          * - zeta_ji, dzeta_ji
1490          *
1491          * to compute the 3bp contribution to:
1492          * - Vij, dVij
1493          * - dVji
1494          *
1495          */
1496
1497         t_tersoff_mult_params *params;
1498         t_tersoff_exchange *exchange;
1499
1500         t_3dvec force,temp;
1501         t_3dvec *dist_ij;
1502         double b,db,tmp;
1503         double f_c,df_c,f_a,df_a;
1504         double chi,ni,betaini,nj,betajnj;
1505         double zeta;
1506
1507         params=moldyn->pot2b_params;
1508         exchange=&(params->exchange);
1509
1510         /* we do not run if f_c_ij was detected to be 0! */
1511         if(!(exchange->run2bp_post))
1512                 return 0;
1513
1514         f_c=exchange->f_c;
1515         df_c=exchange->df_c;
1516         f_a=exchange->f_a;
1517         df_a=exchange->df_a;
1518         betaini=exchange->betaini;
1519         betajnj=exchange->betajnj;
1520         ni=*(exchange->n_i);
1521         nj=*(exchange->n_j);
1522         chi=exchange->chi;
1523         dist_ij=&(exchange->dist_ij);
1524         
1525         /* Vij and dVij */
1526         zeta=exchange->zeta_ij;
1527         if(zeta==0.0) {
1528                 moldyn->debug++;                /* just for debugging ... */
1529                 db=0.0;
1530                 b=chi;
1531                 v3_scale(&force,dist_ij,df_a*b*f_c);
1532         }
1533         else {
1534                 tmp=betaini*pow(zeta,ni-1.0);           /* beta^n * zeta^n-1 */
1535                 b=(1+zeta*tmp);                         /* 1 + beta^n zeta^n */
1536                 db=chi*pow(b,-1.0/(2*ni)-1);            /* x(...)^(-1/2n - 1) */
1537                 b=db*b;                                 /* b_ij */
1538                 db*=-0.5*tmp;                           /* db_ij */
1539                 v3_scale(&force,&(exchange->dzeta_ij),f_a*db);
1540                 v3_scale(&temp,dist_ij,df_a*b);
1541                 v3_add(&force,&force,&temp);
1542                 v3_scale(&force,&force,f_c);
1543         }
1544         v3_scale(&temp,dist_ij,df_c*b*f_a);
1545         v3_add(&force,&force,&temp);
1546         v3_scale(&force,&force,-0.5);
1547
1548         /* add force */
1549         v3_add(&(ai->f),&(ai->f),&force);
1550
1551         /* virial */
1552         ai->virial.xx-=force.x*dist_ij->x;
1553         ai->virial.yy-=force.y*dist_ij->y;
1554         ai->virial.zz-=force.z*dist_ij->z;
1555         ai->virial.xy-=force.x*dist_ij->y;
1556         ai->virial.xz-=force.x*dist_ij->z;
1557         ai->virial.yz-=force.y*dist_ij->z;
1558
1559 #ifdef DEBUG
1560 if(ai==&(moldyn->atom[0])) {
1561         printf("dVij (3bp) contrib:\n");
1562         printf("%f | %f\n",force.x,ai->f.x);
1563         printf("%f | %f\n",force.y,ai->f.y);
1564         printf("%f | %f\n",force.z,ai->f.z);
1565 }
1566 #endif
1567 #ifdef VDEBUG
1568 if(ai==&(moldyn->atom[0])) {
1569         printf("dVij (3bp) contrib:\n");
1570         printf("%f | %f\n",force.x*dist_ij->x,ai->virial.xx);
1571         printf("%f | %f\n",force.y*dist_ij->y,ai->virial.yy);
1572         printf("%f | %f\n",force.z*dist_ij->z,ai->virial.zz);
1573 }
1574 #endif
1575
1576         /* add energy of 3bp sum */
1577         moldyn->energy+=(0.5*f_c*b*f_a);
1578
1579         /* dVji */
1580         zeta=exchange->zeta_ji;
1581         if(zeta==0.0) {
1582                 moldyn->debug++;
1583                 b=chi;
1584                 v3_scale(&force,dist_ij,df_a*b*f_c);
1585         }
1586         else {
1587                 tmp=betajnj*pow(zeta,nj-1.0);           /* beta^n * zeta^n-1 */
1588                 b=(1+zeta*tmp);                         /* 1 + beta^n zeta^n */
1589                 db=chi*pow(b,-1.0/(2*nj)-1);            /* x(...)^(-1/2n - 1) */
1590                 b=db*b;                                 /* b_ij */
1591                 db*=-0.5*tmp;                           /* db_ij */
1592                 v3_scale(&force,&(exchange->dzeta_ji),f_a*db);
1593                 v3_scale(&temp,dist_ij,df_a*b);
1594                 v3_add(&force,&force,&temp);
1595                 v3_scale(&force,&force,f_c);
1596         }
1597         v3_scale(&temp,dist_ij,df_c*b*f_a);
1598         v3_add(&force,&force,&temp);
1599         v3_scale(&force,&force,-0.5);
1600
1601         /* add force */
1602         v3_add(&(ai->f),&(ai->f),&force);
1603
1604         /* virial - plus sign, as dist_ij = - dist_ji - (really??) */
1605         ai->virial.xx+=force.x*dist_ij->x;
1606         ai->virial.yy+=force.y*dist_ij->y;
1607         ai->virial.zz+=force.z*dist_ij->z;
1608         ai->virial.xy+=force.x*dist_ij->y;
1609         ai->virial.xz+=force.x*dist_ij->z;
1610         ai->virial.yz+=force.y*dist_ij->z;
1611
1612 #ifdef DEBUG
1613 if(ai==&(moldyn->atom[0])) {
1614         printf("dVji (3bp) contrib:\n");
1615         printf("%f | %f\n",force.x,ai->f.x);
1616         printf("%f | %f\n",force.y,ai->f.y);
1617         printf("%f | %f\n",force.z,ai->f.z);
1618 }
1619 #endif
1620 #ifdef VDEBUG
1621 if(ai==&(moldyn->atom[0])) {
1622         printf("dVji (3bp) contrib:\n");
1623         printf("%f | %f\n",force.x*dist_ij->x,ai->virial.xx);
1624         printf("%f | %f\n",force.y*dist_ij->y,ai->virial.yy);
1625         printf("%f | %f\n",force.z*dist_ij->z,ai->virial.zz);
1626 }
1627 #endif
1628
1629         return 0;
1630 }
1631
1632 /* tersoff 3 body part */
1633
1634 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1635
1636         t_tersoff_mult_params *params;
1637         t_tersoff_exchange *exchange;
1638         t_3dvec dist_ij,dist_ik,dist_jk;
1639         t_3dvec temp1,temp2;
1640         t_3dvec *dzeta;
1641         double R,S,S2,s_r;
1642         double B,mu;
1643         double d_ij,d_ik,d_jk,d_ij2,d_ik2,d_jk2;
1644         double rr,dd;
1645         double f_c,df_c;
1646         double f_c_ik,df_c_ik,arg;
1647         double f_c_jk;
1648         double n,c,d,h;
1649         double c2,d2,c2d2;
1650         double cos_theta,d_costheta1,d_costheta2;
1651         double h_cos,d2_h_cos2;
1652         double frac,g,zeta,chi;
1653         double tmp;
1654         int brand;
1655
1656         params=moldyn->pot3b_params;
1657         exchange=&(params->exchange);
1658
1659         if(!(exchange->run3bp))
1660                 return 0;
1661
1662         /*
1663          * calc of 3bp contribution of V_ij and dV_ij/ji/jk &
1664          * 2bp contribution of dV_jk
1665          *
1666          * for Vij and dV_ij we still need:
1667          * - b_ij, db_ij (zeta_ij)
1668          *   - f_c_ik, df_c_ik, constants_i, cos_theta_ijk, d_costheta_ijk
1669          *
1670          * for dV_ji we still need:
1671          * - b_ji, db_ji (zeta_ji)
1672          *   - f_c_jk, d_c_jk, constants_j, cos_theta_jik, d_costheta_jik
1673          *
1674          * for dV_jk we need:
1675          * - f_c_jk
1676          * - f_a_jk
1677          * - db_jk (zeta_jk)
1678          *   - f_c_ji, df_c_ji, constants_j, cos_theta_jki, d_costheta_jki
1679          *
1680          */
1681
1682         /*
1683          * get exchange data 
1684          */
1685
1686         /* dist_ij, d_ij - this is < S_ij ! */
1687         dist_ij=exchange->dist_ij;
1688         d_ij=exchange->d_ij;
1689         d_ij2=exchange->d_ij2;
1690
1691         /* f_c_ij, df_c_ij (same for ji) */
1692         f_c=exchange->f_c;
1693         df_c=exchange->df_c;
1694
1695         /*
1696          * calculate unknown values now ...
1697          */
1698
1699         /* V_ij and dV_ij stuff (in b_ij there is f_c_ik) */
1700
1701         /* dist_ik, d_ik */
1702         v3_sub(&dist_ik,&(ak->r),&(ai->r));
1703         if(bc) check_per_bound(moldyn,&dist_ik);
1704         d_ik2=v3_absolute_square(&dist_ik);
1705
1706         /* ik constants */
1707         brand=ai->brand;
1708         if(brand==ak->brand) {
1709                 R=params->R[brand];
1710                 S=params->S[brand];
1711                 S2=params->S2[brand];
1712         }
1713         else {
1714                 R=params->Rmixed;
1715                 S=params->Smixed;
1716                 S2=params->S2mixed;
1717         }
1718
1719         /* zeta_ij/dzeta_ij contribution only for d_ik < S */
1720         if(d_ik2<S2) {
1721
1722                 /* now we need d_ik */
1723                 d_ik=sqrt(d_ik2);
1724
1725                 /* get constants_i from exchange data */
1726                 n=*(exchange->n_i);
1727                 c=*(exchange->c_i);
1728                 d=*(exchange->d_i);
1729                 h=*(exchange->h_i);
1730                 c2=exchange->ci2;
1731                 d2=exchange->di2;
1732                 c2d2=exchange->ci2di2;
1733
1734                 /* cosine of theta_ijk by scalaproduct */
1735                 rr=v3_scalar_product(&dist_ij,&dist_ik);
1736                 dd=d_ij*d_ik;
1737                 cos_theta=rr/dd;
1738
1739                 /* d_costheta */
1740                 tmp=1.0/dd;
1741                 d_costheta1=cos_theta/d_ij2-tmp;
1742                 d_costheta2=cos_theta/d_ik2-tmp;
1743
1744                 /* some usefull values */
1745                 h_cos=(h-cos_theta);
1746                 d2_h_cos2=d2+(h_cos*h_cos);
1747                 frac=c2/(d2_h_cos2);
1748
1749                 /* g(cos_theta) */
1750                 g=1.0+c2d2-frac;
1751
1752                 /* d_costheta_ij and dg(cos_theta) - needed in any case! */
1753                 v3_scale(&temp1,&dist_ij,d_costheta1);
1754                 v3_scale(&temp2,&dist_ik,d_costheta2);
1755                 v3_add(&temp1,&temp1,&temp2);
1756                 v3_scale(&temp1,&temp1,-2.0*frac*h_cos/d2_h_cos2); /* dg */
1757
1758                 /* f_c_ik & df_c_ik + {d,}zeta contribution */
1759                 dzeta=&(exchange->dzeta_ij);
1760                 if(d_ik<R) {
1761                         /* {d,}f_c_ik */
1762                         // => f_c_ik=1.0;
1763                         // => df_c_ik=0.0; of course we do not set this!
1764
1765                         /* zeta_ij */
1766                         exchange->zeta_ij+=g;
1767
1768                         /* dzeta_ij */
1769                         v3_add(dzeta,dzeta,&temp1);
1770                 }
1771                 else {
1772                         /* {d,}f_c_ik */
1773                         s_r=S-R;
1774                         arg=M_PI*(d_ik-R)/s_r;
1775                         f_c_ik=0.5+0.5*cos(arg);
1776                         df_c_ik=0.5*sin(arg)*(M_PI/(s_r*d_ik));
1777
1778                         /* zeta_ij */
1779                         exchange->zeta_ij+=f_c_ik*g;
1780
1781                         /* dzeta_ij */
1782                         v3_scale(&temp1,&temp1,f_c_ik);
1783                         v3_scale(&temp2,&dist_ik,g*df_c_ik);
1784                         v3_add(&temp1,&temp1,&temp2);
1785                         v3_add(dzeta,dzeta,&temp1);
1786                 }
1787         }
1788
1789         /* dV_ji stuff (in b_ji there is f_c_jk) + dV_jk stuff! */
1790
1791         /* dist_jk, d_jk */
1792         v3_sub(&dist_jk,&(ak->r),&(aj->r));
1793         if(bc) check_per_bound(moldyn,&dist_jk);
1794         d_jk2=v3_absolute_square(&dist_jk);
1795
1796         /* jk constants */
1797         brand=aj->brand;
1798         if(brand==ak->brand) {
1799                 R=params->R[brand];
1800                 S=params->S[brand];
1801                 S2=params->S2[brand];
1802                 B=params->B[brand];
1803                 mu=params->mu[brand];
1804                 chi=1.0;
1805         }
1806         else {
1807                 R=params->Rmixed;
1808                 S=params->Smixed;
1809                 S2=params->S2mixed;
1810                 B=params->Bmixed;
1811                 mu=params->mu_m;
1812                 chi=params->chi;
1813         }
1814
1815         /* zeta_ji/dzeta_ji contribution only for d_jk < S_jk */
1816         if(d_jk2<S2) {
1817
1818                 /* now we need d_ik */
1819                 d_jk=sqrt(d_jk2);
1820
1821                 /* constants_j from exchange data */
1822                 n=*(exchange->n_j);
1823                 c=*(exchange->c_j);
1824                 d=*(exchange->d_j);
1825                 h=*(exchange->h_j);
1826                 c2=exchange->cj2;
1827                 d2=exchange->dj2;
1828                 c2d2=exchange->cj2dj2;
1829
1830                 /* cosine of theta_jik by scalaproduct */
1831                 rr=-v3_scalar_product(&dist_ij,&dist_jk); /* -1, as ij -> ji */
1832                 dd=d_ij*d_jk;
1833                 cos_theta=rr/dd;
1834
1835                 /* d_costheta */
1836                 d_costheta1=1.0/dd;
1837                 d_costheta2=cos_theta/d_ij2;
1838
1839                 /* some usefull values */
1840                 h_cos=(h-cos_theta);
1841                 d2_h_cos2=d2+(h_cos*h_cos);
1842                 frac=c2/(d2_h_cos2);
1843
1844                 /* g(cos_theta) */
1845                 g=1.0+c2d2-frac;
1846
1847                 /* d_costheta_jik and dg(cos_theta) - needed in any case! */
1848                 v3_scale(&temp1,&dist_jk,d_costheta1);
1849                 v3_scale(&temp2,&dist_ij,-d_costheta2); /* ji -> ij => -1 */
1850                 //v3_add(&temp1,&temp1,&temp2);
1851                 v3_sub(&temp1,&temp1,&temp2); /* there is a minus! */
1852                 v3_scale(&temp1,&temp1,-2.0*frac*h_cos/d2_h_cos2); /* dg */
1853
1854                 /* store dg in temp2 and use it for dVjk later */
1855                 v3_copy(&temp2,&temp1);
1856
1857                 /* f_c_jk + {d,}zeta contribution (df_c_jk = 0) */
1858                 dzeta=&(exchange->dzeta_ji);
1859                 if(d_jk<R) {
1860                         /* f_c_jk */
1861                         f_c_jk=1.0;
1862
1863                         /* zeta_ji */
1864                         exchange->zeta_ji+=g;
1865
1866                         /* dzeta_ji */
1867                         v3_add(dzeta,dzeta,&temp1);
1868                 }
1869                 else {
1870                         /* f_c_jk */
1871                         s_r=S-R;
1872                         arg=M_PI*(d_jk-R)/s_r;
1873                         f_c_jk=0.5+0.5*cos(arg);
1874
1875                         /* zeta_ji */
1876                         exchange->zeta_ji+=f_c_jk*g;
1877
1878                         /* dzeta_ji */
1879                         v3_scale(&temp1,&temp1,f_c_jk);
1880                         v3_add(dzeta,dzeta,&temp1);
1881                 }
1882
1883                 /* dV_jk stuff | add force contribution on atom i immediately */
1884                 if(exchange->d_ij_between_rs) {
1885                         zeta=f_c*g;
1886                         v3_scale(&temp1,&temp2,f_c);
1887                         v3_scale(&temp2,&dist_ij,df_c*g);
1888                         v3_add(&temp2,&temp2,&temp1); /* -> dzeta_jk in temp2 */
1889                 }
1890                 else {
1891                         zeta=g;
1892                         // dzeta_jk is simply dg, which is stored in temp2
1893                 }
1894                 /* betajnj * zeta_jk ^ nj-1 */
1895                 tmp=exchange->betajnj*pow(zeta,(n-1.0));
1896                 tmp=-chi/2.0*pow((1+tmp*zeta),(-1.0/(2.0*n)-1))*tmp;
1897                 v3_scale(&temp2,&temp2,tmp*B*exp(-mu*d_jk)*f_c_jk*0.5);
1898                 v3_add(&(ai->f),&(ai->f),&temp2); /* -1 skipped in f_a calc ^ */
1899                                                   /* scaled with 0.5 ^ */
1900
1901                 /* virial */
1902                 ai->virial.xx-=temp2.x*dist_jk.x;
1903                 ai->virial.yy-=temp2.y*dist_jk.y;
1904                 ai->virial.zz-=temp2.z*dist_jk.z;
1905                 ai->virial.xy-=temp2.x*dist_jk.y;
1906                 ai->virial.xz-=temp2.x*dist_jk.z;
1907                 ai->virial.yz-=temp2.y*dist_jk.z;
1908
1909 #ifdef DEBUG
1910 if(ai==&(moldyn->atom[0])) {
1911         printf("dVjk (3bp) contrib:\n");
1912         printf("%f | %f\n",temp2.x,ai->f.x);
1913         printf("%f | %f\n",temp2.y,ai->f.y);
1914         printf("%f | %f\n",temp2.z,ai->f.z);
1915 }
1916 #endif
1917 #ifdef VDEBUG
1918 if(ai==&(moldyn->atom[0])) {
1919         printf("dVjk (3bp) contrib:\n");
1920         printf("%f | %f\n",temp2.x*dist_jk.x,ai->virial.xx);
1921         printf("%f | %f\n",temp2.y*dist_jk.y,ai->virial.yy);
1922         printf("%f | %f\n",temp2.z*dist_jk.z,ai->virial.zz);
1923 }
1924 #endif
1925
1926         }
1927
1928         return 0;
1929 }
1930
1931
1932 /*
1933  * debugging / critical check functions
1934  */
1935
1936 int moldyn_bc_check(t_moldyn *moldyn) {
1937
1938         t_atom *atom;
1939         t_3dvec *dim;
1940         int i;
1941         double x;
1942         u8 byte;
1943         int j,k;
1944
1945         atom=moldyn->atom;
1946         dim=&(moldyn->dim);
1947         x=dim->x/2;
1948
1949         for(i=0;i<moldyn->count;i++) {
1950                 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
1951                         printf("FATAL: atom %d: x: %.20f (%.20f)\n",
1952                                i,atom[i].r.x,dim->x/2);
1953                         printf("diagnostic:\n");
1954                         printf("-----------\natom.r.x:\n");
1955                         for(j=0;j<8;j++) {
1956                                 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
1957                                 for(k=0;k<8;k++)
1958                                         printf("%d%c",
1959                                         ((byte)&(1<<k))?1:0,
1960                                         (k==7)?'\n':'|');
1961                         }
1962                         printf("---------------\nx=dim.x/2:\n");
1963                         for(j=0;j<8;j++) {
1964                                 memcpy(&byte,(u8 *)(&x)+j,1);
1965                                 for(k=0;k<8;k++)
1966                                         printf("%d%c",
1967                                         ((byte)&(1<<k))?1:0,
1968                                         (k==7)?'\n':'|');
1969                         }
1970                         if(atom[i].r.x==x) printf("the same!\n");
1971                         else printf("different!\n");
1972                 }
1973                 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
1974                         printf("FATAL: atom %d: y: %.20f (%.20f)\n",
1975                                i,atom[i].r.y,dim->y/2);
1976                 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
1977                         printf("FATAL: atom %d: z: %.20f (%.20f)\n",
1978                                i,atom[i].r.z,dim->z/2);
1979         }
1980
1981         return 0;
1982 }