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_ref) {
81 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
83 moldyn->pt_scale=(ptype|ttype);
90 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
105 int set_nn_dist(t_moldyn *moldyn,double dist) {
112 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
115 moldyn->status|=MOLDYN_STAT_PBX;
118 moldyn->status|=MOLDYN_STAT_PBY;
121 moldyn->status|=MOLDYN_STAT_PBZ;
126 int set_potential1b(t_moldyn *moldyn,pf_func1b func,void *params) {
129 moldyn->pot1b_params=params;
134 int set_potential2b(t_moldyn *moldyn,pf_func2b func,void *params) {
137 moldyn->pot2b_params=params;
142 int set_potential2b_post(t_moldyn *moldyn,pf_func2b_post func,void *params) {
144 moldyn->func2b_post=func;
145 moldyn->pot2b_params=params;
150 int set_potential3b(t_moldyn *moldyn,pf_func3b func,void *params) {
153 moldyn->pot3b_params=params;
158 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
160 strncpy(moldyn->vlsdir,dir,127);
165 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
171 case LOG_TOTAL_ENERGY:
172 moldyn->ewrite=timer;
173 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
174 moldyn->efd=open(filename,
175 O_WRONLY|O_CREAT|O_EXCL,
178 perror("[moldyn] energy log fd open");
181 dprintf(moldyn->efd,"# total energy log file\n");
183 case LOG_TOTAL_MOMENTUM:
184 moldyn->mwrite=timer;
185 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
186 moldyn->mfd=open(filename,
187 O_WRONLY|O_CREAT|O_EXCL,
190 perror("[moldyn] momentum log fd open");
193 dprintf(moldyn->efd,"# total momentum log file\n");
196 moldyn->swrite=timer;
199 moldyn->vwrite=timer;
200 ret=visual_init(&(moldyn->vis),moldyn->vlsdir);
202 printf("[moldyn] visual init failure\n");
207 printf("[moldyn] unknown log mechanism: %02x\n",type);
214 int moldyn_log_shutdown(t_moldyn *moldyn) {
216 printf("[moldyn] log shutdown\n");
217 if(moldyn->efd) close(moldyn->efd);
218 if(moldyn->mfd) close(moldyn->mfd);
219 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
224 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
225 u8 attr,u8 bnum,int a,int b,int c) {
233 if(type==FCC) count*=4;
235 if(type==DIAMOND) count*=8;
237 moldyn->atom=malloc(count*sizeof(t_atom));
238 if(moldyn->atom==NULL) {
239 perror("malloc (atoms)");
247 ret=fcc_init(a,b,c,lc,moldyn->atom,&origin);
250 ret=diamond_init(a,b,c,lc,moldyn->atom,&origin);
253 printf("unknown lattice type (%02x)\n",type);
259 printf("ok, there is something wrong ...\n");
260 printf("calculated -> %d atoms\n",count);
261 printf("created -> %d atoms\n",ret);
266 printf("[moldyn] created lattice with %d atoms\n",count);
270 moldyn->atom[count].element=element;
271 moldyn->atom[count].mass=mass;
272 moldyn->atom[count].attr=attr;
273 moldyn->atom[count].bnum=bnum;
274 check_per_bound(moldyn,&(moldyn->atom[count].r));
281 int add_atom(t_moldyn *moldyn,int element,double mass,u8 bnum,u8 attr,
282 t_3dvec *r,t_3dvec *v) {
289 count=++(moldyn->count);
291 ptr=realloc(atom,count*sizeof(t_atom));
293 perror("[moldyn] realloc (add atom)");
301 atom[count-1].element=element;
302 atom[count-1].mass=mass;
303 atom[count-1].bnum=bnum;
304 atom[count-1].attr=attr;
309 int destroy_atoms(t_moldyn *moldyn) {
311 if(moldyn->atom) free(moldyn->atom);
316 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
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_ref/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,equi_init);
364 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
374 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
377 /* get kinetic energy / temperature & count involved atoms */
380 for(i=0;i<moldyn->count;i++) {
381 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
382 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
386 if(count!=0) moldyn->t=(2.0*e)/(3.0*count*K_BOLTZMANN);
387 else return 0; /* no atoms involved in scaling! */
389 /* (temporary) hack for e,t = 0 */
392 if(moldyn->t_ref!=0.0) {
393 thermal_init(moldyn,equi_init);
397 return 0; /* no scaling needed */
401 /* get scaling factor */
402 scale=moldyn->t_ref/moldyn->t;
406 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
407 scale=1.0+(scale-1.0)/moldyn->t_tc;
410 /* velocity scaling */
411 for(i=0;i<moldyn->count;i++) {
412 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
413 v3_scale(&(atom[i].v),&(atom[i].v),scale);
419 double get_e_kin(t_moldyn *moldyn) {
427 for(i=0;i<moldyn->count;i++)
428 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
433 double get_e_pot(t_moldyn *moldyn) {
435 return moldyn->energy;
438 double update_e_kin(t_moldyn *moldyn) {
440 return(get_e_kin(moldyn));
443 double get_total_energy(t_moldyn *moldyn) {
445 return(moldyn->ekin+moldyn->energy);
448 t_3dvec get_total_p(t_moldyn *moldyn) {
457 for(i=0;i<moldyn->count;i++) {
458 v3_scale(&p,&(atom[i].v),atom[i].mass);
459 v3_add(&p_total,&p_total,&p);
465 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
469 /* nn_dist is the nearest neighbour distance */
471 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
480 /* linked list / cell method */
482 int link_cell_init(t_moldyn *moldyn) {
488 fd=open("/dev/null",O_WRONLY);
492 /* partitioning the md cell */
493 lc->nx=moldyn->dim.x/moldyn->cutoff;
494 lc->x=moldyn->dim.x/lc->nx;
495 lc->ny=moldyn->dim.y/moldyn->cutoff;
496 lc->y=moldyn->dim.y/lc->ny;
497 lc->nz=moldyn->dim.z/moldyn->cutoff;
498 lc->z=moldyn->dim.z/lc->nz;
500 lc->cells=lc->nx*lc->ny*lc->nz;
501 lc->subcell=malloc(lc->cells*sizeof(t_list));
503 printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
505 for(i=0;i<lc->cells;i++)
506 //list_init(&(lc->subcell[i]),1);
507 list_init(&(lc->subcell[i]),fd);
509 link_cell_update(moldyn);
514 int link_cell_update(t_moldyn *moldyn) {
528 for(i=0;i<lc->cells;i++)
529 list_destroy(&(moldyn->lc.subcell[i]));
531 for(count=0;count<moldyn->count;count++) {
532 i=(atom[count].r.x+(moldyn->dim.x/2))/lc->x;
533 j=(atom[count].r.y+(moldyn->dim.y/2))/lc->y;
534 k=(atom[count].r.z+(moldyn->dim.z/2))/lc->z;
535 list_add_immediate_ptr(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
542 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
560 cell[0]=lc->subcell[i+j*nx+k*a];
561 for(ci=-1;ci<=1;ci++) {
568 for(cj=-1;cj<=1;cj++) {
575 for(ck=-1;ck<=1;ck++) {
582 if(!(ci|cj|ck)) continue;
584 cell[--count2]=lc->subcell[x+y*nx+z*a];
587 cell[count1++]=lc->subcell[x+y*nx+z*a];
598 int link_cell_shutdown(t_moldyn *moldyn) {
605 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
606 list_shutdown(&(moldyn->lc.subcell[i]));
611 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
615 t_moldyn_schedule *schedule;
617 schedule=&(moldyn->schedule);
618 count=++(schedule->content_count);
620 ptr=realloc(moldyn->schedule.runs,count*sizeof(int));
622 perror("[moldyn] realloc (runs)");
625 moldyn->schedule.runs=ptr;
626 moldyn->schedule.runs[count-1]=runs;
628 ptr=realloc(schedule->tau,count*sizeof(double));
630 perror("[moldyn] realloc (tau)");
633 moldyn->schedule.tau=ptr;
634 moldyn->schedule.tau[count-1]=tau;
639 int moldyn_set_schedule_hook(t_moldyn *moldyn,void *hook,void *hook_params) {
641 moldyn->schedule.hook=hook;
642 moldyn->schedule.hook_params=hook_params;
649 * 'integration of newtons equation' - algorithms
653 /* start the integration */
655 int moldyn_integrate(t_moldyn *moldyn) {
658 unsigned int e,m,s,v;
660 t_moldyn_schedule *schedule;
666 schedule=&(moldyn->schedule);
669 /* initialize linked cell method */
670 link_cell_init(moldyn);
672 /* logging & visualization */
678 /* sqaure of some variables */
679 moldyn->tau_square=moldyn->tau*moldyn->tau;
680 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
682 /* calculate initial forces */
683 potential_force_calc(moldyn);
685 /* some stupid checks before we actually start calculating bullshit */
686 if(moldyn->cutoff>0.5*moldyn->dim.x)
687 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
688 if(moldyn->cutoff>0.5*moldyn->dim.y)
689 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
690 if(moldyn->cutoff>0.5*moldyn->dim.z)
691 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
692 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
693 if(ds>0.05*moldyn->nnd)
694 printf("[moldyn] warning: forces too high / tau too small!\n");
696 /* zero absolute time */
699 /* debugging, ignore */
702 /* executing the schedule */
703 for(sched=0;sched<moldyn->schedule.content_count;sched++) {
705 /* setting amount of runs and finite time step size */
706 moldyn->tau=schedule->tau[sched];
707 moldyn->tau_square=moldyn->tau*moldyn->tau;
708 moldyn->time_steps=schedule->runs[sched];
710 /* integration according to schedule */
712 for(i=0;i<moldyn->time_steps;i++) {
714 /* integration step */
715 moldyn->integrate(moldyn);
718 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
719 scale_velocity(moldyn,FALSE);
721 /* check for log & visualization */
726 moldyn->time,update_e_kin(moldyn),
728 get_total_energy(moldyn));
732 p=get_total_p(moldyn);
734 "%f %f\n",moldyn->time,v3_norm(&p));
739 snprintf(dir,128,"%s/s-%07.f.save",
740 moldyn->vlsdir,moldyn->time);
741 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT);
742 if(fd<0) perror("[moldyn] save fd open");
744 write(fd,moldyn,sizeof(t_moldyn));
745 write(fd,moldyn->atom,
746 moldyn->count*sizeof(t_atom));
753 visual_atoms(&(moldyn->vis),moldyn->time,
754 moldyn->atom,moldyn->count);
755 printf("\rsched: %d, steps: %d, debug: %d",
756 sched,i,moldyn->debug);
761 /* increase absolute time */
762 moldyn->time+=moldyn->tau;
766 /* check for hooks */
768 schedule->hook(moldyn,schedule->hook_params);
770 /* get a new info line */
778 /* velocity verlet */
780 int velocity_verlet(t_moldyn *moldyn) {
783 double tau,tau_square;
790 tau_square=moldyn->tau_square;
792 for(i=0;i<count;i++) {
794 v3_scale(&delta,&(atom[i].v),tau);
795 v3_add(&(atom[i].r),&(atom[i].r),&delta);
796 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
797 v3_add(&(atom[i].r),&(atom[i].r),&delta);
798 check_per_bound(moldyn,&(atom[i].r));
801 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
802 v3_add(&(atom[i].v),&(atom[i].v),&delta);
805 /* neighbour list update */
806 link_cell_update(moldyn);
808 /* forces depending on chosen potential */
809 potential_force_calc(moldyn);
811 for(i=0;i<count;i++) {
812 /* again velocities */
813 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
814 v3_add(&(atom[i].v),&(atom[i].v),&delta);
823 * potentials & corresponding forces
827 /* generic potential and force calculation */
829 int potential_force_calc(t_moldyn *moldyn) {
832 t_atom *itom,*jtom,*ktom;
834 t_list neighbour_i[27];
835 t_list neighbour_i2[27];
836 //t_list neighbour_j[27];
848 /* get energy and force of every atom */
849 for(i=0;i<count;i++) {
852 v3_zero(&(itom[i].f));
854 /* single particle potential/force */
855 if(itom[i].attr&ATOM_ATTR_1BP)
856 moldyn->func1b(moldyn,&(itom[i]));
858 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
861 /* 2 body pair potential/force */
863 link_cell_neighbour_index(moldyn,
864 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
865 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
866 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
873 this=&(neighbour_i[j]);
876 if(this->start==NULL)
882 jtom=this->current->data;
887 if((jtom->attr&ATOM_ATTR_2BP)&
888 (itom[i].attr&ATOM_ATTR_2BP))
889 moldyn->func2b(moldyn,
894 /* 3 body potential/force */
896 if(!(itom[i].attr&ATOM_ATTR_3BP)||
897 !(jtom->attr&ATOM_ATTR_3BP))
900 /* copy the neighbour lists */
901 memcpy(neighbour_i2,neighbour_i,
904 /* get neighbours of i */
907 that=&(neighbour_i2[k]);
910 if(that->start==NULL)
917 ktom=that->current->data;
919 if(!(ktom->attr&ATOM_ATTR_3BP))
928 moldyn->func3b(moldyn,
934 } while(list_next(that)!=\
939 /* 2bp post function */
940 if(moldyn->func2b_post) {
941 moldyn->func2b_post(moldyn,
946 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
956 * periodic boundayr checking
959 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
970 if(moldyn->status&MOLDYN_STAT_PBX) {
971 if(a->x>=x) a->x-=dim->x;
972 else if(-a->x>x) a->x+=dim->x;
974 if(moldyn->status&MOLDYN_STAT_PBY) {
975 if(a->y>=y) a->y-=dim->y;
976 else if(-a->y>y) a->y+=dim->y;
978 if(moldyn->status&MOLDYN_STAT_PBZ) {
979 if(a->z>=z) a->z-=dim->z;
980 else if(-a->z>z) a->z+=dim->z;
991 /* harmonic oscillator potential and force */
993 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
996 t_3dvec force,distance;
1000 params=moldyn->pot2b_params;
1001 sc=params->spring_constant;
1002 equi_dist=params->equilibrium_distance;
1004 v3_sub(&distance,&(aj->r),&(ai->r));
1006 if(bc) check_per_bound(moldyn,&distance);
1007 d=v3_norm(&distance);
1008 if(d<=moldyn->cutoff) {
1009 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
1010 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
1011 /* f = -grad E; grad r_ij = -1 1/r_ij distance */
1012 v3_scale(&force,&distance,sc*(1.0-(equi_dist/d)));
1013 v3_add(&(ai->f),&(ai->f),&force);
1019 /* lennard jones potential & force for one sort of atoms */
1021 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1023 t_lj_params *params;
1024 t_3dvec force,distance;
1026 double eps,sig6,sig12;
1028 params=moldyn->pot2b_params;
1029 eps=params->epsilon4;
1030 sig6=params->sigma6;
1031 sig12=params->sigma12;
1033 v3_sub(&distance,&(aj->r),&(ai->r));
1034 if(bc) check_per_bound(moldyn,&distance);
1035 d=v3_absolute_square(&distance); /* 1/r^2 */
1036 if(d<=moldyn->cutoff_square) {
1037 d=1.0/d; /* 1/r^2 */
1040 h1=h2*h2; /* 1/r^12 */
1041 /* energy is eps*..., but we will add this twice ... */
1042 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
1049 v3_scale(&force,&distance,-1.0*d); /* f = - grad E */
1050 v3_add(&(ai->f),&(ai->f),&force);
1057 * tersoff potential & force for 2 sorts of atoms
1060 /* create mixed terms from parameters and set them */
1061 int tersoff_mult_complete_params(t_tersoff_mult_params *p) {
1063 printf("[moldyn] tersoff parameter completion\n");
1064 p->Smixed=sqrt(p->S[0]*p->S[1]);
1065 p->Rmixed=sqrt(p->R[0]*p->R[1]);
1066 p->Amixed=sqrt(p->A[0]*p->A[1]);
1067 p->Bmixed=sqrt(p->B[0]*p->B[1]);
1068 p->lambda_m=0.5*(p->lambda[0]+p->lambda[1]);
1069 p->mu_m=0.5*(p->mu[0]+p->mu[1]);
1071 printf("[moldyn] tersoff mult parameter info:\n");
1072 printf(" S (A) | %f | %f | %f\n",p->S[0],p->S[1],p->Smixed);
1073 printf(" R (A) | %f | %f | %f\n",p->R[0],p->R[1],p->Rmixed);
1074 printf(" A (eV) | %f | %f | %f\n",p->A[0]/EV,p->A[1]/EV,p->Amixed/EV);
1075 printf(" B (eV) | %f | %f | %f\n",p->B[0]/EV,p->B[1]/EV,p->Bmixed/EV);
1076 printf(" lambda | %f | %f | %f\n",p->lambda[0],p->lambda[1],
1078 printf(" mu | %f | %f | %f\n",p->mu[0],p->mu[1],p->mu_m);
1079 printf(" beta | %.10f | %.10f\n",p->beta[0],p->beta[1]);
1080 printf(" n | %f | %f\n",p->n[0],p->n[1]);
1081 printf(" c | %f | %f\n",p->c[0],p->c[1]);
1082 printf(" d | %f | %f\n",p->d[0],p->d[1]);
1083 printf(" h | %f | %f\n",p->h[0],p->h[1]);
1084 printf(" chi | %f \n",p->chi);
1089 /* tersoff 1 body part */
1090 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
1093 t_tersoff_mult_params *params;
1094 t_tersoff_exchange *exchange;
1097 params=moldyn->pot1b_params;
1098 exchange=&(params->exchange);
1101 * simple: point constant parameters only depending on atom i to
1102 * their right values
1105 exchange->beta_i=&(params->beta[num]);
1106 exchange->n_i=&(params->n[num]);
1107 exchange->c_i=&(params->c[num]);
1108 exchange->d_i=&(params->d[num]);
1109 exchange->h_i=&(params->h[num]);
1111 exchange->betaini=pow(*(exchange->beta_i),*(exchange->n_i));
1112 exchange->ci2=params->c[num]*params->c[num];
1113 exchange->di2=params->d[num]*params->d[num];
1114 exchange->ci2di2=exchange->ci2/exchange->di2;
1119 /* tersoff 2 body part */
1120 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1122 t_tersoff_mult_params *params;
1123 t_tersoff_exchange *exchange;
1124 t_3dvec dist_ij,force;
1126 double A,B,R,S,lambda,mu;
1133 params=moldyn->pot2b_params;
1135 exchange=&(params->exchange);
1137 /* clear 3bp and 2bp post run */
1139 exchange->run2bp_post=0;
1141 /* reset S > r > R mark */
1142 exchange->d_ij_between_rs=0;
1145 * calc of 2bp contribution of V_ij and dV_ij/ji
1147 * for Vij and dV_ij we need:
1151 * for dV_ji we need:
1152 * - f_c_ji = f_c_ij, df_c_ji = df_c_ij
1153 * - f_r_ji = f_r_ij; df_r_ji = df_r_ij
1158 v3_sub(&dist_ij,&(aj->r),&(ai->r));
1159 if(bc) check_per_bound(moldyn,&dist_ij);
1160 d_ij=v3_norm(&dist_ij);
1162 /* save for use in 3bp */
1163 exchange->d_ij=d_ij;
1164 exchange->dist_ij=dist_ij;
1172 lambda=params->lambda[num];
1181 lambda=params->lambda_m;
1183 params->exchange.chi=params->chi;
1186 /* if d_ij > S => no force & potential energy contribution */
1190 /* more constants */
1191 exchange->beta_j=&(params->beta[num]);
1192 exchange->n_j=&(params->n[num]);
1193 exchange->c_j=&(params->c[num]);
1194 exchange->d_j=&(params->d[num]);
1195 exchange->h_j=&(params->h[num]);
1197 exchange->betajnj=exchange->betaini;
1198 exchange->cj2=exchange->ci2;
1199 exchange->dj2=exchange->di2;
1200 exchange->cj2dj2=exchange->ci2di2;
1203 exchange->betajnj=pow(*(exchange->beta_j),*(exchange->n_j));
1204 exchange->cj2=params->c[num]*params->c[num];
1205 exchange->dj2=params->d[num]*params->d[num];
1206 exchange->cj2dj2=exchange->cj2/exchange->dj2;
1209 /* f_r_ij = f_r_ji, df_r_ij = df_r_ji */
1210 f_r=A*exp(-lambda*d_ij);
1211 df_r=lambda*f_r/d_ij;
1213 /* f_a, df_a calc (again, same for ij and ji) | save for later use! */
1214 exchange->f_a=-B*exp(-mu*d_ij);
1215 exchange->df_a=-mu*exchange->f_a/d_ij;
1217 /* f_c, df_c calc (again, same for ij and ji) */
1219 /* f_c = 1, df_c = 0 */
1222 /* two body contribution (ij, ji) */
1223 v3_scale(&force,&dist_ij,-df_r);
1227 arg=M_PI*(d_ij-R)/s_r;
1228 f_c=0.5+0.5*cos(arg);
1229 df_c=-0.5*sin(arg)*(M_PI/(s_r*d_ij));
1230 /* two body contribution (ij, ji) */
1231 v3_scale(&force,&dist_ij,-df_c*f_r-df_r*f_c);
1232 /* tell 3bp that S > r > R */
1233 exchange->d_ij_between_rs=1;
1236 /* add forces of 2bp (ij, ji) contribution
1237 * dVij = dVji and we sum up both: no 1/2) */
1238 v3_add(&(ai->f),&(ai->f),&force);
1240 /* energy 2bp contribution (ij, ji) is 0.5 f_r f_c ... */
1241 moldyn->energy+=(0.5*f_r*f_c);
1243 /* save for use in 3bp */
1245 exchange->df_c=df_c;
1247 /* enable the run of 3bp function and 2bp post processing */
1249 exchange->run2bp_post=1;
1251 /* reset 3bp sums */
1252 exchange->zeta_ij=0.0;
1253 exchange->zeta_ji=0.0;
1254 v3_zero(&(exchange->dzeta_ij));
1255 v3_zero(&(exchange->dzeta_ji));
1260 /* tersoff 2 body post part */
1262 int tersoff_mult_post_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1265 * here we have to allow for the 3bp sums
1268 * - zeta_ij, dzeta_ij
1269 * - zeta_ji, dzeta_ji
1271 * to compute the 3bp contribution to:
1277 t_tersoff_mult_params *params;
1278 t_tersoff_exchange *exchange;
1283 double f_c,df_c,f_a,df_a;
1284 double chi,ni,betaini,nj,betajnj;
1287 params=moldyn->pot2b_params;
1288 exchange=&(params->exchange);
1290 /* we do not run if f_c_ij was detected to be 0! */
1291 if(!(exchange->run2bp_post))
1295 df_c=exchange->df_c;
1297 df_a=exchange->df_a;
1298 betaini=exchange->betaini;
1299 betajnj=exchange->betajnj;
1300 ni=*(exchange->n_i);
1301 nj=*(exchange->n_j);
1303 dist_ij=&(exchange->dist_ij);
1306 zeta=exchange->zeta_ij;
1308 moldyn->debug++; /* just for debugging ... */
1311 v3_scale(&force,dist_ij,df_a*b*f_c);
1314 tmp=betaini*pow(zeta,ni-1.0); /* beta^n * zeta^n-1 */
1315 b=(1+zeta*tmp); /* 1 + beta^n zeta^n */
1316 db=chi*pow(b,-1.0/(2*ni)-1); /* x(...)^(-1/2n - 1) */
1318 db*=-0.5*tmp; /* db_ij */
1319 v3_scale(&force,&(exchange->dzeta_ij),f_a*db);
1320 v3_scale(&temp,dist_ij,df_a*b);
1321 v3_add(&force,&force,&temp);
1322 v3_scale(&force,&force,f_c);
1324 v3_scale(&temp,dist_ij,df_c*b*f_a);
1325 v3_add(&force,&force,&temp);
1326 v3_scale(&force,&force,-0.5);
1329 v3_add(&(ai->f),&(ai->f),&force);
1331 /* add energy of 3bp sum */
1332 moldyn->energy+=(0.5*f_c*b*f_a);
1335 zeta=exchange->zeta_ji;
1339 v3_scale(&force,dist_ij,df_a*b*f_c);
1342 tmp=betajnj*pow(zeta,nj-1.0); /* beta^n * zeta^n-1 */
1343 b=(1+zeta*tmp); /* 1 + beta^n zeta^n */
1344 db=chi*pow(b,-1.0/(2*nj)-1); /* x(...)^(-1/2n - 1) */
1346 db*=-0.5*tmp; /* db_ij */
1347 v3_scale(&force,&(exchange->dzeta_ji),f_a*db);
1348 v3_scale(&temp,dist_ij,df_a*b);
1349 v3_add(&force,&force,&temp);
1350 v3_scale(&force,&force,f_c);
1352 v3_scale(&temp,dist_ij,df_c*b*f_a);
1353 v3_add(&force,&force,&temp);
1354 v3_scale(&force,&force,-0.5);
1357 v3_sub(&(ai->f),&(ai->f),&force);
1362 /* tersoff 3 body part */
1364 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1366 t_tersoff_mult_params *params;
1367 t_tersoff_exchange *exchange;
1368 t_3dvec dist_ij,dist_ik,dist_jk;
1369 t_3dvec temp1,temp2;
1373 double d_ij,d_ik,d_jk;
1376 double f_c_ik,df_c_ik,arg;
1380 double cos_theta,d_costheta1,d_costheta2;
1381 double h_cos,d2_h_cos2;
1382 double frac,g,zeta,chi;
1386 params=moldyn->pot3b_params;
1387 exchange=&(params->exchange);
1389 if(!(exchange->run3bp))
1393 * calc of 3bp contribution of V_ij and dV_ij/ji/jk &
1394 * 2bp contribution of dV_jk
1396 * for Vij and dV_ij we still need:
1397 * - b_ij, db_ij (zeta_ij)
1398 * - f_c_ik, df_c_ik, constants_i, cos_theta_ijk, d_costheta_ijk
1400 * for dV_ji we still need:
1401 * - b_ji, db_ji (zeta_ji)
1402 * - f_c_jk, d_c_jk, constants_j, cos_theta_jik, d_costheta_jik
1404 * for dV_jk we need:
1408 * - f_c_ji, df_c_ji, constants_j, cos_theta_jki, d_costheta_jki
1416 /* dist_ij, d_ij - this is < S_ij ! */
1417 dist_ij=exchange->dist_ij;
1418 d_ij=exchange->d_ij;
1420 /* f_c_ij, df_c_ij (same for ji) */
1422 df_c=exchange->df_c;
1425 * calculate unknown values now ...
1428 /* V_ij and dV_ij stuff (in b_ij there is f_c_ik) */
1431 v3_sub(&dist_ik,&(ak->r),&(ai->r));
1432 if(bc) check_per_bound(moldyn,&dist_ik);
1433 d_ik=v3_norm(&dist_ik);
1446 /* zeta_ij/dzeta_ij contribution only for d_ik < S */
1449 /* get constants_i from exchange data */
1456 c2d2=exchange->ci2di2;
1458 /* cosine of theta_ijk by scalaproduct */
1459 rr=v3_scalar_product(&dist_ij,&dist_ik);
1465 d_costheta1=cos_theta/(d_ij*d_ij)-tmp;
1466 d_costheta2=cos_theta/(d_ik*d_ik)-tmp;
1468 /* some usefull values */
1469 h_cos=(h-cos_theta);
1470 d2_h_cos2=d2+(h_cos*h_cos);
1471 frac=c2/(d2_h_cos2);
1476 /* d_costheta_ij and dg(cos_theta) - needed in any case! */
1477 v3_scale(&temp1,&dist_ij,d_costheta1);
1478 v3_scale(&temp2,&dist_ik,d_costheta2);
1479 v3_add(&temp1,&temp1,&temp2);
1480 v3_scale(&temp1,&temp1,-2.0*frac*h_cos/d2_h_cos2); /* dg */
1482 /* f_c_ik & df_c_ik + {d,}zeta contribution */
1483 dzeta=&(exchange->dzeta_ij);
1487 // => df_c_ik=0.0; of course we do not set this!
1490 exchange->zeta_ij+=g;
1493 v3_add(dzeta,dzeta,&temp1);
1498 arg=M_PI*(d_ik-R)/s_r;
1499 f_c_ik=0.5+0.5*cos(arg);
1500 df_c_ik=-0.5*sin(arg)*(M_PI/(s_r*d_ik));
1503 exchange->zeta_ij+=f_c_ik*g;
1506 v3_scale(&temp1,&temp1,f_c_ik);
1507 v3_scale(&temp2,&dist_ik,g*df_c_ik);
1508 v3_add(&temp1,&temp1,&temp2);
1509 v3_add(dzeta,dzeta,&temp1);
1513 /* dV_ji stuff (in b_ji there is f_c_jk) + dV_jk stuff! */
1516 v3_sub(&dist_jk,&(ak->r),&(aj->r));
1517 if(bc) check_per_bound(moldyn,&dist_jk);
1518 d_jk=v3_norm(&dist_jk);
1537 /* zeta_ji/dzeta_ji contribution only for d_jk < S_jk */
1540 /* constants_j from exchange data */
1547 c2d2=exchange->cj2dj2;
1549 /* cosine of theta_jik by scalaproduct */
1550 rr=v3_scalar_product(&dist_ij,&dist_jk); /* times -1 */
1555 d_costheta1=1.0/(d_jk*d_ij);
1556 d_costheta2=cos_theta/(d_ij*d_ij); /* in fact -cos(), but ^ */
1558 /* some usefull values */
1559 h_cos=(h-cos_theta);
1560 d2_h_cos2=d2+(h_cos*h_cos);
1561 frac=c2/(d2_h_cos2);
1566 /* d_costheta_ij and dg(cos_theta) - needed in any case! */
1567 v3_scale(&temp1,&dist_jk,d_costheta1);
1568 v3_scale(&temp2,&dist_ij,-d_costheta2); /* ji -> ij => -1 */
1569 v3_add(&temp1,&temp1,&temp2);
1570 v3_scale(&temp1,&temp1,-2.0*frac*h_cos/d2_h_cos2); /* dg */
1572 /* f_c_jk + {d,}zeta contribution (df_c_jk = 0) */
1573 dzeta=&(exchange->dzeta_ji);
1579 exchange->zeta_ji+=g;
1582 v3_add(dzeta,dzeta,&temp1);
1587 arg=M_PI*(d_jk-R)/s_r;
1588 f_c_jk=0.5+0.5*cos(arg);
1591 exchange->zeta_ji+=f_c_jk*g;
1594 v3_scale(&temp1,&temp1,f_c_jk);
1595 v3_add(dzeta,dzeta,&temp1);
1598 /* dV_jk stuff | add force contribution on atom i immediately */
1599 if(exchange->d_ij_between_rs) {
1601 v3_scale(&temp1,&temp1,f_c);
1602 v3_scale(&temp2,&dist_ij,df_c);
1603 v3_add(&temp1,&temp1,&temp2);
1607 // dzeta_jk is simply dg, which is temp1
1609 /* betajnj * zeta_jk ^ nj-1 */
1610 tmp=exchange->betajnj*pow(zeta,(n-1.0));
1611 tmp=-chi/2.0*pow(1+tmp*zeta,-1.0/(2.0*n)-1)*tmp;
1612 v3_scale(&temp1,&temp1,tmp*B*exp(-mu*d_jk)*f_c_jk*0.5);
1613 v3_add(&(ai->f),&(ai->f),&temp1); /* -1 skipped in f_a calc ^ */
1614 /* scaled with 0.5 ^ */
1622 * debugging / critical check functions
1625 int moldyn_bc_check(t_moldyn *moldyn) {
1634 for(i=0;i<moldyn->count;i++) {
1635 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2)
1636 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
1637 i,atom[i].r.x*1e10,dim->x/2*1e10);
1638 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
1639 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
1640 i,atom[i].r.y*1e10,dim->y/2*1e10);
1641 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
1642 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
1643 i,atom[i].r.z*1e10,dim->z/2*1e10);