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"
27 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
31 //ret=moldyn_parse_argv(moldyn,argc,argv);
32 //if(ret<0) return ret;
34 memset(moldyn,0,sizeof(t_moldyn));
36 rand_init(&(moldyn->random),NULL,1);
37 moldyn->random.status|=RAND_STAT_VERBOSE;
42 int moldyn_shutdown(t_moldyn *moldyn) {
44 printf("[moldyn] shutdown\n");
45 moldyn_log_shutdown(moldyn);
46 link_cell_shutdown(moldyn);
47 rand_close(&(moldyn->random));
53 int set_int_alg(t_moldyn *moldyn,u8 algo) {
56 case MOLDYN_INTEGRATE_VERLET:
57 moldyn->integrate=velocity_verlet;
60 printf("unknown integration algorithm: %02x\n",algo);
67 int set_cutoff(t_moldyn *moldyn,double cutoff) {
69 moldyn->cutoff=cutoff;
74 int set_temperature(t_moldyn *moldyn,double t_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) {
105 int set_nn_dist(t_moldyn *moldyn,double dist) {
112 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
115 moldyn->status|=MOLDYN_STAT_PBX;
118 moldyn->status|=MOLDYN_STAT_PBY;
121 moldyn->status|=MOLDYN_STAT_PBZ;
126 int set_potential1b(t_moldyn *moldyn,pf_func1b func,void *params) {
129 moldyn->pot1b_params=params;
134 int set_potential2b(t_moldyn *moldyn,pf_func2b func,void *params) {
137 moldyn->pot2b_params=params;
142 int set_potential2b_post(t_moldyn *moldyn,pf_func2b_post func,void *params) {
145 moldyn->pot2b_params=params;
150 int set_potential3b(t_moldyn *moldyn,pf_func3b func,void *params) {
153 moldyn->pot3b_params=params;
158 int moldyn_set_log(t_moldyn *moldyn,u8 type,char *fb,int timer) {
161 case LOG_TOTAL_ENERGY:
162 moldyn->ewrite=timer;
163 moldyn->efd=open(fb,O_WRONLY|O_CREAT|O_TRUNC);
165 perror("[moldyn] efd open");
168 dprintf(moldyn->efd,"# total energy log file\n");
170 case LOG_TOTAL_MOMENTUM:
171 moldyn->mwrite=timer;
172 moldyn->mfd=open(fb,O_WRONLY|O_CREAT|O_TRUNC);
174 perror("[moldyn] mfd open");
177 dprintf(moldyn->efd,"# total momentum log file\n");
180 moldyn->swrite=timer;
181 strncpy(moldyn->sfb,fb,63);
184 moldyn->vwrite=timer;
185 strncpy(moldyn->vfb,fb,63);
186 visual_init(&(moldyn->vis),fb);
189 printf("unknown log mechanism: %02x\n",type);
196 int moldyn_log_shutdown(t_moldyn *moldyn) {
198 printf("[moldyn] log shutdown\n");
199 if(moldyn->efd) close(moldyn->efd);
200 if(moldyn->mfd) close(moldyn->mfd);
201 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
206 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
207 u8 attr,u8 bnum,int a,int b,int c) {
215 if(type==FCC) count*=4;
217 if(type==DIAMOND) count*=8;
219 moldyn->atom=malloc(count*sizeof(t_atom));
220 if(moldyn->atom==NULL) {
221 perror("malloc (atoms)");
229 ret=fcc_init(a,b,c,lc,moldyn->atom,&origin);
232 ret=diamond_init(a,b,c,lc,moldyn->atom,&origin);
235 printf("unknown lattice type (%02x)\n",type);
241 printf("ok, there is something wrong ...\n");
242 printf("calculated -> %d atoms\n",count);
243 printf("created -> %d atoms\n",ret);
248 printf("[moldyn] created lattice with %d atoms\n",count);
252 moldyn->atom[count].element=element;
253 moldyn->atom[count].mass=mass;
254 moldyn->atom[count].attr=attr;
255 moldyn->atom[count].bnum=bnum;
256 check_per_bound(moldyn,&(moldyn->atom[count].r));
263 int add_atom(t_moldyn *moldyn,int element,double mass,u8 bnum,u8 attr,
264 t_3dvec *r,t_3dvec *v) {
271 count=++(moldyn->count);
273 ptr=realloc(atom,count*sizeof(t_atom));
275 perror("[moldyn] realloc (add atom)");
283 atom[count-1].element=element;
284 atom[count-1].mass=mass;
285 atom[count-1].bnum=bnum;
286 atom[count-1].attr=attr;
291 int destroy_atoms(t_moldyn *moldyn) {
293 if(moldyn->atom) free(moldyn->atom);
298 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
301 * - gaussian distribution of velocities
302 * - zero total momentum
303 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
308 t_3dvec p_total,delta;
313 random=&(moldyn->random);
315 /* gaussian distribution of velocities */
317 for(i=0;i<moldyn->count;i++) {
318 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
320 v=sigma*rand_get_gauss(random);
322 p_total.x+=atom[i].mass*v;
324 v=sigma*rand_get_gauss(random);
326 p_total.y+=atom[i].mass*v;
328 v=sigma*rand_get_gauss(random);
330 p_total.z+=atom[i].mass*v;
333 /* zero total momentum */
334 v3_scale(&p_total,&p_total,1.0/moldyn->count);
335 for(i=0;i<moldyn->count;i++) {
336 v3_scale(&delta,&p_total,1.0/atom[i].mass);
337 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
340 /* velocity scaling */
341 scale_velocity(moldyn,equi_init);
346 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
356 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
359 /* get kinetic energy / temperature & count involved atoms */
362 for(i=0;i<moldyn->count;i++) {
363 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
364 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
368 if(count!=0) moldyn->t=(2.0*e)/(3.0*count*K_BOLTZMANN);
369 else return 0; /* no atoms involved in scaling! */
371 /* (temporary) hack for e,t = 0 */
374 if(moldyn->t_ref!=0.0)
375 thermal_init(moldyn,equi_init);
377 return 0; /* no scaling needed */
381 /* get scaling factor */
382 scale=moldyn->t_ref/moldyn->t;
386 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
387 scale=1.0+moldyn->tau*(scale-1.0)/moldyn->t_tc;
390 /* velocity scaling */
391 for(i=0;i<moldyn->count;i++)
392 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
393 v3_scale(&(atom[i].v),&(atom[i].v),scale);
398 double get_e_kin(t_moldyn *moldyn) {
406 for(i=0;i<moldyn->count;i++)
407 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
412 double get_e_pot(t_moldyn *moldyn) {
414 return moldyn->energy;
417 double update_e_kin(t_moldyn *moldyn) {
419 return(get_e_kin(moldyn));
422 double get_total_energy(t_moldyn *moldyn) {
424 return(moldyn->ekin+moldyn->energy);
427 t_3dvec get_total_p(t_moldyn *moldyn) {
436 for(i=0;i<moldyn->count;i++) {
437 v3_scale(&p,&(atom[i].v),atom[i].mass);
438 v3_add(&p_total,&p_total,&p);
444 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
448 /* nn_dist is the nearest neighbour distance */
451 printf("[moldyn] i do not estimate timesteps below %f K!\n",
452 MOLDYN_CRITICAL_EST_TEMP);
456 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
465 /* linked list / cell method */
467 int link_cell_init(t_moldyn *moldyn) {
473 fd=open("/dev/null",O_WRONLY);
477 /* partitioning the md cell */
478 lc->nx=moldyn->dim.x/moldyn->cutoff;
479 lc->x=moldyn->dim.x/lc->nx;
480 lc->ny=moldyn->dim.y/moldyn->cutoff;
481 lc->y=moldyn->dim.y/lc->ny;
482 lc->nz=moldyn->dim.z/moldyn->cutoff;
483 lc->z=moldyn->dim.z/lc->nz;
485 lc->cells=lc->nx*lc->ny*lc->nz;
486 lc->subcell=malloc(lc->cells*sizeof(t_list));
488 printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
490 for(i=0;i<lc->cells;i++)
491 //list_init(&(lc->subcell[i]),1);
492 list_init(&(lc->subcell[i]),fd);
494 link_cell_update(moldyn);
499 int link_cell_update(t_moldyn *moldyn) {
513 for(i=0;i<lc->cells;i++)
514 list_destroy(&(moldyn->lc.subcell[i]));
516 for(count=0;count<moldyn->count;count++) {
517 i=(atom[count].r.x+(moldyn->dim.x/2))/lc->x;
518 j=(atom[count].r.y+(moldyn->dim.y/2))/lc->y;
519 k=(atom[count].r.z+(moldyn->dim.z/2))/lc->z;
520 list_add_immediate_ptr(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
527 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
545 cell[0]=lc->subcell[i+j*nx+k*a];
546 for(ci=-1;ci<=1;ci++) {
553 for(cj=-1;cj<=1;cj++) {
560 for(ck=-1;ck<=1;ck++) {
567 if(!(ci|cj|ck)) continue;
569 cell[--count2]=lc->subcell[x+y*nx+z*a];
572 cell[count1++]=lc->subcell[x+y*nx+z*a];
584 int link_cell_shutdown(t_moldyn *moldyn) {
591 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
592 list_shutdown(&(moldyn->lc.subcell[i]));
597 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
601 t_moldyn_schedule *schedule;
603 schedule=&(moldyn->schedule);
604 count=++(schedule->content_count);
606 ptr=realloc(moldyn->schedule.runs,count*sizeof(int));
608 perror("[moldyn] realloc (runs)");
611 moldyn->schedule.runs=ptr;
612 moldyn->schedule.runs[count-1]=runs;
614 ptr=realloc(schedule->tau,count*sizeof(double));
616 perror("[moldyn] realloc (tau)");
619 moldyn->schedule.tau=ptr;
620 moldyn->schedule.tau[count-1]=tau;
625 int moldyn_set_schedule_hook(t_moldyn *moldyn,void *hook,void *hook_params) {
627 moldyn->schedule.hook=hook;
628 moldyn->schedule.hook_params=hook_params;
635 * 'integration of newtons equation' - algorithms
639 /* start the integration */
641 int moldyn_integrate(t_moldyn *moldyn) {
644 unsigned int e,m,s,v;
646 t_moldyn_schedule *schedule;
652 schedule=&(moldyn->schedule);
655 /* initialize linked cell method */
656 link_cell_init(moldyn);
658 /* logging & visualization */
664 /* sqaure of some variables */
665 moldyn->tau_square=moldyn->tau*moldyn->tau;
666 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
667 /* calculate initial forces */
668 potential_force_calc(moldyn);
670 /* do some checks before we actually start calculating bullshit */
671 if(moldyn->cutoff>0.5*moldyn->dim.x)
672 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
673 if(moldyn->cutoff>0.5*moldyn->dim.y)
674 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
675 if(moldyn->cutoff>0.5*moldyn->dim.z)
676 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
677 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
678 if(ds>0.05*moldyn->nnd)
679 printf("[moldyn] warning: forces too high / tau too small!\n");
681 /* zero absolute time */
683 for(sched=0;sched<moldyn->schedule.content_count;sched++) {
685 /* setting amount of runs and finite time step size */
686 moldyn->tau=schedule->tau[sched];
687 moldyn->tau_square=moldyn->tau*moldyn->tau;
688 moldyn->time_steps=schedule->runs[sched];
690 /* integration according to schedule */
692 for(i=0;i<moldyn->time_steps;i++) {
694 /* integration step */
695 moldyn->integrate(moldyn);
698 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
699 scale_velocity(moldyn,FALSE);
701 /* increase absolute time */
702 moldyn->time+=moldyn->tau;
704 /* check for log & visualization */
708 "%.15f %.45f %.45f %.45f\n",
709 moldyn->time,update_e_kin(moldyn),
711 get_total_energy(moldyn));
715 p=get_total_p(moldyn);
717 "%.15f %.45f\n",moldyn->time,
723 snprintf(fb,128,"%s-%f-%.15f.save",moldyn->sfb,
724 moldyn->t,i*moldyn->tau);
725 fd=open(fb,O_WRONLY|O_TRUNC|O_CREAT);
726 if(fd<0) perror("[moldyn] save fd open");
728 write(fd,moldyn,sizeof(t_moldyn));
729 write(fd,moldyn->atom,
730 moldyn->count*sizeof(t_atom));
737 visual_atoms(&(moldyn->vis),moldyn->time,
738 moldyn->atom,moldyn->count);
739 printf("\rsched: %d, steps: %d",sched,i);
746 /* check for hooks */
748 schedule->hook(moldyn,schedule->hook_params);
755 /* velocity verlet */
757 int velocity_verlet(t_moldyn *moldyn) {
760 double tau,tau_square;
767 tau_square=moldyn->tau_square;
769 for(i=0;i<count;i++) {
771 v3_scale(&delta,&(atom[i].v),tau);
772 v3_add(&(atom[i].r),&(atom[i].r),&delta);
773 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
774 v3_add(&(atom[i].r),&(atom[i].r),&delta);
775 check_per_bound(moldyn,&(atom[i].r));
778 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
779 v3_add(&(atom[i].v),&(atom[i].v),&delta);
782 /* neighbour list update */
783 link_cell_update(moldyn);
785 /* forces depending on chosen potential */
786 potential_force_calc(moldyn);
787 //moldyn->potential_force_function(moldyn);
789 for(i=0;i<count;i++) {
790 /* again velocities */
791 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
792 v3_add(&(atom[i].v),&(atom[i].v),&delta);
801 * potentials & corresponding forces
805 /* generic potential and force calculation */
807 int potential_force_calc(t_moldyn *moldyn) {
810 t_atom *itom,*jtom,*ktom;
812 t_list neighbour_i[27];
813 t_list neighbour_i2[27];
814 //t_list neighbour_j[27];
826 for(i=0;i<count;i++) {
829 v3_zero(&(itom[i].f));
831 /* single particle potential/force */
832 if(itom[i].attr&ATOM_ATTR_1BP)
833 moldyn->func1b(moldyn,&(itom[i]));
835 /* 2 body pair potential/force */
836 if(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)) {
838 link_cell_neighbour_index(moldyn,
839 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
840 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
841 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
847 for(j=0;j<countn;j++) {
849 this=&(neighbour_i[j]);
852 if(this->start==NULL)
858 jtom=this->current->data;
863 if((jtom->attr&ATOM_ATTR_2BP)&
864 (itom[i].attr&ATOM_ATTR_2BP))
865 moldyn->func2b(moldyn,
870 /* 3 body potential/force */
872 if(!(itom[i].attr&ATOM_ATTR_3BP)||
873 !(jtom->attr&ATOM_ATTR_3BP))
876 /* neighbourhood of atom j is not needed! */
878 // link_cell_neighbour_index(moldyn,
879 // (jtom->r.x+moldyn->dim.x/2)/lc->x,
880 // (jtom->r.y+moldyn->dim.y/2)/lc->y,
881 // (jtom->r.z+moldyn->dim.z/2)/lc->z,
884 // /* neighbours of j */
885 // for(k=0;k<lc->countn;k++) {
887 // that=&(neighbour_j[k]);
890 // if(that->start==NULL)
893 // bc_ijk=(k<lc->dnlc)?0:1;
897 // ktom=that->current->data;
899 // if(!(ktom->attr&ATOM_ATTR_3BP))
905 // if(ktom==&(itom[i]))
908 // moldyn->func3b(moldyn,&(itom[i]),jtom,ktom,bc_ijk);
910 /* } while(list_next(that)!=\ */
911 // L_NO_NEXT_ELEMENT);
915 /* copy the neighbour lists */
916 memcpy(neighbour_i2,neighbour_i,
919 /* get neighbours of i */
920 for(k=0;k<countn;k++) {
922 that=&(neighbour_i2[k]);
925 if(that->start==NULL)
932 ktom=that->current->data;
934 if(!(ktom->attr&ATOM_ATTR_3BP))
943 //printf("Debug: atom %d before 3bp: %08x %08x %08x | %.15f %.15f %.15f\n",i,&itom[i],jtom,ktom,itom[i].r.x,itom[i].f.x,itom[i].v.x);
944 moldyn->func3b(moldyn,&(itom[i]),jtom,ktom,bc_ijk);
945 //printf("Debug: atom %d after 3bp: %08x %08x %08x | %.15f %.15f %.15f\n",i,&itom[i],jtom,ktom,itom[i].r.x,itom[i].f.x,itom[i].v.x);
947 } while(list_next(that)!=\
952 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
954 /* 2bp post function */
955 if(moldyn->func2b_post)
956 moldyn->func2b_post(moldyn,
968 * periodic boundayr checking
971 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
982 if(moldyn->status&MOLDYN_STAT_PBX) {
983 if(a->x>=x) a->x-=dim->x;
984 else if(-a->x>x) a->x+=dim->x;
986 if(moldyn->status&MOLDYN_STAT_PBY) {
987 if(a->y>=y) a->y-=dim->y;
988 else if(-a->y>y) a->y+=dim->y;
990 if(moldyn->status&MOLDYN_STAT_PBZ) {
991 if(a->z>=z) a->z-=dim->z;
992 else if(-a->z>z) a->z+=dim->z;
1000 * example potentials
1003 /* harmonic oscillator potential and force */
1005 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1007 t_ho_params *params;
1008 t_3dvec force,distance;
1010 double sc,equi_dist;
1012 params=moldyn->pot2b_params;
1013 sc=params->spring_constant;
1014 equi_dist=params->equilibrium_distance;
1016 v3_sub(&distance,&(ai->r),&(aj->r));
1018 if(bc) check_per_bound(moldyn,&distance);
1019 d=v3_norm(&distance);
1020 if(d<=moldyn->cutoff) {
1021 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
1022 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
1023 v3_scale(&force,&distance,-sc*(1.0-(equi_dist/d)));
1024 v3_add(&(ai->f),&(ai->f),&force);
1030 /* lennard jones potential & force for one sort of atoms */
1032 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1034 t_lj_params *params;
1035 t_3dvec force,distance;
1037 double eps,sig6,sig12;
1039 params=moldyn->pot2b_params;
1040 eps=params->epsilon4;
1041 sig6=params->sigma6;
1042 sig12=params->sigma12;
1044 v3_sub(&distance,&(ai->r),&(aj->r));
1045 if(bc) check_per_bound(moldyn,&distance);
1046 d=v3_absolute_square(&distance); /* 1/r^2 */
1047 if(d<=moldyn->cutoff_square) {
1048 d=1.0/d; /* 1/r^2 */
1051 h1=h2*h2; /* 1/r^12 */
1052 /* energy is eps*..., but we will add this twice ... */
1053 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
1060 v3_scale(&force,&distance,d);
1061 v3_add(&(ai->f),&(ai->f),&force);
1068 * tersoff potential & force for 2 sorts of atoms
1071 /* create mixed terms from parameters and set them */
1072 int tersoff_mult_complete_params(t_tersoff_mult_params *p) {
1074 printf("[moldyn] tersoff parameter completion\n");
1075 p->Smixed=sqrt(p->S[0]*p->S[1]);
1076 p->Rmixed=sqrt(p->R[0]*p->R[1]);
1077 p->Amixed=sqrt(p->A[0]*p->A[1]);
1078 p->Bmixed=sqrt(p->B[0]*p->B[1]);
1079 p->lambda_m=0.5*(p->lambda[0]+p->lambda[1]);
1080 p->mu_m=0.5*(p->mu[0]+p->mu[1]);
1082 printf("[moldyn] tersoff mult parameter info:\n");
1083 printf(" S (m) | %.12f | %.12f | %.12f\n",p->S[0],p->S[1],p->Smixed);
1084 printf(" R (m) | %.12f | %.12f | %.12f\n",p->R[0],p->R[1],p->Rmixed);
1085 printf(" A (eV) | %f | %f | %f\n",p->A[0]/EV,p->A[1]/EV,p->Amixed/EV);
1086 printf(" B (eV) | %f | %f | %f\n",p->B[0]/EV,p->B[1]/EV,p->Bmixed/EV);
1087 printf(" lambda | %f | %f | %f\n",p->lambda[0],p->lambda[1],
1089 printf(" mu | %f | %f | %f\n",p->mu[0],p->mu[1],p->mu_m);
1090 printf(" beta | %.10f | %.10f\n",p->beta[0],p->beta[1]);
1091 printf(" n | %f | %f\n",p->n[0],p->n[1]);
1092 printf(" c | %f | %f\n",p->c[0],p->c[1]);
1093 printf(" d | %f | %f\n",p->d[0],p->d[1]);
1094 printf(" h | %f | %f\n",p->h[0],p->h[1]);
1095 printf(" chi | %f \n",p->chi);
1100 /* tersoff 1 body part */
1101 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
1104 t_tersoff_mult_params *params;
1105 t_tersoff_exchange *exchange;
1108 params=moldyn->pot1b_params;
1109 exchange=&(params->exchange);
1112 * simple: point constant parameters only depending on atom i to
1113 * their right values
1116 exchange->beta=&(params->beta[num]);
1117 exchange->n=&(params->n[num]);
1118 exchange->c=&(params->c[num]);
1119 exchange->d=&(params->d[num]);
1120 exchange->h=&(params->h[num]);
1122 exchange->betan=pow(*(exchange->beta),*(exchange->n));
1123 exchange->n_betan=*(exchange->n)*exchange->betan;
1124 exchange->c2=params->c[num]*params->c[num];
1125 exchange->d2=params->d[num]*params->d[num];
1126 exchange->c2d2=exchange->c2/exchange->d2;
1131 /* tersoff 2 body part */
1132 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1134 t_tersoff_mult_params *params;
1135 t_tersoff_exchange *exchange;
1136 t_3dvec dist_ij,force;
1138 double A,B,R,S,lambda,mu;
1146 params=moldyn->pot2b_params;
1148 exchange=&(params->exchange);
1151 exchange->run2bp_post=0;
1154 * we need: f_c, df_c, f_r, df_r
1156 * therefore we need: R, S, A, lambda
1159 v3_sub(&dist_ij,&(ai->r),&(aj->r));
1161 if(bc) check_per_bound(moldyn,&dist_ij);
1163 d_ij=v3_norm(&dist_ij);
1165 /* save for use in 3bp */
1166 exchange->d_ij=d_ij;
1167 exchange->dist_ij=dist_ij;
1168 exchange->d_ij2=d_ij*d_ij;
1176 lambda=params->lambda[num];
1178 params->exchange.chi=1.0;
1185 lambda=params->lambda_m;
1187 params->exchange.chi=params->chi;
1193 f_r=A*exp(-lambda*d_ij);
1194 df_r=-lambda*f_r/d_ij;
1196 /* f_a, df_a calc + save for later use */
1197 exchange->f_a=-B*exp(-mu*d_ij);
1198 exchange->df_a=-mu*exchange->f_a/d_ij;
1201 /* f_c = 1, df_c = 0 */
1204 v3_scale(&force,&dist_ij,df_r);
1208 arg=M_PI*(d_ij-R)/s_r;
1209 f_c=0.5+0.5*cos(arg);
1210 df_c=-0.5*sin(arg)*(M_PI/(s_r*d_ij));
1211 scale=df_c*f_r+df_r*f_c;
1212 v3_scale(&force,&dist_ij,scale);
1216 v3_add(&(ai->f),&(ai->f),&force);
1217 /* energy is 0.5 f_r f_c ... */
1218 moldyn->energy+=(0.5*f_r*f_c);
1220 /* save for use in 3bp */
1222 exchange->df_c=df_c;
1224 /* enable the run of 3bp function and 2bp post processing */
1226 exchange->run2bp_post=1;
1228 /* reset 3bp sums */
1229 exchange->sum1_3bp=0.0;
1230 exchange->sum2_3bp=0.0;
1231 v3_zero(&(exchange->db_ij));
1236 /* tersoff 2 body post part */
1238 int tersoff_mult_post_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1240 /* here we have to allow for the 3bp sums */
1242 t_tersoff_mult_params *params;
1243 t_tersoff_exchange *exchange;
1245 t_3dvec force,temp,*db_ij,*dist_ij;
1246 double db_ij_scale1,db_ij_scale2;
1248 double f_c,df_c,f_a,df_a;
1253 params=moldyn->pot2b_params;
1254 exchange=&(params->exchange);
1256 /* we do not run if f_c_ij was dtected to be 0! */
1257 if(!(exchange->run2bp_post))
1260 db_ij=&(exchange->db_ij);
1262 df_c=exchange->df_c;
1264 df_a=exchange->df_a;
1265 betan=exchange->betan;
1268 dist_ij=&(exchange->dist_ij);
1270 db_ij_scale1=(1+betan*exchange->sum1_3bp);
1271 db_ij_scale2=(exchange->n_betan*exchange->sum2_3bp);
1272 help=pow(db_ij_scale1,-1.0/(2*n)-1);
1273 b_ij=chi*db_ij_scale1*help;
1274 db_ij_scale1=-chi/(2*n)*help;
1277 v3_scale(db_ij,db_ij,(db_ij_scale1*db_ij_scale2));
1278 v3_scale(db_ij,db_ij,f_a);
1281 v3_scale(&temp,dist_ij,b_ij*df_a);
1283 /* db_ij + df_a part */
1284 v3_add(&force,&temp,db_ij);
1285 v3_scale(&force,&force,f_c);
1288 v3_scale(&temp,dist_ij,f_a*b_ij*df_c);
1290 /* add energy of 3bp sum */
1291 moldyn->energy+=(0.5*f_c*b_ij*f_a);
1293 /* add force of 3bp calculation (all three parts) */
1294 v3_add(&(ai->f),&temp,&force);
1299 /* tersoff 3 body part */
1301 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1303 t_tersoff_mult_params *params;
1304 t_tersoff_exchange *exchange;
1305 t_3dvec dist_ij,dist_ik,dist_jk;
1308 double d_ij,d_ij2,d_ik,d_jk;
1309 double f_c,df_c,f_a,df_a;
1310 double f_c_ik,df_c_ik,arg;
1314 double theta,cos_theta,sin_theta;
1315 double d_theta,d_theta1,d_theta2;
1316 double h_cos,d2_h_cos2;
1317 double frac,bracket,bracket_n_1,bracket_n;
1321 params=moldyn->pot3b_params;
1323 exchange=&(params->exchange);
1325 if(!(exchange->run3bp))
1329 * we need: f_c, d_fc, b_ij, db_ij, f_a, df_a
1331 * we got f_c, df_c, f_a, df_a from 2bp calculation
1334 d_ij=exchange->d_ij;
1335 d_ij2=exchange->d_ij2;
1336 dist_ij=exchange->dist_ij;
1338 f_a=params->exchange.f_a;
1339 df_a=params->exchange.df_a;
1342 df_c=exchange->df_c;
1344 /* d_ij is <= S, as we didn't return so far! */
1347 * calc of b_ij (scalar) and db_ij (vector)
1349 * - for b_ij: chi, beta, f_c_ik, w(=1), c, d, h, n, cos_theta
1351 * - for db_ij: d_theta, sin_theta, cos_theta, f_c_ik, df_c_ik,
1356 v3_sub(&dist_ik,&(ai->r),&(ak->r));
1357 if(bc) check_per_bound(moldyn,&dist_ik);
1358 d_ik=v3_norm(&dist_ik);
1360 /* constants for f_c_ik calc */
1370 /* calc of f_c_ik */
1381 arg=M_PI*(d_ik-R)/s_r;
1382 f_c_ik=0.5+0.5*cos(arg);
1383 df_c_ik=-0.5*sin(arg)*(M_PI/(s_r*d_ik));
1386 v3_sub(&dist_jk,&(aj->r),&(ak->r));
1387 if(bc) check_per_bound(moldyn,&dist_jk);
1388 d_jk=v3_norm(&dist_jk);
1390 /* get exchange data */
1397 c2d2=exchange->c2d2;
1399 numer=d_ij2+d_ik*d_ik-d_jk*d_jk;
1401 cos_theta=numer/denom;
1402 /* prefere law of cosines, dot product -> nan (often) */
1403 //cos_theta=v3_scalar_product(&dist_ij,&dist_ik)/(d_ij*d_ik);
1404 sin_theta=sqrt(1.0-(cos_theta*cos_theta));
1405 theta=acos(cos_theta);
1406 d_theta=(-1.0/sqrt(1.0-cos_theta*cos_theta))/(denom*denom);
1407 d_theta1=2*denom-numer*2*d_ik/d_ij;
1408 d_theta2=2*denom-numer*2*d_ij/d_ik;
1412 h_cos=(h-cos_theta);
1413 d2_h_cos2=d2+(h_cos*h_cos);
1415 frac=c2/(d2_h_cos2);
1422 //printf("Foo -> 0: ");
1426 bracket_n_1=pow(bracket,n-1.0);
1427 bracket_n=bracket_n_1*bracket;
1428 //printf("Foo -> 1: ");
1430 //printf("%.15f %.15f %.15f\n",bracket_n_1,bracket_n,bracket);
1432 /* calc of db_ij and the 2 sums */
1433 exchange->sum1_3bp+=bracket_n;
1434 exchange->sum2_3bp+=bracket_n_1;
1436 /* derivation of theta */
1437 v3_scale(&force,&dist_ij,d_theta1);
1438 v3_scale(&temp,&dist_ik,d_theta2);
1439 v3_add(&force,&force,&temp);
1441 /* part 1 of db_ij */
1442 v3_scale(&force,&force,sin_theta*2*h_cos*f_c_ik*frac/d2_h_cos2);
1444 /* part 2 of db_ij */
1445 v3_scale(&temp,&dist_ik,df_c_ik*g);
1447 /* sum up and add to db_ij */
1448 v3_add(&temp,&temp,&force);
1449 v3_add(&(exchange->db_ij),&(exchange->db_ij),&temp);