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(u8 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) {
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];
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);
808 * periodic boundayr checking
811 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
819 if(moldyn->MOLDYN_ATTR_PBX)
820 if(a->x>=x) a->x-=dim->x;
821 else if(-a->x>x) a->x+=dim->x;
822 if(moldyn->MOLDYN_ATTR_PBY)
823 if(a->y>=y) a->y-=dim->y;
824 else if(-a->y>y) a->y+=dim->y;
825 if(moldyn->MOLDYN_ATTR_PBZ)
826 if(a->z>=z) a->z-=dim->z;
827 else if(-a->z>z) a->z+=dim->z;
837 /* harmonic oscillator potential and force */
839 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc)) {
842 t_3dvec force,distance;
846 params=moldyn->pot2b_params;
847 sc=params->spring_constant;
848 equi_dist=params->equilibrium_distance;
850 v3_sub(&distance,&(ai->r),&(aj->r);
852 v3_per_bound(&distance,&(moldyn->dim));
853 if(bc) check_per_bound(moldyn,&distance);
854 d=v3_norm(&distance);
855 if(d<=moldyn->cutoff) {
856 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
857 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
858 v3_scale(&force,&distance,-sc*(1.0-(equi_dist/d)));
859 v3_add(&(ai->f),&(ai->f),&force);
865 /* lennard jones potential & force for one sort of atoms */
867 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
870 t_3dvec force,distance;
872 double eps,sig6,sig12;
874 params=moldyn->pot_params;
875 eps=params->epsilon4;
877 sig12=params->sigma12;
879 v3_sub(&distance,&(ai->r),&(aj->r));
880 if(bc) check_per_bound(moldyn,&distance);
881 d=v3_absolute_square(&distance); /* 1/r^2 */
882 if(d<=moldyn->cutoff_square) {
886 h1=h2*h2; /* 1/r^12 */
887 /* energy is eps*..., but we will add this twice ... */
888 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
895 v3_scale(&force,&distance,d);
896 v3_add(&(ai->f),&(aj->f),&force);
903 * tersoff potential & force for 2 sorts of atoms
906 /* tersoff 2 body part */
908 int tersoff_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
910 t_tersoff_params *params;
914 params=moldyn->pot_params;
917 * we need: f_c, df_c, f_r, df_r
919 * therefore we need: R, S
922 v3_sub(&dist_ij,&(ai->r),&(aj->r));
924 if(bc) check_per_bound(moldyn,&dist_ij);
926 if(ai->bnum==aj->bnum) {
927 S=params->S[ai->bnum];
928 R=params->R[ai->bnum];
935 d_ij=v3_norm(&dist_ij);
938 f_r=A*exp(-lamda*d_ij);
939 df_r=-lambda*f_r/d_ij;
941 /* f_c = 1, df_c = 0 */
942 v3_scale(&force,&dist_ij,df_r);
947 f_c=0.5+0.5*cos(arg);
948 df_c=-0.5*sin(arg)*(PI/(s_r*d_ij));
949 scale=df_c*f_r+df_r*f_c;
950 v3_scale(&force,&dist_ij,scale);
951 v3_add(&(ai->f),&(ai->f),&force);
953 moldyn->energy+=(f_r*f_c);
959 /* tersoff 3 body part */
961 int tersoff(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc,u8 bck) {
963 t_tersoff_params *params;
967 params=moldyn->pot_params;
969 /* 2 body part of the tersoff potential */
971 v3_sub(&dist_ij,&(ai->r),&(aj->r));
972 if(bc) check_per_bound(moldyn,&dist_ij);
973 d_ij=v3_norm(&dist_ij);
976 /* determine the tersoff parameters */
977 if(atom[i].element!=btom->element) {
978 S=sqrt(TERSOFF_S[e1]*TERSOFF_S[e2]);
989 df_r=-lambda*A*exp(-lambda*d_ij)/d_ij;
990 v3_scale(&force,&dist_ij,df_r);
991 v3_add(&(atom[i].f),&(atom[i].f),
996 arg1=PI*(d_ij-R)/s_r;
997 f_c=0.5+0.5*cos(arg1);
998 df_c=-0.5*sin(arg1)*(PI/(s_r*d_ij));
999 f_r=A*exp(-lambda*d_ij);
1000 df_r=-lambda*f_r/d_ij;
1001 scale=df_c*f_r+df_r*f_c;
1002 v3_scale(&force,&dist_ij,scale);
1003 v3_add(&(atom[i].f),&(atom[i].f),
1011 /* end 2 body stuff */
1013 /* determine cell neighbours of btom */
1014 ki=(btom->r.x+(moldyn->dim.x/2))/lc->x;
1015 kj=(btom->r.y+(moldyn->dim.y/2))/lc->y;
1016 kk=(btom->r.z+(moldyn->dim.z/2))/lc->z;
1017 ck=link_cell_neighbour_index(moldyn,ki,kj,kk,
1020 /* go for zeta - 3 body stuff! */
1025 thisk=&(neighbourk[0]);
1028 ktom=thisk->current->data;
1031 if(ktom==&(atom[i]))
1034 /* 3 body stuff (1) */
1036 v3_sub(&dist_ik,ktom,&(atom[i]));
1037 d_ik=v3_norm(&dist_ik);
1058 arg1ik=PI*(d_ik-Rik)/sik_rik;
1059 f_cik=0.5+0.5*cos(arg1ik);
1060 df_cik=-0.5*sin(arg1ik)* \
1061 (PI/(sik_rik*d_ik));
1062 f_rik=Aik*exp(-lambda_ik*d_ik);
1063 f_aik=-Bik*exp(-mu_ik*d_ik);
1066 v3_sub(&distance_jk,ktom,btom);
1067 cos_theta=(d_ij2+d_ik*d_ik-d_jk*d_jk)/\
1069 sin_theta=sqrt(1.0/\
1070 (cos_theta*cos_theta));
1071 theta=arccos(cos_theta);
1078 /* end 3 body stuff (1) */
1081 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1083 /* direct neighbours of btom cell */
1085 thisk=&(neighbourk[k]);
1087 if(thisk->start!=NULL) {
1090 ktom=thisk->current->data;
1091 if(ktom==&(atom[i]))
1094 /* 3 body stuff (2) */
1096 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1101 /* indirect neighbours of btom cell */
1102 for(k=ck;k<27;k++) {
1103 thisk=&(neighbourk[k]);
1105 if(thisk->start!=NULL) {
1108 ktom=thisk->current->data;
1109 if(ktom==&(atom[i]))
1114 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1120 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1123 * direct neighbour cells of atom i
1126 this=&(neighbour[j]);
1128 if(this->start!=NULL) {
1131 btom=this->current->data;
1136 /* determine cell neighbours of btom */
1137 ki=(btom->r.x+(moldyn->dim.x/2))/lc->x;
1138 kj=(btom->r.y+(moldyn->dim.y/2))/lc->y;
1139 kk=(btom->r.z+(moldyn->dim.z/2))/lc->z;
1140 ck=link_cell_neighbour_index(moldyn,ki,kj,kk,
1144 thisk=&(neighbourk[0]);
1147 ktom=thisk->current->data;
1150 if(ktom==&(atom[i]))
1153 /* 3 body stuff (1) */
1155 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1157 /* direct neighbours of btom cell */
1159 thisk=&(neighbourk[k]);
1161 if(thisk->start!=NULL) {
1164 ktom=thisk->current->data;
1165 if(ktom==&(atom[i]))
1168 /* 3 body stuff (2) */
1170 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1175 /* indirect neighbours of btom cell */
1176 for(k=ck;k<27;k++) {
1177 thisk=&(neighbourk[k]);
1179 if(thisk->start!=NULL) {
1182 ktom=thisk->current->data;
1183 if(ktom==&(atom[i]))
1186 /* 3 body stuff (3) */
1188 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1194 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1200 * indirect neighbour cells of atom i
1203 this=&(neighbour[j]);
1205 if(this->start!=NULL) {
1208 btom=this->current->data;
1213 /* determine cell neighbours of btom */
1214 ki=(btom->r.x+(moldyn->dim.x/2))/lc->x;
1215 kj=(btom->r.y+(moldyn->dim.y/2))/lc->y;
1216 kk=(btom->r.z+(moldyn->dim.z/2))/lc->z;
1217 ck=link_cell_neighbour_index(moldyn,ki,kj,kk,
1221 thisk=&(neighbourk[0]);
1224 ktom=thisk->current->data;
1227 if(ktom==&(atom[i]))
1230 /* 3 body stuff (1) */
1232 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1234 /* direct neighbours of btom cell */
1236 thisk=&(neighbourk[k]);
1238 if(thisk->start!=NULL) {
1241 ktom=thisk->current->data;
1242 if(ktom==&(atom[i]))
1245 /* 3 body stuff (2) */
1247 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1252 /* indirect neighbours of btom cell */
1253 for(k=ck;k<27;k++) {
1254 thisk=&(neighbourk[k]);
1256 if(thisk->start!=NULL) {
1259 ktom=thisk->current->data;
1260 if(ktom==&(atom[i]))
1263 /* 3 body stuff (3) */
1265 } while(list_next(thisk)!=L_NO_NEXT_ELEMENT);
1271 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1278 moldyn->energy=0.5*u;