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);
961 * periodic boundayr checking
964 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
975 if(moldyn->status&MOLDYN_STAT_PBX) {
976 if(a->x>=x) a->x-=dim->x;
977 else if(-a->x>x) a->x+=dim->x;
979 if(moldyn->status&MOLDYN_STAT_PBY) {
980 if(a->y>=y) a->y-=dim->y;
981 else if(-a->y>y) a->y+=dim->y;
983 if(moldyn->status&MOLDYN_STAT_PBZ) {
984 if(a->z>=z) a->z-=dim->z;
985 else if(-a->z>z) a->z+=dim->z;
996 /* harmonic oscillator potential and force */
998 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1000 t_ho_params *params;
1001 t_3dvec force,distance;
1003 double sc,equi_dist;
1005 params=moldyn->pot2b_params;
1006 sc=params->spring_constant;
1007 equi_dist=params->equilibrium_distance;
1009 v3_sub(&distance,&(ai->r),&(aj->r));
1011 if(bc) check_per_bound(moldyn,&distance);
1012 d=v3_norm(&distance);
1013 if(d<=moldyn->cutoff) {
1014 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
1015 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
1016 v3_scale(&force,&distance,-sc*(1.0-(equi_dist/d)));
1017 v3_add(&(ai->f),&(ai->f),&force);
1023 /* lennard jones potential & force for one sort of atoms */
1025 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1027 t_lj_params *params;
1028 t_3dvec force,distance;
1030 double eps,sig6,sig12;
1032 params=moldyn->pot2b_params;
1033 eps=params->epsilon4;
1034 sig6=params->sigma6;
1035 sig12=params->sigma12;
1037 v3_sub(&distance,&(ai->r),&(aj->r));
1038 if(bc) check_per_bound(moldyn,&distance);
1039 d=v3_absolute_square(&distance); /* 1/r^2 */
1040 if(d<=moldyn->cutoff_square) {
1041 d=1.0/d; /* 1/r^2 */
1044 h1=h2*h2; /* 1/r^12 */
1045 /* energy is eps*..., but we will add this twice ... */
1046 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
1053 v3_scale(&force,&distance,d);
1054 v3_add(&(ai->f),&(ai->f),&force);
1061 * tersoff potential & force for 2 sorts of atoms
1064 /* create mixed terms from parameters and set them */
1065 int tersoff_mult_complete_params(t_tersoff_mult_params *p) {
1067 printf("[moldyn] tersoff parameter completion\n");
1068 p->Smixed=sqrt(p->S[0]*p->S[1]);
1069 p->Rmixed=sqrt(p->R[0]*p->R[1]);
1070 p->Amixed=sqrt(p->A[0]*p->A[1]);
1071 p->Bmixed=sqrt(p->B[0]*p->B[1]);
1072 p->lambda_m=0.5*(p->lambda[0]+p->lambda[1]);
1073 p->mu_m=0.5*(p->mu[0]+p->mu[1]);
1078 /* tersoff 1 body part */
1079 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
1082 t_tersoff_mult_params *params;
1083 t_tersoff_exchange *exchange;
1086 params=moldyn->pot1b_params;
1087 exchange=&(params->exchange);
1090 * simple: point constant parameters only depending on atom i to
1091 * their right values
1094 exchange->beta=&(params->beta[num]);
1095 exchange->n=&(params->n[num]);
1096 exchange->c=&(params->c[num]);
1097 exchange->d=&(params->d[num]);
1098 exchange->h=&(params->h[num]);
1100 exchange->betan=pow(*(exchange->beta),*(exchange->n));
1101 exchange->c2=params->c[num]*params->c[num];
1102 exchange->d2=params->d[num]*params->d[num];
1103 exchange->c2d2=exchange->c2/exchange->d2;
1108 /* tersoff 2 body part */
1109 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1111 t_tersoff_mult_params *params;
1112 t_tersoff_exchange *exchange;
1113 t_3dvec dist_ij,force;
1115 double A,B,R,S,lambda,mu;
1123 params=moldyn->pot2b_params;
1125 exchange=&(params->exchange);
1130 * we need: f_c, df_c, f_r, df_r
1132 * therefore we need: R, S, A, lambda
1135 v3_sub(&dist_ij,&(ai->r),&(aj->r));
1137 if(bc) check_per_bound(moldyn,&dist_ij);
1139 d_ij=v3_norm(&dist_ij);
1141 /* save for use in 3bp */
1142 exchange->dist_ij=dist_ij; /* <- needed ? */
1143 exchange->d_ij=d_ij;
1150 lambda=params->lambda[num];
1151 /* more constants depending of atoms i and j, needed in 3bp */
1152 params->exchange.B=&(params->B[num]);
1153 params->exchange.mu=&(params->mu[num]);
1155 params->exchange.chi=1.0;
1161 lambda=params->lambda_m;
1162 /* more constants depending of atoms i and j, needed in 3bp */
1163 params->exchange.B=&(params->Bmixed);
1164 params->exchange.mu=&(params->mu_m);
1166 params->exchange.chi=params->chi;
1172 f_r=A*exp(-lambda*d_ij);
1173 df_r=-lambda*f_r/d_ij;
1175 /* f_a, df_a calc + save for 3bp use */
1176 exchange->f_a=-B*exp(-mu*d_ij);
1177 exchange->df_a=-mu*exchange->f_a/d_ij;
1180 /* f_c = 1, df_c = 0 */
1183 v3_scale(&force,&dist_ij,df_r);
1187 arg=M_PI*(d_ij-R)/s_r;
1188 f_c=0.5+0.5*cos(arg);
1189 df_c=-0.5*sin(arg)*(M_PI/(s_r*d_ij));
1190 scale=df_c*f_r+df_r*f_c;
1191 v3_scale(&force,&dist_ij,scale);
1195 v3_add(&(ai->f),&(ai->f),&force);
1196 /* energy is 0.5 f_r f_c ... */
1197 moldyn->energy+=(0.5*f_r*f_c);
1199 /* save for use in 3bp */
1201 exchange->df_c=df_c;
1203 /* enable the run of 3bp function */
1209 /* tersoff 3 body part */
1211 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1213 t_tersoff_mult_params *params;
1214 t_tersoff_exchange *exchange;
1215 t_3dvec dist_ij,dist_ik,dist_jk;
1218 double d_ij,d_ij2,d_ik,d_jk;
1219 double f_c,df_c,b_ij,f_a,df_a;
1220 double f_c_ik,df_c_ik,arg;
1223 double n,c,d,h,beta,betan;
1226 double theta,cos_theta,sin_theta;
1227 double d_theta,d_theta1,d_theta2;
1228 double h_cos,h_cos2,d2_h_cos2;
1229 double frac1,bracket1,bracket2,bracket2_n_1,bracket2_n;
1230 double bracket3,bracket3_pow_1,bracket3_pow;
1233 params=moldyn->pot3b_params;
1235 exchange=&(params->exchange);
1237 if(!(exchange->run3bp))
1241 * we need: f_c, d_fc, b_ij, db_ij, f_a, df_a
1243 * we got f_c, df_c, f_a, df_a from 2bp calculation
1246 d_ij=exchange->d_ij;
1247 d_ij2=exchange->d_ij2;
1249 f_a=params->exchange.f_a;
1250 df_a=params->exchange.df_a;
1252 /* d_ij is <= S, as we didn't return so far! */
1255 * calc of b_ij (scalar) and db_ij (vector)
1257 * - for b_ij: chi, beta, f_c_ik, w(=1), c, d, h, n, cos_theta
1259 * - for db_ij: d_theta, sin_theta, cos_theta, f_c_ik, df_c_ik,
1265 v3_sub(&dist_ik,&(ai->r),&(ak->r));
1266 if(bc) check_per_bound(moldyn,&dist_ik);
1267 d_ik=v3_norm(&dist_ik);
1269 /* constants for f_c_ik calc */
1279 /* calc of f_c_ik */
1284 /* f_c_ik = 1, df_c_ik = 0 */
1290 arg=M_PI*(d_ik-R)/s_r;
1291 f_c_ik=0.5+0.5*cos(arg);
1292 df_c_ik=-0.5*sin(arg)*(M_PI/(s_r*d_ik));
1295 v3_sub(&dist_jk,&(aj->r),&(ak->r));
1296 if(bc) check_per_bound(moldyn,&dist_jk);
1297 d_jk=v3_norm(&dist_jk);
1299 beta=*(exchange->beta);
1300 betan=exchange->betan;
1307 c2d2=exchange->c2d2;
1309 numer=d_ij2+d_ik*d_ik-d_jk*d_jk;
1311 cos_theta=numer/denom;
1312 sin_theta=sqrt(1.0-(cos_theta*cos_theta));
1313 theta=acos(cos_theta);
1314 d_theta=(-1.0/sqrt(1.0-cos_theta*cos_theta))/(denom*denom);
1315 d_theta1=2*denom-numer*2*d_ik/d_ij;
1316 d_theta2=2*denom-numer*2*d_ij/d_ik;
1320 h_cos=(h-cos_theta);
1322 d2_h_cos2=d2-h_cos2;
1324 /* some usefull expressions */
1325 frac1=c2/(d2-h_cos2);
1326 bracket1=1+c2d2-frac1;
1327 bracket2=f_c_ik*bracket1;
1328 bracket2_n_1=pow(bracket2,n-1.0);
1329 bracket2_n=bracket2_n_1*bracket2;
1330 bracket3=1+betan*bracket2_n;
1331 bracket3_pow_1=pow(bracket3,(-1.0/(2.0*n))-1.0);
1332 bracket3_pow=bracket3_pow_1*bracket3;
1334 /* now go on with calc of b_ij and derivation of b_ij */
1335 b_ij=chi*bracket3_pow;
1337 /* derivation of theta */
1338 v3_scale(&force,&dist_ij,d_theta1);
1339 v3_scale(&temp,&dist_ik,d_theta2);
1340 v3_add(&force,&force,&temp);
1342 /* part 1 of derivation of b_ij */
1343 v3_scale(&force,&force,sin_theta*2*h_cos*f_c_ik*frac1);
1345 /* part 2 of derivation of b_ij */
1346 v3_scale(&temp,&dist_ik,df_c_ik*bracket1);
1348 /* sum up and scale ... */
1349 v3_add(&temp,&temp,&force);
1350 scale=bracket2_n_1*n*betan*(1+betan*bracket3_pow_1)*chi*(1.0/(2.0*n));
1351 v3_scale(&temp,&temp,scale);
1353 /* now construct an energy and a force out of that */
1354 v3_scale(&temp,&temp,f_a);
1355 v3_scale(&force,&dist_ij,df_a*b_ij);
1356 v3_add(&temp,&temp,&force);
1357 v3_scale(&temp,&temp,f_c);
1358 v3_scale(&force,&dist_ij,df_c*b_ij*f_a);
1359 v3_add(&force,&force,&temp);
1362 v3_add(&(ai->f),&(ai->f),&force);
1363 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1364 moldyn->energy+=(0.25*f_a*b_ij*f_c);