nearly finished init stuff (probs with rand function!)
[physik/posic.git] / moldyn.c
index e96cdd4..b507d65 100644 (file)
--- a/moldyn.c
+++ b/moldyn.c
@@ -9,9 +9,11 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <math.h>
 
 #include "math/math.h"
 #include "init/init.h"
+#include "random/random.h"
 
 
 int create_lattice(unsigned char type,int element,double mass,double lc,
@@ -63,38 +65,83 @@ int create_lattice(unsigned char type,int element,double mass,double lc,
        return ret;
 }
 
-int thermal_init(t_atom *atom,int count,double t) {
+int destroy_lattice(t_atom *atom) {
+
+       if(atom) free(atom);
+
+       return 0;
+}
+
+int thermal_init(t_atom *atom,t_random *random,int count,double t) {
 
        /*
         * - gaussian distribution of velocities
+        * - zero total momentum
         * - velocity scaling (E = 3/2 N k T), E: kinetic energy
         */
 
        int i;
-       double e,c,v;
-
-       e=.0;
+       double v,sigma;
+       t_3dvec p_total,delta;
 
+       /* gaussian distribution of velocities */
+       v3_zero(&p_total);
        for(i=0;i<count;i++) {
+               sigma=sqrt(2.0*K_BOLTZMANN*t/atom[count].mass);
                /* x direction */
-               v=gauss_rand();
+               v=sigma*rand_get_gauss(random);
                atom[count].v.x=v;
-               e+=0.5*atom[count].mass*v*v;
+               p_total.x+=atom[count].mass*v;
                /* y direction */
-               v=gauss_rand();
+               v=sigma*rand_get_gauss(random);
                atom[count].v.y=v;
-               e+=0.5*atom[count].mass*v*v;
+               p_total.x+=atom[count].mass*v;
                /* z direction */
-               v=gauss_rand();
+               v=sigma*rand_get_gauss(random);
                atom[count].v.z=v;
-               e+=0.5*atom[count].mass*v*v;
+               p_total.x+=atom[count].mass*v;
+       }
+
+       /* zero total momentum */
+       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);
        }
 
-       c=sqrt((2.0*e)/(3.0*count*K_BOLTZMANN));
+       /* velocity scaling */
+       scale_velocity(atom,count,t);
 
+       return 0;
+}
+
+int scale_velocity(t_atom *atom,int count,double t) {
+
+       int i;
+       double e,c;
+
+       /*
+        * - velocity scaling (E = 3/2 N k T), E: kinetic energy
+        */
+       e=0.0;
+       for(i=0;i<count;i++)
+               e+=0.5*atom[count].mass*v3_absolute_square(&(atom[count].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));
 
        return 0;
 }
 
+double get_e_kin(t_atom *atom,int count) {
+
+       int i;
+       double e;
+
+       e=0.0;
+
+       for(i=0;i<count;i++)
+               e+=0.5*atom[count].mass*v3_absolute_square(&(atom[count].v));
+
+       return e;
+}
+