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