2 * moldyn.c - molecular dynamics library main file
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
12 #include <sys/types.h>
19 #include "report/report.h"
21 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
23 printf("[moldyn] init\n");
25 memset(moldyn,0,sizeof(t_moldyn));
27 rand_init(&(moldyn->random),NULL,1);
28 moldyn->random.status|=RAND_STAT_VERBOSE;
33 int moldyn_shutdown(t_moldyn *moldyn) {
35 printf("[moldyn] shutdown\n");
37 moldyn_log_shutdown(moldyn);
38 link_cell_shutdown(moldyn);
39 rand_close(&(moldyn->random));
45 int set_int_alg(t_moldyn *moldyn,u8 algo) {
47 printf("[moldyn] integration algorithm: ");
50 case MOLDYN_INTEGRATE_VERLET:
51 moldyn->integrate=velocity_verlet;
52 printf("velocity verlet\n");
55 printf("unknown integration algorithm: %02x\n",algo);
63 int set_cutoff(t_moldyn *moldyn,double cutoff) {
65 moldyn->cutoff=cutoff;
67 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
72 int set_temperature(t_moldyn *moldyn,double t_ref) {
76 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
81 int set_pressure(t_moldyn *moldyn,double p_ref) {
85 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
90 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
92 moldyn->pt_scale=(ptype|ttype);
96 printf("[moldyn] p/t scaling:\n");
98 printf(" p: %s",ptype?"yes":"no ");
100 printf(" | type: %02x | factor: %f",ptype,ptc);
103 printf(" t: %s",ttype?"yes":"no ");
105 printf(" | type: %02x | factor: %f",ttype,ttc);
111 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
117 moldyn->volume=x*y*z;
125 moldyn->dv=0.000001*moldyn->volume;
127 printf("[moldyn] dimensions in A and A^3 respectively:\n");
128 printf(" x: %f\n",moldyn->dim.x);
129 printf(" y: %f\n",moldyn->dim.y);
130 printf(" z: %f\n",moldyn->dim.z);
131 printf(" volume: %f\n",moldyn->volume);
132 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
133 printf(" delta volume (pressure calc): %f\n",moldyn->dv);
138 int set_nn_dist(t_moldyn *moldyn,double dist) {
145 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
147 printf("[moldyn] periodic boundary conditions:\n");
150 moldyn->status|=MOLDYN_STAT_PBX;
153 moldyn->status|=MOLDYN_STAT_PBY;
156 moldyn->status|=MOLDYN_STAT_PBZ;
158 printf(" x: %s\n",x?"yes":"no");
159 printf(" y: %s\n",y?"yes":"no");
160 printf(" z: %s\n",z?"yes":"no");
165 int set_potential1b(t_moldyn *moldyn,pf_func1b func) {
172 int set_potential2b(t_moldyn *moldyn,pf_func2b func) {
179 int set_potential3b_j1(t_moldyn *moldyn,pf_func2b func) {
181 moldyn->func3b_j1=func;
186 int set_potential3b_j2(t_moldyn *moldyn,pf_func2b func) {
188 moldyn->func3b_j2=func;
193 int set_potential3b_j3(t_moldyn *moldyn,pf_func2b func) {
195 moldyn->func3b_j3=func;
200 int set_potential3b_k1(t_moldyn *moldyn,pf_func3b func) {
202 moldyn->func3b_k1=func;
207 int set_potential3b_k2(t_moldyn *moldyn,pf_func3b func) {
209 moldyn->func3b_k2=func;
214 int set_potential_params(t_moldyn *moldyn,void *params) {
216 moldyn->pot_params=params;
221 int set_mean_skip(t_moldyn *moldyn,int skip) {
223 printf("[moldyn] skip %d steps before starting average calc\n",skip);
224 moldyn->mean_skip=skip;
229 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
231 strncpy(moldyn->vlsdir,dir,127);
236 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
238 strncpy(moldyn->rauthor,author,63);
239 strncpy(moldyn->rtitle,title,63);
244 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
249 printf("[moldyn] set log: ");
252 case LOG_TOTAL_ENERGY:
253 moldyn->ewrite=timer;
254 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
255 moldyn->efd=open(filename,
256 O_WRONLY|O_CREAT|O_EXCL,
259 perror("[moldyn] energy log fd open");
262 dprintf(moldyn->efd,"# total energy log file\n");
263 printf("total energy (%d)\n",timer);
265 case LOG_TOTAL_MOMENTUM:
266 moldyn->mwrite=timer;
267 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
268 moldyn->mfd=open(filename,
269 O_WRONLY|O_CREAT|O_EXCL,
272 perror("[moldyn] momentum log fd open");
275 dprintf(moldyn->efd,"# total momentum log file\n");
276 printf("total momentum (%d)\n",timer);
279 moldyn->pwrite=timer;
280 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
281 moldyn->pfd=open(filename,
282 O_WRONLY|O_CREAT|O_EXCL,
285 perror("[moldyn] pressure log file\n");
288 dprintf(moldyn->pfd,"# pressure log file\n");
289 printf("pressure (%d)\n",timer);
291 case LOG_TEMPERATURE:
292 moldyn->twrite=timer;
293 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
294 moldyn->tfd=open(filename,
295 O_WRONLY|O_CREAT|O_EXCL,
298 perror("[moldyn] temperature log file\n");
301 dprintf(moldyn->tfd,"# temperature log file\n");
302 printf("temperature (%d)\n",timer);
305 moldyn->swrite=timer;
306 printf("save file (%d)\n",timer);
309 moldyn->vwrite=timer;
310 ret=visual_init(&(moldyn->vis),moldyn->vlsdir);
312 printf("[moldyn] visual init failure\n");
315 printf("visual file (%d)\n",timer);
318 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
319 moldyn->rfd=open(filename,
320 O_WRONLY|O_CREAT|O_EXCL,
323 perror("[moldyn] report fd open");
326 printf("report -> ");
328 snprintf(filename,127,"%s/e_plot.scr",
330 moldyn->epfd=open(filename,
331 O_WRONLY|O_CREAT|O_EXCL,
334 perror("[moldyn] energy plot fd open");
337 dprintf(moldyn->epfd,e_plot_script);
342 snprintf(filename,127,"%s/pressure_plot.scr",
344 moldyn->ppfd=open(filename,
345 O_WRONLY|O_CREAT|O_EXCL,
348 perror("[moldyn] p plot fd open");
351 dprintf(moldyn->ppfd,pressure_plot_script);
356 snprintf(filename,127,"%s/temperature_plot.scr",
358 moldyn->tpfd=open(filename,
359 O_WRONLY|O_CREAT|O_EXCL,
362 perror("[moldyn] t plot fd open");
365 dprintf(moldyn->tpfd,temperature_plot_script);
367 printf("temperature ");
369 dprintf(moldyn->rfd,report_start,
370 moldyn->rauthor,moldyn->rtitle);
374 printf("unknown log type: %02x\n",type);
381 int moldyn_log_shutdown(t_moldyn *moldyn) {
385 printf("[moldyn] log shutdown\n");
389 dprintf(moldyn->rfd,report_energy);
390 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
395 if(moldyn->mfd) close(moldyn->mfd);
399 dprintf(moldyn->rfd,report_pressure);
400 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
407 dprintf(moldyn->rfd,report_temperature);
408 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
413 dprintf(moldyn->rfd,report_end);
415 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
418 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
421 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
425 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
431 * creating lattice functions
434 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
435 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
446 /* how many atoms do we expect */
447 if(type==CUBIC) new*=1;
448 if(type==FCC) new*=4;
449 if(type==DIAMOND) new*=8;
451 /* allocate space for atoms */
452 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
454 perror("[moldyn] realloc (create lattice)");
458 atom=&(moldyn->atom[count]);
460 /* no atoms on the boundaries (only reason: it looks better!) */
474 set_nn_dist(moldyn,lc);
475 ret=cubic_init(a,b,c,lc,atom,&orig);
479 v3_scale(&orig,&orig,0.5);
480 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
481 ret=fcc_init(a,b,c,lc,atom,&orig);
485 v3_scale(&orig,&orig,0.25);
486 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
487 ret=diamond_init(a,b,c,lc,atom,&orig);
490 printf("unknown lattice type (%02x)\n",type);
496 printf("[moldyn] creating lattice failed\n");
497 printf(" amount of atoms\n");
498 printf(" - expected: %d\n",new);
499 printf(" - created: %d\n",ret);
504 printf("[moldyn] created lattice with %d atoms\n",new);
506 for(ret=0;ret<new;ret++) {
507 atom[ret].element=element;
510 atom[ret].brand=brand;
511 atom[ret].tag=count+ret;
512 check_per_bound(moldyn,&(atom[ret].r));
515 /* update total system mass */
516 total_mass_calc(moldyn);
522 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
541 v3_copy(&(atom[count].r),&r);
550 for(i=0;i<count;i++) {
551 atom[i].r.x-=(a*lc)/2.0;
552 atom[i].r.y-=(b*lc)/2.0;
553 atom[i].r.z-=(c*lc)/2.0;
559 /* fcc lattice init */
560 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
573 /* construct the basis */
574 memset(basis,0,3*sizeof(t_3dvec));
582 /* fill up the room */
590 v3_copy(&(atom[count].r),&r);
593 /* the three face centered atoms */
595 v3_add(&n,&r,&basis[l]);
596 v3_copy(&(atom[count].r),&n);
605 /* coordinate transformation */
606 for(i=0;i<count;i++) {
607 atom[i].r.x-=(a*lc)/2.0;
608 atom[i].r.y-=(b*lc)/2.0;
609 atom[i].r.z-=(c*lc)/2.0;
615 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
620 count=fcc_init(a,b,c,lc,atom,origin);
626 if(origin) v3_add(&o,&o,origin);
628 count+=fcc_init(a,b,c,lc,&atom[count],&o);
633 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
634 t_3dvec *r,t_3dvec *v) {
641 count=(moldyn->count)++;
643 ptr=realloc(atom,(count+1)*sizeof(t_atom));
645 perror("[moldyn] realloc (add atom)");
653 atom[count].element=element;
654 atom[count].mass=mass;
655 atom[count].brand=brand;
656 atom[count].tag=count;
657 atom[count].attr=attr;
659 /* update total system mass */
660 total_mass_calc(moldyn);
665 int destroy_atoms(t_moldyn *moldyn) {
667 if(moldyn->atom) free(moldyn->atom);
672 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
675 * - gaussian distribution of velocities
676 * - zero total momentum
677 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
682 t_3dvec p_total,delta;
687 random=&(moldyn->random);
689 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
691 /* gaussian distribution of velocities */
693 for(i=0;i<moldyn->count;i++) {
694 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
696 v=sigma*rand_get_gauss(random);
698 p_total.x+=atom[i].mass*v;
700 v=sigma*rand_get_gauss(random);
702 p_total.y+=atom[i].mass*v;
704 v=sigma*rand_get_gauss(random);
706 p_total.z+=atom[i].mass*v;
709 /* zero total momentum */
710 v3_scale(&p_total,&p_total,1.0/moldyn->count);
711 for(i=0;i<moldyn->count;i++) {
712 v3_scale(&delta,&p_total,1.0/atom[i].mass);
713 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
716 /* velocity scaling */
717 scale_velocity(moldyn,equi_init);
722 double total_mass_calc(t_moldyn *moldyn) {
728 for(i=0;i<moldyn->count;i++)
729 moldyn->mass+=moldyn->atom[i].mass;
734 double temperature_calc(t_moldyn *moldyn) {
736 /* assume up to date kinetic energy, which is 3/2 N k_B T */
738 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
740 if(moldyn->total_steps<moldyn->mean_skip)
743 moldyn->t_sum+=moldyn->t;
744 moldyn->mean_t=moldyn->t_sum/(moldyn->total_steps+1-moldyn->mean_skip);
749 double get_temperature(t_moldyn *moldyn) {
754 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
764 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
767 /* get kinetic energy / temperature & count involved atoms */
770 for(i=0;i<moldyn->count;i++) {
771 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
772 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
777 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
778 else return 0; /* no atoms involved in scaling! */
780 /* (temporary) hack for e,t = 0 */
783 if(moldyn->t_ref!=0.0) {
784 thermal_init(moldyn,equi_init);
788 return 0; /* no scaling needed */
792 /* get scaling factor */
793 scale=moldyn->t_ref/moldyn->t;
797 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
798 scale=1.0+(scale-1.0)/moldyn->t_tc;
801 /* velocity scaling */
802 for(i=0;i<moldyn->count;i++) {
803 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
804 v3_scale(&(atom[i].v),&(atom[i].v),scale);
810 double ideal_gas_law_pressure(t_moldyn *moldyn) {
814 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
819 double pressure_calc(t_moldyn *moldyn) {
827 * W = 1/3 sum_i f_i r_i
828 * virial = sum_i f_i r_i
830 * => P = (2 Ekin + virial) / (3V)
834 for(i=0;i<moldyn->count;i++) {
835 virial=&(moldyn->atom[i].virial);
836 v+=(virial->xx+virial->yy+virial->zz);
839 /* virial sum and mean virial */
840 moldyn->virial_sum+=v;
841 if(moldyn->total_steps>=moldyn->mean_skip)
842 moldyn->mean_v=moldyn->virial_sum/
843 (moldyn->total_steps+1-moldyn->mean_skip);
845 /* assume up to date kinetic energy */
846 moldyn->p=2.0*moldyn->ekin+moldyn->mean_v;
847 moldyn->p/=(3.0*moldyn->volume);
848 if(moldyn->total_steps>=moldyn->mean_skip) {
849 moldyn->p_sum+=moldyn->p;
850 moldyn->mean_p=moldyn->p_sum/
851 (moldyn->total_steps+1-moldyn->mean_skip);
854 /* pressure from 'absolute coordinates' virial */
855 virial=&(moldyn->virial);
856 v=virial->xx+virial->yy+virial->zz;
857 moldyn->gp=2.0*moldyn->ekin+v;
858 moldyn->gp/=(3.0*moldyn->volume);
859 if(moldyn->total_steps>=moldyn->mean_skip) {
860 moldyn->gp_sum+=moldyn->gp;
861 moldyn->mean_gp=moldyn->gp_sum/
862 (moldyn->total_steps+1-moldyn->mean_skip);
868 int energy_fluctuation_calc(t_moldyn *moldyn) {
870 if(moldyn->total_steps<moldyn->mean_skip)
873 /* assume up to date energies */
876 moldyn->k_sum+=moldyn->ekin;
877 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
878 moldyn->k_mean=moldyn->k_sum/(moldyn->total_steps+1-moldyn->mean_skip);
879 moldyn->k2_mean=moldyn->k2_sum/
880 (moldyn->total_steps+1-moldyn->mean_skip);
881 moldyn->dk2_mean=moldyn->k2_mean-(moldyn->k_mean*moldyn->k_mean);
883 /* potential energy */
884 moldyn->v_sum+=moldyn->energy;
885 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
886 moldyn->v_mean=moldyn->v_sum/(moldyn->total_steps+1-moldyn->mean_skip);
887 moldyn->v2_mean=moldyn->v2_sum/
888 (moldyn->total_steps+1-moldyn->mean_skip);
889 moldyn->dv2_mean=moldyn->v2_mean-(moldyn->v_mean*moldyn->v_mean);
894 int get_heat_capacity(t_moldyn *moldyn) {
898 /* averages needed for heat capacity calc */
899 if(moldyn->total_steps<moldyn->mean_skip)
902 /* (temperature average)^2 */
903 temp2=moldyn->mean_t*moldyn->mean_t;
904 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
907 /* ideal gas contribution */
908 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
909 printf(" ideal gas contribution: %f\n",
910 ighc/moldyn->mass*KILOGRAM/JOULE);
912 /* specific heat for nvt ensemble */
913 moldyn->c_v_nvt=moldyn->dv2_mean/(K_BOLTZMANN*temp2)+ighc;
914 moldyn->c_v_nvt/=moldyn->mass;
916 /* specific heat for nve ensemble */
917 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_mean/(ighc*K_BOLTZMANN*temp2)));
918 moldyn->c_v_nve/=moldyn->mass;
920 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
921 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
922 printf(" --> <dV2> sim: %f experimental: %f\n",moldyn->dv2_mean,1.5*moldyn->count*K_B2*moldyn->mean_t*moldyn->mean_t*(1.0-1.5*moldyn->count*K_BOLTZMANN/(700*moldyn->mass*JOULE/KILOGRAM)));
927 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
930 double u_up,u_down,dv;
942 dv=8*scale*scale*scale*moldyn->volume;
944 store=malloc(moldyn->count*sizeof(t_atom));
946 printf("[moldyn] allocating store mem failed\n");
950 /* save unscaled potential energy + atom/dim configuration */
951 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
954 /* scale up dimension and atom positions */
955 scale_dim(moldyn,SCALE_UP,scale,TRUE,TRUE,TRUE);
956 scale_atoms(moldyn,SCALE_UP,scale,TRUE,TRUE,TRUE);
957 link_cell_shutdown(moldyn);
958 link_cell_init(moldyn,QUIET);
959 potential_force_calc(moldyn);
962 /* restore atomic configuration + dim */
963 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
966 /* scale down dimension and atom positions */
967 scale_dim(moldyn,SCALE_DOWN,scale,TRUE,TRUE,TRUE);
968 scale_atoms(moldyn,SCALE_DOWN,scale,TRUE,TRUE,TRUE);
969 link_cell_shutdown(moldyn);
970 link_cell_init(moldyn,QUIET);
971 potential_force_calc(moldyn);
972 u_down=moldyn->energy;
974 /* calculate pressure */
976 printf("-------> %.10f %.10f %f\n",u_up/EV/moldyn->count,u_down/EV/moldyn->count,p/BAR);
978 /* restore atomic configuration + dim */
979 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
983 potential_force_calc(moldyn);
985 link_cell_shutdown(moldyn);
986 link_cell_init(moldyn,QUIET);
991 double get_pressure(t_moldyn *moldyn) {
997 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1009 if(x) dim->x*=scale;
1010 if(y) dim->y*=scale;
1011 if(z) dim->z*=scale;
1016 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1027 for(i=0;i<moldyn->count;i++) {
1028 r=&(moldyn->atom[i].r);
1037 int scale_volume(t_moldyn *moldyn) {
1043 vdim=&(moldyn->vis.dim);
1047 /* scaling factor */
1048 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1049 scale=1.0-(moldyn->p_ref-moldyn->p)/moldyn->p_tc;
1050 scale=pow(scale,ONE_THIRD);
1053 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1055 moldyn->debug=scale;
1057 /* scale the atoms and dimensions */
1058 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1059 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1061 /* visualize dimensions */
1068 /* recalculate scaled volume */
1069 moldyn->volume=dim->x*dim->y*dim->z;
1071 /* adjust/reinit linkcell */
1072 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1073 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1074 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1075 link_cell_shutdown(moldyn);
1076 link_cell_init(moldyn,QUIET);
1087 double e_kin_calc(t_moldyn *moldyn) {
1095 for(i=0;i<moldyn->count;i++)
1096 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1098 return moldyn->ekin;
1101 double get_total_energy(t_moldyn *moldyn) {
1103 return(moldyn->ekin+moldyn->energy);
1106 t_3dvec get_total_p(t_moldyn *moldyn) {
1115 for(i=0;i<moldyn->count;i++) {
1116 v3_scale(&p,&(atom[i].v),atom[i].mass);
1117 v3_add(&p_total,&p_total,&p);
1123 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1127 /* nn_dist is the nearest neighbour distance */
1129 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1138 /* linked list / cell method */
1140 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1147 /* partitioning the md cell */
1148 lc->nx=moldyn->dim.x/moldyn->cutoff;
1149 lc->x=moldyn->dim.x/lc->nx;
1150 lc->ny=moldyn->dim.y/moldyn->cutoff;
1151 lc->y=moldyn->dim.y/lc->ny;
1152 lc->nz=moldyn->dim.z/moldyn->cutoff;
1153 lc->z=moldyn->dim.z/lc->nz;
1155 lc->cells=lc->nx*lc->ny*lc->nz;
1156 lc->subcell=malloc(lc->cells*sizeof(t_list));
1159 printf("[moldyn] FATAL: less then 27 subcells!\n");
1162 printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
1163 printf(" x: %d x %f A\n",lc->nx,lc->x);
1164 printf(" y: %d x %f A\n",lc->ny,lc->y);
1165 printf(" z: %d x %f A\n",lc->nz,lc->z);
1168 for(i=0;i<lc->cells;i++)
1169 list_init_f(&(lc->subcell[i]));
1171 link_cell_update(moldyn);
1176 int link_cell_update(t_moldyn *moldyn) {
1194 for(i=0;i<lc->cells;i++)
1195 list_destroy_f(&(lc->subcell[i]));
1197 for(count=0;count<moldyn->count;count++) {
1198 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1199 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1200 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1201 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1208 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
1226 cell[0]=lc->subcell[i+j*nx+k*a];
1227 for(ci=-1;ci<=1;ci++) {
1230 if((x<0)||(x>=nx)) {
1234 for(cj=-1;cj<=1;cj++) {
1237 if((y<0)||(y>=ny)) {
1241 for(ck=-1;ck<=1;ck++) {
1244 if((z<0)||(z>=nz)) {
1248 if(!(ci|cj|ck)) continue;
1250 cell[--count2]=lc->subcell[x+y*nx+z*a];
1253 cell[count1++]=lc->subcell[x+y*nx+z*a];
1264 int link_cell_shutdown(t_moldyn *moldyn) {
1271 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
1272 list_destroy_f(&(moldyn->lc.subcell[i]));
1279 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1283 t_moldyn_schedule *schedule;
1285 schedule=&(moldyn->schedule);
1286 count=++(schedule->total_sched);
1288 ptr=realloc(schedule->runs,count*sizeof(int));
1290 perror("[moldyn] realloc (runs)");
1294 schedule->runs[count-1]=runs;
1296 ptr=realloc(schedule->tau,count*sizeof(double));
1298 perror("[moldyn] realloc (tau)");
1302 schedule->tau[count-1]=tau;
1304 printf("[moldyn] schedule added:\n");
1305 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1311 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1313 moldyn->schedule.hook=hook;
1314 moldyn->schedule.hook_params=hook_params;
1321 * 'integration of newtons equation' - algorithms
1325 /* start the integration */
1327 int moldyn_integrate(t_moldyn *moldyn) {
1330 unsigned int e,m,s,v,p,t;
1332 t_moldyn_schedule *sched;
1337 double energy_scale;
1340 sched=&(moldyn->schedule);
1343 /* initialize linked cell method */
1344 link_cell_init(moldyn,VERBOSE);
1346 /* logging & visualization */
1354 /* sqaure of some variables */
1355 moldyn->tau_square=moldyn->tau*moldyn->tau;
1356 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
1358 /* energy scaling factor */
1359 energy_scale=moldyn->count*EV;
1361 /* calculate initial forces */
1362 potential_force_calc(moldyn);
1367 /* some stupid checks before we actually start calculating bullshit */
1368 if(moldyn->cutoff>0.5*moldyn->dim.x)
1369 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
1370 if(moldyn->cutoff>0.5*moldyn->dim.y)
1371 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
1372 if(moldyn->cutoff>0.5*moldyn->dim.z)
1373 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
1374 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1375 if(ds>0.05*moldyn->nnd)
1376 printf("[moldyn] warning: forces too high / tau too small!\n");
1378 /* zero absolute time */
1380 moldyn->total_steps=0;
1382 /* debugging, ignore */
1385 /* tell the world */
1386 printf("[moldyn] integration start, go get a coffee ...\n");
1388 /* executing the schedule */
1389 for(sched->count=0;sched->count<sched->total_sched;sched->count++) {
1391 /* setting amount of runs and finite time step size */
1392 moldyn->tau=sched->tau[sched->count];
1393 moldyn->tau_square=moldyn->tau*moldyn->tau;
1394 moldyn->time_steps=sched->runs[sched->count];
1396 /* integration according to schedule */
1398 for(i=0;i<moldyn->time_steps;i++) {
1400 /* integration step */
1401 moldyn->integrate(moldyn);
1403 /* calculate kinetic energy, temperature and pressure */
1405 temperature_calc(moldyn);
1406 pressure_calc(moldyn);
1407 energy_fluctuation_calc(moldyn);
1408 //tp=thermodynamic_pressure_calc(moldyn);
1409 //printf("thermodynamic p: %f\n",thermodynamic_pressure_calc(moldyn)/BAR);
1412 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1413 scale_velocity(moldyn,FALSE);
1414 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1415 scale_volume(moldyn);
1417 /* check for log & visualization */
1420 dprintf(moldyn->efd,
1422 moldyn->time,moldyn->ekin/energy_scale,
1423 moldyn->energy/energy_scale,
1424 get_total_energy(moldyn)/energy_scale);
1428 momentum=get_total_p(moldyn);
1429 dprintf(moldyn->mfd,
1430 "%f %f %f %f %f\n",moldyn->time,
1431 momentum.x,momentum.y,momentum.z,
1432 v3_norm(&momentum));
1437 dprintf(moldyn->pfd,
1438 "%f %f %f %f %f\n",moldyn->time,
1439 moldyn->p/BAR,moldyn->mean_p/BAR,
1440 moldyn->gp/BAR,moldyn->mean_gp/BAR);
1445 dprintf(moldyn->tfd,
1447 moldyn->time,moldyn->t,moldyn->mean_t);
1452 snprintf(dir,128,"%s/s-%07.f.save",
1453 moldyn->vlsdir,moldyn->time);
1454 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT);
1455 if(fd<0) perror("[moldyn] save fd open");
1457 write(fd,moldyn,sizeof(t_moldyn));
1458 write(fd,moldyn->atom,
1459 moldyn->count*sizeof(t_atom));
1466 visual_atoms(&(moldyn->vis),moldyn->time,
1467 moldyn->atom,moldyn->count);
1471 /* display progress */
1473 printf("\rsched: %d, steps: %d, T: %f, P: %f %f V: %f",
1477 moldyn->mean_gp/BAR,
1481 get_heat_capacity(moldyn);
1484 /* increase absolute time */
1485 moldyn->time+=moldyn->tau;
1486 moldyn->total_steps+=1;
1490 /* check for hooks */
1491 if(sched->count+1<sched->total_sched)
1493 sched->hook(moldyn,sched->hook_params);
1495 /* get a new info line */
1503 /* velocity verlet */
1505 int velocity_verlet(t_moldyn *moldyn) {
1508 double tau,tau_square,h;
1513 count=moldyn->count;
1515 tau_square=moldyn->tau_square;
1517 for(i=0;i<count;i++) {
1520 v3_scale(&delta,&(atom[i].v),tau);
1521 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1522 v3_scale(&delta,&(atom[i].f),h*tau_square);
1523 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1524 check_per_bound(moldyn,&(atom[i].r));
1526 /* velocities [actually v(t+tau/2)] */
1527 v3_scale(&delta,&(atom[i].f),h*tau);
1528 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1531 /* neighbour list update */
1532 link_cell_update(moldyn);
1534 /* forces depending on chosen potential */
1535 potential_force_calc(moldyn);
1537 for(i=0;i<count;i++) {
1538 /* again velocities [actually v(t+tau)] */
1539 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1540 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1549 * potentials & corresponding forces & virial routine
1553 /* generic potential and force calculation */
1555 int potential_force_calc(t_moldyn *moldyn) {
1558 t_atom *itom,*jtom,*ktom;
1561 t_list neighbour_i[27];
1562 t_list neighbour_i2[27];
1567 count=moldyn->count;
1574 /* reset global virial */
1575 memset(&(moldyn->virial),0,sizeof(t_virial));
1577 /* reset force, site energy and virial of every atom */
1578 for(i=0;i<count;i++) {
1581 v3_zero(&(itom[i].f));
1584 virial=(&(itom[i].virial));
1592 /* reset site energy */
1597 /* get energy, force and virial of every atom */
1599 /* first (and only) loop over atoms i */
1600 for(i=0;i<count;i++) {
1602 /* single particle potential/force */
1603 if(itom[i].attr&ATOM_ATTR_1BP)
1605 moldyn->func1b(moldyn,&(itom[i]));
1607 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1610 /* 2 body pair potential/force */
1612 link_cell_neighbour_index(moldyn,
1613 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1614 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1615 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1620 /* first loop over atoms j */
1621 if(moldyn->func2b) {
1624 this=&(neighbour_i[j]);
1627 if(this->start==NULL)
1633 jtom=this->current->data;
1635 if(jtom==&(itom[i]))
1638 if((jtom->attr&ATOM_ATTR_2BP)&
1639 (itom[i].attr&ATOM_ATTR_2BP)) {
1640 moldyn->func2b(moldyn,
1645 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1650 /* 3 body potential/force */
1652 if(!(itom[i].attr&ATOM_ATTR_3BP))
1655 /* copy the neighbour lists */
1656 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
1658 /* second loop over atoms j */
1661 this=&(neighbour_i[j]);
1664 if(this->start==NULL)
1670 jtom=this->current->data;
1672 if(jtom==&(itom[i]))
1675 if(!(jtom->attr&ATOM_ATTR_3BP))
1681 if(moldyn->func3b_j1)
1682 moldyn->func3b_j1(moldyn,
1687 /* in first j loop, 3bp run can be skipped */
1688 if(!(moldyn->run3bp))
1691 /* first loop over atoms k */
1692 if(moldyn->func3b_k1) {
1696 that=&(neighbour_i2[k]);
1699 if(that->start==NULL)
1706 ktom=that->current->data;
1708 if(!(ktom->attr&ATOM_ATTR_3BP))
1714 if(ktom==&(itom[i]))
1717 moldyn->func3b_k1(moldyn,
1723 } while(list_next_f(that)!=\
1730 if(moldyn->func3b_j2)
1731 moldyn->func3b_j2(moldyn,
1736 /* second loop over atoms k */
1737 if(moldyn->func3b_k2) {
1741 that=&(neighbour_i2[k]);
1744 if(that->start==NULL)
1751 ktom=that->current->data;
1753 if(!(ktom->attr&ATOM_ATTR_3BP))
1759 if(ktom==&(itom[i]))
1762 moldyn->func3b_k2(moldyn,
1768 } while(list_next_f(that)!=\
1775 /* 2bp post function */
1776 if(moldyn->func3b_j3) {
1777 moldyn->func3b_j3(moldyn,
1782 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1796 printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
1799 /* calculate global virial */
1800 for(i=0;i<count;i++) {
1801 moldyn->virial.xx+=moldyn->atom[i].r.x*moldyn->atom[i].f.x;
1802 moldyn->virial.yy+=moldyn->atom[i].r.y*moldyn->atom[i].f.y;
1803 moldyn->virial.zz+=moldyn->atom[i].r.z*moldyn->atom[i].f.z;
1804 moldyn->virial.xy+=moldyn->atom[i].r.y*moldyn->atom[i].f.x;
1805 moldyn->virial.xz+=moldyn->atom[i].r.z*moldyn->atom[i].f.x;
1806 moldyn->virial.yz+=moldyn->atom[i].r.z*moldyn->atom[i].f.y;
1813 * virial calculation
1816 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
1817 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
1819 a->virial.xx+=f->x*d->x;
1820 a->virial.yy+=f->y*d->y;
1821 a->virial.zz+=f->z*d->z;
1822 a->virial.xy+=f->x*d->y;
1823 a->virial.xz+=f->x*d->z;
1824 a->virial.yz+=f->y*d->z;
1830 * periodic boundary checking
1833 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1834 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1845 if(moldyn->status&MOLDYN_STAT_PBX) {
1846 if(a->x>=x) a->x-=dim->x;
1847 else if(-a->x>x) a->x+=dim->x;
1849 if(moldyn->status&MOLDYN_STAT_PBY) {
1850 if(a->y>=y) a->y-=dim->y;
1851 else if(-a->y>y) a->y+=dim->y;
1853 if(moldyn->status&MOLDYN_STAT_PBZ) {
1854 if(a->z>=z) a->z-=dim->z;
1855 else if(-a->z>z) a->z+=dim->z;
1862 * debugging / critical check functions
1865 int moldyn_bc_check(t_moldyn *moldyn) {
1878 for(i=0;i<moldyn->count;i++) {
1879 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
1880 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
1881 i,atom[i].r.x,dim->x/2);
1882 printf("diagnostic:\n");
1883 printf("-----------\natom.r.x:\n");
1885 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
1888 ((byte)&(1<<k))?1:0,
1891 printf("---------------\nx=dim.x/2:\n");
1893 memcpy(&byte,(u8 *)(&x)+j,1);
1896 ((byte)&(1<<k))?1:0,
1899 if(atom[i].r.x==x) printf("the same!\n");
1900 else printf("different!\n");
1902 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
1903 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
1904 i,atom[i].r.y,dim->y/2);
1905 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
1906 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
1907 i,atom[i].r.z,dim->z/2);
1914 * post processing functions
1917 int get_line(int fd,char *line,int max) {
1924 if(count==max) return count;
1925 ret=read(fd,line+count,1);
1926 if(ret<=0) return ret;
1927 if(line[count]=='\n') {