2 * moldyn.c - molecular dynamics library main file
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
12 #include <sys/types.h>
20 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
24 //ret=moldyn_parse_argv(moldyn,argc,argv);
25 //if(ret<0) return ret;
27 memset(moldyn,0,sizeof(t_moldyn));
29 rand_init(&(moldyn->random),NULL,1);
30 moldyn->random.status|=RAND_STAT_VERBOSE;
35 int moldyn_shutdown(t_moldyn *moldyn) {
37 printf("[moldyn] shutdown\n");
38 moldyn_log_shutdown(moldyn);
39 link_cell_shutdown(moldyn);
40 rand_close(&(moldyn->random));
46 int set_int_alg(t_moldyn *moldyn,u8 algo) {
49 case MOLDYN_INTEGRATE_VERLET:
50 moldyn->integrate=velocity_verlet;
53 printf("unknown integration algorithm: %02x\n",algo);
60 int set_cutoff(t_moldyn *moldyn,double cutoff) {
62 moldyn->cutoff=cutoff;
67 int set_temperature(t_moldyn *moldyn,double t_ref) {
74 int set_pressure(t_moldyn *moldyn,double p_ref) {
81 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
83 moldyn->pt_scale=(ptype|ttype);
90 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
104 printf("[moldyn] dimensions in A and A^2 respectively:\n");
105 printf(" x: %f\n",moldyn->dim.x);
106 printf(" y: %f\n",moldyn->dim.y);
107 printf(" z: %f\n",moldyn->dim.z);
108 printf(" volume: %f\n",moldyn->volume);
109 printf(" visualize simulation box: %s\n",visualize?"on":"off");
114 int set_nn_dist(t_moldyn *moldyn,double dist) {
121 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
124 moldyn->status|=MOLDYN_STAT_PBX;
127 moldyn->status|=MOLDYN_STAT_PBY;
130 moldyn->status|=MOLDYN_STAT_PBZ;
135 int set_potential1b(t_moldyn *moldyn,pf_func1b func,void *params) {
138 moldyn->pot1b_params=params;
143 int set_potential2b(t_moldyn *moldyn,pf_func2b func,void *params) {
146 moldyn->pot2b_params=params;
151 int set_potential2b_post(t_moldyn *moldyn,pf_func2b_post func,void *params) {
153 moldyn->func2b_post=func;
154 moldyn->pot2b_params=params;
159 int set_potential3b(t_moldyn *moldyn,pf_func3b func,void *params) {
162 moldyn->pot3b_params=params;
167 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
169 strncpy(moldyn->vlsdir,dir,127);
174 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
180 case LOG_TOTAL_ENERGY:
181 moldyn->ewrite=timer;
182 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
183 moldyn->efd=open(filename,
184 O_WRONLY|O_CREAT|O_EXCL,
187 perror("[moldyn] energy log fd open");
190 dprintf(moldyn->efd,"# total energy log file\n");
192 case LOG_TOTAL_MOMENTUM:
193 moldyn->mwrite=timer;
194 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
195 moldyn->mfd=open(filename,
196 O_WRONLY|O_CREAT|O_EXCL,
199 perror("[moldyn] momentum log fd open");
202 dprintf(moldyn->efd,"# total momentum log file\n");
205 moldyn->swrite=timer;
208 moldyn->vwrite=timer;
209 ret=visual_init(&(moldyn->vis),moldyn->vlsdir);
211 printf("[moldyn] visual init failure\n");
216 printf("[moldyn] unknown log mechanism: %02x\n",type);
223 int moldyn_log_shutdown(t_moldyn *moldyn) {
225 printf("[moldyn] log shutdown\n");
226 if(moldyn->efd) close(moldyn->efd);
227 if(moldyn->mfd) close(moldyn->mfd);
228 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
233 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
234 u8 attr,u8 bnum,int a,int b,int c) {
242 /* how many atoms do we expect */
243 if(type==FCC) count*=4;
244 if(type==DIAMOND) count*=8;
246 /* allocate space for atoms */
247 moldyn->atom=malloc(count*sizeof(t_atom));
248 if(moldyn->atom==NULL) {
249 perror("malloc (atoms)");
257 ret=fcc_init(a,b,c,lc,moldyn->atom,&origin);
260 ret=diamond_init(a,b,c,lc,moldyn->atom,&origin);
263 printf("unknown lattice type (%02x)\n",type);
269 printf("[moldyn] creating lattice failed\n");
270 printf(" amount of atoms\n");
271 printf(" - expected: %d\n",count);
272 printf(" - created: %d\n",ret);
277 printf("[moldyn] created lattice with %d atoms\n",count);
281 moldyn->atom[count].element=element;
282 moldyn->atom[count].mass=mass;
283 moldyn->atom[count].attr=attr;
284 moldyn->atom[count].bnum=bnum;
285 check_per_bound(moldyn,&(moldyn->atom[count].r));
291 int add_atom(t_moldyn *moldyn,int element,double mass,u8 bnum,u8 attr,
292 t_3dvec *r,t_3dvec *v) {
299 count=++(moldyn->count);
301 ptr=realloc(atom,count*sizeof(t_atom));
303 perror("[moldyn] realloc (add atom)");
311 atom[count-1].element=element;
312 atom[count-1].mass=mass;
313 atom[count-1].bnum=bnum;
314 atom[count-1].attr=attr;
319 int destroy_atoms(t_moldyn *moldyn) {
321 if(moldyn->atom) free(moldyn->atom);
326 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
329 * - gaussian distribution of velocities
330 * - zero total momentum
331 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
336 t_3dvec p_total,delta;
341 random=&(moldyn->random);
343 /* gaussian distribution of velocities */
345 for(i=0;i<moldyn->count;i++) {
346 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
348 v=sigma*rand_get_gauss(random);
350 p_total.x+=atom[i].mass*v;
352 v=sigma*rand_get_gauss(random);
354 p_total.y+=atom[i].mass*v;
356 v=sigma*rand_get_gauss(random);
358 p_total.z+=atom[i].mass*v;
361 /* zero total momentum */
362 v3_scale(&p_total,&p_total,1.0/moldyn->count);
363 for(i=0;i<moldyn->count;i++) {
364 v3_scale(&delta,&p_total,1.0/atom[i].mass);
365 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
368 /* velocity scaling */
369 scale_velocity(moldyn,equi_init);
374 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
384 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
387 /* get kinetic energy / temperature & count involved atoms */
390 for(i=0;i<moldyn->count;i++) {
391 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
392 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
396 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
397 else return 0; /* no atoms involved in scaling! */
399 /* (temporary) hack for e,t = 0 */
402 if(moldyn->t_ref!=0.0) {
403 thermal_init(moldyn,equi_init);
407 return 0; /* no scaling needed */
411 /* get scaling factor */
412 scale=moldyn->t_ref/moldyn->t;
416 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
417 scale=1.0+(scale-1.0)/moldyn->t_tc;
420 /* velocity scaling */
421 for(i=0;i<moldyn->count;i++) {
422 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
423 v3_scale(&(atom[i].v),&(atom[i].v),scale);
429 int scale_volume(t_moldyn *moldyn) {
439 vdim=&(moldyn->vis.dim);
442 for(i=0;i<moldyn->count;i++)
443 virial+=v3_norm(&(atom[i].virial));
445 printf("%f\n",virial);
446 /* get pressure from virial */
447 moldyn->p=moldyn->count*K_BOLTZMANN*moldyn->t-ONE_THIRD*virial;
448 moldyn->p/=moldyn->volume;
449 printf("%f\n",moldyn->p/(ATM));
452 if(moldyn->pt_scale&P_SCALE_BERENDSEN)
453 scale=3*sqrt(1-(moldyn->p_ref-moldyn->p)/moldyn->p_tc);
455 /* should actually never be used */
456 scale=pow(moldyn->p/moldyn->p_ref,1.0/3.0);
458 printf("scale = %f\n",scale);
463 if(vdim->x) vdim->x=dim->x;
464 if(vdim->y) vdim->y=dim->y;
465 if(vdim->z) vdim->z=dim->z;
466 moldyn->volume*=(scale*scale*scale);
468 /* check whether we need a new linkcell init */
469 if((dim->x/moldyn->cutoff!=lc->nx)||
470 (dim->y/moldyn->cutoff!=lc->ny)||
471 (dim->z/moldyn->cutoff!=lc->nx)) {
472 link_cell_shutdown(moldyn);
473 link_cell_init(moldyn);
480 double get_e_kin(t_moldyn *moldyn) {
488 for(i=0;i<moldyn->count;i++)
489 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
494 double get_e_pot(t_moldyn *moldyn) {
496 return moldyn->energy;
499 double update_e_kin(t_moldyn *moldyn) {
501 return(get_e_kin(moldyn));
504 double get_total_energy(t_moldyn *moldyn) {
506 return(moldyn->ekin+moldyn->energy);
509 t_3dvec get_total_p(t_moldyn *moldyn) {
518 for(i=0;i<moldyn->count;i++) {
519 v3_scale(&p,&(atom[i].v),atom[i].mass);
520 v3_add(&p_total,&p_total,&p);
526 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
530 /* nn_dist is the nearest neighbour distance */
532 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
541 /* linked list / cell method */
543 int link_cell_init(t_moldyn *moldyn) {
550 /* partitioning the md cell */
551 lc->nx=moldyn->dim.x/moldyn->cutoff;
552 lc->x=moldyn->dim.x/lc->nx;
553 lc->ny=moldyn->dim.y/moldyn->cutoff;
554 lc->y=moldyn->dim.y/lc->ny;
555 lc->nz=moldyn->dim.z/moldyn->cutoff;
556 lc->z=moldyn->dim.z/lc->nz;
558 lc->cells=lc->nx*lc->ny*lc->nz;
559 lc->subcell=malloc(lc->cells*sizeof(t_list));
561 printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
563 for(i=0;i<lc->cells;i++)
564 list_init_f(&(lc->subcell[i]));
566 link_cell_update(moldyn);
571 int link_cell_update(t_moldyn *moldyn) {
585 for(i=0;i<lc->cells;i++)
586 list_destroy_f(&(moldyn->lc.subcell[i]));
588 for(count=0;count<moldyn->count;count++) {
589 i=(atom[count].r.x+(moldyn->dim.x/2))/lc->x;
590 j=(atom[count].r.y+(moldyn->dim.y/2))/lc->y;
591 k=(atom[count].r.z+(moldyn->dim.z/2))/lc->z;
592 list_add_immediate_f(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
599 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
617 cell[0]=lc->subcell[i+j*nx+k*a];
618 for(ci=-1;ci<=1;ci++) {
625 for(cj=-1;cj<=1;cj++) {
632 for(ck=-1;ck<=1;ck++) {
639 if(!(ci|cj|ck)) continue;
641 cell[--count2]=lc->subcell[x+y*nx+z*a];
644 cell[count1++]=lc->subcell[x+y*nx+z*a];
655 int link_cell_shutdown(t_moldyn *moldyn) {
662 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
663 list_destroy_f(&(moldyn->lc.subcell[i]));
670 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
674 t_moldyn_schedule *schedule;
676 schedule=&(moldyn->schedule);
677 count=++(schedule->content_count);
679 ptr=realloc(moldyn->schedule.runs,count*sizeof(int));
681 perror("[moldyn] realloc (runs)");
684 moldyn->schedule.runs=ptr;
685 moldyn->schedule.runs[count-1]=runs;
687 ptr=realloc(schedule->tau,count*sizeof(double));
689 perror("[moldyn] realloc (tau)");
692 moldyn->schedule.tau=ptr;
693 moldyn->schedule.tau[count-1]=tau;
698 int moldyn_set_schedule_hook(t_moldyn *moldyn,void *hook,void *hook_params) {
700 moldyn->schedule.hook=hook;
701 moldyn->schedule.hook_params=hook_params;
708 * 'integration of newtons equation' - algorithms
712 /* start the integration */
714 int moldyn_integrate(t_moldyn *moldyn) {
717 unsigned int e,m,s,v;
719 t_moldyn_schedule *schedule;
725 schedule=&(moldyn->schedule);
728 /* initialize linked cell method */
729 link_cell_init(moldyn);
731 /* logging & visualization */
737 /* sqaure of some variables */
738 moldyn->tau_square=moldyn->tau*moldyn->tau;
739 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
741 /* calculate initial forces */
742 potential_force_calc(moldyn);
744 /* some stupid checks before we actually start calculating bullshit */
745 if(moldyn->cutoff>0.5*moldyn->dim.x)
746 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
747 if(moldyn->cutoff>0.5*moldyn->dim.y)
748 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
749 if(moldyn->cutoff>0.5*moldyn->dim.z)
750 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
751 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
752 if(ds>0.05*moldyn->nnd)
753 printf("[moldyn] warning: forces too high / tau too small!\n");
755 /* zero absolute time */
758 /* debugging, ignore */
761 /* executing the schedule */
762 for(sched=0;sched<moldyn->schedule.content_count;sched++) {
764 /* setting amount of runs and finite time step size */
765 moldyn->tau=schedule->tau[sched];
766 moldyn->tau_square=moldyn->tau*moldyn->tau;
767 moldyn->time_steps=schedule->runs[sched];
769 /* integration according to schedule */
771 for(i=0;i<moldyn->time_steps;i++) {
773 /* integration step */
774 moldyn->integrate(moldyn);
777 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
778 scale_velocity(moldyn,FALSE);
779 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
781 printf("going to do p scale ...\n");
782 scale_volume(moldyn);
786 /* check for log & visualization */
791 moldyn->time,update_e_kin(moldyn),
793 get_total_energy(moldyn));
797 p=get_total_p(moldyn);
799 "%f %f\n",moldyn->time,v3_norm(&p));
804 snprintf(dir,128,"%s/s-%07.f.save",
805 moldyn->vlsdir,moldyn->time);
806 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT);
807 if(fd<0) perror("[moldyn] save fd open");
809 write(fd,moldyn,sizeof(t_moldyn));
810 write(fd,moldyn->atom,
811 moldyn->count*sizeof(t_atom));
818 visual_atoms(&(moldyn->vis),moldyn->time,
819 moldyn->atom,moldyn->count);
820 printf("\rsched: %d, steps: %d, debug: %d",
821 sched,i,moldyn->debug);
826 /* increase absolute time */
827 moldyn->time+=moldyn->tau;
831 /* check for hooks */
833 schedule->hook(moldyn,schedule->hook_params);
835 /* get a new info line */
843 /* velocity verlet */
845 int velocity_verlet(t_moldyn *moldyn) {
848 double tau,tau_square;
855 tau_square=moldyn->tau_square;
857 for(i=0;i<count;i++) {
859 v3_scale(&delta,&(atom[i].v),tau);
860 v3_add(&(atom[i].r),&(atom[i].r),&delta);
861 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
862 v3_add(&(atom[i].r),&(atom[i].r),&delta);
863 //if(i==5) printf("v: %f %f %f\n",atom[i].r.x,(atom[i].r.x+moldyn->dim.x/2)/moldyn->lc.x,2*atom[i].r.x/moldyn->dim.x);
864 check_per_bound(moldyn,&(atom[i].r));
865 //if(i==5) printf("n: %f %f %f\n",atom[i].r.x,(atom[i].r.x+moldyn->dim.x/2)/moldyn->lc.x,2*atom[i].r.x/moldyn->dim.x);
868 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
869 v3_add(&(atom[i].v),&(atom[i].v),&delta);
872 //moldyn_bc_check(moldyn);
873 /* neighbour list update */
874 link_cell_update(moldyn);
876 /* forces depending on chosen potential */
877 potential_force_calc(moldyn);
879 for(i=0;i<count;i++) {
880 /* again velocities */
881 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
882 v3_add(&(atom[i].v),&(atom[i].v),&delta);
891 * potentials & corresponding forces
895 /* generic potential and force calculation */
897 int potential_force_calc(t_moldyn *moldyn) {
900 t_atom *itom,*jtom,*ktom;
902 t_list neighbour_i[27];
903 t_list neighbour_i2[27];
915 /* get energy and force of every atom */
916 for(i=0;i<count;i++) {
919 v3_zero(&(itom[i].f));
921 /* reset viral of atom i */
922 v3_zero(&(itom[i].virial));
924 /* single particle potential/force */
925 if(itom[i].attr&ATOM_ATTR_1BP)
926 moldyn->func1b(moldyn,&(itom[i]));
928 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
931 /* 2 body pair potential/force */
933 link_cell_neighbour_index(moldyn,
934 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
935 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
936 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
943 this=&(neighbour_i[j]);
946 if(this->start==NULL)
952 jtom=this->current->data;
957 if((jtom->attr&ATOM_ATTR_2BP)&
958 (itom[i].attr&ATOM_ATTR_2BP)) {
959 moldyn->func2b(moldyn,
965 /* 3 body potential/force */
967 if(!(itom[i].attr&ATOM_ATTR_3BP)||
968 !(jtom->attr&ATOM_ATTR_3BP))
971 /* copy the neighbour lists */
972 memcpy(neighbour_i2,neighbour_i,
975 /* get neighbours of i */
978 that=&(neighbour_i2[k]);
981 if(that->start==NULL)
988 ktom=that->current->data;
990 if(!(ktom->attr&ATOM_ATTR_3BP))
999 moldyn->func3b(moldyn,
1005 } while(list_next_f(that)!=\
1010 /* 2bp post function */
1011 if(moldyn->func2b_post) {
1012 moldyn->func2b_post(moldyn,
1017 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1027 * periodic boundayr checking
1030 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1041 if(moldyn->status&MOLDYN_STAT_PBX) {
1042 if(a->x>=x) a->x-=dim->x;
1043 else if(-a->x>x) a->x+=dim->x;
1045 if(moldyn->status&MOLDYN_STAT_PBY) {
1046 if(a->y>=y) a->y-=dim->y;
1047 else if(-a->y>y) a->y+=dim->y;
1049 if(moldyn->status&MOLDYN_STAT_PBZ) {
1050 if(a->z>=z) a->z-=dim->z;
1051 else if(-a->z>z) a->z+=dim->z;
1059 * example potentials
1062 /* harmonic oscillator potential and force */
1064 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1066 t_ho_params *params;
1067 t_3dvec force,distance;
1069 double sc,equi_dist;
1071 params=moldyn->pot2b_params;
1072 sc=params->spring_constant;
1073 equi_dist=params->equilibrium_distance;
1075 v3_sub(&distance,&(aj->r),&(ai->r));
1077 if(bc) check_per_bound(moldyn,&distance);
1078 d=v3_norm(&distance);
1079 if(d<=moldyn->cutoff) {
1080 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
1081 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
1082 /* f = -grad E; grad r_ij = -1 1/r_ij distance */
1083 v3_scale(&force,&distance,sc*(1.0-(equi_dist/d)));
1084 v3_add(&(ai->f),&(ai->f),&force);
1090 /* lennard jones potential & force for one sort of atoms */
1092 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1094 t_lj_params *params;
1095 t_3dvec force,distance;
1097 double eps,sig6,sig12;
1099 params=moldyn->pot2b_params;
1100 eps=params->epsilon4;
1101 sig6=params->sigma6;
1102 sig12=params->sigma12;
1104 v3_sub(&distance,&(aj->r),&(ai->r));
1105 if(bc) check_per_bound(moldyn,&distance);
1106 d=v3_absolute_square(&distance); /* 1/r^2 */
1107 if(d<=moldyn->cutoff_square) {
1108 d=1.0/d; /* 1/r^2 */
1111 h1=h2*h2; /* 1/r^12 */
1112 /* energy is eps*..., but we will add this twice ... */
1113 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
1120 v3_scale(&force,&distance,-1.0*d); /* f = - grad E */
1121 v3_add(&(ai->f),&(ai->f),&force);
1128 * tersoff potential & force for 2 sorts of atoms
1131 /* create mixed terms from parameters and set them */
1132 int tersoff_mult_complete_params(t_tersoff_mult_params *p) {
1134 printf("[moldyn] tersoff parameter completion\n");
1135 p->Smixed=sqrt(p->S[0]*p->S[1]);
1136 p->Rmixed=sqrt(p->R[0]*p->R[1]);
1137 p->Amixed=sqrt(p->A[0]*p->A[1]);
1138 p->Bmixed=sqrt(p->B[0]*p->B[1]);
1139 p->lambda_m=0.5*(p->lambda[0]+p->lambda[1]);
1140 p->mu_m=0.5*(p->mu[0]+p->mu[1]);
1142 printf("[moldyn] tersoff mult parameter info:\n");
1143 printf(" S (A) | %f | %f | %f\n",p->S[0],p->S[1],p->Smixed);
1144 printf(" R (A) | %f | %f | %f\n",p->R[0],p->R[1],p->Rmixed);
1145 printf(" A (eV) | %f | %f | %f\n",p->A[0]/EV,p->A[1]/EV,p->Amixed/EV);
1146 printf(" B (eV) | %f | %f | %f\n",p->B[0]/EV,p->B[1]/EV,p->Bmixed/EV);
1147 printf(" lambda | %f | %f | %f\n",p->lambda[0],p->lambda[1],
1149 printf(" mu | %f | %f | %f\n",p->mu[0],p->mu[1],p->mu_m);
1150 printf(" beta | %.10f | %.10f\n",p->beta[0],p->beta[1]);
1151 printf(" n | %f | %f\n",p->n[0],p->n[1]);
1152 printf(" c | %f | %f\n",p->c[0],p->c[1]);
1153 printf(" d | %f | %f\n",p->d[0],p->d[1]);
1154 printf(" h | %f | %f\n",p->h[0],p->h[1]);
1155 printf(" chi | %f \n",p->chi);
1160 /* tersoff 1 body part */
1161 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
1164 t_tersoff_mult_params *params;
1165 t_tersoff_exchange *exchange;
1168 params=moldyn->pot1b_params;
1169 exchange=&(params->exchange);
1172 * simple: point constant parameters only depending on atom i to
1173 * their right values
1176 exchange->beta_i=&(params->beta[num]);
1177 exchange->n_i=&(params->n[num]);
1178 exchange->c_i=&(params->c[num]);
1179 exchange->d_i=&(params->d[num]);
1180 exchange->h_i=&(params->h[num]);
1182 exchange->betaini=pow(*(exchange->beta_i),*(exchange->n_i));
1183 exchange->ci2=params->c[num]*params->c[num];
1184 exchange->di2=params->d[num]*params->d[num];
1185 exchange->ci2di2=exchange->ci2/exchange->di2;
1190 /* tersoff 2 body part */
1191 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1193 t_tersoff_mult_params *params;
1194 t_tersoff_exchange *exchange;
1195 t_3dvec dist_ij,force;
1197 double A,B,R,S,lambda,mu;
1204 params=moldyn->pot2b_params;
1206 exchange=&(params->exchange);
1208 /* clear 3bp and 2bp post run */
1210 exchange->run2bp_post=0;
1212 /* reset S > r > R mark */
1213 exchange->d_ij_between_rs=0;
1216 * calc of 2bp contribution of V_ij and dV_ij/ji
1218 * for Vij and dV_ij we need:
1222 * for dV_ji we need:
1223 * - f_c_ji = f_c_ij, df_c_ji = df_c_ij
1224 * - f_r_ji = f_r_ij; df_r_ji = df_r_ij
1229 v3_sub(&dist_ij,&(aj->r),&(ai->r));
1230 if(bc) check_per_bound(moldyn,&dist_ij);
1231 d_ij=v3_norm(&dist_ij);
1233 /* save for use in 3bp */
1234 exchange->d_ij=d_ij;
1235 exchange->dist_ij=dist_ij;
1243 lambda=params->lambda[num];
1252 lambda=params->lambda_m;
1254 params->exchange.chi=params->chi;
1257 /* if d_ij > S => no force & potential energy contribution */
1261 /* more constants */
1262 exchange->beta_j=&(params->beta[num]);
1263 exchange->n_j=&(params->n[num]);
1264 exchange->c_j=&(params->c[num]);
1265 exchange->d_j=&(params->d[num]);
1266 exchange->h_j=&(params->h[num]);
1268 exchange->betajnj=exchange->betaini;
1269 exchange->cj2=exchange->ci2;
1270 exchange->dj2=exchange->di2;
1271 exchange->cj2dj2=exchange->ci2di2;
1274 exchange->betajnj=pow(*(exchange->beta_j),*(exchange->n_j));
1275 exchange->cj2=params->c[num]*params->c[num];
1276 exchange->dj2=params->d[num]*params->d[num];
1277 exchange->cj2dj2=exchange->cj2/exchange->dj2;
1280 /* f_r_ij = f_r_ji, df_r_ij = df_r_ji */
1281 f_r=A*exp(-lambda*d_ij);
1282 df_r=lambda*f_r/d_ij;
1284 /* f_a, df_a calc (again, same for ij and ji) | save for later use! */
1285 exchange->f_a=-B*exp(-mu*d_ij);
1286 exchange->df_a=mu*exchange->f_a/d_ij;
1288 /* f_c, df_c calc (again, same for ij and ji) */
1290 /* f_c = 1, df_c = 0 */
1293 /* two body contribution (ij, ji) */
1294 v3_scale(&force,&dist_ij,-df_r);
1298 arg=M_PI*(d_ij-R)/s_r;
1299 f_c=0.5+0.5*cos(arg);
1300 //df_c=-0.5*sin(arg)*(M_PI/(s_r*d_ij)); /* MARK! */
1301 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
1302 /* two body contribution (ij, ji) */
1303 v3_scale(&force,&dist_ij,-df_c*f_r-df_r*f_c);
1304 /* tell 3bp that S > r > R */
1305 exchange->d_ij_between_rs=1;
1308 /* add forces of 2bp (ij, ji) contribution
1309 * dVij = dVji and we sum up both: no 1/2) */
1310 v3_add(&(ai->f),&(ai->f),&force);
1312 /* energy 2bp contribution (ij, ji) is 0.5 f_r f_c ... */
1313 moldyn->energy+=(0.5*f_r*f_c);
1315 /* save for use in 3bp */
1317 exchange->df_c=df_c;
1319 /* enable the run of 3bp function and 2bp post processing */
1321 exchange->run2bp_post=1;
1323 /* reset 3bp sums */
1324 exchange->zeta_ij=0.0;
1325 exchange->zeta_ji=0.0;
1326 v3_zero(&(exchange->dzeta_ij));
1327 v3_zero(&(exchange->dzeta_ji));
1332 /* tersoff 2 body post part */
1334 int tersoff_mult_post_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1337 * here we have to allow for the 3bp sums
1340 * - zeta_ij, dzeta_ij
1341 * - zeta_ji, dzeta_ji
1343 * to compute the 3bp contribution to:
1349 t_tersoff_mult_params *params;
1350 t_tersoff_exchange *exchange;
1355 double f_c,df_c,f_a,df_a;
1356 double chi,ni,betaini,nj,betajnj;
1359 params=moldyn->pot2b_params;
1360 exchange=&(params->exchange);
1362 /* we do not run if f_c_ij was detected to be 0! */
1363 if(!(exchange->run2bp_post))
1367 df_c=exchange->df_c;
1369 df_a=exchange->df_a;
1370 betaini=exchange->betaini;
1371 betajnj=exchange->betajnj;
1372 ni=*(exchange->n_i);
1373 nj=*(exchange->n_j);
1375 dist_ij=&(exchange->dist_ij);
1378 zeta=exchange->zeta_ij;
1380 moldyn->debug++; /* just for debugging ... */
1383 v3_scale(&force,dist_ij,df_a*b*f_c);
1386 tmp=betaini*pow(zeta,ni-1.0); /* beta^n * zeta^n-1 */
1387 b=(1+zeta*tmp); /* 1 + beta^n zeta^n */
1388 db=chi*pow(b,-1.0/(2*ni)-1); /* x(...)^(-1/2n - 1) */
1390 db*=-0.5*tmp; /* db_ij */
1391 v3_scale(&force,&(exchange->dzeta_ij),f_a*db);
1392 v3_scale(&temp,dist_ij,df_a*b);
1393 v3_add(&force,&force,&temp);
1394 v3_scale(&force,&force,f_c);
1396 v3_scale(&temp,dist_ij,df_c*b*f_a);
1397 v3_add(&force,&force,&temp);
1398 v3_scale(&force,&force,-0.5);
1401 v3_add(&(ai->f),&(ai->f),&force);
1403 /* add energy of 3bp sum */
1404 moldyn->energy+=(0.5*f_c*b*f_a);
1407 zeta=exchange->zeta_ji;
1411 v3_scale(&force,dist_ij,df_a*b*f_c);
1414 tmp=betajnj*pow(zeta,nj-1.0); /* beta^n * zeta^n-1 */
1415 b=(1+zeta*tmp); /* 1 + beta^n zeta^n */
1416 db=chi*pow(b,-1.0/(2*nj)-1); /* x(...)^(-1/2n - 1) */
1418 db*=-0.5*tmp; /* db_ij */
1419 v3_scale(&force,&(exchange->dzeta_ji),f_a*db);
1420 v3_scale(&temp,dist_ij,df_a*b);
1421 v3_add(&force,&force,&temp);
1422 v3_scale(&force,&force,f_c);
1424 v3_scale(&temp,dist_ij,df_c*b*f_a);
1425 v3_add(&force,&force,&temp);
1426 v3_scale(&force,&force,-0.5);
1429 v3_add(&(ai->f),&(ai->f),&force);
1434 /* tersoff 3 body part */
1436 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1438 t_tersoff_mult_params *params;
1439 t_tersoff_exchange *exchange;
1440 t_3dvec dist_ij,dist_ik,dist_jk;
1441 t_3dvec temp1,temp2;
1445 double d_ij,d_ik,d_jk;
1448 double f_c_ik,df_c_ik,arg;
1452 double cos_theta,d_costheta1,d_costheta2;
1453 double h_cos,d2_h_cos2;
1454 double frac,g,zeta,chi;
1458 params=moldyn->pot3b_params;
1459 exchange=&(params->exchange);
1461 if(!(exchange->run3bp))
1465 * calc of 3bp contribution of V_ij and dV_ij/ji/jk &
1466 * 2bp contribution of dV_jk
1468 * for Vij and dV_ij we still need:
1469 * - b_ij, db_ij (zeta_ij)
1470 * - f_c_ik, df_c_ik, constants_i, cos_theta_ijk, d_costheta_ijk
1472 * for dV_ji we still need:
1473 * - b_ji, db_ji (zeta_ji)
1474 * - f_c_jk, d_c_jk, constants_j, cos_theta_jik, d_costheta_jik
1476 * for dV_jk we need:
1480 * - f_c_ji, df_c_ji, constants_j, cos_theta_jki, d_costheta_jki
1488 /* dist_ij, d_ij - this is < S_ij ! */
1489 dist_ij=exchange->dist_ij;
1490 d_ij=exchange->d_ij;
1492 /* f_c_ij, df_c_ij (same for ji) */
1494 df_c=exchange->df_c;
1497 * calculate unknown values now ...
1500 /* V_ij and dV_ij stuff (in b_ij there is f_c_ik) */
1503 v3_sub(&dist_ik,&(ak->r),&(ai->r));
1504 if(bc) check_per_bound(moldyn,&dist_ik);
1505 d_ik=v3_norm(&dist_ik);
1518 /* zeta_ij/dzeta_ij contribution only for d_ik < S */
1521 /* get constants_i from exchange data */
1528 c2d2=exchange->ci2di2;
1530 /* cosine of theta_ijk by scalaproduct */
1531 rr=v3_scalar_product(&dist_ij,&dist_ik);
1537 d_costheta1=cos_theta/(d_ij*d_ij)-tmp;
1538 d_costheta2=cos_theta/(d_ik*d_ik)-tmp;
1540 /* some usefull values */
1541 h_cos=(h-cos_theta);
1542 d2_h_cos2=d2+(h_cos*h_cos);
1543 frac=c2/(d2_h_cos2);
1548 /* d_costheta_ij and dg(cos_theta) - needed in any case! */
1549 v3_scale(&temp1,&dist_ij,d_costheta1);
1550 v3_scale(&temp2,&dist_ik,d_costheta2);
1551 v3_add(&temp1,&temp1,&temp2);
1552 v3_scale(&temp1,&temp1,-2.0*frac*h_cos/d2_h_cos2); /* dg */
1554 /* f_c_ik & df_c_ik + {d,}zeta contribution */
1555 dzeta=&(exchange->dzeta_ij);
1559 // => df_c_ik=0.0; of course we do not set this!
1562 exchange->zeta_ij+=g;
1565 v3_add(dzeta,dzeta,&temp1);
1570 arg=M_PI*(d_ik-R)/s_r;
1571 f_c_ik=0.5+0.5*cos(arg);
1572 //df_c_ik=-0.5*sin(arg)*(M_PI/(s_r*d_ik)); /* MARK */
1573 df_c_ik=0.5*sin(arg)*(M_PI/(s_r*d_ik));
1576 exchange->zeta_ij+=f_c_ik*g;
1579 v3_scale(&temp1,&temp1,f_c_ik);
1580 v3_scale(&temp2,&dist_ik,g*df_c_ik);
1581 v3_add(&temp1,&temp1,&temp2);
1582 v3_add(dzeta,dzeta,&temp1);
1586 /* dV_ji stuff (in b_ji there is f_c_jk) + dV_jk stuff! */
1589 v3_sub(&dist_jk,&(ak->r),&(aj->r));
1590 if(bc) check_per_bound(moldyn,&dist_jk);
1591 d_jk=v3_norm(&dist_jk);
1610 /* zeta_ji/dzeta_ji contribution only for d_jk < S_jk */
1613 /* constants_j from exchange data */
1620 c2d2=exchange->cj2dj2;
1622 /* cosine of theta_jik by scalaproduct */
1623 rr=-v3_scalar_product(&dist_ij,&dist_jk); /* -1, as ij -> ji */
1629 d_costheta2=cos_theta/(d_ij*d_ij);
1631 /* some usefull values */
1632 h_cos=(h-cos_theta);
1633 d2_h_cos2=d2+(h_cos*h_cos);
1634 frac=c2/(d2_h_cos2);
1639 /* d_costheta_ij and dg(cos_theta) - needed in any case! */
1640 v3_scale(&temp1,&dist_jk,d_costheta1);
1641 v3_scale(&temp2,&dist_ij,-d_costheta2); /* ji -> ij => -1 */
1642 v3_add(&temp1,&temp1,&temp2);
1643 v3_scale(&temp1,&temp1,-2.0*frac*h_cos/d2_h_cos2); /* dg */
1645 /* store dg in temp2 and use it for dVjk later */
1646 v3_copy(&temp2,&temp1);
1648 /* f_c_jk + {d,}zeta contribution (df_c_jk = 0) */
1649 dzeta=&(exchange->dzeta_ji);
1655 exchange->zeta_ji+=g;
1658 v3_add(dzeta,dzeta,&temp1);
1663 arg=M_PI*(d_jk-R)/s_r;
1664 f_c_jk=0.5+0.5*cos(arg);
1667 exchange->zeta_ji+=f_c_jk*g;
1670 v3_scale(&temp1,&temp1,f_c_jk);
1671 v3_add(dzeta,dzeta,&temp1);
1674 /* dV_jk stuff | add force contribution on atom i immediately */
1675 if(exchange->d_ij_between_rs) {
1677 v3_scale(&temp1,&temp2,f_c);
1678 v3_scale(&temp2,&dist_ij,df_c*g);
1679 v3_add(&temp2,&temp2,&temp1); /* -> dzeta_jk in temp2 */
1683 // dzeta_jk is simply dg, which is stored in temp2
1685 /* betajnj * zeta_jk ^ nj-1 */
1686 tmp=exchange->betajnj*pow(zeta,(n-1.0));
1687 tmp=-chi/2.0*pow((1+tmp*zeta),(-1.0/(2.0*n)-1))*tmp;
1688 v3_scale(&temp2,&temp2,tmp*B*exp(-mu*d_jk)*f_c_jk*0.5);
1689 v3_add(&(ai->f),&(ai->f),&temp2); /* -1 skipped in f_a calc ^ */
1690 /* scaled with 0.5 ^ */
1699 * debugging / critical check functions
1702 int moldyn_bc_check(t_moldyn *moldyn) {
1715 for(i=0;i<moldyn->count;i++) {
1716 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
1717 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
1718 i,atom[i].r.x,dim->x/2);
1719 printf("diagnostic:\n");
1720 printf("-----------\natom.r.x:\n");
1722 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
1725 ((byte)&(1<<k))?1:0,
1728 printf("---------------\nx=dim.x/2:\n");
1730 memcpy(&byte,(u8 *)(&x)+j,1);
1733 ((byte)&(1<<k))?1:0,
1736 if(atom[i].r.x==x) printf("the same!\n");
1737 else printf("different!\n");
1739 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
1740 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
1741 i,atom[i].r.y,dim->y/2);
1742 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
1743 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
1744 i,atom[i].r.z,dim->z/2);
1751 * lattice creation functions
1754 /* fcc lattice init */
1755 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
1768 if(origin) v3_copy(&o,origin);
1771 /* construct the basis */
1774 if(i!=j) help[j]=0.5*lc;
1777 v3_set(&basis[i],help);
1783 /* fill up the room */
1790 v3_copy(&(atom[count].r),&r);
1791 atom[count].element=1;
1794 v3_add(&n,&r,&basis[i]);
1798 v3_copy(&(atom[count].r),&n);
1809 /* coordinate transformation */
1814 for(i=0;i<count;i++)
1815 v3_sub(&(atom[i].r),&(atom[i].r),&n);
1820 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
1825 count=fcc_init(a,b,c,lc,atom,origin);
1831 if(origin) v3_add(&o,&o,origin);
1833 count+=fcc_init(a,b,c,lc,&atom[count],&o);