]> www.hackdaworld.org Git - physik/posic.git/blob - moldyn.c
4c43f2e1641dbd2a35bfab5f9ffdc78243d9c6b2
[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 >/dev/null 2>&1",
408                          moldyn->vlsdir);
409                 system(sc);
410                 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
411                          moldyn->vlsdir);
412                 system(sc);
413                 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
414                          moldyn->vlsdir);
415                 system(sc);
416         }
417         if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
418
419         return 0;
420 }
421
422 /*
423  * creating lattice functions
424  */
425
426 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
427                    u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
428
429         int new,count;
430         int ret;
431         t_3dvec orig;
432         void *ptr;
433         t_atom *atom;
434
435         new=a*b*c;
436         count=moldyn->count;
437
438         /* how many atoms do we expect */
439         if(type==CUBIC) new*=1;
440         if(type==FCC) new*=4;
441         if(type==DIAMOND) new*=8;
442
443         /* allocate space for atoms */
444         ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
445         if(!ptr) {
446                 perror("[moldyn] realloc (create lattice)");
447                 return -1;
448         }
449         moldyn->atom=ptr;
450         atom=&(moldyn->atom[count]);
451
452         /* no atoms on the boundaries (only reason: it looks better!) */
453         if(!origin) {
454                 orig.x=0.5*lc;
455                 orig.y=0.5*lc;
456                 orig.z=0.5*lc;
457         }
458         else {
459                 orig.x=origin->x;
460                 orig.y=origin->y;
461                 orig.z=origin->z;
462         }
463
464         switch(type) {
465                 case CUBIC:
466                         set_nn_dist(moldyn,lc);
467                         ret=cubic_init(a,b,c,lc,atom,&orig);
468                         break;
469                 case FCC:
470                         if(!origin)
471                                 v3_scale(&orig,&orig,0.5);
472                         set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
473                         ret=fcc_init(a,b,c,lc,atom,&orig);
474                         break;
475                 case DIAMOND:
476                         if(!origin)
477                                 v3_scale(&orig,&orig,0.25);
478                         set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
479                         ret=diamond_init(a,b,c,lc,atom,&orig);
480                         break;
481                 default:
482                         printf("unknown lattice type (%02x)\n",type);
483                         return -1;
484         }
485
486         /* debug */
487         if(ret!=new) {
488                 printf("[moldyn] creating lattice failed\n");
489                 printf("  amount of atoms\n");
490                 printf("  - expected: %d\n",new);
491                 printf("  - created: %d\n",ret);
492                 return -1;
493         }
494
495         moldyn->count+=new;
496         printf("[moldyn] created lattice with %d atoms\n",new);
497
498         for(ret=0;ret<new;ret++) {
499                 atom[ret].element=element;
500                 atom[ret].mass=mass;
501                 atom[ret].attr=attr;
502                 atom[ret].brand=brand;
503                 atom[ret].tag=count+ret;
504                 check_per_bound(moldyn,&(atom[ret].r));
505         }
506
507         return ret;
508 }
509
510 /* cubic init */
511 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
512
513         int count;
514         t_3dvec r;
515         int i,j,k;
516         t_3dvec o;
517
518         count=0;
519         if(origin)
520                 v3_copy(&o,origin);
521         else
522                 v3_zero(&o);
523
524         r.x=o.x;
525         for(i=0;i<a;i++) {
526                 r.y=o.y;
527                 for(j=0;j<b;j++) {
528                         r.z=o.z;
529                         for(k=0;k<c;k++) {
530                                 v3_copy(&(atom[count].r),&r);
531                                 count+=1;
532                                 r.z+=lc;
533                         }
534                         r.y+=lc;
535                 }
536                 r.x+=lc;
537         }
538
539         for(i=0;i<count;i++) {
540                 atom[i].r.x-=(a*lc)/2.0;
541                 atom[i].r.y-=(b*lc)/2.0;
542                 atom[i].r.z-=(c*lc)/2.0;
543         }
544
545         return count;
546 }
547
548 /* fcc lattice init */
549 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
550
551         int count;
552         int i,j,k,l;
553         t_3dvec o,r,n;
554         t_3dvec basis[3];
555
556         count=0;
557         if(origin)
558                 v3_copy(&o,origin);
559         else
560                 v3_zero(&o);
561
562         /* construct the basis */
563         memset(basis,0,3*sizeof(t_3dvec));
564         basis[0].x=0.5*lc;
565         basis[0].y=0.5*lc;
566         basis[1].x=0.5*lc;
567         basis[1].z=0.5*lc;
568         basis[2].y=0.5*lc;
569         basis[2].z=0.5*lc;
570
571         /* fill up the room */
572         r.x=o.x;
573         for(i=0;i<a;i++) {
574                 r.y=o.y;
575                 for(j=0;j<b;j++) {
576                         r.z=o.z;
577                         for(k=0;k<c;k++) {
578                                 /* first atom */
579                                 v3_copy(&(atom[count].r),&r);
580                                 count+=1;
581                                 r.z+=lc;
582                                 /* the three face centered atoms */
583                                 for(l=0;l<3;l++) {
584                                         v3_add(&n,&r,&basis[l]);
585                                         v3_copy(&(atom[count].r),&n);
586                                         count+=1;
587                                 }
588                         }
589                         r.y+=lc;
590                 }
591                 r.x+=lc;
592         }
593                                 
594         /* coordinate transformation */
595         for(i=0;i<count;i++) {
596                 atom[i].r.x-=(a*lc)/2.0;
597                 atom[i].r.y-=(b*lc)/2.0;
598                 atom[i].r.z-=(c*lc)/2.0;
599         }
600
601         return count;
602 }
603
604 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
605
606         int count;
607         t_3dvec o;
608
609         count=fcc_init(a,b,c,lc,atom,origin);
610
611         o.x=0.25*lc;
612         o.y=0.25*lc;
613         o.z=0.25*lc;
614
615         if(origin) v3_add(&o,&o,origin);
616
617         count+=fcc_init(a,b,c,lc,&atom[count],&o);
618
619         return count;
620 }
621
622 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
623              t_3dvec *r,t_3dvec *v) {
624
625         t_atom *atom;
626         void *ptr;
627         int count;
628         
629         atom=moldyn->atom;
630         count=(moldyn->count)++;
631
632         ptr=realloc(atom,(count+1)*sizeof(t_atom));
633         if(!ptr) {
634                 perror("[moldyn] realloc (add atom)");
635                 return -1;
636         }
637         moldyn->atom=ptr;
638
639         atom=moldyn->atom;
640         atom[count].r=*r;
641         atom[count].v=*v;
642         atom[count].element=element;
643         atom[count].mass=mass;
644         atom[count].brand=brand;
645         atom[count].tag=count;
646         atom[count].attr=attr;
647
648         return 0;
649 }
650
651 int destroy_atoms(t_moldyn *moldyn) {
652
653         if(moldyn->atom) free(moldyn->atom);
654
655         return 0;
656 }
657
658 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
659
660         /*
661          * - gaussian distribution of velocities
662          * - zero total momentum
663          * - velocity scaling (E = 3/2 N k T), E: kinetic energy
664          */
665
666         int i;
667         double v,sigma;
668         t_3dvec p_total,delta;
669         t_atom *atom;
670         t_random *random;
671
672         atom=moldyn->atom;
673         random=&(moldyn->random);
674
675         printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
676
677         /* gaussian distribution of velocities */
678         v3_zero(&p_total);
679         for(i=0;i<moldyn->count;i++) {
680                 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
681                 /* x direction */
682                 v=sigma*rand_get_gauss(random);
683                 atom[i].v.x=v;
684                 p_total.x+=atom[i].mass*v;
685                 /* y direction */
686                 v=sigma*rand_get_gauss(random);
687                 atom[i].v.y=v;
688                 p_total.y+=atom[i].mass*v;
689                 /* z direction */
690                 v=sigma*rand_get_gauss(random);
691                 atom[i].v.z=v;
692                 p_total.z+=atom[i].mass*v;
693         }
694
695         /* zero total momentum */
696         v3_scale(&p_total,&p_total,1.0/moldyn->count);
697         for(i=0;i<moldyn->count;i++) {
698                 v3_scale(&delta,&p_total,1.0/atom[i].mass);
699                 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
700         }
701
702         /* velocity scaling */
703         scale_velocity(moldyn,equi_init);
704
705         return 0;
706 }
707
708 double temperature_calc(t_moldyn *moldyn) {
709
710         /* assume up to date kinetic energy, which is 3/2 N k_B T */
711
712         moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
713         moldyn->t_sum+=moldyn->t;
714         moldyn->mean_t=moldyn->t_sum/moldyn->total_steps;
715
716         return moldyn->t;
717 }
718
719 double get_temperature(t_moldyn *moldyn) {
720
721         return moldyn->t;
722 }
723
724 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
725
726         int i;
727         double e,scale;
728         t_atom *atom;
729         int count;
730
731         atom=moldyn->atom;
732
733         /*
734          * - velocity scaling (E = 3/2 N k T), E: kinetic energy
735          */
736
737         /* get kinetic energy / temperature & count involved atoms */
738         e=0.0;
739         count=0;
740         for(i=0;i<moldyn->count;i++) {
741                 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
742                         e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
743                         count+=1;
744                 }
745         }
746         e*=0.5;
747         if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
748         else return 0;  /* no atoms involved in scaling! */
749         
750         /* (temporary) hack for e,t = 0 */
751         if(e==0.0) {
752         moldyn->t=0.0;
753                 if(moldyn->t_ref!=0.0) {
754                         thermal_init(moldyn,equi_init);
755                         return 0;
756                 }
757                 else
758                         return 0; /* no scaling needed */
759         }
760
761
762         /* get scaling factor */
763         scale=moldyn->t_ref/moldyn->t;
764         if(equi_init&TRUE)
765                 scale*=2.0;
766         else
767                 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
768                         scale=1.0+(scale-1.0)/moldyn->t_tc;
769         scale=sqrt(scale);
770
771         /* velocity scaling */
772         for(i=0;i<moldyn->count;i++) {
773                 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
774                         v3_scale(&(atom[i].v),&(atom[i].v),scale);
775         }
776
777         return 0;
778 }
779
780 double ideal_gas_law_pressure(t_moldyn *moldyn) {
781
782         double p;
783
784         p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
785
786         return p;
787 }
788
789 double pressure_calc(t_moldyn *moldyn) {
790
791         int i;
792         double v;
793         t_virial *virial;
794
795         /*
796          * PV = NkT + <W>
797          * W = 1/3 sum_i f_i r_i
798          * virial = sum_i f_i r_i
799          * 
800          * => P = (2 Ekin + virial) / (3V)
801          */
802
803         v=0.0;
804         for(i=0;i<moldyn->count;i++) {
805                 virial=&(moldyn->atom[i].virial);
806                 v+=(virial->xx+virial->yy+virial->zz);
807         }
808
809         /* virial sum and mean virial */
810         moldyn->virial_sum+=v;
811         moldyn->mean_v=moldyn->virial_sum/moldyn->total_steps;
812
813         /* assume up to date kinetic energy */
814         moldyn->p=2.0*moldyn->ekin+moldyn->mean_v;
815         moldyn->p/=(3.0*moldyn->volume);
816         moldyn->p_sum+=moldyn->p;
817         moldyn->mean_p=moldyn->p_sum/moldyn->total_steps;
818
819         /* pressure from 'absolute coordinates' virial */
820         virial=&(moldyn->virial);
821         v=virial->xx+virial->yy+virial->zz;
822         moldyn->gp=2.0*moldyn->ekin+v;
823         moldyn->gp/=(3.0*moldyn->volume);
824         moldyn->gp_sum+=moldyn->gp;
825         moldyn->mean_gp=moldyn->gp_sum/moldyn->total_steps;
826
827         return moldyn->p;
828 }       
829
830 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
831
832         t_3dvec dim,*tp;
833         double u_up,u_down,dv;
834         double scale,p;
835         t_atom *store;
836
837         /*
838          * dU = - p dV
839          *
840          * => p = - dU/dV
841          *
842          */
843
844         scale=0.00001;
845         dv=8*scale*scale*scale*moldyn->volume;
846
847         store=malloc(moldyn->count*sizeof(t_atom));
848         if(store==NULL) {
849                 printf("[moldyn] allocating store mem failed\n");
850                 return -1;
851         }
852
853         /* save unscaled potential energy + atom/dim configuration */
854         memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
855         dim=moldyn->dim;
856
857         /* scale up dimension and atom positions */
858         scale_dim(moldyn,SCALE_UP,scale,TRUE,TRUE,TRUE);
859         scale_atoms(moldyn,SCALE_UP,scale,TRUE,TRUE,TRUE);
860         link_cell_shutdown(moldyn);
861         link_cell_init(moldyn,QUIET);
862         potential_force_calc(moldyn);
863         u_up=moldyn->energy;
864
865         /* restore atomic configuration + dim */
866         memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
867         moldyn->dim=dim;
868
869         /* scale down dimension and atom positions */
870         scale_dim(moldyn,SCALE_DOWN,scale,TRUE,TRUE,TRUE);
871         scale_atoms(moldyn,SCALE_DOWN,scale,TRUE,TRUE,TRUE);
872         link_cell_shutdown(moldyn);
873         link_cell_init(moldyn,QUIET);
874         potential_force_calc(moldyn);
875         u_down=moldyn->energy;
876         
877         /* calculate pressure */
878         p=-(u_up-u_down)/dv;
879 printf("-------> %.10f %.10f %f\n",u_up/EV/moldyn->count,u_down/EV/moldyn->count,p/BAR);
880
881         /* restore atomic configuration + dim */
882         memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
883         moldyn->dim=dim;
884
885         /* restore energy */
886         potential_force_calc(moldyn);
887
888         link_cell_shutdown(moldyn);
889         link_cell_init(moldyn,QUIET);
890
891         return p;
892 }
893
894 double get_pressure(t_moldyn *moldyn) {
895
896         return moldyn->p;
897
898 }
899
900 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
901
902         t_3dvec *dim;
903
904         dim=&(moldyn->dim);
905
906         if(dir==SCALE_UP)
907                 scale=1.0+scale;
908
909         if(dir==SCALE_DOWN)
910                 scale=1.0-scale;
911
912         if(x) dim->x*=scale;
913         if(y) dim->y*=scale;
914         if(z) dim->z*=scale;
915
916         return 0;
917 }
918
919 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
920
921         int i;
922         t_3dvec *r;
923
924         if(dir==SCALE_UP)
925                 scale=1.0+scale;
926
927         if(dir==SCALE_DOWN)
928                 scale=1.0-scale;
929
930         for(i=0;i<moldyn->count;i++) {
931                 r=&(moldyn->atom[i].r);
932                 if(x) r->x*=scale;
933                 if(y) r->y*=scale;
934                 if(z) r->z*=scale;
935         }
936
937         return 0;
938 }
939
940 int scale_volume(t_moldyn *moldyn) {
941
942         t_3dvec *dim,*vdim;
943         double scale;
944         t_linkcell *lc;
945
946         vdim=&(moldyn->vis.dim);
947         dim=&(moldyn->dim);
948         lc=&(moldyn->lc);
949
950         /* scaling factor */
951         if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
952                 scale=1.0-(moldyn->p_ref-moldyn->p)/moldyn->p_tc;
953                 scale=pow(scale,ONE_THIRD);
954         }
955         else {
956                 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
957         }
958 moldyn->debug=scale;
959
960         /* scale the atoms and dimensions */
961         scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
962         scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
963
964         /* visualize dimensions */
965         if(vdim->x!=0) {
966                 vdim->x=dim->x;
967                 vdim->y=dim->y;
968                 vdim->z=dim->z;
969         }
970
971         /* recalculate scaled volume */
972         moldyn->volume=dim->x*dim->y*dim->z;
973
974         /* adjust/reinit linkcell */
975         if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
976            ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
977            ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
978                 link_cell_shutdown(moldyn);
979                 link_cell_init(moldyn,QUIET);
980         } else {
981                 lc->x*=scale;
982                 lc->y*=scale;
983                 lc->z*=scale;
984         }
985
986         return 0;
987
988 }
989
990 double e_kin_calc(t_moldyn *moldyn) {
991
992         int i;
993         t_atom *atom;
994
995         atom=moldyn->atom;
996         moldyn->ekin=0.0;
997
998         for(i=0;i<moldyn->count;i++)
999                 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1000
1001         return moldyn->ekin;
1002 }
1003
1004 double get_total_energy(t_moldyn *moldyn) {
1005
1006         return(moldyn->ekin+moldyn->energy);
1007 }
1008
1009 t_3dvec get_total_p(t_moldyn *moldyn) {
1010
1011         t_3dvec p,p_total;
1012         int i;
1013         t_atom *atom;
1014
1015         atom=moldyn->atom;
1016
1017         v3_zero(&p_total);
1018         for(i=0;i<moldyn->count;i++) {
1019                 v3_scale(&p,&(atom[i].v),atom[i].mass);
1020                 v3_add(&p_total,&p_total,&p);
1021         }
1022
1023         return p_total;
1024 }
1025
1026 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1027
1028         double tau;
1029
1030         /* nn_dist is the nearest neighbour distance */
1031
1032         tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1033
1034         return tau;     
1035 }
1036
1037 /*
1038  * numerical tricks
1039  */
1040
1041 /* linked list / cell method */
1042
1043 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1044
1045         t_linkcell *lc;
1046         int i;
1047
1048         lc=&(moldyn->lc);
1049
1050         /* partitioning the md cell */
1051         lc->nx=moldyn->dim.x/moldyn->cutoff;
1052         lc->x=moldyn->dim.x/lc->nx;
1053         lc->ny=moldyn->dim.y/moldyn->cutoff;
1054         lc->y=moldyn->dim.y/lc->ny;
1055         lc->nz=moldyn->dim.z/moldyn->cutoff;
1056         lc->z=moldyn->dim.z/lc->nz;
1057
1058         lc->cells=lc->nx*lc->ny*lc->nz;
1059         lc->subcell=malloc(lc->cells*sizeof(t_list));
1060
1061         if(lc->cells<27)
1062                 printf("[moldyn] FATAL: less then 27 subcells!\n");
1063
1064         if(vol) {
1065                 printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
1066                 printf("  x: %d x %f A\n",lc->nx,lc->x);
1067                 printf("  y: %d x %f A\n",lc->ny,lc->y);
1068                 printf("  z: %d x %f A\n",lc->nz,lc->z);
1069         }
1070
1071         for(i=0;i<lc->cells;i++)
1072                 list_init_f(&(lc->subcell[i]));
1073
1074         link_cell_update(moldyn);
1075         
1076         return 0;
1077 }
1078
1079 int link_cell_update(t_moldyn *moldyn) {
1080
1081         int count,i,j,k;
1082         int nx,ny;
1083         t_atom *atom;
1084         t_linkcell *lc;
1085         double x,y,z;
1086
1087         atom=moldyn->atom;
1088         lc=&(moldyn->lc);
1089
1090         nx=lc->nx;
1091         ny=lc->ny;
1092
1093         x=moldyn->dim.x/2;
1094         y=moldyn->dim.y/2;
1095         z=moldyn->dim.z/2;
1096
1097         for(i=0;i<lc->cells;i++)
1098                 list_destroy_f(&(lc->subcell[i]));
1099         
1100         for(count=0;count<moldyn->count;count++) {
1101                 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1102                 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1103                 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1104                 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1105                                      &(atom[count]));
1106         }
1107
1108         return 0;
1109 }
1110
1111 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
1112
1113         t_linkcell *lc;
1114         int a;
1115         int count1,count2;
1116         int ci,cj,ck;
1117         int nx,ny,nz;
1118         int x,y,z;
1119         u8 bx,by,bz;
1120
1121         lc=&(moldyn->lc);
1122         nx=lc->nx;
1123         ny=lc->ny;
1124         nz=lc->nz;
1125         count1=1;
1126         count2=27;
1127         a=nx*ny;
1128
1129         cell[0]=lc->subcell[i+j*nx+k*a];
1130         for(ci=-1;ci<=1;ci++) {
1131                 bx=0;
1132                 x=i+ci;
1133                 if((x<0)||(x>=nx)) {
1134                         x=(x+nx)%nx;
1135                         bx=1;
1136                 }
1137                 for(cj=-1;cj<=1;cj++) {
1138                         by=0;
1139                         y=j+cj;
1140                         if((y<0)||(y>=ny)) {
1141                                 y=(y+ny)%ny;
1142                                 by=1;
1143                         }
1144                         for(ck=-1;ck<=1;ck++) {
1145                                 bz=0;
1146                                 z=k+ck;
1147                                 if((z<0)||(z>=nz)) {
1148                                         z=(z+nz)%nz;
1149                                         bz=1;
1150                                 }
1151                                 if(!(ci|cj|ck)) continue;
1152                                 if(bx|by|bz) {
1153                                         cell[--count2]=lc->subcell[x+y*nx+z*a];
1154                                 }
1155                                 else {
1156                                         cell[count1++]=lc->subcell[x+y*nx+z*a];
1157                                 }
1158                         }
1159                 }
1160         }
1161
1162         lc->dnlc=count1;
1163
1164         return count1;
1165 }
1166
1167 int link_cell_shutdown(t_moldyn *moldyn) {
1168
1169         int i;
1170         t_linkcell *lc;
1171
1172         lc=&(moldyn->lc);
1173
1174         for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
1175                 list_destroy_f(&(moldyn->lc.subcell[i]));
1176
1177         free(lc->subcell);
1178
1179         return 0;
1180 }
1181
1182 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1183
1184         int count;
1185         void *ptr;
1186         t_moldyn_schedule *schedule;
1187
1188         schedule=&(moldyn->schedule);
1189         count=++(schedule->total_sched);
1190
1191         ptr=realloc(schedule->runs,count*sizeof(int));
1192         if(!ptr) {
1193                 perror("[moldyn] realloc (runs)");
1194                 return -1;
1195         }
1196         schedule->runs=ptr;
1197         schedule->runs[count-1]=runs;
1198
1199         ptr=realloc(schedule->tau,count*sizeof(double));
1200         if(!ptr) {
1201                 perror("[moldyn] realloc (tau)");
1202                 return -1;
1203         }
1204         schedule->tau=ptr;
1205         schedule->tau[count-1]=tau;
1206
1207         printf("[moldyn] schedule added:\n");
1208         printf("  number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1209                                        
1210
1211         return 0;
1212 }
1213
1214 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1215
1216         moldyn->schedule.hook=hook;
1217         moldyn->schedule.hook_params=hook_params;
1218         
1219         return 0;
1220 }
1221
1222 /*
1223  *
1224  * 'integration of newtons equation' - algorithms
1225  *
1226  */
1227
1228 /* start the integration */
1229
1230 int moldyn_integrate(t_moldyn *moldyn) {
1231
1232         int i;
1233         unsigned int e,m,s,v,p,t;
1234         t_3dvec momentum;
1235         t_moldyn_schedule *sched;
1236         t_atom *atom;
1237         int fd;
1238         char dir[128];
1239         double ds;
1240         double energy_scale;
1241         //double tp;
1242
1243         sched=&(moldyn->schedule);
1244         atom=moldyn->atom;
1245
1246         /* initialize linked cell method */
1247         link_cell_init(moldyn,VERBOSE);
1248
1249         /* logging & visualization */
1250         e=moldyn->ewrite;
1251         m=moldyn->mwrite;
1252         s=moldyn->swrite;
1253         v=moldyn->vwrite;
1254         p=moldyn->pwrite;
1255         t=moldyn->twrite;
1256
1257         /* sqaure of some variables */
1258         moldyn->tau_square=moldyn->tau*moldyn->tau;
1259         moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
1260
1261         /* energy scaling factor */
1262         energy_scale=moldyn->count*EV;
1263
1264         /* calculate initial forces */
1265         potential_force_calc(moldyn);
1266 #ifdef DEBUG
1267 return 0;
1268 #endif
1269
1270         /* some stupid checks before we actually start calculating bullshit */
1271         if(moldyn->cutoff>0.5*moldyn->dim.x)
1272                 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
1273         if(moldyn->cutoff>0.5*moldyn->dim.y)
1274                 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
1275         if(moldyn->cutoff>0.5*moldyn->dim.z)
1276                 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
1277         ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1278         if(ds>0.05*moldyn->nnd)
1279                 printf("[moldyn] warning: forces too high / tau too small!\n");
1280
1281         /* zero absolute time */
1282         moldyn->time=0.0;
1283         moldyn->total_steps=0;
1284
1285         /* debugging, ignore */
1286         moldyn->debug=0;
1287
1288         /* tell the world */
1289         printf("[moldyn] integration start, go get a coffee ...\n");
1290
1291         /* executing the schedule */
1292         for(sched->count=0;sched->count<sched->total_sched;sched->count++) {
1293
1294                 /* setting amount of runs and finite time step size */
1295                 moldyn->tau=sched->tau[sched->count];
1296                 moldyn->tau_square=moldyn->tau*moldyn->tau;
1297                 moldyn->time_steps=sched->runs[sched->count];
1298
1299         /* integration according to schedule */
1300
1301         for(i=0;i<moldyn->time_steps;i++) {
1302
1303                 /* integration step */
1304                 moldyn->integrate(moldyn);
1305
1306                 /* calculate kinetic energy, temperature and pressure */
1307                 e_kin_calc(moldyn);
1308                 temperature_calc(moldyn);
1309                 pressure_calc(moldyn);
1310                 //tp=thermodynamic_pressure_calc(moldyn);
1311 //printf("thermodynamic p: %f\n",thermodynamic_pressure_calc(moldyn)/BAR);
1312
1313                 /* p/t scaling */
1314                 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1315                         scale_velocity(moldyn,FALSE);
1316                 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1317                         scale_volume(moldyn);
1318
1319                 /* check for log & visualization */
1320                 if(e) {
1321                         if(!(i%e))
1322                                 dprintf(moldyn->efd,
1323                                         "%f %f %f %f\n",
1324                                         moldyn->time,moldyn->ekin/energy_scale,
1325                                         moldyn->energy/energy_scale,
1326                                         get_total_energy(moldyn)/energy_scale);
1327                 }
1328                 if(m) {
1329                         if(!(i%m)) {
1330                                 momentum=get_total_p(moldyn);
1331                                 dprintf(moldyn->mfd,
1332                                         "%f %f %f %f %f\n",moldyn->time,
1333                                         momentum.x,momentum.y,momentum.z,
1334                                         v3_norm(&momentum));
1335                         }
1336                 }
1337                 if(p) {
1338                         if(!(i%p)) {
1339                                 dprintf(moldyn->pfd,
1340                                         "%f %f %f %f %f\n",moldyn->time,
1341                                          moldyn->p/BAR,moldyn->mean_p/BAR,
1342                                          moldyn->gp/BAR,moldyn->mean_gp/BAR);
1343                         }
1344                 }
1345                 if(t) {
1346                         if(!(i%t)) {
1347                                 dprintf(moldyn->tfd,
1348                                         "%f %f %f\n",
1349                                         moldyn->time,moldyn->t,moldyn->mean_t);
1350                         }
1351                 }
1352                 if(s) {
1353                         if(!(i%s)) {
1354                                 snprintf(dir,128,"%s/s-%07.f.save",
1355                                          moldyn->vlsdir,moldyn->time);
1356                                 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT);
1357                                 if(fd<0) perror("[moldyn] save fd open");
1358                                 else {
1359                                         write(fd,moldyn,sizeof(t_moldyn));
1360                                         write(fd,moldyn->atom,
1361                                               moldyn->count*sizeof(t_atom));
1362                                 }
1363                                 close(fd);
1364                         }       
1365                 }
1366                 if(v) {
1367                         if(!(i%v)) {
1368                                 visual_atoms(&(moldyn->vis),moldyn->time,
1369                                              moldyn->atom,moldyn->count);
1370                         }
1371                 }
1372
1373                 /* display progress */
1374                 if(!(i%10)) {
1375                         printf("\rsched: %d, steps: %d, T: %f, P: %f %f V: %f",
1376                                sched->count,i,
1377                                moldyn->mean_t,
1378                                moldyn->mean_p/BAR,
1379                                moldyn->mean_gp/BAR,
1380                                moldyn->volume);
1381                         fflush(stdout);
1382                 }
1383
1384                 /* increase absolute time */
1385                 moldyn->time+=moldyn->tau;
1386                 moldyn->total_steps+=1;
1387
1388         }
1389
1390                 /* check for hooks */
1391                 if(sched->count+1<sched->total_sched)
1392                         if(sched->hook)
1393                                 sched->hook(moldyn,sched->hook_params);
1394
1395                 /* get a new info line */
1396                 printf("\n");
1397
1398         }
1399
1400         return 0;
1401 }
1402
1403 /* velocity verlet */
1404
1405 int velocity_verlet(t_moldyn *moldyn) {
1406
1407         int i,count;
1408         double tau,tau_square,h;
1409         t_3dvec delta;
1410         t_atom *atom;
1411
1412         atom=moldyn->atom;
1413         count=moldyn->count;
1414         tau=moldyn->tau;
1415         tau_square=moldyn->tau_square;
1416
1417         for(i=0;i<count;i++) {
1418                 /* new positions */
1419                 h=0.5/atom[i].mass;
1420                 v3_scale(&delta,&(atom[i].v),tau);
1421                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1422                 v3_scale(&delta,&(atom[i].f),h*tau_square);
1423                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1424                 check_per_bound(moldyn,&(atom[i].r));
1425
1426                 /* velocities [actually v(t+tau/2)] */
1427                 v3_scale(&delta,&(atom[i].f),h*tau);
1428                 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1429         }
1430
1431         /* neighbour list update */
1432         link_cell_update(moldyn);
1433
1434         /* forces depending on chosen potential */
1435         potential_force_calc(moldyn);
1436
1437         for(i=0;i<count;i++) {
1438                 /* again velocities [actually v(t+tau)] */
1439                 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1440                 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1441         }
1442
1443         return 0;
1444 }
1445
1446
1447 /*
1448  *
1449  * potentials & corresponding forces & virial routine
1450  * 
1451  */
1452
1453 /* generic potential and force calculation */
1454
1455 int potential_force_calc(t_moldyn *moldyn) {
1456
1457         int i,j,k,count;
1458         t_atom *itom,*jtom,*ktom;
1459         t_virial *virial;
1460         t_linkcell *lc;
1461         t_list neighbour_i[27];
1462         t_list neighbour_i2[27];
1463         t_list *this,*that;
1464         u8 bc_ij,bc_ik;
1465         int dnlc;
1466
1467         count=moldyn->count;
1468         itom=moldyn->atom;
1469         lc=&(moldyn->lc);
1470
1471         /* reset energy */
1472         moldyn->energy=0.0;
1473
1474         /* reset global virial */
1475         memset(&(moldyn->virial),0,sizeof(t_virial));
1476
1477         /* reset force, site energy and virial of every atom */
1478         for(i=0;i<count;i++) {
1479
1480                 /* reset force */
1481                 v3_zero(&(itom[i].f));
1482
1483                 /* reset virial */
1484                 virial=(&(itom[i].virial));
1485                 virial->xx=0.0;
1486                 virial->yy=0.0;
1487                 virial->zz=0.0;
1488                 virial->xy=0.0;
1489                 virial->xz=0.0;
1490                 virial->yz=0.0;
1491         
1492                 /* reset site energy */
1493                 itom[i].e=0.0;
1494
1495         }
1496
1497         /* get energy, force and virial of every atom */
1498
1499         /* first (and only) loop over atoms i */
1500         for(i=0;i<count;i++) {
1501
1502                 /* single particle potential/force */
1503                 if(itom[i].attr&ATOM_ATTR_1BP)
1504                         if(moldyn->func1b)
1505                                 moldyn->func1b(moldyn,&(itom[i]));
1506
1507                 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1508                         continue;
1509
1510                 /* 2 body pair potential/force */
1511         
1512                 link_cell_neighbour_index(moldyn,
1513                                           (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1514                                           (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1515                                           (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1516                                           neighbour_i);
1517
1518                 dnlc=lc->dnlc;
1519
1520                 /* first loop over atoms j */
1521                 if(moldyn->func2b) {
1522                         for(j=0;j<27;j++) {
1523
1524                                 this=&(neighbour_i[j]);
1525                                 list_reset_f(this);
1526
1527                                 if(this->start==NULL)
1528                                         continue;
1529
1530                                 bc_ij=(j<dnlc)?0:1;
1531
1532                                 do {
1533                                         jtom=this->current->data;
1534
1535                                         if(jtom==&(itom[i]))
1536                                                 continue;
1537
1538                                         if((jtom->attr&ATOM_ATTR_2BP)&
1539                                            (itom[i].attr&ATOM_ATTR_2BP)) {
1540                                                 moldyn->func2b(moldyn,
1541                                                                &(itom[i]),
1542                                                                jtom,
1543                                                                bc_ij);
1544                                         }
1545                                 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1546
1547                         }
1548                 }
1549
1550                 /* 3 body potential/force */
1551
1552                 if(!(itom[i].attr&ATOM_ATTR_3BP))
1553                         continue;
1554
1555                 /* copy the neighbour lists */
1556                 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
1557
1558                 /* second loop over atoms j */
1559                 for(j=0;j<27;j++) {
1560
1561                         this=&(neighbour_i[j]);
1562                         list_reset_f(this);
1563
1564                         if(this->start==NULL)
1565                                 continue;
1566
1567                         bc_ij=(j<dnlc)?0:1;
1568
1569                         do {
1570                                 jtom=this->current->data;
1571
1572                                 if(jtom==&(itom[i]))
1573                                         continue;
1574
1575                                 if(!(jtom->attr&ATOM_ATTR_3BP))
1576                                         continue;
1577
1578                                 /* reset 3bp run */
1579                                 moldyn->run3bp=1;
1580
1581                                 if(moldyn->func3b_j1)
1582                                         moldyn->func3b_j1(moldyn,
1583                                                           &(itom[i]),
1584                                                           jtom,
1585                                                           bc_ij);
1586
1587                                 /* in first j loop, 3bp run can be skipped */
1588                                 if(!(moldyn->run3bp))
1589                                         continue;
1590                         
1591                                 /* first loop over atoms k */
1592                                 if(moldyn->func3b_k1) {
1593
1594                                 for(k=0;k<27;k++) {
1595
1596                                         that=&(neighbour_i2[k]);
1597                                         list_reset_f(that);
1598                                         
1599                                         if(that->start==NULL)
1600                                                 continue;
1601
1602                                         bc_ik=(k<dnlc)?0:1;
1603
1604                                         do {
1605
1606                                                 ktom=that->current->data;
1607
1608                                                 if(!(ktom->attr&ATOM_ATTR_3BP))
1609                                                         continue;
1610
1611                                                 if(ktom==jtom)
1612                                                         continue;
1613
1614                                                 if(ktom==&(itom[i]))
1615                                                         continue;
1616
1617                                                 moldyn->func3b_k1(moldyn,
1618                                                                   &(itom[i]),
1619                                                                   jtom,
1620                                                                   ktom,
1621                                                                   bc_ik|bc_ij);
1622
1623                                         } while(list_next_f(that)!=\
1624                                                 L_NO_NEXT_ELEMENT);
1625
1626                                 }
1627
1628                                 }
1629
1630                                 if(moldyn->func3b_j2)
1631                                         moldyn->func3b_j2(moldyn,
1632                                                           &(itom[i]),
1633                                                           jtom,
1634                                                           bc_ij);
1635
1636                                 /* second loop over atoms k */
1637                                 if(moldyn->func3b_k2) {
1638
1639                                 for(k=0;k<27;k++) {
1640
1641                                         that=&(neighbour_i2[k]);
1642                                         list_reset_f(that);
1643                                         
1644                                         if(that->start==NULL)
1645                                                 continue;
1646
1647                                         bc_ik=(k<dnlc)?0:1;
1648
1649                                         do {
1650
1651                                                 ktom=that->current->data;
1652
1653                                                 if(!(ktom->attr&ATOM_ATTR_3BP))
1654                                                         continue;
1655
1656                                                 if(ktom==jtom)
1657                                                         continue;
1658
1659                                                 if(ktom==&(itom[i]))
1660                                                         continue;
1661
1662                                                 moldyn->func3b_k2(moldyn,
1663                                                                   &(itom[i]),
1664                                                                   jtom,
1665                                                                   ktom,
1666                                                                   bc_ik|bc_ij);
1667
1668                                         } while(list_next_f(that)!=\
1669                                                 L_NO_NEXT_ELEMENT);
1670
1671                                 }
1672                                 
1673                                 }
1674
1675                                 /* 2bp post function */
1676                                 if(moldyn->func3b_j3) {
1677                                         moldyn->func3b_j3(moldyn,
1678                                                           &(itom[i]),
1679                                                           jtom,bc_ij);
1680                                 }
1681                                         
1682                         } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1683                 
1684                 }
1685                 
1686 #ifdef DEBUG
1687         //printf("\n\n");
1688 #endif
1689 #ifdef VDEBUG
1690         printf("\n\n");
1691 #endif
1692
1693         }
1694
1695 #ifdef DEBUG
1696         printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
1697 #endif
1698
1699         /* calculate global virial */
1700         for(i=0;i<count;i++) {
1701                 moldyn->virial.xx+=moldyn->atom[i].r.x*moldyn->atom[i].f.x;
1702                 moldyn->virial.yy+=moldyn->atom[i].r.y*moldyn->atom[i].f.y;
1703                 moldyn->virial.zz+=moldyn->atom[i].r.z*moldyn->atom[i].f.z;
1704                 moldyn->virial.xy+=moldyn->atom[i].r.y*moldyn->atom[i].f.x;
1705                 moldyn->virial.xz+=moldyn->atom[i].r.z*moldyn->atom[i].f.x;
1706                 moldyn->virial.yz+=moldyn->atom[i].r.z*moldyn->atom[i].f.y;
1707         }
1708
1709         return 0;
1710 }
1711
1712 /*
1713  * virial calculation
1714  */
1715
1716 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
1717 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
1718
1719         a->virial.xx+=f->x*d->x;
1720         a->virial.yy+=f->y*d->y;
1721         a->virial.zz+=f->z*d->z;
1722         a->virial.xy+=f->x*d->y;
1723         a->virial.xz+=f->x*d->z;
1724         a->virial.yz+=f->y*d->z;
1725
1726         return 0;
1727 }
1728
1729 /*
1730  * periodic boundary checking
1731  */
1732
1733 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1734 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1735         
1736         double x,y,z;
1737         t_3dvec *dim;
1738
1739         dim=&(moldyn->dim);
1740
1741         x=dim->x/2;
1742         y=dim->y/2;
1743         z=dim->z/2;
1744
1745         if(moldyn->status&MOLDYN_STAT_PBX) {
1746                 if(a->x>=x) a->x-=dim->x;
1747                 else if(-a->x>x) a->x+=dim->x;
1748         }
1749         if(moldyn->status&MOLDYN_STAT_PBY) {
1750                 if(a->y>=y) a->y-=dim->y;
1751                 else if(-a->y>y) a->y+=dim->y;
1752         }
1753         if(moldyn->status&MOLDYN_STAT_PBZ) {
1754                 if(a->z>=z) a->z-=dim->z;
1755                 else if(-a->z>z) a->z+=dim->z;
1756         }
1757
1758         return 0;
1759 }
1760         
1761 /*
1762  * debugging / critical check functions
1763  */
1764
1765 int moldyn_bc_check(t_moldyn *moldyn) {
1766
1767         t_atom *atom;
1768         t_3dvec *dim;
1769         int i;
1770         double x;
1771         u8 byte;
1772         int j,k;
1773
1774         atom=moldyn->atom;
1775         dim=&(moldyn->dim);
1776         x=dim->x/2;
1777
1778         for(i=0;i<moldyn->count;i++) {
1779                 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
1780                         printf("FATAL: atom %d: x: %.20f (%.20f)\n",
1781                                i,atom[i].r.x,dim->x/2);
1782                         printf("diagnostic:\n");
1783                         printf("-----------\natom.r.x:\n");
1784                         for(j=0;j<8;j++) {
1785                                 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
1786                                 for(k=0;k<8;k++)
1787                                         printf("%d%c",
1788                                         ((byte)&(1<<k))?1:0,
1789                                         (k==7)?'\n':'|');
1790                         }
1791                         printf("---------------\nx=dim.x/2:\n");
1792                         for(j=0;j<8;j++) {
1793                                 memcpy(&byte,(u8 *)(&x)+j,1);
1794                                 for(k=0;k<8;k++)
1795                                         printf("%d%c",
1796                                         ((byte)&(1<<k))?1:0,
1797                                         (k==7)?'\n':'|');
1798                         }
1799                         if(atom[i].r.x==x) printf("the same!\n");
1800                         else printf("different!\n");
1801                 }
1802                 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
1803                         printf("FATAL: atom %d: y: %.20f (%.20f)\n",
1804                                i,atom[i].r.y,dim->y/2);
1805                 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
1806                         printf("FATAL: atom %d: z: %.20f (%.20f)\n",
1807                                i,atom[i].r.z,dim->z/2);
1808         }
1809
1810         return 0;
1811 }
1812
1813 /*
1814  * postprocessing functions
1815  */
1816
1817 int get_line(int fd,char *line,int max) {
1818
1819         int count,ret;
1820
1821         count=0;
1822
1823         while(1) {
1824                 if(count==max) return count;
1825                 ret=read(fd,line+count,1);
1826                 if(ret<=0) return ret;
1827                 if(line[count]=='\n') {
1828                         line[count]='\0';
1829                         return count+1;
1830                 }
1831                 count+=1;
1832         }
1833 }
1834
1835 int calc_fluctuations(double start,double end,t_moldyn *moldyn) {
1836
1837         int fd;
1838         int count,ret;
1839         double time,pot,kin,tot;
1840         double p_sum,k_sum,t_sum;
1841         double p2_sum,k2_sum,t2_sum;
1842         char buf[64];
1843         char file[128+7];
1844
1845         printf("[moldyn] calculating energy fluctuations [eV]:\n");
1846
1847         snprintf(file,128+7,"%s/energy",moldyn->vlsdir);
1848         fd=open(file,O_RDONLY);
1849         if(fd<0) {
1850                 perror("[moldyn] post proc energy open");
1851                 return fd;
1852         }
1853
1854         /* calc the averages of A and A^2 */
1855         p_sum=0.0;
1856         k_sum=0.0;
1857         t_sum=0.0;
1858         count=0;
1859         while(1) {
1860                 ret=get_line(fd,buf,63);
1861                 if(ret<=0) break;
1862                 if(buf[0]=='#') continue;
1863                 sscanf(buf,"%lf %lf %lf %lf",&time,&kin,&pot,&tot);
1864                 if(time<start) continue;
1865                 if(time>end) break;
1866                 p_sum+=pot;
1867                 k_sum+=kin;
1868                 t_sum+=tot;
1869                 p2_sum+=(pot*pot);
1870                 k2_sum+=(kin*kin);
1871                 t2_sum+=(tot*tot);
1872                 count+=1;
1873         }
1874
1875         /* averages */
1876         moldyn->k_m=k_sum/count;
1877         moldyn->p_m=p_sum/count;
1878         moldyn->t_m=t_sum/count;
1879
1880         /* rms */
1881         moldyn->dk2_m=k2_sum/count-moldyn->k_m*moldyn->k_m;
1882         moldyn->dp2_m=p2_sum/count-moldyn->p_m*moldyn->p_m;
1883         moldyn->dt2_m=t2_sum/count-moldyn->t_m*moldyn->t_m;
1884
1885         printf("  averages   : %f %f %f\n",moldyn->k_m,
1886                                         moldyn->p_m,
1887                                         moldyn->t_m);
1888         printf("  mean square: %f %f %f\n",moldyn->dk2_m,
1889                                            moldyn->dp2_m,
1890                                            moldyn->dt2_m);
1891
1892         close(fd);
1893
1894         return 0;
1895 }
1896
1897 int get_heat_capacity(t_moldyn *moldyn) {
1898
1899         double temp2,mass,ighc;
1900         int i;
1901
1902         /* (temperature average)^2 */
1903         temp2=2.0*moldyn->k_m*EV/(3.0*K_BOLTZMANN);
1904         printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",temp2);
1905         temp2*=temp2;
1906
1907         /* total mass */
1908         mass=0.0;
1909         for(i=0;i<moldyn->count;i++)
1910                 mass+=moldyn->atom[i].mass;
1911
1912         /* ideal gas contribution */
1913         ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1914         printf("  ideal gas contribution: %f\n",ighc/mass*KILOGRAM/JOULE);
1915
1916         moldyn->c_v_nvt=moldyn->dp2_m*moldyn->count*moldyn->count*EV/(K_BOLTZMANN*temp2)+ighc;
1917         moldyn->c_v_nvt/=mass;
1918         moldyn->c_v_nve=ighc/(1.0-(moldyn->dp2_m*moldyn->count*moldyn->count*EV/(ighc*K_BOLTZMANN*temp2)));
1919         moldyn->c_v_nve/=mass;
1920
1921         printf("  NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1922         printf("  NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1923
1924         return 0;
1925 }