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