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(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
231 u8 attr,u8 bnum,int a,int b,int c) {
241 if(type==FCC) count*=4;
242 if(type==DIAMOND) count*=8;
244 atom=malloc(count*sizeof(t_atom));
246 perror("malloc (atoms)");
254 ret=fcc_init(a,b,c,lc,atom,&origin);
257 ret=diamond_init(a,b,c,lc,atom,&origin);
260 printf("unknown lattice type (%02x)\n",type);
266 printf("ok, there is something wrong ...\n");
267 printf("calculated -> %d atoms\n",count);
268 printf("created -> %d atoms\n",ret);
273 atom[count-1].element=element;
274 atom[count-1].mass=mass;
275 atom[count-1].attr=attr;
276 atom[count-1].bnum=bnum;
283 int add_atom(t_moldyn *moldyn,int element,double mass,u8 bnum,u8 attr,
284 t_3dvec r,t_3dvec v) {
291 count=++(moldyn->count);
293 ptr=realloc(atom,count*sizeof(t_atom));
295 perror("[moldyn] realloc (add atom)");
302 atom->element=element;
309 int destroy_atoms(t_moldyn *moldyn) {
311 if(moldyn->atom) free(moldyn->atom);
316 int thermal_init(t_moldyn *moldyn) {
319 * - gaussian distribution of velocities
320 * - zero total momentum
321 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
326 t_3dvec p_total,delta;
331 random=&(moldyn->random);
333 /* gaussian distribution of velocities */
335 for(i=0;i<moldyn->count;i++) {
336 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t/atom[i].mass);
338 v=sigma*rand_get_gauss(random);
340 p_total.x+=atom[i].mass*v;
342 v=sigma*rand_get_gauss(random);
344 p_total.y+=atom[i].mass*v;
346 v=sigma*rand_get_gauss(random);
348 p_total.z+=atom[i].mass*v;
351 /* zero total momentum */
352 v3_scale(&p_total,&p_total,1.0/moldyn->count);
353 for(i=0;i<moldyn->count;i++) {
354 v3_scale(&delta,&p_total,1.0/atom[i].mass);
355 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
358 /* velocity scaling */
359 scale_velocity(moldyn);
364 int scale_velocity(t_moldyn *moldyn) {
373 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
376 for(i=0;i<moldyn->count;i++)
377 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
378 c=sqrt((2.0*e)/(3.0*moldyn->count*K_BOLTZMANN*moldyn->t));
379 for(i=0;i<moldyn->count;i++)
380 v3_scale(&(atom[i].v),&(atom[i].v),(1.0/c));
385 double get_e_kin(t_atom *atom,int count) {
392 for(i=0;i<count;i++) {
393 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
399 double get_e_pot(t_moldyn *moldyn) {
401 return moldyn->energy;
404 double get_total_energy(t_moldyn *moldyn) {
408 e=get_e_kin(moldyn->atom,moldyn->count);
409 e+=get_e_pot(moldyn);
414 t_3dvec get_total_p(t_atom *atom, int count) {
420 for(i=0;i<count;i++) {
421 v3_scale(&p,&(atom[i].v),atom[i].mass);
422 v3_add(&p_total,&p_total,&p);
428 double estimate_time_step(t_moldyn *moldyn,double nn_dist,double t) {
432 tau=0.05*nn_dist/(sqrt(3.0*K_BOLTZMANN*t/moldyn->atom[0].mass));
435 printf("[moldyn] warning: time step (%f > %.15f)\n",
445 /* linked list / cell method */
447 int link_cell_init(t_moldyn *moldyn) {
455 lc->listfd=open("/dev/null",O_WRONLY);
457 /* partitioning the md cell */
458 lc->nx=moldyn->dim.x/moldyn->cutoff;
459 lc->x=moldyn->dim.x/lc->nx;
460 lc->ny=moldyn->dim.y/moldyn->cutoff;
461 lc->y=moldyn->dim.y/lc->ny;
462 lc->nz=moldyn->dim.z/moldyn->cutoff;
463 lc->z=moldyn->dim.z/lc->nz;
465 lc->cells=lc->nx*lc->ny*lc->nz;
466 lc->subcell=malloc(lc->cells*sizeof(t_list));
468 printf("initializing linked cells (%d)\n",lc->cells);
470 for(i=0;i<lc->cells;i++)
471 //list_init(&(lc->subcell[i]),1);
472 list_init(&(lc->subcell[i]));
474 link_cell_update(moldyn);
479 int link_cell_update(t_moldyn *moldyn) {
493 for(i=0;i<lc->cells;i++)
494 list_destroy(&(moldyn->lc.subcell[i]));
496 for(count=0;count<moedyn->count;count++) {
497 i=(atom[count].r.x+(moldyn->dim.x/2))/lc->x;
498 j=(atom[count].r.y+(moldyn->dim.y/2))/lc->y;
499 k=(atom[count].r.z+(moldyn->dim.z/2))/lc->z;
500 list_add_immediate_ptr(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
507 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
526 cell[0]=lc->subcell[i+j*nx+k*a];
527 for(ci=-1;ci<=1;ci++) {
534 for(cj=-1;cj<=1;cj++) {
541 for(ck=-1;ck<=1;ck++) {
548 if(!(ci|cj|ck)) continue;
550 cell[--count2]=lc->subcell[x+y*nx+z*a];
553 cell[count1++]=lc->subcell[x+y*nx+z*a];
565 int link_cell_shutdown(t_moldyn *moldyn) {
572 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
573 list_shutdown(&(moldyn->lc.subcell[i]));
575 if(lc->listfd) close(lc->listfd);
580 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau ) {
584 t_moldyn_schedule *schedule;
586 schedule=moldyn->schedule;
587 count=++(schedule->content_count);
589 ptr=realloc(moldyn->schedule.runs,count*sizeof(int));
591 perror("[moldyn] realloc (runs)");
594 moldyn->schedule.runs[count-1]=runs;
596 ptr=realloc(schedule->tau,count*sizeof(double));
598 perror("[moldyn] realloc (tau)");
601 moldyn->schedule.tau[count-1]=tau;
606 int moldyn_set_schedule_hook(t_moldyn *moldyn,void *hook,void *hook_params) {
608 moldyn->schedule.hook=hook;
609 moldyn->schedule.hook_params=hook_params;
616 * 'integration of newtons equation' - algorithms
620 /* start the integration */
622 int moldyn_integrate(t_moldyn *moldyn) {
625 unsigned int e,m,s,d,v;
631 /* initialize linked cell method */
632 link_cell_init(moldyn);
634 /* logging & visualization */
641 if(!(moldyn->lvstat&MOLDYN_LVSTAT_INITIALIZED)) {
642 printf("[moldyn] warning, lv system not initialized\n");
646 /* sqaure of some variables */
647 moldyn->tau_square=moldyn->tau*moldyn->tau;
648 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
650 /* calculate initial forces */
651 moldyn->potential_force_function(moldyn);
653 for(sched=0;sched<moldyn->schedule.content_count;sched++) {
655 /* setting amont of runs and finite time step size */
656 moldyn->tau=schedule->tau[sched];
657 moldyn->tau_square=moldyn->tau*moldyn->tau;
658 moldyn->timesteps=schedule->runs[sched];
660 /* integration according to schedule */
662 for(i=0;i<moldyn->time_steps;i++) {
664 /* integration step */
665 moldyn->integrate(moldyn);
667 /* check for log & visualization */
671 "%.15f %.45f\n",i*moldyn->tau,
672 get_total_energy(moldyn));
676 p=get_total_p(moldyn->atom,moldyn->count);
678 "%.15f %.45f\n",i*moldyn->tau,
684 snprintf(fb,128,"%s-%f-%.15f.save",moldyn->sfb,
685 moldyn->t,i*moldyn->tau);
686 fd=open(fb,O_WRONLY|O_TRUNC|O_CREAT);
687 if(fd<0) perror("[moldyn] save fd open");
689 write(fd,moldyn,sizeof(t_moldyn));
690 write(fd,moldyn->atom,
691 moldyn->count*sizeof(t_atom));
698 visual_atoms(moldyn->visual,i*moldyn->tau,
699 moldyn->atom,moldyn->count);
700 printf("\rsteps: %d",i);
706 /* check for hooks */
708 schedule->hook(moldyn,schedule->hook_params);
713 /* velocity verlet */
715 int velocity_verlet(t_moldyn *moldyn) {
718 double tau,tau_square;
725 tau_square=moldyn->tau_square;
727 for(i=0;i<count;i++) {
729 v3_scale(&delta,&(atom[i].v),tau);
730 v3_add(&(atom[i].r),&(atom[i].r),&delta);
731 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
732 v3_add(&(atom[i].r),&(atom[i].r),&delta);
733 v3_per_bound(&(atom[i].r),&(moldyn->dim));
736 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
737 v3_add(&(atom[i].v),&(atom[i].v),&delta);
740 /* neighbour list update */
741 printf("list update ...\n");
742 link_cell_update(moldyn);
745 /* forces depending on chosen potential */
746 printf("calc potential/force ...\n");
747 potential_force_calc(moldyn);
748 //moldyn->potential_force_function(moldyn);
751 for(i=0;i<count;i++) {
752 /* again velocities */
753 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
754 v3_add(&(atom[i].v),&(atom[i].v),&delta);
763 * potentials & corresponding forces
767 /* generic potential and force calculation */
769 int potential_force_calc(t_moldyn *moldyn) {
774 t_list neighbour[27];
787 for(i=0;i<count;i++) {
790 v3_zero(&(atom[i].f));
792 /* single particle potential/force */
793 if(atom[i].attr&ATOM_ATTR_1BP)
794 moldyn->pf_func1b(moldyn,&(atom[i]));
796 /* 2 body pair potential/force */
797 if(atom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)) {
799 link_cell_neighbour_index(moldyn,
800 (atom[i].r.x+moldyn->dim.x/2)/lc->x,
801 (atom[i].r.y+moldyn->dim.y/2)/lc->y,
802 (atom[i].r.z+moldyn->dim.z/2)/lc->z,
808 for(j=0;j<countn;j++) {
810 this=&(neighbour[j]);
813 if(this->start==NULL)
819 btom=this->current->data;
824 if((btom->attr&ATOM_ATTR_2BP)&
825 (atom[i].attr&ATOM_ATTR_2BP))
826 moldyn->pf_func2b(moldyn,
831 /* 3 body potential/force */
833 if(!(atom[i].attr&ATOM_ATTR_3BP)||
834 !(btom->attr&ATOM_ATTR_3BP))
837 link_cell_neighbour_index(moldyn,
838 (btom->r.x+moldyn->dim.x/2)/lc->x,
839 (btom->r.y+moldyn->dim.y/2)/lc->y,
840 (btom->r.z+moldyn->dim.z/2)/lc->z,
843 for(k=0;k<lc->countn;k++) {
845 thisk=&(neighbourk[k]);
848 if(thisk->start==NULL)
851 bck=(k<lc->dnlc)?0:1;
855 ktom=thisk->current->data;
857 if(!(ktom->attr&ATOM_ATTR_3BP))
866 moldyn->pf_func3b(moldyn,&(atom[i]),btom,ktom,bck);
868 } while(list_next(thisk)!=\
871 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
880 * periodic boundayr checking
883 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
891 if(moldyn->MOLDYN_ATTR_PBX)
892 if(a->x>=x) a->x-=dim->x;
893 else if(-a->x>x) a->x+=dim->x;
894 if(moldyn->MOLDYN_ATTR_PBY)
895 if(a->y>=y) a->y-=dim->y;
896 else if(-a->y>y) a->y+=dim->y;
897 if(moldyn->MOLDYN_ATTR_PBZ)
898 if(a->z>=z) a->z-=dim->z;
899 else if(-a->z>z) a->z+=dim->z;
909 /* harmonic oscillator potential and force */
911 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc)) {
914 t_3dvec force,distance;
918 params=moldyn->pot2b_params;
919 sc=params->spring_constant;
920 equi_dist=params->equilibrium_distance;
922 v3_sub(&distance,&(ai->r),&(aj->r);
924 v3_per_bound(&distance,&(moldyn->dim));
925 if(bc) check_per_bound(moldyn,&distance);
926 d=v3_norm(&distance);
927 if(d<=moldyn->cutoff) {
928 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
929 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
930 v3_scale(&force,&distance,-sc*(1.0-(equi_dist/d)));
931 v3_add(&(ai->f),&(ai->f),&force);
937 /* lennard jones potential & force for one sort of atoms */
939 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
942 t_3dvec force,distance;
944 double eps,sig6,sig12;
946 params=moldyn->pot_params;
947 eps=params->epsilon4;
949 sig12=params->sigma12;
951 v3_sub(&distance,&(ai->r),&(aj->r));
952 if(bc) check_per_bound(moldyn,&distance);
953 d=v3_absolute_square(&distance); /* 1/r^2 */
954 if(d<=moldyn->cutoff_square) {
958 h1=h2*h2; /* 1/r^12 */
959 /* energy is eps*..., but we will add this twice ... */
960 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
967 v3_scale(&force,&distance,d);
968 v3_add(&(ai->f),&(aj->f),&force);
975 * tersoff potential & force for 2 sorts of atoms
978 /* tersoff 1 body part */
979 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
982 t_tersoff_mult_params *params;
983 t_tersoff_exchange *exchange;
986 params=moldyn->pot1b_params;
987 exchange=&(params->exchange);
990 * simple: point constant parameters only depending on atom i to
994 exchange->beta=&(params->beta[num]);
995 exchange->n=&(params->n[num]);
996 exchange->c=&(params->c[num]);
997 exchange->d=&(params->d[num]);
998 exchange->h=&(params->h[num]);
1000 exchange->betan=pow(*(exchange->beta),*(exchange->n));
1001 exchange->c2=params->c[num]*params->c[num];
1002 exchange->d2=params->d[num]*params->d[num];
1003 exchange->c2d2=exchange->c2/exchange->d2;
1008 /* tersoff 2 body part */
1009 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1011 t_tersoff_mult_params *params;
1012 t_tersoff_exchange *exchange;
1015 double A,B,R,S,lambda;
1018 params=moldyn->pot_params;
1020 exchange=&(params->exchange);
1025 * we need: f_c, df_c, f_r, df_r
1027 * therefore we need: R, S, A, lambda
1030 v3_sub(&dist_ij,&(ai->r),&(aj->r));
1032 if(bc) check_per_bound(moldyn,&dist_ij);
1034 /* save for use in 3bp */ /* REALLY ?!?!?! */
1035 exchange->dist_ij=dist_ij;
1042 lambda=params->lambda[num];
1043 /* more constants depending of atoms i and j, needed in 3bp */
1044 params->exchange.B=&(params->B[num]);
1045 params->exchange.mu=params->mu[num];
1046 params->exchange.chi=1.0;
1052 lambda=params->lambda_m;
1053 /* more constants depending of atoms i and j, needed in 3bp */
1054 params->exchange.B=&(params->Bmixed);
1055 params->exchange.mu=&(params->mu_m);
1056 params->exchange.chi=params->chi;
1059 d_ij=v3_norm(&dist_ij);
1061 /* save for use in 3bp */
1062 exchange->d_ij=d_ij;
1067 f_r=A*exp(-lamda*d_ij);
1068 df_r=-lambda*f_r/d_ij;
1070 /* f_a, df_a calc + save for 3bp use */
1071 exchange->f_a=-B*exp(-mu*d_ij);
1072 exchange->df_a=-mu*exchange->f_a/d_ij;
1075 /* f_c = 1, df_c = 0 */
1078 v3_scale(&force,&dist_ij,df_r);
1082 arg=PI*(d_ij-R)/s_r;
1083 f_c=0.5+0.5*cos(arg);
1084 df_c=-0.5*sin(arg)*(PI/(s_r*d_ij));
1085 scale=df_c*f_r+df_r*f_c;
1086 v3_scale(&force,&dist_ij,scale);
1090 v3_add(&(ai->f),&(ai->f),&force);
1091 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1092 moldyn->energy+=(0.25*f_r*f_c);
1094 /* save for use in 3bp */
1096 exchange->df_c=df_c;
1098 /* enable the run of 3bp function */
1104 /* tersoff 3 body part */
1106 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1108 t_tersoff_mult_params *params;
1109 t_tersoff_exchange *exchange;
1110 t_3dvec dist_ij,dist_ik,dist_jk;
1113 double d_ij,d_ik,d_jk;
1114 double f_c,df_c,b_ij,f_a,df_a;
1115 double n,c,d,h,neta,betan,betan_1;
1116 double theta,cos_theta,sin_theta;
1119 params=moldyn->pot_params;
1121 exchange=params->exchange;
1123 if(!(exchange->run3bp))
1127 * we need: f_c, d_fc, b_ij, db_ij, f_a, df_a
1129 * we got f_c, df_c, f_a, df_a from 2bp calculation
1132 d_ij=exchange->d_ij;
1133 d_ij2=exchange->d_ij2;
1135 f_a=params->exchange.f_a;
1136 df_a=params->exchange.df_a;
1138 /* d_ij is <= S, as we didn't return so far! */
1141 * calc of b_ij (scalar) and db_ij (vector)
1143 * - for b_ij: chi, beta, f_c_ik, w(=1), c, d, h, n, cos_theta
1145 * - for db_ij: d_theta, sin_theta, cos_theta, f_c_ik, df_c_ik,
1151 v3_sub(&dist_ik,&(aj->i),&(ak->r));
1152 if(bc) check_per_bound(moldyn,&dist_ik);
1153 d_ik=v3_norm(&dist_ik);
1155 /* constants for f_c_ik calc */
1165 /* calc of f_c_ik */
1170 /* f_c_ik = 1, df_c_ik = 0 */
1176 arg=PI*(d_ik-R)/s_r;
1177 f_c_ik=0.5+0.5*cos(arg);
1178 df_c_ik=-0.5*sin(arg)*(PI/(s_r*d_ik));
1181 v3_sub(&dist_jk,&(aj->r),&(ak->r));
1182 if(bc) check_per_bound(moldyn,&dist_jk);
1183 d_jk=v3_norm(&dist_jk);
1185 beta=*(exchange->beta);
1186 betan=exchange->betan;
1193 c2d2=exchange->c2d2;
1195 numer=d_ij2+d_ik*d_ik-d_jk*d_jk;
1197 cos_theta=numer/denom;
1198 sin_theta=sqrt(1.0-(cos_theta*cos_theta));
1199 theta=arccos(cos_theta);
1200 d_theta=(-1.0/sqrt(1.0-cos_theta*cos_theta))/(denom*denom);
1201 d_theta1=2*denom-numer*2*d_ik/d_ij;
1202 d_theta2=2*denom-numer*2*d_ij/d_ik;
1206 h_cos=(h-cos_theta);
1208 d2_h_cos2=d2-h_cos2;
1210 /* some usefull expressions */
1211 frac1=c2/(d2-h_cos2);
1212 bracket1=1+c2d2-frac1;
1213 bracket2=f_c_ik*bracket1;
1214 bracket2_n_1=pow(bracket2,n-1.0);
1215 bracket2_n=bracket2_n_1*bracket2;
1216 bracket3=1+betan*bracket2_n;
1217 bracket3_pow_1=pow(bracket3,(-1.0/(2.0*n))-1.0);
1218 bracket3_pow=bracket3_pow_1*bracket3;
1220 /* now go on with calc of b_ij and derivation of b_ij */
1221 b_ij=chi*bracket3_pow;
1223 /* derivation of theta */
1224 v3_scale(&force,&dist_ij,d1_theta);
1225 v3_scale(&temp,&dist_ik,d_theta2);
1226 v3_add(&force,&force,&temp);
1228 /* part 1 of derivation of b_ij */
1229 v3_scale(&force,sin_theta*2*h_cos*f_c_ik*frac1);
1231 /* part 2 of derivation of b_ij */
1232 v3_scale(&temp,&dist_ik,df_c_ik*bracket1);
1234 /* sum up and scale ... */
1235 v3_add(&temp,&temp,&force);
1236 scale=bracket2_n_1*n*betan*(1+betan*bracket3_pow_1)*chi*(1.0/(2.0*n));
1237 v3_scale(&temp,&temp,scale);
1239 /* now construct an energy and a force out of that */
1240 v3_scale(&temp,&temp,f_a);
1241 v3_scale(&force,&dist_ij,df_a*b_ij);
1242 v3_add(&temp,&temp,&force);
1243 v3_scale(&temp,&temp,f_c);
1244 v3_scale(&force,&dist_ij,df_c*b_ij*f_a);
1245 v3_add(&force,&force,&temp);
1248 v3_add(&(ai->f),&(ai->f),&force);
1249 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1250 moldyn->energy+=(0.25*f_a*b_ij*f_c);