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 moldyn_log_shutdown(moldyn);
45 link_cell_shutdown(moldyn);
46 moldyn_log_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_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
99 moldyn->status|=MOLDYN_STAT_PBX;
102 moldyn->status|=MOLDYN_STAT_PBY;
105 moldyn->status|=MOLDYN_STAT_PBZ;
110 int set_potential1b(t_moldyn *moldyn,pf_func1b func,void *params) {
113 moldyn->pot1b_params=params;
118 int set_potential2b(t_moldyn *moldyn,pf_func2b func,void *params) {
121 moldyn->pot2b_params=params;
126 int set_potential3b(t_moldyn *moldyn,pf_func3b func,void *params) {
129 moldyn->pot3b_params=params;
134 int moldyn_set_log(t_moldyn *moldyn,u8 type,char *fb,int timer) {
137 case LOG_TOTAL_ENERGY:
138 moldyn->ewrite=timer;
139 moldyn->efd=open(fb,O_WRONLY|O_CREAT|O_TRUNC);
141 perror("[moldyn] efd open");
144 dprintf(moldyn->efd,"# total energy log file\n");
146 case LOG_TOTAL_MOMENTUM:
147 moldyn->mwrite=timer;
148 moldyn->mfd=open(fb,O_WRONLY|O_CREAT|O_TRUNC);
150 perror("[moldyn] mfd open");
153 dprintf(moldyn->efd,"# total momentum log file\n");
156 moldyn->swrite=timer;
157 strncpy(moldyn->sfb,fb,63);
160 moldyn->vwrite=timer;
161 strncpy(moldyn->vfb,fb,63);
162 visual_init(&(moldyn->vis),fb);
165 printf("unknown log mechanism: %02x\n",type);
172 int moldyn_log_shutdown(t_moldyn *moldyn) {
174 if(moldyn->efd) close(moldyn->efd);
175 if(moldyn->mfd) close(moldyn->mfd);
176 if(moldyn->visual) visual_tini(moldyn->visual);
181 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
182 u8 attr,u8 bnum,int a,int b,int c) {
192 if(type==FCC) count*=4;
194 if(type==DIAMOND) count*=8;
196 atom=malloc(count*sizeof(t_atom));
198 perror("malloc (atoms)");
206 ret=fcc_init(a,b,c,lc,atom,&origin);
209 ret=diamond_init(a,b,c,lc,atom,&origin);
212 printf("unknown lattice type (%02x)\n",type);
218 printf("ok, there is something wrong ...\n");
219 printf("calculated -> %d atoms\n",count);
220 printf("created -> %d atoms\n",ret);
227 atom[count-1].element=element;
228 atom[count-1].mass=mass;
229 atom[count-1].attr=attr;
230 atom[count-1].bnum=bnum;
237 int add_atom(t_moldyn *moldyn,int element,double mass,u8 bnum,u8 attr,
238 t_3dvec *r,t_3dvec *v) {
245 count=++(moldyn->count);
247 ptr=realloc(atom,count*sizeof(t_atom));
249 perror("[moldyn] realloc (add atom)");
257 atom[count-1].element=element;
258 atom[count-1].mass=mass;
259 atom[count-1].bnum=bnum;
260 atom[count-1].attr=attr;
265 int destroy_atoms(t_moldyn *moldyn) {
267 if(moldyn->atom) free(moldyn->atom);
272 int thermal_init(t_moldyn *moldyn) {
275 * - gaussian distribution of velocities
276 * - zero total momentum
277 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
282 t_3dvec p_total,delta;
287 random=&(moldyn->random);
289 /* gaussian distribution of velocities */
291 for(i=0;i<moldyn->count;i++) {
292 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t/atom[i].mass);
294 v=sigma*rand_get_gauss(random);
296 p_total.x+=atom[i].mass*v;
298 v=sigma*rand_get_gauss(random);
300 p_total.y+=atom[i].mass*v;
302 v=sigma*rand_get_gauss(random);
304 p_total.z+=atom[i].mass*v;
307 /* zero total momentum */
308 v3_scale(&p_total,&p_total,1.0/moldyn->count);
309 for(i=0;i<moldyn->count;i++) {
310 v3_scale(&delta,&p_total,1.0/atom[i].mass);
311 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
314 /* velocity scaling */
315 scale_velocity(moldyn);
320 int scale_velocity(t_moldyn *moldyn) {
329 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
333 printf("[moldyn] no velocity scaling for T = 0 K\n");
338 for(i=0;i<moldyn->count;i++)
339 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
340 c=sqrt((2.0*e)/(3.0*moldyn->count*K_BOLTZMANN*moldyn->t));
341 for(i=0;i<moldyn->count;i++)
342 v3_scale(&(atom[i].v),&(atom[i].v),(1.0/c));
347 double get_e_kin(t_moldyn *moldyn) {
355 for(i=0;i<moldyn->count;i++)
356 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
361 double get_e_pot(t_moldyn *moldyn) {
363 return moldyn->energy;
366 double update_e_kin(t_moldyn *moldyn) {
368 return(get_e_kin(moldyn));
371 double get_total_energy(t_moldyn *moldyn) {
373 return(moldyn->ekin+moldyn->energy);
376 t_3dvec get_total_p(t_moldyn *moldyn) {
385 for(i=0;i<moldyn->count;i++) {
386 v3_scale(&p,&(atom[i].v),atom[i].mass);
387 v3_add(&p_total,&p_total,&p);
393 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
397 /* nn_dist is the nearest neighbour distance */
400 printf("[moldyn] i do not estimate timesteps below %f K!\n",
401 MOLDYN_CRITICAL_EST_TEMP);
405 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
414 /* linked list / cell method */
416 int link_cell_init(t_moldyn *moldyn) {
422 fd=open("/dev/null",O_WRONLY);
426 /* partitioning the md cell */
427 lc->nx=moldyn->dim.x/moldyn->cutoff;
428 lc->x=moldyn->dim.x/lc->nx;
429 lc->ny=moldyn->dim.y/moldyn->cutoff;
430 lc->y=moldyn->dim.y/lc->ny;
431 lc->nz=moldyn->dim.z/moldyn->cutoff;
432 lc->z=moldyn->dim.z/lc->nz;
434 lc->cells=lc->nx*lc->ny*lc->nz;
435 lc->subcell=malloc(lc->cells*sizeof(t_list));
437 printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
439 for(i=0;i<lc->cells;i++)
440 //list_init(&(lc->subcell[i]),1);
441 list_init(&(lc->subcell[i]),fd);
443 link_cell_update(moldyn);
448 int link_cell_update(t_moldyn *moldyn) {
462 for(i=0;i<lc->cells;i++)
463 list_destroy(&(moldyn->lc.subcell[i]));
465 for(count=0;count<moldyn->count;count++) {
466 i=(atom[count].r.x+(moldyn->dim.x/2))/lc->x;
467 j=(atom[count].r.y+(moldyn->dim.y/2))/lc->y;
468 k=(atom[count].r.z+(moldyn->dim.z/2))/lc->z;
469 list_add_immediate_ptr(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
476 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
495 cell[0]=lc->subcell[i+j*nx+k*a];
496 for(ci=-1;ci<=1;ci++) {
503 for(cj=-1;cj<=1;cj++) {
510 for(ck=-1;ck<=1;ck++) {
517 if(!(ci|cj|ck)) continue;
519 cell[--count2]=lc->subcell[x+y*nx+z*a];
522 cell[count1++]=lc->subcell[x+y*nx+z*a];
534 int link_cell_shutdown(t_moldyn *moldyn) {
541 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
542 list_shutdown(&(moldyn->lc.subcell[i]));
547 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
551 t_moldyn_schedule *schedule;
553 schedule=&(moldyn->schedule);
554 count=++(schedule->content_count);
556 ptr=realloc(moldyn->schedule.runs,count*sizeof(int));
558 perror("[moldyn] realloc (runs)");
561 moldyn->schedule.runs=ptr;
562 moldyn->schedule.runs[count-1]=runs;
564 ptr=realloc(schedule->tau,count*sizeof(double));
566 perror("[moldyn] realloc (tau)");
569 moldyn->schedule.tau=ptr;
570 moldyn->schedule.tau[count-1]=tau;
575 int moldyn_set_schedule_hook(t_moldyn *moldyn,void *hook,void *hook_params) {
577 moldyn->schedule.hook=hook;
578 moldyn->schedule.hook_params=hook_params;
585 * 'integration of newtons equation' - algorithms
589 /* start the integration */
591 int moldyn_integrate(t_moldyn *moldyn) {
594 unsigned int e,m,s,v;
596 t_moldyn_schedule *schedule;
602 schedule=&(moldyn->schedule);
605 /* initialize linked cell method */
606 link_cell_init(moldyn);
608 /* logging & visualization */
614 /* sqaure of some variables */
615 moldyn->tau_square=moldyn->tau*moldyn->tau;
616 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
618 /* calculate initial forces */
619 potential_force_calc(moldyn);
621 /* zero absolute time */
624 for(sched=0;sched<moldyn->schedule.content_count;sched++) {
626 /* setting amount of runs and finite time step size */
627 moldyn->tau=schedule->tau[sched];
628 moldyn->tau_square=moldyn->tau*moldyn->tau;
629 moldyn->time_steps=schedule->runs[sched];
631 /* integration according to schedule */
633 for(i=0;i<moldyn->time_steps;i++) {
635 /* integration step */
636 moldyn->integrate(moldyn);
638 /* increase absolute time */
639 moldyn->time+=moldyn->tau;
641 /* check for log & visualization */
645 "%.15f %.45f %.45f %.45f\n",
646 moldyn->time,update_e_kin(moldyn),
648 get_total_energy(moldyn));
652 p=get_total_p(moldyn);
654 "%.15f %.45f\n",moldyn->time,
660 snprintf(fb,128,"%s-%f-%.15f.save",moldyn->sfb,
661 moldyn->t,i*moldyn->tau);
662 fd=open(fb,O_WRONLY|O_TRUNC|O_CREAT);
663 if(fd<0) perror("[moldyn] save fd open");
665 write(fd,moldyn,sizeof(t_moldyn));
666 write(fd,moldyn->atom,
667 moldyn->count*sizeof(t_atom));
674 visual_atoms(&(moldyn->vis),moldyn->time,
675 moldyn->atom,moldyn->count);
676 printf("\rsched: %d, steps: %d",sched,i);
683 /* check for hooks */
685 schedule->hook(moldyn,schedule->hook_params);
692 /* velocity verlet */
694 int velocity_verlet(t_moldyn *moldyn) {
697 double tau,tau_square;
704 tau_square=moldyn->tau_square;
706 for(i=0;i<count;i++) {
708 v3_scale(&delta,&(atom[i].v),tau);
709 v3_add(&(atom[i].r),&(atom[i].r),&delta);
710 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
711 v3_add(&(atom[i].r),&(atom[i].r),&delta);
712 check_per_bound(moldyn,&(atom[i].r));
715 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
716 v3_add(&(atom[i].v),&(atom[i].v),&delta);
719 /* neighbour list update */
720 link_cell_update(moldyn);
722 /* forces depending on chosen potential */
723 potential_force_calc(moldyn);
724 //moldyn->potential_force_function(moldyn);
726 for(i=0;i<count;i++) {
727 /* again velocities */
728 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
729 v3_add(&(atom[i].v),&(atom[i].v),&delta);
738 * potentials & corresponding forces
742 /* generic potential and force calculation */
744 int potential_force_calc(t_moldyn *moldyn) {
747 t_atom *atom,*btom,*ktom;
749 t_list neighbour[27];
750 t_list *this,*thisk,*neighbourk;
761 for(i=0;i<count;i++) {
764 v3_zero(&(atom[i].f));
766 /* single particle potential/force */
767 if(atom[i].attr&ATOM_ATTR_1BP)
768 moldyn->func1b(moldyn,&(atom[i]));
770 /* 2 body pair potential/force */
771 if(atom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)) {
773 link_cell_neighbour_index(moldyn,
774 (atom[i].r.x+moldyn->dim.x/2)/lc->x,
775 (atom[i].r.y+moldyn->dim.y/2)/lc->y,
776 (atom[i].r.z+moldyn->dim.z/2)/lc->z,
782 for(j=0;j<countn;j++) {
784 this=&(neighbour[j]);
787 if(this->start==NULL)
793 btom=this->current->data;
798 if((btom->attr&ATOM_ATTR_2BP)&
799 (atom[i].attr&ATOM_ATTR_2BP))
800 moldyn->func2b(moldyn,
805 /* 3 body potential/force */
807 if(!(atom[i].attr&ATOM_ATTR_3BP)||
808 !(btom->attr&ATOM_ATTR_3BP))
811 link_cell_neighbour_index(moldyn,
812 (btom->r.x+moldyn->dim.x/2)/lc->x,
813 (btom->r.y+moldyn->dim.y/2)/lc->y,
814 (btom->r.z+moldyn->dim.z/2)/lc->z,
817 for(k=0;k<lc->countn;k++) {
819 thisk=&(neighbourk[k]);
822 if(thisk->start==NULL)
825 bck=(k<lc->dnlc)?0:1;
829 ktom=thisk->current->data;
831 if(!(ktom->attr&ATOM_ATTR_3BP))
840 moldyn->func3b(moldyn,&(atom[i]),btom,ktom,bck);
842 } while(list_next(thisk)!=\
847 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
856 * periodic boundayr checking
859 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
870 if(moldyn->status&MOLDYN_STAT_PBX) {
871 if(a->x>=x) a->x-=dim->x;
872 else if(-a->x>x) a->x+=dim->x;
874 if(moldyn->status&MOLDYN_STAT_PBY) {
875 if(a->y>=y) a->y-=dim->y;
876 else if(-a->y>y) a->y+=dim->y;
878 if(moldyn->status&MOLDYN_STAT_PBZ) {
879 if(a->z>=z) a->z-=dim->z;
880 else if(-a->z>z) a->z+=dim->z;
891 /* harmonic oscillator potential and force */
893 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
896 t_3dvec force,distance;
900 params=moldyn->pot2b_params;
901 sc=params->spring_constant;
902 equi_dist=params->equilibrium_distance;
904 v3_sub(&distance,&(ai->r),&(aj->r));
906 if(bc) check_per_bound(moldyn,&distance);
907 d=v3_norm(&distance);
908 if(d<=moldyn->cutoff) {
909 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
910 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
911 v3_scale(&force,&distance,-sc*(1.0-(equi_dist/d)));
912 v3_add(&(ai->f),&(ai->f),&force);
918 /* lennard jones potential & force for one sort of atoms */
920 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
923 t_3dvec force,distance;
925 double eps,sig6,sig12;
927 params=moldyn->pot2b_params;
928 eps=params->epsilon4;
930 sig12=params->sigma12;
932 v3_sub(&distance,&(ai->r),&(aj->r));
933 if(bc) check_per_bound(moldyn,&distance);
934 d=v3_absolute_square(&distance); /* 1/r^2 */
935 if(d<=moldyn->cutoff_square) {
939 h1=h2*h2; /* 1/r^12 */
940 /* energy is eps*..., but we will add this twice ... */
941 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
948 v3_scale(&force,&distance,d);
949 v3_add(&(ai->f),&(ai->f),&force);
956 * tersoff potential & force for 2 sorts of atoms
959 /* tersoff 1 body part */
960 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
963 t_tersoff_mult_params *params;
964 t_tersoff_exchange *exchange;
967 params=moldyn->pot1b_params;
968 exchange=&(params->exchange);
971 * simple: point constant parameters only depending on atom i to
975 exchange->beta=&(params->beta[num]);
976 exchange->n=&(params->n[num]);
977 exchange->c=&(params->c[num]);
978 exchange->d=&(params->d[num]);
979 exchange->h=&(params->h[num]);
981 exchange->betan=pow(*(exchange->beta),*(exchange->n));
982 exchange->c2=params->c[num]*params->c[num];
983 exchange->d2=params->d[num]*params->d[num];
984 exchange->c2d2=exchange->c2/exchange->d2;
989 /* tersoff 2 body part */
990 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
992 t_tersoff_mult_params *params;
993 t_tersoff_exchange *exchange;
994 t_3dvec dist_ij,force;
996 double A,B,R,S,lambda,mu;
1004 params=moldyn->pot2b_params;
1006 exchange=&(params->exchange);
1011 * we need: f_c, df_c, f_r, df_r
1013 * therefore we need: R, S, A, lambda
1016 v3_sub(&dist_ij,&(ai->r),&(aj->r));
1018 if(bc) check_per_bound(moldyn,&dist_ij);
1020 /* save for use in 3bp */ /* REALLY ?!?!?! */
1021 exchange->dist_ij=dist_ij;
1028 lambda=params->lambda[num];
1029 /* more constants depending of atoms i and j, needed in 3bp */
1030 params->exchange.B=&(params->B[num]);
1031 params->exchange.mu=&(params->mu[num]);
1033 params->exchange.chi=1.0;
1039 lambda=params->lambda_m;
1040 /* more constants depending of atoms i and j, needed in 3bp */
1041 params->exchange.B=&(params->Bmixed);
1042 params->exchange.mu=&(params->mu_m);
1044 params->exchange.chi=params->chi;
1047 d_ij=v3_norm(&dist_ij);
1049 /* save for use in 3bp */
1050 exchange->d_ij=d_ij;
1055 f_r=A*exp(-lambda*d_ij);
1056 df_r=-lambda*f_r/d_ij;
1058 /* f_a, df_a calc + save for 3bp use */
1059 exchange->f_a=-B*exp(-mu*d_ij);
1060 exchange->df_a=-mu*exchange->f_a/d_ij;
1063 /* f_c = 1, df_c = 0 */
1066 v3_scale(&force,&dist_ij,df_r);
1070 arg=M_PI*(d_ij-R)/s_r;
1071 f_c=0.5+0.5*cos(arg);
1072 df_c=-0.5*sin(arg)*(M_PI/(s_r*d_ij));
1073 scale=df_c*f_r+df_r*f_c;
1074 v3_scale(&force,&dist_ij,scale);
1078 v3_add(&(ai->f),&(ai->f),&force);
1079 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1080 moldyn->energy+=(0.25*f_r*f_c);
1082 /* save for use in 3bp */
1084 exchange->df_c=df_c;
1086 /* enable the run of 3bp function */
1092 /* tersoff 3 body part */
1094 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1096 t_tersoff_mult_params *params;
1097 t_tersoff_exchange *exchange;
1098 t_3dvec dist_ij,dist_ik,dist_jk;
1101 double d_ij,d_ij2,d_ik,d_jk;
1102 double f_c,df_c,b_ij,f_a,df_a;
1103 double f_c_ik,df_c_ik,arg;
1106 double n,c,d,h,beta,betan;
1109 double theta,cos_theta,sin_theta;
1110 double d_theta,d_theta1,d_theta2;
1111 double h_cos,h_cos2,d2_h_cos2;
1112 double frac1,bracket1,bracket2,bracket2_n_1,bracket2_n;
1113 double bracket3,bracket3_pow_1,bracket3_pow;
1116 params=moldyn->pot3b_params;
1118 exchange=&(params->exchange);
1120 if(!(exchange->run3bp))
1124 * we need: f_c, d_fc, b_ij, db_ij, f_a, df_a
1126 * we got f_c, df_c, f_a, df_a from 2bp calculation
1129 d_ij=exchange->d_ij;
1130 d_ij2=exchange->d_ij2;
1132 f_a=params->exchange.f_a;
1133 df_a=params->exchange.df_a;
1135 /* d_ij is <= S, as we didn't return so far! */
1138 * calc of b_ij (scalar) and db_ij (vector)
1140 * - for b_ij: chi, beta, f_c_ik, w(=1), c, d, h, n, cos_theta
1142 * - for db_ij: d_theta, sin_theta, cos_theta, f_c_ik, df_c_ik,
1148 v3_sub(&dist_ik,&(ai->r),&(ak->r));
1149 if(bc) check_per_bound(moldyn,&dist_ik);
1150 d_ik=v3_norm(&dist_ik);
1152 /* constants for f_c_ik calc */
1162 /* calc of f_c_ik */
1167 /* f_c_ik = 1, df_c_ik = 0 */
1173 arg=M_PI*(d_ik-R)/s_r;
1174 f_c_ik=0.5+0.5*cos(arg);
1175 df_c_ik=-0.5*sin(arg)*(M_PI/(s_r*d_ik));
1178 v3_sub(&dist_jk,&(aj->r),&(ak->r));
1179 if(bc) check_per_bound(moldyn,&dist_jk);
1180 d_jk=v3_norm(&dist_jk);
1182 beta=*(exchange->beta);
1183 betan=exchange->betan;
1190 c2d2=exchange->c2d2;
1192 numer=d_ij2+d_ik*d_ik-d_jk*d_jk;
1194 cos_theta=numer/denom;
1195 sin_theta=sqrt(1.0-(cos_theta*cos_theta));
1196 theta=acos(cos_theta);
1197 d_theta=(-1.0/sqrt(1.0-cos_theta*cos_theta))/(denom*denom);
1198 d_theta1=2*denom-numer*2*d_ik/d_ij;
1199 d_theta2=2*denom-numer*2*d_ij/d_ik;
1203 h_cos=(h-cos_theta);
1205 d2_h_cos2=d2-h_cos2;
1207 /* some usefull expressions */
1208 frac1=c2/(d2-h_cos2);
1209 bracket1=1+c2d2-frac1;
1210 bracket2=f_c_ik*bracket1;
1211 bracket2_n_1=pow(bracket2,n-1.0);
1212 bracket2_n=bracket2_n_1*bracket2;
1213 bracket3=1+betan*bracket2_n;
1214 bracket3_pow_1=pow(bracket3,(-1.0/(2.0*n))-1.0);
1215 bracket3_pow=bracket3_pow_1*bracket3;
1217 /* now go on with calc of b_ij and derivation of b_ij */
1218 b_ij=chi*bracket3_pow;
1220 /* derivation of theta */
1221 v3_scale(&force,&dist_ij,d_theta1);
1222 v3_scale(&temp,&dist_ik,d_theta2);
1223 v3_add(&force,&force,&temp);
1225 /* part 1 of derivation of b_ij */
1226 v3_scale(&force,&force,sin_theta*2*h_cos*f_c_ik*frac1);
1228 /* part 2 of derivation of b_ij */
1229 v3_scale(&temp,&dist_ik,df_c_ik*bracket1);
1231 /* sum up and scale ... */
1232 v3_add(&temp,&temp,&force);
1233 scale=bracket2_n_1*n*betan*(1+betan*bracket3_pow_1)*chi*(1.0/(2.0*n));
1234 v3_scale(&temp,&temp,scale);
1236 /* now construct an energy and a force out of that */
1237 v3_scale(&temp,&temp,f_a);
1238 v3_scale(&force,&dist_ij,df_a*b_ij);
1239 v3_add(&temp,&temp,&force);
1240 v3_scale(&temp,&temp,f_c);
1241 v3_scale(&force,&dist_ij,df_c*b_ij*f_a);
1242 v3_add(&force,&force,&temp);
1245 v3_add(&(ai->f),&(ai->f),&force);
1246 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1247 moldyn->energy+=(0.25*f_a*b_ij*f_c);