started potentials
[physik/posic.git] / moldyn.c
index b507d65..ecba8ba 100644 (file)
--- a/moldyn.c
+++ b/moldyn.c
@@ -87,25 +87,26 @@ int thermal_init(t_atom *atom,t_random *random,int count,double t) {
        /* gaussian distribution of velocities */
        v3_zero(&p_total);
        for(i=0;i<count;i++) {
-               sigma=sqrt(2.0*K_BOLTZMANN*t/atom[count].mass);
+               sigma=sqrt(2.0*K_BOLTZMANN*t/atom[i].mass);
                /* x direction */
                v=sigma*rand_get_gauss(random);
-               atom[count].v.x=v;
-               p_total.x+=atom[count].mass*v;
+               atom[i].v.x=v;
+               p_total.x+=atom[i].mass*v;
                /* y direction */
                v=sigma*rand_get_gauss(random);
-               atom[count].v.y=v;
-               p_total.x+=atom[count].mass*v;
+               atom[i].v.y=v;
+               p_total.y+=atom[i].mass*v;
                /* z direction */
                v=sigma*rand_get_gauss(random);
-               atom[count].v.z=v;
-               p_total.x+=atom[count].mass*v;
+               atom[i].v.z=v;
+               p_total.z+=atom[i].mass*v;
        }
 
        /* zero total momentum */
+       v3_scale(&p_total,&p_total,1.0/count);
        for(i=0;i<count;i++) {
-               v3_scale(&delta,&p_total,1.0/atom[count].mass);
-               v3_sub(&(atom[count].v),&(atom[count].v),&delta);
+               v3_scale(&delta,&p_total,1.0/atom[i].mass);
+               v3_sub(&(atom[i].v),&(atom[i].v),&delta);
        }
 
        /* velocity scaling */
@@ -124,10 +125,10 @@ int scale_velocity(t_atom *atom,int count,double t) {
         */
        e=0.0;
        for(i=0;i<count;i++)
-               e+=0.5*atom[count].mass*v3_absolute_square(&(atom[count].v));
+               e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
        c=sqrt((2.0*e)/(3.0*count*K_BOLTZMANN*t));
        for(i=0;i<count;i++)
-               v3_scale(&(atom[count].v),&(atom[count].v),(1.0/c));
+               v3_scale(&(atom[i].v),&(atom[i].v),(1.0/c));
 
        return 0;
 }
@@ -139,9 +140,58 @@ double get_e_kin(t_atom *atom,int count) {
 
        e=0.0;
 
-       for(i=0;i<count;i++)
-               e+=0.5*atom[count].mass*v3_absolute_square(&(atom[count].v));
+       for(i=0;i<count;i++) {
+               e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
+       }
 
        return e;
 }
 
+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 potential_lennard_jones_2(t_moldyn *moldyn,void *ptr) {
+
+       t_lj_params *params;
+       t_atom *atom;
+       int i,j;
+       int count;
+       t_3dvec distance;
+       double d,help;
+       double u;
+       double eps,sig6,sig12;
+
+       params=ptr;
+       atom=moldyn->atom;
+       count=moldyn->count;
+       eps=params->epsilon;
+       sig6=params->sigma6;
+       sig12=params->sigma12;
+
+       u=0.0;
+       for(i=0;i<count;i++) {
+               for(j=0;j<i;j++) {
+                       v3_sub(&distance,&(atom[j].r),&(atom[i].r));
+                       d=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);
+               }
+       }
+       
+       return u;
+}
+
+