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"
26 int moldyn_usage(char **argv) {
28 printf("\n%s usage:\n\n",argv[0]);
29 printf("--- general options ---\n");
30 printf("-E <steps> <file> (log total energy)\n");
31 printf("-M <steps> <file> (log total momentum)\n");
32 printf("-D <steps> <file> (dump total information)\n");
33 printf("-S <steps> <filebase> (single save file)\n");
34 printf("-V <steps> <filebase> (rasmol file)\n");
35 printf("--- physics options ---\n");
36 printf("-T <temperature> [K] (%f)\n",MOLDYN_TEMP);
37 printf("-t <timestep tau> [s] (%.15f)\n",MOLDYN_TAU);
38 printf("-C <cutoff radius> [m] (%.15f)\n",MOLDYN_CUTOFF);
39 printf("-R <runs> (%d)\n",MOLDYN_RUNS);
40 printf(" -- integration algo --\n");
41 printf(" -I <number> (%d)\n",MOLDYN_INTEGRATE_DEFAULT);
42 printf(" 0: velocity verlet\n");
43 printf(" -- potential --\n");
44 printf(" -P <number> <param1 param2 ...>\n");
45 printf(" 0: harmonic oscillator\n");
46 printf(" param1: spring constant\n");
47 printf(" param2: equilibrium distance\n");
48 printf(" 1: lennard jones\n");
49 printf(" param1: epsilon\n");
50 printf(" param2: sigma\n");
56 int moldyn_parse_argv(t_moldyn *moldyn,int argc,char **argv) {
64 memset(moldyn,0,sizeof(t_moldyn));
67 moldyn->t=MOLDYN_TEMP;
68 moldyn->tau=MOLDYN_TAU;
69 moldyn->time_steps=MOLDYN_RUNS;
70 moldyn->integrate=velocity_verlet;
71 moldyn->potential_force_function=lennard_jones;
78 moldyn->ewrite=atoi(argv[++i]);
79 strncpy(moldyn->efb,argv[++i],64);
82 moldyn->mwrite=atoi(argv[++i]);
83 strncpy(moldyn->mfb,argv[++i],64);
86 moldyn->swrite=atoi(argv[++i]);
87 strncpy(moldyn->sfb,argv[++i],64);
90 moldyn->vwrite=atoi(argv[++i]);
91 strncpy(moldyn->vfb,argv[++i],64);
94 moldyn->t=atof(argv[++i]);
97 moldyn->tau=atof(argv[++i]);
100 moldyn->cutoff=atof(argv[++i]);
103 moldyn->time_steps=atoi(argv[++i]);
106 /* integration algorithm */
107 switch(atoi(argv[++i])) {
108 case MOLDYN_INTEGRATE_VERLET:
109 moldyn->integrate=velocity_verlet;
112 printf("unknown integration algo %s\n",argv[i]);
118 /* potential + params */
119 switch(atoi(argv[++i])) {
120 case MOLDYN_POTENTIAL_HO:
121 hop.spring_constant=atof(argv[++i]);
122 hop.equilibrium_distance=atof(argv[++i]);
123 moldyn->pot_params=malloc(sizeof(t_ho_params));
124 memcpy(moldyn->pot_params,&hop,sizeof(t_ho_params));
125 moldyn->potential_force_function=harmonic_oscillator;
127 case MOLDYN_POTENTIAL_LJ:
131 ljp.sigma6=s*s*s*s*s*s;
132 ljp.sigma12=ljp.sigma6*ljp.sigma6;
133 moldyn->pot_params=malloc(sizeof(t_lj_params));
134 memcpy(moldyn->pot_params,&ljp,sizeof(t_lj_params));
135 moldyn->potential_force_function=lennard_jones;
138 printf("unknown potential %s\n",argv[i]);
144 printf("unknown option %s\n",argv[i]);
157 int moldyn_log_init(t_moldyn *moldyn) {
165 moldyn->efd=open(moldyn->efb,O_WRONLY|O_CREAT|O_TRUNC);
167 perror("[moldyn] efd open");
170 dprintf(moldyn->efd,"# moldyn total energy logfile\n");
171 moldyn->lvstat|=MOLDYN_LVSTAT_TOTAL_E;
175 moldyn->mfd=open(moldyn->mfb,O_WRONLY|O_CREAT|O_TRUNC);
177 perror("[moldyn] mfd open");
180 dprintf(moldyn->mfd,"# moldyn total momentum logfile\n");
181 moldyn->lvstat|=MOLDYN_LVSTAT_TOTAL_M;
185 moldyn->lvstat|=MOLDYN_LVSTAT_SAVE;
187 if((moldyn->vwrite)&&(vis)) {
189 visual_init(vis,moldyn->vfb);
190 moldyn->lvstat|=MOLDYN_LVSTAT_VISUAL;
193 moldyn->lvstat|=MOLDYN_LVSTAT_INITIALIZED;
198 int moldyn_log_shutdown(t_moldyn *moldyn) {
200 if(moldyn->efd) close(moldyn->efd);
201 if(moldyn->mfd) close(moldyn->efd);
202 if(moldyn->dfd) close(moldyn->efd);
203 if(moldyn->visual) visual_tini(moldyn->visual);
208 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
212 ret=moldyn_parse_argv(moldyn,argc,argv);
213 if(ret<0) return ret;
215 ret=moldyn_log_init(moldyn);
216 if(ret<0) return ret;
218 rand_init(&(moldyn->random),NULL,1);
219 moldyn->random.status|=RAND_STAT_VERBOSE;
226 int moldyn_shutdown(t_moldyn *moldyn) {
228 moldyn_log_shutdown(moldyn);
229 rand_close(&(moldyn->random));
235 int create_lattice(u8 type,int element,double mass,double lc,
236 int a,int b,int c,t_atom **atom) {
244 if(type==FCC) count*=4;
245 if(type==DIAMOND) count*=8;
247 *atom=malloc(count*sizeof(t_atom));
249 perror("malloc (atoms)");
257 ret=fcc_init(a,b,c,lc,*atom,&origin);
260 ret=diamond_init(a,b,c,lc,*atom,&origin);
263 printf("unknown lattice type (%02x)\n",type);
269 printf("ok, there is something wrong ...\n");
270 printf("calculated -> %d atoms\n",count);
271 printf("created -> %d atoms\n",ret);
276 (*atom)[count-1].element=element;
277 (*atom)[count-1].mass=mass;
284 int destroy_lattice(t_atom *atom) {
291 int thermal_init(t_moldyn *moldyn) {
294 * - gaussian distribution of velocities
295 * - zero total momentum
296 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
301 t_3dvec p_total,delta;
306 random=&(moldyn->random);
308 /* gaussian distribution of velocities */
310 for(i=0;i<moldyn->count;i++) {
311 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t/atom[i].mass);
313 v=sigma*rand_get_gauss(random);
315 p_total.x+=atom[i].mass*v;
317 v=sigma*rand_get_gauss(random);
319 p_total.y+=atom[i].mass*v;
321 v=sigma*rand_get_gauss(random);
323 p_total.z+=atom[i].mass*v;
326 /* zero total momentum */
327 v3_scale(&p_total,&p_total,1.0/moldyn->count);
328 for(i=0;i<moldyn->count;i++) {
329 v3_scale(&delta,&p_total,1.0/atom[i].mass);
330 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
333 /* velocity scaling */
334 scale_velocity(moldyn);
339 int scale_velocity(t_moldyn *moldyn) {
348 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
351 for(i=0;i<moldyn->count;i++)
352 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
353 c=sqrt((2.0*e)/(3.0*moldyn->count*K_BOLTZMANN*moldyn->t));
354 for(i=0;i<moldyn->count;i++)
355 v3_scale(&(atom[i].v),&(atom[i].v),(1.0/c));
360 double get_e_kin(t_atom *atom,int count) {
367 for(i=0;i<count;i++) {
368 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
374 double get_e_pot(t_moldyn *moldyn) {
376 return moldyn->energy;
379 double get_total_energy(t_moldyn *moldyn) {
383 e=get_e_kin(moldyn->atom,moldyn->count);
384 e+=get_e_pot(moldyn);
389 t_3dvec get_total_p(t_atom *atom, int count) {
395 for(i=0;i<count;i++) {
396 v3_scale(&p,&(atom[i].v),atom[i].mass);
397 v3_add(&p_total,&p_total,&p);
403 double estimate_time_step(t_moldyn *moldyn,double nn_dist,double t) {
407 tau=0.05*nn_dist/(sqrt(3.0*K_BOLTZMANN*t/moldyn->atom[0].mass));
410 printf("[moldyn] warning: time step (%f > %.15f)\n",
420 /* linked list / cell method */
422 int link_cell_init(t_moldyn *moldyn) {
430 lc->listfd=open("/dev/null",O_WRONLY);
432 /* partitioning the md cell */
433 lc->nx=moldyn->dim.x/moldyn->cutoff;
434 lc->x=moldyn->dim.x/lc->nx;
435 lc->ny=moldyn->dim.y/moldyn->cutoff;
436 lc->y=moldyn->dim.y/lc->ny;
437 lc->nz=moldyn->dim.z/moldyn->cutoff;
438 lc->z=moldyn->dim.z/lc->nz;
440 lc->cells=lc->nx*lc->ny*lc->nz;
441 lc->subcell=malloc(lc->cells*sizeof(t_list));
443 printf("initializing linked cells (%d)\n",lc->cells);
445 for(i=0;i<lc->cells;i++)
446 //list_init(&(lc->subcell[i]),1);
447 list_init(&(lc->subcell[i]));
449 link_cell_update(moldyn);
454 int link_cell_update(t_moldyn *moldyn) {
468 for(i=0;i<lc->cells;i++)
469 list_destroy(&(moldyn->lc.subcell[i]));
471 for(count=0;count<moedyn->count;count++) {
472 i=(atom[count].r.x+(moldyn->dim.x/2))/lc->x;
473 j=(atom[count].r.y+(moldyn->dim.y/2))/lc->y;
474 k=(atom[count].r.z+(moldyn->dim.z/2))/lc->z;
475 list_add_immediate_ptr(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
482 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
501 cell[0]=lc->subcell[i+j*nx+k*a];
502 for(ci=-1;ci<=1;ci++) {
509 for(cj=-1;cj<=1;cj++) {
516 for(ck=-1;ck<=1;ck++) {
523 if(!(ci|cj|ck)) continue;
525 cell[--count2]=lc->subcell[x+y*nx+z*a];
528 cell[count1++]=lc->subcell[x+y*nx+z*a];
540 int link_cell_shutdown(t_moldyn *moldyn) {
547 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
548 list_shutdown(&(moldyn->lc.subcell[i]));
550 if(lc->listfd) close(lc->listfd);
557 * 'integration of newtons equation' - algorithms
561 /* start the integration */
563 int moldyn_integrate(t_moldyn *moldyn) {
566 unsigned int e,m,s,d,v;
572 /* initialize linked cell method */
573 link_cell_init(moldyn);
575 /* logging & visualization */
582 if(!(moldyn->lvstat&MOLDYN_LVSTAT_INITIALIZED)) {
583 printf("[moldyn] warning, lv system not initialized\n");
587 /* sqaure of some variables */
588 moldyn->tau_square=moldyn->tau*moldyn->tau;
589 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
591 /* calculate initial forces */
592 moldyn->potential_force_function(moldyn);
594 for(i=0;i<moldyn->time_steps;i++) {
596 /* integration step */
597 moldyn->integrate(moldyn);
599 /* check for log & visualization */
603 "%.15f %.45f\n",i*moldyn->tau,
604 get_total_energy(moldyn));
608 p=get_total_p(moldyn->atom,moldyn->count);
610 "%.15f %.45f\n",i*moldyn->tau,
616 snprintf(fb,128,"%s-%f-%.15f.save",moldyn->sfb,
617 moldyn->t,i*moldyn->tau);
618 fd=open(fb,O_WRONLY|O_TRUNC|O_CREAT);
619 if(fd<0) perror("[moldyn] save fd open");
621 write(fd,moldyn,sizeof(t_moldyn));
622 write(fd,moldyn->atom,
623 moldyn->count*sizeof(t_atom));
630 visual_atoms(moldyn->visual,i*moldyn->tau,
631 moldyn->atom,moldyn->count);
632 printf("\rsteps: %d",i);
641 /* velocity verlet */
643 int velocity_verlet(t_moldyn *moldyn) {
646 double tau,tau_square;
653 tau_square=moldyn->tau_square;
655 for(i=0;i<count;i++) {
657 v3_scale(&delta,&(atom[i].v),tau);
658 v3_add(&(atom[i].r),&(atom[i].r),&delta);
659 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
660 v3_add(&(atom[i].r),&(atom[i].r),&delta);
661 v3_per_bound(&(atom[i].r),&(moldyn->dim));
664 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
665 v3_add(&(atom[i].v),&(atom[i].v),&delta);
668 /* neighbour list update */
669 printf("list update ...\n");
670 link_cell_update(moldyn);
673 /* forces depending on chosen potential */
674 printf("calc potential/force ...\n");
675 potential_force_calc(moldyn);
676 //moldyn->potential_force_function(moldyn);
679 for(i=0;i<count;i++) {
680 /* again velocities */
681 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
682 v3_add(&(atom[i].v),&(atom[i].v),&delta);
691 * potentials & corresponding forces
695 /* generic potential and force calculation */
697 int potential_force_calc(t_moldyn *moldyn) {
702 t_list neighbour[27];
715 for(i=0;i<count;i++) {
718 v3_zero(&(atom[i].f));
720 /* single particle potential/force */
721 if(atom[i].attr&ATOM_ATTR_1BP)
722 moldyn->pf_func1b(moldyn,&(atom[i]));
724 /* 2 body pair potential/force */
725 if(atom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)) {
727 link_cell_neighbour_index(moldyn,
728 (atom[i].r.x+moldyn->dim.x/2)/lc->x,
729 (atom[i].r.y+moldyn->dim.y/2)/lc->y,
730 (atom[i].r.z+moldyn->dim.z/2)/lc->z,
736 for(j=0;j<countn;j++) {
738 this=&(neighbour[j]);
741 if(this->start==NULL)
747 btom=this->current->data;
752 if((btom->attr&ATOM_ATTR_2BP)&
753 (atom[i].attr&ATOM_ATTR_2BP))
754 moldyn->pf_func2b(moldyn,
759 /* 3 body potential/force */
761 if(!(atom[i].attr&ATOM_ATTR_3BP)||
762 !(btom->attr&ATOM_ATTR_3BP))
765 link_cell_neighbour_index(moldyn,
766 (btom->r.x+moldyn->dim.x/2)/lc->x,
767 (btom->r.y+moldyn->dim.y/2)/lc->y,
768 (btom->r.z+moldyn->dim.z/2)/lc->z,
771 for(k=0;k<lc->countn;k++) {
773 thisk=&(neighbourk[k]);
776 if(thisk->start==NULL)
779 bck=(k<lc->dnlc)?0:1;
783 ktom=thisk->current->data;
785 if(!(ktom->attr&ATOM_ATTR_3BP))
794 moldyn->pf_func3b(moldyn,&(atom[i]),btom,ktom,bck);
796 } while(list_next(thisk)!=\
799 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
808 * periodic boundayr checking
811 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
819 if(moldyn->MOLDYN_ATTR_PBX)
820 if(a->x>=x) a->x-=dim->x;
821 else if(-a->x>x) a->x+=dim->x;
822 if(moldyn->MOLDYN_ATTR_PBY)
823 if(a->y>=y) a->y-=dim->y;
824 else if(-a->y>y) a->y+=dim->y;
825 if(moldyn->MOLDYN_ATTR_PBZ)
826 if(a->z>=z) a->z-=dim->z;
827 else if(-a->z>z) a->z+=dim->z;
837 /* harmonic oscillator potential and force */
839 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc)) {
842 t_3dvec force,distance;
846 params=moldyn->pot2b_params;
847 sc=params->spring_constant;
848 equi_dist=params->equilibrium_distance;
850 v3_sub(&distance,&(ai->r),&(aj->r);
852 v3_per_bound(&distance,&(moldyn->dim));
853 if(bc) check_per_bound(moldyn,&distance);
854 d=v3_norm(&distance);
855 if(d<=moldyn->cutoff) {
856 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
857 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
858 v3_scale(&force,&distance,-sc*(1.0-(equi_dist/d)));
859 v3_add(&(ai->f),&(ai->f),&force);
865 /* lennard jones potential & force for one sort of atoms */
867 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
870 t_3dvec force,distance;
872 double eps,sig6,sig12;
874 params=moldyn->pot_params;
875 eps=params->epsilon4;
877 sig12=params->sigma12;
879 v3_sub(&distance,&(ai->r),&(aj->r));
880 if(bc) check_per_bound(moldyn,&distance);
881 d=v3_absolute_square(&distance); /* 1/r^2 */
882 if(d<=moldyn->cutoff_square) {
886 h1=h2*h2; /* 1/r^12 */
887 /* energy is eps*..., but we will add this twice ... */
888 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
895 v3_scale(&force,&distance,d);
896 v3_add(&(ai->f),&(aj->f),&force);
903 * tersoff potential & force for 2 sorts of atoms
906 /* tersoff 1 body part */
907 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
910 t_tersoff_mult_params *params;
911 t_tersoff_exchange *exchange;
914 params=moldyn->pot1b_params;
915 exchange=&(params->exchange);
918 * simple: point constant parameters only depending on atom i to
922 exchange->beta=&(params->beta[num]);
923 exchange->n=&(params->n[num]);
924 exchange->c=&(params->c[num]);
925 exchange->d=&(params->d[num]);
926 exchange->h=&(params->h[num]);
928 exchange->betan=pow(*(exchange->beta),*(exchange->n));
929 exchange->c2=params->c[num]*params->c[num];
930 exchange->d2=params->d[num]*params->d[num];
931 exchange->c2d2=exchange->c2/exchange->d2;
936 /* tersoff 2 body part */
937 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
939 t_tersoff_mult_params *params;
940 t_tersoff_exchange *exchange;
943 double A,B,R,S,lambda;
946 params=moldyn->pot_params;
948 exchange=&(params->exchange);
953 * we need: f_c, df_c, f_r, df_r
955 * therefore we need: R, S, A, lambda
958 v3_sub(&dist_ij,&(ai->r),&(aj->r));
960 if(bc) check_per_bound(moldyn,&dist_ij);
962 /* save for use in 3bp */ /* REALLY ?!?!?! */
963 exchange->dist_ij=dist_ij;
970 lambda=params->lambda[num];
971 /* more constants depending of atoms i and j, needed in 3bp */
972 params->exchange.B=&(params->B[num]);
973 params->exchange.mu=params->mu[num];
974 params->exchange.chi=1.0;
980 lambda=params->lambda_m;
981 /* more constants depending of atoms i and j, needed in 3bp */
982 params->exchange.B=&(params->Bmixed);
983 params->exchange.mu=&(params->mu_m);
984 params->exchange.chi=params->chi;
987 d_ij=v3_norm(&dist_ij);
989 /* save for use in 3bp */
995 f_r=A*exp(-lamda*d_ij);
996 df_r=-lambda*f_r/d_ij;
998 /* f_a, df_a calc + save for 3bp use */
999 exchange->f_a=-B*exp(-mu*d_ij);
1000 exchange->df_a=-mu*exchange->f_a/d_ij;
1003 /* f_c = 1, df_c = 0 */
1006 v3_scale(&force,&dist_ij,df_r);
1010 arg=PI*(d_ij-R)/s_r;
1011 f_c=0.5+0.5*cos(arg);
1012 df_c=-0.5*sin(arg)*(PI/(s_r*d_ij));
1013 scale=df_c*f_r+df_r*f_c;
1014 v3_scale(&force,&dist_ij,scale);
1018 v3_add(&(ai->f),&(ai->f),&force);
1019 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1020 moldyn->energy+=(0.25*f_r*f_c);
1022 /* save for use in 3bp */
1024 exchange->df_c=df_c;
1026 /* enable the run of 3bp function */
1032 /* tersoff 3 body part */
1034 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1036 t_tersoff_mult_params *params;
1037 t_tersoff_exchange *exchange;
1038 t_3dvec dist_ij,dist_ik,dist_jk;
1041 double d_ij,d_ik,d_jk;
1042 double f_c,df_c,b_ij,f_a,df_a;
1043 double n,c,d,h,neta,betan,betan_1;
1044 double theta,cos_theta,sin_theta;
1047 params=moldyn->pot_params;
1049 exchange=params->exchange;
1051 if(!(exchange->run3bp))
1055 * we need: f_c, d_fc, b_ij, db_ij, f_a, df_a
1057 * we got f_c, df_c, f_a, df_a from 2bp calculation
1060 d_ij=exchange->d_ij;
1061 d_ij2=exchange->d_ij2;
1063 f_a=params->exchange.f_a;
1064 df_a=params->exchange.df_a;
1066 /* d_ij is <= S, as we didn't return so far! */
1069 * calc of b_ij (scalar) and db_ij (vector)
1071 * - for b_ij: chi, beta, f_c_ik, w(=1), c, d, h, n, cos_theta
1073 * - for db_ij: d_theta, sin_theta, cos_theta, f_c_ik, df_c_ik,
1079 v3_sub(&dist_ik,&(aj->i),&(ak->r));
1080 if(bc) check_per_bound(moldyn,&dist_ik);
1081 d_ik=v3_norm(&dist_ik);
1083 /* constants for f_c_ik calc */
1093 /* calc of f_c_ik */
1098 /* f_c_ik = 1, df_c_ik = 0 */
1104 arg=PI*(d_ik-R)/s_r;
1105 f_c_ik=0.5+0.5*cos(arg);
1106 df_c_ik=-0.5*sin(arg)*(PI/(s_r*d_ik));
1109 v3_sub(&dist_jk,&(aj->r),&(ak->r));
1110 if(bc) check_per_bound(moldyn,&dist_jk);
1111 d_jk=v3_norm(&dist_jk);
1113 beta=*(exchange->beta);
1114 betan=exchange->betan;
1121 c2d2=exchange->c2d2;
1123 numer=d_ij2+d_ik*d_ik-d_jk*d_jk;
1125 cos_theta=numer/denom;
1126 sin_theta=sqrt(1.0-(cos_theta*cos_theta));
1127 theta=arccos(cos_theta);
1128 d_theta=(-1.0/sqrt(1.0-cos_theta*cos_theta))/(denom*denom);
1129 d_theta1=2*denom-numer*2*d_ik/d_ij;
1130 d_theta2=2*denom-numer*2*d_ij/d_ik;
1134 h_cos=(h-cos_theta);
1136 d2_h_cos2=d2-h_cos2;
1138 /* some usefull expressions */
1139 frac1=c2/(d2-h_cos2);
1140 bracket1=1+c2d2-frac1;
1141 bracket2=f_c_ik*bracket1;
1142 bracket2_n_1=pow(bracket2,n-1.0);
1143 bracket2_n=bracket2_n_1*bracket2;
1144 bracket3=1+betan*bracket2_n;
1145 bracket3_pow_1=pow(bracket3,(-1.0/(2.0*n))-1.0);
1146 bracket3_pow=bracket3_pow_1*bracket3;
1148 /* now go on with calc of b_ij and derivation of b_ij */
1149 b_ij=chi*bracket3_pow;
1151 /* derivation of theta */
1152 v3_scale(&force,&dist_ij,d1_theta);
1153 v3_scale(&temp,&dist_ik,d_theta2);
1154 v3_add(&force,&force,&temp);
1156 /* part 1 of derivation of b_ij */
1157 v3_scale(&force,sin_theta*2*h_cos*f_c_ik*frac1);
1159 /* part 2 of derivation of b_ij */
1160 v3_scale(&temp,&dist_ik,df_c_ik*bracket1);
1162 /* sum up and scale ... */
1163 v3_add(&temp,&temp,&force);
1164 scale=bracket2_n_1*n*betan*(1+betan*bracket3_pow_1)*chi*(1.0/(2.0*n));
1165 v3_scale(&temp,&temp,scale);
1167 /* now construct an energy and a force out of that */
1168 v3_scale(&temp,&temp,f_a);
1169 v3_scale(&force,&dist_ij,df_a*b_ij);
1170 v3_add(&temp,&temp,&force);
1171 v3_scale(&temp,&temp,f_c);
1172 v3_scale(&force,&dist_ij,df_c*b_ij*f_a);
1173 v3_add(&force,&force,&temp);
1176 v3_add(&(ai->f),&(ai->f),&force);
1177 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1178 moldyn->energy+=(0.25*f_a*b_ij*f_c);