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;
226 case MOLDYN_POTENTIAL_AM:
227 moldyn->func3b_j1=albe_mult_3bp_j1;
228 moldyn->func3b_k1=albe_mult_3bp_k1;
229 moldyn->func3b_j2=albe_mult_3bp_j2;
230 moldyn->func3b_k2=albe_mult_3bp_k2;
231 moldyn->check_2b_bond=albe_mult_check_2b_bond;
234 case MOLDYN_POTENTIAL_AM:
235 moldyn->func1b=albe_mult_i0;
236 moldyn->func2b=albe_mult_i0_j0;
237 moldyn->func3b_0=albe_mult_i0_j0_k0;
238 moldyn->func3b_1=albe_mult_i0_j1;
239 moldyn->func3b_j1=albe_mult_i0_j2;
240 moldyn->func3b_k2=albe_mult_i0_j2_k0;
241 moldyn->func3b_j2=albe_mult_i0_j3;
242 moldyn->check_2b_bond=albe_mult_check_2b_bond;
244 case MOLDYN_POTENTIAL_HO:
245 moldyn->func2b=harmonic_oscillator;
246 moldyn->check_2b_bond=harmonic_oscillator_check_2b_bond;
248 case MOLDYN_POTENTIAL_LJ:
249 moldyn->func2b=lennard_jones;
250 moldyn->check_2b_bond=lennard_jones_check_2b_bond;
253 printf("[moldyn] set potential: unknown type %02x\n",
261 int set_avg_skip(t_moldyn *moldyn,int skip) {
263 printf("[moldyn] skip %d steps before starting average calc\n",skip);
264 moldyn->avg_skip=skip;
269 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
271 strncpy(moldyn->vlsdir,dir,127);
276 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
278 strncpy(moldyn->rauthor,author,63);
279 strncpy(moldyn->rtitle,title,63);
284 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
289 printf("[moldyn] set log: ");
292 case LOG_TOTAL_ENERGY:
293 moldyn->ewrite=timer;
294 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
295 moldyn->efd=open(filename,
296 O_WRONLY|O_CREAT|O_EXCL,
299 perror("[moldyn] energy log fd open");
302 dprintf(moldyn->efd,"# total energy log file\n");
303 printf("total energy (%d)\n",timer);
305 case LOG_TOTAL_MOMENTUM:
306 moldyn->mwrite=timer;
307 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
308 moldyn->mfd=open(filename,
309 O_WRONLY|O_CREAT|O_EXCL,
312 perror("[moldyn] momentum log fd open");
315 dprintf(moldyn->efd,"# total momentum log file\n");
316 printf("total momentum (%d)\n",timer);
319 moldyn->pwrite=timer;
320 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
321 moldyn->pfd=open(filename,
322 O_WRONLY|O_CREAT|O_EXCL,
325 perror("[moldyn] pressure log file\n");
328 dprintf(moldyn->pfd,"# pressure log file\n");
329 printf("pressure (%d)\n",timer);
331 case LOG_TEMPERATURE:
332 moldyn->twrite=timer;
333 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
334 moldyn->tfd=open(filename,
335 O_WRONLY|O_CREAT|O_EXCL,
338 perror("[moldyn] temperature log file\n");
341 dprintf(moldyn->tfd,"# temperature log file\n");
342 printf("temperature (%d)\n",timer);
345 moldyn->vwrite=timer;
346 snprintf(filename,127,"%s/volume",moldyn->vlsdir);
347 moldyn->vfd=open(filename,
348 O_WRONLY|O_CREAT|O_EXCL,
351 perror("[moldyn] volume log file\n");
354 dprintf(moldyn->vfd,"# volume log file\n");
355 printf("volume (%d)\n",timer);
358 moldyn->swrite=timer;
359 printf("save file (%d)\n",timer);
362 moldyn->awrite=timer;
363 ret=visual_init(moldyn,moldyn->vlsdir);
365 printf("[moldyn] visual init failure\n");
368 printf("visual file (%d)\n",timer);
371 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
372 moldyn->rfd=open(filename,
373 O_WRONLY|O_CREAT|O_EXCL,
376 perror("[moldyn] report fd open");
379 printf("report -> ");
381 snprintf(filename,127,"%s/e_plot.scr",
383 moldyn->epfd=open(filename,
384 O_WRONLY|O_CREAT|O_EXCL,
387 perror("[moldyn] energy plot fd open");
390 dprintf(moldyn->epfd,e_plot_script);
395 snprintf(filename,127,"%s/pressure_plot.scr",
397 moldyn->ppfd=open(filename,
398 O_WRONLY|O_CREAT|O_EXCL,
401 perror("[moldyn] p plot fd open");
404 dprintf(moldyn->ppfd,pressure_plot_script);
409 snprintf(filename,127,"%s/temperature_plot.scr",
411 moldyn->tpfd=open(filename,
412 O_WRONLY|O_CREAT|O_EXCL,
415 perror("[moldyn] t plot fd open");
418 dprintf(moldyn->tpfd,temperature_plot_script);
420 printf("temperature ");
422 dprintf(moldyn->rfd,report_start,
423 moldyn->rauthor,moldyn->rtitle);
427 printf("unknown log type: %02x\n",type);
434 int moldyn_log_shutdown(t_moldyn *moldyn) {
438 printf("[moldyn] log shutdown\n");
442 dprintf(moldyn->rfd,report_energy);
443 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
448 if(moldyn->mfd) close(moldyn->mfd);
452 dprintf(moldyn->rfd,report_pressure);
453 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
460 dprintf(moldyn->rfd,report_temperature);
461 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
466 dprintf(moldyn->rfd,report_end);
468 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
471 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
474 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
483 * creating lattice functions
486 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
487 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
499 /* how many atoms do we expect */
500 if(type==CUBIC) new*=1;
501 if(type==FCC) new*=4;
502 if(type==DIAMOND) new*=8;
504 /* allocate space for atoms */
505 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
507 perror("[moldyn] realloc (create lattice)");
511 atom=&(moldyn->atom[count]);
513 /* no atoms on the boundaries (only reason: it looks better!) */
527 set_nn_dist(moldyn,lc);
528 ret=cubic_init(a,b,c,lc,atom,&orig);
529 strcpy(name,"cubic");
533 v3_scale(&orig,&orig,0.5);
534 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
535 ret=fcc_init(a,b,c,lc,atom,&orig);
540 v3_scale(&orig,&orig,0.25);
541 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
542 ret=diamond_init(a,b,c,lc,atom,&orig);
543 strcpy(name,"diamond");
546 printf("unknown lattice type (%02x)\n",type);
552 printf("[moldyn] creating lattice failed\n");
553 printf(" amount of atoms\n");
554 printf(" - expected: %d\n",new);
555 printf(" - created: %d\n",ret);
560 printf("[moldyn] created %s lattice with %d atoms\n",name,new);
562 for(ret=0;ret<new;ret++) {
563 atom[ret].element=element;
566 atom[ret].brand=brand;
567 atom[ret].tag=count+ret;
568 check_per_bound(moldyn,&(atom[ret].r));
569 atom[ret].r_0=atom[ret].r;
572 /* update total system mass */
573 total_mass_calc(moldyn);
578 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
579 t_3dvec *r,t_3dvec *v) {
586 count=(moldyn->count)++; // asshole style!
588 ptr=realloc(atom,(count+1)*sizeof(t_atom));
590 perror("[moldyn] realloc (add atom)");
597 /* initialize new atom */
598 memset(&(atom[count]),0,sizeof(t_atom));
601 atom[count].element=element;
602 atom[count].mass=mass;
603 atom[count].brand=brand;
604 atom[count].tag=count;
605 atom[count].attr=attr;
606 check_per_bound(moldyn,&(atom[count].r));
607 atom[count].r_0=atom[count].r;
609 /* update total system mass */
610 total_mass_calc(moldyn);
615 int del_atom(t_moldyn *moldyn,int tag) {
622 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
624 perror("[moldyn]malloc (del atom)");
628 for(cnt=0;cnt<tag;cnt++)
631 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
633 new[cnt-1].tag=cnt-1;
645 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
664 v3_copy(&(atom[count].r),&r);
673 for(i=0;i<count;i++) {
674 atom[i].r.x-=(a*lc)/2.0;
675 atom[i].r.y-=(b*lc)/2.0;
676 atom[i].r.z-=(c*lc)/2.0;
682 /* fcc lattice init */
683 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
696 /* construct the basis */
697 memset(basis,0,3*sizeof(t_3dvec));
705 /* fill up the room */
713 v3_copy(&(atom[count].r),&r);
716 /* the three face centered atoms */
718 v3_add(&n,&r,&basis[l]);
719 v3_copy(&(atom[count].r),&n);
728 /* coordinate transformation */
729 for(i=0;i<count;i++) {
730 atom[i].r.x-=(a*lc)/2.0;
731 atom[i].r.y-=(b*lc)/2.0;
732 atom[i].r.z-=(c*lc)/2.0;
738 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
743 count=fcc_init(a,b,c,lc,atom,origin);
749 if(origin) v3_add(&o,&o,origin);
751 count+=fcc_init(a,b,c,lc,&atom[count],&o);
756 int destroy_atoms(t_moldyn *moldyn) {
758 if(moldyn->atom) free(moldyn->atom);
763 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
766 * - gaussian distribution of velocities
767 * - zero total momentum
768 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
773 t_3dvec p_total,delta;
778 random=&(moldyn->random);
780 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
782 /* gaussian distribution of velocities */
784 for(i=0;i<moldyn->count;i++) {
785 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
787 v=sigma*rand_get_gauss(random);
789 p_total.x+=atom[i].mass*v;
791 v=sigma*rand_get_gauss(random);
793 p_total.y+=atom[i].mass*v;
795 v=sigma*rand_get_gauss(random);
797 p_total.z+=atom[i].mass*v;
800 /* zero total momentum */
801 v3_scale(&p_total,&p_total,1.0/moldyn->count);
802 for(i=0;i<moldyn->count;i++) {
803 v3_scale(&delta,&p_total,1.0/atom[i].mass);
804 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
807 /* velocity scaling */
808 scale_velocity(moldyn,equi_init);
813 double total_mass_calc(t_moldyn *moldyn) {
819 for(i=0;i<moldyn->count;i++)
820 moldyn->mass+=moldyn->atom[i].mass;
825 double temperature_calc(t_moldyn *moldyn) {
827 /* assume up to date kinetic energy, which is 3/2 N k_B T */
829 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
834 double get_temperature(t_moldyn *moldyn) {
839 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
849 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
852 /* get kinetic energy / temperature & count involved atoms */
855 for(i=0;i<moldyn->count;i++) {
856 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
857 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
862 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
863 else return 0; /* no atoms involved in scaling! */
865 /* (temporary) hack for e,t = 0 */
868 if(moldyn->t_ref!=0.0) {
869 thermal_init(moldyn,equi_init);
873 return 0; /* no scaling needed */
877 /* get scaling factor */
878 scale=moldyn->t_ref/moldyn->t;
882 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
883 scale=1.0+(scale-1.0)/moldyn->t_tc;
886 /* velocity scaling */
887 for(i=0;i<moldyn->count;i++) {
888 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
889 v3_scale(&(atom[i].v),&(atom[i].v),scale);
895 double ideal_gas_law_pressure(t_moldyn *moldyn) {
899 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
904 double virial_sum(t_moldyn *moldyn) {
909 /* virial (sum over atom virials) */
917 for(i=0;i<moldyn->count;i++) {
918 virial=&(moldyn->atom[i].virial);
919 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
920 moldyn->vir.xx+=virial->xx;
921 moldyn->vir.yy+=virial->yy;
922 moldyn->vir.zz+=virial->zz;
923 moldyn->vir.xy+=virial->xy;
924 moldyn->vir.xz+=virial->xz;
925 moldyn->vir.yz+=virial->yz;
928 /* global virial (absolute coordinates) */
929 virial=&(moldyn->gvir);
930 moldyn->gv=virial->xx+virial->yy+virial->zz;
932 return moldyn->virial;
935 double pressure_calc(t_moldyn *moldyn) {
939 * with W = 1/3 sum_i f_i r_i (- skipped!)
940 * virial = sum_i f_i r_i
942 * => P = (2 Ekin + virial) / (3V)
945 /* assume up to date virial & up to date kinetic energy */
947 /* pressure (atom virials) */
948 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
949 moldyn->p/=(3.0*moldyn->volume);
951 /* pressure (absolute coordinates) */
952 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
953 moldyn->gp/=(3.0*moldyn->volume);
958 int average_reset(t_moldyn *moldyn) {
960 printf("[moldyn] average reset\n");
962 /* update skip value */
963 moldyn->avg_skip=moldyn->total_steps;
969 /* potential energy */
977 moldyn->virial_sum=0.0;
988 int average_and_fluctuation_calc(t_moldyn *moldyn) {
992 if(moldyn->total_steps<moldyn->avg_skip)
995 denom=moldyn->total_steps+1-moldyn->avg_skip;
997 /* assume up to date energies, temperature, pressure etc */
1000 moldyn->k_sum+=moldyn->ekin;
1001 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
1002 moldyn->k_avg=moldyn->k_sum/denom;
1003 moldyn->k2_avg=moldyn->k2_sum/denom;
1004 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
1006 /* potential energy */
1007 moldyn->v_sum+=moldyn->energy;
1008 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
1009 moldyn->v_avg=moldyn->v_sum/denom;
1010 moldyn->v2_avg=moldyn->v2_sum/denom;
1011 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1014 moldyn->t_sum+=moldyn->t;
1015 moldyn->t_avg=moldyn->t_sum/denom;
1018 moldyn->virial_sum+=moldyn->virial;
1019 moldyn->virial_avg=moldyn->virial_sum/denom;
1020 moldyn->gv_sum+=moldyn->gv;
1021 moldyn->gv_avg=moldyn->gv_sum/denom;
1024 moldyn->p_sum+=moldyn->p;
1025 moldyn->p_avg=moldyn->p_sum/denom;
1026 moldyn->gp_sum+=moldyn->gp;
1027 moldyn->gp_avg=moldyn->gp_sum/denom;
1028 moldyn->tp_sum+=moldyn->tp;
1029 moldyn->tp_avg=moldyn->tp_sum/denom;
1034 int get_heat_capacity(t_moldyn *moldyn) {
1038 /* averages needed for heat capacity calc */
1039 if(moldyn->total_steps<moldyn->avg_skip)
1042 /* (temperature average)^2 */
1043 temp2=moldyn->t_avg*moldyn->t_avg;
1044 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1047 /* ideal gas contribution */
1048 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1049 printf(" ideal gas contribution: %f\n",
1050 ighc/moldyn->mass*KILOGRAM/JOULE);
1052 /* specific heat for nvt ensemble */
1053 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1054 moldyn->c_v_nvt/=moldyn->mass;
1056 /* specific heat for nve ensemble */
1057 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1058 moldyn->c_v_nve/=moldyn->mass;
1060 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1061 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1062 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)));
1067 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1083 /* store atomic configuration + dimension */
1084 store=malloc(moldyn->count*sizeof(t_atom));
1086 printf("[moldyn] allocating store mem failed\n");
1089 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1094 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1095 su=pow(2.0-h,ONE_THIRD)-1.0;
1096 dv=(1.0-h)*moldyn->volume;
1098 /* scale up dimension and atom positions */
1099 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1100 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1101 link_cell_shutdown(moldyn);
1102 link_cell_init(moldyn,QUIET);
1103 potential_force_calc(moldyn);
1106 /* restore atomic configuration + dim */
1107 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1110 /* scale down dimension and atom positions */
1111 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1112 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1113 link_cell_shutdown(moldyn);
1114 link_cell_init(moldyn,QUIET);
1115 potential_force_calc(moldyn);
1118 /* calculate pressure */
1119 moldyn->tp=-(y1-y0)/(2.0*dv);
1121 /* restore atomic configuration */
1122 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1124 link_cell_shutdown(moldyn);
1125 link_cell_init(moldyn,QUIET);
1126 //potential_force_calc(moldyn);
1128 /* free store buffer */
1135 double get_pressure(t_moldyn *moldyn) {
1141 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1153 if(x) dim->x*=scale;
1154 if(y) dim->y*=scale;
1155 if(z) dim->z*=scale;
1160 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1171 for(i=0;i<moldyn->count;i++) {
1172 r=&(moldyn->atom[i].r);
1181 int scale_volume(t_moldyn *moldyn) {
1187 vdim=&(moldyn->vis.dim);
1191 /* scaling factor */
1192 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1193 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc;
1194 scale=pow(scale,ONE_THIRD);
1197 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1200 /* scale the atoms and dimensions */
1201 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1202 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1204 /* visualize dimensions */
1211 /* recalculate scaled volume */
1212 moldyn->volume=dim->x*dim->y*dim->z;
1214 /* adjust/reinit linkcell */
1215 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1216 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1217 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1218 link_cell_shutdown(moldyn);
1219 link_cell_init(moldyn,QUIET);
1230 double e_kin_calc(t_moldyn *moldyn) {
1238 for(i=0;i<moldyn->count;i++) {
1239 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1240 moldyn->ekin+=atom[i].ekin;
1243 return moldyn->ekin;
1246 double get_total_energy(t_moldyn *moldyn) {
1248 return(moldyn->ekin+moldyn->energy);
1251 t_3dvec get_total_p(t_moldyn *moldyn) {
1260 for(i=0;i<moldyn->count;i++) {
1261 v3_scale(&p,&(atom[i].v),atom[i].mass);
1262 v3_add(&p_total,&p_total,&p);
1268 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1272 /* nn_dist is the nearest neighbour distance */
1274 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1283 /* linked list / cell method */
1285 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1292 /* partitioning the md cell */
1293 lc->nx=moldyn->dim.x/moldyn->cutoff;
1294 lc->x=moldyn->dim.x/lc->nx;
1295 lc->ny=moldyn->dim.y/moldyn->cutoff;
1296 lc->y=moldyn->dim.y/lc->ny;
1297 lc->nz=moldyn->dim.z/moldyn->cutoff;
1298 lc->z=moldyn->dim.z/lc->nz;
1299 lc->cells=lc->nx*lc->ny*lc->nz;
1302 lc->subcell=malloc(lc->cells*sizeof(int*));
1304 lc->subcell=malloc(lc->cells*sizeof(t_list));
1307 if(lc->subcell==NULL) {
1308 perror("[moldyn] cell init (malloc)");
1313 printf("[moldyn] FATAL: less then 27 subcells!\n");
1317 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1320 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1323 printf(" x: %d x %f A\n",lc->nx,lc->x);
1324 printf(" y: %d x %f A\n",lc->ny,lc->y);
1325 printf(" z: %d x %f A\n",lc->nz,lc->z);
1330 for(i=0;i<lc->cells;i++) {
1331 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1332 if(lc->subcell[i]==NULL) {
1333 perror("[moldyn] list init (malloc)");
1338 printf(" ---> %d malloc %p (%p)\n",
1339 i,lc->subcell[0],lc->subcell);
1343 for(i=0;i<lc->cells;i++)
1344 list_init_f(&(lc->subcell[i]));
1347 /* update the list */
1348 link_cell_update(moldyn);
1353 int link_cell_update(t_moldyn *moldyn) {
1369 for(i=0;i<lc->cells;i++)
1371 memset(lc->subcell[i],0,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1373 list_destroy_f(&(lc->subcell[i]));
1376 for(count=0;count<moldyn->count;count++) {
1377 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1378 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1379 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1383 while(lc->subcell[i+j*nx+k*nx*ny][p]!=0)
1386 if(p>=MAX_ATOMS_PER_LIST) {
1387 printf("[moldyn] FATAL: amount of atoms too high!\n");
1391 lc->subcell[i+j*nx+k*nx*ny][p]=count;
1393 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1397 printf(" ---> %d %d malloc %p (%p)\n",
1398 i,count,lc->subcell[i].current,lc->subcell);
1406 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1430 if(i>=nx||j>=ny||k>=nz)
1431 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1434 cell[0]=lc->subcell[i+j*nx+k*a];
1435 for(ci=-1;ci<=1;ci++) {
1438 if((x<0)||(x>=nx)) {
1442 for(cj=-1;cj<=1;cj++) {
1445 if((y<0)||(y>=ny)) {
1449 for(ck=-1;ck<=1;ck++) {
1452 if((z<0)||(z>=nz)) {
1456 if(!(ci|cj|ck)) continue;
1458 cell[--count2]=lc->subcell[x+y*nx+z*a];
1461 cell[count1++]=lc->subcell[x+y*nx+z*a];
1472 int link_cell_shutdown(t_moldyn *moldyn) {
1479 for(i=0;i<lc->cells;i++) {
1481 free(lc->subcell[i]);
1483 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1484 list_destroy_f(&(lc->subcell[i]));
1493 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1497 t_moldyn_schedule *schedule;
1499 schedule=&(moldyn->schedule);
1500 count=++(schedule->total_sched);
1502 ptr=realloc(schedule->runs,count*sizeof(int));
1504 perror("[moldyn] realloc (runs)");
1508 schedule->runs[count-1]=runs;
1510 ptr=realloc(schedule->tau,count*sizeof(double));
1512 perror("[moldyn] realloc (tau)");
1516 schedule->tau[count-1]=tau;
1518 printf("[moldyn] schedule added:\n");
1519 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1525 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1527 moldyn->schedule.hook=hook;
1528 moldyn->schedule.hook_params=hook_params;
1535 * 'integration of newtons equation' - algorithms
1539 /* start the integration */
1541 int moldyn_integrate(t_moldyn *moldyn) {
1544 unsigned int e,m,s,v,p,t,a;
1546 t_moldyn_schedule *sched;
1551 double energy_scale;
1552 struct timeval t1,t2;
1555 sched=&(moldyn->schedule);
1558 /* initialize linked cell method */
1559 link_cell_init(moldyn,VERBOSE);
1561 /* logging & visualization */
1570 /* sqaure of some variables */
1571 moldyn->tau_square=moldyn->tau*moldyn->tau;
1573 /* get current time */
1574 gettimeofday(&t1,NULL);
1576 /* calculate initial forces */
1577 potential_force_calc(moldyn);
1582 /* some stupid checks before we actually start calculating bullshit */
1583 if(moldyn->cutoff>0.5*moldyn->dim.x)
1584 printf("[moldyn] WARNING: cutoff > 0.5 x dim.x\n");
1585 if(moldyn->cutoff>0.5*moldyn->dim.y)
1586 printf("[moldyn] WARNING: cutoff > 0.5 x dim.y\n");
1587 if(moldyn->cutoff>0.5*moldyn->dim.z)
1588 printf("[moldyn] WARNING: cutoff > 0.5 x dim.z\n");
1589 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1590 if(ds>0.05*moldyn->nnd)
1591 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1593 /* zero absolute time */
1594 // should have right values!
1596 //moldyn->total_steps=0;
1598 /* debugging, ignore */
1601 /* tell the world */
1602 printf("[moldyn] integration start, go get a coffee ...\n");
1604 /* executing the schedule */
1606 while(sched->count<sched->total_sched) {
1608 /* setting amount of runs and finite time step size */
1609 moldyn->tau=sched->tau[sched->count];
1610 moldyn->tau_square=moldyn->tau*moldyn->tau;
1611 moldyn->time_steps=sched->runs[sched->count];
1613 /* energy scaling factor (might change!) */
1614 energy_scale=moldyn->count*EV;
1616 /* integration according to schedule */
1618 for(i=0;i<moldyn->time_steps;i++) {
1620 /* integration step */
1621 moldyn->integrate(moldyn);
1623 /* calculate kinetic energy, temperature and pressure */
1625 temperature_calc(moldyn);
1627 pressure_calc(moldyn);
1629 thermodynamic_pressure_calc(moldyn);
1630 printf("\n\nDEBUG: numeric pressure calc: %f\n\n",
1634 /* calculate fluctuations + averages */
1635 average_and_fluctuation_calc(moldyn);
1638 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1639 scale_velocity(moldyn,FALSE);
1640 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1641 scale_volume(moldyn);
1643 /* check for log & visualization */
1645 if(!(moldyn->total_steps%e))
1646 dprintf(moldyn->efd,
1648 moldyn->time,moldyn->ekin/energy_scale,
1649 moldyn->energy/energy_scale,
1650 get_total_energy(moldyn)/energy_scale);
1653 if(!(moldyn->total_steps%m)) {
1654 momentum=get_total_p(moldyn);
1655 dprintf(moldyn->mfd,
1656 "%f %f %f %f %f\n",moldyn->time,
1657 momentum.x,momentum.y,momentum.z,
1658 v3_norm(&momentum));
1662 if(!(moldyn->total_steps%p)) {
1663 dprintf(moldyn->pfd,
1664 "%f %f %f %f %f %f %f\n",moldyn->time,
1665 moldyn->p/BAR,moldyn->p_avg/BAR,
1666 moldyn->gp/BAR,moldyn->gp_avg/BAR,
1667 moldyn->tp/BAR,moldyn->tp_avg/BAR);
1671 if(!(moldyn->total_steps%t)) {
1672 dprintf(moldyn->tfd,
1674 moldyn->time,moldyn->t,moldyn->t_avg);
1678 if(!(moldyn->total_steps%v)) {
1679 dprintf(moldyn->vfd,
1680 "%f %f\n",moldyn->time,moldyn->volume);
1684 if(!(moldyn->total_steps%s)) {
1685 snprintf(dir,128,"%s/s-%07.f.save",
1686 moldyn->vlsdir,moldyn->time);
1687 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
1689 if(fd<0) perror("[moldyn] save fd open");
1691 write(fd,moldyn,sizeof(t_moldyn));
1692 write(fd,moldyn->atom,
1693 moldyn->count*sizeof(t_atom));
1699 if(!(moldyn->total_steps%a)) {
1700 visual_atoms(moldyn);
1704 /* display progress */
1705 //if(!(moldyn->total_steps%10)) {
1706 /* get current time */
1707 gettimeofday(&t2,NULL);
1709 printf("\rsched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)",
1710 sched->count,i,moldyn->total_steps,
1711 moldyn->t,moldyn->t_avg,
1712 moldyn->p/BAR,moldyn->p_avg/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->func1b(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 /* first loop over atoms j */
1880 if(moldyn->func2b) {
1887 while(neighbour_i[j][p]!=0) {
1889 jtom=&(atom[neighbour_i[j][p]]);
1892 if(jtom==&(itom[i]))
1895 if((jtom->attr&ATOM_ATTR_2BP)&
1896 (itom[i].attr&ATOM_ATTR_2BP)) {
1897 moldyn->func2b(moldyn,
1904 this=&(neighbour_i[j]);
1907 if(this->start==NULL)
1911 jtom=this->current->data;
1913 if(jtom==&(itom[i]))
1919 if((jtom->attr&ATOM_ATTR_2BP)&
1920 (itom[i].attr&ATOM_ATTR_2BP)) {
1921 moldyn->func2b(moldyn,
1927 // REWRITE ONCE WORKING!!!
1928 /* 3 body potential/force */
1930 /* in j loop, 3bp run can be skipped */
1931 if(!(moldyn->run3bp))
1934 if(!(itom[i].attr&ATOM_ATTR_3BP))
1937 if(!(jtom->attr&ATOM_ATTR_3BP))
1940 if(moldyn->func3b_0==NULL)
1949 while(neighbour_i[j][q]!=0) {
1951 ktom=&(atom[neighbour_i[k][q]]);
1954 that=&(neighbour_i2[k]);
1957 if(that->start==NULL)
1961 ktom=that->current->data;
1964 if(!(ktom->attr&ATOM_ATTR_3BP))
1970 if(ktom==&(itom[i]))
1973 moldyn->func3b_0(moldyn,
1981 } while(list_next_f(that)!=\
1988 if(moldyn->func3b_1)
1989 moldyn->func3b_1(moldyn,
1994 // UNTIL HERE + change function names!
1998 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2004 /* 3 body potential/force */
2006 if(!(itom[i].attr&ATOM_ATTR_3BP))
2009 /* copy the neighbour lists */
2011 /* no copy needed for static lists */
2013 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
2016 /* second loop over atoms j */
2023 while(neighbour_i[j][p]!=0) {
2025 jtom=&(atom[neighbour_i[j][p]]);
2028 this=&(neighbour_i[j]);
2031 if(this->start==NULL)
2036 jtom=this->current->data;
2039 if(jtom==&(itom[i]))
2042 if(!(jtom->attr&ATOM_ATTR_3BP))
2048 if(moldyn->func3b_j1)
2049 moldyn->func3b_j1(moldyn,
2054 /* in first j loop, 3bp run can be skipped */
2055 if(!(moldyn->run3bp))
2058 /* first loop over atoms k */
2059 if(moldyn->func3b_k1) {
2067 while(neighbour_i[j][q]!=0) {
2069 ktom=&(atom[neighbour_i[k][q]]);
2072 that=&(neighbour_i2[k]);
2075 if(that->start==NULL)
2079 ktom=that->current->data;
2082 if(!(ktom->attr&ATOM_ATTR_3BP))
2088 if(ktom==&(itom[i]))
2091 moldyn->func3b_k1(moldyn,
2099 } while(list_next_f(that)!=\
2107 if(moldyn->func3b_j2)
2108 moldyn->func3b_j2(moldyn,
2113 /* second loop over atoms k */
2114 if(moldyn->func3b_k2) {
2122 while(neighbour_i[j][q]!=0) {
2124 ktom=&(atom[neighbour_i[k][q]]);
2127 that=&(neighbour_i2[k]);
2130 if(that->start==NULL)
2134 ktom=that->current->data;
2137 if(!(ktom->attr&ATOM_ATTR_3BP))
2143 if(ktom==&(itom[i]))
2146 moldyn->func3b_k2(moldyn,
2155 } while(list_next_f(that)!=\
2163 /* 2bp post function */
2164 if(moldyn->func3b_j3) {
2165 moldyn->func3b_j3(moldyn,
2172 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2187 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2188 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2190 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
2191 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
2192 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
2196 /* some postprocessing */
2197 for(i=0;i<count;i++) {
2198 /* calculate global virial */
2199 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
2200 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
2201 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
2202 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
2203 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
2204 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
2206 /* check forces regarding the given timestep */
2207 if(v3_norm(&(itom[i].f))>\
2208 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
2209 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",
2217 * virial calculation
2220 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2221 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2223 a->virial.xx+=f->x*d->x;
2224 a->virial.yy+=f->y*d->y;
2225 a->virial.zz+=f->z*d->z;
2226 a->virial.xy+=f->x*d->y;
2227 a->virial.xz+=f->x*d->z;
2228 a->virial.yz+=f->y*d->z;
2234 * periodic boundary checking
2237 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2238 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2249 if(moldyn->status&MOLDYN_STAT_PBX) {
2250 if(a->x>=x) a->x-=dim->x;
2251 else if(-a->x>x) a->x+=dim->x;
2253 if(moldyn->status&MOLDYN_STAT_PBY) {
2254 if(a->y>=y) a->y-=dim->y;
2255 else if(-a->y>y) a->y+=dim->y;
2257 if(moldyn->status&MOLDYN_STAT_PBZ) {
2258 if(a->z>=z) a->z-=dim->z;
2259 else if(-a->z>z) a->z+=dim->z;
2266 * debugging / critical check functions
2269 int moldyn_bc_check(t_moldyn *moldyn) {
2282 for(i=0;i<moldyn->count;i++) {
2283 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2284 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2285 i,atom[i].r.x,dim->x/2);
2286 printf("diagnostic:\n");
2287 printf("-----------\natom.r.x:\n");
2289 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2292 ((byte)&(1<<k))?1:0,
2295 printf("---------------\nx=dim.x/2:\n");
2297 memcpy(&byte,(u8 *)(&x)+j,1);
2300 ((byte)&(1<<k))?1:0,
2303 if(atom[i].r.x==x) printf("the same!\n");
2304 else printf("different!\n");
2306 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2307 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2308 i,atom[i].r.y,dim->y/2);
2309 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2310 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2311 i,atom[i].r.z,dim->z/2);
2321 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2328 fd=open(file,O_RDONLY);
2330 perror("[moldyn] load save file open");
2334 fsize=lseek(fd,0,SEEK_END);
2335 lseek(fd,0,SEEK_SET);
2337 size=sizeof(t_moldyn);
2340 cnt=read(fd,moldyn,size);
2342 perror("[moldyn] load save file read (moldyn)");
2348 size=moldyn->count*sizeof(t_atom);
2350 /* correcting possible atom data offset */
2352 if(fsize!=sizeof(t_moldyn)+size) {
2353 corr=fsize-sizeof(t_moldyn)-size;
2354 printf("[moldyn] WARNING: lsf (illegal file size)\n");
2355 printf(" moifying offset:\n");
2356 printf(" - current pos: %d\n",sizeof(t_moldyn));
2357 printf(" - atom size: %d\n",size);
2358 printf(" - file size: %d\n",fsize);
2359 printf(" => correction: %d\n",corr);
2360 lseek(fd,corr,SEEK_CUR);
2363 moldyn->atom=(t_atom *)malloc(size);
2364 if(moldyn->atom==NULL) {
2365 perror("[moldyn] load save file malloc (atoms)");
2370 cnt=read(fd,moldyn->atom,size);
2372 perror("[moldyn] load save file read (atoms)");
2383 int moldyn_free_save_file(t_moldyn *moldyn) {
2390 int moldyn_load(t_moldyn *moldyn) {
2398 * function to find/callback all combinations of 2 body bonds
2401 int process_2b_bonds(t_moldyn *moldyn,void *data,
2402 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2403 void *data,u8 bc)) {
2410 t_list neighbour[27];
2420 for(i=0;i<moldyn->count;i++) {
2421 /* neighbour indexing */
2422 link_cell_neighbour_index(moldyn,
2423 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2424 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2425 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2430 bc=(j<lc->dnlc)?0:1;
2435 while(neighbour[j][p]!=0) {
2437 jtom=&(moldyn->atom[neighbour[j][p]]);
2440 this=&(neighbour[j]);
2443 if(this->start==NULL)
2448 jtom=this->current->data;
2452 process(moldyn,&(itom[i]),jtom,data,bc);
2457 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2467 * post processing functions
2470 int get_line(int fd,char *line,int max) {
2477 if(count==max) return count;
2478 ret=read(fd,line+count,1);
2479 if(ret<=0) return ret;
2480 if(line[count]=='\n') {
2481 memset(line+count,0,max-count-1);
2489 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2495 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
2511 for(i=0;i<moldyn->count;i++) {
2513 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
2514 check_per_bound(moldyn,&dist);
2515 d2=v3_absolute_square(&dist);
2529 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
2530 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
2531 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
2536 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
2541 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
2542 t_atom *jtom,void *data,u8 bc) {
2549 /* only count pairs once,
2550 * skip same atoms */
2551 if(itom->tag>=jtom->tag)
2555 * pair correlation calc
2562 v3_sub(&dist,&(jtom->r),&(itom->r));
2563 if(bc) check_per_bound(moldyn,&dist);
2564 d=v3_absolute_square(&dist);
2566 /* ignore if greater cutoff */
2567 if(d>moldyn->cutoff_square)
2570 /* fill the slots */
2574 /* should never happen but it does 8) -
2575 * related to -ffloat-store problem! */
2577 printf("[moldyn] WARNING: pcc (%d/%d)",
2583 if(itom->brand!=jtom->brand) {
2588 /* type a - type a bonds */
2590 pcc->stat[s+pcc->o1]+=1;
2592 /* type b - type b bonds */
2593 pcc->stat[s+pcc->o2]+=1;
2599 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2606 pcc.o1=moldyn->cutoff/dr;
2609 if(pcc.o1*dr<=moldyn->cutoff)
2610 printf("[moldyn] WARNING: pcc (low #slots)\n");
2612 printf("[moldyn] pair correlation calc info:\n");
2613 printf(" time: %f\n",moldyn->time);
2614 printf(" count: %d\n",moldyn->count);
2615 printf(" cutoff: %f\n",moldyn->cutoff);
2616 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2619 pcc.stat=(double *)ptr;
2622 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
2623 if(pcc.stat==NULL) {
2624 perror("[moldyn] pair correlation malloc");
2629 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
2632 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
2635 for(i=1;i<pcc.o1;i++) {
2636 // normalization: 4 pi r^2 dr
2637 // here: not double counting pairs -> 2 pi r r dr
2638 // ... and actually it's a constant times r^2
2641 pcc.stat[pcc.o1+i]/=norm;
2642 pcc.stat[pcc.o2+i]/=norm;
2647 /* todo: store/print pair correlation function */
2654 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2661 if(itom->tag>=jtom->tag)
2665 v3_sub(&dist,&(jtom->r),&(itom->r));
2666 if(bc) check_per_bound(moldyn,&dist);
2667 d=v3_absolute_square(&dist);
2669 /* ignore if greater or equal cutoff */
2670 if(d>moldyn->cutoff_square)
2673 /* check for potential bond */
2674 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2677 /* now count this bonding ... */
2680 /* increase total bond counter
2681 * ... double counting!
2686 ba->acnt[jtom->tag]+=1;
2688 ba->bcnt[jtom->tag]+=1;
2691 ba->acnt[itom->tag]+=1;
2693 ba->bcnt[itom->tag]+=1;
2698 int bond_analyze(t_moldyn *moldyn,double *quality) {
2700 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
2708 ba.acnt=malloc(moldyn->count*sizeof(int));
2710 perror("[moldyn] bond analyze malloc (a)");
2713 memset(ba.acnt,0,moldyn->count*sizeof(int));
2715 ba.bcnt=malloc(moldyn->count*sizeof(int));
2717 perror("[moldyn] bond analyze malloc (b)");
2720 memset(ba.bcnt,0,moldyn->count*sizeof(int));
2729 process_2b_bonds(moldyn,&ba,bond_analyze_process);
2731 for(i=0;i<moldyn->count;i++) {
2732 if(atom[i].brand==0) {
2733 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
2737 if((ba.acnt[i]==4)&(ba.bcnt[i]==0)) {
2745 printf("[moldyn] bond analyze: c_cnt=%d | set=%d\n",ccnt,cset);
2746 printf("[moldyn] bond analyze: q_cnt=%d | tot=%d\n",qcnt,ba.tcnt);
2749 quality[0]=1.0*ccnt/cset;
2750 quality[1]=1.0*qcnt/ba.tcnt;
2753 printf("[moldyn] bond analyze: c_bnd_q=%f\n",1.0*qcnt/ba.tcnt);
2754 printf("[moldyn] bond analyze: tot_q=%f\n",1.0*qcnt/ba.tcnt);
2761 * visualization code
2764 int visual_init(t_moldyn *moldyn,char *filebase) {
2766 strncpy(moldyn->vis.fb,filebase,128);
2771 int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2778 if(itom->tag>=jtom->tag)
2781 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2784 if((itom->attr&ATOM_ATTR_VB)|(jtom->attr&ATOM_ATTR_VB))
2785 dprintf(vb->fd,"# [B] %f %f %f %f %f %f\n",
2786 itom->r.x,itom->r.y,itom->r.z,
2787 jtom->r.x,jtom->r.y,jtom->r.z);
2792 int visual_atoms(t_moldyn *moldyn) {
2810 sprintf(file,"%s/atomic_conf_%07.f.xyz",v->fb,moldyn->time);
2811 vb.fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
2813 perror("open visual save file fd");
2817 /* write the actual data file */
2820 dprintf(vb.fd,"# [P] %d %07.f <%f,%f,%f>\n",
2821 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
2823 // atomic configuration
2824 for(i=0;i<moldyn->count;i++)
2825 // atom type, positions, color and kinetic energy
2826 dprintf(vb.fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
2830 pse_col[atom[i].element],
2833 // bonds between atoms
2834 process_2b_bonds(moldyn,&vb,visual_bonds_process);
2838 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2839 -dim.x/2,-dim.y/2,-dim.z/2,
2840 dim.x/2,-dim.y/2,-dim.z/2);
2841 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2842 -dim.x/2,-dim.y/2,-dim.z/2,
2843 -dim.x/2,dim.y/2,-dim.z/2);
2844 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2845 dim.x/2,dim.y/2,-dim.z/2,
2846 dim.x/2,-dim.y/2,-dim.z/2);
2847 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2848 -dim.x/2,dim.y/2,-dim.z/2,
2849 dim.x/2,dim.y/2,-dim.z/2);
2851 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2852 -dim.x/2,-dim.y/2,dim.z/2,
2853 dim.x/2,-dim.y/2,dim.z/2);
2854 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2855 -dim.x/2,-dim.y/2,dim.z/2,
2856 -dim.x/2,dim.y/2,dim.z/2);
2857 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2858 dim.x/2,dim.y/2,dim.z/2,
2859 dim.x/2,-dim.y/2,dim.z/2);
2860 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2861 -dim.x/2,dim.y/2,dim.z/2,
2862 dim.x/2,dim.y/2,dim.z/2);
2864 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2865 -dim.x/2,-dim.y/2,dim.z/2,
2866 -dim.x/2,-dim.y/2,-dim.z/2);
2867 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2868 -dim.x/2,dim.y/2,dim.z/2,
2869 -dim.x/2,dim.y/2,-dim.z/2);
2870 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2871 dim.x/2,-dim.y/2,dim.z/2,
2872 dim.x/2,-dim.y/2,-dim.z/2);
2873 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2874 dim.x/2,dim.y/2,dim.z/2,
2875 dim.x/2,dim.y/2,-dim.z/2);