+t_3dvec get_total_p(t_atom *atom, int count) {
+
+ t_3dvec p,p_total;
+ int i;
+
+ v3_zero(&p_total);
+ for(i=0;i<count;i++) {
+ v3_scale(&p,&(atom[i].v),atom[i].mass);
+ v3_add(&p_total,&p_total,&p);
+ }
+
+ return p_total;
+}
+
+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
+ */
+
+/* verlet list */
+
+int verlet_list_init(t_moldyn *moldyn) {
+
+ int i,fd;
+
+ fd=open("/dev/null",O_WRONLY);
+
+ for(i=0;i<moldyn->count;i++)
+ list_init(&(moldyn->atom[i].verlet),fd);
+
+ moldyn->r_verlet=1.1*moldyn->cutoff;
+ /* +moldyn->tau*\
+ sqrt(3.0*K_BOLTZMANN*moldyn->t/moldyn->atom[0].mass); */
+
+ printf("debug: r verlet = %.15f\n",moldyn->r_verlet);
+ printf(" r cutoff = %.15f\n",moldyn->cutoff);
+ printf(" dim = %.15f\n",moldyn->dim.x);
+
+ /* make sure to update the list in the beginning */
+ moldyn->dr_max1=moldyn->r_verlet;
+ moldyn->dr_max2=moldyn->r_verlet;
+
+ return 0;
+}
+
+int link_cell_init(t_moldyn *moldyn) {
+
+ t_linkcell *lc;
+
+ lc=&(moldyn->lc);
+
+ /* 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->subcell=malloc(lc->nx*lc->ny*lc->nz*sizeof(t_list));
+
+ link_cell_update(moldyn);
+
+ return 0;
+}
+
+int verlet_list_update(t_moldyn *moldyn) {
+
+ int i,j;
+ t_3dvec d;
+ t_atom *atom;
+
+ atom=moldyn->atom;
+
+ puts("debug: list update start");
+
+ for(i=0;i<moldyn->count;i++) {
+ list_destroy(&(atom[i].verlet));
+ for(j=0;j<moldyn->count;j++) {
+ if(i!=j) {
+ v3_sub(&d,&(atom[i].r),&(atom[j].r));
+ v3_per_bound(&d,&(moldyn->dim));
+ if(v3_norm(&d)<=moldyn->r_verlet)
+ list_add_immediate_ptr(&(atom[i].verlet),&(atom[j]));
+ }
+ }
+ }
+
+ moldyn->dr_max1=0.0;
+ moldyn->dr_max2=0.0;
+
+ puts("debug: list update end");
+
+ return 0;
+}
+
+int link_cell_update(t_moldyn *moldyn) {
+
+ int count,i,j,k;
+ int nx,ny,nz;
+ t_atom *atom;
+
+ atom=moldyn->atom;
+ nx=moldyn->lc.nx; ny=moldyn->lc.ny; nz=moldyn->lc.nz;
+
+ for(i=0;i<nx*ny*nz;i++)
+ list_destroy(&(moldyn->lc.subcell[i]));
+
+ for(count=0;count<moldyn->count;count++) {
+ for(i=0;i<nx;i++) {
+ if((atom[count].r.x>=i*moldyn->dim.x) && \
+ (atom[count].r.x<(i+1)*moldyn->dim.x))
+ break;
+ }
+ for(j=0;j<ny;j++) {
+ if((atom[count].r.y>=j*moldyn->dim.y) && \
+ (atom[count].r.y<(j+1)*moldyn->dim.y))
+ break;
+ }
+ for(k=0;k<nz;k++) {
+ if((atom[count].r.z>=k*moldyn->dim.z) && \
+ (atom[count].r.z<(k+1)*moldyn->dim.z))
+ break;
+ }
+ list_add_immediate_ptr(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
+ &(atom[count]));
+ }
+
+ return 0;
+}
+
+int verlet_list_shutdown(t_moldyn *moldyn) {
+
+ int i;
+
+ for(i=0;i<moldyn->count;i++)
+ list_shutdown(&(moldyn->atom[i].verlet));
+
+ return 0;
+}
+
+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]));
+
+ 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;
+ double rlc;
+
+ int fd;
+ char fb[128];
+
+ /* logging & visualization */
+ e=moldyn->ewrite;
+ m=moldyn->mwrite;
+ s=moldyn->swrite;
+ d=moldyn->dwrite;
+ v=moldyn->vwrite;
+
+ /* verlet list */
+ rlc=moldyn->r_verlet-moldyn->cutoff;
+
+ if(!(moldyn->lvstat&MOLDYN_LVSTAT_INITIALIZED)) {
+ printf("[moldyn] warning, lv system not initialized\n");
+ return -1;
+ }
+
+ /* create the verlet list */
+ verlet_list_update(moldyn);
+
+ /* calculate initial forces */
+ moldyn->force(moldyn);
+
+ for(i=0;i<moldyn->time_steps;i++) {
+ /* check for velet list update */
+ printf(".");
+ if(moldyn->dr_max1+moldyn->dr_max2>rlc) {
+ printf("\n");
+ verlet_list_update(moldyn);
+ }
+
+ /* integration step */
+ moldyn->integrate(moldyn);
+
+ /* check for log & visualiziation */
+ 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));
+ }
+ }
+ }
+ if(d) {
+ if(!(i%d))
+ write(moldyn->dfd,moldyn->atom,
+ moldyn->count*sizeof(t_atom));
+
+ }
+ if(v) {
+ if(!(i%v))
+ visual_atoms(moldyn->visual,i*moldyn->tau,
+ moldyn->atom,moldyn->count);
+ }
+ }
+
+ return 0;
+}
+
+/* velocity verlet */
+
+int velocity_verlet(t_moldyn *moldyn) {
+
+ int i,count;
+ double tau,tau_square,dr;
+ t_3dvec delta;
+ t_atom *atom;
+
+ atom=moldyn->atom;
+ count=moldyn->count;
+ tau=moldyn->tau;
+
+ tau_square=tau*tau;
+
+ 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_add(&(atom[i].dr),&(atom[i].dr),&delta);
+ v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
+ v3_add(&(atom[i].r),&(atom[i].r),&delta);
+ v3_add(&(atom[i].dr),&(atom[i].dr),&delta);
+ v3_per_bound(&(atom[i].r),&(moldyn->dim));
+
+ /* set maximum dr (possible list update) */
+ dr=v3_norm(&(atom[i].dr));
+ if(dr>moldyn->dr_max1) {
+ moldyn->dr_max2=moldyn->dr_max1;
+ moldyn->dr_max1=dr;
+ }
+ else if(dr>moldyn->dr_max2) moldyn->dr_max2=dr;
+
+ /* velocities */
+ v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
+ v3_add(&(atom[i].v),&(atom[i].v),&delta);
+ }
+
+ /* forces depending on chosen potential */
+ moldyn->force(moldyn);
+
+ 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;
+}
+
+
+/*
+ *
+ * potentials & corresponding forces
+ *
+ */
+
+/* harmonic oscillator potential and force */
+
+double potential_harmonic_oscillator(t_moldyn *moldyn) {
+
+ t_ho_params *params;
+ t_atom *atom;
+ int i,j;
+ int count;
+ t_3dvec distance;
+ double d,u;
+ double sc,equi_dist;
+
+ params=moldyn->pot_params;
+ atom=moldyn->atom;
+ sc=params->spring_constant;
+ equi_dist=params->equilibrium_distance;
+ count=moldyn->count;
+
+ u=0.0;
+ for(i=0;i<count;i++) {
+ for(j=0;j<i;j++) {
+ v3_sub(&distance,&(atom[i].r),&(atom[j].r));
+ d=v3_norm(&distance);
+ u+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
+ }
+ }
+
+ return u;
+}
+
+int force_harmonic_oscillator(t_moldyn *moldyn) {
+
+ t_ho_params *params;
+ int i,j,count;
+ t_atom *atom;
+ t_3dvec distance;
+ t_3dvec force;
+ double d;
+ double sc,equi_dist;
+
+ atom=moldyn->atom;
+ count=moldyn->count;
+ params=moldyn->pot_params;
+ sc=params->spring_constant;
+ equi_dist=params->equilibrium_distance;
+
+ for(i=0;i<count;i++) v3_zero(&(atom[i].f));
+
+ for(i=0;i<count;i++) {
+ for(j=0;j<i;j++) {
+ v3_sub(&distance,&(atom[i].r),&(atom[j].r));
+ v3_per_bound(&distance,&(moldyn->dim));
+ d=v3_norm(&distance);
+ if(d<=moldyn->cutoff) {
+ v3_scale(&force,&distance,
+ -sc*(1.0-(equi_dist/d)));
+ v3_add(&(atom[i].f),&(atom[i].f),&force);
+ v3_sub(&(atom[j].f),&(atom[j].f),&force);
+ }
+ }
+ }
+
+ return 0;
+}
+
+
+/* lennard jones potential & force for one sort of atoms */
+
+double potential_lennard_jones(t_moldyn *moldyn) {
+
+ t_lj_params *params;
+ t_atom *atom,*btom;
+ int i;
+ int count;
+ t_3dvec distance;
+ double d,help;
+ double u;
+ double eps,sig6,sig12;
+
+ params=moldyn->pot_params;
+ atom=moldyn->atom;
+ count=moldyn->count;
+ eps=params->epsilon4;
+ sig6=params->sigma6;
+ sig12=params->sigma12;
+
+ u=0.0;
+ for(i=0;i<count;i++) {
+ list_reset(&(atom[i].verlet));
+ if(atom[i].verlet.current==NULL) continue;
+ while(1) {
+ btom=atom[i].verlet.current->data;
+ v3_sub(&distance,&(atom[i].r),&(btom->r));
+ v3_per_bound(&distance,&(moldyn->dim));
+ d=1.0/v3_absolute_square(&distance); /* 1/r^2 */
+ help=d*d; /* 1/r^4 */
+ help*=d; /* 1/r^6 */
+ d=help*help; /* 1/r^12 */
+ u+=eps*(sig12*d-sig6*help);
+ if(list_next(&(atom[i].verlet))==L_NO_NEXT_ELEMENT)
+ break;
+ }
+ }
+
+ return u;
+}
+
+int force_lennard_jones(t_moldyn *moldyn) {
+
+ t_lj_params *params;
+ int i,count;
+ t_atom *atom,*btom;
+ t_3dvec distance;
+ t_3dvec force;
+ double d,h1,h2;
+ double eps,sig6,sig12;
+
+ atom=moldyn->atom;
+ count=moldyn->count;
+ params=moldyn->pot_params;
+ eps=params->epsilon4;
+ sig6=6*params->sigma6;
+ sig12=12*params->sigma12;
+
+ for(i=0;i<count;i++) v3_zero(&(atom[i].f));
+
+ for(i=0;i<count;i++) {
+ list_reset(&(atom[i].verlet));
+ if(atom[i].verlet.current==NULL) continue;
+ while(1) {
+ btom=atom[i].verlet.current->data;
+ v3_sub(&distance,&(atom[i].r),&(btom->r));
+ v3_per_bound(&distance,&(moldyn->dim));
+ d=v3_absolute_square(&distance);
+ if(d<=moldyn->cutoff_square) {
+ h1=1.0/d; /* 1/r^2 */
+ d=h1*h1; /* 1/r^4 */
+ h2=d*d; /* 1/r^8 */
+ h1*=d; /* 1/r^6 */
+ h1*=h2; /* 1/r^14 */
+ h1*=sig12;
+ h2*=sig6;
+ /* actually there would be a '-', *
+ * but f=-d/dr potential */
+ d=h1+h2;
+ d*=eps;
+ v3_scale(&force,&distance,d);
+ v3_add(&(atom[i].f),&(atom[i].f),&force);
+ //v3_sub(&(atom[j].f),&(atom[j].f),&force);
+ }
+ if(list_next(&(atom[i].verlet))==L_NO_NEXT_ELEMENT)
+ break;
+ }
+ }
+
+ return 0;
+}
+