2 * moldyn.c - molecular dynamics library main file
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
12 #include <sys/types.h>
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"
26 int moldyn_usage(char **argv) {
28 printf("\n%s usage:\n\n",argv[0]);
29 printf("--- general options ---\n");
30 printf("-E <steps> <file> (log total energy)\n");
31 printf("-M <steps> <file> (log total momentum)\n");
32 printf("-D <steps> <file> (dump total information)\n");
33 printf("-S <steps> <filebase> (single save file)\n");
34 printf("-V <steps> <filebase> (rasmol file)\n");
35 printf("--- physics options ---\n");
36 printf("-T <temperature> [K] (%f)\n",MOLDYN_TEMP);
37 printf("-t <timestep tau> [s] (%.15f)\n",MOLDYN_TAU);
38 printf("-C <cutoff radius> [m] (%.15f)\n",MOLDYN_CUTOFF);
39 printf("-R <runs> (%d)\n",MOLDYN_RUNS);
40 printf(" -- integration algo --\n");
41 printf(" -I <number> (%d)\n",MOLDYN_INTEGRATE_DEFAULT);
42 printf(" 0: velocity verlet\n");
43 printf(" -- potential --\n");
44 printf(" -P <number> <param1 param2 ...>\n");
45 printf(" 0: harmonic oscillator\n");
46 printf(" param1: spring constant\n");
47 printf(" param2: equilibrium distance\n");
48 printf(" 1: lennard jones\n");
49 printf(" param1: epsilon\n");
50 printf(" param2: sigma\n");
56 int moldyn_parse_argv(t_moldyn *moldyn,int argc,char **argv) {
64 memset(moldyn,0,sizeof(t_moldyn));
67 moldyn->t=MOLDYN_TEMP;
68 moldyn->tau=MOLDYN_TAU;
69 moldyn->time_steps=MOLDYN_RUNS;
70 moldyn->integrate=velocity_verlet;
71 moldyn->potential_force_function=lennard_jones;
78 moldyn->ewrite=atoi(argv[++i]);
79 strncpy(moldyn->efb,argv[++i],64);
82 moldyn->mwrite=atoi(argv[++i]);
83 strncpy(moldyn->mfb,argv[++i],64);
86 moldyn->swrite=atoi(argv[++i]);
87 strncpy(moldyn->sfb,argv[++i],64);
90 moldyn->vwrite=atoi(argv[++i]);
91 strncpy(moldyn->vfb,argv[++i],64);
94 moldyn->t=atof(argv[++i]);
97 moldyn->tau=atof(argv[++i]);
100 moldyn->cutoff=atof(argv[++i]);
103 moldyn->time_steps=atoi(argv[++i]);
106 /* integration algorithm */
107 switch(atoi(argv[++i])) {
108 case MOLDYN_INTEGRATE_VERLET:
109 moldyn->integrate=velocity_verlet;
112 printf("unknown integration algo %s\n",argv[i]);
118 /* potential + params */
119 switch(atoi(argv[++i])) {
120 case MOLDYN_POTENTIAL_HO:
121 hop.spring_constant=atof(argv[++i]);
122 hop.equilibrium_distance=atof(argv[++i]);
123 moldyn->pot_params=malloc(sizeof(t_ho_params));
124 memcpy(moldyn->pot_params,&hop,sizeof(t_ho_params));
125 moldyn->potential_force_function=harmonic_oscillator;
127 case MOLDYN_POTENTIAL_LJ:
131 ljp.sigma6=s*s*s*s*s*s;
132 ljp.sigma12=ljp.sigma6*ljp.sigma6;
133 moldyn->pot_params=malloc(sizeof(t_lj_params));
134 memcpy(moldyn->pot_params,&ljp,sizeof(t_lj_params));
135 moldyn->potential_force_function=lennard_jones;
138 printf("unknown potential %s\n",argv[i]);
144 printf("unknown option %s\n",argv[i]);
157 int moldyn_log_init(t_moldyn *moldyn) {
165 moldyn->efd=open(moldyn->efb,O_WRONLY|O_CREAT|O_TRUNC);
167 perror("[moldyn] efd open");
170 dprintf(moldyn->efd,"# moldyn total energy logfile\n");
171 moldyn->lvstat|=MOLDYN_LVSTAT_TOTAL_E;
175 moldyn->mfd=open(moldyn->mfb,O_WRONLY|O_CREAT|O_TRUNC);
177 perror("[moldyn] mfd open");
180 dprintf(moldyn->mfd,"# moldyn total momentum logfile\n");
181 moldyn->lvstat|=MOLDYN_LVSTAT_TOTAL_M;
185 moldyn->lvstat|=MOLDYN_LVSTAT_SAVE;
187 if((moldyn->vwrite)&&(vis)) {
189 visual_init(vis,moldyn->vfb);
190 moldyn->lvstat|=MOLDYN_LVSTAT_VISUAL;
193 moldyn->lvstat|=MOLDYN_LVSTAT_INITIALIZED;
198 int moldyn_log_shutdown(t_moldyn *moldyn) {
200 if(moldyn->efd) close(moldyn->efd);
201 if(moldyn->mfd) close(moldyn->efd);
202 if(moldyn->dfd) close(moldyn->efd);
203 if(moldyn->visual) visual_tini(moldyn->visual);
208 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
212 ret=moldyn_parse_argv(moldyn,argc,argv);
213 if(ret<0) return ret;
215 ret=moldyn_log_init(moldyn);
216 if(ret<0) return ret;
218 rand_init(&(moldyn->random),NULL,1);
219 moldyn->random.status|=RAND_STAT_VERBOSE;
226 int moldyn_shutdown(t_moldyn *moldyn) {
228 moldyn_log_shutdown(moldyn);
229 rand_close(&(moldyn->random));
235 int create_lattice(unsigned char type,int element,double mass,double lc,
236 int a,int b,int c,t_atom **atom) {
244 if(type==FCC) count*=4;
245 if(type==DIAMOND) count*=8;
247 *atom=malloc(count*sizeof(t_atom));
249 perror("malloc (atoms)");
257 ret=fcc_init(a,b,c,lc,*atom,&origin);
260 ret=diamond_init(a,b,c,lc,*atom,&origin);
263 printf("unknown lattice type (%02x)\n",type);
269 printf("ok, there is something wrong ...\n");
270 printf("calculated -> %d atoms\n",count);
271 printf("created -> %d atoms\n",ret);
276 (*atom)[count-1].element=element;
277 (*atom)[count-1].mass=mass;
284 int destroy_lattice(t_atom *atom) {
291 int thermal_init(t_moldyn *moldyn) {
294 * - gaussian distribution of velocities
295 * - zero total momentum
296 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
301 t_3dvec p_total,delta;
306 random=&(moldyn->random);
308 /* gaussian distribution of velocities */
310 for(i=0;i<moldyn->count;i++) {
311 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t/atom[i].mass);
313 v=sigma*rand_get_gauss(random);
315 p_total.x+=atom[i].mass*v;
317 v=sigma*rand_get_gauss(random);
319 p_total.y+=atom[i].mass*v;
321 v=sigma*rand_get_gauss(random);
323 p_total.z+=atom[i].mass*v;
326 /* zero total momentum */
327 v3_scale(&p_total,&p_total,1.0/moldyn->count);
328 for(i=0;i<moldyn->count;i++) {
329 v3_scale(&delta,&p_total,1.0/atom[i].mass);
330 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
333 /* velocity scaling */
334 scale_velocity(moldyn);
339 int scale_velocity(t_moldyn *moldyn) {
348 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
351 for(i=0;i<moldyn->count;i++)
352 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
353 c=sqrt((2.0*e)/(3.0*moldyn->count*K_BOLTZMANN*moldyn->t));
354 for(i=0;i<moldyn->count;i++)
355 v3_scale(&(atom[i].v),&(atom[i].v),(1.0/c));
360 double get_e_kin(t_atom *atom,int count) {
367 for(i=0;i<count;i++) {
368 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
374 double get_e_pot(t_moldyn *moldyn) {
376 return moldyn->energy;
379 double get_total_energy(t_moldyn *moldyn) {
383 e=get_e_kin(moldyn->atom,moldyn->count);
384 e+=get_e_pot(moldyn);
389 t_3dvec get_total_p(t_atom *atom, int count) {
395 for(i=0;i<count;i++) {
396 v3_scale(&p,&(atom[i].v),atom[i].mass);
397 v3_add(&p_total,&p_total,&p);
403 double estimate_time_step(t_moldyn *moldyn,double nn_dist,double t) {
407 tau=0.05*nn_dist/(sqrt(3.0*K_BOLTZMANN*t/moldyn->atom[0].mass));
410 printf("[moldyn] warning: time step (%f > %.15f)\n",
420 /* linked list / cell method */
422 int link_cell_init(t_moldyn *moldyn) {
430 lc->listfd=open("/dev/null",O_WRONLY);
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;
440 lc->cells=lc->nx*lc->ny*lc->nz;
441 lc->subcell=malloc(lc->cells*sizeof(t_list));
443 printf("initializing linked cells (%d)\n",lc->cells);
445 for(i=0;i<lc->cells;i++)
446 //list_init(&(lc->subcell[i]),1);
447 list_init(&(lc->subcell[i]));
449 link_cell_update(moldyn);
454 int link_cell_update(t_moldyn *moldyn) {
468 for(i=0;i<lc->cells;i++)
469 list_destroy(&(moldyn->lc.subcell[i]));
471 for(count=0;count<moedyn->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]),
482 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
490 unsigned char bx,by,bz;
501 cell[0]=lc->subcell[i+j*nx+k*a];
502 for(ci=-1;ci<=1;ci++) {
509 for(cj=-1;cj<=1;cj++) {
516 for(ck=-1;ck<=1;ck++) {
523 if(!(ci|cj|ck)) continue;
525 cell[--count2]=lc->subcell[x+y*nx+z*a];
528 cell[count1++]=lc->subcell[x+y*nx+z*a];
540 int link_cell_shutdown(t_moldyn *moldyn) {
547 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
548 list_shutdown(&(moldyn->lc.subcell[i]));
550 if(lc->listfd) close(lc->listfd);
557 * 'integration of newtons equation' - algorithms
561 /* start the integration */
563 int moldyn_integrate(t_moldyn *moldyn) {
566 unsigned int e,m,s,d,v;
572 /* initialize linked cell method */
573 link_cell_init(moldyn);
575 /* logging & visualization */
582 if(!(moldyn->lvstat&MOLDYN_LVSTAT_INITIALIZED)) {
583 printf("[moldyn] warning, lv system not initialized\n");
587 /* sqaure of some variables */
588 moldyn->tau_square=moldyn->tau*moldyn->tau;
589 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
591 /* calculate initial forces */
592 moldyn->potential_force_function(moldyn);
594 for(i=0;i<moldyn->time_steps;i++) {
596 /* integration step */
597 moldyn->integrate(moldyn);
599 /* check for log & visualization */
603 "%.15f %.45f\n",i*moldyn->tau,
604 get_total_energy(moldyn));
608 p=get_total_p(moldyn->atom,moldyn->count);
610 "%.15f %.45f\n",i*moldyn->tau,
616 snprintf(fb,128,"%s-%f-%.15f.save",moldyn->sfb,
617 moldyn->t,i*moldyn->tau);
618 fd=open(fb,O_WRONLY|O_TRUNC|O_CREAT);
619 if(fd<0) perror("[moldyn] save fd open");
621 write(fd,moldyn,sizeof(t_moldyn));
622 write(fd,moldyn->atom,
623 moldyn->count*sizeof(t_atom));
630 visual_atoms(moldyn->visual,i*moldyn->tau,
631 moldyn->atom,moldyn->count);
632 printf("\rsteps: %d",i);
641 /* velocity verlet */
643 int velocity_verlet(t_moldyn *moldyn) {
646 double tau,tau_square;
653 tau_square=moldyn->tau_square;
655 for(i=0;i<count;i++) {
657 v3_scale(&delta,&(atom[i].v),tau);
658 v3_add(&(atom[i].r),&(atom[i].r),&delta);
659 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
660 v3_add(&(atom[i].r),&(atom[i].r),&delta);
661 v3_per_bound(&(atom[i].r),&(moldyn->dim));
664 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
665 v3_add(&(atom[i].v),&(atom[i].v),&delta);
668 /* neighbour list update */
669 printf("list update ...\n");
670 link_cell_update(moldyn);
673 /* forces depending on chosen potential */
674 printf("calc potential/force ...\n");
675 potential_force_calc(moldyn);
676 //moldyn->potential_force_function(moldyn);
679 for(i=0;i<count;i++) {
680 /* again velocities */
681 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
682 v3_add(&(atom[i].v),&(atom[i].v),&delta);
691 * potentials & corresponding forces
695 /* generic potential and force calculation */
697 int potential_force_calc(t_moldyn *moldyn) {
702 t_list neighbour[27];
705 unsigned char bc,bc3;
715 for(i=0;i<count;i++) {
718 v3_zero(&(atom[i].f));
720 /* single particle potential/force */
721 if(atom[i].attr&ATOM_ATTR_1BP)
722 moldyn->pf_func1b(moldyn,&(atom[i]));
724 /* 2 body pair potential/force */
725 if(atom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)) {
727 link_cell_neighbour_index(moldyn,
728 (atom[i].r.x+moldyn->dim.x/2)/lc->x,
729 (atom[i].r.y+moldyn->dim.y/2)/lc->y,
730 (atom[i].r.z+moldyn->dim.z/2)/lc->z,
736 for(j=0;j<countn;j++) {
738 this=&(neighbour[j]);
741 if(this->start==NULL)
747 btom=this->current->data;
752 if((btom->attr&ATOM_ATTR_2BP)&
753 (atom[i].attr&ATOM_ATTR_2BP))
754 moldyn->pf_func2b(moldyn,
759 /* 3 body potential/force */
761 if(!(atom[i].attr&ATOM_ATTR_3BP)||
762 !(btom->attr&ATOM_ATTR_3BP))
765 link_cell_neighbour_index(moldyn,
766 (btom->r.x+moldyn->dim.x/2)/lc->x,
767 (btom->r.y+moldyn->dim.y/2)/lc->y,
768 (btom->r.z+moldyn->dim.z/2)/lc->z,
771 for(k=0;k<lc->countn;k++) {
773 thisk=&(neighbourk[k]);
776 if(thisk->start==NULL)
779 bck=(k<lc->dnlc)?0:1;
783 ktom=thisk->current->data;
785 if(!(ktom->attr&ATOM_ATTR_3BP))
794 moldyn->pf_func3b(moldyn,&(atom[i]),btom,ktom,bck);
796 } while(list_next(thisk)!=\
799 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
811 /* harmonic oscillator potential and force */
813 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,unsigned char bc)) {
818 t_list *this,neighbour[27];
821 t_3dvec force,distance;
826 params=moldyn->pot_params;
829 sc=params->spring_constant;
830 equi_dist=params->equilibrium_distance;
833 /* reset energy counter */
836 for(i=0;i<count;i++) {
838 v3_zero(&(atom[i].f));
840 /* determine cell + neighbours */
841 ni=(atom[i].r.x+(moldyn->dim.x/2))/lc->x;
842 nj=(atom[i].r.y+(moldyn->dim.y/2))/lc->y;
843 nk=(atom[i].r.z+(moldyn->dim.z/2))/lc->z;
844 c=link_cell_neighbour_index(moldyn,ni,nj,nk,neighbour);
847 * processing cell of atom i
848 * => no need to check for empty list (1 element at minimum)
850 this=&(neighbour[0]);
853 btom=this->current->data;
856 v3_sub(&distance,&(atom[i].r),&(btom->r));
857 d=v3_norm(&distance);
858 if(d<=moldyn->cutoff) {
859 u+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
860 v3_scale(&force,&distance,
861 -sc*(1.0-(equi_dist/d)));
862 v3_add(&(atom[i].f),&(atom[i].f),&force);
864 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
867 * direct neighbour cells
868 * => no boundary condition check necessary
871 this=&(neighbour[j]);
872 list_reset(this); /* there might not be a single atom */
873 if(this->start!=NULL) {
876 btom=this->current->data;
877 v3_sub(&distance,&(atom[i].r),&(btom->r));
878 d=v3_norm(&distance);
879 if(d<=moldyn->cutoff) {
880 u+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
881 v3_scale(&force,&distance,
882 -sc*(1.0-(equi_dist/d)));
883 v3_add(&(atom[i].f),&(atom[i].f),
886 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
892 * indirect neighbour cells
893 * => check boundary conditions
896 this=&(neighbour[j]);
897 list_reset(this); /* check boundary conditions */
898 if(this->start!=NULL) {
901 btom=this->current->data;
902 v3_sub(&distance,&(atom[i].r),&(btom->r));
903 v3_per_bound(&distance,&(moldyn->dim));
904 d=v3_norm(&distance);
905 if(d<=moldyn->cutoff) {
906 u+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
907 v3_scale(&force,&distance,
908 -sc*(1.0-(equi_dist/d)));
909 v3_add(&(atom[i].f),&(atom[i].f),
912 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
918 moldyn->energy=0.5*u;
923 /* lennard jones potential & force for one sort of atoms */
925 int lennard_jones(t_moldyn *moldyn) {
930 t_list *this,neighbour[27];
933 t_3dvec force,distance;
935 double eps,sig6,sig12;
938 params=moldyn->pot_params;
942 eps=params->epsilon4;
944 sig12=params->sigma12;
946 /* reset energy counter */
949 for(i=0;i<count;i++) {
951 v3_zero(&(atom[i].f));
953 /* determine cell + neighbours */
954 ni=(atom[i].r.x+(moldyn->dim.x/2))/lc->x;
955 nj=(atom[i].r.y+(moldyn->dim.y/2))/lc->y;
956 nk=(atom[i].r.z+(moldyn->dim.z/2))/lc->z;
957 c=link_cell_neighbour_index(moldyn,ni,nj,nk,neighbour);
959 /* processing cell of atom i */
960 this=&(neighbour[0]);
961 list_reset(this); /* list has 1 element at minimum */
963 btom=this->current->data;
966 v3_sub(&distance,&(atom[i].r),&(btom->r));
967 d=v3_absolute_square(&distance); /* 1/r^2 */
968 if(d<=moldyn->cutoff_square) {
972 h1=h2*h2; /* 1/r^12 */
973 u+=eps*(sig12*h1-sig6*h2);
980 v3_scale(&force,&distance,d);
981 v3_add(&(atom[i].f),&(atom[i].f),&force);
983 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
985 /* neighbours not doing boundary condition overflow */
987 this=&(neighbour[j]);
988 list_reset(this); /* there might not be a single atom */
989 if(this->start!=NULL) {
992 btom=this->current->data;
993 v3_sub(&distance,&(atom[i].r),&(btom->r));
994 d=v3_absolute_square(&distance); /* r^2 */
995 if(d<=moldyn->cutoff_square) {
999 h1=h2*h2; /* 1/r^12 */
1000 u+=eps*(sig12*h1-sig6*h2);
1007 v3_scale(&force,&distance,d);
1008 v3_add(&(atom[i].f),&(atom[i].f),
1011 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1016 /* neighbours due to boundary conditions */
1018 this=&(neighbour[j]);
1019 list_reset(this); /* check boundary conditions */
1020 if(this->start!=NULL) {
1023 btom=this->current->data;
1024 v3_sub(&distance,&(atom[i].r),&(btom->r));
1025 v3_per_bound(&distance,&(moldyn->dim));
1026 d=v3_absolute_square(&distance); /* r^2 */
1027 if(d<=moldyn->cutoff_square) {
1028 d=1.0/d; /* 1/r^2 */
1031 h1=h2*h2; /* 1/r^12 */
1032 u+=eps*(sig12*h1-sig6*h2);
1039 v3_scale(&force,&distance,d);
1040 v3_add(&(atom[i].f),&(atom[i].f),
1043 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1049 moldyn->energy=0.5*u;
1054 /* tersoff potential & force for 2 sorts of atoms */
1056 int tersoff(t_moldyn *moldyn) {
1058 t_tersoff_params *params;
1059 t_atom *atom,*btom,*ktom;
1061 t_list *this,*thisk,neighbour[27],neighbourk[27];
1069 params=moldyn->pot_params;
1072 count=moldyn->count;
1074 /* reset energy counter */
1077 for(i=0;i<count;i++) {
1079 v3_zero(&(atom[i].f));
1081 /* determin cell neighbours */
1082 ni=(atom[i].r.x+(moldyn->dim.x/2))/lc->x;
1083 nj=(atom[i].r.y+(moldyn->dim.y/2))/lc->y;
1084 nk=(atom[i].r.z+(moldyn->dim.z/2))/lc->z;
1085 c=link_cell_neighbour_index(moldyn,ni,nj,nk,neighbour);
1088 * processing cell of atom i
1089 * => no need to check for empty list (1 element at minimum)
1091 this=&(neighbour[0]);
1094 btom=this->current->data;
1095 if(btom==&(atom[i]))
1100 /* we need: f_c, df_c, f_r, df_r */
1102 v3_sub(&dist_ij,btom,&(atom[i]));
1103 d_ij=v3_norm(&dist_ij);
1106 /* determine the tersoff parameters */
1107 if(atom[i].element!=btom->element) {
1108 S=sqrt(TERSOFF_S[e1]*TERSOFF_S[e2]);
1119 df_r=-lambda*A*exp(-lambda*d_ij)/d_ij;
1120 v3_scale(&force,&dist_ij,df_r);
1121 v3_add(&(atom[i].f),&(atom[i].f),
1126 arg1=PI*(d_ij-R)/s_r;
1127 f_c=0.5+0.5*cos(arg1);
1128 df_c=-0.5*sin(arg1)*(PI/(s_r*d_ij));
1129 f_r=A*exp(-lambda*d_ij);
1130 df_r=-lambda*f_r/d_ij;
1131 scale=df_c*f_r+df_r*f_c;
1132 v3_scale(&force,&dist_ij,scale);
1133 v3_add(&(atom[i].f),&(atom[i].f),
1141 /* end 2 body stuff */
1143 /* determine cell neighbours of btom */
1144 ki=(btom->r.x+(moldyn->dim.x/2))/lc->x;
1145 kj=(btom->r.y+(moldyn->dim.y/2))/lc->y;
1146 kk=(btom->r.z+(moldyn->dim.z/2))/lc->z;
1147 ck=link_cell_neighbour_index(moldyn,ki,kj,kk,
1150 /* go for zeta - 3 body stuff! */
1155 thisk=&(neighbourk[0]);
1158 ktom=thisk->current->data;
1161 if(ktom==&(atom[i]))
1164 /* 3 body stuff (1) */
1166 v3_sub(&dist_ik,ktom,&(atom[i]));
1167 d_ik=v3_norm(&dist_ik);
1188 arg1ik=PI*(d_ik-Rik)/sik_rik;
1189 f_cik=0.5+0.5*cos(arg1ik);
1190 df_cik=-0.5*sin(arg1ik)* \
1191 (PI/(sik_rik*d_ik));
1192 f_rik=Aik*exp(-lambda_ik*d_ik);
1193 f_aik=-Bik*exp(-mu_ik*d_ik);
1196 v3_sub(&distance_jk,ktom,btom);
1197 cos_theta=(d_ij2+d_ik*d_ik-d_jk*d_jk)/\
1199 sin_theta=sqrt(1.0/\
1200 (cos_theta*cos_theta));
1201 theta=arccos(cos_theta);
1208 /* end 3 body stuff (1) */
1211 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1213 /* direct neighbours of btom cell */
1215 thisk=&(neighbourk[k]);
1217 if(thisk->start!=NULL) {
1220 ktom=thisk->current->data;
1221 if(ktom==&(atom[i]))
1224 /* 3 body stuff (2) */
1226 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1231 /* indirect neighbours of btom cell */
1232 for(k=ck;k<27;k++) {
1233 thisk=&(neighbourk[k]);
1235 if(thisk->start!=NULL) {
1238 ktom=thisk->current->data;
1239 if(ktom==&(atom[i]))
1244 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1250 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1253 * direct neighbour cells of atom i
1256 this=&(neighbour[j]);
1258 if(this->start!=NULL) {
1261 btom=this->current->data;
1266 /* determine cell neighbours of btom */
1267 ki=(btom->r.x+(moldyn->dim.x/2))/lc->x;
1268 kj=(btom->r.y+(moldyn->dim.y/2))/lc->y;
1269 kk=(btom->r.z+(moldyn->dim.z/2))/lc->z;
1270 ck=link_cell_neighbour_index(moldyn,ki,kj,kk,
1274 thisk=&(neighbourk[0]);
1277 ktom=thisk->current->data;
1280 if(ktom==&(atom[i]))
1283 /* 3 body stuff (1) */
1285 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1287 /* direct neighbours of btom cell */
1289 thisk=&(neighbourk[k]);
1291 if(thisk->start!=NULL) {
1294 ktom=thisk->current->data;
1295 if(ktom==&(atom[i]))
1298 /* 3 body stuff (2) */
1300 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1305 /* indirect neighbours of btom cell */
1306 for(k=ck;k<27;k++) {
1307 thisk=&(neighbourk[k]);
1309 if(thisk->start!=NULL) {
1312 ktom=thisk->current->data;
1313 if(ktom==&(atom[i]))
1316 /* 3 body stuff (3) */
1318 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1324 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1330 * indirect neighbour cells of atom i
1333 this=&(neighbour[j]);
1335 if(this->start!=NULL) {
1338 btom=this->current->data;
1343 /* determine cell neighbours of btom */
1344 ki=(btom->r.x+(moldyn->dim.x/2))/lc->x;
1345 kj=(btom->r.y+(moldyn->dim.y/2))/lc->y;
1346 kk=(btom->r.z+(moldyn->dim.z/2))/lc->z;
1347 ck=link_cell_neighbour_index(moldyn,ki,kj,kk,
1351 thisk=&(neighbourk[0]);
1354 ktom=thisk->current->data;
1357 if(ktom==&(atom[i]))
1360 /* 3 body stuff (1) */
1362 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1364 /* direct neighbours of btom cell */
1366 thisk=&(neighbourk[k]);
1368 if(thisk->start!=NULL) {
1371 ktom=thisk->current->data;
1372 if(ktom==&(atom[i]))
1375 /* 3 body stuff (2) */
1377 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1382 /* indirect neighbours of btom cell */
1383 for(k=ck;k<27;k++) {
1384 thisk=&(neighbourk[k]);
1386 if(thisk->start!=NULL) {
1389 ktom=thisk->current->data;
1390 if(ktom==&(atom[i]))
1393 /* 3 body stuff (3) */
1395 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1401 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1408 moldyn->energy=0.5*u;