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) {
172 int set_potential2b(t_moldyn *moldyn,pf_func2b func) {
179 int set_potential2b_post(t_moldyn *moldyn,pf_func2b_post func) {
181 moldyn->func2b_post=func;
186 int set_potential3b(t_moldyn *moldyn,pf_func3b func) {
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->pwrite=timer;
251 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
252 moldyn->pfd=open(filename,
253 O_WRONLY|O_CREAT|O_EXCL,
256 perror("[moldyn] pressure log file\n");
259 dprintf(moldyn->pfd,"# pressure log file\n");
260 printf("pressure (%d)\n",timer);
262 case LOG_TEMPERATURE:
263 moldyn->twrite=timer;
264 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
265 moldyn->tfd=open(filename,
266 O_WRONLY|O_CREAT|O_EXCL,
269 perror("[moldyn] temperature log file\n");
272 dprintf(moldyn->tfd,"# temperature log file\n");
273 printf("temperature (%d)\n",timer);
276 moldyn->swrite=timer;
277 printf("save file (%d)\n",timer);
280 moldyn->vwrite=timer;
281 ret=visual_init(&(moldyn->vis),moldyn->vlsdir);
283 printf("[moldyn] visual init failure\n");
286 printf("visual file (%d)\n",timer);
289 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
290 moldyn->rfd=open(filename,
291 O_WRONLY|O_CREAT|O_EXCL,
294 perror("[moldyn] report fd open");
298 snprintf(filename,127,"%s/e_plot.scr",
300 moldyn->epfd=open(filename,
301 O_WRONLY|O_CREAT|O_EXCL,
304 perror("[moldyn] energy plot fd open");
307 dprintf(moldyn->epfd,e_plot_script);
311 snprintf(filename,127,"%s/pressure_plot.scr",
313 moldyn->ppfd=open(filename,
314 O_WRONLY|O_CREAT|O_EXCL,
317 perror("[moldyn] p plot fd open");
320 dprintf(moldyn->ppfd,pressure_plot_script);
324 snprintf(filename,127,"%s/temperature_plot.scr",
326 moldyn->tpfd=open(filename,
327 O_WRONLY|O_CREAT|O_EXCL,
330 perror("[moldyn] t plot fd open");
333 dprintf(moldyn->tpfd,temperature_plot_script);
336 dprintf(moldyn->rfd,report_start,
337 moldyn->rauthor,moldyn->rtitle);
340 printf("unknown log type: %02x\n",type);
347 int moldyn_log_shutdown(t_moldyn *moldyn) {
351 printf("[moldyn] log shutdown\n");
355 dprintf(moldyn->rfd,report_energy);
356 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
361 if(moldyn->mfd) close(moldyn->mfd);
365 dprintf(moldyn->rfd,report_pressure);
366 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
373 dprintf(moldyn->rfd,report_temperature);
374 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
379 dprintf(moldyn->rfd,report_end);
381 snprintf(sc,255,"cd %s && pdflatex report",moldyn->vlsdir);
383 snprintf(sc,255,"cd %s && pdflatex report",moldyn->vlsdir);
385 snprintf(sc,255,"cd %s && dvipdf report",moldyn->vlsdir);
388 if(&(moldyn->vis)) visual_tini(&(moldyn->vis));
394 * creating lattice functions
397 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
398 u8 attr,u8 brand,int a,int b,int c) {
409 /* how many atoms do we expect */
410 if(type==CUBIC) new*=1;
411 if(type==FCC) new*=4;
412 if(type==DIAMOND) new*=8;
414 /* allocate space for atoms */
415 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
417 perror("[moldyn] realloc (create lattice)");
421 atom=&(moldyn->atom[count]);
423 /* no atoms on the boundaries (only reason: it looks better!) */
430 set_nn_dist(moldyn,lc);
431 ret=cubic_init(a,b,c,lc,atom,&origin);
434 v3_scale(&origin,&origin,0.5);
435 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
436 ret=fcc_init(a,b,c,lc,atom,&origin);
439 v3_scale(&origin,&origin,0.25);
440 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
441 ret=diamond_init(a,b,c,lc,atom,&origin);
444 printf("unknown lattice type (%02x)\n",type);
450 printf("[moldyn] creating lattice failed\n");
451 printf(" amount of atoms\n");
452 printf(" - expected: %d\n",new);
453 printf(" - created: %d\n",ret);
458 printf("[moldyn] created lattice with %d atoms\n",new);
460 for(ret=0;ret<new;ret++) {
461 atom[ret].element=element;
464 atom[ret].brand=brand;
465 atom[ret].tag=count+ret;
466 check_per_bound(moldyn,&(atom[ret].r));
473 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
492 v3_copy(&(atom[count].r),&r);
501 for(i=0;i<count;i++) {
502 atom[i].r.x-=(a*lc)/2.0;
503 atom[i].r.y-=(b*lc)/2.0;
504 atom[i].r.z-=(c*lc)/2.0;
510 /* fcc lattice init */
511 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
524 /* construct the basis */
525 memset(basis,0,3*sizeof(t_3dvec));
533 /* fill up the room */
541 v3_copy(&(atom[count].r),&r);
544 /* the three face centered atoms */
546 v3_add(&n,&r,&basis[l]);
547 v3_copy(&(atom[count].r),&n);
556 /* coordinate transformation */
557 for(i=0;i<count;i++) {
558 atom[i].r.x-=(a*lc)/2.0;
559 atom[i].r.y-=(b*lc)/2.0;
560 atom[i].r.z-=(c*lc)/2.0;
566 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
571 count=fcc_init(a,b,c,lc,atom,origin);
577 if(origin) v3_add(&o,&o,origin);
579 count+=fcc_init(a,b,c,lc,&atom[count],&o);
584 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
585 t_3dvec *r,t_3dvec *v) {
592 count=(moldyn->count)++;
594 ptr=realloc(atom,(count+1)*sizeof(t_atom));
596 perror("[moldyn] realloc (add atom)");
604 atom[count].element=element;
605 atom[count].mass=mass;
606 atom[count].brand=brand;
607 atom[count].tag=count;
608 atom[count].attr=attr;
613 int destroy_atoms(t_moldyn *moldyn) {
615 if(moldyn->atom) free(moldyn->atom);
620 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
623 * - gaussian distribution of velocities
624 * - zero total momentum
625 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
630 t_3dvec p_total,delta;
635 random=&(moldyn->random);
637 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
639 /* gaussian distribution of velocities */
641 for(i=0;i<moldyn->count;i++) {
642 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
644 v=sigma*rand_get_gauss(random);
646 p_total.x+=atom[i].mass*v;
648 v=sigma*rand_get_gauss(random);
650 p_total.y+=atom[i].mass*v;
652 v=sigma*rand_get_gauss(random);
654 p_total.z+=atom[i].mass*v;
657 /* zero total momentum */
658 v3_scale(&p_total,&p_total,1.0/moldyn->count);
659 for(i=0;i<moldyn->count;i++) {
660 v3_scale(&delta,&p_total,1.0/atom[i].mass);
661 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
664 /* velocity scaling */
665 scale_velocity(moldyn,equi_init);
670 double temperature_calc(t_moldyn *moldyn) {
672 /* assume up to date kinetic energy, which is 3/2 N k_B T */
674 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
679 double get_temperature(t_moldyn *moldyn) {
684 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
694 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
697 /* get kinetic energy / temperature & count involved atoms */
700 for(i=0;i<moldyn->count;i++) {
701 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
702 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
707 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
708 else return 0; /* no atoms involved in scaling! */
710 /* (temporary) hack for e,t = 0 */
713 if(moldyn->t_ref!=0.0) {
714 thermal_init(moldyn,equi_init);
718 return 0; /* no scaling needed */
722 /* get scaling factor */
723 scale=moldyn->t_ref/moldyn->t;
727 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
728 scale=1.0+(scale-1.0)/moldyn->t_tc;
731 /* velocity scaling */
732 for(i=0;i<moldyn->count;i++) {
733 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
734 v3_scale(&(atom[i].v),&(atom[i].v),scale);
740 double ideal_gas_law_pressure(t_moldyn *moldyn) {
744 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
749 double pressure_calc(t_moldyn *moldyn) {
757 * W = 1/3 sum_i f_i r_i
758 * virial = sum_i f_i r_i
760 * => P = (2 Ekin + virial) / (3V)
764 for(i=0;i<moldyn->count;i++) {
765 virial=&(moldyn->atom[i].virial);
766 v+=(virial->xx+virial->yy+virial->zz);
769 /* assume up to date kinetic energy */
770 moldyn->p=2.0*moldyn->ekin+v;
771 moldyn->p/=(3.0*moldyn->volume);
776 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
788 * dV: dx,y,z = 0.001 x,y,z
792 printf("\n\nP-DEBUG:\n");
795 store=malloc(moldyn->count*sizeof(t_atom));
797 printf("[moldyn] allocating store mem failed\n");
801 /* save unscaled potential energy + atom/dim configuration */
803 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
806 /* derivative with respect to x direction */
807 scale_dim(moldyn,scale,TRUE,0,0);
808 scale_atoms(moldyn,scale,TRUE,0,0);
809 dv=0.00001*moldyn->dim.x*moldyn->dim.y*moldyn->dim.z;
810 link_cell_shutdown(moldyn);
811 link_cell_init(moldyn,QUIET);
812 potential_force_calc(moldyn);
813 tp->x=(moldyn->energy-u)/dv;
815 printf("e: %f eV de: %f eV dV: %f A^3\n",moldyn->energy/moldyn->count/EV,(moldyn->energy-u)/moldyn->count/EV,dv);
817 /* restore atomic configuration + dim */
818 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
821 /* derivative with respect to y direction */
822 scale_dim(moldyn,scale,0,TRUE,0);
823 scale_atoms(moldyn,scale,0,TRUE,0);
824 dv=0.00001*moldyn->dim.y*moldyn->dim.x*moldyn->dim.z;
825 link_cell_shutdown(moldyn);
826 link_cell_init(moldyn,QUIET);
827 potential_force_calc(moldyn);
828 tp->y=(moldyn->energy-u)/dv;
831 /* restore atomic configuration + dim */
832 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
835 /* derivative with respect to z direction */
836 scale_dim(moldyn,scale,0,0,TRUE);
837 scale_atoms(moldyn,scale,0,0,TRUE);
838 dv=0.00001*moldyn->dim.z*moldyn->dim.x*moldyn->dim.y;
839 link_cell_shutdown(moldyn);
840 link_cell_init(moldyn,QUIET);
841 potential_force_calc(moldyn);
842 tp->z=(moldyn->energy-u)/dv;
845 /* restore atomic configuration + dim */
846 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
852 link_cell_shutdown(moldyn);
853 link_cell_init(moldyn,QUIET);
858 double get_pressure(t_moldyn *moldyn) {
864 int scale_dim(t_moldyn *moldyn,double scale,u8 x,u8 y,u8 z) {
877 int scale_atoms(t_moldyn *moldyn,double scale,u8 x,u8 y,u8 z) {
882 for(i=0;i<moldyn->count;i++) {
883 r=&(moldyn->atom[i].r);
892 int scale_volume(t_moldyn *moldyn) {
898 vdim=&(moldyn->vis.dim);
903 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
904 scale=1.0-(moldyn->p_ref-moldyn->p)/moldyn->p_tc;
905 scale=pow(scale,ONE_THIRD);
908 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
912 /* scale the atoms and dimensions */
913 scale_atoms(moldyn,scale,TRUE,TRUE,TRUE);
914 scale_dim(moldyn,scale,TRUE,TRUE,TRUE);
916 /* visualize dimensions */
923 /* recalculate scaled volume */
924 moldyn->volume=dim->x*dim->y*dim->z;
926 /* adjust/reinit linkcell */
927 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
928 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
929 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
930 link_cell_shutdown(moldyn);
931 link_cell_init(moldyn,QUIET);
942 double get_e_kin(t_moldyn *moldyn) {
950 for(i=0;i<moldyn->count;i++)
951 moldyn->ekin+=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
956 double update_e_kin(t_moldyn *moldyn) {
958 return(get_e_kin(moldyn));
961 double get_total_energy(t_moldyn *moldyn) {
963 return(moldyn->ekin+moldyn->energy);
966 t_3dvec get_total_p(t_moldyn *moldyn) {
975 for(i=0;i<moldyn->count;i++) {
976 v3_scale(&p,&(atom[i].v),atom[i].mass);
977 v3_add(&p_total,&p_total,&p);
983 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
987 /* nn_dist is the nearest neighbour distance */
989 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
998 /* linked list / cell method */
1000 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1007 /* partitioning the md cell */
1008 lc->nx=moldyn->dim.x/moldyn->cutoff;
1009 lc->x=moldyn->dim.x/lc->nx;
1010 lc->ny=moldyn->dim.y/moldyn->cutoff;
1011 lc->y=moldyn->dim.y/lc->ny;
1012 lc->nz=moldyn->dim.z/moldyn->cutoff;
1013 lc->z=moldyn->dim.z/lc->nz;
1015 lc->cells=lc->nx*lc->ny*lc->nz;
1016 lc->subcell=malloc(lc->cells*sizeof(t_list));
1019 printf("[moldyn] FATAL: less then 27 subcells!\n");
1021 if(vol) printf("[moldyn] initializing linked cells (%d)\n",lc->cells);
1023 for(i=0;i<lc->cells;i++)
1024 list_init_f(&(lc->subcell[i]));
1026 link_cell_update(moldyn);
1031 int link_cell_update(t_moldyn *moldyn) {
1049 for(i=0;i<lc->cells;i++)
1050 list_destroy_f(&(lc->subcell[i]));
1052 for(count=0;count<moldyn->count;count++) {
1053 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1054 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1055 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1056 list_add_immediate_f(&(moldyn->lc.subcell[i+j*nx+k*nx*ny]),
1063 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell) {
1081 cell[0]=lc->subcell[i+j*nx+k*a];
1082 for(ci=-1;ci<=1;ci++) {
1085 if((x<0)||(x>=nx)) {
1089 for(cj=-1;cj<=1;cj++) {
1092 if((y<0)||(y>=ny)) {
1096 for(ck=-1;ck<=1;ck++) {
1099 if((z<0)||(z>=nz)) {
1103 if(!(ci|cj|ck)) continue;
1105 cell[--count2]=lc->subcell[x+y*nx+z*a];
1108 cell[count1++]=lc->subcell[x+y*nx+z*a];
1119 int link_cell_shutdown(t_moldyn *moldyn) {
1126 for(i=0;i<lc->nx*lc->ny*lc->nz;i++)
1127 list_destroy_f(&(moldyn->lc.subcell[i]));
1134 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1138 t_moldyn_schedule *schedule;
1140 schedule=&(moldyn->schedule);
1141 count=++(schedule->total_sched);
1143 ptr=realloc(schedule->runs,count*sizeof(int));
1145 perror("[moldyn] realloc (runs)");
1149 schedule->runs[count-1]=runs;
1151 ptr=realloc(schedule->tau,count*sizeof(double));
1153 perror("[moldyn] realloc (tau)");
1157 schedule->tau[count-1]=tau;
1159 printf("[moldyn] schedule added:\n");
1160 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1166 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1168 moldyn->schedule.hook=hook;
1169 moldyn->schedule.hook_params=hook_params;
1176 * 'integration of newtons equation' - algorithms
1180 /* start the integration */
1182 int moldyn_integrate(t_moldyn *moldyn) {
1185 unsigned int e,m,s,v,p,t;
1187 t_moldyn_schedule *sched;
1192 double energy_scale;
1195 sched=&(moldyn->schedule);
1198 /* initialize linked cell method */
1199 link_cell_init(moldyn,VERBOSE);
1201 /* logging & visualization */
1209 /* sqaure of some variables */
1210 moldyn->tau_square=moldyn->tau*moldyn->tau;
1211 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
1213 /* energy scaling factor */
1214 energy_scale=moldyn->count*EV;
1216 /* calculate initial forces */
1217 potential_force_calc(moldyn);
1219 /* some stupid checks before we actually start calculating bullshit */
1220 if(moldyn->cutoff>0.5*moldyn->dim.x)
1221 printf("[moldyn] warning: cutoff > 0.5 x dim.x\n");
1222 if(moldyn->cutoff>0.5*moldyn->dim.y)
1223 printf("[moldyn] warning: cutoff > 0.5 x dim.y\n");
1224 if(moldyn->cutoff>0.5*moldyn->dim.z)
1225 printf("[moldyn] warning: cutoff > 0.5 x dim.z\n");
1226 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1227 if(ds>0.05*moldyn->nnd)
1228 printf("[moldyn] warning: forces too high / tau too small!\n");
1230 /* zero absolute time */
1233 /* debugging, ignore */
1236 /* tell the world */
1237 printf("[moldyn] integration start, go get a coffee ...\n");
1239 /* executing the schedule */
1240 for(sched->count=0;sched->count<sched->total_sched;sched->count++) {
1242 /* setting amount of runs and finite time step size */
1243 moldyn->tau=sched->tau[sched->count];
1244 moldyn->tau_square=moldyn->tau*moldyn->tau;
1245 moldyn->time_steps=sched->runs[sched->count];
1247 /* integration according to schedule */
1249 for(i=0;i<moldyn->time_steps;i++) {
1251 /* integration step */
1252 moldyn->integrate(moldyn);
1254 /* calculate kinetic energy, temperature and pressure */
1255 update_e_kin(moldyn);
1256 temperature_calc(moldyn);
1257 pressure_calc(moldyn);
1258 //tp=thermodynamic_pressure_calc(moldyn);
1261 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1262 scale_velocity(moldyn,FALSE);
1263 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1264 scale_volume(moldyn);
1266 /* check for log & visualization */
1269 dprintf(moldyn->efd,
1271 moldyn->time,moldyn->ekin/energy_scale,
1272 moldyn->energy/energy_scale,
1273 get_total_energy(moldyn)/energy_scale);
1277 momentum=get_total_p(moldyn);
1278 dprintf(moldyn->mfd,
1279 "%f %f %f %f %f\n",moldyn->time,
1280 momentum.x,momentum.y,momentum.z,
1281 v3_norm(&momentum));
1286 dprintf(moldyn->pfd,
1287 "%f %f\n",moldyn->time,moldyn->p/ATM);
1292 dprintf(moldyn->tfd,
1293 "%f %f\n",moldyn->time,moldyn->t);
1298 snprintf(dir,128,"%s/s-%07.f.save",
1299 moldyn->vlsdir,moldyn->time);
1300 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT);
1301 if(fd<0) perror("[moldyn] save fd open");
1303 write(fd,moldyn,sizeof(t_moldyn));
1304 write(fd,moldyn->atom,
1305 moldyn->count*sizeof(t_atom));
1312 visual_atoms(&(moldyn->vis),moldyn->time,
1313 moldyn->atom,moldyn->count);
1317 /* display progress */
1319 printf("\rsched: %d, steps: %d, T: %f, P: %f V: %f",
1321 moldyn->t,moldyn->p/ATM,moldyn->volume);
1325 /* increase absolute time */
1326 moldyn->time+=moldyn->tau;
1330 /* check for hooks */
1332 sched->hook(moldyn,sched->hook_params);
1334 /* get a new info line */
1342 /* velocity verlet */
1344 int velocity_verlet(t_moldyn *moldyn) {
1347 double tau,tau_square,h;
1352 count=moldyn->count;
1354 tau_square=moldyn->tau_square;
1356 for(i=0;i<count;i++) {
1359 v3_scale(&delta,&(atom[i].v),tau);
1360 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1361 v3_scale(&delta,&(atom[i].f),h*tau_square);
1362 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1363 check_per_bound(moldyn,&(atom[i].r));
1365 /* velocities [actually v(t+tau/2)] */
1366 v3_scale(&delta,&(atom[i].f),h*tau);
1367 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1370 /* neighbour list update */
1371 link_cell_update(moldyn);
1373 /* forces depending on chosen potential */
1374 potential_force_calc(moldyn);
1376 for(i=0;i<count;i++) {
1377 /* again velocities [actually v(t+tau)] */
1378 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1379 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1388 * potentials & corresponding forces & virial routine
1392 /* generic potential and force calculation */
1394 int potential_force_calc(t_moldyn *moldyn) {
1397 t_atom *itom,*jtom,*ktom;
1400 t_list neighbour_i[27];
1401 t_list neighbour_i2[27];
1406 count=moldyn->count;
1413 /* reset force, site energy and virial of every atom */
1414 for(i=0;i<count;i++) {
1417 v3_zero(&(itom[i].f));
1420 virial=(&(itom[i].virial));
1428 /* reset site energy */
1433 /* get energy,force and virial of every atom */
1434 for(i=0;i<count;i++) {
1436 /* single particle potential/force */
1437 if(itom[i].attr&ATOM_ATTR_1BP)
1438 moldyn->func1b(moldyn,&(itom[i]));
1440 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1443 /* 2 body pair potential/force */
1445 link_cell_neighbour_index(moldyn,
1446 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1447 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1448 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1455 this=&(neighbour_i[j]);
1458 if(this->start==NULL)
1464 jtom=this->current->data;
1466 if(jtom==&(itom[i]))
1469 if((jtom->attr&ATOM_ATTR_2BP)&
1470 (itom[i].attr&ATOM_ATTR_2BP)) {
1471 moldyn->func2b(moldyn,
1477 /* 3 body potential/force */
1479 if(!(itom[i].attr&ATOM_ATTR_3BP)||
1480 !(jtom->attr&ATOM_ATTR_3BP))
1483 /* copy the neighbour lists */
1484 memcpy(neighbour_i2,neighbour_i,
1487 /* get neighbours of i */
1490 that=&(neighbour_i2[k]);
1493 if(that->start==NULL)
1500 ktom=that->current->data;
1502 if(!(ktom->attr&ATOM_ATTR_3BP))
1508 if(ktom==&(itom[i]))
1511 moldyn->func3b(moldyn,
1517 } while(list_next_f(that)!=\
1522 /* 2bp post function */
1523 if(moldyn->func2b_post) {
1524 moldyn->func2b_post(moldyn,
1529 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1546 * virial calculation
1549 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
1550 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
1552 a->virial.xx+=f->x*d->x;
1553 a->virial.yy+=f->y*d->y;
1554 a->virial.zz+=f->z*d->z;
1555 a->virial.xy+=f->x*d->y;
1556 a->virial.xz+=f->x*d->z;
1557 a->virial.yz+=f->y*d->z;
1563 * periodic boundayr checking
1566 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1567 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
1578 if(moldyn->status&MOLDYN_STAT_PBX) {
1579 if(a->x>=x) a->x-=dim->x;
1580 else if(-a->x>x) a->x+=dim->x;
1582 if(moldyn->status&MOLDYN_STAT_PBY) {
1583 if(a->y>=y) a->y-=dim->y;
1584 else if(-a->y>y) a->y+=dim->y;
1586 if(moldyn->status&MOLDYN_STAT_PBZ) {
1587 if(a->z>=z) a->z-=dim->z;
1588 else if(-a->z>z) a->z+=dim->z;
1595 * debugging / critical check functions
1598 int moldyn_bc_check(t_moldyn *moldyn) {
1611 for(i=0;i<moldyn->count;i++) {
1612 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
1613 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
1614 i,atom[i].r.x,dim->x/2);
1615 printf("diagnostic:\n");
1616 printf("-----------\natom.r.x:\n");
1618 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
1621 ((byte)&(1<<k))?1:0,
1624 printf("---------------\nx=dim.x/2:\n");
1626 memcpy(&byte,(u8 *)(&x)+j,1);
1629 ((byte)&(1<<k))?1:0,
1632 if(atom[i].r.x==x) printf("the same!\n");
1633 else printf("different!\n");
1635 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
1636 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
1637 i,atom[i].r.y,dim->y/2);
1638 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
1639 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
1640 i,atom[i].r.z,dim->z/2);