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