2 * moldyn.c - molecular dynamics library main file
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
12 #include <sys/types.h>
20 #include <fpu_control.h>
27 #include "report/report.h"
29 /* potential includes */
30 #include "potentials/harmonic_oscillator.h"
31 #include "potentials/lennard_jones.h"
32 #include "potentials/albe.h"
34 #include "potentials/tersoff_orig.h"
36 #include "potentials/tersoff.h"
47 * the moldyn functions
50 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
52 printf("[moldyn] init\n");
54 /* only needed if compiled without -msse2 (float-store prob!) */
57 memset(moldyn,0,sizeof(t_moldyn));
62 rand_init(&(moldyn->random),NULL,1);
63 moldyn->random.status|=RAND_STAT_VERBOSE;
68 int moldyn_shutdown(t_moldyn *moldyn) {
70 printf("[moldyn] shutdown\n");
72 moldyn_log_shutdown(moldyn);
73 link_cell_shutdown(moldyn);
74 rand_close(&(moldyn->random));
80 int set_int_alg(t_moldyn *moldyn,u8 algo) {
82 printf("[moldyn] integration algorithm: ");
85 case MOLDYN_INTEGRATE_VERLET:
86 moldyn->integrate=velocity_verlet;
87 printf("velocity verlet\n");
90 printf("unknown integration algorithm: %02x\n",algo);
98 int set_cutoff(t_moldyn *moldyn,double cutoff) {
100 moldyn->cutoff=cutoff;
101 moldyn->cutoff_square=cutoff*cutoff;
103 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
108 int set_temperature(t_moldyn *moldyn,double t_ref) {
112 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
117 int set_pressure(t_moldyn *moldyn,double p_ref) {
121 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
126 int set_p_scale(t_moldyn *moldyn,u8 ptype,double ptc) {
128 moldyn->pt_scale&=(~(P_SCALE_MASK));
129 moldyn->pt_scale|=ptype;
132 printf("[moldyn] p scaling:\n");
134 printf(" p: %s",ptype?"yes":"no ");
136 printf(" | type: %02x | factor: %f",ptype,ptc);
142 int set_t_scale(t_moldyn *moldyn,u8 ttype,double ttc) {
144 moldyn->pt_scale&=(~(T_SCALE_MASK));
145 moldyn->pt_scale|=ttype;
148 printf("[moldyn] t scaling:\n");
150 printf(" t: %s",ttype?"yes":"no ");
152 printf(" | type: %02x | factor: %f",ttype,ttc);
158 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
160 moldyn->pt_scale=(ptype|ttype);
164 printf("[moldyn] p/t scaling:\n");
166 printf(" p: %s",ptype?"yes":"no ");
168 printf(" | type: %02x | factor: %f",ptype,ptc);
171 printf(" t: %s",ttype?"yes":"no ");
173 printf(" | type: %02x | factor: %f",ttype,ttc);
179 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
185 moldyn->volume=x*y*z;
193 printf("[moldyn] dimensions in A and A^3 respectively:\n");
194 printf(" x: %f\n",moldyn->dim.x);
195 printf(" y: %f\n",moldyn->dim.y);
196 printf(" z: %f\n",moldyn->dim.z);
197 printf(" volume: %f\n",moldyn->volume);
198 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
203 int set_nn_dist(t_moldyn *moldyn,double dist) {
210 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
212 printf("[moldyn] periodic boundary conditions:\n");
215 moldyn->status|=MOLDYN_STAT_PBX;
218 moldyn->status|=MOLDYN_STAT_PBY;
221 moldyn->status|=MOLDYN_STAT_PBZ;
223 printf(" x: %s\n",x?"yes":"no");
224 printf(" y: %s\n",y?"yes":"no");
225 printf(" z: %s\n",z?"yes":"no");
230 int set_potential(t_moldyn *moldyn,u8 type) {
233 case MOLDYN_POTENTIAL_TM:
234 moldyn->func1b=tersoff_mult_1bp;
235 moldyn->func3b_j1=tersoff_mult_3bp_j1;
236 moldyn->func3b_k1=tersoff_mult_3bp_k1;
237 moldyn->func3b_j2=tersoff_mult_3bp_j2;
238 moldyn->func3b_k2=tersoff_mult_3bp_k2;
239 moldyn->check_2b_bond=tersoff_mult_check_2b_bond;
241 case MOLDYN_POTENTIAL_AM:
242 moldyn->func3b_j1=albe_mult_3bp_j1;
243 moldyn->func3b_k1=albe_mult_3bp_k1;
244 moldyn->func3b_j2=albe_mult_3bp_j2;
245 moldyn->func3b_k2=albe_mult_3bp_k2;
246 moldyn->check_2b_bond=albe_mult_check_2b_bond;
248 case MOLDYN_POTENTIAL_HO:
249 moldyn->func2b=harmonic_oscillator;
250 moldyn->check_2b_bond=harmonic_oscillator_check_2b_bond;
252 case MOLDYN_POTENTIAL_LJ:
253 moldyn->func2b=lennard_jones;
254 moldyn->check_2b_bond=lennard_jones_check_2b_bond;
257 printf("[moldyn] set potential: unknown type %02x\n",
265 int set_avg_skip(t_moldyn *moldyn,int skip) {
267 printf("[moldyn] skip %d steps before starting average calc\n",skip);
268 moldyn->avg_skip=skip;
273 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
275 strncpy(moldyn->vlsdir,dir,127);
280 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
282 strncpy(moldyn->rauthor,author,63);
283 strncpy(moldyn->rtitle,title,63);
288 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
293 printf("[moldyn] set log: ");
296 case LOG_TOTAL_ENERGY:
297 moldyn->ewrite=timer;
298 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
299 moldyn->efd=open(filename,
300 O_WRONLY|O_CREAT|O_EXCL,
303 perror("[moldyn] energy log fd open");
306 dprintf(moldyn->efd,"# total energy log file\n");
307 printf("total energy (%d)\n",timer);
309 case LOG_TOTAL_MOMENTUM:
310 moldyn->mwrite=timer;
311 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
312 moldyn->mfd=open(filename,
313 O_WRONLY|O_CREAT|O_EXCL,
316 perror("[moldyn] momentum log fd open");
319 dprintf(moldyn->efd,"# total momentum log file\n");
320 printf("total momentum (%d)\n",timer);
323 moldyn->pwrite=timer;
324 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
325 moldyn->pfd=open(filename,
326 O_WRONLY|O_CREAT|O_EXCL,
329 perror("[moldyn] pressure log file\n");
332 dprintf(moldyn->pfd,"# pressure log file\n");
333 printf("pressure (%d)\n",timer);
335 case LOG_TEMPERATURE:
336 moldyn->twrite=timer;
337 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
338 moldyn->tfd=open(filename,
339 O_WRONLY|O_CREAT|O_EXCL,
342 perror("[moldyn] temperature log file\n");
345 dprintf(moldyn->tfd,"# temperature log file\n");
346 printf("temperature (%d)\n",timer);
349 moldyn->vwrite=timer;
350 snprintf(filename,127,"%s/volume",moldyn->vlsdir);
351 moldyn->vfd=open(filename,
352 O_WRONLY|O_CREAT|O_EXCL,
355 perror("[moldyn] volume log file\n");
358 dprintf(moldyn->vfd,"# volume log file\n");
359 printf("volume (%d)\n",timer);
362 moldyn->swrite=timer;
363 printf("save file (%d)\n",timer);
366 moldyn->awrite=timer;
367 ret=visual_init(moldyn,moldyn->vlsdir);
369 printf("[moldyn] visual init failure\n");
372 printf("visual file (%d)\n",timer);
375 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
376 moldyn->rfd=open(filename,
377 O_WRONLY|O_CREAT|O_EXCL,
380 perror("[moldyn] report fd open");
383 printf("report -> ");
385 snprintf(filename,127,"%s/e_plot.scr",
387 moldyn->epfd=open(filename,
388 O_WRONLY|O_CREAT|O_EXCL,
391 perror("[moldyn] energy plot fd open");
394 dprintf(moldyn->epfd,e_plot_script);
399 snprintf(filename,127,"%s/pressure_plot.scr",
401 moldyn->ppfd=open(filename,
402 O_WRONLY|O_CREAT|O_EXCL,
405 perror("[moldyn] p plot fd open");
408 dprintf(moldyn->ppfd,pressure_plot_script);
413 snprintf(filename,127,"%s/temperature_plot.scr",
415 moldyn->tpfd=open(filename,
416 O_WRONLY|O_CREAT|O_EXCL,
419 perror("[moldyn] t plot fd open");
422 dprintf(moldyn->tpfd,temperature_plot_script);
424 printf("temperature ");
426 dprintf(moldyn->rfd,report_start,
427 moldyn->rauthor,moldyn->rtitle);
431 printf("unknown log type: %02x\n",type);
438 int moldyn_log_shutdown(t_moldyn *moldyn) {
442 printf("[moldyn] log shutdown\n");
446 dprintf(moldyn->rfd,report_energy);
447 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
452 if(moldyn->mfd) close(moldyn->mfd);
456 dprintf(moldyn->rfd,report_pressure);
457 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
464 dprintf(moldyn->rfd,report_temperature);
465 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
470 dprintf(moldyn->rfd,report_end);
472 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
475 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
478 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
487 * creating lattice functions
490 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
491 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
503 /* how many atoms do we expect */
506 printf("[moldyn] WARNING: create 'none' lattice called");
508 if(type==CUBIC) new*=1;
509 if(type==FCC) new*=4;
510 if(type==DIAMOND) new*=8;
512 /* allocate space for atoms */
513 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
515 perror("[moldyn] realloc (create lattice)");
519 atom=&(moldyn->atom[count]);
521 /* no atoms on the boundaries (only reason: it looks better!) */
535 set_nn_dist(moldyn,lc);
536 ret=cubic_init(a,b,c,lc,atom,&orig);
537 strcpy(name,"cubic");
541 v3_scale(&orig,&orig,0.5);
542 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
543 ret=fcc_init(a,b,c,lc,atom,&orig);
548 v3_scale(&orig,&orig,0.25);
549 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
550 ret=diamond_init(a,b,c,lc,atom,&orig);
551 strcpy(name,"diamond");
554 printf("unknown lattice type (%02x)\n",type);
560 printf("[moldyn] creating lattice failed\n");
561 printf(" amount of atoms\n");
562 printf(" - expected: %d\n",new);
563 printf(" - created: %d\n",ret);
568 printf("[moldyn] created %s lattice with %d atoms\n",name,new);
570 for(ret=0;ret<new;ret++) {
571 atom[ret].element=element;
574 atom[ret].brand=brand;
575 atom[ret].tag=count+ret;
576 check_per_bound(moldyn,&(atom[ret].r));
577 atom[ret].r_0=atom[ret].r;
580 /* update total system mass */
581 total_mass_calc(moldyn);
586 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
587 t_3dvec *r,t_3dvec *v) {
594 count=(moldyn->count)++; // asshole style!
596 ptr=realloc(atom,(count+1)*sizeof(t_atom));
598 perror("[moldyn] realloc (add atom)");
604 ptr=realloc(moldyn->lc.subcell->list,(count+1)*sizeof(int));
606 perror("[moldyn] list realloc (add atom)");
609 moldyn->lc.subcell->list=ptr;
614 /* initialize new atom */
615 memset(&(atom[count]),0,sizeof(t_atom));
618 atom[count].element=element;
619 atom[count].mass=mass;
620 atom[count].brand=brand;
621 atom[count].tag=count;
622 atom[count].attr=attr;
623 check_per_bound(moldyn,&(atom[count].r));
624 atom[count].r_0=atom[count].r;
626 /* update total system mass */
627 total_mass_calc(moldyn);
632 int del_atom(t_moldyn *moldyn,int tag) {
639 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
641 perror("[moldyn]malloc (del atom)");
645 for(cnt=0;cnt<tag;cnt++)
648 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
650 new[cnt-1].tag=cnt-1;
662 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
681 v3_copy(&(atom[count].r),&r);
690 for(i=0;i<count;i++) {
691 atom[i].r.x-=(a*lc)/2.0;
692 atom[i].r.y-=(b*lc)/2.0;
693 atom[i].r.z-=(c*lc)/2.0;
699 /* fcc lattice init */
700 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
713 /* construct the basis */
714 memset(basis,0,3*sizeof(t_3dvec));
722 /* fill up the room */
730 v3_copy(&(atom[count].r),&r);
733 /* the three face centered atoms */
735 v3_add(&n,&r,&basis[l]);
736 v3_copy(&(atom[count].r),&n);
745 /* coordinate transformation */
746 for(i=0;i<count;i++) {
747 atom[i].r.x-=(a*lc)/2.0;
748 atom[i].r.y-=(b*lc)/2.0;
749 atom[i].r.z-=(c*lc)/2.0;
755 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
760 count=fcc_init(a,b,c,lc,atom,origin);
766 if(origin) v3_add(&o,&o,origin);
768 count+=fcc_init(a,b,c,lc,&atom[count],&o);
773 int destroy_atoms(t_moldyn *moldyn) {
775 if(moldyn->atom) free(moldyn->atom);
780 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
783 * - gaussian distribution of velocities
784 * - zero total momentum
785 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
790 t_3dvec p_total,delta;
795 random=&(moldyn->random);
797 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
799 /* gaussian distribution of velocities */
801 for(i=0;i<moldyn->count;i++) {
802 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
804 v=sigma*rand_get_gauss(random);
806 p_total.x+=atom[i].mass*v;
808 v=sigma*rand_get_gauss(random);
810 p_total.y+=atom[i].mass*v;
812 v=sigma*rand_get_gauss(random);
814 p_total.z+=atom[i].mass*v;
817 /* zero total momentum */
818 v3_scale(&p_total,&p_total,1.0/moldyn->count);
819 for(i=0;i<moldyn->count;i++) {
820 v3_scale(&delta,&p_total,1.0/atom[i].mass);
821 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
824 /* velocity scaling */
825 scale_velocity(moldyn,equi_init);
830 double total_mass_calc(t_moldyn *moldyn) {
836 for(i=0;i<moldyn->count;i++)
837 moldyn->mass+=moldyn->atom[i].mass;
842 double temperature_calc(t_moldyn *moldyn) {
844 /* assume up to date kinetic energy, which is 3/2 N k_B T */
846 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
851 double get_temperature(t_moldyn *moldyn) {
856 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
866 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
869 /* get kinetic energy / temperature & count involved atoms */
872 for(i=0;i<moldyn->count;i++) {
873 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
874 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
879 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
880 else return 0; /* no atoms involved in scaling! */
882 /* (temporary) hack for e,t = 0 */
885 if(moldyn->t_ref!=0.0) {
886 thermal_init(moldyn,equi_init);
890 return 0; /* no scaling needed */
894 /* get scaling factor */
895 scale=moldyn->t_ref/moldyn->t;
899 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
900 scale=1.0+(scale-1.0)*moldyn->tau/moldyn->t_tc;
903 /* velocity scaling */
904 for(i=0;i<moldyn->count;i++) {
905 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
906 v3_scale(&(atom[i].v),&(atom[i].v),scale);
912 double ideal_gas_law_pressure(t_moldyn *moldyn) {
916 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
921 double virial_sum(t_moldyn *moldyn) {
926 /* virial (sum over atom virials) */
934 for(i=0;i<moldyn->count;i++) {
935 virial=&(moldyn->atom[i].virial);
936 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
937 moldyn->vir.xx+=virial->xx;
938 moldyn->vir.yy+=virial->yy;
939 moldyn->vir.zz+=virial->zz;
940 moldyn->vir.xy+=virial->xy;
941 moldyn->vir.xz+=virial->xz;
942 moldyn->vir.yz+=virial->yz;
945 /* global virial (absolute coordinates) */
946 virial=&(moldyn->gvir);
947 moldyn->gv=virial->xx+virial->yy+virial->zz;
949 return moldyn->virial;
952 double pressure_calc(t_moldyn *moldyn) {
956 * with W = 1/3 sum_i f_i r_i (- skipped!)
957 * virial = sum_i f_i r_i
959 * => P = (2 Ekin + virial) / (3V)
962 /* assume up to date virial & up to date kinetic energy */
964 /* pressure (atom virials) */
965 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
966 moldyn->p/=(3.0*moldyn->volume);
968 /* pressure (absolute coordinates) */
969 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
970 moldyn->gp/=(3.0*moldyn->volume);
975 int average_reset(t_moldyn *moldyn) {
977 printf("[moldyn] average reset\n");
979 /* update skip value */
980 moldyn->avg_skip=moldyn->total_steps;
986 /* potential energy */
994 moldyn->virial_sum=0.0;
1005 int average_and_fluctuation_calc(t_moldyn *moldyn) {
1009 if(moldyn->total_steps<moldyn->avg_skip)
1012 denom=moldyn->total_steps+1-moldyn->avg_skip;
1014 /* assume up to date energies, temperature, pressure etc */
1016 /* kinetic energy */
1017 moldyn->k_sum+=moldyn->ekin;
1018 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
1019 moldyn->k_avg=moldyn->k_sum/denom;
1020 moldyn->k2_avg=moldyn->k2_sum/denom;
1021 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
1023 /* potential energy */
1024 moldyn->v_sum+=moldyn->energy;
1025 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
1026 moldyn->v_avg=moldyn->v_sum/denom;
1027 moldyn->v2_avg=moldyn->v2_sum/denom;
1028 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1031 moldyn->t_sum+=moldyn->t;
1032 moldyn->t_avg=moldyn->t_sum/denom;
1035 moldyn->virial_sum+=moldyn->virial;
1036 moldyn->virial_avg=moldyn->virial_sum/denom;
1037 moldyn->gv_sum+=moldyn->gv;
1038 moldyn->gv_avg=moldyn->gv_sum/denom;
1041 moldyn->p_sum+=moldyn->p;
1042 moldyn->p_avg=moldyn->p_sum/denom;
1043 moldyn->gp_sum+=moldyn->gp;
1044 moldyn->gp_avg=moldyn->gp_sum/denom;
1045 moldyn->tp_sum+=moldyn->tp;
1046 moldyn->tp_avg=moldyn->tp_sum/denom;
1051 int get_heat_capacity(t_moldyn *moldyn) {
1055 /* averages needed for heat capacity calc */
1056 if(moldyn->total_steps<moldyn->avg_skip)
1059 /* (temperature average)^2 */
1060 temp2=moldyn->t_avg*moldyn->t_avg;
1061 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1064 /* ideal gas contribution */
1065 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1066 printf(" ideal gas contribution: %f\n",
1067 ighc/moldyn->mass*KILOGRAM/JOULE);
1069 /* specific heat for nvt ensemble */
1070 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1071 moldyn->c_v_nvt/=moldyn->mass;
1073 /* specific heat for nve ensemble */
1074 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1075 moldyn->c_v_nve/=moldyn->mass;
1077 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1078 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1079 printf(" --> <dV2> sim: %f experimental: %f\n",moldyn->dv2_avg,1.5*moldyn->count*K_B2*moldyn->t_avg*moldyn->t_avg*(1.0-1.5*moldyn->count*K_BOLTZMANN/(700*moldyn->mass*JOULE/KILOGRAM)));
1084 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1100 /* store atomic configuration + dimension */
1101 store=malloc(moldyn->count*sizeof(t_atom));
1103 printf("[moldyn] allocating store mem failed\n");
1106 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1111 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1112 su=pow(2.0-h,ONE_THIRD)-1.0;
1113 dv=(1.0-h)*moldyn->volume;
1115 /* scale up dimension and atom positions */
1116 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1117 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1118 link_cell_shutdown(moldyn);
1119 link_cell_init(moldyn,QUIET);
1120 potential_force_calc(moldyn);
1123 /* restore atomic configuration + dim */
1124 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1127 /* scale down dimension and atom positions */
1128 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1129 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1130 link_cell_shutdown(moldyn);
1131 link_cell_init(moldyn,QUIET);
1132 potential_force_calc(moldyn);
1135 /* calculate pressure */
1136 moldyn->tp=-(y1-y0)/(2.0*dv);
1138 /* restore atomic configuration */
1139 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1141 link_cell_shutdown(moldyn);
1142 link_cell_init(moldyn,QUIET);
1143 //potential_force_calc(moldyn);
1145 /* free store buffer */
1152 double get_pressure(t_moldyn *moldyn) {
1158 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1170 if(x) dim->x*=scale;
1171 if(y) dim->y*=scale;
1172 if(z) dim->z*=scale;
1177 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1188 for(i=0;i<moldyn->count;i++) {
1189 r=&(moldyn->atom[i].r);
1198 int scale_volume(t_moldyn *moldyn) {
1204 vdim=&(moldyn->vis.dim);
1208 /* scaling factor */
1209 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1210 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc*moldyn->tau;
1211 scale=pow(scale,ONE_THIRD);
1214 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1217 /* scale the atoms and dimensions */
1218 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1219 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1221 /* visualize dimensions */
1228 /* recalculate scaled volume */
1229 moldyn->volume=dim->x*dim->y*dim->z;
1231 /* adjust/reinit linkcell */
1232 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1233 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1234 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1235 link_cell_shutdown(moldyn);
1236 link_cell_init(moldyn,QUIET);
1247 double e_kin_calc(t_moldyn *moldyn) {
1255 for(i=0;i<moldyn->count;i++) {
1256 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1257 moldyn->ekin+=atom[i].ekin;
1260 return moldyn->ekin;
1263 double get_total_energy(t_moldyn *moldyn) {
1265 return(moldyn->ekin+moldyn->energy);
1268 t_3dvec get_total_p(t_moldyn *moldyn) {
1277 for(i=0;i<moldyn->count;i++) {
1278 v3_scale(&p,&(atom[i].v),atom[i].mass);
1279 v3_add(&p_total,&p_total,&p);
1285 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1289 /* nn_dist is the nearest neighbour distance */
1291 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1300 /* linked list / cell method */
1302 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1305 #ifndef LOWMEM_LISTS
1311 /* partitioning the md cell */
1312 lc->nx=moldyn->dim.x/moldyn->cutoff;
1313 lc->x=moldyn->dim.x/lc->nx;
1314 lc->ny=moldyn->dim.y/moldyn->cutoff;
1315 lc->y=moldyn->dim.y/lc->ny;
1316 lc->nz=moldyn->dim.z/moldyn->cutoff;
1317 lc->z=moldyn->dim.z/lc->nz;
1318 lc->cells=lc->nx*lc->ny*lc->nz;
1321 lc->subcell=malloc(lc->cells*sizeof(int*));
1323 lc->subcell=malloc(sizeof(t_lowmem_list));
1325 lc->subcell=malloc(lc->cells*sizeof(t_list));
1328 if(lc->subcell==NULL) {
1329 perror("[moldyn] cell init (malloc)");
1334 printf("[moldyn] FATAL: less then 27 subcells! (%d)\n",
1339 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1342 printf("[moldyn] initializing 'lowmem' linked cells (%d)\n",
1345 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1348 printf(" x: %d x %f A\n",lc->nx,lc->x);
1349 printf(" y: %d x %f A\n",lc->ny,lc->y);
1350 printf(" z: %d x %f A\n",lc->nz,lc->z);
1355 for(i=0;i<lc->cells;i++) {
1356 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1357 if(lc->subcell[i]==NULL) {
1358 perror("[moldyn] list init (malloc)");
1363 printf(" ---> %d malloc %p (%p)\n",
1364 i,lc->subcell[0],lc->subcell);
1368 lc->subcell->head=malloc(lc->cells*sizeof(int));
1369 if(lc->subcell->head==NULL) {
1370 perror("[moldyn] head init (malloc)");
1373 lc->subcell->list=malloc(moldyn->count*sizeof(int));
1374 if(lc->subcell->list==NULL) {
1375 perror("[moldyn] list init (malloc)");
1379 for(i=0;i<lc->cells;i++)
1380 list_init_f(&(lc->subcell[i]));
1383 /* update the list */
1384 link_cell_update(moldyn);
1389 int link_cell_update(t_moldyn *moldyn) {
1407 for(i=0;i<lc->cells;i++)
1409 memset(lc->subcell[i],-1,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1411 lc->subcell->head[i]=-1;
1413 list_destroy_f(&(lc->subcell[i]));
1416 for(count=0;count<moldyn->count;count++) {
1417 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1418 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1419 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1423 while(lc->subcell[i+j*nx+k*nxy][p]!=-1)
1426 if(p>=MAX_ATOMS_PER_LIST) {
1427 printf("[moldyn] FATAL: amount of atoms too high!\n");
1431 lc->subcell[i+j*nx+k*nxy][p]=count;
1434 lc->subcell->list[count]=lc->subcell->head[p];
1435 lc->subcell->head[p]=count;
1437 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nxy]),
1441 printf(" ---> %d %d malloc %p (%p)\n",
1442 i,count,lc->subcell[i].current,lc->subcell);
1450 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1476 if(i>=nx||j>=ny||k>=nz)
1477 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1480 #ifndef LOWMEM_LISTS
1481 cell[0]=lc->subcell[i+j*nx+k*a];
1483 cell[0]=lc->subcell->head[i+j*nx+k*a];
1485 for(ci=-1;ci<=1;ci++) {
1488 if((x<0)||(x>=nx)) {
1492 for(cj=-1;cj<=1;cj++) {
1495 if((y<0)||(y>=ny)) {
1499 for(ck=-1;ck<=1;ck++) {
1502 if((z<0)||(z>=nz)) {
1506 if(!(ci|cj|ck)) continue;
1508 #ifndef LOWMEM_LISTS
1509 cell[--count2]=lc->subcell[x+y*nx+z*a];
1511 cell[--count2]=lc->subcell->head[x+y*nx+z*a];
1516 #ifndef LOWMEM_LISTS
1517 cell[count1++]=lc->subcell[x+y*nx+z*a];
1519 cell[count1++]=lc->subcell->head[x+y*nx+z*a];
1531 int link_cell_shutdown(t_moldyn *moldyn) {
1533 #ifndef LOWMEM_LISTS
1541 free(lc->subcell->head);
1542 free(lc->subcell->list);
1546 for(i=0;i<lc->cells;i++) {
1548 free(lc->subcell[i]);
1550 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1551 list_destroy_f(&(lc->subcell[i]));
1561 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1565 t_moldyn_schedule *schedule;
1567 schedule=&(moldyn->schedule);
1568 count=++(schedule->total_sched);
1570 ptr=realloc(schedule->runs,count*sizeof(int));
1572 perror("[moldyn] realloc (runs)");
1576 schedule->runs[count-1]=runs;
1578 ptr=realloc(schedule->tau,count*sizeof(double));
1580 perror("[moldyn] realloc (tau)");
1584 schedule->tau[count-1]=tau;
1586 printf("[moldyn] schedule added:\n");
1587 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1593 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1595 moldyn->schedule.hook=hook;
1596 moldyn->schedule.hook_params=hook_params;
1603 * 'integration of newtons equation' - algorithms
1607 /* start the integration */
1609 int moldyn_integrate(t_moldyn *moldyn) {
1612 unsigned int e,m,s,v,p,t,a;
1614 t_moldyn_schedule *sched;
1619 double energy_scale;
1620 struct timeval t1,t2;
1623 sched=&(moldyn->schedule);
1626 /* initialize linked cell method */
1627 link_cell_init(moldyn,VERBOSE);
1629 /* logging & visualization */
1638 /* sqaure of some variables */
1639 moldyn->tau_square=moldyn->tau*moldyn->tau;
1641 /* get current time */
1642 gettimeofday(&t1,NULL);
1644 /* calculate initial forces */
1645 potential_force_calc(moldyn);
1650 /* some stupid checks before we actually start calculating bullshit */
1651 if(moldyn->cutoff>0.5*moldyn->dim.x)
1652 printf("[moldyn] WARNING: cutoff > 0.5 x dim.x\n");
1653 if(moldyn->cutoff>0.5*moldyn->dim.y)
1654 printf("[moldyn] WARNING: cutoff > 0.5 x dim.y\n");
1655 if(moldyn->cutoff>0.5*moldyn->dim.z)
1656 printf("[moldyn] WARNING: cutoff > 0.5 x dim.z\n");
1658 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1659 if(ds>0.05*moldyn->nnd)
1660 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1663 /* zero absolute time */
1664 // should have right values!
1666 //moldyn->total_steps=0;
1668 /* debugging, ignore */
1671 /* tell the world */
1672 printf("[moldyn] integration start, go get a coffee ...\n");
1674 /* executing the schedule */
1676 while(sched->count<sched->total_sched) {
1678 /* setting amount of runs and finite time step size */
1679 moldyn->tau=sched->tau[sched->count];
1680 moldyn->tau_square=moldyn->tau*moldyn->tau;
1681 moldyn->time_steps=sched->runs[sched->count];
1683 /* energy scaling factor (might change!) */
1684 energy_scale=moldyn->count*EV;
1686 /* integration according to schedule */
1688 for(i=0;i<moldyn->time_steps;i++) {
1690 /* integration step */
1691 moldyn->integrate(moldyn);
1693 /* calculate kinetic energy, temperature and pressure */
1695 temperature_calc(moldyn);
1697 pressure_calc(moldyn);
1699 thermodynamic_pressure_calc(moldyn);
1700 printf("\n\nDEBUG: numeric pressure calc: %f\n\n",
1704 /* calculate fluctuations + averages */
1705 average_and_fluctuation_calc(moldyn);
1708 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1709 scale_velocity(moldyn,FALSE);
1710 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1711 scale_volume(moldyn);
1713 /* check for log & visualization */
1715 if(!(moldyn->total_steps%e))
1716 dprintf(moldyn->efd,
1718 moldyn->time,moldyn->ekin/energy_scale,
1719 moldyn->energy/energy_scale,
1720 get_total_energy(moldyn)/energy_scale);
1723 if(!(moldyn->total_steps%m)) {
1724 momentum=get_total_p(moldyn);
1725 dprintf(moldyn->mfd,
1726 "%f %f %f %f %f\n",moldyn->time,
1727 momentum.x,momentum.y,momentum.z,
1728 v3_norm(&momentum));
1732 if(!(moldyn->total_steps%p)) {
1733 dprintf(moldyn->pfd,
1734 "%f %f %f %f %f %f %f\n",moldyn->time,
1735 moldyn->p/BAR,moldyn->p_avg/BAR,
1736 moldyn->gp/BAR,moldyn->gp_avg/BAR,
1737 moldyn->tp/BAR,moldyn->tp_avg/BAR);
1741 if(!(moldyn->total_steps%t)) {
1742 dprintf(moldyn->tfd,
1744 moldyn->time,moldyn->t,moldyn->t_avg);
1748 if(!(moldyn->total_steps%v)) {
1749 dprintf(moldyn->vfd,
1750 "%f %f\n",moldyn->time,moldyn->volume);
1754 if(!(moldyn->total_steps%s)) {
1755 snprintf(dir,128,"%s/s-%07.f.save",
1756 moldyn->vlsdir,moldyn->time);
1757 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
1759 if(fd<0) perror("[moldyn] save fd open");
1761 write(fd,moldyn,sizeof(t_moldyn));
1762 write(fd,moldyn->atom,
1763 moldyn->count*sizeof(t_atom));
1769 if(!(moldyn->total_steps%a)) {
1770 visual_atoms(moldyn);
1774 /* display progress */
1775 //if(!(moldyn->total_steps%10)) {
1776 /* get current time */
1777 gettimeofday(&t2,NULL);
1779 printf("\rsched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)",
1780 sched->count,i,moldyn->total_steps,
1781 moldyn->t,moldyn->t_avg,
1782 moldyn->p/BAR,moldyn->p_avg/BAR,
1783 //moldyn->p/BAR,(moldyn->p-2.0*moldyn->ekin/(3.0*moldyn->volume))/BAR,
1785 (int)(t2.tv_sec-t1.tv_sec));
1789 /* copy over time */
1793 /* increase absolute time */
1794 moldyn->time+=moldyn->tau;
1795 moldyn->total_steps+=1;
1799 /* check for hooks */
1801 printf("\n ## schedule hook %d start ##\n",
1803 sched->hook(moldyn,sched->hook_params);
1804 printf(" ## schedule hook end ##\n");
1807 /* increase the schedule counter */
1815 /* velocity verlet */
1817 int velocity_verlet(t_moldyn *moldyn) {
1820 double tau,tau_square,h;
1825 count=moldyn->count;
1827 tau_square=moldyn->tau_square;
1829 for(i=0;i<count;i++) {
1830 /* check whether fixed atom */
1831 if(atom[i].attr&ATOM_ATTR_FP)
1835 v3_scale(&delta,&(atom[i].v),tau);
1836 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1837 v3_scale(&delta,&(atom[i].f),h*tau_square);
1838 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1839 check_per_bound(moldyn,&(atom[i].r));
1841 /* velocities [actually v(t+tau/2)] */
1842 v3_scale(&delta,&(atom[i].f),h*tau);
1843 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1846 /* criticial check */
1847 moldyn_bc_check(moldyn);
1849 /* neighbour list update */
1850 link_cell_update(moldyn);
1852 /* forces depending on chosen potential */
1854 potential_force_calc(moldyn);
1856 albe_potential_force_calc(moldyn);
1859 for(i=0;i<count;i++) {
1860 /* check whether fixed atom */
1861 if(atom[i].attr&ATOM_ATTR_FP)
1863 /* again velocities [actually v(t+tau)] */
1864 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1865 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1874 * potentials & corresponding forces & virial routine
1878 /* generic potential and force calculation */
1880 int potential_force_calc(t_moldyn *moldyn) {
1883 t_atom *itom,*jtom,*ktom;
1887 int *neighbour_i[27];
1891 int neighbour_i[27];
1894 t_list neighbour_i[27];
1895 t_list neighbour_i2[27];
1901 count=moldyn->count;
1911 /* reset global virial */
1912 memset(&(moldyn->gvir),0,sizeof(t_virial));
1914 /* reset force, site energy and virial of every atom */
1916 i=omp_get_thread_num();
1917 #pragma omp parallel for private(virial)
1919 for(i=0;i<count;i++) {
1922 v3_zero(&(itom[i].f));
1925 virial=(&(itom[i].virial));
1933 /* reset site energy */
1938 /* get energy, force and virial of every atom */
1940 /* first (and only) loop over atoms i */
1941 for(i=0;i<count;i++) {
1943 /* single particle potential/force */
1944 if(itom[i].attr&ATOM_ATTR_1BP)
1946 moldyn->func1b(moldyn,&(itom[i]));
1948 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1951 /* 2 body pair potential/force */
1953 link_cell_neighbour_index(moldyn,
1954 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1955 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1956 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1961 /* first loop over atoms j */
1962 if(moldyn->func2b) {
1969 while(neighbour_i[j][p]!=-1) {
1971 jtom=&(atom[neighbour_i[j][p]]);
1979 p=lc->subcell->list[p];
1981 this=&(neighbour_i[j]);
1984 if(this->start==NULL)
1988 jtom=this->current->data;
1991 if(jtom==&(itom[i]))
1994 if((jtom->attr&ATOM_ATTR_2BP)&
1995 (itom[i].attr&ATOM_ATTR_2BP)) {
1996 moldyn->func2b(moldyn,
2006 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2012 /* 3 body potential/force */
2014 if(!(itom[i].attr&ATOM_ATTR_3BP))
2017 /* copy the neighbour lists */
2019 /* no copy needed for static lists */
2021 /* no copy needed for lowmem lists */
2023 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
2026 /* second loop over atoms j */
2033 while(neighbour_i[j][p]!=-1) {
2035 jtom=&(atom[neighbour_i[j][p]]);
2043 p=lc->subcell->list[p];
2045 this=&(neighbour_i[j]);
2048 if(this->start==NULL)
2053 jtom=this->current->data;
2056 if(jtom==&(itom[i]))
2059 if(!(jtom->attr&ATOM_ATTR_3BP))
2065 if(moldyn->func3b_j1)
2066 moldyn->func3b_j1(moldyn,
2071 /* in first j loop, 3bp run can be skipped */
2072 if(!(moldyn->run3bp))
2075 /* first loop over atoms k */
2076 if(moldyn->func3b_k1) {
2084 while(neighbour_i[k][q]!=-1) {
2086 ktom=&(atom[neighbour_i[k][q]]);
2094 q=lc->subcell->list[q];
2096 that=&(neighbour_i2[k]);
2099 if(that->start==NULL)
2103 ktom=that->current->data;
2106 if(!(ktom->attr&ATOM_ATTR_3BP))
2112 if(ktom==&(itom[i]))
2115 moldyn->func3b_k1(moldyn,
2125 } while(list_next_f(that)!=\
2133 if(moldyn->func3b_j2)
2134 moldyn->func3b_j2(moldyn,
2139 /* second loop over atoms k */
2140 if(moldyn->func3b_k2) {
2148 while(neighbour_i[k][q]!=-1) {
2150 ktom=&(atom[neighbour_i[k][q]]);
2158 q=lc->subcell->list[q];
2160 that=&(neighbour_i2[k]);
2163 if(that->start==NULL)
2167 ktom=that->current->data;
2170 if(!(ktom->attr&ATOM_ATTR_3BP))
2176 if(ktom==&(itom[i]))
2179 moldyn->func3b_k2(moldyn,
2190 } while(list_next_f(that)!=\
2198 /* 2bp post function */
2199 if(moldyn->func3b_j3) {
2200 moldyn->func3b_j3(moldyn,
2209 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2224 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2225 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2227 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
2228 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
2229 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
2233 /* some postprocessing */
2235 #pragma omp parallel for
2237 for(i=0;i<count;i++) {
2238 /* calculate global virial */
2239 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
2240 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
2241 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
2242 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
2243 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
2244 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
2246 /* check forces regarding the given timestep */
2247 if(v3_norm(&(itom[i].f))>\
2248 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
2249 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",
2257 * virial calculation
2260 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2261 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2263 a->virial.xx+=f->x*d->x;
2264 a->virial.yy+=f->y*d->y;
2265 a->virial.zz+=f->z*d->z;
2266 a->virial.xy+=f->x*d->y;
2267 a->virial.xz+=f->x*d->z;
2268 a->virial.yz+=f->y*d->z;
2274 * periodic boundary checking
2277 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2278 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2289 if(moldyn->status&MOLDYN_STAT_PBX) {
2290 if(a->x>=x) a->x-=dim->x;
2291 else if(-a->x>x) a->x+=dim->x;
2293 if(moldyn->status&MOLDYN_STAT_PBY) {
2294 if(a->y>=y) a->y-=dim->y;
2295 else if(-a->y>y) a->y+=dim->y;
2297 if(moldyn->status&MOLDYN_STAT_PBZ) {
2298 if(a->z>=z) a->z-=dim->z;
2299 else if(-a->z>z) a->z+=dim->z;
2306 * debugging / critical check functions
2309 int moldyn_bc_check(t_moldyn *moldyn) {
2322 for(i=0;i<moldyn->count;i++) {
2323 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2324 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2325 i,atom[i].r.x,dim->x/2);
2326 printf("diagnostic:\n");
2327 printf("-----------\natom.r.x:\n");
2329 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2332 ((byte)&(1<<k))?1:0,
2335 printf("---------------\nx=dim.x/2:\n");
2337 memcpy(&byte,(u8 *)(&x)+j,1);
2340 ((byte)&(1<<k))?1:0,
2343 if(atom[i].r.x==x) printf("the same!\n");
2344 else printf("different!\n");
2346 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2347 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2348 i,atom[i].r.y,dim->y/2);
2349 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2350 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2351 i,atom[i].r.z,dim->z/2);
2361 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2368 fd=open(file,O_RDONLY);
2370 perror("[moldyn] load save file open");
2374 fsize=lseek(fd,0,SEEK_END);
2375 lseek(fd,0,SEEK_SET);
2377 size=sizeof(t_moldyn);
2380 cnt=read(fd,moldyn,size);
2382 perror("[moldyn] load save file read (moldyn)");
2388 size=moldyn->count*sizeof(t_atom);
2390 /* correcting possible atom data offset */
2392 if(fsize!=sizeof(t_moldyn)+size) {
2393 corr=fsize-sizeof(t_moldyn)-size;
2394 printf("[moldyn] WARNING: lsf (illegal file size)\n");
2395 printf(" moifying offset:\n");
2396 printf(" - current pos: %d\n",sizeof(t_moldyn));
2397 printf(" - atom size: %d\n",size);
2398 printf(" - file size: %d\n",fsize);
2399 printf(" => correction: %d\n",corr);
2400 lseek(fd,corr,SEEK_CUR);
2403 moldyn->atom=(t_atom *)malloc(size);
2404 if(moldyn->atom==NULL) {
2405 perror("[moldyn] load save file malloc (atoms)");
2410 cnt=read(fd,moldyn->atom,size);
2412 perror("[moldyn] load save file read (atoms)");
2423 int moldyn_free_save_file(t_moldyn *moldyn) {
2430 int moldyn_load(t_moldyn *moldyn) {
2438 * function to find/callback all combinations of 2 body bonds
2441 int process_2b_bonds(t_moldyn *moldyn,void *data,
2442 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2443 void *data,u8 bc)) {
2453 t_list neighbour[27];
2463 for(i=0;i<moldyn->count;i++) {
2464 /* neighbour indexing */
2465 link_cell_neighbour_index(moldyn,
2466 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2467 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2468 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2473 bc=(j<lc->dnlc)?0:1;
2478 while(neighbour[j][p]!=-1) {
2480 jtom=&(moldyn->atom[neighbour[j][p]]);
2488 p=lc->subcell->list[p];
2490 this=&(neighbour[j]);
2493 if(this->start==NULL)
2498 jtom=this->current->data;
2502 process(moldyn,&(itom[i]),jtom,data,bc);
2509 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2519 * post processing functions
2522 int get_line(int fd,char *line,int max) {
2529 if(count==max) return count;
2530 ret=read(fd,line+count,1);
2531 if(ret<=0) return ret;
2532 if(line[count]=='\n') {
2533 memset(line+count,0,max-count-1);
2541 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2547 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
2563 for(i=0;i<moldyn->count;i++) {
2565 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
2566 check_per_bound(moldyn,&dist);
2567 d2=v3_absolute_square(&dist);
2581 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
2582 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
2583 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
2588 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
2593 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
2594 t_atom *jtom,void *data,u8 bc) {
2601 /* only count pairs once,
2602 * skip same atoms */
2603 if(itom->tag>=jtom->tag)
2607 * pair correlation calc
2614 v3_sub(&dist,&(jtom->r),&(itom->r));
2615 if(bc) check_per_bound(moldyn,&dist);
2616 d=v3_absolute_square(&dist);
2618 /* ignore if greater cutoff */
2619 if(d>moldyn->cutoff_square)
2622 /* fill the slots */
2626 /* should never happen but it does 8) -
2627 * related to -ffloat-store problem! */
2629 printf("[moldyn] WARNING: pcc (%d/%d)",
2635 if(itom->brand!=jtom->brand) {
2640 /* type a - type a bonds */
2642 pcc->stat[s+pcc->o1]+=1;
2644 /* type b - type b bonds */
2645 pcc->stat[s+pcc->o2]+=1;
2651 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2658 pcc.o1=moldyn->cutoff/dr;
2661 if(pcc.o1*dr<=moldyn->cutoff)
2662 printf("[moldyn] WARNING: pcc (low #slots)\n");
2664 printf("[moldyn] pair correlation calc info:\n");
2665 printf(" time: %f\n",moldyn->time);
2666 printf(" count: %d\n",moldyn->count);
2667 printf(" cutoff: %f\n",moldyn->cutoff);
2668 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2671 pcc.stat=(double *)ptr;
2674 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
2675 if(pcc.stat==NULL) {
2676 perror("[moldyn] pair correlation malloc");
2681 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
2684 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
2687 for(i=1;i<pcc.o1;i++) {
2688 // normalization: 4 pi r^2 dr
2689 // here: not double counting pairs -> 2 pi r r dr
2690 // ... and actually it's a constant times r^2
2693 pcc.stat[pcc.o1+i]/=norm;
2694 pcc.stat[pcc.o2+i]/=norm;
2699 /* todo: store/print pair correlation function */
2706 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2713 if(itom->tag>=jtom->tag)
2717 v3_sub(&dist,&(jtom->r),&(itom->r));
2718 if(bc) check_per_bound(moldyn,&dist);
2719 d=v3_absolute_square(&dist);
2721 /* ignore if greater or equal cutoff */
2722 if(d>moldyn->cutoff_square)
2725 /* check for potential bond */
2726 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2729 /* now count this bonding ... */
2732 /* increase total bond counter
2733 * ... double counting!
2738 ba->acnt[jtom->tag]+=1;
2740 ba->bcnt[jtom->tag]+=1;
2743 ba->acnt[itom->tag]+=1;
2745 ba->bcnt[itom->tag]+=1;
2750 int bond_analyze(t_moldyn *moldyn,double *quality) {
2752 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
2760 ba.acnt=malloc(moldyn->count*sizeof(int));
2762 perror("[moldyn] bond analyze malloc (a)");
2765 memset(ba.acnt,0,moldyn->count*sizeof(int));
2767 ba.bcnt=malloc(moldyn->count*sizeof(int));
2769 perror("[moldyn] bond analyze malloc (b)");
2772 memset(ba.bcnt,0,moldyn->count*sizeof(int));
2781 process_2b_bonds(moldyn,&ba,bond_analyze_process);
2783 for(i=0;i<moldyn->count;i++) {
2784 if(atom[i].brand==0) {
2785 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
2789 if((ba.acnt[i]==4)&(ba.bcnt[i]==0)) {
2797 printf("[moldyn] bond analyze: c_cnt=%d | set=%d\n",ccnt,cset);
2798 printf("[moldyn] bond analyze: q_cnt=%d | tot=%d\n",qcnt,ba.tcnt);
2801 quality[0]=1.0*ccnt/cset;
2802 quality[1]=1.0*qcnt/ba.tcnt;
2805 printf("[moldyn] bond analyze: c_bnd_q=%f\n",1.0*qcnt/ba.tcnt);
2806 printf("[moldyn] bond analyze: tot_q=%f\n",1.0*qcnt/ba.tcnt);
2813 * visualization code
2816 int visual_init(t_moldyn *moldyn,char *filebase) {
2818 strncpy(moldyn->vis.fb,filebase,128);
2823 int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2830 if(itom->tag>=jtom->tag)
2833 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2836 if((itom->attr&ATOM_ATTR_VB)|(jtom->attr&ATOM_ATTR_VB))
2837 dprintf(vb->fd,"# [B] %f %f %f %f %f %f\n",
2838 itom->r.x,itom->r.y,itom->r.z,
2839 jtom->r.x,jtom->r.y,jtom->r.z);
2844 int visual_atoms(t_moldyn *moldyn) {
2862 sprintf(file,"%s/atomic_conf_%07.f.xyz",v->fb,moldyn->time);
2863 vb.fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
2865 perror("open visual save file fd");
2869 /* write the actual data file */
2872 dprintf(vb.fd,"# [P] %d %07.f <%f,%f,%f>\n",
2873 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
2875 // atomic configuration
2876 for(i=0;i<moldyn->count;i++)
2877 // atom type, positions, color and kinetic energy
2878 dprintf(vb.fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
2882 pse_col[atom[i].element],
2885 // bonds between atoms
2886 process_2b_bonds(moldyn,&vb,visual_bonds_process);
2890 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2891 -dim.x/2,-dim.y/2,-dim.z/2,
2892 dim.x/2,-dim.y/2,-dim.z/2);
2893 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2894 -dim.x/2,-dim.y/2,-dim.z/2,
2895 -dim.x/2,dim.y/2,-dim.z/2);
2896 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2897 dim.x/2,dim.y/2,-dim.z/2,
2898 dim.x/2,-dim.y/2,-dim.z/2);
2899 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2900 -dim.x/2,dim.y/2,-dim.z/2,
2901 dim.x/2,dim.y/2,-dim.z/2);
2903 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2904 -dim.x/2,-dim.y/2,dim.z/2,
2905 dim.x/2,-dim.y/2,dim.z/2);
2906 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2907 -dim.x/2,-dim.y/2,dim.z/2,
2908 -dim.x/2,dim.y/2,dim.z/2);
2909 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2910 dim.x/2,dim.y/2,dim.z/2,
2911 dim.x/2,-dim.y/2,dim.z/2);
2912 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2913 -dim.x/2,dim.y/2,dim.z/2,
2914 dim.x/2,dim.y/2,dim.z/2);
2916 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2917 -dim.x/2,-dim.y/2,dim.z/2,
2918 -dim.x/2,-dim.y/2,-dim.z/2);
2919 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2920 -dim.x/2,dim.y/2,dim.z/2,
2921 -dim.x/2,dim.y/2,-dim.z/2);
2922 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2923 dim.x/2,-dim.y/2,dim.z/2,
2924 dim.x/2,-dim.y/2,-dim.z/2);
2925 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2926 dim.x/2,dim.y/2,dim.z/2,
2927 dim.x/2,dim.y/2,-dim.z/2);
2936 * fpu cntrol functions
2939 // set rounding to double (eliminates -ffloat-store!)
2940 int fpu_set_rtd(void) {
2946 ctrl&=~_FPU_EXTENDED;