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