started potentials
[physik/posic.git] / moldyn.c
index ba3c1a3..ecba8ba 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,133 @@ 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[i].mass);
+               /* x direction */
+               v=sigma*rand_get_gauss(random);
+               atom[i].v.x=v;
+               p_total.x+=atom[i].mass*v;
+               /* y direction */
+               v=sigma*rand_get_gauss(random);
+               atom[i].v.y=v;
+               p_total.y+=atom[i].mass*v;
+               /* z direction */
+               v=sigma*rand_get_gauss(random);
+               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[i].mass);
+               v3_sub(&(atom[i].v),&(atom[i].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[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[i].v),&(atom[i].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[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;
+}
+
+