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 moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
223 strncpy(moldyn->vlsdir,dir,127);
228 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
230 strncpy(moldyn->rauthor,author,63);
231 strncpy(moldyn->rtitle,title,63);
236 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
241 printf("[moldyn] set log: ");
244 case LOG_TOTAL_ENERGY:
245 moldyn->ewrite=timer;
246 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
247 moldyn->efd=open(filename,
248 O_WRONLY|O_CREAT|O_EXCL,
251 perror("[moldyn] energy log fd open");
254 dprintf(moldyn->efd,"# total energy log file\n");
255 printf("total energy (%d)\n",timer);
257 case LOG_TOTAL_MOMENTUM:
258 moldyn->mwrite=timer;
259 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
260 moldyn->mfd=open(filename,
261 O_WRONLY|O_CREAT|O_EXCL,
264 perror("[moldyn] momentum log fd open");
267 dprintf(moldyn->efd,"# total momentum log file\n");
268 printf("total momentum (%d)\n",timer);
271 moldyn->pwrite=timer;
272 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
273 moldyn->pfd=open(filename,
274 O_WRONLY|O_CREAT|O_EXCL,
277 perror("[moldyn] pressure log file\n");
280 dprintf(moldyn->pfd,"# pressure log file\n");
281 printf("pressure (%d)\n",timer);
283 case LOG_TEMPERATURE:
284 moldyn->twrite=timer;
285 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
286 moldyn->tfd=open(filename,
287 O_WRONLY|O_CREAT|O_EXCL,
290 perror("[moldyn] temperature log file\n");
293 dprintf(moldyn->tfd,"# temperature log file\n");
294 printf("temperature (%d)\n",timer);
297 moldyn->swrite=timer;
298 printf("save file (%d)\n",timer);
301 moldyn->vwrite=timer;
302 ret=visual_init(&(moldyn->vis),moldyn->vlsdir);
304 printf("[moldyn] visual init failure\n");
307 printf("visual file (%d)\n",timer);
310 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
311 moldyn->rfd=open(filename,
312 O_WRONLY|O_CREAT|O_EXCL,
315 perror("[moldyn] report fd open");
318 printf("report -> ");
320 snprintf(filename,127,"%s/e_plot.scr",
322 moldyn->epfd=open(filename,
323 O_WRONLY|O_CREAT|O_EXCL,
326 perror("[moldyn] energy plot fd open");
329 dprintf(moldyn->epfd,e_plot_script);
334 snprintf(filename,127,"%s/pressure_plot.scr",
336 moldyn->ppfd=open(filename,
337 O_WRONLY|O_CREAT|O_EXCL,
340 perror("[moldyn] p plot fd open");
343 dprintf(moldyn->ppfd,pressure_plot_script);
348 snprintf(filename,127,"%s/temperature_plot.scr",
350 moldyn->tpfd=open(filename,
351 O_WRONLY|O_CREAT|O_EXCL,
354 perror("[moldyn] t plot fd open");
357 dprintf(moldyn->tpfd,temperature_plot_script);
359 printf("temperature ");
361 dprintf(moldyn->rfd,report_start,
362 moldyn->rauthor,moldyn->rtitle);
366 printf("unknown log type: %02x\n",type);
373 int moldyn_log_shutdown(t_moldyn *moldyn) {
377 printf("[moldyn] log shutdown\n");
381 dprintf(moldyn->rfd,report_energy);
382 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
387 if(moldyn->mfd) close(moldyn->mfd);
391 dprintf(moldyn->rfd,report_pressure);
392 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
399 dprintf(moldyn->rfd,report_temperature);
400 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
405 dprintf(moldyn->rfd,report_end);
407 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
410 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
413 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
417 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
423 * creating lattice functions
426 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
427 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
438 /* how many atoms do we expect */
439 if(type==CUBIC) new*=1;
440 if(type==FCC) new*=4;
441 if(type==DIAMOND) new*=8;
443 /* allocate space for atoms */
444 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
446 perror("[moldyn] realloc (create lattice)");
450 atom=&(moldyn->atom[count]);
452 /* no atoms on the boundaries (only reason: it looks better!) */
466 set_nn_dist(moldyn,lc);
467 ret=cubic_init(a,b,c,lc,atom,&orig);
471 v3_scale(&orig,&orig,0.5);
472 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
473 ret=fcc_init(a,b,c,lc,atom,&orig);
477 v3_scale(&orig,&orig,0.25);
478 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
479 ret=diamond_init(a,b,c,lc,atom,&orig);
482 printf("unknown lattice type (%02x)\n",type);
488 printf("[moldyn] creating lattice failed\n");
489 printf(" amount of atoms\n");
490 printf(" - expected: %d\n",new);
491 printf(" - created: %d\n",ret);
496 printf("[moldyn] created lattice with %d atoms\n",new);
498 for(ret=0;ret<new;ret++) {
499 atom[ret].element=element;
502 atom[ret].brand=brand;
503 atom[ret].tag=count+ret;
504 check_per_bound(moldyn,&(atom[ret].r));
507 /* update total system mass */
508 total_mass_calc(moldyn);
514 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
533 v3_copy(&(atom[count].r),&r);
542 for(i=0;i<count;i++) {
543 atom[i].r.x-=(a*lc)/2.0;
544 atom[i].r.y-=(b*lc)/2.0;
545 atom[i].r.z-=(c*lc)/2.0;
551 /* fcc lattice init */
552 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
565 /* construct the basis */
566 memset(basis,0,3*sizeof(t_3dvec));
574 /* fill up the room */
582 v3_copy(&(atom[count].r),&r);
585 /* the three face centered atoms */
587 v3_add(&n,&r,&basis[l]);
588 v3_copy(&(atom[count].r),&n);
597 /* coordinate transformation */
598 for(i=0;i<count;i++) {
599 atom[i].r.x-=(a*lc)/2.0;
600 atom[i].r.y-=(b*lc)/2.0;
601 atom[i].r.z-=(c*lc)/2.0;
607 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
612 count=fcc_init(a,b,c,lc,atom,origin);
618 if(origin) v3_add(&o,&o,origin);
620 count+=fcc_init(a,b,c,lc,&atom[count],&o);
625 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
626 t_3dvec *r,t_3dvec *v) {
633 count=(moldyn->count)++;
635 ptr=realloc(atom,(count+1)*sizeof(t_atom));
637 perror("[moldyn] realloc (add atom)");
645 atom[count].element=element;
646 atom[count].mass=mass;
647 atom[count].brand=brand;
648 atom[count].tag=count;
649 atom[count].attr=attr;
651 /* update total system mass */
652 total_mass_calc(moldyn);
657 int destroy_atoms(t_moldyn *moldyn) {
659 if(moldyn->atom) free(moldyn->atom);
664 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
667 * - gaussian distribution of velocities
668 * - zero total momentum
669 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
674 t_3dvec p_total,delta;
679 random=&(moldyn->random);
681 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
683 /* gaussian distribution of velocities */
685 for(i=0;i<moldyn->count;i++) {
686 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
688 v=sigma*rand_get_gauss(random);
690 p_total.x+=atom[i].mass*v;
692 v=sigma*rand_get_gauss(random);
694 p_total.y+=atom[i].mass*v;
696 v=sigma*rand_get_gauss(random);
698 p_total.z+=atom[i].mass*v;
701 /* zero total momentum */
702 v3_scale(&p_total,&p_total,1.0/moldyn->count);
703 for(i=0;i<moldyn->count;i++) {
704 v3_scale(&delta,&p_total,1.0/atom[i].mass);
705 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
708 /* velocity scaling */
709 scale_velocity(moldyn,equi_init);
714 double total_mass_calc(t_moldyn *moldyn) {
720 for(i=0;i<moldyn->count;i++)
721 moldyn->mass+=moldyn->atom[i].mass;
726 double temperature_calc(t_moldyn *moldyn) {
728 /* assume up to date kinetic energy, which is 3/2 N k_B T */
730 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
731 moldyn->t_sum+=moldyn->t;
732 moldyn->mean_t=moldyn->t_sum/moldyn->total_steps;
737 double get_temperature(t_moldyn *moldyn) {
742 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
752 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
755 /* get kinetic energy / temperature & count involved atoms */
758 for(i=0;i<moldyn->count;i++) {
759 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
760 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
765 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
766 else return 0; /* no atoms involved in scaling! */
768 /* (temporary) hack for e,t = 0 */
771 if(moldyn->t_ref!=0.0) {
772 thermal_init(moldyn,equi_init);
776 return 0; /* no scaling needed */
780 /* get scaling factor */
781 scale=moldyn->t_ref/moldyn->t;
785 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
786 scale=1.0+(scale-1.0)/moldyn->t_tc;
789 /* velocity scaling */
790 for(i=0;i<moldyn->count;i++) {
791 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
792 v3_scale(&(atom[i].v),&(atom[i].v),scale);
798 double ideal_gas_law_pressure(t_moldyn *moldyn) {
802 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
807 double pressure_calc(t_moldyn *moldyn) {
815 * W = 1/3 sum_i f_i r_i
816 * virial = sum_i f_i r_i
818 * => P = (2 Ekin + virial) / (3V)
822 for(i=0;i<moldyn->count;i++) {
823 virial=&(moldyn->atom[i].virial);
824 v+=(virial->xx+virial->yy+virial->zz);
827 /* virial sum and mean virial */
828 moldyn->virial_sum+=v;
829 moldyn->mean_v=moldyn->virial_sum/moldyn->total_steps;
831 /* assume up to date kinetic energy */
832 moldyn->p=2.0*moldyn->ekin+moldyn->mean_v;
833 moldyn->p/=(3.0*moldyn->volume);
834 moldyn->p_sum+=moldyn->p;
835 moldyn->mean_p=moldyn->p_sum/moldyn->total_steps;
837 /* pressure from 'absolute coordinates' virial */
838 virial=&(moldyn->virial);
839 v=virial->xx+virial->yy+virial->zz;
840 moldyn->gp=2.0*moldyn->ekin+v;
841 moldyn->gp/=(3.0*moldyn->volume);
842 moldyn->gp_sum+=moldyn->gp;
843 moldyn->mean_gp=moldyn->gp_sum/moldyn->total_steps;
848 int energy_fluctuation_calc(t_moldyn *moldyn) {
850 /* assume up to date energies */
853 moldyn->k_sum+=moldyn->ekin;
854 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
855 moldyn->k_mean=moldyn->k_sum/moldyn->total_steps;
856 moldyn->k2_mean=moldyn->k2_sum/moldyn->total_steps;
857 moldyn->dk2_mean=moldyn->k2_mean-(moldyn->k_mean*moldyn->k_mean);
859 /* potential energy */
860 moldyn->v_sum+=moldyn->energy;
861 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
862 moldyn->v_mean=moldyn->v_sum/moldyn->total_steps;
863 moldyn->v2_mean=moldyn->v2_sum/moldyn->total_steps;
864 moldyn->dv2_mean=moldyn->v2_mean-(moldyn->v_mean*moldyn->v_mean);
869 int get_heat_capacity(t_moldyn *moldyn) {
873 /* (temperature average)^2 */
874 temp2=moldyn->mean_t*moldyn->mean_t;
875 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
878 /* ideal gas contribution */
879 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
880 printf(" ideal gas contribution: %f\n",
881 ighc/moldyn->mass*KILOGRAM/JOULE);
883 /* specific heat for nvt ensemble */
884 moldyn->c_v_nvt=moldyn->dv2_mean/(K_BOLTZMANN*temp2)+ighc;
885 moldyn->c_v_nvt/=moldyn->mass;
887 /* specific heat for nve ensemble */
888 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_mean/(ighc*K_BOLTZMANN*temp2)));
889 moldyn->c_v_nve/=moldyn->mass;
891 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
892 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
897 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
900 double u_up,u_down,dv;
912 dv=8*scale*scale*scale*moldyn->volume;
914 store=malloc(moldyn->count*sizeof(t_atom));
916 printf("[moldyn] allocating store mem failed\n");
920 /* save unscaled potential energy + atom/dim configuration */
921 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
924 /* scale up dimension and atom positions */
925 scale_dim(moldyn,SCALE_UP,scale,TRUE,TRUE,TRUE);
926 scale_atoms(moldyn,SCALE_UP,scale,TRUE,TRUE,TRUE);
927 link_cell_shutdown(moldyn);
928 link_cell_init(moldyn,QUIET);
929 potential_force_calc(moldyn);
932 /* restore atomic configuration + dim */
933 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
936 /* scale down dimension and atom positions */
937 scale_dim(moldyn,SCALE_DOWN,scale,TRUE,TRUE,TRUE);
938 scale_atoms(moldyn,SCALE_DOWN,scale,TRUE,TRUE,TRUE);
939 link_cell_shutdown(moldyn);
940 link_cell_init(moldyn,QUIET);
941 potential_force_calc(moldyn);
942 u_down=moldyn->energy;
944 /* calculate pressure */
946 printf("-------> %.10f %.10f %f\n",u_up/EV/moldyn->count,u_down/EV/moldyn->count,p/BAR);
948 /* restore atomic configuration + dim */
949 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
953 potential_force_calc(moldyn);
955 link_cell_shutdown(moldyn);
956 link_cell_init(moldyn,QUIET);
961 double get_pressure(t_moldyn *moldyn) {
967 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
986 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
997 for(i=0;i<moldyn->count;i++) {
998 r=&(moldyn->atom[i].r);
1007 int scale_volume(t_moldyn *moldyn) {
1013 vdim=&(moldyn->vis.dim);
1017 /* scaling factor */
1018 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1019 scale=1.0-(moldyn->p_ref-moldyn->p)/moldyn->p_tc;
1020 scale=pow(scale,ONE_THIRD);
1023 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1025 moldyn->debug=scale;
1027 /* scale the atoms and dimensions */
1028 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1029 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1031 /* visualize dimensions */
1038 /* recalculate scaled volume */
1039 moldyn->volume=dim->x*dim->y*dim->z;
1041 /* adjust/reinit linkcell */
1042 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1043 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1044 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1045 link_cell_shutdown(moldyn);
1046 link_cell_init(moldyn,QUIET);
1057 double e_kin_calc(t_moldyn *moldyn) {
1065 for(i=0;i<moldyn->count;i++)
1066 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1068 return moldyn->ekin;
1071 double get_total_energy(t_moldyn *moldyn) {
1073 return(moldyn->ekin+moldyn->energy);
1076 t_3dvec get_total_p(t_moldyn *moldyn) {
1085 for(i=0;i<moldyn->count;i++) {
1086 v3_scale(&p,&(atom[i].v),atom[i].mass);
1087 v3_add(&p_total,&p_total,&p);
1093 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1097 /* nn_dist is the nearest neighbour distance */
1099 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1108 /* linked list / cell method */
1110 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1117 /* partitioning the md cell */
1118 lc->nx=moldyn->dim.x/moldyn->cutoff;
1119 lc->x=moldyn->dim.x/lc->nx;
1120 lc->ny=moldyn->dim.y/moldyn->cutoff;
1121 lc->y=moldyn->dim.y/lc->ny;
1122 lc->nz=moldyn->dim.z/moldyn->cutoff;
1123 lc->z=moldyn->dim.z/lc->nz;
1125 lc->cells=lc->nx*lc->ny*lc->nz;
1126 lc->subcell=malloc(lc->cells*sizeof(t_list));
1129 printf("[moldyn] FATAL: less then 27 subcells!\n");
1132 printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
1133 printf(" x: %d x %f A\n",lc->nx,lc->x);
1134 printf(" y: %d x %f A\n",lc->ny,lc->y);
1135 printf(" z: %d x %f A\n",lc->nz,lc->z);
1138 for(i=0;i<lc->cells;i++)
1139 list_init_f(&(lc->subcell[i]));
1141 link_cell_update(moldyn);
1146 int link_cell_update(t_moldyn *moldyn) {
1164 for(i=0;i<lc->cells;i++)
1165 list_destroy_f(&(lc->subcell[i]));
1167 for(count=0;count<moldyn->count;count++) {
1168 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1169 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1170 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1171 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1178 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
1196 cell[0]=lc->subcell[i+j*nx+k*a];
1197 for(ci=-1;ci<=1;ci++) {
1200 if((x<0)||(x>=nx)) {
1204 for(cj=-1;cj<=1;cj++) {
1207 if((y<0)||(y>=ny)) {
1211 for(ck=-1;ck<=1;ck++) {
1214 if((z<0)||(z>=nz)) {
1218 if(!(ci|cj|ck)) continue;
1220 cell[--count2]=lc->subcell[x+y*nx+z*a];
1223 cell[count1++]=lc->subcell[x+y*nx+z*a];
1234 int link_cell_shutdown(t_moldyn *moldyn) {
1241 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
1242 list_destroy_f(&(moldyn->lc.subcell[i]));
1249 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1253 t_moldyn_schedule *schedule;
1255 schedule=&(moldyn->schedule);
1256 count=++(schedule->total_sched);
1258 ptr=realloc(schedule->runs,count*sizeof(int));
1260 perror("[moldyn] realloc (runs)");
1264 schedule->runs[count-1]=runs;
1266 ptr=realloc(schedule->tau,count*sizeof(double));
1268 perror("[moldyn] realloc (tau)");
1272 schedule->tau[count-1]=tau;
1274 printf("[moldyn] schedule added:\n");
1275 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1281 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1283 moldyn->schedule.hook=hook;
1284 moldyn->schedule.hook_params=hook_params;
1291 * 'integration of newtons equation' - algorithms
1295 /* start the integration */
1297 int moldyn_integrate(t_moldyn *moldyn) {
1300 unsigned int e,m,s,v,p,t;
1302 t_moldyn_schedule *sched;
1307 double energy_scale;
1310 sched=&(moldyn->schedule);
1313 /* initialize linked cell method */
1314 link_cell_init(moldyn,VERBOSE);
1316 /* logging & visualization */
1324 /* sqaure of some variables */
1325 moldyn->tau_square=moldyn->tau*moldyn->tau;
1326 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
1328 /* energy scaling factor */
1329 energy_scale=moldyn->count*EV;
1331 /* calculate initial forces */
1332 potential_force_calc(moldyn);
1337 /* some stupid checks before we actually start calculating bullshit */
1338 if(moldyn->cutoff>0.5*moldyn->dim.x)
1339 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
1340 if(moldyn->cutoff>0.5*moldyn->dim.y)
1341 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
1342 if(moldyn->cutoff>0.5*moldyn->dim.z)
1343 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
1344 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1345 if(ds>0.05*moldyn->nnd)
1346 printf("[moldyn] warning: forces too high / tau too small!\n");
1348 /* zero absolute time */
1350 moldyn->total_steps=0;
1352 /* debugging, ignore */
1355 /* tell the world */
1356 printf("[moldyn] integration start, go get a coffee ...\n");
1358 /* executing the schedule */
1359 for(sched->count=0;sched->count<sched->total_sched;sched->count++) {
1361 /* setting amount of runs and finite time step size */
1362 moldyn->tau=sched->tau[sched->count];
1363 moldyn->tau_square=moldyn->tau*moldyn->tau;
1364 moldyn->time_steps=sched->runs[sched->count];
1366 /* integration according to schedule */
1368 for(i=0;i<moldyn->time_steps;i++) {
1370 /* integration step */
1371 moldyn->integrate(moldyn);
1373 /* calculate kinetic energy, temperature and pressure */
1375 temperature_calc(moldyn);
1376 pressure_calc(moldyn);
1377 energy_fluctuation_calc(moldyn);
1378 //tp=thermodynamic_pressure_calc(moldyn);
1379 //printf("thermodynamic p: %f\n",thermodynamic_pressure_calc(moldyn)/BAR);
1382 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1383 scale_velocity(moldyn,FALSE);
1384 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1385 scale_volume(moldyn);
1387 /* check for log & visualization */
1390 dprintf(moldyn->efd,
1392 moldyn->time,moldyn->ekin/energy_scale,
1393 moldyn->energy/energy_scale,
1394 get_total_energy(moldyn)/energy_scale);
1398 momentum=get_total_p(moldyn);
1399 dprintf(moldyn->mfd,
1400 "%f %f %f %f %f\n",moldyn->time,
1401 momentum.x,momentum.y,momentum.z,
1402 v3_norm(&momentum));
1407 dprintf(moldyn->pfd,
1408 "%f %f %f %f %f\n",moldyn->time,
1409 moldyn->p/BAR,moldyn->mean_p/BAR,
1410 moldyn->gp/BAR,moldyn->mean_gp/BAR);
1415 dprintf(moldyn->tfd,
1417 moldyn->time,moldyn->t,moldyn->mean_t);
1422 snprintf(dir,128,"%s/s-%07.f.save",
1423 moldyn->vlsdir,moldyn->time);
1424 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT);
1425 if(fd<0) perror("[moldyn] save fd open");
1427 write(fd,moldyn,sizeof(t_moldyn));
1428 write(fd,moldyn->atom,
1429 moldyn->count*sizeof(t_atom));
1436 visual_atoms(&(moldyn->vis),moldyn->time,
1437 moldyn->atom,moldyn->count);
1441 /* display progress */
1443 printf("\rsched: %d, steps: %d, T: %f, P: %f %f V: %f",
1447 moldyn->mean_gp/BAR,
1451 get_heat_capacity(moldyn);
1454 /* increase absolute time */
1455 moldyn->time+=moldyn->tau;
1456 moldyn->total_steps+=1;
1460 /* check for hooks */
1461 if(sched->count+1<sched->total_sched)
1463 sched->hook(moldyn,sched->hook_params);
1465 /* get a new info line */
1473 /* velocity verlet */
1475 int velocity_verlet(t_moldyn *moldyn) {
1478 double tau,tau_square,h;
1483 count=moldyn->count;
1485 tau_square=moldyn->tau_square;
1487 for(i=0;i<count;i++) {
1490 v3_scale(&delta,&(atom[i].v),tau);
1491 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1492 v3_scale(&delta,&(atom[i].f),h*tau_square);
1493 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1494 check_per_bound(moldyn,&(atom[i].r));
1496 /* velocities [actually v(t+tau/2)] */
1497 v3_scale(&delta,&(atom[i].f),h*tau);
1498 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1501 /* neighbour list update */
1502 link_cell_update(moldyn);
1504 /* forces depending on chosen potential */
1505 potential_force_calc(moldyn);
1507 for(i=0;i<count;i++) {
1508 /* again velocities [actually v(t+tau)] */
1509 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1510 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1519 * potentials & corresponding forces & virial routine
1523 /* generic potential and force calculation */
1525 int potential_force_calc(t_moldyn *moldyn) {
1528 t_atom *itom,*jtom,*ktom;
1531 t_list neighbour_i[27];
1532 t_list neighbour_i2[27];
1537 count=moldyn->count;
1544 /* reset global virial */
1545 memset(&(moldyn->virial),0,sizeof(t_virial));
1547 /* reset force, site energy and virial of every atom */
1548 for(i=0;i<count;i++) {
1551 v3_zero(&(itom[i].f));
1554 virial=(&(itom[i].virial));
1562 /* reset site energy */
1567 /* get energy, force and virial of every atom */
1569 /* first (and only) loop over atoms i */
1570 for(i=0;i<count;i++) {
1572 /* single particle potential/force */
1573 if(itom[i].attr&ATOM_ATTR_1BP)
1575 moldyn->func1b(moldyn,&(itom[i]));
1577 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1580 /* 2 body pair potential/force */
1582 link_cell_neighbour_index(moldyn,
1583 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1584 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1585 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1590 /* first loop over atoms j */
1591 if(moldyn->func2b) {
1594 this=&(neighbour_i[j]);
1597 if(this->start==NULL)
1603 jtom=this->current->data;
1605 if(jtom==&(itom[i]))
1608 if((jtom->attr&ATOM_ATTR_2BP)&
1609 (itom[i].attr&ATOM_ATTR_2BP)) {
1610 moldyn->func2b(moldyn,
1615 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1620 /* 3 body potential/force */
1622 if(!(itom[i].attr&ATOM_ATTR_3BP))
1625 /* copy the neighbour lists */
1626 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
1628 /* second loop over atoms j */
1631 this=&(neighbour_i[j]);
1634 if(this->start==NULL)
1640 jtom=this->current->data;
1642 if(jtom==&(itom[i]))
1645 if(!(jtom->attr&ATOM_ATTR_3BP))
1651 if(moldyn->func3b_j1)
1652 moldyn->func3b_j1(moldyn,
1657 /* in first j loop, 3bp run can be skipped */
1658 if(!(moldyn->run3bp))
1661 /* first loop over atoms k */
1662 if(moldyn->func3b_k1) {
1666 that=&(neighbour_i2[k]);
1669 if(that->start==NULL)
1676 ktom=that->current->data;
1678 if(!(ktom->attr&ATOM_ATTR_3BP))
1684 if(ktom==&(itom[i]))
1687 moldyn->func3b_k1(moldyn,
1693 } while(list_next_f(that)!=\
1700 if(moldyn->func3b_j2)
1701 moldyn->func3b_j2(moldyn,
1706 /* second loop over atoms k */
1707 if(moldyn->func3b_k2) {
1711 that=&(neighbour_i2[k]);
1714 if(that->start==NULL)
1721 ktom=that->current->data;
1723 if(!(ktom->attr&ATOM_ATTR_3BP))
1729 if(ktom==&(itom[i]))
1732 moldyn->func3b_k2(moldyn,
1738 } while(list_next_f(that)!=\
1745 /* 2bp post function */
1746 if(moldyn->func3b_j3) {
1747 moldyn->func3b_j3(moldyn,
1752 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1766 printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
1769 /* calculate global virial */
1770 for(i=0;i<count;i++) {
1771 moldyn->virial.xx+=moldyn->atom[i].r.x*moldyn->atom[i].f.x;
1772 moldyn->virial.yy+=moldyn->atom[i].r.y*moldyn->atom[i].f.y;
1773 moldyn->virial.zz+=moldyn->atom[i].r.z*moldyn->atom[i].f.z;
1774 moldyn->virial.xy+=moldyn->atom[i].r.y*moldyn->atom[i].f.x;
1775 moldyn->virial.xz+=moldyn->atom[i].r.z*moldyn->atom[i].f.x;
1776 moldyn->virial.yz+=moldyn->atom[i].r.z*moldyn->atom[i].f.y;
1783 * virial calculation
1786 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
1787 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
1789 a->virial.xx+=f->x*d->x;
1790 a->virial.yy+=f->y*d->y;
1791 a->virial.zz+=f->z*d->z;
1792 a->virial.xy+=f->x*d->y;
1793 a->virial.xz+=f->x*d->z;
1794 a->virial.yz+=f->y*d->z;
1800 * periodic boundary checking
1803 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1804 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1815 if(moldyn->status&MOLDYN_STAT_PBX) {
1816 if(a->x>=x) a->x-=dim->x;
1817 else if(-a->x>x) a->x+=dim->x;
1819 if(moldyn->status&MOLDYN_STAT_PBY) {
1820 if(a->y>=y) a->y-=dim->y;
1821 else if(-a->y>y) a->y+=dim->y;
1823 if(moldyn->status&MOLDYN_STAT_PBZ) {
1824 if(a->z>=z) a->z-=dim->z;
1825 else if(-a->z>z) a->z+=dim->z;
1832 * debugging / critical check functions
1835 int moldyn_bc_check(t_moldyn *moldyn) {
1848 for(i=0;i<moldyn->count;i++) {
1849 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
1850 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
1851 i,atom[i].r.x,dim->x/2);
1852 printf("diagnostic:\n");
1853 printf("-----------\natom.r.x:\n");
1855 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
1858 ((byte)&(1<<k))?1:0,
1861 printf("---------------\nx=dim.x/2:\n");
1863 memcpy(&byte,(u8 *)(&x)+j,1);
1866 ((byte)&(1<<k))?1:0,
1869 if(atom[i].r.x==x) printf("the same!\n");
1870 else printf("different!\n");
1872 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
1873 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
1874 i,atom[i].r.y,dim->y/2);
1875 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
1876 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
1877 i,atom[i].r.z,dim->z/2);
1884 * post processing functions
1887 int get_line(int fd,char *line,int max) {
1894 if(count==max) return count;
1895 ret=read(fd,line+count,1);
1896 if(ret<=0) return ret;
1897 if(line[count]=='\n') {