first sic tests
[physik/posic.git] / moldyn.c
1 /*
2  * moldyn.c - molecular dynamics library main file
3  *
4  * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
5  *
6  */
7
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <math.h>
17
18 #include "moldyn.h"
19 #include "report/report.h"
20
21 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
22
23         printf("[moldyn] init\n");
24
25         memset(moldyn,0,sizeof(t_moldyn));
26
27         rand_init(&(moldyn->random),NULL,1);
28         moldyn->random.status|=RAND_STAT_VERBOSE;
29
30         return 0;
31 }
32
33 int moldyn_shutdown(t_moldyn *moldyn) {
34
35         printf("[moldyn] shutdown\n");
36
37         moldyn_log_shutdown(moldyn);
38         link_cell_shutdown(moldyn);
39         rand_close(&(moldyn->random));
40         free(moldyn->atom);
41
42         return 0;
43 }
44
45 int set_int_alg(t_moldyn *moldyn,u8 algo) {
46
47         printf("[moldyn] integration algorithm: ");
48
49         switch(algo) {
50                 case MOLDYN_INTEGRATE_VERLET:
51                         moldyn->integrate=velocity_verlet;
52                         printf("velocity verlet\n");
53                         break;
54                 default:
55                         printf("unknown integration algorithm: %02x\n",algo);
56                         printf("unknown\n");
57                         return -1;
58         }
59
60         return 0;
61 }
62
63 int set_cutoff(t_moldyn *moldyn,double cutoff) {
64
65         moldyn->cutoff=cutoff;
66
67         printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
68
69         return 0;
70 }
71
72 int set_temperature(t_moldyn *moldyn,double t_ref) {
73
74         moldyn->t_ref=t_ref;
75
76         printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
77
78         return 0;
79 }
80
81 int set_pressure(t_moldyn *moldyn,double p_ref) {
82
83         moldyn->p_ref=p_ref;
84
85         printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
86
87         return 0;
88 }
89
90 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
91
92         moldyn->pt_scale=(ptype|ttype);
93         moldyn->t_tc=ttc;
94         moldyn->p_tc=ptc;
95
96         printf("[moldyn] p/t scaling:\n");
97
98         printf("  p: %s",ptype?"yes":"no ");
99         if(ptype)
100                 printf(" | type: %02x | factor: %f",ptype,ptc);
101         printf("\n");
102
103         printf("  t: %s",ttype?"yes":"no ");
104         if(ttype)
105                 printf(" | type: %02x | factor: %f",ttype,ttc);
106         printf("\n");
107
108         return 0;
109 }
110
111 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
112
113         moldyn->dim.x=x;
114         moldyn->dim.y=y;
115         moldyn->dim.z=z;
116
117         moldyn->volume=x*y*z;
118
119         if(visualize) {
120                 moldyn->vis.dim.x=x;
121                 moldyn->vis.dim.y=y;
122                 moldyn->vis.dim.z=z;
123         }
124
125         moldyn->dv=0.000001*moldyn->volume;
126
127         printf("[moldyn] dimensions in A and A^3 respectively:\n");
128         printf("  x: %f\n",moldyn->dim.x);
129         printf("  y: %f\n",moldyn->dim.y);
130         printf("  z: %f\n",moldyn->dim.z);
131         printf("  volume: %f\n",moldyn->volume);
132         printf("  visualize simulation box: %s\n",visualize?"yes":"no");
133         printf("  delta volume (pressure calc): %f\n",moldyn->dv);
134
135         return 0;
136 }
137
138 int set_nn_dist(t_moldyn *moldyn,double dist) {
139
140         moldyn->nnd=dist;
141
142         return 0;
143 }
144
145 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
146
147         printf("[moldyn] periodic boundary conditions:\n");
148
149         if(x)
150                 moldyn->status|=MOLDYN_STAT_PBX;
151
152         if(y)
153                 moldyn->status|=MOLDYN_STAT_PBY;
154
155         if(z)
156                 moldyn->status|=MOLDYN_STAT_PBZ;
157
158         printf("  x: %s\n",x?"yes":"no");
159         printf("  y: %s\n",y?"yes":"no");
160         printf("  z: %s\n",z?"yes":"no");
161
162         return 0;
163 }
164
165 int set_potential1b(t_moldyn *moldyn,pf_func1b func) {
166
167         moldyn->func1b=func;
168
169         return 0;
170 }
171
172 int set_potential2b(t_moldyn *moldyn,pf_func2b func) {
173
174         moldyn->func2b=func;
175
176         return 0;
177 }
178
179 int set_potential3b_j1(t_moldyn *moldyn,pf_func2b func) {
180
181         moldyn->func3b_j1=func;
182
183         return 0;
184 }
185
186 int set_potential3b_j2(t_moldyn *moldyn,pf_func2b func) {
187
188         moldyn->func3b_j2=func;
189
190         return 0;
191 }
192
193 int set_potential3b_j3(t_moldyn *moldyn,pf_func2b func) {
194
195         moldyn->func3b_j3=func;
196
197         return 0;
198 }
199
200 int set_potential3b_k1(t_moldyn *moldyn,pf_func3b func) {
201
202         moldyn->func3b_k1=func;
203
204         return 0;
205 }
206
207 int set_potential3b_k2(t_moldyn *moldyn,pf_func3b func) {
208
209         moldyn->func3b_k2=func;
210
211         return 0;
212 }
213
214 int set_potential_params(t_moldyn *moldyn,void *params) {
215
216         moldyn->pot_params=params;
217
218         return 0;
219 }
220
221 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
222
223         strncpy(moldyn->vlsdir,dir,127);
224
225         return 0;
226 }
227
228 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
229
230         strncpy(moldyn->rauthor,author,63);
231         strncpy(moldyn->rtitle,title,63);
232
233         return 0;
234 }
235         
236 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
237
238         char filename[128];
239         int ret;
240
241         printf("[moldyn] set log: ");
242
243         switch(type) {
244                 case LOG_TOTAL_ENERGY:
245                         moldyn->ewrite=timer;
246                         snprintf(filename,127,"%s/energy",moldyn->vlsdir);
247                         moldyn->efd=open(filename,
248                                          O_WRONLY|O_CREAT|O_EXCL,
249                                          S_IRUSR|S_IWUSR);
250                         if(moldyn->efd<0) {
251                                 perror("[moldyn] energy log fd open");
252                                 return moldyn->efd;
253                         }
254                         dprintf(moldyn->efd,"# total energy log file\n");
255                         printf("total energy (%d)\n",timer);
256                         break;
257                 case LOG_TOTAL_MOMENTUM:
258                         moldyn->mwrite=timer;
259                         snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
260                         moldyn->mfd=open(filename,
261                                          O_WRONLY|O_CREAT|O_EXCL,
262                                          S_IRUSR|S_IWUSR);
263                         if(moldyn->mfd<0) {
264                                 perror("[moldyn] momentum log fd open");
265                                 return moldyn->mfd;
266                         }
267                         dprintf(moldyn->efd,"# total momentum log file\n");
268                         printf("total momentum (%d)\n",timer);
269                         break;
270                 case LOG_PRESSURE:
271                         moldyn->pwrite=timer;
272                         snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
273                         moldyn->pfd=open(filename,
274                                          O_WRONLY|O_CREAT|O_EXCL,
275                                          S_IRUSR|S_IWUSR);
276                         if(moldyn->pfd<0) {
277                                 perror("[moldyn] pressure log file\n");
278                                 return moldyn->pfd;
279                         }
280                         dprintf(moldyn->pfd,"# pressure log file\n");
281                         printf("pressure (%d)\n",timer);
282                         break;
283                 case LOG_TEMPERATURE:
284                         moldyn->twrite=timer;
285                         snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
286                         moldyn->tfd=open(filename,
287                                          O_WRONLY|O_CREAT|O_EXCL,
288                                          S_IRUSR|S_IWUSR);
289                         if(moldyn->tfd<0) {
290                                 perror("[moldyn] temperature log file\n");
291                                 return moldyn->tfd;
292                         }
293                         dprintf(moldyn->tfd,"# temperature log file\n");
294                         printf("temperature (%d)\n",timer);
295                         break;
296                 case SAVE_STEP:
297                         moldyn->swrite=timer;
298                         printf("save file (%d)\n",timer);
299                         break;
300                 case VISUAL_STEP:
301                         moldyn->vwrite=timer;
302                         ret=visual_init(&(moldyn->vis),moldyn->vlsdir);
303                         if(ret<0) {
304                                 printf("[moldyn] visual init failure\n");
305                                 return ret;
306                         }
307                         printf("visual file (%d)\n",timer);
308                         break;
309                 case CREATE_REPORT:
310                         snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
311                         moldyn->rfd=open(filename,
312                                          O_WRONLY|O_CREAT|O_EXCL,
313                                          S_IRUSR|S_IWUSR);
314                         if(moldyn->rfd<0) {
315                                 perror("[moldyn] report fd open");      
316                                 return moldyn->rfd;
317                         }
318                         printf("report -> ");
319                         if(moldyn->efd) {
320                                 snprintf(filename,127,"%s/e_plot.scr",
321                                          moldyn->vlsdir);
322                                 moldyn->epfd=open(filename,
323                                                  O_WRONLY|O_CREAT|O_EXCL,
324                                                  S_IRUSR|S_IWUSR);
325                                 if(moldyn->epfd<0) {
326                                         perror("[moldyn] energy plot fd open");
327                                         return moldyn->epfd;
328                                 }
329                                 dprintf(moldyn->epfd,e_plot_script);
330                                 close(moldyn->epfd);
331                                 printf("energy ");
332                         }
333                         if(moldyn->pfd) {
334                                 snprintf(filename,127,"%s/pressure_plot.scr",
335                                          moldyn->vlsdir);
336                                 moldyn->ppfd=open(filename,
337                                                   O_WRONLY|O_CREAT|O_EXCL,
338                                                   S_IRUSR|S_IWUSR);
339                                 if(moldyn->ppfd<0) {
340                                         perror("[moldyn] p plot fd open");
341                                         return moldyn->ppfd;
342                                 }
343                                 dprintf(moldyn->ppfd,pressure_plot_script);
344                                 close(moldyn->ppfd);
345                                 printf("pressure ");
346                         }
347                         if(moldyn->tfd) {
348                                 snprintf(filename,127,"%s/temperature_plot.scr",
349                                          moldyn->vlsdir);
350                                 moldyn->tpfd=open(filename,
351                                                   O_WRONLY|O_CREAT|O_EXCL,
352                                                   S_IRUSR|S_IWUSR);
353                                 if(moldyn->tpfd<0) {
354                                         perror("[moldyn] t plot fd open");
355                                         return moldyn->tpfd;
356                                 }
357                                 dprintf(moldyn->tpfd,temperature_plot_script);
358                                 close(moldyn->tpfd);
359                                 printf("temperature ");
360                         }
361                         dprintf(moldyn->rfd,report_start,
362                                 moldyn->rauthor,moldyn->rtitle);
363                         printf("\n");
364                         break;
365                 default:
366                         printf("unknown log type: %02x\n",type);
367                         return -1;
368         }
369
370         return 0;
371 }
372
373 int moldyn_log_shutdown(t_moldyn *moldyn) {
374
375         char sc[256];
376
377         printf("[moldyn] log shutdown\n");
378         if(moldyn->efd) {
379                 close(moldyn->efd);
380                 if(moldyn->rfd) {
381                         dprintf(moldyn->rfd,report_energy);
382                         snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
383                                  moldyn->vlsdir);
384                         system(sc);
385                 }
386         }
387         if(moldyn->mfd) close(moldyn->mfd);
388         if(moldyn->pfd) {
389                 close(moldyn->pfd);
390                 if(moldyn->rfd)
391                         dprintf(moldyn->rfd,report_pressure);
392                         snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
393                                  moldyn->vlsdir);
394                         system(sc);
395         }
396         if(moldyn->tfd) {
397                 close(moldyn->tfd);
398                 if(moldyn->rfd)
399                         dprintf(moldyn->rfd,report_temperature);
400                         snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
401                                  moldyn->vlsdir);
402                         system(sc);
403         }
404         if(moldyn->rfd) {
405                 dprintf(moldyn->rfd,report_end);
406                 close(moldyn->rfd);
407                 snprintf(sc,255,"cd %s && pdflatex report",moldyn->vlsdir);
408                 system(sc);
409                 snprintf(sc,255,"cd %s && pdflatex report",moldyn->vlsdir);
410                 system(sc);
411                 snprintf(sc,255,"cd %s && dvipdf report",moldyn->vlsdir);
412                 system(sc);
413         }
414         if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
415
416         return 0;
417 }
418
419 /*
420  * creating lattice functions
421  */
422
423 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
424                    u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
425
426         int new,count;
427         int ret;
428         t_3dvec orig;
429         void *ptr;
430         t_atom *atom;
431
432         new=a*b*c;
433         count=moldyn->count;
434
435         /* how many atoms do we expect */
436         if(type==CUBIC) new*=1;
437         if(type==FCC) new*=4;
438         if(type==DIAMOND) new*=8;
439
440         /* allocate space for atoms */
441         ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
442         if(!ptr) {
443                 perror("[moldyn] realloc (create lattice)");
444                 return -1;
445         }
446         moldyn->atom=ptr;
447         atom=&(moldyn->atom[count]);
448
449         /* no atoms on the boundaries (only reason: it looks better!) */
450         if(!origin) {
451                 orig.x=0.5*lc;
452                 orig.y=0.5*lc;
453                 orig.z=0.5*lc;
454         }
455         else {
456                 orig.x=origin->x;
457                 orig.y=origin->y;
458                 orig.z=origin->z;
459         }
460
461         switch(type) {
462                 case CUBIC:
463                         set_nn_dist(moldyn,lc);
464                         ret=cubic_init(a,b,c,lc,atom,&orig);
465                         break;
466                 case FCC:
467                         if(!origin)
468                                 v3_scale(&orig,&orig,0.5);
469                         set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
470                         ret=fcc_init(a,b,c,lc,atom,&orig);
471                         break;
472                 case DIAMOND:
473                         if(!origin)
474                                 v3_scale(&orig,&orig,0.25);
475                         set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
476                         ret=diamond_init(a,b,c,lc,atom,&orig);
477                         break;
478                 default:
479                         printf("unknown lattice type (%02x)\n",type);
480                         return -1;
481         }
482
483         /* debug */
484         if(ret!=new) {
485                 printf("[moldyn] creating lattice failed\n");
486                 printf("  amount of atoms\n");
487                 printf("  - expected: %d\n",new);
488                 printf("  - created: %d\n",ret);
489                 return -1;
490         }
491
492         moldyn->count+=new;
493         printf("[moldyn] created lattice with %d atoms\n",new);
494
495         for(ret=0;ret<new;ret++) {
496                 atom[ret].element=element;
497                 atom[ret].mass=mass;
498                 atom[ret].attr=attr;
499                 atom[ret].brand=brand;
500                 atom[ret].tag=count+ret;
501                 check_per_bound(moldyn,&(atom[ret].r));
502         }
503
504         return ret;
505 }
506
507 /* cubic init */
508 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
509
510         int count;
511         t_3dvec r;
512         int i,j,k;
513         t_3dvec o;
514
515         count=0;
516         if(origin)
517                 v3_copy(&o,origin);
518         else
519                 v3_zero(&o);
520
521         r.x=o.x;
522         for(i=0;i<a;i++) {
523                 r.y=o.y;
524                 for(j=0;j<b;j++) {
525                         r.z=o.z;
526                         for(k=0;k<c;k++) {
527                                 v3_copy(&(atom[count].r),&r);
528                                 count+=1;
529                                 r.z+=lc;
530                         }
531                         r.y+=lc;
532                 }
533                 r.x+=lc;
534         }
535
536         for(i=0;i<count;i++) {
537                 atom[i].r.x-=(a*lc)/2.0;
538                 atom[i].r.y-=(b*lc)/2.0;
539                 atom[i].r.z-=(c*lc)/2.0;
540         }
541
542         return count;
543 }
544
545 /* fcc lattice init */
546 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
547
548         int count;
549         int i,j,k,l;
550         t_3dvec o,r,n;
551         t_3dvec basis[3];
552
553         count=0;
554         if(origin)
555                 v3_copy(&o,origin);
556         else
557                 v3_zero(&o);
558
559         /* construct the basis */
560         memset(basis,0,3*sizeof(t_3dvec));
561         basis[0].x=0.5*lc;
562         basis[0].y=0.5*lc;
563         basis[1].x=0.5*lc;
564         basis[1].z=0.5*lc;
565         basis[2].y=0.5*lc;
566         basis[2].z=0.5*lc;
567
568         /* fill up the room */
569         r.x=o.x;
570         for(i=0;i<a;i++) {
571                 r.y=o.y;
572                 for(j=0;j<b;j++) {
573                         r.z=o.z;
574                         for(k=0;k<c;k++) {
575                                 /* first atom */
576                                 v3_copy(&(atom[count].r),&r);
577                                 count+=1;
578                                 r.z+=lc;
579                                 /* the three face centered atoms */
580                                 for(l=0;l<3;l++) {
581                                         v3_add(&n,&r,&basis[l]);
582                                         v3_copy(&(atom[count].r),&n);
583                                         count+=1;
584                                 }
585                         }
586                         r.y+=lc;
587                 }
588                 r.x+=lc;
589         }
590                                 
591         /* coordinate transformation */
592         for(i=0;i<count;i++) {
593                 atom[i].r.x-=(a*lc)/2.0;
594                 atom[i].r.y-=(b*lc)/2.0;
595                 atom[i].r.z-=(c*lc)/2.0;
596         }
597
598         return count;
599 }
600
601 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
602
603         int count;
604         t_3dvec o;
605
606         count=fcc_init(a,b,c,lc,atom,origin);
607
608         o.x=0.25*lc;
609         o.y=0.25*lc;
610         o.z=0.25*lc;
611
612         if(origin) v3_add(&o,&o,origin);
613
614         count+=fcc_init(a,b,c,lc,&atom[count],&o);
615
616         return count;
617 }
618
619 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
620              t_3dvec *r,t_3dvec *v) {
621
622         t_atom *atom;
623         void *ptr;
624         int count;
625         
626         atom=moldyn->atom;
627         count=(moldyn->count)++;
628
629         ptr=realloc(atom,(count+1)*sizeof(t_atom));
630         if(!ptr) {
631                 perror("[moldyn] realloc (add atom)");
632                 return -1;
633         }
634         moldyn->atom=ptr;
635
636         atom=moldyn->atom;
637         atom[count].r=*r;
638         atom[count].v=*v;
639         atom[count].element=element;
640         atom[count].mass=mass;
641         atom[count].brand=brand;
642         atom[count].tag=count;
643         atom[count].attr=attr;
644
645         return 0;
646 }
647
648 int destroy_atoms(t_moldyn *moldyn) {
649
650         if(moldyn->atom) free(moldyn->atom);
651
652         return 0;
653 }
654
655 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
656
657         /*
658          * - gaussian distribution of velocities
659          * - zero total momentum
660          * - velocity scaling (E = 3/2 N k T), E: kinetic energy
661          */
662
663         int i;
664         double v,sigma;
665         t_3dvec p_total,delta;
666         t_atom *atom;
667         t_random *random;
668
669         atom=moldyn->atom;
670         random=&(moldyn->random);
671
672         printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
673
674         /* gaussian distribution of velocities */
675         v3_zero(&p_total);
676         for(i=0;i<moldyn->count;i++) {
677                 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
678                 /* x direction */
679                 v=sigma*rand_get_gauss(random);
680                 atom[i].v.x=v;
681                 p_total.x+=atom[i].mass*v;
682                 /* y direction */
683                 v=sigma*rand_get_gauss(random);
684                 atom[i].v.y=v;
685                 p_total.y+=atom[i].mass*v;
686                 /* z direction */
687                 v=sigma*rand_get_gauss(random);
688                 atom[i].v.z=v;
689                 p_total.z+=atom[i].mass*v;
690         }
691
692         /* zero total momentum */
693         v3_scale(&p_total,&p_total,1.0/moldyn->count);
694         for(i=0;i<moldyn->count;i++) {
695                 v3_scale(&delta,&p_total,1.0/atom[i].mass);
696                 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
697         }
698
699         /* velocity scaling */
700         scale_velocity(moldyn,equi_init);
701
702         return 0;
703 }
704
705 double temperature_calc(t_moldyn *moldyn) {
706
707         /* assume up to date kinetic energy, which is 3/2 N k_B T */
708
709         moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
710         moldyn->t_sum+=moldyn->t;
711         moldyn->mean_t=moldyn->t_sum/moldyn->total_steps;
712
713         return moldyn->t;
714 }
715
716 double get_temperature(t_moldyn *moldyn) {
717
718         return moldyn->t;
719 }
720
721 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
722
723         int i;
724         double e,scale;
725         t_atom *atom;
726         int count;
727
728         atom=moldyn->atom;
729
730         /*
731          * - velocity scaling (E = 3/2 N k T), E: kinetic energy
732          */
733
734         /* get kinetic energy / temperature & count involved atoms */
735         e=0.0;
736         count=0;
737         for(i=0;i<moldyn->count;i++) {
738                 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
739                         e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
740                         count+=1;
741                 }
742         }
743         e*=0.5;
744         if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
745         else return 0;  /* no atoms involved in scaling! */
746         
747         /* (temporary) hack for e,t = 0 */
748         if(e==0.0) {
749         moldyn->t=0.0;
750                 if(moldyn->t_ref!=0.0) {
751                         thermal_init(moldyn,equi_init);
752                         return 0;
753                 }
754                 else
755                         return 0; /* no scaling needed */
756         }
757
758
759         /* get scaling factor */
760         scale=moldyn->t_ref/moldyn->t;
761         if(equi_init&TRUE)
762                 scale*=2.0;
763         else
764                 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
765                         scale=1.0+(scale-1.0)/moldyn->t_tc;
766         scale=sqrt(scale);
767
768         /* velocity scaling */
769         for(i=0;i<moldyn->count;i++) {
770                 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
771                         v3_scale(&(atom[i].v),&(atom[i].v),scale);
772         }
773
774         return 0;
775 }
776
777 double ideal_gas_law_pressure(t_moldyn *moldyn) {
778
779         double p;
780
781         p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
782
783         return p;
784 }
785
786 double pressure_calc(t_moldyn *moldyn) {
787
788         int i;
789         double v;
790         t_virial *virial;
791
792         /*
793          * PV = NkT + <W>
794          * W = 1/3 sum_i f_i r_i
795          * virial = sum_i f_i r_i
796          * 
797          * => P = (2 Ekin + virial) / (3V)
798          */
799
800         v=0.0;
801         for(i=0;i<moldyn->count;i++) {
802                 virial=&(moldyn->atom[i].virial);
803                 v+=(virial->xx+virial->yy+virial->zz);
804         }
805
806         /* assume up to date kinetic energy */
807         moldyn->p=2.0*moldyn->ekin+v;
808         moldyn->p/=(3.0*moldyn->volume);
809         moldyn->p_sum+=moldyn->p;
810         moldyn->mean_p=moldyn->p_sum/moldyn->total_steps;
811
812         /* pressure from 'absolute coordinates' virial */
813         virial=&(moldyn->virial);
814         v=virial->xx+virial->yy+virial->zz;
815         moldyn->gp=2.0*moldyn->ekin+v;
816         moldyn->gp/=(3.0*moldyn->volume);
817         moldyn->gp_sum+=moldyn->gp;
818         moldyn->mean_gp=moldyn->gp_sum/moldyn->total_steps;
819
820         return moldyn->p;
821 }       
822
823 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
824
825         t_3dvec dim,*tp;
826         double u,p;
827         double scale,dv;
828         t_atom *store;
829
830         /*
831          * dU = - p dV
832          *
833          * => p = - dU/dV
834          *
835          * dV: dx,y,z = 0.001 x,y,z
836          */
837
838         scale=1.00000000000001;
839 printf("\n\nP-DEBUG:\n");
840
841         tp=&(moldyn->tp);
842         store=malloc(moldyn->count*sizeof(t_atom));
843         if(store==NULL) {
844                 printf("[moldyn] allocating store mem failed\n");
845                 return -1;
846         }
847
848         /* save unscaled potential energy + atom/dim configuration */
849         u=moldyn->energy;
850         memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
851         dim=moldyn->dim;
852
853         /* derivative with respect to x direction */
854         scale_dim(moldyn,scale,TRUE,0,0);
855         scale_atoms(moldyn,scale,TRUE,0,0);
856         dv=0.00000000000001*moldyn->dim.x*moldyn->dim.y*moldyn->dim.z;
857         link_cell_shutdown(moldyn);
858         link_cell_init(moldyn,QUIET);
859         potential_force_calc(moldyn);
860         tp->x=(moldyn->energy-u)/dv;
861         p=tp->x*tp->x;
862
863         /* restore atomic configuration + dim */
864         memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
865         moldyn->dim=dim;
866
867         /* derivative with respect to y direction */
868         scale_dim(moldyn,scale,0,TRUE,0);
869         scale_atoms(moldyn,scale,0,TRUE,0);
870         dv=0.00000000000001*moldyn->dim.y*moldyn->dim.x*moldyn->dim.z;
871         link_cell_shutdown(moldyn);
872         link_cell_init(moldyn,QUIET);
873         potential_force_calc(moldyn);
874         tp->y=(moldyn->energy-u)/dv;
875         p+=tp->y*tp->y;
876
877         /* restore atomic configuration + dim */
878         memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
879         moldyn->dim=dim;
880
881         /* derivative with respect to z direction */
882         scale_dim(moldyn,scale,0,0,TRUE);
883         scale_atoms(moldyn,scale,0,0,TRUE);
884         dv=0.00000000000001*moldyn->dim.z*moldyn->dim.x*moldyn->dim.y;
885         link_cell_shutdown(moldyn);
886         link_cell_init(moldyn,QUIET);
887         potential_force_calc(moldyn);
888         tp->z=(moldyn->energy-u)/dv;
889         p+=tp->z*tp->z;
890
891         /* restore atomic configuration + dim */
892         memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
893         moldyn->dim=dim;
894
895         /* restore energy */
896         moldyn->energy=u;
897
898         link_cell_shutdown(moldyn);
899         link_cell_init(moldyn,QUIET);
900
901         return sqrt(p);
902 }
903
904 double get_pressure(t_moldyn *moldyn) {
905
906         return moldyn->p;
907
908 }
909
910 int scale_dim(t_moldyn *moldyn,double scale,u8 x,u8 y,u8 z) {
911
912         t_3dvec *dim;
913
914         dim=&(moldyn->dim);
915
916         if(x) dim->x*=scale;
917         if(y) dim->y*=scale;
918         if(z) dim->z*=scale;
919
920         return 0;
921 }
922
923 int scale_atoms(t_moldyn *moldyn,double scale,u8 x,u8 y,u8 z) {
924
925         int i;
926         t_3dvec *r;
927
928         for(i=0;i<moldyn->count;i++) {
929                 r=&(moldyn->atom[i].r);
930                 if(x) r->x*=scale;
931                 if(y) r->y*=scale;
932                 if(z) r->z*=scale;
933         }
934
935         return 0;
936 }
937
938 int scale_volume(t_moldyn *moldyn) {
939
940         t_3dvec *dim,*vdim;
941         double scale;
942         t_linkcell *lc;
943
944         vdim=&(moldyn->vis.dim);
945         dim=&(moldyn->dim);
946         lc=&(moldyn->lc);
947
948         /* scaling factor */
949         if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
950                 scale=1.0-(moldyn->p_ref-moldyn->p)/moldyn->p_tc;
951                 scale=pow(scale,ONE_THIRD);
952         }
953         else {
954                 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
955         }
956 moldyn->debug=scale;
957
958         /* scale the atoms and dimensions */
959         scale_atoms(moldyn,scale,TRUE,TRUE,TRUE);
960         scale_dim(moldyn,scale,TRUE,TRUE,TRUE);
961
962         /* visualize dimensions */
963         if(vdim->x!=0) {
964                 vdim->x=dim->x;
965                 vdim->y=dim->y;
966                 vdim->z=dim->z;
967         }
968
969         /* recalculate scaled volume */
970         moldyn->volume=dim->x*dim->y*dim->z;
971
972         /* adjust/reinit linkcell */
973         if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
974            ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
975            ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
976                 link_cell_shutdown(moldyn);
977                 link_cell_init(moldyn,QUIET);
978         } else {
979                 lc->x*=scale;
980                 lc->y*=scale;
981                 lc->z*=scale;
982         }
983
984         return 0;
985
986 }
987
988 double e_kin_calc(t_moldyn *moldyn) {
989
990         int i;
991         t_atom *atom;
992
993         atom=moldyn->atom;
994         moldyn->ekin=0.0;
995
996         for(i=0;i<moldyn->count;i++)
997                 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
998
999         return moldyn->ekin;
1000 }
1001
1002 double get_total_energy(t_moldyn *moldyn) {
1003
1004         return(moldyn->ekin+moldyn->energy);
1005 }
1006
1007 t_3dvec get_total_p(t_moldyn *moldyn) {
1008
1009         t_3dvec p,p_total;
1010         int i;
1011         t_atom *atom;
1012
1013         atom=moldyn->atom;
1014
1015         v3_zero(&p_total);
1016         for(i=0;i<moldyn->count;i++) {
1017                 v3_scale(&p,&(atom[i].v),atom[i].mass);
1018                 v3_add(&p_total,&p_total,&p);
1019         }
1020
1021         return p_total;
1022 }
1023
1024 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1025
1026         double tau;
1027
1028         /* nn_dist is the nearest neighbour distance */
1029
1030         tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1031
1032         return tau;     
1033 }
1034
1035 /*
1036  * numerical tricks
1037  */
1038
1039 /* linked list / cell method */
1040
1041 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1042
1043         t_linkcell *lc;
1044         int i;
1045
1046         lc=&(moldyn->lc);
1047
1048         /* partitioning the md cell */
1049         lc->nx=moldyn->dim.x/moldyn->cutoff;
1050         lc->x=moldyn->dim.x/lc->nx;
1051         lc->ny=moldyn->dim.y/moldyn->cutoff;
1052         lc->y=moldyn->dim.y/lc->ny;
1053         lc->nz=moldyn->dim.z/moldyn->cutoff;
1054         lc->z=moldyn->dim.z/lc->nz;
1055
1056         lc->cells=lc->nx*lc->ny*lc->nz;
1057         lc->subcell=malloc(lc->cells*sizeof(t_list));
1058
1059         if(lc->cells<27)
1060                 printf("[moldyn] FATAL: less then 27 subcells!\n");
1061
1062         if(vol) {
1063                 printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
1064                 printf("  x: %d x %f A\n",lc->nx,lc->x);
1065                 printf("  y: %d x %f A\n",lc->ny,lc->y);
1066                 printf("  z: %d x %f A\n",lc->nz,lc->z);
1067         }
1068
1069         for(i=0;i<lc->cells;i++)
1070                 list_init_f(&(lc->subcell[i]));
1071
1072         link_cell_update(moldyn);
1073         
1074         return 0;
1075 }
1076
1077 int link_cell_update(t_moldyn *moldyn) {
1078
1079         int count,i,j,k;
1080         int nx,ny;
1081         t_atom *atom;
1082         t_linkcell *lc;
1083         double x,y,z;
1084
1085         atom=moldyn->atom;
1086         lc=&(moldyn->lc);
1087
1088         nx=lc->nx;
1089         ny=lc->ny;
1090
1091         x=moldyn->dim.x/2;
1092         y=moldyn->dim.y/2;
1093         z=moldyn->dim.z/2;
1094
1095         for(i=0;i<lc->cells;i++)
1096                 list_destroy_f(&(lc->subcell[i]));
1097         
1098         for(count=0;count<moldyn->count;count++) {
1099                 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1100                 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1101                 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1102                 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1103                                      &(atom[count]));
1104         }
1105
1106         return 0;
1107 }
1108
1109 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
1110
1111         t_linkcell *lc;
1112         int a;
1113         int count1,count2;
1114         int ci,cj,ck;
1115         int nx,ny,nz;
1116         int x,y,z;
1117         u8 bx,by,bz;
1118
1119         lc=&(moldyn->lc);
1120         nx=lc->nx;
1121         ny=lc->ny;
1122         nz=lc->nz;
1123         count1=1;
1124         count2=27;
1125         a=nx*ny;
1126
1127         cell[0]=lc->subcell[i+j*nx+k*a];
1128         for(ci=-1;ci<=1;ci++) {
1129                 bx=0;
1130                 x=i+ci;
1131                 if((x<0)||(x>=nx)) {
1132                         x=(x+nx)%nx;
1133                         bx=1;
1134                 }
1135                 for(cj=-1;cj<=1;cj++) {
1136                         by=0;
1137                         y=j+cj;
1138                         if((y<0)||(y>=ny)) {
1139                                 y=(y+ny)%ny;
1140                                 by=1;
1141                         }
1142                         for(ck=-1;ck<=1;ck++) {
1143                                 bz=0;
1144                                 z=k+ck;
1145                                 if((z<0)||(z>=nz)) {
1146                                         z=(z+nz)%nz;
1147                                         bz=1;
1148                                 }
1149                                 if(!(ci|cj|ck)) continue;
1150                                 if(bx|by|bz) {
1151                                         cell[--count2]=lc->subcell[x+y*nx+z*a];
1152                                 }
1153                                 else {
1154                                         cell[count1++]=lc->subcell[x+y*nx+z*a];
1155                                 }
1156                         }
1157                 }
1158         }
1159
1160         lc->dnlc=count1;
1161
1162         return count1;
1163 }
1164
1165 int link_cell_shutdown(t_moldyn *moldyn) {
1166
1167         int i;
1168         t_linkcell *lc;
1169
1170         lc=&(moldyn->lc);
1171
1172         for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
1173                 list_destroy_f(&(moldyn->lc.subcell[i]));
1174
1175         free(lc->subcell);
1176
1177         return 0;
1178 }
1179
1180 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1181
1182         int count;
1183         void *ptr;
1184         t_moldyn_schedule *schedule;
1185
1186         schedule=&(moldyn->schedule);
1187         count=++(schedule->total_sched);
1188
1189         ptr=realloc(schedule->runs,count*sizeof(int));
1190         if(!ptr) {
1191                 perror("[moldyn] realloc (runs)");
1192                 return -1;
1193         }
1194         schedule->runs=ptr;
1195         schedule->runs[count-1]=runs;
1196
1197         ptr=realloc(schedule->tau,count*sizeof(double));
1198         if(!ptr) {
1199                 perror("[moldyn] realloc (tau)");
1200                 return -1;
1201         }
1202         schedule->tau=ptr;
1203         schedule->tau[count-1]=tau;
1204
1205         printf("[moldyn] schedule added:\n");
1206         printf("  number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1207                                        
1208
1209         return 0;
1210 }
1211
1212 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1213
1214         moldyn->schedule.hook=hook;
1215         moldyn->schedule.hook_params=hook_params;
1216         
1217         return 0;
1218 }
1219
1220 /*
1221  *
1222  * 'integration of newtons equation' - algorithms
1223  *
1224  */
1225
1226 /* start the integration */
1227
1228 int moldyn_integrate(t_moldyn *moldyn) {
1229
1230         int i;
1231         unsigned int e,m,s,v,p,t;
1232         t_3dvec momentum;
1233         t_moldyn_schedule *sched;
1234         t_atom *atom;
1235         int fd;
1236         char dir[128];
1237         double ds;
1238         double energy_scale;
1239         //double tp;
1240
1241         sched=&(moldyn->schedule);
1242         atom=moldyn->atom;
1243
1244         /* initialize linked cell method */
1245         link_cell_init(moldyn,VERBOSE);
1246
1247         /* logging & visualization */
1248         e=moldyn->ewrite;
1249         m=moldyn->mwrite;
1250         s=moldyn->swrite;
1251         v=moldyn->vwrite;
1252         p=moldyn->pwrite;
1253         t=moldyn->twrite;
1254
1255         /* sqaure of some variables */
1256         moldyn->tau_square=moldyn->tau*moldyn->tau;
1257         moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
1258
1259         /* energy scaling factor */
1260         energy_scale=moldyn->count*EV;
1261
1262         /* calculate initial forces */
1263         potential_force_calc(moldyn);
1264 #ifdef DEBUG
1265 return 0;
1266 #endif
1267
1268         /* some stupid checks before we actually start calculating bullshit */
1269         if(moldyn->cutoff>0.5*moldyn->dim.x)
1270                 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
1271         if(moldyn->cutoff>0.5*moldyn->dim.y)
1272                 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
1273         if(moldyn->cutoff>0.5*moldyn->dim.z)
1274                 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
1275         ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1276         if(ds>0.05*moldyn->nnd)
1277                 printf("[moldyn] warning: forces too high / tau too small!\n");
1278
1279         /* zero absolute time */
1280         moldyn->time=0.0;
1281         moldyn->total_steps=0;
1282
1283         /* debugging, ignore */
1284         moldyn->debug=0;
1285
1286         /* tell the world */
1287         printf("[moldyn] integration start, go get a coffee ...\n");
1288
1289         /* executing the schedule */
1290         for(sched->count=0;sched->count<sched->total_sched;sched->count++) {
1291
1292                 /* setting amount of runs and finite time step size */
1293                 moldyn->tau=sched->tau[sched->count];
1294                 moldyn->tau_square=moldyn->tau*moldyn->tau;
1295                 moldyn->time_steps=sched->runs[sched->count];
1296
1297         /* integration according to schedule */
1298
1299         for(i=0;i<moldyn->time_steps;i++) {
1300
1301                 /* integration step */
1302                 moldyn->integrate(moldyn);
1303
1304                 /* calculate kinetic energy, temperature and pressure */
1305                 e_kin_calc(moldyn);
1306                 temperature_calc(moldyn);
1307                 pressure_calc(moldyn);
1308                 //tp=thermodynamic_pressure_calc(moldyn);
1309 //printf("thermodynamic p: %f %f %f - %f\n",moldyn->tp.x/BAR,moldyn->tp.y/BAR,moldyn->tp.z/BAR,tp/BAR);
1310
1311                 /* p/t scaling */
1312                 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1313                         scale_velocity(moldyn,FALSE);
1314                 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1315                         scale_volume(moldyn);
1316
1317                 /* check for log & visualization */
1318                 if(e) {
1319                         if(!(i%e))
1320                                 dprintf(moldyn->efd,
1321                                         "%f %f %f %f\n",
1322                                         moldyn->time,moldyn->ekin/energy_scale,
1323                                         moldyn->energy/energy_scale,
1324                                         get_total_energy(moldyn)/energy_scale);
1325                 }
1326                 if(m) {
1327                         if(!(i%m)) {
1328                                 momentum=get_total_p(moldyn);
1329                                 dprintf(moldyn->mfd,
1330                                         "%f %f %f %f %f\n",moldyn->time,
1331                                         momentum.x,momentum.y,momentum.z,
1332                                         v3_norm(&momentum));
1333                         }
1334                 }
1335                 if(p) {
1336                         if(!(i%p)) {
1337                                 dprintf(moldyn->pfd,
1338                                         "%f %f %f %f %f\n",moldyn->time,
1339                                          moldyn->p/BAR,moldyn->mean_p/BAR,
1340                                          moldyn->gp/BAR,moldyn->mean_gp/BAR);
1341                         }
1342                 }
1343                 if(t) {
1344                         if(!(i%t)) {
1345                                 dprintf(moldyn->tfd,
1346                                         "%f %f %f\n",
1347                                         moldyn->time,moldyn->t,moldyn->mean_t);
1348                         }
1349                 }
1350                 if(s) {
1351                         if(!(i%s)) {
1352                                 snprintf(dir,128,"%s/s-%07.f.save",
1353                                          moldyn->vlsdir,moldyn->time);
1354                                 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT);
1355                                 if(fd<0) perror("[moldyn] save fd open");
1356                                 else {
1357                                         write(fd,moldyn,sizeof(t_moldyn));
1358                                         write(fd,moldyn->atom,
1359                                               moldyn->count*sizeof(t_atom));
1360                                 }
1361                                 close(fd);
1362                         }       
1363                 }
1364                 if(v) {
1365                         if(!(i%v)) {
1366                                 visual_atoms(&(moldyn->vis),moldyn->time,
1367                                              moldyn->atom,moldyn->count);
1368                         }
1369                 }
1370
1371                 /* display progress */
1372                 if(!(i%10)) {
1373                         printf("\rsched: %d, steps: %d, T: %f, P: %f %f V: %f",
1374                                sched->count,i,
1375                                moldyn->mean_t,
1376                                moldyn->mean_p/BAR,
1377                                moldyn->mean_gp/BAR,
1378                                moldyn->volume);
1379                         fflush(stdout);
1380                 }
1381
1382                 /* increase absolute time */
1383                 moldyn->time+=moldyn->tau;
1384                 moldyn->total_steps+=1;
1385
1386         }
1387
1388                 /* check for hooks */
1389                 if(sched->hook)
1390                         sched->hook(moldyn,sched->hook_params);
1391
1392                 /* get a new info line */
1393                 printf("\n");
1394
1395         }
1396
1397         return 0;
1398 }
1399
1400 /* velocity verlet */
1401
1402 int velocity_verlet(t_moldyn *moldyn) {
1403
1404         int i,count;
1405         double tau,tau_square,h;
1406         t_3dvec delta;
1407         t_atom *atom;
1408
1409         atom=moldyn->atom;
1410         count=moldyn->count;
1411         tau=moldyn->tau;
1412         tau_square=moldyn->tau_square;
1413
1414         for(i=0;i<count;i++) {
1415                 /* new positions */
1416                 h=0.5/atom[i].mass;
1417                 v3_scale(&delta,&(atom[i].v),tau);
1418                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1419                 v3_scale(&delta,&(atom[i].f),h*tau_square);
1420                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1421                 check_per_bound(moldyn,&(atom[i].r));
1422
1423                 /* velocities [actually v(t+tau/2)] */
1424                 v3_scale(&delta,&(atom[i].f),h*tau);
1425                 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1426         }
1427
1428         /* neighbour list update */
1429         link_cell_update(moldyn);
1430
1431         /* forces depending on chosen potential */
1432         potential_force_calc(moldyn);
1433
1434         for(i=0;i<count;i++) {
1435                 /* again velocities [actually v(t+tau)] */
1436                 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1437                 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1438         }
1439
1440         return 0;
1441 }
1442
1443
1444 /*
1445  *
1446  * potentials & corresponding forces & virial routine
1447  * 
1448  */
1449
1450 /* generic potential and force calculation */
1451
1452 int potential_force_calc(t_moldyn *moldyn) {
1453
1454         int i,j,k,count;
1455         t_atom *itom,*jtom,*ktom;
1456         t_virial *virial;
1457         t_linkcell *lc;
1458         t_list neighbour_i[27];
1459         t_list neighbour_i2[27];
1460         t_list *this,*that;
1461         u8 bc_ij,bc_ik;
1462         int dnlc;
1463
1464         count=moldyn->count;
1465         itom=moldyn->atom;
1466         lc=&(moldyn->lc);
1467
1468         /* reset energy */
1469         moldyn->energy=0.0;
1470
1471         /* reset global virial */
1472         memset(&(moldyn->virial),0,sizeof(t_virial));
1473
1474         /* reset force, site energy and virial of every atom */
1475         for(i=0;i<count;i++) {
1476
1477                 /* reset force */
1478                 v3_zero(&(itom[i].f));
1479
1480                 /* reset virial */
1481                 virial=(&(itom[i].virial));
1482                 virial->xx=0.0;
1483                 virial->yy=0.0;
1484                 virial->zz=0.0;
1485                 virial->xy=0.0;
1486                 virial->xz=0.0;
1487                 virial->yz=0.0;
1488         
1489                 /* reset site energy */
1490                 itom[i].e=0.0;
1491
1492         }
1493
1494         /* get energy, force and virial of every atom */
1495
1496         /* first (and only) loop over atoms i */
1497         for(i=0;i<count;i++) {
1498
1499                 /* single particle potential/force */
1500                 if(itom[i].attr&ATOM_ATTR_1BP)
1501                         moldyn->func1b(moldyn,&(itom[i]));
1502
1503                 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1504                         continue;
1505
1506                 /* 2 body pair potential/force */
1507         
1508                 link_cell_neighbour_index(moldyn,
1509                                           (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1510                                           (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1511                                           (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1512                                           neighbour_i);
1513
1514                 dnlc=lc->dnlc;
1515
1516                 /* first loop over atoms j */
1517                 if(moldyn->func2b) {
1518                         for(j=0;j<27;j++) {
1519
1520                                 this=&(neighbour_i[j]);
1521                                 list_reset_f(this);
1522
1523                                 if(this->start==NULL)
1524                                         continue;
1525
1526                                 bc_ij=(j<dnlc)?0:1;
1527
1528                                 do {
1529                                         jtom=this->current->data;
1530
1531                                         if(jtom==&(itom[i]))
1532                                                 continue;
1533
1534                                         if((jtom->attr&ATOM_ATTR_2BP)&
1535                                            (itom[i].attr&ATOM_ATTR_2BP)) {
1536                                                 moldyn->func2b(moldyn,
1537                                                                &(itom[i]),
1538                                                                jtom,
1539                                                                bc_ij);
1540                                         }
1541                                 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1542
1543                         }
1544                 }
1545
1546                 /* 3 body potential/force */
1547
1548                 if(!(itom[i].attr&ATOM_ATTR_3BP))
1549                         continue;
1550
1551                 /* copy the neighbour lists */
1552                 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
1553
1554                 /* second loop over atoms j */
1555                 for(j=0;j<27;j++) {
1556
1557                         this=&(neighbour_i[j]);
1558                         list_reset_f(this);
1559
1560                         if(this->start==NULL)
1561                                 continue;
1562
1563                         bc_ij=(j<dnlc)?0:1;
1564
1565                         do {
1566                                 jtom=this->current->data;
1567
1568                                 if(jtom==&(itom[i]))
1569                                         continue;
1570
1571                                 if(!(jtom->attr&ATOM_ATTR_3BP))
1572                                         continue;
1573
1574                                 /* reset 3bp run */
1575                                 moldyn->run3bp=1;
1576
1577                                 if(moldyn->func3b_j1)
1578                                         moldyn->func3b_j1(moldyn,
1579                                                           &(itom[i]),
1580                                                           jtom,
1581                                                           bc_ij);
1582
1583                                 /* in first j loop, 3bp run can be skipped */
1584                                 if(!(moldyn->run3bp))
1585                                         continue;
1586                         
1587                                 /* first loop over atoms k */
1588                                 if(moldyn->func3b_k1) {
1589
1590                                 for(k=0;k<27;k++) {
1591
1592                                         that=&(neighbour_i2[k]);
1593                                         list_reset_f(that);
1594                                         
1595                                         if(that->start==NULL)
1596                                                 continue;
1597
1598                                         bc_ik=(k<dnlc)?0:1;
1599
1600                                         do {
1601
1602                                                 ktom=that->current->data;
1603
1604                                                 if(!(ktom->attr&ATOM_ATTR_3BP))
1605                                                         continue;
1606
1607                                                 if(ktom==jtom)
1608                                                         continue;
1609
1610                                                 if(ktom==&(itom[i]))
1611                                                         continue;
1612
1613                                                 moldyn->func3b_k1(moldyn,
1614                                                                   &(itom[i]),
1615                                                                   jtom,
1616                                                                   ktom,
1617                                                                   bc_ik|bc_ij);
1618
1619                                         } while(list_next_f(that)!=\
1620                                                 L_NO_NEXT_ELEMENT);
1621
1622                                 }
1623
1624                                 }
1625
1626                                 if(moldyn->func3b_j2)
1627                                         moldyn->func3b_j2(moldyn,
1628                                                           &(itom[i]),
1629                                                           jtom,
1630                                                           bc_ij);
1631
1632                                 /* second loop over atoms k */
1633                                 if(moldyn->func3b_k2) {
1634
1635                                 for(k=0;k<27;k++) {
1636
1637                                         that=&(neighbour_i2[k]);
1638                                         list_reset_f(that);
1639                                         
1640                                         if(that->start==NULL)
1641                                                 continue;
1642
1643                                         bc_ik=(k<dnlc)?0:1;
1644
1645                                         do {
1646
1647                                                 ktom=that->current->data;
1648
1649                                                 if(!(ktom->attr&ATOM_ATTR_3BP))
1650                                                         continue;
1651
1652                                                 if(ktom==jtom)
1653                                                         continue;
1654
1655                                                 if(ktom==&(itom[i]))
1656                                                         continue;
1657
1658                                                 moldyn->func3b_k2(moldyn,
1659                                                                   &(itom[i]),
1660                                                                   jtom,
1661                                                                   ktom,
1662                                                                   bc_ik|bc_ij);
1663
1664                                         } while(list_next_f(that)!=\
1665                                                 L_NO_NEXT_ELEMENT);
1666
1667                                 }
1668                                 
1669                                 }
1670
1671                                 /* 2bp post function */
1672                                 if(moldyn->func3b_j3) {
1673                                         moldyn->func3b_j3(moldyn,
1674                                                           &(itom[i]),
1675                                                           jtom,bc_ij);
1676                                 }
1677                                         
1678                         } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1679                 
1680                 }
1681                 
1682 #ifdef DEBUG
1683         //printf("\n\n");
1684 #endif
1685 #ifdef VDEBUG
1686         printf("\n\n");
1687 #endif
1688
1689         }
1690
1691 #ifdef DEBUG
1692         printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
1693 #endif
1694
1695         /* calculate global virial */
1696         for(i=0;i<count;i++) {
1697                 moldyn->virial.xx+=moldyn->atom[i].r.x*moldyn->atom[i].f.x;
1698                 moldyn->virial.yy+=moldyn->atom[i].r.y*moldyn->atom[i].f.y;
1699                 moldyn->virial.zz+=moldyn->atom[i].r.z*moldyn->atom[i].f.z;
1700                 moldyn->virial.xy+=moldyn->atom[i].r.y*moldyn->atom[i].f.x;
1701                 moldyn->virial.xz+=moldyn->atom[i].r.z*moldyn->atom[i].f.x;
1702                 moldyn->virial.yz+=moldyn->atom[i].r.z*moldyn->atom[i].f.y;
1703         }
1704
1705         return 0;
1706 }
1707
1708 /*
1709  * virial calculation
1710  */
1711
1712 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
1713 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
1714
1715         a->virial.xx+=f->x*d->x;
1716         a->virial.yy+=f->y*d->y;
1717         a->virial.zz+=f->z*d->z;
1718         a->virial.xy+=f->x*d->y;
1719         a->virial.xz+=f->x*d->z;
1720         a->virial.yz+=f->y*d->z;
1721
1722         return 0;
1723 }
1724
1725 /*
1726  * periodic boundary checking
1727  */
1728
1729 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1730 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1731         
1732         double x,y,z;
1733         t_3dvec *dim;
1734
1735         dim=&(moldyn->dim);
1736
1737         x=dim->x/2;
1738         y=dim->y/2;
1739         z=dim->z/2;
1740
1741         if(moldyn->status&MOLDYN_STAT_PBX) {
1742                 if(a->x>=x) a->x-=dim->x;
1743                 else if(-a->x>x) a->x+=dim->x;
1744         }
1745         if(moldyn->status&MOLDYN_STAT_PBY) {
1746                 if(a->y>=y) a->y-=dim->y;
1747                 else if(-a->y>y) a->y+=dim->y;
1748         }
1749         if(moldyn->status&MOLDYN_STAT_PBZ) {
1750                 if(a->z>=z) a->z-=dim->z;
1751                 else if(-a->z>z) a->z+=dim->z;
1752         }
1753
1754         return 0;
1755 }
1756         
1757 /*
1758  * debugging / critical check functions
1759  */
1760
1761 int moldyn_bc_check(t_moldyn *moldyn) {
1762
1763         t_atom *atom;
1764         t_3dvec *dim;
1765         int i;
1766         double x;
1767         u8 byte;
1768         int j,k;
1769
1770         atom=moldyn->atom;
1771         dim=&(moldyn->dim);
1772         x=dim->x/2;
1773
1774         for(i=0;i<moldyn->count;i++) {
1775                 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
1776                         printf("FATAL: atom %d: x: %.20f (%.20f)\n",
1777                                i,atom[i].r.x,dim->x/2);
1778                         printf("diagnostic:\n");
1779                         printf("-----------\natom.r.x:\n");
1780                         for(j=0;j<8;j++) {
1781                                 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
1782                                 for(k=0;k<8;k++)
1783                                         printf("%d%c",
1784                                         ((byte)&(1<<k))?1:0,
1785                                         (k==7)?'\n':'|');
1786                         }
1787                         printf("---------------\nx=dim.x/2:\n");
1788                         for(j=0;j<8;j++) {
1789                                 memcpy(&byte,(u8 *)(&x)+j,1);
1790                                 for(k=0;k<8;k++)
1791                                         printf("%d%c",
1792                                         ((byte)&(1<<k))?1:0,
1793                                         (k==7)?'\n':'|');
1794                         }
1795                         if(atom[i].r.x==x) printf("the same!\n");
1796                         else printf("different!\n");
1797                 }
1798                 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
1799                         printf("FATAL: atom %d: y: %.20f (%.20f)\n",
1800                                i,atom[i].r.y,dim->y/2);
1801                 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
1802                         printf("FATAL: atom %d: z: %.20f (%.20f)\n",
1803                                i,atom[i].r.z,dim->z/2);
1804         }
1805
1806         return 0;
1807 }