2 * moldyn.c - molecular dynamics library main file
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
14 #include "math/math.h"
15 #include "init/init.h"
16 #include "random/random.h"
17 #include "visual/visual.h"
20 int create_lattice(unsigned char type,int element,double mass,double lc,
21 int a,int b,int c,t_atom **atom) {
29 if(type==FCC) count*=4;
30 if(type==DIAMOND) count*=8;
32 *atom=malloc(count*sizeof(t_atom));
34 perror("malloc (atoms)");
42 ret=fcc_init(a,b,c,lc,*atom,&origin);
45 ret=diamond_init(a,b,c,lc,*atom,&origin);
48 printf("unknown lattice type (%02x)\n",type);
54 printf("ok, there is something wrong ...\n");
55 printf("calculated -> %d atoms\n",count);
56 printf("created -> %d atoms\n",ret);
61 (*atom)[count-1].element=element;
62 (*atom)[count-1].mass=mass;
69 int destroy_lattice(t_atom *atom) {
76 int thermal_init(t_atom *atom,t_random *random,int count,double t) {
79 * - gaussian distribution of velocities
80 * - zero total momentum
81 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
86 t_3dvec p_total,delta;
88 /* gaussian distribution of velocities */
90 for(i=0;i<count;i++) {
91 sigma=sqrt(2.0*K_BOLTZMANN*t/atom[i].mass);
93 v=sigma*rand_get_gauss(random);
95 p_total.x+=atom[i].mass*v;
97 v=sigma*rand_get_gauss(random);
99 p_total.y+=atom[i].mass*v;
101 v=sigma*rand_get_gauss(random);
103 p_total.z+=atom[i].mass*v;
106 /* zero total momentum */
107 v3_scale(&p_total,&p_total,1.0/count);
108 for(i=0;i<count;i++) {
109 v3_scale(&delta,&p_total,1.0/atom[i].mass);
110 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
113 /* velocity scaling */
114 scale_velocity(atom,count,t);
119 int scale_velocity(t_atom *atom,int count,double t) {
125 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
129 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
130 c=sqrt((2.0*e)/(3.0*count*K_BOLTZMANN*t));
132 v3_scale(&(atom[i].v),&(atom[i].v),(1.0/c));
137 double get_e_kin(t_atom *atom,int count) {
144 for(i=0;i<count;i++) {
145 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
151 double get_e_pot(t_moldyn *moldyn) {
153 return(moldyn->potential(moldyn));
156 double get_total_energy(t_moldyn *moldyn) {
160 e=get_e_kin(moldyn->atom,moldyn->count);
161 e+=get_e_pot(moldyn);
166 t_3dvec get_total_p(t_atom *atom, int count) {
172 for(i=0;i<count;i++) {
173 v3_scale(&p,&(atom[i].v),atom[i].mass);
174 v3_add(&p_total,&p_total,&p);
183 * 'integration of newtons equation' - algorithms
187 /* start the integration */
189 int moldyn_integrate(t_moldyn *moldyn) {
193 /* calculate initial forces */
194 moldyn->force(moldyn);
196 for(i=0;i<moldyn->time_steps;i++) {
197 /* integration step */
198 moldyn->integrate(moldyn);
200 /* check for visualiziation */
201 // to be continued ...
203 visual_atoms(moldyn->visual,i*moldyn->tau,
204 moldyn->atom,moldyn->count);
210 /* velocity verlet */
212 int velocity_verlet(t_moldyn *moldyn) {
215 double tau,tau_square;
225 for(i=0;i<count;i++) {
227 v3_scale(&delta,&(atom[i].v),tau);
228 v3_add(&(atom[i].r),&(atom[i].r),&delta);
229 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
230 v3_add(&(atom[i].r),&(atom[i].r),&delta);
231 v3_per_bound(&(atom[i].r),&(moldyn->dim));
234 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
235 v3_add(&(atom[i].r),&(atom[i].r),&delta);
238 /* forces depending on chosen potential */
239 moldyn->force(moldyn);
241 for(i=0;i<count;i++) {
242 /* again velocities */
243 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
244 v3_add(&(atom[i].v),&(atom[i].v),&delta);
253 * potentials & corresponding forces
257 /* lennard jones potential & force for one sort of atoms */
259 double potential_lennard_jones(t_moldyn *moldyn) {
268 double eps,sig6,sig12;
270 params=moldyn->pot_params;
275 sig12=params->sigma12;
278 for(i=0;i<count;i++) {
280 v3_sub(&distance,&(atom[j].r),&(atom[i].r));
281 d=1.0/v3_absolute_square(&distance); /* 1/r^2 */
282 help=d*d; /* 1/r^4 */
284 d=help*help; /* 1/r^12 */
285 u+=eps*(sig12*d-sig6*help);
292 int force_lennard_jones(t_moldyn *moldyn) {
300 double eps,sig6,sig12;
304 params=moldyn->pot_params;
307 sig12=params->sigma12;
309 for(i=0;i<count;i++) v3_zero(&(atom[i].f));
311 for(i=0;i<count;i++) {
313 v3_sub(&distance,&(atom[j].r),&(atom[i].r));
314 v3_per_bound(&distance,&(moldyn->dim));
315 d=v3_absolute_square(&distance);
316 if(d<=moldyn->cutoff_square) {
317 h1=1.0/d; /* 1/r^2 */
326 v3_scale(&force,&distance,d);
327 v3_add(&(atom[j].f),&(atom[j].f),&force);
328 v3_sub(&(atom[i].f),&(atom[i].f),&force);