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));
30 rand_init(&(moldyn->random),NULL,1);
31 moldyn->random.status|=RAND_STAT_VERBOSE;
36 int moldyn_shutdown(t_moldyn *moldyn) {
38 printf("[moldyn] shutdown\n");
40 moldyn_log_shutdown(moldyn);
41 link_cell_shutdown(moldyn);
42 rand_close(&(moldyn->random));
48 int set_int_alg(t_moldyn *moldyn,u8 algo) {
50 printf("[moldyn] integration algorithm: ");
53 case MOLDYN_INTEGRATE_VERLET:
54 moldyn->integrate=velocity_verlet;
55 printf("velocity verlet\n");
58 printf("unknown integration algorithm: %02x\n",algo);
66 int set_cutoff(t_moldyn *moldyn,double cutoff) {
68 moldyn->cutoff=cutoff;
70 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
75 int set_temperature(t_moldyn *moldyn,double t_ref) {
79 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
84 int set_pressure(t_moldyn *moldyn,double p_ref) {
88 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
93 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
95 moldyn->pt_scale=(ptype|ttype);
99 printf("[moldyn] p/t scaling:\n");
101 printf(" p: %s",ptype?"yes":"no ");
103 printf(" | type: %02x | factor: %f",ptype,ptc);
106 printf(" t: %s",ttype?"yes":"no ");
108 printf(" | type: %02x | factor: %f",ttype,ttc);
114 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
120 moldyn->volume=x*y*z;
128 moldyn->dv=0.000001*moldyn->volume;
130 printf("[moldyn] dimensions in A and A^3 respectively:\n");
131 printf(" x: %f\n",moldyn->dim.x);
132 printf(" y: %f\n",moldyn->dim.y);
133 printf(" z: %f\n",moldyn->dim.z);
134 printf(" volume: %f\n",moldyn->volume);
135 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
136 printf(" delta volume (pressure calc): %f\n",moldyn->dv);
141 int set_nn_dist(t_moldyn *moldyn,double dist) {
148 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
150 printf("[moldyn] periodic boundary conditions:\n");
153 moldyn->status|=MOLDYN_STAT_PBX;
156 moldyn->status|=MOLDYN_STAT_PBY;
159 moldyn->status|=MOLDYN_STAT_PBZ;
161 printf(" x: %s\n",x?"yes":"no");
162 printf(" y: %s\n",y?"yes":"no");
163 printf(" z: %s\n",z?"yes":"no");
168 int set_potential1b(t_moldyn *moldyn,pf_func1b func) {
175 int set_potential2b(t_moldyn *moldyn,pf_func2b func) {
182 int set_potential3b_j1(t_moldyn *moldyn,pf_func2b func) {
184 moldyn->func3b_j1=func;
189 int set_potential3b_j2(t_moldyn *moldyn,pf_func2b func) {
191 moldyn->func3b_j2=func;
196 int set_potential3b_j3(t_moldyn *moldyn,pf_func2b func) {
198 moldyn->func3b_j3=func;
203 int set_potential3b_k1(t_moldyn *moldyn,pf_func3b func) {
205 moldyn->func3b_k1=func;
210 int set_potential3b_k2(t_moldyn *moldyn,pf_func3b func) {
212 moldyn->func3b_k2=func;
217 int set_potential_params(t_moldyn *moldyn,void *params) {
219 moldyn->pot_params=params;
224 int set_avg_skip(t_moldyn *moldyn,int skip) {
226 printf("[moldyn] skip %d steps before starting average calc\n",skip);
227 moldyn->avg_skip=skip;
232 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
234 strncpy(moldyn->vlsdir,dir,127);
239 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
241 strncpy(moldyn->rauthor,author,63);
242 strncpy(moldyn->rtitle,title,63);
247 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
252 printf("[moldyn] set log: ");
255 case LOG_TOTAL_ENERGY:
256 moldyn->ewrite=timer;
257 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
258 moldyn->efd=open(filename,
259 O_WRONLY|O_CREAT|O_EXCL,
262 perror("[moldyn] energy log fd open");
265 dprintf(moldyn->efd,"# total energy log file\n");
266 printf("total energy (%d)\n",timer);
268 case LOG_TOTAL_MOMENTUM:
269 moldyn->mwrite=timer;
270 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
271 moldyn->mfd=open(filename,
272 O_WRONLY|O_CREAT|O_EXCL,
275 perror("[moldyn] momentum log fd open");
278 dprintf(moldyn->efd,"# total momentum log file\n");
279 printf("total momentum (%d)\n",timer);
282 moldyn->pwrite=timer;
283 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
284 moldyn->pfd=open(filename,
285 O_WRONLY|O_CREAT|O_EXCL,
288 perror("[moldyn] pressure log file\n");
291 dprintf(moldyn->pfd,"# pressure log file\n");
292 printf("pressure (%d)\n",timer);
294 case LOG_TEMPERATURE:
295 moldyn->twrite=timer;
296 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
297 moldyn->tfd=open(filename,
298 O_WRONLY|O_CREAT|O_EXCL,
301 perror("[moldyn] temperature log file\n");
304 dprintf(moldyn->tfd,"# temperature log file\n");
305 printf("temperature (%d)\n",timer);
308 moldyn->swrite=timer;
309 printf("save file (%d)\n",timer);
312 moldyn->vwrite=timer;
313 ret=visual_init(&(moldyn->vis),moldyn->vlsdir);
315 printf("[moldyn] visual init failure\n");
318 printf("visual file (%d)\n",timer);
321 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
322 moldyn->rfd=open(filename,
323 O_WRONLY|O_CREAT|O_EXCL,
326 perror("[moldyn] report fd open");
329 printf("report -> ");
331 snprintf(filename,127,"%s/e_plot.scr",
333 moldyn->epfd=open(filename,
334 O_WRONLY|O_CREAT|O_EXCL,
337 perror("[moldyn] energy plot fd open");
340 dprintf(moldyn->epfd,e_plot_script);
345 snprintf(filename,127,"%s/pressure_plot.scr",
347 moldyn->ppfd=open(filename,
348 O_WRONLY|O_CREAT|O_EXCL,
351 perror("[moldyn] p plot fd open");
354 dprintf(moldyn->ppfd,pressure_plot_script);
359 snprintf(filename,127,"%s/temperature_plot.scr",
361 moldyn->tpfd=open(filename,
362 O_WRONLY|O_CREAT|O_EXCL,
365 perror("[moldyn] t plot fd open");
368 dprintf(moldyn->tpfd,temperature_plot_script);
370 printf("temperature ");
372 dprintf(moldyn->rfd,report_start,
373 moldyn->rauthor,moldyn->rtitle);
377 printf("unknown log type: %02x\n",type);
384 int moldyn_log_shutdown(t_moldyn *moldyn) {
388 printf("[moldyn] log shutdown\n");
392 dprintf(moldyn->rfd,report_energy);
393 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
398 if(moldyn->mfd) close(moldyn->mfd);
402 dprintf(moldyn->rfd,report_pressure);
403 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
410 dprintf(moldyn->rfd,report_temperature);
411 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
416 dprintf(moldyn->rfd,report_end);
418 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
421 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
424 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
428 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
434 * creating lattice functions
437 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
438 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
449 /* how many atoms do we expect */
450 if(type==CUBIC) new*=1;
451 if(type==FCC) new*=4;
452 if(type==DIAMOND) new*=8;
454 /* allocate space for atoms */
455 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
457 perror("[moldyn] realloc (create lattice)");
461 atom=&(moldyn->atom[count]);
463 /* no atoms on the boundaries (only reason: it looks better!) */
477 set_nn_dist(moldyn,lc);
478 ret=cubic_init(a,b,c,lc,atom,&orig);
482 v3_scale(&orig,&orig,0.5);
483 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
484 ret=fcc_init(a,b,c,lc,atom,&orig);
488 v3_scale(&orig,&orig,0.25);
489 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
490 ret=diamond_init(a,b,c,lc,atom,&orig);
493 printf("unknown lattice type (%02x)\n",type);
499 printf("[moldyn] creating lattice failed\n");
500 printf(" amount of atoms\n");
501 printf(" - expected: %d\n",new);
502 printf(" - created: %d\n",ret);
507 printf("[moldyn] created lattice with %d atoms\n",new);
509 for(ret=0;ret<new;ret++) {
510 atom[ret].element=element;
513 atom[ret].brand=brand;
514 atom[ret].tag=count+ret;
515 check_per_bound(moldyn,&(atom[ret].r));
516 atom[ret].r_0=atom[ret].r;
519 /* update total system mass */
520 total_mass_calc(moldyn);
525 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
526 t_3dvec *r,t_3dvec *v) {
533 count=(moldyn->count)++;
535 ptr=realloc(atom,(count+1)*sizeof(t_atom));
537 perror("[moldyn] realloc (add atom)");
545 atom[count].element=element;
546 atom[count].mass=mass;
547 atom[count].brand=brand;
548 atom[count].tag=count;
549 atom[count].attr=attr;
550 check_per_bound(moldyn,&(atom[count].r));
551 atom[count].r_0=atom[count].r;
553 /* update total system mass */
554 total_mass_calc(moldyn);
560 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
579 v3_copy(&(atom[count].r),&r);
588 for(i=0;i<count;i++) {
589 atom[i].r.x-=(a*lc)/2.0;
590 atom[i].r.y-=(b*lc)/2.0;
591 atom[i].r.z-=(c*lc)/2.0;
597 /* fcc lattice init */
598 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
611 /* construct the basis */
612 memset(basis,0,3*sizeof(t_3dvec));
620 /* fill up the room */
628 v3_copy(&(atom[count].r),&r);
631 /* the three face centered atoms */
633 v3_add(&n,&r,&basis[l]);
634 v3_copy(&(atom[count].r),&n);
643 /* coordinate transformation */
644 for(i=0;i<count;i++) {
645 atom[i].r.x-=(a*lc)/2.0;
646 atom[i].r.y-=(b*lc)/2.0;
647 atom[i].r.z-=(c*lc)/2.0;
653 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
658 count=fcc_init(a,b,c,lc,atom,origin);
664 if(origin) v3_add(&o,&o,origin);
666 count+=fcc_init(a,b,c,lc,&atom[count],&o);
671 int destroy_atoms(t_moldyn *moldyn) {
673 if(moldyn->atom) free(moldyn->atom);
678 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
681 * - gaussian distribution of velocities
682 * - zero total momentum
683 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
688 t_3dvec p_total,delta;
693 random=&(moldyn->random);
695 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
697 /* gaussian distribution of velocities */
699 for(i=0;i<moldyn->count;i++) {
700 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
702 v=sigma*rand_get_gauss(random);
704 p_total.x+=atom[i].mass*v;
706 v=sigma*rand_get_gauss(random);
708 p_total.y+=atom[i].mass*v;
710 v=sigma*rand_get_gauss(random);
712 p_total.z+=atom[i].mass*v;
715 /* zero total momentum */
716 v3_scale(&p_total,&p_total,1.0/moldyn->count);
717 for(i=0;i<moldyn->count;i++) {
718 v3_scale(&delta,&p_total,1.0/atom[i].mass);
719 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
722 /* velocity scaling */
723 scale_velocity(moldyn,equi_init);
728 double total_mass_calc(t_moldyn *moldyn) {
734 for(i=0;i<moldyn->count;i++)
735 moldyn->mass+=moldyn->atom[i].mass;
740 double temperature_calc(t_moldyn *moldyn) {
742 /* assume up to date kinetic energy, which is 3/2 N k_B T */
744 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
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 virial_sum(t_moldyn *moldyn) {
825 /* virial (sum over atom virials) */
827 for(i=0;i<moldyn->count;i++) {
828 virial=&(moldyn->atom[i].virial);
829 v+=(virial->xx+virial->yy+virial->zz);
833 /* global virial (absolute coordinates) */
834 virial=&(moldyn->gvir);
835 moldyn->gv=virial->xx+virial->yy+virial->zz;
837 return moldyn->virial;
840 double pressure_calc(t_moldyn *moldyn) {
844 * with W = 1/3 sum_i f_i r_i (- skipped!)
845 * virial = sum_i f_i r_i
847 * => P = (2 Ekin + virial) / (3V)
850 /* assume up to date virial & up to date kinetic energy */
852 /* pressure (atom virials) */
853 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
854 moldyn->p/=(3.0*moldyn->volume);
856 /* pressure (absolute coordinates) */
857 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
858 moldyn->gp/=(3.0*moldyn->volume);
863 int average_and_fluctuation_calc(t_moldyn *moldyn) {
865 if(moldyn->total_steps<moldyn->avg_skip)
868 int denom=moldyn->total_steps+1-moldyn->avg_skip;
870 /* assume up to date energies, temperature, pressure etc */
873 moldyn->k_sum+=moldyn->ekin;
874 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
875 moldyn->k_avg=moldyn->k_sum/denom;
876 moldyn->k2_avg=moldyn->k2_sum/denom;
877 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
879 /* potential energy */
880 moldyn->v_sum+=moldyn->energy;
881 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
882 moldyn->v_avg=moldyn->v_sum/denom;
883 moldyn->v2_avg=moldyn->v2_sum/denom;
884 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
887 moldyn->t_sum+=moldyn->t;
888 moldyn->t_avg=moldyn->t_sum/denom;
891 moldyn->virial_sum+=moldyn->virial;
892 moldyn->virial_avg=moldyn->virial_sum/denom;
893 moldyn->gv_sum+=moldyn->gv;
894 moldyn->gv_avg=moldyn->gv_sum/denom;
897 moldyn->p_sum+=moldyn->p;
898 moldyn->p_avg=moldyn->p_sum/denom;
899 moldyn->gp_sum+=moldyn->gp;
900 moldyn->gp_avg=moldyn->gp_sum/denom;
905 int get_heat_capacity(t_moldyn *moldyn) {
909 /* averages needed for heat capacity calc */
910 if(moldyn->total_steps<moldyn->avg_skip)
913 /* (temperature average)^2 */
914 temp2=moldyn->t_avg*moldyn->t_avg;
915 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
918 /* ideal gas contribution */
919 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
920 printf(" ideal gas contribution: %f\n",
921 ighc/moldyn->mass*KILOGRAM/JOULE);
923 /* specific heat for nvt ensemble */
924 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
925 moldyn->c_v_nvt/=moldyn->mass;
927 /* specific heat for nve ensemble */
928 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
929 moldyn->c_v_nve/=moldyn->mass;
931 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
932 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
933 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)));
938 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
941 double u_up,u_down,dv;
953 dv=8*scale*scale*scale*moldyn->volume;
955 store=malloc(moldyn->count*sizeof(t_atom));
957 printf("[moldyn] allocating store mem failed\n");
961 /* save unscaled potential energy + atom/dim configuration */
962 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
965 /* scale up dimension and atom positions */
966 scale_dim(moldyn,SCALE_UP,scale,TRUE,TRUE,TRUE);
967 scale_atoms(moldyn,SCALE_UP,scale,TRUE,TRUE,TRUE);
968 link_cell_shutdown(moldyn);
969 link_cell_init(moldyn,QUIET);
970 potential_force_calc(moldyn);
973 /* restore atomic configuration + dim */
974 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
977 /* scale down dimension and atom positions */
978 scale_dim(moldyn,SCALE_DOWN,scale,TRUE,TRUE,TRUE);
979 scale_atoms(moldyn,SCALE_DOWN,scale,TRUE,TRUE,TRUE);
980 link_cell_shutdown(moldyn);
981 link_cell_init(moldyn,QUIET);
982 potential_force_calc(moldyn);
983 u_down=moldyn->energy;
985 /* calculate pressure */
987 printf("-------> %.10f %.10f %f\n",u_up/EV/moldyn->count,u_down/EV/moldyn->count,p/BAR);
989 /* restore atomic configuration + dim */
990 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
994 potential_force_calc(moldyn);
996 link_cell_shutdown(moldyn);
997 link_cell_init(moldyn,QUIET);
1002 double get_pressure(t_moldyn *moldyn) {
1008 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1020 if(x) dim->x*=scale;
1021 if(y) dim->y*=scale;
1022 if(z) dim->z*=scale;
1027 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1038 for(i=0;i<moldyn->count;i++) {
1039 r=&(moldyn->atom[i].r);
1048 int scale_volume(t_moldyn *moldyn) {
1054 vdim=&(moldyn->vis.dim);
1058 /* scaling factor */
1059 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1060 scale=1.0-(moldyn->p_ref-moldyn->p)/moldyn->p_tc;
1061 scale=pow(scale,ONE_THIRD);
1064 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1066 moldyn->debug=scale;
1068 /* scale the atoms and dimensions */
1069 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1070 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1072 /* visualize dimensions */
1079 /* recalculate scaled volume */
1080 moldyn->volume=dim->x*dim->y*dim->z;
1082 /* adjust/reinit linkcell */
1083 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1084 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1085 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1086 link_cell_shutdown(moldyn);
1087 link_cell_init(moldyn,QUIET);
1098 double e_kin_calc(t_moldyn *moldyn) {
1106 for(i=0;i<moldyn->count;i++) {
1107 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1108 moldyn->ekin+=atom[i].ekin;
1111 return moldyn->ekin;
1114 double get_total_energy(t_moldyn *moldyn) {
1116 return(moldyn->ekin+moldyn->energy);
1119 t_3dvec get_total_p(t_moldyn *moldyn) {
1128 for(i=0;i<moldyn->count;i++) {
1129 v3_scale(&p,&(atom[i].v),atom[i].mass);
1130 v3_add(&p_total,&p_total,&p);
1136 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1140 /* nn_dist is the nearest neighbour distance */
1142 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1151 /* linked list / cell method */
1153 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1160 /* partitioning the md cell */
1161 lc->nx=moldyn->dim.x/moldyn->cutoff;
1162 lc->x=moldyn->dim.x/lc->nx;
1163 lc->ny=moldyn->dim.y/moldyn->cutoff;
1164 lc->y=moldyn->dim.y/lc->ny;
1165 lc->nz=moldyn->dim.z/moldyn->cutoff;
1166 lc->z=moldyn->dim.z/lc->nz;
1168 lc->cells=lc->nx*lc->ny*lc->nz;
1169 lc->subcell=malloc(lc->cells*sizeof(t_list));
1172 printf("[moldyn] FATAL: less then 27 subcells!\n");
1175 printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
1176 printf(" x: %d x %f A\n",lc->nx,lc->x);
1177 printf(" y: %d x %f A\n",lc->ny,lc->y);
1178 printf(" z: %d x %f A\n",lc->nz,lc->z);
1181 for(i=0;i<lc->cells;i++)
1182 list_init_f(&(lc->subcell[i]));
1184 link_cell_update(moldyn);
1189 int link_cell_update(t_moldyn *moldyn) {
1207 for(i=0;i<lc->cells;i++)
1208 list_destroy_f(&(lc->subcell[i]));
1210 for(count=0;count<moldyn->count;count++) {
1211 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1212 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1213 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1214 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1221 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
1239 cell[0]=lc->subcell[i+j*nx+k*a];
1240 for(ci=-1;ci<=1;ci++) {
1243 if((x<0)||(x>=nx)) {
1247 for(cj=-1;cj<=1;cj++) {
1250 if((y<0)||(y>=ny)) {
1254 for(ck=-1;ck<=1;ck++) {
1257 if((z<0)||(z>=nz)) {
1261 if(!(ci|cj|ck)) continue;
1263 cell[--count2]=lc->subcell[x+y*nx+z*a];
1266 cell[count1++]=lc->subcell[x+y*nx+z*a];
1277 int link_cell_shutdown(t_moldyn *moldyn) {
1284 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
1285 list_destroy_f(&(moldyn->lc.subcell[i]));
1292 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1296 t_moldyn_schedule *schedule;
1298 schedule=&(moldyn->schedule);
1299 count=++(schedule->total_sched);
1301 ptr=realloc(schedule->runs,count*sizeof(int));
1303 perror("[moldyn] realloc (runs)");
1307 schedule->runs[count-1]=runs;
1309 ptr=realloc(schedule->tau,count*sizeof(double));
1311 perror("[moldyn] realloc (tau)");
1315 schedule->tau[count-1]=tau;
1317 printf("[moldyn] schedule added:\n");
1318 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1324 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1326 moldyn->schedule.hook=hook;
1327 moldyn->schedule.hook_params=hook_params;
1334 * 'integration of newtons equation' - algorithms
1338 /* start the integration */
1340 int moldyn_integrate(t_moldyn *moldyn) {
1343 unsigned int e,m,s,v,p,t;
1345 t_moldyn_schedule *sched;
1350 double energy_scale;
1353 sched=&(moldyn->schedule);
1356 /* initialize linked cell method */
1357 link_cell_init(moldyn,VERBOSE);
1359 /* logging & visualization */
1367 /* sqaure of some variables */
1368 moldyn->tau_square=moldyn->tau*moldyn->tau;
1369 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
1371 /* energy scaling factor */
1372 energy_scale=moldyn->count*EV;
1374 /* calculate initial forces */
1375 potential_force_calc(moldyn);
1380 /* some stupid checks before we actually start calculating bullshit */
1381 if(moldyn->cutoff>0.5*moldyn->dim.x)
1382 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
1383 if(moldyn->cutoff>0.5*moldyn->dim.y)
1384 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
1385 if(moldyn->cutoff>0.5*moldyn->dim.z)
1386 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
1387 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1388 if(ds>0.05*moldyn->nnd)
1389 printf("[moldyn] warning: forces too high / tau too small!\n");
1391 /* zero absolute time */
1393 moldyn->total_steps=0;
1395 /* debugging, ignore */
1398 /* tell the world */
1399 printf("[moldyn] integration start, go get a coffee ...\n");
1401 /* executing the schedule */
1403 while(sched->count<sched->total_sched) {
1405 /* setting amount of runs and finite time step size */
1406 moldyn->tau=sched->tau[sched->count];
1407 moldyn->tau_square=moldyn->tau*moldyn->tau;
1408 moldyn->time_steps=sched->runs[sched->count];
1410 /* integration according to schedule */
1412 for(i=0;i<moldyn->time_steps;i++) {
1414 /* integration step */
1415 moldyn->integrate(moldyn);
1417 /* calculate kinetic energy, temperature and pressure */
1419 temperature_calc(moldyn);
1421 pressure_calc(moldyn);
1422 average_and_fluctuation_calc(moldyn);
1425 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1426 scale_velocity(moldyn,FALSE);
1427 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1428 scale_volume(moldyn);
1430 /* check for log & visualization */
1433 dprintf(moldyn->efd,
1435 moldyn->time,moldyn->ekin/energy_scale,
1436 moldyn->energy/energy_scale,
1437 get_total_energy(moldyn)/energy_scale);
1441 momentum=get_total_p(moldyn);
1442 dprintf(moldyn->mfd,
1443 "%f %f %f %f %f\n",moldyn->time,
1444 momentum.x,momentum.y,momentum.z,
1445 v3_norm(&momentum));
1450 dprintf(moldyn->pfd,
1451 "%f %f %f %f %f\n",moldyn->time,
1452 moldyn->p/BAR,moldyn->p_avg/BAR,
1453 moldyn->gp/BAR,moldyn->gp_avg/BAR);
1458 dprintf(moldyn->tfd,
1460 moldyn->time,moldyn->t,moldyn->t_avg);
1465 snprintf(dir,128,"%s/s-%07.f.save",
1466 moldyn->vlsdir,moldyn->time);
1467 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT);
1468 if(fd<0) perror("[moldyn] save fd open");
1470 write(fd,moldyn,sizeof(t_moldyn));
1471 write(fd,moldyn->atom,
1472 moldyn->count*sizeof(t_atom));
1479 visual_atoms(&(moldyn->vis),moldyn->time,
1480 moldyn->atom,moldyn->count);
1484 /* display progress */
1486 printf("\rsched:%d, steps:%d, T:%3.1f/%3.1f P:%4.1f/%4.1f V:%6.1f",
1488 moldyn->t,moldyn->t_avg,
1489 moldyn->p_avg/BAR,moldyn->gp_avg/BAR,
1494 /* increase absolute time */
1495 moldyn->time+=moldyn->tau;
1496 moldyn->total_steps+=1;
1500 /* check for hooks */
1502 printf("\n ## schedule hook %d/%d start ##\n",
1503 sched->count+1,sched->total_sched-1);
1504 sched->hook(moldyn,sched->hook_params);
1505 printf(" ## schedule hook end ##\n");
1508 /* increase the schedule counter */
1516 /* velocity verlet */
1518 int velocity_verlet(t_moldyn *moldyn) {
1521 double tau,tau_square,h;
1526 count=moldyn->count;
1528 tau_square=moldyn->tau_square;
1530 for(i=0;i<count;i++) {
1533 v3_scale(&delta,&(atom[i].v),tau);
1534 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1535 v3_scale(&delta,&(atom[i].f),h*tau_square);
1536 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1537 check_per_bound(moldyn,&(atom[i].r));
1539 /* velocities [actually v(t+tau/2)] */
1540 v3_scale(&delta,&(atom[i].f),h*tau);
1541 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1544 /* neighbour list update */
1545 link_cell_update(moldyn);
1547 /* forces depending on chosen potential */
1548 potential_force_calc(moldyn);
1550 for(i=0;i<count;i++) {
1551 /* again velocities [actually v(t+tau)] */
1552 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1553 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1562 * potentials & corresponding forces & virial routine
1566 /* generic potential and force calculation */
1568 int potential_force_calc(t_moldyn *moldyn) {
1571 t_atom *itom,*jtom,*ktom;
1574 t_list neighbour_i[27];
1575 t_list neighbour_i2[27];
1580 count=moldyn->count;
1587 /* reset global virial */
1588 memset(&(moldyn->gvir),0,sizeof(t_virial));
1590 /* reset force, site energy and virial of every atom */
1591 for(i=0;i<count;i++) {
1594 v3_zero(&(itom[i].f));
1597 virial=(&(itom[i].virial));
1605 /* reset site energy */
1610 /* get energy, force and virial of every atom */
1612 /* first (and only) loop over atoms i */
1613 for(i=0;i<count;i++) {
1615 /* single particle potential/force */
1616 if(itom[i].attr&ATOM_ATTR_1BP)
1618 moldyn->func1b(moldyn,&(itom[i]));
1620 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1623 /* 2 body pair potential/force */
1625 link_cell_neighbour_index(moldyn,
1626 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1627 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1628 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1633 /* first loop over atoms j */
1634 if(moldyn->func2b) {
1637 this=&(neighbour_i[j]);
1640 if(this->start==NULL)
1646 jtom=this->current->data;
1648 if(jtom==&(itom[i]))
1651 if((jtom->attr&ATOM_ATTR_2BP)&
1652 (itom[i].attr&ATOM_ATTR_2BP)) {
1653 moldyn->func2b(moldyn,
1658 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1663 /* 3 body potential/force */
1665 if(!(itom[i].attr&ATOM_ATTR_3BP))
1668 /* copy the neighbour lists */
1669 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
1671 /* second loop over atoms j */
1674 this=&(neighbour_i[j]);
1677 if(this->start==NULL)
1683 jtom=this->current->data;
1685 if(jtom==&(itom[i]))
1688 if(!(jtom->attr&ATOM_ATTR_3BP))
1694 if(moldyn->func3b_j1)
1695 moldyn->func3b_j1(moldyn,
1700 /* in first j loop, 3bp run can be skipped */
1701 if(!(moldyn->run3bp))
1704 /* first loop over atoms k */
1705 if(moldyn->func3b_k1) {
1709 that=&(neighbour_i2[k]);
1712 if(that->start==NULL)
1719 ktom=that->current->data;
1721 if(!(ktom->attr&ATOM_ATTR_3BP))
1727 if(ktom==&(itom[i]))
1730 moldyn->func3b_k1(moldyn,
1736 } while(list_next_f(that)!=\
1743 if(moldyn->func3b_j2)
1744 moldyn->func3b_j2(moldyn,
1749 /* second loop over atoms k */
1750 if(moldyn->func3b_k2) {
1754 that=&(neighbour_i2[k]);
1757 if(that->start==NULL)
1764 ktom=that->current->data;
1766 if(!(ktom->attr&ATOM_ATTR_3BP))
1772 if(ktom==&(itom[i]))
1775 moldyn->func3b_k2(moldyn,
1781 } while(list_next_f(that)!=\
1788 /* 2bp post function */
1789 if(moldyn->func3b_j3) {
1790 moldyn->func3b_j3(moldyn,
1795 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1809 printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
1812 /* calculate global virial */
1813 for(i=0;i<count;i++) {
1814 moldyn->gvir.xx+=moldyn->atom[i].r.x*moldyn->atom[i].f.x;
1815 moldyn->gvir.yy+=moldyn->atom[i].r.y*moldyn->atom[i].f.y;
1816 moldyn->gvir.zz+=moldyn->atom[i].r.z*moldyn->atom[i].f.z;
1817 moldyn->gvir.xy+=moldyn->atom[i].r.y*moldyn->atom[i].f.x;
1818 moldyn->gvir.xz+=moldyn->atom[i].r.z*moldyn->atom[i].f.x;
1819 moldyn->gvir.yz+=moldyn->atom[i].r.z*moldyn->atom[i].f.y;
1826 * virial calculation
1829 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
1830 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
1832 a->virial.xx+=f->x*d->x;
1833 a->virial.yy+=f->y*d->y;
1834 a->virial.zz+=f->z*d->z;
1835 a->virial.xy+=f->x*d->y;
1836 a->virial.xz+=f->x*d->z;
1837 a->virial.yz+=f->y*d->z;
1843 * periodic boundary checking
1846 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1847 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1858 if(moldyn->status&MOLDYN_STAT_PBX) {
1859 if(a->x>=x) a->x-=dim->x;
1860 else if(-a->x>x) a->x+=dim->x;
1862 if(moldyn->status&MOLDYN_STAT_PBY) {
1863 if(a->y>=y) a->y-=dim->y;
1864 else if(-a->y>y) a->y+=dim->y;
1866 if(moldyn->status&MOLDYN_STAT_PBZ) {
1867 if(a->z>=z) a->z-=dim->z;
1868 else if(-a->z>z) a->z+=dim->z;
1875 * debugging / critical check functions
1878 int moldyn_bc_check(t_moldyn *moldyn) {
1891 for(i=0;i<moldyn->count;i++) {
1892 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
1893 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
1894 i,atom[i].r.x,dim->x/2);
1895 printf("diagnostic:\n");
1896 printf("-----------\natom.r.x:\n");
1898 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
1901 ((byte)&(1<<k))?1:0,
1904 printf("---------------\nx=dim.x/2:\n");
1906 memcpy(&byte,(u8 *)(&x)+j,1);
1909 ((byte)&(1<<k))?1:0,
1912 if(atom[i].r.x==x) printf("the same!\n");
1913 else printf("different!\n");
1915 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
1916 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
1917 i,atom[i].r.y,dim->y/2);
1918 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
1919 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
1920 i,atom[i].r.z,dim->z/2);
1927 * post processing functions
1930 int get_line(int fd,char *line,int max) {
1937 if(count==max) return count;
1938 ret=read(fd,line+count,1);
1939 if(ret<=0) return ret;
1940 if(line[count]=='\n') {