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) {
60 memset(moldyn,0,sizeof(t_moldyn));
63 moldyn->t=MOLDYN_TEMP;
64 moldyn->tau=MOLDYN_TAU;
65 moldyn->time_steps=MOLDYN_RUNS;
66 moldyn->integrate=velocity_verlet;
73 moldyn->ewrite=atoi(argv[++i]);
74 strncpy(moldyn->efb,argv[++i],64);
77 moldyn->mwrite=atoi(argv[++i]);
78 strncpy(moldyn->mfb,argv[++i],64);
81 moldyn->swrite=atoi(argv[++i]);
82 strncpy(moldyn->sfb,argv[++i],64);
85 moldyn->vwrite=atoi(argv[++i]);
86 strncpy(moldyn->vfb,argv[++i],64);
89 moldyn->t=atof(argv[++i]);
92 moldyn->tau=atof(argv[++i]);
95 moldyn->cutoff=atof(argv[++i]);
98 moldyn->time_steps=atoi(argv[++i]);
101 /* integration algorithm */
102 switch(atoi(argv[++i])) {
103 case MOLDYN_INTEGRATE_VERLET:
104 moldyn->integrate=velocity_verlet;
107 printf("unknown integration algo %s\n",argv[i]);
113 /* potential + params */
114 switch(atoi(argv[++i])) {
115 case MOLDYN_POTENTIAL_HO:
116 hop.spring_constant=atof(argv[++i]);
117 hop.equilibrium_distance=atof(argv[++i]);
118 moldyn->pot_params=malloc(sizeof(t_ho_params));
119 memcpy(moldyn->pot_params,&hop,sizeof(t_ho_params));
120 moldyn->potential_force_function=harmonic_oscillator;
122 case MOLDYN_POTENTIAL_LJ:
126 ljp.sigma6=s*s*s*s*s*s;
127 ljp.sigma12=ljp.sigma6*ljp.sigma6;
128 moldyn->pot_params=malloc(sizeof(t_lj_params));
129 memcpy(moldyn->pot_params,&ljp,sizeof(t_lj_params));
130 moldyn->potential_force_function=lennard_jones;
133 printf("unknown potential %s\n",argv[i]);
139 printf("unknown option %s\n",argv[i]);
152 int moldyn_log_init(t_moldyn *moldyn) {
160 moldyn->efd=open(moldyn->efb,O_WRONLY|O_CREAT|O_TRUNC);
162 perror("[moldyn] efd open");
165 dprintf(moldyn->efd,"# moldyn total energy logfile\n");
166 moldyn->lvstat|=MOLDYN_LVSTAT_TOTAL_E;
170 moldyn->mfd=open(moldyn->mfb,O_WRONLY|O_CREAT|O_TRUNC);
172 perror("[moldyn] mfd open");
175 dprintf(moldyn->mfd,"# moldyn total momentum logfile\n");
176 moldyn->lvstat|=MOLDYN_LVSTAT_TOTAL_M;
180 moldyn->lvstat|=MOLDYN_LVSTAT_SAVE;
182 if((moldyn->vwrite)&&(vis)) {
184 visual_init(vis,moldyn->vfb);
185 moldyn->lvstat|=MOLDYN_LVSTAT_VISUAL;
188 moldyn->lvstat|=MOLDYN_LVSTAT_INITIALIZED;
193 int moldyn_log_shutdown(t_moldyn *moldyn) {
195 if(moldyn->efd) close(moldyn->efd);
196 if(moldyn->mfd) close(moldyn->efd);
197 if(moldyn->dfd) close(moldyn->efd);
198 if(moldyn->visual) visual_tini(moldyn->visual);
203 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
207 ret=moldyn_parse_argv(moldyn,argc,argv);
208 if(ret<0) return ret;
210 ret=moldyn_log_init(moldyn);
211 if(ret<0) return ret;
213 rand_init(&(moldyn->random),NULL,1);
214 moldyn->random.status|=RAND_STAT_VERBOSE;
221 int moldyn_shutdown(t_moldyn *moldyn) {
223 moldyn_log_shutdown(moldyn);
224 rand_close(&(moldyn->random));
230 int create_lattice(u8 type,int element,double mass,double lc,
231 int a,int b,int c,t_atom **atom) {
239 if(type==FCC) count*=4;
240 if(type==DIAMOND) count*=8;
242 *atom=malloc(count*sizeof(t_atom));
244 perror("malloc (atoms)");
252 ret=fcc_init(a,b,c,lc,*atom,&origin);
255 ret=diamond_init(a,b,c,lc,*atom,&origin);
258 printf("unknown lattice type (%02x)\n",type);
264 printf("ok, there is something wrong ...\n");
265 printf("calculated -> %d atoms\n",count);
266 printf("created -> %d atoms\n",ret);
271 (*atom)[count-1].element=element;
272 (*atom)[count-1].mass=mass;
279 int destroy_lattice(t_atom *atom) {
286 int thermal_init(t_moldyn *moldyn) {
289 * - gaussian distribution of velocities
290 * - zero total momentum
291 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
296 t_3dvec p_total,delta;
301 random=&(moldyn->random);
303 /* gaussian distribution of velocities */
305 for(i=0;i<moldyn->count;i++) {
306 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t/atom[i].mass);
308 v=sigma*rand_get_gauss(random);
310 p_total.x+=atom[i].mass*v;
312 v=sigma*rand_get_gauss(random);
314 p_total.y+=atom[i].mass*v;
316 v=sigma*rand_get_gauss(random);
318 p_total.z+=atom[i].mass*v;
321 /* zero total momentum */
322 v3_scale(&p_total,&p_total,1.0/moldyn->count);
323 for(i=0;i<moldyn->count;i++) {
324 v3_scale(&delta,&p_total,1.0/atom[i].mass);
325 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
328 /* velocity scaling */
329 scale_velocity(moldyn);
334 int scale_velocity(t_moldyn *moldyn) {
343 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
346 for(i=0;i<moldyn->count;i++)
347 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
348 c=sqrt((2.0*e)/(3.0*moldyn->count*K_BOLTZMANN*moldyn->t));
349 for(i=0;i<moldyn->count;i++)
350 v3_scale(&(atom[i].v),&(atom[i].v),(1.0/c));
355 double get_e_kin(t_atom *atom,int count) {
362 for(i=0;i<count;i++) {
363 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
369 double get_e_pot(t_moldyn *moldyn) {
371 return moldyn->energy;
374 double get_total_energy(t_moldyn *moldyn) {
378 e=get_e_kin(moldyn->atom,moldyn->count);
379 e+=get_e_pot(moldyn);
384 t_3dvec get_total_p(t_atom *atom, int count) {
390 for(i=0;i<count;i++) {
391 v3_scale(&p,&(atom[i].v),atom[i].mass);
392 v3_add(&p_total,&p_total,&p);
398 double estimate_time_step(t_moldyn *moldyn,double nn_dist,double t) {
402 tau=0.05*nn_dist/(sqrt(3.0*K_BOLTZMANN*t/moldyn->atom[0].mass));
405 printf("[moldyn] warning: time step (%f > %.15f)\n",
415 /* linked list / cell method */
417 int link_cell_init(t_moldyn *moldyn) {
425 lc->listfd=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("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]));
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<moedyn->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]));
545 if(lc->listfd) close(lc->listfd);
550 int moldyn_add_schedule(t_moldyn *moldyn,) {
556 int moldyn_set_schedule_hook(t_moldyn *moldyn,void *hook,void *hook_params) {
564 * 'integration of newtons equation' - algorithms
568 /* start the integration */
570 int moldyn_integrate(t_moldyn *moldyn) {
573 unsigned int e,m,s,d,v;
579 /* initialize linked cell method */
580 link_cell_init(moldyn);
582 /* logging & visualization */
589 if(!(moldyn->lvstat&MOLDYN_LVSTAT_INITIALIZED)) {
590 printf("[moldyn] warning, lv system not initialized\n");
594 /* sqaure of some variables */
595 moldyn->tau_square=moldyn->tau*moldyn->tau;
596 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
598 /* calculate initial forces */
599 moldyn->potential_force_function(moldyn);
601 for(sched=0;sched<moldyn->schedule.content_count;sched++) {
603 /* setting amont of runs and finite time step size */
604 moldyn->tau=schedule->tau[sched];
605 moldyn->tau_square=moldyn->tau*moldyn->tau;
606 moldyn->timesteps=schedule->runs[sched];
608 /* integration according to schedule */
610 for(i=0;i<moldyn->time_steps;i++) {
612 /* integration step */
613 moldyn->integrate(moldyn);
615 /* check for log & visualization */
619 "%.15f %.45f\n",i*moldyn->tau,
620 get_total_energy(moldyn));
624 p=get_total_p(moldyn->atom,moldyn->count);
626 "%.15f %.45f\n",i*moldyn->tau,
632 snprintf(fb,128,"%s-%f-%.15f.save",moldyn->sfb,
633 moldyn->t,i*moldyn->tau);
634 fd=open(fb,O_WRONLY|O_TRUNC|O_CREAT);
635 if(fd<0) perror("[moldyn] save fd open");
637 write(fd,moldyn,sizeof(t_moldyn));
638 write(fd,moldyn->atom,
639 moldyn->count*sizeof(t_atom));
646 visual_atoms(moldyn->visual,i*moldyn->tau,
647 moldyn->atom,moldyn->count);
648 printf("\rsteps: %d",i);
654 /* check for hooks */
656 schedule->hook(moldyn,schedule->hook_params);
661 /* velocity verlet */
663 int velocity_verlet(t_moldyn *moldyn) {
666 double tau,tau_square;
673 tau_square=moldyn->tau_square;
675 for(i=0;i<count;i++) {
677 v3_scale(&delta,&(atom[i].v),tau);
678 v3_add(&(atom[i].r),&(atom[i].r),&delta);
679 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
680 v3_add(&(atom[i].r),&(atom[i].r),&delta);
681 v3_per_bound(&(atom[i].r),&(moldyn->dim));
684 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
685 v3_add(&(atom[i].v),&(atom[i].v),&delta);
688 /* neighbour list update */
689 printf("list update ...\n");
690 link_cell_update(moldyn);
693 /* forces depending on chosen potential */
694 printf("calc potential/force ...\n");
695 potential_force_calc(moldyn);
696 //moldyn->potential_force_function(moldyn);
699 for(i=0;i<count;i++) {
700 /* again velocities */
701 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
702 v3_add(&(atom[i].v),&(atom[i].v),&delta);
711 * potentials & corresponding forces
715 /* generic potential and force calculation */
717 int potential_force_calc(t_moldyn *moldyn) {
722 t_list neighbour[27];
735 for(i=0;i<count;i++) {
738 v3_zero(&(atom[i].f));
740 /* single particle potential/force */
741 if(atom[i].attr&ATOM_ATTR_1BP)
742 moldyn->pf_func1b(moldyn,&(atom[i]));
744 /* 2 body pair potential/force */
745 if(atom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)) {
747 link_cell_neighbour_index(moldyn,
748 (atom[i].r.x+moldyn->dim.x/2)/lc->x,
749 (atom[i].r.y+moldyn->dim.y/2)/lc->y,
750 (atom[i].r.z+moldyn->dim.z/2)/lc->z,
756 for(j=0;j<countn;j++) {
758 this=&(neighbour[j]);
761 if(this->start==NULL)
767 btom=this->current->data;
772 if((btom->attr&ATOM_ATTR_2BP)&
773 (atom[i].attr&ATOM_ATTR_2BP))
774 moldyn->pf_func2b(moldyn,
779 /* 3 body potential/force */
781 if(!(atom[i].attr&ATOM_ATTR_3BP)||
782 !(btom->attr&ATOM_ATTR_3BP))
785 link_cell_neighbour_index(moldyn,
786 (btom->r.x+moldyn->dim.x/2)/lc->x,
787 (btom->r.y+moldyn->dim.y/2)/lc->y,
788 (btom->r.z+moldyn->dim.z/2)/lc->z,
791 for(k=0;k<lc->countn;k++) {
793 thisk=&(neighbourk[k]);
796 if(thisk->start==NULL)
799 bck=(k<lc->dnlc)?0:1;
803 ktom=thisk->current->data;
805 if(!(ktom->attr&ATOM_ATTR_3BP))
814 moldyn->pf_func3b(moldyn,&(atom[i]),btom,ktom,bck);
816 } while(list_next(thisk)!=\
819 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
828 * periodic boundayr checking
831 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
839 if(moldyn->MOLDYN_ATTR_PBX)
840 if(a->x>=x) a->x-=dim->x;
841 else if(-a->x>x) a->x+=dim->x;
842 if(moldyn->MOLDYN_ATTR_PBY)
843 if(a->y>=y) a->y-=dim->y;
844 else if(-a->y>y) a->y+=dim->y;
845 if(moldyn->MOLDYN_ATTR_PBZ)
846 if(a->z>=z) a->z-=dim->z;
847 else if(-a->z>z) a->z+=dim->z;
857 /* harmonic oscillator potential and force */
859 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc)) {
862 t_3dvec force,distance;
866 params=moldyn->pot2b_params;
867 sc=params->spring_constant;
868 equi_dist=params->equilibrium_distance;
870 v3_sub(&distance,&(ai->r),&(aj->r);
872 v3_per_bound(&distance,&(moldyn->dim));
873 if(bc) check_per_bound(moldyn,&distance);
874 d=v3_norm(&distance);
875 if(d<=moldyn->cutoff) {
876 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
877 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
878 v3_scale(&force,&distance,-sc*(1.0-(equi_dist/d)));
879 v3_add(&(ai->f),&(ai->f),&force);
885 /* lennard jones potential & force for one sort of atoms */
887 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
890 t_3dvec force,distance;
892 double eps,sig6,sig12;
894 params=moldyn->pot_params;
895 eps=params->epsilon4;
897 sig12=params->sigma12;
899 v3_sub(&distance,&(ai->r),&(aj->r));
900 if(bc) check_per_bound(moldyn,&distance);
901 d=v3_absolute_square(&distance); /* 1/r^2 */
902 if(d<=moldyn->cutoff_square) {
906 h1=h2*h2; /* 1/r^12 */
907 /* energy is eps*..., but we will add this twice ... */
908 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
915 v3_scale(&force,&distance,d);
916 v3_add(&(ai->f),&(aj->f),&force);
923 * tersoff potential & force for 2 sorts of atoms
926 /* tersoff 1 body part */
927 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
930 t_tersoff_mult_params *params;
931 t_tersoff_exchange *exchange;
934 params=moldyn->pot1b_params;
935 exchange=&(params->exchange);
938 * simple: point constant parameters only depending on atom i to
942 exchange->beta=&(params->beta[num]);
943 exchange->n=&(params->n[num]);
944 exchange->c=&(params->c[num]);
945 exchange->d=&(params->d[num]);
946 exchange->h=&(params->h[num]);
948 exchange->betan=pow(*(exchange->beta),*(exchange->n));
949 exchange->c2=params->c[num]*params->c[num];
950 exchange->d2=params->d[num]*params->d[num];
951 exchange->c2d2=exchange->c2/exchange->d2;
956 /* tersoff 2 body part */
957 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
959 t_tersoff_mult_params *params;
960 t_tersoff_exchange *exchange;
963 double A,B,R,S,lambda;
966 params=moldyn->pot_params;
968 exchange=&(params->exchange);
973 * we need: f_c, df_c, f_r, df_r
975 * therefore we need: R, S, A, lambda
978 v3_sub(&dist_ij,&(ai->r),&(aj->r));
980 if(bc) check_per_bound(moldyn,&dist_ij);
982 /* save for use in 3bp */ /* REALLY ?!?!?! */
983 exchange->dist_ij=dist_ij;
990 lambda=params->lambda[num];
991 /* more constants depending of atoms i and j, needed in 3bp */
992 params->exchange.B=&(params->B[num]);
993 params->exchange.mu=params->mu[num];
994 params->exchange.chi=1.0;
1000 lambda=params->lambda_m;
1001 /* more constants depending of atoms i and j, needed in 3bp */
1002 params->exchange.B=&(params->Bmixed);
1003 params->exchange.mu=&(params->mu_m);
1004 params->exchange.chi=params->chi;
1007 d_ij=v3_norm(&dist_ij);
1009 /* save for use in 3bp */
1010 exchange->d_ij=d_ij;
1015 f_r=A*exp(-lamda*d_ij);
1016 df_r=-lambda*f_r/d_ij;
1018 /* f_a, df_a calc + save for 3bp use */
1019 exchange->f_a=-B*exp(-mu*d_ij);
1020 exchange->df_a=-mu*exchange->f_a/d_ij;
1023 /* f_c = 1, df_c = 0 */
1026 v3_scale(&force,&dist_ij,df_r);
1030 arg=PI*(d_ij-R)/s_r;
1031 f_c=0.5+0.5*cos(arg);
1032 df_c=-0.5*sin(arg)*(PI/(s_r*d_ij));
1033 scale=df_c*f_r+df_r*f_c;
1034 v3_scale(&force,&dist_ij,scale);
1038 v3_add(&(ai->f),&(ai->f),&force);
1039 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1040 moldyn->energy+=(0.25*f_r*f_c);
1042 /* save for use in 3bp */
1044 exchange->df_c=df_c;
1046 /* enable the run of 3bp function */
1052 /* tersoff 3 body part */
1054 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1056 t_tersoff_mult_params *params;
1057 t_tersoff_exchange *exchange;
1058 t_3dvec dist_ij,dist_ik,dist_jk;
1061 double d_ij,d_ik,d_jk;
1062 double f_c,df_c,b_ij,f_a,df_a;
1063 double n,c,d,h,neta,betan,betan_1;
1064 double theta,cos_theta,sin_theta;
1067 params=moldyn->pot_params;
1069 exchange=params->exchange;
1071 if(!(exchange->run3bp))
1075 * we need: f_c, d_fc, b_ij, db_ij, f_a, df_a
1077 * we got f_c, df_c, f_a, df_a from 2bp calculation
1080 d_ij=exchange->d_ij;
1081 d_ij2=exchange->d_ij2;
1083 f_a=params->exchange.f_a;
1084 df_a=params->exchange.df_a;
1086 /* d_ij is <= S, as we didn't return so far! */
1089 * calc of b_ij (scalar) and db_ij (vector)
1091 * - for b_ij: chi, beta, f_c_ik, w(=1), c, d, h, n, cos_theta
1093 * - for db_ij: d_theta, sin_theta, cos_theta, f_c_ik, df_c_ik,
1099 v3_sub(&dist_ik,&(aj->i),&(ak->r));
1100 if(bc) check_per_bound(moldyn,&dist_ik);
1101 d_ik=v3_norm(&dist_ik);
1103 /* constants for f_c_ik calc */
1113 /* calc of f_c_ik */
1118 /* f_c_ik = 1, df_c_ik = 0 */
1124 arg=PI*(d_ik-R)/s_r;
1125 f_c_ik=0.5+0.5*cos(arg);
1126 df_c_ik=-0.5*sin(arg)*(PI/(s_r*d_ik));
1129 v3_sub(&dist_jk,&(aj->r),&(ak->r));
1130 if(bc) check_per_bound(moldyn,&dist_jk);
1131 d_jk=v3_norm(&dist_jk);
1133 beta=*(exchange->beta);
1134 betan=exchange->betan;
1141 c2d2=exchange->c2d2;
1143 numer=d_ij2+d_ik*d_ik-d_jk*d_jk;
1145 cos_theta=numer/denom;
1146 sin_theta=sqrt(1.0-(cos_theta*cos_theta));
1147 theta=arccos(cos_theta);
1148 d_theta=(-1.0/sqrt(1.0-cos_theta*cos_theta))/(denom*denom);
1149 d_theta1=2*denom-numer*2*d_ik/d_ij;
1150 d_theta2=2*denom-numer*2*d_ij/d_ik;
1154 h_cos=(h-cos_theta);
1156 d2_h_cos2=d2-h_cos2;
1158 /* some usefull expressions */
1159 frac1=c2/(d2-h_cos2);
1160 bracket1=1+c2d2-frac1;
1161 bracket2=f_c_ik*bracket1;
1162 bracket2_n_1=pow(bracket2,n-1.0);
1163 bracket2_n=bracket2_n_1*bracket2;
1164 bracket3=1+betan*bracket2_n;
1165 bracket3_pow_1=pow(bracket3,(-1.0/(2.0*n))-1.0);
1166 bracket3_pow=bracket3_pow_1*bracket3;
1168 /* now go on with calc of b_ij and derivation of b_ij */
1169 b_ij=chi*bracket3_pow;
1171 /* derivation of theta */
1172 v3_scale(&force,&dist_ij,d1_theta);
1173 v3_scale(&temp,&dist_ik,d_theta2);
1174 v3_add(&force,&force,&temp);
1176 /* part 1 of derivation of b_ij */
1177 v3_scale(&force,sin_theta*2*h_cos*f_c_ik*frac1);
1179 /* part 2 of derivation of b_ij */
1180 v3_scale(&temp,&dist_ik,df_c_ik*bracket1);
1182 /* sum up and scale ... */
1183 v3_add(&temp,&temp,&force);
1184 scale=bracket2_n_1*n*betan*(1+betan*bracket3_pow_1)*chi*(1.0/(2.0*n));
1185 v3_scale(&temp,&temp,scale);
1187 /* now construct an energy and a force out of that */
1188 v3_scale(&temp,&temp,f_a);
1189 v3_scale(&force,&dist_ij,df_a*b_ij);
1190 v3_add(&temp,&temp,&force);
1191 v3_scale(&temp,&temp,f_c);
1192 v3_scale(&force,&dist_ij,df_c*b_ij*f_a);
1193 v3_add(&force,&force,&temp);
1196 v3_add(&(ai->f),&(ai->f),&force);
1197 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1198 moldyn->energy+=(0.25*f_a*b_ij*f_c);