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