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 static double pse_mass[]={
105 static double pse_lc[]={
129 * the moldyn functions
132 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
134 printf("[moldyn] init\n");
136 memset(moldyn,0,sizeof(t_moldyn));
141 rand_init(&(moldyn->random),NULL,1);
142 moldyn->random.status|=RAND_STAT_VERBOSE;
147 int moldyn_shutdown(t_moldyn *moldyn) {
149 printf("[moldyn] shutdown\n");
151 moldyn_log_shutdown(moldyn);
152 link_cell_shutdown(moldyn);
153 rand_close(&(moldyn->random));
159 int set_int_alg(t_moldyn *moldyn,u8 algo) {
161 printf("[moldyn] integration algorithm: ");
164 case MOLDYN_INTEGRATE_VERLET:
165 moldyn->integrate=velocity_verlet;
166 printf("velocity verlet\n");
169 printf("unknown integration algorithm: %02x\n",algo);
177 int set_cutoff(t_moldyn *moldyn,double cutoff) {
179 moldyn->cutoff=cutoff;
180 moldyn->cutoff_square=cutoff*cutoff;
182 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
187 int set_temperature(t_moldyn *moldyn,double t_ref) {
191 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
196 int set_pressure(t_moldyn *moldyn,double p_ref) {
200 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
205 int set_p_scale(t_moldyn *moldyn,u8 ptype,double ptc) {
207 moldyn->pt_scale&=(~(P_SCALE_MASK));
208 moldyn->pt_scale|=ptype;
211 printf("[moldyn] p/t scaling:\n");
213 printf(" p: %s",ptype?"yes":"no ");
215 printf(" | type: %02x | factor: %f",ptype,ptc);
221 int set_t_scale(t_moldyn *moldyn,u8 ttype,double ttc) {
223 moldyn->pt_scale&=(~(T_SCALE_MASK));
224 moldyn->pt_scale|=ttype;
227 printf("[moldyn] p/t scaling:\n");
229 printf(" t: %s",ttype?"yes":"no ");
231 printf(" | type: %02x | factor: %f",ttype,ttc);
237 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
239 moldyn->pt_scale=(ptype|ttype);
243 printf("[moldyn] p/t scaling:\n");
245 printf(" p: %s",ptype?"yes":"no ");
247 printf(" | type: %02x | factor: %f",ptype,ptc);
250 printf(" t: %s",ttype?"yes":"no ");
252 printf(" | type: %02x | factor: %f",ttype,ttc);
258 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
264 moldyn->volume=x*y*z;
272 printf("[moldyn] dimensions in A and A^3 respectively:\n");
273 printf(" x: %f\n",moldyn->dim.x);
274 printf(" y: %f\n",moldyn->dim.y);
275 printf(" z: %f\n",moldyn->dim.z);
276 printf(" volume: %f\n",moldyn->volume);
277 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
282 int set_nn_dist(t_moldyn *moldyn,double dist) {
289 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
291 printf("[moldyn] periodic boundary conditions:\n");
294 moldyn->status|=MOLDYN_STAT_PBX;
297 moldyn->status|=MOLDYN_STAT_PBY;
300 moldyn->status|=MOLDYN_STAT_PBZ;
302 printf(" x: %s\n",x?"yes":"no");
303 printf(" y: %s\n",y?"yes":"no");
304 printf(" z: %s\n",z?"yes":"no");
309 int set_potential(t_moldyn *moldyn,u8 type) {
312 case MOLDYN_POTENTIAL_TM:
313 moldyn->func1b=tersoff_mult_1bp;
314 moldyn->func3b_j1=tersoff_mult_3bp_j1;
315 moldyn->func3b_k1=tersoff_mult_3bp_k1;
316 moldyn->func3b_j2=tersoff_mult_3bp_j2;
317 moldyn->func3b_k2=tersoff_mult_3bp_k2;
318 // missing: check 2b bond func
320 case MOLDYN_POTENTIAL_AM:
321 moldyn->func3b_j1=albe_mult_3bp_j1;
322 moldyn->func3b_k1=albe_mult_3bp_k1;
323 moldyn->func3b_j2=albe_mult_3bp_j2;
324 moldyn->func3b_k2=albe_mult_3bp_k2;
325 moldyn->check_2b_bond=albe_mult_check_2b_bond;
327 case MOLDYN_POTENTIAL_HO:
328 moldyn->func2b=harmonic_oscillator;
329 moldyn->check_2b_bond=harmonic_oscillator_check_2b_bond;
331 case MOLDYN_POTENTIAL_LJ:
332 moldyn->func2b=lennard_jones;
333 moldyn->check_2b_bond=lennard_jones_check_2b_bond;
336 printf("[moldyn] set potential: unknown type %02x\n",
344 int set_avg_skip(t_moldyn *moldyn,int skip) {
346 printf("[moldyn] skip %d steps before starting average calc\n",skip);
347 moldyn->avg_skip=skip;
352 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
354 strncpy(moldyn->vlsdir,dir,127);
359 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
361 strncpy(moldyn->rauthor,author,63);
362 strncpy(moldyn->rtitle,title,63);
367 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
372 printf("[moldyn] set log: ");
375 case LOG_TOTAL_ENERGY:
376 moldyn->ewrite=timer;
377 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
378 moldyn->efd=open(filename,
379 O_WRONLY|O_CREAT|O_EXCL,
382 perror("[moldyn] energy log fd open");
385 dprintf(moldyn->efd,"# total energy log file\n");
386 printf("total energy (%d)\n",timer);
388 case LOG_TOTAL_MOMENTUM:
389 moldyn->mwrite=timer;
390 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
391 moldyn->mfd=open(filename,
392 O_WRONLY|O_CREAT|O_EXCL,
395 perror("[moldyn] momentum log fd open");
398 dprintf(moldyn->efd,"# total momentum log file\n");
399 printf("total momentum (%d)\n",timer);
402 moldyn->pwrite=timer;
403 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
404 moldyn->pfd=open(filename,
405 O_WRONLY|O_CREAT|O_EXCL,
408 perror("[moldyn] pressure log file\n");
411 dprintf(moldyn->pfd,"# pressure log file\n");
412 printf("pressure (%d)\n",timer);
414 case LOG_TEMPERATURE:
415 moldyn->twrite=timer;
416 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
417 moldyn->tfd=open(filename,
418 O_WRONLY|O_CREAT|O_EXCL,
421 perror("[moldyn] temperature log file\n");
424 dprintf(moldyn->tfd,"# temperature log file\n");
425 printf("temperature (%d)\n",timer);
428 moldyn->vwrite=timer;
429 snprintf(filename,127,"%s/volume",moldyn->vlsdir);
430 moldyn->vfd=open(filename,
431 O_WRONLY|O_CREAT|O_EXCL,
434 perror("[moldyn] volume log file\n");
437 dprintf(moldyn->vfd,"# volume log file\n");
438 printf("volume (%d)\n",timer);
441 moldyn->swrite=timer;
442 printf("save file (%d)\n",timer);
445 moldyn->awrite=timer;
446 ret=visual_init(moldyn,moldyn->vlsdir);
448 printf("[moldyn] visual init failure\n");
451 printf("visual file (%d)\n",timer);
454 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
455 moldyn->rfd=open(filename,
456 O_WRONLY|O_CREAT|O_EXCL,
459 perror("[moldyn] report fd open");
462 printf("report -> ");
464 snprintf(filename,127,"%s/e_plot.scr",
466 moldyn->epfd=open(filename,
467 O_WRONLY|O_CREAT|O_EXCL,
470 perror("[moldyn] energy plot fd open");
473 dprintf(moldyn->epfd,e_plot_script);
478 snprintf(filename,127,"%s/pressure_plot.scr",
480 moldyn->ppfd=open(filename,
481 O_WRONLY|O_CREAT|O_EXCL,
484 perror("[moldyn] p plot fd open");
487 dprintf(moldyn->ppfd,pressure_plot_script);
492 snprintf(filename,127,"%s/temperature_plot.scr",
494 moldyn->tpfd=open(filename,
495 O_WRONLY|O_CREAT|O_EXCL,
498 perror("[moldyn] t plot fd open");
501 dprintf(moldyn->tpfd,temperature_plot_script);
503 printf("temperature ");
505 dprintf(moldyn->rfd,report_start,
506 moldyn->rauthor,moldyn->rtitle);
510 printf("unknown log type: %02x\n",type);
517 int moldyn_log_shutdown(t_moldyn *moldyn) {
521 printf("[moldyn] log shutdown\n");
525 dprintf(moldyn->rfd,report_energy);
526 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
531 if(moldyn->mfd) close(moldyn->mfd);
535 dprintf(moldyn->rfd,report_pressure);
536 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
543 dprintf(moldyn->rfd,report_temperature);
544 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
549 dprintf(moldyn->rfd,report_end);
551 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
554 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
557 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
566 * creating lattice functions
569 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
570 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
581 /* how many atoms do we expect */
582 if(type==CUBIC) new*=1;
583 if(type==FCC) new*=4;
584 if(type==DIAMOND) new*=8;
586 /* allocate space for atoms */
587 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
589 perror("[moldyn] realloc (create lattice)");
593 atom=&(moldyn->atom[count]);
595 /* no atoms on the boundaries (only reason: it looks better!) */
609 set_nn_dist(moldyn,lc);
610 ret=cubic_init(a,b,c,lc,atom,&orig);
614 v3_scale(&orig,&orig,0.5);
615 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
616 ret=fcc_init(a,b,c,lc,atom,&orig);
620 v3_scale(&orig,&orig,0.25);
621 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
622 ret=diamond_init(a,b,c,lc,atom,&orig);
625 printf("unknown lattice type (%02x)\n",type);
631 printf("[moldyn] creating lattice failed\n");
632 printf(" amount of atoms\n");
633 printf(" - expected: %d\n",new);
634 printf(" - created: %d\n",ret);
639 printf("[moldyn] created lattice with %d atoms\n",new);
641 for(ret=0;ret<new;ret++) {
642 atom[ret].element=element;
645 atom[ret].brand=brand;
646 atom[ret].tag=count+ret;
647 check_per_bound(moldyn,&(atom[ret].r));
648 atom[ret].r_0=atom[ret].r;
651 /* update total system mass */
652 total_mass_calc(moldyn);
657 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
658 t_3dvec *r,t_3dvec *v) {
665 count=(moldyn->count)++; // asshole style!
667 ptr=realloc(atom,(count+1)*sizeof(t_atom));
669 perror("[moldyn] realloc (add atom)");
676 /* initialize new atom */
677 memset(&(atom[count]),0,sizeof(t_atom));
680 atom[count].element=element;
681 atom[count].mass=mass;
682 atom[count].brand=brand;
683 atom[count].tag=count;
684 atom[count].attr=attr;
685 check_per_bound(moldyn,&(atom[count].r));
686 atom[count].r_0=atom[count].r;
688 /* update total system mass */
689 total_mass_calc(moldyn);
694 int del_atom(t_moldyn *moldyn,int tag) {
701 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
703 perror("[moldyn]malloc (del atom)");
707 for(cnt=0;cnt<tag;cnt++)
710 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
712 new[cnt-1].tag=cnt-1;
724 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
743 v3_copy(&(atom[count].r),&r);
752 for(i=0;i<count;i++) {
753 atom[i].r.x-=(a*lc)/2.0;
754 atom[i].r.y-=(b*lc)/2.0;
755 atom[i].r.z-=(c*lc)/2.0;
761 /* fcc lattice init */
762 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
775 /* construct the basis */
776 memset(basis,0,3*sizeof(t_3dvec));
784 /* fill up the room */
792 v3_copy(&(atom[count].r),&r);
795 /* the three face centered atoms */
797 v3_add(&n,&r,&basis[l]);
798 v3_copy(&(atom[count].r),&n);
807 /* coordinate transformation */
808 for(i=0;i<count;i++) {
809 atom[i].r.x-=(a*lc)/2.0;
810 atom[i].r.y-=(b*lc)/2.0;
811 atom[i].r.z-=(c*lc)/2.0;
817 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
822 count=fcc_init(a,b,c,lc,atom,origin);
828 if(origin) v3_add(&o,&o,origin);
830 count+=fcc_init(a,b,c,lc,&atom[count],&o);
835 int destroy_atoms(t_moldyn *moldyn) {
837 if(moldyn->atom) free(moldyn->atom);
842 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
845 * - gaussian distribution of velocities
846 * - zero total momentum
847 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
852 t_3dvec p_total,delta;
857 random=&(moldyn->random);
859 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
861 /* gaussian distribution of velocities */
863 for(i=0;i<moldyn->count;i++) {
864 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
866 v=sigma*rand_get_gauss(random);
868 p_total.x+=atom[i].mass*v;
870 v=sigma*rand_get_gauss(random);
872 p_total.y+=atom[i].mass*v;
874 v=sigma*rand_get_gauss(random);
876 p_total.z+=atom[i].mass*v;
879 /* zero total momentum */
880 v3_scale(&p_total,&p_total,1.0/moldyn->count);
881 for(i=0;i<moldyn->count;i++) {
882 v3_scale(&delta,&p_total,1.0/atom[i].mass);
883 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
886 /* velocity scaling */
887 scale_velocity(moldyn,equi_init);
892 double total_mass_calc(t_moldyn *moldyn) {
898 for(i=0;i<moldyn->count;i++)
899 moldyn->mass+=moldyn->atom[i].mass;
904 double temperature_calc(t_moldyn *moldyn) {
906 /* assume up to date kinetic energy, which is 3/2 N k_B T */
908 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
913 double get_temperature(t_moldyn *moldyn) {
918 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
928 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
931 /* get kinetic energy / temperature & count involved atoms */
934 for(i=0;i<moldyn->count;i++) {
935 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
936 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
941 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
942 else return 0; /* no atoms involved in scaling! */
944 /* (temporary) hack for e,t = 0 */
947 if(moldyn->t_ref!=0.0) {
948 thermal_init(moldyn,equi_init);
952 return 0; /* no scaling needed */
956 /* get scaling factor */
957 scale=moldyn->t_ref/moldyn->t;
961 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
962 scale=1.0+(scale-1.0)/moldyn->t_tc;
965 /* velocity scaling */
966 for(i=0;i<moldyn->count;i++) {
967 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
968 v3_scale(&(atom[i].v),&(atom[i].v),scale);
974 double ideal_gas_law_pressure(t_moldyn *moldyn) {
978 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
983 double virial_sum(t_moldyn *moldyn) {
988 /* virial (sum over atom virials) */
996 for(i=0;i<moldyn->count;i++) {
997 virial=&(moldyn->atom[i].virial);
998 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
999 moldyn->vir.xx+=virial->xx;
1000 moldyn->vir.yy+=virial->yy;
1001 moldyn->vir.zz+=virial->zz;
1002 moldyn->vir.xy+=virial->xy;
1003 moldyn->vir.xz+=virial->xz;
1004 moldyn->vir.yz+=virial->yz;
1007 /* global virial (absolute coordinates) */
1008 virial=&(moldyn->gvir);
1009 moldyn->gv=virial->xx+virial->yy+virial->zz;
1011 return moldyn->virial;
1014 double pressure_calc(t_moldyn *moldyn) {
1018 * with W = 1/3 sum_i f_i r_i (- skipped!)
1019 * virial = sum_i f_i r_i
1021 * => P = (2 Ekin + virial) / (3V)
1024 /* assume up to date virial & up to date kinetic energy */
1026 /* pressure (atom virials) */
1027 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
1028 moldyn->p/=(3.0*moldyn->volume);
1030 /* pressure (absolute coordinates) */
1031 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
1032 moldyn->gp/=(3.0*moldyn->volume);
1037 int average_reset(t_moldyn *moldyn) {
1039 printf("[moldyn] average reset\n");
1041 /* update skip value */
1042 moldyn->avg_skip=moldyn->total_steps;
1044 /* kinetic energy */
1048 /* potential energy */
1056 moldyn->virial_sum=0.0;
1067 int average_and_fluctuation_calc(t_moldyn *moldyn) {
1071 if(moldyn->total_steps<moldyn->avg_skip)
1074 denom=moldyn->total_steps+1-moldyn->avg_skip;
1076 /* assume up to date energies, temperature, pressure etc */
1078 /* kinetic energy */
1079 moldyn->k_sum+=moldyn->ekin;
1080 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
1081 moldyn->k_avg=moldyn->k_sum/denom;
1082 moldyn->k2_avg=moldyn->k2_sum/denom;
1083 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
1085 /* potential energy */
1086 moldyn->v_sum+=moldyn->energy;
1087 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
1088 moldyn->v_avg=moldyn->v_sum/denom;
1089 moldyn->v2_avg=moldyn->v2_sum/denom;
1090 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1093 moldyn->t_sum+=moldyn->t;
1094 moldyn->t_avg=moldyn->t_sum/denom;
1097 moldyn->virial_sum+=moldyn->virial;
1098 moldyn->virial_avg=moldyn->virial_sum/denom;
1099 moldyn->gv_sum+=moldyn->gv;
1100 moldyn->gv_avg=moldyn->gv_sum/denom;
1103 moldyn->p_sum+=moldyn->p;
1104 moldyn->p_avg=moldyn->p_sum/denom;
1105 moldyn->gp_sum+=moldyn->gp;
1106 moldyn->gp_avg=moldyn->gp_sum/denom;
1107 moldyn->tp_sum+=moldyn->tp;
1108 moldyn->tp_avg=moldyn->tp_sum/denom;
1113 int get_heat_capacity(t_moldyn *moldyn) {
1117 /* averages needed for heat capacity calc */
1118 if(moldyn->total_steps<moldyn->avg_skip)
1121 /* (temperature average)^2 */
1122 temp2=moldyn->t_avg*moldyn->t_avg;
1123 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1126 /* ideal gas contribution */
1127 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1128 printf(" ideal gas contribution: %f\n",
1129 ighc/moldyn->mass*KILOGRAM/JOULE);
1131 /* specific heat for nvt ensemble */
1132 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1133 moldyn->c_v_nvt/=moldyn->mass;
1135 /* specific heat for nve ensemble */
1136 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1137 moldyn->c_v_nve/=moldyn->mass;
1139 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1140 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1141 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)));
1146 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1162 /* store atomic configuration + dimension */
1163 store=malloc(moldyn->count*sizeof(t_atom));
1165 printf("[moldyn] allocating store mem failed\n");
1168 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1173 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1174 su=pow(2.0-h,ONE_THIRD)-1.0;
1175 dv=(1.0-h)*moldyn->volume;
1177 /* scale up dimension and atom positions */
1178 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1179 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1180 link_cell_shutdown(moldyn);
1181 link_cell_init(moldyn,QUIET);
1182 potential_force_calc(moldyn);
1185 /* restore atomic configuration + dim */
1186 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1189 /* scale down dimension and atom positions */
1190 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1191 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1192 link_cell_shutdown(moldyn);
1193 link_cell_init(moldyn,QUIET);
1194 potential_force_calc(moldyn);
1197 /* calculate pressure */
1198 moldyn->tp=-(y1-y0)/(2.0*dv);
1200 /* restore atomic configuration */
1201 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1203 link_cell_shutdown(moldyn);
1204 link_cell_init(moldyn,QUIET);
1205 //potential_force_calc(moldyn);
1207 /* free store buffer */
1214 double get_pressure(t_moldyn *moldyn) {
1220 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1232 if(x) dim->x*=scale;
1233 if(y) dim->y*=scale;
1234 if(z) dim->z*=scale;
1239 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1250 for(i=0;i<moldyn->count;i++) {
1251 r=&(moldyn->atom[i].r);
1260 int scale_volume(t_moldyn *moldyn) {
1266 vdim=&(moldyn->vis.dim);
1270 /* scaling factor */
1271 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1272 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc;
1273 scale=pow(scale,ONE_THIRD);
1276 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1279 /* scale the atoms and dimensions */
1280 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1281 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1283 /* visualize dimensions */
1290 /* recalculate scaled volume */
1291 moldyn->volume=dim->x*dim->y*dim->z;
1293 /* adjust/reinit linkcell */
1294 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1295 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1296 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1297 link_cell_shutdown(moldyn);
1298 link_cell_init(moldyn,QUIET);
1309 double e_kin_calc(t_moldyn *moldyn) {
1317 for(i=0;i<moldyn->count;i++) {
1318 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1319 moldyn->ekin+=atom[i].ekin;
1322 return moldyn->ekin;
1325 double get_total_energy(t_moldyn *moldyn) {
1327 return(moldyn->ekin+moldyn->energy);
1330 t_3dvec get_total_p(t_moldyn *moldyn) {
1339 for(i=0;i<moldyn->count;i++) {
1340 v3_scale(&p,&(atom[i].v),atom[i].mass);
1341 v3_add(&p_total,&p_total,&p);
1347 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1351 /* nn_dist is the nearest neighbour distance */
1353 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1362 /* linked list / cell method */
1364 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1371 /* partitioning the md cell */
1372 lc->nx=moldyn->dim.x/moldyn->cutoff;
1373 lc->x=moldyn->dim.x/lc->nx;
1374 lc->ny=moldyn->dim.y/moldyn->cutoff;
1375 lc->y=moldyn->dim.y/lc->ny;
1376 lc->nz=moldyn->dim.z/moldyn->cutoff;
1377 lc->z=moldyn->dim.z/lc->nz;
1378 lc->cells=lc->nx*lc->ny*lc->nz;
1381 lc->subcell=malloc(lc->cells*sizeof(int*));
1383 lc->subcell=malloc(lc->cells*sizeof(t_list));
1386 if(lc->subcell==NULL) {
1387 perror("[moldyn] cell init (malloc)");
1392 printf("[moldyn] FATAL: less then 27 subcells!\n");
1396 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1399 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1402 printf(" x: %d x %f A\n",lc->nx,lc->x);
1403 printf(" y: %d x %f A\n",lc->ny,lc->y);
1404 printf(" z: %d x %f A\n",lc->nz,lc->z);
1409 for(i=0;i<lc->cells;i++) {
1410 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1411 if(lc->subcell[i]==NULL) {
1412 perror("[moldyn] list init (malloc)");
1417 printf(" ---> %d malloc %p (%p)\n",
1418 i,lc->subcell[0],lc->subcell);
1422 for(i=0;i<lc->cells;i++)
1423 list_init_f(&(lc->subcell[i]));
1426 /* update the list */
1427 link_cell_update(moldyn);
1432 int link_cell_update(t_moldyn *moldyn) {
1448 for(i=0;i<lc->cells;i++)
1450 memset(lc->subcell[i],0,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1452 list_destroy_f(&(lc->subcell[i]));
1455 for(count=0;count<moldyn->count;count++) {
1456 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1457 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1458 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1462 while(lc->subcell[i+j*nx+k*nx*ny][p]!=0)
1465 if(p>=MAX_ATOMS_PER_LIST) {
1466 printf("[moldyn] FATAL: amount of atoms too high!\n");
1470 lc->subcell[i+j*nx+k*nx*ny][p]=count;
1472 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1476 printf(" ---> %d %d malloc %p (%p)\n",
1477 i,count,lc->subcell[i].current,lc->subcell);
1485 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1509 if(i>=nx||j>=ny||k>=nz)
1510 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1513 cell[0]=lc->subcell[i+j*nx+k*a];
1514 for(ci=-1;ci<=1;ci++) {
1517 if((x<0)||(x>=nx)) {
1521 for(cj=-1;cj<=1;cj++) {
1524 if((y<0)||(y>=ny)) {
1528 for(ck=-1;ck<=1;ck++) {
1531 if((z<0)||(z>=nz)) {
1535 if(!(ci|cj|ck)) continue;
1537 cell[--count2]=lc->subcell[x+y*nx+z*a];
1540 cell[count1++]=lc->subcell[x+y*nx+z*a];
1551 int link_cell_shutdown(t_moldyn *moldyn) {
1558 for(i=0;i<lc->cells;i++) {
1560 free(lc->subcell[i]);
1562 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1563 list_destroy_f(&(lc->subcell[i]));
1572 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1576 t_moldyn_schedule *schedule;
1578 schedule=&(moldyn->schedule);
1579 count=++(schedule->total_sched);
1581 ptr=realloc(schedule->runs,count*sizeof(int));
1583 perror("[moldyn] realloc (runs)");
1587 schedule->runs[count-1]=runs;
1589 ptr=realloc(schedule->tau,count*sizeof(double));
1591 perror("[moldyn] realloc (tau)");
1595 schedule->tau[count-1]=tau;
1597 printf("[moldyn] schedule added:\n");
1598 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1604 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1606 moldyn->schedule.hook=hook;
1607 moldyn->schedule.hook_params=hook_params;
1614 * 'integration of newtons equation' - algorithms
1618 /* start the integration */
1620 int moldyn_integrate(t_moldyn *moldyn) {
1623 unsigned int e,m,s,v,p,t,a;
1625 t_moldyn_schedule *sched;
1630 double energy_scale;
1631 struct timeval t1,t2;
1634 sched=&(moldyn->schedule);
1637 /* initialize linked cell method */
1638 printf(" hier soll es sein\n");
1639 link_cell_init(moldyn,VERBOSE);
1641 /* logging & visualization */
1650 /* sqaure of some variables */
1651 moldyn->tau_square=moldyn->tau*moldyn->tau;
1653 /* get current time */
1654 gettimeofday(&t1,NULL);
1656 /* calculate initial forces */
1657 potential_force_calc(moldyn);
1662 /* some stupid checks before we actually start calculating bullshit */
1663 if(moldyn->cutoff>0.5*moldyn->dim.x)
1664 printf("[moldyn] WARNING: cutoff > 0.5 x dim.x\n");
1665 if(moldyn->cutoff>0.5*moldyn->dim.y)
1666 printf("[moldyn] WARNING: cutoff > 0.5 x dim.y\n");
1667 if(moldyn->cutoff>0.5*moldyn->dim.z)
1668 printf("[moldyn] WARNING: cutoff > 0.5 x dim.z\n");
1669 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1670 if(ds>0.05*moldyn->nnd)
1671 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1673 /* zero absolute time */
1675 moldyn->total_steps=0;
1677 /* debugging, ignore */
1680 /* tell the world */
1681 printf("[moldyn] integration start, go get a coffee ...\n");
1683 /* executing the schedule */
1685 while(sched->count<sched->total_sched) {
1687 /* setting amount of runs and finite time step size */
1688 moldyn->tau=sched->tau[sched->count];
1689 moldyn->tau_square=moldyn->tau*moldyn->tau;
1690 moldyn->time_steps=sched->runs[sched->count];
1692 /* energy scaling factor (might change!) */
1693 energy_scale=moldyn->count*EV;
1695 /* integration according to schedule */
1697 for(i=0;i<moldyn->time_steps;i++) {
1699 /* integration step */
1700 moldyn->integrate(moldyn);
1702 /* calculate kinetic energy, temperature and pressure */
1704 temperature_calc(moldyn);
1706 pressure_calc(moldyn);
1707 //thermodynamic_pressure_calc(moldyn);
1709 /* calculate fluctuations + averages */
1710 average_and_fluctuation_calc(moldyn);
1713 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1714 scale_velocity(moldyn,FALSE);
1715 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1716 scale_volume(moldyn);
1718 /* check for log & visualization */
1720 if(!(moldyn->total_steps%e))
1721 dprintf(moldyn->efd,
1723 moldyn->time,moldyn->ekin/energy_scale,
1724 moldyn->energy/energy_scale,
1725 get_total_energy(moldyn)/energy_scale);
1728 if(!(moldyn->total_steps%m)) {
1729 momentum=get_total_p(moldyn);
1730 dprintf(moldyn->mfd,
1731 "%f %f %f %f %f\n",moldyn->time,
1732 momentum.x,momentum.y,momentum.z,
1733 v3_norm(&momentum));
1737 if(!(moldyn->total_steps%p)) {
1738 dprintf(moldyn->pfd,
1739 "%f %f %f %f %f %f %f\n",moldyn->time,
1740 moldyn->p/BAR,moldyn->p_avg/BAR,
1741 moldyn->gp/BAR,moldyn->gp_avg/BAR,
1742 moldyn->tp/BAR,moldyn->tp_avg/BAR);
1746 if(!(moldyn->total_steps%t)) {
1747 dprintf(moldyn->tfd,
1749 moldyn->time,moldyn->t,moldyn->t_avg);
1753 if(!(moldyn->total_steps%v)) {
1754 dprintf(moldyn->vfd,
1755 "%f %f\n",moldyn->time,moldyn->volume);
1759 if(!(moldyn->total_steps%s)) {
1760 snprintf(dir,128,"%s/s-%07.f.save",
1761 moldyn->vlsdir,moldyn->time);
1762 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
1764 if(fd<0) perror("[moldyn] save fd open");
1766 write(fd,moldyn,sizeof(t_moldyn));
1767 write(fd,moldyn->atom,
1768 moldyn->count*sizeof(t_atom));
1774 if(!(moldyn->total_steps%a)) {
1775 visual_atoms(moldyn);
1779 /* display progress */
1780 //if(!(moldyn->total_steps%10)) {
1781 /* get current time */
1782 gettimeofday(&t2,NULL);
1784 printf("\rsched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)",
1785 sched->count,i,moldyn->total_steps,
1786 moldyn->t,moldyn->t_avg,
1787 moldyn->p/BAR,moldyn->p_avg/BAR,
1789 (int)(t2.tv_sec-t1.tv_sec));
1793 /* copy over time */
1797 /* increase absolute time */
1798 moldyn->time+=moldyn->tau;
1799 moldyn->total_steps+=1;
1803 /* check for hooks */
1805 printf("\n ## schedule hook %d start ##\n",
1807 sched->hook(moldyn,sched->hook_params);
1808 printf(" ## schedule hook end ##\n");
1811 /* increase the schedule counter */
1819 /* velocity verlet */
1821 int velocity_verlet(t_moldyn *moldyn) {
1824 double tau,tau_square,h;
1829 count=moldyn->count;
1831 tau_square=moldyn->tau_square;
1833 for(i=0;i<count;i++) {
1834 /* check whether fixed atom */
1835 if(atom[i].attr&ATOM_ATTR_FP)
1839 v3_scale(&delta,&(atom[i].v),tau);
1840 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1841 v3_scale(&delta,&(atom[i].f),h*tau_square);
1842 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1843 check_per_bound(moldyn,&(atom[i].r));
1845 /* velocities [actually v(t+tau/2)] */
1846 v3_scale(&delta,&(atom[i].f),h*tau);
1847 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1850 /* criticial check */
1851 moldyn_bc_check(moldyn);
1853 /* neighbour list update */
1854 link_cell_update(moldyn);
1856 /* forces depending on chosen potential */
1857 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 t_list neighbour_i[27];
1892 t_list neighbour_i2[27];
1898 count=moldyn->count;
1908 /* reset global virial */
1909 memset(&(moldyn->gvir),0,sizeof(t_virial));
1911 /* reset force, site energy and virial of every atom */
1912 for(i=0;i<count;i++) {
1915 v3_zero(&(itom[i].f));
1918 virial=(&(itom[i].virial));
1926 /* reset site energy */
1931 /* get energy, force and virial of every atom */
1933 /* first (and only) loop over atoms i */
1934 for(i=0;i<count;i++) {
1936 /* single particle potential/force */
1937 if(itom[i].attr&ATOM_ATTR_1BP)
1939 moldyn->func1b(moldyn,&(itom[i]));
1941 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1944 /* 2 body pair potential/force */
1946 link_cell_neighbour_index(moldyn,
1947 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1948 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1949 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1954 /* first loop over atoms j */
1955 if(moldyn->func2b) {
1962 while(neighbour_i[j][p]!=0) {
1964 jtom=&(atom[neighbour_i[j][p]]);
1967 if(jtom==&(itom[i]))
1970 if((jtom->attr&ATOM_ATTR_2BP)&
1971 (itom[i].attr&ATOM_ATTR_2BP)) {
1972 moldyn->func2b(moldyn,
1979 this=&(neighbour_i[j]);
1982 if(this->start==NULL)
1986 jtom=this->current->data;
1988 if(jtom==&(itom[i]))
1991 if((jtom->attr&ATOM_ATTR_2BP)&
1992 (itom[i].attr&ATOM_ATTR_2BP)) {
1993 moldyn->func2b(moldyn,
1998 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2004 /* 3 body potential/force */
2006 if(!(itom[i].attr&ATOM_ATTR_3BP))
2009 /* copy the neighbour lists */
2011 /* no copy needed for static lists */
2013 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
2016 /* second loop over atoms j */
2023 while(neighbour_i[j][p]!=0) {
2025 jtom=&(atom[neighbour_i[j][p]]);
2028 this=&(neighbour_i[j]);
2031 if(this->start==NULL)
2036 jtom=this->current->data;
2039 if(jtom==&(itom[i]))
2042 if(!(jtom->attr&ATOM_ATTR_3BP))
2048 if(moldyn->func3b_j1)
2049 moldyn->func3b_j1(moldyn,
2054 /* in first j loop, 3bp run can be skipped */
2055 if(!(moldyn->run3bp))
2058 /* first loop over atoms k */
2059 if(moldyn->func3b_k1) {
2067 while(neighbour_i[j][q]!=0) {
2069 ktom=&(atom[neighbour_i[k][q]]);
2072 that=&(neighbour_i2[k]);
2075 if(that->start==NULL)
2079 ktom=that->current->data;
2082 if(!(ktom->attr&ATOM_ATTR_3BP))
2088 if(ktom==&(itom[i]))
2091 moldyn->func3b_k1(moldyn,
2099 } while(list_next_f(that)!=\
2107 if(moldyn->func3b_j2)
2108 moldyn->func3b_j2(moldyn,
2113 /* second loop over atoms k */
2114 if(moldyn->func3b_k2) {
2122 while(neighbour_i[j][q]!=0) {
2124 ktom=&(atom[neighbour_i[k][q]]);
2127 that=&(neighbour_i2[k]);
2130 if(that->start==NULL)
2134 ktom=that->current->data;
2137 if(!(ktom->attr&ATOM_ATTR_3BP))
2143 if(ktom==&(itom[i]))
2146 moldyn->func3b_k2(moldyn,
2155 } while(list_next_f(that)!=\
2163 /* 2bp post function */
2164 if(moldyn->func3b_j3) {
2165 moldyn->func3b_j3(moldyn,
2172 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2187 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2188 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2190 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
2191 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
2192 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
2196 /* some postprocessing */
2197 for(i=0;i<count;i++) {
2198 /* calculate global virial */
2199 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
2200 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
2201 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
2202 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
2203 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
2204 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
2206 /* check forces regarding the given timestep */
2207 if(v3_norm(&(itom[i].f))>\
2208 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
2209 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",
2217 * virial calculation
2220 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2221 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2223 a->virial.xx+=f->x*d->x;
2224 a->virial.yy+=f->y*d->y;
2225 a->virial.zz+=f->z*d->z;
2226 a->virial.xy+=f->x*d->y;
2227 a->virial.xz+=f->x*d->z;
2228 a->virial.yz+=f->y*d->z;
2234 * periodic boundary checking
2237 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2238 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2249 if(moldyn->status&MOLDYN_STAT_PBX) {
2250 if(a->x>=x) a->x-=dim->x;
2251 else if(-a->x>x) a->x+=dim->x;
2253 if(moldyn->status&MOLDYN_STAT_PBY) {
2254 if(a->y>=y) a->y-=dim->y;
2255 else if(-a->y>y) a->y+=dim->y;
2257 if(moldyn->status&MOLDYN_STAT_PBZ) {
2258 if(a->z>=z) a->z-=dim->z;
2259 else if(-a->z>z) a->z+=dim->z;
2266 * debugging / critical check functions
2269 int moldyn_bc_check(t_moldyn *moldyn) {
2282 for(i=0;i<moldyn->count;i++) {
2283 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2284 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2285 i,atom[i].r.x,dim->x/2);
2286 printf("diagnostic:\n");
2287 printf("-----------\natom.r.x:\n");
2289 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2292 ((byte)&(1<<k))?1:0,
2295 printf("---------------\nx=dim.x/2:\n");
2297 memcpy(&byte,(u8 *)(&x)+j,1);
2300 ((byte)&(1<<k))?1:0,
2303 if(atom[i].r.x==x) printf("the same!\n");
2304 else printf("different!\n");
2306 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2307 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2308 i,atom[i].r.y,dim->y/2);
2309 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2310 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2311 i,atom[i].r.z,dim->z/2);
2321 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2328 fd=open(file,O_RDONLY);
2330 perror("[moldyn] load save file open");
2334 fsize=lseek(fd,0,SEEK_END);
2335 lseek(fd,0,SEEK_SET);
2337 size=sizeof(t_moldyn);
2340 cnt=read(fd,moldyn,size);
2342 perror("[moldyn] load save file read (moldyn)");
2348 size=moldyn->count*sizeof(t_atom);
2350 /* correcting possible atom data offset */
2352 if(fsize!=sizeof(t_moldyn)+size) {
2353 corr=fsize-sizeof(t_moldyn)-size;
2354 printf("[moldyn] WARNING: lsf (illegal file size)\n");
2355 printf(" moifying offset:\n");
2356 printf(" - current pos: %d\n",sizeof(t_moldyn));
2357 printf(" - atom size: %d\n",size);
2358 printf(" - file size: %d\n",fsize);
2359 printf(" => correction: %d\n",corr);
2360 lseek(fd,corr,SEEK_CUR);
2363 moldyn->atom=(t_atom *)malloc(size);
2364 if(moldyn->atom==NULL) {
2365 perror("[moldyn] load save file malloc (atoms)");
2370 cnt=read(fd,moldyn->atom,size);
2372 perror("[moldyn] load save file read (atoms)");
2383 int moldyn_free_save_file(t_moldyn *moldyn) {
2390 int moldyn_load(t_moldyn *moldyn) {
2398 * function to find/callback all combinations of 2 body bonds
2401 int process_2b_bonds(t_moldyn *moldyn,void *data,
2402 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2403 void *data,u8 bc)) {
2410 t_list neighbour[27];
2419 /* only init link cell if it doesn't exist! */
2421 link_cell_init(moldyn,VERBOSE);
2425 for(i=0;i<moldyn->count;i++) {
2426 /* neighbour indexing */
2427 link_cell_neighbour_index(moldyn,
2428 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2429 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2430 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2435 bc=(j<lc->dnlc)?0:1;
2440 while(neighbour[j][p]!=0) {
2442 jtom=&(moldyn->atom[neighbour[j][p]]);
2445 this=&(neighbour[j]);
2448 if(this->start==NULL)
2453 jtom=this->current->data;
2457 process(moldyn,&(itom[i]),jtom,data,bc);
2462 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2472 * post processing functions
2475 int get_line(int fd,char *line,int max) {
2482 if(count==max) return count;
2483 ret=read(fd,line+count,1);
2484 if(ret<=0) return ret;
2485 if(line[count]=='\n') {
2486 memset(line+count,0,max-count-1);
2494 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2500 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
2516 for(i=0;i<moldyn->count;i++) {
2518 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
2519 check_per_bound(moldyn,&dist);
2520 d2=v3_absolute_square(&dist);
2534 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
2535 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
2536 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
2541 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
2546 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
2547 t_atom *jtom,void *data,u8 bc) {
2554 /* only count pairs once,
2555 * skip same atoms */
2556 if(itom->tag>=jtom->tag)
2560 * pair correlation calc
2567 v3_sub(&dist,&(jtom->r),&(itom->r));
2568 if(bc) check_per_bound(moldyn,&dist);
2569 d=v3_absolute_square(&dist);
2571 /* ignore if greater cutoff */
2572 if(d>moldyn->cutoff_square)
2575 /* fill the slots */
2579 /* should never happen but it does 8) -
2580 * related to -ffloat-store problem! */
2582 printf("[moldyn] WARNING: pcc (%d/%d)",
2588 if(itom->brand!=jtom->brand) {
2593 /* type a - type a bonds */
2595 pcc->stat[s+pcc->o1]+=1;
2597 /* type b - type b bonds */
2598 pcc->stat[s+pcc->o2]+=1;
2604 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2611 pcc.o1=moldyn->cutoff/dr;
2614 if(pcc.o1*dr<=moldyn->cutoff)
2615 printf("[moldyn] WARNING: pcc (low #slots)\n");
2617 printf("[moldyn] pair correlation calc info:\n");
2618 printf(" time: %f\n",moldyn->time);
2619 printf(" count: %d\n",moldyn->count);
2620 printf(" cutoff: %f\n",moldyn->cutoff);
2621 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2624 pcc.stat=(double *)ptr;
2627 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
2628 if(pcc.stat==NULL) {
2629 perror("[moldyn] pair correlation malloc");
2634 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
2637 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
2640 for(i=1;i<pcc.o1;i++) {
2641 // normalization: 4 pi r^2 dr
2642 // here: not double counting pairs -> 2 pi r r dr
2643 // ... and actually it's a constant times r^2
2646 pcc.stat[pcc.o1+i]/=norm;
2647 pcc.stat[pcc.o2+i]/=norm;
2652 /* todo: store/print pair correlation function */
2659 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2666 if(itom->tag>=jtom->tag)
2670 v3_sub(&dist,&(jtom->r),&(itom->r));
2671 if(bc) check_per_bound(moldyn,&dist);
2672 d=v3_absolute_square(&dist);
2674 /* ignore if greater or equal cutoff */
2675 if(d>moldyn->cutoff_square)
2678 /* check for potential bond */
2679 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2684 /* now count this bonding ... */
2687 /* increase total bond counter
2688 * ... double counting!
2693 ba->acnt[jtom->tag]+=1;
2695 ba->bcnt[jtom->tag]+=1;
2698 ba->acnt[itom->tag]+=1;
2700 ba->bcnt[itom->tag]+=1;
2705 int bond_analyze(t_moldyn *moldyn,double *quality) {
2707 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
2715 ba.acnt=malloc(moldyn->count*sizeof(int));
2717 perror("[moldyn] bond analyze malloc (a)");
2720 memset(ba.acnt,0,moldyn->count*sizeof(int));
2722 ba.bcnt=malloc(moldyn->count*sizeof(int));
2724 perror("[moldyn] bond analyze malloc (b)");
2727 memset(ba.bcnt,0,moldyn->count*sizeof(int));
2736 process_2b_bonds(moldyn,&ba,bond_analyze_process);
2738 for(i=0;i<moldyn->count;i++) {
2739 if(atom[i].brand==0) {
2740 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
2744 if((ba.acnt[i]==4)&(ba.bcnt[i]==0)) {
2752 printf("[moldyn] bond analyze: c_cnt=%d | set=%d\n",ccnt,cset);
2753 printf("[moldyn] bond analyze: q_cnt=%d | tot=%d\n",qcnt,ba.tcnt);
2756 quality[0]=1.0*ccnt/cset;
2757 quality[1]=1.0*qcnt/ba.tcnt;
2760 printf("[moldyn] bond analyze: c_bnd_q=%f\n",1.0*qcnt/ba.tcnt);
2761 printf("[moldyn] bond analyze: tot_q=%f\n",1.0*qcnt/ba.tcnt);
2768 * visualization code
2771 int visual_init(t_moldyn *moldyn,char *filebase) {
2773 strncpy(moldyn->vis.fb,filebase,128);
2778 int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2785 if(itom->tag>=jtom->tag)
2788 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2791 if((itom->attr&ATOM_ATTR_VB)|(jtom->attr&ATOM_ATTR_VB))
2792 dprintf(vb->fd,"# [B] %f %f %f %f %f %f\n",
2793 itom->r.x,itom->r.y,itom->r.z,
2794 jtom->r.x,jtom->r.y,jtom->r.z);
2799 int visual_atoms(t_moldyn *moldyn) {
2817 sprintf(file,"%s/atomic_conf_%07.f.xyz",v->fb,moldyn->time);
2818 vb.fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
2820 perror("open visual save file fd");
2824 /* write the actual data file */
2827 dprintf(vb.fd,"# [P] %d %07.f <%f,%f,%f>\n",
2828 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
2830 // atomic configuration
2831 for(i=0;i<moldyn->count;i++)
2832 // atom type, positions, color and kinetic energy
2833 dprintf(vb.fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
2837 pse_col[atom[i].element],
2840 // bonds between atoms
2841 process_2b_bonds(moldyn,&vb,visual_bonds_process);
2845 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2846 -dim.x/2,-dim.y/2,-dim.z/2,
2847 dim.x/2,-dim.y/2,-dim.z/2);
2848 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2849 -dim.x/2,-dim.y/2,-dim.z/2,
2850 -dim.x/2,dim.y/2,-dim.z/2);
2851 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2852 dim.x/2,dim.y/2,-dim.z/2,
2853 dim.x/2,-dim.y/2,-dim.z/2);
2854 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2855 -dim.x/2,dim.y/2,-dim.z/2,
2856 dim.x/2,dim.y/2,-dim.z/2);
2858 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2859 -dim.x/2,-dim.y/2,dim.z/2,
2860 dim.x/2,-dim.y/2,dim.z/2);
2861 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2862 -dim.x/2,-dim.y/2,dim.z/2,
2863 -dim.x/2,dim.y/2,dim.z/2);
2864 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2865 dim.x/2,dim.y/2,dim.z/2,
2866 dim.x/2,-dim.y/2,dim.z/2);
2867 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2868 -dim.x/2,dim.y/2,dim.z/2,
2869 dim.x/2,dim.y/2,dim.z/2);
2871 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2872 -dim.x/2,-dim.y/2,dim.z/2,
2873 -dim.x/2,-dim.y/2,-dim.z/2);
2874 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2875 -dim.x/2,dim.y/2,dim.z/2,
2876 -dim.x/2,dim.y/2,-dim.z/2);
2877 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2878 dim.x/2,-dim.y/2,dim.z/2,
2879 dim.x/2,-dim.y/2,-dim.z/2);
2880 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2881 dim.x/2,dim.y/2,dim.z/2,
2882 dim.x/2,dim.y/2,-dim.z/2);