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 moldyn->check_2b_bond=tersoff_mult_check_2b_bond;
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 */
490 printf("[moldyn] WARNING: create 'none' lattice called");
492 if(type==CUBIC) new*=1;
493 if(type==FCC) new*=4;
494 if(type==DIAMOND) new*=8;
496 /* allocate space for atoms */
497 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
499 perror("[moldyn] realloc (create lattice)");
503 atom=&(moldyn->atom[count]);
505 /* no atoms on the boundaries (only reason: it looks better!) */
519 set_nn_dist(moldyn,lc);
520 ret=cubic_init(a,b,c,lc,atom,&orig);
521 strcpy(name,"cubic");
525 v3_scale(&orig,&orig,0.5);
526 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
527 ret=fcc_init(a,b,c,lc,atom,&orig);
532 v3_scale(&orig,&orig,0.25);
533 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
534 ret=diamond_init(a,b,c,lc,atom,&orig);
535 strcpy(name,"diamond");
538 printf("unknown lattice type (%02x)\n",type);
544 printf("[moldyn] creating lattice failed\n");
545 printf(" amount of atoms\n");
546 printf(" - expected: %d\n",new);
547 printf(" - created: %d\n",ret);
552 printf("[moldyn] created %s lattice with %d atoms\n",name,new);
554 for(ret=0;ret<new;ret++) {
555 atom[ret].element=element;
558 atom[ret].brand=brand;
559 atom[ret].tag=count+ret;
560 check_per_bound(moldyn,&(atom[ret].r));
561 atom[ret].r_0=atom[ret].r;
564 /* update total system mass */
565 total_mass_calc(moldyn);
570 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
571 t_3dvec *r,t_3dvec *v) {
578 count=(moldyn->count)++; // asshole style!
580 ptr=realloc(atom,(count+1)*sizeof(t_atom));
582 perror("[moldyn] realloc (add atom)");
589 /* initialize new atom */
590 memset(&(atom[count]),0,sizeof(t_atom));
593 atom[count].element=element;
594 atom[count].mass=mass;
595 atom[count].brand=brand;
596 atom[count].tag=count;
597 atom[count].attr=attr;
598 check_per_bound(moldyn,&(atom[count].r));
599 atom[count].r_0=atom[count].r;
601 /* update total system mass */
602 total_mass_calc(moldyn);
607 int del_atom(t_moldyn *moldyn,int tag) {
614 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
616 perror("[moldyn]malloc (del atom)");
620 for(cnt=0;cnt<tag;cnt++)
623 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
625 new[cnt-1].tag=cnt-1;
637 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
656 v3_copy(&(atom[count].r),&r);
665 for(i=0;i<count;i++) {
666 atom[i].r.x-=(a*lc)/2.0;
667 atom[i].r.y-=(b*lc)/2.0;
668 atom[i].r.z-=(c*lc)/2.0;
674 /* fcc lattice init */
675 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
688 /* construct the basis */
689 memset(basis,0,3*sizeof(t_3dvec));
697 /* fill up the room */
705 v3_copy(&(atom[count].r),&r);
708 /* the three face centered atoms */
710 v3_add(&n,&r,&basis[l]);
711 v3_copy(&(atom[count].r),&n);
720 /* coordinate transformation */
721 for(i=0;i<count;i++) {
722 atom[i].r.x-=(a*lc)/2.0;
723 atom[i].r.y-=(b*lc)/2.0;
724 atom[i].r.z-=(c*lc)/2.0;
730 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
735 count=fcc_init(a,b,c,lc,atom,origin);
741 if(origin) v3_add(&o,&o,origin);
743 count+=fcc_init(a,b,c,lc,&atom[count],&o);
748 int destroy_atoms(t_moldyn *moldyn) {
750 if(moldyn->atom) free(moldyn->atom);
755 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
758 * - gaussian distribution of velocities
759 * - zero total momentum
760 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
765 t_3dvec p_total,delta;
770 random=&(moldyn->random);
772 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
774 /* gaussian distribution of velocities */
776 for(i=0;i<moldyn->count;i++) {
777 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
779 v=sigma*rand_get_gauss(random);
781 p_total.x+=atom[i].mass*v;
783 v=sigma*rand_get_gauss(random);
785 p_total.y+=atom[i].mass*v;
787 v=sigma*rand_get_gauss(random);
789 p_total.z+=atom[i].mass*v;
792 /* zero total momentum */
793 v3_scale(&p_total,&p_total,1.0/moldyn->count);
794 for(i=0;i<moldyn->count;i++) {
795 v3_scale(&delta,&p_total,1.0/atom[i].mass);
796 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
799 /* velocity scaling */
800 scale_velocity(moldyn,equi_init);
805 double total_mass_calc(t_moldyn *moldyn) {
811 for(i=0;i<moldyn->count;i++)
812 moldyn->mass+=moldyn->atom[i].mass;
817 double temperature_calc(t_moldyn *moldyn) {
819 /* assume up to date kinetic energy, which is 3/2 N k_B T */
821 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
826 double get_temperature(t_moldyn *moldyn) {
831 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
841 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
844 /* get kinetic energy / temperature & count involved atoms */
847 for(i=0;i<moldyn->count;i++) {
848 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
849 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
854 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
855 else return 0; /* no atoms involved in scaling! */
857 /* (temporary) hack for e,t = 0 */
860 if(moldyn->t_ref!=0.0) {
861 thermal_init(moldyn,equi_init);
865 return 0; /* no scaling needed */
869 /* get scaling factor */
870 scale=moldyn->t_ref/moldyn->t;
874 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
875 scale=1.0+(scale-1.0)/moldyn->t_tc;
878 /* velocity scaling */
879 for(i=0;i<moldyn->count;i++) {
880 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
881 v3_scale(&(atom[i].v),&(atom[i].v),scale);
887 double ideal_gas_law_pressure(t_moldyn *moldyn) {
891 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
896 double virial_sum(t_moldyn *moldyn) {
901 /* virial (sum over atom virials) */
909 for(i=0;i<moldyn->count;i++) {
910 virial=&(moldyn->atom[i].virial);
911 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
912 moldyn->vir.xx+=virial->xx;
913 moldyn->vir.yy+=virial->yy;
914 moldyn->vir.zz+=virial->zz;
915 moldyn->vir.xy+=virial->xy;
916 moldyn->vir.xz+=virial->xz;
917 moldyn->vir.yz+=virial->yz;
920 /* global virial (absolute coordinates) */
921 virial=&(moldyn->gvir);
922 moldyn->gv=virial->xx+virial->yy+virial->zz;
924 return moldyn->virial;
927 double pressure_calc(t_moldyn *moldyn) {
931 * with W = 1/3 sum_i f_i r_i (- skipped!)
932 * virial = sum_i f_i r_i
934 * => P = (2 Ekin + virial) / (3V)
937 /* assume up to date virial & up to date kinetic energy */
939 /* pressure (atom virials) */
940 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
941 moldyn->p/=(3.0*moldyn->volume);
943 /* pressure (absolute coordinates) */
944 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
945 moldyn->gp/=(3.0*moldyn->volume);
950 int average_reset(t_moldyn *moldyn) {
952 printf("[moldyn] average reset\n");
954 /* update skip value */
955 moldyn->avg_skip=moldyn->total_steps;
961 /* potential energy */
969 moldyn->virial_sum=0.0;
980 int average_and_fluctuation_calc(t_moldyn *moldyn) {
984 if(moldyn->total_steps<moldyn->avg_skip)
987 denom=moldyn->total_steps+1-moldyn->avg_skip;
989 /* assume up to date energies, temperature, pressure etc */
992 moldyn->k_sum+=moldyn->ekin;
993 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
994 moldyn->k_avg=moldyn->k_sum/denom;
995 moldyn->k2_avg=moldyn->k2_sum/denom;
996 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
998 /* potential energy */
999 moldyn->v_sum+=moldyn->energy;
1000 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
1001 moldyn->v_avg=moldyn->v_sum/denom;
1002 moldyn->v2_avg=moldyn->v2_sum/denom;
1003 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1006 moldyn->t_sum+=moldyn->t;
1007 moldyn->t_avg=moldyn->t_sum/denom;
1010 moldyn->virial_sum+=moldyn->virial;
1011 moldyn->virial_avg=moldyn->virial_sum/denom;
1012 moldyn->gv_sum+=moldyn->gv;
1013 moldyn->gv_avg=moldyn->gv_sum/denom;
1016 moldyn->p_sum+=moldyn->p;
1017 moldyn->p_avg=moldyn->p_sum/denom;
1018 moldyn->gp_sum+=moldyn->gp;
1019 moldyn->gp_avg=moldyn->gp_sum/denom;
1020 moldyn->tp_sum+=moldyn->tp;
1021 moldyn->tp_avg=moldyn->tp_sum/denom;
1026 int get_heat_capacity(t_moldyn *moldyn) {
1030 /* averages needed for heat capacity calc */
1031 if(moldyn->total_steps<moldyn->avg_skip)
1034 /* (temperature average)^2 */
1035 temp2=moldyn->t_avg*moldyn->t_avg;
1036 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1039 /* ideal gas contribution */
1040 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1041 printf(" ideal gas contribution: %f\n",
1042 ighc/moldyn->mass*KILOGRAM/JOULE);
1044 /* specific heat for nvt ensemble */
1045 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1046 moldyn->c_v_nvt/=moldyn->mass;
1048 /* specific heat for nve ensemble */
1049 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1050 moldyn->c_v_nve/=moldyn->mass;
1052 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1053 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1054 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)));
1059 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1075 /* store atomic configuration + dimension */
1076 store=malloc(moldyn->count*sizeof(t_atom));
1078 printf("[moldyn] allocating store mem failed\n");
1081 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1086 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1087 su=pow(2.0-h,ONE_THIRD)-1.0;
1088 dv=(1.0-h)*moldyn->volume;
1090 /* scale up dimension and atom positions */
1091 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1092 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1093 link_cell_shutdown(moldyn);
1094 link_cell_init(moldyn,QUIET);
1095 potential_force_calc(moldyn);
1098 /* restore atomic configuration + dim */
1099 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1102 /* scale down dimension and atom positions */
1103 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1104 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1105 link_cell_shutdown(moldyn);
1106 link_cell_init(moldyn,QUIET);
1107 potential_force_calc(moldyn);
1110 /* calculate pressure */
1111 moldyn->tp=-(y1-y0)/(2.0*dv);
1113 /* restore atomic configuration */
1114 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1116 link_cell_shutdown(moldyn);
1117 link_cell_init(moldyn,QUIET);
1118 //potential_force_calc(moldyn);
1120 /* free store buffer */
1127 double get_pressure(t_moldyn *moldyn) {
1133 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1145 if(x) dim->x*=scale;
1146 if(y) dim->y*=scale;
1147 if(z) dim->z*=scale;
1152 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1163 for(i=0;i<moldyn->count;i++) {
1164 r=&(moldyn->atom[i].r);
1173 int scale_volume(t_moldyn *moldyn) {
1179 vdim=&(moldyn->vis.dim);
1183 /* scaling factor */
1184 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1185 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc;
1186 scale=pow(scale,ONE_THIRD);
1189 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1192 /* scale the atoms and dimensions */
1193 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1194 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1196 /* visualize dimensions */
1203 /* recalculate scaled volume */
1204 moldyn->volume=dim->x*dim->y*dim->z;
1206 /* adjust/reinit linkcell */
1207 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1208 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1209 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1210 link_cell_shutdown(moldyn);
1211 link_cell_init(moldyn,QUIET);
1222 double e_kin_calc(t_moldyn *moldyn) {
1230 for(i=0;i<moldyn->count;i++) {
1231 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1232 moldyn->ekin+=atom[i].ekin;
1235 return moldyn->ekin;
1238 double get_total_energy(t_moldyn *moldyn) {
1240 return(moldyn->ekin+moldyn->energy);
1243 t_3dvec get_total_p(t_moldyn *moldyn) {
1252 for(i=0;i<moldyn->count;i++) {
1253 v3_scale(&p,&(atom[i].v),atom[i].mass);
1254 v3_add(&p_total,&p_total,&p);
1260 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1264 /* nn_dist is the nearest neighbour distance */
1266 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1275 /* linked list / cell method */
1277 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1284 /* partitioning the md cell */
1285 lc->nx=moldyn->dim.x/moldyn->cutoff;
1286 lc->x=moldyn->dim.x/lc->nx;
1287 lc->ny=moldyn->dim.y/moldyn->cutoff;
1288 lc->y=moldyn->dim.y/lc->ny;
1289 lc->nz=moldyn->dim.z/moldyn->cutoff;
1290 lc->z=moldyn->dim.z/lc->nz;
1291 lc->cells=lc->nx*lc->ny*lc->nz;
1294 lc->subcell=malloc(lc->cells*sizeof(int*));
1296 lc->subcell=malloc(lc->cells*sizeof(t_list));
1299 if(lc->subcell==NULL) {
1300 perror("[moldyn] cell init (malloc)");
1305 printf("[moldyn] FATAL: less then 27 subcells!\n");
1309 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1312 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1315 printf(" x: %d x %f A\n",lc->nx,lc->x);
1316 printf(" y: %d x %f A\n",lc->ny,lc->y);
1317 printf(" z: %d x %f A\n",lc->nz,lc->z);
1322 for(i=0;i<lc->cells;i++) {
1323 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1324 if(lc->subcell[i]==NULL) {
1325 perror("[moldyn] list init (malloc)");
1330 printf(" ---> %d malloc %p (%p)\n",
1331 i,lc->subcell[0],lc->subcell);
1335 for(i=0;i<lc->cells;i++)
1336 list_init_f(&(lc->subcell[i]));
1339 /* update the list */
1340 link_cell_update(moldyn);
1345 int link_cell_update(t_moldyn *moldyn) {
1361 for(i=0;i<lc->cells;i++)
1363 memset(lc->subcell[i],0,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1365 list_destroy_f(&(lc->subcell[i]));
1368 for(count=0;count<moldyn->count;count++) {
1369 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1370 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1371 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1375 while(lc->subcell[i+j*nx+k*nx*ny][p]!=0)
1378 if(p>=MAX_ATOMS_PER_LIST) {
1379 printf("[moldyn] FATAL: amount of atoms too high!\n");
1383 lc->subcell[i+j*nx+k*nx*ny][p]=count;
1385 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1389 printf(" ---> %d %d malloc %p (%p)\n",
1390 i,count,lc->subcell[i].current,lc->subcell);
1398 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1422 if(i>=nx||j>=ny||k>=nz)
1423 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1426 cell[0]=lc->subcell[i+j*nx+k*a];
1427 for(ci=-1;ci<=1;ci++) {
1430 if((x<0)||(x>=nx)) {
1434 for(cj=-1;cj<=1;cj++) {
1437 if((y<0)||(y>=ny)) {
1441 for(ck=-1;ck<=1;ck++) {
1444 if((z<0)||(z>=nz)) {
1448 if(!(ci|cj|ck)) continue;
1450 cell[--count2]=lc->subcell[x+y*nx+z*a];
1453 cell[count1++]=lc->subcell[x+y*nx+z*a];
1464 int link_cell_shutdown(t_moldyn *moldyn) {
1471 for(i=0;i<lc->cells;i++) {
1473 free(lc->subcell[i]);
1475 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1476 list_destroy_f(&(lc->subcell[i]));
1485 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1489 t_moldyn_schedule *schedule;
1491 schedule=&(moldyn->schedule);
1492 count=++(schedule->total_sched);
1494 ptr=realloc(schedule->runs,count*sizeof(int));
1496 perror("[moldyn] realloc (runs)");
1500 schedule->runs[count-1]=runs;
1502 ptr=realloc(schedule->tau,count*sizeof(double));
1504 perror("[moldyn] realloc (tau)");
1508 schedule->tau[count-1]=tau;
1510 printf("[moldyn] schedule added:\n");
1511 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1517 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1519 moldyn->schedule.hook=hook;
1520 moldyn->schedule.hook_params=hook_params;
1527 * 'integration of newtons equation' - algorithms
1531 /* start the integration */
1533 int moldyn_integrate(t_moldyn *moldyn) {
1536 unsigned int e,m,s,v,p,t,a;
1538 t_moldyn_schedule *sched;
1543 double energy_scale;
1544 struct timeval t1,t2;
1547 sched=&(moldyn->schedule);
1550 /* initialize linked cell method */
1551 link_cell_init(moldyn,VERBOSE);
1553 /* logging & visualization */
1562 /* sqaure of some variables */
1563 moldyn->tau_square=moldyn->tau*moldyn->tau;
1565 /* get current time */
1566 gettimeofday(&t1,NULL);
1568 /* calculate initial forces */
1569 potential_force_calc(moldyn);
1574 /* some stupid checks before we actually start calculating bullshit */
1575 if(moldyn->cutoff>0.5*moldyn->dim.x)
1576 printf("[moldyn] WARNING: cutoff > 0.5 x dim.x\n");
1577 if(moldyn->cutoff>0.5*moldyn->dim.y)
1578 printf("[moldyn] WARNING: cutoff > 0.5 x dim.y\n");
1579 if(moldyn->cutoff>0.5*moldyn->dim.z)
1580 printf("[moldyn] WARNING: cutoff > 0.5 x dim.z\n");
1582 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1583 if(ds>0.05*moldyn->nnd)
1584 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1587 /* zero absolute time */
1588 // should have right values!
1590 //moldyn->total_steps=0;
1592 /* debugging, ignore */
1595 /* tell the world */
1596 printf("[moldyn] integration start, go get a coffee ...\n");
1598 /* executing the schedule */
1600 while(sched->count<sched->total_sched) {
1602 /* setting amount of runs and finite time step size */
1603 moldyn->tau=sched->tau[sched->count];
1604 moldyn->tau_square=moldyn->tau*moldyn->tau;
1605 moldyn->time_steps=sched->runs[sched->count];
1607 /* energy scaling factor (might change!) */
1608 energy_scale=moldyn->count*EV;
1610 /* integration according to schedule */
1612 for(i=0;i<moldyn->time_steps;i++) {
1614 /* integration step */
1615 moldyn->integrate(moldyn);
1617 /* calculate kinetic energy, temperature and pressure */
1619 temperature_calc(moldyn);
1621 pressure_calc(moldyn);
1623 thermodynamic_pressure_calc(moldyn);
1624 printf("\n\nDEBUG: numeric pressure calc: %f\n\n",
1628 /* calculate fluctuations + averages */
1629 average_and_fluctuation_calc(moldyn);
1632 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1633 scale_velocity(moldyn,FALSE);
1634 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1635 scale_volume(moldyn);
1637 /* check for log & visualization */
1639 if(!(moldyn->total_steps%e))
1640 dprintf(moldyn->efd,
1642 moldyn->time,moldyn->ekin/energy_scale,
1643 moldyn->energy/energy_scale,
1644 get_total_energy(moldyn)/energy_scale);
1647 if(!(moldyn->total_steps%m)) {
1648 momentum=get_total_p(moldyn);
1649 dprintf(moldyn->mfd,
1650 "%f %f %f %f %f\n",moldyn->time,
1651 momentum.x,momentum.y,momentum.z,
1652 v3_norm(&momentum));
1656 if(!(moldyn->total_steps%p)) {
1657 dprintf(moldyn->pfd,
1658 "%f %f %f %f %f %f %f\n",moldyn->time,
1659 moldyn->p/BAR,moldyn->p_avg/BAR,
1660 moldyn->gp/BAR,moldyn->gp_avg/BAR,
1661 moldyn->tp/BAR,moldyn->tp_avg/BAR);
1665 if(!(moldyn->total_steps%t)) {
1666 dprintf(moldyn->tfd,
1668 moldyn->time,moldyn->t,moldyn->t_avg);
1672 if(!(moldyn->total_steps%v)) {
1673 dprintf(moldyn->vfd,
1674 "%f %f\n",moldyn->time,moldyn->volume);
1678 if(!(moldyn->total_steps%s)) {
1679 snprintf(dir,128,"%s/s-%07.f.save",
1680 moldyn->vlsdir,moldyn->time);
1681 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
1683 if(fd<0) perror("[moldyn] save fd open");
1685 write(fd,moldyn,sizeof(t_moldyn));
1686 write(fd,moldyn->atom,
1687 moldyn->count*sizeof(t_atom));
1693 if(!(moldyn->total_steps%a)) {
1694 visual_atoms(moldyn);
1698 /* display progress */
1699 //if(!(moldyn->total_steps%10)) {
1700 /* get current time */
1701 gettimeofday(&t2,NULL);
1703 printf("\rsched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)",
1704 sched->count,i,moldyn->total_steps,
1705 moldyn->t,moldyn->t_avg,
1706 moldyn->p/BAR,moldyn->p_avg/BAR,
1707 //moldyn->p/BAR,(moldyn->p-2.0*moldyn->ekin/(3.0*moldyn->volume))/BAR,
1709 (int)(t2.tv_sec-t1.tv_sec));
1713 /* copy over time */
1717 /* increase absolute time */
1718 moldyn->time+=moldyn->tau;
1719 moldyn->total_steps+=1;
1723 /* check for hooks */
1725 printf("\n ## schedule hook %d start ##\n",
1727 sched->hook(moldyn,sched->hook_params);
1728 printf(" ## schedule hook end ##\n");
1731 /* increase the schedule counter */
1739 /* velocity verlet */
1741 int velocity_verlet(t_moldyn *moldyn) {
1744 double tau,tau_square,h;
1749 count=moldyn->count;
1751 tau_square=moldyn->tau_square;
1753 for(i=0;i<count;i++) {
1754 /* check whether fixed atom */
1755 if(atom[i].attr&ATOM_ATTR_FP)
1759 v3_scale(&delta,&(atom[i].v),tau);
1760 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1761 v3_scale(&delta,&(atom[i].f),h*tau_square);
1762 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1763 check_per_bound(moldyn,&(atom[i].r));
1765 /* velocities [actually v(t+tau/2)] */
1766 v3_scale(&delta,&(atom[i].f),h*tau);
1767 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1770 /* criticial check */
1771 moldyn_bc_check(moldyn);
1773 /* neighbour list update */
1774 link_cell_update(moldyn);
1776 /* forces depending on chosen potential */
1777 potential_force_calc(moldyn);
1779 for(i=0;i<count;i++) {
1780 /* check whether fixed atom */
1781 if(atom[i].attr&ATOM_ATTR_FP)
1783 /* again velocities [actually v(t+tau)] */
1784 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1785 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1794 * potentials & corresponding forces & virial routine
1798 /* generic potential and force calculation */
1800 int potential_force_calc(t_moldyn *moldyn) {
1803 t_atom *itom,*jtom,*ktom;
1807 int *neighbour_i[27];
1811 t_list neighbour_i[27];
1812 t_list neighbour_i2[27];
1818 count=moldyn->count;
1828 /* reset global virial */
1829 memset(&(moldyn->gvir),0,sizeof(t_virial));
1831 /* reset force, site energy and virial of every atom */
1832 for(i=0;i<count;i++) {
1835 v3_zero(&(itom[i].f));
1838 virial=(&(itom[i].virial));
1846 /* reset site energy */
1851 /* get energy, force and virial of every atom */
1853 /* first (and only) loop over atoms i */
1854 for(i=0;i<count;i++) {
1856 /* single particle potential/force */
1857 if(itom[i].attr&ATOM_ATTR_1BP)
1859 moldyn->func1b(moldyn,&(itom[i]));
1861 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1864 /* 2 body pair potential/force */
1866 link_cell_neighbour_index(moldyn,
1867 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1868 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1869 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1874 /* first loop over atoms j */
1875 if(moldyn->func2b) {
1882 while(neighbour_i[j][p]!=0) {
1884 jtom=&(atom[neighbour_i[j][p]]);
1887 if(jtom==&(itom[i]))
1890 if((jtom->attr&ATOM_ATTR_2BP)&
1891 (itom[i].attr&ATOM_ATTR_2BP)) {
1892 moldyn->func2b(moldyn,
1899 this=&(neighbour_i[j]);
1902 if(this->start==NULL)
1906 jtom=this->current->data;
1908 if(jtom==&(itom[i]))
1911 if((jtom->attr&ATOM_ATTR_2BP)&
1912 (itom[i].attr&ATOM_ATTR_2BP)) {
1913 moldyn->func2b(moldyn,
1918 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1924 /* 3 body potential/force */
1926 if(!(itom[i].attr&ATOM_ATTR_3BP))
1929 /* copy the neighbour lists */
1931 /* no copy needed for static lists */
1933 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
1936 /* second loop over atoms j */
1943 while(neighbour_i[j][p]!=0) {
1945 jtom=&(atom[neighbour_i[j][p]]);
1948 this=&(neighbour_i[j]);
1951 if(this->start==NULL)
1956 jtom=this->current->data;
1959 if(jtom==&(itom[i]))
1962 if(!(jtom->attr&ATOM_ATTR_3BP))
1968 if(moldyn->func3b_j1)
1969 moldyn->func3b_j1(moldyn,
1974 /* in first j loop, 3bp run can be skipped */
1975 if(!(moldyn->run3bp))
1978 /* first loop over atoms k */
1979 if(moldyn->func3b_k1) {
1987 while(neighbour_i[j][q]!=0) {
1989 ktom=&(atom[neighbour_i[k][q]]);
1992 that=&(neighbour_i2[k]);
1995 if(that->start==NULL)
1999 ktom=that->current->data;
2002 if(!(ktom->attr&ATOM_ATTR_3BP))
2008 if(ktom==&(itom[i]))
2011 moldyn->func3b_k1(moldyn,
2019 } while(list_next_f(that)!=\
2027 if(moldyn->func3b_j2)
2028 moldyn->func3b_j2(moldyn,
2033 /* second loop over atoms k */
2034 if(moldyn->func3b_k2) {
2042 while(neighbour_i[j][q]!=0) {
2044 ktom=&(atom[neighbour_i[k][q]]);
2047 that=&(neighbour_i2[k]);
2050 if(that->start==NULL)
2054 ktom=that->current->data;
2057 if(!(ktom->attr&ATOM_ATTR_3BP))
2063 if(ktom==&(itom[i]))
2066 moldyn->func3b_k2(moldyn,
2075 } while(list_next_f(that)!=\
2083 /* 2bp post function */
2084 if(moldyn->func3b_j3) {
2085 moldyn->func3b_j3(moldyn,
2092 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2107 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2108 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2110 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
2111 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
2112 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
2116 /* some postprocessing */
2117 for(i=0;i<count;i++) {
2118 /* calculate global virial */
2119 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
2120 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
2121 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
2122 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
2123 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
2124 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
2126 /* check forces regarding the given timestep */
2127 if(v3_norm(&(itom[i].f))>\
2128 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
2129 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",
2137 * virial calculation
2140 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2141 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2143 a->virial.xx+=f->x*d->x;
2144 a->virial.yy+=f->y*d->y;
2145 a->virial.zz+=f->z*d->z;
2146 a->virial.xy+=f->x*d->y;
2147 a->virial.xz+=f->x*d->z;
2148 a->virial.yz+=f->y*d->z;
2154 * periodic boundary checking
2157 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2158 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2169 if(moldyn->status&MOLDYN_STAT_PBX) {
2170 if(a->x>=x) a->x-=dim->x;
2171 else if(-a->x>x) a->x+=dim->x;
2173 if(moldyn->status&MOLDYN_STAT_PBY) {
2174 if(a->y>=y) a->y-=dim->y;
2175 else if(-a->y>y) a->y+=dim->y;
2177 if(moldyn->status&MOLDYN_STAT_PBZ) {
2178 if(a->z>=z) a->z-=dim->z;
2179 else if(-a->z>z) a->z+=dim->z;
2186 * debugging / critical check functions
2189 int moldyn_bc_check(t_moldyn *moldyn) {
2202 for(i=0;i<moldyn->count;i++) {
2203 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2204 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2205 i,atom[i].r.x,dim->x/2);
2206 printf("diagnostic:\n");
2207 printf("-----------\natom.r.x:\n");
2209 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2212 ((byte)&(1<<k))?1:0,
2215 printf("---------------\nx=dim.x/2:\n");
2217 memcpy(&byte,(u8 *)(&x)+j,1);
2220 ((byte)&(1<<k))?1:0,
2223 if(atom[i].r.x==x) printf("the same!\n");
2224 else printf("different!\n");
2226 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2227 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2228 i,atom[i].r.y,dim->y/2);
2229 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2230 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2231 i,atom[i].r.z,dim->z/2);
2241 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2248 fd=open(file,O_RDONLY);
2250 perror("[moldyn] load save file open");
2254 fsize=lseek(fd,0,SEEK_END);
2255 lseek(fd,0,SEEK_SET);
2257 size=sizeof(t_moldyn);
2260 cnt=read(fd,moldyn,size);
2262 perror("[moldyn] load save file read (moldyn)");
2268 size=moldyn->count*sizeof(t_atom);
2270 /* correcting possible atom data offset */
2272 if(fsize!=sizeof(t_moldyn)+size) {
2273 corr=fsize-sizeof(t_moldyn)-size;
2274 printf("[moldyn] WARNING: lsf (illegal file size)\n");
2275 printf(" moifying offset:\n");
2276 printf(" - current pos: %d\n",sizeof(t_moldyn));
2277 printf(" - atom size: %d\n",size);
2278 printf(" - file size: %d\n",fsize);
2279 printf(" => correction: %d\n",corr);
2280 lseek(fd,corr,SEEK_CUR);
2283 moldyn->atom=(t_atom *)malloc(size);
2284 if(moldyn->atom==NULL) {
2285 perror("[moldyn] load save file malloc (atoms)");
2290 cnt=read(fd,moldyn->atom,size);
2292 perror("[moldyn] load save file read (atoms)");
2303 int moldyn_free_save_file(t_moldyn *moldyn) {
2310 int moldyn_load(t_moldyn *moldyn) {
2318 * function to find/callback all combinations of 2 body bonds
2321 int process_2b_bonds(t_moldyn *moldyn,void *data,
2322 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2323 void *data,u8 bc)) {
2330 t_list neighbour[27];
2340 for(i=0;i<moldyn->count;i++) {
2341 /* neighbour indexing */
2342 link_cell_neighbour_index(moldyn,
2343 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2344 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2345 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2350 bc=(j<lc->dnlc)?0:1;
2355 while(neighbour[j][p]!=0) {
2357 jtom=&(moldyn->atom[neighbour[j][p]]);
2360 this=&(neighbour[j]);
2363 if(this->start==NULL)
2368 jtom=this->current->data;
2372 process(moldyn,&(itom[i]),jtom,data,bc);
2377 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2387 * post processing functions
2390 int get_line(int fd,char *line,int max) {
2397 if(count==max) return count;
2398 ret=read(fd,line+count,1);
2399 if(ret<=0) return ret;
2400 if(line[count]=='\n') {
2401 memset(line+count,0,max-count-1);
2409 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2415 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
2431 for(i=0;i<moldyn->count;i++) {
2433 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
2434 check_per_bound(moldyn,&dist);
2435 d2=v3_absolute_square(&dist);
2449 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
2450 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
2451 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
2456 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
2461 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
2462 t_atom *jtom,void *data,u8 bc) {
2469 /* only count pairs once,
2470 * skip same atoms */
2471 if(itom->tag>=jtom->tag)
2475 * pair correlation calc
2482 v3_sub(&dist,&(jtom->r),&(itom->r));
2483 if(bc) check_per_bound(moldyn,&dist);
2484 d=v3_absolute_square(&dist);
2486 /* ignore if greater cutoff */
2487 if(d>moldyn->cutoff_square)
2490 /* fill the slots */
2494 /* should never happen but it does 8) -
2495 * related to -ffloat-store problem! */
2497 printf("[moldyn] WARNING: pcc (%d/%d)",
2503 if(itom->brand!=jtom->brand) {
2508 /* type a - type a bonds */
2510 pcc->stat[s+pcc->o1]+=1;
2512 /* type b - type b bonds */
2513 pcc->stat[s+pcc->o2]+=1;
2519 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2526 pcc.o1=moldyn->cutoff/dr;
2529 if(pcc.o1*dr<=moldyn->cutoff)
2530 printf("[moldyn] WARNING: pcc (low #slots)\n");
2532 printf("[moldyn] pair correlation calc info:\n");
2533 printf(" time: %f\n",moldyn->time);
2534 printf(" count: %d\n",moldyn->count);
2535 printf(" cutoff: %f\n",moldyn->cutoff);
2536 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2539 pcc.stat=(double *)ptr;
2542 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
2543 if(pcc.stat==NULL) {
2544 perror("[moldyn] pair correlation malloc");
2549 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
2552 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
2555 for(i=1;i<pcc.o1;i++) {
2556 // normalization: 4 pi r^2 dr
2557 // here: not double counting pairs -> 2 pi r r dr
2558 // ... and actually it's a constant times r^2
2561 pcc.stat[pcc.o1+i]/=norm;
2562 pcc.stat[pcc.o2+i]/=norm;
2567 /* todo: store/print pair correlation function */
2574 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2581 if(itom->tag>=jtom->tag)
2585 v3_sub(&dist,&(jtom->r),&(itom->r));
2586 if(bc) check_per_bound(moldyn,&dist);
2587 d=v3_absolute_square(&dist);
2589 /* ignore if greater or equal cutoff */
2590 if(d>moldyn->cutoff_square)
2593 /* check for potential bond */
2594 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2597 /* now count this bonding ... */
2600 /* increase total bond counter
2601 * ... double counting!
2606 ba->acnt[jtom->tag]+=1;
2608 ba->bcnt[jtom->tag]+=1;
2611 ba->acnt[itom->tag]+=1;
2613 ba->bcnt[itom->tag]+=1;
2618 int bond_analyze(t_moldyn *moldyn,double *quality) {
2620 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
2628 ba.acnt=malloc(moldyn->count*sizeof(int));
2630 perror("[moldyn] bond analyze malloc (a)");
2633 memset(ba.acnt,0,moldyn->count*sizeof(int));
2635 ba.bcnt=malloc(moldyn->count*sizeof(int));
2637 perror("[moldyn] bond analyze malloc (b)");
2640 memset(ba.bcnt,0,moldyn->count*sizeof(int));
2649 process_2b_bonds(moldyn,&ba,bond_analyze_process);
2651 for(i=0;i<moldyn->count;i++) {
2652 if(atom[i].brand==0) {
2653 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
2657 if((ba.acnt[i]==4)&(ba.bcnt[i]==0)) {
2665 printf("[moldyn] bond analyze: c_cnt=%d | set=%d\n",ccnt,cset);
2666 printf("[moldyn] bond analyze: q_cnt=%d | tot=%d\n",qcnt,ba.tcnt);
2669 quality[0]=1.0*ccnt/cset;
2670 quality[1]=1.0*qcnt/ba.tcnt;
2673 printf("[moldyn] bond analyze: c_bnd_q=%f\n",1.0*qcnt/ba.tcnt);
2674 printf("[moldyn] bond analyze: tot_q=%f\n",1.0*qcnt/ba.tcnt);
2681 * visualization code
2684 int visual_init(t_moldyn *moldyn,char *filebase) {
2686 strncpy(moldyn->vis.fb,filebase,128);
2691 int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2698 if(itom->tag>=jtom->tag)
2701 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2704 if((itom->attr&ATOM_ATTR_VB)|(jtom->attr&ATOM_ATTR_VB))
2705 dprintf(vb->fd,"# [B] %f %f %f %f %f %f\n",
2706 itom->r.x,itom->r.y,itom->r.z,
2707 jtom->r.x,jtom->r.y,jtom->r.z);
2712 int visual_atoms(t_moldyn *moldyn) {
2730 sprintf(file,"%s/atomic_conf_%07.f.xyz",v->fb,moldyn->time);
2731 vb.fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
2733 perror("open visual save file fd");
2737 /* write the actual data file */
2740 dprintf(vb.fd,"# [P] %d %07.f <%f,%f,%f>\n",
2741 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
2743 // atomic configuration
2744 for(i=0;i<moldyn->count;i++)
2745 // atom type, positions, color and kinetic energy
2746 dprintf(vb.fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
2750 pse_col[atom[i].element],
2753 // bonds between atoms
2754 process_2b_bonds(moldyn,&vb,visual_bonds_process);
2758 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2759 -dim.x/2,-dim.y/2,-dim.z/2,
2760 dim.x/2,-dim.y/2,-dim.z/2);
2761 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2762 -dim.x/2,-dim.y/2,-dim.z/2,
2763 -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);
2771 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2772 -dim.x/2,-dim.y/2,dim.z/2,
2773 dim.x/2,-dim.y/2,dim.z/2);
2774 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2775 -dim.x/2,-dim.y/2,dim.z/2,
2776 -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);
2784 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2785 -dim.x/2,-dim.y/2,dim.z/2,
2786 -dim.x/2,-dim.y/2,-dim.z/2);
2787 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2788 -dim.x/2,dim.y/2,dim.z/2,
2789 -dim.x/2,dim.y/2,-dim.z/2);
2790 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2791 dim.x/2,-dim.y/2,dim.z/2,
2792 dim.x/2,-dim.y/2,-dim.z/2);
2793 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2794 dim.x/2,dim.y/2,dim.z/2,
2795 dim.x/2,dim.y/2,-dim.z/2);