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) {
102 printf("[moldyn] dimensions in A:\n");
103 printf(" x: %f\n",moldyn->dim.x);
104 printf(" y: %f\n",moldyn->dim.y);
105 printf(" z: %f\n",moldyn->dim.z);
106 printf(" visualize simulation box: %s\n",visualize?"on":"off");
111 int set_nn_dist(t_moldyn *moldyn,double dist) {
118 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
121 moldyn->status|=MOLDYN_STAT_PBX;
124 moldyn->status|=MOLDYN_STAT_PBY;
127 moldyn->status|=MOLDYN_STAT_PBZ;
132 int set_potential1b(t_moldyn *moldyn,pf_func1b func,void *params) {
135 moldyn->pot1b_params=params;
140 int set_potential2b(t_moldyn *moldyn,pf_func2b func,void *params) {
143 moldyn->pot2b_params=params;
148 int set_potential2b_post(t_moldyn *moldyn,pf_func2b_post func,void *params) {
150 moldyn->func2b_post=func;
151 moldyn->pot2b_params=params;
156 int set_potential3b(t_moldyn *moldyn,pf_func3b func,void *params) {
159 moldyn->pot3b_params=params;
164 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
166 strncpy(moldyn->vlsdir,dir,127);
171 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
177 case LOG_TOTAL_ENERGY:
178 moldyn->ewrite=timer;
179 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
180 moldyn->efd=open(filename,
181 O_WRONLY|O_CREAT|O_EXCL,
184 perror("[moldyn] energy log fd open");
187 dprintf(moldyn->efd,"# total energy log file\n");
189 case LOG_TOTAL_MOMENTUM:
190 moldyn->mwrite=timer;
191 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
192 moldyn->mfd=open(filename,
193 O_WRONLY|O_CREAT|O_EXCL,
196 perror("[moldyn] momentum log fd open");
199 dprintf(moldyn->efd,"# total momentum log file\n");
202 moldyn->swrite=timer;
205 moldyn->vwrite=timer;
206 ret=visual_init(&(moldyn->vis),moldyn->vlsdir);
208 printf("[moldyn] visual init failure\n");
213 printf("[moldyn] unknown log mechanism: %02x\n",type);
220 int moldyn_log_shutdown(t_moldyn *moldyn) {
222 printf("[moldyn] log shutdown\n");
223 if(moldyn->efd) close(moldyn->efd);
224 if(moldyn->mfd) close(moldyn->mfd);
225 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
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) {
239 /* how many atoms do we expect */
240 if(type==FCC) count*=4;
241 if(type==DIAMOND) count*=8;
243 /* allocate space for atoms */
244 moldyn->atom=malloc(count*sizeof(t_atom));
245 if(moldyn->atom==NULL) {
246 perror("malloc (atoms)");
254 ret=fcc_init(a,b,c,lc,moldyn->atom,&origin);
257 ret=diamond_init(a,b,c,lc,moldyn->atom,&origin);
260 printf("unknown lattice type (%02x)\n",type);
266 printf("[moldyn] creating lattice failed\n");
267 printf(" amount of atoms\n");
268 printf(" - expected: %d\n",count);
269 printf(" - created: %d\n",ret);
274 printf("[moldyn] created lattice with %d atoms\n",count);
278 moldyn->atom[count].element=element;
279 moldyn->atom[count].mass=mass;
280 moldyn->atom[count].attr=attr;
281 moldyn->atom[count].bnum=bnum;
282 check_per_bound(moldyn,&(moldyn->atom[count].r));
288 int add_atom(t_moldyn *moldyn,int element,double mass,u8 bnum,u8 attr,
289 t_3dvec *r,t_3dvec *v) {
296 count=++(moldyn->count);
298 ptr=realloc(atom,count*sizeof(t_atom));
300 perror("[moldyn] realloc (add atom)");
308 atom[count-1].element=element;
309 atom[count-1].mass=mass;
310 atom[count-1].bnum=bnum;
311 atom[count-1].attr=attr;
316 int destroy_atoms(t_moldyn *moldyn) {
318 if(moldyn->atom) free(moldyn->atom);
323 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
326 * - gaussian distribution of velocities
327 * - zero total momentum
328 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
333 t_3dvec p_total,delta;
338 random=&(moldyn->random);
340 /* gaussian distribution of velocities */
342 for(i=0;i<moldyn->count;i++) {
343 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
345 v=sigma*rand_get_gauss(random);
347 p_total.x+=atom[i].mass*v;
349 v=sigma*rand_get_gauss(random);
351 p_total.y+=atom[i].mass*v;
353 v=sigma*rand_get_gauss(random);
355 p_total.z+=atom[i].mass*v;
358 /* zero total momentum */
359 v3_scale(&p_total,&p_total,1.0/moldyn->count);
360 for(i=0;i<moldyn->count;i++) {
361 v3_scale(&delta,&p_total,1.0/atom[i].mass);
362 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
365 /* velocity scaling */
366 scale_velocity(moldyn,equi_init);
371 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
381 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
384 /* get kinetic energy / temperature & count involved atoms */
387 for(i=0;i<moldyn->count;i++) {
388 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
389 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
393 if(count!=0) moldyn->t=(2.0*e)/(3.0*count*K_BOLTZMANN);
394 else return 0; /* no atoms involved in scaling! */
396 /* (temporary) hack for e,t = 0 */
399 if(moldyn->t_ref!=0.0) {
400 thermal_init(moldyn,equi_init);
404 return 0; /* no scaling needed */
408 /* get scaling factor */
409 scale=moldyn->t_ref/moldyn->t;
413 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
414 scale=1.0+(scale-1.0)/moldyn->t_tc;
417 /* velocity scaling */
418 for(i=0;i<moldyn->count;i++) {
419 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
420 v3_scale(&(atom[i].v),&(atom[i].v),scale);
426 double get_e_kin(t_moldyn *moldyn) {
434 for(i=0;i<moldyn->count;i++)
435 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
440 double get_e_pot(t_moldyn *moldyn) {
442 return moldyn->energy;
445 double update_e_kin(t_moldyn *moldyn) {
447 return(get_e_kin(moldyn));
450 double get_total_energy(t_moldyn *moldyn) {
452 return(moldyn->ekin+moldyn->energy);
455 t_3dvec get_total_p(t_moldyn *moldyn) {
464 for(i=0;i<moldyn->count;i++) {
465 v3_scale(&p,&(atom[i].v),atom[i].mass);
466 v3_add(&p_total,&p_total,&p);
472 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
476 /* nn_dist is the nearest neighbour distance */
478 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
487 /* linked list / cell method */
489 int link_cell_init(t_moldyn *moldyn) {
495 fd=open("/dev/null",O_WRONLY);
499 /* partitioning the md cell */
500 lc->nx=moldyn->dim.x/moldyn->cutoff;
501 lc->x=moldyn->dim.x/lc->nx;
502 lc->ny=moldyn->dim.y/moldyn->cutoff;
503 lc->y=moldyn->dim.y/lc->ny;
504 lc->nz=moldyn->dim.z/moldyn->cutoff;
505 lc->z=moldyn->dim.z/lc->nz;
507 lc->cells=lc->nx*lc->ny*lc->nz;
508 lc->subcell=malloc(lc->cells*sizeof(t_list));
510 printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
512 for(i=0;i<lc->cells;i++)
513 //list_init(&(lc->subcell[i]),1);
514 list_init(&(lc->subcell[i]),fd);
516 link_cell_update(moldyn);
521 int link_cell_update(t_moldyn *moldyn) {
535 for(i=0;i<lc->cells;i++)
536 list_destroy(&(moldyn->lc.subcell[i]));
538 for(count=0;count<moldyn->count;count++) {
539 i=(atom[count].r.x+(moldyn->dim.x/2))/lc->x;
540 j=(atom[count].r.y+(moldyn->dim.y/2))/lc->y;
541 k=(atom[count].r.z+(moldyn->dim.z/2))/lc->z;
542 list_add_immediate_ptr(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
549 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
567 cell[0]=lc->subcell[i+j*nx+k*a];
568 for(ci=-1;ci<=1;ci++) {
575 for(cj=-1;cj<=1;cj++) {
582 for(ck=-1;ck<=1;ck++) {
589 if(!(ci|cj|ck)) continue;
591 cell[--count2]=lc->subcell[x+y*nx+z*a];
594 cell[count1++]=lc->subcell[x+y*nx+z*a];
605 int link_cell_shutdown(t_moldyn *moldyn) {
612 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
613 list_shutdown(&(moldyn->lc.subcell[i]));
618 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
622 t_moldyn_schedule *schedule;
624 schedule=&(moldyn->schedule);
625 count=++(schedule->content_count);
627 ptr=realloc(moldyn->schedule.runs,count*sizeof(int));
629 perror("[moldyn] realloc (runs)");
632 moldyn->schedule.runs=ptr;
633 moldyn->schedule.runs[count-1]=runs;
635 ptr=realloc(schedule->tau,count*sizeof(double));
637 perror("[moldyn] realloc (tau)");
640 moldyn->schedule.tau=ptr;
641 moldyn->schedule.tau[count-1]=tau;
646 int moldyn_set_schedule_hook(t_moldyn *moldyn,void *hook,void *hook_params) {
648 moldyn->schedule.hook=hook;
649 moldyn->schedule.hook_params=hook_params;
656 * 'integration of newtons equation' - algorithms
660 /* start the integration */
662 int moldyn_integrate(t_moldyn *moldyn) {
665 unsigned int e,m,s,v;
667 t_moldyn_schedule *schedule;
673 schedule=&(moldyn->schedule);
676 /* initialize linked cell method */
677 link_cell_init(moldyn);
679 /* logging & visualization */
685 /* sqaure of some variables */
686 moldyn->tau_square=moldyn->tau*moldyn->tau;
687 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
689 /* calculate initial forces */
690 potential_force_calc(moldyn);
692 /* some stupid checks before we actually start calculating bullshit */
693 if(moldyn->cutoff>0.5*moldyn->dim.x)
694 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
695 if(moldyn->cutoff>0.5*moldyn->dim.y)
696 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
697 if(moldyn->cutoff>0.5*moldyn->dim.z)
698 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
699 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
700 if(ds>0.05*moldyn->nnd)
701 printf("[moldyn] warning: forces too high / tau too small!\n");
703 /* zero absolute time */
706 /* debugging, ignore */
709 /* executing the schedule */
710 for(sched=0;sched<moldyn->schedule.content_count;sched++) {
712 /* setting amount of runs and finite time step size */
713 moldyn->tau=schedule->tau[sched];
714 moldyn->tau_square=moldyn->tau*moldyn->tau;
715 moldyn->time_steps=schedule->runs[sched];
717 /* integration according to schedule */
719 for(i=0;i<moldyn->time_steps;i++) {
721 /* integration step */
722 moldyn->integrate(moldyn);
725 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
726 scale_velocity(moldyn,FALSE);
728 /* check for log & visualization */
733 moldyn->time,update_e_kin(moldyn),
735 get_total_energy(moldyn));
739 p=get_total_p(moldyn);
741 "%f %f\n",moldyn->time,v3_norm(&p));
746 snprintf(dir,128,"%s/s-%07.f.save",
747 moldyn->vlsdir,moldyn->time);
748 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT);
749 if(fd<0) perror("[moldyn] save fd open");
751 write(fd,moldyn,sizeof(t_moldyn));
752 write(fd,moldyn->atom,
753 moldyn->count*sizeof(t_atom));
760 visual_atoms(&(moldyn->vis),moldyn->time,
761 moldyn->atom,moldyn->count);
762 printf("\rsched: %d, steps: %d, debug: %d",
763 sched,i,moldyn->debug);
768 /* increase absolute time */
769 moldyn->time+=moldyn->tau;
773 /* check for hooks */
775 schedule->hook(moldyn,schedule->hook_params);
777 /* get a new info line */
785 /* velocity verlet */
787 int velocity_verlet(t_moldyn *moldyn) {
790 double tau,tau_square;
797 tau_square=moldyn->tau_square;
799 for(i=0;i<count;i++) {
801 v3_scale(&delta,&(atom[i].v),tau);
802 v3_add(&(atom[i].r),&(atom[i].r),&delta);
803 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
804 v3_add(&(atom[i].r),&(atom[i].r),&delta);
805 check_per_bound(moldyn,&(atom[i].r));
808 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
809 v3_add(&(atom[i].v),&(atom[i].v),&delta);
812 moldyn_bc_check(moldyn);
813 /* neighbour list update */
814 link_cell_update(moldyn);
816 /* forces depending on chosen potential */
817 potential_force_calc(moldyn);
819 for(i=0;i<count;i++) {
820 /* again velocities */
821 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
822 v3_add(&(atom[i].v),&(atom[i].v),&delta);
831 * potentials & corresponding forces
835 /* generic potential and force calculation */
837 int potential_force_calc(t_moldyn *moldyn) {
840 t_atom *itom,*jtom,*ktom;
842 t_list neighbour_i[27];
843 t_list neighbour_i2[27];
844 //t_list neighbour_j[27];
856 /* get energy and force of every atom */
857 for(i=0;i<count;i++) {
860 v3_zero(&(itom[i].f));
862 /* single particle potential/force */
863 if(itom[i].attr&ATOM_ATTR_1BP)
864 moldyn->func1b(moldyn,&(itom[i]));
866 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
869 /* 2 body pair potential/force */
871 link_cell_neighbour_index(moldyn,
872 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
873 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
874 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
881 this=&(neighbour_i[j]);
884 if(this->start==NULL)
890 jtom=this->current->data;
895 if((jtom->attr&ATOM_ATTR_2BP)&
896 (itom[i].attr&ATOM_ATTR_2BP)) {
897 moldyn->func2b(moldyn,
903 /* 3 body potential/force */
905 if(!(itom[i].attr&ATOM_ATTR_3BP)||
906 !(jtom->attr&ATOM_ATTR_3BP))
909 /* copy the neighbour lists */
910 memcpy(neighbour_i2,neighbour_i,
913 /* get neighbours of i */
916 that=&(neighbour_i2[k]);
919 if(that->start==NULL)
926 ktom=that->current->data;
928 if(!(ktom->attr&ATOM_ATTR_3BP))
937 moldyn->func3b(moldyn,
943 } while(list_next(that)!=\
948 /* 2bp post function */
949 if(moldyn->func2b_post) {
950 moldyn->func2b_post(moldyn,
955 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
965 * periodic boundayr checking
968 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
979 if(moldyn->status&MOLDYN_STAT_PBX) {
980 if(a->x>=x) a->x-=dim->x;
981 else if(-a->x>x) a->x+=dim->x;
983 if(moldyn->status&MOLDYN_STAT_PBY) {
984 if(a->y>=y) a->y-=dim->y;
985 else if(-a->y>y) a->y+=dim->y;
987 if(moldyn->status&MOLDYN_STAT_PBZ) {
988 if(a->z>=z) a->z-=dim->z;
989 else if(-a->z>z) a->z+=dim->z;
991 printf("%f %f %f\n",a->x,x,a->x/x);
1001 /* harmonic oscillator potential and force */
1003 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1005 t_ho_params *params;
1006 t_3dvec force,distance;
1008 double sc,equi_dist;
1010 params=moldyn->pot2b_params;
1011 sc=params->spring_constant;
1012 equi_dist=params->equilibrium_distance;
1014 v3_sub(&distance,&(aj->r),&(ai->r));
1016 if(bc) check_per_bound(moldyn,&distance);
1017 d=v3_norm(&distance);
1018 if(d<=moldyn->cutoff) {
1019 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
1020 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
1021 /* f = -grad E; grad r_ij = -1 1/r_ij distance */
1022 v3_scale(&force,&distance,sc*(1.0-(equi_dist/d)));
1023 v3_add(&(ai->f),&(ai->f),&force);
1029 /* lennard jones potential & force for one sort of atoms */
1031 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1033 t_lj_params *params;
1034 t_3dvec force,distance;
1036 double eps,sig6,sig12;
1038 params=moldyn->pot2b_params;
1039 eps=params->epsilon4;
1040 sig6=params->sigma6;
1041 sig12=params->sigma12;
1043 v3_sub(&distance,&(aj->r),&(ai->r));
1044 if(bc) check_per_bound(moldyn,&distance);
1045 d=v3_absolute_square(&distance); /* 1/r^2 */
1046 if(d<=moldyn->cutoff_square) {
1047 d=1.0/d; /* 1/r^2 */
1050 h1=h2*h2; /* 1/r^12 */
1051 /* energy is eps*..., but we will add this twice ... */
1052 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
1059 v3_scale(&force,&distance,-1.0*d); /* f = - grad E */
1060 v3_add(&(ai->f),&(ai->f),&force);
1067 * tersoff potential & force for 2 sorts of atoms
1070 /* create mixed terms from parameters and set them */
1071 int tersoff_mult_complete_params(t_tersoff_mult_params *p) {
1073 printf("[moldyn] tersoff parameter completion\n");
1074 p->Smixed=sqrt(p->S[0]*p->S[1]);
1075 p->Rmixed=sqrt(p->R[0]*p->R[1]);
1076 p->Amixed=sqrt(p->A[0]*p->A[1]);
1077 p->Bmixed=sqrt(p->B[0]*p->B[1]);
1078 p->lambda_m=0.5*(p->lambda[0]+p->lambda[1]);
1079 p->mu_m=0.5*(p->mu[0]+p->mu[1]);
1081 printf("[moldyn] tersoff mult parameter info:\n");
1082 printf(" S (A) | %f | %f | %f\n",p->S[0],p->S[1],p->Smixed);
1083 printf(" R (A) | %f | %f | %f\n",p->R[0],p->R[1],p->Rmixed);
1084 printf(" A (eV) | %f | %f | %f\n",p->A[0]/EV,p->A[1]/EV,p->Amixed/EV);
1085 printf(" B (eV) | %f | %f | %f\n",p->B[0]/EV,p->B[1]/EV,p->Bmixed/EV);
1086 printf(" lambda | %f | %f | %f\n",p->lambda[0],p->lambda[1],
1088 printf(" mu | %f | %f | %f\n",p->mu[0],p->mu[1],p->mu_m);
1089 printf(" beta | %.10f | %.10f\n",p->beta[0],p->beta[1]);
1090 printf(" n | %f | %f\n",p->n[0],p->n[1]);
1091 printf(" c | %f | %f\n",p->c[0],p->c[1]);
1092 printf(" d | %f | %f\n",p->d[0],p->d[1]);
1093 printf(" h | %f | %f\n",p->h[0],p->h[1]);
1094 printf(" chi | %f \n",p->chi);
1099 /* tersoff 1 body part */
1100 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
1103 t_tersoff_mult_params *params;
1104 t_tersoff_exchange *exchange;
1107 params=moldyn->pot1b_params;
1108 exchange=&(params->exchange);
1111 * simple: point constant parameters only depending on atom i to
1112 * their right values
1115 exchange->beta_i=&(params->beta[num]);
1116 exchange->n_i=&(params->n[num]);
1117 exchange->c_i=&(params->c[num]);
1118 exchange->d_i=&(params->d[num]);
1119 exchange->h_i=&(params->h[num]);
1121 exchange->betaini=pow(*(exchange->beta_i),*(exchange->n_i));
1122 exchange->ci2=params->c[num]*params->c[num];
1123 exchange->di2=params->d[num]*params->d[num];
1124 exchange->ci2di2=exchange->ci2/exchange->di2;
1129 /* tersoff 2 body part */
1130 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1132 t_tersoff_mult_params *params;
1133 t_tersoff_exchange *exchange;
1134 t_3dvec dist_ij,force;
1136 double A,B,R,S,lambda,mu;
1143 params=moldyn->pot2b_params;
1145 exchange=&(params->exchange);
1147 /* clear 3bp and 2bp post run */
1149 exchange->run2bp_post=0;
1151 /* reset S > r > R mark */
1152 exchange->d_ij_between_rs=0;
1155 * calc of 2bp contribution of V_ij and dV_ij/ji
1157 * for Vij and dV_ij we need:
1161 * for dV_ji we need:
1162 * - f_c_ji = f_c_ij, df_c_ji = df_c_ij
1163 * - f_r_ji = f_r_ij; df_r_ji = df_r_ij
1168 v3_sub(&dist_ij,&(aj->r),&(ai->r));
1169 if(bc) check_per_bound(moldyn,&dist_ij);
1170 d_ij=v3_norm(&dist_ij);
1172 /* save for use in 3bp */
1173 exchange->d_ij=d_ij;
1174 exchange->dist_ij=dist_ij;
1182 lambda=params->lambda[num];
1191 lambda=params->lambda_m;
1193 params->exchange.chi=params->chi;
1196 /* if d_ij > S => no force & potential energy contribution */
1200 /* more constants */
1201 exchange->beta_j=&(params->beta[num]);
1202 exchange->n_j=&(params->n[num]);
1203 exchange->c_j=&(params->c[num]);
1204 exchange->d_j=&(params->d[num]);
1205 exchange->h_j=&(params->h[num]);
1207 exchange->betajnj=exchange->betaini;
1208 exchange->cj2=exchange->ci2;
1209 exchange->dj2=exchange->di2;
1210 exchange->cj2dj2=exchange->ci2di2;
1213 exchange->betajnj=pow(*(exchange->beta_j),*(exchange->n_j));
1214 exchange->cj2=params->c[num]*params->c[num];
1215 exchange->dj2=params->d[num]*params->d[num];
1216 exchange->cj2dj2=exchange->cj2/exchange->dj2;
1219 /* f_r_ij = f_r_ji, df_r_ij = df_r_ji */
1220 f_r=A*exp(-lambda*d_ij);
1221 df_r=lambda*f_r/d_ij;
1223 /* f_a, df_a calc (again, same for ij and ji) | save for later use! */
1224 exchange->f_a=-B*exp(-mu*d_ij);
1225 exchange->df_a=mu*exchange->f_a/d_ij;
1227 /* f_c, df_c calc (again, same for ij and ji) */
1229 /* f_c = 1, df_c = 0 */
1232 /* two body contribution (ij, ji) */
1233 v3_scale(&force,&dist_ij,-df_r);
1237 arg=M_PI*(d_ij-R)/s_r;
1238 f_c=0.5+0.5*cos(arg);
1239 df_c=-0.5*sin(arg)*(M_PI/(s_r*d_ij));
1240 /* two body contribution (ij, ji) */
1241 v3_scale(&force,&dist_ij,-df_c*f_r-df_r*f_c);
1242 /* tell 3bp that S > r > R */
1243 exchange->d_ij_between_rs=1;
1246 /* add forces of 2bp (ij, ji) contribution
1247 * dVij = dVji and we sum up both: no 1/2) */
1248 v3_add(&(ai->f),&(ai->f),&force);
1250 /* energy 2bp contribution (ij, ji) is 0.5 f_r f_c ... */
1251 moldyn->energy+=(0.5*f_r*f_c);
1253 /* save for use in 3bp */
1255 exchange->df_c=df_c;
1257 /* enable the run of 3bp function and 2bp post processing */
1259 exchange->run2bp_post=1;
1261 /* reset 3bp sums */
1262 exchange->zeta_ij=0.0;
1263 exchange->zeta_ji=0.0;
1264 v3_zero(&(exchange->dzeta_ij));
1265 v3_zero(&(exchange->dzeta_ji));
1270 /* tersoff 2 body post part */
1272 int tersoff_mult_post_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1275 * here we have to allow for the 3bp sums
1278 * - zeta_ij, dzeta_ij
1279 * - zeta_ji, dzeta_ji
1281 * to compute the 3bp contribution to:
1287 t_tersoff_mult_params *params;
1288 t_tersoff_exchange *exchange;
1293 double f_c,df_c,f_a,df_a;
1294 double chi,ni,betaini,nj,betajnj;
1297 params=moldyn->pot2b_params;
1298 exchange=&(params->exchange);
1300 /* we do not run if f_c_ij was detected to be 0! */
1301 if(!(exchange->run2bp_post))
1305 df_c=exchange->df_c;
1307 df_a=exchange->df_a;
1308 betaini=exchange->betaini;
1309 betajnj=exchange->betajnj;
1310 ni=*(exchange->n_i);
1311 nj=*(exchange->n_j);
1313 dist_ij=&(exchange->dist_ij);
1316 zeta=exchange->zeta_ij;
1318 moldyn->debug++; /* just for debugging ... */
1321 v3_scale(&force,dist_ij,df_a*b*f_c);
1324 tmp=betaini*pow(zeta,ni-1.0); /* beta^n * zeta^n-1 */
1325 b=(1+zeta*tmp); /* 1 + beta^n zeta^n */
1326 db=chi*pow(b,-1.0/(2*ni)-1); /* x(...)^(-1/2n - 1) */
1328 db*=-0.5*tmp; /* db_ij */
1329 v3_scale(&force,&(exchange->dzeta_ij),f_a*db);
1330 v3_scale(&temp,dist_ij,df_a*b);
1331 v3_add(&force,&force,&temp);
1332 v3_scale(&force,&force,f_c);
1334 v3_scale(&temp,dist_ij,df_c*b*f_a);
1335 v3_add(&force,&force,&temp);
1336 v3_scale(&force,&force,-0.5);
1339 v3_add(&(ai->f),&(ai->f),&force);
1341 /* add energy of 3bp sum */
1342 moldyn->energy+=(0.5*f_c*b*f_a);
1345 zeta=exchange->zeta_ji;
1349 v3_scale(&force,dist_ij,df_a*b*f_c);
1352 tmp=betajnj*pow(zeta,nj-1.0); /* beta^n * zeta^n-1 */
1353 b=(1+zeta*tmp); /* 1 + beta^n zeta^n */
1354 db=chi*pow(b,-1.0/(2*nj)-1); /* x(...)^(-1/2n - 1) */
1356 db*=-0.5*tmp; /* db_ij */
1357 v3_scale(&force,&(exchange->dzeta_ji),f_a*db);
1358 v3_scale(&temp,dist_ij,df_a*b);
1359 v3_add(&force,&force,&temp);
1360 v3_scale(&force,&force,f_c);
1362 v3_scale(&temp,dist_ij,df_c*b*f_a);
1363 v3_add(&force,&force,&temp);
1364 v3_scale(&force,&force,-0.5);
1367 v3_add(&(ai->f),&(ai->f),&force);
1372 /* tersoff 3 body part */
1374 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1376 t_tersoff_mult_params *params;
1377 t_tersoff_exchange *exchange;
1378 t_3dvec dist_ij,dist_ik,dist_jk;
1379 t_3dvec temp1,temp2;
1383 double d_ij,d_ik,d_jk;
1386 double f_c_ik,df_c_ik,arg;
1390 double cos_theta,d_costheta1,d_costheta2;
1391 double h_cos,d2_h_cos2;
1392 double frac,g,zeta,chi;
1396 params=moldyn->pot3b_params;
1397 exchange=&(params->exchange);
1399 if(!(exchange->run3bp))
1403 * calc of 3bp contribution of V_ij and dV_ij/ji/jk &
1404 * 2bp contribution of dV_jk
1406 * for Vij and dV_ij we still need:
1407 * - b_ij, db_ij (zeta_ij)
1408 * - f_c_ik, df_c_ik, constants_i, cos_theta_ijk, d_costheta_ijk
1410 * for dV_ji we still need:
1411 * - b_ji, db_ji (zeta_ji)
1412 * - f_c_jk, d_c_jk, constants_j, cos_theta_jik, d_costheta_jik
1414 * for dV_jk we need:
1418 * - f_c_ji, df_c_ji, constants_j, cos_theta_jki, d_costheta_jki
1426 /* dist_ij, d_ij - this is < S_ij ! */
1427 dist_ij=exchange->dist_ij;
1428 d_ij=exchange->d_ij;
1430 /* f_c_ij, df_c_ij (same for ji) */
1432 df_c=exchange->df_c;
1435 * calculate unknown values now ...
1438 /* V_ij and dV_ij stuff (in b_ij there is f_c_ik) */
1441 v3_sub(&dist_ik,&(ak->r),&(ai->r));
1442 if(bc) check_per_bound(moldyn,&dist_ik);
1443 d_ik=v3_norm(&dist_ik);
1456 /* zeta_ij/dzeta_ij contribution only for d_ik < S */
1459 /* get constants_i from exchange data */
1466 c2d2=exchange->ci2di2;
1468 /* cosine of theta_ijk by scalaproduct */
1469 rr=v3_scalar_product(&dist_ij,&dist_ik);
1475 d_costheta1=cos_theta/(d_ij*d_ij)-tmp;
1476 d_costheta2=cos_theta/(d_ik*d_ik)-tmp;
1478 /* some usefull values */
1479 h_cos=(h-cos_theta);
1480 d2_h_cos2=d2+(h_cos*h_cos);
1481 frac=c2/(d2_h_cos2);
1486 /* d_costheta_ij and dg(cos_theta) - needed in any case! */
1487 v3_scale(&temp1,&dist_ij,d_costheta1);
1488 v3_scale(&temp2,&dist_ik,d_costheta2);
1489 v3_add(&temp1,&temp1,&temp2);
1490 v3_scale(&temp1,&temp1,-2.0*frac*h_cos/d2_h_cos2); /* dg */
1492 /* f_c_ik & df_c_ik + {d,}zeta contribution */
1493 dzeta=&(exchange->dzeta_ij);
1497 // => df_c_ik=0.0; of course we do not set this!
1500 exchange->zeta_ij+=g;
1503 v3_add(dzeta,dzeta,&temp1);
1508 arg=M_PI*(d_ik-R)/s_r;
1509 f_c_ik=0.5+0.5*cos(arg);
1510 df_c_ik=-0.5*sin(arg)*(M_PI/(s_r*d_ik));
1513 exchange->zeta_ij+=f_c_ik*g;
1516 v3_scale(&temp1,&temp1,f_c_ik);
1517 v3_scale(&temp2,&dist_ik,g*df_c_ik);
1518 v3_add(&temp1,&temp1,&temp2);
1519 v3_add(dzeta,dzeta,&temp1);
1523 /* dV_ji stuff (in b_ji there is f_c_jk) + dV_jk stuff! */
1526 v3_sub(&dist_jk,&(ak->r),&(aj->r));
1527 if(bc) check_per_bound(moldyn,&dist_jk);
1528 d_jk=v3_norm(&dist_jk);
1547 /* zeta_ji/dzeta_ji contribution only for d_jk < S_jk */
1550 /* constants_j from exchange data */
1557 c2d2=exchange->cj2dj2;
1559 /* cosine of theta_jik by scalaproduct */
1560 rr=v3_scalar_product(&dist_ij,&dist_jk); /* times -1 */
1565 d_costheta1=1.0/(d_jk*d_ij);
1566 d_costheta2=cos_theta/(d_ij*d_ij); /* in fact -cos(), but ^ */
1568 /* some usefull values */
1569 h_cos=(h-cos_theta);
1570 d2_h_cos2=d2+(h_cos*h_cos);
1571 frac=c2/(d2_h_cos2);
1576 /* d_costheta_ij and dg(cos_theta) - needed in any case! */
1577 v3_scale(&temp1,&dist_jk,d_costheta1);
1578 v3_scale(&temp2,&dist_ij,-d_costheta2); /* ji -> ij => -1 */
1579 v3_add(&temp1,&temp1,&temp2);
1580 v3_scale(&temp1,&temp1,-2.0*frac*h_cos/d2_h_cos2); /* dg */
1582 /* f_c_jk + {d,}zeta contribution (df_c_jk = 0) */
1583 dzeta=&(exchange->dzeta_ji);
1589 exchange->zeta_ji+=g;
1592 v3_add(dzeta,dzeta,&temp1);
1597 arg=M_PI*(d_jk-R)/s_r;
1598 f_c_jk=0.5+0.5*cos(arg);
1601 exchange->zeta_ji+=f_c_jk*g;
1604 v3_scale(&temp1,&temp1,f_c_jk);
1605 v3_add(dzeta,dzeta,&temp1);
1608 /* dV_jk stuff | add force contribution on atom i immediately */
1609 if(exchange->d_ij_between_rs) {
1611 v3_scale(&temp1,&temp1,f_c);
1612 v3_scale(&temp2,&dist_ij,df_c);
1613 v3_add(&temp1,&temp1,&temp2);
1617 // dzeta_jk is simply dg, which is temp1
1619 /* betajnj * zeta_jk ^ nj-1 */
1620 tmp=exchange->betajnj*pow(zeta,(n-1.0));
1621 tmp=-chi/2.0*pow(1+tmp*zeta,-1.0/(2.0*n)-1)*tmp;
1622 v3_scale(&temp1,&temp1,tmp*B*exp(-mu*d_jk)*f_c_jk*0.5);
1623 v3_add(&(ai->f),&(ai->f),&temp1); /* -1 skipped in f_a calc ^ */
1624 /* scaled with 0.5 ^ */
1632 * debugging / critical check functions
1635 int moldyn_bc_check(t_moldyn *moldyn) {
1644 for(i=0;i<moldyn->count;i++) {
1645 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2)
1646 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
1647 i,atom[i].r.x,dim->x/2);
1648 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
1649 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
1650 i,atom[i].r.y,dim->y/2);
1651 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
1652 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
1653 i,atom[i].r.z,dim->z/2);