2 * moldyn.c - molecular dynamics library main file
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
12 #include <sys/types.h>
20 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
22 printf("[moldyn] init\n");
24 memset(moldyn,0,sizeof(t_moldyn));
26 rand_init(&(moldyn->random),NULL,1);
27 moldyn->random.status|=RAND_STAT_VERBOSE;
32 int moldyn_shutdown(t_moldyn *moldyn) {
34 printf("[moldyn] shutdown\n");
36 moldyn_log_shutdown(moldyn);
37 link_cell_shutdown(moldyn);
38 rand_close(&(moldyn->random));
44 int set_int_alg(t_moldyn *moldyn,u8 algo) {
46 printf("[moldyn] integration algorithm: ");
49 case MOLDYN_INTEGRATE_VERLET:
50 moldyn->integrate=velocity_verlet;
51 printf("velocity verlet\n");
54 printf("unknown integration algorithm: %02x\n",algo);
62 int set_cutoff(t_moldyn *moldyn,double cutoff) {
64 moldyn->cutoff=cutoff;
66 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
71 int set_temperature(t_moldyn *moldyn,double t_ref) {
75 printf("[moldyn] temperature: %f\n",moldyn->t_ref);
80 int set_pressure(t_moldyn *moldyn,double p_ref) {
84 printf("[moldyn] pressure: %f\n",moldyn->p_ref);
89 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
91 moldyn->pt_scale=(ptype|ttype);
95 printf("[moldyn] p/t scaling:\n");
97 printf(" p: %s",ptype?"yes":"no ");
99 printf(" | type: %02x | factor: %f",ptype,ptc);
102 printf(" t: %s",ttype?"yes":"no ");
104 printf(" | type: %02x | factor: %f",ttype,ttc);
110 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
116 moldyn->volume=x*y*z;
124 moldyn->dv=0.0001*moldyn->volume;
126 printf("[moldyn] dimensions in A and A^3 respectively:\n");
127 printf(" x: %f\n",moldyn->dim.x);
128 printf(" y: %f\n",moldyn->dim.y);
129 printf(" z: %f\n",moldyn->dim.z);
130 printf(" volume: %f\n",moldyn->volume);
131 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
132 printf(" delta volume (pressure calc): %f\n",moldyn->dv);
137 int set_nn_dist(t_moldyn *moldyn,double dist) {
144 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
146 printf("[moldyn] periodic boundary conditions:\n");
149 moldyn->status|=MOLDYN_STAT_PBX;
152 moldyn->status|=MOLDYN_STAT_PBY;
155 moldyn->status|=MOLDYN_STAT_PBZ;
157 printf(" x: %s\n",x?"yes":"no");
158 printf(" y: %s\n",y?"yes":"no");
159 printf(" z: %s\n",z?"yes":"no");
164 int set_potential1b(t_moldyn *moldyn,pf_func1b func,void *params) {
167 moldyn->pot1b_params=params;
172 int set_potential2b(t_moldyn *moldyn,pf_func2b func,void *params) {
175 moldyn->pot2b_params=params;
180 int set_potential2b_post(t_moldyn *moldyn,pf_func2b_post func,void *params) {
182 moldyn->func2b_post=func;
183 moldyn->pot2b_params=params;
188 int set_potential3b(t_moldyn *moldyn,pf_func3b func,void *params) {
191 moldyn->pot3b_params=params;
196 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
198 strncpy(moldyn->vlsdir,dir,127);
203 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
208 printf("[moldyn] set log: ");
211 case LOG_TOTAL_ENERGY:
212 moldyn->ewrite=timer;
213 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
214 moldyn->efd=open(filename,
215 O_WRONLY|O_CREAT|O_EXCL,
218 perror("[moldyn] energy log fd open");
221 dprintf(moldyn->efd,"# total energy log file\n");
222 printf("total energy (%d)\n",timer);
224 case LOG_TOTAL_MOMENTUM:
225 moldyn->mwrite=timer;
226 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
227 moldyn->mfd=open(filename,
228 O_WRONLY|O_CREAT|O_EXCL,
231 perror("[moldyn] momentum log fd open");
234 dprintf(moldyn->efd,"# total momentum log file\n");
235 printf("total momentum (%d)\n",timer);
238 moldyn->swrite=timer;
239 printf("save file (%d)\n",timer);
242 moldyn->vwrite=timer;
243 ret=visual_init(&(moldyn->vis),moldyn->vlsdir);
245 printf("[moldyn] visual init failure\n");
248 printf("visual file (%d)\n",timer);
251 printf("unknown log type: %02x\n",type);
258 int moldyn_log_shutdown(t_moldyn *moldyn) {
260 printf("[moldyn] log shutdown\n");
261 if(moldyn->efd) close(moldyn->efd);
262 if(moldyn->mfd) close(moldyn->mfd);
263 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
269 * creating lattice functions
272 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
273 u8 attr,u8 brand,int a,int b,int c) {
284 /* how many atoms do we expect */
285 if(type==CUBIC) new*=1;
286 if(type==FCC) new*=4;
287 if(type==DIAMOND) new*=8;
289 /* allocate space for atoms */
290 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
292 perror("[moldyn] realloc (create lattice)");
296 atom=&(moldyn->atom[count]);
302 ret=cubic_init(a,b,c,atom,&origin);
305 ret=fcc_init(a,b,c,lc,atom,&origin);
308 ret=diamond_init(a,b,c,lc,atom,&origin);
311 printf("unknown lattice type (%02x)\n",type);
317 printf("[moldyn] creating lattice failed\n");
318 printf(" amount of atoms\n");
319 printf(" - expected: %d\n",new);
320 printf(" - created: %d\n",ret);
325 printf("[moldyn] created lattice with %d atoms\n",new);
327 for(ret=0;ret<new;ret++) {
328 atom[ret].element=element;
331 atom[ret].brand=brand;
332 atom[ret].tag=count+ret;
333 check_per_bound(moldyn,&(atom[ret].r));
340 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
348 /* fcc lattice init */
349 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
362 if(origin) v3_copy(&o,origin);
365 /* construct the basis */
368 if(i!=j) help[j]=0.5*lc;
371 v3_set(&basis[i],help);
377 /* fill up the room */
384 v3_copy(&(atom[count].r),&r);
385 atom[count].element=1;
388 v3_add(&n,&r,&basis[i]);
392 v3_copy(&(atom[count].r),&n);
403 /* coordinate transformation */
409 v3_sub(&(atom[i].r),&(atom[i].r),&n);
414 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
419 count=fcc_init(a,b,c,lc,atom,origin);
425 if(origin) v3_add(&o,&o,origin);
427 count+=fcc_init(a,b,c,lc,&atom[count],&o);
432 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
433 t_3dvec *r,t_3dvec *v) {
440 count=(moldyn->count)++;
442 ptr=realloc(atom,(count+1)*sizeof(t_atom));
444 perror("[moldyn] realloc (add atom)");
452 atom[count].element=element;
453 atom[count].mass=mass;
454 atom[count].brand=brand;
455 atom[count].tag=count;
456 atom[count].attr=attr;
461 int destroy_atoms(t_moldyn *moldyn) {
463 if(moldyn->atom) free(moldyn->atom);
468 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
471 * - gaussian distribution of velocities
472 * - zero total momentum
473 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
478 t_3dvec p_total,delta;
483 random=&(moldyn->random);
485 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
487 /* gaussian distribution of velocities */
489 for(i=0;i<moldyn->count;i++) {
490 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
492 v=sigma*rand_get_gauss(random);
494 p_total.x+=atom[i].mass*v;
496 v=sigma*rand_get_gauss(random);
498 p_total.y+=atom[i].mass*v;
500 v=sigma*rand_get_gauss(random);
502 p_total.z+=atom[i].mass*v;
505 /* zero total momentum */
506 v3_scale(&p_total,&p_total,1.0/moldyn->count);
507 for(i=0;i<moldyn->count;i++) {
508 v3_scale(&delta,&p_total,1.0/atom[i].mass);
509 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
512 /* velocity scaling */
513 scale_velocity(moldyn,equi_init);
518 double temperature_calc(t_moldyn *moldyn) {
527 for(i=0;i<moldyn->count;i++)
528 double_ekin+=atom[i].mass*v3_absolute_square(&(atom[i].v));
530 /* kinetic energy = 3/2 N k_B T */
531 moldyn->t=double_ekin/(3.0*K_BOLTZMANN*moldyn->count);
536 double get_temperature(t_moldyn *moldyn) {
541 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
551 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
554 /* get kinetic energy / temperature & count involved atoms */
557 for(i=0;i<moldyn->count;i++) {
558 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
559 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
564 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
565 else return 0; /* no atoms involved in scaling! */
567 /* (temporary) hack for e,t = 0 */
570 if(moldyn->t_ref!=0.0) {
571 thermal_init(moldyn,equi_init);
575 return 0; /* no scaling needed */
579 /* get scaling factor */
580 scale=moldyn->t_ref/moldyn->t;
584 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
585 scale=1.0+(scale-1.0)/moldyn->t_tc;
588 /* velocity scaling */
589 for(i=0;i<moldyn->count;i++) {
590 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
591 v3_scale(&(atom[i].v),&(atom[i].v),scale);
597 double ideal_gas_law_pressure(t_moldyn *moldyn) {
601 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
602 printf("temp = %f => ideal gas law pressure: %f\n",moldyn->t,p/ATM);
607 double pressure_calc(t_moldyn *moldyn) {
615 for(i=0;i<moldyn->count;i++) {
616 virial=&(moldyn->atom[i].virial);
617 v+=(virial->xx+virial->yy+virial->zz);
620 h=moldyn->count*K_BOLTZMANN*moldyn->t;
625 p1=(moldyn->count*K_BOLTZMANN*moldyn->t-ONE_THIRD*moldyn->vt1);
628 printf("debug: vt1=%f v=%f nkt=%f\n",moldyn->vt1,v,h);
630 printf("compare pressures: %f %f %f\n",p1/ATM,p/ATM,h/moldyn->volume/ATM);
635 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
643 store=malloc(moldyn->count*sizeof(t_atom));
645 printf("[moldyn] allocating store mem failed\n");
649 /* save unscaled potential energy + atom/dim configuration */
651 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
654 /* derivative with respect to x direction */
655 scale=1.0+moldyn->dv/(moldyn->dim.y*moldyn->dim.z);
656 scale_dim(moldyn,scale,TRUE,0,0);
657 scale_atoms(moldyn,scale,TRUE,0,0);
658 potential_force_calc(moldyn);
659 tp->x=(moldyn->energy-u)/moldyn->dv;
662 /* restore atomic configuration + dim */
663 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
666 /* derivative with respect to y direction */
667 scale=1.0+moldyn->dv/(moldyn->dim.x*moldyn->dim.z);
668 scale_dim(moldyn,scale,0,TRUE,0);
669 scale_atoms(moldyn,scale,0,TRUE,0);
670 potential_force_calc(moldyn);
671 tp->y=(moldyn->energy-u)/moldyn->dv;
674 /* restore atomic configuration + dim */
675 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
678 /* derivative with respect to z direction */
679 scale=1.0+moldyn->dv/(moldyn->dim.x*moldyn->dim.y);
680 scale_dim(moldyn,scale,0,0,TRUE);
681 scale_atoms(moldyn,scale,0,0,TRUE);
682 potential_force_calc(moldyn);
683 tp->z=(moldyn->energy-u)/moldyn->dv;
686 /* restore atomic configuration + dim */
687 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
690 printf("dU/dV komp addiert = %f\n",(tp->x+tp->y+tp->z)/ATM);
692 scale=1.0+pow(moldyn->dv/moldyn->volume,ONE_THIRD);
694 scale_dim(moldyn,scale,1,1,1);
695 scale_dim(moldyn,scale,1,1,1);
696 potential_force_calc(moldyn);
698 printf("dU/dV einfach = %f\n",(moldyn->energy-u)/moldyn->dv/ATM);
700 /* restore atomic configuration + dim */
701 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
710 double get_pressure(t_moldyn *moldyn) {
716 int scale_dim(t_moldyn *moldyn,double scale,u8 x,u8 y,u8 z) {
729 int scale_atoms(t_moldyn *moldyn,double scale,u8 x,u8 y,u8 z) {
734 for(i=0;i<moldyn->count;i++) {
735 r=&(moldyn->atom[i].r);
744 int scale_volume(t_moldyn *moldyn) {
755 vdim=&(moldyn->vis.dim);
758 memset(&virial,0,sizeof(t_virial));
760 for(i=0;i<moldyn->count;i++) {
761 virial.xx+=atom[i].virial.xx;
762 virial.yy+=atom[i].virial.yy;
763 virial.zz+=atom[i].virial.zz;
764 virial.xy+=atom[i].virial.xy;
765 virial.xz+=atom[i].virial.xz;
766 virial.yz+=atom[i].virial.yz;
769 /* just a guess so far ... */
770 v=virial.xx+virial.yy+virial.zz;
773 /* get pressure from virial */
774 moldyn->p=moldyn->count*K_BOLTZMANN*moldyn->t+ONE_THIRD*v;
775 moldyn->p/=moldyn->volume;
776 printf("%f | %f\n",moldyn->p/(ATM),moldyn->p_ref/ATM);
779 if(moldyn->pt_scale&P_SCALE_BERENDSEN)
780 scale=3*sqrt(1-(moldyn->p_ref-moldyn->p)/moldyn->p_tc);
782 /* should actually never be used */
783 scale=pow(moldyn->p/moldyn->p_ref,1.0/3.0);
785 printf("scale = %f\n",scale);
790 if(vdim->x) vdim->x=dim->x;
791 if(vdim->y) vdim->y=dim->y;
792 if(vdim->z) vdim->z=dim->z;
793 moldyn->volume*=(scale*scale*scale);
795 /* check whether we need a new linkcell init */
796 if((dim->x/moldyn->cutoff!=lc->nx)||
797 (dim->y/moldyn->cutoff!=lc->ny)||
798 (dim->z/moldyn->cutoff!=lc->nx)) {
799 link_cell_shutdown(moldyn);
800 link_cell_init(moldyn);
807 double get_e_kin(t_moldyn *moldyn) {
815 for(i=0;i<moldyn->count;i++)
816 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
821 double update_e_kin(t_moldyn *moldyn) {
823 return(get_e_kin(moldyn));
826 double get_total_energy(t_moldyn *moldyn) {
828 return(moldyn->ekin+moldyn->energy);
831 t_3dvec get_total_p(t_moldyn *moldyn) {
840 for(i=0;i<moldyn->count;i++) {
841 v3_scale(&p,&(atom[i].v),atom[i].mass);
842 v3_add(&p_total,&p_total,&p);
848 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
852 /* nn_dist is the nearest neighbour distance */
854 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
863 /* linked list / cell method */
865 int link_cell_init(t_moldyn *moldyn) {
872 /* partitioning the md cell */
873 lc->nx=moldyn->dim.x/moldyn->cutoff;
874 lc->x=moldyn->dim.x/lc->nx;
875 lc->ny=moldyn->dim.y/moldyn->cutoff;
876 lc->y=moldyn->dim.y/lc->ny;
877 lc->nz=moldyn->dim.z/moldyn->cutoff;
878 lc->z=moldyn->dim.z/lc->nz;
880 lc->cells=lc->nx*lc->ny*lc->nz;
881 lc->subcell=malloc(lc->cells*sizeof(t_list));
884 printf("[moldyn] FATAL: less then 27 subcells!\n");
886 printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
888 for(i=0;i<lc->cells;i++)
889 list_init_f(&(lc->subcell[i]));
891 link_cell_update(moldyn);
896 int link_cell_update(t_moldyn *moldyn) {
914 for(i=0;i<lc->cells;i++)
915 list_destroy_f(&(lc->subcell[i]));
917 for(count=0;count<moldyn->count;count++) {
918 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
919 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
920 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
921 list_add_immediate_f(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
928 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
946 cell[0]=lc->subcell[i+j*nx+k*a];
947 for(ci=-1;ci<=1;ci++) {
954 for(cj=-1;cj<=1;cj++) {
961 for(ck=-1;ck<=1;ck++) {
968 if(!(ci|cj|ck)) continue;
970 cell[--count2]=lc->subcell[x+y*nx+z*a];
973 cell[count1++]=lc->subcell[x+y*nx+z*a];
984 int link_cell_shutdown(t_moldyn *moldyn) {
991 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
992 list_destroy_f(&(moldyn->lc.subcell[i]));
999 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1003 t_moldyn_schedule *schedule;
1005 schedule=&(moldyn->schedule);
1006 count=++(schedule->total_sched);
1008 ptr=realloc(schedule->runs,count*sizeof(int));
1010 perror("[moldyn] realloc (runs)");
1014 schedule->runs[count-1]=runs;
1016 ptr=realloc(schedule->tau,count*sizeof(double));
1018 perror("[moldyn] realloc (tau)");
1022 schedule->tau[count-1]=tau;
1024 printf("[moldyn] schedule added:\n");
1025 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1031 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1033 moldyn->schedule.hook=hook;
1034 moldyn->schedule.hook_params=hook_params;
1041 * 'integration of newtons equation' - algorithms
1045 /* start the integration */
1047 int moldyn_integrate(t_moldyn *moldyn) {
1050 unsigned int e,m,s,v;
1052 t_moldyn_schedule *sched;
1058 sched=&(moldyn->schedule);
1061 /* initialize linked cell method */
1062 link_cell_init(moldyn);
1064 /* logging & visualization */
1070 /* sqaure of some variables */
1071 moldyn->tau_square=moldyn->tau*moldyn->tau;
1072 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
1074 /* calculate initial forces */
1075 potential_force_calc(moldyn);
1077 /* some stupid checks before we actually start calculating bullshit */
1078 if(moldyn->cutoff>0.5*moldyn->dim.x)
1079 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
1080 if(moldyn->cutoff>0.5*moldyn->dim.y)
1081 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
1082 if(moldyn->cutoff>0.5*moldyn->dim.z)
1083 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
1084 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1085 if(ds>0.05*moldyn->nnd)
1086 printf("[moldyn] warning: forces too high / tau too small!\n");
1088 /* zero absolute time */
1091 /* debugging, ignore */
1094 /* tell the world */
1095 printf("[moldyn] integration start, go get a coffee ...\n");
1097 /* executing the schedule */
1098 for(sched->count=0;sched->count<sched->total_sched;sched->count++) {
1100 /* setting amount of runs and finite time step size */
1101 moldyn->tau=sched->tau[sched->count];
1102 moldyn->tau_square=moldyn->tau*moldyn->tau;
1103 moldyn->time_steps=sched->runs[sched->count];
1105 /* integration according to schedule */
1107 for(i=0;i<moldyn->time_steps;i++) {
1109 /* integration step */
1110 moldyn->integrate(moldyn);
1113 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1114 scale_velocity(moldyn,FALSE);
1115 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1116 scale_volume(moldyn);
1118 printf("-> %f\n",thermodynamic_pressure_calc(moldyn)/ATM);
1120 /* check for log & visualization */
1123 update_e_kin(moldyn);
1124 dprintf(moldyn->efd,
1126 moldyn->time,moldyn->ekin,
1128 get_total_energy(moldyn));
1132 p=get_total_p(moldyn);
1133 dprintf(moldyn->mfd,
1134 "%f %f\n",moldyn->time,v3_norm(&p));
1139 snprintf(dir,128,"%s/s-%07.f.save",
1140 moldyn->vlsdir,moldyn->time);
1141 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT);
1142 if(fd<0) perror("[moldyn] save fd open");
1144 write(fd,moldyn,sizeof(t_moldyn));
1145 write(fd,moldyn->atom,
1146 moldyn->count*sizeof(t_atom));
1153 visual_atoms(&(moldyn->vis),moldyn->time,
1154 moldyn->atom,moldyn->count);
1155 printf("\rsched: %d, steps: %d, debug: %d",
1156 sched->count,i,moldyn->debug);
1161 /* increase absolute time */
1162 moldyn->time+=moldyn->tau;
1166 /* check for hooks */
1168 sched->hook(moldyn,sched->hook_params);
1170 /* get a new info line */
1178 /* velocity verlet */
1180 int velocity_verlet(t_moldyn *moldyn) {
1183 double tau,tau_square,h;
1188 count=moldyn->count;
1190 tau_square=moldyn->tau_square;
1192 for(i=0;i<count;i++) {
1195 v3_scale(&delta,&(atom[i].v),tau);
1196 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1197 v3_scale(&delta,&(atom[i].f),h*tau_square);
1198 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1199 check_per_bound(moldyn,&(atom[i].r));
1201 /* velocities [actually v(t+tau/2)] */
1202 v3_scale(&delta,&(atom[i].f),h*tau);
1203 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1206 /* neighbour list update */
1207 link_cell_update(moldyn);
1209 /* forces depending on chosen potential */
1210 potential_force_calc(moldyn);
1212 for(i=0;i<count;i++) {
1213 /* again velocities [actually v(t+tau)] */
1214 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1215 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1224 * potentials & corresponding forces & virial routine
1228 /* generic potential and force calculation */
1230 int potential_force_calc(t_moldyn *moldyn) {
1233 t_atom *itom,*jtom,*ktom;
1236 t_list neighbour_i[27];
1237 t_list neighbour_i2[27];
1242 count=moldyn->count;
1252 /* reset force, site energy and virial of every atom */
1253 for(i=0;i<count;i++) {
1256 v3_zero(&(itom[i].f));
1259 virial=(&(itom[i].virial));
1267 /* reset site energy */
1272 /* get energy,force and virial of every atom */
1273 for(i=0;i<count;i++) {
1275 /* single particle potential/force */
1276 if(itom[i].attr&ATOM_ATTR_1BP)
1277 moldyn->func1b(moldyn,&(itom[i]));
1279 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1282 /* 2 body pair potential/force */
1284 link_cell_neighbour_index(moldyn,
1285 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1286 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1287 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1294 this=&(neighbour_i[j]);
1297 if(this->start==NULL)
1303 jtom=this->current->data;
1305 if(jtom==&(itom[i]))
1308 if((jtom->attr&ATOM_ATTR_2BP)&
1309 (itom[i].attr&ATOM_ATTR_2BP)) {
1310 moldyn->func2b(moldyn,
1316 /* 3 body potential/force */
1318 if(!(itom[i].attr&ATOM_ATTR_3BP)||
1319 !(jtom->attr&ATOM_ATTR_3BP))
1322 /* copy the neighbour lists */
1323 memcpy(neighbour_i2,neighbour_i,
1326 /* get neighbours of i */
1329 that=&(neighbour_i2[k]);
1332 if(that->start==NULL)
1339 ktom=that->current->data;
1341 if(!(ktom->attr&ATOM_ATTR_3BP))
1347 if(ktom==&(itom[i]))
1350 moldyn->func3b(moldyn,
1356 } while(list_next_f(that)!=\
1361 /* 2bp post function */
1362 if(moldyn->func2b_post) {
1363 moldyn->func2b_post(moldyn,
1368 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1380 temperature_calc(moldyn);
1381 pressure_calc(moldyn);
1382 ideal_gas_law_pressure(moldyn);
1388 * virial calculation
1391 inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
1393 a->virial.xx-=f->x*d->x;
1394 a->virial.yy-=f->y*d->y;
1395 a->virial.zz-=f->z*d->z;
1396 a->virial.xy-=f->x*d->y;
1397 a->virial.xz-=f->x*d->z;
1398 a->virial.yz-=f->y*d->z;
1404 * periodic boundayr checking
1407 inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1418 if(moldyn->status&MOLDYN_STAT_PBX) {
1419 if(a->x>=x) a->x-=dim->x;
1420 else if(-a->x>x) a->x+=dim->x;
1422 if(moldyn->status&MOLDYN_STAT_PBY) {
1423 if(a->y>=y) a->y-=dim->y;
1424 else if(-a->y>y) a->y+=dim->y;
1426 if(moldyn->status&MOLDYN_STAT_PBZ) {
1427 if(a->z>=z) a->z-=dim->z;
1428 else if(-a->z>z) a->z+=dim->z;
1436 * example potentials
1439 /* harmonic oscillator potential and force */
1441 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1443 t_ho_params *params;
1444 t_3dvec force,distance;
1446 double sc,equi_dist;
1448 params=moldyn->pot2b_params;
1449 sc=params->spring_constant;
1450 equi_dist=params->equilibrium_distance;
1454 v3_sub(&distance,&(aj->r),&(ai->r));
1456 if(bc) check_per_bound(moldyn,&distance);
1457 d=v3_norm(&distance);
1458 if(d<=moldyn->cutoff) {
1459 moldyn->energy+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
1460 /* f = -grad E; grad r_ij = -1 1/r_ij distance */
1461 f=sc*(1.0-equi_dist/d);
1462 v3_scale(&force,&distance,f);
1463 v3_add(&(ai->f),&(ai->f),&force);
1464 virial_calc(ai,&force,&distance);
1465 virial_calc(aj,&force,&distance); /* f and d signe switched */
1466 v3_scale(&force,&distance,-f);
1467 v3_add(&(aj->f),&(aj->f),&force);
1473 /* lennard jones potential & force for one sort of atoms */
1475 int lennard_jones(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1477 t_lj_params *params;
1478 t_3dvec force,distance;
1480 double eps,sig6,sig12;
1482 params=moldyn->pot2b_params;
1483 eps=params->epsilon4;
1484 sig6=params->sigma6;
1485 sig12=params->sigma12;
1489 v3_sub(&distance,&(aj->r),&(ai->r));
1490 if(bc) check_per_bound(moldyn,&distance);
1491 d=v3_absolute_square(&distance); /* 1/r^2 */
1492 if(d<=moldyn->cutoff_square) {
1493 d=1.0/d; /* 1/r^2 */
1496 h1=h2*h2; /* 1/r^12 */
1497 moldyn->energy+=(eps*(sig12*h1-sig6*h2)-params->uc);
1504 v3_scale(&force,&distance,d);
1505 v3_add(&(aj->f),&(aj->f),&force);
1506 v3_scale(&force,&distance,-1.0*d); /* f = - grad E */
1507 v3_add(&(ai->f),&(ai->f),&force);
1508 virial_calc(ai,&force,&distance);
1509 virial_calc(aj,&force,&distance); /* f and d signe switched */
1510 moldyn->vt1-=v3_scalar_product(&force,&distance);
1517 * tersoff potential & force for 2 sorts of atoms
1520 /* create mixed terms from parameters and set them */
1521 int tersoff_mult_complete_params(t_tersoff_mult_params *p) {
1523 printf("[moldyn] tersoff parameter completion\n");
1524 p->S2[0]=p->S[0]*p->S[0];
1525 p->S2[1]=p->S[1]*p->S[1];
1526 p->Smixed=sqrt(p->S[0]*p->S[1]);
1527 p->S2mixed=p->Smixed*p->Smixed;
1528 p->Rmixed=sqrt(p->R[0]*p->R[1]);
1529 p->Amixed=sqrt(p->A[0]*p->A[1]);
1530 p->Bmixed=sqrt(p->B[0]*p->B[1]);
1531 p->lambda_m=0.5*(p->lambda[0]+p->lambda[1]);
1532 p->mu_m=0.5*(p->mu[0]+p->mu[1]);
1534 printf("[moldyn] tersoff mult parameter info:\n");
1535 printf(" S (A) | %f | %f | %f\n",p->S[0],p->S[1],p->Smixed);
1536 printf(" R (A) | %f | %f | %f\n",p->R[0],p->R[1],p->Rmixed);
1537 printf(" A (eV) | %f | %f | %f\n",p->A[0]/EV,p->A[1]/EV,p->Amixed/EV);
1538 printf(" B (eV) | %f | %f | %f\n",p->B[0]/EV,p->B[1]/EV,p->Bmixed/EV);
1539 printf(" lambda | %f | %f | %f\n",p->lambda[0],p->lambda[1],
1541 printf(" mu | %f | %f | %f\n",p->mu[0],p->mu[1],p->mu_m);
1542 printf(" beta | %.10f | %.10f\n",p->beta[0],p->beta[1]);
1543 printf(" n | %f | %f\n",p->n[0],p->n[1]);
1544 printf(" c | %f | %f\n",p->c[0],p->c[1]);
1545 printf(" d | %f | %f\n",p->d[0],p->d[1]);
1546 printf(" h | %f | %f\n",p->h[0],p->h[1]);
1547 printf(" chi | %f \n",p->chi);
1552 /* tersoff 1 body part */
1553 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
1556 t_tersoff_mult_params *params;
1557 t_tersoff_exchange *exchange;
1560 params=moldyn->pot1b_params;
1561 exchange=&(params->exchange);
1564 * simple: point constant parameters only depending on atom i to
1565 * their right values
1568 exchange->beta_i=&(params->beta[brand]);
1569 exchange->n_i=&(params->n[brand]);
1570 exchange->c_i=&(params->c[brand]);
1571 exchange->d_i=&(params->d[brand]);
1572 exchange->h_i=&(params->h[brand]);
1574 exchange->betaini=pow(*(exchange->beta_i),*(exchange->n_i));
1575 exchange->ci2=params->c[brand]*params->c[brand];
1576 exchange->di2=params->d[brand]*params->d[brand];
1577 exchange->ci2di2=exchange->ci2/exchange->di2;
1582 /* tersoff 2 body part */
1583 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1585 t_tersoff_mult_params *params;
1586 t_tersoff_exchange *exchange;
1587 t_3dvec dist_ij,force;
1589 double A,B,R,S,S2,lambda,mu;
1596 params=moldyn->pot2b_params;
1598 exchange=&(params->exchange);
1600 /* clear 3bp and 2bp post run */
1602 exchange->run2bp_post=0;
1604 /* reset S > r > R mark */
1605 exchange->d_ij_between_rs=0;
1608 * calc of 2bp contribution of V_ij and dV_ij/ji
1610 * for Vij and dV_ij we need:
1614 * for dV_ji we need:
1615 * - f_c_ji = f_c_ij, df_c_ji = df_c_ij
1616 * - f_r_ji = f_r_ij; df_r_ji = df_r_ij
1621 if(brand==ai->brand) {
1623 S2=params->S2[brand];
1627 lambda=params->lambda[brand];
1628 mu=params->mu[brand];
1637 lambda=params->lambda_m;
1639 params->exchange.chi=params->chi;
1643 v3_sub(&dist_ij,&(aj->r),&(ai->r));
1644 if(bc) check_per_bound(moldyn,&dist_ij);
1645 d_ij2=v3_absolute_square(&dist_ij);
1647 /* if d_ij2 > S2 => no force & potential energy contribution */
1651 /* now we will need the distance */
1652 //d_ij=v3_norm(&dist_ij);
1655 /* save for use in 3bp */
1656 exchange->d_ij=d_ij;
1657 exchange->d_ij2=d_ij2;
1658 exchange->dist_ij=dist_ij;
1660 /* more constants */
1661 exchange->beta_j=&(params->beta[brand]);
1662 exchange->n_j=&(params->n[brand]);
1663 exchange->c_j=&(params->c[brand]);
1664 exchange->d_j=&(params->d[brand]);
1665 exchange->h_j=&(params->h[brand]);
1666 if(brand==ai->brand) {
1667 exchange->betajnj=exchange->betaini;
1668 exchange->cj2=exchange->ci2;
1669 exchange->dj2=exchange->di2;
1670 exchange->cj2dj2=exchange->ci2di2;
1673 exchange->betajnj=pow(*(exchange->beta_j),*(exchange->n_j));
1674 exchange->cj2=params->c[brand]*params->c[brand];
1675 exchange->dj2=params->d[brand]*params->d[brand];
1676 exchange->cj2dj2=exchange->cj2/exchange->dj2;
1679 /* f_r_ij = f_r_ji, df_r_ij = df_r_ji */
1680 f_r=A*exp(-lambda*d_ij);
1681 df_r=lambda*f_r/d_ij;
1683 /* f_a, df_a calc (again, same for ij and ji) | save for later use! */
1684 exchange->f_a=-B*exp(-mu*d_ij);
1685 exchange->df_a=mu*exchange->f_a/d_ij;
1687 /* f_c, df_c calc (again, same for ij and ji) */
1689 /* f_c = 1, df_c = 0 */
1692 /* two body contribution (ij, ji) */
1693 v3_scale(&force,&dist_ij,-df_r);
1697 arg=M_PI*(d_ij-R)/s_r;
1698 f_c=0.5+0.5*cos(arg);
1699 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
1700 /* two body contribution (ij, ji) */
1701 v3_scale(&force,&dist_ij,-df_c*f_r-df_r*f_c);
1702 /* tell 3bp that S > r > R */
1703 exchange->d_ij_between_rs=1;
1706 /* add forces of 2bp (ij, ji) contribution
1707 * dVij = dVji and we sum up both: no 1/2) */
1708 v3_add(&(ai->f),&(ai->f),&force);
1711 ai->virial.xx-=force.x*dist_ij.x;
1712 ai->virial.yy-=force.y*dist_ij.y;
1713 ai->virial.zz-=force.z*dist_ij.z;
1714 ai->virial.xy-=force.x*dist_ij.y;
1715 ai->virial.xz-=force.x*dist_ij.z;
1716 ai->virial.yz-=force.y*dist_ij.z;
1719 if(ai==&(moldyn->atom[0])) {
1720 printf("dVij, dVji (2bp) contrib:\n");
1721 printf("%f | %f\n",force.x,ai->f.x);
1722 printf("%f | %f\n",force.y,ai->f.y);
1723 printf("%f | %f\n",force.z,ai->f.z);
1727 if(ai==&(moldyn->atom[0])) {
1728 printf("dVij, dVji (2bp) contrib:\n");
1729 printf("%f | %f\n",force.x*dist_ij.x,ai->virial.xx);
1730 printf("%f | %f\n",force.y*dist_ij.y,ai->virial.yy);
1731 printf("%f | %f\n",force.z*dist_ij.z,ai->virial.zz);
1735 /* energy 2bp contribution (ij, ji) is 0.5 f_r f_c ... */
1736 moldyn->energy+=(0.5*f_r*f_c);
1738 /* save for use in 3bp */
1740 exchange->df_c=df_c;
1742 /* enable the run of 3bp function and 2bp post processing */
1744 exchange->run2bp_post=1;
1746 /* reset 3bp sums */
1747 exchange->zeta_ij=0.0;
1748 exchange->zeta_ji=0.0;
1749 v3_zero(&(exchange->dzeta_ij));
1750 v3_zero(&(exchange->dzeta_ji));
1755 /* tersoff 2 body post part */
1757 int tersoff_mult_post_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1760 * here we have to allow for the 3bp sums
1763 * - zeta_ij, dzeta_ij
1764 * - zeta_ji, dzeta_ji
1766 * to compute the 3bp contribution to:
1772 t_tersoff_mult_params *params;
1773 t_tersoff_exchange *exchange;
1778 double f_c,df_c,f_a,df_a;
1779 double chi,ni,betaini,nj,betajnj;
1782 params=moldyn->pot2b_params;
1783 exchange=&(params->exchange);
1785 /* we do not run if f_c_ij was detected to be 0! */
1786 if(!(exchange->run2bp_post))
1790 df_c=exchange->df_c;
1792 df_a=exchange->df_a;
1793 betaini=exchange->betaini;
1794 betajnj=exchange->betajnj;
1795 ni=*(exchange->n_i);
1796 nj=*(exchange->n_j);
1798 dist_ij=&(exchange->dist_ij);
1801 zeta=exchange->zeta_ij;
1803 moldyn->debug++; /* just for debugging ... */
1805 v3_scale(&force,dist_ij,df_a*b*f_c);
1808 tmp=betaini*pow(zeta,ni-1.0); /* beta^n * zeta^n-1 */
1809 b=(1+zeta*tmp); /* 1 + beta^n zeta^n */
1810 db=chi*pow(b,-1.0/(2*ni)-1); /* x(...)^(-1/2n - 1) */
1812 db*=-0.5*tmp; /* db_ij */
1813 v3_scale(&force,&(exchange->dzeta_ij),f_a*db);
1814 v3_scale(&temp,dist_ij,df_a*b);
1815 v3_add(&force,&force,&temp);
1816 v3_scale(&force,&force,f_c);
1818 v3_scale(&temp,dist_ij,df_c*b*f_a);
1819 v3_add(&force,&force,&temp);
1820 v3_scale(&force,&force,-0.5);
1823 v3_add(&(ai->f),&(ai->f),&force);
1826 ai->virial.xx-=force.x*dist_ij->x;
1827 ai->virial.yy-=force.y*dist_ij->y;
1828 ai->virial.zz-=force.z*dist_ij->z;
1829 ai->virial.xy-=force.x*dist_ij->y;
1830 ai->virial.xz-=force.x*dist_ij->z;
1831 ai->virial.yz-=force.y*dist_ij->z;
1834 if(ai==&(moldyn->atom[0])) {
1835 printf("dVij (3bp) contrib:\n");
1836 printf("%f | %f\n",force.x,ai->f.x);
1837 printf("%f | %f\n",force.y,ai->f.y);
1838 printf("%f | %f\n",force.z,ai->f.z);
1842 if(ai==&(moldyn->atom[0])) {
1843 printf("dVij (3bp) contrib:\n");
1844 printf("%f | %f\n",force.x*dist_ij->x,ai->virial.xx);
1845 printf("%f | %f\n",force.y*dist_ij->y,ai->virial.yy);
1846 printf("%f | %f\n",force.z*dist_ij->z,ai->virial.zz);
1850 /* add energy of 3bp sum */
1851 moldyn->energy+=(0.5*f_c*b*f_a);
1854 zeta=exchange->zeta_ji;
1858 v3_scale(&force,dist_ij,df_a*b*f_c);
1861 tmp=betajnj*pow(zeta,nj-1.0); /* beta^n * zeta^n-1 */
1862 b=(1+zeta*tmp); /* 1 + beta^n zeta^n */
1863 db=chi*pow(b,-1.0/(2*nj)-1); /* x(...)^(-1/2n - 1) */
1865 db*=-0.5*tmp; /* db_ij */
1866 v3_scale(&force,&(exchange->dzeta_ji),f_a*db);
1867 v3_scale(&temp,dist_ij,df_a*b);
1868 v3_add(&force,&force,&temp);
1869 v3_scale(&force,&force,f_c);
1871 v3_scale(&temp,dist_ij,df_c*b*f_a);
1872 v3_add(&force,&force,&temp);
1873 v3_scale(&force,&force,-0.5);
1876 v3_add(&(ai->f),&(ai->f),&force);
1878 /* virial - plus sign, as dist_ij = - dist_ji - (really??) */
1879 // TEST ... with a minus instead
1880 ai->virial.xx-=force.x*dist_ij->x;
1881 ai->virial.yy-=force.y*dist_ij->y;
1882 ai->virial.zz-=force.z*dist_ij->z;
1883 ai->virial.xy-=force.x*dist_ij->y;
1884 ai->virial.xz-=force.x*dist_ij->z;
1885 ai->virial.yz-=force.y*dist_ij->z;
1888 if(ai==&(moldyn->atom[0])) {
1889 printf("dVji (3bp) contrib:\n");
1890 printf("%f | %f\n",force.x,ai->f.x);
1891 printf("%f | %f\n",force.y,ai->f.y);
1892 printf("%f | %f\n",force.z,ai->f.z);
1896 if(ai==&(moldyn->atom[0])) {
1897 printf("dVji (3bp) contrib:\n");
1898 printf("%f | %f\n",force.x*dist_ij->x,ai->virial.xx);
1899 printf("%f | %f\n",force.y*dist_ij->y,ai->virial.yy);
1900 printf("%f | %f\n",force.z*dist_ij->z,ai->virial.zz);
1907 /* tersoff 3 body part */
1909 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
1911 t_tersoff_mult_params *params;
1912 t_tersoff_exchange *exchange;
1913 t_3dvec dist_ij,dist_ik,dist_jk;
1914 t_3dvec temp1,temp2;
1918 double d_ij,d_ik,d_jk,d_ij2,d_ik2,d_jk2;
1921 double f_c_ik,df_c_ik,arg;
1925 double cos_theta,d_costheta1,d_costheta2;
1926 double h_cos,d2_h_cos2;
1927 double frac,g,zeta,chi;
1931 params=moldyn->pot3b_params;
1932 exchange=&(params->exchange);
1934 if(!(exchange->run3bp))
1938 * calc of 3bp contribution of V_ij and dV_ij/ji/jk &
1939 * 2bp contribution of dV_jk
1941 * for Vij and dV_ij we still need:
1942 * - b_ij, db_ij (zeta_ij)
1943 * - f_c_ik, df_c_ik, constants_i, cos_theta_ijk, d_costheta_ijk
1945 * for dV_ji we still need:
1946 * - b_ji, db_ji (zeta_ji)
1947 * - f_c_jk, d_c_jk, constants_j, cos_theta_jik, d_costheta_jik
1949 * for dV_jk we need:
1953 * - f_c_ji, df_c_ji, constants_j, cos_theta_jki, d_costheta_jki
1961 /* dist_ij, d_ij - this is < S_ij ! */
1962 dist_ij=exchange->dist_ij;
1963 d_ij=exchange->d_ij;
1964 d_ij2=exchange->d_ij2;
1966 /* f_c_ij, df_c_ij (same for ji) */
1968 df_c=exchange->df_c;
1971 * calculate unknown values now ...
1974 /* V_ij and dV_ij stuff (in b_ij there is f_c_ik) */
1977 v3_sub(&dist_ik,&(ak->r),&(ai->r));
1978 if(bc) check_per_bound(moldyn,&dist_ik);
1979 d_ik2=v3_absolute_square(&dist_ik);
1983 if(brand==ak->brand) {
1986 S2=params->S2[brand];
1994 /* zeta_ij/dzeta_ij contribution only for d_ik < S */
1997 /* now we need d_ik */
2000 /* get constants_i from exchange data */
2007 c2d2=exchange->ci2di2;
2009 /* cosine of theta_ijk by scalaproduct */
2010 rr=v3_scalar_product(&dist_ij,&dist_ik);
2016 d_costheta1=cos_theta/d_ij2-tmp;
2017 d_costheta2=cos_theta/d_ik2-tmp;
2019 /* some usefull values */
2020 h_cos=(h-cos_theta);
2021 d2_h_cos2=d2+(h_cos*h_cos);
2022 frac=c2/(d2_h_cos2);
2027 /* d_costheta_ij and dg(cos_theta) - needed in any case! */
2028 v3_scale(&temp1,&dist_ij,d_costheta1);
2029 v3_scale(&temp2,&dist_ik,d_costheta2);
2030 v3_add(&temp1,&temp1,&temp2);
2031 v3_scale(&temp1,&temp1,-2.0*frac*h_cos/d2_h_cos2); /* dg */
2033 /* f_c_ik & df_c_ik + {d,}zeta contribution */
2034 dzeta=&(exchange->dzeta_ij);
2038 // => df_c_ik=0.0; of course we do not set this!
2041 exchange->zeta_ij+=g;
2044 v3_add(dzeta,dzeta,&temp1);
2049 arg=M_PI*(d_ik-R)/s_r;
2050 f_c_ik=0.5+0.5*cos(arg);
2051 df_c_ik=0.5*sin(arg)*(M_PI/(s_r*d_ik));
2054 exchange->zeta_ij+=f_c_ik*g;
2057 v3_scale(&temp1,&temp1,f_c_ik);
2058 v3_scale(&temp2,&dist_ik,g*df_c_ik);
2059 v3_add(&temp1,&temp1,&temp2);
2060 v3_add(dzeta,dzeta,&temp1);
2064 /* dV_ji stuff (in b_ji there is f_c_jk) + dV_jk stuff! */
2067 v3_sub(&dist_jk,&(ak->r),&(aj->r));
2068 if(bc) check_per_bound(moldyn,&dist_jk);
2069 d_jk2=v3_absolute_square(&dist_jk);
2073 if(brand==ak->brand) {
2076 S2=params->S2[brand];
2078 mu=params->mu[brand];
2090 /* zeta_ji/dzeta_ji contribution only for d_jk < S_jk */
2093 /* now we need d_ik */
2096 /* constants_j from exchange data */
2103 c2d2=exchange->cj2dj2;
2105 /* cosine of theta_jik by scalaproduct */
2106 rr=-v3_scalar_product(&dist_ij,&dist_jk); /* -1, as ij -> ji */
2112 d_costheta2=cos_theta/d_ij2;
2114 /* some usefull values */
2115 h_cos=(h-cos_theta);
2116 d2_h_cos2=d2+(h_cos*h_cos);
2117 frac=c2/(d2_h_cos2);
2122 /* d_costheta_jik and dg(cos_theta) - needed in any case! */
2123 v3_scale(&temp1,&dist_jk,d_costheta1);
2124 v3_scale(&temp2,&dist_ij,-d_costheta2); /* ji -> ij => -1 */
2125 //v3_add(&temp1,&temp1,&temp2);
2126 v3_sub(&temp1,&temp1,&temp2); /* there is a minus! */
2127 v3_scale(&temp1,&temp1,-2.0*frac*h_cos/d2_h_cos2); /* dg */
2129 /* store dg in temp2 and use it for dVjk later */
2130 v3_copy(&temp2,&temp1);
2132 /* f_c_jk + {d,}zeta contribution (df_c_jk = 0) */
2133 dzeta=&(exchange->dzeta_ji);
2139 exchange->zeta_ji+=g;
2142 v3_add(dzeta,dzeta,&temp1);
2147 arg=M_PI*(d_jk-R)/s_r;
2148 f_c_jk=0.5+0.5*cos(arg);
2151 exchange->zeta_ji+=f_c_jk*g;
2154 v3_scale(&temp1,&temp1,f_c_jk);
2155 v3_add(dzeta,dzeta,&temp1);
2158 /* dV_jk stuff | add force contribution on atom i immediately */
2159 if(exchange->d_ij_between_rs) {
2161 v3_scale(&temp1,&temp2,f_c);
2162 v3_scale(&temp2,&dist_ij,df_c*g);
2163 v3_add(&temp2,&temp2,&temp1); /* -> dzeta_jk in temp2 */
2167 // dzeta_jk is simply dg, which is stored in temp2
2169 /* betajnj * zeta_jk ^ nj-1 */
2170 tmp=exchange->betajnj*pow(zeta,(n-1.0));
2171 tmp=-chi/2.0*pow((1+tmp*zeta),(-1.0/(2.0*n)-1))*tmp;
2172 v3_scale(&temp2,&temp2,tmp*B*exp(-mu*d_jk)*f_c_jk*0.5);
2173 v3_add(&(ai->f),&(ai->f),&temp2); /* -1 skipped in f_a calc ^ */
2174 /* scaled with 0.5 ^ */
2177 ai->virial.xx-=temp2.x*dist_jk.x;
2178 ai->virial.yy-=temp2.y*dist_jk.y;
2179 ai->virial.zz-=temp2.z*dist_jk.z;
2180 ai->virial.xy-=temp2.x*dist_jk.y;
2181 ai->virial.xz-=temp2.x*dist_jk.z;
2182 ai->virial.yz-=temp2.y*dist_jk.z;
2185 if(ai==&(moldyn->atom[0])) {
2186 printf("dVjk (3bp) contrib:\n");
2187 printf("%f | %f\n",temp2.x,ai->f.x);
2188 printf("%f | %f\n",temp2.y,ai->f.y);
2189 printf("%f | %f\n",temp2.z,ai->f.z);
2193 if(ai==&(moldyn->atom[0])) {
2194 printf("dVjk (3bp) contrib:\n");
2195 printf("%f | %f\n",temp2.x*dist_jk.x,ai->virial.xx);
2196 printf("%f | %f\n",temp2.y*dist_jk.y,ai->virial.yy);
2197 printf("%f | %f\n",temp2.z*dist_jk.z,ai->virial.zz);
2208 * debugging / critical check functions
2211 int moldyn_bc_check(t_moldyn *moldyn) {
2224 for(i=0;i<moldyn->count;i++) {
2225 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2226 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2227 i,atom[i].r.x,dim->x/2);
2228 printf("diagnostic:\n");
2229 printf("-----------\natom.r.x:\n");
2231 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2234 ((byte)&(1<<k))?1:0,
2237 printf("---------------\nx=dim.x/2:\n");
2239 memcpy(&byte,(u8 *)(&x)+j,1);
2242 ((byte)&(1<<k))?1:0,
2245 if(atom[i].r.x==x) printf("the same!\n");
2246 else printf("different!\n");
2248 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2249 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2250 i,atom[i].r.y,dim->y/2);
2251 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2252 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2253 i,atom[i].r.z,dim->z/2);