b507d65354dabcf690628bc8bc31846962542fe3
[physik/posic.git] / moldyn.c
1 /*
2  * moldyn.c - molecular dynamics library main file
3  *
4  * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
5  *
6  */
7
8 #include "moldyn.h"
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <math.h>
13
14 #include "math/math.h"
15 #include "init/init.h"
16 #include "random/random.h"
17
18
19 int create_lattice(unsigned char type,int element,double mass,double lc,
20                    int a,int b,int c,t_atom **atom) {
21
22         int count;
23         int ret;
24         t_3dvec origin;
25
26         count=a*b*c;
27
28         if(type==FCC) count*=4;
29         if(type==DIAMOND) count*=8;
30
31         *atom=malloc(count*sizeof(t_atom));
32         if(*atom==NULL) {
33                 perror("malloc (atoms)");
34                 return -1;
35         }
36
37         v3_zero(&origin);
38
39         switch(type) {
40                 case FCC:
41                         ret=fcc_init(a,b,c,lc,*atom,&origin);
42                         break;
43                 case DIAMOND:
44                         ret=diamond_init(a,b,c,lc,*atom,&origin);
45                         break;
46                 default:
47                         printf("unknown lattice type (%02x)\n",type);
48                         return -1;
49         }
50
51         /* debug */
52         if(ret!=count) {
53                 printf("ok, there is something wrong ...\n");
54                 printf("calculated -> %d atoms\n",count);
55                 printf("created -> %d atoms\n",ret);
56                 return -1;
57         }
58
59         while(count) {
60                 (*atom)[count-1].element=element;
61                 (*atom)[count-1].mass=mass;
62                 count-=1;
63         }
64
65         return ret;
66 }
67
68 int destroy_lattice(t_atom *atom) {
69
70         if(atom) free(atom);
71
72         return 0;
73 }
74
75 int thermal_init(t_atom *atom,t_random *random,int count,double t) {
76
77         /*
78          * - gaussian distribution of velocities
79          * - zero total momentum
80          * - velocity scaling (E = 3/2 N k T), E: kinetic energy
81          */
82
83         int i;
84         double v,sigma;
85         t_3dvec p_total,delta;
86
87         /* gaussian distribution of velocities */
88         v3_zero(&p_total);
89         for(i=0;i<count;i++) {
90                 sigma=sqrt(2.0*K_BOLTZMANN*t/atom[count].mass);
91                 /* x direction */
92                 v=sigma*rand_get_gauss(random);
93                 atom[count].v.x=v;
94                 p_total.x+=atom[count].mass*v;
95                 /* y direction */
96                 v=sigma*rand_get_gauss(random);
97                 atom[count].v.y=v;
98                 p_total.x+=atom[count].mass*v;
99                 /* z direction */
100                 v=sigma*rand_get_gauss(random);
101                 atom[count].v.z=v;
102                 p_total.x+=atom[count].mass*v;
103         }
104
105         /* zero total momentum */
106         for(i=0;i<count;i++) {
107                 v3_scale(&delta,&p_total,1.0/atom[count].mass);
108                 v3_sub(&(atom[count].v),&(atom[count].v),&delta);
109         }
110
111         /* velocity scaling */
112         scale_velocity(atom,count,t);
113
114         return 0;
115 }
116
117 int scale_velocity(t_atom *atom,int count,double t) {
118
119         int i;
120         double e,c;
121
122         /*
123          * - velocity scaling (E = 3/2 N k T), E: kinetic energy
124          */
125         e=0.0;
126         for(i=0;i<count;i++)
127                 e+=0.5*atom[count].mass*v3_absolute_square(&(atom[count].v));
128         c=sqrt((2.0*e)/(3.0*count*K_BOLTZMANN*t));
129         for(i=0;i<count;i++)
130                 v3_scale(&(atom[count].v),&(atom[count].v),(1.0/c));
131
132         return 0;
133 }
134
135 double get_e_kin(t_atom *atom,int count) {
136
137         int i;
138         double e;
139
140         e=0.0;
141
142         for(i=0;i<count;i++)
143                 e+=0.5*atom[count].mass*v3_absolute_square(&(atom[count].v));
144
145         return e;
146 }
147