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_nn_dist(t_moldyn *moldyn,double dist) {
103 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
106 moldyn->status|=MOLDYN_STAT_PBX;
109 moldyn->status|=MOLDYN_STAT_PBY;
112 moldyn->status|=MOLDYN_STAT_PBZ;
117 int set_potential1b(t_moldyn *moldyn,pf_func1b func,void *params) {
120 moldyn->pot1b_params=params;
125 int set_potential2b(t_moldyn *moldyn,pf_func2b func,void *params) {
128 moldyn->pot2b_params=params;
133 int set_potential3b(t_moldyn *moldyn,pf_func3b func,void *params) {
136 moldyn->pot3b_params=params;
141 int moldyn_set_log(t_moldyn *moldyn,u8 type,char *fb,int timer) {
144 case LOG_TOTAL_ENERGY:
145 moldyn->ewrite=timer;
146 moldyn->efd=open(fb,O_WRONLY|O_CREAT|O_TRUNC);
148 perror("[moldyn] efd open");
151 dprintf(moldyn->efd,"# total energy log file\n");
153 case LOG_TOTAL_MOMENTUM:
154 moldyn->mwrite=timer;
155 moldyn->mfd=open(fb,O_WRONLY|O_CREAT|O_TRUNC);
157 perror("[moldyn] mfd open");
160 dprintf(moldyn->efd,"# total momentum log file\n");
163 moldyn->swrite=timer;
164 strncpy(moldyn->sfb,fb,63);
167 moldyn->vwrite=timer;
168 strncpy(moldyn->vfb,fb,63);
169 visual_init(&(moldyn->vis),fb);
172 printf("unknown log mechanism: %02x\n",type);
179 int moldyn_log_shutdown(t_moldyn *moldyn) {
181 printf("[moldyn] log shutdown\n");
182 if(moldyn->efd) close(moldyn->efd);
183 if(moldyn->mfd) close(moldyn->mfd);
184 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
189 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
190 u8 attr,u8 bnum,int a,int b,int c) {
198 if(type==FCC) count*=4;
200 if(type==DIAMOND) count*=8;
202 moldyn->atom=malloc(count*sizeof(t_atom));
203 if(moldyn->atom==NULL) {
204 perror("malloc (atoms)");
212 ret=fcc_init(a,b,c,lc,moldyn->atom,&origin);
215 ret=diamond_init(a,b,c,lc,moldyn->atom,&origin);
218 printf("unknown lattice type (%02x)\n",type);
224 printf("ok, there is something wrong ...\n");
225 printf("calculated -> %d atoms\n",count);
226 printf("created -> %d atoms\n",ret);
231 printf("[moldyn] created lattice with %d atoms\n",count);
235 moldyn->atom[count].element=element;
236 moldyn->atom[count].mass=mass;
237 moldyn->atom[count].attr=attr;
238 moldyn->atom[count].bnum=bnum;
239 check_per_bound(moldyn,&(moldyn->atom[count].r));
246 int add_atom(t_moldyn *moldyn,int element,double mass,u8 bnum,u8 attr,
247 t_3dvec *r,t_3dvec *v) {
254 count=++(moldyn->count);
256 ptr=realloc(atom,count*sizeof(t_atom));
258 perror("[moldyn] realloc (add atom)");
266 atom[count-1].element=element;
267 atom[count-1].mass=mass;
268 atom[count-1].bnum=bnum;
269 atom[count-1].attr=attr;
274 int destroy_atoms(t_moldyn *moldyn) {
276 if(moldyn->atom) free(moldyn->atom);
281 int thermal_init(t_moldyn *moldyn) {
284 * - gaussian distribution of velocities
285 * - zero total momentum
286 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
291 t_3dvec p_total,delta;
296 random=&(moldyn->random);
298 /* gaussian distribution of velocities */
300 for(i=0;i<moldyn->count;i++) {
301 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t/atom[i].mass);
303 v=sigma*rand_get_gauss(random);
305 p_total.x+=atom[i].mass*v;
307 v=sigma*rand_get_gauss(random);
309 p_total.y+=atom[i].mass*v;
311 v=sigma*rand_get_gauss(random);
313 p_total.z+=atom[i].mass*v;
316 /* zero total momentum */
317 v3_scale(&p_total,&p_total,1.0/moldyn->count);
318 for(i=0;i<moldyn->count;i++) {
319 v3_scale(&delta,&p_total,1.0/atom[i].mass);
320 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
323 /* velocity scaling */
324 scale_velocity(moldyn,VSCALE_INIT_EQUI);
329 int scale_velocity(t_moldyn *moldyn,u8 type) {
338 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
342 for(i=0;i<moldyn->count;i++)
343 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
344 scale=(1.5*moldyn->count*K_BOLTZMANN*moldyn->t)/e;
345 if(type&VSCALE_INIT_EQUI) scale*=2.0; /* equipartition theorem */
347 for(i=0;i<moldyn->count;i++)
348 v3_scale(&(atom[i].v),&(atom[i].v),scale);
353 double get_e_kin(t_moldyn *moldyn) {
361 for(i=0;i<moldyn->count;i++)
362 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
367 double get_e_pot(t_moldyn *moldyn) {
369 return moldyn->energy;
372 double update_e_kin(t_moldyn *moldyn) {
374 return(get_e_kin(moldyn));
377 double get_total_energy(t_moldyn *moldyn) {
379 return(moldyn->ekin+moldyn->energy);
382 t_3dvec get_total_p(t_moldyn *moldyn) {
391 for(i=0;i<moldyn->count;i++) {
392 v3_scale(&p,&(atom[i].v),atom[i].mass);
393 v3_add(&p_total,&p_total,&p);
399 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
403 /* nn_dist is the nearest neighbour distance */
406 printf("[moldyn] i do not estimate timesteps below %f K!\n",
407 MOLDYN_CRITICAL_EST_TEMP);
411 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
420 /* linked list / cell method */
422 int link_cell_init(t_moldyn *moldyn) {
428 fd=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("[moldyn] 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]),fd);
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<moldyn->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) {
500 cell[0]=lc->subcell[i+j*nx+k*a];
501 for(ci=-1;ci<=1;ci++) {
508 for(cj=-1;cj<=1;cj++) {
515 for(ck=-1;ck<=1;ck++) {
522 if(!(ci|cj|ck)) continue;
524 cell[--count2]=lc->subcell[x+y*nx+z*a];
527 cell[count1++]=lc->subcell[x+y*nx+z*a];
539 int link_cell_shutdown(t_moldyn *moldyn) {
546 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
547 list_shutdown(&(moldyn->lc.subcell[i]));
552 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
556 t_moldyn_schedule *schedule;
558 schedule=&(moldyn->schedule);
559 count=++(schedule->content_count);
561 ptr=realloc(moldyn->schedule.runs,count*sizeof(int));
563 perror("[moldyn] realloc (runs)");
566 moldyn->schedule.runs=ptr;
567 moldyn->schedule.runs[count-1]=runs;
569 ptr=realloc(schedule->tau,count*sizeof(double));
571 perror("[moldyn] realloc (tau)");
574 moldyn->schedule.tau=ptr;
575 moldyn->schedule.tau[count-1]=tau;
580 int moldyn_set_schedule_hook(t_moldyn *moldyn,void *hook,void *hook_params) {
582 moldyn->schedule.hook=hook;
583 moldyn->schedule.hook_params=hook_params;
590 * 'integration of newtons equation' - algorithms
594 /* start the integration */
596 int moldyn_integrate(t_moldyn *moldyn) {
599 unsigned int e,m,s,v;
601 t_moldyn_schedule *schedule;
607 schedule=&(moldyn->schedule);
610 /* initialize linked cell method */
611 link_cell_init(moldyn);
613 /* logging & visualization */
619 /* sqaure of some variables */
620 moldyn->tau_square=moldyn->tau*moldyn->tau;
621 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
622 /* calculate initial forces */
623 potential_force_calc(moldyn);
625 /* do some checks before we actually start calculating bullshit */
626 if(moldyn->cutoff>0.5*moldyn->dim.x)
627 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
628 if(moldyn->cutoff>0.5*moldyn->dim.y)
629 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
630 if(moldyn->cutoff>0.5*moldyn->dim.z)
631 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
632 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
633 if(ds>0.05*moldyn->nnd)
634 printf("[moldyn] warning: forces too high / tau too small!\n");
636 /* zero absolute time */
639 for(sched=0;sched<moldyn->schedule.content_count;sched++) {
641 /* setting amount of runs and finite time step size */
642 moldyn->tau=schedule->tau[sched];
643 moldyn->tau_square=moldyn->tau*moldyn->tau;
644 moldyn->time_steps=schedule->runs[sched];
646 /* integration according to schedule */
648 for(i=0;i<moldyn->time_steps;i++) {
650 /* integration step */
651 moldyn->integrate(moldyn);
653 /* increase absolute time */
654 moldyn->time+=moldyn->tau;
656 /* check for log & visualization */
660 "%.15f %.45f %.45f %.45f\n",
661 moldyn->time,update_e_kin(moldyn),
663 get_total_energy(moldyn));
667 p=get_total_p(moldyn);
669 "%.15f %.45f\n",moldyn->time,
675 snprintf(fb,128,"%s-%f-%.15f.save",moldyn->sfb,
676 moldyn->t,i*moldyn->tau);
677 fd=open(fb,O_WRONLY|O_TRUNC|O_CREAT);
678 if(fd<0) perror("[moldyn] save fd open");
680 write(fd,moldyn,sizeof(t_moldyn));
681 write(fd,moldyn->atom,
682 moldyn->count*sizeof(t_atom));
689 visual_atoms(&(moldyn->vis),moldyn->time,
690 moldyn->atom,moldyn->count);
691 printf("\rsched: %d, steps: %d",sched,i);
698 /* check for hooks */
700 schedule->hook(moldyn,schedule->hook_params);
707 /* velocity verlet */
709 int velocity_verlet(t_moldyn *moldyn) {
712 double tau,tau_square;
719 tau_square=moldyn->tau_square;
721 for(i=0;i<count;i++) {
723 v3_scale(&delta,&(atom[i].v),tau);
724 v3_add(&(atom[i].r),&(atom[i].r),&delta);
725 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
726 v3_add(&(atom[i].r),&(atom[i].r),&delta);
727 check_per_bound(moldyn,&(atom[i].r));
730 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
731 v3_add(&(atom[i].v),&(atom[i].v),&delta);
734 /* neighbour list update */
735 link_cell_update(moldyn);
737 /* forces depending on chosen potential */
738 potential_force_calc(moldyn);
739 //moldyn->potential_force_function(moldyn);
741 for(i=0;i<count;i++) {
742 /* again velocities */
743 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
744 v3_add(&(atom[i].v),&(atom[i].v),&delta);
753 * potentials & corresponding forces
757 /* generic potential and force calculation */
759 int potential_force_calc(t_moldyn *moldyn) {
762 t_atom *atom,*btom,*ktom;
764 t_list neighbour[27],neighbourk[27];
776 for(i=0;i<count;i++) {
779 v3_zero(&(atom[i].f));
781 /* single particle potential/force */
782 if(atom[i].attr&ATOM_ATTR_1BP)
783 moldyn->func1b(moldyn,&(atom[i]));
785 /* 2 body pair potential/force */
786 if(atom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)) {
788 link_cell_neighbour_index(moldyn,
789 (atom[i].r.x+moldyn->dim.x/2)/lc->x,
790 (atom[i].r.y+moldyn->dim.y/2)/lc->y,
791 (atom[i].r.z+moldyn->dim.z/2)/lc->z,
797 for(j=0;j<countn;j++) {
799 this=&(neighbour[j]);
802 if(this->start==NULL)
808 btom=this->current->data;
813 if((btom->attr&ATOM_ATTR_2BP)&
814 (atom[i].attr&ATOM_ATTR_2BP))
815 moldyn->func2b(moldyn,
820 /* 3 body potential/force */
822 if(!(atom[i].attr&ATOM_ATTR_3BP)||
823 !(btom->attr&ATOM_ATTR_3BP))
826 link_cell_neighbour_index(moldyn,
827 (btom->r.x+moldyn->dim.x/2)/lc->x,
828 (btom->r.y+moldyn->dim.y/2)/lc->y,
829 (btom->r.z+moldyn->dim.z/2)/lc->z,
832 for(k=0;k<lc->countn;k++) {
834 thisk=&(neighbourk[k]);
837 if(thisk->start==NULL)
840 bck=(k<lc->dnlc)?0:1;
844 ktom=thisk->current->data;
846 if(!(ktom->attr&ATOM_ATTR_3BP))
855 moldyn->func3b(moldyn,&(atom[i]),btom,ktom,bck);
857 } while(list_next(thisk)!=\
862 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
871 * periodic boundayr checking
874 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
885 if(moldyn->status&MOLDYN_STAT_PBX) {
886 if(a->x>=x) a->x-=dim->x;
887 else if(-a->x>x) a->x+=dim->x;
889 if(moldyn->status&MOLDYN_STAT_PBY) {
890 if(a->y>=y) a->y-=dim->y;
891 else if(-a->y>y) a->y+=dim->y;
893 if(moldyn->status&MOLDYN_STAT_PBZ) {
894 if(a->z>=z) a->z-=dim->z;
895 else if(-a->z>z) a->z+=dim->z;
906 /* harmonic oscillator potential and force */
908 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
911 t_3dvec force,distance;
915 params=moldyn->pot2b_params;
916 sc=params->spring_constant;
917 equi_dist=params->equilibrium_distance;
919 v3_sub(&distance,&(ai->r),&(aj->r));
921 if(bc) check_per_bound(moldyn,&distance);
922 d=v3_norm(&distance);
923 if(d<=moldyn->cutoff) {
924 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
925 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
926 v3_scale(&force,&distance,-sc*(1.0-(equi_dist/d)));
927 v3_add(&(ai->f),&(ai->f),&force);
933 /* lennard jones potential & force for one sort of atoms */
935 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
938 t_3dvec force,distance;
940 double eps,sig6,sig12;
942 params=moldyn->pot2b_params;
943 eps=params->epsilon4;
945 sig12=params->sigma12;
947 v3_sub(&distance,&(ai->r),&(aj->r));
948 if(bc) check_per_bound(moldyn,&distance);
949 d=v3_absolute_square(&distance); /* 1/r^2 */
950 if(d<=moldyn->cutoff_square) {
954 h1=h2*h2; /* 1/r^12 */
955 /* energy is eps*..., but we will add this twice ... */
956 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
963 v3_scale(&force,&distance,d);
964 v3_add(&(ai->f),&(ai->f),&force);
971 * tersoff potential & force for 2 sorts of atoms
974 /* create mixed terms from parameters and set them */
975 int tersoff_mult_complete_params(t_tersoff_mult_params *p) {
977 printf("[moldyn] tersoff parameter completion\n");
978 p->Smixed=sqrt(p->S[0]*p->S[1]);
979 p->Rmixed=sqrt(p->R[0]*p->R[1]);
980 p->Amixed=sqrt(p->A[0]*p->A[1]);
981 p->Bmixed=sqrt(p->B[0]*p->B[1]);
982 p->lambda_m=0.5*(p->lambda[0]+p->lambda[1]);
983 p->mu_m=0.5*(p->mu[0]+p->mu[1]);
988 /* tersoff 1 body part */
989 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
992 t_tersoff_mult_params *params;
993 t_tersoff_exchange *exchange;
996 params=moldyn->pot1b_params;
997 exchange=&(params->exchange);
1000 * simple: point constant parameters only depending on atom i to
1001 * their right values
1004 exchange->beta=&(params->beta[num]);
1005 exchange->n=&(params->n[num]);
1006 exchange->c=&(params->c[num]);
1007 exchange->d=&(params->d[num]);
1008 exchange->h=&(params->h[num]);
1010 exchange->betan=pow(*(exchange->beta),*(exchange->n));
1011 exchange->c2=params->c[num]*params->c[num];
1012 exchange->d2=params->d[num]*params->d[num];
1013 exchange->c2d2=exchange->c2/exchange->d2;
1018 /* tersoff 2 body part */
1019 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1021 t_tersoff_mult_params *params;
1022 t_tersoff_exchange *exchange;
1023 t_3dvec dist_ij,force;
1025 double A,B,R,S,lambda,mu;
1033 params=moldyn->pot2b_params;
1035 exchange=&(params->exchange);
1040 * we need: f_c, df_c, f_r, df_r
1042 * therefore we need: R, S, A, lambda
1045 v3_sub(&dist_ij,&(ai->r),&(aj->r));
1047 if(bc) check_per_bound(moldyn,&dist_ij);
1049 /* save for use in 3bp */ /* REALLY ?!?!?! */
1050 exchange->dist_ij=dist_ij;
1057 lambda=params->lambda[num];
1058 /* more constants depending of atoms i and j, needed in 3bp */
1059 params->exchange.B=&(params->B[num]);
1060 params->exchange.mu=&(params->mu[num]);
1062 params->exchange.chi=1.0;
1068 lambda=params->lambda_m;
1069 /* more constants depending of atoms i and j, needed in 3bp */
1070 params->exchange.B=&(params->Bmixed);
1071 params->exchange.mu=&(params->mu_m);
1073 params->exchange.chi=params->chi;
1076 d_ij=v3_norm(&dist_ij);
1078 /* save for use in 3bp */
1079 exchange->d_ij=d_ij;
1084 f_r=A*exp(-lambda*d_ij);
1085 df_r=-lambda*f_r/d_ij;
1087 /* f_a, df_a calc + save for 3bp use */
1088 exchange->f_a=-B*exp(-mu*d_ij);
1089 exchange->df_a=-mu*exchange->f_a/d_ij;
1092 /* f_c = 1, df_c = 0 */
1095 v3_scale(&force,&dist_ij,df_r);
1099 arg=M_PI*(d_ij-R)/s_r;
1100 f_c=0.5+0.5*cos(arg);
1101 df_c=-0.5*sin(arg)*(M_PI/(s_r*d_ij));
1102 scale=df_c*f_r+df_r*f_c;
1103 v3_scale(&force,&dist_ij,scale);
1107 v3_add(&(ai->f),&(ai->f),&force);
1108 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1109 moldyn->energy+=(0.25*f_r*f_c);
1111 /* save for use in 3bp */
1113 exchange->df_c=df_c;
1115 /* enable the run of 3bp function */
1121 /* tersoff 3 body part */
1123 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1125 t_tersoff_mult_params *params;
1126 t_tersoff_exchange *exchange;
1127 t_3dvec dist_ij,dist_ik,dist_jk;
1130 double d_ij,d_ij2,d_ik,d_jk;
1131 double f_c,df_c,b_ij,f_a,df_a;
1132 double f_c_ik,df_c_ik,arg;
1135 double n,c,d,h,beta,betan;
1138 double theta,cos_theta,sin_theta;
1139 double d_theta,d_theta1,d_theta2;
1140 double h_cos,h_cos2,d2_h_cos2;
1141 double frac1,bracket1,bracket2,bracket2_n_1,bracket2_n;
1142 double bracket3,bracket3_pow_1,bracket3_pow;
1145 params=moldyn->pot3b_params;
1147 exchange=&(params->exchange);
1149 if(!(exchange->run3bp))
1153 * we need: f_c, d_fc, b_ij, db_ij, f_a, df_a
1155 * we got f_c, df_c, f_a, df_a from 2bp calculation
1158 d_ij=exchange->d_ij;
1159 d_ij2=exchange->d_ij2;
1161 f_a=params->exchange.f_a;
1162 df_a=params->exchange.df_a;
1164 /* d_ij is <= S, as we didn't return so far! */
1167 * calc of b_ij (scalar) and db_ij (vector)
1169 * - for b_ij: chi, beta, f_c_ik, w(=1), c, d, h, n, cos_theta
1171 * - for db_ij: d_theta, sin_theta, cos_theta, f_c_ik, df_c_ik,
1177 v3_sub(&dist_ik,&(ai->r),&(ak->r));
1178 if(bc) check_per_bound(moldyn,&dist_ik);
1179 d_ik=v3_norm(&dist_ik);
1181 /* constants for f_c_ik calc */
1191 /* calc of f_c_ik */
1196 /* f_c_ik = 1, df_c_ik = 0 */
1202 arg=M_PI*(d_ik-R)/s_r;
1203 f_c_ik=0.5+0.5*cos(arg);
1204 df_c_ik=-0.5*sin(arg)*(M_PI/(s_r*d_ik));
1207 v3_sub(&dist_jk,&(aj->r),&(ak->r));
1208 if(bc) check_per_bound(moldyn,&dist_jk);
1209 d_jk=v3_norm(&dist_jk);
1211 beta=*(exchange->beta);
1212 betan=exchange->betan;
1219 c2d2=exchange->c2d2;
1221 numer=d_ij2+d_ik*d_ik-d_jk*d_jk;
1223 cos_theta=numer/denom;
1224 sin_theta=sqrt(1.0-(cos_theta*cos_theta));
1225 theta=acos(cos_theta);
1226 d_theta=(-1.0/sqrt(1.0-cos_theta*cos_theta))/(denom*denom);
1227 d_theta1=2*denom-numer*2*d_ik/d_ij;
1228 d_theta2=2*denom-numer*2*d_ij/d_ik;
1232 h_cos=(h-cos_theta);
1234 d2_h_cos2=d2-h_cos2;
1236 /* some usefull expressions */
1237 frac1=c2/(d2-h_cos2);
1238 bracket1=1+c2d2-frac1;
1239 bracket2=f_c_ik*bracket1;
1240 bracket2_n_1=pow(bracket2,n-1.0);
1241 bracket2_n=bracket2_n_1*bracket2;
1242 bracket3=1+betan*bracket2_n;
1243 bracket3_pow_1=pow(bracket3,(-1.0/(2.0*n))-1.0);
1244 bracket3_pow=bracket3_pow_1*bracket3;
1246 /* now go on with calc of b_ij and derivation of b_ij */
1247 b_ij=chi*bracket3_pow;
1249 /* derivation of theta */
1250 v3_scale(&force,&dist_ij,d_theta1);
1251 v3_scale(&temp,&dist_ik,d_theta2);
1252 v3_add(&force,&force,&temp);
1254 /* part 1 of derivation of b_ij */
1255 v3_scale(&force,&force,sin_theta*2*h_cos*f_c_ik*frac1);
1257 /* part 2 of derivation of b_ij */
1258 v3_scale(&temp,&dist_ik,df_c_ik*bracket1);
1260 /* sum up and scale ... */
1261 v3_add(&temp,&temp,&force);
1262 scale=bracket2_n_1*n*betan*(1+betan*bracket3_pow_1)*chi*(1.0/(2.0*n));
1263 v3_scale(&temp,&temp,scale);
1265 /* now construct an energy and a force out of that */
1266 v3_scale(&temp,&temp,f_a);
1267 v3_scale(&force,&dist_ij,df_a*b_ij);
1268 v3_add(&temp,&temp,&force);
1269 v3_scale(&temp,&temp,f_c);
1270 v3_scale(&force,&dist_ij,df_c*b_ij*f_a);
1271 v3_add(&force,&force,&temp);
1274 v3_add(&(ai->f),&(ai->f),&force);
1275 /* energy is 0.5 f_r f_c, but we will sum it up twice ... */
1276 moldyn->energy+=(0.25*f_a*b_ij*f_c);