nearly finished init stuff (probs with rand function!)
[physik/posic.git] / moldyn.c
index ba3c1a3..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,
@@ -42,8 +44,8 @@ int create_lattice(unsigned char type,int element,double mass,double lc,
                        ret=diamond_init(a,b,c,lc,*atom,&origin);
                        break;
                default:
-                       ret=-1;
                        printf("unknown lattice type (%02x)\n",type);
+                       return -1;
        }
 
        /* debug */
@@ -51,6 +53,7 @@ int create_lattice(unsigned char type,int element,double mass,double lc,
                printf("ok, there is something wrong ...\n");
                printf("calculated -> %d atoms\n",count);
                printf("created -> %d atoms\n",ret);
+               return -1;
        }
 
        while(count) {
@@ -62,3 +65,83 @@ int create_lattice(unsigned char type,int element,double mass,double lc,
        return ret;
 }
 
+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 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=sigma*rand_get_gauss(random);
+               atom[count].v.x=v;
+               p_total.x+=atom[count].mass*v;
+               /* y direction */
+               v=sigma*rand_get_gauss(random);
+               atom[count].v.y=v;
+               p_total.x+=atom[count].mass*v;
+               /* z direction */
+               v=sigma*rand_get_gauss(random);
+               atom[count].v.z=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);
+       }
+
+       /* 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;
+}
+