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