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"
24 #include "list/list.h"
26 int moldyn_usage(char **argv) {
28 printf("\n%s usage:\n\n",argv[0]);
29 printf("--- general options ---\n");
30 printf("-E <steps> <file> (log total energy)\n");
31 printf("-M <steps> <file> (log total momentum)\n");
32 printf("-D <steps> <file> (dump total information)\n");
33 printf("-S <steps> <filebase> (single save file)\n");
34 printf("-V <steps> <filebase> (rasmol file)\n");
35 printf("--- physics options ---\n");
36 printf("-T <temperature> [K] (%f)\n",MOLDYN_TEMP);
37 printf("-t <timestep tau> [s] (%.15f)\n",MOLDYN_TAU);
38 printf("-R <runs> (%d)\n",MOLDYN_RUNS);
44 int moldyn_parse_argv(t_moldyn *moldyn,int argc,char **argv) {
48 memset(moldyn,0,sizeof(t_moldyn));
51 moldyn->t=MOLDYN_TEMP;
52 moldyn->tau=MOLDYN_TAU;
53 moldyn->time_steps=MOLDYN_RUNS;
60 moldyn->ewrite=atoi(argv[++i]);
61 strncpy(moldyn->efb,argv[++i],64);
64 moldyn->mwrite=atoi(argv[++i]);
65 strncpy(moldyn->mfb,argv[++i],64);
68 moldyn->dwrite=atoi(argv[++i]);
69 strncpy(moldyn->dfb,argv[++i],64);
72 moldyn->swrite=atoi(argv[++i]);
73 strncpy(moldyn->sfb,argv[++i],64);
76 moldyn->vwrite=atoi(argv[++i]);
77 strncpy(moldyn->vfb,argv[++i],64);
80 moldyn->t=atof(argv[++i]);
83 moldyn->tau=atof(argv[++i]);
86 moldyn->time_steps=atoi(argv[++i]);
89 printf("unknown option %s\n",argv[i]);
102 int moldyn_log_init(t_moldyn *moldyn,void *v) {
110 moldyn->efd=open(moldyn->efb,O_WRONLY|O_CREAT|O_TRUNC);
112 perror("[moldyn] efd open");
115 dprintf(moldyn->efd,"# moldyn total energy logfile\n");
116 moldyn->lvstat|=MOLDYN_LVSTAT_TOTAL_E;
120 moldyn->mfd=open(moldyn->mfb,O_WRONLY|O_CREAT|O_TRUNC);
122 perror("[moldyn] mfd open");
125 dprintf(moldyn->mfd,"# moldyn total momentum logfile\n");
126 moldyn->lvstat|=MOLDYN_LVSTAT_TOTAL_M;
130 moldyn->lvstat|=MOLDYN_LVSTAT_SAVE;
133 moldyn->dfd=open(moldyn->dfb,O_WRONLY|O_CREAT|O_TRUNC);
135 perror("[moldyn] dfd open");
138 write(moldyn->dfd,moldyn,sizeof(t_moldyn));
139 moldyn->lvstat|=MOLDYN_LVSTAT_DUMP;
142 if((moldyn->vwrite)&&(vis)) {
144 visual_init(vis,moldyn->vfb);
145 moldyn->lvstat|=MOLDYN_LVSTAT_VISUAL;
148 moldyn->lvstat|=MOLDYN_LVSTAT_INITIALIZED;
153 int moldyn_shutdown(t_moldyn *moldyn) {
155 if(moldyn->efd) close(moldyn->efd);
156 if(moldyn->mfd) close(moldyn->efd);
157 if(moldyn->dfd) close(moldyn->efd);
158 if(moldyn->visual) visual_tini(moldyn->visual);
163 int create_lattice(unsigned char type,int element,double mass,double lc,
164 int a,int b,int c,t_atom **atom) {
172 if(type==FCC) count*=4;
173 if(type==DIAMOND) count*=8;
175 *atom=malloc(count*sizeof(t_atom));
177 perror("malloc (atoms)");
185 ret=fcc_init(a,b,c,lc,*atom,&origin);
188 ret=diamond_init(a,b,c,lc,*atom,&origin);
191 printf("unknown lattice type (%02x)\n",type);
197 printf("ok, there is something wrong ...\n");
198 printf("calculated -> %d atoms\n",count);
199 printf("created -> %d atoms\n",ret);
204 (*atom)[count-1].element=element;
205 (*atom)[count-1].mass=mass;
212 int destroy_lattice(t_atom *atom) {
219 int thermal_init(t_moldyn *moldyn,t_random *random,int count) {
222 * - gaussian distribution of velocities
223 * - zero total momentum
224 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
229 t_3dvec p_total,delta;
234 /* gaussian distribution of velocities */
236 for(i=0;i<count;i++) {
237 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t/atom[i].mass);
239 v=sigma*rand_get_gauss(random);
241 p_total.x+=atom[i].mass*v;
243 v=sigma*rand_get_gauss(random);
245 p_total.y+=atom[i].mass*v;
247 v=sigma*rand_get_gauss(random);
249 p_total.z+=atom[i].mass*v;
252 /* zero total momentum */
253 v3_scale(&p_total,&p_total,1.0/count);
254 for(i=0;i<count;i++) {
255 v3_scale(&delta,&p_total,1.0/atom[i].mass);
256 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
259 /* velocity scaling */
260 scale_velocity(moldyn,count);
265 int scale_velocity(t_moldyn *moldyn,int count) {
274 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
278 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
279 c=sqrt((2.0*e)/(3.0*count*K_BOLTZMANN*moldyn->t));
281 v3_scale(&(atom[i].v),&(atom[i].v),(1.0/c));
286 double get_e_kin(t_atom *atom,int count) {
293 for(i=0;i<count;i++) {
294 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
300 double get_e_pot(t_moldyn *moldyn) {
302 return(moldyn->potential(moldyn));
305 double get_total_energy(t_moldyn *moldyn) {
309 e=get_e_kin(moldyn->atom,moldyn->count);
310 e+=get_e_pot(moldyn);
315 t_3dvec get_total_p(t_atom *atom, int count) {
321 for(i=0;i<count;i++) {
322 v3_scale(&p,&(atom[i].v),atom[i].mass);
323 v3_add(&p_total,&p_total,&p);
329 double estimate_time_step(t_moldyn *moldyn,double nn_dist,double t) {
333 tau=0.05*nn_dist/(sqrt(3.0*K_BOLTZMANN*t/moldyn->atom[0].mass));
336 printf("[moldyn] warning: time step (%f > %.15f)\n",
348 int verlet_list_init(t_moldyn *moldyn) {
352 fd=open("/dev/null",O_WRONLY);
354 for(i=0;i<moldyn->count;i++)
355 list_init(&(moldyn->atom[i].verlet),fd);
357 moldyn->r_verlet=1.1*moldyn->cutoff;
359 sqrt(3.0*K_BOLTZMANN*moldyn->t/moldyn->atom[0].mass); */
361 printf("debug: r verlet = %.15f\n",moldyn->r_verlet);
362 printf(" r cutoff = %.15f\n",moldyn->cutoff);
363 printf(" dim = %.15f\n",moldyn->dim.x);
365 /* make sure to update the list in the beginning */
366 moldyn->dr_max1=moldyn->r_verlet;
367 moldyn->dr_max2=moldyn->r_verlet;
372 int link_cell_init(t_moldyn *moldyn) {
378 /* partitioning the md cell */
379 lc->nx=moldyn->dim.x/moldyn->cutoff;
380 lc->x=moldyn->dim.x/lc->nx;
381 lc->ny=moldyn->dim.y/moldyn->cutoff;
382 lc->y=moldyn->dim.y/lc->ny;
383 lc->nz=moldyn->dim.z/moldyn->cutoff;
384 lc->z=moldyn->dim.z/lc->nz;
386 lc->subcell=malloc(lc->nx*lc->ny*lc->nz*sizeof(t_list));
388 link_cell_update(moldyn);
393 int verlet_list_update(t_moldyn *moldyn) {
401 puts("debug: list update start");
403 for(i=0;i<moldyn->count;i++) {
404 list_destroy(&(atom[i].verlet));
405 for(j=0;j<moldyn->count;j++) {
407 v3_sub(&d,&(atom[i].r),&(atom[j].r));
408 v3_per_bound(&d,&(moldyn->dim));
409 if(v3_norm(&d)<=moldyn->r_verlet)
410 list_add_immediate_ptr(&(atom[i].verlet),&(atom[j]));
418 puts("debug: list update end");
423 int link_cell_update(t_moldyn *moldyn) {
430 nx=moldyn->lc.nx; ny=moldyn->lc.ny; nz=moldyn->lc.nz;
432 for(i=0;i<nx*ny*nz;i++)
433 list_destroy(&(moldyn->lc.subcell[i]));
435 for(count=0;count<moldyn->count;count++) {
437 if((atom[count].r.x>=i*moldyn->dim.x) && \
438 (atom[count].r.x<(i+1)*moldyn->dim.x))
442 if((atom[count].r.y>=j*moldyn->dim.y) && \
443 (atom[count].r.y<(j+1)*moldyn->dim.y))
447 if((atom[count].r.z>=k*moldyn->dim.z) && \
448 (atom[count].r.z<(k+1)*moldyn->dim.z))
451 list_add_immediate_ptr(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
458 int verlet_list_shutdown(t_moldyn *moldyn) {
462 for(i=0;i<moldyn->count;i++)
463 list_shutdown(&(moldyn->atom[i].verlet));
468 int link_cell_shutdown(t_moldyn *moldyn) {
475 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
476 list_shutdown(&(moldyn->lc.subcell[i]));
483 * 'integration of newtons equation' - algorithms
487 /* start the integration */
489 int moldyn_integrate(t_moldyn *moldyn) {
492 unsigned int e,m,s,d,v;
499 /* logging & visualization */
507 rlc=moldyn->r_verlet-moldyn->cutoff;
509 if(!(moldyn->lvstat&MOLDYN_LVSTAT_INITIALIZED)) {
510 printf("[moldyn] warning, lv system not initialized\n");
514 /* create the neighbour list */
515 //verlet_list_update(moldyn);
516 link_cell_update(moldyn);
518 /* calculate initial forces */
519 moldyn->force(moldyn);
521 for(i=0;i<moldyn->time_steps;i++) {
525 /* neighbour list update */
526 link_cell_update(moldyn);
527 //if(moldyn->dr_max1+moldyn->dr_max2>rlc) {
529 // verlet_list_update(moldyn);
532 /* integration step */
533 moldyn->integrate(moldyn);
535 /* check for log & visualiziation */
539 "%.15f %.45f\n",i*moldyn->tau,
540 get_total_energy(moldyn));
544 p=get_total_p(moldyn->atom,moldyn->count);
546 "%.15f %.45f\n",i*moldyn->tau,
552 snprintf(fb,128,"%s-%f-%.15f.save",moldyn->sfb,
553 moldyn->t,i*moldyn->tau);
554 fd=open(fb,O_WRONLY|O_TRUNC|O_CREAT);
555 if(fd<0) perror("[moldyn] save fd open");
557 write(fd,moldyn,sizeof(t_moldyn));
558 write(fd,moldyn->atom,
559 moldyn->count*sizeof(t_atom));
565 write(moldyn->dfd,moldyn->atom,
566 moldyn->count*sizeof(t_atom));
571 visual_atoms(moldyn->visual,i*moldyn->tau,
572 moldyn->atom,moldyn->count);
579 /* velocity verlet */
581 int velocity_verlet(t_moldyn *moldyn) {
584 double tau,tau_square,dr;
594 for(i=0;i<count;i++) {
596 v3_scale(&delta,&(atom[i].v),tau);
597 v3_add(&(atom[i].r),&(atom[i].r),&delta);
598 v3_add(&(atom[i].dr),&(atom[i].dr),&delta);
599 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
600 v3_add(&(atom[i].r),&(atom[i].r),&delta);
601 v3_add(&(atom[i].dr),&(atom[i].dr),&delta);
602 v3_per_bound(&(atom[i].r),&(moldyn->dim));
604 /* set maximum dr (possible list update) [obsolete] */
605 //dr=v3_norm(&(atom[i].dr));
606 //if(dr>moldyn->dr_max1) {
607 // moldyn->dr_max2=moldyn->dr_max1;
608 // moldyn->dr_max1=dr;
610 //else if(dr>moldyn->dr_max2) moldyn->dr_max2=dr;
613 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
614 v3_add(&(atom[i].v),&(atom[i].v),&delta);
617 /* forces depending on chosen potential */
618 moldyn->force(moldyn);
620 for(i=0;i<count;i++) {
621 /* again velocities */
622 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
623 v3_add(&(atom[i].v),&(atom[i].v),&delta);
632 * potentials & corresponding forces
636 /* harmonic oscillator potential and force */
638 double potential_harmonic_oscillator(t_moldyn *moldyn) {
648 params=moldyn->pot_params;
650 sc=params->spring_constant;
651 equi_dist=params->equilibrium_distance;
655 for(i=0;i<count;i++) {
657 v3_sub(&distance,&(atom[i].r),&(atom[j].r));
658 d=v3_norm(&distance);
659 u+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
666 int force_harmonic_oscillator(t_moldyn *moldyn) {
678 params=moldyn->pot_params;
679 sc=params->spring_constant;
680 equi_dist=params->equilibrium_distance;
682 for(i=0;i<count;i++) v3_zero(&(atom[i].f));
684 for(i=0;i<count;i++) {
686 v3_sub(&distance,&(atom[i].r),&(atom[j].r));
687 v3_per_bound(&distance,&(moldyn->dim));
688 d=v3_norm(&distance);
689 if(d<=moldyn->cutoff) {
690 v3_scale(&force,&distance,
691 -sc*(1.0-(equi_dist/d)));
692 v3_add(&(atom[i].f),&(atom[i].f),&force);
693 v3_sub(&(atom[j].f),&(atom[j].f),&force);
702 /* lennard jones potential & force for one sort of atoms */
704 double potential_lennard_jones(t_moldyn *moldyn) {
714 double eps,sig6,sig12;
718 params=moldyn->pot_params;
722 eps=params->epsilon4;
724 sig12=params->sigma12;
727 for(i=0;i<count;i++) {
728 /* verlet list [obsolete] */
729 //list_reset(&(atom[i].verlet));
730 //if(atom[i].verlet.current==NULL) continue;
733 ni=atom[i].r.x/lc->x;
734 nj=atom[i].r.y/lc->y;
735 nk=atom[i].r.z/lc->z;
738 btom=atom[i].verlet.current->data;
739 v3_sub(&distance,&(atom[i].r),&(btom->r));
740 v3_per_bound(&distance,&(moldyn->dim));
741 d=1.0/v3_absolute_square(&distance); /* 1/r^2 */
742 help=d*d; /* 1/r^4 */
744 d=help*help; /* 1/r^12 */
745 u+=eps*(sig12*d-sig6*help);
746 if(list_next(&(atom[i].verlet))==L_NO_NEXT_ELEMENT)
754 int force_lennard_jones(t_moldyn *moldyn) {
762 double eps,sig6,sig12;
766 params=moldyn->pot_params;
767 eps=params->epsilon4;
768 sig6=6*params->sigma6;
769 sig12=12*params->sigma12;
771 for(i=0;i<count;i++) v3_zero(&(atom[i].f));
773 for(i=0;i<count;i++) {
774 list_reset(&(atom[i].verlet));
775 if(atom[i].verlet.current==NULL) continue;
777 btom=atom[i].verlet.current->data;
778 v3_sub(&distance,&(atom[i].r),&(btom->r));
779 v3_per_bound(&distance,&(moldyn->dim));
780 d=v3_absolute_square(&distance);
781 if(d<=moldyn->cutoff_square) {
782 h1=1.0/d; /* 1/r^2 */
789 /* actually there would be a '-', *
790 * but f=-d/dr potential */
793 v3_scale(&force,&distance,d);
794 v3_add(&(atom[i].f),&(atom[i].f),&force);
795 //v3_sub(&(atom[j].f),&(atom[j].f),&force);
797 if(list_next(&(atom[i].verlet))==L_NO_NEXT_ELEMENT)