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