+double estimate_time_step(t_moldyn *moldyn,double nn_dist,double t) {
+
+ double tau;
+
+ tau=0.05*nn_dist/(sqrt(3.0*K_BOLTZMANN*t/moldyn->atom[0].mass));
+ tau*=1.0E-9;
+ if(tau<moldyn->tau)
+ printf("[moldyn] warning: time step (%f > %.15f)\n",
+ moldyn->tau,tau);
+
+ return tau;
+}
+
+/*
+ * numerical tricks
+ */
+
+/* linked list / cell method */
+
+int link_cell_init(t_moldyn *moldyn) {
+
+ t_linkcell *lc;
+ int i;
+
+ lc=&(moldyn->lc);
+
+ /* list log fd */
+ lc->listfd=open("/dev/null",O_WRONLY);
+
+ /* partitioning the md cell */
+ lc->nx=moldyn->dim.x/moldyn->cutoff;
+ lc->x=moldyn->dim.x/lc->nx;
+ lc->ny=moldyn->dim.y/moldyn->cutoff;
+ lc->y=moldyn->dim.y/lc->ny;
+ lc->nz=moldyn->dim.z/moldyn->cutoff;
+ lc->z=moldyn->dim.z/lc->nz;
+
+ lc->cells=lc->nx*lc->ny*lc->nz;
+ lc->subcell=malloc(lc->cells*sizeof(t_list));
+
+ printf("initializing linked cells (%d)\n",lc->cells);
+
+ for(i=0;i<lc->cells;i++)
+ //list_init(&(lc->subcell[i]),1);
+ list_init(&(lc->subcell[i]));
+
+ link_cell_update(moldyn);
+
+ return 0;
+}
+
+int link_cell_update(t_moldyn *moldyn) {
+
+ int count,i,j,k;
+ int nx,ny,nz;
+ t_atom *atom;
+ t_linkcell *lc;
+
+ atom=moldyn->atom;
+ lc=&(moldyn->lc);
+
+ nx=lc->nx;
+ ny=lc->ny;
+ nz=lc->nz;
+
+ for(i=0;i<lc->cells;i++)
+ list_destroy(&(moldyn->lc.subcell[i]));
+
+ for(count=0;count<moedyn->count;count++) {
+ i=(atom[count].r.x+(moldyn->dim.x/2))/lc->x;
+ j=(atom[count].r.y+(moldyn->dim.y/2))/lc->y;
+ k=(atom[count].r.z+(moldyn->dim.z/2))/lc->z;
+ list_add_immediate_ptr(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
+ &(atom[count]));
+ }
+
+ return 0;
+}
+
+int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
+
+ t_linkcell *lc;
+ int a;
+ int count1,count2;
+ int ci,cj,ck;
+ int nx,ny,nz;
+ int x,y,z;
+ u8 bx,by,bz;
+
+ lc=&(moldyn->lc);
+ nx=lc->nx;
+ ny=lc->ny;
+ nz=lc->nz;
+ count1=1;
+ count2=27;
+ a=nx*ny;
+
+
+ cell[0]=lc->subcell[i+j*nx+k*a];
+ for(ci=-1;ci<=1;ci++) {
+ bx=0;
+ x=i+ci;
+ if((x<0)||(x>=nx)) {
+ x=(x+nx)%nx;
+ bx=1;
+ }
+ for(cj=-1;cj<=1;cj++) {
+ by=0;
+ y=j+cj;
+ if((y<0)||(y>=ny)) {
+ y=(y+ny)%ny;
+ by=1;
+ }
+ for(ck=-1;ck<=1;ck++) {
+ bz=0;
+ z=k+ck;
+ if((z<0)||(z>=nz)) {
+ z=(z+nz)%nz;
+ bz=1;
+ }
+ if(!(ci|cj|ck)) continue;
+ if(bx|by|bz) {
+ cell[--count2]=lc->subcell[x+y*nx+z*a];
+ }
+ else {
+ cell[count1++]=lc->subcell[x+y*nx+z*a];
+ }
+ }
+ }
+ }
+
+ lc->dnlc=count2;
+ lc->countn=27;
+
+ return count2;
+}
+
+int link_cell_shutdown(t_moldyn *moldyn) {
+
+ int i;
+ t_linkcell *lc;
+
+ lc=&(moldyn->lc);
+
+ for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
+ list_shutdown(&(moldyn->lc.subcell[i]));
+
+ if(lc->listfd) close(lc->listfd);
+
+ return 0;
+}
+
+/*
+ *
+ * 'integration of newtons equation' - algorithms
+ *
+ */
+
+/* start the integration */
+
+int moldyn_integrate(t_moldyn *moldyn) {
+
+ int i;
+ unsigned int e,m,s,d,v;
+ t_3dvec p;
+
+ int fd;
+ char fb[128];
+
+ /* initialize linked cell method */
+ link_cell_init(moldyn);
+
+ /* logging & visualization */
+ e=moldyn->ewrite;
+ m=moldyn->mwrite;
+ s=moldyn->swrite;
+ d=moldyn->dwrite;
+ v=moldyn->vwrite;
+
+ if(!(moldyn->lvstat&MOLDYN_LVSTAT_INITIALIZED)) {
+ printf("[moldyn] warning, lv system not initialized\n");
+ return -1;
+ }
+
+ /* sqaure of some variables */
+ moldyn->tau_square=moldyn->tau*moldyn->tau;
+ moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
+
+ /* calculate initial forces */
+ moldyn->potential_force_function(moldyn);
+
+ for(i=0;i<moldyn->time_steps;i++) {
+
+ /* integration step */
+ moldyn->integrate(moldyn);
+
+ /* check for log & visualization */
+ if(e) {
+ if(!(i%e))
+ dprintf(moldyn->efd,
+ "%.15f %.45f\n",i*moldyn->tau,
+ get_total_energy(moldyn));
+ }
+ if(m) {
+ if(!(i%m)) {
+ p=get_total_p(moldyn->atom,moldyn->count);
+ dprintf(moldyn->mfd,
+ "%.15f %.45f\n",i*moldyn->tau,
+ v3_norm(&p));
+ }
+ }
+ if(s) {
+ if(!(i%s)) {
+ snprintf(fb,128,"%s-%f-%.15f.save",moldyn->sfb,
+ moldyn->t,i*moldyn->tau);
+ fd=open(fb,O_WRONLY|O_TRUNC|O_CREAT);
+ if(fd<0) perror("[moldyn] save fd open");
+ else {
+ write(fd,moldyn,sizeof(t_moldyn));
+ write(fd,moldyn->atom,
+ moldyn->count*sizeof(t_atom));
+ }
+ close(fd);
+ }
+ }
+ if(v) {
+ if(!(i%v)) {
+ visual_atoms(moldyn->visual,i*moldyn->tau,
+ moldyn->atom,moldyn->count);
+ printf("\rsteps: %d",i);
+ fflush(stdout);
+ }
+ }
+ }
+
+ return 0;
+}
+
+/* velocity verlet */
+
+int velocity_verlet(t_moldyn *moldyn) {
+
+ int i,count;
+ double tau,tau_square;
+ t_3dvec delta;
+ t_atom *atom;
+
+ atom=moldyn->atom;
+ count=moldyn->count;
+ tau=moldyn->tau;
+ tau_square=moldyn->tau_square;
+
+ for(i=0;i<count;i++) {
+ /* new positions */
+ v3_scale(&delta,&(atom[i].v),tau);
+ v3_add(&(atom[i].r),&(atom[i].r),&delta);
+ v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
+ v3_add(&(atom[i].r),&(atom[i].r),&delta);
+ v3_per_bound(&(atom[i].r),&(moldyn->dim));
+
+ /* velocities */
+ v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
+ v3_add(&(atom[i].v),&(atom[i].v),&delta);
+ }
+
+ /* neighbour list update */
+printf("list update ...\n");
+ link_cell_update(moldyn);
+printf("done\n");
+
+ /* forces depending on chosen potential */
+printf("calc potential/force ...\n");
+ potential_force_calc(moldyn);
+ //moldyn->potential_force_function(moldyn);
+printf("done\n");
+
+ for(i=0;i<count;i++) {
+ /* again velocities */
+ v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
+ v3_add(&(atom[i].v),&(atom[i].v),&delta);
+ }
+
+ return 0;
+}
+