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 link_cell_shutdown(moldyn);
45 moldyn_log_shutdown(moldyn);
46 rand_close(&(moldyn->random));
52 int set_int_alg(t_moldyn *moldyn,u8 algo) {
55 case MOLDYN_INTEGRATE_VERLET:
56 moldyn->integrate=velocity_verlet;
59 printf("unknown integration algorithm: %02x\n",algo);
66 int set_cutoff(t_moldyn *moldyn,double cutoff) {
68 moldyn->cutoff=cutoff;
73 int set_temperature(t_moldyn *moldyn,double t) {
80 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
95 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
98 moldyn->status|=MOLDYN_STAT_PBX;
101 moldyn->status|=MOLDYN_STAT_PBY;
104 moldyn->status|=MOLDYN_STAT_PBZ;
109 int set_potential1b(t_moldyn *moldyn,pf_func1b func,void *params) {
112 moldyn->pot1b_params=params;
117 int set_potential2b(t_moldyn *moldyn,pf_func2b func,void *params) {
120 moldyn->pot2b_params=params;
125 int set_potential3b(t_moldyn *moldyn,pf_func3b func,void *params) {
128 moldyn->pot3b_params=params;
133 int moldyn_set_log(t_moldyn *moldyn,u8 type,char *fb,int timer) {
136 case LOG_TOTAL_ENERGY:
137 moldyn->ewrite=timer;
138 moldyn->efd=open(fb,O_WRONLY|O_CREAT|O_TRUNC);
140 perror("[moldyn] efd open");
143 dprintf(moldyn->efd,"# total energy log file\n");
145 case LOG_TOTAL_MOMENTUM:
146 moldyn->mwrite=timer;
147 moldyn->mfd=open(fb,O_WRONLY|O_CREAT|O_TRUNC);
149 perror("[moldyn] mfd open");
152 dprintf(moldyn->efd,"# total momentum log file\n");
155 moldyn->swrite=timer;
156 strncpy(moldyn->sfb,fb,63);
159 moldyn->mwrite=timer;
160 strncpy(moldyn->vfb,fb,63);
161 visual_init(&(moldyn->vis),fb);
164 printf("unknown log mechanism: %02x\n",type);
171 int moldyn_log_shutdown(t_moldyn *moldyn) {
173 if(moldyn->efd) close(moldyn->efd);
174 if(moldyn->mfd) close(moldyn->mfd);
175 if(moldyn->visual) visual_tini(moldyn->visual);
180 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
181 u8 attr,u8 bnum,int a,int b,int c) {
191 if(type==FCC) count*=4;
193 if(type==DIAMOND) count*=8;
195 atom=malloc(count*sizeof(t_atom));
197 perror("malloc (atoms)");
205 ret=fcc_init(a,b,c,lc,atom,&origin);
208 ret=diamond_init(a,b,c,lc,atom,&origin);
211 printf("unknown lattice type (%02x)\n",type);
217 printf("ok, there is something wrong ...\n");
218 printf("calculated -> %d atoms\n",count);
219 printf("created -> %d atoms\n",ret);
226 atom[count-1].element=element;
227 atom[count-1].mass=mass;
228 atom[count-1].attr=attr;
229 atom[count-1].bnum=bnum;
236 int add_atom(t_moldyn *moldyn,int element,double mass,u8 bnum,u8 attr,
237 t_3dvec r,t_3dvec v) {
244 count=++(moldyn->count);
246 ptr=realloc(atom,count*sizeof(t_atom));
248 perror("[moldyn] realloc (add atom)");
255 atom->element=element;
262 int destroy_atoms(t_moldyn *moldyn) {
264 if(moldyn->atom) free(moldyn->atom);
269 int thermal_init(t_moldyn *moldyn) {
272 * - gaussian distribution of velocities
273 * - zero total momentum
274 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
279 t_3dvec p_total,delta;
284 random=&(moldyn->random);
286 /* gaussian distribution of velocities */
288 for(i=0;i<moldyn->count;i++) {
289 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t/atom[i].mass);
291 v=sigma*rand_get_gauss(random);
293 p_total.x+=atom[i].mass*v;
295 v=sigma*rand_get_gauss(random);
297 p_total.y+=atom[i].mass*v;
299 v=sigma*rand_get_gauss(random);
301 p_total.z+=atom[i].mass*v;
304 /* zero total momentum */
305 v3_scale(&p_total,&p_total,1.0/moldyn->count);
306 for(i=0;i<moldyn->count;i++) {
307 v3_scale(&delta,&p_total,1.0/atom[i].mass);
308 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
311 /* velocity scaling */
312 scale_velocity(moldyn);
317 int scale_velocity(t_moldyn *moldyn) {
326 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
329 for(i=0;i<moldyn->count;i++)
330 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
331 c=sqrt((2.0*e)/(3.0*moldyn->count*K_BOLTZMANN*moldyn->t));
332 for(i=0;i<moldyn->count;i++)
333 v3_scale(&(atom[i].v),&(atom[i].v),(1.0/c));
338 double get_e_kin(t_moldyn *moldyn) {
346 for(i=0;i<moldyn->count;i++)
347 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
352 double get_e_pot(t_moldyn *moldyn) {
354 return moldyn->energy;
357 double update_e_kin(t_moldyn *moldyn) {
359 return(get_e_kin(moldyn));
362 double get_total_energy(t_moldyn *moldyn) {
364 return(moldyn->ekin+moldyn->energy);
367 t_3dvec get_total_p(t_moldyn *moldyn) {
376 for(i=0;i<moldyn->count;i++) {
377 v3_scale(&p,&(atom[i].v),atom[i].mass);
378 v3_add(&p_total,&p_total,&p);
384 double estimate_time_step(t_moldyn *moldyn,double nn_dist,double t) {
388 tau=0.05*nn_dist/(sqrt(3.0*K_BOLTZMANN*t/moldyn->atom[0].mass));
391 printf("[moldyn] warning: time step (%f > %.15f)\n",
401 /* linked list / cell method */
403 int link_cell_init(t_moldyn *moldyn) {
410 /* partitioning the md cell */
411 lc->nx=moldyn->dim.x/moldyn->cutoff;
412 lc->x=moldyn->dim.x/lc->nx;
413 lc->ny=moldyn->dim.y/moldyn->cutoff;
414 lc->y=moldyn->dim.y/lc->ny;
415 lc->nz=moldyn->dim.z/moldyn->cutoff;
416 lc->z=moldyn->dim.z/lc->nz;
418 lc->cells=lc->nx*lc->ny*lc->nz;
419 lc->subcell=malloc(lc->cells*sizeof(t_list));
421 printf("initializing linked cells (%d)\n",lc->cells);
423 for(i=0;i<lc->cells;i++)
424 list_init(&(lc->subcell[i]),1);
426 link_cell_update(moldyn);
431 int link_cell_update(t_moldyn *moldyn) {
445 for(i=0;i<lc->cells;i++)
446 list_destroy(&(moldyn->lc.subcell[i]));
448 for(count=0;count<moldyn->count;count++) {
449 i=(atom[count].r.x+(moldyn->dim.x/2))/lc->x;
450 j=(atom[count].r.y+(moldyn->dim.y/2))/lc->y;
451 k=(atom[count].r.z+(moldyn->dim.z/2))/lc->z;
452 list_add_immediate_ptr(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
459 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
478 cell[0]=lc->subcell[i+j*nx+k*a];
479 for(ci=-1;ci<=1;ci++) {
486 for(cj=-1;cj<=1;cj++) {
493 for(ck=-1;ck<=1;ck++) {
500 if(!(ci|cj|ck)) continue;
502 cell[--count2]=lc->subcell[x+y*nx+z*a];
505 cell[count1++]=lc->subcell[x+y*nx+z*a];
517 int link_cell_shutdown(t_moldyn *moldyn) {
524 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
525 list_shutdown(&(moldyn->lc.subcell[i]));
530 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
534 t_moldyn_schedule *schedule;
536 schedule=&(moldyn->schedule);
537 count=++(schedule->content_count);
539 ptr=realloc(moldyn->schedule.runs,count*sizeof(int));
541 perror("[moldyn] realloc (runs)");
544 moldyn->schedule.runs[count-1]=runs;
546 ptr=realloc(schedule->tau,count*sizeof(double));
548 perror("[moldyn] realloc (tau)");
551 moldyn->schedule.tau[count-1]=tau;
556 int moldyn_set_schedule_hook(t_moldyn *moldyn,void *hook,void *hook_params) {
558 moldyn->schedule.hook=hook;
559 moldyn->schedule.hook_params=hook_params;
566 * 'integration of newtons equation' - algorithms
570 /* start the integration */
572 int moldyn_integrate(t_moldyn *moldyn) {
575 unsigned int e,m,s,v;
577 t_moldyn_schedule *schedule;
582 schedule=&(moldyn->schedule);
584 /* initialize linked cell method */
585 link_cell_init(moldyn);
587 /* logging & visualization */
593 if(!(moldyn->lvstat&MOLDYN_LVSTAT_INITIALIZED)) {
594 printf("[moldyn] warning, lv system not initialized\n");
598 /* sqaure of some variables */
599 moldyn->tau_square=moldyn->tau*moldyn->tau;
600 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
602 /* calculate initial forces */
603 potential_force_calc(moldyn);
605 /* zero absolute time */
608 for(sched=0;sched<moldyn->schedule.content_count;sched++) {
610 /* setting amount of runs and finite time step size */
611 moldyn->tau=schedule->tau[sched];
612 moldyn->tau_square=moldyn->tau*moldyn->tau;
613 moldyn->time_steps=schedule->runs[sched];
615 /* integration according to schedule */
617 for(i=0;i<moldyn->time_steps;i++) {
619 /* integration step */
620 moldyn->integrate(moldyn);
622 /* increase absolute time */
623 moldyn->time+=moldyn->tau;
625 /* check for log & visualization */
629 "%.15f %.45f %.45f %.45f\n",
630 moldyn->time,update_e_kin(moldyn),
632 get_total_energy(moldyn));
636 p=get_total_p(moldyn);
638 "%.15f %.45f\n",moldyn->time,
644 snprintf(fb,128,"%s-%f-%.15f.save",moldyn->sfb,
645 moldyn->t,i*moldyn->tau);
646 fd=open(fb,O_WRONLY|O_TRUNC|O_CREAT);
647 if(fd<0) perror("[moldyn] save fd open");
649 write(fd,moldyn,sizeof(t_moldyn));
650 write(fd,moldyn->atom,
651 moldyn->count*sizeof(t_atom));
658 visual_atoms(moldyn->visual,i*moldyn->tau,
659 moldyn->atom,moldyn->count);
660 printf("\rsteps: %d",i);
666 /* check for hooks */
668 schedule->hook(moldyn,schedule->hook_params);
673 /* velocity verlet */
675 int velocity_verlet(t_moldyn *moldyn) {
678 double tau,tau_square;
685 tau_square=moldyn->tau_square;
687 for(i=0;i<count;i++) {
689 v3_scale(&delta,&(atom[i].v),tau);
690 v3_add(&(atom[i].r),&(atom[i].r),&delta);
691 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
692 v3_add(&(atom[i].r),&(atom[i].r),&delta);
693 v3_per_bound(&(atom[i].r),&(moldyn->dim));
696 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
697 v3_add(&(atom[i].v),&(atom[i].v),&delta);
700 /* neighbour list update */
701 printf("list update ...\n");
702 link_cell_update(moldyn);
705 /* forces depending on chosen potential */
706 printf("calc potential/force ...\n");
707 potential_force_calc(moldyn);
708 //moldyn->potential_force_function(moldyn);
711 for(i=0;i<count;i++) {
712 /* again velocities */
713 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
714 v3_add(&(atom[i].v),&(atom[i].v),&delta);
723 * potentials & corresponding forces
727 /* generic potential and force calculation */
729 int potential_force_calc(t_moldyn *moldyn) {
732 t_atom *atom,*btom,*ktom;
734 t_list neighbour[27];
735 t_list *this,*thisk,*neighbourk;
746 for(i=0;i<count;i++) {
749 v3_zero(&(atom[i].f));
751 /* single particle potential/force */
752 if(atom[i].attr&ATOM_ATTR_1BP)
753 moldyn->func1b(moldyn,&(atom[i]));
755 /* 2 body pair potential/force */
756 if(atom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)) {
758 link_cell_neighbour_index(moldyn,
759 (atom[i].r.x+moldyn->dim.x/2)/lc->x,
760 (atom[i].r.y+moldyn->dim.y/2)/lc->y,
761 (atom[i].r.z+moldyn->dim.z/2)/lc->z,
767 for(j=0;j<countn;j++) {
769 this=&(neighbour[j]);
772 if(this->start==NULL)
778 btom=this->current->data;
783 if((btom->attr&ATOM_ATTR_2BP)&
784 (atom[i].attr&ATOM_ATTR_2BP))
785 moldyn->func2b(moldyn,
790 /* 3 body potential/force */
792 if(!(atom[i].attr&ATOM_ATTR_3BP)||
793 !(btom->attr&ATOM_ATTR_3BP))
796 link_cell_neighbour_index(moldyn,
797 (btom->r.x+moldyn->dim.x/2)/lc->x,
798 (btom->r.y+moldyn->dim.y/2)/lc->y,
799 (btom->r.z+moldyn->dim.z/2)/lc->z,
802 for(k=0;k<lc->countn;k++) {
804 thisk=&(neighbourk[k]);
807 if(thisk->start==NULL)
810 bck=(k<lc->dnlc)?0:1;
814 ktom=thisk->current->data;
816 if(!(ktom->attr&ATOM_ATTR_3BP))
825 moldyn->func3b(moldyn,&(atom[i]),btom,ktom,bck);
827 } while(list_next(thisk)!=\
832 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
841 * periodic boundayr checking
844 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
855 if(moldyn->status&MOLDYN_STAT_PBX) {
856 if(a->x>=x) a->x-=dim->x;
857 else if(-a->x>x) a->x+=dim->x;
859 if(moldyn->status&MOLDYN_STAT_PBY) {
860 if(a->y>=y) a->y-=dim->y;
861 else if(-a->y>y) a->y+=dim->y;
863 if(moldyn->status&MOLDYN_STAT_PBZ) {
864 if(a->z>=z) a->z-=dim->z;
865 else if(-a->z>z) a->z+=dim->z;
876 /* harmonic oscillator potential and force */
878 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
881 t_3dvec force,distance;
885 params=moldyn->pot2b_params;
886 sc=params->spring_constant;
887 equi_dist=params->equilibrium_distance;
889 v3_sub(&distance,&(ai->r),&(aj->r));
891 v3_per_bound(&distance,&(moldyn->dim));
892 if(bc) check_per_bound(moldyn,&distance);
893 d=v3_norm(&distance);
894 if(d<=moldyn->cutoff) {
895 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
896 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
897 v3_scale(&force,&distance,-sc*(1.0-(equi_dist/d)));
898 v3_add(&(ai->f),&(ai->f),&force);
904 /* lennard jones potential & force for one sort of atoms */
906 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
909 t_3dvec force,distance;
911 double eps,sig6,sig12;
913 params=moldyn->pot2b_params;
914 eps=params->epsilon4;
916 sig12=params->sigma12;
918 v3_sub(&distance,&(ai->r),&(aj->r));
919 if(bc) check_per_bound(moldyn,&distance);
920 d=v3_absolute_square(&distance); /* 1/r^2 */
921 if(d<=moldyn->cutoff_square) {
925 h1=h2*h2; /* 1/r^12 */
926 /* energy is eps*..., but we will add this twice ... */
927 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
934 v3_scale(&force,&distance,d);
935 v3_add(&(ai->f),&(aj->f),&force);
942 * tersoff potential & force for 2 sorts of atoms
945 /* tersoff 1 body part */
946 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
949 t_tersoff_mult_params *params;
950 t_tersoff_exchange *exchange;
953 params=moldyn->pot1b_params;
954 exchange=&(params->exchange);
957 * simple: point constant parameters only depending on atom i to
961 exchange->beta=&(params->beta[num]);
962 exchange->n=&(params->n[num]);
963 exchange->c=&(params->c[num]);
964 exchange->d=&(params->d[num]);
965 exchange->h=&(params->h[num]);
967 exchange->betan=pow(*(exchange->beta),*(exchange->n));
968 exchange->c2=params->c[num]*params->c[num];
969 exchange->d2=params->d[num]*params->d[num];
970 exchange->c2d2=exchange->c2/exchange->d2;
975 /* tersoff 2 body part */
976 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
978 t_tersoff_mult_params *params;
979 t_tersoff_exchange *exchange;
980 t_3dvec dist_ij,force;
982 double A,B,R,S,lambda,mu;
990 params=moldyn->pot2b_params;
992 exchange=&(params->exchange);
997 * we need: f_c, df_c, f_r, df_r
999 * therefore we need: R, S, A, lambda
1002 v3_sub(&dist_ij,&(ai->r),&(aj->r));
1004 if(bc) check_per_bound(moldyn,&dist_ij);
1006 /* save for use in 3bp */ /* REALLY ?!?!?! */
1007 exchange->dist_ij=dist_ij;
1014 lambda=params->lambda[num];
1015 /* more constants depending of atoms i and j, needed in 3bp */
1016 params->exchange.B=&(params->B[num]);
1017 params->exchange.mu=&(params->mu[num]);
1019 params->exchange.chi=1.0;
1025 lambda=params->lambda_m;
1026 /* more constants depending of atoms i and j, needed in 3bp */
1027 params->exchange.B=&(params->Bmixed);
1028 params->exchange.mu=&(params->mu_m);
1030 params->exchange.chi=params->chi;
1033 d_ij=v3_norm(&dist_ij);
1035 /* save for use in 3bp */
1036 exchange->d_ij=d_ij;
1041 f_r=A*exp(-lambda*d_ij);
1042 df_r=-lambda*f_r/d_ij;
1044 /* f_a, df_a calc + save for 3bp use */
1045 exchange->f_a=-B*exp(-mu*d_ij);
1046 exchange->df_a=-mu*exchange->f_a/d_ij;
1049 /* f_c = 1, df_c = 0 */
1052 v3_scale(&force,&dist_ij,df_r);
1056 arg=M_PI*(d_ij-R)/s_r;
1057 f_c=0.5+0.5*cos(arg);
1058 df_c=-0.5*sin(arg)*(M_PI/(s_r*d_ij));
1059 scale=df_c*f_r+df_r*f_c;
1060 v3_scale(&force,&dist_ij,scale);
1064 v3_add(&(ai->f),&(ai->f),&force);
1065 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1066 moldyn->energy+=(0.25*f_r*f_c);
1068 /* save for use in 3bp */
1070 exchange->df_c=df_c;
1072 /* enable the run of 3bp function */
1078 /* tersoff 3 body part */
1080 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1082 t_tersoff_mult_params *params;
1083 t_tersoff_exchange *exchange;
1084 t_3dvec dist_ij,dist_ik,dist_jk;
1087 double d_ij,d_ij2,d_ik,d_jk;
1088 double f_c,df_c,b_ij,f_a,df_a;
1089 double f_c_ik,df_c_ik,arg;
1092 double n,c,d,h,beta,betan;
1095 double theta,cos_theta,sin_theta;
1096 double d_theta,d_theta1,d_theta2;
1097 double h_cos,h_cos2,d2_h_cos2;
1098 double frac1,bracket1,bracket2,bracket2_n_1,bracket2_n;
1099 double bracket3,bracket3_pow_1,bracket3_pow;
1102 params=moldyn->pot3b_params;
1104 exchange=&(params->exchange);
1106 if(!(exchange->run3bp))
1110 * we need: f_c, d_fc, b_ij, db_ij, f_a, df_a
1112 * we got f_c, df_c, f_a, df_a from 2bp calculation
1115 d_ij=exchange->d_ij;
1116 d_ij2=exchange->d_ij2;
1118 f_a=params->exchange.f_a;
1119 df_a=params->exchange.df_a;
1121 /* d_ij is <= S, as we didn't return so far! */
1124 * calc of b_ij (scalar) and db_ij (vector)
1126 * - for b_ij: chi, beta, f_c_ik, w(=1), c, d, h, n, cos_theta
1128 * - for db_ij: d_theta, sin_theta, cos_theta, f_c_ik, df_c_ik,
1134 v3_sub(&dist_ik,&(ai->r),&(ak->r));
1135 if(bc) check_per_bound(moldyn,&dist_ik);
1136 d_ik=v3_norm(&dist_ik);
1138 /* constants for f_c_ik calc */
1148 /* calc of f_c_ik */
1153 /* f_c_ik = 1, df_c_ik = 0 */
1159 arg=M_PI*(d_ik-R)/s_r;
1160 f_c_ik=0.5+0.5*cos(arg);
1161 df_c_ik=-0.5*sin(arg)*(M_PI/(s_r*d_ik));
1164 v3_sub(&dist_jk,&(aj->r),&(ak->r));
1165 if(bc) check_per_bound(moldyn,&dist_jk);
1166 d_jk=v3_norm(&dist_jk);
1168 beta=*(exchange->beta);
1169 betan=exchange->betan;
1176 c2d2=exchange->c2d2;
1178 numer=d_ij2+d_ik*d_ik-d_jk*d_jk;
1180 cos_theta=numer/denom;
1181 sin_theta=sqrt(1.0-(cos_theta*cos_theta));
1182 theta=acos(cos_theta);
1183 d_theta=(-1.0/sqrt(1.0-cos_theta*cos_theta))/(denom*denom);
1184 d_theta1=2*denom-numer*2*d_ik/d_ij;
1185 d_theta2=2*denom-numer*2*d_ij/d_ik;
1189 h_cos=(h-cos_theta);
1191 d2_h_cos2=d2-h_cos2;
1193 /* some usefull expressions */
1194 frac1=c2/(d2-h_cos2);
1195 bracket1=1+c2d2-frac1;
1196 bracket2=f_c_ik*bracket1;
1197 bracket2_n_1=pow(bracket2,n-1.0);
1198 bracket2_n=bracket2_n_1*bracket2;
1199 bracket3=1+betan*bracket2_n;
1200 bracket3_pow_1=pow(bracket3,(-1.0/(2.0*n))-1.0);
1201 bracket3_pow=bracket3_pow_1*bracket3;
1203 /* now go on with calc of b_ij and derivation of b_ij */
1204 b_ij=chi*bracket3_pow;
1206 /* derivation of theta */
1207 v3_scale(&force,&dist_ij,d_theta1);
1208 v3_scale(&temp,&dist_ik,d_theta2);
1209 v3_add(&force,&force,&temp);
1211 /* part 1 of derivation of b_ij */
1212 v3_scale(&force,&force,sin_theta*2*h_cos*f_c_ik*frac1);
1214 /* part 2 of derivation of b_ij */
1215 v3_scale(&temp,&dist_ik,df_c_ik*bracket1);
1217 /* sum up and scale ... */
1218 v3_add(&temp,&temp,&force);
1219 scale=bracket2_n_1*n*betan*(1+betan*bracket3_pow_1)*chi*(1.0/(2.0*n));
1220 v3_scale(&temp,&temp,scale);
1222 /* now construct an energy and a force out of that */
1223 v3_scale(&temp,&temp,f_a);
1224 v3_scale(&force,&dist_ij,df_a*b_ij);
1225 v3_add(&temp,&temp,&force);
1226 v3_scale(&temp,&temp,f_c);
1227 v3_scale(&force,&dist_ij,df_c*b_ij*f_a);
1228 v3_add(&force,&force,&temp);
1231 v3_add(&(ai->f),&(ai->f),&force);
1232 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1233 moldyn->energy+=(0.25*f_a*b_ij*f_c);