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_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 printf("[moldyn] log shutdown\n");
175 if(moldyn->efd) close(moldyn->efd);
176 if(moldyn->mfd) close(moldyn->mfd);
177 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
182 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
183 u8 attr,u8 bnum,int a,int b,int c) {
193 if(type==FCC) count*=4;
195 if(type==DIAMOND) count*=8;
197 atom=malloc(count*sizeof(t_atom));
199 perror("malloc (atoms)");
207 ret=fcc_init(a,b,c,lc,atom,&origin);
210 ret=diamond_init(a,b,c,lc,atom,&origin);
213 printf("unknown lattice type (%02x)\n",type);
219 printf("ok, there is something wrong ...\n");
220 printf("calculated -> %d atoms\n",count);
221 printf("created -> %d atoms\n",ret);
228 atom[count-1].element=element;
229 atom[count-1].mass=mass;
230 atom[count-1].attr=attr;
231 atom[count-1].bnum=bnum;
238 int add_atom(t_moldyn *moldyn,int element,double mass,u8 bnum,u8 attr,
239 t_3dvec *r,t_3dvec *v) {
246 count=++(moldyn->count);
248 ptr=realloc(atom,count*sizeof(t_atom));
250 perror("[moldyn] realloc (add atom)");
258 atom[count-1].element=element;
259 atom[count-1].mass=mass;
260 atom[count-1].bnum=bnum;
261 atom[count-1].attr=attr;
266 int destroy_atoms(t_moldyn *moldyn) {
268 if(moldyn->atom) free(moldyn->atom);
273 int thermal_init(t_moldyn *moldyn) {
276 * - gaussian distribution of velocities
277 * - zero total momentum
278 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
283 t_3dvec p_total,delta;
288 random=&(moldyn->random);
290 /* gaussian distribution of velocities */
292 for(i=0;i<moldyn->count;i++) {
293 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t/atom[i].mass);
295 v=sigma*rand_get_gauss(random);
297 p_total.x+=atom[i].mass*v;
299 v=sigma*rand_get_gauss(random);
301 p_total.y+=atom[i].mass*v;
303 v=sigma*rand_get_gauss(random);
305 p_total.z+=atom[i].mass*v;
308 /* zero total momentum */
309 v3_scale(&p_total,&p_total,1.0/moldyn->count);
310 for(i=0;i<moldyn->count;i++) {
311 v3_scale(&delta,&p_total,1.0/atom[i].mass);
312 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
315 /* velocity scaling */
316 scale_velocity(moldyn);
321 int scale_velocity(t_moldyn *moldyn) {
330 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
334 printf("[moldyn] no velocity scaling for T = 0 K\n");
339 for(i=0;i<moldyn->count;i++)
340 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
341 c=sqrt((2.0*e)/(3.0*moldyn->count*K_BOLTZMANN*moldyn->t));
342 for(i=0;i<moldyn->count;i++)
343 v3_scale(&(atom[i].v),&(atom[i].v),(1.0/c));
348 double get_e_kin(t_moldyn *moldyn) {
356 for(i=0;i<moldyn->count;i++)
357 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
362 double get_e_pot(t_moldyn *moldyn) {
364 return moldyn->energy;
367 double update_e_kin(t_moldyn *moldyn) {
369 return(get_e_kin(moldyn));
372 double get_total_energy(t_moldyn *moldyn) {
374 return(moldyn->ekin+moldyn->energy);
377 t_3dvec get_total_p(t_moldyn *moldyn) {
386 for(i=0;i<moldyn->count;i++) {
387 v3_scale(&p,&(atom[i].v),atom[i].mass);
388 v3_add(&p_total,&p_total,&p);
394 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
398 /* nn_dist is the nearest neighbour distance */
401 printf("[moldyn] i do not estimate timesteps below %f K!\n",
402 MOLDYN_CRITICAL_EST_TEMP);
406 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
415 /* linked list / cell method */
417 int link_cell_init(t_moldyn *moldyn) {
423 fd=open("/dev/null",O_WRONLY);
427 /* partitioning the md cell */
428 lc->nx=moldyn->dim.x/moldyn->cutoff;
429 lc->x=moldyn->dim.x/lc->nx;
430 lc->ny=moldyn->dim.y/moldyn->cutoff;
431 lc->y=moldyn->dim.y/lc->ny;
432 lc->nz=moldyn->dim.z/moldyn->cutoff;
433 lc->z=moldyn->dim.z/lc->nz;
435 lc->cells=lc->nx*lc->ny*lc->nz;
436 lc->subcell=malloc(lc->cells*sizeof(t_list));
438 printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
440 for(i=0;i<lc->cells;i++)
441 //list_init(&(lc->subcell[i]),1);
442 list_init(&(lc->subcell[i]),fd);
444 link_cell_update(moldyn);
449 int link_cell_update(t_moldyn *moldyn) {
463 for(i=0;i<lc->cells;i++)
464 list_destroy(&(moldyn->lc.subcell[i]));
466 for(count=0;count<moldyn->count;count++) {
467 i=(atom[count].r.x+(moldyn->dim.x/2))/lc->x;
468 j=(atom[count].r.y+(moldyn->dim.y/2))/lc->y;
469 k=(atom[count].r.z+(moldyn->dim.z/2))/lc->z;
470 list_add_immediate_ptr(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
477 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
496 cell[0]=lc->subcell[i+j*nx+k*a];
497 for(ci=-1;ci<=1;ci++) {
504 for(cj=-1;cj<=1;cj++) {
511 for(ck=-1;ck<=1;ck++) {
518 if(!(ci|cj|ck)) continue;
520 cell[--count2]=lc->subcell[x+y*nx+z*a];
523 cell[count1++]=lc->subcell[x+y*nx+z*a];
535 int link_cell_shutdown(t_moldyn *moldyn) {
542 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
543 list_shutdown(&(moldyn->lc.subcell[i]));
548 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
552 t_moldyn_schedule *schedule;
554 schedule=&(moldyn->schedule);
555 count=++(schedule->content_count);
557 ptr=realloc(moldyn->schedule.runs,count*sizeof(int));
559 perror("[moldyn] realloc (runs)");
562 moldyn->schedule.runs=ptr;
563 moldyn->schedule.runs[count-1]=runs;
565 ptr=realloc(schedule->tau,count*sizeof(double));
567 perror("[moldyn] realloc (tau)");
570 moldyn->schedule.tau=ptr;
571 moldyn->schedule.tau[count-1]=tau;
576 int moldyn_set_schedule_hook(t_moldyn *moldyn,void *hook,void *hook_params) {
578 moldyn->schedule.hook=hook;
579 moldyn->schedule.hook_params=hook_params;
586 * 'integration of newtons equation' - algorithms
590 /* start the integration */
592 int moldyn_integrate(t_moldyn *moldyn) {
595 unsigned int e,m,s,v;
597 t_moldyn_schedule *schedule;
603 schedule=&(moldyn->schedule);
606 /* initialize linked cell method */
607 link_cell_init(moldyn);
609 /* logging & visualization */
615 /* sqaure of some variables */
616 moldyn->tau_square=moldyn->tau*moldyn->tau;
617 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
619 /* calculate initial forces */
620 potential_force_calc(moldyn);
622 /* zero absolute time */
625 for(sched=0;sched<moldyn->schedule.content_count;sched++) {
627 /* setting amount of runs and finite time step size */
628 moldyn->tau=schedule->tau[sched];
629 moldyn->tau_square=moldyn->tau*moldyn->tau;
630 moldyn->time_steps=schedule->runs[sched];
632 /* integration according to schedule */
634 for(i=0;i<moldyn->time_steps;i++) {
636 /* integration step */
637 moldyn->integrate(moldyn);
639 /* increase absolute time */
640 moldyn->time+=moldyn->tau;
642 /* check for log & visualization */
646 "%.15f %.45f %.45f %.45f\n",
647 moldyn->time,update_e_kin(moldyn),
649 get_total_energy(moldyn));
653 p=get_total_p(moldyn);
655 "%.15f %.45f\n",moldyn->time,
661 snprintf(fb,128,"%s-%f-%.15f.save",moldyn->sfb,
662 moldyn->t,i*moldyn->tau);
663 fd=open(fb,O_WRONLY|O_TRUNC|O_CREAT);
664 if(fd<0) perror("[moldyn] save fd open");
666 write(fd,moldyn,sizeof(t_moldyn));
667 write(fd,moldyn->atom,
668 moldyn->count*sizeof(t_atom));
675 visual_atoms(&(moldyn->vis),moldyn->time,
676 moldyn->atom,moldyn->count);
677 printf("\rsched: %d, steps: %d",sched,i);
684 /* check for hooks */
686 schedule->hook(moldyn,schedule->hook_params);
693 /* velocity verlet */
695 int velocity_verlet(t_moldyn *moldyn) {
698 double tau,tau_square;
705 tau_square=moldyn->tau_square;
707 for(i=0;i<count;i++) {
709 v3_scale(&delta,&(atom[i].v),tau);
710 v3_add(&(atom[i].r),&(atom[i].r),&delta);
711 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
712 v3_add(&(atom[i].r),&(atom[i].r),&delta);
713 check_per_bound(moldyn,&(atom[i].r));
716 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
717 v3_add(&(atom[i].v),&(atom[i].v),&delta);
720 /* neighbour list update */
721 link_cell_update(moldyn);
723 /* forces depending on chosen potential */
724 potential_force_calc(moldyn);
725 //moldyn->potential_force_function(moldyn);
727 for(i=0;i<count;i++) {
728 /* again velocities */
729 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
730 v3_add(&(atom[i].v),&(atom[i].v),&delta);
739 * potentials & corresponding forces
743 /* generic potential and force calculation */
745 int potential_force_calc(t_moldyn *moldyn) {
748 t_atom *atom,*btom,*ktom;
750 t_list neighbour[27];
751 t_list *this,*thisk,*neighbourk;
762 for(i=0;i<count;i++) {
765 v3_zero(&(atom[i].f));
767 /* single particle potential/force */
768 if(atom[i].attr&ATOM_ATTR_1BP)
769 moldyn->func1b(moldyn,&(atom[i]));
771 /* 2 body pair potential/force */
772 if(atom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)) {
774 link_cell_neighbour_index(moldyn,
775 (atom[i].r.x+moldyn->dim.x/2)/lc->x,
776 (atom[i].r.y+moldyn->dim.y/2)/lc->y,
777 (atom[i].r.z+moldyn->dim.z/2)/lc->z,
783 for(j=0;j<countn;j++) {
785 this=&(neighbour[j]);
788 if(this->start==NULL)
794 btom=this->current->data;
799 if((btom->attr&ATOM_ATTR_2BP)&
800 (atom[i].attr&ATOM_ATTR_2BP))
801 moldyn->func2b(moldyn,
806 /* 3 body potential/force */
808 if(!(atom[i].attr&ATOM_ATTR_3BP)||
809 !(btom->attr&ATOM_ATTR_3BP))
812 link_cell_neighbour_index(moldyn,
813 (btom->r.x+moldyn->dim.x/2)/lc->x,
814 (btom->r.y+moldyn->dim.y/2)/lc->y,
815 (btom->r.z+moldyn->dim.z/2)/lc->z,
818 for(k=0;k<lc->countn;k++) {
820 thisk=&(neighbourk[k]);
823 if(thisk->start==NULL)
826 bck=(k<lc->dnlc)?0:1;
830 ktom=thisk->current->data;
832 if(!(ktom->attr&ATOM_ATTR_3BP))
841 moldyn->func3b(moldyn,&(atom[i]),btom,ktom,bck);
843 } while(list_next(thisk)!=\
848 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
857 * periodic boundayr checking
860 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
871 if(moldyn->status&MOLDYN_STAT_PBX) {
872 if(a->x>=x) a->x-=dim->x;
873 else if(-a->x>x) a->x+=dim->x;
875 if(moldyn->status&MOLDYN_STAT_PBY) {
876 if(a->y>=y) a->y-=dim->y;
877 else if(-a->y>y) a->y+=dim->y;
879 if(moldyn->status&MOLDYN_STAT_PBZ) {
880 if(a->z>=z) a->z-=dim->z;
881 else if(-a->z>z) a->z+=dim->z;
892 /* harmonic oscillator potential and force */
894 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
897 t_3dvec force,distance;
901 params=moldyn->pot2b_params;
902 sc=params->spring_constant;
903 equi_dist=params->equilibrium_distance;
905 v3_sub(&distance,&(ai->r),&(aj->r));
907 if(bc) check_per_bound(moldyn,&distance);
908 d=v3_norm(&distance);
909 if(d<=moldyn->cutoff) {
910 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
911 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
912 v3_scale(&force,&distance,-sc*(1.0-(equi_dist/d)));
913 v3_add(&(ai->f),&(ai->f),&force);
919 /* lennard jones potential & force for one sort of atoms */
921 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
924 t_3dvec force,distance;
926 double eps,sig6,sig12;
928 params=moldyn->pot2b_params;
929 eps=params->epsilon4;
931 sig12=params->sigma12;
933 v3_sub(&distance,&(ai->r),&(aj->r));
934 if(bc) check_per_bound(moldyn,&distance);
935 d=v3_absolute_square(&distance); /* 1/r^2 */
936 if(d<=moldyn->cutoff_square) {
940 h1=h2*h2; /* 1/r^12 */
941 /* energy is eps*..., but we will add this twice ... */
942 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
949 v3_scale(&force,&distance,d);
950 v3_add(&(ai->f),&(ai->f),&force);
957 * tersoff potential & force for 2 sorts of atoms
960 /* tersoff 1 body part */
961 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
964 t_tersoff_mult_params *params;
965 t_tersoff_exchange *exchange;
968 params=moldyn->pot1b_params;
969 exchange=&(params->exchange);
972 * simple: point constant parameters only depending on atom i to
976 exchange->beta=&(params->beta[num]);
977 exchange->n=&(params->n[num]);
978 exchange->c=&(params->c[num]);
979 exchange->d=&(params->d[num]);
980 exchange->h=&(params->h[num]);
982 exchange->betan=pow(*(exchange->beta),*(exchange->n));
983 exchange->c2=params->c[num]*params->c[num];
984 exchange->d2=params->d[num]*params->d[num];
985 exchange->c2d2=exchange->c2/exchange->d2;
990 /* tersoff 2 body part */
991 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
993 t_tersoff_mult_params *params;
994 t_tersoff_exchange *exchange;
995 t_3dvec dist_ij,force;
997 double A,B,R,S,lambda,mu;
1005 params=moldyn->pot2b_params;
1007 exchange=&(params->exchange);
1012 * we need: f_c, df_c, f_r, df_r
1014 * therefore we need: R, S, A, lambda
1017 v3_sub(&dist_ij,&(ai->r),&(aj->r));
1019 if(bc) check_per_bound(moldyn,&dist_ij);
1021 /* save for use in 3bp */ /* REALLY ?!?!?! */
1022 exchange->dist_ij=dist_ij;
1029 lambda=params->lambda[num];
1030 /* more constants depending of atoms i and j, needed in 3bp */
1031 params->exchange.B=&(params->B[num]);
1032 params->exchange.mu=&(params->mu[num]);
1034 params->exchange.chi=1.0;
1040 lambda=params->lambda_m;
1041 /* more constants depending of atoms i and j, needed in 3bp */
1042 params->exchange.B=&(params->Bmixed);
1043 params->exchange.mu=&(params->mu_m);
1045 params->exchange.chi=params->chi;
1048 d_ij=v3_norm(&dist_ij);
1050 /* save for use in 3bp */
1051 exchange->d_ij=d_ij;
1056 f_r=A*exp(-lambda*d_ij);
1057 df_r=-lambda*f_r/d_ij;
1059 /* f_a, df_a calc + save for 3bp use */
1060 exchange->f_a=-B*exp(-mu*d_ij);
1061 exchange->df_a=-mu*exchange->f_a/d_ij;
1064 /* f_c = 1, df_c = 0 */
1067 v3_scale(&force,&dist_ij,df_r);
1071 arg=M_PI*(d_ij-R)/s_r;
1072 f_c=0.5+0.5*cos(arg);
1073 df_c=-0.5*sin(arg)*(M_PI/(s_r*d_ij));
1074 scale=df_c*f_r+df_r*f_c;
1075 v3_scale(&force,&dist_ij,scale);
1079 v3_add(&(ai->f),&(ai->f),&force);
1080 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1081 moldyn->energy+=(0.25*f_r*f_c);
1083 /* save for use in 3bp */
1085 exchange->df_c=df_c;
1087 /* enable the run of 3bp function */
1093 /* tersoff 3 body part */
1095 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1097 t_tersoff_mult_params *params;
1098 t_tersoff_exchange *exchange;
1099 t_3dvec dist_ij,dist_ik,dist_jk;
1102 double d_ij,d_ij2,d_ik,d_jk;
1103 double f_c,df_c,b_ij,f_a,df_a;
1104 double f_c_ik,df_c_ik,arg;
1107 double n,c,d,h,beta,betan;
1110 double theta,cos_theta,sin_theta;
1111 double d_theta,d_theta1,d_theta2;
1112 double h_cos,h_cos2,d2_h_cos2;
1113 double frac1,bracket1,bracket2,bracket2_n_1,bracket2_n;
1114 double bracket3,bracket3_pow_1,bracket3_pow;
1117 params=moldyn->pot3b_params;
1119 exchange=&(params->exchange);
1121 if(!(exchange->run3bp))
1125 * we need: f_c, d_fc, b_ij, db_ij, f_a, df_a
1127 * we got f_c, df_c, f_a, df_a from 2bp calculation
1130 d_ij=exchange->d_ij;
1131 d_ij2=exchange->d_ij2;
1133 f_a=params->exchange.f_a;
1134 df_a=params->exchange.df_a;
1136 /* d_ij is <= S, as we didn't return so far! */
1139 * calc of b_ij (scalar) and db_ij (vector)
1141 * - for b_ij: chi, beta, f_c_ik, w(=1), c, d, h, n, cos_theta
1143 * - for db_ij: d_theta, sin_theta, cos_theta, f_c_ik, df_c_ik,
1149 v3_sub(&dist_ik,&(ai->r),&(ak->r));
1150 if(bc) check_per_bound(moldyn,&dist_ik);
1151 d_ik=v3_norm(&dist_ik);
1153 /* constants for f_c_ik calc */
1163 /* calc of f_c_ik */
1168 /* f_c_ik = 1, df_c_ik = 0 */
1174 arg=M_PI*(d_ik-R)/s_r;
1175 f_c_ik=0.5+0.5*cos(arg);
1176 df_c_ik=-0.5*sin(arg)*(M_PI/(s_r*d_ik));
1179 v3_sub(&dist_jk,&(aj->r),&(ak->r));
1180 if(bc) check_per_bound(moldyn,&dist_jk);
1181 d_jk=v3_norm(&dist_jk);
1183 beta=*(exchange->beta);
1184 betan=exchange->betan;
1191 c2d2=exchange->c2d2;
1193 numer=d_ij2+d_ik*d_ik-d_jk*d_jk;
1195 cos_theta=numer/denom;
1196 sin_theta=sqrt(1.0-(cos_theta*cos_theta));
1197 theta=acos(cos_theta);
1198 d_theta=(-1.0/sqrt(1.0-cos_theta*cos_theta))/(denom*denom);
1199 d_theta1=2*denom-numer*2*d_ik/d_ij;
1200 d_theta2=2*denom-numer*2*d_ij/d_ik;
1204 h_cos=(h-cos_theta);
1206 d2_h_cos2=d2-h_cos2;
1208 /* some usefull expressions */
1209 frac1=c2/(d2-h_cos2);
1210 bracket1=1+c2d2-frac1;
1211 bracket2=f_c_ik*bracket1;
1212 bracket2_n_1=pow(bracket2,n-1.0);
1213 bracket2_n=bracket2_n_1*bracket2;
1214 bracket3=1+betan*bracket2_n;
1215 bracket3_pow_1=pow(bracket3,(-1.0/(2.0*n))-1.0);
1216 bracket3_pow=bracket3_pow_1*bracket3;
1218 /* now go on with calc of b_ij and derivation of b_ij */
1219 b_ij=chi*bracket3_pow;
1221 /* derivation of theta */
1222 v3_scale(&force,&dist_ij,d_theta1);
1223 v3_scale(&temp,&dist_ik,d_theta2);
1224 v3_add(&force,&force,&temp);
1226 /* part 1 of derivation of b_ij */
1227 v3_scale(&force,&force,sin_theta*2*h_cos*f_c_ik*frac1);
1229 /* part 2 of derivation of b_ij */
1230 v3_scale(&temp,&dist_ik,df_c_ik*bracket1);
1232 /* sum up and scale ... */
1233 v3_add(&temp,&temp,&force);
1234 scale=bracket2_n_1*n*betan*(1+betan*bracket3_pow_1)*chi*(1.0/(2.0*n));
1235 v3_scale(&temp,&temp,scale);
1237 /* now construct an energy and a force out of that */
1238 v3_scale(&temp,&temp,f_a);
1239 v3_scale(&force,&dist_ij,df_a*b_ij);
1240 v3_add(&temp,&temp,&force);
1241 v3_scale(&temp,&temp,f_c);
1242 v3_scale(&force,&dist_ij,df_c*b_ij*f_a);
1243 v3_add(&force,&force,&temp);
1246 v3_add(&(ai->f),&(ai->f),&force);
1247 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1248 moldyn->energy+=(0.25*f_a*b_ij*f_c);