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