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) {
81 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
96 int set_nn_dist(t_moldyn *moldyn,double dist) {
103 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
106 moldyn->status|=MOLDYN_STAT_PBX;
109 moldyn->status|=MOLDYN_STAT_PBY;
112 moldyn->status|=MOLDYN_STAT_PBZ;
117 int set_potential1b(t_moldyn *moldyn,pf_func1b func,void *params) {
120 moldyn->pot1b_params=params;
125 int set_potential2b(t_moldyn *moldyn,pf_func2b func,void *params) {
128 moldyn->pot2b_params=params;
133 int set_potential3b(t_moldyn *moldyn,pf_func3b func,void *params) {
136 moldyn->pot3b_params=params;
141 int moldyn_set_log(t_moldyn *moldyn,u8 type,char *fb,int timer) {
144 case LOG_TOTAL_ENERGY:
145 moldyn->ewrite=timer;
146 moldyn->efd=open(fb,O_WRONLY|O_CREAT|O_TRUNC);
148 perror("[moldyn] efd open");
151 dprintf(moldyn->efd,"# total energy log file\n");
153 case LOG_TOTAL_MOMENTUM:
154 moldyn->mwrite=timer;
155 moldyn->mfd=open(fb,O_WRONLY|O_CREAT|O_TRUNC);
157 perror("[moldyn] mfd open");
160 dprintf(moldyn->efd,"# total momentum log file\n");
163 moldyn->swrite=timer;
164 strncpy(moldyn->sfb,fb,63);
167 moldyn->vwrite=timer;
168 strncpy(moldyn->vfb,fb,63);
169 visual_init(&(moldyn->vis),fb);
172 printf("unknown log mechanism: %02x\n",type);
179 int moldyn_log_shutdown(t_moldyn *moldyn) {
181 printf("[moldyn] log shutdown\n");
182 if(moldyn->efd) close(moldyn->efd);
183 if(moldyn->mfd) close(moldyn->mfd);
184 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
189 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
190 u8 attr,u8 bnum,int a,int b,int c) {
198 if(type==FCC) count*=4;
200 if(type==DIAMOND) count*=8;
202 moldyn->atom=malloc(count*sizeof(t_atom));
203 if(moldyn->atom==NULL) {
204 perror("malloc (atoms)");
212 ret=fcc_init(a,b,c,lc,moldyn->atom,&origin);
215 ret=diamond_init(a,b,c,lc,moldyn->atom,&origin);
218 printf("unknown lattice type (%02x)\n",type);
224 printf("ok, there is something wrong ...\n");
225 printf("calculated -> %d atoms\n",count);
226 printf("created -> %d atoms\n",ret);
231 printf("[moldyn] created lattice with %d atoms\n",count);
235 moldyn->atom[count].element=element;
236 moldyn->atom[count].mass=mass;
237 moldyn->atom[count].attr=attr;
238 moldyn->atom[count].bnum=bnum;
239 check_per_bound(moldyn,&(moldyn->atom[count].r));
246 int add_atom(t_moldyn *moldyn,int element,double mass,u8 bnum,u8 attr,
247 t_3dvec *r,t_3dvec *v) {
254 count=++(moldyn->count);
256 ptr=realloc(atom,count*sizeof(t_atom));
258 perror("[moldyn] realloc (add atom)");
266 atom[count-1].element=element;
267 atom[count-1].mass=mass;
268 atom[count-1].bnum=bnum;
269 atom[count-1].attr=attr;
274 int destroy_atoms(t_moldyn *moldyn) {
276 if(moldyn->atom) free(moldyn->atom);
281 int thermal_init(t_moldyn *moldyn) {
284 * - gaussian distribution of velocities
285 * - zero total momentum
286 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
291 t_3dvec p_total,delta;
296 random=&(moldyn->random);
298 /* gaussian distribution of velocities */
300 for(i=0;i<moldyn->count;i++) {
301 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t/atom[i].mass);
303 v=sigma*rand_get_gauss(random);
305 p_total.x+=atom[i].mass*v;
307 v=sigma*rand_get_gauss(random);
309 p_total.y+=atom[i].mass*v;
311 v=sigma*rand_get_gauss(random);
313 p_total.z+=atom[i].mass*v;
316 /* zero total momentum */
317 v3_scale(&p_total,&p_total,1.0/moldyn->count);
318 for(i=0;i<moldyn->count;i++) {
319 v3_scale(&delta,&p_total,1.0/atom[i].mass);
320 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
323 /* velocity scaling */
324 scale_velocity(moldyn,VSCALE_INIT_EQUI);
329 int scale_velocity(t_moldyn *moldyn,u8 type) {
339 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
344 for(i=0;i<moldyn->count;i++) {
345 if(atom[i].attr&ATOM_ATTR_HB) {
346 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
351 /* temporary hack for e,t = 0 */
354 thermal_init(moldyn);
360 scale=(1.5*count*K_BOLTZMANN*moldyn->t)/e;
361 if(type&VSCALE_INIT_EQUI) scale*=2.0; /* equipartition theorem */
363 for(i=0;i<moldyn->count;i++)
364 if(atom[i].attr&ATOM_ATTR_HB)
365 v3_scale(&(atom[i].v),&(atom[i].v),scale);
370 double get_e_kin(t_moldyn *moldyn) {
378 for(i=0;i<moldyn->count;i++)
379 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
384 double get_e_pot(t_moldyn *moldyn) {
386 return moldyn->energy;
389 double update_e_kin(t_moldyn *moldyn) {
391 return(get_e_kin(moldyn));
394 double get_total_energy(t_moldyn *moldyn) {
396 return(moldyn->ekin+moldyn->energy);
399 t_3dvec get_total_p(t_moldyn *moldyn) {
408 for(i=0;i<moldyn->count;i++) {
409 v3_scale(&p,&(atom[i].v),atom[i].mass);
410 v3_add(&p_total,&p_total,&p);
416 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
420 /* nn_dist is the nearest neighbour distance */
423 printf("[moldyn] i do not estimate timesteps below %f K!\n",
424 MOLDYN_CRITICAL_EST_TEMP);
428 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
437 /* linked list / cell method */
439 int link_cell_init(t_moldyn *moldyn) {
445 fd=open("/dev/null",O_WRONLY);
449 /* partitioning the md cell */
450 lc->nx=moldyn->dim.x/moldyn->cutoff;
451 lc->x=moldyn->dim.x/lc->nx;
452 lc->ny=moldyn->dim.y/moldyn->cutoff;
453 lc->y=moldyn->dim.y/lc->ny;
454 lc->nz=moldyn->dim.z/moldyn->cutoff;
455 lc->z=moldyn->dim.z/lc->nz;
457 lc->cells=lc->nx*lc->ny*lc->nz;
458 lc->subcell=malloc(lc->cells*sizeof(t_list));
460 printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
462 for(i=0;i<lc->cells;i++)
463 //list_init(&(lc->subcell[i]),1);
464 list_init(&(lc->subcell[i]),fd);
466 link_cell_update(moldyn);
471 int link_cell_update(t_moldyn *moldyn) {
485 for(i=0;i<lc->cells;i++)
486 list_destroy(&(moldyn->lc.subcell[i]));
488 for(count=0;count<moldyn->count;count++) {
489 i=(atom[count].r.x+(moldyn->dim.x/2))/lc->x;
490 j=(atom[count].r.y+(moldyn->dim.y/2))/lc->y;
491 k=(atom[count].r.z+(moldyn->dim.z/2))/lc->z;
492 list_add_immediate_ptr(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
499 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
517 cell[0]=lc->subcell[i+j*nx+k*a];
518 for(ci=-1;ci<=1;ci++) {
525 for(cj=-1;cj<=1;cj++) {
532 for(ck=-1;ck<=1;ck++) {
539 if(!(ci|cj|ck)) continue;
541 cell[--count2]=lc->subcell[x+y*nx+z*a];
544 cell[count1++]=lc->subcell[x+y*nx+z*a];
556 int link_cell_shutdown(t_moldyn *moldyn) {
563 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
564 list_shutdown(&(moldyn->lc.subcell[i]));
569 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
573 t_moldyn_schedule *schedule;
575 schedule=&(moldyn->schedule);
576 count=++(schedule->content_count);
578 ptr=realloc(moldyn->schedule.runs,count*sizeof(int));
580 perror("[moldyn] realloc (runs)");
583 moldyn->schedule.runs=ptr;
584 moldyn->schedule.runs[count-1]=runs;
586 ptr=realloc(schedule->tau,count*sizeof(double));
588 perror("[moldyn] realloc (tau)");
591 moldyn->schedule.tau=ptr;
592 moldyn->schedule.tau[count-1]=tau;
597 int moldyn_set_schedule_hook(t_moldyn *moldyn,void *hook,void *hook_params) {
599 moldyn->schedule.hook=hook;
600 moldyn->schedule.hook_params=hook_params;
607 * 'integration of newtons equation' - algorithms
611 /* start the integration */
613 int moldyn_integrate(t_moldyn *moldyn) {
616 unsigned int e,m,s,v;
618 t_moldyn_schedule *schedule;
624 schedule=&(moldyn->schedule);
627 /* initialize linked cell method */
628 link_cell_init(moldyn);
630 /* logging & visualization */
636 /* sqaure of some variables */
637 moldyn->tau_square=moldyn->tau*moldyn->tau;
638 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
639 /* calculate initial forces */
640 potential_force_calc(moldyn);
642 /* do some checks before we actually start calculating bullshit */
643 if(moldyn->cutoff>0.5*moldyn->dim.x)
644 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
645 if(moldyn->cutoff>0.5*moldyn->dim.y)
646 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
647 if(moldyn->cutoff>0.5*moldyn->dim.z)
648 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
649 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
650 if(ds>0.05*moldyn->nnd)
651 printf("[moldyn] warning: forces too high / tau too small!\n");
653 /* zero absolute time */
655 for(sched=0;sched<moldyn->schedule.content_count;sched++) {
657 /* setting amount of runs and finite time step size */
658 moldyn->tau=schedule->tau[sched];
659 moldyn->tau_square=moldyn->tau*moldyn->tau;
660 moldyn->time_steps=schedule->runs[sched];
662 /* integration according to schedule */
664 for(i=0;i<moldyn->time_steps;i++) {
666 /* integration step */
667 moldyn->integrate(moldyn);
669 /* increase absolute time */
670 moldyn->time+=moldyn->tau;
672 /* check for log & visualization */
676 "%.15f %.45f %.45f %.45f\n",
677 moldyn->time,update_e_kin(moldyn),
679 get_total_energy(moldyn));
683 p=get_total_p(moldyn);
685 "%.15f %.45f\n",moldyn->time,
691 snprintf(fb,128,"%s-%f-%.15f.save",moldyn->sfb,
692 moldyn->t,i*moldyn->tau);
693 fd=open(fb,O_WRONLY|O_TRUNC|O_CREAT);
694 if(fd<0) perror("[moldyn] save fd open");
696 write(fd,moldyn,sizeof(t_moldyn));
697 write(fd,moldyn->atom,
698 moldyn->count*sizeof(t_atom));
705 visual_atoms(&(moldyn->vis),moldyn->time,
706 moldyn->atom,moldyn->count);
707 printf("\rsched: %d, steps: %d",sched,i);
714 /* check for hooks */
716 schedule->hook(moldyn,schedule->hook_params);
723 /* velocity verlet */
725 int velocity_verlet(t_moldyn *moldyn) {
728 double tau,tau_square;
735 tau_square=moldyn->tau_square;
737 for(i=0;i<count;i++) {
739 v3_scale(&delta,&(atom[i].v),tau);
740 v3_add(&(atom[i].r),&(atom[i].r),&delta);
741 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
742 v3_add(&(atom[i].r),&(atom[i].r),&delta);
743 check_per_bound(moldyn,&(atom[i].r));
746 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
747 v3_add(&(atom[i].v),&(atom[i].v),&delta);
750 /* neighbour list update */
751 link_cell_update(moldyn);
753 /* forces depending on chosen potential */
754 potential_force_calc(moldyn);
755 //moldyn->potential_force_function(moldyn);
757 for(i=0;i<count;i++) {
758 /* again velocities */
759 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
760 v3_add(&(atom[i].v),&(atom[i].v),&delta);
769 * potentials & corresponding forces
773 /* generic potential and force calculation */
775 int potential_force_calc(t_moldyn *moldyn) {
778 t_atom *itom,*jtom,*ktom;
780 t_list neighbour_i[27],neighbour_j[27];
792 for(i=0;i<count;i++) {
795 v3_zero(&(itom[i].f));
797 /* single particle potential/force */
798 if(itom[i].attr&ATOM_ATTR_1BP)
799 moldyn->func1b(moldyn,&(itom[i]));
801 /* 2 body pair potential/force */
802 if(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)) {
804 link_cell_neighbour_index(moldyn,
805 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
806 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
807 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
813 for(j=0;j<countn;j++) {
815 this=&(neighbour_i[j]);
818 if(this->start==NULL)
824 jtom=this->current->data;
829 if((jtom->attr&ATOM_ATTR_2BP)&
830 (itom[i].attr&ATOM_ATTR_2BP))
831 moldyn->func2b(moldyn,
836 /* 3 body potential/force */
838 if(!(itom[i].attr&ATOM_ATTR_3BP)||
839 !(jtom->attr&ATOM_ATTR_3BP))
842 link_cell_neighbour_index(moldyn,
843 (jtom->r.x+moldyn->dim.x/2)/lc->x,
844 (jtom->r.y+moldyn->dim.y/2)/lc->y,
845 (jtom->r.z+moldyn->dim.z/2)/lc->z,
848 /* neighbours of j */
849 for(k=0;k<lc->countn;k++) {
851 that=&(neighbour_j[k]);
854 if(that->start==NULL)
857 bc_ijk=(k<lc->dnlc)?0:1;
861 ktom=that->current->data;
863 if(!(ktom->attr&ATOM_ATTR_3BP))
872 moldyn->func3b(moldyn,&(itom[i]),jtom,ktom,bc_ijk);
874 } while(list_next(that)!=\
879 /* neighbours of i */
880 for(k=0;k<countn;k++) {
882 that=&(neighbour_i[k]);
885 if(that->start==NULL)
892 ktom=that->current->data;
894 if(!(ktom->attr&ATOM_ATTR_3BP))
903 moldyn->func3b(moldyn,&(itom[i]),jtom,ktom,bc_ijk);
905 } while(list_next(that)!=\
910 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
919 * periodic boundayr checking
922 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
933 if(moldyn->status&MOLDYN_STAT_PBX) {
934 if(a->x>=x) a->x-=dim->x;
935 else if(-a->x>x) a->x+=dim->x;
937 if(moldyn->status&MOLDYN_STAT_PBY) {
938 if(a->y>=y) a->y-=dim->y;
939 else if(-a->y>y) a->y+=dim->y;
941 if(moldyn->status&MOLDYN_STAT_PBZ) {
942 if(a->z>=z) a->z-=dim->z;
943 else if(-a->z>z) a->z+=dim->z;
954 /* harmonic oscillator potential and force */
956 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
959 t_3dvec force,distance;
963 params=moldyn->pot2b_params;
964 sc=params->spring_constant;
965 equi_dist=params->equilibrium_distance;
967 v3_sub(&distance,&(ai->r),&(aj->r));
969 if(bc) check_per_bound(moldyn,&distance);
970 d=v3_norm(&distance);
971 if(d<=moldyn->cutoff) {
972 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
973 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
974 v3_scale(&force,&distance,-sc*(1.0-(equi_dist/d)));
975 v3_add(&(ai->f),&(ai->f),&force);
981 /* lennard jones potential & force for one sort of atoms */
983 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
986 t_3dvec force,distance;
988 double eps,sig6,sig12;
990 params=moldyn->pot2b_params;
991 eps=params->epsilon4;
993 sig12=params->sigma12;
995 v3_sub(&distance,&(ai->r),&(aj->r));
996 if(bc) check_per_bound(moldyn,&distance);
997 d=v3_absolute_square(&distance); /* 1/r^2 */
998 if(d<=moldyn->cutoff_square) {
1002 h1=h2*h2; /* 1/r^12 */
1003 /* energy is eps*..., but we will add this twice ... */
1004 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
1011 v3_scale(&force,&distance,d);
1012 v3_add(&(ai->f),&(ai->f),&force);
1019 * tersoff potential & force for 2 sorts of atoms
1022 /* create mixed terms from parameters and set them */
1023 int tersoff_mult_complete_params(t_tersoff_mult_params *p) {
1025 printf("[moldyn] tersoff parameter completion\n");
1026 p->Smixed=sqrt(p->S[0]*p->S[1]);
1027 p->Rmixed=sqrt(p->R[0]*p->R[1]);
1028 p->Amixed=sqrt(p->A[0]*p->A[1]);
1029 p->Bmixed=sqrt(p->B[0]*p->B[1]);
1030 p->lambda_m=0.5*(p->lambda[0]+p->lambda[1]);
1031 p->mu_m=0.5*(p->mu[0]+p->mu[1]);
1036 /* tersoff 1 body part */
1037 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
1040 t_tersoff_mult_params *params;
1041 t_tersoff_exchange *exchange;
1044 params=moldyn->pot1b_params;
1045 exchange=&(params->exchange);
1048 * simple: point constant parameters only depending on atom i to
1049 * their right values
1052 exchange->beta=&(params->beta[num]);
1053 exchange->n=&(params->n[num]);
1054 exchange->c=&(params->c[num]);
1055 exchange->d=&(params->d[num]);
1056 exchange->h=&(params->h[num]);
1058 exchange->betan=pow(*(exchange->beta),*(exchange->n));
1059 exchange->c2=params->c[num]*params->c[num];
1060 exchange->d2=params->d[num]*params->d[num];
1061 exchange->c2d2=exchange->c2/exchange->d2;
1066 /* tersoff 2 body part */
1067 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1069 t_tersoff_mult_params *params;
1070 t_tersoff_exchange *exchange;
1071 t_3dvec dist_ij,force;
1073 double A,B,R,S,lambda,mu;
1081 params=moldyn->pot2b_params;
1083 exchange=&(params->exchange);
1088 * we need: f_c, df_c, f_r, df_r
1090 * therefore we need: R, S, A, lambda
1093 v3_sub(&dist_ij,&(ai->r),&(aj->r));
1095 if(bc) check_per_bound(moldyn,&dist_ij);
1097 /* save for use in 3bp */ /* REALLY ?!?!?! */
1098 exchange->dist_ij=dist_ij;
1105 lambda=params->lambda[num];
1106 /* more constants depending of atoms i and j, needed in 3bp */
1107 params->exchange.B=&(params->B[num]);
1108 params->exchange.mu=&(params->mu[num]);
1110 params->exchange.chi=1.0;
1116 lambda=params->lambda_m;
1117 /* more constants depending of atoms i and j, needed in 3bp */
1118 params->exchange.B=&(params->Bmixed);
1119 params->exchange.mu=&(params->mu_m);
1121 params->exchange.chi=params->chi;
1124 d_ij=v3_norm(&dist_ij);
1126 /* save for use in 3bp */
1127 exchange->d_ij=d_ij;
1132 f_r=A*exp(-lambda*d_ij);
1133 df_r=-lambda*f_r/d_ij;
1135 /* f_a, df_a calc + save for 3bp use */
1136 exchange->f_a=-B*exp(-mu*d_ij);
1137 exchange->df_a=-mu*exchange->f_a/d_ij;
1140 /* f_c = 1, df_c = 0 */
1143 v3_scale(&force,&dist_ij,df_r);
1147 arg=M_PI*(d_ij-R)/s_r;
1148 f_c=0.5+0.5*cos(arg);
1149 df_c=-0.5*sin(arg)*(M_PI/(s_r*d_ij));
1150 scale=df_c*f_r+df_r*f_c;
1151 v3_scale(&force,&dist_ij,scale);
1155 v3_add(&(ai->f),&(ai->f),&force);
1156 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1157 moldyn->energy+=(0.25*f_r*f_c);
1159 /* save for use in 3bp */
1161 exchange->df_c=df_c;
1163 /* enable the run of 3bp function */
1169 /* tersoff 3 body part */
1171 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1173 t_tersoff_mult_params *params;
1174 t_tersoff_exchange *exchange;
1175 t_3dvec dist_ij,dist_ik,dist_jk;
1178 double d_ij,d_ij2,d_ik,d_jk;
1179 double f_c,df_c,b_ij,f_a,df_a;
1180 double f_c_ik,df_c_ik,arg;
1183 double n,c,d,h,beta,betan;
1186 double theta,cos_theta,sin_theta;
1187 double d_theta,d_theta1,d_theta2;
1188 double h_cos,h_cos2,d2_h_cos2;
1189 double frac1,bracket1,bracket2,bracket2_n_1,bracket2_n;
1190 double bracket3,bracket3_pow_1,bracket3_pow;
1193 params=moldyn->pot3b_params;
1195 exchange=&(params->exchange);
1197 if(!(exchange->run3bp))
1201 * we need: f_c, d_fc, b_ij, db_ij, f_a, df_a
1203 * we got f_c, df_c, f_a, df_a from 2bp calculation
1206 d_ij=exchange->d_ij;
1207 d_ij2=exchange->d_ij2;
1209 f_a=params->exchange.f_a;
1210 df_a=params->exchange.df_a;
1212 /* d_ij is <= S, as we didn't return so far! */
1215 * calc of b_ij (scalar) and db_ij (vector)
1217 * - for b_ij: chi, beta, f_c_ik, w(=1), c, d, h, n, cos_theta
1219 * - for db_ij: d_theta, sin_theta, cos_theta, f_c_ik, df_c_ik,
1225 v3_sub(&dist_ik,&(ai->r),&(ak->r));
1226 if(bc) check_per_bound(moldyn,&dist_ik);
1227 d_ik=v3_norm(&dist_ik);
1229 /* constants for f_c_ik calc */
1239 /* calc of f_c_ik */
1244 /* f_c_ik = 1, df_c_ik = 0 */
1250 arg=M_PI*(d_ik-R)/s_r;
1251 f_c_ik=0.5+0.5*cos(arg);
1252 df_c_ik=-0.5*sin(arg)*(M_PI/(s_r*d_ik));
1255 v3_sub(&dist_jk,&(aj->r),&(ak->r));
1256 if(bc) check_per_bound(moldyn,&dist_jk);
1257 d_jk=v3_norm(&dist_jk);
1259 beta=*(exchange->beta);
1260 betan=exchange->betan;
1267 c2d2=exchange->c2d2;
1269 numer=d_ij2+d_ik*d_ik-d_jk*d_jk;
1271 cos_theta=numer/denom;
1272 sin_theta=sqrt(1.0-(cos_theta*cos_theta));
1273 theta=acos(cos_theta);
1274 d_theta=(-1.0/sqrt(1.0-cos_theta*cos_theta))/(denom*denom);
1275 d_theta1=2*denom-numer*2*d_ik/d_ij;
1276 d_theta2=2*denom-numer*2*d_ij/d_ik;
1280 h_cos=(h-cos_theta);
1282 d2_h_cos2=d2-h_cos2;
1284 /* some usefull expressions */
1285 frac1=c2/(d2-h_cos2);
1286 bracket1=1+c2d2-frac1;
1287 bracket2=f_c_ik*bracket1;
1288 bracket2_n_1=pow(bracket2,n-1.0);
1289 bracket2_n=bracket2_n_1*bracket2;
1290 bracket3=1+betan*bracket2_n;
1291 bracket3_pow_1=pow(bracket3,(-1.0/(2.0*n))-1.0);
1292 bracket3_pow=bracket3_pow_1*bracket3;
1294 /* now go on with calc of b_ij and derivation of b_ij */
1295 b_ij=chi*bracket3_pow;
1297 /* derivation of theta */
1298 v3_scale(&force,&dist_ij,d_theta1);
1299 v3_scale(&temp,&dist_ik,d_theta2);
1300 v3_add(&force,&force,&temp);
1302 /* part 1 of derivation of b_ij */
1303 v3_scale(&force,&force,sin_theta*2*h_cos*f_c_ik*frac1);
1305 /* part 2 of derivation of b_ij */
1306 v3_scale(&temp,&dist_ik,df_c_ik*bracket1);
1308 /* sum up and scale ... */
1309 v3_add(&temp,&temp,&force);
1310 scale=bracket2_n_1*n*betan*(1+betan*bracket3_pow_1)*chi*(1.0/(2.0*n));
1311 v3_scale(&temp,&temp,scale);
1313 /* now construct an energy and a force out of that */
1314 v3_scale(&temp,&temp,f_a);
1315 v3_scale(&force,&dist_ij,df_a*b_ij);
1316 v3_add(&temp,&temp,&force);
1317 v3_scale(&temp,&temp,f_c);
1318 v3_scale(&force,&dist_ij,df_c*b_ij*f_a);
1319 v3_add(&force,&force,&temp);
1322 v3_add(&(ai->f),&(ai->f),&force);
1323 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1324 moldyn->energy+=(0.25*f_a*b_ij*f_c);