]> hackdaworld.org Git - physik/posic.git/blob - moldyn.c
ed831aed93ec4861c71a383033af863ef1378074
[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) {
75         
76         moldyn->t=t;
77
78         return 0;
79 }
80
81 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
82
83         moldyn->dim.x=x;
84         moldyn->dim.y=y;
85         moldyn->dim.z=z;
86
87         if(visualize) {
88                 moldyn->vis.dim.x=x;
89                 moldyn->vis.dim.y=y;
90                 moldyn->vis.dim.z=z;
91         }
92
93         return 0;
94 }
95
96 int set_nn_dist(t_moldyn *moldyn,double dist) {
97
98         moldyn->nnd=dist;
99
100         return 0;
101 }
102
103 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
104
105         if(x)
106                 moldyn->status|=MOLDYN_STAT_PBX;
107
108         if(y)
109                 moldyn->status|=MOLDYN_STAT_PBY;
110
111         if(z)
112                 moldyn->status|=MOLDYN_STAT_PBZ;
113
114         return 0;
115 }
116
117 int set_potential1b(t_moldyn *moldyn,pf_func1b func,void *params) {
118
119         moldyn->func1b=func;
120         moldyn->pot1b_params=params;
121
122         return 0;
123 }
124
125 int set_potential2b(t_moldyn *moldyn,pf_func2b func,void *params) {
126
127         moldyn->func2b=func;
128         moldyn->pot2b_params=params;
129
130         return 0;
131 }
132
133 int set_potential3b(t_moldyn *moldyn,pf_func3b func,void *params) {
134
135         moldyn->func3b=func;
136         moldyn->pot3b_params=params;
137
138         return 0;
139 }
140
141 int moldyn_set_log(t_moldyn *moldyn,u8 type,char *fb,int timer) {
142
143         switch(type) {
144                 case LOG_TOTAL_ENERGY:
145                         moldyn->ewrite=timer;
146                         moldyn->efd=open(fb,O_WRONLY|O_CREAT|O_TRUNC);
147                         if(moldyn->efd<0) {
148                                 perror("[moldyn] efd open");
149                                 return moldyn->efd;
150                         }
151                         dprintf(moldyn->efd,"# total energy log file\n");
152                         break;
153                 case LOG_TOTAL_MOMENTUM:
154                         moldyn->mwrite=timer;
155                         moldyn->mfd=open(fb,O_WRONLY|O_CREAT|O_TRUNC);
156                         if(moldyn->mfd<0) {
157                                 perror("[moldyn] mfd open");
158                                 return moldyn->mfd;
159                         }
160                         dprintf(moldyn->efd,"# total momentum log file\n");
161                         break;
162                 case SAVE_STEP:
163                         moldyn->swrite=timer;
164                         strncpy(moldyn->sfb,fb,63);
165                         break;
166                 case VISUAL_STEP:
167                         moldyn->vwrite=timer;
168                         strncpy(moldyn->vfb,fb,63);
169                         visual_init(&(moldyn->vis),fb);
170                         break;
171                 default:
172                         printf("unknown log mechanism: %02x\n",type);
173                         return -1;
174         }
175
176         return 0;
177 }
178
179 int moldyn_log_shutdown(t_moldyn *moldyn) {
180
181         printf("[moldyn] log shutdown\n");
182         if(moldyn->efd) close(moldyn->efd);
183         if(moldyn->mfd) close(moldyn->mfd);
184         if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
185
186         return 0;
187 }
188
189 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
190                    u8 attr,u8 bnum,int a,int b,int c) {
191
192         int count;
193         int ret;
194         t_3dvec origin;
195
196         count=a*b*c;
197
198         if(type==FCC) count*=4;
199
200         if(type==DIAMOND) count*=8;
201
202         moldyn->atom=malloc(count*sizeof(t_atom));
203         if(moldyn->atom==NULL) {
204                 perror("malloc (atoms)");
205                 return -1;
206         }
207
208         v3_zero(&origin);
209
210         switch(type) {
211                 case FCC:
212                         ret=fcc_init(a,b,c,lc,moldyn->atom,&origin);
213                         break;
214                 case DIAMOND:
215                         ret=diamond_init(a,b,c,lc,moldyn->atom,&origin);
216                         break;
217                 default:
218                         printf("unknown lattice type (%02x)\n",type);
219                         return -1;
220         }
221
222         /* debug */
223         if(ret!=count) {
224                 printf("ok, there is something wrong ...\n");
225                 printf("calculated -> %d atoms\n",count);
226                 printf("created -> %d atoms\n",ret);
227                 return -1;
228         }
229
230         moldyn->count=count;
231         printf("[moldyn] created lattice with %d atoms\n",count);
232
233         while(count) {
234                 count-=1;
235                 moldyn->atom[count].element=element;
236                 moldyn->atom[count].mass=mass;
237                 moldyn->atom[count].attr=attr;
238                 moldyn->atom[count].bnum=bnum;
239                 check_per_bound(moldyn,&(moldyn->atom[count].r));
240         }
241
242
243         return ret;
244 }
245
246 int add_atom(t_moldyn *moldyn,int element,double mass,u8 bnum,u8 attr,
247              t_3dvec *r,t_3dvec *v) {
248
249         t_atom *atom;
250         void *ptr;
251         int count;
252         
253         atom=moldyn->atom;
254         count=++(moldyn->count);
255
256         ptr=realloc(atom,count*sizeof(t_atom));
257         if(!ptr) {
258                 perror("[moldyn] realloc (add atom)");
259                 return -1;
260         }
261         moldyn->atom=ptr;
262
263         atom=moldyn->atom;
264         atom[count-1].r=*r;
265         atom[count-1].v=*v;
266         atom[count-1].element=element;
267         atom[count-1].mass=mass;
268         atom[count-1].bnum=bnum;
269         atom[count-1].attr=attr;
270
271         return 0;
272 }
273
274 int destroy_atoms(t_moldyn *moldyn) {
275
276         if(moldyn->atom) free(moldyn->atom);
277
278         return 0;
279 }
280
281 int thermal_init(t_moldyn *moldyn) {
282
283         /*
284          * - gaussian distribution of velocities
285          * - zero total momentum
286          * - velocity scaling (E = 3/2 N k T), E: kinetic energy
287          */
288
289         int i;
290         double v,sigma;
291         t_3dvec p_total,delta;
292         t_atom *atom;
293         t_random *random;
294
295         atom=moldyn->atom;
296         random=&(moldyn->random);
297
298         /* gaussian distribution of velocities */
299         v3_zero(&p_total);
300         for(i=0;i<moldyn->count;i++) {
301                 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t/atom[i].mass);
302                 /* x direction */
303                 v=sigma*rand_get_gauss(random);
304                 atom[i].v.x=v;
305                 p_total.x+=atom[i].mass*v;
306                 /* y direction */
307                 v=sigma*rand_get_gauss(random);
308                 atom[i].v.y=v;
309                 p_total.y+=atom[i].mass*v;
310                 /* z direction */
311                 v=sigma*rand_get_gauss(random);
312                 atom[i].v.z=v;
313                 p_total.z+=atom[i].mass*v;
314         }
315
316         /* zero total momentum */
317         v3_scale(&p_total,&p_total,1.0/moldyn->count);
318         for(i=0;i<moldyn->count;i++) {
319                 v3_scale(&delta,&p_total,1.0/atom[i].mass);
320                 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
321         }
322
323         /* velocity scaling */
324         scale_velocity(moldyn,VSCALE_INIT_EQUI);
325
326         return 0;
327 }
328
329 int scale_velocity(t_moldyn *moldyn,u8 type) {
330
331         int i;
332         double e,scale;
333         t_atom *atom;
334
335         atom=moldyn->atom;
336
337         /*
338          * - velocity scaling (E = 3/2 N k T), E: kinetic energy
339          */
340
341         e=0.0;
342         for(i=0;i<moldyn->count;i++)
343                 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
344         scale=(1.5*moldyn->count*K_BOLTZMANN*moldyn->t)/e;
345         if(type&VSCALE_INIT_EQUI) scale*=2.0; /* equipartition theorem */
346         scale=sqrt(scale);
347         for(i=0;i<moldyn->count;i++)
348                 v3_scale(&(atom[i].v),&(atom[i].v),scale);
349
350         return 0;
351 }
352
353 double get_e_kin(t_moldyn *moldyn) {
354
355         int i;
356         t_atom *atom;
357
358         atom=moldyn->atom;
359         moldyn->ekin=0.0;
360
361         for(i=0;i<moldyn->count;i++)
362                 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
363
364         return moldyn->ekin;
365 }
366
367 double get_e_pot(t_moldyn *moldyn) {
368
369         return moldyn->energy;
370 }
371
372 double update_e_kin(t_moldyn *moldyn) {
373
374         return(get_e_kin(moldyn));
375 }
376
377 double get_total_energy(t_moldyn *moldyn) {
378
379         return(moldyn->ekin+moldyn->energy);
380 }
381
382 t_3dvec get_total_p(t_moldyn *moldyn) {
383
384         t_3dvec p,p_total;
385         int i;
386         t_atom *atom;
387
388         atom=moldyn->atom;
389
390         v3_zero(&p_total);
391         for(i=0;i<moldyn->count;i++) {
392                 v3_scale(&p,&(atom[i].v),atom[i].mass);
393                 v3_add(&p_total,&p_total,&p);
394         }
395
396         return p_total;
397 }
398
399 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
400
401         double tau;
402
403         /* nn_dist is the nearest neighbour distance */
404
405         if(moldyn->t==5.0) {
406                 printf("[moldyn] i do not estimate timesteps below %f K!\n",
407                        MOLDYN_CRITICAL_EST_TEMP);
408                 return 23.42;
409         }
410
411         tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
412
413         return tau;     
414 }
415
416 /*
417  * numerical tricks
418  */
419
420 /* linked list / cell method */
421
422 int link_cell_init(t_moldyn *moldyn) {
423
424         t_linkcell *lc;
425         int i;
426         int fd;
427
428         fd=open("/dev/null",O_WRONLY);
429
430         lc=&(moldyn->lc);
431
432         /* partitioning the md cell */
433         lc->nx=moldyn->dim.x/moldyn->cutoff;
434         lc->x=moldyn->dim.x/lc->nx;
435         lc->ny=moldyn->dim.y/moldyn->cutoff;
436         lc->y=moldyn->dim.y/lc->ny;
437         lc->nz=moldyn->dim.z/moldyn->cutoff;
438         lc->z=moldyn->dim.z/lc->nz;
439
440         lc->cells=lc->nx*lc->ny*lc->nz;
441         lc->subcell=malloc(lc->cells*sizeof(t_list));
442
443         printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
444
445         for(i=0;i<lc->cells;i++)
446                 //list_init(&(lc->subcell[i]),1);
447                 list_init(&(lc->subcell[i]),fd);
448
449         link_cell_update(moldyn);
450         
451         return 0;
452 }
453
454 int link_cell_update(t_moldyn *moldyn) {
455
456         int count,i,j,k;
457         int nx,ny,nz;
458         t_atom *atom;
459         t_linkcell *lc;
460
461         atom=moldyn->atom;
462         lc=&(moldyn->lc);
463
464         nx=lc->nx;
465         ny=lc->ny;
466         nz=lc->nz;
467
468         for(i=0;i<lc->cells;i++)
469                 list_destroy(&(moldyn->lc.subcell[i]));
470         
471         for(count=0;count<moldyn->count;count++) {
472                 i=(atom[count].r.x+(moldyn->dim.x/2))/lc->x;
473                 j=(atom[count].r.y+(moldyn->dim.y/2))/lc->y;
474                 k=(atom[count].r.z+(moldyn->dim.z/2))/lc->z;
475                 list_add_immediate_ptr(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
476                                        &(atom[count]));
477         }
478
479         return 0;
480 }
481
482 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
483
484         t_linkcell *lc;
485         int a;
486         int count1,count2;
487         int ci,cj,ck;
488         int nx,ny,nz;
489         int x,y,z;
490         u8 bx,by,bz;
491
492         lc=&(moldyn->lc);
493         nx=lc->nx;
494         ny=lc->ny;
495         nz=lc->nz;
496         count1=1;
497         count2=27;
498         a=nx*ny;
499
500
501         cell[0]=lc->subcell[i+j*nx+k*a];
502         for(ci=-1;ci<=1;ci++) {
503                 bx=0;
504                 x=i+ci;
505                 if((x<0)||(x>=nx)) {
506                         x=(x+nx)%nx;
507                         bx=1;
508                 }
509                 for(cj=-1;cj<=1;cj++) {
510                         by=0;
511                         y=j+cj;
512                         if((y<0)||(y>=ny)) {
513                                 y=(y+ny)%ny;
514                                 by=1;
515                         }
516                         for(ck=-1;ck<=1;ck++) {
517                                 bz=0;
518                                 z=k+ck;
519                                 if((z<0)||(z>=nz)) {
520                                         z=(z+nz)%nz;
521                                         bz=1;
522                                 }
523                                 if(!(ci|cj|ck)) continue;
524                                 if(bx|by|bz) {
525                                         cell[--count2]=lc->subcell[x+y*nx+z*a];
526                                 }
527                                 else {
528                                         cell[count1++]=lc->subcell[x+y*nx+z*a];
529                                 }
530                         }
531                 }
532         }
533
534         lc->dnlc=count1;
535         lc->countn=27;
536
537         return count2;
538 }
539
540 int link_cell_shutdown(t_moldyn *moldyn) {
541
542         int i;
543         t_linkcell *lc;
544
545         lc=&(moldyn->lc);
546
547         for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
548                 list_shutdown(&(moldyn->lc.subcell[i]));
549
550         return 0;
551 }
552
553 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
554
555         int count;
556         void *ptr;
557         t_moldyn_schedule *schedule;
558
559         schedule=&(moldyn->schedule);
560         count=++(schedule->content_count);
561
562         ptr=realloc(moldyn->schedule.runs,count*sizeof(int));
563         if(!ptr) {
564                 perror("[moldyn] realloc (runs)");
565                 return -1;
566         }
567         moldyn->schedule.runs=ptr;
568         moldyn->schedule.runs[count-1]=runs;
569
570         ptr=realloc(schedule->tau,count*sizeof(double));
571         if(!ptr) {
572                 perror("[moldyn] realloc (tau)");
573                 return -1;
574         }
575         moldyn->schedule.tau=ptr;
576         moldyn->schedule.tau[count-1]=tau;
577
578         return 0;
579 }
580
581 int moldyn_set_schedule_hook(t_moldyn *moldyn,void *hook,void *hook_params) {
582
583         moldyn->schedule.hook=hook;
584         moldyn->schedule.hook_params=hook_params;
585         
586         return 0;
587 }
588
589 /*
590  *
591  * 'integration of newtons equation' - algorithms
592  *
593  */
594
595 /* start the integration */
596
597 int moldyn_integrate(t_moldyn *moldyn) {
598
599         int i,sched;
600         unsigned int e,m,s,v;
601         t_3dvec p;
602         t_moldyn_schedule *schedule;
603         t_atom *atom;
604         int fd;
605         char fb[128];
606         double ds;
607
608         schedule=&(moldyn->schedule);
609         atom=moldyn->atom;
610
611         /* initialize linked cell method */
612         link_cell_init(moldyn);
613
614         /* logging & visualization */
615         e=moldyn->ewrite;
616         m=moldyn->mwrite;
617         s=moldyn->swrite;
618         v=moldyn->vwrite;
619
620         /* sqaure of some variables */
621         moldyn->tau_square=moldyn->tau*moldyn->tau;
622         moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
623         /* calculate initial forces */
624         potential_force_calc(moldyn);
625
626         /* do some checks before we actually start calculating bullshit */
627         if(moldyn->cutoff>0.5*moldyn->dim.x)
628                 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
629         if(moldyn->cutoff>0.5*moldyn->dim.y)
630                 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
631         if(moldyn->cutoff>0.5*moldyn->dim.z)
632                 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
633         ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
634         if(ds>0.05*moldyn->nnd)
635                 printf("[moldyn] warning: forces too high / tau too small!\n");
636
637         /* zero absolute time */
638         moldyn->time=0.0;
639
640         for(sched=0;sched<moldyn->schedule.content_count;sched++) {
641
642                 /* setting amount of runs and finite time step size */
643                 moldyn->tau=schedule->tau[sched];
644                 moldyn->tau_square=moldyn->tau*moldyn->tau;
645                 moldyn->time_steps=schedule->runs[sched];
646
647         /* integration according to schedule */
648
649         for(i=0;i<moldyn->time_steps;i++) {
650
651                 /* integration step */
652                 moldyn->integrate(moldyn);
653
654                 /* increase absolute time */
655                 moldyn->time+=moldyn->tau;
656
657                 /* check for log & visualization */
658                 if(e) {
659                         if(!(i%e))
660                                 dprintf(moldyn->efd,
661                                         "%.15f %.45f %.45f %.45f\n",
662                                         moldyn->time,update_e_kin(moldyn),
663                                         moldyn->energy,
664                                         get_total_energy(moldyn));
665                 }
666                 if(m) {
667                         if(!(i%m)) {
668                                 p=get_total_p(moldyn);
669                                 dprintf(moldyn->mfd,
670                                         "%.15f %.45f\n",moldyn->time,
671                                         v3_norm(&p));
672                         }
673                 }
674                 if(s) {
675                         if(!(i%s)) {
676                                 snprintf(fb,128,"%s-%f-%.15f.save",moldyn->sfb,
677                                          moldyn->t,i*moldyn->tau);
678                                 fd=open(fb,O_WRONLY|O_TRUNC|O_CREAT);
679                                 if(fd<0) perror("[moldyn] save fd open");
680                                 else {
681                                         write(fd,moldyn,sizeof(t_moldyn));
682                                         write(fd,moldyn->atom,
683                                               moldyn->count*sizeof(t_atom));
684                                 }
685                                 close(fd);
686                         }       
687                 }
688                 if(v) {
689                         if(!(i%v)) {
690                                 visual_atoms(&(moldyn->vis),moldyn->time,
691                                              moldyn->atom,moldyn->count);
692                                 printf("\rsched: %d, steps: %d",sched,i);
693                                 fflush(stdout);
694                         }
695                 }
696
697         }
698
699                 /* check for hooks */
700                 if(schedule->hook)
701                         schedule->hook(moldyn,schedule->hook_params);
702
703         }
704
705         return 0;
706 }
707
708 /* velocity verlet */
709
710 int velocity_verlet(t_moldyn *moldyn) {
711
712         int i,count;
713         double tau,tau_square;
714         t_3dvec delta;
715         t_atom *atom;
716
717         atom=moldyn->atom;
718         count=moldyn->count;
719         tau=moldyn->tau;
720         tau_square=moldyn->tau_square;
721
722         for(i=0;i<count;i++) {
723                 /* new positions */
724                 v3_scale(&delta,&(atom[i].v),tau);
725                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
726                 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
727                 v3_add(&(atom[i].r),&(atom[i].r),&delta);
728                 check_per_bound(moldyn,&(atom[i].r));
729
730                 /* velocities */
731                 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
732                 v3_add(&(atom[i].v),&(atom[i].v),&delta);
733         }
734
735         /* neighbour list update */
736         link_cell_update(moldyn);
737
738         /* forces depending on chosen potential */
739         potential_force_calc(moldyn);
740         //moldyn->potential_force_function(moldyn);
741
742         for(i=0;i<count;i++) {
743                 /* again velocities */
744                 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
745                 v3_add(&(atom[i].v),&(atom[i].v),&delta);
746         }
747
748         return 0;
749 }
750
751
752 /*
753  *
754  * potentials & corresponding forces
755  * 
756  */
757
758 /* generic potential and force calculation */
759
760 int potential_force_calc(t_moldyn *moldyn) {
761
762         int i,j,k,count;
763         t_atom *atom,*btom,*ktom;
764         t_linkcell *lc;
765         t_list neighbour[27];
766         t_list *this,*thisk,*neighbourk;
767         u8 bc,bck;
768         int countn,dnlc;
769
770         count=moldyn->count;
771         atom=moldyn->atom;
772         lc=&(moldyn->lc);
773
774         /* reset energy */
775         moldyn->energy=0.0;
776
777         for(i=0;i<count;i++) {
778
779                 /* reset force */
780                 v3_zero(&(atom[i].f));
781
782                 /* single particle potential/force */
783                 if(atom[i].attr&ATOM_ATTR_1BP)
784                         moldyn->func1b(moldyn,&(atom[i]));
785
786                 /* 2 body pair potential/force */
787                 if(atom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)) {
788         
789                         link_cell_neighbour_index(moldyn,
790                                 (atom[i].r.x+moldyn->dim.x/2)/lc->x,
791                                 (atom[i].r.y+moldyn->dim.y/2)/lc->y,
792                                 (atom[i].r.z+moldyn->dim.z/2)/lc->z,
793                                 neighbour);
794
795                         countn=lc->countn;
796                         dnlc=lc->dnlc;
797
798                         for(j=0;j<countn;j++) {
799
800                                 this=&(neighbour[j]);
801                                 list_reset(this);
802
803                                 if(this->start==NULL)
804                                         continue;
805
806                                 bc=(j<dnlc)?0:1;
807
808                                 do {
809                                         btom=this->current->data;
810
811                                         if(btom==&(atom[i]))
812                                                 continue;
813
814                                         if((btom->attr&ATOM_ATTR_2BP)&
815                                            (atom[i].attr&ATOM_ATTR_2BP))
816                                                 moldyn->func2b(moldyn,
817                                                                &(atom[i]),
818                                                                btom,
819                                                                bc);
820
821                                         /* 3 body potential/force */
822
823                                         if(!(atom[i].attr&ATOM_ATTR_3BP)||
824                                            !(btom->attr&ATOM_ATTR_3BP))
825                                                 continue;
826
827 printf("DEBUG: problem exists here ...\n");
828                                         link_cell_neighbour_index(moldyn,
829                                            (btom->r.x+moldyn->dim.x/2)/lc->x,
830                                            (btom->r.y+moldyn->dim.y/2)/lc->y,
831                                            (btom->r.z+moldyn->dim.z/2)/lc->z,
832                                            neighbourk);
833 printf("DEBUG: as you won't see that!\n");
834
835                                         for(k=0;k<lc->countn;k++) {
836
837                                                 thisk=&(neighbourk[k]);
838                                                 list_reset(thisk);
839                                         
840                                                 if(thisk->start==NULL)
841                                                         continue;
842
843                                                 bck=(k<lc->dnlc)?0:1;
844
845                                                 do {
846
847                         ktom=thisk->current->data;
848
849                         if(!(ktom->attr&ATOM_ATTR_3BP))
850                                 continue;
851
852                         if(ktom==btom)
853                                 continue;
854
855                         if(ktom==&(atom[i]))
856                                 continue;
857
858                         moldyn->func3b(moldyn,&(atom[i]),btom,ktom,bck);
859
860                                                 } while(list_next(thisk)!=\
861                                                         L_NO_NEXT_ELEMENT);
862
863                                         }
864                                         
865                                 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
866                         }
867                 }
868         }
869
870         return 0;
871 }
872
873 /*
874  * periodic boundayr checking
875  */
876
877 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
878         
879         double x,y,z;
880         t_3dvec *dim;
881
882         dim=&(moldyn->dim);
883
884         x=0.5*dim->x;
885         y=0.5*dim->y;
886         z=0.5*dim->z;
887
888         if(moldyn->status&MOLDYN_STAT_PBX) {
889                 if(a->x>=x) a->x-=dim->x;
890                 else if(-a->x>x) a->x+=dim->x;
891         }
892         if(moldyn->status&MOLDYN_STAT_PBY) {
893                 if(a->y>=y) a->y-=dim->y;
894                 else if(-a->y>y) a->y+=dim->y;
895         }
896         if(moldyn->status&MOLDYN_STAT_PBZ) {
897                 if(a->z>=z) a->z-=dim->z;
898                 else if(-a->z>z) a->z+=dim->z;
899         }
900
901         return 0;
902 }
903         
904
905 /*
906  * example potentials
907  */
908
909 /* harmonic oscillator potential and force */
910
911 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
912
913         t_ho_params *params;
914         t_3dvec force,distance;
915         double d;
916         double sc,equi_dist;
917
918         params=moldyn->pot2b_params;
919         sc=params->spring_constant;
920         equi_dist=params->equilibrium_distance;
921
922         v3_sub(&distance,&(ai->r),&(aj->r));
923         
924         if(bc) check_per_bound(moldyn,&distance);
925         d=v3_norm(&distance);
926         if(d<=moldyn->cutoff) {
927                 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
928                 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
929                 v3_scale(&force,&distance,-sc*(1.0-(equi_dist/d)));
930                 v3_add(&(ai->f),&(ai->f),&force);
931         }
932
933         return 0;
934 }
935
936 /* lennard jones potential & force for one sort of atoms */
937  
938 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
939
940         t_lj_params *params;
941         t_3dvec force,distance;
942         double d,h1,h2;
943         double eps,sig6,sig12;
944
945         params=moldyn->pot2b_params;
946         eps=params->epsilon4;
947         sig6=params->sigma6;
948         sig12=params->sigma12;
949
950         v3_sub(&distance,&(ai->r),&(aj->r));
951         if(bc) check_per_bound(moldyn,&distance);
952         d=v3_absolute_square(&distance);        /* 1/r^2 */
953         if(d<=moldyn->cutoff_square) {
954                 d=1.0/d;                        /* 1/r^2 */
955                 h2=d*d;                         /* 1/r^4 */
956                 h2*=d;                          /* 1/r^6 */
957                 h1=h2*h2;                       /* 1/r^12 */
958                 /* energy is eps*..., but we will add this twice ... */
959                 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
960                 h2*=d;                          /* 1/r^8 */
961                 h1*=d;                          /* 1/r^14 */
962                 h2*=6*sig6;
963                 h1*=12*sig12;
964                 d=+h1-h2;
965                 d*=eps;
966                 v3_scale(&force,&distance,d);
967                 v3_add(&(ai->f),&(ai->f),&force);
968         }
969
970         return 0;
971 }
972
973 /*
974  * tersoff potential & force for 2 sorts of atoms
975  */
976
977 /* create mixed terms from parameters and set them */
978 int tersoff_mult_complete_params(t_tersoff_mult_params *p) {
979
980         printf("[moldyn] tersoff parameter completion\n");
981         p->Smixed=sqrt(p->S[0]*p->S[1]);
982         p->Rmixed=sqrt(p->R[0]*p->R[1]);
983         p->Amixed=sqrt(p->A[0]*p->A[1]);
984         p->Bmixed=sqrt(p->B[0]*p->B[1]);
985         p->lambda_m=0.5*(p->lambda[0]+p->lambda[1]);
986         p->mu_m=0.5*(p->mu[0]+p->mu[1]);
987
988         return 0;
989 }
990
991 /* tersoff 1 body part */
992 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
993
994         int num;
995         t_tersoff_mult_params *params;
996         t_tersoff_exchange *exchange;
997         
998         num=ai->bnum;
999         params=moldyn->pot1b_params;
1000         exchange=&(params->exchange);
1001
1002         /*
1003          * simple: point constant parameters only depending on atom i to
1004          *         their right values
1005          */
1006
1007         exchange->beta=&(params->beta[num]);
1008         exchange->n=&(params->n[num]);
1009         exchange->c=&(params->c[num]);
1010         exchange->d=&(params->d[num]);
1011         exchange->h=&(params->h[num]);
1012
1013         exchange->betan=pow(*(exchange->beta),*(exchange->n));
1014         exchange->c2=params->c[num]*params->c[num];
1015         exchange->d2=params->d[num]*params->d[num];
1016         exchange->c2d2=exchange->c2/exchange->d2;
1017
1018         return 0;
1019 }
1020         
1021 /* tersoff 2 body part */
1022 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1023
1024         t_tersoff_mult_params *params;
1025         t_tersoff_exchange *exchange;
1026         t_3dvec dist_ij,force;
1027         double d_ij;
1028         double A,B,R,S,lambda,mu;
1029         double f_r,df_r;
1030         double f_c,df_c;
1031         int num;
1032         double s_r;
1033         double arg;
1034         double scale;
1035
1036         params=moldyn->pot2b_params;
1037         num=ai->bnum;
1038         exchange=&(params->exchange);
1039
1040         exchange->run3bp=0;
1041         
1042         /*
1043          * we need: f_c, df_c, f_r, df_r
1044          *
1045          * therefore we need: R, S, A, lambda
1046          */
1047
1048         v3_sub(&dist_ij,&(ai->r),&(aj->r));
1049
1050         if(bc) check_per_bound(moldyn,&dist_ij);
1051
1052         /* save for use in 3bp */ /* REALLY ?!?!?! */
1053         exchange->dist_ij=dist_ij;
1054
1055         /* constants */
1056         if(num==aj->bnum) {
1057                 S=params->S[num];
1058                 R=params->R[num];
1059                 A=params->A[num];
1060                 lambda=params->lambda[num];
1061                 /* more constants depending of atoms i and j, needed in 3bp */
1062                 params->exchange.B=&(params->B[num]);
1063                 params->exchange.mu=&(params->mu[num]);
1064                 mu=params->mu[num];
1065                 params->exchange.chi=1.0;
1066         }
1067         else {
1068                 S=params->Smixed;
1069                 R=params->Rmixed;
1070                 A=params->Amixed;
1071                 lambda=params->lambda_m;
1072                 /* more constants depending of atoms i and j, needed in 3bp */
1073                 params->exchange.B=&(params->Bmixed);
1074                 params->exchange.mu=&(params->mu_m);
1075                 mu=params->mu_m;
1076                 params->exchange.chi=params->chi;
1077         }
1078
1079         d_ij=v3_norm(&dist_ij);
1080
1081         /* save for use in 3bp */
1082         exchange->d_ij=d_ij;
1083
1084         if(d_ij>S)
1085                 return 0;
1086
1087         f_r=A*exp(-lambda*d_ij);
1088         df_r=-lambda*f_r/d_ij;
1089
1090         /* f_a, df_a calc + save for 3bp use */
1091         exchange->f_a=-B*exp(-mu*d_ij);
1092         exchange->df_a=-mu*exchange->f_a/d_ij;
1093
1094         if(d_ij<R) {
1095                 /* f_c = 1, df_c = 0 */
1096                 f_c=1.0;
1097                 df_c=0.0;
1098                 v3_scale(&force,&dist_ij,df_r);
1099         }
1100         else {
1101                 s_r=S-R;
1102                 arg=M_PI*(d_ij-R)/s_r;
1103                 f_c=0.5+0.5*cos(arg);
1104                 df_c=-0.5*sin(arg)*(M_PI/(s_r*d_ij));
1105                 scale=df_c*f_r+df_r*f_c;
1106                 v3_scale(&force,&dist_ij,scale);
1107         }
1108
1109         /* add forces */
1110         v3_add(&(ai->f),&(ai->f),&force);
1111         /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1112         moldyn->energy+=(0.25*f_r*f_c);
1113
1114         /* save for use in 3bp */
1115         exchange->f_c=f_c;
1116         exchange->df_c=df_c;
1117
1118         /* enable the run of 3bp function */
1119         exchange->run3bp=1;
1120
1121         return 0;
1122 }
1123
1124 /* tersoff 3 body part */
1125
1126 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1127
1128         t_tersoff_mult_params *params;
1129         t_tersoff_exchange *exchange;
1130         t_3dvec dist_ij,dist_ik,dist_jk;
1131         t_3dvec temp,force;
1132         double R,S,s_r;
1133         double d_ij,d_ij2,d_ik,d_jk;
1134         double f_c,df_c,b_ij,f_a,df_a;
1135         double f_c_ik,df_c_ik,arg;
1136         double scale;
1137         double chi;
1138         double n,c,d,h,beta,betan;
1139         double c2,d2,c2d2;
1140         double numer,denom;
1141         double theta,cos_theta,sin_theta;
1142         double d_theta,d_theta1,d_theta2;
1143         double h_cos,h_cos2,d2_h_cos2;
1144         double frac1,bracket1,bracket2,bracket2_n_1,bracket2_n;
1145         double bracket3,bracket3_pow_1,bracket3_pow;
1146         int num;
1147
1148         params=moldyn->pot3b_params;
1149         num=ai->bnum;
1150         exchange=&(params->exchange);
1151
1152         if(!(exchange->run3bp))
1153                 return 0;
1154
1155         /*
1156          * we need: f_c, d_fc, b_ij, db_ij, f_a, df_a
1157          *
1158          * we got f_c, df_c, f_a, df_a from 2bp calculation
1159          */
1160
1161         d_ij=exchange->d_ij;
1162         d_ij2=exchange->d_ij2;
1163
1164         f_a=params->exchange.f_a;
1165         df_a=params->exchange.df_a;
1166         
1167         /* d_ij is <= S, as we didn't return so far! */
1168
1169         /*
1170          * calc of b_ij (scalar) and db_ij (vector)
1171          *
1172          * - for b_ij: chi, beta, f_c_ik, w(=1), c, d, h, n, cos_theta
1173          *
1174          * - for db_ij: d_theta, sin_theta, cos_theta, f_c_ik, df_c_ik,
1175          *              w_ik,
1176          *
1177          */
1178
1179         
1180         v3_sub(&dist_ik,&(ai->r),&(ak->r));
1181         if(bc) check_per_bound(moldyn,&dist_ik);
1182         d_ik=v3_norm(&dist_ik);
1183
1184         /* constants for f_c_ik calc */
1185         if(num==ak->bnum) {
1186                 R=params->R[num];
1187                 S=params->S[num];
1188         }
1189         else {
1190                 R=params->Rmixed;
1191                 S=params->Smixed;
1192         }
1193
1194         /* calc of f_c_ik */
1195         if(d_ik>S)
1196                 return 0;
1197
1198         if(d_ik<R) {
1199                 /* f_c_ik = 1, df_c_ik = 0 */
1200                 f_c_ik=1.0;
1201                 df_c_ik=0.0;
1202         }
1203         else {
1204                 s_r=S-R;
1205                 arg=M_PI*(d_ik-R)/s_r;
1206                 f_c_ik=0.5+0.5*cos(arg);
1207                 df_c_ik=-0.5*sin(arg)*(M_PI/(s_r*d_ik));
1208         }
1209         
1210         v3_sub(&dist_jk,&(aj->r),&(ak->r));
1211         if(bc) check_per_bound(moldyn,&dist_jk);
1212         d_jk=v3_norm(&dist_jk);
1213
1214         beta=*(exchange->beta);
1215         betan=exchange->betan;
1216         n=*(exchange->n);
1217         c=*(exchange->c);
1218         d=*(exchange->d);
1219         h=*(exchange->h);
1220         c2=exchange->c2;
1221         d2=exchange->d2;
1222         c2d2=exchange->c2d2;
1223
1224         numer=d_ij2+d_ik*d_ik-d_jk*d_jk;
1225         denom=2*d_ij*d_ik;
1226         cos_theta=numer/denom;
1227         sin_theta=sqrt(1.0-(cos_theta*cos_theta));
1228         theta=acos(cos_theta);
1229         d_theta=(-1.0/sqrt(1.0-cos_theta*cos_theta))/(denom*denom);
1230         d_theta1=2*denom-numer*2*d_ik/d_ij;
1231         d_theta2=2*denom-numer*2*d_ij/d_ik;
1232         d_theta1*=d_theta;
1233         d_theta2*=d_theta;
1234
1235         h_cos=(h-cos_theta);
1236         h_cos2=h_cos*h_cos;
1237         d2_h_cos2=d2-h_cos2;
1238
1239         /* some usefull expressions */
1240         frac1=c2/(d2-h_cos2);
1241         bracket1=1+c2d2-frac1;
1242         bracket2=f_c_ik*bracket1;
1243         bracket2_n_1=pow(bracket2,n-1.0);
1244         bracket2_n=bracket2_n_1*bracket2;
1245         bracket3=1+betan*bracket2_n;
1246         bracket3_pow_1=pow(bracket3,(-1.0/(2.0*n))-1.0);
1247         bracket3_pow=bracket3_pow_1*bracket3;
1248
1249         /* now go on with calc of b_ij and derivation of b_ij */
1250         b_ij=chi*bracket3_pow;
1251
1252         /* derivation of theta */
1253         v3_scale(&force,&dist_ij,d_theta1);
1254         v3_scale(&temp,&dist_ik,d_theta2);
1255         v3_add(&force,&force,&temp);
1256
1257         /* part 1 of derivation of b_ij */
1258         v3_scale(&force,&force,sin_theta*2*h_cos*f_c_ik*frac1);
1259
1260         /* part 2 of derivation of b_ij */
1261         v3_scale(&temp,&dist_ik,df_c_ik*bracket1);
1262
1263         /* sum up and scale ... */
1264         v3_add(&temp,&temp,&force);
1265         scale=bracket2_n_1*n*betan*(1+betan*bracket3_pow_1)*chi*(1.0/(2.0*n));
1266         v3_scale(&temp,&temp,scale);
1267
1268         /* now construct an energy and a force out of that */
1269         v3_scale(&temp,&temp,f_a);
1270         v3_scale(&force,&dist_ij,df_a*b_ij);
1271         v3_add(&temp,&temp,&force);
1272         v3_scale(&temp,&temp,f_c);
1273         v3_scale(&force,&dist_ij,df_c*b_ij*f_a);
1274         v3_add(&force,&force,&temp);
1275
1276         /* add forces */
1277         v3_add(&(ai->f),&(ai->f),&force);
1278         /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1279         moldyn->energy+=(0.25*f_a*b_ij*f_c);
1280                                 
1281         return 0;
1282 }
1283