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_potential3b(t_moldyn *moldyn,pf_func3b func,void *params) {
145 moldyn->pot3b_params=params;
150 int moldyn_set_log(t_moldyn *moldyn,u8 type,char *fb,int timer) {
153 case LOG_TOTAL_ENERGY:
154 moldyn->ewrite=timer;
155 moldyn->efd=open(fb,O_WRONLY|O_CREAT|O_TRUNC);
157 perror("[moldyn] efd open");
160 dprintf(moldyn->efd,"# total energy log file\n");
162 case LOG_TOTAL_MOMENTUM:
163 moldyn->mwrite=timer;
164 moldyn->mfd=open(fb,O_WRONLY|O_CREAT|O_TRUNC);
166 perror("[moldyn] mfd open");
169 dprintf(moldyn->efd,"# total momentum log file\n");
172 moldyn->swrite=timer;
173 strncpy(moldyn->sfb,fb,63);
176 moldyn->vwrite=timer;
177 strncpy(moldyn->vfb,fb,63);
178 visual_init(&(moldyn->vis),fb);
181 printf("unknown log mechanism: %02x\n",type);
188 int moldyn_log_shutdown(t_moldyn *moldyn) {
190 printf("[moldyn] log shutdown\n");
191 if(moldyn->efd) close(moldyn->efd);
192 if(moldyn->mfd) close(moldyn->mfd);
193 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
198 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
199 u8 attr,u8 bnum,int a,int b,int c) {
207 if(type==FCC) count*=4;
209 if(type==DIAMOND) count*=8;
211 moldyn->atom=malloc(count*sizeof(t_atom));
212 if(moldyn->atom==NULL) {
213 perror("malloc (atoms)");
221 ret=fcc_init(a,b,c,lc,moldyn->atom,&origin);
224 ret=diamond_init(a,b,c,lc,moldyn->atom,&origin);
227 printf("unknown lattice type (%02x)\n",type);
233 printf("ok, there is something wrong ...\n");
234 printf("calculated -> %d atoms\n",count);
235 printf("created -> %d atoms\n",ret);
240 printf("[moldyn] created lattice with %d atoms\n",count);
244 moldyn->atom[count].element=element;
245 moldyn->atom[count].mass=mass;
246 moldyn->atom[count].attr=attr;
247 moldyn->atom[count].bnum=bnum;
248 check_per_bound(moldyn,&(moldyn->atom[count].r));
255 int add_atom(t_moldyn *moldyn,int element,double mass,u8 bnum,u8 attr,
256 t_3dvec *r,t_3dvec *v) {
263 count=++(moldyn->count);
265 ptr=realloc(atom,count*sizeof(t_atom));
267 perror("[moldyn] realloc (add atom)");
275 atom[count-1].element=element;
276 atom[count-1].mass=mass;
277 atom[count-1].bnum=bnum;
278 atom[count-1].attr=attr;
283 int destroy_atoms(t_moldyn *moldyn) {
285 if(moldyn->atom) free(moldyn->atom);
290 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
293 * - gaussian distribution of velocities
294 * - zero total momentum
295 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
300 t_3dvec p_total,delta;
305 random=&(moldyn->random);
307 /* gaussian distribution of velocities */
309 for(i=0;i<moldyn->count;i++) {
310 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
312 v=sigma*rand_get_gauss(random);
314 p_total.x+=atom[i].mass*v;
316 v=sigma*rand_get_gauss(random);
318 p_total.y+=atom[i].mass*v;
320 v=sigma*rand_get_gauss(random);
322 p_total.z+=atom[i].mass*v;
325 /* zero total momentum */
326 v3_scale(&p_total,&p_total,1.0/moldyn->count);
327 for(i=0;i<moldyn->count;i++) {
328 v3_scale(&delta,&p_total,1.0/atom[i].mass);
329 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
332 /* velocity scaling */
333 scale_velocity(moldyn,equi_init);
338 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
348 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
351 /* get kinetic energy / temperature & count involved atoms */
354 for(i=0;i<moldyn->count;i++) {
355 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
356 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
360 if(count!=0) moldyn->t=(2.0*e)/(3.0*count*K_BOLTZMANN);
361 else return 0; /* no atoms involved in scaling! */
363 /* (temporary) hack for e,t = 0 */
366 if(moldyn->t_ref!=0.0)
367 thermal_init(moldyn,equi_init);
369 return 0; /* no scaling needed */
373 /* get scaling factor */
374 scale=moldyn->t_ref/moldyn->t;
378 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
379 scale=1.0+moldyn->tau*(scale-1.0)/moldyn->t_tc;
382 /* velocity scaling */
383 for(i=0;i<moldyn->count;i++)
384 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
385 v3_scale(&(atom[i].v),&(atom[i].v),scale);
390 double get_e_kin(t_moldyn *moldyn) {
398 for(i=0;i<moldyn->count;i++)
399 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
404 double get_e_pot(t_moldyn *moldyn) {
406 return moldyn->energy;
409 double update_e_kin(t_moldyn *moldyn) {
411 return(get_e_kin(moldyn));
414 double get_total_energy(t_moldyn *moldyn) {
416 return(moldyn->ekin+moldyn->energy);
419 t_3dvec get_total_p(t_moldyn *moldyn) {
428 for(i=0;i<moldyn->count;i++) {
429 v3_scale(&p,&(atom[i].v),atom[i].mass);
430 v3_add(&p_total,&p_total,&p);
436 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
440 /* nn_dist is the nearest neighbour distance */
443 printf("[moldyn] i do not estimate timesteps below %f K!\n",
444 MOLDYN_CRITICAL_EST_TEMP);
448 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
457 /* linked list / cell method */
459 int link_cell_init(t_moldyn *moldyn) {
465 fd=open("/dev/null",O_WRONLY);
469 /* partitioning the md cell */
470 lc->nx=moldyn->dim.x/moldyn->cutoff;
471 lc->x=moldyn->dim.x/lc->nx;
472 lc->ny=moldyn->dim.y/moldyn->cutoff;
473 lc->y=moldyn->dim.y/lc->ny;
474 lc->nz=moldyn->dim.z/moldyn->cutoff;
475 lc->z=moldyn->dim.z/lc->nz;
477 lc->cells=lc->nx*lc->ny*lc->nz;
478 lc->subcell=malloc(lc->cells*sizeof(t_list));
480 printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
482 for(i=0;i<lc->cells;i++)
483 //list_init(&(lc->subcell[i]),1);
484 list_init(&(lc->subcell[i]),fd);
486 link_cell_update(moldyn);
491 int link_cell_update(t_moldyn *moldyn) {
505 for(i=0;i<lc->cells;i++)
506 list_destroy(&(moldyn->lc.subcell[i]));
508 for(count=0;count<moldyn->count;count++) {
509 i=(atom[count].r.x+(moldyn->dim.x/2))/lc->x;
510 j=(atom[count].r.y+(moldyn->dim.y/2))/lc->y;
511 k=(atom[count].r.z+(moldyn->dim.z/2))/lc->z;
512 list_add_immediate_ptr(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
519 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
537 cell[0]=lc->subcell[i+j*nx+k*a];
538 for(ci=-1;ci<=1;ci++) {
545 for(cj=-1;cj<=1;cj++) {
552 for(ck=-1;ck<=1;ck++) {
559 if(!(ci|cj|ck)) continue;
561 cell[--count2]=lc->subcell[x+y*nx+z*a];
564 cell[count1++]=lc->subcell[x+y*nx+z*a];
576 int link_cell_shutdown(t_moldyn *moldyn) {
583 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
584 list_shutdown(&(moldyn->lc.subcell[i]));
589 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
593 t_moldyn_schedule *schedule;
595 schedule=&(moldyn->schedule);
596 count=++(schedule->content_count);
598 ptr=realloc(moldyn->schedule.runs,count*sizeof(int));
600 perror("[moldyn] realloc (runs)");
603 moldyn->schedule.runs=ptr;
604 moldyn->schedule.runs[count-1]=runs;
606 ptr=realloc(schedule->tau,count*sizeof(double));
608 perror("[moldyn] realloc (tau)");
611 moldyn->schedule.tau=ptr;
612 moldyn->schedule.tau[count-1]=tau;
617 int moldyn_set_schedule_hook(t_moldyn *moldyn,void *hook,void *hook_params) {
619 moldyn->schedule.hook=hook;
620 moldyn->schedule.hook_params=hook_params;
627 * 'integration of newtons equation' - algorithms
631 /* start the integration */
633 int moldyn_integrate(t_moldyn *moldyn) {
636 unsigned int e,m,s,v;
638 t_moldyn_schedule *schedule;
644 schedule=&(moldyn->schedule);
647 /* initialize linked cell method */
648 link_cell_init(moldyn);
650 /* logging & visualization */
656 /* sqaure of some variables */
657 moldyn->tau_square=moldyn->tau*moldyn->tau;
658 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
659 /* calculate initial forces */
660 potential_force_calc(moldyn);
662 /* do some checks before we actually start calculating bullshit */
663 if(moldyn->cutoff>0.5*moldyn->dim.x)
664 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
665 if(moldyn->cutoff>0.5*moldyn->dim.y)
666 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
667 if(moldyn->cutoff>0.5*moldyn->dim.z)
668 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
669 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
670 if(ds>0.05*moldyn->nnd)
671 printf("[moldyn] warning: forces too high / tau too small!\n");
673 /* zero absolute time */
675 for(sched=0;sched<moldyn->schedule.content_count;sched++) {
677 /* setting amount of runs and finite time step size */
678 moldyn->tau=schedule->tau[sched];
679 moldyn->tau_square=moldyn->tau*moldyn->tau;
680 moldyn->time_steps=schedule->runs[sched];
682 /* integration according to schedule */
684 for(i=0;i<moldyn->time_steps;i++) {
686 /* integration step */
687 moldyn->integrate(moldyn);
690 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
691 scale_velocity(moldyn,FALSE);
693 /* increase absolute time */
694 moldyn->time+=moldyn->tau;
696 /* check for log & visualization */
700 "%.15f %.45f %.45f %.45f\n",
701 moldyn->time,update_e_kin(moldyn),
703 get_total_energy(moldyn));
707 p=get_total_p(moldyn);
709 "%.15f %.45f\n",moldyn->time,
715 snprintf(fb,128,"%s-%f-%.15f.save",moldyn->sfb,
716 moldyn->t,i*moldyn->tau);
717 fd=open(fb,O_WRONLY|O_TRUNC|O_CREAT);
718 if(fd<0) perror("[moldyn] save fd open");
720 write(fd,moldyn,sizeof(t_moldyn));
721 write(fd,moldyn->atom,
722 moldyn->count*sizeof(t_atom));
729 visual_atoms(&(moldyn->vis),moldyn->time,
730 moldyn->atom,moldyn->count);
731 printf("\rsched: %d, steps: %d",sched,i);
738 /* check for hooks */
740 schedule->hook(moldyn,schedule->hook_params);
747 /* velocity verlet */
749 int velocity_verlet(t_moldyn *moldyn) {
752 double tau,tau_square;
759 tau_square=moldyn->tau_square;
761 for(i=0;i<count;i++) {
763 v3_scale(&delta,&(atom[i].v),tau);
764 v3_add(&(atom[i].r),&(atom[i].r),&delta);
765 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
766 v3_add(&(atom[i].r),&(atom[i].r),&delta);
767 check_per_bound(moldyn,&(atom[i].r));
770 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
771 v3_add(&(atom[i].v),&(atom[i].v),&delta);
774 /* neighbour list update */
775 link_cell_update(moldyn);
777 /* forces depending on chosen potential */
778 potential_force_calc(moldyn);
779 //moldyn->potential_force_function(moldyn);
781 for(i=0;i<count;i++) {
782 /* again velocities */
783 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
784 v3_add(&(atom[i].v),&(atom[i].v),&delta);
793 * potentials & corresponding forces
797 /* generic potential and force calculation */
799 int potential_force_calc(t_moldyn *moldyn) {
802 t_atom *itom,*jtom,*ktom;
804 t_list neighbour_i[27];
805 t_list neighbour_i2[27];
806 //t_list neighbour_j[27];
818 for(i=0;i<count;i++) {
821 v3_zero(&(itom[i].f));
823 /* single particle potential/force */
824 if(itom[i].attr&ATOM_ATTR_1BP)
825 moldyn->func1b(moldyn,&(itom[i]));
827 /* 2 body pair potential/force */
828 if(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)) {
830 link_cell_neighbour_index(moldyn,
831 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
832 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
833 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
839 for(j=0;j<countn;j++) {
841 this=&(neighbour_i[j]);
844 if(this->start==NULL)
850 jtom=this->current->data;
855 if((jtom->attr&ATOM_ATTR_2BP)&
856 (itom[i].attr&ATOM_ATTR_2BP))
857 moldyn->func2b(moldyn,
862 /* 3 body potential/force */
864 if(!(itom[i].attr&ATOM_ATTR_3BP)||
865 !(jtom->attr&ATOM_ATTR_3BP))
869 * according to mr. nordlund, we dont need to take the
870 * sum over all atoms now, as 'this is centered' around
872 * i am not quite sure though! there is a not vanishing
873 * part even if f_c_ik is zero ...
874 * this analytical potentials suck!
875 * switching from mc to md to dft soon!
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 mlodyn->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->c2=params->c[num]*params->c[num];
1124 exchange->d2=params->d[num]*params->d[num];
1125 exchange->c2d2=exchange->c2/exchange->d2;
1130 /* tersoff 2 body part */
1131 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1133 t_tersoff_mult_params *params;
1134 t_tersoff_exchange *exchange;
1135 t_3dvec dist_ij,force;
1137 double A,B,R,S,lambda,mu;
1145 params=moldyn->pot2b_params;
1147 exchange=&(params->exchange);
1152 * we need: f_c, df_c, f_r, df_r
1154 * therefore we need: R, S, A, lambda
1157 v3_sub(&dist_ij,&(ai->r),&(aj->r));
1159 if(bc) check_per_bound(moldyn,&dist_ij);
1161 d_ij=v3_norm(&dist_ij);
1163 /* save for use in 3bp */
1164 exchange->d_ij=d_ij;
1165 exchange->dist_ij=dist_ij;
1166 exchange->d_ij2=d_ij*d_ij;
1174 lambda=params->lambda[num];
1176 params->exchange.chi=1.0;
1183 lambda=params->lambda_m;
1185 params->exchange.chi=params->chi;
1191 f_r=A*exp(-lambda*d_ij);
1192 df_r=-lambda*f_r/d_ij;
1194 /* f_a, df_a calc + save for 3bp use */
1195 exchange->f_a=-B*exp(-mu*d_ij);
1196 exchange->df_a=-mu*exchange->f_a/d_ij;
1199 /* f_c = 1, df_c = 0 */
1202 v3_scale(&force,&dist_ij,df_r);
1206 arg=M_PI*(d_ij-R)/s_r;
1207 f_c=0.5+0.5*cos(arg);
1208 df_c=-0.5*sin(arg)*(M_PI/(s_r*d_ij));
1209 scale=df_c*f_r+df_r*f_c;
1210 v3_scale(&force,&dist_ij,scale);
1214 v3_add(&(ai->f),&(ai->f),&force);
1215 /* energy is 0.5 f_r f_c ... */
1216 moldyn->energy+=(0.5*f_r*f_c);
1218 /* save for use in 3bp */
1220 exchange->df_c=df_c;
1222 /* enable the run of 3bp function */
1225 /* reset 3bp sums */
1226 exchange->3bp_sum1=0.0;
1227 exchange->3bp_sum2=0.0;
1232 /* tersoff 2 body post part */
1234 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1236 /* here we have to allow for the 3bp sums */
1238 t_tersoff_mult_params *params;
1239 t_tersoff_exchange *exchange;
1241 t_3dvec force,temp,*db_ij;
1242 double db_ij_scale1,db_ij_scale2;
1244 double f_c,df_c,f_a,df_a;
1246 params=moldyn->pot2b_params;
1247 exchange=&(moldyn->exchange);
1249 db_ij=&(exchange->db_ij);
1251 df_c=exchange->df_c;
1253 df_a=exchange->df_a;
1255 db_ij_scale1=(1+betan*3bp_sum1);
1256 db_ij_scale2=(n*betan*3bp_sum2);
1257 help=pow(db_ij_scale1,-1.0/(2*n)-1);
1258 b_ij=chi*db_ij_scale1*help;
1259 db_ij_scale1=-chi/(2*n)*help;
1261 v3_scale(db_ij,db_ij,(db_ij_scale1*db_ij_scale2));
1262 v3_scale(db_ij,db_ij,f_a);
1264 v3_scale(&temp,dist_ij,b_ij*df_a);
1266 v3_add(&force,&temp,db_ij);
1267 v3_scale(&force,&force,f_c);
1269 v3_scale(&temp,&dist_ij,f_a*b_ij*df_c);
1271 /* add energy of 3bp sum */
1272 moldyn->energy+=(0.5*f_c*b_ij*f_a);
1273 /* add force of 3bp calculation */
1274 v3_add(&(ai->f),&temp,&force);
1279 /* tersoff 3 body part */
1281 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1283 t_tersoff_mult_params *params;
1284 t_tersoff_exchange *exchange;
1285 t_3dvec dist_ij,dist_ik,dist_jk;
1288 double d_ij,d_ij2,d_ik,d_jk;
1289 double f_c,df_c,b_ij,f_a,df_a;
1290 double f_c_ik,df_c_ik,arg;
1293 double n,c,d,h,beta,betan;
1296 double theta,cos_theta,sin_theta;
1297 double d_theta,d_theta1,d_theta2;
1298 double h_cos,h_cos2,d2_h_cos2;
1299 double frac1,bracket1,bracket2,bracket2_n_1,bracket2_n;
1300 double bracket3,bracket3_pow_1,bracket3_pow;
1303 params=moldyn->pot3b_params;
1305 exchange=&(params->exchange);
1307 if(!(exchange->run3bp))
1311 * we need: f_c, d_fc, b_ij, db_ij, f_a, df_a
1313 * we got f_c, df_c, f_a, df_a from 2bp calculation
1316 d_ij=exchange->d_ij;
1317 d_ij2=exchange->d_ij2;
1318 dist_ij=exchange->dist_ij;
1320 f_a=params->exchange.f_a;
1321 df_a=params->exchange.df_a;
1324 df_c=exchange->df_c;
1326 /* d_ij is <= S, as we didn't return so far! */
1329 * calc of b_ij (scalar) and db_ij (vector)
1331 * - for b_ij: chi, beta, f_c_ik, w(=1), c, d, h, n, cos_theta
1333 * - for db_ij: d_theta, sin_theta, cos_theta, f_c_ik, df_c_ik,
1338 v3_sub(&dist_ik,&(ai->r),&(ak->r));
1339 if(bc) check_per_bound(moldyn,&dist_ik);
1340 d_ik=v3_norm(&dist_ik);
1342 /* constants for f_c_ik calc */
1352 /* calc of f_c_ik */
1363 arg=M_PI*(d_ik-R)/s_r;
1364 f_c_ik=0.5+0.5*cos(arg);
1365 df_c_ik=-0.5*sin(arg)*(M_PI/(s_r*d_ik));
1368 v3_sub(&dist_jk,&(aj->r),&(ak->r));
1369 if(bc) check_per_bound(moldyn,&dist_jk);
1370 d_jk=v3_norm(&dist_jk);
1372 beta=*(exchange->beta);
1373 betan=exchange->betan;
1381 c2d2=exchange->c2d2;
1383 numer=d_ij2+d_ik*d_ik-d_jk*d_jk;
1385 cos_theta=numer/denom;
1386 //cos_theta=v3_scalar_product(&dist_ij,&dist_ik)/(d_ij*d_ik);
1387 sin_theta=sqrt(1.0-(cos_theta*cos_theta));
1388 theta=acos(cos_theta);
1389 d_theta=(-1.0/sqrt(1.0-cos_theta*cos_theta))/(denom*denom);
1390 d_theta1=2*denom-numer*2*d_ik/d_ij;
1391 d_theta2=2*denom-numer*2*d_ij/d_ik;
1395 h_cos=(h-cos_theta);
1397 d2_h_cos2=d2+h_cos2;
1399 /* some usefull expressions */
1400 frac1=c2/(d2_h_cos2);
1401 bracket1=1+c2d2-frac1;
1407 printf("Foo -> 0: ");
1410 bracket2=f_c_ik*bracket1;
1411 bracket2_n_1=pow(bracket2,n-1.0);
1412 bracket2_n=bracket2_n_1*bracket2;
1413 bracket3=1.0+betan*bracket2_n;
1414 printf("Foo -> 1: ");
1416 bracket3_pow_1=pow(bracket3,(-1.0/(2.0*n))-1.0);
1417 bracket3_pow=bracket3_pow_1*bracket3;
1418 printf("%.15f %.15f %.15f\n",bracket2_n_1,bracket2_n);
1420 /* now go on with calc of b_ij and derivation of b_ij */
1421 b_ij=chi*bracket3_pow;
1423 /* derivation of theta */
1424 v3_scale(&force,&dist_ij,d_theta1);
1425 v3_scale(&temp,&dist_ik,d_theta2);
1426 v3_add(&force,&force,&temp);
1428 /* part 1 of derivation of b_ij */
1429 v3_scale(&force,&force,sin_theta*2*h_cos*f_c_ik*frac1);
1431 /* part 2 of derivation of b_ij */
1432 v3_scale(&temp,&dist_ik,df_c_ik*bracket1);
1434 /* sum up and scale ... */
1435 v3_add(&temp,&temp,&force);
1436 scale=bracket2_n_1*n*betan*(1+betan*bracket3_pow_1)*chi*(1.0/(2.0*n));
1437 v3_scale(&temp,&temp,scale);
1439 /* now construct an energy and a force out of that */
1440 v3_scale(&temp,&temp,f_a);
1441 v3_scale(&force,&dist_ij,df_a*b_ij);
1442 v3_add(&temp,&temp,&force);
1443 v3_scale(&temp,&temp,f_c);
1444 v3_scale(&force,&dist_ij,df_c*b_ij*f_a);
1445 v3_add(&force,&force,&temp);
1448 v3_add(&(ai->f),&(ai->f),&force);
1449 /* energy is 0.5 f_r f_c */
1450 moldyn->energy+=(0.5*f_a*b_ij*f_c);