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 moldyn->dv=0.000001*moldyn->volume;
196 printf("[moldyn] dimensions in A and A^3 respectively:\n");
197 printf(" x: %f\n",moldyn->dim.x);
198 printf(" y: %f\n",moldyn->dim.y);
199 printf(" z: %f\n",moldyn->dim.z);
200 printf(" volume: %f\n",moldyn->volume);
201 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
202 printf(" delta volume (pressure calc): %f\n",moldyn->dv);
207 int set_nn_dist(t_moldyn *moldyn,double dist) {
214 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
216 printf("[moldyn] periodic boundary conditions:\n");
219 moldyn->status|=MOLDYN_STAT_PBX;
222 moldyn->status|=MOLDYN_STAT_PBY;
225 moldyn->status|=MOLDYN_STAT_PBZ;
227 printf(" x: %s\n",x?"yes":"no");
228 printf(" y: %s\n",y?"yes":"no");
229 printf(" z: %s\n",z?"yes":"no");
234 int set_potential1b(t_moldyn *moldyn,pf_func1b func) {
241 int set_potential2b(t_moldyn *moldyn,pf_func2b func) {
248 int set_potential3b_j1(t_moldyn *moldyn,pf_func2b func) {
250 moldyn->func3b_j1=func;
255 int set_potential3b_j2(t_moldyn *moldyn,pf_func2b func) {
257 moldyn->func3b_j2=func;
262 int set_potential3b_j3(t_moldyn *moldyn,pf_func2b func) {
264 moldyn->func3b_j3=func;
269 int set_potential3b_k1(t_moldyn *moldyn,pf_func3b func) {
271 moldyn->func3b_k1=func;
276 int set_potential3b_k2(t_moldyn *moldyn,pf_func3b func) {
278 moldyn->func3b_k2=func;
283 int set_potential_params(t_moldyn *moldyn,void *params) {
285 moldyn->pot_params=params;
290 int set_avg_skip(t_moldyn *moldyn,int skip) {
292 printf("[moldyn] skip %d steps before starting average calc\n",skip);
293 moldyn->avg_skip=skip;
298 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
300 strncpy(moldyn->vlsdir,dir,127);
305 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
307 strncpy(moldyn->rauthor,author,63);
308 strncpy(moldyn->rtitle,title,63);
313 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
318 printf("[moldyn] set log: ");
321 case LOG_TOTAL_ENERGY:
322 moldyn->ewrite=timer;
323 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
324 moldyn->efd=open(filename,
325 O_WRONLY|O_CREAT|O_EXCL,
328 perror("[moldyn] energy log fd open");
331 dprintf(moldyn->efd,"# total energy log file\n");
332 printf("total energy (%d)\n",timer);
334 case LOG_TOTAL_MOMENTUM:
335 moldyn->mwrite=timer;
336 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
337 moldyn->mfd=open(filename,
338 O_WRONLY|O_CREAT|O_EXCL,
341 perror("[moldyn] momentum log fd open");
344 dprintf(moldyn->efd,"# total momentum log file\n");
345 printf("total momentum (%d)\n",timer);
348 moldyn->pwrite=timer;
349 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
350 moldyn->pfd=open(filename,
351 O_WRONLY|O_CREAT|O_EXCL,
354 perror("[moldyn] pressure log file\n");
357 dprintf(moldyn->pfd,"# pressure log file\n");
358 printf("pressure (%d)\n",timer);
360 case LOG_TEMPERATURE:
361 moldyn->twrite=timer;
362 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
363 moldyn->tfd=open(filename,
364 O_WRONLY|O_CREAT|O_EXCL,
367 perror("[moldyn] temperature log file\n");
370 dprintf(moldyn->tfd,"# temperature log file\n");
371 printf("temperature (%d)\n",timer);
374 moldyn->swrite=timer;
375 printf("save file (%d)\n",timer);
378 moldyn->vwrite=timer;
379 ret=visual_init(moldyn,moldyn->vlsdir);
381 printf("[moldyn] visual init failure\n");
384 printf("visual file (%d)\n",timer);
387 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
388 moldyn->rfd=open(filename,
389 O_WRONLY|O_CREAT|O_EXCL,
392 perror("[moldyn] report fd open");
395 printf("report -> ");
397 snprintf(filename,127,"%s/e_plot.scr",
399 moldyn->epfd=open(filename,
400 O_WRONLY|O_CREAT|O_EXCL,
403 perror("[moldyn] energy plot fd open");
406 dprintf(moldyn->epfd,e_plot_script);
411 snprintf(filename,127,"%s/pressure_plot.scr",
413 moldyn->ppfd=open(filename,
414 O_WRONLY|O_CREAT|O_EXCL,
417 perror("[moldyn] p plot fd open");
420 dprintf(moldyn->ppfd,pressure_plot_script);
425 snprintf(filename,127,"%s/temperature_plot.scr",
427 moldyn->tpfd=open(filename,
428 O_WRONLY|O_CREAT|O_EXCL,
431 perror("[moldyn] t plot fd open");
434 dprintf(moldyn->tpfd,temperature_plot_script);
436 printf("temperature ");
438 dprintf(moldyn->rfd,report_start,
439 moldyn->rauthor,moldyn->rtitle);
443 printf("unknown log type: %02x\n",type);
450 int moldyn_log_shutdown(t_moldyn *moldyn) {
454 printf("[moldyn] log shutdown\n");
458 dprintf(moldyn->rfd,report_energy);
459 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
464 if(moldyn->mfd) close(moldyn->mfd);
468 dprintf(moldyn->rfd,report_pressure);
469 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
476 dprintf(moldyn->rfd,report_temperature);
477 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
482 dprintf(moldyn->rfd,report_end);
484 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
487 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
490 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
499 * creating lattice functions
502 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
503 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
514 /* how many atoms do we expect */
515 if(type==CUBIC) new*=1;
516 if(type==FCC) new*=4;
517 if(type==DIAMOND) new*=8;
519 /* allocate space for atoms */
520 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
522 perror("[moldyn] realloc (create lattice)");
526 atom=&(moldyn->atom[count]);
528 /* no atoms on the boundaries (only reason: it looks better!) */
542 set_nn_dist(moldyn,lc);
543 ret=cubic_init(a,b,c,lc,atom,&orig);
547 v3_scale(&orig,&orig,0.5);
548 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
549 ret=fcc_init(a,b,c,lc,atom,&orig);
553 v3_scale(&orig,&orig,0.25);
554 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
555 ret=diamond_init(a,b,c,lc,atom,&orig);
558 printf("unknown lattice type (%02x)\n",type);
564 printf("[moldyn] creating lattice failed\n");
565 printf(" amount of atoms\n");
566 printf(" - expected: %d\n",new);
567 printf(" - created: %d\n",ret);
572 printf("[moldyn] created lattice with %d atoms\n",new);
574 for(ret=0;ret<new;ret++) {
575 atom[ret].element=element;
578 atom[ret].brand=brand;
579 atom[ret].tag=count+ret;
580 check_per_bound(moldyn,&(atom[ret].r));
581 atom[ret].r_0=atom[ret].r;
584 /* update total system mass */
585 total_mass_calc(moldyn);
590 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
591 t_3dvec *r,t_3dvec *v) {
598 count=(moldyn->count)++;
600 ptr=realloc(atom,(count+1)*sizeof(t_atom));
602 perror("[moldyn] realloc (add atom)");
610 atom[count].element=element;
611 atom[count].mass=mass;
612 atom[count].brand=brand;
613 atom[count].tag=count;
614 atom[count].attr=attr;
615 check_per_bound(moldyn,&(atom[count].r));
616 atom[count].r_0=atom[count].r;
618 /* update total system mass */
619 total_mass_calc(moldyn);
624 int del_atom(t_moldyn *moldyn,int tag) {
631 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
633 perror("[moldyn]malloc (del atom)");
637 for(cnt=0;cnt<tag;cnt++)
640 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
642 new[cnt-1].tag=cnt-1;
654 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
673 v3_copy(&(atom[count].r),&r);
682 for(i=0;i<count;i++) {
683 atom[i].r.x-=(a*lc)/2.0;
684 atom[i].r.y-=(b*lc)/2.0;
685 atom[i].r.z-=(c*lc)/2.0;
691 /* fcc lattice init */
692 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
705 /* construct the basis */
706 memset(basis,0,3*sizeof(t_3dvec));
714 /* fill up the room */
722 v3_copy(&(atom[count].r),&r);
725 /* the three face centered atoms */
727 v3_add(&n,&r,&basis[l]);
728 v3_copy(&(atom[count].r),&n);
737 /* coordinate transformation */
738 for(i=0;i<count;i++) {
739 atom[i].r.x-=(a*lc)/2.0;
740 atom[i].r.y-=(b*lc)/2.0;
741 atom[i].r.z-=(c*lc)/2.0;
747 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
752 count=fcc_init(a,b,c,lc,atom,origin);
758 if(origin) v3_add(&o,&o,origin);
760 count+=fcc_init(a,b,c,lc,&atom[count],&o);
765 int destroy_atoms(t_moldyn *moldyn) {
767 if(moldyn->atom) free(moldyn->atom);
772 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
775 * - gaussian distribution of velocities
776 * - zero total momentum
777 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
782 t_3dvec p_total,delta;
787 random=&(moldyn->random);
789 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
791 /* gaussian distribution of velocities */
793 for(i=0;i<moldyn->count;i++) {
794 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
796 v=sigma*rand_get_gauss(random);
798 p_total.x+=atom[i].mass*v;
800 v=sigma*rand_get_gauss(random);
802 p_total.y+=atom[i].mass*v;
804 v=sigma*rand_get_gauss(random);
806 p_total.z+=atom[i].mass*v;
809 /* zero total momentum */
810 v3_scale(&p_total,&p_total,1.0/moldyn->count);
811 for(i=0;i<moldyn->count;i++) {
812 v3_scale(&delta,&p_total,1.0/atom[i].mass);
813 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
816 /* velocity scaling */
817 scale_velocity(moldyn,equi_init);
822 double total_mass_calc(t_moldyn *moldyn) {
828 for(i=0;i<moldyn->count;i++)
829 moldyn->mass+=moldyn->atom[i].mass;
834 double temperature_calc(t_moldyn *moldyn) {
836 /* assume up to date kinetic energy, which is 3/2 N k_B T */
838 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
843 double get_temperature(t_moldyn *moldyn) {
848 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
858 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
861 /* get kinetic energy / temperature & count involved atoms */
864 for(i=0;i<moldyn->count;i++) {
865 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
866 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
871 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
872 else return 0; /* no atoms involved in scaling! */
874 /* (temporary) hack for e,t = 0 */
877 if(moldyn->t_ref!=0.0) {
878 thermal_init(moldyn,equi_init);
882 return 0; /* no scaling needed */
886 /* get scaling factor */
887 scale=moldyn->t_ref/moldyn->t;
891 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
892 scale=1.0+(scale-1.0)/moldyn->t_tc;
895 /* velocity scaling */
896 for(i=0;i<moldyn->count;i++) {
897 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
898 v3_scale(&(atom[i].v),&(atom[i].v),scale);
904 double ideal_gas_law_pressure(t_moldyn *moldyn) {
908 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
913 double virial_sum(t_moldyn *moldyn) {
919 /* virial (sum over atom virials) */
921 for(i=0;i<moldyn->count;i++) {
922 virial=&(moldyn->atom[i].virial);
923 v+=(virial->xx+virial->yy+virial->zz);
927 /* global virial (absolute coordinates) */
928 virial=&(moldyn->gvir);
929 moldyn->gv=virial->xx+virial->yy+virial->zz;
931 return moldyn->virial;
934 double pressure_calc(t_moldyn *moldyn) {
938 * with W = 1/3 sum_i f_i r_i (- skipped!)
939 * virial = sum_i f_i r_i
941 * => P = (2 Ekin + virial) / (3V)
944 /* assume up to date virial & up to date kinetic energy */
946 /* pressure (atom virials) */
947 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
948 moldyn->p/=(3.0*moldyn->volume);
950 /* pressure (absolute coordinates) */
951 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
952 moldyn->gp/=(3.0*moldyn->volume);
957 int average_and_fluctuation_calc(t_moldyn *moldyn) {
961 if(moldyn->total_steps<moldyn->avg_skip)
964 denom=moldyn->total_steps+1-moldyn->avg_skip;
966 /* assume up to date energies, temperature, pressure etc */
969 moldyn->k_sum+=moldyn->ekin;
970 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
971 moldyn->k_avg=moldyn->k_sum/denom;
972 moldyn->k2_avg=moldyn->k2_sum/denom;
973 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
975 /* potential energy */
976 moldyn->v_sum+=moldyn->energy;
977 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
978 moldyn->v_avg=moldyn->v_sum/denom;
979 moldyn->v2_avg=moldyn->v2_sum/denom;
980 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
983 moldyn->t_sum+=moldyn->t;
984 moldyn->t_avg=moldyn->t_sum/denom;
987 moldyn->virial_sum+=moldyn->virial;
988 moldyn->virial_avg=moldyn->virial_sum/denom;
989 moldyn->gv_sum+=moldyn->gv;
990 moldyn->gv_avg=moldyn->gv_sum/denom;
993 moldyn->p_sum+=moldyn->p;
994 moldyn->p_avg=moldyn->p_sum/denom;
995 moldyn->gp_sum+=moldyn->gp;
996 moldyn->gp_avg=moldyn->gp_sum/denom;
1001 int get_heat_capacity(t_moldyn *moldyn) {
1005 /* averages needed for heat capacity calc */
1006 if(moldyn->total_steps<moldyn->avg_skip)
1009 /* (temperature average)^2 */
1010 temp2=moldyn->t_avg*moldyn->t_avg;
1011 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1014 /* ideal gas contribution */
1015 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1016 printf(" ideal gas contribution: %f\n",
1017 ighc/moldyn->mass*KILOGRAM/JOULE);
1019 /* specific heat for nvt ensemble */
1020 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1021 moldyn->c_v_nvt/=moldyn->mass;
1023 /* specific heat for nve ensemble */
1024 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1025 moldyn->c_v_nve/=moldyn->mass;
1027 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1028 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1029 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)));
1034 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1038 double u_up,u_down,dv;
1050 dv=8*scale*scale*scale*moldyn->volume;
1052 store=malloc(moldyn->count*sizeof(t_atom));
1054 printf("[moldyn] allocating store mem failed\n");
1058 /* save unscaled potential energy + atom/dim configuration */
1059 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1062 /* scale up dimension and atom positions */
1063 scale_dim(moldyn,SCALE_UP,scale,TRUE,TRUE,TRUE);
1064 scale_atoms(moldyn,SCALE_UP,scale,TRUE,TRUE,TRUE);
1065 link_cell_shutdown(moldyn);
1066 link_cell_init(moldyn,QUIET);
1067 potential_force_calc(moldyn);
1068 u_up=moldyn->energy;
1070 /* restore atomic configuration + dim */
1071 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1074 /* scale down dimension and atom positions */
1075 scale_dim(moldyn,SCALE_DOWN,scale,TRUE,TRUE,TRUE);
1076 scale_atoms(moldyn,SCALE_DOWN,scale,TRUE,TRUE,TRUE);
1077 link_cell_shutdown(moldyn);
1078 link_cell_init(moldyn,QUIET);
1079 potential_force_calc(moldyn);
1080 u_down=moldyn->energy;
1082 /* calculate pressure */
1083 p=-(u_up-u_down)/dv;
1084 printf("-------> %.10f %.10f %f\n",u_up/EV/moldyn->count,u_down/EV/moldyn->count,p/BAR);
1086 /* restore atomic configuration + dim */
1087 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1090 /* restore energy */
1091 potential_force_calc(moldyn);
1093 link_cell_shutdown(moldyn);
1094 link_cell_init(moldyn,QUIET);
1099 double get_pressure(t_moldyn *moldyn) {
1105 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1117 if(x) dim->x*=scale;
1118 if(y) dim->y*=scale;
1119 if(z) dim->z*=scale;
1124 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1135 for(i=0;i<moldyn->count;i++) {
1136 r=&(moldyn->atom[i].r);
1145 int scale_volume(t_moldyn *moldyn) {
1151 vdim=&(moldyn->vis.dim);
1155 /* scaling factor */
1156 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1157 scale=1.0-(moldyn->p_ref-moldyn->p)/moldyn->p_tc;
1158 scale=pow(scale,ONE_THIRD);
1161 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1163 moldyn->debug=scale;
1165 /* scale the atoms and dimensions */
1166 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1167 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1169 /* visualize dimensions */
1176 /* recalculate scaled volume */
1177 moldyn->volume=dim->x*dim->y*dim->z;
1179 /* adjust/reinit linkcell */
1180 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1181 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1182 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1183 link_cell_shutdown(moldyn);
1184 link_cell_init(moldyn,QUIET);
1195 double e_kin_calc(t_moldyn *moldyn) {
1203 for(i=0;i<moldyn->count;i++) {
1204 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1205 moldyn->ekin+=atom[i].ekin;
1208 return moldyn->ekin;
1211 double get_total_energy(t_moldyn *moldyn) {
1213 return(moldyn->ekin+moldyn->energy);
1216 t_3dvec get_total_p(t_moldyn *moldyn) {
1225 for(i=0;i<moldyn->count;i++) {
1226 v3_scale(&p,&(atom[i].v),atom[i].mass);
1227 v3_add(&p_total,&p_total,&p);
1233 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1237 /* nn_dist is the nearest neighbour distance */
1239 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1248 /* linked list / cell method */
1250 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1257 /* partitioning the md cell */
1258 lc->nx=moldyn->dim.x/moldyn->cutoff;
1259 lc->x=moldyn->dim.x/lc->nx;
1260 lc->ny=moldyn->dim.y/moldyn->cutoff;
1261 lc->y=moldyn->dim.y/lc->ny;
1262 lc->nz=moldyn->dim.z/moldyn->cutoff;
1263 lc->z=moldyn->dim.z/lc->nz;
1264 lc->cells=lc->nx*lc->ny*lc->nz;
1267 lc->subcell=malloc(lc->cells*sizeof(int*));
1269 lc->subcell=malloc(lc->cells*sizeof(t_list));
1272 if(lc->subcell==NULL) {
1273 perror("[moldyn] cell init (malloc)");
1278 printf("[moldyn] FATAL: less then 27 subcells!\n");
1282 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1285 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1288 printf(" x: %d x %f A\n",lc->nx,lc->x);
1289 printf(" y: %d x %f A\n",lc->ny,lc->y);
1290 printf(" z: %d x %f A\n",lc->nz,lc->z);
1295 for(i=0;i<lc->cells;i++) {
1296 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1297 if(lc->subcell[i]==NULL) {
1298 perror("[moldyn] list init (malloc)");
1303 printf(" ---> %d malloc %p (%p)\n",
1304 i,lc->subcell[0],lc->subcell);
1308 for(i=0;i<lc->cells;i++)
1309 list_init_f(&(lc->subcell[i]));
1312 /* update the list */
1313 link_cell_update(moldyn);
1318 int link_cell_update(t_moldyn *moldyn) {
1334 for(i=0;i<lc->cells;i++)
1336 memset(lc->subcell[i],0,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1338 list_destroy_f(&(lc->subcell[i]));
1341 for(count=0;count<moldyn->count;count++) {
1342 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1343 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1344 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1348 while(lc->subcell[i+j*nx+k*nx*ny][p]!=0)
1351 if(p>=MAX_ATOMS_PER_LIST) {
1352 printf("[moldyn] FATAL: amount of atoms too high!\n");
1356 lc->subcell[i+j*nx+k*nx*ny][p]=count;
1358 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1362 printf(" ---> %d %d malloc %p (%p)\n",
1363 i,count,lc->subcell[i].current,lc->subcell);
1371 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1395 cell[0]=lc->subcell[i+j*nx+k*a];
1396 for(ci=-1;ci<=1;ci++) {
1399 if((x<0)||(x>=nx)) {
1403 for(cj=-1;cj<=1;cj++) {
1406 if((y<0)||(y>=ny)) {
1410 for(ck=-1;ck<=1;ck++) {
1413 if((z<0)||(z>=nz)) {
1417 if(!(ci|cj|ck)) continue;
1419 cell[--count2]=lc->subcell[x+y*nx+z*a];
1422 cell[count1++]=lc->subcell[x+y*nx+z*a];
1433 int link_cell_shutdown(t_moldyn *moldyn) {
1440 for(i=0;i<lc->cells;i++) {
1442 free(lc->subcell[i]);
1444 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1445 list_destroy_f(&(lc->subcell[i]));
1454 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1458 t_moldyn_schedule *schedule;
1460 schedule=&(moldyn->schedule);
1461 count=++(schedule->total_sched);
1463 ptr=realloc(schedule->runs,count*sizeof(int));
1465 perror("[moldyn] realloc (runs)");
1469 schedule->runs[count-1]=runs;
1471 ptr=realloc(schedule->tau,count*sizeof(double));
1473 perror("[moldyn] realloc (tau)");
1477 schedule->tau[count-1]=tau;
1479 printf("[moldyn] schedule added:\n");
1480 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1486 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1488 moldyn->schedule.hook=hook;
1489 moldyn->schedule.hook_params=hook_params;
1496 * 'integration of newtons equation' - algorithms
1500 /* start the integration */
1502 int moldyn_integrate(t_moldyn *moldyn) {
1505 unsigned int e,m,s,v,p,t;
1507 t_moldyn_schedule *sched;
1512 double energy_scale;
1513 struct timeval t1,t2;
1516 sched=&(moldyn->schedule);
1519 /* initialize linked cell method */
1520 link_cell_init(moldyn,VERBOSE);
1522 /* logging & visualization */
1530 /* sqaure of some variables */
1531 moldyn->tau_square=moldyn->tau*moldyn->tau;
1532 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
1534 /* get current time */
1535 gettimeofday(&t1,NULL);
1537 /* calculate initial forces */
1538 potential_force_calc(moldyn);
1543 /* some stupid checks before we actually start calculating bullshit */
1544 if(moldyn->cutoff>0.5*moldyn->dim.x)
1545 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
1546 if(moldyn->cutoff>0.5*moldyn->dim.y)
1547 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
1548 if(moldyn->cutoff>0.5*moldyn->dim.z)
1549 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
1550 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1551 if(ds>0.05*moldyn->nnd)
1552 printf("[moldyn] warning: forces too high / tau too small!\n");
1554 /* zero absolute time */
1556 moldyn->total_steps=0;
1558 /* debugging, ignore */
1561 /* tell the world */
1562 printf("[moldyn] integration start, go get a coffee ...\n");
1564 /* executing the schedule */
1566 while(sched->count<sched->total_sched) {
1568 /* setting amount of runs and finite time step size */
1569 moldyn->tau=sched->tau[sched->count];
1570 moldyn->tau_square=moldyn->tau*moldyn->tau;
1571 moldyn->time_steps=sched->runs[sched->count];
1573 /* energy scaling factor (might change!) */
1574 energy_scale=moldyn->count*EV;
1576 /* integration according to schedule */
1578 for(i=0;i<moldyn->time_steps;i++) {
1580 /* integration step */
1581 moldyn->integrate(moldyn);
1583 /* calculate kinetic energy, temperature and pressure */
1585 temperature_calc(moldyn);
1587 pressure_calc(moldyn);
1588 average_and_fluctuation_calc(moldyn);
1591 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1592 scale_velocity(moldyn,FALSE);
1593 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1594 scale_volume(moldyn);
1596 /* check for log & visualization */
1598 if(!(moldyn->total_steps%e))
1599 dprintf(moldyn->efd,
1601 moldyn->time,moldyn->ekin/energy_scale,
1602 moldyn->energy/energy_scale,
1603 get_total_energy(moldyn)/energy_scale);
1606 if(!(moldyn->total_steps%m)) {
1607 momentum=get_total_p(moldyn);
1608 dprintf(moldyn->mfd,
1609 "%f %f %f %f %f\n",moldyn->time,
1610 momentum.x,momentum.y,momentum.z,
1611 v3_norm(&momentum));
1615 if(!(moldyn->total_steps%p)) {
1616 dprintf(moldyn->pfd,
1617 "%f %f %f %f %f\n",moldyn->time,
1618 moldyn->p/BAR,moldyn->p_avg/BAR,
1619 moldyn->gp/BAR,moldyn->gp_avg/BAR);
1623 if(!(moldyn->total_steps%t)) {
1624 dprintf(moldyn->tfd,
1626 moldyn->time,moldyn->t,moldyn->t_avg);
1630 if(!(moldyn->total_steps%s)) {
1631 snprintf(dir,128,"%s/s-%07.f.save",
1632 moldyn->vlsdir,moldyn->time);
1633 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
1635 if(fd<0) perror("[moldyn] save fd open");
1637 write(fd,moldyn,sizeof(t_moldyn));
1638 write(fd,moldyn->atom,
1639 moldyn->count*sizeof(t_atom));
1645 if(!(moldyn->total_steps%v)) {
1646 visual_atoms(moldyn);
1650 /* display progress */
1651 //if(!(moldyn->total_steps%10)) {
1652 /* get current time */
1653 gettimeofday(&t2,NULL);
1655 printf("\rsched:%d, steps:%d/%d, T:%3.1f/%3.1f P:%4.1f/%4.1f V:%6.1f (%d)",
1656 sched->count,i,moldyn->total_steps,
1657 moldyn->t,moldyn->t_avg,
1658 moldyn->p_avg/BAR,moldyn->gp_avg/BAR,
1660 (int)(t2.tv_sec-t1.tv_sec));
1664 /* copy over time */
1668 /* increase absolute time */
1669 moldyn->time+=moldyn->tau;
1670 moldyn->total_steps+=1;
1674 /* check for hooks */
1676 printf("\n ## schedule hook %d start ##\n",
1678 sched->hook(moldyn,sched->hook_params);
1679 printf(" ## schedule hook end ##\n");
1682 /* increase the schedule counter */
1690 /* velocity verlet */
1692 int velocity_verlet(t_moldyn *moldyn) {
1695 double tau,tau_square,h;
1700 count=moldyn->count;
1702 tau_square=moldyn->tau_square;
1704 for(i=0;i<count;i++) {
1707 v3_scale(&delta,&(atom[i].v),tau);
1708 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1709 v3_scale(&delta,&(atom[i].f),h*tau_square);
1710 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1711 check_per_bound(moldyn,&(atom[i].r));
1713 /* velocities [actually v(t+tau/2)] */
1714 v3_scale(&delta,&(atom[i].f),h*tau);
1715 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1718 /* neighbour list update */
1719 link_cell_update(moldyn);
1721 /* forces depending on chosen potential */
1722 potential_force_calc(moldyn);
1724 for(i=0;i<count;i++) {
1725 /* again velocities [actually v(t+tau)] */
1726 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1727 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1736 * potentials & corresponding forces & virial routine
1740 /* generic potential and force calculation */
1742 int potential_force_calc(t_moldyn *moldyn) {
1745 t_atom *itom,*jtom,*ktom;
1749 int *neighbour_i[27];
1753 t_list neighbour_i[27];
1754 t_list neighbour_i2[27];
1760 count=moldyn->count;
1770 /* reset global virial */
1771 memset(&(moldyn->gvir),0,sizeof(t_virial));
1773 /* reset force, site energy and virial of every atom */
1774 for(i=0;i<count;i++) {
1777 v3_zero(&(itom[i].f));
1780 virial=(&(itom[i].virial));
1788 /* reset site energy */
1793 /* get energy, force and virial of every atom */
1795 /* first (and only) loop over atoms i */
1796 for(i=0;i<count;i++) {
1798 /* single particle potential/force */
1799 if(itom[i].attr&ATOM_ATTR_1BP)
1801 moldyn->func1b(moldyn,&(itom[i]));
1803 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1806 /* 2 body pair potential/force */
1808 link_cell_neighbour_index(moldyn,
1809 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1810 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1811 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1816 /* first loop over atoms j */
1817 if(moldyn->func2b) {
1824 while(neighbour_i[j][p]!=0) {
1826 jtom=&(atom[neighbour_i[j][p]]);
1829 if(jtom==&(itom[i]))
1832 if((jtom->attr&ATOM_ATTR_2BP)&
1833 (itom[i].attr&ATOM_ATTR_2BP)) {
1834 moldyn->func2b(moldyn,
1841 this=&(neighbour_i[j]);
1844 if(this->start==NULL)
1848 jtom=this->current->data;
1850 if(jtom==&(itom[i]))
1853 if((jtom->attr&ATOM_ATTR_2BP)&
1854 (itom[i].attr&ATOM_ATTR_2BP)) {
1855 moldyn->func2b(moldyn,
1860 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1866 /* 3 body potential/force */
1868 if(!(itom[i].attr&ATOM_ATTR_3BP))
1871 /* copy the neighbour lists */
1873 /* no copy needed for static lists */
1875 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
1878 /* second loop over atoms j */
1885 while(neighbour_i[j][p]!=0) {
1887 jtom=&(atom[neighbour_i[j][p]]);
1890 this=&(neighbour_i[j]);
1893 if(this->start==NULL)
1898 jtom=this->current->data;
1901 if(jtom==&(itom[i]))
1904 if(!(jtom->attr&ATOM_ATTR_3BP))
1910 if(moldyn->func3b_j1)
1911 moldyn->func3b_j1(moldyn,
1916 /* in first j loop, 3bp run can be skipped */
1917 if(!(moldyn->run3bp))
1920 /* first loop over atoms k */
1921 if(moldyn->func3b_k1) {
1929 while(neighbour_i[j][q]!=0) {
1931 ktom=&(atom[neighbour_i[k][q]]);
1934 that=&(neighbour_i2[k]);
1937 if(that->start==NULL)
1941 ktom=that->current->data;
1944 if(!(ktom->attr&ATOM_ATTR_3BP))
1950 if(ktom==&(itom[i]))
1953 moldyn->func3b_k1(moldyn,
1961 } while(list_next_f(that)!=\
1969 if(moldyn->func3b_j2)
1970 moldyn->func3b_j2(moldyn,
1975 /* second loop over atoms k */
1976 if(moldyn->func3b_k2) {
1984 while(neighbour_i[j][q]!=0) {
1986 ktom=&(atom[neighbour_i[k][q]]);
1989 that=&(neighbour_i2[k]);
1992 if(that->start==NULL)
1996 ktom=that->current->data;
1999 if(!(ktom->attr&ATOM_ATTR_3BP))
2005 if(ktom==&(itom[i]))
2008 moldyn->func3b_k2(moldyn,
2017 } while(list_next_f(that)!=\
2025 /* 2bp post function */
2026 if(moldyn->func3b_j3) {
2027 moldyn->func3b_j3(moldyn,
2034 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2049 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2050 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2052 printf(" x: %0.40f\n",moldyn->atom[5832].f.x);
2053 printf(" y: %0.40f\n",moldyn->atom[5832].f.y);
2054 printf(" z: %0.40f\n",moldyn->atom[5832].f.z);
2058 /* calculate global virial */
2059 for(i=0;i<count;i++) {
2060 moldyn->gvir.xx+=moldyn->atom[i].r.x*moldyn->atom[i].f.x;
2061 moldyn->gvir.yy+=moldyn->atom[i].r.y*moldyn->atom[i].f.y;
2062 moldyn->gvir.zz+=moldyn->atom[i].r.z*moldyn->atom[i].f.z;
2063 moldyn->gvir.xy+=moldyn->atom[i].r.y*moldyn->atom[i].f.x;
2064 moldyn->gvir.xz+=moldyn->atom[i].r.z*moldyn->atom[i].f.x;
2065 moldyn->gvir.yz+=moldyn->atom[i].r.z*moldyn->atom[i].f.y;
2072 * virial calculation
2075 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2076 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2078 a->virial.xx+=f->x*d->x;
2079 a->virial.yy+=f->y*d->y;
2080 a->virial.zz+=f->z*d->z;
2081 a->virial.xy+=f->x*d->y;
2082 a->virial.xz+=f->x*d->z;
2083 a->virial.yz+=f->y*d->z;
2089 * periodic boundary checking
2092 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2093 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2104 if(moldyn->status&MOLDYN_STAT_PBX) {
2105 if(a->x>=x) a->x-=dim->x;
2106 else if(-a->x>x) a->x+=dim->x;
2108 if(moldyn->status&MOLDYN_STAT_PBY) {
2109 if(a->y>=y) a->y-=dim->y;
2110 else if(-a->y>y) a->y+=dim->y;
2112 if(moldyn->status&MOLDYN_STAT_PBZ) {
2113 if(a->z>=z) a->z-=dim->z;
2114 else if(-a->z>z) a->z+=dim->z;
2121 * debugging / critical check functions
2124 int moldyn_bc_check(t_moldyn *moldyn) {
2137 for(i=0;i<moldyn->count;i++) {
2138 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2139 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2140 i,atom[i].r.x,dim->x/2);
2141 printf("diagnostic:\n");
2142 printf("-----------\natom.r.x:\n");
2144 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2147 ((byte)&(1<<k))?1:0,
2150 printf("---------------\nx=dim.x/2:\n");
2152 memcpy(&byte,(u8 *)(&x)+j,1);
2155 ((byte)&(1<<k))?1:0,
2158 if(atom[i].r.x==x) printf("the same!\n");
2159 else printf("different!\n");
2161 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2162 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2163 i,atom[i].r.y,dim->y/2);
2164 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2165 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2166 i,atom[i].r.z,dim->z/2);
2176 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2181 fd=open(file,O_RDONLY);
2183 perror("[moldyn] load save file open");
2187 size=sizeof(t_moldyn);
2190 cnt=read(fd,moldyn,size);
2192 perror("[moldyn] load save file read (moldyn)");
2198 size=moldyn->count*sizeof(t_atom);
2200 moldyn->atom=(t_atom *)malloc(size);
2201 if(moldyn->atom==NULL) {
2202 perror("[moldyn] load save file malloc (atoms)");
2207 cnt=read(fd,moldyn->atom,size);
2209 perror("[moldyn] load save file read (atoms)");
2220 int moldyn_load(t_moldyn *moldyn) {
2228 * post processing functions
2231 int get_line(int fd,char *line,int max) {
2238 if(count==max) return count;
2239 ret=read(fd,line+count,1);
2240 if(ret<=0) return ret;
2241 if(line[count]=='\n') {
2249 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2255 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2265 t_list neighbour[27];
2274 unsigned char ibrand;
2278 slots=moldyn->cutoff/dr;
2281 printf("[moldyn] pair correlation calc info:\n");
2282 printf(" time: %f\n",moldyn->time);
2283 printf(" count: %d\n",moldyn->count);
2284 printf(" cutoff: %f\n",moldyn->cutoff);
2285 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2291 stat=(double *)malloc(3*slots*sizeof(double));
2293 perror("[moldyn] pair correlation malloc");
2298 memset(stat,0,3*slots*sizeof(double));
2300 link_cell_init(moldyn,VERBOSE);
2304 for(i=0;i<moldyn->count;i++) {
2305 /* neighbour indexing */
2306 link_cell_neighbour_index(moldyn,
2307 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2308 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2309 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2312 /* brand of atom i */
2313 ibrand=itom[i].brand;
2317 bc=(j<lc->dnlc)?0:1;
2322 while(neighbour[j][p]!=0) {
2324 jtom=&(moldyn->atom[neighbour[j][p]]);
2327 this=&(neighbour[j]);
2330 if(this->start==NULL)
2335 jtom=this->current->data;
2337 /* only count pairs once,
2338 * skip same atoms */
2339 if(itom[i].tag>=jtom->tag)
2343 * pair correlation calc
2347 v3_sub(&dist,&(jtom->r),&(itom[i].r));
2348 if(bc) check_per_bound(moldyn,&dist);
2349 d=v3_absolute_square(&dist);
2351 /* ignore if greater or equal cutoff */
2352 if(d>=moldyn->cutoff_square)
2355 /* fill the slots */
2359 /* should never happen but it does 8) -
2360 * related to -ffloat-store problem! */
2361 if(s>=slots) s=slots-1;
2363 if(ibrand!=jtom->brand) {
2368 /* type a - type a bonds */
2372 /* type b - type b bonds */
2378 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2384 for(i=1;i<slots;i++) {
2385 // normalization: 4 pi r r dr
2386 // here: not double counting pairs -> 2 pi r r dr
2387 norm=2*M_PI*moldyn->count*(i*dr*i*dr)*dr;
2389 stat[slots+i]/=norm;
2395 /* todo: store/print pair correlation function */
2401 link_cell_shutdown(moldyn);
2406 int analyze_bonds(t_moldyn *moldyn) {
2415 * visualization code
2418 int visual_init(t_moldyn *moldyn,char *filebase) {
2420 strncpy(moldyn->vis.fb,filebase,128);
2425 int visual_atoms(t_moldyn *moldyn) {
2439 t_list neighbour[27];
2455 sprintf(file,"%s/atomic_conf_%07.f.xyz",v->fb,moldyn->time);
2456 fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
2458 perror("open visual save file fd");
2462 /* write the actual data file */
2465 dprintf(fd,"# [P] %d %07.f <%f,%f,%f>\n",
2466 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
2468 // atomic configuration
2469 for(i=0;i<moldyn->count;i++) {
2470 // atom type, positions, color and kinetic energy
2471 dprintf(fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
2475 pse_col[atom[i].element],
2479 * bond detection should usually be done by potential
2480 * functions. brrrrr! EVIL!
2482 * todo: potentials need to export a 'find_bonds' function!
2485 // bonds between atoms
2486 if(!(atom[i].attr&ATOM_ATTR_VB))
2488 link_cell_neighbour_index(moldyn,
2489 (atom[i].r.x+moldyn->dim.x/2)/lc->x,
2490 (atom[i].r.y+moldyn->dim.y/2)/lc->y,
2491 (atom[i].r.z+moldyn->dim.z/2)/lc->z,
2497 while(neighbour[j][p]!=0) {
2498 btom=&(atom[neighbour[j][p]]);
2501 list_reset_f(&neighbour[j]);
2502 if(neighbour[j].start==NULL)
2505 btom=neighbour[j].current->data;
2507 if(btom==&atom[i]) // skip identical atoms
2509 //if(btom<&atom[i]) // skip half of them
2511 v3_sub(&dist,&(atom[i].r),&(btom->r));
2512 if(bc) check_per_bound(moldyn,&dist);
2513 d2=v3_absolute_square(&dist);
2514 brand=atom[i].brand;
2515 if(brand==btom->brand) {
2516 if(d2>moldyn->bondlen[brand])
2520 if(d2>moldyn->bondlen[2])
2523 dprintf(fd,"# [B] %f %f %f %f %f %f\n",
2524 atom[i].r.x,atom[i].r.y,atom[i].r.z,
2525 btom->r.x,btom->r.y,btom->r.z);
2529 } while(list_next_f(&neighbour[j])!=L_NO_NEXT_ELEMENT);
2536 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2537 -dim.x/2,-dim.y/2,-dim.z/2,
2538 dim.x/2,-dim.y/2,-dim.z/2);
2539 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2540 -dim.x/2,-dim.y/2,-dim.z/2,
2541 -dim.x/2,dim.y/2,-dim.z/2);
2542 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2543 dim.x/2,dim.y/2,-dim.z/2,
2544 dim.x/2,-dim.y/2,-dim.z/2);
2545 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2546 -dim.x/2,dim.y/2,-dim.z/2,
2547 dim.x/2,dim.y/2,-dim.z/2);
2549 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2550 -dim.x/2,-dim.y/2,dim.z/2,
2551 dim.x/2,-dim.y/2,dim.z/2);
2552 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2553 -dim.x/2,-dim.y/2,dim.z/2,
2554 -dim.x/2,dim.y/2,dim.z/2);
2555 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2556 dim.x/2,dim.y/2,dim.z/2,
2557 dim.x/2,-dim.y/2,dim.z/2);
2558 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2559 -dim.x/2,dim.y/2,dim.z/2,
2560 dim.x/2,dim.y/2,dim.z/2);
2562 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2563 -dim.x/2,-dim.y/2,dim.z/2,
2564 -dim.x/2,-dim.y/2,-dim.z/2);
2565 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2566 -dim.x/2,dim.y/2,dim.z/2,
2567 -dim.x/2,dim.y/2,-dim.z/2);
2568 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2569 dim.x/2,-dim.y/2,dim.z/2,
2570 dim.x/2,-dim.y/2,-dim.z/2);
2571 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2572 dim.x/2,dim.y/2,dim.z/2,
2573 dim.x/2,dim.y/2,-dim.z/2);