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) {
172 int set_potential2b(t_moldyn *moldyn,pf_func2b func,void *params) {
179 int set_potential2b_post(t_moldyn *moldyn,pf_func2b_post func,void *params) {
181 moldyn->func2b_post=func;
186 int set_potential3b(t_moldyn *moldyn,pf_func3b func,void *params) {
193 int set_potential_params(t_moldyn *moldyn,void *params) {
195 moldyn->pot_params=params;
200 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
202 strncpy(moldyn->vlsdir,dir,127);
207 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
209 strncpy(moldyn->rauthor,author,63);
210 strncpy(moldyn->rtitle,title,63);
215 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
220 printf("[moldyn] set log: ");
223 case LOG_TOTAL_ENERGY:
224 moldyn->ewrite=timer;
225 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
226 moldyn->efd=open(filename,
227 O_WRONLY|O_CREAT|O_EXCL,
230 perror("[moldyn] energy log fd open");
233 dprintf(moldyn->efd,"# total energy log file\n");
234 printf("total energy (%d)\n",timer);
236 case LOG_TOTAL_MOMENTUM:
237 moldyn->mwrite=timer;
238 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
239 moldyn->mfd=open(filename,
240 O_WRONLY|O_CREAT|O_EXCL,
243 perror("[moldyn] momentum log fd open");
246 dprintf(moldyn->efd,"# total momentum log file\n");
247 printf("total momentum (%d)\n",timer);
250 moldyn->swrite=timer;
251 printf("save file (%d)\n",timer);
254 moldyn->vwrite=timer;
255 ret=visual_init(&(moldyn->vis),moldyn->vlsdir);
257 printf("[moldyn] visual init failure\n");
260 printf("visual file (%d)\n",timer);
263 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
264 moldyn->rfd=open(filename,
265 O_WRONLY|O_CREAT|O_EXCL,
268 perror("[moldyn] report fd open");
271 snprintf(filename,127,"%s/plot.scr",moldyn->vlsdir);
272 moldyn->pfd=open(filename,
273 O_WRONLY|O_CREAT|O_EXCL,
276 perror("[moldyn] plot fd open");
279 dprintf(moldyn->rfd,report_start,
280 moldyn->rauthor,moldyn->rtitle);
281 dprintf(moldyn->pfd,plot_script);
285 printf("unknown log type: %02x\n",type);
292 int moldyn_log_shutdown(t_moldyn *moldyn) {
296 printf("[moldyn] log shutdown\n");
297 if(moldyn->efd) close(moldyn->efd);
298 if(moldyn->mfd) close(moldyn->mfd);
300 dprintf(moldyn->rfd,report_end);
302 snprintf(sc,255,"cd %s && gnuplot plot.scr",moldyn->vlsdir);
304 snprintf(sc,255,"cd %s && pdflatex report",moldyn->vlsdir);
306 snprintf(sc,255,"cd %s && pdflatex report",moldyn->vlsdir);
308 snprintf(sc,255,"cd %s && dvipdf report",moldyn->vlsdir);
311 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
317 * creating lattice functions
320 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
321 u8 attr,u8 brand,int a,int b,int c) {
332 /* how many atoms do we expect */
333 if(type==CUBIC) new*=1;
334 if(type==FCC) new*=4;
335 if(type==DIAMOND) new*=8;
337 /* allocate space for atoms */
338 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
340 perror("[moldyn] realloc (create lattice)");
344 atom=&(moldyn->atom[count]);
346 /* no atoms on the boundaries (only reason: it looks better!) */
353 set_nn_dist(moldyn,lc);
354 ret=cubic_init(a,b,c,lc,atom,&origin);
357 v3_scale(&origin,&origin,0.5);
358 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
359 ret=fcc_init(a,b,c,lc,atom,&origin);
362 v3_scale(&origin,&origin,0.25);
363 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
364 ret=diamond_init(a,b,c,lc,atom,&origin);
367 printf("unknown lattice type (%02x)\n",type);
373 printf("[moldyn] creating lattice failed\n");
374 printf(" amount of atoms\n");
375 printf(" - expected: %d\n",new);
376 printf(" - created: %d\n",ret);
381 printf("[moldyn] created lattice with %d atoms\n",new);
383 for(ret=0;ret<new;ret++) {
384 atom[ret].element=element;
387 atom[ret].brand=brand;
388 atom[ret].tag=count+ret;
389 check_per_bound(moldyn,&(atom[ret].r));
396 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
415 v3_copy(&(atom[count].r),&r);
424 for(i=0;i<count;i++) {
425 atom[i].r.x-=(a*lc)/2.0;
426 atom[i].r.y-=(b*lc)/2.0;
427 atom[i].r.z-=(c*lc)/2.0;
433 /* fcc lattice init */
434 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
447 /* construct the basis */
448 memset(basis,0,3*sizeof(t_3dvec));
456 /* fill up the room */
464 v3_copy(&(atom[count].r),&r);
467 /* the three face centered atoms */
469 v3_add(&n,&r,&basis[l]);
470 v3_copy(&(atom[count].r),&n);
479 /* coordinate transformation */
480 for(i=0;i<count;i++) {
481 atom[i].r.x-=(a*lc)/2.0;
482 atom[i].r.y-=(b*lc)/2.0;
483 atom[i].r.z-=(c*lc)/2.0;
489 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
494 count=fcc_init(a,b,c,lc,atom,origin);
500 if(origin) v3_add(&o,&o,origin);
502 count+=fcc_init(a,b,c,lc,&atom[count],&o);
507 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
508 t_3dvec *r,t_3dvec *v) {
515 count=(moldyn->count)++;
517 ptr=realloc(atom,(count+1)*sizeof(t_atom));
519 perror("[moldyn] realloc (add atom)");
527 atom[count].element=element;
528 atom[count].mass=mass;
529 atom[count].brand=brand;
530 atom[count].tag=count;
531 atom[count].attr=attr;
536 int destroy_atoms(t_moldyn *moldyn) {
538 if(moldyn->atom) free(moldyn->atom);
543 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
546 * - gaussian distribution of velocities
547 * - zero total momentum
548 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
553 t_3dvec p_total,delta;
558 random=&(moldyn->random);
560 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
562 /* gaussian distribution of velocities */
564 for(i=0;i<moldyn->count;i++) {
565 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
567 v=sigma*rand_get_gauss(random);
569 p_total.x+=atom[i].mass*v;
571 v=sigma*rand_get_gauss(random);
573 p_total.y+=atom[i].mass*v;
575 v=sigma*rand_get_gauss(random);
577 p_total.z+=atom[i].mass*v;
580 /* zero total momentum */
581 v3_scale(&p_total,&p_total,1.0/moldyn->count);
582 for(i=0;i<moldyn->count;i++) {
583 v3_scale(&delta,&p_total,1.0/atom[i].mass);
584 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
587 /* velocity scaling */
588 scale_velocity(moldyn,equi_init);
593 double temperature_calc(t_moldyn *moldyn) {
595 /* assume up to date kinetic energy, which is 3/2 N k_B T */
597 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
602 double get_temperature(t_moldyn *moldyn) {
607 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
617 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
620 /* get kinetic energy / temperature & count involved atoms */
623 for(i=0;i<moldyn->count;i++) {
624 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
625 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
630 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
631 else return 0; /* no atoms involved in scaling! */
633 /* (temporary) hack for e,t = 0 */
636 if(moldyn->t_ref!=0.0) {
637 thermal_init(moldyn,equi_init);
641 return 0; /* no scaling needed */
645 /* get scaling factor */
646 scale=moldyn->t_ref/moldyn->t;
650 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
651 scale=1.0+(scale-1.0)/moldyn->t_tc;
654 /* velocity scaling */
655 for(i=0;i<moldyn->count;i++) {
656 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
657 v3_scale(&(atom[i].v),&(atom[i].v),scale);
663 double ideal_gas_law_pressure(t_moldyn *moldyn) {
667 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
672 double pressure_calc(t_moldyn *moldyn) {
679 * P = 1/(3V) sum_i ( p_i^2 / 2m + f_i r_i )
685 for(i=0;i<moldyn->count;i++) {
686 virial=&(moldyn->atom[i].virial);
687 v+=(virial->xx+virial->yy+virial->zz);
690 /* assume up to date kinetic energy */
691 moldyn->p=2.0*moldyn->ekin+v;
692 moldyn->p/=(3.0*moldyn->volume);
697 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
705 store=malloc(moldyn->count*sizeof(t_atom));
707 printf("[moldyn] allocating store mem failed\n");
711 /* save unscaled potential energy + atom/dim configuration */
713 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
716 /* derivative with respect to x direction */
717 scale=1.0+moldyn->dv/(moldyn->dim.y*moldyn->dim.z);
718 scale_dim(moldyn,scale,TRUE,0,0);
719 scale_atoms(moldyn,scale,TRUE,0,0);
720 link_cell_shutdown(moldyn);
721 link_cell_init(moldyn,QUIET);
722 potential_force_calc(moldyn);
723 tp->x=(moldyn->energy-u)/moldyn->dv;
726 /* restore atomic configuration + dim */
727 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
730 /* derivative with respect to y direction */
731 scale=1.0+moldyn->dv/(moldyn->dim.x*moldyn->dim.z);
732 scale_dim(moldyn,scale,0,TRUE,0);
733 scale_atoms(moldyn,scale,0,TRUE,0);
734 link_cell_shutdown(moldyn);
735 link_cell_init(moldyn,QUIET);
736 potential_force_calc(moldyn);
737 tp->y=(moldyn->energy-u)/moldyn->dv;
740 /* restore atomic configuration + dim */
741 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
744 /* derivative with respect to z direction */
745 scale=1.0+moldyn->dv/(moldyn->dim.x*moldyn->dim.y);
746 scale_dim(moldyn,scale,0,0,TRUE);
747 scale_atoms(moldyn,scale,0,0,TRUE);
748 link_cell_shutdown(moldyn);
749 link_cell_init(moldyn,QUIET);
750 potential_force_calc(moldyn);
751 tp->z=(moldyn->energy-u)/moldyn->dv;
754 /* restore atomic configuration + dim */
755 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
758 printf("dU/dV komp addiert = %f %f %f\n",tp->x,tp->y,tp->z);
760 scale=1.0+pow(moldyn->dv/moldyn->volume,ONE_THIRD);
762 printf("debug: %f %f\n",moldyn->atom[0].r.x,moldyn->dim.x);
763 scale_dim(moldyn,scale,1,1,1);
764 scale_atoms(moldyn,scale,1,1,1);
765 link_cell_shutdown(moldyn);
766 link_cell_init(moldyn,QUIET);
767 potential_force_calc(moldyn);
768 printf("debug: %f %f\n",moldyn->atom[0].r.x,moldyn->dim.x);
770 printf("dU/dV einfach = %f\n",((moldyn->energy-u)/moldyn->dv)/ATM);
772 /* restore atomic configuration + dim */
773 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
779 link_cell_shutdown(moldyn);
780 link_cell_init(moldyn,QUIET);
785 double get_pressure(t_moldyn *moldyn) {
791 int scale_dim(t_moldyn *moldyn,double scale,u8 x,u8 y,u8 z) {
804 int scale_atoms(t_moldyn *moldyn,double scale,u8 x,u8 y,u8 z) {
809 for(i=0;i<moldyn->count;i++) {
810 r=&(moldyn->atom[i].r);
819 int scale_volume(t_moldyn *moldyn) {
825 vdim=&(moldyn->vis.dim);
830 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
831 scale=1.0-(moldyn->p_ref-moldyn->p)/moldyn->p_tc;
832 scale=pow(scale,ONE_THIRD);
835 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
839 /* scale the atoms and dimensions */
840 scale_atoms(moldyn,scale,TRUE,TRUE,TRUE);
841 scale_dim(moldyn,scale,TRUE,TRUE,TRUE);
843 /* visualize dimensions */
850 /* recalculate scaled volume */
851 moldyn->volume=dim->x*dim->y*dim->z;
853 /* adjust/reinit linkcell */
854 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
855 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
856 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
857 link_cell_shutdown(moldyn);
858 link_cell_init(moldyn,QUIET);
869 double get_e_kin(t_moldyn *moldyn) {
877 for(i=0;i<moldyn->count;i++)
878 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
883 double update_e_kin(t_moldyn *moldyn) {
885 return(get_e_kin(moldyn));
888 double get_total_energy(t_moldyn *moldyn) {
890 return(moldyn->ekin+moldyn->energy);
893 t_3dvec get_total_p(t_moldyn *moldyn) {
902 for(i=0;i<moldyn->count;i++) {
903 v3_scale(&p,&(atom[i].v),atom[i].mass);
904 v3_add(&p_total,&p_total,&p);
910 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
914 /* nn_dist is the nearest neighbour distance */
916 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
925 /* linked list / cell method */
927 int link_cell_init(t_moldyn *moldyn,u8 vol) {
934 /* partitioning the md cell */
935 lc->nx=moldyn->dim.x/moldyn->cutoff;
936 lc->x=moldyn->dim.x/lc->nx;
937 lc->ny=moldyn->dim.y/moldyn->cutoff;
938 lc->y=moldyn->dim.y/lc->ny;
939 lc->nz=moldyn->dim.z/moldyn->cutoff;
940 lc->z=moldyn->dim.z/lc->nz;
942 lc->cells=lc->nx*lc->ny*lc->nz;
943 lc->subcell=malloc(lc->cells*sizeof(t_list));
946 printf("[moldyn] FATAL: less then 27 subcells!\n");
948 if(vol) printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
950 for(i=0;i<lc->cells;i++)
951 list_init_f(&(lc->subcell[i]));
953 link_cell_update(moldyn);
958 int link_cell_update(t_moldyn *moldyn) {
976 for(i=0;i<lc->cells;i++)
977 list_destroy_f(&(lc->subcell[i]));
979 for(count=0;count<moldyn->count;count++) {
980 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
981 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
982 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
983 list_add_immediate_f(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
990 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
1008 cell[0]=lc->subcell[i+j*nx+k*a];
1009 for(ci=-1;ci<=1;ci++) {
1012 if((x<0)||(x>=nx)) {
1016 for(cj=-1;cj<=1;cj++) {
1019 if((y<0)||(y>=ny)) {
1023 for(ck=-1;ck<=1;ck++) {
1026 if((z<0)||(z>=nz)) {
1030 if(!(ci|cj|ck)) continue;
1032 cell[--count2]=lc->subcell[x+y*nx+z*a];
1035 cell[count1++]=lc->subcell[x+y*nx+z*a];
1046 int link_cell_shutdown(t_moldyn *moldyn) {
1053 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
1054 list_destroy_f(&(moldyn->lc.subcell[i]));
1061 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1065 t_moldyn_schedule *schedule;
1067 schedule=&(moldyn->schedule);
1068 count=++(schedule->total_sched);
1070 ptr=realloc(schedule->runs,count*sizeof(int));
1072 perror("[moldyn] realloc (runs)");
1076 schedule->runs[count-1]=runs;
1078 ptr=realloc(schedule->tau,count*sizeof(double));
1080 perror("[moldyn] realloc (tau)");
1084 schedule->tau[count-1]=tau;
1086 printf("[moldyn] schedule added:\n");
1087 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1093 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1095 moldyn->schedule.hook=hook;
1096 moldyn->schedule.hook_params=hook_params;
1103 * 'integration of newtons equation' - algorithms
1107 /* start the integration */
1109 int moldyn_integrate(t_moldyn *moldyn) {
1112 unsigned int e,m,s,v;
1114 t_moldyn_schedule *sched;
1119 double energy_scale;
1121 sched=&(moldyn->schedule);
1124 /* initialize linked cell method */
1125 link_cell_init(moldyn,VERBOSE);
1127 /* logging & visualization */
1133 /* sqaure of some variables */
1134 moldyn->tau_square=moldyn->tau*moldyn->tau;
1135 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
1137 /* energy scaling factor */
1138 energy_scale=moldyn->count*EV;
1140 /* calculate initial forces */
1141 potential_force_calc(moldyn);
1143 /* some stupid checks before we actually start calculating bullshit */
1144 if(moldyn->cutoff>0.5*moldyn->dim.x)
1145 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
1146 if(moldyn->cutoff>0.5*moldyn->dim.y)
1147 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
1148 if(moldyn->cutoff>0.5*moldyn->dim.z)
1149 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
1150 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1151 if(ds>0.05*moldyn->nnd)
1152 printf("[moldyn] warning: forces too high / tau too small!\n");
1154 /* zero absolute time */
1157 /* debugging, ignore */
1160 /* tell the world */
1161 printf("[moldyn] integration start, go get a coffee ...\n");
1163 /* executing the schedule */
1164 for(sched->count=0;sched->count<sched->total_sched;sched->count++) {
1166 /* setting amount of runs and finite time step size */
1167 moldyn->tau=sched->tau[sched->count];
1168 moldyn->tau_square=moldyn->tau*moldyn->tau;
1169 moldyn->time_steps=sched->runs[sched->count];
1171 /* integration according to schedule */
1173 for(i=0;i<moldyn->time_steps;i++) {
1175 /* integration step */
1176 moldyn->integrate(moldyn);
1178 /* calculate kinetic energy, temperature and pressure */
1179 update_e_kin(moldyn);
1180 temperature_calc(moldyn);
1181 pressure_calc(moldyn);
1182 //thermodynamic_pressure_calc(moldyn);
1185 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1186 scale_velocity(moldyn,FALSE);
1187 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1188 scale_volume(moldyn);
1190 /* check for log & visualization */
1193 dprintf(moldyn->efd,
1195 moldyn->time,moldyn->ekin/energy_scale,
1196 moldyn->energy/energy_scale,
1197 get_total_energy(moldyn)/energy_scale);
1201 p=get_total_p(moldyn);
1202 dprintf(moldyn->mfd,
1203 "%f %f\n",moldyn->time,v3_norm(&p));
1208 snprintf(dir,128,"%s/s-%07.f.save",
1209 moldyn->vlsdir,moldyn->time);
1210 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT);
1211 if(fd<0) perror("[moldyn] save fd open");
1213 write(fd,moldyn,sizeof(t_moldyn));
1214 write(fd,moldyn->atom,
1215 moldyn->count*sizeof(t_atom));
1222 visual_atoms(&(moldyn->vis),moldyn->time,
1223 moldyn->atom,moldyn->count);
1224 printf("\rsched: %d, steps: %d, T: %f, P: %f V: %f",
1226 moldyn->t,moldyn->p/ATM,moldyn->volume);
1231 /* increase absolute time */
1232 moldyn->time+=moldyn->tau;
1236 /* check for hooks */
1238 sched->hook(moldyn,sched->hook_params);
1240 /* get a new info line */
1248 /* velocity verlet */
1250 int velocity_verlet(t_moldyn *moldyn) {
1253 double tau,tau_square,h;
1258 count=moldyn->count;
1260 tau_square=moldyn->tau_square;
1262 for(i=0;i<count;i++) {
1265 v3_scale(&delta,&(atom[i].v),tau);
1266 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1267 v3_scale(&delta,&(atom[i].f),h*tau_square);
1268 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1269 check_per_bound(moldyn,&(atom[i].r));
1271 /* velocities [actually v(t+tau/2)] */
1272 v3_scale(&delta,&(atom[i].f),h*tau);
1273 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1276 /* neighbour list update */
1277 link_cell_update(moldyn);
1279 /* forces depending on chosen potential */
1280 potential_force_calc(moldyn);
1282 for(i=0;i<count;i++) {
1283 /* again velocities [actually v(t+tau)] */
1284 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1285 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1294 * potentials & corresponding forces & virial routine
1298 /* generic potential and force calculation */
1300 int potential_force_calc(t_moldyn *moldyn) {
1303 t_atom *itom,*jtom,*ktom;
1306 t_list neighbour_i[27];
1307 t_list neighbour_i2[27];
1312 count=moldyn->count;
1319 /* reset force, site energy and virial of every atom */
1320 for(i=0;i<count;i++) {
1323 v3_zero(&(itom[i].f));
1326 virial=(&(itom[i].virial));
1334 /* reset site energy */
1339 /* get energy,force and virial of every atom */
1340 for(i=0;i<count;i++) {
1342 /* single particle potential/force */
1343 if(itom[i].attr&ATOM_ATTR_1BP)
1344 moldyn->func1b(moldyn,&(itom[i]));
1346 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1349 /* 2 body pair potential/force */
1351 link_cell_neighbour_index(moldyn,
1352 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1353 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1354 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1361 this=&(neighbour_i[j]);
1364 if(this->start==NULL)
1370 jtom=this->current->data;
1372 if(jtom==&(itom[i]))
1375 if((jtom->attr&ATOM_ATTR_2BP)&
1376 (itom[i].attr&ATOM_ATTR_2BP)) {
1377 moldyn->func2b(moldyn,
1383 /* 3 body potential/force */
1385 if(!(itom[i].attr&ATOM_ATTR_3BP)||
1386 !(jtom->attr&ATOM_ATTR_3BP))
1389 /* copy the neighbour lists */
1390 memcpy(neighbour_i2,neighbour_i,
1393 /* get neighbours of i */
1396 that=&(neighbour_i2[k]);
1399 if(that->start==NULL)
1406 ktom=that->current->data;
1408 if(!(ktom->attr&ATOM_ATTR_3BP))
1414 if(ktom==&(itom[i]))
1417 moldyn->func3b(moldyn,
1423 } while(list_next_f(that)!=\
1428 /* 2bp post function */
1429 if(moldyn->func2b_post) {
1430 moldyn->func2b_post(moldyn,
1435 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1452 * virial calculation
1455 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
1456 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
1458 a->virial.xx+=f->x*d->x;
1459 a->virial.yy+=f->y*d->y;
1460 a->virial.zz+=f->z*d->z;
1461 a->virial.xy+=f->x*d->y;
1462 a->virial.xz+=f->x*d->z;
1463 a->virial.yz+=f->y*d->z;
1469 * periodic boundayr checking
1472 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1473 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1484 if(moldyn->status&MOLDYN_STAT_PBX) {
1485 if(a->x>=x) a->x-=dim->x;
1486 else if(-a->x>x) a->x+=dim->x;
1488 if(moldyn->status&MOLDYN_STAT_PBY) {
1489 if(a->y>=y) a->y-=dim->y;
1490 else if(-a->y>y) a->y+=dim->y;
1492 if(moldyn->status&MOLDYN_STAT_PBZ) {
1493 if(a->z>=z) a->z-=dim->z;
1494 else if(-a->z>z) a->z+=dim->z;
1501 * debugging / critical check functions
1504 int moldyn_bc_check(t_moldyn *moldyn) {
1517 for(i=0;i<moldyn->count;i++) {
1518 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
1519 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
1520 i,atom[i].r.x,dim->x/2);
1521 printf("diagnostic:\n");
1522 printf("-----------\natom.r.x:\n");
1524 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
1527 ((byte)&(1<<k))?1:0,
1530 printf("---------------\nx=dim.x/2:\n");
1532 memcpy(&byte,(u8 *)(&x)+j,1);
1535 ((byte)&(1<<k))?1:0,
1538 if(atom[i].r.x==x) printf("the same!\n");
1539 else printf("different!\n");
1541 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
1542 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
1543 i,atom[i].r.y,dim->y/2);
1544 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
1545 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
1546 i,atom[i].r.z,dim->z/2);