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