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"
23 /* potential includes */
24 #include "potentials/harmonic_oscillator.h"
25 #include "potentials/lennard_jones.h"
26 #include "potentials/albe.h"
28 #include "potentials/tersoff_orig.h"
30 #include "potentials/tersoff.h"
34 * the moldyn functions
37 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
39 printf("[moldyn] init\n");
41 memset(moldyn,0,sizeof(t_moldyn));
46 rand_init(&(moldyn->random),NULL,1);
47 moldyn->random.status|=RAND_STAT_VERBOSE;
52 int moldyn_shutdown(t_moldyn *moldyn) {
54 printf("[moldyn] shutdown\n");
56 moldyn_log_shutdown(moldyn);
57 link_cell_shutdown(moldyn);
58 rand_close(&(moldyn->random));
64 int set_int_alg(t_moldyn *moldyn,u8 algo) {
66 printf("[moldyn] integration algorithm: ");
69 case MOLDYN_INTEGRATE_VERLET:
70 moldyn->integrate=velocity_verlet;
71 printf("velocity verlet\n");
74 printf("unknown integration algorithm: %02x\n",algo);
82 int set_cutoff(t_moldyn *moldyn,double cutoff) {
84 moldyn->cutoff=cutoff;
85 moldyn->cutoff_square=cutoff*cutoff;
87 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
92 int set_temperature(t_moldyn *moldyn,double t_ref) {
96 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
101 int set_pressure(t_moldyn *moldyn,double p_ref) {
105 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
110 int set_p_scale(t_moldyn *moldyn,u8 ptype,double ptc) {
112 moldyn->pt_scale&=(~(P_SCALE_MASK));
113 moldyn->pt_scale|=ptype;
116 printf("[moldyn] p scaling:\n");
118 printf(" p: %s",ptype?"yes":"no ");
120 printf(" | type: %02x | factor: %f",ptype,ptc);
126 int set_t_scale(t_moldyn *moldyn,u8 ttype,double ttc) {
128 moldyn->pt_scale&=(~(T_SCALE_MASK));
129 moldyn->pt_scale|=ttype;
132 printf("[moldyn] t scaling:\n");
134 printf(" t: %s",ttype?"yes":"no ");
136 printf(" | type: %02x | factor: %f",ttype,ttc);
142 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
144 moldyn->pt_scale=(ptype|ttype);
148 printf("[moldyn] p/t scaling:\n");
150 printf(" p: %s",ptype?"yes":"no ");
152 printf(" | type: %02x | factor: %f",ptype,ptc);
155 printf(" t: %s",ttype?"yes":"no ");
157 printf(" | type: %02x | factor: %f",ttype,ttc);
163 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
169 moldyn->volume=x*y*z;
177 printf("[moldyn] dimensions in A and A^3 respectively:\n");
178 printf(" x: %f\n",moldyn->dim.x);
179 printf(" y: %f\n",moldyn->dim.y);
180 printf(" z: %f\n",moldyn->dim.z);
181 printf(" volume: %f\n",moldyn->volume);
182 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
187 int set_nn_dist(t_moldyn *moldyn,double dist) {
194 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
196 printf("[moldyn] periodic boundary conditions:\n");
199 moldyn->status|=MOLDYN_STAT_PBX;
202 moldyn->status|=MOLDYN_STAT_PBY;
205 moldyn->status|=MOLDYN_STAT_PBZ;
207 printf(" x: %s\n",x?"yes":"no");
208 printf(" y: %s\n",y?"yes":"no");
209 printf(" z: %s\n",z?"yes":"no");
214 int set_potential(t_moldyn *moldyn,u8 type) {
217 case MOLDYN_POTENTIAL_TM:
218 moldyn->func1b=tersoff_mult_1bp;
219 moldyn->func3b_j1=tersoff_mult_3bp_j1;
220 moldyn->func3b_k1=tersoff_mult_3bp_k1;
221 moldyn->func3b_j2=tersoff_mult_3bp_j2;
222 moldyn->func3b_k2=tersoff_mult_3bp_k2;
223 // missing: check 2b bond func
225 case MOLDYN_POTENTIAL_AM:
226 moldyn->func3b_j1=albe_mult_3bp_j1;
227 moldyn->func3b_k1=albe_mult_3bp_k1;
228 moldyn->func3b_j2=albe_mult_3bp_j2;
229 moldyn->func3b_k2=albe_mult_3bp_k2;
230 moldyn->check_2b_bond=albe_mult_check_2b_bond;
232 case MOLDYN_POTENTIAL_HO:
233 moldyn->func2b=harmonic_oscillator;
234 moldyn->check_2b_bond=harmonic_oscillator_check_2b_bond;
236 case MOLDYN_POTENTIAL_LJ:
237 moldyn->func2b=lennard_jones;
238 moldyn->check_2b_bond=lennard_jones_check_2b_bond;
241 printf("[moldyn] set potential: unknown type %02x\n",
249 int set_avg_skip(t_moldyn *moldyn,int skip) {
251 printf("[moldyn] skip %d steps before starting average calc\n",skip);
252 moldyn->avg_skip=skip;
257 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
259 strncpy(moldyn->vlsdir,dir,127);
264 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
266 strncpy(moldyn->rauthor,author,63);
267 strncpy(moldyn->rtitle,title,63);
272 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
277 printf("[moldyn] set log: ");
280 case LOG_TOTAL_ENERGY:
281 moldyn->ewrite=timer;
282 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
283 moldyn->efd=open(filename,
284 O_WRONLY|O_CREAT|O_EXCL,
287 perror("[moldyn] energy log fd open");
290 dprintf(moldyn->efd,"# total energy log file\n");
291 printf("total energy (%d)\n",timer);
293 case LOG_TOTAL_MOMENTUM:
294 moldyn->mwrite=timer;
295 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
296 moldyn->mfd=open(filename,
297 O_WRONLY|O_CREAT|O_EXCL,
300 perror("[moldyn] momentum log fd open");
303 dprintf(moldyn->efd,"# total momentum log file\n");
304 printf("total momentum (%d)\n",timer);
307 moldyn->pwrite=timer;
308 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
309 moldyn->pfd=open(filename,
310 O_WRONLY|O_CREAT|O_EXCL,
313 perror("[moldyn] pressure log file\n");
316 dprintf(moldyn->pfd,"# pressure log file\n");
317 printf("pressure (%d)\n",timer);
319 case LOG_TEMPERATURE:
320 moldyn->twrite=timer;
321 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
322 moldyn->tfd=open(filename,
323 O_WRONLY|O_CREAT|O_EXCL,
326 perror("[moldyn] temperature log file\n");
329 dprintf(moldyn->tfd,"# temperature log file\n");
330 printf("temperature (%d)\n",timer);
333 moldyn->vwrite=timer;
334 snprintf(filename,127,"%s/volume",moldyn->vlsdir);
335 moldyn->vfd=open(filename,
336 O_WRONLY|O_CREAT|O_EXCL,
339 perror("[moldyn] volume log file\n");
342 dprintf(moldyn->vfd,"# volume log file\n");
343 printf("volume (%d)\n",timer);
346 moldyn->swrite=timer;
347 printf("save file (%d)\n",timer);
350 moldyn->awrite=timer;
351 ret=visual_init(moldyn,moldyn->vlsdir);
353 printf("[moldyn] visual init failure\n");
356 printf("visual file (%d)\n",timer);
359 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
360 moldyn->rfd=open(filename,
361 O_WRONLY|O_CREAT|O_EXCL,
364 perror("[moldyn] report fd open");
367 printf("report -> ");
369 snprintf(filename,127,"%s/e_plot.scr",
371 moldyn->epfd=open(filename,
372 O_WRONLY|O_CREAT|O_EXCL,
375 perror("[moldyn] energy plot fd open");
378 dprintf(moldyn->epfd,e_plot_script);
383 snprintf(filename,127,"%s/pressure_plot.scr",
385 moldyn->ppfd=open(filename,
386 O_WRONLY|O_CREAT|O_EXCL,
389 perror("[moldyn] p plot fd open");
392 dprintf(moldyn->ppfd,pressure_plot_script);
397 snprintf(filename,127,"%s/temperature_plot.scr",
399 moldyn->tpfd=open(filename,
400 O_WRONLY|O_CREAT|O_EXCL,
403 perror("[moldyn] t plot fd open");
406 dprintf(moldyn->tpfd,temperature_plot_script);
408 printf("temperature ");
410 dprintf(moldyn->rfd,report_start,
411 moldyn->rauthor,moldyn->rtitle);
415 printf("unknown log type: %02x\n",type);
422 int moldyn_log_shutdown(t_moldyn *moldyn) {
426 printf("[moldyn] log shutdown\n");
430 dprintf(moldyn->rfd,report_energy);
431 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
436 if(moldyn->mfd) close(moldyn->mfd);
440 dprintf(moldyn->rfd,report_pressure);
441 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
448 dprintf(moldyn->rfd,report_temperature);
449 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
454 dprintf(moldyn->rfd,report_end);
456 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
459 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
462 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
471 * creating lattice functions
474 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
475 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
487 /* how many atoms do we expect */
488 if(type==CUBIC) new*=1;
489 if(type==FCC) new*=4;
490 if(type==DIAMOND) new*=8;
492 /* allocate space for atoms */
493 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
495 perror("[moldyn] realloc (create lattice)");
499 atom=&(moldyn->atom[count]);
501 /* no atoms on the boundaries (only reason: it looks better!) */
515 set_nn_dist(moldyn,lc);
516 ret=cubic_init(a,b,c,lc,atom,&orig);
517 strcpy(name,"cubic");
521 v3_scale(&orig,&orig,0.5);
522 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
523 ret=fcc_init(a,b,c,lc,atom,&orig);
528 v3_scale(&orig,&orig,0.25);
529 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
530 ret=diamond_init(a,b,c,lc,atom,&orig);
531 strcpy(name,"diamond");
534 printf("unknown lattice type (%02x)\n",type);
540 printf("[moldyn] creating lattice failed\n");
541 printf(" amount of atoms\n");
542 printf(" - expected: %d\n",new);
543 printf(" - created: %d\n",ret);
548 printf("[moldyn] created %s lattice with %d atoms\n",name,new);
550 for(ret=0;ret<new;ret++) {
551 atom[ret].element=element;
554 atom[ret].brand=brand;
555 atom[ret].tag=count+ret;
556 check_per_bound(moldyn,&(atom[ret].r));
557 atom[ret].r_0=atom[ret].r;
560 /* update total system mass */
561 total_mass_calc(moldyn);
566 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
567 t_3dvec *r,t_3dvec *v) {
574 count=(moldyn->count)++; // asshole style!
576 ptr=realloc(atom,(count+1)*sizeof(t_atom));
578 perror("[moldyn] realloc (add atom)");
585 /* initialize new atom */
586 memset(&(atom[count]),0,sizeof(t_atom));
589 atom[count].element=element;
590 atom[count].mass=mass;
591 atom[count].brand=brand;
592 atom[count].tag=count;
593 atom[count].attr=attr;
594 check_per_bound(moldyn,&(atom[count].r));
595 atom[count].r_0=atom[count].r;
597 /* update total system mass */
598 total_mass_calc(moldyn);
603 int del_atom(t_moldyn *moldyn,int tag) {
610 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
612 perror("[moldyn]malloc (del atom)");
616 for(cnt=0;cnt<tag;cnt++)
619 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
621 new[cnt-1].tag=cnt-1;
633 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
652 v3_copy(&(atom[count].r),&r);
661 for(i=0;i<count;i++) {
662 atom[i].r.x-=(a*lc)/2.0;
663 atom[i].r.y-=(b*lc)/2.0;
664 atom[i].r.z-=(c*lc)/2.0;
670 /* fcc lattice init */
671 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
684 /* construct the basis */
685 memset(basis,0,3*sizeof(t_3dvec));
693 /* fill up the room */
701 v3_copy(&(atom[count].r),&r);
704 /* the three face centered atoms */
706 v3_add(&n,&r,&basis[l]);
707 v3_copy(&(atom[count].r),&n);
716 /* coordinate transformation */
717 for(i=0;i<count;i++) {
718 atom[i].r.x-=(a*lc)/2.0;
719 atom[i].r.y-=(b*lc)/2.0;
720 atom[i].r.z-=(c*lc)/2.0;
726 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
731 count=fcc_init(a,b,c,lc,atom,origin);
737 if(origin) v3_add(&o,&o,origin);
739 count+=fcc_init(a,b,c,lc,&atom[count],&o);
744 int destroy_atoms(t_moldyn *moldyn) {
746 if(moldyn->atom) free(moldyn->atom);
751 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
754 * - gaussian distribution of velocities
755 * - zero total momentum
756 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
761 t_3dvec p_total,delta;
766 random=&(moldyn->random);
768 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
770 /* gaussian distribution of velocities */
772 for(i=0;i<moldyn->count;i++) {
773 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
775 v=sigma*rand_get_gauss(random);
777 p_total.x+=atom[i].mass*v;
779 v=sigma*rand_get_gauss(random);
781 p_total.y+=atom[i].mass*v;
783 v=sigma*rand_get_gauss(random);
785 p_total.z+=atom[i].mass*v;
788 /* zero total momentum */
789 v3_scale(&p_total,&p_total,1.0/moldyn->count);
790 for(i=0;i<moldyn->count;i++) {
791 v3_scale(&delta,&p_total,1.0/atom[i].mass);
792 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
795 /* velocity scaling */
796 scale_velocity(moldyn,equi_init);
801 double total_mass_calc(t_moldyn *moldyn) {
807 for(i=0;i<moldyn->count;i++)
808 moldyn->mass+=moldyn->atom[i].mass;
813 double temperature_calc(t_moldyn *moldyn) {
815 /* assume up to date kinetic energy, which is 3/2 N k_B T */
817 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
822 double get_temperature(t_moldyn *moldyn) {
827 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
837 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
840 /* get kinetic energy / temperature & count involved atoms */
843 for(i=0;i<moldyn->count;i++) {
844 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
845 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
850 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
851 else return 0; /* no atoms involved in scaling! */
853 /* (temporary) hack for e,t = 0 */
856 if(moldyn->t_ref!=0.0) {
857 thermal_init(moldyn,equi_init);
861 return 0; /* no scaling needed */
865 /* get scaling factor */
866 scale=moldyn->t_ref/moldyn->t;
870 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
871 scale=1.0+(scale-1.0)/moldyn->t_tc;
874 /* velocity scaling */
875 for(i=0;i<moldyn->count;i++) {
876 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
877 v3_scale(&(atom[i].v),&(atom[i].v),scale);
883 double ideal_gas_law_pressure(t_moldyn *moldyn) {
887 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
892 double virial_sum(t_moldyn *moldyn) {
897 /* virial (sum over atom virials) */
905 for(i=0;i<moldyn->count;i++) {
906 virial=&(moldyn->atom[i].virial);
907 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
908 moldyn->vir.xx+=virial->xx;
909 moldyn->vir.yy+=virial->yy;
910 moldyn->vir.zz+=virial->zz;
911 moldyn->vir.xy+=virial->xy;
912 moldyn->vir.xz+=virial->xz;
913 moldyn->vir.yz+=virial->yz;
916 /* global virial (absolute coordinates) */
917 virial=&(moldyn->gvir);
918 moldyn->gv=virial->xx+virial->yy+virial->zz;
920 return moldyn->virial;
923 double pressure_calc(t_moldyn *moldyn) {
927 * with W = 1/3 sum_i f_i r_i (- skipped!)
928 * virial = sum_i f_i r_i
930 * => P = (2 Ekin + virial) / (3V)
933 /* assume up to date virial & up to date kinetic energy */
935 /* pressure (atom virials) */
936 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
937 moldyn->p/=(3.0*moldyn->volume);
939 /* pressure (absolute coordinates) */
940 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
941 moldyn->gp/=(3.0*moldyn->volume);
946 int average_reset(t_moldyn *moldyn) {
948 printf("[moldyn] average reset\n");
950 /* update skip value */
951 moldyn->avg_skip=moldyn->total_steps;
957 /* potential energy */
965 moldyn->virial_sum=0.0;
976 int average_and_fluctuation_calc(t_moldyn *moldyn) {
980 if(moldyn->total_steps<moldyn->avg_skip)
983 denom=moldyn->total_steps+1-moldyn->avg_skip;
985 /* assume up to date energies, temperature, pressure etc */
988 moldyn->k_sum+=moldyn->ekin;
989 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
990 moldyn->k_avg=moldyn->k_sum/denom;
991 moldyn->k2_avg=moldyn->k2_sum/denom;
992 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
994 /* potential energy */
995 moldyn->v_sum+=moldyn->energy;
996 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
997 moldyn->v_avg=moldyn->v_sum/denom;
998 moldyn->v2_avg=moldyn->v2_sum/denom;
999 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1002 moldyn->t_sum+=moldyn->t;
1003 moldyn->t_avg=moldyn->t_sum/denom;
1006 moldyn->virial_sum+=moldyn->virial;
1007 moldyn->virial_avg=moldyn->virial_sum/denom;
1008 moldyn->gv_sum+=moldyn->gv;
1009 moldyn->gv_avg=moldyn->gv_sum/denom;
1012 moldyn->p_sum+=moldyn->p;
1013 moldyn->p_avg=moldyn->p_sum/denom;
1014 moldyn->gp_sum+=moldyn->gp;
1015 moldyn->gp_avg=moldyn->gp_sum/denom;
1016 moldyn->tp_sum+=moldyn->tp;
1017 moldyn->tp_avg=moldyn->tp_sum/denom;
1022 int get_heat_capacity(t_moldyn *moldyn) {
1026 /* averages needed for heat capacity calc */
1027 if(moldyn->total_steps<moldyn->avg_skip)
1030 /* (temperature average)^2 */
1031 temp2=moldyn->t_avg*moldyn->t_avg;
1032 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1035 /* ideal gas contribution */
1036 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1037 printf(" ideal gas contribution: %f\n",
1038 ighc/moldyn->mass*KILOGRAM/JOULE);
1040 /* specific heat for nvt ensemble */
1041 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1042 moldyn->c_v_nvt/=moldyn->mass;
1044 /* specific heat for nve ensemble */
1045 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1046 moldyn->c_v_nve/=moldyn->mass;
1048 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1049 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1050 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)));
1055 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1071 /* store atomic configuration + dimension */
1072 store=malloc(moldyn->count*sizeof(t_atom));
1074 printf("[moldyn] allocating store mem failed\n");
1077 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1082 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1083 su=pow(2.0-h,ONE_THIRD)-1.0;
1084 dv=(1.0-h)*moldyn->volume;
1086 /* scale up dimension and atom positions */
1087 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1088 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1089 link_cell_shutdown(moldyn);
1090 link_cell_init(moldyn,QUIET);
1091 potential_force_calc(moldyn);
1094 /* restore atomic configuration + dim */
1095 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1098 /* scale down dimension and atom positions */
1099 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1100 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1101 link_cell_shutdown(moldyn);
1102 link_cell_init(moldyn,QUIET);
1103 potential_force_calc(moldyn);
1106 /* calculate pressure */
1107 moldyn->tp=-(y1-y0)/(2.0*dv);
1109 /* restore atomic configuration */
1110 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1112 link_cell_shutdown(moldyn);
1113 link_cell_init(moldyn,QUIET);
1114 //potential_force_calc(moldyn);
1116 /* free store buffer */
1123 double get_pressure(t_moldyn *moldyn) {
1129 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1141 if(x) dim->x*=scale;
1142 if(y) dim->y*=scale;
1143 if(z) dim->z*=scale;
1148 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1159 for(i=0;i<moldyn->count;i++) {
1160 r=&(moldyn->atom[i].r);
1169 int scale_volume(t_moldyn *moldyn) {
1175 vdim=&(moldyn->vis.dim);
1179 /* scaling factor */
1180 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1181 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc;
1182 scale=pow(scale,ONE_THIRD);
1185 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1188 /* scale the atoms and dimensions */
1189 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1190 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1192 /* visualize dimensions */
1199 /* recalculate scaled volume */
1200 moldyn->volume=dim->x*dim->y*dim->z;
1202 /* adjust/reinit linkcell */
1203 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1204 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1205 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1206 link_cell_shutdown(moldyn);
1207 link_cell_init(moldyn,QUIET);
1218 double e_kin_calc(t_moldyn *moldyn) {
1226 for(i=0;i<moldyn->count;i++) {
1227 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1228 moldyn->ekin+=atom[i].ekin;
1231 return moldyn->ekin;
1234 double get_total_energy(t_moldyn *moldyn) {
1236 return(moldyn->ekin+moldyn->energy);
1239 t_3dvec get_total_p(t_moldyn *moldyn) {
1248 for(i=0;i<moldyn->count;i++) {
1249 v3_scale(&p,&(atom[i].v),atom[i].mass);
1250 v3_add(&p_total,&p_total,&p);
1256 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1260 /* nn_dist is the nearest neighbour distance */
1262 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1271 /* linked list / cell method */
1273 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1280 /* partitioning the md cell */
1281 lc->nx=moldyn->dim.x/moldyn->cutoff;
1282 lc->x=moldyn->dim.x/lc->nx;
1283 lc->ny=moldyn->dim.y/moldyn->cutoff;
1284 lc->y=moldyn->dim.y/lc->ny;
1285 lc->nz=moldyn->dim.z/moldyn->cutoff;
1286 lc->z=moldyn->dim.z/lc->nz;
1287 lc->cells=lc->nx*lc->ny*lc->nz;
1290 lc->subcell=malloc(lc->cells*sizeof(int*));
1292 lc->subcell=malloc(lc->cells*sizeof(t_list));
1295 if(lc->subcell==NULL) {
1296 perror("[moldyn] cell init (malloc)");
1301 printf("[moldyn] FATAL: less then 27 subcells!\n");
1305 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1308 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1311 printf(" x: %d x %f A\n",lc->nx,lc->x);
1312 printf(" y: %d x %f A\n",lc->ny,lc->y);
1313 printf(" z: %d x %f A\n",lc->nz,lc->z);
1318 for(i=0;i<lc->cells;i++) {
1319 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1320 if(lc->subcell[i]==NULL) {
1321 perror("[moldyn] list init (malloc)");
1326 printf(" ---> %d malloc %p (%p)\n",
1327 i,lc->subcell[0],lc->subcell);
1331 for(i=0;i<lc->cells;i++)
1332 list_init_f(&(lc->subcell[i]));
1335 /* update the list */
1336 link_cell_update(moldyn);
1341 int link_cell_update(t_moldyn *moldyn) {
1357 for(i=0;i<lc->cells;i++)
1359 memset(lc->subcell[i],0,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1361 list_destroy_f(&(lc->subcell[i]));
1364 for(count=0;count<moldyn->count;count++) {
1365 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1366 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1367 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1371 while(lc->subcell[i+j*nx+k*nx*ny][p]!=0)
1374 if(p>=MAX_ATOMS_PER_LIST) {
1375 printf("[moldyn] FATAL: amount of atoms too high!\n");
1379 lc->subcell[i+j*nx+k*nx*ny][p]=count;
1381 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1385 printf(" ---> %d %d malloc %p (%p)\n",
1386 i,count,lc->subcell[i].current,lc->subcell);
1394 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1418 if(i>=nx||j>=ny||k>=nz)
1419 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1422 cell[0]=lc->subcell[i+j*nx+k*a];
1423 for(ci=-1;ci<=1;ci++) {
1426 if((x<0)||(x>=nx)) {
1430 for(cj=-1;cj<=1;cj++) {
1433 if((y<0)||(y>=ny)) {
1437 for(ck=-1;ck<=1;ck++) {
1440 if((z<0)||(z>=nz)) {
1444 if(!(ci|cj|ck)) continue;
1446 cell[--count2]=lc->subcell[x+y*nx+z*a];
1449 cell[count1++]=lc->subcell[x+y*nx+z*a];
1460 int link_cell_shutdown(t_moldyn *moldyn) {
1467 for(i=0;i<lc->cells;i++) {
1469 free(lc->subcell[i]);
1471 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1472 list_destroy_f(&(lc->subcell[i]));
1481 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1485 t_moldyn_schedule *schedule;
1487 schedule=&(moldyn->schedule);
1488 count=++(schedule->total_sched);
1490 ptr=realloc(schedule->runs,count*sizeof(int));
1492 perror("[moldyn] realloc (runs)");
1496 schedule->runs[count-1]=runs;
1498 ptr=realloc(schedule->tau,count*sizeof(double));
1500 perror("[moldyn] realloc (tau)");
1504 schedule->tau[count-1]=tau;
1506 printf("[moldyn] schedule added:\n");
1507 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1513 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1515 moldyn->schedule.hook=hook;
1516 moldyn->schedule.hook_params=hook_params;
1523 * 'integration of newtons equation' - algorithms
1527 /* start the integration */
1529 int moldyn_integrate(t_moldyn *moldyn) {
1532 unsigned int e,m,s,v,p,t,a;
1534 t_moldyn_schedule *sched;
1539 double energy_scale;
1540 struct timeval t1,t2;
1543 sched=&(moldyn->schedule);
1546 /* initialize linked cell method */
1547 link_cell_init(moldyn,VERBOSE);
1549 /* logging & visualization */
1558 /* sqaure of some variables */
1559 moldyn->tau_square=moldyn->tau*moldyn->tau;
1561 /* get current time */
1562 gettimeofday(&t1,NULL);
1564 /* calculate initial forces */
1565 potential_force_calc(moldyn);
1570 /* some stupid checks before we actually start calculating bullshit */
1571 if(moldyn->cutoff>0.5*moldyn->dim.x)
1572 printf("[moldyn] WARNING: cutoff > 0.5 x dim.x\n");
1573 if(moldyn->cutoff>0.5*moldyn->dim.y)
1574 printf("[moldyn] WARNING: cutoff > 0.5 x dim.y\n");
1575 if(moldyn->cutoff>0.5*moldyn->dim.z)
1576 printf("[moldyn] WARNING: cutoff > 0.5 x dim.z\n");
1577 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1578 if(ds>0.05*moldyn->nnd)
1579 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1581 /* zero absolute time */
1582 // should have right values!
1584 //moldyn->total_steps=0;
1586 /* debugging, ignore */
1589 /* tell the world */
1590 printf("[moldyn] integration start, go get a coffee ...\n");
1592 /* executing the schedule */
1594 while(sched->count<sched->total_sched) {
1596 /* setting amount of runs and finite time step size */
1597 moldyn->tau=sched->tau[sched->count];
1598 moldyn->tau_square=moldyn->tau*moldyn->tau;
1599 moldyn->time_steps=sched->runs[sched->count];
1601 /* energy scaling factor (might change!) */
1602 energy_scale=moldyn->count*EV;
1604 /* integration according to schedule */
1606 for(i=0;i<moldyn->time_steps;i++) {
1608 /* integration step */
1609 moldyn->integrate(moldyn);
1611 /* calculate kinetic energy, temperature and pressure */
1613 temperature_calc(moldyn);
1615 pressure_calc(moldyn);
1617 thermodynamic_pressure_calc(moldyn);
1618 printf("\n\nDEBUG: numeric pressure calc: %f\n\n",
1622 /* calculate fluctuations + averages */
1623 average_and_fluctuation_calc(moldyn);
1626 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1627 scale_velocity(moldyn,FALSE);
1628 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1629 scale_volume(moldyn);
1631 /* check for log & visualization */
1633 if(!(moldyn->total_steps%e))
1634 dprintf(moldyn->efd,
1636 moldyn->time,moldyn->ekin/energy_scale,
1637 moldyn->energy/energy_scale,
1638 get_total_energy(moldyn)/energy_scale);
1641 if(!(moldyn->total_steps%m)) {
1642 momentum=get_total_p(moldyn);
1643 dprintf(moldyn->mfd,
1644 "%f %f %f %f %f\n",moldyn->time,
1645 momentum.x,momentum.y,momentum.z,
1646 v3_norm(&momentum));
1650 if(!(moldyn->total_steps%p)) {
1651 dprintf(moldyn->pfd,
1652 "%f %f %f %f %f %f %f\n",moldyn->time,
1653 moldyn->p/BAR,moldyn->p_avg/BAR,
1654 moldyn->gp/BAR,moldyn->gp_avg/BAR,
1655 moldyn->tp/BAR,moldyn->tp_avg/BAR);
1659 if(!(moldyn->total_steps%t)) {
1660 dprintf(moldyn->tfd,
1662 moldyn->time,moldyn->t,moldyn->t_avg);
1666 if(!(moldyn->total_steps%v)) {
1667 dprintf(moldyn->vfd,
1668 "%f %f\n",moldyn->time,moldyn->volume);
1672 if(!(moldyn->total_steps%s)) {
1673 snprintf(dir,128,"%s/s-%07.f.save",
1674 moldyn->vlsdir,moldyn->time);
1675 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
1677 if(fd<0) perror("[moldyn] save fd open");
1679 write(fd,moldyn,sizeof(t_moldyn));
1680 write(fd,moldyn->atom,
1681 moldyn->count*sizeof(t_atom));
1687 if(!(moldyn->total_steps%a)) {
1688 visual_atoms(moldyn);
1692 /* display progress */
1693 //if(!(moldyn->total_steps%10)) {
1694 /* get current time */
1695 gettimeofday(&t2,NULL);
1697 printf("\rsched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)",
1698 sched->count,i,moldyn->total_steps,
1699 moldyn->t,moldyn->t_avg,
1700 moldyn->p/BAR,moldyn->p_avg/BAR,
1702 (int)(t2.tv_sec-t1.tv_sec));
1706 /* copy over time */
1710 /* increase absolute time */
1711 moldyn->time+=moldyn->tau;
1712 moldyn->total_steps+=1;
1716 /* check for hooks */
1718 printf("\n ## schedule hook %d start ##\n",
1720 sched->hook(moldyn,sched->hook_params);
1721 printf(" ## schedule hook end ##\n");
1724 /* increase the schedule counter */
1732 /* velocity verlet */
1734 int velocity_verlet(t_moldyn *moldyn) {
1737 double tau,tau_square,h;
1742 count=moldyn->count;
1744 tau_square=moldyn->tau_square;
1746 for(i=0;i<count;i++) {
1747 /* check whether fixed atom */
1748 if(atom[i].attr&ATOM_ATTR_FP)
1752 v3_scale(&delta,&(atom[i].v),tau);
1753 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1754 v3_scale(&delta,&(atom[i].f),h*tau_square);
1755 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1756 check_per_bound(moldyn,&(atom[i].r));
1758 /* velocities [actually v(t+tau/2)] */
1759 v3_scale(&delta,&(atom[i].f),h*tau);
1760 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1763 /* criticial check */
1764 moldyn_bc_check(moldyn);
1766 /* neighbour list update */
1767 link_cell_update(moldyn);
1769 /* forces depending on chosen potential */
1770 potential_force_calc(moldyn);
1772 for(i=0;i<count;i++) {
1773 /* check whether fixed atom */
1774 if(atom[i].attr&ATOM_ATTR_FP)
1776 /* again velocities [actually v(t+tau)] */
1777 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1778 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1787 * potentials & corresponding forces & virial routine
1791 /* generic potential and force calculation */
1793 int potential_force_calc(t_moldyn *moldyn) {
1796 t_atom *itom,*jtom,*ktom;
1800 int *neighbour_i[27];
1804 t_list neighbour_i[27];
1805 t_list neighbour_i2[27];
1811 count=moldyn->count;
1821 /* reset global virial */
1822 memset(&(moldyn->gvir),0,sizeof(t_virial));
1824 /* reset force, site energy and virial of every atom */
1825 for(i=0;i<count;i++) {
1828 v3_zero(&(itom[i].f));
1831 virial=(&(itom[i].virial));
1839 /* reset site energy */
1844 /* get energy, force and virial of every atom */
1846 /* first (and only) loop over atoms i */
1847 for(i=0;i<count;i++) {
1849 /* single particle potential/force */
1850 if(itom[i].attr&ATOM_ATTR_1BP)
1852 moldyn->func1b(moldyn,&(itom[i]));
1854 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1857 /* 2 body pair potential/force */
1859 link_cell_neighbour_index(moldyn,
1860 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1861 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1862 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1867 /* first loop over atoms j */
1868 if(moldyn->func2b) {
1875 while(neighbour_i[j][p]!=0) {
1877 jtom=&(atom[neighbour_i[j][p]]);
1880 if(jtom==&(itom[i]))
1883 if((jtom->attr&ATOM_ATTR_2BP)&
1884 (itom[i].attr&ATOM_ATTR_2BP)) {
1885 moldyn->func2b(moldyn,
1892 this=&(neighbour_i[j]);
1895 if(this->start==NULL)
1899 jtom=this->current->data;
1901 if(jtom==&(itom[i]))
1904 if((jtom->attr&ATOM_ATTR_2BP)&
1905 (itom[i].attr&ATOM_ATTR_2BP)) {
1906 moldyn->func2b(moldyn,
1911 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1917 /* 3 body potential/force */
1919 if(!(itom[i].attr&ATOM_ATTR_3BP))
1922 /* copy the neighbour lists */
1924 /* no copy needed for static lists */
1926 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
1929 /* second loop over atoms j */
1936 while(neighbour_i[j][p]!=0) {
1938 jtom=&(atom[neighbour_i[j][p]]);
1941 this=&(neighbour_i[j]);
1944 if(this->start==NULL)
1949 jtom=this->current->data;
1952 if(jtom==&(itom[i]))
1955 if(!(jtom->attr&ATOM_ATTR_3BP))
1961 if(moldyn->func3b_j1)
1962 moldyn->func3b_j1(moldyn,
1967 /* in first j loop, 3bp run can be skipped */
1968 if(!(moldyn->run3bp))
1971 /* first loop over atoms k */
1972 if(moldyn->func3b_k1) {
1980 while(neighbour_i[j][q]!=0) {
1982 ktom=&(atom[neighbour_i[k][q]]);
1985 that=&(neighbour_i2[k]);
1988 if(that->start==NULL)
1992 ktom=that->current->data;
1995 if(!(ktom->attr&ATOM_ATTR_3BP))
2001 if(ktom==&(itom[i]))
2004 moldyn->func3b_k1(moldyn,
2012 } while(list_next_f(that)!=\
2020 if(moldyn->func3b_j2)
2021 moldyn->func3b_j2(moldyn,
2026 /* second loop over atoms k */
2027 if(moldyn->func3b_k2) {
2035 while(neighbour_i[j][q]!=0) {
2037 ktom=&(atom[neighbour_i[k][q]]);
2040 that=&(neighbour_i2[k]);
2043 if(that->start==NULL)
2047 ktom=that->current->data;
2050 if(!(ktom->attr&ATOM_ATTR_3BP))
2056 if(ktom==&(itom[i]))
2059 moldyn->func3b_k2(moldyn,
2068 } while(list_next_f(that)!=\
2076 /* 2bp post function */
2077 if(moldyn->func3b_j3) {
2078 moldyn->func3b_j3(moldyn,
2085 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2100 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2101 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2103 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
2104 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
2105 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
2109 /* some postprocessing */
2110 for(i=0;i<count;i++) {
2111 /* calculate global virial */
2112 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
2113 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
2114 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
2115 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
2116 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
2117 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
2119 /* check forces regarding the given timestep */
2120 if(v3_norm(&(itom[i].f))>\
2121 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
2122 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",
2130 * virial calculation
2133 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2134 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2136 a->virial.xx+=f->x*d->x;
2137 a->virial.yy+=f->y*d->y;
2138 a->virial.zz+=f->z*d->z;
2139 a->virial.xy+=f->x*d->y;
2140 a->virial.xz+=f->x*d->z;
2141 a->virial.yz+=f->y*d->z;
2147 * periodic boundary checking
2150 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2151 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2162 if(moldyn->status&MOLDYN_STAT_PBX) {
2163 if(a->x>=x) a->x-=dim->x;
2164 else if(-a->x>x) a->x+=dim->x;
2166 if(moldyn->status&MOLDYN_STAT_PBY) {
2167 if(a->y>=y) a->y-=dim->y;
2168 else if(-a->y>y) a->y+=dim->y;
2170 if(moldyn->status&MOLDYN_STAT_PBZ) {
2171 if(a->z>=z) a->z-=dim->z;
2172 else if(-a->z>z) a->z+=dim->z;
2179 * debugging / critical check functions
2182 int moldyn_bc_check(t_moldyn *moldyn) {
2195 for(i=0;i<moldyn->count;i++) {
2196 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2197 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2198 i,atom[i].r.x,dim->x/2);
2199 printf("diagnostic:\n");
2200 printf("-----------\natom.r.x:\n");
2202 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2205 ((byte)&(1<<k))?1:0,
2208 printf("---------------\nx=dim.x/2:\n");
2210 memcpy(&byte,(u8 *)(&x)+j,1);
2213 ((byte)&(1<<k))?1:0,
2216 if(atom[i].r.x==x) printf("the same!\n");
2217 else printf("different!\n");
2219 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2220 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2221 i,atom[i].r.y,dim->y/2);
2222 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2223 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2224 i,atom[i].r.z,dim->z/2);
2234 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2241 fd=open(file,O_RDONLY);
2243 perror("[moldyn] load save file open");
2247 fsize=lseek(fd,0,SEEK_END);
2248 lseek(fd,0,SEEK_SET);
2250 size=sizeof(t_moldyn);
2253 cnt=read(fd,moldyn,size);
2255 perror("[moldyn] load save file read (moldyn)");
2261 size=moldyn->count*sizeof(t_atom);
2263 /* correcting possible atom data offset */
2265 if(fsize!=sizeof(t_moldyn)+size) {
2266 corr=fsize-sizeof(t_moldyn)-size;
2267 printf("[moldyn] WARNING: lsf (illegal file size)\n");
2268 printf(" moifying offset:\n");
2269 printf(" - current pos: %d\n",sizeof(t_moldyn));
2270 printf(" - atom size: %d\n",size);
2271 printf(" - file size: %d\n",fsize);
2272 printf(" => correction: %d\n",corr);
2273 lseek(fd,corr,SEEK_CUR);
2276 moldyn->atom=(t_atom *)malloc(size);
2277 if(moldyn->atom==NULL) {
2278 perror("[moldyn] load save file malloc (atoms)");
2283 cnt=read(fd,moldyn->atom,size);
2285 perror("[moldyn] load save file read (atoms)");
2296 int moldyn_free_save_file(t_moldyn *moldyn) {
2303 int moldyn_load(t_moldyn *moldyn) {
2311 * function to find/callback all combinations of 2 body bonds
2314 int process_2b_bonds(t_moldyn *moldyn,void *data,
2315 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2316 void *data,u8 bc)) {
2323 t_list neighbour[27];
2333 for(i=0;i<moldyn->count;i++) {
2334 /* neighbour indexing */
2335 link_cell_neighbour_index(moldyn,
2336 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2337 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2338 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2343 bc=(j<lc->dnlc)?0:1;
2348 while(neighbour[j][p]!=0) {
2350 jtom=&(moldyn->atom[neighbour[j][p]]);
2353 this=&(neighbour[j]);
2356 if(this->start==NULL)
2361 jtom=this->current->data;
2365 process(moldyn,&(itom[i]),jtom,data,bc);
2370 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2380 * post processing functions
2383 int get_line(int fd,char *line,int max) {
2390 if(count==max) return count;
2391 ret=read(fd,line+count,1);
2392 if(ret<=0) return ret;
2393 if(line[count]=='\n') {
2394 memset(line+count,0,max-count-1);
2402 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2408 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
2424 for(i=0;i<moldyn->count;i++) {
2426 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
2427 check_per_bound(moldyn,&dist);
2428 d2=v3_absolute_square(&dist);
2442 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
2443 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
2444 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
2449 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
2454 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
2455 t_atom *jtom,void *data,u8 bc) {
2462 /* only count pairs once,
2463 * skip same atoms */
2464 if(itom->tag>=jtom->tag)
2468 * pair correlation calc
2475 v3_sub(&dist,&(jtom->r),&(itom->r));
2476 if(bc) check_per_bound(moldyn,&dist);
2477 d=v3_absolute_square(&dist);
2479 /* ignore if greater cutoff */
2480 if(d>moldyn->cutoff_square)
2483 /* fill the slots */
2487 /* should never happen but it does 8) -
2488 * related to -ffloat-store problem! */
2490 printf("[moldyn] WARNING: pcc (%d/%d)",
2496 if(itom->brand!=jtom->brand) {
2501 /* type a - type a bonds */
2503 pcc->stat[s+pcc->o1]+=1;
2505 /* type b - type b bonds */
2506 pcc->stat[s+pcc->o2]+=1;
2512 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2519 pcc.o1=moldyn->cutoff/dr;
2522 if(pcc.o1*dr<=moldyn->cutoff)
2523 printf("[moldyn] WARNING: pcc (low #slots)\n");
2525 printf("[moldyn] pair correlation calc info:\n");
2526 printf(" time: %f\n",moldyn->time);
2527 printf(" count: %d\n",moldyn->count);
2528 printf(" cutoff: %f\n",moldyn->cutoff);
2529 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2532 pcc.stat=(double *)ptr;
2535 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
2536 if(pcc.stat==NULL) {
2537 perror("[moldyn] pair correlation malloc");
2542 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
2545 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
2548 for(i=1;i<pcc.o1;i++) {
2549 // normalization: 4 pi r^2 dr
2550 // here: not double counting pairs -> 2 pi r r dr
2551 // ... and actually it's a constant times r^2
2554 pcc.stat[pcc.o1+i]/=norm;
2555 pcc.stat[pcc.o2+i]/=norm;
2560 /* todo: store/print pair correlation function */
2567 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2574 if(itom->tag>=jtom->tag)
2578 v3_sub(&dist,&(jtom->r),&(itom->r));
2579 if(bc) check_per_bound(moldyn,&dist);
2580 d=v3_absolute_square(&dist);
2582 /* ignore if greater or equal cutoff */
2583 if(d>moldyn->cutoff_square)
2586 /* check for potential bond */
2587 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2590 /* now count this bonding ... */
2593 /* increase total bond counter
2594 * ... double counting!
2599 ba->acnt[jtom->tag]+=1;
2601 ba->bcnt[jtom->tag]+=1;
2604 ba->acnt[itom->tag]+=1;
2606 ba->bcnt[itom->tag]+=1;
2611 int bond_analyze(t_moldyn *moldyn,double *quality) {
2613 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
2621 ba.acnt=malloc(moldyn->count*sizeof(int));
2623 perror("[moldyn] bond analyze malloc (a)");
2626 memset(ba.acnt,0,moldyn->count*sizeof(int));
2628 ba.bcnt=malloc(moldyn->count*sizeof(int));
2630 perror("[moldyn] bond analyze malloc (b)");
2633 memset(ba.bcnt,0,moldyn->count*sizeof(int));
2642 process_2b_bonds(moldyn,&ba,bond_analyze_process);
2644 for(i=0;i<moldyn->count;i++) {
2645 if(atom[i].brand==0) {
2646 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
2650 if((ba.acnt[i]==4)&(ba.bcnt[i]==0)) {
2658 printf("[moldyn] bond analyze: c_cnt=%d | set=%d\n",ccnt,cset);
2659 printf("[moldyn] bond analyze: q_cnt=%d | tot=%d\n",qcnt,ba.tcnt);
2662 quality[0]=1.0*ccnt/cset;
2663 quality[1]=1.0*qcnt/ba.tcnt;
2666 printf("[moldyn] bond analyze: c_bnd_q=%f\n",1.0*qcnt/ba.tcnt);
2667 printf("[moldyn] bond analyze: tot_q=%f\n",1.0*qcnt/ba.tcnt);
2674 * visualization code
2677 int visual_init(t_moldyn *moldyn,char *filebase) {
2679 strncpy(moldyn->vis.fb,filebase,128);
2684 int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2691 if(itom->tag>=jtom->tag)
2694 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2697 if((itom->attr&ATOM_ATTR_VB)|(jtom->attr&ATOM_ATTR_VB))
2698 dprintf(vb->fd,"# [B] %f %f %f %f %f %f\n",
2699 itom->r.x,itom->r.y,itom->r.z,
2700 jtom->r.x,jtom->r.y,jtom->r.z);
2705 int visual_atoms(t_moldyn *moldyn) {
2723 sprintf(file,"%s/atomic_conf_%07.f.xyz",v->fb,moldyn->time);
2724 vb.fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
2726 perror("open visual save file fd");
2730 /* write the actual data file */
2733 dprintf(vb.fd,"# [P] %d %07.f <%f,%f,%f>\n",
2734 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
2736 // atomic configuration
2737 for(i=0;i<moldyn->count;i++)
2738 // atom type, positions, color and kinetic energy
2739 dprintf(vb.fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
2743 pse_col[atom[i].element],
2746 // bonds between atoms
2747 process_2b_bonds(moldyn,&vb,visual_bonds_process);
2751 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2752 -dim.x/2,-dim.y/2,-dim.z/2,
2753 dim.x/2,-dim.y/2,-dim.z/2);
2754 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2755 -dim.x/2,-dim.y/2,-dim.z/2,
2756 -dim.x/2,dim.y/2,-dim.z/2);
2757 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2758 dim.x/2,dim.y/2,-dim.z/2,
2759 dim.x/2,-dim.y/2,-dim.z/2);
2760 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2761 -dim.x/2,dim.y/2,-dim.z/2,
2762 dim.x/2,dim.y/2,-dim.z/2);
2764 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2765 -dim.x/2,-dim.y/2,dim.z/2,
2766 dim.x/2,-dim.y/2,dim.z/2);
2767 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2768 -dim.x/2,-dim.y/2,dim.z/2,
2769 -dim.x/2,dim.y/2,dim.z/2);
2770 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2771 dim.x/2,dim.y/2,dim.z/2,
2772 dim.x/2,-dim.y/2,dim.z/2);
2773 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2774 -dim.x/2,dim.y/2,dim.z/2,
2775 dim.x/2,dim.y/2,dim.z/2);
2777 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2778 -dim.x/2,-dim.y/2,dim.z/2,
2779 -dim.x/2,-dim.y/2,-dim.z/2);
2780 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2781 -dim.x/2,dim.y/2,dim.z/2,
2782 -dim.x/2,dim.y/2,-dim.z/2);
2783 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2784 dim.x/2,-dim.y/2,dim.z/2,
2785 dim.x/2,-dim.y/2,-dim.z/2);
2786 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2787 dim.x/2,dim.y/2,dim.z/2,
2788 dim.x/2,dim.y/2,-dim.z/2);