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_pressure(t_moldyn *moldyn,double p_ref) {
88 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
90 moldyn->pt_scale=(ptype|ttype);
97 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
103 moldyn->volume=x*y*z;
111 printf("[moldyn] dimensions in A and A^2 respectively:\n");
112 printf(" x: %f\n",moldyn->dim.x);
113 printf(" y: %f\n",moldyn->dim.y);
114 printf(" z: %f\n",moldyn->dim.z);
115 printf(" volume: %f\n",moldyn->volume);
116 printf(" visualize simulation box: %s\n",visualize?"on":"off");
121 int set_nn_dist(t_moldyn *moldyn,double dist) {
128 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
131 moldyn->status|=MOLDYN_STAT_PBX;
134 moldyn->status|=MOLDYN_STAT_PBY;
137 moldyn->status|=MOLDYN_STAT_PBZ;
142 int set_potential1b(t_moldyn *moldyn,pf_func1b func,void *params) {
145 moldyn->pot1b_params=params;
150 int set_potential2b(t_moldyn *moldyn,pf_func2b func,void *params) {
153 moldyn->pot2b_params=params;
158 int set_potential2b_post(t_moldyn *moldyn,pf_func2b_post func,void *params) {
160 moldyn->func2b_post=func;
161 moldyn->pot2b_params=params;
166 int set_potential3b(t_moldyn *moldyn,pf_func3b func,void *params) {
169 moldyn->pot3b_params=params;
174 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
176 strncpy(moldyn->vlsdir,dir,127);
181 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
187 case LOG_TOTAL_ENERGY:
188 moldyn->ewrite=timer;
189 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
190 moldyn->efd=open(filename,
191 O_WRONLY|O_CREAT|O_EXCL,
194 perror("[moldyn] energy log fd open");
197 dprintf(moldyn->efd,"# total energy log file\n");
199 case LOG_TOTAL_MOMENTUM:
200 moldyn->mwrite=timer;
201 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
202 moldyn->mfd=open(filename,
203 O_WRONLY|O_CREAT|O_EXCL,
206 perror("[moldyn] momentum log fd open");
209 dprintf(moldyn->efd,"# total momentum log file\n");
212 moldyn->swrite=timer;
215 moldyn->vwrite=timer;
216 ret=visual_init(&(moldyn->vis),moldyn->vlsdir);
218 printf("[moldyn] visual init failure\n");
223 printf("[moldyn] unknown log mechanism: %02x\n",type);
230 int moldyn_log_shutdown(t_moldyn *moldyn) {
232 printf("[moldyn] log shutdown\n");
233 if(moldyn->efd) close(moldyn->efd);
234 if(moldyn->mfd) close(moldyn->mfd);
235 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
240 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
241 u8 attr,u8 bnum,int a,int b,int c) {
249 /* how many atoms do we expect */
250 if(type==FCC) count*=4;
251 if(type==DIAMOND) count*=8;
253 /* allocate space for atoms */
254 moldyn->atom=malloc(count*sizeof(t_atom));
255 if(moldyn->atom==NULL) {
256 perror("malloc (atoms)");
264 ret=fcc_init(a,b,c,lc,moldyn->atom,&origin);
267 ret=diamond_init(a,b,c,lc,moldyn->atom,&origin);
270 printf("unknown lattice type (%02x)\n",type);
276 printf("[moldyn] creating lattice failed\n");
277 printf(" amount of atoms\n");
278 printf(" - expected: %d\n",count);
279 printf(" - created: %d\n",ret);
284 printf("[moldyn] created lattice with %d atoms\n",count);
288 moldyn->atom[count].element=element;
289 moldyn->atom[count].mass=mass;
290 moldyn->atom[count].attr=attr;
291 moldyn->atom[count].bnum=bnum;
292 check_per_bound(moldyn,&(moldyn->atom[count].r));
298 int add_atom(t_moldyn *moldyn,int element,double mass,u8 bnum,u8 attr,
299 t_3dvec *r,t_3dvec *v) {
306 count=++(moldyn->count);
308 ptr=realloc(atom,count*sizeof(t_atom));
310 perror("[moldyn] realloc (add atom)");
318 atom[count-1].element=element;
319 atom[count-1].mass=mass;
320 atom[count-1].bnum=bnum;
321 atom[count-1].attr=attr;
326 int destroy_atoms(t_moldyn *moldyn) {
328 if(moldyn->atom) free(moldyn->atom);
333 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
336 * - gaussian distribution of velocities
337 * - zero total momentum
338 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
343 t_3dvec p_total,delta;
348 random=&(moldyn->random);
350 /* gaussian distribution of velocities */
352 for(i=0;i<moldyn->count;i++) {
353 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
355 v=sigma*rand_get_gauss(random);
357 p_total.x+=atom[i].mass*v;
359 v=sigma*rand_get_gauss(random);
361 p_total.y+=atom[i].mass*v;
363 v=sigma*rand_get_gauss(random);
365 p_total.z+=atom[i].mass*v;
368 /* zero total momentum */
369 v3_scale(&p_total,&p_total,1.0/moldyn->count);
370 for(i=0;i<moldyn->count;i++) {
371 v3_scale(&delta,&p_total,1.0/atom[i].mass);
372 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
375 /* velocity scaling */
376 scale_velocity(moldyn,equi_init);
381 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
391 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
394 /* get kinetic energy / temperature & count involved atoms */
397 for(i=0;i<moldyn->count;i++) {
398 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
399 e+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
403 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
404 else return 0; /* no atoms involved in scaling! */
406 /* (temporary) hack for e,t = 0 */
409 if(moldyn->t_ref!=0.0) {
410 thermal_init(moldyn,equi_init);
414 return 0; /* no scaling needed */
418 /* get scaling factor */
419 scale=moldyn->t_ref/moldyn->t;
423 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
424 scale=1.0+(scale-1.0)/moldyn->t_tc;
427 /* velocity scaling */
428 for(i=0;i<moldyn->count;i++) {
429 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
430 v3_scale(&(atom[i].v),&(atom[i].v),scale);
436 int scale_volume(t_moldyn *moldyn) {
446 vdim=&(moldyn->vis.dim);
449 for(i=0;i<moldyn->count;i++)
450 virial+=v3_norm(&(atom[i].virial));
452 printf("%f\n",virial);
453 /* get pressure from virial */
454 moldyn->p=moldyn->count*K_BOLTZMANN*moldyn->t-ONE_THIRD*virial;
455 moldyn->p/=moldyn->volume;
456 printf("%f\n",moldyn->p/(ATM));
459 if(moldyn->pt_scale&P_SCALE_BERENDSEN)
460 scale=3*sqrt(1-(moldyn->p_ref-moldyn->p)/moldyn->p_tc);
462 /* should actually never be used */
463 scale=pow(moldyn->p/moldyn->p_ref,1.0/3.0);
465 printf("scale = %f\n",scale);
470 if(vdim->x) vdim->x=dim->x;
471 if(vdim->y) vdim->y=dim->y;
472 if(vdim->z) vdim->z=dim->z;
473 moldyn->volume*=(scale*scale*scale);
475 /* check whether we need a new linkcell init */
476 if((dim->x/moldyn->cutoff!=lc->nx)||
477 (dim->y/moldyn->cutoff!=lc->ny)||
478 (dim->z/moldyn->cutoff!=lc->nx)) {
479 link_cell_shutdown(moldyn);
480 link_cell_init(moldyn);
487 double get_e_kin(t_moldyn *moldyn) {
495 for(i=0;i<moldyn->count;i++)
496 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
501 double get_e_pot(t_moldyn *moldyn) {
503 return moldyn->energy;
506 double update_e_kin(t_moldyn *moldyn) {
508 return(get_e_kin(moldyn));
511 double get_total_energy(t_moldyn *moldyn) {
513 return(moldyn->ekin+moldyn->energy);
516 t_3dvec get_total_p(t_moldyn *moldyn) {
525 for(i=0;i<moldyn->count;i++) {
526 v3_scale(&p,&(atom[i].v),atom[i].mass);
527 v3_add(&p_total,&p_total,&p);
533 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
537 /* nn_dist is the nearest neighbour distance */
539 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
548 /* linked list / cell method */
550 int link_cell_init(t_moldyn *moldyn) {
556 fd=open("/dev/null",O_WRONLY);
560 /* partitioning the md cell */
561 lc->nx=moldyn->dim.x/moldyn->cutoff;
562 lc->x=moldyn->dim.x/lc->nx;
563 lc->ny=moldyn->dim.y/moldyn->cutoff;
564 lc->y=moldyn->dim.y/lc->ny;
565 lc->nz=moldyn->dim.z/moldyn->cutoff;
566 lc->z=moldyn->dim.z/lc->nz;
568 lc->cells=lc->nx*lc->ny*lc->nz;
569 lc->subcell=malloc(lc->cells*sizeof(t_list));
571 printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
573 for(i=0;i<lc->cells;i++)
574 //list_init(&(lc->subcell[i]),1);
575 list_init(&(lc->subcell[i]),fd);
577 link_cell_update(moldyn);
582 int link_cell_update(t_moldyn *moldyn) {
596 for(i=0;i<lc->cells;i++)
597 list_destroy(&(moldyn->lc.subcell[i]));
599 for(count=0;count<moldyn->count;count++) {
600 i=(atom[count].r.x+(moldyn->dim.x/2))/lc->x;
601 j=(atom[count].r.y+(moldyn->dim.y/2))/lc->y;
602 k=(atom[count].r.z+(moldyn->dim.z/2))/lc->z;
603 list_add_immediate_ptr(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
610 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
628 cell[0]=lc->subcell[i+j*nx+k*a];
629 for(ci=-1;ci<=1;ci++) {
636 for(cj=-1;cj<=1;cj++) {
643 for(ck=-1;ck<=1;ck++) {
650 if(!(ci|cj|ck)) continue;
652 cell[--count2]=lc->subcell[x+y*nx+z*a];
655 cell[count1++]=lc->subcell[x+y*nx+z*a];
666 int link_cell_shutdown(t_moldyn *moldyn) {
673 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
674 list_shutdown(&(moldyn->lc.subcell[i]));
679 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
683 t_moldyn_schedule *schedule;
685 schedule=&(moldyn->schedule);
686 count=++(schedule->content_count);
688 ptr=realloc(moldyn->schedule.runs,count*sizeof(int));
690 perror("[moldyn] realloc (runs)");
693 moldyn->schedule.runs=ptr;
694 moldyn->schedule.runs[count-1]=runs;
696 ptr=realloc(schedule->tau,count*sizeof(double));
698 perror("[moldyn] realloc (tau)");
701 moldyn->schedule.tau=ptr;
702 moldyn->schedule.tau[count-1]=tau;
707 int moldyn_set_schedule_hook(t_moldyn *moldyn,void *hook,void *hook_params) {
709 moldyn->schedule.hook=hook;
710 moldyn->schedule.hook_params=hook_params;
717 * 'integration of newtons equation' - algorithms
721 /* start the integration */
723 int moldyn_integrate(t_moldyn *moldyn) {
726 unsigned int e,m,s,v;
728 t_moldyn_schedule *schedule;
734 schedule=&(moldyn->schedule);
737 /* initialize linked cell method */
738 link_cell_init(moldyn);
740 /* logging & visualization */
746 /* sqaure of some variables */
747 moldyn->tau_square=moldyn->tau*moldyn->tau;
748 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
750 /* calculate initial forces */
751 potential_force_calc(moldyn);
753 /* some stupid checks before we actually start calculating bullshit */
754 if(moldyn->cutoff>0.5*moldyn->dim.x)
755 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
756 if(moldyn->cutoff>0.5*moldyn->dim.y)
757 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
758 if(moldyn->cutoff>0.5*moldyn->dim.z)
759 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
760 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
761 if(ds>0.05*moldyn->nnd)
762 printf("[moldyn] warning: forces too high / tau too small!\n");
764 /* zero absolute time */
767 /* debugging, ignore */
770 /* executing the schedule */
771 for(sched=0;sched<moldyn->schedule.content_count;sched++) {
773 /* setting amount of runs and finite time step size */
774 moldyn->tau=schedule->tau[sched];
775 moldyn->tau_square=moldyn->tau*moldyn->tau;
776 moldyn->time_steps=schedule->runs[sched];
778 /* integration according to schedule */
780 for(i=0;i<moldyn->time_steps;i++) {
782 /* integration step */
783 moldyn->integrate(moldyn);
786 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
787 scale_velocity(moldyn,FALSE);
788 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
790 printf("going to do p scale ...\n");
791 scale_volume(moldyn);
795 /* check for log & visualization */
800 moldyn->time,update_e_kin(moldyn),
802 get_total_energy(moldyn));
806 p=get_total_p(moldyn);
808 "%f %f\n",moldyn->time,v3_norm(&p));
813 snprintf(dir,128,"%s/s-%07.f.save",
814 moldyn->vlsdir,moldyn->time);
815 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT);
816 if(fd<0) perror("[moldyn] save fd open");
818 write(fd,moldyn,sizeof(t_moldyn));
819 write(fd,moldyn->atom,
820 moldyn->count*sizeof(t_atom));
827 visual_atoms(&(moldyn->vis),moldyn->time,
828 moldyn->atom,moldyn->count);
829 printf("\rsched: %d, steps: %d, debug: %d",
830 sched,i,moldyn->debug);
835 /* increase absolute time */
836 moldyn->time+=moldyn->tau;
840 /* check for hooks */
842 schedule->hook(moldyn,schedule->hook_params);
844 /* get a new info line */
852 /* velocity verlet */
854 int velocity_verlet(t_moldyn *moldyn) {
857 double tau,tau_square;
864 tau_square=moldyn->tau_square;
866 for(i=0;i<count;i++) {
868 v3_scale(&delta,&(atom[i].v),tau);
869 v3_add(&(atom[i].r),&(atom[i].r),&delta);
870 v3_scale(&delta,&(atom[i].f),0.5*tau_square/atom[i].mass);
871 v3_add(&(atom[i].r),&(atom[i].r),&delta);
872 //if(i==5) printf("v: %f %f %f\n",atom[i].r.x,(atom[i].r.x+moldyn->dim.x/2)/moldyn->lc.x,2*atom[i].r.x/moldyn->dim.x);
873 check_per_bound(moldyn,&(atom[i].r));
874 //if(i==5) printf("n: %f %f %f\n",atom[i].r.x,(atom[i].r.x+moldyn->dim.x/2)/moldyn->lc.x,2*atom[i].r.x/moldyn->dim.x);
877 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
878 v3_add(&(atom[i].v),&(atom[i].v),&delta);
881 //moldyn_bc_check(moldyn);
882 /* neighbour list update */
883 link_cell_update(moldyn);
885 /* forces depending on chosen potential */
886 potential_force_calc(moldyn);
888 for(i=0;i<count;i++) {
889 /* again velocities */
890 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
891 v3_add(&(atom[i].v),&(atom[i].v),&delta);
900 * potentials & corresponding forces
904 /* generic potential and force calculation */
906 int potential_force_calc(t_moldyn *moldyn) {
909 t_atom *itom,*jtom,*ktom;
911 t_list neighbour_i[27];
912 t_list neighbour_i2[27];
924 /* get energy and force of every atom */
925 for(i=0;i<count;i++) {
928 v3_zero(&(itom[i].f));
930 /* reset viral of atom i */
931 v3_zero(&(itom[i].virial));
933 /* single particle potential/force */
934 if(itom[i].attr&ATOM_ATTR_1BP)
935 moldyn->func1b(moldyn,&(itom[i]));
937 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
940 /* 2 body pair potential/force */
942 link_cell_neighbour_index(moldyn,
943 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
944 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
945 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
952 this=&(neighbour_i[j]);
955 if(this->start==NULL)
961 jtom=this->current->data;
966 if((jtom->attr&ATOM_ATTR_2BP)&
967 (itom[i].attr&ATOM_ATTR_2BP)) {
968 moldyn->func2b(moldyn,
974 /* 3 body potential/force */
976 if(!(itom[i].attr&ATOM_ATTR_3BP)||
977 !(jtom->attr&ATOM_ATTR_3BP))
980 /* copy the neighbour lists */
981 memcpy(neighbour_i2,neighbour_i,
984 /* get neighbours of i */
987 that=&(neighbour_i2[k]);
990 if(that->start==NULL)
997 ktom=that->current->data;
999 if(!(ktom->attr&ATOM_ATTR_3BP))
1005 if(ktom==&(itom[i]))
1008 moldyn->func3b(moldyn,
1014 } while(list_next(that)!=\
1019 /* 2bp post function */
1020 if(moldyn->func2b_post) {
1021 moldyn->func2b_post(moldyn,
1026 } while(list_next(this)!=L_NO_NEXT_ELEMENT);
1036 * periodic boundayr checking
1039 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1050 if(moldyn->status&MOLDYN_STAT_PBX) {
1051 if(a->x>=x) a->x-=dim->x;
1052 else if(-a->x>x) a->x+=dim->x;
1054 if(moldyn->status&MOLDYN_STAT_PBY) {
1055 if(a->y>=y) a->y-=dim->y;
1056 else if(-a->y>y) a->y+=dim->y;
1058 if(moldyn->status&MOLDYN_STAT_PBZ) {
1059 if(a->z>=z) a->z-=dim->z;
1060 else if(-a->z>z) a->z+=dim->z;
1068 * example potentials
1071 /* harmonic oscillator potential and force */
1073 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1075 t_ho_params *params;
1076 t_3dvec force,distance;
1078 double sc,equi_dist;
1080 params=moldyn->pot2b_params;
1081 sc=params->spring_constant;
1082 equi_dist=params->equilibrium_distance;
1084 v3_sub(&distance,&(aj->r),&(ai->r));
1086 if(bc) check_per_bound(moldyn,&distance);
1087 d=v3_norm(&distance);
1088 if(d<=moldyn->cutoff) {
1089 /* energy is 1/2 (d-d0)^2, but we will add this twice ... */
1090 moldyn->energy+=(0.25*sc*(d-equi_dist)*(d-equi_dist));
1091 /* f = -grad E; grad r_ij = -1 1/r_ij distance */
1092 v3_scale(&force,&distance,sc*(1.0-(equi_dist/d)));
1093 v3_add(&(ai->f),&(ai->f),&force);
1099 /* lennard jones potential & force for one sort of atoms */
1101 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1103 t_lj_params *params;
1104 t_3dvec force,distance;
1106 double eps,sig6,sig12;
1108 params=moldyn->pot2b_params;
1109 eps=params->epsilon4;
1110 sig6=params->sigma6;
1111 sig12=params->sigma12;
1113 v3_sub(&distance,&(aj->r),&(ai->r));
1114 if(bc) check_per_bound(moldyn,&distance);
1115 d=v3_absolute_square(&distance); /* 1/r^2 */
1116 if(d<=moldyn->cutoff_square) {
1117 d=1.0/d; /* 1/r^2 */
1120 h1=h2*h2; /* 1/r^12 */
1121 /* energy is eps*..., but we will add this twice ... */
1122 moldyn->energy+=0.5*eps*(sig12*h1-sig6*h2);
1129 v3_scale(&force,&distance,-1.0*d); /* f = - grad E */
1130 v3_add(&(ai->f),&(ai->f),&force);
1137 * tersoff potential & force for 2 sorts of atoms
1140 /* create mixed terms from parameters and set them */
1141 int tersoff_mult_complete_params(t_tersoff_mult_params *p) {
1143 printf("[moldyn] tersoff parameter completion\n");
1144 p->Smixed=sqrt(p->S[0]*p->S[1]);
1145 p->Rmixed=sqrt(p->R[0]*p->R[1]);
1146 p->Amixed=sqrt(p->A[0]*p->A[1]);
1147 p->Bmixed=sqrt(p->B[0]*p->B[1]);
1148 p->lambda_m=0.5*(p->lambda[0]+p->lambda[1]);
1149 p->mu_m=0.5*(p->mu[0]+p->mu[1]);
1151 printf("[moldyn] tersoff mult parameter info:\n");
1152 printf(" S (A) | %f | %f | %f\n",p->S[0],p->S[1],p->Smixed);
1153 printf(" R (A) | %f | %f | %f\n",p->R[0],p->R[1],p->Rmixed);
1154 printf(" A (eV) | %f | %f | %f\n",p->A[0]/EV,p->A[1]/EV,p->Amixed/EV);
1155 printf(" B (eV) | %f | %f | %f\n",p->B[0]/EV,p->B[1]/EV,p->Bmixed/EV);
1156 printf(" lambda | %f | %f | %f\n",p->lambda[0],p->lambda[1],
1158 printf(" mu | %f | %f | %f\n",p->mu[0],p->mu[1],p->mu_m);
1159 printf(" beta | %.10f | %.10f\n",p->beta[0],p->beta[1]);
1160 printf(" n | %f | %f\n",p->n[0],p->n[1]);
1161 printf(" c | %f | %f\n",p->c[0],p->c[1]);
1162 printf(" d | %f | %f\n",p->d[0],p->d[1]);
1163 printf(" h | %f | %f\n",p->h[0],p->h[1]);
1164 printf(" chi | %f \n",p->chi);
1169 /* tersoff 1 body part */
1170 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
1173 t_tersoff_mult_params *params;
1174 t_tersoff_exchange *exchange;
1177 params=moldyn->pot1b_params;
1178 exchange=&(params->exchange);
1181 * simple: point constant parameters only depending on atom i to
1182 * their right values
1185 exchange->beta_i=&(params->beta[num]);
1186 exchange->n_i=&(params->n[num]);
1187 exchange->c_i=&(params->c[num]);
1188 exchange->d_i=&(params->d[num]);
1189 exchange->h_i=&(params->h[num]);
1191 exchange->betaini=pow(*(exchange->beta_i),*(exchange->n_i));
1192 exchange->ci2=params->c[num]*params->c[num];
1193 exchange->di2=params->d[num]*params->d[num];
1194 exchange->ci2di2=exchange->ci2/exchange->di2;
1199 /* tersoff 2 body part */
1200 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1202 t_tersoff_mult_params *params;
1203 t_tersoff_exchange *exchange;
1204 t_3dvec dist_ij,force;
1206 double A,B,R,S,lambda,mu;
1213 params=moldyn->pot2b_params;
1215 exchange=&(params->exchange);
1217 /* clear 3bp and 2bp post run */
1219 exchange->run2bp_post=0;
1221 /* reset S > r > R mark */
1222 exchange->d_ij_between_rs=0;
1225 * calc of 2bp contribution of V_ij and dV_ij/ji
1227 * for Vij and dV_ij we need:
1231 * for dV_ji we need:
1232 * - f_c_ji = f_c_ij, df_c_ji = df_c_ij
1233 * - f_r_ji = f_r_ij; df_r_ji = df_r_ij
1238 v3_sub(&dist_ij,&(aj->r),&(ai->r));
1239 if(bc) check_per_bound(moldyn,&dist_ij);
1240 d_ij=v3_norm(&dist_ij);
1242 /* save for use in 3bp */
1243 exchange->d_ij=d_ij;
1244 exchange->dist_ij=dist_ij;
1252 lambda=params->lambda[num];
1261 lambda=params->lambda_m;
1263 params->exchange.chi=params->chi;
1266 /* if d_ij > S => no force & potential energy contribution */
1270 /* more constants */
1271 exchange->beta_j=&(params->beta[num]);
1272 exchange->n_j=&(params->n[num]);
1273 exchange->c_j=&(params->c[num]);
1274 exchange->d_j=&(params->d[num]);
1275 exchange->h_j=&(params->h[num]);
1277 exchange->betajnj=exchange->betaini;
1278 exchange->cj2=exchange->ci2;
1279 exchange->dj2=exchange->di2;
1280 exchange->cj2dj2=exchange->ci2di2;
1283 exchange->betajnj=pow(*(exchange->beta_j),*(exchange->n_j));
1284 exchange->cj2=params->c[num]*params->c[num];
1285 exchange->dj2=params->d[num]*params->d[num];
1286 exchange->cj2dj2=exchange->cj2/exchange->dj2;
1289 /* f_r_ij = f_r_ji, df_r_ij = df_r_ji */
1290 f_r=A*exp(-lambda*d_ij);
1291 df_r=lambda*f_r/d_ij;
1293 /* f_a, df_a calc (again, same for ij and ji) | save for later use! */
1294 exchange->f_a=-B*exp(-mu*d_ij);
1295 exchange->df_a=mu*exchange->f_a/d_ij;
1297 /* f_c, df_c calc (again, same for ij and ji) */
1299 /* f_c = 1, df_c = 0 */
1302 /* two body contribution (ij, ji) */
1303 v3_scale(&force,&dist_ij,-df_r);
1307 arg=M_PI*(d_ij-R)/s_r;
1308 f_c=0.5+0.5*cos(arg);
1309 //df_c=-0.5*sin(arg)*(M_PI/(s_r*d_ij)); /* MARK! */
1310 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
1311 /* two body contribution (ij, ji) */
1312 v3_scale(&force,&dist_ij,-df_c*f_r-df_r*f_c);
1313 /* tell 3bp that S > r > R */
1314 exchange->d_ij_between_rs=1;
1317 /* add forces of 2bp (ij, ji) contribution
1318 * dVij = dVji and we sum up both: no 1/2) */
1319 v3_add(&(ai->f),&(ai->f),&force);
1321 /* energy 2bp contribution (ij, ji) is 0.5 f_r f_c ... */
1322 moldyn->energy+=(0.5*f_r*f_c);
1324 /* save for use in 3bp */
1326 exchange->df_c=df_c;
1328 /* enable the run of 3bp function and 2bp post processing */
1330 exchange->run2bp_post=1;
1332 /* reset 3bp sums */
1333 exchange->zeta_ij=0.0;
1334 exchange->zeta_ji=0.0;
1335 v3_zero(&(exchange->dzeta_ij));
1336 v3_zero(&(exchange->dzeta_ji));
1341 /* tersoff 2 body post part */
1343 int tersoff_mult_post_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1346 * here we have to allow for the 3bp sums
1349 * - zeta_ij, dzeta_ij
1350 * - zeta_ji, dzeta_ji
1352 * to compute the 3bp contribution to:
1358 t_tersoff_mult_params *params;
1359 t_tersoff_exchange *exchange;
1364 double f_c,df_c,f_a,df_a;
1365 double chi,ni,betaini,nj,betajnj;
1368 params=moldyn->pot2b_params;
1369 exchange=&(params->exchange);
1371 /* we do not run if f_c_ij was detected to be 0! */
1372 if(!(exchange->run2bp_post))
1376 df_c=exchange->df_c;
1378 df_a=exchange->df_a;
1379 betaini=exchange->betaini;
1380 betajnj=exchange->betajnj;
1381 ni=*(exchange->n_i);
1382 nj=*(exchange->n_j);
1384 dist_ij=&(exchange->dist_ij);
1387 zeta=exchange->zeta_ij;
1389 moldyn->debug++; /* just for debugging ... */
1392 v3_scale(&force,dist_ij,df_a*b*f_c);
1395 tmp=betaini*pow(zeta,ni-1.0); /* beta^n * zeta^n-1 */
1396 b=(1+zeta*tmp); /* 1 + beta^n zeta^n */
1397 db=chi*pow(b,-1.0/(2*ni)-1); /* x(...)^(-1/2n - 1) */
1399 db*=-0.5*tmp; /* db_ij */
1400 v3_scale(&force,&(exchange->dzeta_ij),f_a*db);
1401 v3_scale(&temp,dist_ij,df_a*b);
1402 v3_add(&force,&force,&temp);
1403 v3_scale(&force,&force,f_c);
1405 v3_scale(&temp,dist_ij,df_c*b*f_a);
1406 v3_add(&force,&force,&temp);
1407 v3_scale(&force,&force,-0.5);
1410 v3_add(&(ai->f),&(ai->f),&force);
1412 /* add energy of 3bp sum */
1413 moldyn->energy+=(0.5*f_c*b*f_a);
1416 zeta=exchange->zeta_ji;
1420 v3_scale(&force,dist_ij,df_a*b*f_c);
1423 tmp=betajnj*pow(zeta,nj-1.0); /* beta^n * zeta^n-1 */
1424 b=(1+zeta*tmp); /* 1 + beta^n zeta^n */
1425 db=chi*pow(b,-1.0/(2*nj)-1); /* x(...)^(-1/2n - 1) */
1427 db*=-0.5*tmp; /* db_ij */
1428 v3_scale(&force,&(exchange->dzeta_ji),f_a*db);
1429 v3_scale(&temp,dist_ij,df_a*b);
1430 v3_add(&force,&force,&temp);
1431 v3_scale(&force,&force,f_c);
1433 v3_scale(&temp,dist_ij,df_c*b*f_a);
1434 v3_add(&force,&force,&temp);
1435 v3_scale(&force,&force,-0.5);
1438 v3_add(&(ai->f),&(ai->f),&force);
1443 /* tersoff 3 body part */
1445 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1447 t_tersoff_mult_params *params;
1448 t_tersoff_exchange *exchange;
1449 t_3dvec dist_ij,dist_ik,dist_jk;
1450 t_3dvec temp1,temp2;
1454 double d_ij,d_ik,d_jk;
1457 double f_c_ik,df_c_ik,arg;
1461 double cos_theta,d_costheta1,d_costheta2;
1462 double h_cos,d2_h_cos2;
1463 double frac,g,zeta,chi;
1467 params=moldyn->pot3b_params;
1468 exchange=&(params->exchange);
1470 if(!(exchange->run3bp))
1474 * calc of 3bp contribution of V_ij and dV_ij/ji/jk &
1475 * 2bp contribution of dV_jk
1477 * for Vij and dV_ij we still need:
1478 * - b_ij, db_ij (zeta_ij)
1479 * - f_c_ik, df_c_ik, constants_i, cos_theta_ijk, d_costheta_ijk
1481 * for dV_ji we still need:
1482 * - b_ji, db_ji (zeta_ji)
1483 * - f_c_jk, d_c_jk, constants_j, cos_theta_jik, d_costheta_jik
1485 * for dV_jk we need:
1489 * - f_c_ji, df_c_ji, constants_j, cos_theta_jki, d_costheta_jki
1497 /* dist_ij, d_ij - this is < S_ij ! */
1498 dist_ij=exchange->dist_ij;
1499 d_ij=exchange->d_ij;
1501 /* f_c_ij, df_c_ij (same for ji) */
1503 df_c=exchange->df_c;
1506 * calculate unknown values now ...
1509 /* V_ij and dV_ij stuff (in b_ij there is f_c_ik) */
1512 v3_sub(&dist_ik,&(ak->r),&(ai->r));
1513 if(bc) check_per_bound(moldyn,&dist_ik);
1514 d_ik=v3_norm(&dist_ik);
1527 /* zeta_ij/dzeta_ij contribution only for d_ik < S */
1530 /* get constants_i from exchange data */
1537 c2d2=exchange->ci2di2;
1539 /* cosine of theta_ijk by scalaproduct */
1540 rr=v3_scalar_product(&dist_ij,&dist_ik);
1546 d_costheta1=cos_theta/(d_ij*d_ij)-tmp;
1547 d_costheta2=cos_theta/(d_ik*d_ik)-tmp;
1549 /* some usefull values */
1550 h_cos=(h-cos_theta);
1551 d2_h_cos2=d2+(h_cos*h_cos);
1552 frac=c2/(d2_h_cos2);
1557 /* d_costheta_ij and dg(cos_theta) - needed in any case! */
1558 v3_scale(&temp1,&dist_ij,d_costheta1);
1559 v3_scale(&temp2,&dist_ik,d_costheta2);
1560 v3_add(&temp1,&temp1,&temp2);
1561 v3_scale(&temp1,&temp1,-2.0*frac*h_cos/d2_h_cos2); /* dg */
1563 /* f_c_ik & df_c_ik + {d,}zeta contribution */
1564 dzeta=&(exchange->dzeta_ij);
1568 // => df_c_ik=0.0; of course we do not set this!
1571 exchange->zeta_ij+=g;
1574 v3_add(dzeta,dzeta,&temp1);
1579 arg=M_PI*(d_ik-R)/s_r;
1580 f_c_ik=0.5+0.5*cos(arg);
1581 //df_c_ik=-0.5*sin(arg)*(M_PI/(s_r*d_ik)); /* MARK */
1582 df_c_ik=0.5*sin(arg)*(M_PI/(s_r*d_ik));
1585 exchange->zeta_ij+=f_c_ik*g;
1588 v3_scale(&temp1,&temp1,f_c_ik);
1589 v3_scale(&temp2,&dist_ik,g*df_c_ik);
1590 v3_add(&temp1,&temp1,&temp2);
1591 v3_add(dzeta,dzeta,&temp1);
1595 /* dV_ji stuff (in b_ji there is f_c_jk) + dV_jk stuff! */
1598 v3_sub(&dist_jk,&(ak->r),&(aj->r));
1599 if(bc) check_per_bound(moldyn,&dist_jk);
1600 d_jk=v3_norm(&dist_jk);
1619 /* zeta_ji/dzeta_ji contribution only for d_jk < S_jk */
1622 /* constants_j from exchange data */
1629 c2d2=exchange->cj2dj2;
1631 /* cosine of theta_jik by scalaproduct */
1632 rr=-v3_scalar_product(&dist_ij,&dist_jk); /* -1, as ij -> ji */
1638 d_costheta2=cos_theta/(d_ij*d_ij);
1640 /* some usefull values */
1641 h_cos=(h-cos_theta);
1642 d2_h_cos2=d2+(h_cos*h_cos);
1643 frac=c2/(d2_h_cos2);
1648 /* d_costheta_ij and dg(cos_theta) - needed in any case! */
1649 v3_scale(&temp1,&dist_jk,d_costheta1);
1650 v3_scale(&temp2,&dist_ij,-d_costheta2); /* ji -> ij => -1 */
1651 v3_add(&temp1,&temp1,&temp2);
1652 v3_scale(&temp1,&temp1,-2.0*frac*h_cos/d2_h_cos2); /* dg */
1654 /* store dg in temp2 and use it for dVjk later */
1655 v3_copy(&temp2,&temp1);
1657 /* f_c_jk + {d,}zeta contribution (df_c_jk = 0) */
1658 dzeta=&(exchange->dzeta_ji);
1664 exchange->zeta_ji+=g;
1667 v3_add(dzeta,dzeta,&temp1);
1672 arg=M_PI*(d_jk-R)/s_r;
1673 f_c_jk=0.5+0.5*cos(arg);
1676 exchange->zeta_ji+=f_c_jk*g;
1679 v3_scale(&temp1,&temp1,f_c_jk);
1680 v3_add(dzeta,dzeta,&temp1);
1683 /* dV_jk stuff | add force contribution on atom i immediately */
1684 if(exchange->d_ij_between_rs) {
1686 v3_scale(&temp1,&temp2,f_c);
1687 v3_scale(&temp2,&dist_ij,df_c*g);
1688 v3_add(&temp2,&temp2,&temp1); /* -> dzeta_jk in temp2 */
1692 // dzeta_jk is simply dg, which is stored in temp2
1694 /* betajnj * zeta_jk ^ nj-1 */
1695 tmp=exchange->betajnj*pow(zeta,(n-1.0));
1696 tmp=-chi/2.0*pow((1+tmp*zeta),(-1.0/(2.0*n)-1))*tmp;
1697 v3_scale(&temp2,&temp2,tmp*B*exp(-mu*d_jk)*f_c_jk*0.5);
1698 v3_add(&(ai->f),&(ai->f),&temp2); /* -1 skipped in f_a calc ^ */
1699 /* scaled with 0.5 ^ */
1708 * debugging / critical check functions
1711 int moldyn_bc_check(t_moldyn *moldyn) {
1724 for(i=0;i<moldyn->count;i++) {
1725 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
1726 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
1727 i,atom[i].r.x,dim->x/2);
1728 printf("diagnostic:\n");
1729 printf("-----------\natom.r.x:\n");
1731 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
1734 ((byte)&(1<<k))?1:0,
1737 printf("---------------\nx=dim.x/2:\n");
1739 memcpy(&byte,(u8 *)(&x)+j,1);
1742 ((byte)&(1<<k))?1:0,
1745 if(atom[i].r.x==x) printf("the same!\n");
1746 else printf("different!\n");
1748 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
1749 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
1750 i,atom[i].r.y,dim->y/2);
1751 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
1752 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
1753 i,atom[i].r.z,dim->z/2);