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