2 * moldyn.c - molecular dynamics library main file
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
12 #include <sys/types.h>
20 #include "math/math.h"
21 #include "init/init.h"
22 #include "random/random.h"
23 #include "visual/visual.h"
25 int moldyn_usage(char **argv) {
27 printf("\n%s usage:\n\n",argv[0]);
28 printf("--- general options ---\n");
29 printf("-E <steps> <file> (log total energy)\n");
30 printf("-M <steps> <file> (log total momentum)\n");
31 printf("-D <steps> <file> (dump total information)\n");
32 printf("-S <steps> <filebase> (single save file)\n");
33 printf("-V <steps> <filebase> (rasmol file)\n");
34 printf("--- physics options ---\n");
35 printf("-T <temperature> [K] (%f)\n",MOLDYN_TEMP);
36 printf("-t <timestep tau> [s] (%.15f)\n",MOLDYN_TAU);
37 printf("-R <runs> (%d)\n",MOLDYN_RUNS);
43 int moldyn_parse_argv(t_moldyn *moldyn,int argc,char **argv) {
47 memset(moldyn,0,sizeof(t_moldyn));
50 moldyn->t=MOLDYN_TEMP;
51 moldyn->tau=MOLDYN_TAU;
52 moldyn->time_steps=MOLDYN_RUNS;
59 moldyn->ewrite=atoi(argv[++i]);
60 strncpy(moldyn->efb,argv[++i],64);
63 moldyn->mwrite=atoi(argv[++i]);
64 strncpy(moldyn->mfb,argv[++i],64);
67 moldyn->dwrite=atoi(argv[++i]);
68 strncpy(moldyn->dfb,argv[++i],64);
71 moldyn->swrite=atoi(argv[++i]);
72 strncpy(moldyn->sfb,argv[++i],64);
75 moldyn->vwrite=atoi(argv[++i]);
76 strncpy(moldyn->vfb,argv[++i],64);
79 moldyn->t=atof(argv[++i]);
82 moldyn->tau=atof(argv[++i]);
85 moldyn->time_steps=atoi(argv[++i]);
88 printf("unknown option %s\n",argv[i]);
101 int moldyn_log_init(t_moldyn *moldyn,void *v) {
109 moldyn->efd=open(moldyn->efb,O_WRONLY|O_CREAT|O_TRUNC);
111 perror("[moldyn] efd open");
114 dprintf(moldyn->efd,"# moldyn total energy logfile\n");
115 moldyn->lvstat|=MOLDYN_LVSTAT_TOTAL_E;
119 moldyn->mfd=open(moldyn->mfb,O_WRONLY|O_CREAT|O_TRUNC);
121 perror("[moldyn] mfd open");
124 dprintf(moldyn->mfd,"# moldyn total momentum logfile\n");
125 moldyn->lvstat|=MOLDYN_LVSTAT_TOTAL_M;
129 moldyn->lvstat|=MOLDYN_LVSTAT_SAVE;
132 moldyn->dfd=open(moldyn->dfb,O_WRONLY|O_CREAT|O_TRUNC);
134 perror("[moldyn] dfd open");
137 write(moldyn->dfd,moldyn,sizeof(t_moldyn));
138 moldyn->lvstat|=MOLDYN_LVSTAT_DUMP;
141 if((moldyn->vwrite)&&(vis)) {
143 visual_init(vis,moldyn->vfb);
144 moldyn->lvstat|=MOLDYN_LVSTAT_VISUAL;
147 moldyn->lvstat|=MOLDYN_LVSTAT_INITIALIZED;
152 int moldyn_shutdown(t_moldyn *moldyn) {
154 if(moldyn->efd) close(moldyn->efd);
155 if(moldyn->mfd) close(moldyn->efd);
156 if(moldyn->dfd) close(moldyn->efd);
157 if(moldyn->visual) visual_tini(moldyn->visual);
162 int create_lattice(unsigned char type,int element,double mass,double lc,
163 int a,int b,int c,t_atom **atom) {
171 if(type==FCC) count*=4;
172 if(type==DIAMOND) count*=8;
174 *atom=malloc(count*sizeof(t_atom));
176 perror("malloc (atoms)");
184 ret=fcc_init(a,b,c,lc,*atom,&origin);
187 ret=diamond_init(a,b,c,lc,*atom,&origin);
190 printf("unknown lattice type (%02x)\n",type);
196 printf("ok, there is something wrong ...\n");
197 printf("calculated -> %d atoms\n",count);
198 printf("created -> %d atoms\n",ret);
203 (*atom)[count-1].element=element;
204 (*atom)[count-1].mass=mass;
211 int destroy_lattice(t_atom *atom) {
218 int thermal_init(t_moldyn *moldyn,t_random *random,int count) {
221 * - gaussian distribution of velocities
222 * - zero total momentum
223 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
228 t_3dvec p_total,delta;
233 /* gaussian distribution of velocities */
235 for(i=0;i<count;i++) {
236 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t/atom[i].mass);
238 v=sigma*rand_get_gauss(random);
240 p_total.x+=atom[i].mass*v;
242 v=sigma*rand_get_gauss(random);
244 p_total.y+=atom[i].mass*v;
246 v=sigma*rand_get_gauss(random);
248 p_total.z+=atom[i].mass*v;
251 /* zero total momentum */
252 v3_scale(&p_total,&p_total,1.0/count);
253 for(i=0;i<count;i++) {
254 v3_scale(&delta,&p_total,1.0/atom[i].mass);
255 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
258 /* velocity scaling */
259 scale_velocity(moldyn,count);
264 int scale_velocity(t_moldyn *moldyn,int count) {
273 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
277 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
278 c=sqrt((2.0*e)/(3.0*count*K_BOLTZMANN*moldyn->t));
280 v3_scale(&(atom[i].v),&(atom[i].v),(1.0/c));
285 double get_e_kin(t_atom *atom,int count) {
292 for(i=0;i<count;i++) {
293 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
299 double get_e_pot(t_moldyn *moldyn) {
301 return(moldyn->potential(moldyn));
304 double get_total_energy(t_moldyn *moldyn) {
308 e=get_e_kin(moldyn->atom,moldyn->count);
309 e+=get_e_pot(moldyn);
314 t_3dvec get_total_p(t_atom *atom, int count) {
320 for(i=0;i<count;i++) {
321 v3_scale(&p,&(atom[i].v),atom[i].mass);
322 v3_add(&p_total,&p_total,&p);
328 double estimate_time_step(t_moldyn *moldyn,double nn_dist,double t) {
332 tau=0.05*nn_dist/(sqrt(3.0*K_BOLTZMANN*t/moldyn->atom[0].mass));
335 printf("[moldyn] warning: time step (%f > %.15f)\n",
344 * 'integration of newtons equation' - algorithms
348 /* start the integration */
350 int moldyn_integrate(t_moldyn *moldyn) {
353 unsigned int e,m,s,d,v;
365 if(!(moldyn->lvstat&MOLDYN_LVSTAT_INITIALIZED)) {
366 printf("[moldyn] warning, lv system not initialized\n");
370 /* calculate initial forces */
371 moldyn->force(moldyn);
373 for(i=0;i<moldyn->time_steps;i++) {
374 /* integration step */
375 moldyn->integrate(moldyn);
377 /* check for log & visualiziation */
381 "%.15f %.45f\n",i*moldyn->tau,
382 get_total_energy(moldyn));
386 p=get_total_p(moldyn->atom,moldyn->count);
388 "%.15f %.45f\n",i*moldyn->tau,
394 snprintf(fb,128,"%s-%f-%.15f.save",moldyn->sfb,
395 moldyn->t,i*moldyn->tau);
396 fd=open(fb,O_WRONLY|O_TRUNC|O_CREAT);
397 if(fd<0) perror("[moldyn] save fd open");
399 write(fd,moldyn,sizeof(t_moldyn));
400 write(fd,moldyn->atom,
401 moldyn->count*sizeof(t_atom));
407 write(moldyn->dfd,moldyn->atom,
408 moldyn->count*sizeof(t_atom));
413 visual_atoms(moldyn->visual,i*moldyn->tau,
414 moldyn->atom,moldyn->count);
421 /* velocity verlet */
423 int velocity_verlet(t_moldyn *moldyn) {
426 double tau,tau_square;
436 for(i=0;i<count;i++) {
438 v3_scale(&delta,&(atom[i].v),tau);
439 v3_add(&(atom[i].r),&(atom[i].r),&delta);
440 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
441 v3_add(&(atom[i].r),&(atom[i].r),&delta);
442 v3_per_bound(&(atom[i].r),&(moldyn->dim));
445 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
446 v3_add(&(atom[i].v),&(atom[i].v),&delta);
449 /* forces depending on chosen potential */
450 moldyn->force(moldyn);
452 for(i=0;i<count;i++) {
453 /* again velocities */
454 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
455 v3_add(&(atom[i].v),&(atom[i].v),&delta);
464 * potentials & corresponding forces
468 /* harmonic oscillator potential and force */
470 double potential_harmonic_oscillator(t_moldyn *moldyn) {
480 params=moldyn->pot_params;
482 sc=params->spring_constant;
483 equi_dist=params->equilibrium_distance;
487 for(i=0;i<count;i++) {
489 v3_sub(&distance,&(atom[i].r),&(atom[j].r));
490 d=v3_norm(&distance);
491 u+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
498 int force_harmonic_oscillator(t_moldyn *moldyn) {
510 params=moldyn->pot_params;
511 sc=params->spring_constant;
512 equi_dist=params->equilibrium_distance;
514 for(i=0;i<count;i++) v3_zero(&(atom[i].f));
516 for(i=0;i<count;i++) {
518 v3_sub(&distance,&(atom[i].r),&(atom[j].r));
519 v3_per_bound(&distance,&(moldyn->dim));
520 d=v3_norm(&distance);
521 if(d<=moldyn->cutoff) {
522 v3_scale(&force,&distance,
523 -sc*(1.0-(equi_dist/d)));
524 v3_add(&(atom[i].f),&(atom[i].f),&force);
525 v3_sub(&(atom[j].f),&(atom[j].f),&force);
534 /* lennard jones potential & force for one sort of atoms */
536 double potential_lennard_jones(t_moldyn *moldyn) {
545 double eps,sig6,sig12;
547 params=moldyn->pot_params;
550 eps=params->epsilon4;
552 sig12=params->sigma12;
555 for(i=0;i<count;i++) {
557 v3_sub(&distance,&(atom[i].r),&(atom[j].r));
558 d=1.0/v3_absolute_square(&distance); /* 1/r^2 */
559 help=d*d; /* 1/r^4 */
561 d=help*help; /* 1/r^12 */
562 u+=eps*(sig6*help-sig12*d);
569 int force_lennard_jones(t_moldyn *moldyn) {
577 double eps,sig6,sig12;
581 params=moldyn->pot_params;
582 eps=params->epsilon4;
583 sig6=6*params->sigma6;
584 sig12=12*params->sigma12;
586 for(i=0;i<count;i++) v3_zero(&(atom[i].f));
588 for(i=0;i<count;i++) {
590 v3_sub(&distance,&(atom[i].r),&(atom[j].r));
591 v3_per_bound(&distance,&(moldyn->dim));
592 d=v3_absolute_square(&distance);
593 if(d<=moldyn->cutoff_square) {
594 h1=1.0/d; /* 1/r^2 */
601 /* actually there would be a '-', *
602 * but f=-d/dr potential */
605 v3_scale(&force,&distance,d);
606 v3_add(&(atom[i].f),&(atom[i].f),&force);
607 v3_sub(&(atom[j].f),&(atom[j].f),&force);