2 * moldyn.c - molecular dynamics library main file
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
12 #include <sys/types.h>
21 #include "report/report.h"
23 /* potential includes */
24 #include "potentials/harmonic_oscillator.h"
25 #include "potentials/lennard_jones.h"
26 #include "potentials/albe.h"
28 #include "potentials/tersoff_orig.h"
30 #include "potentials/tersoff.h"
35 * global variables, pse and atom colors (only needed here)
38 static char *pse_name[]={
60 static char *pse_col[]={
83 * the moldyn functions
86 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
88 printf("[moldyn] init\n");
90 memset(moldyn,0,sizeof(t_moldyn));
95 rand_init(&(moldyn->random),NULL,1);
96 moldyn->random.status|=RAND_STAT_VERBOSE;
101 int moldyn_shutdown(t_moldyn *moldyn) {
103 printf("[moldyn] shutdown\n");
105 moldyn_log_shutdown(moldyn);
106 link_cell_shutdown(moldyn);
107 rand_close(&(moldyn->random));
113 int set_int_alg(t_moldyn *moldyn,u8 algo) {
115 printf("[moldyn] integration algorithm: ");
118 case MOLDYN_INTEGRATE_VERLET:
119 moldyn->integrate=velocity_verlet;
120 printf("velocity verlet\n");
123 printf("unknown integration algorithm: %02x\n",algo);
131 int set_cutoff(t_moldyn *moldyn,double cutoff) {
133 moldyn->cutoff=cutoff;
134 moldyn->cutoff_square=cutoff*cutoff;
136 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
141 int set_temperature(t_moldyn *moldyn,double t_ref) {
145 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
150 int set_pressure(t_moldyn *moldyn,double p_ref) {
154 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
159 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
161 moldyn->pt_scale=(ptype|ttype);
165 printf("[moldyn] p/t scaling:\n");
167 printf(" p: %s",ptype?"yes":"no ");
169 printf(" | type: %02x | factor: %f",ptype,ptc);
172 printf(" t: %s",ttype?"yes":"no ");
174 printf(" | type: %02x | factor: %f",ttype,ttc);
180 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
186 moldyn->volume=x*y*z;
194 printf("[moldyn] dimensions in A and A^3 respectively:\n");
195 printf(" x: %f\n",moldyn->dim.x);
196 printf(" y: %f\n",moldyn->dim.y);
197 printf(" z: %f\n",moldyn->dim.z);
198 printf(" volume: %f\n",moldyn->volume);
199 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
204 int set_nn_dist(t_moldyn *moldyn,double dist) {
211 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
213 printf("[moldyn] periodic boundary conditions:\n");
216 moldyn->status|=MOLDYN_STAT_PBX;
219 moldyn->status|=MOLDYN_STAT_PBY;
222 moldyn->status|=MOLDYN_STAT_PBZ;
224 printf(" x: %s\n",x?"yes":"no");
225 printf(" y: %s\n",y?"yes":"no");
226 printf(" z: %s\n",z?"yes":"no");
231 int set_potential(t_moldyn *moldyn,u8 type) {
234 case MOLDYN_POTENTIAL_TM:
235 moldyn->func1b=tersoff_mult_1bp;
236 moldyn->func3b_j1=tersoff_mult_3bp_j1;
237 moldyn->func3b_k1=tersoff_mult_3bp_k1;
238 moldyn->func3b_j2=tersoff_mult_3bp_j2;
239 moldyn->func3b_k2=tersoff_mult_3bp_k2;
240 // missing: check 2b bond func
242 case MOLDYN_POTENTIAL_AM:
243 moldyn->func3b_j1=albe_mult_3bp_j1;
244 moldyn->func3b_k1=albe_mult_3bp_k1;
245 moldyn->func3b_j2=albe_mult_3bp_j2;
246 moldyn->func3b_k2=albe_mult_3bp_k2;
247 moldyn->check_2b_bond=albe_mult_check_2b_bond;
249 case MOLDYN_POTENTIAL_HO:
250 moldyn->func2b=harmonic_oscillator;
251 moldyn->check_2b_bond=harmonic_oscillator_check_2b_bond;
253 case MOLDYN_POTENTIAL_LJ:
254 moldyn->func2b=lennard_jones;
255 moldyn->check_2b_bond=lennard_jones_check_2b_bond;
258 printf("[moldyn] set potential: unknown type %02x\n",
266 int set_avg_skip(t_moldyn *moldyn,int skip) {
268 printf("[moldyn] skip %d steps before starting average calc\n",skip);
269 moldyn->avg_skip=skip;
274 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
276 strncpy(moldyn->vlsdir,dir,127);
281 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
283 strncpy(moldyn->rauthor,author,63);
284 strncpy(moldyn->rtitle,title,63);
289 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
294 printf("[moldyn] set log: ");
297 case LOG_TOTAL_ENERGY:
298 moldyn->ewrite=timer;
299 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
300 moldyn->efd=open(filename,
301 O_WRONLY|O_CREAT|O_EXCL,
304 perror("[moldyn] energy log fd open");
307 dprintf(moldyn->efd,"# total energy log file\n");
308 printf("total energy (%d)\n",timer);
310 case LOG_TOTAL_MOMENTUM:
311 moldyn->mwrite=timer;
312 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
313 moldyn->mfd=open(filename,
314 O_WRONLY|O_CREAT|O_EXCL,
317 perror("[moldyn] momentum log fd open");
320 dprintf(moldyn->efd,"# total momentum log file\n");
321 printf("total momentum (%d)\n",timer);
324 moldyn->pwrite=timer;
325 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
326 moldyn->pfd=open(filename,
327 O_WRONLY|O_CREAT|O_EXCL,
330 perror("[moldyn] pressure log file\n");
333 dprintf(moldyn->pfd,"# pressure log file\n");
334 printf("pressure (%d)\n",timer);
336 case LOG_TEMPERATURE:
337 moldyn->twrite=timer;
338 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
339 moldyn->tfd=open(filename,
340 O_WRONLY|O_CREAT|O_EXCL,
343 perror("[moldyn] temperature log file\n");
346 dprintf(moldyn->tfd,"# temperature log file\n");
347 printf("temperature (%d)\n",timer);
350 moldyn->vwrite=timer;
351 snprintf(filename,127,"%s/volume",moldyn->vlsdir);
352 moldyn->vfd=open(filename,
353 O_WRONLY|O_CREAT|O_EXCL,
356 perror("[moldyn] volume log file\n");
359 dprintf(moldyn->vfd,"# volume log file\n");
360 printf("volume (%d)\n",timer);
363 moldyn->swrite=timer;
364 printf("save file (%d)\n",timer);
367 moldyn->awrite=timer;
368 ret=visual_init(moldyn,moldyn->vlsdir);
370 printf("[moldyn] visual init failure\n");
373 printf("visual file (%d)\n",timer);
376 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
377 moldyn->rfd=open(filename,
378 O_WRONLY|O_CREAT|O_EXCL,
381 perror("[moldyn] report fd open");
384 printf("report -> ");
386 snprintf(filename,127,"%s/e_plot.scr",
388 moldyn->epfd=open(filename,
389 O_WRONLY|O_CREAT|O_EXCL,
392 perror("[moldyn] energy plot fd open");
395 dprintf(moldyn->epfd,e_plot_script);
400 snprintf(filename,127,"%s/pressure_plot.scr",
402 moldyn->ppfd=open(filename,
403 O_WRONLY|O_CREAT|O_EXCL,
406 perror("[moldyn] p plot fd open");
409 dprintf(moldyn->ppfd,pressure_plot_script);
414 snprintf(filename,127,"%s/temperature_plot.scr",
416 moldyn->tpfd=open(filename,
417 O_WRONLY|O_CREAT|O_EXCL,
420 perror("[moldyn] t plot fd open");
423 dprintf(moldyn->tpfd,temperature_plot_script);
425 printf("temperature ");
427 dprintf(moldyn->rfd,report_start,
428 moldyn->rauthor,moldyn->rtitle);
432 printf("unknown log type: %02x\n",type);
439 int moldyn_log_shutdown(t_moldyn *moldyn) {
443 printf("[moldyn] log shutdown\n");
447 dprintf(moldyn->rfd,report_energy);
448 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
453 if(moldyn->mfd) close(moldyn->mfd);
457 dprintf(moldyn->rfd,report_pressure);
458 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
465 dprintf(moldyn->rfd,report_temperature);
466 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
471 dprintf(moldyn->rfd,report_end);
473 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
476 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
479 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
488 * creating lattice functions
491 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
492 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
503 /* how many atoms do we expect */
504 if(type==CUBIC) new*=1;
505 if(type==FCC) new*=4;
506 if(type==DIAMOND) new*=8;
508 /* allocate space for atoms */
509 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
511 perror("[moldyn] realloc (create lattice)");
515 atom=&(moldyn->atom[count]);
517 /* no atoms on the boundaries (only reason: it looks better!) */
531 set_nn_dist(moldyn,lc);
532 ret=cubic_init(a,b,c,lc,atom,&orig);
536 v3_scale(&orig,&orig,0.5);
537 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
538 ret=fcc_init(a,b,c,lc,atom,&orig);
542 v3_scale(&orig,&orig,0.25);
543 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
544 ret=diamond_init(a,b,c,lc,atom,&orig);
547 printf("unknown lattice type (%02x)\n",type);
553 printf("[moldyn] creating lattice failed\n");
554 printf(" amount of atoms\n");
555 printf(" - expected: %d\n",new);
556 printf(" - created: %d\n",ret);
561 printf("[moldyn] created lattice with %d atoms\n",new);
563 for(ret=0;ret<new;ret++) {
564 atom[ret].element=element;
567 atom[ret].brand=brand;
568 atom[ret].tag=count+ret;
569 check_per_bound(moldyn,&(atom[ret].r));
570 atom[ret].r_0=atom[ret].r;
573 /* update total system mass */
574 total_mass_calc(moldyn);
579 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
580 t_3dvec *r,t_3dvec *v) {
587 count=(moldyn->count)++; // asshole style!
589 ptr=realloc(atom,(count+1)*sizeof(t_atom));
591 perror("[moldyn] realloc (add atom)");
598 /* initialize new atom */
599 memset(&(atom[count]),0,sizeof(t_atom));
602 atom[count].element=element;
603 atom[count].mass=mass;
604 atom[count].brand=brand;
605 atom[count].tag=count;
606 atom[count].attr=attr;
607 check_per_bound(moldyn,&(atom[count].r));
608 atom[count].r_0=atom[count].r;
610 /* update total system mass */
611 total_mass_calc(moldyn);
616 int del_atom(t_moldyn *moldyn,int tag) {
623 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
625 perror("[moldyn]malloc (del atom)");
629 for(cnt=0;cnt<tag;cnt++)
632 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
634 new[cnt-1].tag=cnt-1;
646 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
665 v3_copy(&(atom[count].r),&r);
674 for(i=0;i<count;i++) {
675 atom[i].r.x-=(a*lc)/2.0;
676 atom[i].r.y-=(b*lc)/2.0;
677 atom[i].r.z-=(c*lc)/2.0;
683 /* fcc lattice init */
684 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
697 /* construct the basis */
698 memset(basis,0,3*sizeof(t_3dvec));
706 /* fill up the room */
714 v3_copy(&(atom[count].r),&r);
717 /* the three face centered atoms */
719 v3_add(&n,&r,&basis[l]);
720 v3_copy(&(atom[count].r),&n);
729 /* coordinate transformation */
730 for(i=0;i<count;i++) {
731 atom[i].r.x-=(a*lc)/2.0;
732 atom[i].r.y-=(b*lc)/2.0;
733 atom[i].r.z-=(c*lc)/2.0;
739 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
744 count=fcc_init(a,b,c,lc,atom,origin);
750 if(origin) v3_add(&o,&o,origin);
752 count+=fcc_init(a,b,c,lc,&atom[count],&o);
757 int destroy_atoms(t_moldyn *moldyn) {
759 if(moldyn->atom) free(moldyn->atom);
764 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
767 * - gaussian distribution of velocities
768 * - zero total momentum
769 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
774 t_3dvec p_total,delta;
779 random=&(moldyn->random);
781 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
783 /* gaussian distribution of velocities */
785 for(i=0;i<moldyn->count;i++) {
786 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
788 v=sigma*rand_get_gauss(random);
790 p_total.x+=atom[i].mass*v;
792 v=sigma*rand_get_gauss(random);
794 p_total.y+=atom[i].mass*v;
796 v=sigma*rand_get_gauss(random);
798 p_total.z+=atom[i].mass*v;
801 /* zero total momentum */
802 v3_scale(&p_total,&p_total,1.0/moldyn->count);
803 for(i=0;i<moldyn->count;i++) {
804 v3_scale(&delta,&p_total,1.0/atom[i].mass);
805 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
808 /* velocity scaling */
809 scale_velocity(moldyn,equi_init);
814 double total_mass_calc(t_moldyn *moldyn) {
820 for(i=0;i<moldyn->count;i++)
821 moldyn->mass+=moldyn->atom[i].mass;
826 double temperature_calc(t_moldyn *moldyn) {
828 /* assume up to date kinetic energy, which is 3/2 N k_B T */
830 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
835 double get_temperature(t_moldyn *moldyn) {
840 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
850 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
853 /* get kinetic energy / temperature & count involved atoms */
856 for(i=0;i<moldyn->count;i++) {
857 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
858 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
863 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
864 else return 0; /* no atoms involved in scaling! */
866 /* (temporary) hack for e,t = 0 */
869 if(moldyn->t_ref!=0.0) {
870 thermal_init(moldyn,equi_init);
874 return 0; /* no scaling needed */
878 /* get scaling factor */
879 scale=moldyn->t_ref/moldyn->t;
883 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
884 scale=1.0+(scale-1.0)/moldyn->t_tc;
887 /* velocity scaling */
888 for(i=0;i<moldyn->count;i++) {
889 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
890 v3_scale(&(atom[i].v),&(atom[i].v),scale);
896 double ideal_gas_law_pressure(t_moldyn *moldyn) {
900 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
905 double virial_sum(t_moldyn *moldyn) {
910 /* virial (sum over atom virials) */
918 for(i=0;i<moldyn->count;i++) {
919 virial=&(moldyn->atom[i].virial);
920 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
921 moldyn->vir.xx+=virial->xx;
922 moldyn->vir.yy+=virial->yy;
923 moldyn->vir.zz+=virial->zz;
924 moldyn->vir.xy+=virial->xy;
925 moldyn->vir.xz+=virial->xz;
926 moldyn->vir.yz+=virial->yz;
929 /* global virial (absolute coordinates) */
930 virial=&(moldyn->gvir);
931 moldyn->gv=virial->xx+virial->yy+virial->zz;
933 return moldyn->virial;
936 double pressure_calc(t_moldyn *moldyn) {
940 * with W = 1/3 sum_i f_i r_i (- skipped!)
941 * virial = sum_i f_i r_i
943 * => P = (2 Ekin + virial) / (3V)
946 /* assume up to date virial & up to date kinetic energy */
948 /* pressure (atom virials) */
949 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
950 moldyn->p/=(3.0*moldyn->volume);
952 /* pressure (absolute coordinates) */
953 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
954 moldyn->gp/=(3.0*moldyn->volume);
959 int average_reset(t_moldyn *moldyn) {
961 printf("[moldyn] average reset\n");
963 /* update skip value */
964 moldyn->avg_skip=moldyn->total_steps;
970 /* potential energy */
978 moldyn->virial_sum=0.0;
989 int average_and_fluctuation_calc(t_moldyn *moldyn) {
993 if(moldyn->total_steps<moldyn->avg_skip)
996 denom=moldyn->total_steps+1-moldyn->avg_skip;
998 /* assume up to date energies, temperature, pressure etc */
1000 /* kinetic energy */
1001 moldyn->k_sum+=moldyn->ekin;
1002 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
1003 moldyn->k_avg=moldyn->k_sum/denom;
1004 moldyn->k2_avg=moldyn->k2_sum/denom;
1005 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
1007 /* potential energy */
1008 moldyn->v_sum+=moldyn->energy;
1009 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
1010 moldyn->v_avg=moldyn->v_sum/denom;
1011 moldyn->v2_avg=moldyn->v2_sum/denom;
1012 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1015 moldyn->t_sum+=moldyn->t;
1016 moldyn->t_avg=moldyn->t_sum/denom;
1019 moldyn->virial_sum+=moldyn->virial;
1020 moldyn->virial_avg=moldyn->virial_sum/denom;
1021 moldyn->gv_sum+=moldyn->gv;
1022 moldyn->gv_avg=moldyn->gv_sum/denom;
1025 moldyn->p_sum+=moldyn->p;
1026 moldyn->p_avg=moldyn->p_sum/denom;
1027 moldyn->gp_sum+=moldyn->gp;
1028 moldyn->gp_avg=moldyn->gp_sum/denom;
1029 moldyn->tp_sum+=moldyn->tp;
1030 moldyn->tp_avg=moldyn->tp_sum/denom;
1035 int get_heat_capacity(t_moldyn *moldyn) {
1039 /* averages needed for heat capacity calc */
1040 if(moldyn->total_steps<moldyn->avg_skip)
1043 /* (temperature average)^2 */
1044 temp2=moldyn->t_avg*moldyn->t_avg;
1045 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1048 /* ideal gas contribution */
1049 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1050 printf(" ideal gas contribution: %f\n",
1051 ighc/moldyn->mass*KILOGRAM/JOULE);
1053 /* specific heat for nvt ensemble */
1054 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1055 moldyn->c_v_nvt/=moldyn->mass;
1057 /* specific heat for nve ensemble */
1058 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1059 moldyn->c_v_nve/=moldyn->mass;
1061 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1062 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1063 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)));
1068 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1084 /* store atomic configuration + dimension */
1085 store=malloc(moldyn->count*sizeof(t_atom));
1087 printf("[moldyn] allocating store mem failed\n");
1090 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1095 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1096 su=pow(2.0-h,ONE_THIRD)-1.0;
1097 dv=(1.0-h)*moldyn->volume;
1099 /* scale up dimension and atom positions */
1100 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1101 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1102 link_cell_shutdown(moldyn);
1103 link_cell_init(moldyn,QUIET);
1104 potential_force_calc(moldyn);
1107 /* restore atomic configuration + dim */
1108 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1111 /* scale down dimension and atom positions */
1112 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1113 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1114 link_cell_shutdown(moldyn);
1115 link_cell_init(moldyn,QUIET);
1116 potential_force_calc(moldyn);
1119 /* calculate pressure */
1120 moldyn->tp=-(y1-y0)/(2.0*dv);
1122 /* restore atomic configuration */
1123 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1125 link_cell_shutdown(moldyn);
1126 link_cell_init(moldyn,QUIET);
1127 //potential_force_calc(moldyn);
1129 /* free store buffer */
1136 double get_pressure(t_moldyn *moldyn) {
1142 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1154 if(x) dim->x*=scale;
1155 if(y) dim->y*=scale;
1156 if(z) dim->z*=scale;
1161 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1172 for(i=0;i<moldyn->count;i++) {
1173 r=&(moldyn->atom[i].r);
1182 int scale_volume(t_moldyn *moldyn) {
1188 vdim=&(moldyn->vis.dim);
1192 /* scaling factor */
1193 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1194 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc;
1195 scale=pow(scale,ONE_THIRD);
1198 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1201 /* scale the atoms and dimensions */
1202 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1203 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1205 /* visualize dimensions */
1212 /* recalculate scaled volume */
1213 moldyn->volume=dim->x*dim->y*dim->z;
1215 /* adjust/reinit linkcell */
1216 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1217 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1218 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1219 link_cell_shutdown(moldyn);
1220 link_cell_init(moldyn,QUIET);
1231 double e_kin_calc(t_moldyn *moldyn) {
1239 for(i=0;i<moldyn->count;i++) {
1240 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1241 moldyn->ekin+=atom[i].ekin;
1244 return moldyn->ekin;
1247 double get_total_energy(t_moldyn *moldyn) {
1249 return(moldyn->ekin+moldyn->energy);
1252 t_3dvec get_total_p(t_moldyn *moldyn) {
1261 for(i=0;i<moldyn->count;i++) {
1262 v3_scale(&p,&(atom[i].v),atom[i].mass);
1263 v3_add(&p_total,&p_total,&p);
1269 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1273 /* nn_dist is the nearest neighbour distance */
1275 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1284 /* linked list / cell method */
1286 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1293 /* partitioning the md cell */
1294 lc->nx=moldyn->dim.x/moldyn->cutoff;
1295 lc->x=moldyn->dim.x/lc->nx;
1296 lc->ny=moldyn->dim.y/moldyn->cutoff;
1297 lc->y=moldyn->dim.y/lc->ny;
1298 lc->nz=moldyn->dim.z/moldyn->cutoff;
1299 lc->z=moldyn->dim.z/lc->nz;
1300 lc->cells=lc->nx*lc->ny*lc->nz;
1303 lc->subcell=malloc(lc->cells*sizeof(int*));
1305 lc->subcell=malloc(lc->cells*sizeof(t_list));
1308 if(lc->subcell==NULL) {
1309 perror("[moldyn] cell init (malloc)");
1314 printf("[moldyn] FATAL: less then 27 subcells!\n");
1318 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1321 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1324 printf(" x: %d x %f A\n",lc->nx,lc->x);
1325 printf(" y: %d x %f A\n",lc->ny,lc->y);
1326 printf(" z: %d x %f A\n",lc->nz,lc->z);
1331 for(i=0;i<lc->cells;i++) {
1332 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1333 if(lc->subcell[i]==NULL) {
1334 perror("[moldyn] list init (malloc)");
1339 printf(" ---> %d malloc %p (%p)\n",
1340 i,lc->subcell[0],lc->subcell);
1344 for(i=0;i<lc->cells;i++)
1345 list_init_f(&(lc->subcell[i]));
1348 /* update the list */
1349 link_cell_update(moldyn);
1354 int link_cell_update(t_moldyn *moldyn) {
1370 for(i=0;i<lc->cells;i++)
1372 memset(lc->subcell[i],0,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1374 list_destroy_f(&(lc->subcell[i]));
1377 for(count=0;count<moldyn->count;count++) {
1378 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1379 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1380 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1384 while(lc->subcell[i+j*nx+k*nx*ny][p]!=0)
1387 if(p>=MAX_ATOMS_PER_LIST) {
1388 printf("[moldyn] FATAL: amount of atoms too high!\n");
1392 lc->subcell[i+j*nx+k*nx*ny][p]=count;
1394 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1398 printf(" ---> %d %d malloc %p (%p)\n",
1399 i,count,lc->subcell[i].current,lc->subcell);
1407 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1431 if(i>=nx||j>=ny||k>=nz)
1432 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1435 cell[0]=lc->subcell[i+j*nx+k*a];
1436 for(ci=-1;ci<=1;ci++) {
1439 if((x<0)||(x>=nx)) {
1443 for(cj=-1;cj<=1;cj++) {
1446 if((y<0)||(y>=ny)) {
1450 for(ck=-1;ck<=1;ck++) {
1453 if((z<0)||(z>=nz)) {
1457 if(!(ci|cj|ck)) continue;
1459 cell[--count2]=lc->subcell[x+y*nx+z*a];
1462 cell[count1++]=lc->subcell[x+y*nx+z*a];
1473 int link_cell_shutdown(t_moldyn *moldyn) {
1480 for(i=0;i<lc->cells;i++) {
1482 free(lc->subcell[i]);
1484 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1485 list_destroy_f(&(lc->subcell[i]));
1494 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1498 t_moldyn_schedule *schedule;
1500 schedule=&(moldyn->schedule);
1501 count=++(schedule->total_sched);
1503 ptr=realloc(schedule->runs,count*sizeof(int));
1505 perror("[moldyn] realloc (runs)");
1509 schedule->runs[count-1]=runs;
1511 ptr=realloc(schedule->tau,count*sizeof(double));
1513 perror("[moldyn] realloc (tau)");
1517 schedule->tau[count-1]=tau;
1519 printf("[moldyn] schedule added:\n");
1520 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1526 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1528 moldyn->schedule.hook=hook;
1529 moldyn->schedule.hook_params=hook_params;
1536 * 'integration of newtons equation' - algorithms
1540 /* start the integration */
1542 int moldyn_integrate(t_moldyn *moldyn) {
1545 unsigned int e,m,s,v,p,t,a;
1547 t_moldyn_schedule *sched;
1552 double energy_scale;
1553 struct timeval t1,t2;
1556 sched=&(moldyn->schedule);
1559 /* initialize linked cell method */
1560 link_cell_init(moldyn,VERBOSE);
1562 /* logging & visualization */
1571 /* sqaure of some variables */
1572 moldyn->tau_square=moldyn->tau*moldyn->tau;
1574 /* get current time */
1575 gettimeofday(&t1,NULL);
1577 /* calculate initial forces */
1578 potential_force_calc(moldyn);
1583 /* some stupid checks before we actually start calculating bullshit */
1584 if(moldyn->cutoff>0.5*moldyn->dim.x)
1585 printf("[moldyn] WARNING: cutoff > 0.5 x dim.x\n");
1586 if(moldyn->cutoff>0.5*moldyn->dim.y)
1587 printf("[moldyn] WARNING: cutoff > 0.5 x dim.y\n");
1588 if(moldyn->cutoff>0.5*moldyn->dim.z)
1589 printf("[moldyn] WARNING: cutoff > 0.5 x dim.z\n");
1590 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1591 if(ds>0.05*moldyn->nnd)
1592 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1594 /* zero absolute time */
1596 moldyn->total_steps=0;
1598 /* debugging, ignore */
1601 /* tell the world */
1602 printf("[moldyn] integration start, go get a coffee ...\n");
1604 /* executing the schedule */
1606 while(sched->count<sched->total_sched) {
1608 /* setting amount of runs and finite time step size */
1609 moldyn->tau=sched->tau[sched->count];
1610 moldyn->tau_square=moldyn->tau*moldyn->tau;
1611 moldyn->time_steps=sched->runs[sched->count];
1613 /* energy scaling factor (might change!) */
1614 energy_scale=moldyn->count*EV;
1616 /* integration according to schedule */
1618 for(i=0;i<moldyn->time_steps;i++) {
1620 /* integration step */
1621 moldyn->integrate(moldyn);
1623 /* calculate kinetic energy, temperature and pressure */
1625 temperature_calc(moldyn);
1627 pressure_calc(moldyn);
1628 //thermodynamic_pressure_calc(moldyn);
1630 /* calculate fluctuations + averages */
1631 average_and_fluctuation_calc(moldyn);
1634 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1635 scale_velocity(moldyn,FALSE);
1636 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1637 scale_volume(moldyn);
1639 /* check for log & visualization */
1641 if(!(moldyn->total_steps%e))
1642 dprintf(moldyn->efd,
1644 moldyn->time,moldyn->ekin/energy_scale,
1645 moldyn->energy/energy_scale,
1646 get_total_energy(moldyn)/energy_scale);
1649 if(!(moldyn->total_steps%m)) {
1650 momentum=get_total_p(moldyn);
1651 dprintf(moldyn->mfd,
1652 "%f %f %f %f %f\n",moldyn->time,
1653 momentum.x,momentum.y,momentum.z,
1654 v3_norm(&momentum));
1658 if(!(moldyn->total_steps%p)) {
1659 dprintf(moldyn->pfd,
1660 "%f %f %f %f %f %f %f\n",moldyn->time,
1661 moldyn->p/BAR,moldyn->p_avg/BAR,
1662 moldyn->gp/BAR,moldyn->gp_avg/BAR,
1663 moldyn->tp/BAR,moldyn->tp_avg/BAR);
1667 if(!(moldyn->total_steps%t)) {
1668 dprintf(moldyn->tfd,
1670 moldyn->time,moldyn->t,moldyn->t_avg);
1674 if(!(moldyn->total_steps%v)) {
1675 dprintf(moldyn->vfd,
1676 "%f %f\n",moldyn->time,moldyn->volume);
1680 if(!(moldyn->total_steps%s)) {
1681 snprintf(dir,128,"%s/s-%07.f.save",
1682 moldyn->vlsdir,moldyn->time);
1683 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
1685 if(fd<0) perror("[moldyn] save fd open");
1687 write(fd,moldyn,sizeof(t_moldyn));
1688 write(fd,moldyn->atom,
1689 moldyn->count*sizeof(t_atom));
1695 if(!(moldyn->total_steps%a)) {
1696 visual_atoms(moldyn);
1700 /* display progress */
1701 //if(!(moldyn->total_steps%10)) {
1702 /* get current time */
1703 gettimeofday(&t2,NULL);
1705 printf("\rsched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)",
1706 sched->count,i,moldyn->total_steps,
1707 moldyn->t,moldyn->t_avg,
1708 moldyn->p/BAR,moldyn->p_avg/BAR,
1710 (int)(t2.tv_sec-t1.tv_sec));
1714 /* copy over time */
1718 /* increase absolute time */
1719 moldyn->time+=moldyn->tau;
1720 moldyn->total_steps+=1;
1724 /* check for hooks */
1726 printf("\n ## schedule hook %d start ##\n",
1728 sched->hook(moldyn,sched->hook_params);
1729 printf(" ## schedule hook end ##\n");
1732 /* increase the schedule counter */
1740 /* velocity verlet */
1742 int velocity_verlet(t_moldyn *moldyn) {
1745 double tau,tau_square,h;
1750 count=moldyn->count;
1752 tau_square=moldyn->tau_square;
1754 for(i=0;i<count;i++) {
1755 /* check whether fixed atom */
1756 if(atom[i].attr&ATOM_ATTR_FP)
1760 v3_scale(&delta,&(atom[i].v),tau);
1761 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1762 v3_scale(&delta,&(atom[i].f),h*tau_square);
1763 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1764 check_per_bound(moldyn,&(atom[i].r));
1766 /* velocities [actually v(t+tau/2)] */
1767 v3_scale(&delta,&(atom[i].f),h*tau);
1768 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1771 /* criticial check */
1772 moldyn_bc_check(moldyn);
1774 /* neighbour list update */
1775 link_cell_update(moldyn);
1777 /* forces depending on chosen potential */
1778 potential_force_calc(moldyn);
1780 for(i=0;i<count;i++) {
1781 /* check whether fixed atom */
1782 if(atom[i].attr&ATOM_ATTR_FP)
1784 /* again velocities [actually v(t+tau)] */
1785 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1786 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1795 * potentials & corresponding forces & virial routine
1799 /* generic potential and force calculation */
1801 int potential_force_calc(t_moldyn *moldyn) {
1804 t_atom *itom,*jtom,*ktom;
1808 int *neighbour_i[27];
1812 t_list neighbour_i[27];
1813 t_list neighbour_i2[27];
1819 count=moldyn->count;
1829 /* reset global virial */
1830 memset(&(moldyn->gvir),0,sizeof(t_virial));
1832 /* reset force, site energy and virial of every atom */
1833 for(i=0;i<count;i++) {
1836 v3_zero(&(itom[i].f));
1839 virial=(&(itom[i].virial));
1847 /* reset site energy */
1852 /* get energy, force and virial of every atom */
1854 /* first (and only) loop over atoms i */
1855 for(i=0;i<count;i++) {
1857 /* single particle potential/force */
1858 if(itom[i].attr&ATOM_ATTR_1BP)
1860 moldyn->func1b(moldyn,&(itom[i]));
1862 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1865 /* 2 body pair potential/force */
1867 link_cell_neighbour_index(moldyn,
1868 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1869 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1870 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1875 /* first loop over atoms j */
1876 if(moldyn->func2b) {
1883 while(neighbour_i[j][p]!=0) {
1885 jtom=&(atom[neighbour_i[j][p]]);
1888 if(jtom==&(itom[i]))
1891 if((jtom->attr&ATOM_ATTR_2BP)&
1892 (itom[i].attr&ATOM_ATTR_2BP)) {
1893 moldyn->func2b(moldyn,
1900 this=&(neighbour_i[j]);
1903 if(this->start==NULL)
1907 jtom=this->current->data;
1909 if(jtom==&(itom[i]))
1912 if((jtom->attr&ATOM_ATTR_2BP)&
1913 (itom[i].attr&ATOM_ATTR_2BP)) {
1914 moldyn->func2b(moldyn,
1919 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1925 /* 3 body potential/force */
1927 if(!(itom[i].attr&ATOM_ATTR_3BP))
1930 /* copy the neighbour lists */
1932 /* no copy needed for static lists */
1934 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
1937 /* second loop over atoms j */
1944 while(neighbour_i[j][p]!=0) {
1946 jtom=&(atom[neighbour_i[j][p]]);
1949 this=&(neighbour_i[j]);
1952 if(this->start==NULL)
1957 jtom=this->current->data;
1960 if(jtom==&(itom[i]))
1963 if(!(jtom->attr&ATOM_ATTR_3BP))
1969 if(moldyn->func3b_j1)
1970 moldyn->func3b_j1(moldyn,
1975 /* in first j loop, 3bp run can be skipped */
1976 if(!(moldyn->run3bp))
1979 /* first loop over atoms k */
1980 if(moldyn->func3b_k1) {
1988 while(neighbour_i[j][q]!=0) {
1990 ktom=&(atom[neighbour_i[k][q]]);
1993 that=&(neighbour_i2[k]);
1996 if(that->start==NULL)
2000 ktom=that->current->data;
2003 if(!(ktom->attr&ATOM_ATTR_3BP))
2009 if(ktom==&(itom[i]))
2012 moldyn->func3b_k1(moldyn,
2020 } while(list_next_f(that)!=\
2028 if(moldyn->func3b_j2)
2029 moldyn->func3b_j2(moldyn,
2034 /* second loop over atoms k */
2035 if(moldyn->func3b_k2) {
2043 while(neighbour_i[j][q]!=0) {
2045 ktom=&(atom[neighbour_i[k][q]]);
2048 that=&(neighbour_i2[k]);
2051 if(that->start==NULL)
2055 ktom=that->current->data;
2058 if(!(ktom->attr&ATOM_ATTR_3BP))
2064 if(ktom==&(itom[i]))
2067 moldyn->func3b_k2(moldyn,
2076 } while(list_next_f(that)!=\
2084 /* 2bp post function */
2085 if(moldyn->func3b_j3) {
2086 moldyn->func3b_j3(moldyn,
2093 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2108 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2109 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2111 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
2112 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
2113 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
2117 /* some postprocessing */
2118 for(i=0;i<count;i++) {
2119 /* calculate global virial */
2120 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
2121 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
2122 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
2123 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
2124 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
2125 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
2127 /* check forces regarding the given timestep */
2128 if(v3_norm(&(itom[i].f))>\
2129 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
2130 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",
2138 * virial calculation
2141 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2142 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2144 a->virial.xx+=f->x*d->x;
2145 a->virial.yy+=f->y*d->y;
2146 a->virial.zz+=f->z*d->z;
2147 a->virial.xy+=f->x*d->y;
2148 a->virial.xz+=f->x*d->z;
2149 a->virial.yz+=f->y*d->z;
2155 * periodic boundary checking
2158 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2159 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2170 if(moldyn->status&MOLDYN_STAT_PBX) {
2171 if(a->x>=x) a->x-=dim->x;
2172 else if(-a->x>x) a->x+=dim->x;
2174 if(moldyn->status&MOLDYN_STAT_PBY) {
2175 if(a->y>=y) a->y-=dim->y;
2176 else if(-a->y>y) a->y+=dim->y;
2178 if(moldyn->status&MOLDYN_STAT_PBZ) {
2179 if(a->z>=z) a->z-=dim->z;
2180 else if(-a->z>z) a->z+=dim->z;
2187 * debugging / critical check functions
2190 int moldyn_bc_check(t_moldyn *moldyn) {
2203 for(i=0;i<moldyn->count;i++) {
2204 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2205 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2206 i,atom[i].r.x,dim->x/2);
2207 printf("diagnostic:\n");
2208 printf("-----------\natom.r.x:\n");
2210 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2213 ((byte)&(1<<k))?1:0,
2216 printf("---------------\nx=dim.x/2:\n");
2218 memcpy(&byte,(u8 *)(&x)+j,1);
2221 ((byte)&(1<<k))?1:0,
2224 if(atom[i].r.x==x) printf("the same!\n");
2225 else printf("different!\n");
2227 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2228 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2229 i,atom[i].r.y,dim->y/2);
2230 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2231 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2232 i,atom[i].r.z,dim->z/2);
2242 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2249 fd=open(file,O_RDONLY);
2251 perror("[moldyn] load save file open");
2255 fsize=lseek(fd,0,SEEK_END);
2256 lseek(fd,0,SEEK_SET);
2258 size=sizeof(t_moldyn);
2261 cnt=read(fd,moldyn,size);
2263 perror("[moldyn] load save file read (moldyn)");
2269 size=moldyn->count*sizeof(t_atom);
2271 /* correcting possible atom data offset */
2273 if(fsize!=sizeof(t_moldyn)+size) {
2274 corr=fsize-sizeof(t_moldyn)-size;
2275 printf("[moldyn] WARNING: lsf (illegal file size)\n");
2276 printf(" moifying offset:\n");
2277 printf(" - current pos: %d\n",sizeof(t_moldyn));
2278 printf(" - atom size: %d\n",size);
2279 printf(" - file size: %d\n",fsize);
2280 printf(" => correction: %d\n",corr);
2281 lseek(fd,corr,SEEK_CUR);
2284 moldyn->atom=(t_atom *)malloc(size);
2285 if(moldyn->atom==NULL) {
2286 perror("[moldyn] load save file malloc (atoms)");
2291 cnt=read(fd,moldyn->atom,size);
2293 perror("[moldyn] load save file read (atoms)");
2304 int moldyn_free_save_file(t_moldyn *moldyn) {
2311 int moldyn_load(t_moldyn *moldyn) {
2319 * function to find/callback all combinations of 2 body bonds
2322 int process_2b_bonds(t_moldyn *moldyn,void *data,
2323 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2324 void *data,u8 bc)) {
2331 t_list neighbour[27];
2340 link_cell_init(moldyn,VERBOSE);
2344 for(i=0;i<moldyn->count;i++) {
2345 /* neighbour indexing */
2346 link_cell_neighbour_index(moldyn,
2347 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2348 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2349 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2354 bc=(j<lc->dnlc)?0:1;
2359 while(neighbour[j][p]!=0) {
2361 jtom=&(moldyn->atom[neighbour[j][p]]);
2364 this=&(neighbour[j]);
2367 if(this->start==NULL)
2372 jtom=this->current->data;
2376 process(moldyn,&(itom[i]),jtom,data,bc);
2381 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2391 * post processing functions
2394 int get_line(int fd,char *line,int max) {
2401 if(count==max) return count;
2402 ret=read(fd,line+count,1);
2403 if(ret<=0) return ret;
2404 if(line[count]=='\n') {
2405 memset(line+count,0,max-count-1);
2413 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2419 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
2435 for(i=0;i<moldyn->count;i++) {
2437 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
2438 check_per_bound(moldyn,&dist);
2439 d2=v3_absolute_square(&dist);
2453 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
2454 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
2455 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
2460 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
2465 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
2466 t_atom *jtom,void *data,u8 bc) {
2473 /* only count pairs once,
2474 * skip same atoms */
2475 if(itom->tag>=jtom->tag)
2479 * pair correlation calc
2486 v3_sub(&dist,&(jtom->r),&(itom->r));
2487 if(bc) check_per_bound(moldyn,&dist);
2488 d=v3_absolute_square(&dist);
2490 /* ignore if greater cutoff */
2491 if(d>moldyn->cutoff_square)
2494 /* fill the slots */
2498 /* should never happen but it does 8) -
2499 * related to -ffloat-store problem! */
2501 printf("[moldyn] WARNING: pcc (%d/%d)",
2507 if(itom->brand!=jtom->brand) {
2512 /* type a - type a bonds */
2514 pcc->stat[s+pcc->o1]+=1;
2516 /* type b - type b bonds */
2517 pcc->stat[s+pcc->o2]+=1;
2523 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2530 pcc.o1=moldyn->cutoff/dr;
2533 if(pcc.o1*dr<=moldyn->cutoff)
2534 printf("[moldyn] WARNING: pcc (low #slots)\n");
2536 printf("[moldyn] pair correlation calc info:\n");
2537 printf(" time: %f\n",moldyn->time);
2538 printf(" count: %d\n",moldyn->count);
2539 printf(" cutoff: %f\n",moldyn->cutoff);
2540 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2543 pcc.stat=(double *)ptr;
2546 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
2547 if(pcc.stat==NULL) {
2548 perror("[moldyn] pair correlation malloc");
2553 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
2556 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
2559 for(i=1;i<pcc.o1;i++) {
2560 // normalization: 4 pi r^2 dr
2561 // here: not double counting pairs -> 2 pi r r dr
2562 // ... and actually it's a constant times r^2
2565 pcc.stat[pcc.o1+i]/=norm;
2566 pcc.stat[pcc.o2+i]/=norm;
2571 /* todo: store/print pair correlation function */
2578 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2585 if(itom->tag>=jtom->tag)
2589 v3_sub(&dist,&(jtom->r),&(itom->r));
2590 if(bc) check_per_bound(moldyn,&dist);
2591 d=v3_absolute_square(&dist);
2593 /* ignore if greater or equal cutoff */
2594 if(d>moldyn->cutoff_square)
2597 /* check for potential bond */
2598 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2603 /* now count this bonding ... */
2606 /* increase total bond counter
2607 * ... double counting!
2612 ba->acnt[jtom->tag]+=1;
2614 ba->bcnt[jtom->tag]+=1;
2617 ba->acnt[itom->tag]+=1;
2619 ba->bcnt[itom->tag]+=1;
2624 int bond_analyze(t_moldyn *moldyn,double *quality) {
2626 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
2634 ba.acnt=malloc(moldyn->count*sizeof(int));
2636 perror("[moldyn] bond analyze malloc (a)");
2639 memset(ba.acnt,0,moldyn->count*sizeof(int));
2641 ba.bcnt=malloc(moldyn->count*sizeof(int));
2643 perror("[moldyn] bond analyze malloc (b)");
2646 memset(ba.bcnt,0,moldyn->count*sizeof(int));
2655 process_2b_bonds(moldyn,&ba,bond_analyze_process);
2657 for(i=0;i<moldyn->count;i++) {
2658 if(atom[i].brand==0) {
2659 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
2663 if((ba.acnt[i]==4)&(ba.bcnt[i]==0)) {
2671 printf("[moldyn] bond analyze: c_cnt=%d | set=%d\n",ccnt,cset);
2672 printf("[moldyn] bond analyze: q_cnt=%d | tot=%d\n",qcnt,ba.tcnt);
2675 quality[0]=1.0*ccnt/cset;
2676 quality[1]=1.0*qcnt/ba.tcnt;
2679 printf("[moldyn] bond analyze: c_bnd_q=%f\n",1.0*qcnt/ba.tcnt);
2680 printf("[moldyn] bond analyze: tot_q=%f\n",1.0*qcnt/ba.tcnt);
2687 * visualization code
2690 int visual_init(t_moldyn *moldyn,char *filebase) {
2692 strncpy(moldyn->vis.fb,filebase,128);
2697 int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2704 if(itom->tag>=jtom->tag)
2707 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2710 if((itom->attr&ATOM_ATTR_VB)|(jtom->attr&ATOM_ATTR_VB))
2711 dprintf(vb->fd,"# [B] %f %f %f %f %f %f\n",
2712 itom->r.x,itom->r.y,itom->r.z,
2713 jtom->r.x,jtom->r.y,jtom->r.z);
2718 int visual_atoms(t_moldyn *moldyn) {
2736 sprintf(file,"%s/atomic_conf_%07.f.xyz",v->fb,moldyn->time);
2737 vb.fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
2739 perror("open visual save file fd");
2743 /* write the actual data file */
2746 dprintf(vb.fd,"# [P] %d %07.f <%f,%f,%f>\n",
2747 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
2749 // atomic configuration
2750 for(i=0;i<moldyn->count;i++)
2751 // atom type, positions, color and kinetic energy
2752 dprintf(vb.fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
2756 pse_col[atom[i].element],
2759 // bonds between atoms
2760 process_2b_bonds(moldyn,&vb,visual_bonds_process);
2764 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2765 -dim.x/2,-dim.y/2,-dim.z/2,
2766 dim.x/2,-dim.y/2,-dim.z/2);
2767 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2768 -dim.x/2,-dim.y/2,-dim.z/2,
2769 -dim.x/2,dim.y/2,-dim.z/2);
2770 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2771 dim.x/2,dim.y/2,-dim.z/2,
2772 dim.x/2,-dim.y/2,-dim.z/2);
2773 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2774 -dim.x/2,dim.y/2,-dim.z/2,
2775 dim.x/2,dim.y/2,-dim.z/2);
2777 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2778 -dim.x/2,-dim.y/2,dim.z/2,
2779 dim.x/2,-dim.y/2,dim.z/2);
2780 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2781 -dim.x/2,-dim.y/2,dim.z/2,
2782 -dim.x/2,dim.y/2,dim.z/2);
2783 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2784 dim.x/2,dim.y/2,dim.z/2,
2785 dim.x/2,-dim.y/2,dim.z/2);
2786 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2787 -dim.x/2,dim.y/2,dim.z/2,
2788 dim.x/2,dim.y/2,dim.z/2);
2790 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2791 -dim.x/2,-dim.y/2,dim.z/2,
2792 -dim.x/2,-dim.y/2,-dim.z/2);
2793 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2794 -dim.x/2,dim.y/2,dim.z/2,
2795 -dim.x/2,dim.y/2,-dim.z/2);
2796 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2797 dim.x/2,-dim.y/2,dim.z/2,
2798 dim.x/2,-dim.y/2,-dim.z/2);
2799 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2800 dim.x/2,dim.y/2,dim.z/2,
2801 dim.x/2,dim.y/2,-dim.z/2);