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