2 * moldyn.c - molecular dynamics library main file
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
12 #include <sys/types.h>
21 #include "report/report.h"
24 * global variables, pse and atom colors (only needed here)
27 static char *pse_name[]={
49 static char *pse_col[]={
72 * the moldyn functions
75 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
77 printf("[moldyn] init\n");
79 memset(moldyn,0,sizeof(t_moldyn));
84 rand_init(&(moldyn->random),NULL,1);
85 moldyn->random.status|=RAND_STAT_VERBOSE;
90 int moldyn_shutdown(t_moldyn *moldyn) {
92 printf("[moldyn] shutdown\n");
94 moldyn_log_shutdown(moldyn);
95 link_cell_shutdown(moldyn);
96 rand_close(&(moldyn->random));
102 int set_int_alg(t_moldyn *moldyn,u8 algo) {
104 printf("[moldyn] integration algorithm: ");
107 case MOLDYN_INTEGRATE_VERLET:
108 moldyn->integrate=velocity_verlet;
109 printf("velocity verlet\n");
112 printf("unknown integration algorithm: %02x\n",algo);
120 int set_cutoff(t_moldyn *moldyn,double cutoff) {
122 moldyn->cutoff=cutoff;
124 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
129 int set_bondlen(t_moldyn *moldyn,double b0,double b1,double bm) {
131 moldyn->bondlen[0]=b0*b0;
132 moldyn->bondlen[1]=b1*b1;
134 moldyn->bondlen[2]=b0*b1;
136 moldyn->bondlen[2]=bm*bm;
141 int set_temperature(t_moldyn *moldyn,double t_ref) {
145 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
150 int set_pressure(t_moldyn *moldyn,double p_ref) {
154 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
159 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
161 moldyn->pt_scale=(ptype|ttype);
165 printf("[moldyn] p/t scaling:\n");
167 printf(" p: %s",ptype?"yes":"no ");
169 printf(" | type: %02x | factor: %f",ptype,ptc);
172 printf(" t: %s",ttype?"yes":"no ");
174 printf(" | type: %02x | factor: %f",ttype,ttc);
180 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
186 moldyn->volume=x*y*z;
194 printf("[moldyn] dimensions in A and A^3 respectively:\n");
195 printf(" x: %f\n",moldyn->dim.x);
196 printf(" y: %f\n",moldyn->dim.y);
197 printf(" z: %f\n",moldyn->dim.z);
198 printf(" volume: %f\n",moldyn->volume);
199 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
204 int set_nn_dist(t_moldyn *moldyn,double dist) {
211 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
213 printf("[moldyn] periodic boundary conditions:\n");
216 moldyn->status|=MOLDYN_STAT_PBX;
219 moldyn->status|=MOLDYN_STAT_PBY;
222 moldyn->status|=MOLDYN_STAT_PBZ;
224 printf(" x: %s\n",x?"yes":"no");
225 printf(" y: %s\n",y?"yes":"no");
226 printf(" z: %s\n",z?"yes":"no");
231 int set_potential1b(t_moldyn *moldyn,pf_func1b func) {
238 int set_potential2b(t_moldyn *moldyn,pf_func2b func) {
245 int set_potential3b_j1(t_moldyn *moldyn,pf_func2b func) {
247 moldyn->func3b_j1=func;
252 int set_potential3b_j2(t_moldyn *moldyn,pf_func2b func) {
254 moldyn->func3b_j2=func;
259 int set_potential3b_j3(t_moldyn *moldyn,pf_func2b func) {
261 moldyn->func3b_j3=func;
266 int set_potential3b_k1(t_moldyn *moldyn,pf_func3b func) {
268 moldyn->func3b_k1=func;
273 int set_potential3b_k2(t_moldyn *moldyn,pf_func3b func) {
275 moldyn->func3b_k2=func;
280 int set_potential_params(t_moldyn *moldyn,void *params) {
282 moldyn->pot_params=params;
287 int set_avg_skip(t_moldyn *moldyn,int skip) {
289 printf("[moldyn] skip %d steps before starting average calc\n",skip);
290 moldyn->avg_skip=skip;
295 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
297 strncpy(moldyn->vlsdir,dir,127);
302 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
304 strncpy(moldyn->rauthor,author,63);
305 strncpy(moldyn->rtitle,title,63);
310 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
315 printf("[moldyn] set log: ");
318 case LOG_TOTAL_ENERGY:
319 moldyn->ewrite=timer;
320 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
321 moldyn->efd=open(filename,
322 O_WRONLY|O_CREAT|O_EXCL,
325 perror("[moldyn] energy log fd open");
328 dprintf(moldyn->efd,"# total energy log file\n");
329 printf("total energy (%d)\n",timer);
331 case LOG_TOTAL_MOMENTUM:
332 moldyn->mwrite=timer;
333 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
334 moldyn->mfd=open(filename,
335 O_WRONLY|O_CREAT|O_EXCL,
338 perror("[moldyn] momentum log fd open");
341 dprintf(moldyn->efd,"# total momentum log file\n");
342 printf("total momentum (%d)\n",timer);
345 moldyn->pwrite=timer;
346 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
347 moldyn->pfd=open(filename,
348 O_WRONLY|O_CREAT|O_EXCL,
351 perror("[moldyn] pressure log file\n");
354 dprintf(moldyn->pfd,"# pressure log file\n");
355 printf("pressure (%d)\n",timer);
357 case LOG_TEMPERATURE:
358 moldyn->twrite=timer;
359 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
360 moldyn->tfd=open(filename,
361 O_WRONLY|O_CREAT|O_EXCL,
364 perror("[moldyn] temperature log file\n");
367 dprintf(moldyn->tfd,"# temperature log file\n");
368 printf("temperature (%d)\n",timer);
371 moldyn->vwrite=timer;
372 snprintf(filename,127,"%s/volume",moldyn->vlsdir);
373 moldyn->vfd=open(filename,
374 O_WRONLY|O_CREAT|O_EXCL,
377 perror("[moldyn] volume log file\n");
380 dprintf(moldyn->vfd,"# volume log file\n");
381 printf("volume (%d)\n",timer);
384 moldyn->swrite=timer;
385 printf("save file (%d)\n",timer);
388 moldyn->awrite=timer;
389 ret=visual_init(moldyn,moldyn->vlsdir);
391 printf("[moldyn] visual init failure\n");
394 printf("visual file (%d)\n",timer);
397 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
398 moldyn->rfd=open(filename,
399 O_WRONLY|O_CREAT|O_EXCL,
402 perror("[moldyn] report fd open");
405 printf("report -> ");
407 snprintf(filename,127,"%s/e_plot.scr",
409 moldyn->epfd=open(filename,
410 O_WRONLY|O_CREAT|O_EXCL,
413 perror("[moldyn] energy plot fd open");
416 dprintf(moldyn->epfd,e_plot_script);
421 snprintf(filename,127,"%s/pressure_plot.scr",
423 moldyn->ppfd=open(filename,
424 O_WRONLY|O_CREAT|O_EXCL,
427 perror("[moldyn] p plot fd open");
430 dprintf(moldyn->ppfd,pressure_plot_script);
435 snprintf(filename,127,"%s/temperature_plot.scr",
437 moldyn->tpfd=open(filename,
438 O_WRONLY|O_CREAT|O_EXCL,
441 perror("[moldyn] t plot fd open");
444 dprintf(moldyn->tpfd,temperature_plot_script);
446 printf("temperature ");
448 dprintf(moldyn->rfd,report_start,
449 moldyn->rauthor,moldyn->rtitle);
453 printf("unknown log type: %02x\n",type);
460 int moldyn_log_shutdown(t_moldyn *moldyn) {
464 printf("[moldyn] log shutdown\n");
468 dprintf(moldyn->rfd,report_energy);
469 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
474 if(moldyn->mfd) close(moldyn->mfd);
478 dprintf(moldyn->rfd,report_pressure);
479 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
486 dprintf(moldyn->rfd,report_temperature);
487 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
492 dprintf(moldyn->rfd,report_end);
494 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
497 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
500 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
509 * creating lattice functions
512 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
513 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
524 /* how many atoms do we expect */
525 if(type==CUBIC) new*=1;
526 if(type==FCC) new*=4;
527 if(type==DIAMOND) new*=8;
529 /* allocate space for atoms */
530 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
532 perror("[moldyn] realloc (create lattice)");
536 atom=&(moldyn->atom[count]);
538 /* no atoms on the boundaries (only reason: it looks better!) */
552 set_nn_dist(moldyn,lc);
553 ret=cubic_init(a,b,c,lc,atom,&orig);
557 v3_scale(&orig,&orig,0.5);
558 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
559 ret=fcc_init(a,b,c,lc,atom,&orig);
563 v3_scale(&orig,&orig,0.25);
564 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
565 ret=diamond_init(a,b,c,lc,atom,&orig);
568 printf("unknown lattice type (%02x)\n",type);
574 printf("[moldyn] creating lattice failed\n");
575 printf(" amount of atoms\n");
576 printf(" - expected: %d\n",new);
577 printf(" - created: %d\n",ret);
582 printf("[moldyn] created lattice with %d atoms\n",new);
584 for(ret=0;ret<new;ret++) {
585 atom[ret].element=element;
588 atom[ret].brand=brand;
589 atom[ret].tag=count+ret;
590 check_per_bound(moldyn,&(atom[ret].r));
591 atom[ret].r_0=atom[ret].r;
594 /* update total system mass */
595 total_mass_calc(moldyn);
600 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
601 t_3dvec *r,t_3dvec *v) {
608 count=(moldyn->count)++; // asshole style!
610 ptr=realloc(atom,(count+1)*sizeof(t_atom));
612 perror("[moldyn] realloc (add atom)");
619 /* initialize new atom */
620 memset(&(atom[count]),0,sizeof(t_atom));
623 atom[count].element=element;
624 atom[count].mass=mass;
625 atom[count].brand=brand;
626 atom[count].tag=count;
627 atom[count].attr=attr;
628 check_per_bound(moldyn,&(atom[count].r));
629 atom[count].r_0=atom[count].r;
631 /* update total system mass */
632 total_mass_calc(moldyn);
637 int del_atom(t_moldyn *moldyn,int tag) {
644 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
646 perror("[moldyn]malloc (del atom)");
650 for(cnt=0;cnt<tag;cnt++)
653 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
655 new[cnt-1].tag=cnt-1;
667 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
686 v3_copy(&(atom[count].r),&r);
695 for(i=0;i<count;i++) {
696 atom[i].r.x-=(a*lc)/2.0;
697 atom[i].r.y-=(b*lc)/2.0;
698 atom[i].r.z-=(c*lc)/2.0;
704 /* fcc lattice init */
705 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
718 /* construct the basis */
719 memset(basis,0,3*sizeof(t_3dvec));
727 /* fill up the room */
735 v3_copy(&(atom[count].r),&r);
738 /* the three face centered atoms */
740 v3_add(&n,&r,&basis[l]);
741 v3_copy(&(atom[count].r),&n);
750 /* coordinate transformation */
751 for(i=0;i<count;i++) {
752 atom[i].r.x-=(a*lc)/2.0;
753 atom[i].r.y-=(b*lc)/2.0;
754 atom[i].r.z-=(c*lc)/2.0;
760 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
765 count=fcc_init(a,b,c,lc,atom,origin);
771 if(origin) v3_add(&o,&o,origin);
773 count+=fcc_init(a,b,c,lc,&atom[count],&o);
778 int destroy_atoms(t_moldyn *moldyn) {
780 if(moldyn->atom) free(moldyn->atom);
785 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
788 * - gaussian distribution of velocities
789 * - zero total momentum
790 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
795 t_3dvec p_total,delta;
800 random=&(moldyn->random);
802 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
804 /* gaussian distribution of velocities */
806 for(i=0;i<moldyn->count;i++) {
807 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
809 v=sigma*rand_get_gauss(random);
811 p_total.x+=atom[i].mass*v;
813 v=sigma*rand_get_gauss(random);
815 p_total.y+=atom[i].mass*v;
817 v=sigma*rand_get_gauss(random);
819 p_total.z+=atom[i].mass*v;
822 /* zero total momentum */
823 v3_scale(&p_total,&p_total,1.0/moldyn->count);
824 for(i=0;i<moldyn->count;i++) {
825 v3_scale(&delta,&p_total,1.0/atom[i].mass);
826 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
829 /* velocity scaling */
830 scale_velocity(moldyn,equi_init);
835 double total_mass_calc(t_moldyn *moldyn) {
841 for(i=0;i<moldyn->count;i++)
842 moldyn->mass+=moldyn->atom[i].mass;
847 double temperature_calc(t_moldyn *moldyn) {
849 /* assume up to date kinetic energy, which is 3/2 N k_B T */
851 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
856 double get_temperature(t_moldyn *moldyn) {
861 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
871 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
874 /* get kinetic energy / temperature & count involved atoms */
877 for(i=0;i<moldyn->count;i++) {
878 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
879 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
884 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
885 else return 0; /* no atoms involved in scaling! */
887 /* (temporary) hack for e,t = 0 */
890 if(moldyn->t_ref!=0.0) {
891 thermal_init(moldyn,equi_init);
895 return 0; /* no scaling needed */
899 /* get scaling factor */
900 scale=moldyn->t_ref/moldyn->t;
904 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
905 scale=1.0+(scale-1.0)/moldyn->t_tc;
908 /* velocity scaling */
909 for(i=0;i<moldyn->count;i++) {
910 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
911 v3_scale(&(atom[i].v),&(atom[i].v),scale);
917 double ideal_gas_law_pressure(t_moldyn *moldyn) {
921 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
926 double virial_sum(t_moldyn *moldyn) {
931 /* virial (sum over atom virials) */
939 for(i=0;i<moldyn->count;i++) {
940 virial=&(moldyn->atom[i].virial);
941 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
942 moldyn->vir.xx+=virial->xx;
943 moldyn->vir.yy+=virial->yy;
944 moldyn->vir.zz+=virial->zz;
945 moldyn->vir.xy+=virial->xy;
946 moldyn->vir.xz+=virial->xz;
947 moldyn->vir.yz+=virial->yz;
950 /* global virial (absolute coordinates) */
951 virial=&(moldyn->gvir);
952 moldyn->gv=virial->xx+virial->yy+virial->zz;
954 return moldyn->virial;
957 double pressure_calc(t_moldyn *moldyn) {
961 * with W = 1/3 sum_i f_i r_i (- skipped!)
962 * virial = sum_i f_i r_i
964 * => P = (2 Ekin + virial) / (3V)
967 /* assume up to date virial & up to date kinetic energy */
969 /* pressure (atom virials) */
970 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
971 moldyn->p/=(3.0*moldyn->volume);
973 /* pressure (absolute coordinates) */
974 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
975 moldyn->gp/=(3.0*moldyn->volume);
980 int average_reset(t_moldyn *moldyn) {
982 printf("[moldyn] average reset\n");
984 /* update skip value */
985 moldyn->avg_skip=moldyn->total_steps;
991 /* potential energy */
999 moldyn->virial_sum=0.0;
1010 int average_and_fluctuation_calc(t_moldyn *moldyn) {
1014 if(moldyn->total_steps<moldyn->avg_skip)
1017 denom=moldyn->total_steps+1-moldyn->avg_skip;
1019 /* assume up to date energies, temperature, pressure etc */
1021 /* kinetic energy */
1022 moldyn->k_sum+=moldyn->ekin;
1023 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
1024 moldyn->k_avg=moldyn->k_sum/denom;
1025 moldyn->k2_avg=moldyn->k2_sum/denom;
1026 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
1028 /* potential energy */
1029 moldyn->v_sum+=moldyn->energy;
1030 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
1031 moldyn->v_avg=moldyn->v_sum/denom;
1032 moldyn->v2_avg=moldyn->v2_sum/denom;
1033 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1036 moldyn->t_sum+=moldyn->t;
1037 moldyn->t_avg=moldyn->t_sum/denom;
1040 moldyn->virial_sum+=moldyn->virial;
1041 moldyn->virial_avg=moldyn->virial_sum/denom;
1042 moldyn->gv_sum+=moldyn->gv;
1043 moldyn->gv_avg=moldyn->gv_sum/denom;
1046 moldyn->p_sum+=moldyn->p;
1047 moldyn->p_avg=moldyn->p_sum/denom;
1048 moldyn->gp_sum+=moldyn->gp;
1049 moldyn->gp_avg=moldyn->gp_sum/denom;
1050 moldyn->tp_sum+=moldyn->tp;
1051 moldyn->tp_avg=moldyn->tp_sum/denom;
1056 int get_heat_capacity(t_moldyn *moldyn) {
1060 /* averages needed for heat capacity calc */
1061 if(moldyn->total_steps<moldyn->avg_skip)
1064 /* (temperature average)^2 */
1065 temp2=moldyn->t_avg*moldyn->t_avg;
1066 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1069 /* ideal gas contribution */
1070 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1071 printf(" ideal gas contribution: %f\n",
1072 ighc/moldyn->mass*KILOGRAM/JOULE);
1074 /* specific heat for nvt ensemble */
1075 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1076 moldyn->c_v_nvt/=moldyn->mass;
1078 /* specific heat for nve ensemble */
1079 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1080 moldyn->c_v_nve/=moldyn->mass;
1082 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1083 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1084 printf(" --> <dV2> sim: %f experimental: %f\n",moldyn->dv2_avg,1.5*moldyn->count*K_B2*moldyn->t_avg*moldyn->t_avg*(1.0-1.5*moldyn->count*K_BOLTZMANN/(700*moldyn->mass*JOULE/KILOGRAM)));
1089 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1105 /* store atomic configuration + dimension */
1106 store=malloc(moldyn->count*sizeof(t_atom));
1108 printf("[moldyn] allocating store mem failed\n");
1111 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1116 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1117 su=pow(2.0-h,ONE_THIRD)-1.0;
1118 dv=(1.0-h)*moldyn->volume;
1120 /* scale up dimension and atom positions */
1121 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1122 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1123 link_cell_shutdown(moldyn);
1124 link_cell_init(moldyn,QUIET);
1125 potential_force_calc(moldyn);
1128 /* restore atomic configuration + dim */
1129 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1132 /* scale down dimension and atom positions */
1133 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1134 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1135 link_cell_shutdown(moldyn);
1136 link_cell_init(moldyn,QUIET);
1137 potential_force_calc(moldyn);
1140 /* calculate pressure */
1141 moldyn->tp=-(y1-y0)/(2.0*dv);
1143 /* restore atomic configuration */
1144 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1146 link_cell_shutdown(moldyn);
1147 link_cell_init(moldyn,QUIET);
1148 //potential_force_calc(moldyn);
1150 /* free store buffer */
1157 double get_pressure(t_moldyn *moldyn) {
1163 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1175 if(x) dim->x*=scale;
1176 if(y) dim->y*=scale;
1177 if(z) dim->z*=scale;
1182 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1193 for(i=0;i<moldyn->count;i++) {
1194 r=&(moldyn->atom[i].r);
1203 int scale_volume(t_moldyn *moldyn) {
1209 vdim=&(moldyn->vis.dim);
1213 /* scaling factor */
1214 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1215 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc;
1216 scale=pow(scale,ONE_THIRD);
1219 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1222 /* scale the atoms and dimensions */
1223 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1224 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1226 /* visualize dimensions */
1233 /* recalculate scaled volume */
1234 moldyn->volume=dim->x*dim->y*dim->z;
1236 /* adjust/reinit linkcell */
1237 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1238 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1239 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1240 link_cell_shutdown(moldyn);
1241 link_cell_init(moldyn,QUIET);
1252 double e_kin_calc(t_moldyn *moldyn) {
1260 for(i=0;i<moldyn->count;i++) {
1261 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1262 moldyn->ekin+=atom[i].ekin;
1265 return moldyn->ekin;
1268 double get_total_energy(t_moldyn *moldyn) {
1270 return(moldyn->ekin+moldyn->energy);
1273 t_3dvec get_total_p(t_moldyn *moldyn) {
1282 for(i=0;i<moldyn->count;i++) {
1283 v3_scale(&p,&(atom[i].v),atom[i].mass);
1284 v3_add(&p_total,&p_total,&p);
1290 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1294 /* nn_dist is the nearest neighbour distance */
1296 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1305 /* linked list / cell method */
1307 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1314 /* partitioning the md cell */
1315 lc->nx=moldyn->dim.x/moldyn->cutoff;
1316 lc->x=moldyn->dim.x/lc->nx;
1317 lc->ny=moldyn->dim.y/moldyn->cutoff;
1318 lc->y=moldyn->dim.y/lc->ny;
1319 lc->nz=moldyn->dim.z/moldyn->cutoff;
1320 lc->z=moldyn->dim.z/lc->nz;
1321 lc->cells=lc->nx*lc->ny*lc->nz;
1324 lc->subcell=malloc(lc->cells*sizeof(int*));
1326 lc->subcell=malloc(lc->cells*sizeof(t_list));
1329 if(lc->subcell==NULL) {
1330 perror("[moldyn] cell init (malloc)");
1335 printf("[moldyn] FATAL: less then 27 subcells!\n");
1339 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1342 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1345 printf(" x: %d x %f A\n",lc->nx,lc->x);
1346 printf(" y: %d x %f A\n",lc->ny,lc->y);
1347 printf(" z: %d x %f A\n",lc->nz,lc->z);
1352 for(i=0;i<lc->cells;i++) {
1353 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1354 if(lc->subcell[i]==NULL) {
1355 perror("[moldyn] list init (malloc)");
1360 printf(" ---> %d malloc %p (%p)\n",
1361 i,lc->subcell[0],lc->subcell);
1365 for(i=0;i<lc->cells;i++)
1366 list_init_f(&(lc->subcell[i]));
1369 /* update the list */
1370 link_cell_update(moldyn);
1375 int link_cell_update(t_moldyn *moldyn) {
1391 for(i=0;i<lc->cells;i++)
1393 memset(lc->subcell[i],0,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1395 list_destroy_f(&(lc->subcell[i]));
1398 for(count=0;count<moldyn->count;count++) {
1399 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1400 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1401 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1405 while(lc->subcell[i+j*nx+k*nx*ny][p]!=0)
1408 if(p>=MAX_ATOMS_PER_LIST) {
1409 printf("[moldyn] FATAL: amount of atoms too high!\n");
1413 lc->subcell[i+j*nx+k*nx*ny][p]=count;
1415 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1419 printf(" ---> %d %d malloc %p (%p)\n",
1420 i,count,lc->subcell[i].current,lc->subcell);
1428 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1452 if(i>=nx||j>=ny||k>=nz)
1453 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1456 cell[0]=lc->subcell[i+j*nx+k*a];
1457 for(ci=-1;ci<=1;ci++) {
1460 if((x<0)||(x>=nx)) {
1464 for(cj=-1;cj<=1;cj++) {
1467 if((y<0)||(y>=ny)) {
1471 for(ck=-1;ck<=1;ck++) {
1474 if((z<0)||(z>=nz)) {
1478 if(!(ci|cj|ck)) continue;
1480 cell[--count2]=lc->subcell[x+y*nx+z*a];
1483 cell[count1++]=lc->subcell[x+y*nx+z*a];
1494 int link_cell_shutdown(t_moldyn *moldyn) {
1501 for(i=0;i<lc->cells;i++) {
1503 free(lc->subcell[i]);
1505 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1506 list_destroy_f(&(lc->subcell[i]));
1515 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1519 t_moldyn_schedule *schedule;
1521 schedule=&(moldyn->schedule);
1522 count=++(schedule->total_sched);
1524 ptr=realloc(schedule->runs,count*sizeof(int));
1526 perror("[moldyn] realloc (runs)");
1530 schedule->runs[count-1]=runs;
1532 ptr=realloc(schedule->tau,count*sizeof(double));
1534 perror("[moldyn] realloc (tau)");
1538 schedule->tau[count-1]=tau;
1540 printf("[moldyn] schedule added:\n");
1541 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1547 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1549 moldyn->schedule.hook=hook;
1550 moldyn->schedule.hook_params=hook_params;
1557 * 'integration of newtons equation' - algorithms
1561 /* start the integration */
1563 int moldyn_integrate(t_moldyn *moldyn) {
1566 unsigned int e,m,s,v,p,t,a;
1568 t_moldyn_schedule *sched;
1573 double energy_scale;
1574 struct timeval t1,t2;
1577 sched=&(moldyn->schedule);
1580 /* initialize linked cell method */
1581 link_cell_init(moldyn,VERBOSE);
1583 /* logging & visualization */
1592 /* sqaure of some variables */
1593 moldyn->tau_square=moldyn->tau*moldyn->tau;
1594 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
1596 /* get current time */
1597 gettimeofday(&t1,NULL);
1599 /* calculate initial forces */
1600 potential_force_calc(moldyn);
1605 /* some stupid checks before we actually start calculating bullshit */
1606 if(moldyn->cutoff>0.5*moldyn->dim.x)
1607 printf("[moldyn] WARNING: cutoff > 0.5 x dim.x\n");
1608 if(moldyn->cutoff>0.5*moldyn->dim.y)
1609 printf("[moldyn] WARNING: cutoff > 0.5 x dim.y\n");
1610 if(moldyn->cutoff>0.5*moldyn->dim.z)
1611 printf("[moldyn] WARNING: cutoff > 0.5 x dim.z\n");
1612 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1613 if(ds>0.05*moldyn->nnd)
1614 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1616 /* zero absolute time */
1618 moldyn->total_steps=0;
1620 /* debugging, ignore */
1623 /* tell the world */
1624 printf("[moldyn] integration start, go get a coffee ...\n");
1626 /* executing the schedule */
1628 while(sched->count<sched->total_sched) {
1630 /* setting amount of runs and finite time step size */
1631 moldyn->tau=sched->tau[sched->count];
1632 moldyn->tau_square=moldyn->tau*moldyn->tau;
1633 moldyn->time_steps=sched->runs[sched->count];
1635 /* energy scaling factor (might change!) */
1636 energy_scale=moldyn->count*EV;
1638 /* integration according to schedule */
1640 for(i=0;i<moldyn->time_steps;i++) {
1642 /* integration step */
1643 moldyn->integrate(moldyn);
1645 /* calculate kinetic energy, temperature and pressure */
1647 temperature_calc(moldyn);
1649 pressure_calc(moldyn);
1650 //thermodynamic_pressure_calc(moldyn);
1652 /* calculate fluctuations + averages */
1653 average_and_fluctuation_calc(moldyn);
1656 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1657 scale_velocity(moldyn,FALSE);
1658 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1659 scale_volume(moldyn);
1661 /* check for log & visualization */
1663 if(!(moldyn->total_steps%e))
1664 dprintf(moldyn->efd,
1666 moldyn->time,moldyn->ekin/energy_scale,
1667 moldyn->energy/energy_scale,
1668 get_total_energy(moldyn)/energy_scale);
1671 if(!(moldyn->total_steps%m)) {
1672 momentum=get_total_p(moldyn);
1673 dprintf(moldyn->mfd,
1674 "%f %f %f %f %f\n",moldyn->time,
1675 momentum.x,momentum.y,momentum.z,
1676 v3_norm(&momentum));
1680 if(!(moldyn->total_steps%p)) {
1681 dprintf(moldyn->pfd,
1682 "%f %f %f %f %f %f %f\n",moldyn->time,
1683 moldyn->p/BAR,moldyn->p_avg/BAR,
1684 moldyn->gp/BAR,moldyn->gp_avg/BAR,
1685 moldyn->tp/BAR,moldyn->tp_avg/BAR);
1689 if(!(moldyn->total_steps%t)) {
1690 dprintf(moldyn->tfd,
1692 moldyn->time,moldyn->t,moldyn->t_avg);
1696 if(!(moldyn->total_steps%v)) {
1697 dprintf(moldyn->vfd,
1698 "%f %f\n",moldyn->time,moldyn->volume);
1702 if(!(moldyn->total_steps%s)) {
1703 snprintf(dir,128,"%s/s-%07.f.save",
1704 moldyn->vlsdir,moldyn->time);
1705 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
1707 if(fd<0) perror("[moldyn] save fd open");
1709 write(fd,moldyn,sizeof(t_moldyn));
1710 write(fd,moldyn->atom,
1711 moldyn->count*sizeof(t_atom));
1717 if(!(moldyn->total_steps%a)) {
1718 visual_atoms(moldyn);
1722 /* display progress */
1723 //if(!(moldyn->total_steps%10)) {
1724 /* get current time */
1725 gettimeofday(&t2,NULL);
1727 printf("\rsched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)",
1728 sched->count,i,moldyn->total_steps,
1729 moldyn->t,moldyn->t_avg,
1730 moldyn->p/BAR,moldyn->p_avg/BAR,
1732 (int)(t2.tv_sec-t1.tv_sec));
1736 /* copy over time */
1740 /* increase absolute time */
1741 moldyn->time+=moldyn->tau;
1742 moldyn->total_steps+=1;
1746 /* check for hooks */
1748 printf("\n ## schedule hook %d start ##\n",
1750 sched->hook(moldyn,sched->hook_params);
1751 printf(" ## schedule hook end ##\n");
1754 /* increase the schedule counter */
1762 /* velocity verlet */
1764 int velocity_verlet(t_moldyn *moldyn) {
1767 double tau,tau_square,h;
1772 count=moldyn->count;
1774 tau_square=moldyn->tau_square;
1776 for(i=0;i<count;i++) {
1777 /* check whether fixed atom */
1778 if(atom[i].attr&ATOM_ATTR_FP)
1782 v3_scale(&delta,&(atom[i].v),tau);
1783 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1784 v3_scale(&delta,&(atom[i].f),h*tau_square);
1785 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1786 check_per_bound(moldyn,&(atom[i].r));
1788 /* velocities [actually v(t+tau/2)] */
1789 v3_scale(&delta,&(atom[i].f),h*tau);
1790 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1793 /* criticial check */
1794 moldyn_bc_check(moldyn);
1796 /* neighbour list update */
1797 link_cell_update(moldyn);
1799 /* forces depending on chosen potential */
1800 potential_force_calc(moldyn);
1802 for(i=0;i<count;i++) {
1803 /* check whether fixed atom */
1804 if(atom[i].attr&ATOM_ATTR_FP)
1806 /* again velocities [actually v(t+tau)] */
1807 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1808 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1817 * potentials & corresponding forces & virial routine
1821 /* generic potential and force calculation */
1823 int potential_force_calc(t_moldyn *moldyn) {
1826 t_atom *itom,*jtom,*ktom;
1830 int *neighbour_i[27];
1834 t_list neighbour_i[27];
1835 t_list neighbour_i2[27];
1841 count=moldyn->count;
1851 /* reset global virial */
1852 memset(&(moldyn->gvir),0,sizeof(t_virial));
1854 /* reset force, site energy and virial of every atom */
1855 for(i=0;i<count;i++) {
1858 v3_zero(&(itom[i].f));
1861 virial=(&(itom[i].virial));
1869 /* reset site energy */
1874 /* get energy, force and virial of every atom */
1876 /* first (and only) loop over atoms i */
1877 for(i=0;i<count;i++) {
1879 /* single particle potential/force */
1880 if(itom[i].attr&ATOM_ATTR_1BP)
1882 moldyn->func1b(moldyn,&(itom[i]));
1884 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1887 /* 2 body pair potential/force */
1889 link_cell_neighbour_index(moldyn,
1890 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1891 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1892 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1897 /* first loop over atoms j */
1898 if(moldyn->func2b) {
1905 while(neighbour_i[j][p]!=0) {
1907 jtom=&(atom[neighbour_i[j][p]]);
1910 if(jtom==&(itom[i]))
1913 if((jtom->attr&ATOM_ATTR_2BP)&
1914 (itom[i].attr&ATOM_ATTR_2BP)) {
1915 moldyn->func2b(moldyn,
1922 this=&(neighbour_i[j]);
1925 if(this->start==NULL)
1929 jtom=this->current->data;
1931 if(jtom==&(itom[i]))
1934 if((jtom->attr&ATOM_ATTR_2BP)&
1935 (itom[i].attr&ATOM_ATTR_2BP)) {
1936 moldyn->func2b(moldyn,
1941 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1947 /* 3 body potential/force */
1949 if(!(itom[i].attr&ATOM_ATTR_3BP))
1952 /* copy the neighbour lists */
1954 /* no copy needed for static lists */
1956 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
1959 /* second loop over atoms j */
1966 while(neighbour_i[j][p]!=0) {
1968 jtom=&(atom[neighbour_i[j][p]]);
1971 this=&(neighbour_i[j]);
1974 if(this->start==NULL)
1979 jtom=this->current->data;
1982 if(jtom==&(itom[i]))
1985 if(!(jtom->attr&ATOM_ATTR_3BP))
1991 if(moldyn->func3b_j1)
1992 moldyn->func3b_j1(moldyn,
1997 /* in first j loop, 3bp run can be skipped */
1998 if(!(moldyn->run3bp))
2001 /* first loop over atoms k */
2002 if(moldyn->func3b_k1) {
2010 while(neighbour_i[j][q]!=0) {
2012 ktom=&(atom[neighbour_i[k][q]]);
2015 that=&(neighbour_i2[k]);
2018 if(that->start==NULL)
2022 ktom=that->current->data;
2025 if(!(ktom->attr&ATOM_ATTR_3BP))
2031 if(ktom==&(itom[i]))
2034 moldyn->func3b_k1(moldyn,
2042 } while(list_next_f(that)!=\
2050 if(moldyn->func3b_j2)
2051 moldyn->func3b_j2(moldyn,
2056 /* second loop over atoms k */
2057 if(moldyn->func3b_k2) {
2065 while(neighbour_i[j][q]!=0) {
2067 ktom=&(atom[neighbour_i[k][q]]);
2070 that=&(neighbour_i2[k]);
2073 if(that->start==NULL)
2077 ktom=that->current->data;
2080 if(!(ktom->attr&ATOM_ATTR_3BP))
2086 if(ktom==&(itom[i]))
2089 moldyn->func3b_k2(moldyn,
2098 } while(list_next_f(that)!=\
2106 /* 2bp post function */
2107 if(moldyn->func3b_j3) {
2108 moldyn->func3b_j3(moldyn,
2115 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2130 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2131 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2133 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
2134 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
2135 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
2139 /* some postprocessing */
2140 for(i=0;i<count;i++) {
2141 /* calculate global virial */
2142 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
2143 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
2144 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
2145 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
2146 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
2147 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
2149 /* check forces regarding the given timestep */
2150 if(v3_norm(&(itom[i].f))>\
2151 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
2152 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",
2160 * virial calculation
2163 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2164 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2166 a->virial.xx+=f->x*d->x;
2167 a->virial.yy+=f->y*d->y;
2168 a->virial.zz+=f->z*d->z;
2169 a->virial.xy+=f->x*d->y;
2170 a->virial.xz+=f->x*d->z;
2171 a->virial.yz+=f->y*d->z;
2177 * periodic boundary checking
2180 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2181 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2192 if(moldyn->status&MOLDYN_STAT_PBX) {
2193 if(a->x>=x) a->x-=dim->x;
2194 else if(-a->x>x) a->x+=dim->x;
2196 if(moldyn->status&MOLDYN_STAT_PBY) {
2197 if(a->y>=y) a->y-=dim->y;
2198 else if(-a->y>y) a->y+=dim->y;
2200 if(moldyn->status&MOLDYN_STAT_PBZ) {
2201 if(a->z>=z) a->z-=dim->z;
2202 else if(-a->z>z) a->z+=dim->z;
2209 * debugging / critical check functions
2212 int moldyn_bc_check(t_moldyn *moldyn) {
2225 for(i=0;i<moldyn->count;i++) {
2226 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2227 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2228 i,atom[i].r.x,dim->x/2);
2229 printf("diagnostic:\n");
2230 printf("-----------\natom.r.x:\n");
2232 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2235 ((byte)&(1<<k))?1:0,
2238 printf("---------------\nx=dim.x/2:\n");
2240 memcpy(&byte,(u8 *)(&x)+j,1);
2243 ((byte)&(1<<k))?1:0,
2246 if(atom[i].r.x==x) printf("the same!\n");
2247 else printf("different!\n");
2249 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2250 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2251 i,atom[i].r.y,dim->y/2);
2252 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2253 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2254 i,atom[i].r.z,dim->z/2);
2264 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2271 fd=open(file,O_RDONLY);
2273 perror("[moldyn] load save file open");
2277 fsize=lseek(fd,0,SEEK_END);
2278 lseek(fd,0,SEEK_SET);
2280 size=sizeof(t_moldyn);
2283 cnt=read(fd,moldyn,size);
2285 perror("[moldyn] load save file read (moldyn)");
2291 size=moldyn->count*sizeof(t_atom);
2293 /* correcting possible atom data offset */
2295 if(fsize!=sizeof(t_moldyn)+size) {
2296 corr=fsize-sizeof(t_moldyn)-size;
2297 printf("[moldyn] WARNING: lsf (illegal file size)\n");
2298 printf(" moifying offset:\n");
2299 printf(" - current pos: %d\n",sizeof(t_moldyn));
2300 printf(" - atom size: %d\n",size);
2301 printf(" - file size: %d\n",fsize);
2302 printf(" => correction: %d\n",corr);
2303 lseek(fd,corr,SEEK_CUR);
2306 moldyn->atom=(t_atom *)malloc(size);
2307 if(moldyn->atom==NULL) {
2308 perror("[moldyn] load save file malloc (atoms)");
2313 cnt=read(fd,moldyn->atom,size);
2315 perror("[moldyn] load save file read (atoms)");
2326 int moldyn_free_save_file(t_moldyn *moldyn) {
2333 int moldyn_load(t_moldyn *moldyn) {
2341 * function to find/callback all combinations of 2 body bonds
2344 int process_2b_bonds(t_moldyn *moldyn,void *data,
2345 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2346 void *data,u8 bc)) {
2353 t_list neighbour[27];
2362 link_cell_init(moldyn,VERBOSE);
2366 for(i=0;i<moldyn->count;i++) {
2367 /* neighbour indexing */
2368 link_cell_neighbour_index(moldyn,
2369 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2370 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2371 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2376 bc=(j<lc->dnlc)?0:1;
2381 while(neighbour[j][p]!=0) {
2383 jtom=&(moldyn->atom[neighbour[j][p]]);
2386 this=&(neighbour[j]);
2389 if(this->start==NULL)
2394 jtom=this->current->data;
2398 process(moldyn,&(itom[i]),jtom,data,bc);
2403 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2413 * post processing functions
2416 int get_line(int fd,char *line,int max) {
2423 if(count==max) return count;
2424 ret=read(fd,line+count,1);
2425 if(ret<=0) return ret;
2426 if(line[count]=='\n') {
2434 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2440 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
2456 for(i=0;i<moldyn->count;i++) {
2458 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
2459 check_per_bound(moldyn,&dist);
2460 d2=v3_absolute_square(&dist);
2474 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
2475 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
2476 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
2481 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
2486 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
2487 t_atom *jtom,void *data,u8 bc) {
2494 /* only count pairs once,
2495 * skip same atoms */
2496 if(itom->tag>=jtom->tag)
2500 * pair correlation calc
2507 v3_sub(&dist,&(jtom->r),&(itom->r));
2508 if(bc) check_per_bound(moldyn,&dist);
2509 d=v3_absolute_square(&dist);
2511 /* ignore if greater or equal 2 times cutoff */
2512 if(d>=4.0*moldyn->cutoff_square)
2515 /* fill the slots */
2519 /* should never happen but it does 8) -
2520 * related to -ffloat-store problem! */
2522 printf("[moldyn] WARNING: pcc (%d/%d)",
2528 if(itom->brand!=jtom->brand) {
2533 /* type a - type a bonds */
2535 pcc->stat[s+pcc->o1]+=1;
2537 /* type b - type b bonds */
2538 pcc->stat[s+pcc->o2]+=1;
2544 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2551 pcc.o1=2.0*moldyn->cutoff/dr;
2554 if(pcc.o1*dr<=moldyn->cutoff)
2555 printf("[moldyn] WARNING: pcc (low #slots)\n");
2557 printf("[moldyn] pair correlation calc info:\n");
2558 printf(" time: %f\n",moldyn->time);
2559 printf(" count: %d\n",moldyn->count);
2560 printf(" cutoff: %f\n",moldyn->cutoff);
2561 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2564 pcc.stat=(double *)ptr;
2567 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
2568 if(pcc.stat==NULL) {
2569 perror("[moldyn] pair correlation malloc");
2574 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
2577 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
2580 for(i=1;i<pcc.o1;i++) {
2581 // normalization: 4 pi r^2 dr
2582 // here: not double counting pairs -> 2 pi r r dr
2583 // ... and actually it's a constant times r^2
2586 pcc.stat[pcc.o1+i]/=norm;
2587 pcc.stat[pcc.o2+i]/=norm;
2592 /* todo: store/print pair correlation function */
2599 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2606 if(itom->tag>=jtom->tag)
2610 v3_sub(&dist,&(jtom->r),&(itom->r));
2611 if(bc) check_per_bound(moldyn,&dist);
2612 d=v3_absolute_square(&dist);
2614 /* ignore if greater or equal cutoff */
2615 if(d>=moldyn->cutoff_square)
2618 /* now count this bonding ... */
2621 /* increase total bond counter
2622 * ... double counting!
2627 ba->acnt[jtom->tag]+=1;
2629 ba->bcnt[jtom->tag]+=1;
2632 ba->acnt[itom->tag]+=1;
2634 ba->bcnt[itom->tag]+=1;
2639 int bond_analyze(t_moldyn *moldyn,double *quality) {
2641 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
2648 ba.acnt=malloc(moldyn->count*sizeof(int));
2650 perror("[moldyn] bond analyze malloc (a)");
2653 memset(ba.acnt,0,moldyn->count*sizeof(int));
2655 ba.bcnt=malloc(moldyn->count*sizeof(int));
2657 perror("[moldyn] bond analyze malloc (b)");
2660 memset(ba.bcnt,0,moldyn->count*sizeof(int));
2668 process_2b_bonds(moldyn,&ba,bond_analyze_process);
2670 for(i=0;i<moldyn->count;i++) {
2671 if(atom[i].brand==0) {
2672 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
2676 if((ba.acnt[i]==4)&(ba.bcnt[i]==0))
2681 printf("%d %d\n",qcnt,ba.tcnt);
2683 *quality=1.0*qcnt/ba.tcnt;
2685 printf("[moldyn] bond analyze: quality = %f\n",
2692 * visualization code
2695 int visual_init(t_moldyn *moldyn,char *filebase) {
2697 strncpy(moldyn->vis.fb,filebase,128);
2702 int visual_atoms(t_moldyn *moldyn) {
2716 t_list neighbour[27];
2732 sprintf(file,"%s/atomic_conf_%07.f.xyz",v->fb,moldyn->time);
2733 fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
2735 perror("open visual save file fd");
2739 /* write the actual data file */
2742 dprintf(fd,"# [P] %d %07.f <%f,%f,%f>\n",
2743 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
2745 // atomic configuration
2746 for(i=0;i<moldyn->count;i++) {
2747 // atom type, positions, color and kinetic energy
2748 dprintf(fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
2752 pse_col[atom[i].element],
2756 * bond detection should usually be done by potential
2757 * functions. brrrrr! EVIL!
2759 * todo: potentials need to export a 'find_bonds' function!
2762 // bonds between atoms
2763 if(!(atom[i].attr&ATOM_ATTR_VB))
2765 link_cell_neighbour_index(moldyn,
2766 (atom[i].r.x+moldyn->dim.x/2)/lc->x,
2767 (atom[i].r.y+moldyn->dim.y/2)/lc->y,
2768 (atom[i].r.z+moldyn->dim.z/2)/lc->z,
2774 while(neighbour[j][p]!=0) {
2775 btom=&(atom[neighbour[j][p]]);
2778 list_reset_f(&neighbour[j]);
2779 if(neighbour[j].start==NULL)
2782 btom=neighbour[j].current->data;
2784 if(btom==&atom[i]) // skip identical atoms
2786 //if(btom<&atom[i]) // skip half of them
2788 v3_sub(&dist,&(atom[i].r),&(btom->r));
2789 if(bc) check_per_bound(moldyn,&dist);
2790 d2=v3_absolute_square(&dist);
2791 brand=atom[i].brand;
2792 if(brand==btom->brand) {
2793 if(d2>moldyn->bondlen[brand])
2797 if(d2>moldyn->bondlen[2])
2800 dprintf(fd,"# [B] %f %f %f %f %f %f\n",
2801 atom[i].r.x,atom[i].r.y,atom[i].r.z,
2802 btom->r.x,btom->r.y,btom->r.z);
2806 } while(list_next_f(&neighbour[j])!=L_NO_NEXT_ELEMENT);
2813 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2814 -dim.x/2,-dim.y/2,-dim.z/2,
2815 dim.x/2,-dim.y/2,-dim.z/2);
2816 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2817 -dim.x/2,-dim.y/2,-dim.z/2,
2818 -dim.x/2,dim.y/2,-dim.z/2);
2819 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2820 dim.x/2,dim.y/2,-dim.z/2,
2821 dim.x/2,-dim.y/2,-dim.z/2);
2822 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2823 -dim.x/2,dim.y/2,-dim.z/2,
2824 dim.x/2,dim.y/2,-dim.z/2);
2826 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2827 -dim.x/2,-dim.y/2,dim.z/2,
2828 dim.x/2,-dim.y/2,dim.z/2);
2829 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2830 -dim.x/2,-dim.y/2,dim.z/2,
2831 -dim.x/2,dim.y/2,dim.z/2);
2832 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2833 dim.x/2,dim.y/2,dim.z/2,
2834 dim.x/2,-dim.y/2,dim.z/2);
2835 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2836 -dim.x/2,dim.y/2,dim.z/2,
2837 dim.x/2,dim.y/2,dim.z/2);
2839 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2840 -dim.x/2,-dim.y/2,dim.z/2,
2841 -dim.x/2,-dim.y/2,-dim.z/2);
2842 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2843 -dim.x/2,dim.y/2,dim.z/2,
2844 -dim.x/2,dim.y/2,-dim.z/2);
2845 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2846 dim.x/2,-dim.y/2,dim.z/2,
2847 dim.x/2,-dim.y/2,-dim.z/2);
2848 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2849 dim.x/2,dim.y/2,dim.z/2,
2850 dim.x/2,dim.y/2,-dim.z/2);