added post proc stuff
[physik/posic.git] / moldyn.c
index 4d73617..c130ef4 100644 (file)
--- a/moldyn.c
+++ b/moldyn.c
@@ -404,11 +404,14 @@ int moldyn_log_shutdown(t_moldyn *moldyn) {
        if(moldyn->rfd) {
                dprintf(moldyn->rfd,report_end);
                close(moldyn->rfd);
-               snprintf(sc,255,"cd %s && pdflatex report",moldyn->vlsdir);
+               snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
+                        moldyn->vlsdir);
                system(sc);
-               snprintf(sc,255,"cd %s && pdflatex report",moldyn->vlsdir);
+               snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
+                        moldyn->vlsdir);
                system(sc);
-               snprintf(sc,255,"cd %s && dvipdf report",moldyn->vlsdir);
+               snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
+                        moldyn->vlsdir);
                system(sc);
        }
        if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
@@ -421,11 +424,11 @@ int moldyn_log_shutdown(t_moldyn *moldyn) {
  */
 
 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
-                   u8 attr,u8 brand,int a,int b,int c) {
+                   u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
 
        int new,count;
        int ret;
-       t_3dvec origin;
+       t_3dvec orig;
        void *ptr;
        t_atom *atom;
 
@@ -447,24 +450,33 @@ int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
        atom=&(moldyn->atom[count]);
 
        /* no atoms on the boundaries (only reason: it looks better!) */
-       origin.x=0.5*lc;
-       origin.y=0.5*lc;
-       origin.z=0.5*lc;
+       if(!origin) {
+               orig.x=0.5*lc;
+               orig.y=0.5*lc;
+               orig.z=0.5*lc;
+       }
+       else {
+               orig.x=origin->x;
+               orig.y=origin->y;
+               orig.z=origin->z;
+       }
 
        switch(type) {
                case CUBIC:
                        set_nn_dist(moldyn,lc);
-                       ret=cubic_init(a,b,c,lc,atom,&origin);
+                       ret=cubic_init(a,b,c,lc,atom,&orig);
                        break;
                case FCC:
-                       v3_scale(&origin,&origin,0.5);
+                       if(!origin)
+                               v3_scale(&orig,&orig,0.5);
                        set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
-                       ret=fcc_init(a,b,c,lc,atom,&origin);
+                       ret=fcc_init(a,b,c,lc,atom,&orig);
                        break;
                case DIAMOND:
-                       v3_scale(&origin,&origin,0.25);
+                       if(!origin)
+                               v3_scale(&orig,&orig,0.25);
                        set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
-                       ret=diamond_init(a,b,c,lc,atom,&origin);
+                       ret=diamond_init(a,b,c,lc,atom,&orig);
                        break;
                default:
                        printf("unknown lattice type (%02x)\n",type);
@@ -794,8 +806,12 @@ double pressure_calc(t_moldyn *moldyn) {
                v+=(virial->xx+virial->yy+virial->zz);
        }
 
+       /* virial sum and mean virial */
+       moldyn->virial_sum+=v;
+       moldyn->mean_v=moldyn->virial_sum/moldyn->total_steps;
+
        /* assume up to date kinetic energy */
-       moldyn->p=2.0*moldyn->ekin+v;
+       moldyn->p=2.0*moldyn->ekin+moldyn->mean_v;
        moldyn->p/=(3.0*moldyn->volume);
        moldyn->p_sum+=moldyn->p;
        moldyn->mean_p=moldyn->p_sum/moldyn->total_steps;
@@ -814,8 +830,8 @@ double pressure_calc(t_moldyn *moldyn) {
 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
 
        t_3dvec dim,*tp;
-       double u,p;
-       double scale,dv;
+       double u_up,u_down,dv;
+       double scale,p;
        t_atom *store;
 
        /*
@@ -823,13 +839,11 @@ double thermodynamic_pressure_calc(t_moldyn *moldyn) {
         *
         * => p = - dU/dV
         *
-        * dV: dx,y,z = 0.001 x,y,z
         */
 
-       scale=1.00000000000001;
-printf("\n\nP-DEBUG:\n");
+       scale=0.00001;
+       dv=8*scale*scale*scale*moldyn->volume;
 
-       tp=&(moldyn->tp);
        store=malloc(moldyn->count*sizeof(t_atom));
        if(store==NULL) {
                printf("[moldyn] allocating store mem failed\n");
@@ -837,59 +851,44 @@ printf("\n\nP-DEBUG:\n");
        }
 
        /* save unscaled potential energy + atom/dim configuration */
-       u=moldyn->energy;
        memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
        dim=moldyn->dim;
 
-       /* derivative with respect to x direction */
-       scale_dim(moldyn,scale,TRUE,0,0);
-       scale_atoms(moldyn,scale,TRUE,0,0);
-       dv=0.00000000000001*moldyn->dim.x*moldyn->dim.y*moldyn->dim.z;
-       link_cell_shutdown(moldyn);
-       link_cell_init(moldyn,QUIET);
-       potential_force_calc(moldyn);
-       tp->x=(moldyn->energy-u)/dv;
-       p=tp->x*tp->x;
-
-       /* restore atomic configuration + dim */
-       memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
-       moldyn->dim=dim;
-
-       /* derivative with respect to y direction */
-       scale_dim(moldyn,scale,0,TRUE,0);
-       scale_atoms(moldyn,scale,0,TRUE,0);
-       dv=0.00000000000001*moldyn->dim.y*moldyn->dim.x*moldyn->dim.z;
+       /* scale up dimension and atom positions */
+       scale_dim(moldyn,SCALE_UP,scale,TRUE,TRUE,TRUE);
+       scale_atoms(moldyn,SCALE_UP,scale,TRUE,TRUE,TRUE);
        link_cell_shutdown(moldyn);
        link_cell_init(moldyn,QUIET);
        potential_force_calc(moldyn);
-       tp->y=(moldyn->energy-u)/dv;
-       p+=tp->y*tp->y;
+       u_up=moldyn->energy;
 
        /* restore atomic configuration + dim */
        memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
        moldyn->dim=dim;
 
-       /* derivative with respect to z direction */
-       scale_dim(moldyn,scale,0,0,TRUE);
-       scale_atoms(moldyn,scale,0,0,TRUE);
-       dv=0.00000000000001*moldyn->dim.z*moldyn->dim.x*moldyn->dim.y;
+       /* scale down dimension and atom positions */
+       scale_dim(moldyn,SCALE_DOWN,scale,TRUE,TRUE,TRUE);
+       scale_atoms(moldyn,SCALE_DOWN,scale,TRUE,TRUE,TRUE);
        link_cell_shutdown(moldyn);
        link_cell_init(moldyn,QUIET);
        potential_force_calc(moldyn);
-       tp->z=(moldyn->energy-u)/dv;
-       p+=tp->z*tp->z;
+       u_down=moldyn->energy;
+       
+       /* calculate pressure */
+       p=-(u_up-u_down)/dv;
+printf("-------> %.10f %.10f %f\n",u_up/EV/moldyn->count,u_down/EV/moldyn->count,p/BAR);
 
        /* restore atomic configuration + dim */
        memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
        moldyn->dim=dim;
 
        /* restore energy */
-       moldyn->energy=u;
+       potential_force_calc(moldyn);
 
        link_cell_shutdown(moldyn);
        link_cell_init(moldyn,QUIET);
 
-       return sqrt(p);
+       return p;
 }
 
 double get_pressure(t_moldyn *moldyn) {
@@ -898,12 +897,18 @@ double get_pressure(t_moldyn *moldyn) {
 
 }
 
-int scale_dim(t_moldyn *moldyn,double scale,u8 x,u8 y,u8 z) {
+int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
 
        t_3dvec *dim;
 
        dim=&(moldyn->dim);
 
+       if(dir==SCALE_UP)
+               scale=1.0+scale;
+
+       if(dir==SCALE_DOWN)
+               scale=1.0-scale;
+
        if(x) dim->x*=scale;
        if(y) dim->y*=scale;
        if(z) dim->z*=scale;
@@ -911,11 +916,17 @@ int scale_dim(t_moldyn *moldyn,double scale,u8 x,u8 y,u8 z) {
        return 0;
 }
 
-int scale_atoms(t_moldyn *moldyn,double scale,u8 x,u8 y,u8 z) {
+int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
 
        int i;
        t_3dvec *r;
 
+       if(dir==SCALE_UP)
+               scale=1.0+scale;
+
+       if(dir==SCALE_DOWN)
+               scale=1.0-scale;
+
        for(i=0;i<moldyn->count;i++) {
                r=&(moldyn->atom[i].r);
                if(x) r->x*=scale;
@@ -947,8 +958,8 @@ int scale_volume(t_moldyn *moldyn) {
 moldyn->debug=scale;
 
        /* scale the atoms and dimensions */
-       scale_atoms(moldyn,scale,TRUE,TRUE,TRUE);
-       scale_dim(moldyn,scale,TRUE,TRUE,TRUE);
+       scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
+       scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
 
        /* visualize dimensions */
        if(vdim->x!=0) {
@@ -1252,6 +1263,9 @@ int moldyn_integrate(t_moldyn *moldyn) {
 
        /* calculate initial forces */
        potential_force_calc(moldyn);
+#ifdef DEBUG
+return 0;
+#endif
 
        /* some stupid checks before we actually start calculating bullshit */
        if(moldyn->cutoff>0.5*moldyn->dim.x)
@@ -1294,7 +1308,7 @@ int moldyn_integrate(t_moldyn *moldyn) {
                temperature_calc(moldyn);
                pressure_calc(moldyn);
                //tp=thermodynamic_pressure_calc(moldyn);
-//printf("thermodynamic p: %f %f %f - %f\n",moldyn->tp.x/BAR,moldyn->tp.y/BAR,moldyn->tp.z/BAR,tp/BAR);
+//printf("thermodynamic p: %f\n",thermodynamic_pressure_calc(moldyn)/BAR);
 
                /* p/t scaling */
                if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
@@ -1374,8 +1388,9 @@ int moldyn_integrate(t_moldyn *moldyn) {
        }
 
                /* check for hooks */
-               if(sched->hook)
-                       sched->hook(moldyn,sched->hook_params);
+               if(sched->count+1<sched->total_sched)
+                       if(sched->hook)
+                               sched->hook(moldyn,sched->hook_params);
 
                /* get a new info line */
                printf("\n");
@@ -1486,7 +1501,8 @@ int potential_force_calc(t_moldyn *moldyn) {
 
                /* single particle potential/force */
                if(itom[i].attr&ATOM_ATTR_1BP)
-                       moldyn->func1b(moldyn,&(itom[i]));
+                       if(moldyn->func1b)
+                               moldyn->func1b(moldyn,&(itom[i]));
 
                if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
                        continue;
@@ -1711,7 +1727,7 @@ int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
 }
 
 /*
- * periodic boundayr checking
+ * periodic boundary checking
  */
 
 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
@@ -1793,3 +1809,133 @@ int moldyn_bc_check(t_moldyn *moldyn) {
 
        return 0;
 }
+
+/*
+ * postprocessing functions
+ */
+
+int get_line(int fd,char *line,int max) {
+
+       int count,ret;
+
+       count=0;
+
+       while(1) {
+               if(count==max) return count;
+               ret=read(fd,line+count,1);
+               if(ret<=0) return ret;
+               if(line[count]=='\n') {
+                       line[count]='\0';
+                       return count+1;
+               }
+               count+=1;
+       }
+}
+
+int calc_fluctuations(double start,double end,t_moldyn *moldyn) {
+
+       int fd;
+       int count,ret;
+       double time,pot,kin,tot;
+       double p_sum,k_sum,t_sum;
+       char buf[64];
+       char file[128+7];
+
+       printf("[moldyn] calculating energy fluctuations [eV]:\n");
+
+       snprintf(file,128+7,"%s/energy",moldyn->vlsdir);
+       fd=open(file,O_RDONLY);
+       if(fd<0) {
+               perror("[moldyn] post proc energy open");
+               return fd;
+       }
+
+       /* first calc the averages */
+       p_sum=0.0;
+       k_sum=0.0;
+       t_sum=0.0;
+       count=0;
+       while(1) {
+               ret=get_line(fd,buf,63);
+               if(ret<=0) break;
+               if(buf[0]=='#') continue;
+               sscanf(buf,"%lf %lf %lf %lf",&time,&kin,&pot,&tot);
+               if(time<start) continue;
+               if(time>end) break;
+               p_sum+=pot;
+               k_sum+=kin;
+               t_sum+=tot;
+               count+=1;
+       }
+
+       moldyn->p_m=p_sum/count;
+       moldyn->k_m=k_sum/count;
+       moldyn->t_m=t_sum/count;
+
+       /* mean square fluctuations */
+       if(lseek(fd,SEEK_SET,0)<0) {
+               perror("[moldyn] lseek");
+               return -1;
+       }
+       count=0;
+       p_sum=0.0;
+       k_sum=0.0;
+       t_sum=0.0;
+       while(1) {
+               ret=get_line(fd,buf,63);
+               if(ret<=0) break;
+               if(buf[0]=='#') continue;
+               sscanf(buf,"%lf %lf %lf %lf",&time,&kin,&pot,&tot);
+               if(time<start) continue;
+               if(time>end) break;
+               k_sum+=((kin-moldyn->k_m)*(kin-moldyn->k_m));
+               p_sum+=((pot-moldyn->p_m)*(pot-moldyn->p_m));
+               t_sum+=((tot-moldyn->t_m)*(tot-moldyn->t_m));
+               count+=1;
+       }
+
+       moldyn->dp2_m=p_sum/count;
+       moldyn->dk2_m=k_sum/count;
+       moldyn->dt2_m=t_sum/count;
+
+       printf("  averages   : %f %f %f\n",moldyn->k_m,
+                                       moldyn->p_m,
+                                       moldyn->t_m);
+       printf("  mean square: %f %f %f\n",moldyn->dk2_m,
+                                          moldyn->dp2_m,
+                                          moldyn->dt2_m);
+
+       close(fd);
+
+       return 0;
+}
+
+int get_heat_capacity(t_moldyn *moldyn) {
+
+       double temp2,mass,ighc;
+       int i;
+
+       /* (temperature average)^2 */
+       temp2=2.0*moldyn->k_m*EV/(3.0*K_BOLTZMANN);
+       printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",temp2);
+       temp2*=temp2;
+
+       /* total mass */
+       mass=0.0;
+       for(i=0;i<moldyn->count;i++)
+               mass+=moldyn->atom[i].mass;
+
+       /* ideal gas contribution */
+       ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
+       printf("  ideal gas contribution: %f\n",ighc/mass*KILOGRAM/JOULE);
+
+       moldyn->c_v_nvt=moldyn->dp2_m*moldyn->count*moldyn->count*EV/(K_BOLTZMANN*temp2)+ighc;
+       moldyn->c_v_nvt/=mass;
+       moldyn->c_v_nve=ighc/(1.0-(moldyn->dp2_m*moldyn->count*moldyn->count*EV/(ighc*K_BOLTZMANN*temp2)));
+       moldyn->c_v_nve/=mass;
+
+       printf("  NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
+       printf("  NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
+
+       return 0;
+}