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