2 * moldyn.c - molecular dynamics library main file
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
12 #include <sys/types.h>
20 #include <fpu_control.h>
27 #include "report/report.h"
29 /* potential includes */
30 #include "potentials/harmonic_oscillator.h"
31 #include "potentials/lennard_jones.h"
32 #include "potentials/albe.h"
34 #include "potentials/tersoff_orig.h"
36 #include "potentials/tersoff.h"
47 * the moldyn functions
50 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
52 printf("[moldyn] init\n");
54 /* only needed if compiled without -msse2 (float-store prob!) */
57 memset(moldyn,0,sizeof(t_moldyn));
62 rand_init(&(moldyn->random),NULL,1);
63 moldyn->random.status|=RAND_STAT_VERBOSE;
68 int moldyn_shutdown(t_moldyn *moldyn) {
70 printf("[moldyn] shutdown\n");
72 moldyn_log_shutdown(moldyn);
73 link_cell_shutdown(moldyn);
74 rand_close(&(moldyn->random));
80 int set_int_alg(t_moldyn *moldyn,u8 algo) {
82 printf("[moldyn] integration algorithm: ");
85 case MOLDYN_INTEGRATE_VERLET:
86 moldyn->integrate=velocity_verlet;
87 printf("velocity verlet\n");
90 printf("unknown integration algorithm: %02x\n",algo);
98 int set_cutoff(t_moldyn *moldyn,double cutoff) {
100 moldyn->cutoff=cutoff;
101 moldyn->cutoff_square=cutoff*cutoff;
103 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
108 int set_temperature(t_moldyn *moldyn,double t_ref) {
112 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
117 int set_pressure(t_moldyn *moldyn,double p_ref) {
121 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
126 int set_p_scale(t_moldyn *moldyn,u8 ptype,double ptc) {
128 moldyn->pt_scale&=(~(P_SCALE_MASK));
129 moldyn->pt_scale|=ptype;
132 printf("[moldyn] p scaling:\n");
134 printf(" p: %s",ptype?"yes":"no ");
136 printf(" | type: %02x | factor: %f",ptype,ptc);
142 int set_t_scale(t_moldyn *moldyn,u8 ttype,double ttc) {
144 moldyn->pt_scale&=(~(T_SCALE_MASK));
145 moldyn->pt_scale|=ttype;
148 printf("[moldyn] t scaling:\n");
150 printf(" t: %s",ttype?"yes":"no ");
152 printf(" | type: %02x | factor: %f",ttype,ttc);
158 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
160 moldyn->pt_scale=(ptype|ttype);
164 printf("[moldyn] p/t scaling:\n");
166 printf(" p: %s",ptype?"yes":"no ");
168 printf(" | type: %02x | factor: %f",ptype,ptc);
171 printf(" t: %s",ttype?"yes":"no ");
173 printf(" | type: %02x | factor: %f",ttype,ttc);
179 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
185 moldyn->volume=x*y*z;
193 printf("[moldyn] dimensions in A and A^3 respectively:\n");
194 printf(" x: %f\n",moldyn->dim.x);
195 printf(" y: %f\n",moldyn->dim.y);
196 printf(" z: %f\n",moldyn->dim.z);
197 printf(" volume: %f\n",moldyn->volume);
198 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
203 int set_nn_dist(t_moldyn *moldyn,double dist) {
210 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
212 printf("[moldyn] periodic boundary conditions:\n");
215 moldyn->status|=MOLDYN_STAT_PBX;
218 moldyn->status|=MOLDYN_STAT_PBY;
221 moldyn->status|=MOLDYN_STAT_PBZ;
223 printf(" x: %s\n",x?"yes":"no");
224 printf(" y: %s\n",y?"yes":"no");
225 printf(" z: %s\n",z?"yes":"no");
230 int set_potential(t_moldyn *moldyn,u8 type) {
233 case MOLDYN_POTENTIAL_TM:
234 moldyn->func1b=tersoff_mult_1bp;
235 moldyn->func3b_j1=tersoff_mult_3bp_j1;
236 moldyn->func3b_k1=tersoff_mult_3bp_k1;
237 moldyn->func3b_j2=tersoff_mult_3bp_j2;
238 moldyn->func3b_k2=tersoff_mult_3bp_k2;
239 moldyn->check_2b_bond=tersoff_mult_check_2b_bond;
241 case MOLDYN_POTENTIAL_AM:
242 moldyn->func3b_j1=albe_mult_3bp_j1;
243 moldyn->func3b_k1=albe_mult_3bp_k1;
244 moldyn->func3b_j2=albe_mult_3bp_j2;
245 moldyn->func3b_k2=albe_mult_3bp_k2;
246 moldyn->check_2b_bond=albe_mult_check_2b_bond;
248 case MOLDYN_POTENTIAL_HO:
249 moldyn->func2b=harmonic_oscillator;
250 moldyn->check_2b_bond=harmonic_oscillator_check_2b_bond;
252 case MOLDYN_POTENTIAL_LJ:
253 moldyn->func2b=lennard_jones;
254 moldyn->check_2b_bond=lennard_jones_check_2b_bond;
257 printf("[moldyn] set potential: unknown type %02x\n",
265 int set_avg_skip(t_moldyn *moldyn,int skip) {
267 printf("[moldyn] skip %d steps before starting average calc\n",skip);
268 moldyn->avg_skip=skip;
273 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
275 strncpy(moldyn->vlsdir,dir,127);
280 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
282 strncpy(moldyn->rauthor,author,63);
283 strncpy(moldyn->rtitle,title,63);
288 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
293 printf("[moldyn] set log: ");
296 case LOG_TOTAL_ENERGY:
297 moldyn->ewrite=timer;
298 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
299 moldyn->efd=open(filename,
300 O_WRONLY|O_CREAT|O_EXCL,
303 perror("[moldyn] energy log fd open");
306 dprintf(moldyn->efd,"# total energy log file\n");
307 printf("total energy (%d)\n",timer);
309 case LOG_TOTAL_MOMENTUM:
310 moldyn->mwrite=timer;
311 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
312 moldyn->mfd=open(filename,
313 O_WRONLY|O_CREAT|O_EXCL,
316 perror("[moldyn] momentum log fd open");
319 dprintf(moldyn->efd,"# total momentum log file\n");
320 printf("total momentum (%d)\n",timer);
323 moldyn->pwrite=timer;
324 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
325 moldyn->pfd=open(filename,
326 O_WRONLY|O_CREAT|O_EXCL,
329 perror("[moldyn] pressure log file\n");
332 dprintf(moldyn->pfd,"# pressure log file\n");
333 printf("pressure (%d)\n",timer);
335 case LOG_TEMPERATURE:
336 moldyn->twrite=timer;
337 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
338 moldyn->tfd=open(filename,
339 O_WRONLY|O_CREAT|O_EXCL,
342 perror("[moldyn] temperature log file\n");
345 dprintf(moldyn->tfd,"# temperature log file\n");
346 printf("temperature (%d)\n",timer);
349 moldyn->vwrite=timer;
350 snprintf(filename,127,"%s/volume",moldyn->vlsdir);
351 moldyn->vfd=open(filename,
352 O_WRONLY|O_CREAT|O_EXCL,
355 perror("[moldyn] volume log file\n");
358 dprintf(moldyn->vfd,"# volume log file\n");
359 printf("volume (%d)\n",timer);
362 moldyn->swrite=timer;
363 printf("save file (%d)\n",timer);
366 moldyn->awrite=timer;
367 ret=visual_init(moldyn,moldyn->vlsdir);
369 printf("[moldyn] visual init failure\n");
372 printf("visual file (%d)\n",timer);
375 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
376 moldyn->rfd=open(filename,
377 O_WRONLY|O_CREAT|O_EXCL,
380 perror("[moldyn] report fd open");
383 printf("report -> ");
385 snprintf(filename,127,"%s/e_plot.scr",
387 moldyn->epfd=open(filename,
388 O_WRONLY|O_CREAT|O_EXCL,
391 perror("[moldyn] energy plot fd open");
394 dprintf(moldyn->epfd,e_plot_script);
399 snprintf(filename,127,"%s/pressure_plot.scr",
401 moldyn->ppfd=open(filename,
402 O_WRONLY|O_CREAT|O_EXCL,
405 perror("[moldyn] p plot fd open");
408 dprintf(moldyn->ppfd,pressure_plot_script);
413 snprintf(filename,127,"%s/temperature_plot.scr",
415 moldyn->tpfd=open(filename,
416 O_WRONLY|O_CREAT|O_EXCL,
419 perror("[moldyn] t plot fd open");
422 dprintf(moldyn->tpfd,temperature_plot_script);
424 printf("temperature ");
426 dprintf(moldyn->rfd,report_start,
427 moldyn->rauthor,moldyn->rtitle);
431 printf("unknown log type: %02x\n",type);
438 int moldyn_log_shutdown(t_moldyn *moldyn) {
442 printf("[moldyn] log shutdown\n");
446 dprintf(moldyn->rfd,report_energy);
447 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
452 if(moldyn->mfd) close(moldyn->mfd);
456 dprintf(moldyn->rfd,report_pressure);
457 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
464 dprintf(moldyn->rfd,report_temperature);
465 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
470 dprintf(moldyn->rfd,report_end);
472 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
475 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
478 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
487 * creating lattice functions
490 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
491 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
503 /* how many atoms do we expect */
506 printf("[moldyn] WARNING: create 'none' lattice called");
508 if(type==CUBIC) new*=1;
509 if(type==FCC) new*=4;
510 if(type==DIAMOND) new*=8;
512 /* allocate space for atoms */
513 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
515 perror("[moldyn] realloc (create lattice)");
519 atom=&(moldyn->atom[count]);
521 /* no atoms on the boundaries (only reason: it looks better!) */
535 set_nn_dist(moldyn,lc);
536 ret=cubic_init(a,b,c,lc,atom,&orig);
537 strcpy(name,"cubic");
541 v3_scale(&orig,&orig,0.5);
542 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
543 ret=fcc_init(a,b,c,lc,atom,&orig);
548 v3_scale(&orig,&orig,0.25);
549 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
550 ret=diamond_init(a,b,c,lc,atom,&orig);
551 strcpy(name,"diamond");
554 printf("unknown lattice type (%02x)\n",type);
560 printf("[moldyn] creating lattice failed\n");
561 printf(" amount of atoms\n");
562 printf(" - expected: %d\n",new);
563 printf(" - created: %d\n",ret);
568 printf("[moldyn] created %s lattice with %d atoms\n",name,new);
570 for(ret=0;ret<new;ret++) {
571 atom[ret].element=element;
574 atom[ret].brand=brand;
575 atom[ret].tag=count+ret;
576 check_per_bound(moldyn,&(atom[ret].r));
577 atom[ret].r_0=atom[ret].r;
580 /* update total system mass */
581 total_mass_calc(moldyn);
586 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
587 t_3dvec *r,t_3dvec *v) {
594 count=(moldyn->count)++; // asshole style!
596 ptr=realloc(atom,(count+1)*sizeof(t_atom));
598 perror("[moldyn] realloc (add atom)");
605 /* initialize new atom */
606 memset(&(atom[count]),0,sizeof(t_atom));
609 atom[count].element=element;
610 atom[count].mass=mass;
611 atom[count].brand=brand;
612 atom[count].tag=count;
613 atom[count].attr=attr;
614 check_per_bound(moldyn,&(atom[count].r));
615 atom[count].r_0=atom[count].r;
617 /* update total system mass */
618 total_mass_calc(moldyn);
623 int del_atom(t_moldyn *moldyn,int tag) {
630 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
632 perror("[moldyn]malloc (del atom)");
636 for(cnt=0;cnt<tag;cnt++)
639 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
641 new[cnt-1].tag=cnt-1;
653 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
672 v3_copy(&(atom[count].r),&r);
681 for(i=0;i<count;i++) {
682 atom[i].r.x-=(a*lc)/2.0;
683 atom[i].r.y-=(b*lc)/2.0;
684 atom[i].r.z-=(c*lc)/2.0;
690 /* fcc lattice init */
691 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
704 /* construct the basis */
705 memset(basis,0,3*sizeof(t_3dvec));
713 /* fill up the room */
721 v3_copy(&(atom[count].r),&r);
724 /* the three face centered atoms */
726 v3_add(&n,&r,&basis[l]);
727 v3_copy(&(atom[count].r),&n);
736 /* coordinate transformation */
737 for(i=0;i<count;i++) {
738 atom[i].r.x-=(a*lc)/2.0;
739 atom[i].r.y-=(b*lc)/2.0;
740 atom[i].r.z-=(c*lc)/2.0;
746 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
751 count=fcc_init(a,b,c,lc,atom,origin);
757 if(origin) v3_add(&o,&o,origin);
759 count+=fcc_init(a,b,c,lc,&atom[count],&o);
764 int destroy_atoms(t_moldyn *moldyn) {
766 if(moldyn->atom) free(moldyn->atom);
771 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
774 * - gaussian distribution of velocities
775 * - zero total momentum
776 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
781 t_3dvec p_total,delta;
786 random=&(moldyn->random);
788 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
790 /* gaussian distribution of velocities */
792 for(i=0;i<moldyn->count;i++) {
793 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
795 v=sigma*rand_get_gauss(random);
797 p_total.x+=atom[i].mass*v;
799 v=sigma*rand_get_gauss(random);
801 p_total.y+=atom[i].mass*v;
803 v=sigma*rand_get_gauss(random);
805 p_total.z+=atom[i].mass*v;
808 /* zero total momentum */
809 v3_scale(&p_total,&p_total,1.0/moldyn->count);
810 for(i=0;i<moldyn->count;i++) {
811 v3_scale(&delta,&p_total,1.0/atom[i].mass);
812 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
815 /* velocity scaling */
816 scale_velocity(moldyn,equi_init);
821 double total_mass_calc(t_moldyn *moldyn) {
827 for(i=0;i<moldyn->count;i++)
828 moldyn->mass+=moldyn->atom[i].mass;
833 double temperature_calc(t_moldyn *moldyn) {
835 /* assume up to date kinetic energy, which is 3/2 N k_B T */
837 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
842 double get_temperature(t_moldyn *moldyn) {
847 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
857 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
860 /* get kinetic energy / temperature & count involved atoms */
863 for(i=0;i<moldyn->count;i++) {
864 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
865 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
870 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
871 else return 0; /* no atoms involved in scaling! */
873 /* (temporary) hack for e,t = 0 */
876 if(moldyn->t_ref!=0.0) {
877 thermal_init(moldyn,equi_init);
881 return 0; /* no scaling needed */
885 /* get scaling factor */
886 scale=moldyn->t_ref/moldyn->t;
890 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
891 scale=1.0+(scale-1.0)*moldyn->tau/moldyn->t_tc;
894 /* velocity scaling */
895 for(i=0;i<moldyn->count;i++) {
896 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
897 v3_scale(&(atom[i].v),&(atom[i].v),scale);
903 double ideal_gas_law_pressure(t_moldyn *moldyn) {
907 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
912 double virial_sum(t_moldyn *moldyn) {
917 /* virial (sum over atom virials) */
925 for(i=0;i<moldyn->count;i++) {
926 virial=&(moldyn->atom[i].virial);
927 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
928 moldyn->vir.xx+=virial->xx;
929 moldyn->vir.yy+=virial->yy;
930 moldyn->vir.zz+=virial->zz;
931 moldyn->vir.xy+=virial->xy;
932 moldyn->vir.xz+=virial->xz;
933 moldyn->vir.yz+=virial->yz;
936 /* global virial (absolute coordinates) */
937 virial=&(moldyn->gvir);
938 moldyn->gv=virial->xx+virial->yy+virial->zz;
940 return moldyn->virial;
943 double pressure_calc(t_moldyn *moldyn) {
947 * with W = 1/3 sum_i f_i r_i (- skipped!)
948 * virial = sum_i f_i r_i
950 * => P = (2 Ekin + virial) / (3V)
953 /* assume up to date virial & up to date kinetic energy */
955 /* pressure (atom virials) */
956 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
957 moldyn->p/=(3.0*moldyn->volume);
959 /* pressure (absolute coordinates) */
960 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
961 moldyn->gp/=(3.0*moldyn->volume);
966 int average_reset(t_moldyn *moldyn) {
968 printf("[moldyn] average reset\n");
970 /* update skip value */
971 moldyn->avg_skip=moldyn->total_steps;
977 /* potential energy */
985 moldyn->virial_sum=0.0;
996 int average_and_fluctuation_calc(t_moldyn *moldyn) {
1000 if(moldyn->total_steps<moldyn->avg_skip)
1003 denom=moldyn->total_steps+1-moldyn->avg_skip;
1005 /* assume up to date energies, temperature, pressure etc */
1007 /* kinetic energy */
1008 moldyn->k_sum+=moldyn->ekin;
1009 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
1010 moldyn->k_avg=moldyn->k_sum/denom;
1011 moldyn->k2_avg=moldyn->k2_sum/denom;
1012 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
1014 /* potential energy */
1015 moldyn->v_sum+=moldyn->energy;
1016 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
1017 moldyn->v_avg=moldyn->v_sum/denom;
1018 moldyn->v2_avg=moldyn->v2_sum/denom;
1019 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1022 moldyn->t_sum+=moldyn->t;
1023 moldyn->t_avg=moldyn->t_sum/denom;
1026 moldyn->virial_sum+=moldyn->virial;
1027 moldyn->virial_avg=moldyn->virial_sum/denom;
1028 moldyn->gv_sum+=moldyn->gv;
1029 moldyn->gv_avg=moldyn->gv_sum/denom;
1032 moldyn->p_sum+=moldyn->p;
1033 moldyn->p_avg=moldyn->p_sum/denom;
1034 moldyn->gp_sum+=moldyn->gp;
1035 moldyn->gp_avg=moldyn->gp_sum/denom;
1036 moldyn->tp_sum+=moldyn->tp;
1037 moldyn->tp_avg=moldyn->tp_sum/denom;
1042 int get_heat_capacity(t_moldyn *moldyn) {
1046 /* averages needed for heat capacity calc */
1047 if(moldyn->total_steps<moldyn->avg_skip)
1050 /* (temperature average)^2 */
1051 temp2=moldyn->t_avg*moldyn->t_avg;
1052 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1055 /* ideal gas contribution */
1056 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1057 printf(" ideal gas contribution: %f\n",
1058 ighc/moldyn->mass*KILOGRAM/JOULE);
1060 /* specific heat for nvt ensemble */
1061 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1062 moldyn->c_v_nvt/=moldyn->mass;
1064 /* specific heat for nve ensemble */
1065 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1066 moldyn->c_v_nve/=moldyn->mass;
1068 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1069 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1070 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)));
1075 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1091 /* store atomic configuration + dimension */
1092 store=malloc(moldyn->count*sizeof(t_atom));
1094 printf("[moldyn] allocating store mem failed\n");
1097 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1102 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1103 su=pow(2.0-h,ONE_THIRD)-1.0;
1104 dv=(1.0-h)*moldyn->volume;
1106 /* scale up dimension and atom positions */
1107 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1108 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1109 link_cell_shutdown(moldyn);
1110 link_cell_init(moldyn,QUIET);
1111 potential_force_calc(moldyn);
1114 /* restore atomic configuration + dim */
1115 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1118 /* scale down dimension and atom positions */
1119 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1120 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1121 link_cell_shutdown(moldyn);
1122 link_cell_init(moldyn,QUIET);
1123 potential_force_calc(moldyn);
1126 /* calculate pressure */
1127 moldyn->tp=-(y1-y0)/(2.0*dv);
1129 /* restore atomic configuration */
1130 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1132 link_cell_shutdown(moldyn);
1133 link_cell_init(moldyn,QUIET);
1134 //potential_force_calc(moldyn);
1136 /* free store buffer */
1143 double get_pressure(t_moldyn *moldyn) {
1149 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1161 if(x) dim->x*=scale;
1162 if(y) dim->y*=scale;
1163 if(z) dim->z*=scale;
1168 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1179 for(i=0;i<moldyn->count;i++) {
1180 r=&(moldyn->atom[i].r);
1189 int scale_volume(t_moldyn *moldyn) {
1195 vdim=&(moldyn->vis.dim);
1199 /* scaling factor */
1200 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1201 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc*moldyn->tau;
1202 scale=pow(scale,ONE_THIRD);
1205 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1208 /* scale the atoms and dimensions */
1209 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1210 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1212 /* visualize dimensions */
1219 /* recalculate scaled volume */
1220 moldyn->volume=dim->x*dim->y*dim->z;
1222 /* adjust/reinit linkcell */
1223 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1224 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1225 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1226 link_cell_shutdown(moldyn);
1227 link_cell_init(moldyn,QUIET);
1238 double e_kin_calc(t_moldyn *moldyn) {
1246 for(i=0;i<moldyn->count;i++) {
1247 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1248 moldyn->ekin+=atom[i].ekin;
1251 return moldyn->ekin;
1254 double get_total_energy(t_moldyn *moldyn) {
1256 return(moldyn->ekin+moldyn->energy);
1259 t_3dvec get_total_p(t_moldyn *moldyn) {
1268 for(i=0;i<moldyn->count;i++) {
1269 v3_scale(&p,&(atom[i].v),atom[i].mass);
1270 v3_add(&p_total,&p_total,&p);
1276 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1280 /* nn_dist is the nearest neighbour distance */
1282 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1291 /* linked list / cell method */
1293 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1300 /* partitioning the md cell */
1301 lc->nx=moldyn->dim.x/moldyn->cutoff;
1302 lc->x=moldyn->dim.x/lc->nx;
1303 lc->ny=moldyn->dim.y/moldyn->cutoff;
1304 lc->y=moldyn->dim.y/lc->ny;
1305 lc->nz=moldyn->dim.z/moldyn->cutoff;
1306 lc->z=moldyn->dim.z/lc->nz;
1307 lc->cells=lc->nx*lc->ny*lc->nz;
1310 lc->subcell=malloc(lc->cells*sizeof(int*));
1312 lc->subcell=malloc(lc->cells*sizeof(t_list));
1315 if(lc->subcell==NULL) {
1316 perror("[moldyn] cell init (malloc)");
1321 printf("[moldyn] FATAL: less then 27 subcells! (%d)\n",
1326 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1329 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1332 printf(" x: %d x %f A\n",lc->nx,lc->x);
1333 printf(" y: %d x %f A\n",lc->ny,lc->y);
1334 printf(" z: %d x %f A\n",lc->nz,lc->z);
1339 for(i=0;i<lc->cells;i++) {
1340 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1341 if(lc->subcell[i]==NULL) {
1342 perror("[moldyn] list init (malloc)");
1347 printf(" ---> %d malloc %p (%p)\n",
1348 i,lc->subcell[0],lc->subcell);
1352 for(i=0;i<lc->cells;i++)
1353 list_init_f(&(lc->subcell[i]));
1356 /* update the list */
1357 link_cell_update(moldyn);
1362 int link_cell_update(t_moldyn *moldyn) {
1378 for(i=0;i<lc->cells;i++)
1380 memset(lc->subcell[i],0,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1382 list_destroy_f(&(lc->subcell[i]));
1385 for(count=0;count<moldyn->count;count++) {
1386 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1387 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1388 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1392 while(lc->subcell[i+j*nx+k*nx*ny][p]!=0)
1395 if(p>=MAX_ATOMS_PER_LIST) {
1396 printf("[moldyn] FATAL: amount of atoms too high!\n");
1400 lc->subcell[i+j*nx+k*nx*ny][p]=count;
1402 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1406 printf(" ---> %d %d malloc %p (%p)\n",
1407 i,count,lc->subcell[i].current,lc->subcell);
1415 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1439 if(i>=nx||j>=ny||k>=nz)
1440 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1443 cell[0]=lc->subcell[i+j*nx+k*a];
1444 for(ci=-1;ci<=1;ci++) {
1447 if((x<0)||(x>=nx)) {
1451 for(cj=-1;cj<=1;cj++) {
1454 if((y<0)||(y>=ny)) {
1458 for(ck=-1;ck<=1;ck++) {
1461 if((z<0)||(z>=nz)) {
1465 if(!(ci|cj|ck)) continue;
1467 cell[--count2]=lc->subcell[x+y*nx+z*a];
1470 cell[count1++]=lc->subcell[x+y*nx+z*a];
1481 int link_cell_shutdown(t_moldyn *moldyn) {
1488 for(i=0;i<lc->cells;i++) {
1490 free(lc->subcell[i]);
1492 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1493 list_destroy_f(&(lc->subcell[i]));
1502 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1506 t_moldyn_schedule *schedule;
1508 schedule=&(moldyn->schedule);
1509 count=++(schedule->total_sched);
1511 ptr=realloc(schedule->runs,count*sizeof(int));
1513 perror("[moldyn] realloc (runs)");
1517 schedule->runs[count-1]=runs;
1519 ptr=realloc(schedule->tau,count*sizeof(double));
1521 perror("[moldyn] realloc (tau)");
1525 schedule->tau[count-1]=tau;
1527 printf("[moldyn] schedule added:\n");
1528 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1534 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1536 moldyn->schedule.hook=hook;
1537 moldyn->schedule.hook_params=hook_params;
1544 * 'integration of newtons equation' - algorithms
1548 /* start the integration */
1550 int moldyn_integrate(t_moldyn *moldyn) {
1553 unsigned int e,m,s,v,p,t,a;
1555 t_moldyn_schedule *sched;
1560 double energy_scale;
1561 struct timeval t1,t2;
1564 sched=&(moldyn->schedule);
1567 /* initialize linked cell method */
1568 link_cell_init(moldyn,VERBOSE);
1570 /* logging & visualization */
1579 /* sqaure of some variables */
1580 moldyn->tau_square=moldyn->tau*moldyn->tau;
1582 /* get current time */
1583 gettimeofday(&t1,NULL);
1585 /* calculate initial forces */
1586 potential_force_calc(moldyn);
1591 /* some stupid checks before we actually start calculating bullshit */
1592 if(moldyn->cutoff>0.5*moldyn->dim.x)
1593 printf("[moldyn] WARNING: cutoff > 0.5 x dim.x\n");
1594 if(moldyn->cutoff>0.5*moldyn->dim.y)
1595 printf("[moldyn] WARNING: cutoff > 0.5 x dim.y\n");
1596 if(moldyn->cutoff>0.5*moldyn->dim.z)
1597 printf("[moldyn] WARNING: cutoff > 0.5 x dim.z\n");
1599 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1600 if(ds>0.05*moldyn->nnd)
1601 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1604 /* zero absolute time */
1605 // should have right values!
1607 //moldyn->total_steps=0;
1609 /* debugging, ignore */
1612 /* tell the world */
1613 printf("[moldyn] integration start, go get a coffee ...\n");
1615 /* executing the schedule */
1617 while(sched->count<sched->total_sched) {
1619 /* setting amount of runs and finite time step size */
1620 moldyn->tau=sched->tau[sched->count];
1621 moldyn->tau_square=moldyn->tau*moldyn->tau;
1622 moldyn->time_steps=sched->runs[sched->count];
1624 /* energy scaling factor (might change!) */
1625 energy_scale=moldyn->count*EV;
1627 /* integration according to schedule */
1629 for(i=0;i<moldyn->time_steps;i++) {
1631 /* integration step */
1632 moldyn->integrate(moldyn);
1634 /* calculate kinetic energy, temperature and pressure */
1636 temperature_calc(moldyn);
1638 pressure_calc(moldyn);
1640 thermodynamic_pressure_calc(moldyn);
1641 printf("\n\nDEBUG: numeric pressure calc: %f\n\n",
1645 /* calculate fluctuations + averages */
1646 average_and_fluctuation_calc(moldyn);
1649 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1650 scale_velocity(moldyn,FALSE);
1651 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1652 scale_volume(moldyn);
1654 /* check for log & visualization */
1656 if(!(moldyn->total_steps%e))
1657 dprintf(moldyn->efd,
1659 moldyn->time,moldyn->ekin/energy_scale,
1660 moldyn->energy/energy_scale,
1661 get_total_energy(moldyn)/energy_scale);
1664 if(!(moldyn->total_steps%m)) {
1665 momentum=get_total_p(moldyn);
1666 dprintf(moldyn->mfd,
1667 "%f %f %f %f %f\n",moldyn->time,
1668 momentum.x,momentum.y,momentum.z,
1669 v3_norm(&momentum));
1673 if(!(moldyn->total_steps%p)) {
1674 dprintf(moldyn->pfd,
1675 "%f %f %f %f %f %f %f\n",moldyn->time,
1676 moldyn->p/BAR,moldyn->p_avg/BAR,
1677 moldyn->gp/BAR,moldyn->gp_avg/BAR,
1678 moldyn->tp/BAR,moldyn->tp_avg/BAR);
1682 if(!(moldyn->total_steps%t)) {
1683 dprintf(moldyn->tfd,
1685 moldyn->time,moldyn->t,moldyn->t_avg);
1689 if(!(moldyn->total_steps%v)) {
1690 dprintf(moldyn->vfd,
1691 "%f %f\n",moldyn->time,moldyn->volume);
1695 if(!(moldyn->total_steps%s)) {
1696 snprintf(dir,128,"%s/s-%07.f.save",
1697 moldyn->vlsdir,moldyn->time);
1698 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
1700 if(fd<0) perror("[moldyn] save fd open");
1702 write(fd,moldyn,sizeof(t_moldyn));
1703 write(fd,moldyn->atom,
1704 moldyn->count*sizeof(t_atom));
1710 if(!(moldyn->total_steps%a)) {
1711 visual_atoms(moldyn);
1715 /* display progress */
1716 //if(!(moldyn->total_steps%10)) {
1717 /* get current time */
1718 gettimeofday(&t2,NULL);
1720 printf("\rsched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)",
1721 sched->count,i,moldyn->total_steps,
1722 moldyn->t,moldyn->t_avg,
1723 moldyn->p/BAR,moldyn->p_avg/BAR,
1724 //moldyn->p/BAR,(moldyn->p-2.0*moldyn->ekin/(3.0*moldyn->volume))/BAR,
1726 (int)(t2.tv_sec-t1.tv_sec));
1730 /* copy over time */
1734 /* increase absolute time */
1735 moldyn->time+=moldyn->tau;
1736 moldyn->total_steps+=1;
1740 /* check for hooks */
1742 printf("\n ## schedule hook %d start ##\n",
1744 sched->hook(moldyn,sched->hook_params);
1745 printf(" ## schedule hook end ##\n");
1748 /* increase the schedule counter */
1756 /* velocity verlet */
1758 int velocity_verlet(t_moldyn *moldyn) {
1761 double tau,tau_square,h;
1766 count=moldyn->count;
1768 tau_square=moldyn->tau_square;
1770 for(i=0;i<count;i++) {
1771 /* check whether fixed atom */
1772 if(atom[i].attr&ATOM_ATTR_FP)
1776 v3_scale(&delta,&(atom[i].v),tau);
1777 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1778 v3_scale(&delta,&(atom[i].f),h*tau_square);
1779 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1780 check_per_bound(moldyn,&(atom[i].r));
1782 /* velocities [actually v(t+tau/2)] */
1783 v3_scale(&delta,&(atom[i].f),h*tau);
1784 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1787 /* criticial check */
1788 moldyn_bc_check(moldyn);
1790 /* neighbour list update */
1791 link_cell_update(moldyn);
1793 /* forces depending on chosen potential */
1795 potential_force_calc(moldyn);
1797 albe_potential_force_calc(moldyn);
1800 for(i=0;i<count;i++) {
1801 /* check whether fixed atom */
1802 if(atom[i].attr&ATOM_ATTR_FP)
1804 /* again velocities [actually v(t+tau)] */
1805 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1806 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1815 * potentials & corresponding forces & virial routine
1819 /* generic potential and force calculation */
1821 int potential_force_calc(t_moldyn *moldyn) {
1824 t_atom *itom,*jtom,*ktom;
1828 int *neighbour_i[27];
1832 t_list neighbour_i[27];
1833 t_list neighbour_i2[27];
1839 count=moldyn->count;
1849 /* reset global virial */
1850 memset(&(moldyn->gvir),0,sizeof(t_virial));
1852 /* reset force, site energy and virial of every atom */
1854 #pragma omp parallel for private(virial)
1856 for(i=0;i<count;i++) {
1859 v3_zero(&(itom[i].f));
1862 virial=(&(itom[i].virial));
1870 /* reset site energy */
1875 /* get energy, force and virial of every atom */
1877 /* first (and only) loop over atoms i */
1878 for(i=0;i<count;i++) {
1880 /* single particle potential/force */
1881 if(itom[i].attr&ATOM_ATTR_1BP)
1883 moldyn->func1b(moldyn,&(itom[i]));
1885 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1888 /* 2 body pair potential/force */
1890 link_cell_neighbour_index(moldyn,
1891 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1892 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1893 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1898 /* first loop over atoms j */
1899 if(moldyn->func2b) {
1906 while(neighbour_i[j][p]!=0) {
1908 jtom=&(atom[neighbour_i[j][p]]);
1911 this=&(neighbour_i[j]);
1914 if(this->start==NULL)
1918 jtom=this->current->data;
1921 if(jtom==&(itom[i]))
1924 if((jtom->attr&ATOM_ATTR_2BP)&
1925 (itom[i].attr&ATOM_ATTR_2BP)) {
1926 moldyn->func2b(moldyn,
1934 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1940 /* 3 body potential/force */
1942 if(!(itom[i].attr&ATOM_ATTR_3BP))
1945 /* copy the neighbour lists */
1947 /* no copy needed for static lists */
1949 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
1952 /* second loop over atoms j */
1959 while(neighbour_i[j][p]!=0) {
1961 jtom=&(atom[neighbour_i[j][p]]);
1964 this=&(neighbour_i[j]);
1967 if(this->start==NULL)
1972 jtom=this->current->data;
1975 if(jtom==&(itom[i]))
1978 if(!(jtom->attr&ATOM_ATTR_3BP))
1984 if(moldyn->func3b_j1)
1985 moldyn->func3b_j1(moldyn,
1990 /* in first j loop, 3bp run can be skipped */
1991 if(!(moldyn->run3bp))
1994 /* first loop over atoms k */
1995 if(moldyn->func3b_k1) {
2003 while(neighbour_i[j][q]!=0) {
2005 ktom=&(atom[neighbour_i[k][q]]);
2008 that=&(neighbour_i2[k]);
2011 if(that->start==NULL)
2015 ktom=that->current->data;
2018 if(!(ktom->attr&ATOM_ATTR_3BP))
2024 if(ktom==&(itom[i]))
2027 moldyn->func3b_k1(moldyn,
2035 } while(list_next_f(that)!=\
2043 if(moldyn->func3b_j2)
2044 moldyn->func3b_j2(moldyn,
2049 /* second loop over atoms k */
2050 if(moldyn->func3b_k2) {
2058 while(neighbour_i[j][q]!=0) {
2060 ktom=&(atom[neighbour_i[k][q]]);
2063 that=&(neighbour_i2[k]);
2066 if(that->start==NULL)
2070 ktom=that->current->data;
2073 if(!(ktom->attr&ATOM_ATTR_3BP))
2079 if(ktom==&(itom[i]))
2082 moldyn->func3b_k2(moldyn,
2091 } while(list_next_f(that)!=\
2099 /* 2bp post function */
2100 if(moldyn->func3b_j3) {
2101 moldyn->func3b_j3(moldyn,
2108 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2123 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2124 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2126 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
2127 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
2128 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
2132 /* some postprocessing */
2134 #pragma omp parallel for
2136 for(i=0;i<count;i++) {
2137 /* calculate global virial */
2138 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
2139 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
2140 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
2141 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
2142 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
2143 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
2145 /* check forces regarding the given timestep */
2146 if(v3_norm(&(itom[i].f))>\
2147 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
2148 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",
2156 * virial calculation
2159 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2160 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2162 a->virial.xx+=f->x*d->x;
2163 a->virial.yy+=f->y*d->y;
2164 a->virial.zz+=f->z*d->z;
2165 a->virial.xy+=f->x*d->y;
2166 a->virial.xz+=f->x*d->z;
2167 a->virial.yz+=f->y*d->z;
2173 * periodic boundary checking
2176 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2177 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2188 if(moldyn->status&MOLDYN_STAT_PBX) {
2189 if(a->x>=x) a->x-=dim->x;
2190 else if(-a->x>x) a->x+=dim->x;
2192 if(moldyn->status&MOLDYN_STAT_PBY) {
2193 if(a->y>=y) a->y-=dim->y;
2194 else if(-a->y>y) a->y+=dim->y;
2196 if(moldyn->status&MOLDYN_STAT_PBZ) {
2197 if(a->z>=z) a->z-=dim->z;
2198 else if(-a->z>z) a->z+=dim->z;
2205 * debugging / critical check functions
2208 int moldyn_bc_check(t_moldyn *moldyn) {
2221 for(i=0;i<moldyn->count;i++) {
2222 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2223 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2224 i,atom[i].r.x,dim->x/2);
2225 printf("diagnostic:\n");
2226 printf("-----------\natom.r.x:\n");
2228 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2231 ((byte)&(1<<k))?1:0,
2234 printf("---------------\nx=dim.x/2:\n");
2236 memcpy(&byte,(u8 *)(&x)+j,1);
2239 ((byte)&(1<<k))?1:0,
2242 if(atom[i].r.x==x) printf("the same!\n");
2243 else printf("different!\n");
2245 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2246 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2247 i,atom[i].r.y,dim->y/2);
2248 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2249 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2250 i,atom[i].r.z,dim->z/2);
2260 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2267 fd=open(file,O_RDONLY);
2269 perror("[moldyn] load save file open");
2273 fsize=lseek(fd,0,SEEK_END);
2274 lseek(fd,0,SEEK_SET);
2276 size=sizeof(t_moldyn);
2279 cnt=read(fd,moldyn,size);
2281 perror("[moldyn] load save file read (moldyn)");
2287 size=moldyn->count*sizeof(t_atom);
2289 /* correcting possible atom data offset */
2291 if(fsize!=sizeof(t_moldyn)+size) {
2292 corr=fsize-sizeof(t_moldyn)-size;
2293 printf("[moldyn] WARNING: lsf (illegal file size)\n");
2294 printf(" moifying offset:\n");
2295 printf(" - current pos: %d\n",sizeof(t_moldyn));
2296 printf(" - atom size: %d\n",size);
2297 printf(" - file size: %d\n",fsize);
2298 printf(" => correction: %d\n",corr);
2299 lseek(fd,corr,SEEK_CUR);
2302 moldyn->atom=(t_atom *)malloc(size);
2303 if(moldyn->atom==NULL) {
2304 perror("[moldyn] load save file malloc (atoms)");
2309 cnt=read(fd,moldyn->atom,size);
2311 perror("[moldyn] load save file read (atoms)");
2322 int moldyn_free_save_file(t_moldyn *moldyn) {
2329 int moldyn_load(t_moldyn *moldyn) {
2337 * function to find/callback all combinations of 2 body bonds
2340 int process_2b_bonds(t_moldyn *moldyn,void *data,
2341 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2342 void *data,u8 bc)) {
2349 t_list neighbour[27];
2359 for(i=0;i<moldyn->count;i++) {
2360 /* neighbour indexing */
2361 link_cell_neighbour_index(moldyn,
2362 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2363 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2364 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2369 bc=(j<lc->dnlc)?0:1;
2374 while(neighbour[j][p]!=0) {
2376 jtom=&(moldyn->atom[neighbour[j][p]]);
2379 this=&(neighbour[j]);
2382 if(this->start==NULL)
2387 jtom=this->current->data;
2391 process(moldyn,&(itom[i]),jtom,data,bc);
2396 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2406 * post processing functions
2409 int get_line(int fd,char *line,int max) {
2416 if(count==max) return count;
2417 ret=read(fd,line+count,1);
2418 if(ret<=0) return ret;
2419 if(line[count]=='\n') {
2420 memset(line+count,0,max-count-1);
2428 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2434 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
2450 for(i=0;i<moldyn->count;i++) {
2452 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
2453 check_per_bound(moldyn,&dist);
2454 d2=v3_absolute_square(&dist);
2468 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
2469 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
2470 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
2475 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
2480 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
2481 t_atom *jtom,void *data,u8 bc) {
2488 /* only count pairs once,
2489 * skip same atoms */
2490 if(itom->tag>=jtom->tag)
2494 * pair correlation calc
2501 v3_sub(&dist,&(jtom->r),&(itom->r));
2502 if(bc) check_per_bound(moldyn,&dist);
2503 d=v3_absolute_square(&dist);
2505 /* ignore if greater cutoff */
2506 if(d>moldyn->cutoff_square)
2509 /* fill the slots */
2513 /* should never happen but it does 8) -
2514 * related to -ffloat-store problem! */
2516 printf("[moldyn] WARNING: pcc (%d/%d)",
2522 if(itom->brand!=jtom->brand) {
2527 /* type a - type a bonds */
2529 pcc->stat[s+pcc->o1]+=1;
2531 /* type b - type b bonds */
2532 pcc->stat[s+pcc->o2]+=1;
2538 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2545 pcc.o1=moldyn->cutoff/dr;
2548 if(pcc.o1*dr<=moldyn->cutoff)
2549 printf("[moldyn] WARNING: pcc (low #slots)\n");
2551 printf("[moldyn] pair correlation calc info:\n");
2552 printf(" time: %f\n",moldyn->time);
2553 printf(" count: %d\n",moldyn->count);
2554 printf(" cutoff: %f\n",moldyn->cutoff);
2555 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2558 pcc.stat=(double *)ptr;
2561 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
2562 if(pcc.stat==NULL) {
2563 perror("[moldyn] pair correlation malloc");
2568 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
2571 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
2574 for(i=1;i<pcc.o1;i++) {
2575 // normalization: 4 pi r^2 dr
2576 // here: not double counting pairs -> 2 pi r r dr
2577 // ... and actually it's a constant times r^2
2580 pcc.stat[pcc.o1+i]/=norm;
2581 pcc.stat[pcc.o2+i]/=norm;
2586 /* todo: store/print pair correlation function */
2593 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2600 if(itom->tag>=jtom->tag)
2604 v3_sub(&dist,&(jtom->r),&(itom->r));
2605 if(bc) check_per_bound(moldyn,&dist);
2606 d=v3_absolute_square(&dist);
2608 /* ignore if greater or equal cutoff */
2609 if(d>moldyn->cutoff_square)
2612 /* check for potential bond */
2613 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2616 /* now count this bonding ... */
2619 /* increase total bond counter
2620 * ... double counting!
2625 ba->acnt[jtom->tag]+=1;
2627 ba->bcnt[jtom->tag]+=1;
2630 ba->acnt[itom->tag]+=1;
2632 ba->bcnt[itom->tag]+=1;
2637 int bond_analyze(t_moldyn *moldyn,double *quality) {
2639 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
2647 ba.acnt=malloc(moldyn->count*sizeof(int));
2649 perror("[moldyn] bond analyze malloc (a)");
2652 memset(ba.acnt,0,moldyn->count*sizeof(int));
2654 ba.bcnt=malloc(moldyn->count*sizeof(int));
2656 perror("[moldyn] bond analyze malloc (b)");
2659 memset(ba.bcnt,0,moldyn->count*sizeof(int));
2668 process_2b_bonds(moldyn,&ba,bond_analyze_process);
2670 for(i=0;i<moldyn->count;i++) {
2671 if(atom[i].brand==0) {
2672 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
2676 if((ba.acnt[i]==4)&(ba.bcnt[i]==0)) {
2684 printf("[moldyn] bond analyze: c_cnt=%d | set=%d\n",ccnt,cset);
2685 printf("[moldyn] bond analyze: q_cnt=%d | tot=%d\n",qcnt,ba.tcnt);
2688 quality[0]=1.0*ccnt/cset;
2689 quality[1]=1.0*qcnt/ba.tcnt;
2692 printf("[moldyn] bond analyze: c_bnd_q=%f\n",1.0*qcnt/ba.tcnt);
2693 printf("[moldyn] bond analyze: tot_q=%f\n",1.0*qcnt/ba.tcnt);
2700 * visualization code
2703 int visual_init(t_moldyn *moldyn,char *filebase) {
2705 strncpy(moldyn->vis.fb,filebase,128);
2710 int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2717 if(itom->tag>=jtom->tag)
2720 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2723 if((itom->attr&ATOM_ATTR_VB)|(jtom->attr&ATOM_ATTR_VB))
2724 dprintf(vb->fd,"# [B] %f %f %f %f %f %f\n",
2725 itom->r.x,itom->r.y,itom->r.z,
2726 jtom->r.x,jtom->r.y,jtom->r.z);
2731 int visual_atoms(t_moldyn *moldyn) {
2749 sprintf(file,"%s/atomic_conf_%07.f.xyz",v->fb,moldyn->time);
2750 vb.fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
2752 perror("open visual save file fd");
2756 /* write the actual data file */
2759 dprintf(vb.fd,"# [P] %d %07.f <%f,%f,%f>\n",
2760 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
2762 // atomic configuration
2763 for(i=0;i<moldyn->count;i++)
2764 // atom type, positions, color and kinetic energy
2765 dprintf(vb.fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
2769 pse_col[atom[i].element],
2772 // bonds between atoms
2773 process_2b_bonds(moldyn,&vb,visual_bonds_process);
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);
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);
2796 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2797 dim.x/2,dim.y/2,dim.z/2,
2798 dim.x/2,-dim.y/2,dim.z/2);
2799 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2800 -dim.x/2,dim.y/2,dim.z/2,
2801 dim.x/2,dim.y/2,dim.z/2);
2803 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2804 -dim.x/2,-dim.y/2,dim.z/2,
2805 -dim.x/2,-dim.y/2,-dim.z/2);
2806 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2807 -dim.x/2,dim.y/2,dim.z/2,
2808 -dim.x/2,dim.y/2,-dim.z/2);
2809 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2810 dim.x/2,-dim.y/2,dim.z/2,
2811 dim.x/2,-dim.y/2,-dim.z/2);
2812 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2813 dim.x/2,dim.y/2,dim.z/2,
2814 dim.x/2,dim.y/2,-dim.z/2);
2823 * fpu cntrol functions
2826 // set rounding to double (eliminates -ffloat-store!)
2827 int fpu_set_rtd(void) {
2833 ctrl&=~_FPU_EXTENDED;