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);
180 double estimate_time_step(t_moldyn *moldyn,double nn_dist,double t) {
184 tau=0.05*nn_dist/(sqrt(3.0*K_BOLTZMANN*t/moldyn->atom[0].mass));
187 printf("[moldyn] warning: time step (%f > %.15f)\n",
196 * 'integration of newtons equation' - algorithms
200 /* start the integration */
202 int moldyn_integrate(t_moldyn *moldyn) {
209 /* calculate initial forces */
210 moldyn->force(moldyn);
212 for(i=0;i<moldyn->time_steps;i++) {
213 /* integration step */
214 moldyn->integrate(moldyn);
216 /* check for visualiziation */
218 visual_atoms(moldyn->visual,i*moldyn->tau,
219 moldyn->atom,moldyn->count);
226 /* velocity verlet */
228 int velocity_verlet(t_moldyn *moldyn) {
231 double tau,tau_square;
241 for(i=0;i<count;i++) {
243 v3_scale(&delta,&(atom[i].v),tau);
244 v3_add(&(atom[i].r),&(atom[i].r),&delta);
245 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
246 v3_add(&(atom[i].r),&(atom[i].r),&delta);
247 v3_per_bound(&(atom[i].r),&(moldyn->dim));
250 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
251 v3_add(&(atom[i].v),&(atom[i].v),&delta);
254 /* forces depending on chosen potential */
255 moldyn->force(moldyn);
257 for(i=0;i<count;i++) {
258 /* again velocities */
259 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
260 v3_add(&(atom[i].v),&(atom[i].v),&delta);
269 * potentials & corresponding forces
273 /* harmonic oscillator potential and force */
275 double potential_harmonic_oscillator(t_moldyn *moldyn) {
285 params=moldyn->pot_params;
287 sc=params->spring_constant;
288 equi_dist=params->equilibrium_distance;
292 for(i=0;i<count;i++) {
294 v3_sub(&distance,&(atom[i].r),&(atom[j].r));
295 d=v3_norm(&distance);
296 u+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
303 int force_harmonic_oscillator(t_moldyn *moldyn) {
315 params=moldyn->pot_params;
316 sc=params->spring_constant;
317 equi_dist=params->equilibrium_distance;
319 for(i=0;i<count;i++) v3_zero(&(atom[i].f));
321 for(i=0;i<count;i++) {
323 v3_sub(&distance,&(atom[i].r),&(atom[j].r));
324 v3_per_bound(&distance,&(moldyn->dim));
325 d=v3_norm(&distance);
326 if(d<=moldyn->cutoff) {
327 v3_scale(&force,&distance,
328 (-sc*(1.0-(equi_dist/d))));
329 v3_add(&(atom[i].f),&(atom[i].f),&force);
330 v3_sub(&(atom[j].f),&(atom[j].f),&force);
339 /* lennard jones potential & force for one sort of atoms */
341 double potential_lennard_jones(t_moldyn *moldyn) {
350 double eps,sig6,sig12;
352 params=moldyn->pot_params;
357 sig12=params->sigma12;
360 for(i=0;i<count;i++) {
362 v3_sub(&distance,&(atom[j].r),&(atom[i].r));
363 d=1.0/v3_absolute_square(&distance); /* 1/r^2 */
364 help=d*d; /* 1/r^4 */
366 d=help*help; /* 1/r^12 */
367 u+=eps*(sig12*d-sig6*help);
374 int force_lennard_jones(t_moldyn *moldyn) {
382 double eps,sig6,sig12;
386 params=moldyn->pot_params;
389 sig12=params->sigma12;
391 for(i=0;i<count;i++) v3_zero(&(atom[i].f));
393 for(i=0;i<count;i++) {
395 v3_sub(&distance,&(atom[j].r),&(atom[i].r));
396 v3_per_bound(&distance,&(moldyn->dim));
397 d=v3_absolute_square(&distance);
398 if(d<=moldyn->cutoff_square) {
399 h1=1.0/d; /* 1/r^2 */
408 v3_scale(&force,&distance,d);
409 v3_add(&(atom[j].f),&(atom[j].f),&force);
410 v3_sub(&(atom[i].f),&(atom[i].f),&force);