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 [atm]: %f\n",moldyn->p_ref/ATM);
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,void *params) {
168 moldyn->pot1b_params=params;
173 int set_potential2b(t_moldyn *moldyn,pf_func2b func,void *params) {
176 moldyn->pot2b_params=params;
181 int set_potential2b_post(t_moldyn *moldyn,pf_func2b_post func,void *params) {
183 moldyn->func2b_post=func;
184 moldyn->pot2b_params=params;
189 int set_potential3b(t_moldyn *moldyn,pf_func3b func,void *params) {
192 moldyn->pot3b_params=params;
197 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
199 strncpy(moldyn->vlsdir,dir,127);
204 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
206 strncpy(moldyn->rauthor,author,63);
207 strncpy(moldyn->rtitle,title,63);
212 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
217 printf("[moldyn] set log: ");
220 case LOG_TOTAL_ENERGY:
221 moldyn->ewrite=timer;
222 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
223 moldyn->efd=open(filename,
224 O_WRONLY|O_CREAT|O_EXCL,
227 perror("[moldyn] energy log fd open");
230 dprintf(moldyn->efd,"# total energy log file\n");
231 printf("total energy (%d)\n",timer);
233 case LOG_TOTAL_MOMENTUM:
234 moldyn->mwrite=timer;
235 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
236 moldyn->mfd=open(filename,
237 O_WRONLY|O_CREAT|O_EXCL,
240 perror("[moldyn] momentum log fd open");
243 dprintf(moldyn->efd,"# total momentum log file\n");
244 printf("total momentum (%d)\n",timer);
247 moldyn->swrite=timer;
248 printf("save file (%d)\n",timer);
251 moldyn->vwrite=timer;
252 ret=visual_init(&(moldyn->vis),moldyn->vlsdir);
254 printf("[moldyn] visual init failure\n");
257 printf("visual file (%d)\n",timer);
260 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
261 moldyn->rfd=open(filename,
262 O_WRONLY|O_CREAT|O_EXCL,
265 perror("[moldyn] report fd open");
268 snprintf(filename,127,"%s/plot.scr",moldyn->vlsdir);
269 moldyn->pfd=open(filename,
270 O_WRONLY|O_CREAT|O_EXCL,
273 perror("[moldyn] plot fd open");
276 dprintf(moldyn->rfd,report_start,
277 moldyn->rauthor,moldyn->rtitle);
278 dprintf(moldyn->pfd,plot_script);
282 printf("unknown log type: %02x\n",type);
289 int moldyn_log_shutdown(t_moldyn *moldyn) {
293 printf("[moldyn] log shutdown\n");
294 if(moldyn->efd) close(moldyn->efd);
295 if(moldyn->mfd) close(moldyn->mfd);
297 dprintf(moldyn->rfd,report_end);
299 snprintf(sc,255,"cd %s && gnuplot plot.scr",moldyn->vlsdir);
301 snprintf(sc,255,"cd %s && pdflatex report",moldyn->vlsdir);
303 snprintf(sc,255,"cd %s && pdflatex report",moldyn->vlsdir);
305 snprintf(sc,255,"cd %s && dvipdf report",moldyn->vlsdir);
308 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
314 * creating lattice functions
317 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
318 u8 attr,u8 brand,int a,int b,int c) {
329 /* how many atoms do we expect */
330 if(type==CUBIC) new*=1;
331 if(type==FCC) new*=4;
332 if(type==DIAMOND) new*=8;
334 /* allocate space for atoms */
335 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
337 perror("[moldyn] realloc (create lattice)");
341 atom=&(moldyn->atom[count]);
343 /* no atoms on the boundaries (only reason: it looks better!) */
350 set_nn_dist(moldyn,lc);
351 ret=cubic_init(a,b,c,lc,atom,&origin);
354 v3_scale(&origin,&origin,0.5);
355 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
356 ret=fcc_init(a,b,c,lc,atom,&origin);
359 v3_scale(&origin,&origin,0.25);
360 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
361 ret=diamond_init(a,b,c,lc,atom,&origin);
364 printf("unknown lattice type (%02x)\n",type);
370 printf("[moldyn] creating lattice failed\n");
371 printf(" amount of atoms\n");
372 printf(" - expected: %d\n",new);
373 printf(" - created: %d\n",ret);
378 printf("[moldyn] created lattice with %d atoms\n",new);
380 for(ret=0;ret<new;ret++) {
381 atom[ret].element=element;
384 atom[ret].brand=brand;
385 atom[ret].tag=count+ret;
386 check_per_bound(moldyn,&(atom[ret].r));
393 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
412 v3_copy(&(atom[count].r),&r);
421 for(i=0;i<count;i++) {
422 atom[i].r.x-=(a*lc)/2.0;
423 atom[i].r.y-=(b*lc)/2.0;
424 atom[i].r.z-=(c*lc)/2.0;
430 /* fcc lattice init */
431 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
444 /* construct the basis */
445 memset(basis,0,3*sizeof(t_3dvec));
453 /* fill up the room */
461 v3_copy(&(atom[count].r),&r);
464 /* the three face centered atoms */
466 v3_add(&n,&r,&basis[l]);
467 v3_copy(&(atom[count].r),&n);
476 /* coordinate transformation */
477 for(i=0;i<count;i++) {
478 atom[i].r.x-=(a*lc)/2.0;
479 atom[i].r.y-=(b*lc)/2.0;
480 atom[i].r.z-=(c*lc)/2.0;
486 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
491 count=fcc_init(a,b,c,lc,atom,origin);
497 if(origin) v3_add(&o,&o,origin);
499 count+=fcc_init(a,b,c,lc,&atom[count],&o);
504 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
505 t_3dvec *r,t_3dvec *v) {
512 count=(moldyn->count)++;
514 ptr=realloc(atom,(count+1)*sizeof(t_atom));
516 perror("[moldyn] realloc (add atom)");
524 atom[count].element=element;
525 atom[count].mass=mass;
526 atom[count].brand=brand;
527 atom[count].tag=count;
528 atom[count].attr=attr;
533 int destroy_atoms(t_moldyn *moldyn) {
535 if(moldyn->atom) free(moldyn->atom);
540 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
543 * - gaussian distribution of velocities
544 * - zero total momentum
545 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
550 t_3dvec p_total,delta;
555 random=&(moldyn->random);
557 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
559 /* gaussian distribution of velocities */
561 for(i=0;i<moldyn->count;i++) {
562 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
564 v=sigma*rand_get_gauss(random);
566 p_total.x+=atom[i].mass*v;
568 v=sigma*rand_get_gauss(random);
570 p_total.y+=atom[i].mass*v;
572 v=sigma*rand_get_gauss(random);
574 p_total.z+=atom[i].mass*v;
577 /* zero total momentum */
578 v3_scale(&p_total,&p_total,1.0/moldyn->count);
579 for(i=0;i<moldyn->count;i++) {
580 v3_scale(&delta,&p_total,1.0/atom[i].mass);
581 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
584 /* velocity scaling */
585 scale_velocity(moldyn,equi_init);
590 double temperature_calc(t_moldyn *moldyn) {
592 /* assume up to date kinetic energy, which is 3/2 N k_B T */
594 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
599 double get_temperature(t_moldyn *moldyn) {
604 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
614 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
617 /* get kinetic energy / temperature & count involved atoms */
620 for(i=0;i<moldyn->count;i++) {
621 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
622 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
627 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
628 else return 0; /* no atoms involved in scaling! */
630 /* (temporary) hack for e,t = 0 */
633 if(moldyn->t_ref!=0.0) {
634 thermal_init(moldyn,equi_init);
638 return 0; /* no scaling needed */
642 /* get scaling factor */
643 scale=moldyn->t_ref/moldyn->t;
647 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
648 scale=1.0+(scale-1.0)/moldyn->t_tc;
651 /* velocity scaling */
652 for(i=0;i<moldyn->count;i++) {
653 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
654 v3_scale(&(atom[i].v),&(atom[i].v),scale);
660 double ideal_gas_law_pressure(t_moldyn *moldyn) {
664 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
669 double pressure_calc(t_moldyn *moldyn) {
676 * P = 1/(3V) sum_i ( p_i^2 / 2m + f_i r_i )
682 for(i=0;i<moldyn->count;i++) {
683 virial=&(moldyn->atom[i].virial);
684 v+=(virial->xx+virial->yy+virial->zz);
687 /* assume up to date kinetic energy */
688 moldyn->p=2.0*moldyn->ekin+v;
689 moldyn->p/=(3.0*moldyn->volume);
694 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
702 store=malloc(moldyn->count*sizeof(t_atom));
704 printf("[moldyn] allocating store mem failed\n");
708 /* save unscaled potential energy + atom/dim configuration */
710 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
713 /* derivative with respect to x direction */
714 scale=1.0+moldyn->dv/(moldyn->dim.y*moldyn->dim.z);
715 scale_dim(moldyn,scale,TRUE,0,0);
716 scale_atoms(moldyn,scale,TRUE,0,0);
717 link_cell_shutdown(moldyn);
718 link_cell_init(moldyn,QUIET);
719 potential_force_calc(moldyn);
720 tp->x=(moldyn->energy-u)/moldyn->dv;
723 /* restore atomic configuration + dim */
724 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
727 /* derivative with respect to y direction */
728 scale=1.0+moldyn->dv/(moldyn->dim.x*moldyn->dim.z);
729 scale_dim(moldyn,scale,0,TRUE,0);
730 scale_atoms(moldyn,scale,0,TRUE,0);
731 link_cell_shutdown(moldyn);
732 link_cell_init(moldyn,QUIET);
733 potential_force_calc(moldyn);
734 tp->y=(moldyn->energy-u)/moldyn->dv;
737 /* restore atomic configuration + dim */
738 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
741 /* derivative with respect to z direction */
742 scale=1.0+moldyn->dv/(moldyn->dim.x*moldyn->dim.y);
743 scale_dim(moldyn,scale,0,0,TRUE);
744 scale_atoms(moldyn,scale,0,0,TRUE);
745 link_cell_shutdown(moldyn);
746 link_cell_init(moldyn,QUIET);
747 potential_force_calc(moldyn);
748 tp->z=(moldyn->energy-u)/moldyn->dv;
751 /* restore atomic configuration + dim */
752 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
755 printf("dU/dV komp addiert = %f %f %f\n",tp->x,tp->y,tp->z);
757 scale=1.0+pow(moldyn->dv/moldyn->volume,ONE_THIRD);
759 printf("debug: %f %f\n",moldyn->atom[0].r.x,moldyn->dim.x);
760 scale_dim(moldyn,scale,1,1,1);
761 scale_atoms(moldyn,scale,1,1,1);
762 link_cell_shutdown(moldyn);
763 link_cell_init(moldyn,QUIET);
764 potential_force_calc(moldyn);
765 printf("debug: %f %f\n",moldyn->atom[0].r.x,moldyn->dim.x);
767 printf("dU/dV einfach = %f\n",((moldyn->energy-u)/moldyn->dv)/ATM);
769 /* restore atomic configuration + dim */
770 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
776 link_cell_shutdown(moldyn);
777 link_cell_init(moldyn,QUIET);
782 double get_pressure(t_moldyn *moldyn) {
788 int scale_dim(t_moldyn *moldyn,double scale,u8 x,u8 y,u8 z) {
801 int scale_atoms(t_moldyn *moldyn,double scale,u8 x,u8 y,u8 z) {
806 for(i=0;i<moldyn->count;i++) {
807 r=&(moldyn->atom[i].r);
816 int scale_volume(t_moldyn *moldyn) {
822 vdim=&(moldyn->vis.dim);
827 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
828 scale=1.0-(moldyn->p_ref-moldyn->p)/moldyn->p_tc;
829 scale=pow(scale,ONE_THIRD);
832 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
836 /* scale the atoms and dimensions */
837 scale_atoms(moldyn,scale,TRUE,TRUE,TRUE);
838 scale_dim(moldyn,scale,TRUE,TRUE,TRUE);
840 /* visualize dimensions */
847 /* recalculate scaled volume */
848 moldyn->volume=dim->x*dim->y*dim->z;
850 /* adjust/reinit linkcell */
851 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
852 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
853 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
854 link_cell_shutdown(moldyn);
855 link_cell_init(moldyn,QUIET);
866 double get_e_kin(t_moldyn *moldyn) {
874 for(i=0;i<moldyn->count;i++)
875 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
880 double update_e_kin(t_moldyn *moldyn) {
882 return(get_e_kin(moldyn));
885 double get_total_energy(t_moldyn *moldyn) {
887 return(moldyn->ekin+moldyn->energy);
890 t_3dvec get_total_p(t_moldyn *moldyn) {
899 for(i=0;i<moldyn->count;i++) {
900 v3_scale(&p,&(atom[i].v),atom[i].mass);
901 v3_add(&p_total,&p_total,&p);
907 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
911 /* nn_dist is the nearest neighbour distance */
913 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
922 /* linked list / cell method */
924 int link_cell_init(t_moldyn *moldyn,u8 vol) {
931 /* partitioning the md cell */
932 lc->nx=moldyn->dim.x/moldyn->cutoff;
933 lc->x=moldyn->dim.x/lc->nx;
934 lc->ny=moldyn->dim.y/moldyn->cutoff;
935 lc->y=moldyn->dim.y/lc->ny;
936 lc->nz=moldyn->dim.z/moldyn->cutoff;
937 lc->z=moldyn->dim.z/lc->nz;
939 lc->cells=lc->nx*lc->ny*lc->nz;
940 lc->subcell=malloc(lc->cells*sizeof(t_list));
943 printf("[moldyn] FATAL: less then 27 subcells!\n");
945 if(vol) printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
947 for(i=0;i<lc->cells;i++)
948 list_init_f(&(lc->subcell[i]));
950 link_cell_update(moldyn);
955 int link_cell_update(t_moldyn *moldyn) {
973 for(i=0;i<lc->cells;i++)
974 list_destroy_f(&(lc->subcell[i]));
976 for(count=0;count<moldyn->count;count++) {
977 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
978 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
979 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
980 list_add_immediate_f(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
987 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
1005 cell[0]=lc->subcell[i+j*nx+k*a];
1006 for(ci=-1;ci<=1;ci++) {
1009 if((x<0)||(x>=nx)) {
1013 for(cj=-1;cj<=1;cj++) {
1016 if((y<0)||(y>=ny)) {
1020 for(ck=-1;ck<=1;ck++) {
1023 if((z<0)||(z>=nz)) {
1027 if(!(ci|cj|ck)) continue;
1029 cell[--count2]=lc->subcell[x+y*nx+z*a];
1032 cell[count1++]=lc->subcell[x+y*nx+z*a];
1043 int link_cell_shutdown(t_moldyn *moldyn) {
1050 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
1051 list_destroy_f(&(moldyn->lc.subcell[i]));
1058 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1062 t_moldyn_schedule *schedule;
1064 schedule=&(moldyn->schedule);
1065 count=++(schedule->total_sched);
1067 ptr=realloc(schedule->runs,count*sizeof(int));
1069 perror("[moldyn] realloc (runs)");
1073 schedule->runs[count-1]=runs;
1075 ptr=realloc(schedule->tau,count*sizeof(double));
1077 perror("[moldyn] realloc (tau)");
1081 schedule->tau[count-1]=tau;
1083 printf("[moldyn] schedule added:\n");
1084 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1090 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1092 moldyn->schedule.hook=hook;
1093 moldyn->schedule.hook_params=hook_params;
1100 * 'integration of newtons equation' - algorithms
1104 /* start the integration */
1106 int moldyn_integrate(t_moldyn *moldyn) {
1109 unsigned int e,m,s,v;
1111 t_moldyn_schedule *sched;
1116 double energy_scale;
1118 sched=&(moldyn->schedule);
1121 /* initialize linked cell method */
1122 link_cell_init(moldyn,VERBOSE);
1124 /* logging & visualization */
1130 /* sqaure of some variables */
1131 moldyn->tau_square=moldyn->tau*moldyn->tau;
1132 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
1134 /* energy scaling factor */
1135 energy_scale=moldyn->count*EV;
1137 /* calculate initial forces */
1138 potential_force_calc(moldyn);
1140 /* some stupid checks before we actually start calculating bullshit */
1141 if(moldyn->cutoff>0.5*moldyn->dim.x)
1142 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
1143 if(moldyn->cutoff>0.5*moldyn->dim.y)
1144 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
1145 if(moldyn->cutoff>0.5*moldyn->dim.z)
1146 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
1147 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1148 if(ds>0.05*moldyn->nnd)
1149 printf("[moldyn] warning: forces too high / tau too small!\n");
1151 /* zero absolute time */
1154 /* debugging, ignore */
1157 /* tell the world */
1158 printf("[moldyn] integration start, go get a coffee ...\n");
1160 /* executing the schedule */
1161 for(sched->count=0;sched->count<sched->total_sched;sched->count++) {
1163 /* setting amount of runs and finite time step size */
1164 moldyn->tau=sched->tau[sched->count];
1165 moldyn->tau_square=moldyn->tau*moldyn->tau;
1166 moldyn->time_steps=sched->runs[sched->count];
1168 /* integration according to schedule */
1170 for(i=0;i<moldyn->time_steps;i++) {
1172 /* integration step */
1173 moldyn->integrate(moldyn);
1175 /* calculate kinetic energy, temperature and pressure */
1176 update_e_kin(moldyn);
1177 temperature_calc(moldyn);
1178 pressure_calc(moldyn);
1179 //thermodynamic_pressure_calc(moldyn);
1182 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1183 scale_velocity(moldyn,FALSE);
1184 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1185 scale_volume(moldyn);
1187 /* check for log & visualization */
1190 dprintf(moldyn->efd,
1192 moldyn->time,moldyn->ekin/energy_scale,
1193 moldyn->energy/energy_scale,
1194 get_total_energy(moldyn)/energy_scale);
1198 p=get_total_p(moldyn);
1199 dprintf(moldyn->mfd,
1200 "%f %f\n",moldyn->time,v3_norm(&p));
1205 snprintf(dir,128,"%s/s-%07.f.save",
1206 moldyn->vlsdir,moldyn->time);
1207 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT);
1208 if(fd<0) perror("[moldyn] save fd open");
1210 write(fd,moldyn,sizeof(t_moldyn));
1211 write(fd,moldyn->atom,
1212 moldyn->count*sizeof(t_atom));
1219 visual_atoms(&(moldyn->vis),moldyn->time,
1220 moldyn->atom,moldyn->count);
1221 printf("\rsched: %d, steps: %d, T: %f, P: %f V: %f",
1223 moldyn->t,moldyn->p/ATM,moldyn->volume);
1228 /* increase absolute time */
1229 moldyn->time+=moldyn->tau;
1233 /* check for hooks */
1235 sched->hook(moldyn,sched->hook_params);
1237 /* get a new info line */
1245 /* velocity verlet */
1247 int velocity_verlet(t_moldyn *moldyn) {
1250 double tau,tau_square,h;
1255 count=moldyn->count;
1257 tau_square=moldyn->tau_square;
1259 for(i=0;i<count;i++) {
1262 v3_scale(&delta,&(atom[i].v),tau);
1263 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1264 v3_scale(&delta,&(atom[i].f),h*tau_square);
1265 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1266 check_per_bound(moldyn,&(atom[i].r));
1268 /* velocities [actually v(t+tau/2)] */
1269 v3_scale(&delta,&(atom[i].f),h*tau);
1270 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1273 /* neighbour list update */
1274 link_cell_update(moldyn);
1276 /* forces depending on chosen potential */
1277 potential_force_calc(moldyn);
1279 for(i=0;i<count;i++) {
1280 /* again velocities [actually v(t+tau)] */
1281 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1282 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1291 * potentials & corresponding forces & virial routine
1295 /* generic potential and force calculation */
1297 int potential_force_calc(t_moldyn *moldyn) {
1300 t_atom *itom,*jtom,*ktom;
1303 t_list neighbour_i[27];
1304 t_list neighbour_i2[27];
1309 count=moldyn->count;
1316 /* reset force, site energy and virial of every atom */
1317 for(i=0;i<count;i++) {
1320 v3_zero(&(itom[i].f));
1323 virial=(&(itom[i].virial));
1331 /* reset site energy */
1336 /* get energy,force and virial of every atom */
1337 for(i=0;i<count;i++) {
1339 /* single particle potential/force */
1340 if(itom[i].attr&ATOM_ATTR_1BP)
1341 moldyn->func1b(moldyn,&(itom[i]));
1343 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1346 /* 2 body pair potential/force */
1348 link_cell_neighbour_index(moldyn,
1349 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1350 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1351 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1358 this=&(neighbour_i[j]);
1361 if(this->start==NULL)
1367 jtom=this->current->data;
1369 if(jtom==&(itom[i]))
1372 if((jtom->attr&ATOM_ATTR_2BP)&
1373 (itom[i].attr&ATOM_ATTR_2BP)) {
1374 moldyn->func2b(moldyn,
1380 /* 3 body potential/force */
1382 if(!(itom[i].attr&ATOM_ATTR_3BP)||
1383 !(jtom->attr&ATOM_ATTR_3BP))
1386 /* copy the neighbour lists */
1387 memcpy(neighbour_i2,neighbour_i,
1390 /* get neighbours of i */
1393 that=&(neighbour_i2[k]);
1396 if(that->start==NULL)
1403 ktom=that->current->data;
1405 if(!(ktom->attr&ATOM_ATTR_3BP))
1411 if(ktom==&(itom[i]))
1414 moldyn->func3b(moldyn,
1420 } while(list_next_f(that)!=\
1425 /* 2bp post function */
1426 if(moldyn->func2b_post) {
1427 moldyn->func2b_post(moldyn,
1432 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1449 * virial calculation
1452 inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
1454 a->virial.xx+=f->x*d->x;
1455 a->virial.yy+=f->y*d->y;
1456 a->virial.zz+=f->z*d->z;
1457 a->virial.xy+=f->x*d->y;
1458 a->virial.xz+=f->x*d->z;
1459 a->virial.yz+=f->y*d->z;
1465 * periodic boundayr checking
1468 inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1479 if(moldyn->status&MOLDYN_STAT_PBX) {
1480 if(a->x>=x) a->x-=dim->x;
1481 else if(-a->x>x) a->x+=dim->x;
1483 if(moldyn->status&MOLDYN_STAT_PBY) {
1484 if(a->y>=y) a->y-=dim->y;
1485 else if(-a->y>y) a->y+=dim->y;
1487 if(moldyn->status&MOLDYN_STAT_PBZ) {
1488 if(a->z>=z) a->z-=dim->z;
1489 else if(-a->z>z) a->z+=dim->z;
1497 * example potentials
1500 /* harmonic oscillator potential and force */
1502 int harmonic_oscillator(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
1504 t_ho_params *params;
1505 t_3dvec force,distance;
1507 double sc,equi_dist;
1509 params=moldyn->pot2b_params;
1510 sc=params->spring_constant;
1511 equi_dist=params->equilibrium_distance;
1515 v3_sub(&distance,&(aj->r),&(ai->r));
1517 if(bc) check_per_bound(moldyn,&distance);
1518 d=v3_norm(&distance);
1519 if(d<=moldyn->cutoff) {
1520 moldyn->energy+=(0.5*sc*(d-equi_dist)*(d-equi_dist));
1521 /* f = -grad E; grad r_ij = -1 1/r_ij distance */
1522 f=sc*(1.0-equi_dist/d);
1523 v3_scale(&force,&distance,f);
1524 v3_add(&(ai->f),&(ai->f),&force);
1525 virial_calc(ai,&force,&distance);
1526 virial_calc(aj,&force,&distance); /* f and d signe switched */
1527 v3_scale(&force,&distance,-f);
1528 v3_add(&(aj->f),&(aj->f),&force);
1535 * debugging / critical check functions
1538 int moldyn_bc_check(t_moldyn *moldyn) {
1551 for(i=0;i<moldyn->count;i++) {
1552 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
1553 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
1554 i,atom[i].r.x,dim->x/2);
1555 printf("diagnostic:\n");
1556 printf("-----------\natom.r.x:\n");
1558 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
1561 ((byte)&(1<<k))?1:0,
1564 printf("---------------\nx=dim.x/2:\n");
1566 memcpy(&byte,(u8 *)(&x)+j,1);
1569 ((byte)&(1<<k))?1:0,
1572 if(atom[i].r.x==x) printf("the same!\n");
1573 else printf("different!\n");
1575 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
1576 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
1577 i,atom[i].r.y,dim->y/2);
1578 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
1579 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
1580 i,atom[i].r.z,dim->z/2);