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 #define CREATE_CELL_LIST(nb_list) \
698 link_cell_neighbour_index(moldyn,\
699 (atom[i].r.x+moldyn->dim.x/2)/lc->x,\
700 (atom[i].r.y+moldyn->dim.y/2)/lc->y,\
701 (atom[i].r.z+moldyn->dim.z/2)/lc->z,\
705 int potential_force_calc(t_moldyn *moldyn) {
710 t_list neighbour[27];
721 for(i=0;i<count;i++) {
724 v3_zero(&(atom[i].f));
726 /* single particle potential/force */
727 if(moldyn->status&MOLDYN_STAT_1BP)
728 moldyn->pf_func1b(moldyn,&(atom[i]));
730 /* 2 body pair potential/force */
731 if(moldyn->status&MOLDYN_STAT_2BP) {
733 CREATE_CELL_LIST(neighbour);
736 * processing cell of atom i
737 * => no need to check for empty list
738 * (1 element at minimum)
741 this=&(neighbour[0]);
744 btom=this->current->data;
746 moldyn->pf_func2b(moldyn,
748 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
751 * direct neighbour cells
752 * => no boundary condition check necessary
754 for(j=0;j<lc->dnlc;j++) {
755 this=&(neighbour[j]);
757 if(this->start!=NULL) {
759 btom=this->current->data;
760 moldyn->pf_func2b(moldyn,
763 } while(list_next(this)!=\
768 * neighbour cells due to periodic bc
769 * => check boundary conditions
771 for(j=lc->dnlc;j<lc->countn;j++) {
772 this=&(neighbour[j]);
774 if(this->start!=NULL) {
776 btom=this->current->data;
777 moldyn->pf_func2b(moldyn,
789 /* harmonic oscillator potential and force */
791 int harmonic_oscillator(t_moldyn *moldyn) {
796 t_list *this,neighbour[27];
799 t_3dvec force,distance;
804 params=moldyn->pot_params;
807 sc=params->spring_constant;
808 equi_dist=params->equilibrium_distance;
811 /* reset energy counter */
814 for(i=0;i<count;i++) {
816 v3_zero(&(atom[i].f));
818 /* determine cell + neighbours */
819 ni=(atom[i].r.x+(moldyn->dim.x/2))/lc->x;
820 nj=(atom[i].r.y+(moldyn->dim.y/2))/lc->y;
821 nk=(atom[i].r.z+(moldyn->dim.z/2))/lc->z;
822 c=link_cell_neighbour_index(moldyn,ni,nj,nk,neighbour);
825 * processing cell of atom i
826 * => no need to check for empty list (1 element at minimum)
828 this=&(neighbour[0]);
831 btom=this->current->data;
834 v3_sub(&distance,&(atom[i].r),&(btom->r));
835 d=v3_norm(&distance);
836 if(d<=moldyn->cutoff) {
837 u+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
838 v3_scale(&force,&distance,
839 -sc*(1.0-(equi_dist/d)));
840 v3_add(&(atom[i].f),&(atom[i].f),&force);
842 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
845 * direct neighbour cells
846 * => no boundary condition check necessary
849 this=&(neighbour[j]);
850 list_reset(this); /* there might not be a single atom */
851 if(this->start!=NULL) {
854 btom=this->current->data;
855 v3_sub(&distance,&(atom[i].r),&(btom->r));
856 d=v3_norm(&distance);
857 if(d<=moldyn->cutoff) {
858 u+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
859 v3_scale(&force,&distance,
860 -sc*(1.0-(equi_dist/d)));
861 v3_add(&(atom[i].f),&(atom[i].f),
864 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
870 * indirect neighbour cells
871 * => check boundary conditions
874 this=&(neighbour[j]);
875 list_reset(this); /* check boundary conditions */
876 if(this->start!=NULL) {
879 btom=this->current->data;
880 v3_sub(&distance,&(atom[i].r),&(btom->r));
881 v3_per_bound(&distance,&(moldyn->dim));
882 d=v3_norm(&distance);
883 if(d<=moldyn->cutoff) {
884 u+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
885 v3_scale(&force,&distance,
886 -sc*(1.0-(equi_dist/d)));
887 v3_add(&(atom[i].f),&(atom[i].f),
890 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
896 moldyn->energy=0.5*u;
901 /* lennard jones potential & force for one sort of atoms */
903 int lennard_jones(t_moldyn *moldyn) {
908 t_list *this,neighbour[27];
911 t_3dvec force,distance;
913 double eps,sig6,sig12;
916 params=moldyn->pot_params;
920 eps=params->epsilon4;
922 sig12=params->sigma12;
924 /* reset energy counter */
927 for(i=0;i<count;i++) {
929 v3_zero(&(atom[i].f));
931 /* determine cell + neighbours */
932 ni=(atom[i].r.x+(moldyn->dim.x/2))/lc->x;
933 nj=(atom[i].r.y+(moldyn->dim.y/2))/lc->y;
934 nk=(atom[i].r.z+(moldyn->dim.z/2))/lc->z;
935 c=link_cell_neighbour_index(moldyn,ni,nj,nk,neighbour);
937 /* processing cell of atom i */
938 this=&(neighbour[0]);
939 list_reset(this); /* list has 1 element at minimum */
941 btom=this->current->data;
944 v3_sub(&distance,&(atom[i].r),&(btom->r));
945 d=v3_absolute_square(&distance); /* 1/r^2 */
946 if(d<=moldyn->cutoff_square) {
950 h1=h2*h2; /* 1/r^12 */
951 u+=eps*(sig12*h1-sig6*h2);
958 v3_scale(&force,&distance,d);
959 v3_add(&(atom[i].f),&(atom[i].f),&force);
961 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
963 /* neighbours not doing boundary condition overflow */
965 this=&(neighbour[j]);
966 list_reset(this); /* there might not be a single atom */
967 if(this->start!=NULL) {
970 btom=this->current->data;
971 v3_sub(&distance,&(atom[i].r),&(btom->r));
972 d=v3_absolute_square(&distance); /* r^2 */
973 if(d<=moldyn->cutoff_square) {
977 h1=h2*h2; /* 1/r^12 */
978 u+=eps*(sig12*h1-sig6*h2);
985 v3_scale(&force,&distance,d);
986 v3_add(&(atom[i].f),&(atom[i].f),
989 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
994 /* neighbours due to boundary conditions */
996 this=&(neighbour[j]);
997 list_reset(this); /* check boundary conditions */
998 if(this->start!=NULL) {
1001 btom=this->current->data;
1002 v3_sub(&distance,&(atom[i].r),&(btom->r));
1003 v3_per_bound(&distance,&(moldyn->dim));
1004 d=v3_absolute_square(&distance); /* r^2 */
1005 if(d<=moldyn->cutoff_square) {
1006 d=1.0/d; /* 1/r^2 */
1009 h1=h2*h2; /* 1/r^12 */
1010 u+=eps*(sig12*h1-sig6*h2);
1017 v3_scale(&force,&distance,d);
1018 v3_add(&(atom[i].f),&(atom[i].f),
1021 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1027 moldyn->energy=0.5*u;
1032 /* tersoff potential & force for 2 sorts of atoms */
1034 int tersoff(t_moldyn *moldyn) {
1036 t_tersoff_params *params;
1037 t_atom *atom,*btom,*ktom;
1039 t_list *this,*thisk,neighbour[27],neighbourk[27];
1047 params=moldyn->pot_params;
1050 count=moldyn->count;
1052 /* reset energy counter */
1055 for(i=0;i<count;i++) {
1057 v3_zero(&(atom[i].f));
1059 /* determin cell neighbours */
1060 ni=(atom[i].r.x+(moldyn->dim.x/2))/lc->x;
1061 nj=(atom[i].r.y+(moldyn->dim.y/2))/lc->y;
1062 nk=(atom[i].r.z+(moldyn->dim.z/2))/lc->z;
1063 c=link_cell_neighbour_index(moldyn,ni,nj,nk,neighbour);
1066 * processing cell of atom i
1067 * => no need to check for empty list (1 element at minimum)
1069 this=&(neighbour[0]);
1072 btom=this->current->data;
1073 if(btom==&(atom[i]))
1078 /* we need: f_c, df_c, f_r, df_r */
1080 v3_sub(&dist_ij,btom,&(atom[i]));
1081 d_ij=v3_norm(&dist_ij);
1084 /* determine the tersoff parameters */
1085 if(atom[i].element!=btom->element) {
1086 S=sqrt(TERSOFF_S[e1]*TERSOFF_S[e2]);
1097 df_r=-lambda*A*exp(-lambda*d_ij)/d_ij;
1098 v3_scale(&force,&dist_ij,df_r);
1099 v3_add(&(atom[i].f),&(atom[i].f),
1104 arg1=PI*(d_ij-R)/s_r;
1105 f_c=0.5+0.5*cos(arg1);
1106 df_c=-0.5*sin(arg1)*(PI/(s_r*d_ij));
1107 f_r=A*exp(-lambda*d_ij);
1108 df_r=-lambda*f_r/d_ij;
1109 scale=df_c*f_r+df_r*f_c;
1110 v3_scale(&force,&dist_ij,scale);
1111 v3_add(&(atom[i].f),&(atom[i].f),
1119 /* end 2 body stuff */
1121 /* determine cell neighbours of btom */
1122 ki=(btom->r.x+(moldyn->dim.x/2))/lc->x;
1123 kj=(btom->r.y+(moldyn->dim.y/2))/lc->y;
1124 kk=(btom->r.z+(moldyn->dim.z/2))/lc->z;
1125 ck=link_cell_neighbour_index(moldyn,ki,kj,kk,
1128 /* go for zeta - 3 body stuff! */
1133 thisk=&(neighbourk[0]);
1136 ktom=thisk->current->data;
1139 if(ktom==&(atom[i]))
1142 /* 3 body stuff (1) */
1144 v3_sub(&dist_ik,ktom,&(atom[i]));
1145 d_ik=v3_norm(&dist_ik);
1166 arg1ik=PI*(d_ik-Rik)/sik_rik;
1167 f_cik=0.5+0.5*cos(arg1ik);
1168 df_cik=-0.5*sin(arg1ik)* \
1169 (PI/(sik_rik*d_ik));
1170 f_rik=Aik*exp(-lambda_ik*d_ik);
1171 f_aik=-Bik*exp(-mu_ik*d_ik);
1174 v3_sub(&distance_jk,ktom,btom);
1175 cos_theta=(d_ij2+d_ik*d_ik-d_jk*d_jk)/\
1177 sin_theta=sqrt(1.0/\
1178 (cos_theta*cos_theta));
1179 theta=arccos(cos_theta);
1186 /* end 3 body stuff (1) */
1189 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1191 /* direct neighbours of btom cell */
1193 thisk=&(neighbourk[k]);
1195 if(thisk->start!=NULL) {
1198 ktom=thisk->current->data;
1199 if(ktom==&(atom[i]))
1202 /* 3 body stuff (2) */
1204 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1209 /* indirect neighbours of btom cell */
1210 for(k=ck;k<27;k++) {
1211 thisk=&(neighbourk[k]);
1213 if(thisk->start!=NULL) {
1216 ktom=thisk->current->data;
1217 if(ktom==&(atom[i]))
1222 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1228 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1231 * direct neighbour cells of atom i
1234 this=&(neighbour[j]);
1236 if(this->start!=NULL) {
1239 btom=this->current->data;
1244 /* determine cell neighbours of btom */
1245 ki=(btom->r.x+(moldyn->dim.x/2))/lc->x;
1246 kj=(btom->r.y+(moldyn->dim.y/2))/lc->y;
1247 kk=(btom->r.z+(moldyn->dim.z/2))/lc->z;
1248 ck=link_cell_neighbour_index(moldyn,ki,kj,kk,
1252 thisk=&(neighbourk[0]);
1255 ktom=thisk->current->data;
1258 if(ktom==&(atom[i]))
1261 /* 3 body stuff (1) */
1263 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1265 /* direct neighbours of btom cell */
1267 thisk=&(neighbourk[k]);
1269 if(thisk->start!=NULL) {
1272 ktom=thisk->current->data;
1273 if(ktom==&(atom[i]))
1276 /* 3 body stuff (2) */
1278 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1283 /* indirect neighbours of btom cell */
1284 for(k=ck;k<27;k++) {
1285 thisk=&(neighbourk[k]);
1287 if(thisk->start!=NULL) {
1290 ktom=thisk->current->data;
1291 if(ktom==&(atom[i]))
1294 /* 3 body stuff (3) */
1296 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1302 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1308 * indirect neighbour cells of atom i
1311 this=&(neighbour[j]);
1313 if(this->start!=NULL) {
1316 btom=this->current->data;
1321 /* determine cell neighbours of btom */
1322 ki=(btom->r.x+(moldyn->dim.x/2))/lc->x;
1323 kj=(btom->r.y+(moldyn->dim.y/2))/lc->y;
1324 kk=(btom->r.z+(moldyn->dim.z/2))/lc->z;
1325 ck=link_cell_neighbour_index(moldyn,ki,kj,kk,
1329 thisk=&(neighbourk[0]);
1332 ktom=thisk->current->data;
1335 if(ktom==&(atom[i]))
1338 /* 3 body stuff (1) */
1340 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1342 /* direct neighbours of btom cell */
1344 thisk=&(neighbourk[k]);
1346 if(thisk->start!=NULL) {
1349 ktom=thisk->current->data;
1350 if(ktom==&(atom[i]))
1353 /* 3 body stuff (2) */
1355 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1360 /* indirect neighbours of btom cell */
1361 for(k=ck;k<27;k++) {
1362 thisk=&(neighbourk[k]);
1364 if(thisk->start!=NULL) {
1367 ktom=thisk->current->data;
1368 if(ktom==&(atom[i]))
1371 /* 3 body stuff (3) */
1373 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1379 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1386 moldyn->energy=0.5*u;