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;
135 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
140 int set_bondlen(t_moldyn *moldyn,double b0,double b1,double bm) {
142 moldyn->bondlen[0]=b0*b0;
143 moldyn->bondlen[1]=b1*b1;
145 moldyn->bondlen[2]=b0*b1;
147 moldyn->bondlen[2]=bm*bm;
152 int set_temperature(t_moldyn *moldyn,double t_ref) {
156 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
161 int set_pressure(t_moldyn *moldyn,double p_ref) {
165 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
170 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
172 moldyn->pt_scale=(ptype|ttype);
176 printf("[moldyn] p/t scaling:\n");
178 printf(" p: %s",ptype?"yes":"no ");
180 printf(" | type: %02x | factor: %f",ptype,ptc);
183 printf(" t: %s",ttype?"yes":"no ");
185 printf(" | type: %02x | factor: %f",ttype,ttc);
191 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
197 moldyn->volume=x*y*z;
205 printf("[moldyn] dimensions in A and A^3 respectively:\n");
206 printf(" x: %f\n",moldyn->dim.x);
207 printf(" y: %f\n",moldyn->dim.y);
208 printf(" z: %f\n",moldyn->dim.z);
209 printf(" volume: %f\n",moldyn->volume);
210 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
215 int set_nn_dist(t_moldyn *moldyn,double dist) {
222 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
224 printf("[moldyn] periodic boundary conditions:\n");
227 moldyn->status|=MOLDYN_STAT_PBX;
230 moldyn->status|=MOLDYN_STAT_PBY;
233 moldyn->status|=MOLDYN_STAT_PBZ;
235 printf(" x: %s\n",x?"yes":"no");
236 printf(" y: %s\n",y?"yes":"no");
237 printf(" z: %s\n",z?"yes":"no");
242 int set_potential(t_moldyn *moldyn,u8 type) {
245 case MOLDYN_POTENTIAL_TM:
246 moldyn->func1b=tersoff_mult_1bp;
247 moldyn->func3b_j1=tersoff_mult_3bp_j1;
248 moldyn->func3b_k1=tersoff_mult_3bp_k1;
249 moldyn->func3b_j2=tersoff_mult_3bp_j2;
250 moldyn->func3b_k2=tersoff_mult_3bp_k2;
251 // missing: check 2b bond func
253 case MOLDYN_POTENTIAL_AM:
254 moldyn->func3b_j1=albe_mult_3bp_j1;
255 moldyn->func3b_k1=albe_mult_3bp_k1;
256 moldyn->func3b_j2=albe_mult_3bp_j2;
257 moldyn->func3b_k2=albe_mult_3bp_k2;
258 moldyn->check_2b_bond=albe_mult_check_2b_bond;
261 printf("[moldyn] set potential: unknown type %02x\n",
269 int set_avg_skip(t_moldyn *moldyn,int skip) {
271 printf("[moldyn] skip %d steps before starting average calc\n",skip);
272 moldyn->avg_skip=skip;
277 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
279 strncpy(moldyn->vlsdir,dir,127);
284 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
286 strncpy(moldyn->rauthor,author,63);
287 strncpy(moldyn->rtitle,title,63);
292 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
297 printf("[moldyn] set log: ");
300 case LOG_TOTAL_ENERGY:
301 moldyn->ewrite=timer;
302 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
303 moldyn->efd=open(filename,
304 O_WRONLY|O_CREAT|O_EXCL,
307 perror("[moldyn] energy log fd open");
310 dprintf(moldyn->efd,"# total energy log file\n");
311 printf("total energy (%d)\n",timer);
313 case LOG_TOTAL_MOMENTUM:
314 moldyn->mwrite=timer;
315 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
316 moldyn->mfd=open(filename,
317 O_WRONLY|O_CREAT|O_EXCL,
320 perror("[moldyn] momentum log fd open");
323 dprintf(moldyn->efd,"# total momentum log file\n");
324 printf("total momentum (%d)\n",timer);
327 moldyn->pwrite=timer;
328 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
329 moldyn->pfd=open(filename,
330 O_WRONLY|O_CREAT|O_EXCL,
333 perror("[moldyn] pressure log file\n");
336 dprintf(moldyn->pfd,"# pressure log file\n");
337 printf("pressure (%d)\n",timer);
339 case LOG_TEMPERATURE:
340 moldyn->twrite=timer;
341 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
342 moldyn->tfd=open(filename,
343 O_WRONLY|O_CREAT|O_EXCL,
346 perror("[moldyn] temperature log file\n");
349 dprintf(moldyn->tfd,"# temperature log file\n");
350 printf("temperature (%d)\n",timer);
353 moldyn->vwrite=timer;
354 snprintf(filename,127,"%s/volume",moldyn->vlsdir);
355 moldyn->vfd=open(filename,
356 O_WRONLY|O_CREAT|O_EXCL,
359 perror("[moldyn] volume log file\n");
362 dprintf(moldyn->vfd,"# volume log file\n");
363 printf("volume (%d)\n",timer);
366 moldyn->swrite=timer;
367 printf("save file (%d)\n",timer);
370 moldyn->awrite=timer;
371 ret=visual_init(moldyn,moldyn->vlsdir);
373 printf("[moldyn] visual init failure\n");
376 printf("visual file (%d)\n",timer);
379 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
380 moldyn->rfd=open(filename,
381 O_WRONLY|O_CREAT|O_EXCL,
384 perror("[moldyn] report fd open");
387 printf("report -> ");
389 snprintf(filename,127,"%s/e_plot.scr",
391 moldyn->epfd=open(filename,
392 O_WRONLY|O_CREAT|O_EXCL,
395 perror("[moldyn] energy plot fd open");
398 dprintf(moldyn->epfd,e_plot_script);
403 snprintf(filename,127,"%s/pressure_plot.scr",
405 moldyn->ppfd=open(filename,
406 O_WRONLY|O_CREAT|O_EXCL,
409 perror("[moldyn] p plot fd open");
412 dprintf(moldyn->ppfd,pressure_plot_script);
417 snprintf(filename,127,"%s/temperature_plot.scr",
419 moldyn->tpfd=open(filename,
420 O_WRONLY|O_CREAT|O_EXCL,
423 perror("[moldyn] t plot fd open");
426 dprintf(moldyn->tpfd,temperature_plot_script);
428 printf("temperature ");
430 dprintf(moldyn->rfd,report_start,
431 moldyn->rauthor,moldyn->rtitle);
435 printf("unknown log type: %02x\n",type);
442 int moldyn_log_shutdown(t_moldyn *moldyn) {
446 printf("[moldyn] log shutdown\n");
450 dprintf(moldyn->rfd,report_energy);
451 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
456 if(moldyn->mfd) close(moldyn->mfd);
460 dprintf(moldyn->rfd,report_pressure);
461 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
468 dprintf(moldyn->rfd,report_temperature);
469 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
474 dprintf(moldyn->rfd,report_end);
476 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
479 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
482 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
491 * creating lattice functions
494 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
495 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
506 /* how many atoms do we expect */
507 if(type==CUBIC) new*=1;
508 if(type==FCC) new*=4;
509 if(type==DIAMOND) new*=8;
511 /* allocate space for atoms */
512 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
514 perror("[moldyn] realloc (create lattice)");
518 atom=&(moldyn->atom[count]);
520 /* no atoms on the boundaries (only reason: it looks better!) */
534 set_nn_dist(moldyn,lc);
535 ret=cubic_init(a,b,c,lc,atom,&orig);
539 v3_scale(&orig,&orig,0.5);
540 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
541 ret=fcc_init(a,b,c,lc,atom,&orig);
545 v3_scale(&orig,&orig,0.25);
546 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
547 ret=diamond_init(a,b,c,lc,atom,&orig);
550 printf("unknown lattice type (%02x)\n",type);
556 printf("[moldyn] creating lattice failed\n");
557 printf(" amount of atoms\n");
558 printf(" - expected: %d\n",new);
559 printf(" - created: %d\n",ret);
564 printf("[moldyn] created lattice with %d atoms\n",new);
566 for(ret=0;ret<new;ret++) {
567 atom[ret].element=element;
570 atom[ret].brand=brand;
571 atom[ret].tag=count+ret;
572 check_per_bound(moldyn,&(atom[ret].r));
573 atom[ret].r_0=atom[ret].r;
576 /* update total system mass */
577 total_mass_calc(moldyn);
582 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
583 t_3dvec *r,t_3dvec *v) {
590 count=(moldyn->count)++; // asshole style!
592 ptr=realloc(atom,(count+1)*sizeof(t_atom));
594 perror("[moldyn] realloc (add atom)");
601 /* initialize new atom */
602 memset(&(atom[count]),0,sizeof(t_atom));
605 atom[count].element=element;
606 atom[count].mass=mass;
607 atom[count].brand=brand;
608 atom[count].tag=count;
609 atom[count].attr=attr;
610 check_per_bound(moldyn,&(atom[count].r));
611 atom[count].r_0=atom[count].r;
613 /* update total system mass */
614 total_mass_calc(moldyn);
619 int del_atom(t_moldyn *moldyn,int tag) {
626 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
628 perror("[moldyn]malloc (del atom)");
632 for(cnt=0;cnt<tag;cnt++)
635 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
637 new[cnt-1].tag=cnt-1;
649 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
668 v3_copy(&(atom[count].r),&r);
677 for(i=0;i<count;i++) {
678 atom[i].r.x-=(a*lc)/2.0;
679 atom[i].r.y-=(b*lc)/2.0;
680 atom[i].r.z-=(c*lc)/2.0;
686 /* fcc lattice init */
687 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
700 /* construct the basis */
701 memset(basis,0,3*sizeof(t_3dvec));
709 /* fill up the room */
717 v3_copy(&(atom[count].r),&r);
720 /* the three face centered atoms */
722 v3_add(&n,&r,&basis[l]);
723 v3_copy(&(atom[count].r),&n);
732 /* coordinate transformation */
733 for(i=0;i<count;i++) {
734 atom[i].r.x-=(a*lc)/2.0;
735 atom[i].r.y-=(b*lc)/2.0;
736 atom[i].r.z-=(c*lc)/2.0;
742 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
747 count=fcc_init(a,b,c,lc,atom,origin);
753 if(origin) v3_add(&o,&o,origin);
755 count+=fcc_init(a,b,c,lc,&atom[count],&o);
760 int destroy_atoms(t_moldyn *moldyn) {
762 if(moldyn->atom) free(moldyn->atom);
767 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
770 * - gaussian distribution of velocities
771 * - zero total momentum
772 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
777 t_3dvec p_total,delta;
782 random=&(moldyn->random);
784 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
786 /* gaussian distribution of velocities */
788 for(i=0;i<moldyn->count;i++) {
789 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
791 v=sigma*rand_get_gauss(random);
793 p_total.x+=atom[i].mass*v;
795 v=sigma*rand_get_gauss(random);
797 p_total.y+=atom[i].mass*v;
799 v=sigma*rand_get_gauss(random);
801 p_total.z+=atom[i].mass*v;
804 /* zero total momentum */
805 v3_scale(&p_total,&p_total,1.0/moldyn->count);
806 for(i=0;i<moldyn->count;i++) {
807 v3_scale(&delta,&p_total,1.0/atom[i].mass);
808 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
811 /* velocity scaling */
812 scale_velocity(moldyn,equi_init);
817 double total_mass_calc(t_moldyn *moldyn) {
823 for(i=0;i<moldyn->count;i++)
824 moldyn->mass+=moldyn->atom[i].mass;
829 double temperature_calc(t_moldyn *moldyn) {
831 /* assume up to date kinetic energy, which is 3/2 N k_B T */
833 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
838 double get_temperature(t_moldyn *moldyn) {
843 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
853 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
856 /* get kinetic energy / temperature & count involved atoms */
859 for(i=0;i<moldyn->count;i++) {
860 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
861 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
866 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
867 else return 0; /* no atoms involved in scaling! */
869 /* (temporary) hack for e,t = 0 */
872 if(moldyn->t_ref!=0.0) {
873 thermal_init(moldyn,equi_init);
877 return 0; /* no scaling needed */
881 /* get scaling factor */
882 scale=moldyn->t_ref/moldyn->t;
886 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
887 scale=1.0+(scale-1.0)/moldyn->t_tc;
890 /* velocity scaling */
891 for(i=0;i<moldyn->count;i++) {
892 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
893 v3_scale(&(atom[i].v),&(atom[i].v),scale);
899 double ideal_gas_law_pressure(t_moldyn *moldyn) {
903 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
908 double virial_sum(t_moldyn *moldyn) {
913 /* virial (sum over atom virials) */
921 for(i=0;i<moldyn->count;i++) {
922 virial=&(moldyn->atom[i].virial);
923 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
924 moldyn->vir.xx+=virial->xx;
925 moldyn->vir.yy+=virial->yy;
926 moldyn->vir.zz+=virial->zz;
927 moldyn->vir.xy+=virial->xy;
928 moldyn->vir.xz+=virial->xz;
929 moldyn->vir.yz+=virial->yz;
932 /* global virial (absolute coordinates) */
933 virial=&(moldyn->gvir);
934 moldyn->gv=virial->xx+virial->yy+virial->zz;
936 return moldyn->virial;
939 double pressure_calc(t_moldyn *moldyn) {
943 * with W = 1/3 sum_i f_i r_i (- skipped!)
944 * virial = sum_i f_i r_i
946 * => P = (2 Ekin + virial) / (3V)
949 /* assume up to date virial & up to date kinetic energy */
951 /* pressure (atom virials) */
952 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
953 moldyn->p/=(3.0*moldyn->volume);
955 /* pressure (absolute coordinates) */
956 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
957 moldyn->gp/=(3.0*moldyn->volume);
962 int average_reset(t_moldyn *moldyn) {
964 printf("[moldyn] average reset\n");
966 /* update skip value */
967 moldyn->avg_skip=moldyn->total_steps;
973 /* potential energy */
981 moldyn->virial_sum=0.0;
992 int average_and_fluctuation_calc(t_moldyn *moldyn) {
996 if(moldyn->total_steps<moldyn->avg_skip)
999 denom=moldyn->total_steps+1-moldyn->avg_skip;
1001 /* assume up to date energies, temperature, pressure etc */
1003 /* kinetic energy */
1004 moldyn->k_sum+=moldyn->ekin;
1005 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
1006 moldyn->k_avg=moldyn->k_sum/denom;
1007 moldyn->k2_avg=moldyn->k2_sum/denom;
1008 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
1010 /* potential energy */
1011 moldyn->v_sum+=moldyn->energy;
1012 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
1013 moldyn->v_avg=moldyn->v_sum/denom;
1014 moldyn->v2_avg=moldyn->v2_sum/denom;
1015 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1018 moldyn->t_sum+=moldyn->t;
1019 moldyn->t_avg=moldyn->t_sum/denom;
1022 moldyn->virial_sum+=moldyn->virial;
1023 moldyn->virial_avg=moldyn->virial_sum/denom;
1024 moldyn->gv_sum+=moldyn->gv;
1025 moldyn->gv_avg=moldyn->gv_sum/denom;
1028 moldyn->p_sum+=moldyn->p;
1029 moldyn->p_avg=moldyn->p_sum/denom;
1030 moldyn->gp_sum+=moldyn->gp;
1031 moldyn->gp_avg=moldyn->gp_sum/denom;
1032 moldyn->tp_sum+=moldyn->tp;
1033 moldyn->tp_avg=moldyn->tp_sum/denom;
1038 int get_heat_capacity(t_moldyn *moldyn) {
1042 /* averages needed for heat capacity calc */
1043 if(moldyn->total_steps<moldyn->avg_skip)
1046 /* (temperature average)^2 */
1047 temp2=moldyn->t_avg*moldyn->t_avg;
1048 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1051 /* ideal gas contribution */
1052 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1053 printf(" ideal gas contribution: %f\n",
1054 ighc/moldyn->mass*KILOGRAM/JOULE);
1056 /* specific heat for nvt ensemble */
1057 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1058 moldyn->c_v_nvt/=moldyn->mass;
1060 /* specific heat for nve ensemble */
1061 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1062 moldyn->c_v_nve/=moldyn->mass;
1064 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1065 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1066 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)));
1071 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1087 /* store atomic configuration + dimension */
1088 store=malloc(moldyn->count*sizeof(t_atom));
1090 printf("[moldyn] allocating store mem failed\n");
1093 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1098 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1099 su=pow(2.0-h,ONE_THIRD)-1.0;
1100 dv=(1.0-h)*moldyn->volume;
1102 /* scale up dimension and atom positions */
1103 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1104 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1105 link_cell_shutdown(moldyn);
1106 link_cell_init(moldyn,QUIET);
1107 potential_force_calc(moldyn);
1110 /* restore atomic configuration + dim */
1111 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1114 /* scale down dimension and atom positions */
1115 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1116 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1117 link_cell_shutdown(moldyn);
1118 link_cell_init(moldyn,QUIET);
1119 potential_force_calc(moldyn);
1122 /* calculate pressure */
1123 moldyn->tp=-(y1-y0)/(2.0*dv);
1125 /* restore atomic configuration */
1126 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1128 link_cell_shutdown(moldyn);
1129 link_cell_init(moldyn,QUIET);
1130 //potential_force_calc(moldyn);
1132 /* free store buffer */
1139 double get_pressure(t_moldyn *moldyn) {
1145 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1157 if(x) dim->x*=scale;
1158 if(y) dim->y*=scale;
1159 if(z) dim->z*=scale;
1164 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1175 for(i=0;i<moldyn->count;i++) {
1176 r=&(moldyn->atom[i].r);
1185 int scale_volume(t_moldyn *moldyn) {
1191 vdim=&(moldyn->vis.dim);
1195 /* scaling factor */
1196 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1197 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc;
1198 scale=pow(scale,ONE_THIRD);
1201 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1204 /* scale the atoms and dimensions */
1205 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1206 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1208 /* visualize dimensions */
1215 /* recalculate scaled volume */
1216 moldyn->volume=dim->x*dim->y*dim->z;
1218 /* adjust/reinit linkcell */
1219 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1220 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1221 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1222 link_cell_shutdown(moldyn);
1223 link_cell_init(moldyn,QUIET);
1234 double e_kin_calc(t_moldyn *moldyn) {
1242 for(i=0;i<moldyn->count;i++) {
1243 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1244 moldyn->ekin+=atom[i].ekin;
1247 return moldyn->ekin;
1250 double get_total_energy(t_moldyn *moldyn) {
1252 return(moldyn->ekin+moldyn->energy);
1255 t_3dvec get_total_p(t_moldyn *moldyn) {
1264 for(i=0;i<moldyn->count;i++) {
1265 v3_scale(&p,&(atom[i].v),atom[i].mass);
1266 v3_add(&p_total,&p_total,&p);
1272 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1276 /* nn_dist is the nearest neighbour distance */
1278 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1287 /* linked list / cell method */
1289 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1296 /* partitioning the md cell */
1297 lc->nx=moldyn->dim.x/moldyn->cutoff;
1298 lc->x=moldyn->dim.x/lc->nx;
1299 lc->ny=moldyn->dim.y/moldyn->cutoff;
1300 lc->y=moldyn->dim.y/lc->ny;
1301 lc->nz=moldyn->dim.z/moldyn->cutoff;
1302 lc->z=moldyn->dim.z/lc->nz;
1303 lc->cells=lc->nx*lc->ny*lc->nz;
1306 lc->subcell=malloc(lc->cells*sizeof(int*));
1308 lc->subcell=malloc(lc->cells*sizeof(t_list));
1311 if(lc->subcell==NULL) {
1312 perror("[moldyn] cell init (malloc)");
1317 printf("[moldyn] FATAL: less then 27 subcells!\n");
1321 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1324 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1327 printf(" x: %d x %f A\n",lc->nx,lc->x);
1328 printf(" y: %d x %f A\n",lc->ny,lc->y);
1329 printf(" z: %d x %f A\n",lc->nz,lc->z);
1334 for(i=0;i<lc->cells;i++) {
1335 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1336 if(lc->subcell[i]==NULL) {
1337 perror("[moldyn] list init (malloc)");
1342 printf(" ---> %d malloc %p (%p)\n",
1343 i,lc->subcell[0],lc->subcell);
1347 for(i=0;i<lc->cells;i++)
1348 list_init_f(&(lc->subcell[i]));
1351 /* update the list */
1352 link_cell_update(moldyn);
1357 int link_cell_update(t_moldyn *moldyn) {
1373 for(i=0;i<lc->cells;i++)
1375 memset(lc->subcell[i],0,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1377 list_destroy_f(&(lc->subcell[i]));
1380 for(count=0;count<moldyn->count;count++) {
1381 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1382 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1383 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1387 while(lc->subcell[i+j*nx+k*nx*ny][p]!=0)
1390 if(p>=MAX_ATOMS_PER_LIST) {
1391 printf("[moldyn] FATAL: amount of atoms too high!\n");
1395 lc->subcell[i+j*nx+k*nx*ny][p]=count;
1397 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1401 printf(" ---> %d %d malloc %p (%p)\n",
1402 i,count,lc->subcell[i].current,lc->subcell);
1410 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1434 if(i>=nx||j>=ny||k>=nz)
1435 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1438 cell[0]=lc->subcell[i+j*nx+k*a];
1439 for(ci=-1;ci<=1;ci++) {
1442 if((x<0)||(x>=nx)) {
1446 for(cj=-1;cj<=1;cj++) {
1449 if((y<0)||(y>=ny)) {
1453 for(ck=-1;ck<=1;ck++) {
1456 if((z<0)||(z>=nz)) {
1460 if(!(ci|cj|ck)) continue;
1462 cell[--count2]=lc->subcell[x+y*nx+z*a];
1465 cell[count1++]=lc->subcell[x+y*nx+z*a];
1476 int link_cell_shutdown(t_moldyn *moldyn) {
1483 for(i=0;i<lc->cells;i++) {
1485 free(lc->subcell[i]);
1487 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1488 list_destroy_f(&(lc->subcell[i]));
1497 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1501 t_moldyn_schedule *schedule;
1503 schedule=&(moldyn->schedule);
1504 count=++(schedule->total_sched);
1506 ptr=realloc(schedule->runs,count*sizeof(int));
1508 perror("[moldyn] realloc (runs)");
1512 schedule->runs[count-1]=runs;
1514 ptr=realloc(schedule->tau,count*sizeof(double));
1516 perror("[moldyn] realloc (tau)");
1520 schedule->tau[count-1]=tau;
1522 printf("[moldyn] schedule added:\n");
1523 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1529 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1531 moldyn->schedule.hook=hook;
1532 moldyn->schedule.hook_params=hook_params;
1539 * 'integration of newtons equation' - algorithms
1543 /* start the integration */
1545 int moldyn_integrate(t_moldyn *moldyn) {
1548 unsigned int e,m,s,v,p,t,a;
1550 t_moldyn_schedule *sched;
1555 double energy_scale;
1556 struct timeval t1,t2;
1559 sched=&(moldyn->schedule);
1562 /* initialize linked cell method */
1563 link_cell_init(moldyn,VERBOSE);
1565 /* logging & visualization */
1574 /* sqaure of some variables */
1575 moldyn->tau_square=moldyn->tau*moldyn->tau;
1576 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
1578 /* get current time */
1579 gettimeofday(&t1,NULL);
1581 /* calculate initial forces */
1582 potential_force_calc(moldyn);
1587 /* some stupid checks before we actually start calculating bullshit */
1588 if(moldyn->cutoff>0.5*moldyn->dim.x)
1589 printf("[moldyn] WARNING: cutoff > 0.5 x dim.x\n");
1590 if(moldyn->cutoff>0.5*moldyn->dim.y)
1591 printf("[moldyn] WARNING: cutoff > 0.5 x dim.y\n");
1592 if(moldyn->cutoff>0.5*moldyn->dim.z)
1593 printf("[moldyn] WARNING: cutoff > 0.5 x dim.z\n");
1594 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1595 if(ds>0.05*moldyn->nnd)
1596 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1598 /* zero absolute time */
1600 moldyn->total_steps=0;
1602 /* debugging, ignore */
1605 /* tell the world */
1606 printf("[moldyn] integration start, go get a coffee ...\n");
1608 /* executing the schedule */
1610 while(sched->count<sched->total_sched) {
1612 /* setting amount of runs and finite time step size */
1613 moldyn->tau=sched->tau[sched->count];
1614 moldyn->tau_square=moldyn->tau*moldyn->tau;
1615 moldyn->time_steps=sched->runs[sched->count];
1617 /* energy scaling factor (might change!) */
1618 energy_scale=moldyn->count*EV;
1620 /* integration according to schedule */
1622 for(i=0;i<moldyn->time_steps;i++) {
1624 /* integration step */
1625 moldyn->integrate(moldyn);
1627 /* calculate kinetic energy, temperature and pressure */
1629 temperature_calc(moldyn);
1631 pressure_calc(moldyn);
1632 //thermodynamic_pressure_calc(moldyn);
1634 /* calculate fluctuations + averages */
1635 average_and_fluctuation_calc(moldyn);
1638 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1639 scale_velocity(moldyn,FALSE);
1640 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1641 scale_volume(moldyn);
1643 /* check for log & visualization */
1645 if(!(moldyn->total_steps%e))
1646 dprintf(moldyn->efd,
1648 moldyn->time,moldyn->ekin/energy_scale,
1649 moldyn->energy/energy_scale,
1650 get_total_energy(moldyn)/energy_scale);
1653 if(!(moldyn->total_steps%m)) {
1654 momentum=get_total_p(moldyn);
1655 dprintf(moldyn->mfd,
1656 "%f %f %f %f %f\n",moldyn->time,
1657 momentum.x,momentum.y,momentum.z,
1658 v3_norm(&momentum));
1662 if(!(moldyn->total_steps%p)) {
1663 dprintf(moldyn->pfd,
1664 "%f %f %f %f %f %f %f\n",moldyn->time,
1665 moldyn->p/BAR,moldyn->p_avg/BAR,
1666 moldyn->gp/BAR,moldyn->gp_avg/BAR,
1667 moldyn->tp/BAR,moldyn->tp_avg/BAR);
1671 if(!(moldyn->total_steps%t)) {
1672 dprintf(moldyn->tfd,
1674 moldyn->time,moldyn->t,moldyn->t_avg);
1678 if(!(moldyn->total_steps%v)) {
1679 dprintf(moldyn->vfd,
1680 "%f %f\n",moldyn->time,moldyn->volume);
1684 if(!(moldyn->total_steps%s)) {
1685 snprintf(dir,128,"%s/s-%07.f.save",
1686 moldyn->vlsdir,moldyn->time);
1687 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
1689 if(fd<0) perror("[moldyn] save fd open");
1691 write(fd,moldyn,sizeof(t_moldyn));
1692 write(fd,moldyn->atom,
1693 moldyn->count*sizeof(t_atom));
1699 if(!(moldyn->total_steps%a)) {
1700 visual_atoms(moldyn);
1704 /* display progress */
1705 //if(!(moldyn->total_steps%10)) {
1706 /* get current time */
1707 gettimeofday(&t2,NULL);
1709 printf("\rsched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)",
1710 sched->count,i,moldyn->total_steps,
1711 moldyn->t,moldyn->t_avg,
1712 moldyn->p/BAR,moldyn->p_avg/BAR,
1714 (int)(t2.tv_sec-t1.tv_sec));
1718 /* copy over time */
1722 /* increase absolute time */
1723 moldyn->time+=moldyn->tau;
1724 moldyn->total_steps+=1;
1728 /* check for hooks */
1730 printf("\n ## schedule hook %d start ##\n",
1732 sched->hook(moldyn,sched->hook_params);
1733 printf(" ## schedule hook end ##\n");
1736 /* increase the schedule counter */
1744 /* velocity verlet */
1746 int velocity_verlet(t_moldyn *moldyn) {
1749 double tau,tau_square,h;
1754 count=moldyn->count;
1756 tau_square=moldyn->tau_square;
1758 for(i=0;i<count;i++) {
1759 /* check whether fixed atom */
1760 if(atom[i].attr&ATOM_ATTR_FP)
1764 v3_scale(&delta,&(atom[i].v),tau);
1765 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1766 v3_scale(&delta,&(atom[i].f),h*tau_square);
1767 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1768 check_per_bound(moldyn,&(atom[i].r));
1770 /* velocities [actually v(t+tau/2)] */
1771 v3_scale(&delta,&(atom[i].f),h*tau);
1772 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1775 /* criticial check */
1776 moldyn_bc_check(moldyn);
1778 /* neighbour list update */
1779 link_cell_update(moldyn);
1781 /* forces depending on chosen potential */
1782 potential_force_calc(moldyn);
1784 for(i=0;i<count;i++) {
1785 /* check whether fixed atom */
1786 if(atom[i].attr&ATOM_ATTR_FP)
1788 /* again velocities [actually v(t+tau)] */
1789 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1790 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1799 * potentials & corresponding forces & virial routine
1803 /* generic potential and force calculation */
1805 int potential_force_calc(t_moldyn *moldyn) {
1808 t_atom *itom,*jtom,*ktom;
1812 int *neighbour_i[27];
1816 t_list neighbour_i[27];
1817 t_list neighbour_i2[27];
1823 count=moldyn->count;
1833 /* reset global virial */
1834 memset(&(moldyn->gvir),0,sizeof(t_virial));
1836 /* reset force, site energy and virial of every atom */
1837 for(i=0;i<count;i++) {
1840 v3_zero(&(itom[i].f));
1843 virial=(&(itom[i].virial));
1851 /* reset site energy */
1856 /* get energy, force and virial of every atom */
1858 /* first (and only) loop over atoms i */
1859 for(i=0;i<count;i++) {
1861 /* single particle potential/force */
1862 if(itom[i].attr&ATOM_ATTR_1BP)
1864 moldyn->func1b(moldyn,&(itom[i]));
1866 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1869 /* 2 body pair potential/force */
1871 link_cell_neighbour_index(moldyn,
1872 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1873 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1874 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1879 /* first loop over atoms j */
1880 if(moldyn->func2b) {
1887 while(neighbour_i[j][p]!=0) {
1889 jtom=&(atom[neighbour_i[j][p]]);
1892 if(jtom==&(itom[i]))
1895 if((jtom->attr&ATOM_ATTR_2BP)&
1896 (itom[i].attr&ATOM_ATTR_2BP)) {
1897 moldyn->func2b(moldyn,
1904 this=&(neighbour_i[j]);
1907 if(this->start==NULL)
1911 jtom=this->current->data;
1913 if(jtom==&(itom[i]))
1916 if((jtom->attr&ATOM_ATTR_2BP)&
1917 (itom[i].attr&ATOM_ATTR_2BP)) {
1918 moldyn->func2b(moldyn,
1923 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1929 /* 3 body potential/force */
1931 if(!(itom[i].attr&ATOM_ATTR_3BP))
1934 /* copy the neighbour lists */
1936 /* no copy needed for static lists */
1938 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
1941 /* second loop over atoms j */
1948 while(neighbour_i[j][p]!=0) {
1950 jtom=&(atom[neighbour_i[j][p]]);
1953 this=&(neighbour_i[j]);
1956 if(this->start==NULL)
1961 jtom=this->current->data;
1964 if(jtom==&(itom[i]))
1967 if(!(jtom->attr&ATOM_ATTR_3BP))
1973 if(moldyn->func3b_j1)
1974 moldyn->func3b_j1(moldyn,
1979 /* in first j loop, 3bp run can be skipped */
1980 if(!(moldyn->run3bp))
1983 /* first loop over atoms k */
1984 if(moldyn->func3b_k1) {
1992 while(neighbour_i[j][q]!=0) {
1994 ktom=&(atom[neighbour_i[k][q]]);
1997 that=&(neighbour_i2[k]);
2000 if(that->start==NULL)
2004 ktom=that->current->data;
2007 if(!(ktom->attr&ATOM_ATTR_3BP))
2013 if(ktom==&(itom[i]))
2016 moldyn->func3b_k1(moldyn,
2024 } while(list_next_f(that)!=\
2032 if(moldyn->func3b_j2)
2033 moldyn->func3b_j2(moldyn,
2038 /* second loop over atoms k */
2039 if(moldyn->func3b_k2) {
2047 while(neighbour_i[j][q]!=0) {
2049 ktom=&(atom[neighbour_i[k][q]]);
2052 that=&(neighbour_i2[k]);
2055 if(that->start==NULL)
2059 ktom=that->current->data;
2062 if(!(ktom->attr&ATOM_ATTR_3BP))
2068 if(ktom==&(itom[i]))
2071 moldyn->func3b_k2(moldyn,
2080 } while(list_next_f(that)!=\
2088 /* 2bp post function */
2089 if(moldyn->func3b_j3) {
2090 moldyn->func3b_j3(moldyn,
2097 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2112 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2113 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2115 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
2116 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
2117 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
2121 /* some postprocessing */
2122 for(i=0;i<count;i++) {
2123 /* calculate global virial */
2124 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
2125 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
2126 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
2127 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
2128 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
2129 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
2131 /* check forces regarding the given timestep */
2132 if(v3_norm(&(itom[i].f))>\
2133 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
2134 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",
2142 * virial calculation
2145 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2146 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2148 a->virial.xx+=f->x*d->x;
2149 a->virial.yy+=f->y*d->y;
2150 a->virial.zz+=f->z*d->z;
2151 a->virial.xy+=f->x*d->y;
2152 a->virial.xz+=f->x*d->z;
2153 a->virial.yz+=f->y*d->z;
2159 * periodic boundary checking
2162 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2163 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2174 if(moldyn->status&MOLDYN_STAT_PBX) {
2175 if(a->x>=x) a->x-=dim->x;
2176 else if(-a->x>x) a->x+=dim->x;
2178 if(moldyn->status&MOLDYN_STAT_PBY) {
2179 if(a->y>=y) a->y-=dim->y;
2180 else if(-a->y>y) a->y+=dim->y;
2182 if(moldyn->status&MOLDYN_STAT_PBZ) {
2183 if(a->z>=z) a->z-=dim->z;
2184 else if(-a->z>z) a->z+=dim->z;
2191 * debugging / critical check functions
2194 int moldyn_bc_check(t_moldyn *moldyn) {
2207 for(i=0;i<moldyn->count;i++) {
2208 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2209 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2210 i,atom[i].r.x,dim->x/2);
2211 printf("diagnostic:\n");
2212 printf("-----------\natom.r.x:\n");
2214 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2217 ((byte)&(1<<k))?1:0,
2220 printf("---------------\nx=dim.x/2:\n");
2222 memcpy(&byte,(u8 *)(&x)+j,1);
2225 ((byte)&(1<<k))?1:0,
2228 if(atom[i].r.x==x) printf("the same!\n");
2229 else printf("different!\n");
2231 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2232 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2233 i,atom[i].r.y,dim->y/2);
2234 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2235 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2236 i,atom[i].r.z,dim->z/2);
2246 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2253 fd=open(file,O_RDONLY);
2255 perror("[moldyn] load save file open");
2259 fsize=lseek(fd,0,SEEK_END);
2260 lseek(fd,0,SEEK_SET);
2262 size=sizeof(t_moldyn);
2265 cnt=read(fd,moldyn,size);
2267 perror("[moldyn] load save file read (moldyn)");
2273 size=moldyn->count*sizeof(t_atom);
2275 /* correcting possible atom data offset */
2277 if(fsize!=sizeof(t_moldyn)+size) {
2278 corr=fsize-sizeof(t_moldyn)-size;
2279 printf("[moldyn] WARNING: lsf (illegal file size)\n");
2280 printf(" moifying offset:\n");
2281 printf(" - current pos: %d\n",sizeof(t_moldyn));
2282 printf(" - atom size: %d\n",size);
2283 printf(" - file size: %d\n",fsize);
2284 printf(" => correction: %d\n",corr);
2285 lseek(fd,corr,SEEK_CUR);
2288 moldyn->atom=(t_atom *)malloc(size);
2289 if(moldyn->atom==NULL) {
2290 perror("[moldyn] load save file malloc (atoms)");
2295 cnt=read(fd,moldyn->atom,size);
2297 perror("[moldyn] load save file read (atoms)");
2308 int moldyn_free_save_file(t_moldyn *moldyn) {
2315 int moldyn_load(t_moldyn *moldyn) {
2323 * function to find/callback all combinations of 2 body bonds
2326 int process_2b_bonds(t_moldyn *moldyn,void *data,
2327 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2328 void *data,u8 bc)) {
2335 t_list neighbour[27];
2344 link_cell_init(moldyn,VERBOSE);
2348 for(i=0;i<moldyn->count;i++) {
2349 /* neighbour indexing */
2350 link_cell_neighbour_index(moldyn,
2351 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2352 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2353 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2358 bc=(j<lc->dnlc)?0:1;
2363 while(neighbour[j][p]!=0) {
2365 jtom=&(moldyn->atom[neighbour[j][p]]);
2368 this=&(neighbour[j]);
2371 if(this->start==NULL)
2376 jtom=this->current->data;
2380 process(moldyn,&(itom[i]),jtom,data,bc);
2385 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2395 * post processing functions
2398 int get_line(int fd,char *line,int max) {
2405 if(count==max) return count;
2406 ret=read(fd,line+count,1);
2407 if(ret<=0) return ret;
2408 if(line[count]=='\n') {
2416 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2422 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
2438 for(i=0;i<moldyn->count;i++) {
2440 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
2441 check_per_bound(moldyn,&dist);
2442 d2=v3_absolute_square(&dist);
2456 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
2457 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
2458 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
2463 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
2468 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
2469 t_atom *jtom,void *data,u8 bc) {
2476 /* only count pairs once,
2477 * skip same atoms */
2478 if(itom->tag>=jtom->tag)
2482 * pair correlation calc
2489 v3_sub(&dist,&(jtom->r),&(itom->r));
2490 if(bc) check_per_bound(moldyn,&dist);
2491 d=v3_absolute_square(&dist);
2493 /* ignore if greater cutoff */
2494 if(d>moldyn->cutoff_square)
2497 /* fill the slots */
2501 /* should never happen but it does 8) -
2502 * related to -ffloat-store problem! */
2504 printf("[moldyn] WARNING: pcc (%d/%d)",
2510 if(itom->brand!=jtom->brand) {
2515 /* type a - type a bonds */
2517 pcc->stat[s+pcc->o1]+=1;
2519 /* type b - type b bonds */
2520 pcc->stat[s+pcc->o2]+=1;
2526 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2533 pcc.o1=moldyn->cutoff/dr;
2536 if(pcc.o1*dr<=moldyn->cutoff)
2537 printf("[moldyn] WARNING: pcc (low #slots)\n");
2539 printf("[moldyn] pair correlation calc info:\n");
2540 printf(" time: %f\n",moldyn->time);
2541 printf(" count: %d\n",moldyn->count);
2542 printf(" cutoff: %f\n",moldyn->cutoff);
2543 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2546 pcc.stat=(double *)ptr;
2549 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
2550 if(pcc.stat==NULL) {
2551 perror("[moldyn] pair correlation malloc");
2556 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
2559 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
2562 for(i=1;i<pcc.o1;i++) {
2563 // normalization: 4 pi r^2 dr
2564 // here: not double counting pairs -> 2 pi r r dr
2565 // ... and actually it's a constant times r^2
2568 pcc.stat[pcc.o1+i]/=norm;
2569 pcc.stat[pcc.o2+i]/=norm;
2574 /* todo: store/print pair correlation function */
2581 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2588 if(itom->tag>=jtom->tag)
2592 v3_sub(&dist,&(jtom->r),&(itom->r));
2593 if(bc) check_per_bound(moldyn,&dist);
2594 d=v3_absolute_square(&dist);
2596 /* ignore if greater or equal cutoff */
2597 if(d>moldyn->cutoff_square)
2600 /* check for potential bond */
2601 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2606 /* now count this bonding ... */
2609 /* increase total bond counter
2610 * ... double counting!
2615 ba->acnt[jtom->tag]+=1;
2617 ba->bcnt[jtom->tag]+=1;
2620 ba->acnt[itom->tag]+=1;
2622 ba->bcnt[itom->tag]+=1;
2627 int bond_analyze(t_moldyn *moldyn,double *quality) {
2629 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
2637 ba.acnt=malloc(moldyn->count*sizeof(int));
2639 perror("[moldyn] bond analyze malloc (a)");
2642 memset(ba.acnt,0,moldyn->count*sizeof(int));
2644 ba.bcnt=malloc(moldyn->count*sizeof(int));
2646 perror("[moldyn] bond analyze malloc (b)");
2649 memset(ba.bcnt,0,moldyn->count*sizeof(int));
2658 process_2b_bonds(moldyn,&ba,bond_analyze_process);
2660 for(i=0;i<moldyn->count;i++) {
2661 if(atom[i].brand==0) {
2662 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
2666 if((ba.acnt[i]==4)&(ba.bcnt[i]==0)) {
2674 printf("[moldyn] bond analyze: c_cnt=%d | set=%d\n",ccnt,cset);
2675 printf("[moldyn] bond analyze: q_cnt=%d | tot=%d\n",qcnt,ba.tcnt);
2678 quality[0]=1.0*ccnt/cset;
2679 quality[1]=1.0*qcnt/ba.tcnt;
2682 printf("[moldyn] bond analyze: c_bnd_q=%f\n",1.0*qcnt/ba.tcnt);
2683 printf("[moldyn] bond analyze: tot_q=%f\n",1.0*qcnt/ba.tcnt);
2690 * visualization code
2693 int visual_init(t_moldyn *moldyn,char *filebase) {
2695 strncpy(moldyn->vis.fb,filebase,128);
2700 int visual_atoms(t_moldyn *moldyn) {
2714 t_list neighbour[27];
2730 sprintf(file,"%s/atomic_conf_%07.f.xyz",v->fb,moldyn->time);
2731 fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
2733 perror("open visual save file fd");
2737 /* write the actual data file */
2740 dprintf(fd,"# [P] %d %07.f <%f,%f,%f>\n",
2741 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
2743 // atomic configuration
2744 for(i=0;i<moldyn->count;i++) {
2745 // atom type, positions, color and kinetic energy
2746 dprintf(fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
2750 pse_col[atom[i].element],
2754 * bond detection should usually be done by potential
2755 * functions. brrrrr! EVIL!
2757 * todo: potentials need to export a 'find_bonds' function!
2760 // bonds between atoms
2761 if(!(atom[i].attr&ATOM_ATTR_VB))
2763 link_cell_neighbour_index(moldyn,
2764 (atom[i].r.x+moldyn->dim.x/2)/lc->x,
2765 (atom[i].r.y+moldyn->dim.y/2)/lc->y,
2766 (atom[i].r.z+moldyn->dim.z/2)/lc->z,
2772 while(neighbour[j][p]!=0) {
2773 btom=&(atom[neighbour[j][p]]);
2776 list_reset_f(&neighbour[j]);
2777 if(neighbour[j].start==NULL)
2780 btom=neighbour[j].current->data;
2782 if(btom==&atom[i]) // skip identical atoms
2784 //if(btom<&atom[i]) // skip half of them
2786 v3_sub(&dist,&(atom[i].r),&(btom->r));
2787 if(bc) check_per_bound(moldyn,&dist);
2788 d2=v3_absolute_square(&dist);
2789 brand=atom[i].brand;
2790 if(brand==btom->brand) {
2791 if(d2>moldyn->bondlen[brand])
2795 if(d2>moldyn->bondlen[2])
2798 dprintf(fd,"# [B] %f %f %f %f %f %f\n",
2799 atom[i].r.x,atom[i].r.y,atom[i].r.z,
2800 btom->r.x,btom->r.y,btom->r.z);
2804 } while(list_next_f(&neighbour[j])!=L_NO_NEXT_ELEMENT);
2811 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2812 -dim.x/2,-dim.y/2,-dim.z/2,
2813 dim.x/2,-dim.y/2,-dim.z/2);
2814 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2815 -dim.x/2,-dim.y/2,-dim.z/2,
2816 -dim.x/2,dim.y/2,-dim.z/2);
2817 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2818 dim.x/2,dim.y/2,-dim.z/2,
2819 dim.x/2,-dim.y/2,-dim.z/2);
2820 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2821 -dim.x/2,dim.y/2,-dim.z/2,
2822 dim.x/2,dim.y/2,-dim.z/2);
2824 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2825 -dim.x/2,-dim.y/2,dim.z/2,
2826 dim.x/2,-dim.y/2,dim.z/2);
2827 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2828 -dim.x/2,-dim.y/2,dim.z/2,
2829 -dim.x/2,dim.y/2,dim.z/2);
2830 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2831 dim.x/2,dim.y/2,dim.z/2,
2832 dim.x/2,-dim.y/2,dim.z/2);
2833 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2834 -dim.x/2,dim.y/2,dim.z/2,
2835 dim.x/2,dim.y/2,dim.z/2);
2837 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2838 -dim.x/2,-dim.y/2,dim.z/2,
2839 -dim.x/2,-dim.y/2,-dim.z/2);
2840 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2841 -dim.x/2,dim.y/2,dim.z/2,
2842 -dim.x/2,dim.y/2,-dim.z/2);
2843 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2844 dim.x/2,-dim.y/2,dim.z/2,
2845 dim.x/2,-dim.y/2,-dim.z/2);
2846 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2847 dim.x/2,dim.y/2,dim.z/2,
2848 dim.x/2,dim.y/2,-dim.z/2);