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) {
22 memset(moldyn,0,sizeof(t_moldyn));
24 rand_init(&(moldyn->random),NULL,1);
25 moldyn->random.status|=RAND_STAT_VERBOSE;
30 int moldyn_shutdown(t_moldyn *moldyn) {
32 printf("[moldyn] shutdown\n");
33 moldyn_log_shutdown(moldyn);
34 link_cell_shutdown(moldyn);
35 rand_close(&(moldyn->random));
41 int set_int_alg(t_moldyn *moldyn,u8 algo) {
44 case MOLDYN_INTEGRATE_VERLET:
45 moldyn->integrate=velocity_verlet;
48 printf("unknown integration algorithm: %02x\n",algo);
55 int set_cutoff(t_moldyn *moldyn,double cutoff) {
57 moldyn->cutoff=cutoff;
62 int set_temperature(t_moldyn *moldyn,double t_ref) {
69 int set_pressure(t_moldyn *moldyn,double p_ref) {
76 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
78 moldyn->pt_scale=(ptype|ttype);
85 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
99 printf("[moldyn] dimensions in A and A^3 respectively:\n");
100 printf(" x: %f\n",moldyn->dim.x);
101 printf(" y: %f\n",moldyn->dim.y);
102 printf(" z: %f\n",moldyn->dim.z);
103 printf(" volume: %f\n",moldyn->volume);
104 printf(" visualize simulation box: %s\n",visualize?"on":"off");
109 int set_nn_dist(t_moldyn *moldyn,double dist) {
116 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
119 moldyn->status|=MOLDYN_STAT_PBX;
122 moldyn->status|=MOLDYN_STAT_PBY;
125 moldyn->status|=MOLDYN_STAT_PBZ;
130 int set_potential1b(t_moldyn *moldyn,pf_func1b func,void *params) {
133 moldyn->pot1b_params=params;
138 int set_potential2b(t_moldyn *moldyn,pf_func2b func,void *params) {
141 moldyn->pot2b_params=params;
146 int set_potential2b_post(t_moldyn *moldyn,pf_func2b_post func,void *params) {
148 moldyn->func2b_post=func;
149 moldyn->pot2b_params=params;
154 int set_potential3b(t_moldyn *moldyn,pf_func3b func,void *params) {
157 moldyn->pot3b_params=params;
162 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
164 strncpy(moldyn->vlsdir,dir,127);
169 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
175 case LOG_TOTAL_ENERGY:
176 moldyn->ewrite=timer;
177 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
178 moldyn->efd=open(filename,
179 O_WRONLY|O_CREAT|O_EXCL,
182 perror("[moldyn] energy log fd open");
185 dprintf(moldyn->efd,"# total energy log file\n");
187 case LOG_TOTAL_MOMENTUM:
188 moldyn->mwrite=timer;
189 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
190 moldyn->mfd=open(filename,
191 O_WRONLY|O_CREAT|O_EXCL,
194 perror("[moldyn] momentum log fd open");
197 dprintf(moldyn->efd,"# total momentum log file\n");
200 moldyn->swrite=timer;
203 moldyn->vwrite=timer;
204 ret=visual_init(&(moldyn->vis),moldyn->vlsdir);
206 printf("[moldyn] visual init failure\n");
211 printf("[moldyn] unknown log mechanism: %02x\n",type);
218 int moldyn_log_shutdown(t_moldyn *moldyn) {
220 printf("[moldyn] log shutdown\n");
221 if(moldyn->efd) close(moldyn->efd);
222 if(moldyn->mfd) close(moldyn->mfd);
223 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
229 * creating lattice functions
232 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
233 u8 attr,u8 bnum,int a,int b,int c) {
241 /* how many atoms do we expect */
242 if(type==FCC) count*=4;
243 if(type==DIAMOND) count*=8;
245 /* allocate space for atoms */
246 moldyn->atom=malloc(count*sizeof(t_atom));
247 if(moldyn->atom==NULL) {
248 perror("malloc (atoms)");
256 ret=fcc_init(a,b,c,lc,moldyn->atom,&origin);
259 ret=diamond_init(a,b,c,lc,moldyn->atom,&origin);
262 printf("unknown lattice type (%02x)\n",type);
268 printf("[moldyn] creating lattice failed\n");
269 printf(" amount of atoms\n");
270 printf(" - expected: %d\n",count);
271 printf(" - created: %d\n",ret);
276 printf("[moldyn] created lattice with %d atoms\n",count);
280 moldyn->atom[count].element=element;
281 moldyn->atom[count].mass=mass;
282 moldyn->atom[count].attr=attr;
283 moldyn->atom[count].bnum=bnum;
284 check_per_bound(moldyn,&(moldyn->atom[count].r));
290 /* fcc lattice init */
291 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
304 if(origin) v3_copy(&o,origin);
307 /* construct the basis */
310 if(i!=j) help[j]=0.5*lc;
313 v3_set(&basis[i],help);
319 /* fill up the room */
326 v3_copy(&(atom[count].r),&r);
327 atom[count].element=1;
330 v3_add(&n,&r,&basis[i]);
334 v3_copy(&(atom[count].r),&n);
345 /* coordinate transformation */
351 v3_sub(&(atom[i].r),&(atom[i].r),&n);
356 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
361 count=fcc_init(a,b,c,lc,atom,origin);
367 if(origin) v3_add(&o,&o,origin);
369 count+=fcc_init(a,b,c,lc,&atom[count],&o);
374 int add_atom(t_moldyn *moldyn,int element,double mass,u8 bnum,u8 attr,
375 t_3dvec *r,t_3dvec *v) {
382 count=++(moldyn->count);
384 ptr=realloc(atom,count*sizeof(t_atom));
386 perror("[moldyn] realloc (add atom)");
394 atom[count-1].element=element;
395 atom[count-1].mass=mass;
396 atom[count-1].bnum=bnum;
397 atom[count-1].attr=attr;
402 int destroy_atoms(t_moldyn *moldyn) {
404 if(moldyn->atom) free(moldyn->atom);
409 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
412 * - gaussian distribution of velocities
413 * - zero total momentum
414 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
419 t_3dvec p_total,delta;
424 random=&(moldyn->random);
426 /* gaussian distribution of velocities */
428 for(i=0;i<moldyn->count;i++) {
429 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
431 v=sigma*rand_get_gauss(random);
433 p_total.x+=atom[i].mass*v;
435 v=sigma*rand_get_gauss(random);
437 p_total.y+=atom[i].mass*v;
439 v=sigma*rand_get_gauss(random);
441 p_total.z+=atom[i].mass*v;
444 /* zero total momentum */
445 v3_scale(&p_total,&p_total,1.0/moldyn->count);
446 for(i=0;i<moldyn->count;i++) {
447 v3_scale(&delta,&p_total,1.0/atom[i].mass);
448 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
451 /* velocity scaling */
452 scale_velocity(moldyn,equi_init);
457 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
467 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
470 /* get kinetic energy / temperature & count involved atoms */
473 for(i=0;i<moldyn->count;i++) {
474 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
475 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
479 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
480 else return 0; /* no atoms involved in scaling! */
482 /* (temporary) hack for e,t = 0 */
485 if(moldyn->t_ref!=0.0) {
486 thermal_init(moldyn,equi_init);
490 return 0; /* no scaling needed */
494 /* get scaling factor */
495 scale=moldyn->t_ref/moldyn->t;
499 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
500 scale=1.0+(scale-1.0)/moldyn->t_tc;
503 /* velocity scaling */
504 for(i=0;i<moldyn->count;i++) {
505 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
506 v3_scale(&(atom[i].v),&(atom[i].v),scale);
512 int scale_volume(t_moldyn *moldyn) {
522 vdim=&(moldyn->vis.dim);
525 for(i=0;i<moldyn->count;i++)
526 virial+=v3_norm(&(atom[i].virial));
528 printf("%f\n",virial);
529 /* get pressure from virial */
530 moldyn->p=moldyn->count*K_BOLTZMANN*moldyn->t-ONE_THIRD*virial;
531 moldyn->p/=moldyn->volume;
532 printf("%f\n",moldyn->p/(ATM));
535 if(moldyn->pt_scale&P_SCALE_BERENDSEN)
536 scale=3*sqrt(1-(moldyn->p_ref-moldyn->p)/moldyn->p_tc);
538 /* should actually never be used */
539 scale=pow(moldyn->p/moldyn->p_ref,1.0/3.0);
541 printf("scale = %f\n",scale);
546 if(vdim->x) vdim->x=dim->x;
547 if(vdim->y) vdim->y=dim->y;
548 if(vdim->z) vdim->z=dim->z;
549 moldyn->volume*=(scale*scale*scale);
551 /* check whether we need a new linkcell init */
552 if((dim->x/moldyn->cutoff!=lc->nx)||
553 (dim->y/moldyn->cutoff!=lc->ny)||
554 (dim->z/moldyn->cutoff!=lc->nx)) {
555 link_cell_shutdown(moldyn);
556 link_cell_init(moldyn);
563 double get_e_kin(t_moldyn *moldyn) {
571 for(i=0;i<moldyn->count;i++)
572 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
577 double get_e_pot(t_moldyn *moldyn) {
579 return moldyn->energy;
582 double update_e_kin(t_moldyn *moldyn) {
584 return(get_e_kin(moldyn));
587 double get_total_energy(t_moldyn *moldyn) {
589 return(moldyn->ekin+moldyn->energy);
592 t_3dvec get_total_p(t_moldyn *moldyn) {
601 for(i=0;i<moldyn->count;i++) {
602 v3_scale(&p,&(atom[i].v),atom[i].mass);
603 v3_add(&p_total,&p_total,&p);
609 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
613 /* nn_dist is the nearest neighbour distance */
615 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
624 /* linked list / cell method */
626 int link_cell_init(t_moldyn *moldyn) {
633 /* partitioning the md cell */
634 lc->nx=moldyn->dim.x/moldyn->cutoff;
635 lc->x=moldyn->dim.x/lc->nx;
636 lc->ny=moldyn->dim.y/moldyn->cutoff;
637 lc->y=moldyn->dim.y/lc->ny;
638 lc->nz=moldyn->dim.z/moldyn->cutoff;
639 lc->z=moldyn->dim.z/lc->nz;
641 lc->cells=lc->nx*lc->ny*lc->nz;
642 lc->subcell=malloc(lc->cells*sizeof(t_list));
644 printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
646 for(i=0;i<lc->cells;i++)
647 list_init_f(&(lc->subcell[i]));
649 link_cell_update(moldyn);
654 int link_cell_update(t_moldyn *moldyn) {
672 for(i=0;i<lc->cells;i++)
673 list_destroy_f(&(lc->subcell[i]));
675 for(count=0;count<moldyn->count;count++) {
676 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
677 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
678 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
679 list_add_immediate_f(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
686 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
704 cell[0]=lc->subcell[i+j*nx+k*a];
705 for(ci=-1;ci<=1;ci++) {
712 for(cj=-1;cj<=1;cj++) {
719 for(ck=-1;ck<=1;ck++) {
726 if(!(ci|cj|ck)) continue;
728 cell[--count2]=lc->subcell[x+y*nx+z*a];
731 cell[count1++]=lc->subcell[x+y*nx+z*a];
742 int link_cell_shutdown(t_moldyn *moldyn) {
749 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
750 list_destroy_f(&(moldyn->lc.subcell[i]));
757 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
761 t_moldyn_schedule *schedule;
763 schedule=&(moldyn->schedule);
764 count=++(schedule->content_count);
766 ptr=realloc(moldyn->schedule.runs,count*sizeof(int));
768 perror("[moldyn] realloc (runs)");
771 moldyn->schedule.runs=ptr;
772 moldyn->schedule.runs[count-1]=runs;
774 ptr=realloc(schedule->tau,count*sizeof(double));
776 perror("[moldyn] realloc (tau)");
779 moldyn->schedule.tau=ptr;
780 moldyn->schedule.tau[count-1]=tau;
785 int moldyn_set_schedule_hook(t_moldyn *moldyn,void *hook,void *hook_params) {
787 moldyn->schedule.hook=hook;
788 moldyn->schedule.hook_params=hook_params;
795 * 'integration of newtons equation' - algorithms
799 /* start the integration */
801 int moldyn_integrate(t_moldyn *moldyn) {
804 unsigned int e,m,s,v;
806 t_moldyn_schedule *schedule;
812 schedule=&(moldyn->schedule);
815 /* initialize linked cell method */
816 link_cell_init(moldyn);
818 /* logging & visualization */
824 /* sqaure of some variables */
825 moldyn->tau_square=moldyn->tau*moldyn->tau;
826 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
828 /* calculate initial forces */
829 potential_force_calc(moldyn);
831 /* some stupid checks before we actually start calculating bullshit */
832 if(moldyn->cutoff>0.5*moldyn->dim.x)
833 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
834 if(moldyn->cutoff>0.5*moldyn->dim.y)
835 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
836 if(moldyn->cutoff>0.5*moldyn->dim.z)
837 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
838 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
839 if(ds>0.05*moldyn->nnd)
840 printf("[moldyn] warning: forces too high / tau too small!\n");
842 /* zero absolute time */
845 /* debugging, ignore */
848 /* executing the schedule */
849 for(sched=0;sched<moldyn->schedule.content_count;sched++) {
851 /* setting amount of runs and finite time step size */
852 moldyn->tau=schedule->tau[sched];
853 moldyn->tau_square=moldyn->tau*moldyn->tau;
854 moldyn->time_steps=schedule->runs[sched];
856 /* integration according to schedule */
858 for(i=0;i<moldyn->time_steps;i++) {
860 /* integration step */
861 moldyn->integrate(moldyn);
864 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
865 scale_velocity(moldyn,FALSE);
866 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
867 scale_volume(moldyn);
869 /* check for log & visualization */
874 moldyn->time,update_e_kin(moldyn),
876 get_total_energy(moldyn));
880 p=get_total_p(moldyn);
882 "%f %f\n",moldyn->time,v3_norm(&p));
887 snprintf(dir,128,"%s/s-%07.f.save",
888 moldyn->vlsdir,moldyn->time);
889 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT);
890 if(fd<0) perror("[moldyn] save fd open");
892 write(fd,moldyn,sizeof(t_moldyn));
893 write(fd,moldyn->atom,
894 moldyn->count*sizeof(t_atom));
901 visual_atoms(&(moldyn->vis),moldyn->time,
902 moldyn->atom,moldyn->count);
903 printf("\rsched: %d, steps: %d, debug: %d",
904 sched,i,moldyn->debug);
909 /* increase absolute time */
910 moldyn->time+=moldyn->tau;
914 /* check for hooks */
916 schedule->hook(moldyn,schedule->hook_params);
918 /* get a new info line */
926 /* velocity verlet */
928 int velocity_verlet(t_moldyn *moldyn) {
931 double tau,tau_square;
938 tau_square=moldyn->tau_square;
940 for(i=0;i<count;i++) {
942 v3_scale(&delta,&(atom[i].v),tau);
943 v3_add(&(atom[i].r),&(atom[i].r),&delta);
944 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
945 v3_add(&(atom[i].r),&(atom[i].r),&delta);
946 check_per_bound(moldyn,&(atom[i].r));
949 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
950 v3_add(&(atom[i].v),&(atom[i].v),&delta);
953 /* neighbour list update */
954 link_cell_update(moldyn);
956 /* forces depending on chosen potential */
957 potential_force_calc(moldyn);
959 for(i=0;i<count;i++) {
960 /* again velocities */
961 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
962 v3_add(&(atom[i].v),&(atom[i].v),&delta);
971 * potentials & corresponding forces
975 /* generic potential and force calculation */
977 int potential_force_calc(t_moldyn *moldyn) {
980 t_atom *itom,*jtom,*ktom;
982 t_list neighbour_i[27];
983 t_list neighbour_i2[27];
995 /* get energy and force of every atom */
996 for(i=0;i<count;i++) {
999 v3_zero(&(itom[i].f));
1001 /* reset viral of atom i */
1002 v3_zero(&(itom[i].virial));
1004 /* single particle potential/force */
1005 if(itom[i].attr&ATOM_ATTR_1BP)
1006 moldyn->func1b(moldyn,&(itom[i]));
1008 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1011 /* 2 body pair potential/force */
1013 link_cell_neighbour_index(moldyn,
1014 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1015 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1016 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1023 this=&(neighbour_i[j]);
1026 if(this->start==NULL)
1032 jtom=this->current->data;
1034 if(jtom==&(itom[i]))
1037 if((jtom->attr&ATOM_ATTR_2BP)&
1038 (itom[i].attr&ATOM_ATTR_2BP)) {
1039 moldyn->func2b(moldyn,
1045 /* 3 body potential/force */
1047 if(!(itom[i].attr&ATOM_ATTR_3BP)||
1048 !(jtom->attr&ATOM_ATTR_3BP))
1051 /* copy the neighbour lists */
1052 memcpy(neighbour_i2,neighbour_i,
1055 /* get neighbours of i */
1058 that=&(neighbour_i2[k]);
1061 if(that->start==NULL)
1068 ktom=that->current->data;
1070 if(!(ktom->attr&ATOM_ATTR_3BP))
1076 if(ktom==&(itom[i]))
1079 moldyn->func3b(moldyn,
1085 } while(list_next_f(that)!=\
1090 /* 2bp post function */
1091 if(moldyn->func2b_post) {
1092 moldyn->func2b_post(moldyn,
1097 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1107 * periodic boundayr checking
1110 inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1121 if(moldyn->status&MOLDYN_STAT_PBX) {
1122 if(a->x>=x) a->x-=dim->x;
1123 else if(-a->x>x) a->x+=dim->x;
1125 if(moldyn->status&MOLDYN_STAT_PBY) {
1126 if(a->y>=y) a->y-=dim->y;
1127 else if(-a->y>y) a->y+=dim->y;
1129 if(moldyn->status&MOLDYN_STAT_PBZ) {
1130 if(a->z>=z) a->z-=dim->z;
1131 else if(-a->z>z) a->z+=dim->z;
1139 * example potentials
1142 /* harmonic oscillator potential and force */
1144 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1146 t_ho_params *params;
1147 t_3dvec force,distance;
1149 double sc,equi_dist;
1151 params=moldyn->pot2b_params;
1152 sc=params->spring_constant;
1153 equi_dist=params->equilibrium_distance;
1155 v3_sub(&distance,&(aj->r),&(ai->r));
1157 if(bc) check_per_bound(moldyn,&distance);
1158 d=v3_norm(&distance);
1159 if(d<=moldyn->cutoff) {
1160 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
1161 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
1162 /* f = -grad E; grad r_ij = -1 1/r_ij distance */
1163 v3_scale(&force,&distance,sc*(1.0-(equi_dist/d)));
1164 v3_add(&(ai->f),&(ai->f),&force);
1170 /* lennard jones potential & force for one sort of atoms */
1172 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1174 t_lj_params *params;
1175 t_3dvec force,distance;
1177 double eps,sig6,sig12;
1179 params=moldyn->pot2b_params;
1180 eps=params->epsilon4;
1181 sig6=params->sigma6;
1182 sig12=params->sigma12;
1184 v3_sub(&distance,&(aj->r),&(ai->r));
1185 if(bc) check_per_bound(moldyn,&distance);
1186 d=v3_absolute_square(&distance); /* 1/r^2 */
1187 if(d<=moldyn->cutoff_square) {
1188 d=1.0/d; /* 1/r^2 */
1191 h1=h2*h2; /* 1/r^12 */
1192 /* energy is eps*..., but we will add this twice ... */
1193 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
1200 v3_scale(&force,&distance,-1.0*d); /* f = - grad E */
1201 v3_add(&(ai->f),&(ai->f),&force);
1208 * tersoff potential & force for 2 sorts of atoms
1211 /* create mixed terms from parameters and set them */
1212 int tersoff_mult_complete_params(t_tersoff_mult_params *p) {
1214 printf("[moldyn] tersoff parameter completion\n");
1215 p->Smixed=sqrt(p->S[0]*p->S[1]);
1216 p->Rmixed=sqrt(p->R[0]*p->R[1]);
1217 p->Amixed=sqrt(p->A[0]*p->A[1]);
1218 p->Bmixed=sqrt(p->B[0]*p->B[1]);
1219 p->lambda_m=0.5*(p->lambda[0]+p->lambda[1]);
1220 p->mu_m=0.5*(p->mu[0]+p->mu[1]);
1222 printf("[moldyn] tersoff mult parameter info:\n");
1223 printf(" S (A) | %f | %f | %f\n",p->S[0],p->S[1],p->Smixed);
1224 printf(" R (A) | %f | %f | %f\n",p->R[0],p->R[1],p->Rmixed);
1225 printf(" A (eV) | %f | %f | %f\n",p->A[0]/EV,p->A[1]/EV,p->Amixed/EV);
1226 printf(" B (eV) | %f | %f | %f\n",p->B[0]/EV,p->B[1]/EV,p->Bmixed/EV);
1227 printf(" lambda | %f | %f | %f\n",p->lambda[0],p->lambda[1],
1229 printf(" mu | %f | %f | %f\n",p->mu[0],p->mu[1],p->mu_m);
1230 printf(" beta | %.10f | %.10f\n",p->beta[0],p->beta[1]);
1231 printf(" n | %f | %f\n",p->n[0],p->n[1]);
1232 printf(" c | %f | %f\n",p->c[0],p->c[1]);
1233 printf(" d | %f | %f\n",p->d[0],p->d[1]);
1234 printf(" h | %f | %f\n",p->h[0],p->h[1]);
1235 printf(" chi | %f \n",p->chi);
1240 /* tersoff 1 body part */
1241 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
1244 t_tersoff_mult_params *params;
1245 t_tersoff_exchange *exchange;
1248 params=moldyn->pot1b_params;
1249 exchange=&(params->exchange);
1252 * simple: point constant parameters only depending on atom i to
1253 * their right values
1256 exchange->beta_i=&(params->beta[num]);
1257 exchange->n_i=&(params->n[num]);
1258 exchange->c_i=&(params->c[num]);
1259 exchange->d_i=&(params->d[num]);
1260 exchange->h_i=&(params->h[num]);
1262 exchange->betaini=pow(*(exchange->beta_i),*(exchange->n_i));
1263 exchange->ci2=params->c[num]*params->c[num];
1264 exchange->di2=params->d[num]*params->d[num];
1265 exchange->ci2di2=exchange->ci2/exchange->di2;
1270 /* tersoff 2 body part */
1271 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1273 t_tersoff_mult_params *params;
1274 t_tersoff_exchange *exchange;
1275 t_3dvec dist_ij,force;
1277 double A,B,R,S,lambda,mu;
1284 params=moldyn->pot2b_params;
1286 exchange=&(params->exchange);
1288 /* clear 3bp and 2bp post run */
1290 exchange->run2bp_post=0;
1292 /* reset S > r > R mark */
1293 exchange->d_ij_between_rs=0;
1296 * calc of 2bp contribution of V_ij and dV_ij/ji
1298 * for Vij and dV_ij we need:
1302 * for dV_ji we need:
1303 * - f_c_ji = f_c_ij, df_c_ji = df_c_ij
1304 * - f_r_ji = f_r_ij; df_r_ji = df_r_ij
1309 v3_sub(&dist_ij,&(aj->r),&(ai->r));
1310 if(bc) check_per_bound(moldyn,&dist_ij);
1311 d_ij=v3_norm(&dist_ij);
1313 /* save for use in 3bp */
1314 exchange->d_ij=d_ij;
1315 exchange->dist_ij=dist_ij;
1323 lambda=params->lambda[num];
1332 lambda=params->lambda_m;
1334 params->exchange.chi=params->chi;
1337 /* if d_ij > S => no force & potential energy contribution */
1341 /* more constants */
1342 exchange->beta_j=&(params->beta[num]);
1343 exchange->n_j=&(params->n[num]);
1344 exchange->c_j=&(params->c[num]);
1345 exchange->d_j=&(params->d[num]);
1346 exchange->h_j=&(params->h[num]);
1348 exchange->betajnj=exchange->betaini;
1349 exchange->cj2=exchange->ci2;
1350 exchange->dj2=exchange->di2;
1351 exchange->cj2dj2=exchange->ci2di2;
1354 exchange->betajnj=pow(*(exchange->beta_j),*(exchange->n_j));
1355 exchange->cj2=params->c[num]*params->c[num];
1356 exchange->dj2=params->d[num]*params->d[num];
1357 exchange->cj2dj2=exchange->cj2/exchange->dj2;
1360 /* f_r_ij = f_r_ji, df_r_ij = df_r_ji */
1361 f_r=A*exp(-lambda*d_ij);
1362 df_r=lambda*f_r/d_ij;
1364 /* f_a, df_a calc (again, same for ij and ji) | save for later use! */
1365 exchange->f_a=-B*exp(-mu*d_ij);
1366 exchange->df_a=mu*exchange->f_a/d_ij;
1368 /* f_c, df_c calc (again, same for ij and ji) */
1370 /* f_c = 1, df_c = 0 */
1373 /* two body contribution (ij, ji) */
1374 v3_scale(&force,&dist_ij,-df_r);
1378 arg=M_PI*(d_ij-R)/s_r;
1379 f_c=0.5+0.5*cos(arg);
1380 //df_c=-0.5*sin(arg)*(M_PI/(s_r*d_ij)); /* MARK! */
1381 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
1382 /* two body contribution (ij, ji) */
1383 v3_scale(&force,&dist_ij,-df_c*f_r-df_r*f_c);
1384 /* tell 3bp that S > r > R */
1385 exchange->d_ij_between_rs=1;
1388 /* add forces of 2bp (ij, ji) contribution
1389 * dVij = dVji and we sum up both: no 1/2) */
1390 v3_add(&(ai->f),&(ai->f),&force);
1392 /* energy 2bp contribution (ij, ji) is 0.5 f_r f_c ... */
1393 moldyn->energy+=(0.5*f_r*f_c);
1395 /* save for use in 3bp */
1397 exchange->df_c=df_c;
1399 /* enable the run of 3bp function and 2bp post processing */
1401 exchange->run2bp_post=1;
1403 /* reset 3bp sums */
1404 exchange->zeta_ij=0.0;
1405 exchange->zeta_ji=0.0;
1406 v3_zero(&(exchange->dzeta_ij));
1407 v3_zero(&(exchange->dzeta_ji));
1412 /* tersoff 2 body post part */
1414 int tersoff_mult_post_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1417 * here we have to allow for the 3bp sums
1420 * - zeta_ij, dzeta_ij
1421 * - zeta_ji, dzeta_ji
1423 * to compute the 3bp contribution to:
1429 t_tersoff_mult_params *params;
1430 t_tersoff_exchange *exchange;
1435 double f_c,df_c,f_a,df_a;
1436 double chi,ni,betaini,nj,betajnj;
1439 params=moldyn->pot2b_params;
1440 exchange=&(params->exchange);
1442 /* we do not run if f_c_ij was detected to be 0! */
1443 if(!(exchange->run2bp_post))
1447 df_c=exchange->df_c;
1449 df_a=exchange->df_a;
1450 betaini=exchange->betaini;
1451 betajnj=exchange->betajnj;
1452 ni=*(exchange->n_i);
1453 nj=*(exchange->n_j);
1455 dist_ij=&(exchange->dist_ij);
1458 zeta=exchange->zeta_ij;
1460 moldyn->debug++; /* just for debugging ... */
1463 v3_scale(&force,dist_ij,df_a*b*f_c);
1466 tmp=betaini*pow(zeta,ni-1.0); /* beta^n * zeta^n-1 */
1467 b=(1+zeta*tmp); /* 1 + beta^n zeta^n */
1468 db=chi*pow(b,-1.0/(2*ni)-1); /* x(...)^(-1/2n - 1) */
1470 db*=-0.5*tmp; /* db_ij */
1471 v3_scale(&force,&(exchange->dzeta_ij),f_a*db);
1472 v3_scale(&temp,dist_ij,df_a*b);
1473 v3_add(&force,&force,&temp);
1474 v3_scale(&force,&force,f_c);
1476 v3_scale(&temp,dist_ij,df_c*b*f_a);
1477 v3_add(&force,&force,&temp);
1478 v3_scale(&force,&force,-0.5);
1481 v3_add(&(ai->f),&(ai->f),&force);
1483 /* add energy of 3bp sum */
1484 moldyn->energy+=(0.5*f_c*b*f_a);
1487 zeta=exchange->zeta_ji;
1491 v3_scale(&force,dist_ij,df_a*b*f_c);
1494 tmp=betajnj*pow(zeta,nj-1.0); /* beta^n * zeta^n-1 */
1495 b=(1+zeta*tmp); /* 1 + beta^n zeta^n */
1496 db=chi*pow(b,-1.0/(2*nj)-1); /* x(...)^(-1/2n - 1) */
1498 db*=-0.5*tmp; /* db_ij */
1499 v3_scale(&force,&(exchange->dzeta_ji),f_a*db);
1500 v3_scale(&temp,dist_ij,df_a*b);
1501 v3_add(&force,&force,&temp);
1502 v3_scale(&force,&force,f_c);
1504 v3_scale(&temp,dist_ij,df_c*b*f_a);
1505 v3_add(&force,&force,&temp);
1506 v3_scale(&force,&force,-0.5);
1509 v3_add(&(ai->f),&(ai->f),&force);
1514 /* tersoff 3 body part */
1516 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1518 t_tersoff_mult_params *params;
1519 t_tersoff_exchange *exchange;
1520 t_3dvec dist_ij,dist_ik,dist_jk;
1521 t_3dvec temp1,temp2;
1525 double d_ij,d_ik,d_jk;
1528 double f_c_ik,df_c_ik,arg;
1532 double cos_theta,d_costheta1,d_costheta2;
1533 double h_cos,d2_h_cos2;
1534 double frac,g,zeta,chi;
1538 params=moldyn->pot3b_params;
1539 exchange=&(params->exchange);
1541 if(!(exchange->run3bp))
1545 * calc of 3bp contribution of V_ij and dV_ij/ji/jk &
1546 * 2bp contribution of dV_jk
1548 * for Vij and dV_ij we still need:
1549 * - b_ij, db_ij (zeta_ij)
1550 * - f_c_ik, df_c_ik, constants_i, cos_theta_ijk, d_costheta_ijk
1552 * for dV_ji we still need:
1553 * - b_ji, db_ji (zeta_ji)
1554 * - f_c_jk, d_c_jk, constants_j, cos_theta_jik, d_costheta_jik
1556 * for dV_jk we need:
1560 * - f_c_ji, df_c_ji, constants_j, cos_theta_jki, d_costheta_jki
1568 /* dist_ij, d_ij - this is < S_ij ! */
1569 dist_ij=exchange->dist_ij;
1570 d_ij=exchange->d_ij;
1572 /* f_c_ij, df_c_ij (same for ji) */
1574 df_c=exchange->df_c;
1577 * calculate unknown values now ...
1580 /* V_ij and dV_ij stuff (in b_ij there is f_c_ik) */
1583 v3_sub(&dist_ik,&(ak->r),&(ai->r));
1584 if(bc) check_per_bound(moldyn,&dist_ik);
1585 d_ik=v3_norm(&dist_ik);
1598 /* zeta_ij/dzeta_ij contribution only for d_ik < S */
1601 /* get constants_i from exchange data */
1608 c2d2=exchange->ci2di2;
1610 /* cosine of theta_ijk by scalaproduct */
1611 rr=v3_scalar_product(&dist_ij,&dist_ik);
1617 d_costheta1=cos_theta/(d_ij*d_ij)-tmp;
1618 d_costheta2=cos_theta/(d_ik*d_ik)-tmp;
1620 /* some usefull values */
1621 h_cos=(h-cos_theta);
1622 d2_h_cos2=d2+(h_cos*h_cos);
1623 frac=c2/(d2_h_cos2);
1628 /* d_costheta_ij and dg(cos_theta) - needed in any case! */
1629 v3_scale(&temp1,&dist_ij,d_costheta1);
1630 v3_scale(&temp2,&dist_ik,d_costheta2);
1631 v3_add(&temp1,&temp1,&temp2);
1632 v3_scale(&temp1,&temp1,-2.0*frac*h_cos/d2_h_cos2); /* dg */
1634 /* f_c_ik & df_c_ik + {d,}zeta contribution */
1635 dzeta=&(exchange->dzeta_ij);
1639 // => df_c_ik=0.0; of course we do not set this!
1642 exchange->zeta_ij+=g;
1645 v3_add(dzeta,dzeta,&temp1);
1650 arg=M_PI*(d_ik-R)/s_r;
1651 f_c_ik=0.5+0.5*cos(arg);
1652 //df_c_ik=-0.5*sin(arg)*(M_PI/(s_r*d_ik)); /* MARK */
1653 df_c_ik=0.5*sin(arg)*(M_PI/(s_r*d_ik));
1656 exchange->zeta_ij+=f_c_ik*g;
1659 v3_scale(&temp1,&temp1,f_c_ik);
1660 v3_scale(&temp2,&dist_ik,g*df_c_ik);
1661 v3_add(&temp1,&temp1,&temp2);
1662 v3_add(dzeta,dzeta,&temp1);
1666 /* dV_ji stuff (in b_ji there is f_c_jk) + dV_jk stuff! */
1669 v3_sub(&dist_jk,&(ak->r),&(aj->r));
1670 if(bc) check_per_bound(moldyn,&dist_jk);
1671 d_jk=v3_norm(&dist_jk);
1690 /* zeta_ji/dzeta_ji contribution only for d_jk < S_jk */
1693 /* constants_j from exchange data */
1700 c2d2=exchange->cj2dj2;
1702 /* cosine of theta_jik by scalaproduct */
1703 rr=-v3_scalar_product(&dist_ij,&dist_jk); /* -1, as ij -> ji */
1709 d_costheta2=cos_theta/(d_ij*d_ij);
1711 /* some usefull values */
1712 h_cos=(h-cos_theta);
1713 d2_h_cos2=d2+(h_cos*h_cos);
1714 frac=c2/(d2_h_cos2);
1719 /* d_costheta_ij and dg(cos_theta) - needed in any case! */
1720 v3_scale(&temp1,&dist_jk,d_costheta1);
1721 v3_scale(&temp2,&dist_ij,-d_costheta2); /* ji -> ij => -1 */
1722 v3_add(&temp1,&temp1,&temp2);
1723 v3_scale(&temp1,&temp1,-2.0*frac*h_cos/d2_h_cos2); /* dg */
1725 /* store dg in temp2 and use it for dVjk later */
1726 v3_copy(&temp2,&temp1);
1728 /* f_c_jk + {d,}zeta contribution (df_c_jk = 0) */
1729 dzeta=&(exchange->dzeta_ji);
1735 exchange->zeta_ji+=g;
1738 v3_add(dzeta,dzeta,&temp1);
1743 arg=M_PI*(d_jk-R)/s_r;
1744 f_c_jk=0.5+0.5*cos(arg);
1747 exchange->zeta_ji+=f_c_jk*g;
1750 v3_scale(&temp1,&temp1,f_c_jk);
1751 v3_add(dzeta,dzeta,&temp1);
1754 /* dV_jk stuff | add force contribution on atom i immediately */
1755 if(exchange->d_ij_between_rs) {
1757 v3_scale(&temp1,&temp2,f_c);
1758 v3_scale(&temp2,&dist_ij,df_c*g);
1759 v3_add(&temp2,&temp2,&temp1); /* -> dzeta_jk in temp2 */
1763 // dzeta_jk is simply dg, which is stored in temp2
1765 /* betajnj * zeta_jk ^ nj-1 */
1766 tmp=exchange->betajnj*pow(zeta,(n-1.0));
1767 tmp=-chi/2.0*pow((1+tmp*zeta),(-1.0/(2.0*n)-1))*tmp;
1768 v3_scale(&temp2,&temp2,tmp*B*exp(-mu*d_jk)*f_c_jk*0.5);
1769 v3_add(&(ai->f),&(ai->f),&temp2); /* -1 skipped in f_a calc ^ */
1770 /* scaled with 0.5 ^ */
1779 * debugging / critical check functions
1782 int moldyn_bc_check(t_moldyn *moldyn) {
1795 for(i=0;i<moldyn->count;i++) {
1796 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
1797 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
1798 i,atom[i].r.x,dim->x/2);
1799 printf("diagnostic:\n");
1800 printf("-----------\natom.r.x:\n");
1802 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
1805 ((byte)&(1<<k))?1:0,
1808 printf("---------------\nx=dim.x/2:\n");
1810 memcpy(&byte,(u8 *)(&x)+j,1);
1813 ((byte)&(1<<k))?1:0,
1816 if(atom[i].r.x==x) printf("the same!\n");
1817 else printf("different!\n");
1819 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
1820 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
1821 i,atom[i].r.y,dim->y/2);
1822 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
1823 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
1824 i,atom[i].r.z,dim->z/2);