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"
27 #include "potentials/albe_orig.h"
29 #include "potentials/tersoff_orig.h"
31 #include "potentials/tersoff.h"
35 * the moldyn functions
38 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
40 printf("[moldyn] init\n");
42 memset(moldyn,0,sizeof(t_moldyn));
47 rand_init(&(moldyn->random),NULL,1);
48 moldyn->random.status|=RAND_STAT_VERBOSE;
53 int moldyn_shutdown(t_moldyn *moldyn) {
55 printf("[moldyn] shutdown\n");
57 moldyn_log_shutdown(moldyn);
58 link_cell_shutdown(moldyn);
59 rand_close(&(moldyn->random));
65 int set_int_alg(t_moldyn *moldyn,u8 algo) {
67 printf("[moldyn] integration algorithm: ");
70 case MOLDYN_INTEGRATE_VERLET:
71 moldyn->integrate=velocity_verlet;
72 printf("velocity verlet\n");
75 printf("unknown integration algorithm: %02x\n",algo);
83 int set_cutoff(t_moldyn *moldyn,double cutoff) {
85 moldyn->cutoff=cutoff;
86 moldyn->cutoff_square=cutoff*cutoff;
88 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
93 int set_temperature(t_moldyn *moldyn,double t_ref) {
97 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
102 int set_pressure(t_moldyn *moldyn,double p_ref) {
106 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
111 int set_p_scale(t_moldyn *moldyn,u8 ptype,double ptc) {
113 moldyn->pt_scale&=(~(P_SCALE_MASK));
114 moldyn->pt_scale|=ptype;
117 printf("[moldyn] p scaling:\n");
119 printf(" p: %s",ptype?"yes":"no ");
121 printf(" | type: %02x | factor: %f",ptype,ptc);
127 int set_t_scale(t_moldyn *moldyn,u8 ttype,double ttc) {
129 moldyn->pt_scale&=(~(T_SCALE_MASK));
130 moldyn->pt_scale|=ttype;
133 printf("[moldyn] t scaling:\n");
135 printf(" t: %s",ttype?"yes":"no ");
137 printf(" | type: %02x | factor: %f",ttype,ttc);
143 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
145 moldyn->pt_scale=(ptype|ttype);
149 printf("[moldyn] p/t scaling:\n");
151 printf(" p: %s",ptype?"yes":"no ");
153 printf(" | type: %02x | factor: %f",ptype,ptc);
156 printf(" t: %s",ttype?"yes":"no ");
158 printf(" | type: %02x | factor: %f",ttype,ttc);
164 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
170 moldyn->volume=x*y*z;
178 printf("[moldyn] dimensions in A and A^3 respectively:\n");
179 printf(" x: %f\n",moldyn->dim.x);
180 printf(" y: %f\n",moldyn->dim.y);
181 printf(" z: %f\n",moldyn->dim.z);
182 printf(" volume: %f\n",moldyn->volume);
183 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
188 int set_nn_dist(t_moldyn *moldyn,double dist) {
195 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
197 printf("[moldyn] periodic boundary conditions:\n");
200 moldyn->status|=MOLDYN_STAT_PBX;
203 moldyn->status|=MOLDYN_STAT_PBY;
206 moldyn->status|=MOLDYN_STAT_PBZ;
208 printf(" x: %s\n",x?"yes":"no");
209 printf(" y: %s\n",y?"yes":"no");
210 printf(" z: %s\n",z?"yes":"no");
215 int set_potential(t_moldyn *moldyn,u8 type) {
218 case MOLDYN_POTENTIAL_TM:
219 moldyn->func_i0=tersoff_mult_1bp;
220 moldyn->func_j1=tersoff_mult_3bp_j1;
221 moldyn->func_j1_k0=tersoff_mult_3bp_k1;
222 moldyn->func_j1c=tersoff_mult_3bp_j2;
223 moldyn->func_j1_k1=tersoff_mult_3bp_k2;
224 moldyn->check_2b_bond=tersoff_mult_check_2b_bond;
226 case MOLDYN_POTENTIAL_AO:
227 moldyn->func_j1=albe_orig_mult_3bp_j1;
228 moldyn->func_j1_k0=albe_orig_mult_3bp_k1;
229 moldyn->func_j1c=albe_orig_mult_3bp_j2;
230 moldyn->func_j1_k1=albe_orig_mult_3bp_k2;
231 moldyn->check_2b_bond=albe_orig_mult_check_2b_bond;
233 case MOLDYN_POTENTIAL_AM:
234 moldyn->func_i0=albe_mult_i0;
235 moldyn->func_j0=albe_mult_i0_j0;
236 moldyn->func_j0_k0=albe_mult_i0_j0_k0;
237 moldyn->func_j0e=albe_mult_i0_j1;
238 moldyn->func_j1=albe_mult_i0_j2;
239 moldyn->func_j1_k0=albe_mult_i0_j2_k0;
240 moldyn->func_j1c=albe_mult_i0_j3;
241 moldyn->check_2b_bond=albe_mult_check_2b_bond;
243 case MOLDYN_POTENTIAL_HO:
244 moldyn->func_j0=harmonic_oscillator;
245 moldyn->check_2b_bond=harmonic_oscillator_check_2b_bond;
247 case MOLDYN_POTENTIAL_LJ:
248 moldyn->func_j0=lennard_jones;
249 moldyn->check_2b_bond=lennard_jones_check_2b_bond;
252 printf("[moldyn] set potential: unknown type %02x\n",
260 int set_avg_skip(t_moldyn *moldyn,int skip) {
262 printf("[moldyn] skip %d steps before starting average calc\n",skip);
263 moldyn->avg_skip=skip;
268 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
270 strncpy(moldyn->vlsdir,dir,127);
275 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
277 strncpy(moldyn->rauthor,author,63);
278 strncpy(moldyn->rtitle,title,63);
283 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
288 printf("[moldyn] set log: ");
291 case LOG_TOTAL_ENERGY:
292 moldyn->ewrite=timer;
293 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
294 moldyn->efd=open(filename,
295 O_WRONLY|O_CREAT|O_EXCL,
298 perror("[moldyn] energy log fd open");
301 dprintf(moldyn->efd,"# total energy log file\n");
302 printf("total energy (%d)\n",timer);
304 case LOG_TOTAL_MOMENTUM:
305 moldyn->mwrite=timer;
306 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
307 moldyn->mfd=open(filename,
308 O_WRONLY|O_CREAT|O_EXCL,
311 perror("[moldyn] momentum log fd open");
314 dprintf(moldyn->efd,"# total momentum log file\n");
315 printf("total momentum (%d)\n",timer);
318 moldyn->pwrite=timer;
319 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
320 moldyn->pfd=open(filename,
321 O_WRONLY|O_CREAT|O_EXCL,
324 perror("[moldyn] pressure log file\n");
327 dprintf(moldyn->pfd,"# pressure log file\n");
328 printf("pressure (%d)\n",timer);
330 case LOG_TEMPERATURE:
331 moldyn->twrite=timer;
332 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
333 moldyn->tfd=open(filename,
334 O_WRONLY|O_CREAT|O_EXCL,
337 perror("[moldyn] temperature log file\n");
340 dprintf(moldyn->tfd,"# temperature log file\n");
341 printf("temperature (%d)\n",timer);
344 moldyn->vwrite=timer;
345 snprintf(filename,127,"%s/volume",moldyn->vlsdir);
346 moldyn->vfd=open(filename,
347 O_WRONLY|O_CREAT|O_EXCL,
350 perror("[moldyn] volume log file\n");
353 dprintf(moldyn->vfd,"# volume log file\n");
354 printf("volume (%d)\n",timer);
357 moldyn->swrite=timer;
358 printf("save file (%d)\n",timer);
361 moldyn->awrite=timer;
362 ret=visual_init(moldyn,moldyn->vlsdir);
364 printf("[moldyn] visual init failure\n");
367 printf("visual file (%d)\n",timer);
370 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
371 moldyn->rfd=open(filename,
372 O_WRONLY|O_CREAT|O_EXCL,
375 perror("[moldyn] report fd open");
378 printf("report -> ");
380 snprintf(filename,127,"%s/e_plot.scr",
382 moldyn->epfd=open(filename,
383 O_WRONLY|O_CREAT|O_EXCL,
386 perror("[moldyn] energy plot fd open");
389 dprintf(moldyn->epfd,e_plot_script);
394 snprintf(filename,127,"%s/pressure_plot.scr",
396 moldyn->ppfd=open(filename,
397 O_WRONLY|O_CREAT|O_EXCL,
400 perror("[moldyn] p plot fd open");
403 dprintf(moldyn->ppfd,pressure_plot_script);
408 snprintf(filename,127,"%s/temperature_plot.scr",
410 moldyn->tpfd=open(filename,
411 O_WRONLY|O_CREAT|O_EXCL,
414 perror("[moldyn] t plot fd open");
417 dprintf(moldyn->tpfd,temperature_plot_script);
419 printf("temperature ");
421 dprintf(moldyn->rfd,report_start,
422 moldyn->rauthor,moldyn->rtitle);
426 printf("unknown log type: %02x\n",type);
433 int moldyn_log_shutdown(t_moldyn *moldyn) {
437 printf("[moldyn] log shutdown\n");
441 dprintf(moldyn->rfd,report_energy);
442 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
447 if(moldyn->mfd) close(moldyn->mfd);
451 dprintf(moldyn->rfd,report_pressure);
452 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
459 dprintf(moldyn->rfd,report_temperature);
460 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
465 dprintf(moldyn->rfd,report_end);
467 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
470 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
473 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
482 * creating lattice functions
485 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
486 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
498 /* how many atoms do we expect */
499 if(type==CUBIC) new*=1;
500 if(type==FCC) new*=4;
501 if(type==DIAMOND) new*=8;
503 /* allocate space for atoms */
504 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
506 perror("[moldyn] realloc (create lattice)");
510 atom=&(moldyn->atom[count]);
512 /* no atoms on the boundaries (only reason: it looks better!) */
526 set_nn_dist(moldyn,lc);
527 ret=cubic_init(a,b,c,lc,atom,&orig);
528 strcpy(name,"cubic");
532 v3_scale(&orig,&orig,0.5);
533 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
534 ret=fcc_init(a,b,c,lc,atom,&orig);
539 v3_scale(&orig,&orig,0.25);
540 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
541 ret=diamond_init(a,b,c,lc,atom,&orig);
542 strcpy(name,"diamond");
545 printf("unknown lattice type (%02x)\n",type);
551 printf("[moldyn] creating lattice failed\n");
552 printf(" amount of atoms\n");
553 printf(" - expected: %d\n",new);
554 printf(" - created: %d\n",ret);
559 printf("[moldyn] created %s lattice with %d atoms\n",name,new);
561 for(ret=0;ret<new;ret++) {
562 atom[ret].element=element;
565 atom[ret].brand=brand;
566 atom[ret].tag=count+ret;
567 check_per_bound(moldyn,&(atom[ret].r));
568 atom[ret].r_0=atom[ret].r;
571 /* update total system mass */
572 total_mass_calc(moldyn);
577 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
578 t_3dvec *r,t_3dvec *v) {
585 count=(moldyn->count)++; // asshole style!
587 ptr=realloc(atom,(count+1)*sizeof(t_atom));
589 perror("[moldyn] realloc (add atom)");
596 /* initialize new atom */
597 memset(&(atom[count]),0,sizeof(t_atom));
600 atom[count].element=element;
601 atom[count].mass=mass;
602 atom[count].brand=brand;
603 atom[count].tag=count;
604 atom[count].attr=attr;
605 check_per_bound(moldyn,&(atom[count].r));
606 atom[count].r_0=atom[count].r;
608 /* update total system mass */
609 total_mass_calc(moldyn);
614 int del_atom(t_moldyn *moldyn,int tag) {
621 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
623 perror("[moldyn]malloc (del atom)");
627 for(cnt=0;cnt<tag;cnt++)
630 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
632 new[cnt-1].tag=cnt-1;
644 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
663 v3_copy(&(atom[count].r),&r);
672 for(i=0;i<count;i++) {
673 atom[i].r.x-=(a*lc)/2.0;
674 atom[i].r.y-=(b*lc)/2.0;
675 atom[i].r.z-=(c*lc)/2.0;
681 /* fcc lattice init */
682 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
695 /* construct the basis */
696 memset(basis,0,3*sizeof(t_3dvec));
704 /* fill up the room */
712 v3_copy(&(atom[count].r),&r);
715 /* the three face centered atoms */
717 v3_add(&n,&r,&basis[l]);
718 v3_copy(&(atom[count].r),&n);
727 /* coordinate transformation */
728 for(i=0;i<count;i++) {
729 atom[i].r.x-=(a*lc)/2.0;
730 atom[i].r.y-=(b*lc)/2.0;
731 atom[i].r.z-=(c*lc)/2.0;
737 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
742 count=fcc_init(a,b,c,lc,atom,origin);
748 if(origin) v3_add(&o,&o,origin);
750 count+=fcc_init(a,b,c,lc,&atom[count],&o);
755 int destroy_atoms(t_moldyn *moldyn) {
757 if(moldyn->atom) free(moldyn->atom);
762 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
765 * - gaussian distribution of velocities
766 * - zero total momentum
767 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
772 t_3dvec p_total,delta;
777 random=&(moldyn->random);
779 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
781 /* gaussian distribution of velocities */
783 for(i=0;i<moldyn->count;i++) {
784 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
786 v=sigma*rand_get_gauss(random);
788 p_total.x+=atom[i].mass*v;
790 v=sigma*rand_get_gauss(random);
792 p_total.y+=atom[i].mass*v;
794 v=sigma*rand_get_gauss(random);
796 p_total.z+=atom[i].mass*v;
799 /* zero total momentum */
800 v3_scale(&p_total,&p_total,1.0/moldyn->count);
801 for(i=0;i<moldyn->count;i++) {
802 v3_scale(&delta,&p_total,1.0/atom[i].mass);
803 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
806 /* velocity scaling */
807 scale_velocity(moldyn,equi_init);
812 double total_mass_calc(t_moldyn *moldyn) {
818 for(i=0;i<moldyn->count;i++)
819 moldyn->mass+=moldyn->atom[i].mass;
824 double temperature_calc(t_moldyn *moldyn) {
826 /* assume up to date kinetic energy, which is 3/2 N k_B T */
828 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
833 double get_temperature(t_moldyn *moldyn) {
838 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
848 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
851 /* get kinetic energy / temperature & count involved atoms */
854 for(i=0;i<moldyn->count;i++) {
855 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
856 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
861 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
862 else return 0; /* no atoms involved in scaling! */
864 /* (temporary) hack for e,t = 0 */
867 if(moldyn->t_ref!=0.0) {
868 thermal_init(moldyn,equi_init);
872 return 0; /* no scaling needed */
876 /* get scaling factor */
877 scale=moldyn->t_ref/moldyn->t;
881 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
882 scale=1.0+(scale-1.0)/moldyn->t_tc;
885 /* velocity scaling */
886 for(i=0;i<moldyn->count;i++) {
887 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
888 v3_scale(&(atom[i].v),&(atom[i].v),scale);
894 double ideal_gas_law_pressure(t_moldyn *moldyn) {
898 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
903 double virial_sum(t_moldyn *moldyn) {
908 /* virial (sum over atom virials) */
916 for(i=0;i<moldyn->count;i++) {
917 virial=&(moldyn->atom[i].virial);
918 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
919 moldyn->vir.xx+=virial->xx;
920 moldyn->vir.yy+=virial->yy;
921 moldyn->vir.zz+=virial->zz;
922 moldyn->vir.xy+=virial->xy;
923 moldyn->vir.xz+=virial->xz;
924 moldyn->vir.yz+=virial->yz;
927 /* global virial (absolute coordinates) */
928 virial=&(moldyn->gvir);
929 moldyn->gv=virial->xx+virial->yy+virial->zz;
931 return moldyn->virial;
934 double pressure_calc(t_moldyn *moldyn) {
938 * with W = 1/3 sum_i f_i r_i (- skipped!)
939 * virial = sum_i f_i r_i
941 * => P = (2 Ekin + virial) / (3V)
944 /* assume up to date virial & up to date kinetic energy */
946 /* pressure (atom virials) */
947 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
948 moldyn->p/=(3.0*moldyn->volume);
950 /* pressure (absolute coordinates) */
951 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
952 moldyn->gp/=(3.0*moldyn->volume);
957 int average_reset(t_moldyn *moldyn) {
959 printf("[moldyn] average reset\n");
961 /* update skip value */
962 moldyn->avg_skip=moldyn->total_steps;
968 /* potential energy */
976 moldyn->virial_sum=0.0;
987 int average_and_fluctuation_calc(t_moldyn *moldyn) {
991 if(moldyn->total_steps<moldyn->avg_skip)
994 denom=moldyn->total_steps+1-moldyn->avg_skip;
996 /* assume up to date energies, temperature, pressure etc */
999 moldyn->k_sum+=moldyn->ekin;
1000 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
1001 moldyn->k_avg=moldyn->k_sum/denom;
1002 moldyn->k2_avg=moldyn->k2_sum/denom;
1003 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
1005 /* potential energy */
1006 moldyn->v_sum+=moldyn->energy;
1007 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
1008 moldyn->v_avg=moldyn->v_sum/denom;
1009 moldyn->v2_avg=moldyn->v2_sum/denom;
1010 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1013 moldyn->t_sum+=moldyn->t;
1014 moldyn->t_avg=moldyn->t_sum/denom;
1017 moldyn->virial_sum+=moldyn->virial;
1018 moldyn->virial_avg=moldyn->virial_sum/denom;
1019 moldyn->gv_sum+=moldyn->gv;
1020 moldyn->gv_avg=moldyn->gv_sum/denom;
1023 moldyn->p_sum+=moldyn->p;
1024 moldyn->p_avg=moldyn->p_sum/denom;
1025 moldyn->gp_sum+=moldyn->gp;
1026 moldyn->gp_avg=moldyn->gp_sum/denom;
1027 moldyn->tp_sum+=moldyn->tp;
1028 moldyn->tp_avg=moldyn->tp_sum/denom;
1033 int get_heat_capacity(t_moldyn *moldyn) {
1037 /* averages needed for heat capacity calc */
1038 if(moldyn->total_steps<moldyn->avg_skip)
1041 /* (temperature average)^2 */
1042 temp2=moldyn->t_avg*moldyn->t_avg;
1043 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1046 /* ideal gas contribution */
1047 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1048 printf(" ideal gas contribution: %f\n",
1049 ighc/moldyn->mass*KILOGRAM/JOULE);
1051 /* specific heat for nvt ensemble */
1052 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1053 moldyn->c_v_nvt/=moldyn->mass;
1055 /* specific heat for nve ensemble */
1056 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1057 moldyn->c_v_nve/=moldyn->mass;
1059 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1060 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1061 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)));
1066 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1082 /* store atomic configuration + dimension */
1083 store=malloc(moldyn->count*sizeof(t_atom));
1085 printf("[moldyn] allocating store mem failed\n");
1088 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1093 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1094 su=pow(2.0-h,ONE_THIRD)-1.0;
1095 dv=(1.0-h)*moldyn->volume;
1097 /* scale up dimension and atom positions */
1098 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1099 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1100 link_cell_shutdown(moldyn);
1101 link_cell_init(moldyn,QUIET);
1102 potential_force_calc(moldyn);
1105 /* restore atomic configuration + dim */
1106 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1109 /* scale down dimension and atom positions */
1110 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1111 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1112 link_cell_shutdown(moldyn);
1113 link_cell_init(moldyn,QUIET);
1114 potential_force_calc(moldyn);
1117 /* calculate pressure */
1118 moldyn->tp=-(y1-y0)/(2.0*dv);
1120 /* restore atomic configuration */
1121 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1123 link_cell_shutdown(moldyn);
1124 link_cell_init(moldyn,QUIET);
1125 //potential_force_calc(moldyn);
1127 /* free store buffer */
1134 double get_pressure(t_moldyn *moldyn) {
1140 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1152 if(x) dim->x*=scale;
1153 if(y) dim->y*=scale;
1154 if(z) dim->z*=scale;
1159 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1170 for(i=0;i<moldyn->count;i++) {
1171 r=&(moldyn->atom[i].r);
1180 int scale_volume(t_moldyn *moldyn) {
1186 vdim=&(moldyn->vis.dim);
1190 /* scaling factor */
1191 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1192 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc;
1193 scale=pow(scale,ONE_THIRD);
1196 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1199 /* scale the atoms and dimensions */
1200 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1201 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1203 /* visualize dimensions */
1210 /* recalculate scaled volume */
1211 moldyn->volume=dim->x*dim->y*dim->z;
1213 /* adjust/reinit linkcell */
1214 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1215 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1216 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1217 link_cell_shutdown(moldyn);
1218 link_cell_init(moldyn,QUIET);
1229 double e_kin_calc(t_moldyn *moldyn) {
1237 for(i=0;i<moldyn->count;i++) {
1238 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1239 moldyn->ekin+=atom[i].ekin;
1242 return moldyn->ekin;
1245 double get_total_energy(t_moldyn *moldyn) {
1247 return(moldyn->ekin+moldyn->energy);
1250 t_3dvec get_total_p(t_moldyn *moldyn) {
1259 for(i=0;i<moldyn->count;i++) {
1260 v3_scale(&p,&(atom[i].v),atom[i].mass);
1261 v3_add(&p_total,&p_total,&p);
1267 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1271 /* nn_dist is the nearest neighbour distance */
1273 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1282 /* linked list / cell method */
1284 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1291 /* partitioning the md cell */
1292 lc->nx=moldyn->dim.x/moldyn->cutoff;
1293 lc->x=moldyn->dim.x/lc->nx;
1294 lc->ny=moldyn->dim.y/moldyn->cutoff;
1295 lc->y=moldyn->dim.y/lc->ny;
1296 lc->nz=moldyn->dim.z/moldyn->cutoff;
1297 lc->z=moldyn->dim.z/lc->nz;
1298 lc->cells=lc->nx*lc->ny*lc->nz;
1301 lc->subcell=malloc(lc->cells*sizeof(int*));
1303 lc->subcell=malloc(lc->cells*sizeof(t_list));
1306 if(lc->subcell==NULL) {
1307 perror("[moldyn] cell init (malloc)");
1312 printf("[moldyn] FATAL: less then 27 subcells!\n");
1316 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1319 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1322 printf(" x: %d x %f A\n",lc->nx,lc->x);
1323 printf(" y: %d x %f A\n",lc->ny,lc->y);
1324 printf(" z: %d x %f A\n",lc->nz,lc->z);
1329 for(i=0;i<lc->cells;i++) {
1330 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1331 if(lc->subcell[i]==NULL) {
1332 perror("[moldyn] list init (malloc)");
1337 printf(" ---> %d malloc %p (%p)\n",
1338 i,lc->subcell[0],lc->subcell);
1342 for(i=0;i<lc->cells;i++)
1343 list_init_f(&(lc->subcell[i]));
1346 /* update the list */
1347 link_cell_update(moldyn);
1352 int link_cell_update(t_moldyn *moldyn) {
1368 for(i=0;i<lc->cells;i++)
1370 memset(lc->subcell[i],0,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1372 list_destroy_f(&(lc->subcell[i]));
1375 for(count=0;count<moldyn->count;count++) {
1376 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1377 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1378 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1382 while(lc->subcell[i+j*nx+k*nx*ny][p]!=0)
1385 if(p>=MAX_ATOMS_PER_LIST) {
1386 printf("[moldyn] FATAL: amount of atoms too high!\n");
1390 lc->subcell[i+j*nx+k*nx*ny][p]=count;
1392 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1396 printf(" ---> %d %d malloc %p (%p)\n",
1397 i,count,lc->subcell[i].current,lc->subcell);
1405 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1429 if(i>=nx||j>=ny||k>=nz)
1430 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1433 cell[0]=lc->subcell[i+j*nx+k*a];
1434 for(ci=-1;ci<=1;ci++) {
1437 if((x<0)||(x>=nx)) {
1441 for(cj=-1;cj<=1;cj++) {
1444 if((y<0)||(y>=ny)) {
1448 for(ck=-1;ck<=1;ck++) {
1451 if((z<0)||(z>=nz)) {
1455 if(!(ci|cj|ck)) continue;
1457 cell[--count2]=lc->subcell[x+y*nx+z*a];
1460 cell[count1++]=lc->subcell[x+y*nx+z*a];
1471 int link_cell_shutdown(t_moldyn *moldyn) {
1478 for(i=0;i<lc->cells;i++) {
1480 free(lc->subcell[i]);
1482 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1483 list_destroy_f(&(lc->subcell[i]));
1492 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1496 t_moldyn_schedule *schedule;
1498 schedule=&(moldyn->schedule);
1499 count=++(schedule->total_sched);
1501 ptr=realloc(schedule->runs,count*sizeof(int));
1503 perror("[moldyn] realloc (runs)");
1507 schedule->runs[count-1]=runs;
1509 ptr=realloc(schedule->tau,count*sizeof(double));
1511 perror("[moldyn] realloc (tau)");
1515 schedule->tau[count-1]=tau;
1517 printf("[moldyn] schedule added:\n");
1518 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1524 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1526 moldyn->schedule.hook=hook;
1527 moldyn->schedule.hook_params=hook_params;
1534 * 'integration of newtons equation' - algorithms
1538 /* start the integration */
1540 int moldyn_integrate(t_moldyn *moldyn) {
1543 unsigned int e,m,s,v,p,t,a;
1545 t_moldyn_schedule *sched;
1550 double energy_scale;
1551 struct timeval t1,t2;
1554 sched=&(moldyn->schedule);
1557 /* initialize linked cell method */
1558 link_cell_init(moldyn,VERBOSE);
1560 /* logging & visualization */
1569 /* sqaure of some variables */
1570 moldyn->tau_square=moldyn->tau*moldyn->tau;
1572 /* get current time */
1573 gettimeofday(&t1,NULL);
1575 /* calculate initial forces */
1576 potential_force_calc(moldyn);
1581 /* some stupid checks before we actually start calculating bullshit */
1582 if(moldyn->cutoff>0.5*moldyn->dim.x)
1583 printf("[moldyn] WARNING: cutoff > 0.5 x dim.x\n");
1584 if(moldyn->cutoff>0.5*moldyn->dim.y)
1585 printf("[moldyn] WARNING: cutoff > 0.5 x dim.y\n");
1586 if(moldyn->cutoff>0.5*moldyn->dim.z)
1587 printf("[moldyn] WARNING: cutoff > 0.5 x dim.z\n");
1588 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1589 if(ds>0.05*moldyn->nnd)
1590 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1592 /* zero absolute time */
1593 // should have right values!
1595 //moldyn->total_steps=0;
1597 /* debugging, ignore */
1600 /* tell the world */
1601 printf("[moldyn] integration start, go get a coffee ...\n");
1603 /* executing the schedule */
1605 while(sched->count<sched->total_sched) {
1607 /* setting amount of runs and finite time step size */
1608 moldyn->tau=sched->tau[sched->count];
1609 moldyn->tau_square=moldyn->tau*moldyn->tau;
1610 moldyn->time_steps=sched->runs[sched->count];
1612 /* energy scaling factor (might change!) */
1613 energy_scale=moldyn->count*EV;
1615 /* integration according to schedule */
1617 for(i=0;i<moldyn->time_steps;i++) {
1619 /* integration step */
1620 moldyn->integrate(moldyn);
1622 /* calculate kinetic energy, temperature and pressure */
1624 temperature_calc(moldyn);
1626 pressure_calc(moldyn);
1628 thermodynamic_pressure_calc(moldyn);
1629 printf("\n\nDEBUG: numeric pressure calc: %f\n\n",
1633 /* calculate fluctuations + averages */
1634 average_and_fluctuation_calc(moldyn);
1637 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1638 scale_velocity(moldyn,FALSE);
1639 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1640 scale_volume(moldyn);
1642 /* check for log & visualization */
1644 if(!(moldyn->total_steps%e))
1645 dprintf(moldyn->efd,
1647 moldyn->time,moldyn->ekin/energy_scale,
1648 moldyn->energy/energy_scale,
1649 get_total_energy(moldyn)/energy_scale);
1652 if(!(moldyn->total_steps%m)) {
1653 momentum=get_total_p(moldyn);
1654 dprintf(moldyn->mfd,
1655 "%f %f %f %f %f\n",moldyn->time,
1656 momentum.x,momentum.y,momentum.z,
1657 v3_norm(&momentum));
1661 if(!(moldyn->total_steps%p)) {
1662 dprintf(moldyn->pfd,
1663 "%f %f %f %f %f %f %f\n",moldyn->time,
1664 moldyn->p/BAR,moldyn->p_avg/BAR,
1665 moldyn->gp/BAR,moldyn->gp_avg/BAR,
1666 moldyn->tp/BAR,moldyn->tp_avg/BAR);
1670 if(!(moldyn->total_steps%t)) {
1671 dprintf(moldyn->tfd,
1673 moldyn->time,moldyn->t,moldyn->t_avg);
1677 if(!(moldyn->total_steps%v)) {
1678 dprintf(moldyn->vfd,
1679 "%f %f\n",moldyn->time,moldyn->volume);
1683 if(!(moldyn->total_steps%s)) {
1684 snprintf(dir,128,"%s/s-%07.f.save",
1685 moldyn->vlsdir,moldyn->time);
1686 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
1688 if(fd<0) perror("[moldyn] save fd open");
1690 write(fd,moldyn,sizeof(t_moldyn));
1691 write(fd,moldyn->atom,
1692 moldyn->count*sizeof(t_atom));
1698 if(!(moldyn->total_steps%a)) {
1699 visual_atoms(moldyn);
1703 /* display progress */
1704 //if(!(moldyn->total_steps%10)) {
1705 /* get current time */
1706 gettimeofday(&t2,NULL);
1708 printf("\rsched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)",
1709 sched->count,i,moldyn->total_steps,
1710 moldyn->t,moldyn->t_avg,
1711 moldyn->p/BAR,moldyn->p_avg/BAR,
1712 //moldyn->p/BAR,(moldyn->p-2.0*moldyn->ekin/(3.0*moldyn->volume))/BAR,
1714 (int)(t2.tv_sec-t1.tv_sec));
1718 /* copy over time */
1722 /* increase absolute time */
1723 moldyn->time+=moldyn->tau;
1724 moldyn->total_steps+=1;
1728 /* check for hooks */
1730 printf("\n ## schedule hook %d start ##\n",
1732 sched->hook(moldyn,sched->hook_params);
1733 printf(" ## schedule hook end ##\n");
1736 /* increase the schedule counter */
1744 /* velocity verlet */
1746 int velocity_verlet(t_moldyn *moldyn) {
1749 double tau,tau_square,h;
1754 count=moldyn->count;
1756 tau_square=moldyn->tau_square;
1758 for(i=0;i<count;i++) {
1759 /* check whether fixed atom */
1760 if(atom[i].attr&ATOM_ATTR_FP)
1764 v3_scale(&delta,&(atom[i].v),tau);
1765 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1766 v3_scale(&delta,&(atom[i].f),h*tau_square);
1767 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1768 check_per_bound(moldyn,&(atom[i].r));
1770 /* velocities [actually v(t+tau/2)] */
1771 v3_scale(&delta,&(atom[i].f),h*tau);
1772 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1775 /* criticial check */
1776 moldyn_bc_check(moldyn);
1778 /* neighbour list update */
1779 link_cell_update(moldyn);
1781 /* forces depending on chosen potential */
1782 potential_force_calc(moldyn);
1784 for(i=0;i<count;i++) {
1785 /* check whether fixed atom */
1786 if(atom[i].attr&ATOM_ATTR_FP)
1788 /* again velocities [actually v(t+tau)] */
1789 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1790 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1799 * potentials & corresponding forces & virial routine
1803 /* generic potential and force calculation */
1805 int potential_force_calc(t_moldyn *moldyn) {
1808 t_atom *itom,*jtom,*ktom;
1812 int *neighbour_i[27];
1816 t_list neighbour_i[27];
1817 t_list neighbour_i2[27];
1823 count=moldyn->count;
1833 /* reset global virial */
1834 memset(&(moldyn->gvir),0,sizeof(t_virial));
1836 /* reset force, site energy and virial of every atom */
1837 for(i=0;i<count;i++) {
1840 v3_zero(&(itom[i].f));
1843 virial=(&(itom[i].virial));
1851 /* reset site energy */
1856 /* get energy, force and virial of every atom */
1858 /* first (and only) loop over atoms i */
1859 for(i=0;i<count;i++) {
1861 /* single particle potential/force */
1862 if(itom[i].attr&ATOM_ATTR_1BP)
1864 moldyn->func_i0(moldyn,&(itom[i]));
1866 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1869 /* 2 body pair potential/force */
1871 link_cell_neighbour_index(moldyn,
1872 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1873 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1874 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1879 #ifndef STATIC_LISTS
1880 /* check for later 3 body interaction */
1881 if(itom[i].attr&ATOM_ATTR_3BP)
1882 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
1885 /* first loop over atoms j */
1886 if(moldyn->func_j0) {
1893 while(neighbour_i[j][p]!=0) {
1895 jtom=&(atom[neighbour_i[j][p]]);
1898 this=&(neighbour_i[j]);
1901 if(this->start==NULL)
1905 jtom=this->current->data;
1908 if(jtom==&(itom[i]))
1914 if((jtom->attr&ATOM_ATTR_2BP)&
1915 (itom[i].attr&ATOM_ATTR_2BP)) {
1916 moldyn->func_j0(moldyn,
1922 /* 3 body potential/force */
1924 /* in j loop, 3bp run can be skipped */
1925 if(!(moldyn->run3bp))
1928 if(!(itom[i].attr&ATOM_ATTR_3BP))
1931 if(!(jtom->attr&ATOM_ATTR_3BP))
1934 if(moldyn->func_j0_k0==NULL)
1937 /* first loop over atoms k in first j loop */
1944 while(neighbour_i[j][q]!=0) {
1946 ktom=&(atom[neighbour_i[k][q]]);
1949 that=&(neighbour_i2[k]);
1952 if(that->start==NULL)
1956 ktom=that->current->data;
1959 if(!(ktom->attr&ATOM_ATTR_3BP))
1965 if(ktom==&(itom[i]))
1968 moldyn->func_j0_k0(moldyn,
1976 } while(list_next_f(that)!=\
1982 /* finish of first j loop after first k loop */
1983 if(moldyn->func_j0e)
1984 moldyn->func_j0e(moldyn,
1992 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1998 /* continued 3 body potential/force */
2000 if(!(itom[i].attr&ATOM_ATTR_3BP))
2003 /* second loop over atoms j */
2010 while(neighbour_i[j][p]!=0) {
2012 jtom=&(atom[neighbour_i[j][p]]);
2015 this=&(neighbour_i[j]);
2018 if(this->start==NULL)
2023 jtom=this->current->data;
2026 if(jtom==&(itom[i]))
2029 if(!(jtom->attr&ATOM_ATTR_3BP))
2036 moldyn->func_j1(moldyn,
2041 /* in j loop, 3bp run can be skipped */
2042 if(!(moldyn->run3bp))
2045 /* first loop over atoms k in second j loop */
2046 if(moldyn->func_j1_k0) {
2054 while(neighbour_i[j][q]!=0) {
2056 ktom=&(atom[neighbour_i[k][q]]);
2059 that=&(neighbour_i2[k]);
2062 if(that->start==NULL)
2066 ktom=that->current->data;
2069 if(!(ktom->attr&ATOM_ATTR_3BP))
2075 if(ktom==&(itom[i]))
2078 moldyn->func_j1_k0(moldyn,
2086 } while(list_next_f(that)!=\
2094 /* continued j loop after first k loop */
2095 if(moldyn->func_j1c)
2096 moldyn->func_j1c(moldyn,
2101 /* second loop over atoms k */
2102 if(moldyn->func_j1_k1) {
2110 while(neighbour_i[j][q]!=0) {
2112 ktom=&(atom[neighbour_i[k][q]]);
2115 that=&(neighbour_i2[k]);
2118 if(that->start==NULL)
2122 ktom=that->current->data;
2125 if(!(ktom->attr&ATOM_ATTR_3BP))
2131 if(ktom==&(itom[i]))
2134 moldyn->func_j1_k1(moldyn,
2143 } while(list_next_f(that)!=\
2151 /* finish of j loop after second k loop */
2152 if(moldyn->func_j1e) {
2153 moldyn->func_j1e(moldyn,
2160 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2175 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2176 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2178 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
2179 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
2180 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
2184 /* some postprocessing */
2185 for(i=0;i<count;i++) {
2186 /* calculate global virial */
2187 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
2188 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
2189 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
2190 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
2191 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
2192 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
2194 /* check forces regarding the given timestep */
2195 if(v3_norm(&(itom[i].f))>\
2196 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
2197 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",
2205 * virial calculation
2208 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2209 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2211 a->virial.xx+=f->x*d->x;
2212 a->virial.yy+=f->y*d->y;
2213 a->virial.zz+=f->z*d->z;
2214 a->virial.xy+=f->x*d->y;
2215 a->virial.xz+=f->x*d->z;
2216 a->virial.yz+=f->y*d->z;
2222 * periodic boundary checking
2225 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2226 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2237 if(moldyn->status&MOLDYN_STAT_PBX) {
2238 if(a->x>=x) a->x-=dim->x;
2239 else if(-a->x>x) a->x+=dim->x;
2241 if(moldyn->status&MOLDYN_STAT_PBY) {
2242 if(a->y>=y) a->y-=dim->y;
2243 else if(-a->y>y) a->y+=dim->y;
2245 if(moldyn->status&MOLDYN_STAT_PBZ) {
2246 if(a->z>=z) a->z-=dim->z;
2247 else if(-a->z>z) a->z+=dim->z;
2254 * debugging / critical check functions
2257 int moldyn_bc_check(t_moldyn *moldyn) {
2270 for(i=0;i<moldyn->count;i++) {
2271 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2272 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2273 i,atom[i].r.x,dim->x/2);
2274 printf("diagnostic:\n");
2275 printf("-----------\natom.r.x:\n");
2277 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2280 ((byte)&(1<<k))?1:0,
2283 printf("---------------\nx=dim.x/2:\n");
2285 memcpy(&byte,(u8 *)(&x)+j,1);
2288 ((byte)&(1<<k))?1:0,
2291 if(atom[i].r.x==x) printf("the same!\n");
2292 else printf("different!\n");
2294 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2295 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2296 i,atom[i].r.y,dim->y/2);
2297 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2298 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2299 i,atom[i].r.z,dim->z/2);
2309 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2316 fd=open(file,O_RDONLY);
2318 perror("[moldyn] load save file open");
2322 fsize=lseek(fd,0,SEEK_END);
2323 lseek(fd,0,SEEK_SET);
2325 size=sizeof(t_moldyn);
2328 cnt=read(fd,moldyn,size);
2330 perror("[moldyn] load save file read (moldyn)");
2336 size=moldyn->count*sizeof(t_atom);
2338 /* correcting possible atom data offset */
2340 if(fsize!=sizeof(t_moldyn)+size) {
2341 corr=fsize-sizeof(t_moldyn)-size;
2342 printf("[moldyn] WARNING: lsf (illegal file size)\n");
2343 printf(" moifying offset:\n");
2344 printf(" - current pos: %d\n",sizeof(t_moldyn));
2345 printf(" - atom size: %d\n",size);
2346 printf(" - file size: %d\n",fsize);
2347 printf(" => correction: %d\n",corr);
2348 lseek(fd,corr,SEEK_CUR);
2351 moldyn->atom=(t_atom *)malloc(size);
2352 if(moldyn->atom==NULL) {
2353 perror("[moldyn] load save file malloc (atoms)");
2358 cnt=read(fd,moldyn->atom,size);
2360 perror("[moldyn] load save file read (atoms)");
2371 int moldyn_free_save_file(t_moldyn *moldyn) {
2378 int moldyn_load(t_moldyn *moldyn) {
2386 * function to find/callback all combinations of 2 body bonds
2389 int process_2b_bonds(t_moldyn *moldyn,void *data,
2390 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2391 void *data,u8 bc)) {
2398 t_list neighbour[27];
2408 for(i=0;i<moldyn->count;i++) {
2409 /* neighbour indexing */
2410 link_cell_neighbour_index(moldyn,
2411 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2412 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2413 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2418 bc=(j<lc->dnlc)?0:1;
2423 while(neighbour[j][p]!=0) {
2425 jtom=&(moldyn->atom[neighbour[j][p]]);
2428 this=&(neighbour[j]);
2431 if(this->start==NULL)
2436 jtom=this->current->data;
2440 process(moldyn,&(itom[i]),jtom,data,bc);
2445 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2455 * post processing functions
2458 int get_line(int fd,char *line,int max) {
2465 if(count==max) return count;
2466 ret=read(fd,line+count,1);
2467 if(ret<=0) return ret;
2468 if(line[count]=='\n') {
2469 memset(line+count,0,max-count-1);
2477 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2483 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
2499 for(i=0;i<moldyn->count;i++) {
2501 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
2502 check_per_bound(moldyn,&dist);
2503 d2=v3_absolute_square(&dist);
2517 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
2518 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
2519 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
2524 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
2529 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
2530 t_atom *jtom,void *data,u8 bc) {
2537 /* only count pairs once,
2538 * skip same atoms */
2539 if(itom->tag>=jtom->tag)
2543 * pair correlation calc
2550 v3_sub(&dist,&(jtom->r),&(itom->r));
2551 if(bc) check_per_bound(moldyn,&dist);
2552 d=v3_absolute_square(&dist);
2554 /* ignore if greater cutoff */
2555 if(d>moldyn->cutoff_square)
2558 /* fill the slots */
2562 /* should never happen but it does 8) -
2563 * related to -ffloat-store problem! */
2565 printf("[moldyn] WARNING: pcc (%d/%d)",
2571 if(itom->brand!=jtom->brand) {
2576 /* type a - type a bonds */
2578 pcc->stat[s+pcc->o1]+=1;
2580 /* type b - type b bonds */
2581 pcc->stat[s+pcc->o2]+=1;
2587 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2594 pcc.o1=moldyn->cutoff/dr;
2597 if(pcc.o1*dr<=moldyn->cutoff)
2598 printf("[moldyn] WARNING: pcc (low #slots)\n");
2600 printf("[moldyn] pair correlation calc info:\n");
2601 printf(" time: %f\n",moldyn->time);
2602 printf(" count: %d\n",moldyn->count);
2603 printf(" cutoff: %f\n",moldyn->cutoff);
2604 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2607 pcc.stat=(double *)ptr;
2610 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
2611 if(pcc.stat==NULL) {
2612 perror("[moldyn] pair correlation malloc");
2617 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
2620 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
2623 for(i=1;i<pcc.o1;i++) {
2624 // normalization: 4 pi r^2 dr
2625 // here: not double counting pairs -> 2 pi r r dr
2626 // ... and actually it's a constant times r^2
2629 pcc.stat[pcc.o1+i]/=norm;
2630 pcc.stat[pcc.o2+i]/=norm;
2635 /* todo: store/print pair correlation function */
2642 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2649 if(itom->tag>=jtom->tag)
2653 v3_sub(&dist,&(jtom->r),&(itom->r));
2654 if(bc) check_per_bound(moldyn,&dist);
2655 d=v3_absolute_square(&dist);
2657 /* ignore if greater or equal cutoff */
2658 if(d>moldyn->cutoff_square)
2661 /* check for potential bond */
2662 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2665 /* now count this bonding ... */
2668 /* increase total bond counter
2669 * ... double counting!
2674 ba->acnt[jtom->tag]+=1;
2676 ba->bcnt[jtom->tag]+=1;
2679 ba->acnt[itom->tag]+=1;
2681 ba->bcnt[itom->tag]+=1;
2686 int bond_analyze(t_moldyn *moldyn,double *quality) {
2688 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
2696 ba.acnt=malloc(moldyn->count*sizeof(int));
2698 perror("[moldyn] bond analyze malloc (a)");
2701 memset(ba.acnt,0,moldyn->count*sizeof(int));
2703 ba.bcnt=malloc(moldyn->count*sizeof(int));
2705 perror("[moldyn] bond analyze malloc (b)");
2708 memset(ba.bcnt,0,moldyn->count*sizeof(int));
2717 process_2b_bonds(moldyn,&ba,bond_analyze_process);
2719 for(i=0;i<moldyn->count;i++) {
2720 if(atom[i].brand==0) {
2721 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
2725 if((ba.acnt[i]==4)&(ba.bcnt[i]==0)) {
2733 printf("[moldyn] bond analyze: c_cnt=%d | set=%d\n",ccnt,cset);
2734 printf("[moldyn] bond analyze: q_cnt=%d | tot=%d\n",qcnt,ba.tcnt);
2737 quality[0]=1.0*ccnt/cset;
2738 quality[1]=1.0*qcnt/ba.tcnt;
2741 printf("[moldyn] bond analyze: c_bnd_q=%f\n",1.0*qcnt/ba.tcnt);
2742 printf("[moldyn] bond analyze: tot_q=%f\n",1.0*qcnt/ba.tcnt);
2749 * visualization code
2752 int visual_init(t_moldyn *moldyn,char *filebase) {
2754 strncpy(moldyn->vis.fb,filebase,128);
2759 int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2766 if(itom->tag>=jtom->tag)
2769 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2772 if((itom->attr&ATOM_ATTR_VB)|(jtom->attr&ATOM_ATTR_VB))
2773 dprintf(vb->fd,"# [B] %f %f %f %f %f %f\n",
2774 itom->r.x,itom->r.y,itom->r.z,
2775 jtom->r.x,jtom->r.y,jtom->r.z);
2780 int visual_atoms(t_moldyn *moldyn) {
2798 sprintf(file,"%s/atomic_conf_%07.f.xyz",v->fb,moldyn->time);
2799 vb.fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
2801 perror("open visual save file fd");
2805 /* write the actual data file */
2808 dprintf(vb.fd,"# [P] %d %07.f <%f,%f,%f>\n",
2809 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
2811 // atomic configuration
2812 for(i=0;i<moldyn->count;i++)
2813 // atom type, positions, color and kinetic energy
2814 dprintf(vb.fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
2818 pse_col[atom[i].element],
2821 // bonds between atoms
2822 process_2b_bonds(moldyn,&vb,visual_bonds_process);
2826 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2827 -dim.x/2,-dim.y/2,-dim.z/2,
2828 dim.x/2,-dim.y/2,-dim.z/2);
2829 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2830 -dim.x/2,-dim.y/2,-dim.z/2,
2831 -dim.x/2,dim.y/2,-dim.z/2);
2832 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2833 dim.x/2,dim.y/2,-dim.z/2,
2834 dim.x/2,-dim.y/2,-dim.z/2);
2835 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2836 -dim.x/2,dim.y/2,-dim.z/2,
2837 dim.x/2,dim.y/2,-dim.z/2);
2839 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2840 -dim.x/2,-dim.y/2,dim.z/2,
2841 dim.x/2,-dim.y/2,dim.z/2);
2842 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2843 -dim.x/2,-dim.y/2,dim.z/2,
2844 -dim.x/2,dim.y/2,dim.z/2);
2845 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2846 dim.x/2,dim.y/2,dim.z/2,
2847 dim.x/2,-dim.y/2,dim.z/2);
2848 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2849 -dim.x/2,dim.y/2,dim.z/2,
2850 dim.x/2,dim.y/2,dim.z/2);
2852 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2853 -dim.x/2,-dim.y/2,dim.z/2,
2854 -dim.x/2,-dim.y/2,-dim.z/2);
2855 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2856 -dim.x/2,dim.y/2,dim.z/2,
2857 -dim.x/2,dim.y/2,-dim.z/2);
2858 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2859 dim.x/2,-dim.y/2,dim.z/2,
2860 dim.x/2,-dim.y/2,-dim.z/2);
2861 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2862 dim.x/2,dim.y/2,dim.z/2,
2863 dim.x/2,dim.y/2,-dim.z/2);