more testing
[physik/posic.git] / posic.c
diff --git a/posic.c b/posic.c
index d8491f4..f026abe 100644 (file)
--- a/posic.c
+++ b/posic.c
 /*
  * posic.c - precipitation process of silicon carbide in silicon
  *
- * author: Frank Zirkelbach <hackbard@hackdaworld.org>
+ * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
  *
  */
+
+#include <math.h>
  
-#include "posic.h"
+#include "moldyn.h"
+#include "math/math.h"
+#include "init/init.h"
+#include "visual/visual.h"
 
-#define RAND(max) (max*(0.5-(1.0*rand()/RAND_MAX+1)));
+#include "posic.h"
 
 int main(int argc,char **argv) {
 
+       t_moldyn md;
        t_atom *si;
-       //t_si *c;
-       int i,j,runs,amount_si;
-       double time;
-       int fd1,fd2;
-       unsigned char xyz[128];
-       unsigned char scr[128];
-       unsigned char ppm[128];
-
-       double tau,tau2,m,m2;
-       double deltax,deltay,deltaz,distance;
-       double deltax2,deltay2,deltaz2,tmp;
-       double lj1,lj2,lj;
-
-       /* silicon */
-       amount_si=AMOUNT_SI;
-       printf("simulating %d silicon atoms\n",amount_si);
-       si=malloc(amount_si*sizeof(t_atom));
-       if(!si) {
-               perror("silicon malloc");
-               return -1;
-       }
-       memset(si,0,amount_si*sizeof(t_atom));
-       m=SI_M; m2=2.0*m;
+       t_visual vis;
+       t_random random;
+
+       int a,b,c;
+       double e,u;
+       double help;
+       t_3dvec p;
+       int count;
+
+       t_lj_params lj;
+       t_ho_params ho;
+
+       /* parse arguments */
+       a=moldyn_parse_argv(&md,argc,argv);
+       if(a<0) return -1;
 
        /* init */
-       printf("placing silicon atoms\n");
-       for(i=0;i<amount_si;i++) {
-               si[i].x=RAND(LEN_X);
-               si[i].y=RAND(LEN_Y);
-               si[i].z=RAND(LEN_Z);
-               si[i].vx=.0;
-               si[i].vy=.0;
-               si[i].vz=.0;
-               si[i].fx=.0;
-               si[i].fy=.0;
-               si[i].fz=.0;
-       }
-
-       /* time */
-       time=.0;
-       tau=TAU;
-       tau2=tau*tau;
-
-
-       printf("starting velocity verlet: ");
-       fflush(stdout);
-
-       for(runs=0;runs<RUNS;runs++) {
-
-       /* 
-        * velocity verlet
-        *
-        * r(t+h) = r(t) + h * dr/dt|t + h^2/2m * F(t)
-        * dr/dt|(t+h) = dr/dt|t + h/2m * (F(t) + F(t+h))
+       moldyn_log_init(&md,&vis);
+       rand_init(&random,NULL,1);
+       random.status|=RAND_STAT_VERBOSE;
+
+       /* testing random numbers */
+       //for(a=0;a<1000000;a++)
+       //      printf("%f %f\n",rand_get_gauss(&random),
+       //                       rand_get_gauss(&random));
+
+       a=LEN_X;
+       b=LEN_Y;
+       c=LEN_Z;
+
+       /* set for 'bounding atoms' */
+       vis.dim.x=a*LC_SI;
+       vis.dim.y=b*LC_SI;
+       vis.dim.z=c*LC_SI;
+
+       /* init lattice
+       printf("placing silicon atoms ... ");
+       count=create_lattice(DIAMOND,SI,M_SI,LC_SI,a,b,c,&si);
+       printf("(%d) ok!\n",count); */
+       /* testing purpose */
+       count=2;
+       si=malloc(2*sizeof(t_atom));
+       si[0].r.x=0.35*sqrt(3.0)*LC_SI/2.0;
+       si[0].r.y=0;
+       si[0].r.z=0;
+       si[0].element=SI;
+       si[0].mass=M_SI;
+       si[1].r.x=-si[0].r.x;
+       si[1].r.y=0;
+       si[1].r.z=0;
+       si[1].element=SI;
+       si[1].mass=M_SI;
+       /* */
+
+       /* moldyn init (now si is a valid address) */
+       md.count=count;
+       md.atom=si;
+       md.potential=potential_lennard_jones;
+       md.force=force_lennard_jones;
+       //md.potential=potential_harmonic_oscillator;
+       //md.force=force_harmonic_oscillator;
+       md.cutoff=R_CUTOFF;
+       md.cutoff_square=(R_CUTOFF*R_CUTOFF);
+       md.pot_params=&lj;
+       //md.pot_params=&ho;
+       md.integrate=velocity_verlet;
+       //md.time_steps=RUNS;
+       //md.tau=TAU;
+       md.status=0;
+       md.visual=&vis;
+
+       printf("setting thermal fluctuations (T=%f K)\n",md.t);
+       //thermal_init(&md,&random,count);
+       for(a=0;a<count;a++) v3_zero(&(si[0].v));
+       //v3_zero(&(si[0].v));
+       //v3_zero(&(si[1].v));
+
+       /* check kinetic energy */
+
+       e=get_e_kin(si,count);
+       printf("kinetic energy: %.40f [J]\n",e);
+       printf("3/2 N k T = %.40f [J]\n",1.5*count*K_BOLTZMANN*md.t);
+
+       /* check total momentum */
+       p=get_total_p(si,count);
+       printf("total momentum: %.30f [Ns]\n",v3_norm(&p));
+
+       /* check potential energy */
+       lj.sigma6=LJ_SIGMA_SI*LJ_SIGMA_SI;
+       help=lj.sigma6*lj.sigma6;
+       lj.sigma6*=help;
+       lj.sigma12=lj.sigma6*lj.sigma6;
+       lj.epsilon=LJ_EPSILON_SI;
+
+       ho.equilibrium_distance=0.25*sqrt(3.0)*LC_SI;
+       ho.spring_constant=LJ_EPSILON_SI;
+
+       u=get_e_pot(&md);
+
+       printf("potential energy: %.40f [J]\n",u);
+       printf("total energy (1): %.40f [J]\n",e+u);
+       printf("total energy (2): %.40f [J]\n",get_total_energy(&md));
+
+       md.dim.x=a*LC_SI;
+       md.dim.y=b*LC_SI;
+       md.dim.z=c*LC_SI;
+
+       printf("estimated accurate time step: %.30f [s]\n",
+              estimate_time_step(&md,3.0,md.t));
+
+
+       /*
+        * let's do the actual md algorithm now
         *
+        * integration of newtons equations
         */
-       for(i=0;i<amount_si;i++) {
-               /* calculation of new positions r(t+h) */
-               si[i].x+=si[i].vx*tau;
-               si[i].y+=si[i].vy*tau;
-               si[i].z+=si[i].vz*tau;
-               si[i].x+=(tau2*si[i].fx/m2);
-               if(si[i].x>LX) si[i].x-=LEN_X;
-               else if(si[i].x<-LX) si[i].x+=LEN_X;
-               si[i].y+=(tau2*si[i].fy/m2);
-               if(si[i].y>LY) si[i].y-=LEN_Y;
-               else if(si[i].y<-LY) si[i].y+=LEN_Y;
-               si[i].z+=(tau2*si[i].fz/m2);
-               if(si[i].z>LZ) si[i].z-=LEN_Z;
-               else if(si[i].z<-LZ) si[i].z+=LEN_Z;
-               /* calculation of velocities v(t+h/2) */
-               si[i].vx+=(tau*si[i].fx/m2);
-               si[i].vy+=(tau*si[i].fy/m2);
-               si[i].vz+=(tau*si[i].fz/m2);
-       }
-       for(i=0;i<amount_si;i++) {
-               /* calculation of forces at new positions r(t+h) */
-               for(j=0;j<i;j++) {
-                       deltax=si[i].x-si[j].x;
-                       if(deltax>LX) deltax-=LEN_X;
-                       else if(-deltax>LX) deltax+=LEN_X;
-                       deltax2=deltax*deltax;
-                       deltay=si[i].y-si[j].y;
-                       if(deltay>LY) deltay-=LEN_Y;
-                       else if(-deltay>LY) deltay+=LEN_Y;
-                       deltay2=deltay*deltay;
-                       deltaz=si[i].z-si[j].z;
-                       if(deltaz>LZ) deltaz-=LEN_Z;
-                       else if(-deltaz>LZ) deltaz+=LEN_Z;
-                       deltaz2=deltaz*deltaz;
-                       distance=deltax2+deltay2+deltaz2;
-                       if(distance<=R2_CUTOFF) {
-                               tmp=1.0/distance; // 1/r^2
-                               lj1=tmp; // 1/r^2
-                               tmp*=tmp; // 1/r^4
-                               lj1*=tmp; // 1/r^6
-                               tmp*=tmp; // 1/r^8
-                               lj2=tmp; // 1/r^8
-                               lj1*=tmp; // 1/r^14
-                               lj1*=LJ_SIGMA_12;
-                               lj2*=LJ_SIGMA_06;
-                               lj=-2*lj1+lj2;
-                               si[i].fx=lj*deltax;
-                               si[i].fy=lj*deltay;
-                               si[i].fz=lj*deltaz;
-                               si[i].fx=-lj*deltax;
-                               si[i].fy=-lj*deltay;
-                               si[i].fz=-lj*deltaz;
-                       }
-               }
-               /* calculation of new velocities v(t+h) */
-               si[i].vx+=(tau*si[i].fx/m2);
-               si[i].vy+=(tau*si[i].fy/m2);
-               si[i].vz+=(tau*si[i].fz/m2);
-       }
-
-       if(!(runs%10)) {
-
-       /* rasmol script & xyz file */
-       sprintf(xyz,"./saves/si-%.15f.xyz",time);
-       sprintf(scr,"./saves/si-%.15f.scr",time);
-       sprintf(ppm,"./video/si-%.15f.ppm",time);
-       fd1=open(xyz,O_WRONLY|O_CREAT|O_TRUNC);
-       fd2=open(scr,O_WRONLY|O_CREAT|O_TRUNC);
-       if((fd1<0)||(fd2<0)) {
-               perror("rasmol file open");
-               return -1;
-       }
-       dprintf(fd2,"load xyz %s\n",xyz);
-       dprintf(fd2,"spacefill 200\n");
-       dprintf(fd2,"rotate x 11\n");
-       dprintf(fd2,"rotate y 13\n");
-       dprintf(fd2,"set ambient 20\n");
-       dprintf(fd2,"set specular on\n");
-       dprintf(fd2,"set write on\n");
-       dprintf(fd2,"write ppm %s\n",ppm);
-       dprintf(fd1,"%d\nsilicon\n",amount_si);
-       for(i=0;i<amount_si;i++)
-               dprintf(fd1,"Si %f %f %f %f\n",
-                       si[i].x,si[i].y,si[i].z,time);
-       close(fd1);
-       close(fd2);
-
-       }
-
-       /* increase time */
-       time+=tau;
-       printf(".");
-       fflush(stdout);
-
-       }
-
-       printf(" done\n");
-       free(si);
 
+       moldyn_integrate(&md);
+
+       printf("total energy (after integration): %.40f [J]\n",
+              get_total_energy(&md));
+
+       /* close */
+
+       rand_close(&random);
+
+       moldyn_shutdown(&md);
+       
        return 0;
 }