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